Skip to content

Commit d606a3e

Browse files
authored
Merge pull request #3484 from astrobokonon/esp32s2-morenet
Esp32s2: Expose more network parameters
2 parents d98b312 + 76f1db7 commit d606a3e

File tree

4 files changed

+137
-5
lines changed

4 files changed

+137
-5
lines changed

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

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include "shared-bindings/wifi/Radio.h"
28+
#include "shared-bindings/wifi/Network.h"
2829

2930
#include <string.h>
3031

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

42+
#define MAC_ADDRESS_LENGTH 6
43+
4144
static void start_station(wifi_radio_obj_t *self) {
4245
if (self->sta_mode) {
4346
return;
@@ -50,6 +53,8 @@ static void start_station(wifi_radio_obj_t *self) {
5053
}
5154
esp_wifi_set_mode(next_mode);
5255

56+
self->sta_mode = 1;
57+
5358
esp_wifi_set_config(WIFI_MODE_STA, &self->sta_config);
5459
}
5560

@@ -73,8 +78,6 @@ void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled) {
7378
}
7479
}
7580

76-
#define MAC_ADDRESS_LENGTH 6
77-
7881
mp_obj_t common_hal_wifi_radio_get_mac_address(wifi_radio_obj_t *self) {
7982
uint8_t mac[MAC_ADDRESS_LENGTH];
8083
esp_wifi_get_mac(ESP_IF_WIFI_STA, mac);
@@ -162,13 +165,66 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
162165
return WIFI_RADIO_ERROR_NONE;
163166
}
164167

168+
mp_obj_t common_hal_wifi_radio_get_ap_info(wifi_radio_obj_t *self) {
169+
if (!esp_netif_is_netif_up(self->netif)) {
170+
return mp_const_none;
171+
}
172+
173+
// Make sure the interface is in STA mode
174+
if (!self->sta_mode){
175+
return mp_const_none;
176+
}
177+
178+
wifi_network_obj_t *ap_info = m_new_obj(wifi_network_obj_t);
179+
ap_info->base.type = &wifi_network_type;
180+
// From esp_wifi.h, the possible return values (typos theirs):
181+
// ESP_OK: succeed
182+
// ESP_ERR_WIFI_CONN: The station interface don't initialized
183+
// ESP_ERR_WIFI_NOT_CONNECT: The station is in disconnect status
184+
if (esp_wifi_sta_get_ap_info(&self->ap_info.record) != ESP_OK){
185+
return mp_const_none;
186+
} else {
187+
memcpy(&ap_info->record, &self->ap_info.record, sizeof(wifi_ap_record_t));
188+
return MP_OBJ_FROM_PTR(ap_info);
189+
}
190+
}
191+
192+
mp_obj_t common_hal_wifi_radio_get_ipv4_gateway(wifi_radio_obj_t *self) {
193+
if (!esp_netif_is_netif_up(self->netif)) {
194+
return mp_const_none;
195+
}
196+
esp_netif_get_ip_info(self->netif, &self->ip_info);
197+
return common_hal_ipaddress_new_ipv4address(self->ip_info.gw.addr);
198+
}
199+
200+
mp_obj_t common_hal_wifi_radio_get_ipv4_subnet(wifi_radio_obj_t *self) {
201+
if (!esp_netif_is_netif_up(self->netif)) {
202+
return mp_const_none;
203+
}
204+
esp_netif_get_ip_info(self->netif, &self->ip_info);
205+
return common_hal_ipaddress_new_ipv4address(self->ip_info.netmask.addr);
206+
}
207+
165208
mp_obj_t common_hal_wifi_radio_get_ipv4_address(wifi_radio_obj_t *self) {
166209
if (!esp_netif_is_netif_up(self->netif)) {
167210
return mp_const_none;
168211
}
169-
esp_netif_ip_info_t ip_info;
170-
esp_netif_get_ip_info(self->netif, &ip_info);
171-
return common_hal_ipaddress_new_ipv4address(ip_info.ip.addr);
212+
esp_netif_get_ip_info(self->netif, &self->ip_info);
213+
return common_hal_ipaddress_new_ipv4address(self->ip_info.ip.addr);
214+
}
215+
216+
mp_obj_t common_hal_wifi_radio_get_ipv4_dns(wifi_radio_obj_t *self) {
217+
if (!esp_netif_is_netif_up(self->netif)) {
218+
return mp_const_none;
219+
}
220+
221+
esp_netif_get_dns_info(self->netif, ESP_NETIF_DNS_MAIN, &self->dns_info);
222+
223+
// dns_info is of type esp_netif_dns_info_t, which is just ever so slightly
224+
// different than esp_netif_ip_info_t used for
225+
// common_hal_wifi_radio_get_ipv4_address (includes both ipv4 and 6),
226+
// so some extra jumping is required to get to the actual address
227+
return common_hal_ipaddress_new_ipv4address(self->dns_info.ip.u_addr.ip4.addr);
172228
}
173229

174230
mp_int_t common_hal_wifi_radio_ping(wifi_radio_obj_t *self, mp_obj_t ip_address, mp_float_t timeout) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "components/esp_event/include/esp_event.h"
3333

3434
#include "shared-bindings/wifi/ScannedNetworks.h"
35+
#include "shared-bindings/wifi/Network.h"
3536

3637
// Event bits for the Radio event group.
3738
#define WIFI_SCAN_DONE_BIT BIT0
@@ -46,6 +47,9 @@ typedef struct {
4647
StaticEventGroup_t event_group;
4748
EventGroupHandle_t event_group_handle;
4849
wifi_config_t sta_config;
50+
wifi_network_obj_t ap_info;
51+
esp_netif_ip_info_t ip_info;
52+
esp_netif_dns_info_t dns_info;
4953
esp_netif_t *netif;
5054
bool started;
5155
bool ap_mode;

shared-bindings/wifi/Radio.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,38 @@ STATIC mp_obj_t wifi_radio_connect(size_t n_args, const mp_obj_t *pos_args, mp_m
203203
}
204204
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wifi_radio_connect_obj, 1, wifi_radio_connect);
205205

206+
//| ipv4_gateway: Optional[ipaddress.IPv4Address]
207+
//| """IP v4 Address of the gateway when connected to an access point. None otherwise."""
208+
//|
209+
STATIC mp_obj_t wifi_radio_get_ipv4_gateway(mp_obj_t self) {
210+
return common_hal_wifi_radio_get_ipv4_gateway(self);
211+
212+
}
213+
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_gateway_obj, wifi_radio_get_ipv4_gateway);
214+
215+
const mp_obj_property_t wifi_radio_ipv4_gateway_obj = {
216+
.base.type = &mp_type_property,
217+
.proxy = { (mp_obj_t)&wifi_radio_get_ipv4_gateway_obj,
218+
(mp_obj_t)&mp_const_none_obj,
219+
(mp_obj_t)&mp_const_none_obj },
220+
};
221+
222+
//| ipv4_subnet: Optional[ipaddress.IPv4Address]
223+
//| """IP v4 Address of the subnet when connected to an access point. None otherwise."""
224+
//|
225+
STATIC mp_obj_t wifi_radio_get_ipv4_subnet(mp_obj_t self) {
226+
return common_hal_wifi_radio_get_ipv4_subnet(self);
227+
228+
}
229+
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_subnet_obj, wifi_radio_get_ipv4_subnet);
230+
231+
const mp_obj_property_t wifi_radio_ipv4_subnet_obj = {
232+
.base.type = &mp_type_property,
233+
.proxy = { (mp_obj_t)&wifi_radio_get_ipv4_subnet_obj,
234+
(mp_obj_t)&mp_const_none_obj,
235+
(mp_obj_t)&mp_const_none_obj },
236+
};
237+
206238
//| ipv4_address: Optional[ipaddress.IPv4Address]
207239
//| """IP v4 Address of the radio when connected to an access point. None otherwise."""
208240
//|
@@ -219,6 +251,38 @@ const mp_obj_property_t wifi_radio_ipv4_address_obj = {
219251
(mp_obj_t)&mp_const_none_obj },
220252
};
221253

254+
//| ipv4_dns: Optional[ipaddress.IPv4Address]
255+
//| """IP v4 Address of the DNS server in use when connected to an access point. None otherwise."""
256+
//|
257+
STATIC mp_obj_t wifi_radio_get_ipv4_dns(mp_obj_t self) {
258+
return common_hal_wifi_radio_get_ipv4_dns(self);
259+
260+
}
261+
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_dns_obj, wifi_radio_get_ipv4_dns);
262+
263+
const mp_obj_property_t wifi_radio_ipv4_dns_obj = {
264+
.base.type = &mp_type_property,
265+
.proxy = { (mp_obj_t)&wifi_radio_get_ipv4_dns_obj,
266+
(mp_obj_t)&mp_const_none_obj,
267+
(mp_obj_t)&mp_const_none_obj },
268+
};
269+
270+
//| ap_info: Optional[Network]
271+
//| """Network object containing BSSID, SSID, channel, and RSSI when connected to an access point. None otherwise."""
272+
//|
273+
STATIC mp_obj_t wifi_radio_get_ap_info(mp_obj_t self) {
274+
return common_hal_wifi_radio_get_ap_info(self);
275+
276+
}
277+
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ap_info_obj, wifi_radio_get_ap_info);
278+
279+
const mp_obj_property_t wifi_radio_ap_info_obj = {
280+
.base.type = &mp_type_property,
281+
.proxy = { (mp_obj_t)&wifi_radio_get_ap_info_obj,
282+
(mp_obj_t)&mp_const_none_obj,
283+
(mp_obj_t)&mp_const_none_obj },
284+
};
285+
222286
//| def ping(self, ip, *, timeout: float = 0.5) -> float:
223287
//| """Ping an IP to test connectivity. Returns echo time in seconds.
224288
//| Returns None when it times out."""
@@ -261,6 +325,10 @@ STATIC const mp_rom_map_elem_t wifi_radio_locals_dict_table[] = {
261325
{ MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&wifi_radio_connect_obj) },
262326
// { MP_ROM_QSTR(MP_QSTR_connect_to_enterprise), MP_ROM_PTR(&wifi_radio_connect_to_enterprise_obj) },
263327

328+
{ MP_ROM_QSTR(MP_QSTR_ap_info), MP_ROM_PTR(&wifi_radio_ap_info_obj) },
329+
{ MP_ROM_QSTR(MP_QSTR_ipv4_dns), MP_ROM_PTR(&wifi_radio_ipv4_dns_obj) },
330+
{ MP_ROM_QSTR(MP_QSTR_ipv4_gateway), MP_ROM_PTR(&wifi_radio_ipv4_gateway_obj) },
331+
{ MP_ROM_QSTR(MP_QSTR_ipv4_subnet), MP_ROM_PTR(&wifi_radio_ipv4_subnet_obj) },
264332
{ MP_ROM_QSTR(MP_QSTR_ipv4_address), MP_ROM_PTR(&wifi_radio_ipv4_address_obj) },
265333

266334
// { MP_ROM_QSTR(MP_QSTR_access_point_active), MP_ROM_PTR(&wifi_radio_access_point_active_obj) },

shared-bindings/wifi/Radio.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ extern void common_hal_wifi_radio_stop_scanning_networks(wifi_radio_obj_t *self)
5555

5656
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);
5757

58+
extern mp_obj_t common_hal_wifi_radio_get_ap_info(wifi_radio_obj_t *self);
59+
extern mp_obj_t common_hal_wifi_radio_get_ipv4_dns(wifi_radio_obj_t *self);
60+
extern mp_obj_t common_hal_wifi_radio_get_ipv4_gateway(wifi_radio_obj_t *self);
61+
extern mp_obj_t common_hal_wifi_radio_get_ipv4_subnet(wifi_radio_obj_t *self);
5862
extern mp_obj_t common_hal_wifi_radio_get_ipv4_address(wifi_radio_obj_t *self);
5963

6064
extern mp_int_t common_hal_wifi_radio_ping(wifi_radio_obj_t *self, mp_obj_t ip_address, mp_float_t timeout);

0 commit comments

Comments
 (0)