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>
This commit is contained in:
parent
b555d6ccaf
commit
9ece9f9b52
@ -1,3 +1,4 @@
|
||||
extern const mp_obj_type_t esp_network_wlan_type;
|
||||
|
||||
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(esp_network_phy_mode_obj);
|
||||
MP_DECLARE_CONST_FUN_OBJ_KW(esp_network_ipconfig_obj);
|
||||
|
@ -1,5 +1,6 @@
|
||||
{ MP_ROM_QSTR(MP_QSTR_WLAN), MP_ROM_PTR(&esp_network_wlan_type) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_phy_mode), MP_ROM_PTR(&esp_network_phy_mode_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_ipconfig), MP_ROM_PTR(&esp_network_ipconfig_obj) },
|
||||
|
||||
{ MP_ROM_QSTR(MP_QSTR_STA_IF), MP_ROM_INT(STATION_IF)},
|
||||
{ MP_ROM_QSTR(MP_QSTR_AP_IF), MP_ROM_INT(SOFTAP_IF)},
|
||||
|
@ -31,6 +31,7 @@
|
||||
#include "py/objlist.h"
|
||||
#include "py/runtime.h"
|
||||
#include "py/mphal.h"
|
||||
#include "py/parsenum.h"
|
||||
#include "extmod/modnetwork.h"
|
||||
#include "shared/netutils/netutils.h"
|
||||
#include "queue.h"
|
||||
@ -41,6 +42,8 @@
|
||||
#include "lwip/dns.h"
|
||||
#include "modnetwork.h"
|
||||
|
||||
#define IPADDR_STRLEN_MAX (20)
|
||||
|
||||
typedef struct _wlan_if_obj_t {
|
||||
mp_obj_base_t base;
|
||||
int if_id;
|
||||
@ -330,6 +333,173 @@ static mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) {
|
||||
}
|
||||
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_ifconfig_obj, 1, 2, esp_ifconfig);
|
||||
|
||||
static mp_obj_t esp_network_ipconfig(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
|
||||
if (kwargs->used == 0) {
|
||||
// Get config value
|
||||
if (n_args != 1) {
|
||||
mp_raise_TypeError(MP_ERROR_TEXT("must query one param"));
|
||||
}
|
||||
|
||||
switch (mp_obj_str_get_qstr(args[0])) {
|
||||
case MP_QSTR_dns: {
|
||||
char addr_str[IPADDR_STRLEN_MAX];
|
||||
ip_addr_t dns_addr = dns_getserver(0);
|
||||
ipaddr_ntoa_r(&dns_addr, addr_str, sizeof(addr_str));
|
||||
return mp_obj_new_str(addr_str, strlen(addr_str));
|
||||
}
|
||||
default: {
|
||||
mp_raise_ValueError(MP_ERROR_TEXT("unexpected key"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Set config value(s)
|
||||
if (n_args != 0) {
|
||||
mp_raise_TypeError(MP_ERROR_TEXT("can't specify pos and kw args"));
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < kwargs->alloc; ++i) {
|
||||
if (MP_MAP_SLOT_IS_FILLED(kwargs, i)) {
|
||||
mp_map_elem_t *e = &kwargs->table[i];
|
||||
switch (mp_obj_str_get_qstr(e->key)) {
|
||||
case MP_QSTR_dns: {
|
||||
ip_addr_t dns;
|
||||
size_t addr_len;
|
||||
const char *addr_str = mp_obj_str_get_data(e->value, &addr_len);
|
||||
if (!ipaddr_aton(addr_str, &dns)) {
|
||||
mp_raise_ValueError(MP_ERROR_TEXT("invalid arguments as dns server"));
|
||||
}
|
||||
dns_setserver(0, &dns);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
mp_raise_ValueError(MP_ERROR_TEXT("unexpected key"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(esp_network_ipconfig_obj, 0, esp_network_ipconfig);
|
||||
|
||||
static mp_obj_t esp_ipconfig(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
|
||||
wlan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
|
||||
struct ip_info info;
|
||||
wifi_get_ip_info(self->if_id, &info);
|
||||
|
||||
if (kwargs->used == 0) {
|
||||
// Get config value
|
||||
if (n_args != 2) {
|
||||
mp_raise_TypeError(MP_ERROR_TEXT("must query one param"));
|
||||
}
|
||||
|
||||
switch (mp_obj_str_get_qstr(args[1])) {
|
||||
case MP_QSTR_dhcp4: {
|
||||
if (self->if_id == STATION_IF) {
|
||||
return mp_obj_new_bool(wifi_station_dhcpc_status() == DHCP_STARTED);
|
||||
} else if (self->if_id == SOFTAP_IF) {
|
||||
return mp_obj_new_bool(wifi_softap_dhcps_status() == DHCP_STARTED);
|
||||
} else {
|
||||
mp_raise_ValueError(MP_ERROR_TEXT("unexpected key"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
case MP_QSTR_addr4: {
|
||||
mp_obj_t tuple[2] = {
|
||||
netutils_format_ipv4_addr((uint8_t *)&info.ip, NETUTILS_BIG),
|
||||
netutils_format_ipv4_addr((uint8_t *)&info.netmask, NETUTILS_BIG),
|
||||
};
|
||||
return mp_obj_new_tuple(2, tuple);
|
||||
}
|
||||
case MP_QSTR_gw4: {
|
||||
return netutils_format_ipv4_addr((uint8_t *)&info.gw, NETUTILS_BIG);
|
||||
}
|
||||
default: {
|
||||
mp_raise_ValueError(MP_ERROR_TEXT("unexpected key"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
return mp_const_none;
|
||||
} else {
|
||||
// Set config value(s)
|
||||
if (n_args != 1) {
|
||||
mp_raise_TypeError(MP_ERROR_TEXT("can't specify pos and kw args"));
|
||||
}
|
||||
int touched_ip_info = 0;
|
||||
for (size_t i = 0; i < kwargs->alloc; ++i) {
|
||||
if (MP_MAP_SLOT_IS_FILLED(kwargs, i)) {
|
||||
mp_map_elem_t *e = &kwargs->table[i];
|
||||
switch (mp_obj_str_get_qstr(e->key)) {
|
||||
case MP_QSTR_dhcp4: {
|
||||
if (self->if_id == STATION_IF) {
|
||||
enum dhcp_status status = wifi_station_dhcpc_status();
|
||||
if (mp_obj_is_true(e->value) && status != DHCP_STARTED) {
|
||||
wifi_station_dhcpc_start();
|
||||
} else if (!mp_obj_is_true(e->value) && status == DHCP_STARTED) {
|
||||
wifi_station_dhcpc_stop();
|
||||
}
|
||||
} else {
|
||||
mp_raise_ValueError(MP_ERROR_TEXT("unexpected key"));
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case MP_QSTR_addr4: {
|
||||
if (e->value != mp_const_none && mp_obj_is_str(e->value)) {
|
||||
size_t addr_len;
|
||||
const char *input_str = mp_obj_str_get_data(e->value, &addr_len);
|
||||
char *split = strchr(input_str, '/');
|
||||
if (split) {
|
||||
mp_obj_t prefix_obj = mp_parse_num_integer(split + 1, strlen(split + 1), 10, NULL);
|
||||
int prefix_bits = mp_obj_get_int(prefix_obj);
|
||||
uint32_t mask = -(1u << (32 - prefix_bits));
|
||||
uint32_t *m = (uint32_t *)&info.netmask;
|
||||
*m = htonl(mask);
|
||||
}
|
||||
netutils_parse_ipv4_addr(e->value, (void *)&info.ip, NETUTILS_BIG);
|
||||
} else if (e->value != mp_const_none) {
|
||||
mp_obj_t *items;
|
||||
mp_obj_get_array_fixed_n(e->value, 2, &items);
|
||||
netutils_parse_ipv4_addr(items[0], (void *)&info.ip, NETUTILS_BIG);
|
||||
netutils_parse_ipv4_addr(items[1], (void *)&info.netmask, NETUTILS_BIG);
|
||||
}
|
||||
touched_ip_info = 1;
|
||||
break;
|
||||
}
|
||||
case MP_QSTR_gw4: {
|
||||
netutils_parse_ipv4_addr(e->value, (void *)&info.gw, NETUTILS_BIG);
|
||||
touched_ip_info = 1;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
mp_raise_ValueError(MP_ERROR_TEXT("unexpected key"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
bool restart_dhcp_server = false;
|
||||
if (self->if_id == STATION_IF) {
|
||||
if (touched_ip_info) {
|
||||
wifi_station_dhcpc_stop();
|
||||
wifi_set_ip_info(self->if_id, &info);
|
||||
}
|
||||
} else {
|
||||
restart_dhcp_server = wifi_softap_dhcps_status();
|
||||
wifi_softap_dhcps_stop();
|
||||
wifi_set_ip_info(self->if_id, &info);
|
||||
}
|
||||
if (restart_dhcp_server) {
|
||||
wifi_softap_dhcps_start();
|
||||
}
|
||||
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
static MP_DEFINE_CONST_FUN_OBJ_KW(esp_nic_ipconfig_obj, 1, esp_ipconfig);
|
||||
|
||||
static mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
|
||||
if (n_args != 1 && kwargs->used != 0) {
|
||||
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[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&esp_isconnected_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&esp_config_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&esp_ifconfig_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_ipconfig), MP_ROM_PTR(&esp_nic_ipconfig_obj) },
|
||||
|
||||
// Constants
|
||||
{ MP_ROM_QSTR(MP_QSTR_IF_STA), MP_ROM_INT(STATION_IF)},
|
||||
|
Loading…
Reference in New Issue
Block a user