Skip to content

Commit

Permalink
ports/{esp32,...}: Implement nic.ipconfig for more network interfaces.
Browse files Browse the repository at this point in the history
Signed-off-by: Felix Dörre <felix@dogcraft.de>
  • Loading branch information
felixdoerre committed Mar 27, 2024
1 parent 35e8d18 commit 8f32d70
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 0 deletions.
1 change: 1 addition & 0 deletions ports/esp32/modnetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(esp_network_get_wlan_obj);
MP_DECLARE_CONST_FUN_OBJ_KW(esp_network_get_lan_obj);
MP_DECLARE_CONST_FUN_OBJ_1(esp_network_ppp_make_new_obj);
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(esp_network_ifconfig_obj);
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(esp_network_ipconfig_obj);
MP_DECLARE_CONST_FUN_OBJ_KW(esp_network_config_obj);
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(esp_network_phy_mode_obj);

Expand Down
143 changes: 143 additions & 0 deletions ports/esp32/network_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <string.h>

#include "py/runtime.h"
#include "py/parsenum.h"
#include "py/mperrno.h"
#include "shared/netutils/netutils.h"
#include "modnetwork.h"
Expand Down Expand Up @@ -154,6 +155,148 @@ static mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) {
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_network_ifconfig_obj, 1, 2, esp_ifconfig);

static mp_obj_t esp_ipconfig(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
base_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
esp_netif_ip_info_t info;
esp_netif_dns_info_t dns_info;
esp_netif_get_ip_info(self->netif, &info);
esp_netif_get_dns_info(self->netif, ESP_NETIF_DNS_MAIN, &dns_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 == ESP_IF_WIFI_STA || self->if_id == ESP_IF_ETH) {
esp_netif_dhcp_status_t status;
esp_exceptions(esp_netif_dhcpc_get_status(self->netif, &status));
return mp_obj_new_bool(status == ESP_NETIF_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 != 0) {
mp_raise_TypeError(MP_ERROR_TEXT("can't specify pos and kw args"));
}
int touched_ip_info = 0;
int touched_dns_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: {
esp_netif_dhcp_status_t status;
if (self->if_id == ESP_IF_WIFI_STA || self->if_id == ESP_IF_ETH) {
esp_exceptions(esp_netif_dhcpc_get_status(self->netif, &status));
if (mp_obj_is_true(e->value) && status != ESP_NETIF_DHCP_STARTED) {
esp_exceptions(esp_netif_dhcpc_start(self->netif));
} else if (!mp_obj_is_true(e->value) && status == ESP_NETIF_DHCP_STARTED) {
esp_exceptions(esp_netif_dhcpc_stop(self->netif));
}
} else {
mp_raise_ValueError(MP_ERROR_TEXT("unexpected key"));
break;
}
break;
}
case MP_QSTR_addr4: {
int prefix_bits = 32;
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 plain_ip[IPADDR_STRLEN_MAX];
char *split = strchr(input_str, '/');
const char *addr_str = input_str;
if (split) {
int to_copy = sizeof(plain_ip) - 1;
if (split - addr_str < to_copy) {
to_copy = split - addr_str;
}
memcpy(plain_ip, addr_str, to_copy);
mp_obj_t prefix_obj = mp_parse_num_integer(split + 1, strlen(split + 1), 10, NULL);
prefix_bits = mp_obj_get_int(prefix_obj);
}
uint32_t mask = -(1u << (32 - prefix_bits));
uint32_t *m = (uint32_t *)&info.netmask;
*m = esp_netif_htonl(mask);
// TODO: netutils code does not allow inputing a C-string, but requires a mp_string
// This makes parsing CIDR-Notation a bit more complex.
// netutils_parse_ipv4_addr(addr_str, (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;
}
case MP_QSTR_dns: {
netutils_parse_ipv4_addr(e->value, (void *)&ns_info.ip, NETUTILS_BIG);
touched_dns_info = 1;
break;
}
default: {
mp_raise_ValueError(MP_ERROR_TEXT("unexpected key"));
break;
}
}
}
}
if (self->if_id == ESP_IF_WIFI_STA || self->if_id == ESP_IF_ETH) {
if (touched_ip_info) {
esp_err_t e = esp_netif_dhcpc_stop(self->netif);
if (e != ESP_OK && e != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
esp_exceptions_helper(e);
}
esp_exceptions(esp_netif_set_ip_info(self->netif, &info));
}
if (touched_dns_info) {
esp_exceptions(esp_netif_set_dns_info(self->netif, ESP_NETIF_DNS_MAIN, &dns_info));
}
} else if (self->if_id == ESP_IF_WIFI_AP) {
esp_err_t e = esp_netif_dhcps_stop(self->netif);
if (e != ESP_OK && e != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
esp_exceptions_helper(e);
}
esp_exceptions(esp_netif_set_ip_info(self->netif, &info));
esp_exceptions(esp_netif_set_dns_info(self->netif, ESP_NETIF_DNS_MAIN, &dns_info));
esp_exceptions(esp_netif_dhcps_start(self->netif));
}

}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(esp_network_ipconfig_obj, 1, esp_ipconfig);


mp_obj_t esp_ifname(esp_netif_t *netif) {
char ifname[NETIF_NAMESIZE + 1] = {0};
mp_obj_t ret = mp_const_none;
Expand Down
1 change: 1 addition & 0 deletions ports/esp32/network_lan.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ static const mp_rom_map_elem_t lan_if_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&lan_status_obj) },
{ MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&lan_config_obj) },
{ MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&esp_network_ifconfig_obj) },
{ MP_ROM_QSTR(MP_QSTR_ipconfig), MP_ROM_PTR(&esp_network_ipconfig_obj) },
};

static MP_DEFINE_CONST_DICT(lan_if_locals_dict, lan_if_locals_dict_table);
Expand Down
1 change: 1 addition & 0 deletions ports/esp32/network_wlan.c
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,7 @@ static const mp_rom_map_elem_t wlan_if_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&network_wlan_isconnected_obj) },
{ MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&network_wlan_config_obj) },
{ MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&esp_network_ifconfig_obj) },
{ MP_ROM_QSTR(MP_QSTR_ipconfig), MP_ROM_PTR(&esp_network_ipconfig_obj) },

// Constants
{ MP_ROM_QSTR(MP_QSTR_PM_NONE), MP_ROM_INT(WIFI_PS_NONE) },
Expand Down
7 changes: 7 additions & 0 deletions ports/mimxrt/network_lan.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ static mp_obj_t network_lan_ifconfig(size_t n_args, const mp_obj_t *args) {
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(network_lan_ifconfig_obj, 1, 2, network_lan_ifconfig);

static mp_obj_t network_lan_ipconfig(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
network_lan_obj_t *self = MP_OBJ_TO_PTR(args[0]);
return mod_network_nic_ipconfig(eth_netif(self->eth), n_args - 1, args + 1, kwargs);
}
static MP_DEFINE_CONST_FUN_OBJ_KW(network_lan_ipconfig_obj, 1, network_lan_ipconfig);

static mp_obj_t network_lan_status(size_t n_args, const mp_obj_t *args) {
network_lan_obj_t *self = MP_OBJ_TO_PTR(args[0]);
(void)self;
Expand Down Expand Up @@ -241,6 +247,7 @@ static const mp_rom_map_elem_t network_lan_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&network_lan_active_obj) },
{ MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&network_lan_isconnected_obj) },
{ MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&network_lan_ifconfig_obj) },
{ MP_ROM_QSTR(MP_QSTR_ipconfig), MP_ROM_PTR(&network_lan_ipconfig_obj) },
{ MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&network_lan_status_obj) },
{ MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&network_lan_config_obj) },

Expand Down
7 changes: 7 additions & 0 deletions ports/stm32/network_lan.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ static mp_obj_t network_lan_ifconfig(size_t n_args, const mp_obj_t *args) {
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(network_lan_ifconfig_obj, 1, 2, network_lan_ifconfig);

static mp_obj_t network_lan_ipconfig(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
network_lan_obj_t *self = MP_OBJ_TO_PTR(args[0]);
return mod_network_nic_ipconfig(eth_netif(self->eth), n_args - 1, args + 1, kwargs);
}
static MP_DEFINE_CONST_FUN_OBJ_KW(network_lan_ipconfig_obj, 1, network_lan_ipconfig);

static mp_obj_t network_lan_status(size_t n_args, const mp_obj_t *args) {
network_lan_obj_t *self = MP_OBJ_TO_PTR(args[0]);
(void)self;
Expand Down Expand Up @@ -163,6 +169,7 @@ static const mp_rom_map_elem_t network_lan_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&network_lan_active_obj) },
{ MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&network_lan_isconnected_obj) },
{ MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&network_lan_ifconfig_obj) },
{ MP_ROM_QSTR(MP_QSTR_ipconfig), MP_ROM_PTR(&network_lan_ipconfig_obj) },
{ MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&network_lan_status_obj) },
{ MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&network_lan_config_obj) },

Expand Down

0 comments on commit 8f32d70

Please sign in to comment.