Skip to content

Commit bd22667

Browse files
authored
Merge pull request #5571 from anecdata/set_mac
Set Station MAC address & validate connect SSID len
2 parents 41d4e57 + 7a89f62 commit bd22667

File tree

5 files changed

+56
-8
lines changed

5 files changed

+56
-8
lines changed

locale/circuitpython.pot

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,10 @@ msgstr ""
12801280
msgid "Invalid DAC pin supplied"
12811281
msgstr ""
12821282

1283+
#: shared-bindings/wifi/Radio.c
1284+
msgid "Invalid MAC address"
1285+
msgstr ""
1286+
12831287
#: shared-bindings/synthio/__init__.c
12841288
msgid "Invalid MIDI file"
12851289
msgstr ""
@@ -1353,6 +1357,10 @@ msgstr ""
13531357
msgid "Invalid memory access."
13541358
msgstr ""
13551359

1360+
#: ports/espressif/common-hal/wifi/Radio.c
1361+
msgid "Invalid multicast MAC address"
1362+
msgstr ""
1363+
13561364
#: shared-bindings/bitbangio/SPI.c shared-bindings/busio/SPI.c
13571365
msgid "Invalid number of bits"
13581366
msgstr ""
@@ -2134,6 +2142,10 @@ msgstr ""
21342142
msgid "Stack size must be at least 256"
21352143
msgstr ""
21362144

2145+
#: ports/espressif/common-hal/wifi/Radio.c
2146+
msgid "Station must be started"
2147+
msgstr ""
2148+
21372149
#: ports/raspberrypi/common-hal/audiopwmio/PWMAudioOut.c
21382150
msgid "Stereo left must be on PWM channel A"
21392151
msgstr ""
@@ -3924,7 +3936,7 @@ msgid "pow() with 3 arguments requires integers"
39243936
msgstr ""
39253937

39263938
#: ports/espressif/boards/adafruit_feather_esp32s2/mpconfigboard.h
3927-
#: ports/espressif/boards/adafruit_feather_esp32s2_nopsram/mpconfigboard.h
3939+
#: ports/espressif/boards/adafruit_feather_esp32s2_tft/mpconfigboard.h
39283940
#: ports/espressif/boards/adafruit_feather_esp32s2_tftback_nopsram/mpconfigboard.h
39293941
#: ports/espressif/boards/adafruit_funhouse/mpconfigboard.h
39303942
#: ports/espressif/boards/adafruit_magtag_2.9_grayscale/mpconfigboard.h
@@ -4142,6 +4154,10 @@ msgstr ""
41424154
msgid "source_bitmap must have value_count of 8"
41434155
msgstr ""
41444156

4157+
#: shared-bindings/wifi/Radio.c
4158+
msgid "ssid can't be more than 32 bytes"
4159+
msgstr ""
4160+
41454161
#: py/objstr.c
41464162
msgid "start/end indices"
41474163
msgstr ""

ports/espressif/common-hal/wifi/Radio.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ mp_obj_t common_hal_wifi_radio_get_mac_address(wifi_radio_obj_t *self) {
120120
return mp_obj_new_bytes(mac, MAC_ADDRESS_LENGTH);
121121
}
122122

123+
void common_hal_wifi_radio_set_mac_address(wifi_radio_obj_t *self, const uint8_t *mac) {
124+
if (!self->sta_mode) {
125+
mp_raise_RuntimeError(translate("Station must be started"));
126+
}
127+
if ((mac[0] & 0b1) == 0b1) {
128+
mp_raise_RuntimeError(translate("Invalid multicast MAC address"));
129+
}
130+
esp_wifi_set_mac(ESP_IF_WIFI_STA, mac);
131+
}
132+
123133
mp_obj_t common_hal_wifi_radio_get_mac_address_ap(wifi_radio_obj_t *self) {
124134
uint8_t mac[MAC_ADDRESS_LENGTH];
125135
esp_wifi_get_mac(ESP_IF_WIFI_AP, mac);

ports/espressif/common-hal/wifi/__init__.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ void common_hal_wifi_init(void) {
152152
mp_raise_RuntimeError(translate("Failed to init wifi"));
153153
}
154154
// set station mode to avoid the default SoftAP
155-
esp_wifi_set_mode(WIFI_MODE_STA);
155+
common_hal_wifi_radio_start_station(self);
156156
// start wifi
157157
common_hal_wifi_radio_set_enabled(self, true);
158158
}

shared-bindings/wifi/Radio.c

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include "py/runtime.h"
3434
#include "py/objproperty.h"
3535

36+
#define MAC_ADDRESS_LENGTH 6
37+
3638
//| class Radio:
3739
//| """Native wifi radio.
3840
//|
@@ -115,23 +117,38 @@ const mp_obj_property_t wifi_radio_hostname_obj = {
115117
MP_ROM_NONE},
116118
};
117119

118-
//| mac_address: bytes
119-
//| """MAC address of the wifi radio station. (read-only)"""
120+
//| mac_address: ReadableBuffer
121+
//| """MAC address for the station. When the address is altered after interface is connected
122+
//| the changes would only be reflected once the interface reconnects."""
120123
//|
121-
STATIC mp_obj_t wifi_radio_get_mac_address(mp_obj_t self) {
124+
STATIC mp_obj_t wifi_radio_get_mac_address(mp_obj_t self_in) {
125+
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
122126
return MP_OBJ_FROM_PTR(common_hal_wifi_radio_get_mac_address(self));
123-
124127
}
125128
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_mac_address_obj, wifi_radio_get_mac_address);
126129

130+
STATIC mp_obj_t wifi_radio_set_mac_address(mp_obj_t self_in, mp_obj_t mac_address_in) {
131+
mp_buffer_info_t mac_address;
132+
mp_get_buffer_raise(mac_address_in, &mac_address, MP_BUFFER_READ);
133+
134+
if (mac_address.len != MAC_ADDRESS_LENGTH) {
135+
mp_raise_ValueError(translate("Invalid MAC address"));
136+
}
137+
138+
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
139+
common_hal_wifi_radio_set_mac_address(self, mac_address.buf);
140+
141+
return mp_const_none;
142+
}
143+
MP_DEFINE_CONST_FUN_OBJ_2(wifi_radio_set_mac_address_obj, wifi_radio_set_mac_address);
144+
127145
const mp_obj_property_t wifi_radio_mac_address_obj = {
128146
.base.type = &mp_type_property,
129147
.proxy = { (mp_obj_t)&wifi_radio_get_mac_address_obj,
130-
MP_ROM_NONE,
148+
(mp_obj_t)&wifi_radio_set_mac_address_obj,
131149
MP_ROM_NONE },
132150
};
133151

134-
135152
//| mac_address_ap: bytes
136153
//| """MAC address of the wifi radio access point. (read-only)"""
137154
//|
@@ -307,7 +324,11 @@ STATIC mp_obj_t wifi_radio_connect(size_t n_args, const mp_obj_t *pos_args, mp_m
307324
}
308325

309326
mp_buffer_info_t ssid;
327+
ssid.len = 0;
310328
mp_get_buffer_raise(args[ARG_ssid].u_obj, &ssid, MP_BUFFER_READ);
329+
if (ssid.len > 32) {
330+
mp_raise_ValueError(translate("ssid can't be more than 32 bytes"));
331+
}
311332

312333
mp_buffer_info_t password;
313334
password.len = 0;

shared-bindings/wifi/Radio.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ extern mp_obj_t common_hal_wifi_radio_get_hostname(wifi_radio_obj_t *self);
7878
extern void common_hal_wifi_radio_set_hostname(wifi_radio_obj_t *self, const char *hostname);
7979

8080
extern mp_obj_t common_hal_wifi_radio_get_mac_address(wifi_radio_obj_t *self);
81+
extern void common_hal_wifi_radio_set_mac_address(wifi_radio_obj_t *self, const uint8_t *mac);
8182
extern mp_obj_t common_hal_wifi_radio_get_mac_address_ap(wifi_radio_obj_t *self);
8283

8384
extern mp_obj_t common_hal_wifi_radio_start_scanning_networks(wifi_radio_obj_t *self);

0 commit comments

Comments
 (0)