Skip to content

Commit

Permalink
Merge pull request #3433 from astrobokonon/esp32s2-bssid
Browse files Browse the repository at this point in the history
ESP32S2: Allow connecting to specific bssid
  • Loading branch information
dhalbert authored Sep 19, 2020
2 parents 8cf0171 + 1805e92 commit dfd09bb
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 6 deletions.
6 changes: 5 additions & 1 deletion locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-09-11 13:30+0200\n"
"POT-Creation-Date: 2020-09-16 17:07-0700\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down Expand Up @@ -960,6 +960,10 @@ msgstr ""
msgid "Invalid BMP file"
msgstr ""

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

#: ports/stm/common-hal/analogio/AnalogOut.c
msgid "Invalid DAC pin supplied"
msgstr ""
Expand Down
6 changes: 6 additions & 0 deletions ports/esp32s2/common-hal/wifi/Network.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ mp_obj_t common_hal_wifi_network_get_ssid(wifi_network_obj_t *self) {
return mp_obj_new_str(cstr, strlen(cstr));
}

#define MAC_ADDRESS_LENGTH 6

mp_obj_t common_hal_wifi_network_get_bssid(wifi_network_obj_t *self) {
return mp_obj_new_bytes(self->record.bssid, MAC_ADDRESS_LENGTH);
}

mp_obj_t common_hal_wifi_network_get_rssi(wifi_network_obj_t *self) {
return mp_obj_new_int(self->record.rssi);
}
Expand Down
12 changes: 11 additions & 1 deletion ports/esp32s2/common-hal/wifi/Radio.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,24 @@ void common_hal_wifi_radio_stop_scanning_networks(wifi_radio_obj_t *self) {
self->current_scan = NULL;
}

wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t* ssid, size_t ssid_len, uint8_t* password, size_t password_len, uint8_t channel, mp_float_t timeout) {
wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t* ssid, size_t ssid_len, uint8_t* password, size_t password_len, uint8_t channel, mp_float_t timeout, uint8_t* bssid, size_t bssid_len) {
// check enabled
wifi_config_t* config = &self->sta_config;
memcpy(&config->sta.ssid, ssid, ssid_len);
config->sta.ssid[ssid_len] = 0;
memcpy(&config->sta.password, password, password_len);
config->sta.password[password_len] = 0;
config->sta.channel = channel;
// From esp_wifi_types.h:
// Generally, station_config.bssid_set needs to be 0; and it needs
// to be 1 only when users need to check the MAC address of the AP
if (bssid_len > 0){
memcpy(&config->sta.bssid, bssid, bssid_len);
config->sta.bssid[bssid_len] = 0;
config->sta.bssid_set = 1;
} else {
config->sta.bssid_set = 0;
}
esp_wifi_set_config(ESP_IF_WIFI_STA, config);
self->starting_retries = 5;
self->retries_left = 5;
Expand Down
18 changes: 18 additions & 0 deletions shared-bindings/wifi/Network.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ const mp_obj_property_t wifi_network_ssid_obj = {
};


//| bssid: bytes
//| """BSSID of the network (usually the AP's MAC address)"""
//|
STATIC mp_obj_t wifi_network_get_bssid(mp_obj_t self) {
return common_hal_wifi_network_get_bssid(self);

}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_network_get_bssid_obj, wifi_network_get_bssid);

const mp_obj_property_t wifi_network_bssid_obj = {
.base.type = &mp_type_property,
.proxy = { (mp_obj_t)&wifi_network_get_bssid_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj },
};


//| rssi: int
//| """Signal strength of the network"""
//|
Expand Down Expand Up @@ -94,6 +111,7 @@ const mp_obj_property_t wifi_network_channel_obj = {

STATIC const mp_rom_map_elem_t wifi_network_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_ssid), MP_ROM_PTR(&wifi_network_ssid_obj) },
{ MP_ROM_QSTR(MP_QSTR_bssid), MP_ROM_PTR(&wifi_network_bssid_obj) },
{ MP_ROM_QSTR(MP_QSTR_rssi), MP_ROM_PTR(&wifi_network_rssi_obj) },
{ MP_ROM_QSTR(MP_QSTR_channel), MP_ROM_PTR(&wifi_network_channel_obj) },
};
Expand Down
1 change: 1 addition & 0 deletions shared-bindings/wifi/Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
const mp_obj_type_t wifi_network_type;

extern mp_obj_t common_hal_wifi_network_get_ssid(wifi_network_obj_t *self);
extern mp_obj_t common_hal_wifi_network_get_bssid(wifi_network_obj_t *self);
extern mp_obj_t common_hal_wifi_network_get_rssi(wifi_network_obj_t *self);
extern mp_obj_t common_hal_wifi_network_get_channel(wifi_network_obj_t *self);

Expand Down
18 changes: 15 additions & 3 deletions shared-bindings/wifi/Radio.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_scanning_networks_obj, wifi_rad
//| ...
//|
STATIC mp_obj_t wifi_radio_connect(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_ssid, ARG_password, ARG_channel, ARG_timeout };
enum { ARG_ssid, ARG_password, ARG_channel, ARG_bssid, ARG_timeout };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_ssid, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_password, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_channel, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_bssid, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
};

Expand All @@ -125,7 +126,6 @@ STATIC mp_obj_t wifi_radio_connect(size_t n_args, const mp_obj_t *pos_args, mp_m
timeout = mp_obj_get_float(args[ARG_timeout].u_obj);
}


mp_buffer_info_t ssid;
mp_get_buffer_raise(args[ARG_ssid].u_obj, &ssid, MP_BUFFER_READ);

Expand All @@ -138,7 +138,19 @@ STATIC mp_obj_t wifi_radio_connect(size_t n_args, const mp_obj_t *pos_args, mp_m
}
}

wifi_radio_error_t error = common_hal_wifi_radio_connect(self, ssid.buf, ssid.len, password.buf, password.len, args[ARG_channel].u_int, timeout);
#define MAC_ADDRESS_LENGTH 6

mp_buffer_info_t bssid;
bssid.len = 0;
// Should probably make sure bssid is just bytes and not something else too
if (args[ARG_bssid].u_obj != MP_OBJ_NULL) {
mp_get_buffer_raise(args[ARG_bssid].u_obj, &bssid, MP_BUFFER_READ);
if (bssid.len != MAC_ADDRESS_LENGTH) {
mp_raise_ValueError(translate("Invalid BSSID"));
}
}

wifi_radio_error_t error = common_hal_wifi_radio_connect(self, ssid.buf, ssid.len, password.buf, password.len, args[ARG_channel].u_int, timeout, bssid.buf, bssid.len);
if (error == WIFI_RADIO_ERROR_AUTH) {
mp_raise_ConnectionError(translate("Authentication failure"));
} else if (error == WIFI_RADIO_ERROR_NO_AP_FOUND) {
Expand Down
2 changes: 1 addition & 1 deletion shared-bindings/wifi/Radio.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ extern mp_obj_t common_hal_wifi_radio_get_mac_address(wifi_radio_obj_t *self);
extern mp_obj_t common_hal_wifi_radio_start_scanning_networks(wifi_radio_obj_t *self);
extern void common_hal_wifi_radio_stop_scanning_networks(wifi_radio_obj_t *self);

extern wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t* ssid, size_t ssid_len, uint8_t* password, size_t password_len, uint8_t channel, mp_float_t timeout);
extern wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t* ssid, size_t ssid_len, uint8_t* password, size_t password_len, uint8_t channel, mp_float_t timeout, uint8_t* bssid, size_t bssid_len);

extern mp_obj_t common_hal_wifi_radio_get_ipv4_address(wifi_radio_obj_t *self);

Expand Down

0 comments on commit dfd09bb

Please sign in to comment.