Skip to content

Commit c3396e4

Browse files
authored
Merge pull request #3868 from BennyE/wifi-enhancement-countrycode
esp32-s2: wifi enhancement to include countrycode
2 parents 171efd5 + 64bb055 commit c3396e4

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed

ports/esp32s2/common-hal/wifi/Network.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,9 @@ mp_obj_t common_hal_wifi_network_get_rssi(wifi_network_obj_t *self) {
4848
mp_obj_t common_hal_wifi_network_get_channel(wifi_network_obj_t *self) {
4949
return mp_obj_new_int(self->record.primary);
5050
}
51+
52+
mp_obj_t common_hal_wifi_network_get_country(wifi_network_obj_t *self) {
53+
const char* cstr = (const char*) self->record.country.cc;
54+
// 2 instead of strlen(cstr) as this gives us only the country-code
55+
return mp_obj_new_str(cstr, 2);
56+
}

ports/esp32s2/common-hal/wifi/Radio.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,17 @@ mp_obj_t common_hal_wifi_radio_get_ap_info(wifi_radio_obj_t *self) {
198198
if (esp_wifi_sta_get_ap_info(&self->ap_info.record) != ESP_OK){
199199
return mp_const_none;
200200
} else {
201+
if (strlen(self->ap_info.record.country.cc) == 0) {
202+
// Workaround to fill country related information in ap_info until ESP-IDF carries a fix
203+
// esp_wifi_sta_get_ap_info does not appear to fill wifi_country_t (e.g. country.cc) details
204+
// (IDFGH-4437) #6267
205+
// Note: It is possible that Wi-Fi APs don't have a CC set, then even after this workaround
206+
// the element would remain empty.
207+
memset(&self->ap_info.record.country, 0, sizeof(wifi_country_t));
208+
if (esp_wifi_get_country(&self->ap_info.record.country) != ESP_OK) {
209+
return mp_const_none;
210+
}
211+
}
201212
memcpy(&ap_info->record, &self->ap_info.record, sizeof(wifi_ap_record_t));
202213
return MP_OBJ_FROM_PTR(ap_info);
203214
}

shared-bindings/wifi/Network.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,29 @@ const mp_obj_property_t wifi_network_channel_obj = {
108108
(mp_obj_t)&mp_const_none_obj },
109109
};
110110

111+
//| country: str
112+
//| """String id of the country code"""
113+
//|
114+
STATIC mp_obj_t wifi_network_get_country(mp_obj_t self) {
115+
return common_hal_wifi_network_get_country(self);
116+
117+
}
118+
MP_DEFINE_CONST_FUN_OBJ_1(wifi_network_get_country_obj, wifi_network_get_country);
119+
120+
const mp_obj_property_t wifi_network_country_obj = {
121+
.base.type = &mp_type_property,
122+
.proxy = { (mp_obj_t)&wifi_network_get_country_obj,
123+
(mp_obj_t)&mp_const_none_obj,
124+
(mp_obj_t)&mp_const_none_obj },
125+
};
126+
111127

112128
STATIC const mp_rom_map_elem_t wifi_network_locals_dict_table[] = {
113129
{ MP_ROM_QSTR(MP_QSTR_ssid), MP_ROM_PTR(&wifi_network_ssid_obj) },
114130
{ MP_ROM_QSTR(MP_QSTR_bssid), MP_ROM_PTR(&wifi_network_bssid_obj) },
115131
{ MP_ROM_QSTR(MP_QSTR_rssi), MP_ROM_PTR(&wifi_network_rssi_obj) },
116132
{ MP_ROM_QSTR(MP_QSTR_channel), MP_ROM_PTR(&wifi_network_channel_obj) },
133+
{ MP_ROM_QSTR(MP_QSTR_country), MP_ROM_PTR(&wifi_network_country_obj) },
117134
};
118135

119136
STATIC MP_DEFINE_CONST_DICT(wifi_network_locals_dict, wifi_network_locals_dict_table);

shared-bindings/wifi/Network.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ extern mp_obj_t common_hal_wifi_network_get_ssid(wifi_network_obj_t *self);
3939
extern mp_obj_t common_hal_wifi_network_get_bssid(wifi_network_obj_t *self);
4040
extern mp_obj_t common_hal_wifi_network_get_rssi(wifi_network_obj_t *self);
4141
extern mp_obj_t common_hal_wifi_network_get_channel(wifi_network_obj_t *self);
42+
extern mp_obj_t common_hal_wifi_network_get_country(wifi_network_obj_t *self);
4243

4344
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WIFI_NETWORK_H

shared-bindings/wifi/Radio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ const mp_obj_property_t wifi_radio_ipv4_dns_obj = {
295295
};
296296

297297
//| ap_info: Optional[Network]
298-
//| """Network object containing BSSID, SSID, channel, and RSSI when connected to an access point. None otherwise."""
298+
//| """Network object containing BSSID, SSID, channel, country and RSSI when connected to an access point. None otherwise."""
299299
//|
300300
STATIC mp_obj_t wifi_radio_get_ap_info(mp_obj_t self) {
301301
return common_hal_wifi_radio_get_ap_info(self);

0 commit comments

Comments
 (0)