[realtek-ambz] Fix WiFi AP mode and DNS

This commit is contained in:
Kuba Szczodrzyński
2023-03-18 13:27:16 +01:00
parent 250e67ab1f
commit 201db4668e
9 changed files with 305 additions and 129 deletions

View File

@@ -71,6 +71,12 @@ class WiFiClass {
static uint8_t calculateSubnetCIDR(IPAddress subnetMask); static uint8_t calculateSubnetCIDR(IPAddress subnetMask);
static String macToString(uint8_t *mac); static String macToString(uint8_t *mac);
static void resetNetworkInfo(WiFiNetworkInfo &info);
private: /* WiFiGeneric.cpp */
bool restoreSTAConfig(const WiFiNetworkInfo &info);
bool restoreAPConfig(const WiFiNetworkInfo &info);
protected: /* WiFiEvents.cpp */ protected: /* WiFiEvents.cpp */
static std::vector<EventHandler> handlers; static std::vector<EventHandler> handlers;

View File

@@ -119,3 +119,36 @@ String WiFiClass::macToString(uint8_t *mac) {
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return macStr; return macStr;
} }
void WiFiClass::resetNetworkInfo(WiFiNetworkInfo &info) {
LT_VM(WIFI, "Resetting network info: %s", info.ssid);
free(info.ssid);
free(info.password);
free(info.bssid);
// wipe the structure, except IP addresses
memset(&info, 0x00, sizeof(WiFiNetworkInfo) - 5 * sizeof(uint32_t));
}
bool WiFiClass::restoreSTAConfig(const WiFiNetworkInfo &info) {
LT_DM(WIFI, "Restoring %s config: %s", "STA", info.ssid);
if (!info.ssid)
return false;
if (info.localIP) {
LT_DM(WIFI, "Restoring STA IP config");
if (!config(info.localIP, info.gateway, info.subnet, info.dns1, info.dns2))
return false;
}
return begin(info.ssid, info.password, info.channel, info.bssid);
}
bool WiFiClass::restoreAPConfig(const WiFiNetworkInfo &info) {
LT_DM(WIFI, "Restoring %s config: %s", "AP", info.ssid);
if (!info.ssid)
return false;
if (info.localIP) {
LT_DM(WIFI, "Restoring AP IP config");
if (!softAPConfig(info.localIP, info.gateway, info.subnet))
return false;
}
return softAP(info.ssid, info.password, info.channel, info.ssidHidden);
}

View File

@@ -128,6 +128,20 @@ typedef enum {
WIFI_REASON_ROAMING = 207, WIFI_REASON_ROAMING = 207,
} wifi_err_reason_t; } wifi_err_reason_t;
typedef struct {
char *ssid;
char *password;
uint8_t *bssid;
bool ssidHidden;
int channel;
int auth;
uint32_t localIP;
uint32_t subnet;
uint32_t gateway;
uint32_t dns1;
uint32_t dns2;
} WiFiNetworkInfo;
typedef struct { typedef struct {
char *ssid; char *ssid;
WiFiAuthMode auth; WiFiAuthMode auth;

View File

@@ -2,28 +2,7 @@
#include "WiFiPrivate.h" #include "WiFiPrivate.h"
// TODO move these to WiFiData
rtw_network_info_t wifi = {0};
rtw_ap_info_t ap = {0};
rtw_wifi_setting_t wifi_setting; rtw_wifi_setting_t wifi_setting;
unsigned char sta_password[65] = {0};
unsigned char ap_password[65] = {0};
void reset_wifi_struct(void) {
memset(wifi.ssid.val, 0, sizeof(wifi.ssid.val));
memset(wifi.bssid.octet, 0, ETH_ALEN);
memset(sta_password, 0, sizeof(sta_password));
memset(ap_password, 0, sizeof(ap_password));
wifi.ssid.len = 0;
wifi.password = NULL;
wifi.password_len = 0;
wifi.key_id = -1;
memset(ap.ssid.val, 0, sizeof(ap.ssid.val));
ap.ssid.len = 0;
ap.password = NULL;
ap.password_len = 0;
ap.channel = 1;
}
WiFiClass::WiFiClass() { WiFiClass::WiFiClass() {
data = (WiFiData *)calloc(1, sizeof(WiFiData)); data = (WiFiData *)calloc(1, sizeof(WiFiData));

View File

@@ -14,22 +14,23 @@ bool WiFiClass::softAP(const char *ssid, const char *passphrase, int channel, bo
return WL_CONNECT_FAILED; return WL_CONNECT_FAILED;
LT_HEAP_I(); LT_HEAP_I();
vTaskDelay(20); vTaskDelay(20);
strcpy((char *)ap.ssid.val, ssid); WiFiNetworkInfo &info = DATA->ap;
ap.ssid.len = strlen(ssid); if (info.ssid != ssid)
ap.channel = channel; // free network info, if not called from restoreAPConfig()
resetNetworkInfo(info);
ap.security_type = RTW_SECURITY_OPEN; if (info.ssid != ssid)
ap.password = NULL; info.ssid = strdup(ssid);
ap.password_len = 0; info.ssidHidden = ssidHidden;
info.channel = channel;
info.auth = RTW_SECURITY_OPEN;
if (passphrase) { if (passphrase) {
strcpy((char *)ap_password, passphrase); if (info.password != passphrase)
ap.security_type = RTW_SECURITY_WPA2_AES_PSK; info.password = strdup(passphrase);
ap.password = ap_password; info.auth = RTW_SECURITY_WPA2_AES_PSK;
ap.password_len = strlen(passphrase);
} }
dhcps_deinit(); dhcps_deinit();
@@ -39,26 +40,24 @@ bool WiFiClass::softAP(const char *ssid, const char *passphrase, int channel, bo
int ret; int ret;
if (!ssidHidden) { if (!ssidHidden) {
ret = wifi_start_ap( ret = wifi_start_ap(
(char *)ap.ssid.val, info.ssid,
ap.security_type, (rtw_security_t)info.auth,
(char *)ap.password, info.password,
ap.ssid.len, strlen(info.ssid),
ap.password_len, strlen(info.password),
ap.channel info.channel
); );
} else { } else {
ret = wifi_start_ap_with_hidden_ssid( ret = wifi_start_ap_with_hidden_ssid(
(char *)ap.ssid.val, info.ssid,
ap.security_type, (rtw_security_t)info.auth,
(char *)ap.password, info.password,
ap.ssid.len, strlen(info.ssid),
ap.password_len, strlen(info.password),
ap.channel info.channel
); );
} }
wifi_indication(WIFI_EVENT_CONNECT, NULL, ARDUINO_EVENT_WIFI_AP_START, -2);
if (ret < 0) { if (ret < 0) {
LT_EM(WIFI, "SoftAP failed; ret=%d", ret); LT_EM(WIFI, "SoftAP failed; ret=%d", ret);
return false; return false;
@@ -72,7 +71,7 @@ bool WiFiClass::softAP(const char *ssid, const char *passphrase, int channel, bo
while (1) { while (1) {
if (wext_get_ssid(ifname, essid) > 0) { if (wext_get_ssid(ifname, essid) > 0) {
if (strcmp((const char *)essid, (const char *)ap.ssid.val) == 0) if (strcmp((const char *)essid, info.ssid) == 0)
break; break;
} }
@@ -83,27 +82,28 @@ bool WiFiClass::softAP(const char *ssid, const char *passphrase, int channel, bo
timeout--; timeout--;
} }
wifi_indication(WIFI_EVENT_CONNECT, NULL, ARDUINO_EVENT_WIFI_AP_START, -2);
dhcps_init(ifs); dhcps_init(ifs);
dns_server_deinit();
return true; return true;
} }
bool WiFiClass::softAPConfig(IPAddress localIP, IPAddress gateway, IPAddress subnet) { bool WiFiClass::softAPConfig(IPAddress localIP, IPAddress gateway, IPAddress subnet) {
if (!enableAP(true)) if (!enableAP(true))
return false; return false;
struct netif *ifs = NETIF_RTW_AP; WiFiNetworkInfo &info = DATA->ap;
struct netif *ifs = NETIF_RTW_AP;
struct ip_addr ipaddr, netmask, gw; struct ip_addr ipaddr, netmask, gw;
ipaddr.addr = localIP; ipaddr.addr = info.localIP = localIP;
netmask.addr = subnet; netmask.addr = info.subnet = subnet;
gw.addr = gateway; gw.addr = info.gateway = gateway;
netif_set_addr(ifs, &ipaddr, &netmask, &gw); netif_set_addr(ifs, &ipaddr, &netmask, &gw);
return true; return true;
} }
bool WiFiClass::softAPdisconnect(bool wifiOff) { bool WiFiClass::softAPdisconnect(bool wifiOff) {
// TODO implement wifi_restart_ap return enableAP(false);
if (wifiOff)
return enableAP(false);
return true;
} }
uint8_t WiFiClass::softAPgetStationNum() { uint8_t WiFiClass::softAPgetStationNum() {
@@ -115,11 +115,11 @@ uint8_t WiFiClass::softAPgetStationNum() {
} }
IPAddress WiFiClass::softAPIP() { IPAddress WiFiClass::softAPIP() {
return LwIP_GetIP(NETIF_RTW_AP); return netif_ip_addr4(NETIF_RTW_AP)->addr;
} }
IPAddress WiFiClass::softAPSubnetMask() { IPAddress WiFiClass::softAPSubnetMask() {
return LwIP_GetMASK(NETIF_RTW_AP); return netif_ip_netmask4(NETIF_RTW_AP)->addr;
} }
const char *WiFiClass::softAPgetHostname() { const char *WiFiClass::softAPgetHostname() {
@@ -132,19 +132,17 @@ bool WiFiClass::softAPsetHostname(const char *hostname) {
} }
uint8_t *WiFiClass::softAPmacAddress(uint8_t *mac) { uint8_t *WiFiClass::softAPmacAddress(uint8_t *mac) {
uint8_t *macLocal = LwIP_GetMAC(NETIF_RTW_AP); memcpy(mac, NETIF_RTW_AP->hwaddr, ETH_ALEN);
memcpy(mac, macLocal, ETH_ALEN);
free(macLocal);
return mac; return mac;
} }
String WiFiClass::softAPmacAddress(void) { String WiFiClass::softAPmacAddress(void) {
uint8_t mac[ETH_ALEN]; uint8_t mac[ETH_ALEN];
macAddress(mac); softAPmacAddress(mac);
return macToString(mac); return macToString(mac);
} }
const String WiFiClass::softAPSSID(void) { const String WiFiClass::softAPSSID(void) {
wifi_get_setting(NETNAME_AP, &wifi_setting); wext_get_ssid(NETNAME_AP, wifi_setting.ssid);
return (char *)wifi_setting.ssid; return (char *)wifi_setting.ssid;
} }

View File

@@ -17,23 +17,128 @@ bool WiFiClass::modePriv(WiFiMode mode, WiFiModeAction sta, WiFiModeAction ap) {
// initialize wifi first // initialize wifi first
LT_IM(WIFI, "Initializing LwIP"); LT_IM(WIFI, "Initializing LwIP");
LwIP_Init(); LwIP_Init();
reset_wifi_struct();
DATA->initialized = true; DATA->initialized = true;
} }
LT_HEAP_I(); LT_HEAP_I();
if (getMode()) { WiFiMode currentMode = getMode();
// stop wifi to change mode WiFiNetworkInfo &staInfo = DATA->sta;
LT_DM(WIFI, "Stopping WiFi to change mode"); WiFiNetworkInfo &apInfo = DATA->ap;
if (wifi_off() != RTW_SUCCESS) bool reenableSTA = false;
goto error; bool reenableAP = false;
vTaskDelay(20);
if (mode == WIFI_MODE_NULL) if (mode == WIFI_MODE_APSTA) {
goto error; if (currentMode == WIFI_MODE_STA) {
// adding AP mode doesn't seem to work fine:
// - STA -> AP+STA
LT_DM(WIFI, "STA was enabled: %s", staInfo.ssid);
reenableSTA = true;
}
if (currentMode == WIFI_MODE_AP) {
// restart AP mode later, as wifi has to be stopped first:
// - AP -> AP+STA
LT_DM(WIFI, "AP was enabled: %s", apInfo.ssid);
reenableAP = true;
}
} }
if (wifi_on((rtw_mode_t)mode) != RTW_SUCCESS) { if (reenableSTA || reenableAP || mode == WIFI_MODE_NULL || currentMode == WIFI_MODE_AP ||
LT_EM(WIFI, "Error while changing mode(%u)", mode); currentMode && mode == WIFI_MODE_AP) {
goto error; // must stop wifi first:
// - STA -> NULL
// - STA -> AP
// - STA -> AP+STA
// - AP -> NULL
// - AP -> STA
// - AP -> AP+STA
// - AP+STA -> NULL
// - AP+STA -> AP
LT_DM(WIFI, "Stopping WiFi to change mode");
if (wifi_off() != RTW_SUCCESS) {
LT_EM(WIFI, "Error while changing mode(%u)", mode);
goto error;
}
rltk_wlan_deinit_fastly();
rltk_wlan_rf_off();
init_event_callback_list();
vTaskDelay(20);
currentMode = getMode();
}
if (currentMode == WIFI_MODE_NULL && mode != WIFI_MODE_NULL) {
// wifi is not running, enable it the usual way:
// - NULL -> STA
// - NULL -> AP
// - NULL -> AP+STA
if (wifi_on((rtw_mode_t)mode) != RTW_SUCCESS) {
LT_EM(WIFI, "Error while changing mode(%u)", mode);
goto error;
}
} else {
// just enable/disable wlan1:
// - STA -> AP+STA - unused (wifi reset required)
// - AP+STA -> STA
wifi_mode = mode;
/* if (ap == WLMODE_ENABLE) {
LT_DM(WIFI, "Mode: %s ENABLE", WLAN1_NAME);
rltk_wlan_init(WLAN1_IDX, RTW_MODE_AP);
rltk_wlan_start(WLAN1_IDX);
uint32_t timeout = 20;
while (1) {
if (rltk_wlan_running(WLAN1_IDX)) {
wifi_set_country_code();
break;
}
if (timeout == 0) {
LT_EM(WIFI, "Error while changing mode(%u)", mode);
goto error;
}
vTaskDelay(1 * configTICK_RATE_HZ);
timeout--;
}
netif_set_up(WLAN1_NETIF);
netif_set_link_up(WLAN1_NETIF);
} */
if (ap == WLMODE_DISABLE) {
LT_DM(WIFI, "Mode: %s DISABLE", WLAN1_NAME);
netif_set_link_down(WLAN1_NETIF);
netif_set_down(WLAN1_NETIF);
rltk_stop_softap(WLAN1_NAME);
rltk_wlan_init(WLAN1_IDX, RTW_MODE_NONE);
wext_set_mode(WLAN1_NAME, IW_MODE_INFRA);
}
vTaskDelay(50);
}
if (mode & WIFI_MODE_AP) {
// indicate that the interface is an AP
// use NETNAME_AP to retrieve the actual iface name (wlan0/wlan1)
// (this is determined by STA bit being set in wifi_mode)
wext_set_mode(NETNAME_AP, IW_MODE_MASTER);
}
if (sta == WLMODE_DISABLE) {
// mark that STA mode has been disabled manually
free(staInfo.ssid);
staInfo.ssid = NULL;
}
if (ap == WLMODE_DISABLE) {
// mark that AP mode has been disabled manually
free(apInfo.ssid);
apInfo.ssid = NULL;
}
// force checking WiFi mode again (which will update wifi_mode)
getMode();
if (reenableSTA) {
// restart STA mode from previously used config (if set)
if (!restoreSTAConfig(staInfo))
LT_EM(WIFI, "Couldn't restore STA mode: %s", staInfo.ssid);
}
if (reenableAP) {
// restart AP mode from previously used config (if set)
if (!restoreAPConfig(apInfo))
LT_EM(WIFI, "Couldn't restore AP mode: %s", apInfo.ssid);
} }
// send STA start/stop events and AP stop event (start is handled in softAP()) // send STA start/stop events and AP stop event (start is handled in softAP())
@@ -60,11 +165,30 @@ error:
WiFiMode WiFiClass::getMode() { WiFiMode WiFiClass::getMode() {
if (!DATA->initialized) if (!DATA->initialized)
return WIFI_MODE_NULL; return WIFI_MODE_NULL;
return (WiFiMode)wifi_mode; uint8_t wlan0_state = rltk_wlan_running(WLAN0_IDX);
uint8_t wlan1_state = rltk_wlan_running(WLAN1_IDX);
wifi_mode = (wifi_mode_t)wlan0_state;
LT_DM(WIFI, "WLAN: %s=%u, %s=%u", WLAN0_NAME, wlan0_state, WLAN1_NAME, wlan1_state);
if (wlan1_state) {
if (netif_is_up(WLAN1_NETIF))
wifi_mode = (wifi_mode_t)(WIFI_MODE_AP | wlan0_state);
} else {
wifi_mode = (wifi_mode_t)(wlan0_state);
int mode = 0;
// check wlan0 mode to determine if it's an AP
if (wlan0_state)
wext_get_mode(WLAN0_NAME, &mode);
if (mode == IW_MODE_MASTER)
wifi_mode = (wifi_mode_t)(wifi_mode << 1);
}
return wifi_mode;
} }
WiFiStatus WiFiClass::status() { WiFiStatus WiFiClass::status() {
if (wifi_is_connected_to_ap() == 0) { if (rltk_wlan_is_connected_to_ap() == 0) {
return WL_CONNECTED; return WL_CONNECTED;
} else { } else {
return WL_DISCONNECTED; return WL_DISCONNECTED;

View File

@@ -26,31 +26,40 @@ extern "C" {
extern struct netif xnetif[NET_IF_NUM]; extern struct netif xnetif[NET_IF_NUM];
// dhcps.c
extern void dns_server_init(struct netif *pnetif);
extern void dns_server_deinit(void);
// wifi_util.c
extern void rltk_stop_softap(const char *ifname);
extern void rltk_suspend_softap(const char *ifname);
extern void rltk_suspend_softap_beacon(const char *ifname);
} // extern "C" } // extern "C"
// WiFi.cpp // WiFi.cpp
extern rtw_network_info_t wifi;
extern rtw_ap_info_t ap;
extern rtw_wifi_setting_t wifi_setting; extern rtw_wifi_setting_t wifi_setting;
extern unsigned char sta_password[65];
extern unsigned char ap_password[65];
extern void reset_wifi_struct(void);
extern wifi_mode_t wifi_mode; extern wifi_mode_t wifi_mode;
extern WiFiAuthMode securityTypeToAuthMode(uint8_t type); extern WiFiAuthMode securityTypeToAuthMode(uint8_t type);
// WiFiEvents.cpp // WiFiEvents.cpp
extern void startWifiTask(); extern void startWifiTask();
extern void handleRtwEvent(uint16_t event, char *data, int len, int flags); extern void handleRtwEvent(uint16_t event, char *data, int len, int flags);
#define NETIF_RTW_STA &xnetif[RTW_STA_INTERFACE] #define WLAN0_NETIF &xnetif[RTW_STA_INTERFACE]
#define NETIF_RTW_AP (wifi_mode == WIFI_MODE_APSTA ? &xnetif[RTW_AP_INTERFACE] : NETIF_RTW_STA) #define WLAN1_NETIF &xnetif[RTW_AP_INTERFACE]
#define NETIF_RTW_STA WLAN0_NETIF
#define NETIF_RTW_AP (wifi_mode & WIFI_MODE_STA ? WLAN1_NETIF : WLAN0_NETIF)
#define NETNAME_STA WLAN0_NAME #define NETNAME_STA WLAN0_NAME
#define NETNAME_AP (wifi_mode == WIFI_MODE_APSTA ? WLAN1_NAME : WLAN0_NAME) #define NETNAME_AP (wifi_mode & WIFI_MODE_STA ? WLAN1_NAME : WLAN0_NAME)
typedef struct { typedef struct {
bool initialized; bool initialized;
bool sleep; bool sleep;
SemaphoreHandle_t scanSem; SemaphoreHandle_t scanSem;
WiFiNetworkInfo sta;
WiFiNetworkInfo ap;
} WiFiData; } WiFiData;
#define DATA ((WiFiData *)data) #define DATA ((WiFiData *)data)

View File

@@ -11,20 +11,20 @@ WiFiClass::begin(const char *ssid, const char *passphrase, int32_t channel, cons
LT_HEAP_I(); LT_HEAP_I();
memset(wifi.bssid.octet, 0, ETH_ALEN); WiFiNetworkInfo &info = DATA->sta;
strcpy((char *)wifi.ssid.val, ssid); if (info.ssid != ssid)
wifi.ssid.len = strlen(ssid); // free network info, if not called from restoreSTAConfig()
resetNetworkInfo(info);
wifi.security_type = RTW_SECURITY_OPEN; if (info.ssid != ssid)
wifi.password = NULL; info.ssid = strdup(ssid);
wifi.password_len = 0; info.channel = channel;
wifi.key_id = 0; info.auth = RTW_SECURITY_OPEN;
if (passphrase) { if (passphrase) {
strcpy((char *)sta_password, passphrase); if (info.password != passphrase)
wifi.security_type = RTW_SECURITY_WPA2_AES_PSK; info.password = strdup(passphrase);
wifi.password = sta_password; info.auth = RTW_SECURITY_WPA2_AES_PSK;
wifi.password_len = strlen(passphrase);
} }
if (reconnect(bssid)) if (reconnect(bssid))
@@ -36,24 +36,26 @@ WiFiClass::begin(const char *ssid, const char *passphrase, int32_t channel, cons
bool WiFiClass::config(IPAddress localIP, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2) { bool WiFiClass::config(IPAddress localIP, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2) {
if (!enableSTA(true)) if (!enableSTA(true))
return false; return false;
WiFiNetworkInfo &info = DATA->sta;
struct ip_addr d1, d2; struct ip_addr d1, d2;
d1.addr = dns1; d1.addr = info.dns1 = dns1;
d2.addr = dns2; d2.addr = info.dns2 = dns2;
if (dns1[0]) if (d1.addr)
dns_setserver(0, &d1); dns_setserver(0, &d1);
if (dns2[0]) if (d2.addr)
dns_setserver(0, &d2); dns_setserver(0, &d2);
if (!localIP[0]) { if (!localIP[0]) {
info.localIP = 0;
LwIP_DHCP(0, DHCP_START); LwIP_DHCP(0, DHCP_START);
return true; return true;
} }
struct netif *ifs = NETIF_RTW_STA; struct netif *ifs = NETIF_RTW_STA;
struct ip_addr ipaddr, netmask, gw; struct ip_addr ipaddr, netmask, gw;
ipaddr.addr = localIP; ipaddr.addr = info.localIP = localIP;
netmask.addr = subnet; netmask.addr = info.subnet = subnet;
gw.addr = gateway; gw.addr = info.gateway = gateway;
netif_set_addr(ifs, &ipaddr, &netmask, &gw); netif_set_addr(ifs, &ipaddr, &netmask, &gw);
LwIP_DHCP(0, DHCP_STOP); LwIP_DHCP(0, DHCP_STOP);
return true; return true;
@@ -62,31 +64,39 @@ bool WiFiClass::config(IPAddress localIP, IPAddress gateway, IPAddress subnet, I
bool WiFiClass::reconnect(const uint8_t *bssid) { bool WiFiClass::reconnect(const uint8_t *bssid) {
int ret; int ret;
uint8_t dhcpRet; uint8_t dhcpRet;
WiFiNetworkInfo &info = DATA->sta;
LT_IM(WIFI, "Connecting to %s", wifi.ssid.val); LT_IM(WIFI, "Connecting to %s (bssid=%p)", info.ssid, bssid);
__wrap_rtl_printf_disable(); __wrap_rtl_printf_disable();
__wrap_DiagPrintf_disable(); __wrap_DiagPrintf_disable();
wext_set_ssid(WLAN0_NAME, (uint8_t *)"-", 1);
if (!bssid) { if (!bssid) {
ret = wifi_connect( ret = wifi_connect(
(char *)wifi.ssid.val, info.ssid,
wifi.security_type, (rtw_security_t)info.auth,
(char *)wifi.password, info.password,
wifi.ssid.len, strlen(info.ssid),
wifi.password_len, strlen(info.password),
wifi.key_id, -1,
NULL NULL
); );
} else { } else {
if (info.bssid != bssid) {
free(info.bssid);
info.bssid = (uint8_t *)malloc(ETH_ALEN);
memcpy(info.bssid, bssid, ETH_ALEN);
}
ret = wifi_connect_bssid( ret = wifi_connect_bssid(
(unsigned char *)bssid, (unsigned char *)bssid,
(char *)wifi.ssid.val, info.ssid,
wifi.security_type, (rtw_security_t)info.auth,
(char *)wifi.password, info.password,
ETH_ALEN, ETH_ALEN,
wifi.ssid.len, strlen(info.ssid),
wifi.password_len, strlen(info.password),
wifi.key_id, -1,
NULL NULL
); );
} }
@@ -122,7 +132,9 @@ error:
} }
bool WiFiClass::disconnect(bool wifiOff) { bool WiFiClass::disconnect(bool wifiOff) {
int ret = wifi_disconnect(); free(DATA->sta.ssid);
DATA->sta.ssid = NULL;
int ret = wifi_disconnect();
if (wifiOff) if (wifiOff)
enableSTA(false); enableSTA(false);
return ret == RTW_SUCCESS; return ret == RTW_SUCCESS;
@@ -141,33 +153,31 @@ bool WiFiClass::getAutoReconnect() {
IPAddress WiFiClass::localIP() { IPAddress WiFiClass::localIP() {
if (!wifi_mode) if (!wifi_mode)
return IPAddress(); return IPAddress();
return LwIP_GetIP(NETIF_RTW_STA); return netif_ip_addr4(NETIF_RTW_STA)->addr;
} }
uint8_t *WiFiClass::macAddress(uint8_t *mac) { uint8_t *WiFiClass::macAddress(uint8_t *mac) {
if (getMode() == WIFI_MODE_NULL) { if ((getMode() & WIFI_MODE_STA) == 0) {
uint8_t *efuse = (uint8_t *)malloc(512); uint8_t *efuse = (uint8_t *)malloc(512);
EFUSE_LogicalMap_Read(efuse); EFUSE_LogicalMap_Read(efuse);
memcpy(mac, efuse + 0x11A, ETH_ALEN); memcpy(mac, efuse + 0x11A, ETH_ALEN);
free(efuse); free(efuse);
return mac; return mac;
} }
memcpy(mac, LwIP_GetMAC(NETIF_RTW_STA), ETH_ALEN); memcpy(mac, NETIF_RTW_STA.hwaddr, ETH_ALEN);
return mac; return mac;
} }
IPAddress WiFiClass::subnetMask() { IPAddress WiFiClass::subnetMask() {
return LwIP_GetMASK(NETIF_RTW_STA); return netif_ip_netmask4(NETIF_RTW_STA)->addr;
} }
IPAddress WiFiClass::gatewayIP() { IPAddress WiFiClass::gatewayIP() {
return LwIP_GetGW(NETIF_RTW_STA); return netif_ip_gw4(NETIF_RTW_STA)->addr;
} }
IPAddress WiFiClass::dnsIP(uint8_t dns_no) { IPAddress WiFiClass::dnsIP(uint8_t dns_no) {
struct ip_addr dns; return dns_getserver(0)->addr;
LwIP_GetDNS(&dns);
return dns.addr;
} }
IPAddress WiFiClass::broadcastIP() { IPAddress WiFiClass::broadcastIP() {
@@ -195,14 +205,17 @@ const String WiFiClass::SSID() {
} }
const String WiFiClass::psk() { const String WiFiClass::psk() {
if (!isConnected() || !wifi.password) if (!isConnected() || !DATA->sta.password)
return ""; return "";
return (char *)wifi.password; return DATA->sta.password;
} }
uint8_t *WiFiClass::BSSID() { uint8_t *WiFiClass::BSSID() {
wext_get_bssid(NETNAME_STA, wifi.bssid.octet); WiFiNetworkInfo &info = DATA->sta;
return wifi.bssid.octet; if (!info.bssid)
info.bssid = (uint8_t *)malloc(ETH_ALEN);
wext_get_bssid(NETNAME_STA, info.bssid);
return info.bssid;
} }
int8_t WiFiClass::RSSI() { int8_t WiFiClass::RSSI() {

View File

@@ -34,7 +34,7 @@ static rtw_result_t scanHandler(rtw_scan_handler_result_t *result) {
int16_t WiFiClass::scanNetworks(bool async, bool showHidden, bool passive, uint32_t maxMsPerChannel, uint8_t channel) { int16_t WiFiClass::scanNetworks(bool async, bool showHidden, bool passive, uint32_t maxMsPerChannel, uint8_t channel) {
if (scan && scan->running) if (scan && scan->running)
return WIFI_SCAN_RUNNING; return WIFI_SCAN_RUNNING;
if (wifi_mode == WIFI_MODE_NULL) if (getMode() == WIFI_MODE_NULL)
enableSTA(true); enableSTA(true);
scanDelete(); scanDelete();
scanInit(); scanInit();