Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esp32-s2: wifi enhancement to include countrycode #3868

Merged
merged 6 commits into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -48,3 +48,9 @@ mp_obj_t common_hal_wifi_network_get_rssi(wifi_network_obj_t *self) {
mp_obj_t common_hal_wifi_network_get_channel(wifi_network_obj_t *self) {
return mp_obj_new_int(self->record.primary);
}

mp_obj_t common_hal_wifi_network_get_country(wifi_network_obj_t *self) {
const char* cstr = (const char*) self->record.country.cc;
// 2 instead of strlen(cstr) as this gives us only the country-code
return mp_obj_new_str(cstr, 2);
}
11 changes: 11 additions & 0 deletions ports/esp32s2/common-hal/wifi/Radio.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,17 @@ mp_obj_t common_hal_wifi_radio_get_ap_info(wifi_radio_obj_t *self) {
if (esp_wifi_sta_get_ap_info(&self->ap_info.record) != ESP_OK){
return mp_const_none;
} else {
if (strlen(self->ap_info.record.country.cc) == 0) {
BennyE marked this conversation as resolved.
Show resolved Hide resolved
// Workaround to fill country related information in ap_info until ESP-IDF carries a fix
// esp_wifi_sta_get_ap_info does not appear to fill wifi_country_t (e.g. country.cc) details
// (IDFGH-4437) #6267
// Note: It is possible that Wi-Fi APs don't have a CC set, then even after this workaround
// the element would remain empty.
memset(&self->ap_info.record.country, 0, sizeof(wifi_country_t));
if (esp_wifi_get_country(&self->ap_info.record.country) != ESP_OK) {
return mp_const_none;
}
}
memcpy(&ap_info->record, &self->ap_info.record, sizeof(wifi_ap_record_t));
return MP_OBJ_FROM_PTR(ap_info);
}
Expand Down
17 changes: 17 additions & 0 deletions shared-bindings/wifi/Network.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,29 @@ const mp_obj_property_t wifi_network_channel_obj = {
(mp_obj_t)&mp_const_none_obj },
};

//| country: str
//| """String id of the country code"""
//|
STATIC mp_obj_t wifi_network_get_country(mp_obj_t self) {
return common_hal_wifi_network_get_country(self);

}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_network_get_country_obj, wifi_network_get_country);

const mp_obj_property_t wifi_network_country_obj = {
.base.type = &mp_type_property,
.proxy = { (mp_obj_t)&wifi_network_get_country_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) },
{ 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) },
{ MP_ROM_QSTR(MP_QSTR_country), MP_ROM_PTR(&wifi_network_country_obj) },
};

STATIC MP_DEFINE_CONST_DICT(wifi_network_locals_dict, wifi_network_locals_dict_table);
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 @@ -39,5 +39,6 @@ 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);
extern mp_obj_t common_hal_wifi_network_get_country(wifi_network_obj_t *self);

#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WIFI_NETWORK_H
2 changes: 1 addition & 1 deletion shared-bindings/wifi/Radio.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ const mp_obj_property_t wifi_radio_ipv4_dns_obj = {
};

//| ap_info: Optional[Network]
//| """Network object containing BSSID, SSID, channel, and RSSI when connected to an access point. None otherwise."""
//| """Network object containing BSSID, SSID, 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);
Expand Down