diff --git a/ports/esp32s2/common-hal/wifi/Radio.c b/ports/esp32s2/common-hal/wifi/Radio.c index 0184793a1a..019e8316c1 100644 --- a/ports/esp32s2/common-hal/wifi/Radio.c +++ b/ports/esp32s2/common-hal/wifi/Radio.c @@ -38,6 +38,8 @@ #include "esp-idf/components/esp_wifi/include/esp_wifi.h" #include "esp-idf/components/lwip/include/apps/ping/ping_sock.h" +#define MAC_ADDRESS_LENGTH 6 + static void start_station(wifi_radio_obj_t *self) { if (self->sta_mode) { return; @@ -73,8 +75,6 @@ void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled) { } } -#define MAC_ADDRESS_LENGTH 6 - mp_obj_t common_hal_wifi_radio_get_mac_address(wifi_radio_obj_t *self) { uint8_t mac[MAC_ADDRESS_LENGTH]; esp_wifi_get_mac(ESP_IF_WIFI_STA, mac); @@ -168,6 +168,41 @@ mp_obj_t common_hal_wifi_radio_get_ap_rssi(wifi_radio_obj_t *self) { } } +mp_obj_t common_hal_wifi_radio_get_ap_ssid(wifi_radio_obj_t *self) { + if (!esp_netif_is_netif_up(self->netif)) { + return mp_const_none; + } + + // Make sure the interface is in STA mode + if (self->sta_mode){ + return mp_const_none; + } + + if (esp_wifi_sta_get_ap_info(&self->ap_info) != ESP_OK){ + return mp_const_none; + } else { + const char* cstr = (const char*) self->ap_info.ssid; + return mp_obj_new_str(cstr, strlen(cstr)); + } +} + +mp_obj_t common_hal_wifi_radio_get_ap_bssid(wifi_radio_obj_t *self) { + if (!esp_netif_is_netif_up(self->netif)) { + return mp_const_none; + } + + // Make sure the interface is in STA mode + if (self->sta_mode){ + return mp_const_none; + } + + if (esp_wifi_sta_get_ap_info(&self->ap_info) != ESP_OK){ + return mp_const_none; + } else { + return mp_obj_new_bytes(self->ap_info.bssid, MAC_ADDRESS_LENGTH); + } +} + mp_obj_t common_hal_wifi_radio_get_ipv4_gateway(wifi_radio_obj_t *self) { if (!esp_netif_is_netif_up(self->netif)) { return mp_const_none; diff --git a/shared-bindings/wifi/Radio.c b/shared-bindings/wifi/Radio.c index 8c556aafe6..96abadf6ff 100644 --- a/shared-bindings/wifi/Radio.c +++ b/shared-bindings/wifi/Radio.c @@ -179,6 +179,38 @@ const mp_obj_property_t wifi_radio_ap_rssi_obj = { (mp_obj_t)&mp_const_none_obj }, }; +//| ap_ssid: int +//| """SSID of the currently connected AP. Returns none if not connected""" +//| +STATIC mp_obj_t wifi_radio_get_ap_ssid(mp_obj_t self) { + return common_hal_wifi_radio_get_ap_ssid(self); + +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ap_ssid_obj, wifi_radio_get_ap_ssid); + +const mp_obj_property_t wifi_radio_ap_ssid_obj = { + .base.type = &mp_type_property, + .proxy = { (mp_obj_t)&wifi_radio_get_ap_ssid_obj, + (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&mp_const_none_obj }, +}; + +//| ap_bssid: int +//| """BSSID (usually MAC) of the currently connected AP. Returns none if not connected""" +//| +STATIC mp_obj_t wifi_radio_get_ap_bssid(mp_obj_t self) { + return common_hal_wifi_radio_get_ap_bssid(self); + +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ap_bssid_obj, wifi_radio_get_ap_bssid); + +const mp_obj_property_t wifi_radio_ap_bssid_obj = { + .base.type = &mp_type_property, + .proxy = { (mp_obj_t)&wifi_radio_get_ap_bssid_obj, + (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&mp_const_none_obj }, +}; + //| ipv4_gateway: Optional[ipaddress.IPv4Address] //| """IP v4 Address of the gateway when connected to an access point. None otherwise.""" //| @@ -284,6 +316,8 @@ STATIC const mp_rom_map_elem_t wifi_radio_locals_dict_table[] = { // { MP_ROM_QSTR(MP_QSTR_connect_to_enterprise), MP_ROM_PTR(&wifi_radio_connect_to_enterprise_obj) }, { MP_ROM_QSTR(MP_QSTR_ap_rssi), MP_ROM_PTR(&wifi_radio_ap_rssi_obj) }, + { MP_ROM_QSTR(MP_QSTR_ap_ssid), MP_ROM_PTR(&wifi_radio_ap_ssid_obj) }, + { MP_ROM_QSTR(MP_QSTR_ap_bssid), MP_ROM_PTR(&wifi_radio_ap_bssid_obj) }, { MP_ROM_QSTR(MP_QSTR_ipv4_dns), MP_ROM_PTR(&wifi_radio_ipv4_dns_obj) }, { MP_ROM_QSTR(MP_QSTR_ipv4_gateway), MP_ROM_PTR(&wifi_radio_ipv4_gateway_obj) }, { MP_ROM_QSTR(MP_QSTR_ipv4_subnet), MP_ROM_PTR(&wifi_radio_ipv4_subnet_obj) }, diff --git a/shared-bindings/wifi/Radio.h b/shared-bindings/wifi/Radio.h index fd0807a86e..bf30ac3405 100644 --- a/shared-bindings/wifi/Radio.h +++ b/shared-bindings/wifi/Radio.h @@ -54,6 +54,8 @@ 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, uint8_t* bssid, size_t bssid_len); extern mp_obj_t common_hal_wifi_radio_get_ap_rssi(wifi_radio_obj_t *self); +extern mp_obj_t common_hal_wifi_radio_get_ap_ssid(wifi_radio_obj_t *self); +extern mp_obj_t common_hal_wifi_radio_get_ap_bssid(wifi_radio_obj_t *self); extern mp_obj_t common_hal_wifi_radio_get_ipv4_dns(wifi_radio_obj_t *self); extern mp_obj_t common_hal_wifi_radio_get_ipv4_gateway(wifi_radio_obj_t *self); extern mp_obj_t common_hal_wifi_radio_get_ipv4_subnet(wifi_radio_obj_t *self);