Skip to content

Commit 18f2019

Browse files
committed
Set Station MAC address & validate connect SSID len
1 parent 27d0f1d commit 18f2019

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

locale/circuitpython.pot

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,10 @@ msgstr ""
682682
msgid "Can't set CCCD on local Characteristic"
683683
msgstr ""
684684

685+
#: ports/espressif/common-hal/wifi/Radio.c
686+
msgid "Can't change MAC address while station is started"
687+
msgstr ""
688+
685689
#: shared-bindings/storage/__init__.c shared-bindings/usb_cdc/__init__.c
686690
#: shared-bindings/usb_hid/__init__.c shared-bindings/usb_midi/__init__.c
687691
msgid "Cannot change USB devices now"
@@ -1349,6 +1353,10 @@ msgstr ""
13491353
msgid "Invalid format chunk size"
13501354
msgstr ""
13511355

1356+
#: shared-bindings/wifi/Radio.c
1357+
msgid ""Invalid MAC address""
1358+
msgstr ""
1359+
13521360
#: supervisor/shared/safe_mode.c
13531361
msgid "Invalid memory access."
13541362
msgstr ""
@@ -1461,6 +1469,10 @@ msgstr ""
14611469
msgid "Layer must be a Group or TileGrid subclass."
14621470
msgstr ""
14631471

1472+
#: ports/espressif/common-hal/wifi/Radio.c
1473+
msgid "MAC address can't be a multicast address"
1474+
msgstr ""
1475+
14641476
#: ports/espressif/bindings/espidf/__init__.c ports/espressif/esp_error.c
14651477
msgid "MAC address was invalid"
14661478
msgstr ""
@@ -4445,6 +4457,10 @@ msgstr ""
44454457
msgid "wifi is not enabled"
44464458
msgstr ""
44474459

4460+
#: shared-bindings/wifi/Radio.c
4461+
msgid "wifi ssid must be between 1 and 32 characters"
4462+
msgstr ""
4463+
44484464
#: shared-bindings/_bleio/Adapter.c
44494465
msgid "window must be <= interval"
44504466
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("Can't change MAC address while station is started"));
126+
}
127+
if ((mac[0] & 0b1) == 0b1) {
128+
mp_raise_RuntimeError(translate("MAC address can't be a multicast 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);

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 of the wifi radio station.
122+
//| Can only be set while the Station is not started."""
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("wifi ssid must be between 1 and 32 characters"));
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)