[realtek-ambz] Add a wrapper around WiFi::mode()
This commit is contained in:
@@ -50,6 +50,7 @@ class WiFiClass {
|
||||
public: /* WiFiGeneric.cpp */
|
||||
int32_t channel(void);
|
||||
bool mode(WiFiMode mode);
|
||||
bool modePriv(WiFiMode mode, WiFiModeAction sta, WiFiModeAction ap);
|
||||
WiFiMode getMode();
|
||||
WiFiStatus status();
|
||||
|
||||
|
||||
@@ -2,6 +2,26 @@
|
||||
|
||||
#include "WiFi.h"
|
||||
|
||||
bool WiFiClass::mode(WiFiMode mode) {
|
||||
// store a pointer to WiFi for WiFiEvents.cpp
|
||||
pWiFi = this;
|
||||
|
||||
WiFiMode currentMode = getMode();
|
||||
LT_D_WG("Mode changing %u -> %u", currentMode, mode);
|
||||
if (mode == currentMode)
|
||||
return true;
|
||||
|
||||
// get mode changes as 0/1
|
||||
WiFiModeAction sta = WiFiModeAction((mode & WIFI_MODE_STA) != (currentMode & WIFI_MODE_STA));
|
||||
WiFiModeAction ap = WiFiModeAction((mode & WIFI_MODE_AP) != (currentMode & WIFI_MODE_AP));
|
||||
// change 0/1 to 1/2
|
||||
sta = WiFiModeAction(sta + sta * (mode & WIFI_MODE_STA));
|
||||
ap = WiFiModeAction(ap + ap * (mode & WIFI_MODE_AP));
|
||||
// actually change the mode
|
||||
LT_HEAP_I();
|
||||
return modePriv(mode, sta, ap);
|
||||
}
|
||||
|
||||
bool WiFiClass::enableSTA(bool enable) {
|
||||
WiFiMode currentMode = getMode();
|
||||
if (((currentMode & WIFI_MODE_STA) != 0) != enable) {
|
||||
|
||||
@@ -137,3 +137,9 @@ typedef struct {
|
||||
WiFiMacAddr *bssid = NULL;
|
||||
int32_t *channel = NULL;
|
||||
} WiFiScanData;
|
||||
|
||||
typedef enum {
|
||||
WLMODE_NONE = 0,
|
||||
WLMODE_DISABLE = 1,
|
||||
WLMODE_ENABLE = 2,
|
||||
} WiFiModeAction;
|
||||
|
||||
@@ -8,18 +8,10 @@ int32_t WiFiClass::channel() {
|
||||
return channel;
|
||||
}
|
||||
|
||||
bool WiFiClass::mode(WiFiMode mode) {
|
||||
// store a pointer to WiFi for WiFiEvents.cpp
|
||||
pWiFi = this;
|
||||
|
||||
WiFiMode currentMode = getMode();
|
||||
LT_D_WG("Mode changing %u -> %u", currentMode, mode);
|
||||
if (mode == currentMode)
|
||||
return true;
|
||||
LT_HEAP_I();
|
||||
bool WiFiClass::modePriv(WiFiMode mode, WiFiModeAction sta, WiFiModeAction ap) {
|
||||
startWifiTask();
|
||||
|
||||
if (!currentMode && mode && !data.initialized) {
|
||||
if (!data.initialized) {
|
||||
// initialize wifi first
|
||||
LT_I("Initializing LwIP");
|
||||
LwIP_Init();
|
||||
@@ -27,7 +19,7 @@ bool WiFiClass::mode(WiFiMode mode) {
|
||||
data.initialized = true;
|
||||
}
|
||||
LT_HEAP_I();
|
||||
if (currentMode) {
|
||||
if (getMode()) {
|
||||
// stop wifi to change mode
|
||||
LT_D_WG("Stopping WiFi to change mode");
|
||||
if (wifi_off() != RTW_SUCCESS)
|
||||
@@ -43,13 +35,12 @@ bool WiFiClass::mode(WiFiMode mode) {
|
||||
}
|
||||
|
||||
// send STA start/stop events and AP stop event (start is handled in softAP())
|
||||
if ((mode & WIFI_MODE_STA) != (currentMode & WIFI_MODE_STA)) {
|
||||
if (mode & WIFI_MODE_STA)
|
||||
wifi_indication(WIFI_EVENT_CONNECT, NULL, ARDUINO_EVENT_WIFI_STA_START, -2);
|
||||
else
|
||||
wifi_indication(WIFI_EVENT_CONNECT, NULL, ARDUINO_EVENT_WIFI_STA_STOP, -2);
|
||||
if (sta == WLMODE_ENABLE) {
|
||||
wifi_indication(WIFI_EVENT_CONNECT, NULL, ARDUINO_EVENT_WIFI_STA_START, -2);
|
||||
} else if (sta == WLMODE_DISABLE) {
|
||||
wifi_indication(WIFI_EVENT_CONNECT, NULL, ARDUINO_EVENT_WIFI_STA_STOP, -2);
|
||||
}
|
||||
if (!(mode & WIFI_MODE_AP) && (currentMode & WIFI_MODE_AP)) {
|
||||
if (ap == WLMODE_DISABLE) {
|
||||
wifi_indication(WIFI_EVENT_CONNECT, NULL, ARDUINO_EVENT_WIFI_AP_STOP, -2);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,9 +4,7 @@
|
||||
|
||||
#include "WiFi.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // __cplusplus
|
||||
|
||||
// these are defined in PIO builder (for IDE to understand)
|
||||
#define LWIP_TIMEVAL_PRIVATE 0
|
||||
@@ -40,9 +38,7 @@ extern "C" {
|
||||
|
||||
extern struct netif xnetif[NET_IF_NUM];
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
// WiFi.cpp
|
||||
extern rtw_network_info_t wifi;
|
||||
|
||||
Reference in New Issue
Block a user