Skip to content

Commit

Permalink
[beken-72xx] Add IPv6 and lwIP 2.2.0 support (#292)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
dwmw2 and kuba2k2 authored Sep 5, 2024
1 parent e1c5761 commit 17043f6
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 6 deletions.
25 changes: 24 additions & 1 deletion cores/beken-72xx/arduino/libraries/WiFi/WiFiGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
38 changes: 38 additions & 0 deletions cores/beken-72xx/arduino/libraries/WiFi/WiFiSTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<IPv6Address> 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<IPv6Address> WiFiClass::allLocalIPv6() {
struct netif *ifs = (struct netif *)net_get_sta_handle();
std::vector<IPv6Address> 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
2 changes: 2 additions & 0 deletions cores/beken-72xx/base/config/lwipopts.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions cores/common/arduino/libraries/api/WiFi/WiFi.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ class WiFiClass {
uint8_t subnetCIDR();
bool enableIpV6();
IPv6Address localIPv6();
std::vector<IPv6Address> allLocalIPv6();

const char *getHostname();
bool setHostname(const char *hostname);
bool setMacAddress(const uint8_t *mac);
Expand Down
6 changes: 5 additions & 1 deletion cores/common/arduino/libraries/api/WiFi/WiFiSTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,9 @@ __attribute__((weak)) bool WiFiClass::enableIpV6() {
}

__attribute__((weak)) IPv6Address WiFiClass::localIPv6() {
return IPv6Address();
return {};
}

__attribute__((weak)) std::vector<IPv6Address> WiFiClass::allLocalIPv6() {
return {};
}
14 changes: 14 additions & 0 deletions cores/common/arduino/libraries/common/mDNS/LwIPmDNS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<void *>(i) // index of newly added service
);
Expand All @@ -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
Expand Down Expand Up @@ -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
);
Expand Down
9 changes: 6 additions & 3 deletions external-libs.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@
"+<src/api/*.c>",
"+<src/core/*.c>",
"+<src/core/ipv4/*.c>",
"+<src/core/ipv6/*.c>",
"+<src/netif/ethernet.c>",
"+<src/netif/etharp.c>",
"+<src/apps/mdns/mdns.c>",
"+<src/apps/mdns/*.c>",
"+<src/apps/sntp/sntp.c>",
"+<port/realtek/freertos/ethernetif.c>",
"+<port/realtek/freertos/sys_arch.c>"
Expand All @@ -110,9 +111,10 @@
"+<src/api/*.c>",
"+<src/core/*.c>",
"+<src/core/ipv4/*.c>",
"+<src/core/ipv6/*.c>",
"+<src/netif/ethernet.c>",
"+<src/netif/etharp.c>",
"+<src/apps/mdns/mdns.c>",
"+<src/apps/mdns/*.c>",
"+<src/apps/sntp/sntp.c>",
"+<port/*.c>"
],
Expand All @@ -128,9 +130,10 @@
"+<src/api/*.c>",
"+<src/core/*.c>",
"+<src/core/ipv4/*.c>",
"+<src/core/ipv6/*.c>",
"+<src/netif/ethernet.c>",
"+<src/netif/etharp.c>",
"+<src/apps/mdns/mdns.c>",
"+<src/apps/mdns/*.c>",
"+<src/apps/sntp/sntp.c>",
"+<port/realtek/freertos/ethernetif.c>",
"+<port/realtek/freertos/sys_arch.c>"
Expand Down
3 changes: 2 additions & 1 deletion platform.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
},
Expand Down

0 comments on commit 17043f6

Please sign in to comment.