Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set Station MAC address & validate connect SSID len #5571

Merged
merged 7 commits into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,10 @@ msgstr ""
msgid "Invalid DAC pin supplied"
msgstr ""

#: shared-bindings/wifi/Radio.c
msgid "Invalid MAC address"
msgstr ""

#: shared-bindings/synthio/__init__.c
msgid "Invalid MIDI file"
msgstr ""
Expand Down Expand Up @@ -1353,6 +1357,10 @@ msgstr ""
msgid "Invalid memory access."
msgstr ""

#: ports/espressif/common-hal/wifi/Radio.c
msgid "Invalid multicast MAC address"
msgstr ""

#: shared-bindings/bitbangio/SPI.c shared-bindings/busio/SPI.c
msgid "Invalid number of bits"
msgstr ""
Expand Down Expand Up @@ -2134,6 +2142,10 @@ msgstr ""
msgid "Stack size must be at least 256"
msgstr ""

#: ports/espressif/common-hal/wifi/Radio.c
msgid "Station must be started"
msgstr ""

#: ports/raspberrypi/common-hal/audiopwmio/PWMAudioOut.c
msgid "Stereo left must be on PWM channel A"
msgstr ""
Expand Down Expand Up @@ -3924,7 +3936,7 @@ msgid "pow() with 3 arguments requires integers"
msgstr ""

#: ports/espressif/boards/adafruit_feather_esp32s2/mpconfigboard.h
#: ports/espressif/boards/adafruit_feather_esp32s2_nopsram/mpconfigboard.h
#: ports/espressif/boards/adafruit_feather_esp32s2_tft/mpconfigboard.h
#: ports/espressif/boards/adafruit_feather_esp32s2_tftback_nopsram/mpconfigboard.h
#: ports/espressif/boards/adafruit_funhouse/mpconfigboard.h
#: ports/espressif/boards/adafruit_magtag_2.9_grayscale/mpconfigboard.h
Expand Down Expand Up @@ -4142,6 +4154,10 @@ msgstr ""
msgid "source_bitmap must have value_count of 8"
msgstr ""

#: shared-bindings/wifi/Radio.c
msgid "ssid can't be more than 32 bytes"
msgstr ""

#: py/objstr.c
msgid "start/end indices"
msgstr ""
Expand Down
10 changes: 10 additions & 0 deletions ports/espressif/common-hal/wifi/Radio.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ mp_obj_t common_hal_wifi_radio_get_mac_address(wifi_radio_obj_t *self) {
return mp_obj_new_bytes(mac, MAC_ADDRESS_LENGTH);
}

void common_hal_wifi_radio_set_mac_address(wifi_radio_obj_t *self, const uint8_t *mac) {
if (!self->sta_mode) {
mp_raise_RuntimeError(translate("Station must be started"));
}
if ((mac[0] & 0b1) == 0b1) {
mp_raise_RuntimeError(translate("Invalid multicast MAC address"));
}
esp_wifi_set_mac(ESP_IF_WIFI_STA, mac);
}

mp_obj_t common_hal_wifi_radio_get_mac_address_ap(wifi_radio_obj_t *self) {
uint8_t mac[MAC_ADDRESS_LENGTH];
esp_wifi_get_mac(ESP_IF_WIFI_AP, mac);
Expand Down
2 changes: 1 addition & 1 deletion ports/espressif/common-hal/wifi/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void common_hal_wifi_init(void) {
mp_raise_RuntimeError(translate("Failed to init wifi"));
}
// set station mode to avoid the default SoftAP
esp_wifi_set_mode(WIFI_MODE_STA);
common_hal_wifi_radio_start_station(self);
// start wifi
common_hal_wifi_radio_set_enabled(self, true);
}
Expand Down
33 changes: 27 additions & 6 deletions shared-bindings/wifi/Radio.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include "py/runtime.h"
#include "py/objproperty.h"

#define MAC_ADDRESS_LENGTH 6

//| class Radio:
//| """Native wifi radio.
//|
Expand Down Expand Up @@ -115,23 +117,38 @@ const mp_obj_property_t wifi_radio_hostname_obj = {
MP_ROM_NONE},
};

//| mac_address: bytes
//| """MAC address of the wifi radio station. (read-only)"""
//| mac_address: ReadableBuffer
//| """MAC address for the station. When the address is altered after interface is connected
//| the changes would only be reflected once the interface reconnects."""
//|
STATIC mp_obj_t wifi_radio_get_mac_address(mp_obj_t self) {
STATIC mp_obj_t wifi_radio_get_mac_address(mp_obj_t self_in) {
wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_FROM_PTR(common_hal_wifi_radio_get_mac_address(self));

}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_mac_address_obj, wifi_radio_get_mac_address);

STATIC mp_obj_t wifi_radio_set_mac_address(mp_obj_t self_in, mp_obj_t mac_address_in) {
mp_buffer_info_t mac_address;
mp_get_buffer_raise(mac_address_in, &mac_address, MP_BUFFER_READ);

if (mac_address.len != MAC_ADDRESS_LENGTH) {
mp_raise_ValueError(translate("Invalid MAC address"));
}

wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_wifi_radio_set_mac_address(self, mac_address.buf);

return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(wifi_radio_set_mac_address_obj, wifi_radio_set_mac_address);

const mp_obj_property_t wifi_radio_mac_address_obj = {
.base.type = &mp_type_property,
.proxy = { (mp_obj_t)&wifi_radio_get_mac_address_obj,
MP_ROM_NONE,
(mp_obj_t)&wifi_radio_set_mac_address_obj,
MP_ROM_NONE },
};


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

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

mp_buffer_info_t password;
password.len = 0;
Expand Down
1 change: 1 addition & 0 deletions shared-bindings/wifi/Radio.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ extern mp_obj_t common_hal_wifi_radio_get_hostname(wifi_radio_obj_t *self);
extern void common_hal_wifi_radio_set_hostname(wifi_radio_obj_t *self, const char *hostname);

extern mp_obj_t common_hal_wifi_radio_get_mac_address(wifi_radio_obj_t *self);
extern void common_hal_wifi_radio_set_mac_address(wifi_radio_obj_t *self, const uint8_t *mac);
extern mp_obj_t common_hal_wifi_radio_get_mac_address_ap(wifi_radio_obj_t *self);

extern mp_obj_t common_hal_wifi_radio_start_scanning_networks(wifi_radio_obj_t *self);
Expand Down