Add gateway, subnet, and rssi info for current connected AP

ap_rssi is a bound method, which I'm not keen on, but it works
This commit is contained in:
Ryan T. Hamilton 2020-09-19 13:38:04 -07:00
parent 9d9bc5adee
commit 04ffd0dca0
3 changed files with 85 additions and 0 deletions

View File

@ -147,6 +147,44 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
return WIFI_RADIO_ERROR_NONE;
}
mp_obj_t common_hal_wifi_radio_get_ap_rssi(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
wifi_mode_t if_mode;
esp_wifi_get_mode(&if_mode);
if (if_mode != WIFI_MODE_STA){
return mp_const_none;
}
wifi_ap_record_t ap_info;
esp_wifi_sta_get_ap_info(&ap_info);
mp_obj_t rssi;
rssi = MP_OBJ_NEW_SMALL_INT(ap_info.rssi);
return rssi;
}
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;
}
esp_netif_ip_info_t ip_info;
esp_netif_get_ip_info(self->netif, &ip_info);
return common_hal_ipaddress_new_ipv4address(ip_info.gw.addr);
}
mp_obj_t common_hal_wifi_radio_get_ipv4_subnet(wifi_radio_obj_t *self) {
if (!esp_netif_is_netif_up(self->netif)) {
return mp_const_none;
}
esp_netif_ip_info_t ip_info;
esp_netif_get_ip_info(self->netif, &ip_info);
return common_hal_ipaddress_new_ipv4address(ip_info.netmask.addr);
}
mp_obj_t common_hal_wifi_radio_get_ipv4_address(wifi_radio_obj_t *self) {
if (!esp_netif_is_netif_up(self->netif)) {
return mp_const_none;

View File

@ -163,6 +163,47 @@ STATIC mp_obj_t wifi_radio_connect(size_t n_args, const mp_obj_t *pos_args, mp_m
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wifi_radio_connect_obj, 1, wifi_radio_connect);
//| ap_rssi: int
//| """RSSI of the currently connected AP. Returns none if not connected"""
//|
STATIC mp_obj_t wifi_radio_get_ap_rssi(mp_obj_t self) {
return common_hal_wifi_radio_get_ap_rssi(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ap_rssi_obj, wifi_radio_get_ap_rssi);
//| ipv4_gateway: Optional[ipaddress.IPv4Address]
//| """IP v4 Address of the gateway when connected to an access point. None otherwise."""
//|
STATIC mp_obj_t wifi_radio_get_ipv4_gateway(mp_obj_t self) {
return common_hal_wifi_radio_get_ipv4_gateway(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_gateway_obj, wifi_radio_get_ipv4_gateway);
const mp_obj_property_t wifi_radio_ipv4_gateway_obj = {
.base.type = &mp_type_property,
.proxy = { (mp_obj_t)&wifi_radio_get_ipv4_gateway_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj },
};
//| ipv4_subnet: Optional[ipaddress.IPv4Address]
//| """IP v4 Address of the subnet when connected to an access point. None otherwise."""
//|
STATIC mp_obj_t wifi_radio_get_ipv4_subnet(mp_obj_t self) {
return common_hal_wifi_radio_get_ipv4_subnet(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_subnet_obj, wifi_radio_get_ipv4_subnet);
const mp_obj_property_t wifi_radio_ipv4_subnet_obj = {
.base.type = &mp_type_property,
.proxy = { (mp_obj_t)&wifi_radio_get_ipv4_subnet_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj },
};
//| ipv4_address: Optional[ipaddress.IPv4Address]
//| """IP v4 Address of the radio when connected to an access point. None otherwise."""
//|
@ -219,6 +260,9 @@ STATIC const mp_rom_map_elem_t wifi_radio_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&wifi_radio_connect_obj) },
// { 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_get_ap_rssi_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) },
{ MP_ROM_QSTR(MP_QSTR_ipv4_address), MP_ROM_PTR(&wifi_radio_ipv4_address_obj) },
// { MP_ROM_QSTR(MP_QSTR_access_point_active), MP_ROM_PTR(&wifi_radio_access_point_active_obj) },

View File

@ -53,6 +53,9 @@ 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_ipv4_gateway(wifi_radio_obj_t *self);
extern mp_obj_t common_hal_wifi_radio_get_ipv4_subnet(wifi_radio_obj_t *self);
extern mp_obj_t common_hal_wifi_radio_get_ipv4_address(wifi_radio_obj_t *self);
extern mp_int_t common_hal_wifi_radio_ping(wifi_radio_obj_t *self, mp_obj_t ip_address, mp_float_t timeout);