Merge pull request #5571 from anecdata/set_mac

Set Station MAC address & validate connect SSID len
This commit is contained in:
Dan Halbert 2021-11-15 20:33:54 -05:00 committed by GitHub
commit bd22667eb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 8 deletions

View File

@ -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 ""
@ -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 ""
@ -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 ""
@ -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
@ -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 ""

View File

@ -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);

View File

@ -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);
}

View File

@ -33,6 +33,8 @@
#include "py/runtime.h"
#include "py/objproperty.h"
#define MAC_ADDRESS_LENGTH 6
//| class Radio:
//| """Native wifi radio.
//|
@ -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)"""
//|
@ -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;

View File

@ -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);