Files
libretiny/cores/beken-72xx/arduino/libraries/WiFi/WiFiGeneric.cpp
David Woodhouse 17043f634f [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 <kuba@szczodrzynski.pl>
2024-09-05 14:46:13 +02:00

127 lines
3.3 KiB
C++

/* Copyright (c) Kuba Szczodrzyński 2022-06-26. */
#include "WiFiPrivate.h"
bool WiFiClass::modePriv(WiFiMode mode, WiFiModeAction sta, WiFiModeAction ap) {
__wrap_bk_printf_disable();
startWifiTask();
if (!__bk_rf_is_init) {
LT_DM(WIFI, "Initializing func&app");
func_init_extended();
app_pre_start();
// wait for the init_thread to finish its job
while (xTaskGetHandle("init_thread")) {
LT_VM(WIFI, "Waiting for init_thread");
delay(10);
}
LT_DM(WIFI, "Init OK");
__bk_rf_is_init = true;
}
LT_HEAP_I();
if (mode) {
LT_DM(WIFI, "Wakeup RF");
uint32_t reg = 1; // this is only checked for being true-ish
sddev_control(SCTRL_DEV_NAME, CMD_RF_HOLD_BIT_SET, &reg);
}
if (sta == WLMODE_ENABLE) {
LT_DM(WIFI, "Enabling STA");
bk_wlan_sta_init(NULL);
#if CFG_WPA_CTRL_IFACE
wlan_sta_enable();
#endif
wifiEventSendArduino(ARDUINO_EVENT_WIFI_STA_START);
} else if (sta == WLMODE_DISABLE) {
LT_DM(WIFI, "Disabling STA");
bk_wlan_stop(BK_STATION);
wifiEventSendArduino(ARDUINO_EVENT_WIFI_STA_STOP);
}
LT_HEAP_I();
if (ap == WLMODE_ENABLE) {
LT_DM(WIFI, "Enabling AP");
// fake it - on BK7231, enabling the AP without starting it breaks all connection attempts
DATA->apEnabled = true;
wifiEventSendArduino(ARDUINO_EVENT_WIFI_AP_START);
} else if (ap == WLMODE_DISABLE) {
LT_DM(WIFI, "Disabling AP");
bk_wlan_stop(BK_SOFT_AP);
DATA->apEnabled = false;
wifiEventSendArduino(ARDUINO_EVENT_WIFI_AP_STOP);
}
// force checking actual mode again
mode = getMode();
LT_HEAP_I();
__wrap_bk_printf_enable();
return true;
}
WiFiMode WiFiClass::getMode() {
uint8_t sta = !!bk_wlan_has_role(VIF_STA) * WIFI_MODE_STA;
uint8_t ap = DATA->apEnabled * WIFI_MODE_AP; // report the faked value
return (WiFiMode)(sta | ap);
}
WiFiStatus WiFiClass::status() {
rw_evt_type status = DATA->lastStaEvent;
if (status == RW_EVT_STA_CONNECTED && STA_CFG.dhcp_mode == DHCP_DISABLE)
status = RW_EVT_STA_GOT_IP;
return eventTypeToStatus(status);
}
IPAddress WiFiClass::hostByName(const char *hostname) {
ip_addr_t ip;
int ret = netconn_gethostbyname(hostname, &ip);
if (ret == ERR_OK) {
#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();
}
bool WiFiClass::setSleep(bool enable) {
LT_DM(WIFI, "WiFi sleep mode %u", enable);
if (enable) {
// Replicating OpenBeken PowerSave feature
// https://github.com/openshwprojects/OpenBK7231T_App/blob/567c5756b489f0670988fad1c2742a19f0f217ea/src/cmnds/cmd_main.c#L58
bk_wlan_power_save_set_level((BK_PS_LEVEL)(PS_RF_SLEEP_BIT | PS_MCU_SLEEP_BIT));
} else {
bk_wlan_power_save_set_level((BK_PS_LEVEL)0);
}
DATA->sleep = enable;
return true;
}
bool WiFiClass::getSleep() {
return DATA->sleep;
}