[beken-72xx] Initialize WiFiData when needed, null-terminate SSIDs
This commit is contained in:
@@ -11,6 +11,30 @@ WiFiClass::~WiFiClass() {
|
|||||||
vSemaphoreDelete(data.scanSem);
|
vSemaphoreDelete(data.scanSem);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WiFiClass::dataInitialize() {
|
||||||
|
if (data.statusIp)
|
||||||
|
return;
|
||||||
|
LT_D_WG("Init data struct");
|
||||||
|
data.configSta = zalloc(sizeof(network_InitTypeDef_st));
|
||||||
|
data.configAp = zalloc(sizeof(network_InitTypeDef_ap_st));
|
||||||
|
data.statusIp = malloc(sizeof(IPStatusTypedef));
|
||||||
|
data.statusLink = malloc(sizeof(LinkStatusTypeDef));
|
||||||
|
STA_CFG->dhcp_mode = DHCP_CLIENT;
|
||||||
|
LT_D_WG("data status = %p", data.configSta);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WiFiClass::dataFree() {
|
||||||
|
LT_D_WG("Free data struct");
|
||||||
|
free(data.configSta);
|
||||||
|
free(data.configAp);
|
||||||
|
free(data.statusIp);
|
||||||
|
free(data.statusLink);
|
||||||
|
data.configSta = NULL;
|
||||||
|
data.configAp = NULL;
|
||||||
|
data.statusIp = NULL;
|
||||||
|
data.statusLink = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
WiFiStatus eventTypeToStatus(uint8_t type) {
|
WiFiStatus eventTypeToStatus(uint8_t type) {
|
||||||
// rw_msg_pub.h:9
|
// rw_msg_pub.h:9
|
||||||
switch (type) {
|
switch (type) {
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ bool WiFiClass::softAP(const char *ssid, const char *passphrase, int channel, bo
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool WiFiClass::softAPConfig(IPAddress localIP, IPAddress gateway, IPAddress subnet) {
|
bool WiFiClass::softAPConfig(IPAddress localIP, IPAddress gateway, IPAddress subnet) {
|
||||||
|
dataInitialize();
|
||||||
if (!localIP) {
|
if (!localIP) {
|
||||||
localIP = gateway = IPAddress(192, 168, 43, 1);
|
localIP = gateway = IPAddress(192, 168, 43, 1);
|
||||||
subnet = IPAddress(255, 255, 255, 0);
|
subnet = IPAddress(255, 255, 255, 0);
|
||||||
|
|||||||
@@ -6,16 +6,6 @@ bool WiFiClass::modePriv(WiFiMode mode, WiFiModeAction sta, WiFiModeAction ap) {
|
|||||||
__wrap_bk_printf_disable();
|
__wrap_bk_printf_disable();
|
||||||
startWifiTask();
|
startWifiTask();
|
||||||
|
|
||||||
if (mode && !data.statusIp) {
|
|
||||||
LT_D_WG("Init data struct");
|
|
||||||
data.configSta = zalloc(sizeof(network_InitTypeDef_st));
|
|
||||||
data.configAp = zalloc(sizeof(network_InitTypeDef_ap_st));
|
|
||||||
data.statusIp = malloc(sizeof(IPStatusTypedef));
|
|
||||||
data.statusLink = malloc(sizeof(LinkStatusTypeDef));
|
|
||||||
STA_CFG->dhcp_mode = DHCP_CLIENT;
|
|
||||||
LT_D_WG("data status = %p", data.configSta);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!__bk_rf_is_init) {
|
if (!__bk_rf_is_init) {
|
||||||
LT_D_WG("Initializing func&app");
|
LT_D_WG("Initializing func&app");
|
||||||
func_init_extended();
|
func_init_extended();
|
||||||
@@ -62,17 +52,8 @@ bool WiFiClass::modePriv(WiFiMode mode, WiFiModeAction sta, WiFiModeAction ap) {
|
|||||||
// force checking actual mode again
|
// force checking actual mode again
|
||||||
mode = getMode();
|
mode = getMode();
|
||||||
|
|
||||||
if (!mode) {
|
if (!mode)
|
||||||
LT_D_WG("Free data struct");
|
dataFree();
|
||||||
free(data.configSta);
|
|
||||||
free(data.configAp);
|
|
||||||
free(data.statusIp);
|
|
||||||
free(data.statusLink);
|
|
||||||
data.configSta = NULL;
|
|
||||||
data.configAp = NULL;
|
|
||||||
data.statusIp = NULL;
|
|
||||||
data.statusLink = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
LT_HEAP_I();
|
LT_HEAP_I();
|
||||||
|
|
||||||
|
|||||||
@@ -55,4 +55,20 @@ extern void wifiEventHandler(rw_evt_type event);
|
|||||||
#define IP_STATUS ((IPStatusTypedef *)data.statusIp)
|
#define IP_STATUS ((IPStatusTypedef *)data.statusIp)
|
||||||
#define LINK_STATUS ((LinkStatusTypeDef *)data.statusLink)
|
#define LINK_STATUS ((LinkStatusTypeDef *)data.statusLink)
|
||||||
|
|
||||||
|
#define GET_LINK_STATUS_RETURN(ret) \
|
||||||
|
{ \
|
||||||
|
if (!isConnected()) \
|
||||||
|
return ret; \
|
||||||
|
memset(LINK_STATUS, 0x00, sizeof(LinkStatusTypeDef)); \
|
||||||
|
bk_wlan_get_link_status(LINK_STATUS); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define GET_IP_STATUS_RETURN(ret, iface) \
|
||||||
|
{ \
|
||||||
|
if (!isConnected()) \
|
||||||
|
return ret; \
|
||||||
|
memset(IP_STATUS, 0x00, sizeof(IPStatusTypedef)); \
|
||||||
|
bk_wlan_get_ip_status(IP_STATUS, iface); \
|
||||||
|
}
|
||||||
|
|
||||||
} // extern "C"
|
} // extern "C"
|
||||||
|
|||||||
@@ -27,8 +27,7 @@ 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) {
|
||||||
// need to initialize data struct first
|
dataInitialize();
|
||||||
enableSTA(true);
|
|
||||||
|
|
||||||
STA_CFG->dhcp_mode = localIP ? DHCP_DISABLE : DHCP_CLIENT;
|
STA_CFG->dhcp_mode = localIP ? DHCP_DISABLE : DHCP_CLIENT;
|
||||||
if (localIP) {
|
if (localIP) {
|
||||||
@@ -64,6 +63,7 @@ bool WiFiClass::config(IPAddress localIP, IPAddress gateway, IPAddress subnet, I
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool WiFiClass::reconnect(const uint8_t *bssid) {
|
bool WiFiClass::reconnect(const uint8_t *bssid) {
|
||||||
|
dataInitialize();
|
||||||
if (!bssid && !STA_CFG->wifi_ssid[0]) {
|
if (!bssid && !STA_CFG->wifi_ssid[0]) {
|
||||||
LT_E("(B)SSID not specified");
|
LT_E("(B)SSID not specified");
|
||||||
goto error;
|
goto error;
|
||||||
@@ -106,8 +106,9 @@ error:
|
|||||||
|
|
||||||
bool WiFiClass::disconnect(bool wifiOff) {
|
bool WiFiClass::disconnect(bool wifiOff) {
|
||||||
#if LT_DEBUG_WIFI
|
#if LT_DEBUG_WIFI
|
||||||
|
memset(LINK_STATUS, 0x00, sizeof(LinkStatusTypeDef));
|
||||||
bk_wlan_get_link_status(LINK_STATUS);
|
bk_wlan_get_link_status(LINK_STATUS);
|
||||||
LT_D_WG("Disconnecting from %s", LINK_STATUS ? LINK_STATUS->ssid : NULL);
|
LT_D_WG("Disconnecting from %s (wifiOff=%d)", LINK_STATUS ? LINK_STATUS->ssid : NULL, wifiOff);
|
||||||
#endif
|
#endif
|
||||||
bk_wlan_connection_loss();
|
bk_wlan_connection_loss();
|
||||||
if (wifiOff)
|
if (wifiOff)
|
||||||
@@ -124,28 +125,28 @@ bool WiFiClass::getAutoReconnect() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
IPAddress WiFiClass::localIP() {
|
IPAddress WiFiClass::localIP() {
|
||||||
bk_wlan_get_ip_status(IP_STATUS, BK_STATION);
|
GET_IP_STATUS_RETURN((uint32_t)0, BK_STATION);
|
||||||
IPAddress ip;
|
IPAddress ip;
|
||||||
ip.fromString(IP_STATUS->ip);
|
ip.fromString(IP_STATUS->ip);
|
||||||
return ip;
|
return ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
IPAddress WiFiClass::subnetMask() {
|
IPAddress WiFiClass::subnetMask() {
|
||||||
bk_wlan_get_ip_status(IP_STATUS, BK_STATION);
|
GET_IP_STATUS_RETURN((uint32_t)0, BK_STATION);
|
||||||
IPAddress ip;
|
IPAddress ip;
|
||||||
ip.fromString(IP_STATUS->mask);
|
ip.fromString(IP_STATUS->mask);
|
||||||
return ip;
|
return ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
IPAddress WiFiClass::gatewayIP() {
|
IPAddress WiFiClass::gatewayIP() {
|
||||||
bk_wlan_get_ip_status(IP_STATUS, BK_STATION);
|
GET_IP_STATUS_RETURN((uint32_t)0, BK_STATION);
|
||||||
IPAddress ip;
|
IPAddress ip;
|
||||||
ip.fromString(IP_STATUS->gate);
|
ip.fromString(IP_STATUS->gate);
|
||||||
return ip;
|
return ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
IPAddress WiFiClass::dnsIP(uint8_t dns_no) {
|
IPAddress WiFiClass::dnsIP(uint8_t dns_no) {
|
||||||
bk_wlan_get_ip_status(IP_STATUS, BK_STATION);
|
GET_IP_STATUS_RETURN((uint32_t)0, BK_STATION);
|
||||||
IPAddress ip;
|
IPAddress ip;
|
||||||
ip.fromString(IP_STATUS->dns);
|
ip.fromString(IP_STATUS->dns);
|
||||||
return ip;
|
return ip;
|
||||||
@@ -189,7 +190,7 @@ bool WiFiClass::setMacAddress(const uint8_t *mac) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const String WiFiClass::SSID() {
|
const String WiFiClass::SSID() {
|
||||||
bk_wlan_get_link_status(LINK_STATUS);
|
GET_LINK_STATUS_RETURN("");
|
||||||
return (char *)LINK_STATUS->ssid;
|
return (char *)LINK_STATUS->ssid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,21 +204,21 @@ const String WiFiClass::psk() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
uint8_t *WiFiClass::BSSID() {
|
uint8_t *WiFiClass::BSSID() {
|
||||||
bk_wlan_get_link_status(LINK_STATUS);
|
GET_LINK_STATUS_RETURN(NULL);
|
||||||
return LINK_STATUS->bssid;
|
return LINK_STATUS->bssid;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t WiFiClass::channel() {
|
int32_t WiFiClass::channel() {
|
||||||
bk_wlan_get_link_status(LINK_STATUS);
|
GET_LINK_STATUS_RETURN(0);
|
||||||
return LINK_STATUS->channel;
|
return LINK_STATUS->channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
int8_t WiFiClass::RSSI() {
|
int8_t WiFiClass::RSSI() {
|
||||||
bk_wlan_get_link_status(LINK_STATUS);
|
GET_LINK_STATUS_RETURN(0);
|
||||||
return LINK_STATUS->wifi_strength;
|
return LINK_STATUS->wifi_strength;
|
||||||
}
|
}
|
||||||
|
|
||||||
WiFiAuthMode WiFiClass::getEncryption() {
|
WiFiAuthMode WiFiClass::getEncryption() {
|
||||||
bk_wlan_get_link_status(LINK_STATUS);
|
GET_LINK_STATUS_RETURN(WIFI_AUTH_INVALID);
|
||||||
return securityTypeToAuthMode(LINK_STATUS->security);
|
return securityTypeToAuthMode(LINK_STATUS->security);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,5 +63,9 @@ bool WiFiClass::validate(const char *ssid, const char *passphrase) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
__attribute__((weak)) void WiFiClass::dataInitialize() {}
|
||||||
|
|
||||||
|
__attribute__((weak)) void WiFiClass::dataFree() {}
|
||||||
|
|
||||||
WiFiClass WiFi;
|
WiFiClass WiFi;
|
||||||
WiFiClass *pWiFi = NULL;
|
WiFiClass *pWiFi = NULL;
|
||||||
|
|||||||
@@ -47,6 +47,8 @@ class WiFiClass {
|
|||||||
~WiFiClass();
|
~WiFiClass();
|
||||||
void printDiag(Print &dest);
|
void printDiag(Print &dest);
|
||||||
bool validate(const char *ssid, const char *passphrase);
|
bool validate(const char *ssid, const char *passphrase);
|
||||||
|
void dataInitialize();
|
||||||
|
void dataFree();
|
||||||
|
|
||||||
public: /* WiFiGeneric.cpp */
|
public: /* WiFiGeneric.cpp */
|
||||||
bool mode(WiFiMode mode);
|
bool mode(WiFiMode mode);
|
||||||
|
|||||||
@@ -17,6 +17,9 @@ bool WiFiClass::mode(WiFiMode mode) {
|
|||||||
// change 0/1 to 1/2
|
// change 0/1 to 1/2
|
||||||
sta = WiFiModeAction(sta + sta * (mode & WIFI_MODE_STA));
|
sta = WiFiModeAction(sta + sta * (mode & WIFI_MODE_STA));
|
||||||
ap = WiFiModeAction(ap + ap * (mode & WIFI_MODE_AP));
|
ap = WiFiModeAction(ap + ap * (mode & WIFI_MODE_AP));
|
||||||
|
// initialize data structures if wifi is enabled
|
||||||
|
if (mode)
|
||||||
|
dataInitialize();
|
||||||
// actually change the mode
|
// actually change the mode
|
||||||
LT_HEAP_I();
|
LT_HEAP_I();
|
||||||
return modePriv(mode, sta, ap);
|
return modePriv(mode, sta, ap);
|
||||||
|
|||||||
Reference in New Issue
Block a user