Try to reuse/share existing objects

This commit is contained in:
Ryan T. Hamilton 2020-09-21 21:36:46 -07:00
parent 2fa269ccbc
commit a77966a736
2 changed files with 11 additions and 15 deletions

View File

@ -153,21 +153,18 @@ mp_obj_t common_hal_wifi_radio_get_ap_rssi(wifi_radio_obj_t *self) {
} }
// Make sure the interface is in STA mode // Make sure the interface is in STA mode
wifi_mode_t if_mode; if (self->sta_mode){
esp_wifi_get_mode(&if_mode);
if (if_mode != WIFI_MODE_STA){
return mp_const_none; return mp_const_none;
} }
wifi_ap_record_t ap_info;
// From esp_wifi.h, the possible return values (typos theirs): // From esp_wifi.h, the possible return values (typos theirs):
// ESP_OK: succeed // ESP_OK: succeed
// ESP_ERR_WIFI_CONN: The station interface don't initialized // ESP_ERR_WIFI_CONN: The station interface don't initialized
// ESP_ERR_WIFI_NOT_CONNECT: The station is in disconnect status // ESP_ERR_WIFI_NOT_CONNECT: The station is in disconnect status
if (esp_wifi_sta_get_ap_info(&ap_info) != ESP_OK){ if (esp_wifi_sta_get_ap_info(&self->ap_info) != ESP_OK){
return mp_const_none; return mp_const_none;
} else { } else {
return mp_obj_new_int(ap_info.rssi); return mp_obj_new_int(self->ap_info.rssi);
} }
} }
@ -175,27 +172,24 @@ mp_obj_t common_hal_wifi_radio_get_ipv4_gateway(wifi_radio_obj_t *self) {
if (!esp_netif_is_netif_up(self->netif)) { if (!esp_netif_is_netif_up(self->netif)) {
return mp_const_none; return mp_const_none;
} }
esp_netif_ip_info_t ip_info; esp_netif_get_ip_info(self->netif, &self->ip_info);
esp_netif_get_ip_info(self->netif, &ip_info); return common_hal_ipaddress_new_ipv4address(self->ip_info.gw.addr);
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) { mp_obj_t common_hal_wifi_radio_get_ipv4_subnet(wifi_radio_obj_t *self) {
if (!esp_netif_is_netif_up(self->netif)) { if (!esp_netif_is_netif_up(self->netif)) {
return mp_const_none; return mp_const_none;
} }
esp_netif_ip_info_t ip_info; esp_netif_get_ip_info(self->netif, &self->ip_info);
esp_netif_get_ip_info(self->netif, &ip_info); return common_hal_ipaddress_new_ipv4address(self->ip_info.netmask.addr);
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) { mp_obj_t common_hal_wifi_radio_get_ipv4_address(wifi_radio_obj_t *self) {
if (!esp_netif_is_netif_up(self->netif)) { if (!esp_netif_is_netif_up(self->netif)) {
return mp_const_none; return mp_const_none;
} }
esp_netif_ip_info_t ip_info; esp_netif_get_ip_info(self->netif, &self->ip_info);
esp_netif_get_ip_info(self->netif, &ip_info); return common_hal_ipaddress_new_ipv4address(self->ip_info.ip.addr);
return common_hal_ipaddress_new_ipv4address(ip_info.ip.addr);
} }
mp_int_t common_hal_wifi_radio_ping(wifi_radio_obj_t *self, mp_obj_t ip_address, mp_float_t timeout) { mp_int_t common_hal_wifi_radio_ping(wifi_radio_obj_t *self, mp_obj_t ip_address, mp_float_t timeout) {

View File

@ -46,6 +46,8 @@ typedef struct {
StaticEventGroup_t event_group; StaticEventGroup_t event_group;
EventGroupHandle_t event_group_handle; EventGroupHandle_t event_group_handle;
wifi_config_t sta_config; wifi_config_t sta_config;
wifi_ap_record_t ap_info;
esp_netif_ip_info_t ip_info;
esp_netif_t *netif; esp_netif_t *netif;
bool started; bool started;
bool ap_mode; bool ap_mode;