Skip to content

Add nic.ipconfig for other ports (like esp32, stm32 and others) #14199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 4, 2024
Merged
5 changes: 4 additions & 1 deletion extmod/modnetwork.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,15 @@ static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_network_hostname_obj, 0, 1, mod_n
#if LWIP_VERSION_MAJOR >= 2
MP_DEFINE_CONST_FUN_OBJ_KW(mod_network_ipconfig_obj, 0, mod_network_ipconfig);
#endif
#if MICROPY_PY_NETWORK_NINAW10
MP_DEFINE_CONST_FUN_OBJ_KW(mod_network_ipconfig_obj, 0, network_ninaw10_ipconfig);
#endif

static const mp_rom_map_elem_t mp_module_network_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_network) },
{ MP_ROM_QSTR(MP_QSTR_country), MP_ROM_PTR(&mod_network_country_obj) },
{ MP_ROM_QSTR(MP_QSTR_hostname), MP_ROM_PTR(&mod_network_hostname_obj) },
#if LWIP_VERSION_MAJOR >= 2
#if LWIP_VERSION_MAJOR >= 2 || MICROPY_PY_NETWORK_NINAW10
{ MP_ROM_QSTR(MP_QSTR_ipconfig), MP_ROM_PTR(&mod_network_ipconfig_obj) },
#endif

Expand Down
4 changes: 4 additions & 0 deletions extmod/modnetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ extern int mp_mod_network_prefer_dns_use_ip_version;
#endif
#elif defined(MICROPY_PORT_NETWORK_INTERFACES)

#if MICROPY_PY_NETWORK_NINAW10
mp_obj_t network_ninaw10_ipconfig(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
#endif

struct _mod_network_socket_obj_t;

typedef struct _mod_network_nic_protocol_t {
Expand Down
17 changes: 10 additions & 7 deletions extmod/network_lwip.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,20 +297,23 @@ mp_obj_t mod_network_nic_ipconfig(struct netif *netif, size_t n_args, const mp_o
char plain_ip[IPADDR_STRLEN_MAX];
char *split = strchr(input_str, '/');
const char *addr_str = input_str;
int to_copy = MIN(sizeof(plain_ip) - 1, addr_len);
memcpy(plain_ip, addr_str, to_copy);
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);
if (mp_obj_str_get_qstr(args[0]) == MP_QSTR_addr4) {
uint32_t mask = -(1u << (32 - prefix_bits));
ip_addr_set_ip4_u32_val(netmask, ((mask & 0xFF) << 24) | ((mask & 0xFF00) << 8) | ((mask >> 8) & 0xFF00) | ((mask >> 24) & 0xFF));
}
} else {
netmask = netif->netmask;
}
if (mp_obj_str_get_qstr(args[0]) == MP_QSTR_addr4) {
uint32_t mask = -(1u << (32 - prefix_bits));
ip_addr_set_ip4_u32_val(netmask, ((mask & 0xFF) << 24) | ((mask & 0xFF00) << 8) | ((mask >> 8) & 0xFF00) | ((mask >> 24) & 0xFF));
}
if (!ipaddr_aton(addr_str, &ip_addr)) {
plain_ip[to_copy] = '\0';
if (!ipaddr_aton(plain_ip, &ip_addr)) {
mp_raise_ValueError(MP_ERROR_TEXT("invalid arguments"));
}
if ((mp_obj_str_get_qstr(args[0]) == MP_QSTR_addr6) != IP_IS_V6(&ip_addr)
Expand Down
151 changes: 151 additions & 0 deletions extmod/network_ninaw10.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "py/runtime.h"
#include "py/misc.h"
#include "py/mperrno.h"
#include "py/parsenum.h"
#include "shared/netutils/netutils.h"
#include "shared/runtime/softtimer.h"
#include "extmod/modnetwork.h"
Expand Down Expand Up @@ -75,6 +76,8 @@ typedef struct _nina_obj_t {
#define SO_NO_CHECK (0x100a)
#define NINAW10_POLL_INTERVAL (100)

#define IPADDR_STRLEN_MAX 46

#define is_nonblocking_error(errno) ((errno) == MP_EAGAIN || (errno) == MP_EWOULDBLOCK || (errno) == MP_EINPROGRESS)

#define debug_printf(...) // mp_printf(&mp_plat_print, __VA_ARGS__)
Expand All @@ -87,6 +90,8 @@ static mp_sched_node_t mp_wifi_poll_node;
static soft_timer_entry_t mp_wifi_poll_timer;
static void network_ninaw10_deinit(void);

static bool network_ninaw10_dhcp_active = false;

static bool network_ninaw10_poll_list_is_empty(void) {
return MP_STATE_PORT(mp_wifi_poll_list) == NULL ||
MP_STATE_PORT(mp_wifi_poll_list)->len == 0;
Expand Down Expand Up @@ -199,6 +204,7 @@ static mp_obj_t network_ninaw10_active(size_t n_args, const mp_obj_t *args) {
mp_raise_msg_varg(&mp_type_OSError,
MP_ERROR_TEXT("failed to initialize Nina-W10 module, error: %d"), error);
}
network_ninaw10_dhcp_active = true;
// check firmware version
uint8_t semver[NINA_FW_VER_LEN];
if (nina_fw_version(semver) != 0) {
Expand Down Expand Up @@ -367,11 +373,155 @@ static mp_obj_t network_ninaw10_ifconfig(size_t n_args, const mp_obj_t *args) {
netutils_parse_ipv4_addr(items[2], ifconfig.gateway_addr, NETUTILS_BIG);
netutils_parse_ipv4_addr(items[3], ifconfig.dns_addr, NETUTILS_BIG);
nina_ifconfig(&ifconfig, true);
network_ninaw10_dhcp_active = false;
return mp_const_none;
}
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(network_ninaw10_ifconfig_obj, 1, 2, network_ninaw10_ifconfig);

mp_obj_t network_ninaw10_ipconfig(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
nina_ifconfig_t ifconfig;
// get ifconfig info
nina_ifconfig(&ifconfig, false);

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: {
return netutils_format_ipv4_addr(ifconfig.dns_addr, NETUTILS_BIG);
}
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: {
netutils_parse_ipv4_addr(e->value, ifconfig.dns_addr, NETUTILS_BIG);
nina_ifconfig(&ifconfig, true);
break;
}
default: {
mp_raise_ValueError(MP_ERROR_TEXT("unexpected key"));
break;
}
}
}
}
}
return mp_const_none;
}

static mp_obj_t network_ninaw10_nic_ipconfig(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
nina_ifconfig_t ifconfig;
// get ifconfig info
nina_ifconfig(&ifconfig, false);

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: {
return mp_obj_new_bool(network_ninaw10_dhcp_active);
}
case MP_QSTR_has_dhcp4: {
uint16_t ip_sum =
ifconfig.ip_addr[0] + ifconfig.ip_addr[1] + ifconfig.ip_addr[2] + ifconfig.ip_addr[3];
if (network_ninaw10_dhcp_active) {
return mp_obj_new_bool(ip_sum != 0);
} else {
return mp_const_false;
}
}
case MP_QSTR_addr4: {
mp_obj_t tuple[2] = {
netutils_format_ipv4_addr(ifconfig.ip_addr, NETUTILS_BIG),
netutils_format_ipv4_addr(ifconfig.subnet_addr, NETUTILS_BIG),
};
return mp_obj_new_tuple(2, tuple);
}
case MP_QSTR_gw4: {
return netutils_format_ipv4_addr(ifconfig.gateway_addr, 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"));
}

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: {
mp_raise_ValueError(MP_ERROR_TEXT("DHCP control unsupported"));
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 *split = strchr(input_str, '/');
if (split) {
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));
ifconfig.subnet_addr[0] = (mask >> 24) & 0xFF;
ifconfig.subnet_addr[1] = (mask >> 16) & 0xFF;
ifconfig.subnet_addr[2] = (mask >> 8) & 0xFF;
ifconfig.subnet_addr[3] = mask & 0xFF;
}
netutils_parse_ipv4_addr(e->value, ifconfig.ip_addr, 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], ifconfig.ip_addr, NETUTILS_BIG);
netutils_parse_ipv4_addr(items[1], ifconfig.subnet_addr, NETUTILS_BIG);
}
nina_ifconfig(&ifconfig, true);
network_ninaw10_dhcp_active = false;
break;
}
case MP_QSTR_gw4: {
netutils_parse_ipv4_addr(e->value, ifconfig.gateway_addr, NETUTILS_BIG);
nina_ifconfig(&ifconfig, true);
network_ninaw10_dhcp_active = false;
break;
}
default: {
mp_raise_ValueError(MP_ERROR_TEXT("unexpected key"));
break;
}
}
}
}
}
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_KW(network_ninaw10_nic_ipconfig_obj, 1, network_ninaw10_nic_ipconfig);

static mp_obj_t network_ninaw10_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
nina_obj_t *self = MP_OBJ_TO_PTR(args[0]);
(void)self;
Expand Down Expand Up @@ -856,6 +1006,7 @@ static const mp_rom_map_elem_t nina_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_disconnect), MP_ROM_PTR(&network_ninaw10_disconnect_obj) },
{ MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&network_ninaw10_isconnected_obj) },
{ MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&network_ninaw10_ifconfig_obj) },
{ MP_ROM_QSTR(MP_QSTR_ipconfig), MP_ROM_PTR(&network_ninaw10_nic_ipconfig_obj) },
{ MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&network_ninaw10_config_obj) },
{ MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&network_ninaw10_status_obj) },
{ MP_ROM_QSTR(MP_QSTR_ioctl), MP_ROM_PTR(&network_ninaw10_ioctl_obj) },
Expand Down
49 changes: 49 additions & 0 deletions ports/cc3200/mods/modnetwork.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "py/runtime.h"
#include "py/mperrno.h"
#include "py/mphal.h"
#include "shared/netutils/netutils.h"
#include "modnetwork.h"
#include "serverstask.h"
#include "simplelink.h"
Expand All @@ -53,6 +54,53 @@ static const mp_obj_type_t network_server_type;
void mod_network_init0(void) {
}

static mp_obj_t mod_network_ipconfig(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
unsigned char len = sizeof(SlNetCfgIpV4Args_t);
unsigned char dhcpIsOn;
SlNetCfgIpV4Args_t ipV4;
sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO, &dhcpIsOn, &len, (uint8_t *)&ipV4);

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: {
return netutils_format_ipv4_addr((uint8_t *)&ipV4.ipV4DnsServer, NETUTILS_LITTLE);
}
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: {
netutils_parse_ipv4_addr(e->value, (uint8_t *)&ipV4.ipV4DnsServer, NETUTILS_LITTLE);
sl_NetCfgSet(SL_IPV4_STA_P2P_CL_STATIC_ENABLE, IPCONFIG_MODE_ENABLE_IPV4, sizeof(SlNetCfgIpV4Args_t), (_u8 *)&ipV4);
break;
}
default: {
mp_raise_ValueError(MP_ERROR_TEXT("unexpected key"));
break;
}
}
}
}
}
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_KW(mod_network_ipconfig_obj, 0, mod_network_ipconfig);

#if (MICROPY_PORT_HAS_TELNET || MICROPY_PORT_HAS_FTP)
static mp_obj_t network_server_init_helper(mp_obj_t self, const mp_arg_val_t *args) {
const char *user = SERVERS_DEF_USER;
Expand Down Expand Up @@ -145,6 +193,7 @@ static MP_DEFINE_CONST_FUN_OBJ_1(network_server_deinit_obj, network_server_deini

static const mp_rom_map_elem_t mp_module_network_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_network) },
{ MP_ROM_QSTR(MP_QSTR_ipconfig), MP_ROM_PTR(&mod_network_ipconfig_obj) },
{ MP_ROM_QSTR(MP_QSTR_WLAN), MP_ROM_PTR(&mod_network_nic_type_wlan) },

#if (MICROPY_PORT_HAS_TELNET || MICROPY_PORT_HAS_FTP)
Expand Down
Loading
Loading