extmod/modnetwork: Allow more extensive port-specific customisation.

This allows for a port (e.g. esp8266/esp32) to use extmod/modnetwork.c
and provide the globals dict, rather than just a list of interfaces.

When this is used, the default implementation of `network.route` and the
NIC list is not enabled.

Also splits out the LWIP-specific helpers from modnetwork.c into
network_lwip.c.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This commit is contained in:
Jim Mussared
2023-02-01 14:19:45 +11:00
committed by Damien George
parent a377302623
commit f78464c12b
5 changed files with 119 additions and 67 deletions

View File

@@ -37,19 +37,15 @@
#include "shared/netutils/netutils.h"
#include "modnetwork.h"
#if MICROPY_PY_LWIP
#include "lwip/netif.h"
#include "lwip/timeouts.h"
#include "lwip/dns.h"
#include "lwip/dhcp.h"
#include "lwip/apps/mdns.h"
#endif
#if MICROPY_PY_NETWORK_CYW43 && MICROPY_PY_NETWORK_CYW43_USE_LIB_DRIVER
// So that CYW43_LINK_xxx constants are available to MICROPY_PORT_NETWORK_INTERFACES.
#include "lib/cyw43-driver/src/cyw43.h"
#endif
#ifdef MICROPY_PY_NETWORK_INCLUDEFILE
#include MICROPY_PY_NETWORK_INCLUDEFILE
#endif
/// \module network - network configuration
///
/// This module provides network drivers and routing configuration.
@@ -62,6 +58,8 @@ char mod_network_country_code[2] = "XX";
char mod_network_hostname[MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN] = MICROPY_PY_NETWORK_HOSTNAME_DEFAULT;
#ifdef MICROPY_PORT_NETWORK_INTERFACES
void mod_network_init(void) {
mp_obj_list_init(&MP_STATE_PORT(mod_network_nic_list), 0);
}
@@ -97,6 +95,10 @@ STATIC mp_obj_t network_route(void) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(network_route_obj, network_route);
MP_REGISTER_ROOT_POINTER(mp_obj_list_t mod_network_nic_list);
#endif // MICROPY_PORT_NETWORK_INTERFACES
STATIC mp_obj_t network_country(size_t n_args, const mp_obj_t *args) {
if (n_args == 0) {
return mp_obj_new_str(mod_network_country_code, 2);
@@ -131,16 +133,23 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_network_hostname_obj, 0, 1, netwo
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_route), MP_ROM_PTR(&network_route_obj) },
{ 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) },
// Defined per port in mpconfigport.h
#ifdef MICROPY_PORT_NETWORK_INTERFACES
{ MP_ROM_QSTR(MP_QSTR_route), MP_ROM_PTR(&network_route_obj) },
MICROPY_PORT_NETWORK_INTERFACES
#endif
// Allow a port to take mostly full control of the network module.
#ifdef MICROPY_PY_NETWORK_MODULE_GLOBALS_INCLUDEFILE
#include MICROPY_PY_NETWORK_MODULE_GLOBALS_INCLUDEFILE
#else
// Constants
{ MP_ROM_QSTR(MP_QSTR_STA_IF), MP_ROM_INT(MOD_NETWORK_STA_IF) },
{ MP_ROM_QSTR(MP_QSTR_AP_IF), MP_ROM_INT(MOD_NETWORK_AP_IF) },
#endif
};
STATIC MP_DEFINE_CONST_DICT(mp_module_network_globals, mp_module_network_globals_table);
@@ -152,60 +161,4 @@ const mp_obj_module_t mp_module_network = {
MP_REGISTER_MODULE(MP_QSTR_network, mp_module_network);
/*******************************************************************************/
// Implementations of network methods that can be used by any interface
#if MICROPY_PY_LWIP
mp_obj_t mod_network_nic_ifconfig(struct netif *netif, size_t n_args, const mp_obj_t *args) {
if (n_args == 0) {
// Get IP addresses
const ip_addr_t *dns = dns_getserver(0);
mp_obj_t tuple[4] = {
netutils_format_ipv4_addr((uint8_t *)&netif->ip_addr, NETUTILS_BIG),
netutils_format_ipv4_addr((uint8_t *)&netif->netmask, NETUTILS_BIG),
netutils_format_ipv4_addr((uint8_t *)&netif->gw, NETUTILS_BIG),
netutils_format_ipv4_addr((uint8_t *)dns, NETUTILS_BIG),
};
return mp_obj_new_tuple(4, tuple);
} else if (args[0] == MP_OBJ_NEW_QSTR(MP_QSTR_dhcp)) {
// Start the DHCP client
if (dhcp_supplied_address(netif)) {
dhcp_renew(netif);
} else {
dhcp_stop(netif);
dhcp_start(netif);
}
// Wait for DHCP to get IP address
uint32_t start = mp_hal_ticks_ms();
while (!dhcp_supplied_address(netif)) {
if (mp_hal_ticks_ms() - start > 10000) {
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("timeout waiting for DHCP to get IP address"));
}
mp_hal_delay_ms(100);
}
return mp_const_none;
} else {
// Release and stop any existing DHCP
dhcp_release(netif);
dhcp_stop(netif);
// Set static IP addresses
mp_obj_t *items;
mp_obj_get_array_fixed_n(args[0], 4, &items);
netutils_parse_ipv4_addr(items[0], (uint8_t *)&netif->ip_addr, NETUTILS_BIG);
netutils_parse_ipv4_addr(items[1], (uint8_t *)&netif->netmask, NETUTILS_BIG);
netutils_parse_ipv4_addr(items[2], (uint8_t *)&netif->gw, NETUTILS_BIG);
ip_addr_t dns;
netutils_parse_ipv4_addr(items[3], (uint8_t *)&dns, NETUTILS_BIG);
dns_setserver(0, &dns);
return mp_const_none;
}
}
#endif
MP_REGISTER_ROOT_POINTER(mp_obj_list_t mod_network_nic_list);
#endif // MICROPY_PY_NETWORK