Skip to content

Commit 17043f6

Browse files
dwmw2kuba2k2
andauthored
[beken-72xx] Add IPv6 and lwIP 2.2.0 support (#292)
* 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]>
1 parent e1c5761 commit 17043f6

File tree

8 files changed

+93
-6
lines changed

8 files changed

+93
-6
lines changed

cores/beken-72xx/arduino/libraries/WiFi/WiFiGeneric.cpp

+24-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,30 @@ IPAddress WiFiClass::hostByName(const char *hostname) {
8080
ip_addr_t ip;
8181
int ret = netconn_gethostbyname(hostname, &ip);
8282
if (ret == ERR_OK) {
83-
return ip.addr;
83+
#ifdef CONFIG_IPV6
84+
if (IP_IS_V6(&ip)) {
85+
ip6_addr_t *ip6 = ip_2_ip6(&ip);
86+
return IPAddress(
87+
IP6_ADDR_BLOCK1(ip6) >> 8,
88+
IP6_ADDR_BLOCK1(ip6) & 0xff,
89+
IP6_ADDR_BLOCK2(ip6) >> 8,
90+
IP6_ADDR_BLOCK2(ip6) & 0xff,
91+
IP6_ADDR_BLOCK3(ip6) >> 8,
92+
IP6_ADDR_BLOCK3(ip6) & 0xff,
93+
IP6_ADDR_BLOCK4(ip6) >> 8,
94+
IP6_ADDR_BLOCK4(ip6) & 0xff,
95+
IP6_ADDR_BLOCK5(ip6) >> 8,
96+
IP6_ADDR_BLOCK5(ip6) & 0xff,
97+
IP6_ADDR_BLOCK6(ip6) >> 8,
98+
IP6_ADDR_BLOCK6(ip6) & 0xff,
99+
IP6_ADDR_BLOCK7(ip6) >> 8,
100+
IP6_ADDR_BLOCK7(ip6) & 0xff,
101+
IP6_ADDR_BLOCK8(ip6) >> 8,
102+
IP6_ADDR_BLOCK8(ip6) & 0xff
103+
);
104+
}
105+
#endif
106+
return IPAddress(ip_addr_get_ip4_u32(&ip));
84107
}
85108
return IPAddress();
86109
}

cores/beken-72xx/arduino/libraries/WiFi/WiFiSTA.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,41 @@ WiFiAuthMode WiFiClass::getEncryption() {
246246
STA_GET_LINK_STATUS_RETURN(WIFI_AUTH_INVALID);
247247
return securityTypeToAuthMode(LINK_STATUS.security);
248248
}
249+
#ifdef CONFIG_IPV6
250+
bool WiFiClass::enableIpV6() {
251+
return true;
252+
}
253+
254+
IPv6Address WiFiClass::localIPv6() {
255+
struct netif *ifs = (struct netif *)net_get_sta_handle();
256+
std::vector<IPv6Address> result;
257+
struct wlan_ip_config addr;
258+
int nr_addresses = 0;
259+
260+
if (sta_ip_is_start())
261+
nr_addresses = net_get_if_ipv6_pref_addr(&addr, ifs);
262+
263+
for (int i = 0; i < nr_addresses; i++) {
264+
if (ip6_addr_islinklocal(&addr.ipv6[i]))
265+
return IPv6Address(addr.ipv6[i].addr);
266+
}
267+
268+
return IPv6Address();
269+
}
270+
271+
std::vector<IPv6Address> WiFiClass::allLocalIPv6() {
272+
struct netif *ifs = (struct netif *)net_get_sta_handle();
273+
std::vector<IPv6Address> result;
274+
struct wlan_ip_config addr;
275+
int nr_addresses = 0;
276+
277+
if (sta_ip_is_start())
278+
nr_addresses = net_get_if_ipv6_pref_addr(&addr, ifs);
279+
280+
for (int i = 0; i < nr_addresses; i++) {
281+
result.push_back(IPv6Address(addr.ipv6[i].addr));
282+
}
283+
284+
return result;
285+
}
286+
#endif

cores/beken-72xx/base/config/lwipopts.h

+2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
#define LWIP_NETIF_EXT_STATUS_CALLBACK 1
1111
#define MEMP_NUM_UDP_PCB (MAX_SOCKETS_UDP + 2 + 1)
1212

13+
#ifndef CONFIG_IPV6
1314
#define ip_addr ip4_addr
1415
#define ip_addr_t ip4_addr_t
16+
#endif
1517

1618
// increase TCP/IP thread stack size (was 512)
1719
#undef TCPIP_THREAD_STACKSIZE

cores/common/arduino/libraries/api/WiFi/WiFi.h

+2
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ class WiFiClass {
129129
uint8_t subnetCIDR();
130130
bool enableIpV6();
131131
IPv6Address localIPv6();
132+
std::vector<IPv6Address> allLocalIPv6();
133+
132134
const char *getHostname();
133135
bool setHostname(const char *hostname);
134136
bool setMacAddress(const uint8_t *mac);

cores/common/arduino/libraries/api/WiFi/WiFiSTA.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,9 @@ __attribute__((weak)) bool WiFiClass::enableIpV6() {
4444
}
4545

4646
__attribute__((weak)) IPv6Address WiFiClass::localIPv6() {
47-
return IPv6Address();
47+
return {};
48+
}
49+
50+
__attribute__((weak)) std::vector<IPv6Address> WiFiClass::allLocalIPv6() {
51+
return {};
4852
}

cores/common/arduino/libraries/common/mDNS/LwIPmDNS.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,15 @@ static void mdnsTxtCallback(struct mdns_service *service, void *userdata) {
7373
}
7474
}
7575

76+
#if LWIP_VERSION_SIMPLE < 20200 // TTL removed in LwIP commit 62fb2fd749b (2.2.0 release)
7677
static void mdnsStatusCallback(struct netif *netif, uint8_t result) {
7778
LT_DM(MDNS, "Status: netif %u, status %u", netif->num, result);
7879
}
80+
#else
81+
static void mdnsStatusCallback(struct netif *netif, uint8_t result, int8_t slot) {
82+
LT_DM(MDNS, "Status: netif %u, status %u slot %d", netif->num, result, slot);
83+
}
84+
#endif
7985

8086
#ifdef LWIP_NETIF_EXT_STATUS_CALLBACK
8187
static void addServices(struct netif *netif) {
@@ -95,7 +101,9 @@ static void addServices(struct netif *netif) {
95101
services[i],
96102
(mdns_sd_proto)protos[i],
97103
ports[i],
104+
#if LWIP_VERSION_SIMPLE < 20200 // TTL removed in LwIP commit 62fb2fd749b (2.2.0 release)
98105
255,
106+
#endif
99107
mdnsTxtCallback,
100108
reinterpret_cast<void *>(i) // index of newly added service
101109
);
@@ -111,7 +119,11 @@ static bool enableMDNS(struct netif *netif) {
111119
igmp_start(netif);
112120
LT_DM(MDNS, "Added IGMP to netif %u", netif->num);
113121
}
122+
#if LWIP_VERSION_SIMPLE < 20200 // TTL removed in LwIP commit 62fb2fd749b (2.2.0 release)
114123
err_t ret = mdns_resp_add_netif(netif, hostName, 255);
124+
#else
125+
err_t ret = mdns_resp_add_netif(netif, hostName);
126+
#endif
115127
if (ret == ERR_OK) {
116128
LT_DM(MDNS, "mDNS started on netif %u, announcing it to network", netif->num);
117129
#if LWIP_VERSION_SIMPLE >= 20100
@@ -190,7 +202,9 @@ bool mDNS::addServiceImpl(const char *name, const char *service, uint8_t proto,
190202
service,
191203
(mdns_sd_proto)proto,
192204
port,
205+
#if LWIP_VERSION_SIMPLE < 20200 // TTL removed in LwIP commit 62fb2fd749b (2.2.0 release)
193206
255,
207+
#endif
194208
mdnsTxtCallback,
195209
(void *)services.size() // index of newly added service
196210
);

external-libs.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,10 @@
9090
"+<src/api/*.c>",
9191
"+<src/core/*.c>",
9292
"+<src/core/ipv4/*.c>",
93+
"+<src/core/ipv6/*.c>",
9394
"+<src/netif/ethernet.c>",
9495
"+<src/netif/etharp.c>",
95-
"+<src/apps/mdns/mdns.c>",
96+
"+<src/apps/mdns/*.c>",
9697
"+<src/apps/sntp/sntp.c>",
9798
"+<port/realtek/freertos/ethernetif.c>",
9899
"+<port/realtek/freertos/sys_arch.c>"
@@ -110,9 +111,10 @@
110111
"+<src/api/*.c>",
111112
"+<src/core/*.c>",
112113
"+<src/core/ipv4/*.c>",
114+
"+<src/core/ipv6/*.c>",
113115
"+<src/netif/ethernet.c>",
114116
"+<src/netif/etharp.c>",
115-
"+<src/apps/mdns/mdns.c>",
117+
"+<src/apps/mdns/*.c>",
116118
"+<src/apps/sntp/sntp.c>",
117119
"+<port/*.c>"
118120
],
@@ -128,9 +130,10 @@
128130
"+<src/api/*.c>",
129131
"+<src/core/*.c>",
130132
"+<src/core/ipv4/*.c>",
133+
"+<src/core/ipv6/*.c>",
131134
"+<src/netif/ethernet.c>",
132135
"+<src/netif/etharp.c>",
133-
"+<src/apps/mdns/mdns.c>",
136+
"+<src/apps/mdns/*.c>",
134137
"+<src/apps/sntp/sntp.c>",
135138
"+<port/realtek/freertos/ethernetif.c>",
136139
"+<port/realtek/freertos/sys_arch.c>"

platform.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@
6767
"2.0.2": "2.0.2-bdk",
6868
"2.1.0": "2.1.0-bdk",
6969
"2.1.3": "2.1.3-bdk",
70-
"default": "2.1.3-bdk"
70+
"2.2.0": "2.2.0-bdk",
71+
"default": "2.2.0-bdk"
7172
}
7273
}
7374
},

0 commit comments

Comments
 (0)