From 66d87782be3793fd179b12fa864e71509f1dbf6f Mon Sep 17 00:00:00 2001 From: BennyE Date: Wed, 6 Jan 2021 23:52:30 +0100 Subject: [PATCH 1/3] Adding authmode keyword --- ports/esp32s2/common-hal/wifi/Network.c | 35 +++++++++++++++++++++++++ shared-bindings/wifi/Network.c | 16 +++++++++++ shared-bindings/wifi/Network.h | 1 + shared-bindings/wifi/Radio.c | 2 +- 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/ports/esp32s2/common-hal/wifi/Network.c b/ports/esp32s2/common-hal/wifi/Network.c index 2674df0651..0289ad4ebb 100644 --- a/ports/esp32s2/common-hal/wifi/Network.c +++ b/ports/esp32s2/common-hal/wifi/Network.c @@ -54,3 +54,38 @@ mp_obj_t common_hal_wifi_network_get_country(wifi_network_obj_t *self) { // 2 instead of strlen(cstr) as this gives us only the country-code return mp_obj_new_str(cstr, 2); } + +mp_obj_t common_hal_wifi_network_get_authmode(wifi_network_obj_t *self) { + char authmode[16]; + switch (self->record.authmode) { + case WIFI_AUTH_OPEN: + strcpy(authmode, "OPEN"); + break; + case WIFI_AUTH_WEP: + strcpy(authmode, "WEP"); + break; + case WIFI_AUTH_WPA_PSK: + strcpy(authmode, "WPA_PSK"); + break; + case WIFI_AUTH_WPA2_PSK: + strcpy(authmode, "WPA2_PSK"); + break; + case WIFI_AUTH_WPA_WPA2_PSK: + strcpy(authmode, "WPA_WPA2_PSK"); + break; + case WIFI_AUTH_WPA2_ENTERPRISE: + strcpy(authmode, "WPA2_ENTERPRISE"); + break; + case WIFI_AUTH_WPA3_PSK: + strcpy(authmode, "WPA3_PSK"); + break; + case WIFI_AUTH_WPA2_WPA3_PSK: + strcpy(authmode, "WPA2_WPA3_PSK"); + break; + default: + strcpy(authmode, "UNKNOWN"); + break; + } + const char* cstr = (const char*) authmode; + return mp_obj_new_str(cstr, strlen(cstr)); +} diff --git a/shared-bindings/wifi/Network.c b/shared-bindings/wifi/Network.c index 009712ad19..0f3006f2ea 100644 --- a/shared-bindings/wifi/Network.c +++ b/shared-bindings/wifi/Network.c @@ -124,6 +124,21 @@ const mp_obj_property_t wifi_network_country_obj = { (mp_obj_t)&mp_const_none_obj }, }; +//| authmode: str +//| """String id of the authmode""" +//| +STATIC mp_obj_t wifi_network_get_authmode(mp_obj_t self) { + return common_hal_wifi_network_get_authmode(self); + +} +MP_DEFINE_CONST_FUN_OBJ_1(wifi_network_get_authmode_obj, wifi_network_get_authmode); + +const mp_obj_property_t wifi_network_authmode_obj = { + .base.type = &mp_type_property, + .proxy = { (mp_obj_t)&wifi_network_get_authmode_obj, + (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&mp_const_none_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) }, @@ -131,6 +146,7 @@ STATIC const mp_rom_map_elem_t wifi_network_locals_dict_table[] = { { 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) }, { MP_ROM_QSTR(MP_QSTR_country), MP_ROM_PTR(&wifi_network_country_obj) }, + { MP_ROM_QSTR(MP_QSTR_authmode), MP_ROM_PTR(&wifi_network_authmode_obj) }, }; STATIC MP_DEFINE_CONST_DICT(wifi_network_locals_dict, wifi_network_locals_dict_table); diff --git a/shared-bindings/wifi/Network.h b/shared-bindings/wifi/Network.h index e672e3108a..0f07e7b555 100644 --- a/shared-bindings/wifi/Network.h +++ b/shared-bindings/wifi/Network.h @@ -40,5 +40,6 @@ 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); extern mp_obj_t common_hal_wifi_network_get_country(wifi_network_obj_t *self); +extern mp_obj_t common_hal_wifi_network_get_authmode(wifi_network_obj_t *self); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_WIFI_NETWORK_H diff --git a/shared-bindings/wifi/Radio.c b/shared-bindings/wifi/Radio.c index 723572a321..63f507067a 100644 --- a/shared-bindings/wifi/Radio.c +++ b/shared-bindings/wifi/Radio.c @@ -295,7 +295,7 @@ const mp_obj_property_t wifi_radio_ipv4_dns_obj = { }; //| ap_info: Optional[Network] -//| """Network object containing BSSID, SSID, channel, country and RSSI when connected to an access point. None otherwise.""" +//| """Network object containing BSSID, SSID, authmode, channel, country and RSSI when connected to an access point. None otherwise.""" //| STATIC mp_obj_t wifi_radio_get_ap_info(mp_obj_t self) { return common_hal_wifi_radio_get_ap_info(self); From 115f3e08677520806b95dfc92e4462d119854023 Mon Sep 17 00:00:00 2001 From: BennyE Date: Thu, 7 Jan 2021 23:05:16 +0100 Subject: [PATCH 2/3] Updated code as per helpful suggestion --- ports/esp32s2/common-hal/wifi/Network.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/ports/esp32s2/common-hal/wifi/Network.c b/ports/esp32s2/common-hal/wifi/Network.c index 0289ad4ebb..83c675a190 100644 --- a/ports/esp32s2/common-hal/wifi/Network.c +++ b/ports/esp32s2/common-hal/wifi/Network.c @@ -56,34 +56,34 @@ mp_obj_t common_hal_wifi_network_get_country(wifi_network_obj_t *self) { } mp_obj_t common_hal_wifi_network_get_authmode(wifi_network_obj_t *self) { - char authmode[16]; + const char* authmode = ""; switch (self->record.authmode) { case WIFI_AUTH_OPEN: - strcpy(authmode, "OPEN"); + authmode = "OPEN"; break; case WIFI_AUTH_WEP: - strcpy(authmode, "WEP"); + authmode = "WEP"; break; case WIFI_AUTH_WPA_PSK: - strcpy(authmode, "WPA_PSK"); + authmode = "WPA_PSK"; break; case WIFI_AUTH_WPA2_PSK: - strcpy(authmode, "WPA2_PSK"); + authmode = "WPA2_PSK"; break; case WIFI_AUTH_WPA_WPA2_PSK: - strcpy(authmode, "WPA_WPA2_PSK"); + authmode = "WPA_WPA2_PSK"; break; case WIFI_AUTH_WPA2_ENTERPRISE: - strcpy(authmode, "WPA2_ENTERPRISE"); + authmode = "WPA2_ENTERPRISE"; break; case WIFI_AUTH_WPA3_PSK: - strcpy(authmode, "WPA3_PSK"); + authmode = "WPA3_PSK"; break; case WIFI_AUTH_WPA2_WPA3_PSK: - strcpy(authmode, "WPA2_WPA3_PSK"); + authmode = "WPA2_WPA3_PSK"; break; default: - strcpy(authmode, "UNKNOWN"); + authmode = "UNKNOWN"; break; } const char* cstr = (const char*) authmode; From 53e4d78a3cf42b4a8766d8d2d8315464b80b9632 Mon Sep 17 00:00:00 2001 From: BennyE Date: Sun, 10 Jan 2021 17:18:43 +0100 Subject: [PATCH 3/3] Update ports/esp32s2/common-hal/wifi/Network.c Avoid to use yet another variable. Co-authored-by: Scott Shawcroft --- ports/esp32s2/common-hal/wifi/Network.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ports/esp32s2/common-hal/wifi/Network.c b/ports/esp32s2/common-hal/wifi/Network.c index 83c675a190..b6eb6bb433 100644 --- a/ports/esp32s2/common-hal/wifi/Network.c +++ b/ports/esp32s2/common-hal/wifi/Network.c @@ -86,6 +86,5 @@ mp_obj_t common_hal_wifi_network_get_authmode(wifi_network_obj_t *self) { authmode = "UNKNOWN"; break; } - const char* cstr = (const char*) authmode; - return mp_obj_new_str(cstr, strlen(cstr)); + return mp_obj_new_str(authmode, strlen(authmode)); }