Skip to content

Commit 1f23ab1

Browse files
felixdoerredpgeorge
authored andcommitted
esp32,mimxrt,stm32: Implement ipconfig() for more network interfaces.
Implements: - esp32: network.ipconfig() - esp32: network.LAN.ipconfig() - esp32: network.WLAN.ipconfig() - mimxrt: network.LAN.ipconfig() - stm32: network.LAN.ipconfig() Signed-off-by: Felix Dörre <[email protected]>
1 parent 0e19286 commit 1f23ab1

File tree

7 files changed

+190
-1
lines changed

7 files changed

+190
-1
lines changed

ports/esp32/modnetwork.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(esp_network_get_wlan_obj);
5454
MP_DECLARE_CONST_FUN_OBJ_KW(esp_network_get_lan_obj);
5555
MP_DECLARE_CONST_FUN_OBJ_1(esp_network_ppp_make_new_obj);
5656
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(esp_network_ifconfig_obj);
57+
MP_DECLARE_CONST_FUN_OBJ_KW(esp_network_ipconfig_obj);
58+
MP_DECLARE_CONST_FUN_OBJ_KW(esp_nic_ipconfig_obj);
5759
MP_DECLARE_CONST_FUN_OBJ_KW(esp_network_config_obj);
5860
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(esp_network_phy_mode_obj);
5961

ports/esp32/modnetwork_globals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
{ MP_ROM_QSTR(MP_QSTR_PPP), MP_ROM_PTR(&esp_network_ppp_make_new_obj) },
1212
#endif
1313
{ MP_ROM_QSTR(MP_QSTR_phy_mode), MP_ROM_PTR(&esp_network_phy_mode_obj) },
14+
{ MP_ROM_QSTR(MP_QSTR_ipconfig), MP_ROM_PTR(&esp_network_ipconfig_obj) },
1415

1516
#if MICROPY_PY_NETWORK_WLAN
1617
{ MP_ROM_QSTR(MP_QSTR_STA_IF), MP_ROM_INT(WIFI_IF_STA)},

ports/esp32/network_common.c

Lines changed: 171 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <string.h>
3535

3636
#include "py/runtime.h"
37+
#include "py/parsenum.h"
3738
#include "py/mperrno.h"
3839
#include "shared/netutils/netutils.h"
3940
#include "modnetwork.h"
@@ -42,7 +43,7 @@
4243
#include "esp_netif.h"
4344
#include "esp_wifi.h"
4445
#include "lwip/sockets.h"
45-
// #include "lwip/dns.h"
46+
#include "lwip/dns.h"
4647

4748
NORETURN void esp_exceptions_helper(esp_err_t e) {
4849
switch (e) {
@@ -154,6 +155,175 @@ static mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) {
154155
}
155156
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_network_ifconfig_obj, 1, 2, esp_ifconfig);
156157

158+
static mp_obj_t esp_network_ipconfig(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
159+
if (kwargs->used == 0) {
160+
// Get config value
161+
if (n_args != 1) {
162+
mp_raise_TypeError(MP_ERROR_TEXT("must query one param"));
163+
}
164+
165+
switch (mp_obj_str_get_qstr(args[0])) {
166+
case MP_QSTR_dns: {
167+
char addr_str[IPADDR_STRLEN_MAX];
168+
ipaddr_ntoa_r(dns_getserver(0), addr_str, sizeof(addr_str));
169+
return mp_obj_new_str(addr_str, strlen(addr_str));
170+
}
171+
default: {
172+
mp_raise_ValueError(MP_ERROR_TEXT("unexpected key"));
173+
break;
174+
}
175+
}
176+
} else {
177+
// Set config value(s)
178+
if (n_args != 0) {
179+
mp_raise_TypeError(MP_ERROR_TEXT("can't specify pos and kw args"));
180+
}
181+
182+
for (size_t i = 0; i < kwargs->alloc; ++i) {
183+
if (MP_MAP_SLOT_IS_FILLED(kwargs, i)) {
184+
mp_map_elem_t *e = &kwargs->table[i];
185+
switch (mp_obj_str_get_qstr(e->key)) {
186+
case MP_QSTR_dns: {
187+
ip_addr_t dns;
188+
size_t addr_len;
189+
const char *addr_str = mp_obj_str_get_data(e->value, &addr_len);
190+
if (!ipaddr_aton(addr_str, &dns)) {
191+
mp_raise_ValueError(MP_ERROR_TEXT("invalid arguments as dns server"));
192+
}
193+
dns_setserver(0, &dns);
194+
break;
195+
}
196+
default: {
197+
mp_raise_ValueError(MP_ERROR_TEXT("unexpected key"));
198+
break;
199+
}
200+
}
201+
}
202+
}
203+
}
204+
return mp_const_none;
205+
}
206+
MP_DEFINE_CONST_FUN_OBJ_KW(esp_network_ipconfig_obj, 0, esp_network_ipconfig);
207+
208+
static mp_obj_t esp_ipconfig(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
209+
base_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
210+
esp_netif_ip_info_t info;
211+
esp_netif_get_ip_info(self->netif, &info);
212+
213+
if (kwargs->used == 0) {
214+
// Get config value
215+
if (n_args != 2) {
216+
mp_raise_TypeError(MP_ERROR_TEXT("must query one param"));
217+
}
218+
219+
switch (mp_obj_str_get_qstr(args[1])) {
220+
case MP_QSTR_dhcp4: {
221+
if (self->if_id == ESP_IF_WIFI_STA || self->if_id == ESP_IF_ETH) {
222+
esp_netif_dhcp_status_t status;
223+
esp_exceptions(esp_netif_dhcpc_get_status(self->netif, &status));
224+
return mp_obj_new_bool(status == ESP_NETIF_DHCP_STARTED);
225+
} else {
226+
mp_raise_ValueError(MP_ERROR_TEXT("unexpected key"));
227+
break;
228+
}
229+
}
230+
case MP_QSTR_addr4: {
231+
mp_obj_t tuple[2] = {
232+
netutils_format_ipv4_addr((uint8_t *)&info.ip, NETUTILS_BIG),
233+
netutils_format_ipv4_addr((uint8_t *)&info.netmask, NETUTILS_BIG),
234+
};
235+
return mp_obj_new_tuple(2, tuple);
236+
}
237+
case MP_QSTR_gw4: {
238+
return netutils_format_ipv4_addr((uint8_t *)&info.gw, NETUTILS_BIG);
239+
}
240+
default: {
241+
mp_raise_ValueError(MP_ERROR_TEXT("unexpected key"));
242+
break;
243+
}
244+
}
245+
return mp_const_none;
246+
} else {
247+
// Set config value(s)
248+
if (n_args != 1) {
249+
mp_raise_TypeError(MP_ERROR_TEXT("can't specify pos and kw args"));
250+
}
251+
int touched_ip_info = 0;
252+
for (size_t i = 0; i < kwargs->alloc; ++i) {
253+
if (MP_MAP_SLOT_IS_FILLED(kwargs, i)) {
254+
mp_map_elem_t *e = &kwargs->table[i];
255+
switch (mp_obj_str_get_qstr(e->key)) {
256+
case MP_QSTR_dhcp4: {
257+
esp_netif_dhcp_status_t status;
258+
if (self->if_id == ESP_IF_WIFI_STA || self->if_id == ESP_IF_ETH) {
259+
esp_exceptions(esp_netif_dhcpc_get_status(self->netif, &status));
260+
if (mp_obj_is_true(e->value) && status != ESP_NETIF_DHCP_STARTED) {
261+
esp_exceptions(esp_netif_dhcpc_start(self->netif));
262+
} else if (!mp_obj_is_true(e->value) && status == ESP_NETIF_DHCP_STARTED) {
263+
esp_exceptions(esp_netif_dhcpc_stop(self->netif));
264+
}
265+
} else {
266+
mp_raise_ValueError(MP_ERROR_TEXT("unexpected key"));
267+
break;
268+
}
269+
break;
270+
}
271+
case MP_QSTR_addr4: {
272+
if (e->value != mp_const_none && mp_obj_is_str(e->value)) {
273+
size_t addr_len;
274+
const char *input_str = mp_obj_str_get_data(e->value, &addr_len);
275+
char *split = strchr(input_str, '/');
276+
if (split) {
277+
mp_obj_t prefix_obj = mp_parse_num_integer(split + 1, strlen(split + 1), 10, NULL);
278+
int prefix_bits = mp_obj_get_int(prefix_obj);
279+
uint32_t mask = -(1u << (32 - prefix_bits));
280+
uint32_t *m = (uint32_t *)&info.netmask;
281+
*m = esp_netif_htonl(mask);
282+
}
283+
netutils_parse_ipv4_addr(e->value, (void *)&info.ip, NETUTILS_BIG);
284+
} else if (e->value != mp_const_none) {
285+
mp_obj_t *items;
286+
mp_obj_get_array_fixed_n(e->value, 2, &items);
287+
netutils_parse_ipv4_addr(items[0], (void *)&info.ip, NETUTILS_BIG);
288+
netutils_parse_ipv4_addr(items[1], (void *)&info.netmask, NETUTILS_BIG);
289+
}
290+
touched_ip_info = 1;
291+
break;
292+
}
293+
case MP_QSTR_gw4: {
294+
netutils_parse_ipv4_addr(e->value, (void *)&info.gw, NETUTILS_BIG);
295+
touched_ip_info = 1;
296+
break;
297+
}
298+
default: {
299+
mp_raise_ValueError(MP_ERROR_TEXT("unexpected key"));
300+
break;
301+
}
302+
}
303+
}
304+
}
305+
if (self->if_id == ESP_IF_WIFI_STA || self->if_id == ESP_IF_ETH) {
306+
if (touched_ip_info) {
307+
esp_err_t e = esp_netif_dhcpc_stop(self->netif);
308+
if (e != ESP_OK && e != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
309+
esp_exceptions_helper(e);
310+
}
311+
esp_exceptions(esp_netif_set_ip_info(self->netif, &info));
312+
}
313+
} else if (self->if_id == ESP_IF_WIFI_AP) {
314+
esp_err_t e = esp_netif_dhcps_stop(self->netif);
315+
if (e != ESP_OK && e != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
316+
esp_exceptions_helper(e);
317+
}
318+
esp_exceptions(esp_netif_set_ip_info(self->netif, &info));
319+
esp_exceptions(esp_netif_dhcps_start(self->netif));
320+
}
321+
322+
}
323+
return mp_const_none;
324+
}
325+
MP_DEFINE_CONST_FUN_OBJ_KW(esp_nic_ipconfig_obj, 1, esp_ipconfig);
326+
157327
mp_obj_t esp_ifname(esp_netif_t *netif) {
158328
char ifname[NETIF_NAMESIZE + 1] = {0};
159329
mp_obj_t ret = mp_const_none;

ports/esp32/network_lan.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ static const mp_rom_map_elem_t lan_if_locals_dict_table[] = {
404404
{ MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&lan_status_obj) },
405405
{ MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&lan_config_obj) },
406406
{ MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&esp_network_ifconfig_obj) },
407+
{ MP_ROM_QSTR(MP_QSTR_ipconfig), MP_ROM_PTR(&esp_nic_ipconfig_obj) },
407408
};
408409

409410
static MP_DEFINE_CONST_DICT(lan_if_locals_dict, lan_if_locals_dict_table);

ports/esp32/network_wlan.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,7 @@ static const mp_rom_map_elem_t wlan_if_locals_dict_table[] = {
728728
{ MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&network_wlan_isconnected_obj) },
729729
{ MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&network_wlan_config_obj) },
730730
{ MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&esp_network_ifconfig_obj) },
731+
{ MP_ROM_QSTR(MP_QSTR_ipconfig), MP_ROM_PTR(&esp_nic_ipconfig_obj) },
731732

732733
// Constants
733734
{ MP_ROM_QSTR(MP_QSTR_IF_STA), MP_ROM_INT(WIFI_IF_STA)},

ports/mimxrt/network_lan.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ static mp_obj_t network_lan_ifconfig(size_t n_args, const mp_obj_t *args) {
179179
}
180180
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(network_lan_ifconfig_obj, 1, 2, network_lan_ifconfig);
181181

182+
static mp_obj_t network_lan_ipconfig(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
183+
network_lan_obj_t *self = MP_OBJ_TO_PTR(args[0]);
184+
return mod_network_nic_ipconfig(eth_netif(self->eth), n_args - 1, args + 1, kwargs);
185+
}
186+
static MP_DEFINE_CONST_FUN_OBJ_KW(network_lan_ipconfig_obj, 1, network_lan_ipconfig);
187+
182188
static mp_obj_t network_lan_status(size_t n_args, const mp_obj_t *args) {
183189
network_lan_obj_t *self = MP_OBJ_TO_PTR(args[0]);
184190
(void)self;
@@ -241,6 +247,7 @@ static const mp_rom_map_elem_t network_lan_locals_dict_table[] = {
241247
{ MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&network_lan_active_obj) },
242248
{ MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&network_lan_isconnected_obj) },
243249
{ MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&network_lan_ifconfig_obj) },
250+
{ MP_ROM_QSTR(MP_QSTR_ipconfig), MP_ROM_PTR(&network_lan_ipconfig_obj) },
244251
{ MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&network_lan_status_obj) },
245252
{ MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&network_lan_config_obj) },
246253

ports/stm32/network_lan.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ static mp_obj_t network_lan_ifconfig(size_t n_args, const mp_obj_t *args) {
101101
}
102102
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(network_lan_ifconfig_obj, 1, 2, network_lan_ifconfig);
103103

104+
static mp_obj_t network_lan_ipconfig(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
105+
network_lan_obj_t *self = MP_OBJ_TO_PTR(args[0]);
106+
return mod_network_nic_ipconfig(eth_netif(self->eth), n_args - 1, args + 1, kwargs);
107+
}
108+
static MP_DEFINE_CONST_FUN_OBJ_KW(network_lan_ipconfig_obj, 1, network_lan_ipconfig);
109+
104110
static mp_obj_t network_lan_status(size_t n_args, const mp_obj_t *args) {
105111
network_lan_obj_t *self = MP_OBJ_TO_PTR(args[0]);
106112
(void)self;
@@ -163,6 +169,7 @@ static const mp_rom_map_elem_t network_lan_locals_dict_table[] = {
163169
{ MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&network_lan_active_obj) },
164170
{ MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&network_lan_isconnected_obj) },
165171
{ MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&network_lan_ifconfig_obj) },
172+
{ MP_ROM_QSTR(MP_QSTR_ipconfig), MP_ROM_PTR(&network_lan_ipconfig_obj) },
166173
{ MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&network_lan_status_obj) },
167174
{ MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&network_lan_config_obj) },
168175

0 commit comments

Comments
 (0)