Skip to content

Esp32s2: Expose more network parameters #3484

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

Merged
merged 13 commits into from
Oct 19, 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
66 changes: 61 additions & 5 deletions ports/esp32s2/common-hal/wifi/Radio.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/

#include "shared-bindings/wifi/Radio.h"
#include "shared-bindings/wifi/Network.h"

#include <string.h>

Expand All @@ -38,6 +39,8 @@
#include "components/esp_wifi/include/esp_wifi.h"
#include "components/lwip/include/apps/ping/ping_sock.h"

#define MAC_ADDRESS_LENGTH 6

static void start_station(wifi_radio_obj_t *self) {
if (self->sta_mode) {
return;
Expand All @@ -50,6 +53,8 @@ static void start_station(wifi_radio_obj_t *self) {
}
esp_wifi_set_mode(next_mode);

self->sta_mode = 1;

esp_wifi_set_config(WIFI_MODE_STA, &self->sta_config);
}

Expand All @@ -73,8 +78,6 @@ void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled) {
}
}

#define MAC_ADDRESS_LENGTH 6

mp_obj_t common_hal_wifi_radio_get_mac_address(wifi_radio_obj_t *self) {
uint8_t mac[MAC_ADDRESS_LENGTH];
esp_wifi_get_mac(ESP_IF_WIFI_STA, mac);
Expand Down Expand Up @@ -149,13 +152,66 @@ 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_info(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
if (!self->sta_mode){
return mp_const_none;
}

wifi_network_obj_t *ap_info = m_new_obj(wifi_network_obj_t);
ap_info->base.type = &wifi_network_type;
// From esp_wifi.h, the possible return values (typos theirs):
// ESP_OK: succeed
// ESP_ERR_WIFI_CONN: The station interface don't initialized
// ESP_ERR_WIFI_NOT_CONNECT: The station is in disconnect status
if (esp_wifi_sta_get_ap_info(&self->ap_info.record) != ESP_OK){
return mp_const_none;
} else {
memcpy(&ap_info->record, &self->ap_info.record, sizeof(wifi_ap_record_t));
return MP_OBJ_FROM_PTR(ap_info);
}
}

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_get_ip_info(self->netif, &self->ip_info);
return common_hal_ipaddress_new_ipv4address(self->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_get_ip_info(self->netif, &self->ip_info);
return common_hal_ipaddress_new_ipv4address(self->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;
}
esp_netif_ip_info_t ip_info;
esp_netif_get_ip_info(self->netif, &ip_info);
return common_hal_ipaddress_new_ipv4address(ip_info.ip.addr);
esp_netif_get_ip_info(self->netif, &self->ip_info);
return common_hal_ipaddress_new_ipv4address(self->ip_info.ip.addr);
}

mp_obj_t common_hal_wifi_radio_get_ipv4_dns(wifi_radio_obj_t *self) {
if (!esp_netif_is_netif_up(self->netif)) {
return mp_const_none;
}

esp_netif_get_dns_info(self->netif, ESP_NETIF_DNS_MAIN, &self->dns_info);

// dns_info is of type esp_netif_dns_info_t, which is just ever so slightly
// different than esp_netif_ip_info_t used for
// common_hal_wifi_radio_get_ipv4_address (includes both ipv4 and 6),
// so some extra jumping is required to get to the actual address
return common_hal_ipaddress_new_ipv4address(self->dns_info.ip.u_addr.ip4.addr);
}

mp_int_t common_hal_wifi_radio_ping(wifi_radio_obj_t *self, mp_obj_t ip_address, mp_float_t timeout) {
Expand Down
4 changes: 4 additions & 0 deletions ports/esp32s2/common-hal/wifi/Radio.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "components/esp_event/include/esp_event.h"

#include "shared-bindings/wifi/ScannedNetworks.h"
#include "shared-bindings/wifi/Network.h"

// Event bits for the Radio event group.
#define WIFI_SCAN_DONE_BIT BIT0
Expand All @@ -46,6 +47,9 @@ typedef struct {
StaticEventGroup_t event_group;
EventGroupHandle_t event_group_handle;
wifi_config_t sta_config;
wifi_network_obj_t ap_info;
esp_netif_ip_info_t ip_info;
esp_netif_dns_info_t dns_info;
esp_netif_t *netif;
bool started;
bool ap_mode;
Expand Down
68 changes: 68 additions & 0 deletions shared-bindings/wifi/Radio.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,38 @@ 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);

//| 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."""
//|
Expand All @@ -179,6 +211,38 @@ const mp_obj_property_t wifi_radio_ipv4_address_obj = {
(mp_obj_t)&mp_const_none_obj },
};

//| ipv4_dns: Optional[ipaddress.IPv4Address]
//| """IP v4 Address of the DNS server in use when connected to an access point. None otherwise."""
//|
STATIC mp_obj_t wifi_radio_get_ipv4_dns(mp_obj_t self) {
return common_hal_wifi_radio_get_ipv4_dns(self);

}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_dns_obj, wifi_radio_get_ipv4_dns);

const mp_obj_property_t wifi_radio_ipv4_dns_obj = {
.base.type = &mp_type_property,
.proxy = { (mp_obj_t)&wifi_radio_get_ipv4_dns_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj },
};

//| ap_info: Optional[Network]
//| """Network object containing BSSID, SSID, channel, 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);

}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ap_info_obj, wifi_radio_get_ap_info);

const mp_obj_property_t wifi_radio_ap_info_obj = {
.base.type = &mp_type_property,
.proxy = { (mp_obj_t)&wifi_radio_get_ap_info_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj },
};

//| def ping(self, ip, *, timeout: float = 0.5) -> float:
//| """Ping an IP to test connectivity. Returns echo time in seconds.
//| Returns None when it times out."""
Expand Down Expand Up @@ -219,6 +283,10 @@ 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_info), MP_ROM_PTR(&wifi_radio_ap_info_obj) },
{ MP_ROM_QSTR(MP_QSTR_ipv4_dns), MP_ROM_PTR(&wifi_radio_ipv4_dns_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) },
Expand Down
4 changes: 4 additions & 0 deletions shared-bindings/wifi/Radio.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ 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_info(wifi_radio_obj_t *self);
extern mp_obj_t common_hal_wifi_radio_get_ipv4_dns(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);
Expand Down