From 17043f634fdf9e9922c7a3cac82ca78735ebc5e1 Mon Sep 17 00:00:00 2001 From: David Woodhouse Date: Thu, 5 Sep 2024 13:46:13 +0100 Subject: [PATCH] [beken-72xx] Add IPv6 and lwIP 2.2.0 support (#292) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * mDNS: Fix build against LwIP 2.2.0 * Stop defining ip_addr_t when !CONFIG_IPV6 The only reason we had to do this is because we forgot to define LWIP_IPV4, which is fixed in our LwIP port now, but keep it around for !CONFIG_IPV6 for now for builds against older LwIP. * Allow returning IPv6 results from WiFiClass::hostByName() * Add ipv6 and extra mDNS files for LwIP 2.2.0 * Add IPv6 support to BK72xx WifiSTA Add an allLocalIPv6() method to return a *vector* of addresses, rather than just one. It's not clear where the enableIpV6() and localIPv6() methods came from; they don't seem to be part of the standard Arduino class. Eventually at least for ESPHome, I'd like to stop using these classes and just let the ESPHome wifi component talk directly to LwIP. Or maybe LibreTiny should offer an API compatible with the esp-idf one which is a light wrapper around LwIP. But short of a major refactor, this seems like a reasonable option. * Update LwIP default to 2.2.0 * Apply suggestions from code review --------- Co-authored-by: Kuba SzczodrzyƄski --- .../arduino/libraries/WiFi/WiFiGeneric.cpp | 25 +++++++++++- .../arduino/libraries/WiFi/WiFiSTA.cpp | 38 +++++++++++++++++++ cores/beken-72xx/base/config/lwipopts.h | 2 + .../common/arduino/libraries/api/WiFi/WiFi.h | 2 + .../arduino/libraries/api/WiFi/WiFiSTA.cpp | 6 ++- .../libraries/common/mDNS/LwIPmDNS.cpp | 14 +++++++ external-libs.json | 9 +++-- platform.json | 3 +- 8 files changed, 93 insertions(+), 6 deletions(-) diff --git a/cores/beken-72xx/arduino/libraries/WiFi/WiFiGeneric.cpp b/cores/beken-72xx/arduino/libraries/WiFi/WiFiGeneric.cpp index 1374c76..42b4737 100644 --- a/cores/beken-72xx/arduino/libraries/WiFi/WiFiGeneric.cpp +++ b/cores/beken-72xx/arduino/libraries/WiFi/WiFiGeneric.cpp @@ -80,7 +80,30 @@ IPAddress WiFiClass::hostByName(const char *hostname) { ip_addr_t ip; int ret = netconn_gethostbyname(hostname, &ip); if (ret == ERR_OK) { - return ip.addr; +#ifdef CONFIG_IPV6 + if (IP_IS_V6(&ip)) { + ip6_addr_t *ip6 = ip_2_ip6(&ip); + return IPAddress( + IP6_ADDR_BLOCK1(ip6) >> 8, + IP6_ADDR_BLOCK1(ip6) & 0xff, + IP6_ADDR_BLOCK2(ip6) >> 8, + IP6_ADDR_BLOCK2(ip6) & 0xff, + IP6_ADDR_BLOCK3(ip6) >> 8, + IP6_ADDR_BLOCK3(ip6) & 0xff, + IP6_ADDR_BLOCK4(ip6) >> 8, + IP6_ADDR_BLOCK4(ip6) & 0xff, + IP6_ADDR_BLOCK5(ip6) >> 8, + IP6_ADDR_BLOCK5(ip6) & 0xff, + IP6_ADDR_BLOCK6(ip6) >> 8, + IP6_ADDR_BLOCK6(ip6) & 0xff, + IP6_ADDR_BLOCK7(ip6) >> 8, + IP6_ADDR_BLOCK7(ip6) & 0xff, + IP6_ADDR_BLOCK8(ip6) >> 8, + IP6_ADDR_BLOCK8(ip6) & 0xff + ); + } +#endif + return IPAddress(ip_addr_get_ip4_u32(&ip)); } return IPAddress(); } diff --git a/cores/beken-72xx/arduino/libraries/WiFi/WiFiSTA.cpp b/cores/beken-72xx/arduino/libraries/WiFi/WiFiSTA.cpp index a5558d1..ee24e17 100644 --- a/cores/beken-72xx/arduino/libraries/WiFi/WiFiSTA.cpp +++ b/cores/beken-72xx/arduino/libraries/WiFi/WiFiSTA.cpp @@ -246,3 +246,41 @@ WiFiAuthMode WiFiClass::getEncryption() { STA_GET_LINK_STATUS_RETURN(WIFI_AUTH_INVALID); return securityTypeToAuthMode(LINK_STATUS.security); } +#ifdef CONFIG_IPV6 +bool WiFiClass::enableIpV6() { + return true; +} + +IPv6Address WiFiClass::localIPv6() { + struct netif *ifs = (struct netif *)net_get_sta_handle(); + std::vector result; + struct wlan_ip_config addr; + int nr_addresses = 0; + + if (sta_ip_is_start()) + nr_addresses = net_get_if_ipv6_pref_addr(&addr, ifs); + + for (int i = 0; i < nr_addresses; i++) { + if (ip6_addr_islinklocal(&addr.ipv6[i])) + return IPv6Address(addr.ipv6[i].addr); + } + + return IPv6Address(); +} + +std::vector WiFiClass::allLocalIPv6() { + struct netif *ifs = (struct netif *)net_get_sta_handle(); + std::vector result; + struct wlan_ip_config addr; + int nr_addresses = 0; + + if (sta_ip_is_start()) + nr_addresses = net_get_if_ipv6_pref_addr(&addr, ifs); + + for (int i = 0; i < nr_addresses; i++) { + result.push_back(IPv6Address(addr.ipv6[i].addr)); + } + + return result; +} +#endif diff --git a/cores/beken-72xx/base/config/lwipopts.h b/cores/beken-72xx/base/config/lwipopts.h index 07408f1..728c2a8 100644 --- a/cores/beken-72xx/base/config/lwipopts.h +++ b/cores/beken-72xx/base/config/lwipopts.h @@ -10,8 +10,10 @@ #define LWIP_NETIF_EXT_STATUS_CALLBACK 1 #define MEMP_NUM_UDP_PCB (MAX_SOCKETS_UDP + 2 + 1) +#ifndef CONFIG_IPV6 #define ip_addr ip4_addr #define ip_addr_t ip4_addr_t +#endif // increase TCP/IP thread stack size (was 512) #undef TCPIP_THREAD_STACKSIZE diff --git a/cores/common/arduino/libraries/api/WiFi/WiFi.h b/cores/common/arduino/libraries/api/WiFi/WiFi.h index 92e0266..f777973 100644 --- a/cores/common/arduino/libraries/api/WiFi/WiFi.h +++ b/cores/common/arduino/libraries/api/WiFi/WiFi.h @@ -129,6 +129,8 @@ class WiFiClass { uint8_t subnetCIDR(); bool enableIpV6(); IPv6Address localIPv6(); + std::vector allLocalIPv6(); + const char *getHostname(); bool setHostname(const char *hostname); bool setMacAddress(const uint8_t *mac); diff --git a/cores/common/arduino/libraries/api/WiFi/WiFiSTA.cpp b/cores/common/arduino/libraries/api/WiFi/WiFiSTA.cpp index 684cc63..9fcfe59 100644 --- a/cores/common/arduino/libraries/api/WiFi/WiFiSTA.cpp +++ b/cores/common/arduino/libraries/api/WiFi/WiFiSTA.cpp @@ -44,5 +44,9 @@ __attribute__((weak)) bool WiFiClass::enableIpV6() { } __attribute__((weak)) IPv6Address WiFiClass::localIPv6() { - return IPv6Address(); + return {}; +} + +__attribute__((weak)) std::vector WiFiClass::allLocalIPv6() { + return {}; } diff --git a/cores/common/arduino/libraries/common/mDNS/LwIPmDNS.cpp b/cores/common/arduino/libraries/common/mDNS/LwIPmDNS.cpp index 0b8c435..6b0a476 100644 --- a/cores/common/arduino/libraries/common/mDNS/LwIPmDNS.cpp +++ b/cores/common/arduino/libraries/common/mDNS/LwIPmDNS.cpp @@ -73,9 +73,15 @@ static void mdnsTxtCallback(struct mdns_service *service, void *userdata) { } } +#if LWIP_VERSION_SIMPLE < 20200 // TTL removed in LwIP commit 62fb2fd749b (2.2.0 release) static void mdnsStatusCallback(struct netif *netif, uint8_t result) { LT_DM(MDNS, "Status: netif %u, status %u", netif->num, result); } +#else +static void mdnsStatusCallback(struct netif *netif, uint8_t result, int8_t slot) { + LT_DM(MDNS, "Status: netif %u, status %u slot %d", netif->num, result, slot); +} +#endif #ifdef LWIP_NETIF_EXT_STATUS_CALLBACK static void addServices(struct netif *netif) { @@ -95,7 +101,9 @@ static void addServices(struct netif *netif) { services[i], (mdns_sd_proto)protos[i], ports[i], +#if LWIP_VERSION_SIMPLE < 20200 // TTL removed in LwIP commit 62fb2fd749b (2.2.0 release) 255, +#endif mdnsTxtCallback, reinterpret_cast(i) // index of newly added service ); @@ -111,7 +119,11 @@ static bool enableMDNS(struct netif *netif) { igmp_start(netif); LT_DM(MDNS, "Added IGMP to netif %u", netif->num); } +#if LWIP_VERSION_SIMPLE < 20200 // TTL removed in LwIP commit 62fb2fd749b (2.2.0 release) err_t ret = mdns_resp_add_netif(netif, hostName, 255); +#else + err_t ret = mdns_resp_add_netif(netif, hostName); +#endif if (ret == ERR_OK) { LT_DM(MDNS, "mDNS started on netif %u, announcing it to network", netif->num); #if LWIP_VERSION_SIMPLE >= 20100 @@ -190,7 +202,9 @@ bool mDNS::addServiceImpl(const char *name, const char *service, uint8_t proto, service, (mdns_sd_proto)proto, port, +#if LWIP_VERSION_SIMPLE < 20200 // TTL removed in LwIP commit 62fb2fd749b (2.2.0 release) 255, +#endif mdnsTxtCallback, (void *)services.size() // index of newly added service ); diff --git a/external-libs.json b/external-libs.json index 264e7ba..9df4631 100644 --- a/external-libs.json +++ b/external-libs.json @@ -90,9 +90,10 @@ "+", "+", "+", + "+", "+", "+", - "+", + "+", "+", "+", "+" @@ -110,9 +111,10 @@ "+", "+", "+", + "+", "+", "+", - "+", + "+", "+", "+" ], @@ -128,9 +130,10 @@ "+", "+", "+", + "+", "+", "+", - "+", + "+", "+", "+", "+" diff --git a/platform.json b/platform.json index f8b81ba..6dbf350 100644 --- a/platform.json +++ b/platform.json @@ -67,7 +67,8 @@ "2.0.2": "2.0.2-bdk", "2.1.0": "2.1.0-bdk", "2.1.3": "2.1.3-bdk", - "default": "2.1.3-bdk" + "2.2.0": "2.2.0-bdk", + "default": "2.2.0-bdk" } } },