diff options
author | Jim Mussared <jim.mussared@gmail.com> | 2023-02-01 14:19:45 +1100 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2023-03-01 01:26:44 +1100 |
commit | f78464c12ba97688e3d234d88362a90e38892f7c (patch) | |
tree | 7d5b7fbd5a63a4b020558236c125b768862961e2 /extmod/network_lwip.c | |
parent | a3773026238c7c779cad79aa9d9c468e680e7004 (diff) | |
download | micropython-f78464c12ba97688e3d234d88362a90e38892f7c.tar.gz micropython-f78464c12ba97688e3d234d88362a90e38892f7c.zip |
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>
Diffstat (limited to 'extmod/network_lwip.c')
-rw-r--r-- | extmod/network_lwip.c | 95 |
1 files changed, 95 insertions, 0 deletions
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 |