From f78464c12ba97688e3d234d88362a90e38892f7c Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Wed, 1 Feb 2023 14:19:45 +1100 Subject: [PATCH] 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 --- extmod/extmod.cmake | 1 + extmod/extmod.mk | 1 + extmod/modnetwork.c | 83 ++++++++----------------------------- extmod/modnetwork.h | 6 ++- extmod/network_lwip.c | 95 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 119 insertions(+), 67 deletions(-) create mode 100644 extmod/network_lwip.c diff --git a/extmod/extmod.cmake b/extmod/extmod.cmake index 2a19a0e85c..0e5ed94226 100644 --- a/extmod/extmod.cmake +++ b/extmod/extmod.cmake @@ -38,6 +38,7 @@ set(MICROPY_SOURCE_EXTMOD ${MICROPY_EXTMOD_DIR}/moduzlib.c ${MICROPY_EXTMOD_DIR}/modwebrepl.c ${MICROPY_EXTMOD_DIR}/network_cyw43.c + ${MICROPY_EXTMOD_DIR}/network_lwip.c ${MICROPY_EXTMOD_DIR}/network_ninaw10.c ${MICROPY_EXTMOD_DIR}/network_wiznet5k.c ${MICROPY_EXTMOD_DIR}/uos_dupterm.c diff --git a/extmod/extmod.mk b/extmod/extmod.mk index 08138ead56..03fdb43546 100644 --- a/extmod/extmod.mk +++ b/extmod/extmod.mk @@ -37,6 +37,7 @@ SRC_EXTMOD_C += \ extmod/moduzlib.c \ extmod/modwebrepl.c \ extmod/network_cyw43.c \ + extmod/network_lwip.c \ extmod/network_ninaw10.c \ extmod/network_wiznet5k.c \ extmod/uos_dupterm.c \ diff --git a/extmod/modnetwork.c b/extmod/modnetwork.c index 0431e5f418..438c1ed744 100644 --- a/extmod/modnetwork.c +++ b/extmod/modnetwork.c @@ -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 diff --git a/extmod/modnetwork.h b/extmod/modnetwork.h index 0ef612d108..11691140fd 100644 --- a/extmod/modnetwork.h +++ b/extmod/modnetwork.h @@ -65,7 +65,7 @@ struct netif; void mod_network_lwip_init(void); void mod_network_lwip_poll_wrapper(uint32_t ticks_ms); mp_obj_t mod_network_nic_ifconfig(struct netif *netif, size_t n_args, const mp_obj_t *args); -#else +#elif defined(MICROPY_PORT_NETWORK_INTERFACES) struct _mod_network_socket_obj_t; @@ -107,11 +107,13 @@ typedef struct _mod_network_socket_obj_t { #endif } mod_network_socket_obj_t; -#endif // MICROPY_PY_LWIP +#endif // MICROPY_PY_LWIP / MICROPY_PORT_NETWORK_INTERFACES +#ifdef MICROPY_PORT_NETWORK_INTERFACES void mod_network_init(void); void mod_network_deinit(void); void mod_network_register_nic(mp_obj_t nic); mp_obj_t mod_network_find_nic(const uint8_t *ip); +#endif #endif // MICROPY_INCLUDED_MODNETWORK_H diff --git a/extmod/network_lwip.c b/extmod/network_lwip.c new file mode 100644 index 0000000000..caa30f6fff --- /dev/null +++ b/extmod/network_lwip.c @@ -0,0 +1,95 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2014 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/mphal.h" + +#if MICROPY_PY_NETWORK && MICROPY_PY_LWIP + +#include "shared/netutils/netutils.h" +#include "extmod/modnetwork.h" + +#include "lwip/init.h" + +#if LWIP_VERSION_MAJOR >= 2 + +#include "lwip/netif.h" +#include "lwip/timeouts.h" +#include "lwip/dns.h" +#include "lwip/dhcp.h" + +// Implementations of network methods that can be used by any interface. + +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 // LWIP_VERSION_MAJOR >= 2 + +#endif // MICROPY_PY_NETWORK && MICROPY_PY_LWIP