Skip to content

Commit 9ece9f9

Browse files
felixdoerredpgeorge
authored andcommitted
esp8266/network_wlan: Implement network.ipconfig and WLAN.ipconfig.
Co-authored-by: robert-hh <robert@hammelrath.com> Signed-off-by: Felix Dörre <felix@dogcraft.de>
1 parent b555d6c commit 9ece9f9

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed

ports/esp8266/modnetwork.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
extern const mp_obj_type_t esp_network_wlan_type;
22

33
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(esp_network_phy_mode_obj);
4+
MP_DECLARE_CONST_FUN_OBJ_KW(esp_network_ipconfig_obj);

ports/esp8266/modnetwork_globals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{ MP_ROM_QSTR(MP_QSTR_WLAN), MP_ROM_PTR(&esp_network_wlan_type) },
22
{ MP_ROM_QSTR(MP_QSTR_phy_mode), MP_ROM_PTR(&esp_network_phy_mode_obj) },
3+
{ MP_ROM_QSTR(MP_QSTR_ipconfig), MP_ROM_PTR(&esp_network_ipconfig_obj) },
34

45
{ MP_ROM_QSTR(MP_QSTR_STA_IF), MP_ROM_INT(STATION_IF)},
56
{ MP_ROM_QSTR(MP_QSTR_AP_IF), MP_ROM_INT(SOFTAP_IF)},

ports/esp8266/network_wlan.c

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "py/objlist.h"
3232
#include "py/runtime.h"
3333
#include "py/mphal.h"
34+
#include "py/parsenum.h"
3435
#include "extmod/modnetwork.h"
3536
#include "shared/netutils/netutils.h"
3637
#include "queue.h"
@@ -41,6 +42,8 @@
4142
#include "lwip/dns.h"
4243
#include "modnetwork.h"
4344

45+
#define IPADDR_STRLEN_MAX (20)
46+
4447
typedef struct _wlan_if_obj_t {
4548
mp_obj_base_t base;
4649
int if_id;
@@ -330,6 +333,173 @@ static mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) {
330333
}
331334
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_ifconfig_obj, 1, 2, esp_ifconfig);
332335

336+
static mp_obj_t esp_network_ipconfig(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
337+
if (kwargs->used == 0) {
338+
// Get config value
339+
if (n_args != 1) {
340+
mp_raise_TypeError(MP_ERROR_TEXT("must query one param"));
341+
}
342+
343+
switch (mp_obj_str_get_qstr(args[0])) {
344+
case MP_QSTR_dns: {
345+
char addr_str[IPADDR_STRLEN_MAX];
346+
ip_addr_t dns_addr = dns_getserver(0);
347+
ipaddr_ntoa_r(&dns_addr, addr_str, sizeof(addr_str));
348+
return mp_obj_new_str(addr_str, strlen(addr_str));
349+
}
350+
default: {
351+
mp_raise_ValueError(MP_ERROR_TEXT("unexpected key"));
352+
break;
353+
}
354+
}
355+
} else {
356+
// Set config value(s)
357+
if (n_args != 0) {
358+
mp_raise_TypeError(MP_ERROR_TEXT("can't specify pos and kw args"));
359+
}
360+
361+
for (size_t i = 0; i < kwargs->alloc; ++i) {
362+
if (MP_MAP_SLOT_IS_FILLED(kwargs, i)) {
363+
mp_map_elem_t *e = &kwargs->table[i];
364+
switch (mp_obj_str_get_qstr(e->key)) {
365+
case MP_QSTR_dns: {
366+
ip_addr_t dns;
367+
size_t addr_len;
368+
const char *addr_str = mp_obj_str_get_data(e->value, &addr_len);
369+
if (!ipaddr_aton(addr_str, &dns)) {
370+
mp_raise_ValueError(MP_ERROR_TEXT("invalid arguments as dns server"));
371+
}
372+
dns_setserver(0, &dns);
373+
break;
374+
}
375+
default: {
376+
mp_raise_ValueError(MP_ERROR_TEXT("unexpected key"));
377+
break;
378+
}
379+
}
380+
}
381+
}
382+
}
383+
return mp_const_none;
384+
}
385+
MP_DEFINE_CONST_FUN_OBJ_KW(esp_network_ipconfig_obj, 0, esp_network_ipconfig);
386+
387+
static mp_obj_t esp_ipconfig(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
388+
wlan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
389+
struct ip_info info;
390+
wifi_get_ip_info(self->if_id, &info);
391+
392+
if (kwargs->used == 0) {
393+
// Get config value
394+
if (n_args != 2) {
395+
mp_raise_TypeError(MP_ERROR_TEXT("must query one param"));
396+
}
397+
398+
switch (mp_obj_str_get_qstr(args[1])) {
399+
case MP_QSTR_dhcp4: {
400+
if (self->if_id == STATION_IF) {
401+
return mp_obj_new_bool(wifi_station_dhcpc_status() == DHCP_STARTED);
402+
} else if (self->if_id == SOFTAP_IF) {
403+
return mp_obj_new_bool(wifi_softap_dhcps_status() == DHCP_STARTED);
404+
} else {
405+
mp_raise_ValueError(MP_ERROR_TEXT("unexpected key"));
406+
break;
407+
}
408+
}
409+
case MP_QSTR_addr4: {
410+
mp_obj_t tuple[2] = {
411+
netutils_format_ipv4_addr((uint8_t *)&info.ip, NETUTILS_BIG),
412+
netutils_format_ipv4_addr((uint8_t *)&info.netmask, NETUTILS_BIG),
413+
};
414+
return mp_obj_new_tuple(2, tuple);
415+
}
416+
case MP_QSTR_gw4: {
417+
return netutils_format_ipv4_addr((uint8_t *)&info.gw, NETUTILS_BIG);
418+
}
419+
default: {
420+
mp_raise_ValueError(MP_ERROR_TEXT("unexpected key"));
421+
break;
422+
}
423+
}
424+
return mp_const_none;
425+
} else {
426+
// Set config value(s)
427+
if (n_args != 1) {
428+
mp_raise_TypeError(MP_ERROR_TEXT("can't specify pos and kw args"));
429+
}
430+
int touched_ip_info = 0;
431+
for (size_t i = 0; i < kwargs->alloc; ++i) {
432+
if (MP_MAP_SLOT_IS_FILLED(kwargs, i)) {
433+
mp_map_elem_t *e = &kwargs->table[i];
434+
switch (mp_obj_str_get_qstr(e->key)) {
435+
case MP_QSTR_dhcp4: {
436+
if (self->if_id == STATION_IF) {
437+
enum dhcp_status status = wifi_station_dhcpc_status();
438+
if (mp_obj_is_true(e->value) && status != DHCP_STARTED) {
439+
wifi_station_dhcpc_start();
440+
} else if (!mp_obj_is_true(e->value) && status == DHCP_STARTED) {
441+
wifi_station_dhcpc_stop();
442+
}
443+
} else {
444+
mp_raise_ValueError(MP_ERROR_TEXT("unexpected key"));
445+
break;
446+
}
447+
break;
448+
}
449+
case MP_QSTR_addr4: {
450+
if (e->value != mp_const_none && mp_obj_is_str(e->value)) {
451+
size_t addr_len;
452+
const char *input_str = mp_obj_str_get_data(e->value, &addr_len);
453+
char *split = strchr(input_str, '/');
454+
if (split) {
455+
mp_obj_t prefix_obj = mp_parse_num_integer(split + 1, strlen(split + 1), 10, NULL);
456+
int prefix_bits = mp_obj_get_int(prefix_obj);
457+
uint32_t mask = -(1u << (32 - prefix_bits));
458+
uint32_t *m = (uint32_t *)&info.netmask;
459+
*m = htonl(mask);
460+
}
461+
netutils_parse_ipv4_addr(e->value, (void *)&info.ip, NETUTILS_BIG);
462+
} else if (e->value != mp_const_none) {
463+
mp_obj_t *items;
464+
mp_obj_get_array_fixed_n(e->value, 2, &items);
465+
netutils_parse_ipv4_addr(items[0], (void *)&info.ip, NETUTILS_BIG);
466+
netutils_parse_ipv4_addr(items[1], (void *)&info.netmask, NETUTILS_BIG);
467+
}
468+
touched_ip_info = 1;
469+
break;
470+
}
471+
case MP_QSTR_gw4: {
472+
netutils_parse_ipv4_addr(e->value, (void *)&info.gw, NETUTILS_BIG);
473+
touched_ip_info = 1;
474+
break;
475+
}
476+
default: {
477+
mp_raise_ValueError(MP_ERROR_TEXT("unexpected key"));
478+
break;
479+
}
480+
}
481+
}
482+
}
483+
bool restart_dhcp_server = false;
484+
if (self->if_id == STATION_IF) {
485+
if (touched_ip_info) {
486+
wifi_station_dhcpc_stop();
487+
wifi_set_ip_info(self->if_id, &info);
488+
}
489+
} else {
490+
restart_dhcp_server = wifi_softap_dhcps_status();
491+
wifi_softap_dhcps_stop();
492+
wifi_set_ip_info(self->if_id, &info);
493+
}
494+
if (restart_dhcp_server) {
495+
wifi_softap_dhcps_start();
496+
}
497+
498+
}
499+
return mp_const_none;
500+
}
501+
static MP_DEFINE_CONST_FUN_OBJ_KW(esp_nic_ipconfig_obj, 1, esp_ipconfig);
502+
333503
static mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
334504
if (n_args != 1 && kwargs->used != 0) {
335505
mp_raise_TypeError(MP_ERROR_TEXT("either pos or kw args are allowed"));
@@ -514,6 +684,7 @@ static const mp_rom_map_elem_t wlan_if_locals_dict_table[] = {
514684
{ MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&esp_isconnected_obj) },
515685
{ MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&esp_config_obj) },
516686
{ MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&esp_ifconfig_obj) },
687+
{ MP_ROM_QSTR(MP_QSTR_ipconfig), MP_ROM_PTR(&esp_nic_ipconfig_obj) },
517688

518689
// Constants
519690
{ MP_ROM_QSTR(MP_QSTR_IF_STA), MP_ROM_INT(STATION_IF)},

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy