Skip to content

Commit da52456

Browse files
committed
Use WiFi.mode to enable/disable the Network Interfaces
1 parent 7b29bac commit da52456

File tree

6 files changed

+102
-60
lines changed

6 files changed

+102
-60
lines changed

Diff for: libraries/Network/src/NetworkInterface.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,10 @@ IPAddress NetworkInterface::globalIPv6() const
671671

672672
size_t NetworkInterface::printTo(Print & out) const {
673673
size_t bytes = 0;
674-
bytes += out.print(esp_netif_get_desc(_esp_netif));
674+
const char * dscr = esp_netif_get_desc(_esp_netif);
675+
if(dscr != NULL){
676+
bytes += out.print(dscr);
677+
}
675678
bytes += out.print(":");
676679
if(esp_netif_is_netif_up(_esp_netif)){
677680
bytes += out.print(" <UP");

Diff for: libraries/WiFi/src/AP.cpp

+22-24
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ static void _ap_event_cb(void* arg, esp_event_base_t event_base, int32_t event_i
8282
}
8383
}
8484

85-
static void _onApArduinoEvent(arduino_event_id_t event, arduino_event_info_t info)
85+
static void _onApArduinoEvent(arduino_event_t *ev)
8686
{
87-
if(_ap_network_if == NULL || event < ARDUINO_EVENT_WIFI_AP_START || event > ARDUINO_EVENT_WIFI_AP_GOT_IP6){
87+
if(_ap_network_if == NULL || ev->event_id < ARDUINO_EVENT_WIFI_AP_START || ev->event_id > ARDUINO_EVENT_WIFI_AP_GOT_IP6){
8888
return;
8989
}
90-
log_d("Arduino AP Event: %d - %s", event, Network.eventName(event));
91-
if(event == ARDUINO_EVENT_WIFI_AP_START) {
90+
log_d("Arduino AP Event: %d - %s", ev->event_id, Network.eventName(ev->event_id));
91+
if(ev->event_id == ARDUINO_EVENT_WIFI_AP_START) {
9292
if (_ap_network_if->getStatusBits() & ESP_NETIF_WANT_IP6_BIT){
9393
esp_err_t err = esp_netif_create_ip6_linklocal(_ap_network_if->netif());
9494
if(err != ESP_OK){
@@ -157,37 +157,21 @@ APClass::~APClass(){
157157
_ap_network_if = NULL;
158158
}
159159

160-
bool APClass::begin(){
161-
162-
Network.begin();
160+
bool APClass::onEnable(){
163161
if(_ap_ev_instance == NULL && esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &_ap_event_cb, this, &_ap_ev_instance)){
164162
log_e("event_handler_instance_register for WIFI_EVENT Failed!");
165163
return false;
166164
}
167165
if(_esp_netif == NULL){
168166
Network.onSysEvent(_onApArduinoEvent);
169-
}
170-
171-
if(!WiFi.enableAP(true)) {
172-
log_e("AP enable failed!");
173-
return false;
174-
}
175-
176-
// attach events and esp_netif here
177-
if(_esp_netif == NULL){
178167
_esp_netif = get_esp_interface_netif(ESP_IF_WIFI_AP);
179168
/* attach to receive events */
180169
initNetif(ESP_NETIF_ID_AP);
181170
}
182-
183171
return true;
184172
}
185173

186-
bool APClass::end(){
187-
if(!WiFi.enableAP(false)) {
188-
log_e("AP disable failed!");
189-
return false;
190-
}
174+
bool APClass::onDisable(){
191175
Network.removeEvent(_onApArduinoEvent);
192176
// we just set _esp_netif to NULL here, so destroyNetif() does not try to destroy it.
193177
// That would be done by WiFi.enableAP(false) if STA is not enabled, or when it gets disabled
@@ -200,15 +184,29 @@ bool APClass::end(){
200184
return true;
201185
}
202186

187+
bool APClass::begin(){
188+
if(!WiFi.enableAP(true)) {
189+
log_e("AP enable failed!");
190+
return false;
191+
}
192+
return true;
193+
}
194+
195+
bool APClass::end(){
196+
if(!WiFi.enableAP(false)) {
197+
log_e("AP disable failed!");
198+
return false;
199+
}
200+
return true;
201+
}
202+
203203
bool APClass::create(const char* ssid, const char* passphrase, int channel, int ssid_hidden, int max_connection, bool ftm_responder){
204204
if(!ssid || *ssid == 0) {
205-
// fail SSID missing
206205
log_e("SSID missing!");
207206
return false;
208207
}
209208

210209
if(passphrase && (strlen(passphrase) > 0 && strlen(passphrase) < 8)) {
211-
// fail passphrase too short
212210
log_e("passphrase too short!");
213211
return false;
214212
}

Diff for: libraries/WiFi/src/STA.cpp

+37-33
Original file line numberDiff line numberDiff line change
@@ -124,22 +124,22 @@ static const char * auth_mode_str(int authmode)
124124
}
125125
#endif
126126

127-
static void _onStaArduinoEvent(arduino_event_id_t event, arduino_event_info_t info)
127+
static void _onStaArduinoEvent(arduino_event_t *ev)
128128
{
129-
if(_sta_network_if == NULL || event < ARDUINO_EVENT_WIFI_STA_START || event > ARDUINO_EVENT_WIFI_STA_LOST_IP){
129+
if(_sta_network_if == NULL || ev->event_id < ARDUINO_EVENT_WIFI_STA_START || ev->event_id > ARDUINO_EVENT_WIFI_STA_LOST_IP){
130130
return;
131131
}
132132
static bool first_connect = true;
133-
log_d("Arduino STA Event: %d - %s", event, Network.eventName(event));
133+
log_d("Arduino STA Event: %d - %s", ev->event_id, Network.eventName(ev->event_id));
134134

135-
if(event == ARDUINO_EVENT_WIFI_STA_START) {
135+
if(ev->event_id == ARDUINO_EVENT_WIFI_STA_START) {
136136
_sta_network_if->_setStatus(WL_DISCONNECTED);
137137
if(esp_wifi_set_ps(WiFi.getSleep()) != ESP_OK){
138138
log_e("esp_wifi_set_ps failed");
139139
}
140-
} else if(event == ARDUINO_EVENT_WIFI_STA_STOP) {
140+
} else if(ev->event_id == ARDUINO_EVENT_WIFI_STA_STOP) {
141141
_sta_network_if->_setStatus(WL_STOPPED);
142-
} else if(event == ARDUINO_EVENT_WIFI_STA_CONNECTED) {
142+
} else if(ev->event_id == ARDUINO_EVENT_WIFI_STA_CONNECTED) {
143143
_sta_network_if->_setStatus(WL_IDLE_STATUS);
144144
if (_sta_network_if->getStatusBits() & ESP_NETIF_WANT_IP6_BIT){
145145
esp_err_t err = esp_netif_create_ip6_linklocal(_sta_network_if->netif());
@@ -149,8 +149,8 @@ static void _onStaArduinoEvent(arduino_event_id_t event, arduino_event_info_t in
149149
log_v("Enabled IPv6 Link Local on %s", _sta_network_if->desc());
150150
}
151151
}
152-
} else if(event == ARDUINO_EVENT_WIFI_STA_DISCONNECTED) {
153-
uint8_t reason = info.wifi_sta_disconnected.reason;
152+
} else if(ev->event_id == ARDUINO_EVENT_WIFI_STA_DISCONNECTED) {
153+
uint8_t reason = ev->event_info.wifi_sta_disconnected.reason;
154154
// Reason 0 causes crash, use reason 1 (UNSPECIFIED) instead
155155
if(!reason)
156156
reason = WIFI_REASON_UNSPECIFIED;
@@ -186,18 +186,18 @@ static void _onStaArduinoEvent(arduino_event_id_t event, arduino_event_info_t in
186186
_sta_network_if->disconnect();
187187
_sta_network_if->connect();
188188
}
189-
} else if(event == ARDUINO_EVENT_WIFI_STA_GOT_IP) {
189+
} else if(ev->event_id == ARDUINO_EVENT_WIFI_STA_GOT_IP) {
190190
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_DEBUG
191-
uint8_t * ip = (uint8_t *)&(info.got_ip.ip_info.ip.addr);
192-
uint8_t * mask = (uint8_t *)&(info.got_ip.ip_info.netmask.addr);
193-
uint8_t * gw = (uint8_t *)&(info.got_ip.ip_info.gw.addr);
191+
uint8_t * ip = (uint8_t *)&(ev->event_info.got_ip.ip_info.ip.addr);
192+
uint8_t * mask = (uint8_t *)&(ev->event_info.got_ip.ip_info.netmask.addr);
193+
uint8_t * gw = (uint8_t *)&(ev->event_info.got_ip.ip_info.gw.addr);
194194
log_d("STA IP: %u.%u.%u.%u, MASK: %u.%u.%u.%u, GW: %u.%u.%u.%u",
195195
ip[0], ip[1], ip[2], ip[3],
196196
mask[0], mask[1], mask[2], mask[3],
197197
gw[0], gw[1], gw[2], gw[3]);
198198
#endif
199199
_sta_network_if->_setStatus(WL_CONNECTED);
200-
} else if(event == ARDUINO_EVENT_WIFI_STA_LOST_IP) {
200+
} else if(ev->event_id == ARDUINO_EVENT_WIFI_STA_LOST_IP) {
201201
_sta_network_if->_setStatus(WL_IDLE_STATUS);
202202
}
203203
}
@@ -288,29 +288,42 @@ bool STAClass::bandwidth(wifi_bandwidth_t bandwidth) {
288288
return true;
289289
}
290290

291-
bool STAClass::begin(bool tryConnect){
292-
293-
Network.begin();
291+
bool STAClass::onEnable(){
294292
if(_sta_ev_instance == NULL && esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &_sta_event_cb, this, &_sta_ev_instance)){
295293
log_e("event_handler_instance_register for WIFI_EVENT Failed!");
296294
return false;
297295
}
298296
if(_esp_netif == NULL){
297+
_esp_netif = get_esp_interface_netif(ESP_IF_WIFI_STA);
298+
if(_esp_netif == NULL){
299+
log_e("STA was enabled, but netif is NULL???");
300+
return false;
301+
}
302+
/* attach to receive events */
299303
Network.onSysEvent(_onStaArduinoEvent);
304+
initNetif(ESP_NETIF_ID_STA);
305+
}
306+
return true;
307+
}
308+
309+
bool STAClass::onDisable(){
310+
Network.removeEvent(_onStaArduinoEvent);
311+
// we just set _esp_netif to NULL here, so destroyNetif() does not try to destroy it.
312+
// That would be done by WiFi.enableSTA(false) if AP is not enabled, or when it gets disabled
313+
_esp_netif = NULL;
314+
destroyNetif();
315+
if(_sta_ev_instance != NULL){
316+
esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &_sta_event_cb);
317+
_sta_ev_instance = NULL;
300318
}
319+
return true;
320+
}
301321

322+
bool STAClass::begin(bool tryConnect){
302323
if(!WiFi.enableSTA(true)) {
303324
log_e("STA enable failed!");
304325
return false;
305326
}
306-
307-
// attach events and esp_netif here
308-
if(_esp_netif == NULL){
309-
_esp_netif = get_esp_interface_netif(ESP_IF_WIFI_STA);
310-
/* attach to receive events */
311-
initNetif(ESP_NETIF_ID_STA);
312-
}
313-
314327
if(tryConnect){
315328
return connect();
316329
}
@@ -322,15 +335,6 @@ bool STAClass::end(){
322335
log_e("STA disable failed!");
323336
return false;
324337
}
325-
Network.removeEvent(_onStaArduinoEvent);
326-
// we just set _esp_netif to NULL here, so destroyNetif() does not try to destroy it.
327-
// That would be done by WiFi.enableSTA(false) if AP is not enabled, or when it gets disabled
328-
_esp_netif = NULL;
329-
destroyNetif();
330-
if(_sta_ev_instance != NULL){
331-
esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &_sta_event_cb);
332-
_sta_ev_instance = NULL;
333-
}
334338
return true;
335339
}
336340

Diff for: libraries/WiFi/src/WiFiAP.h

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ class APClass: public NetworkInterface {
5555

5656
protected:
5757
size_t printDriverInfo(Print & out) const;
58+
59+
friend class WiFiGenericClass;
60+
bool onEnable();
61+
bool onDisable();
5862
};
5963

6064
// ----------------------------------------------------------------------------------------------

Diff for: libraries/WiFi/src/WiFiGeneric.cpp

+31-2
Original file line numberDiff line numberDiff line change
@@ -471,17 +471,36 @@ bool WiFiGenericClass::mode(wifi_mode_t m)
471471
return false;
472472
}
473473
Network.onSysEvent(_eventCallback);
474-
} else if(cm && !m){
474+
}
475+
476+
if(((m & WIFI_MODE_STA) != 0) && ((cm & WIFI_MODE_STA) == 0)){
477+
// we are enabling STA interface
478+
WiFi.STA.onEnable();
479+
}
480+
if(((m & WIFI_MODE_AP) != 0) && ((cm & WIFI_MODE_AP) == 0)){
481+
// we are enabling AP interface
482+
WiFi.AP.onEnable();
483+
}
484+
485+
if(cm && !m){
475486
// Turn OFF WiFi
476487
if(!espWiFiStop()){
477488
return false;
478489
}
490+
if((cm & WIFI_MODE_STA) != 0){
491+
// we are disabling STA interface
492+
WiFi.STA.onDisable();
493+
}
494+
if((cm & WIFI_MODE_AP) != 0){
495+
// we are disabling AP interface
496+
WiFi.AP.onDisable();
497+
}
479498
Network.removeEvent(_eventCallback);
480499
return true;
481500
}
482501

483502
esp_err_t err;
484-
if(m & WIFI_MODE_STA){
503+
if(((m & WIFI_MODE_STA) != 0) && ((cm & WIFI_MODE_STA) == 0)){
485504
err = esp_netif_set_hostname(esp_netifs[ESP_IF_WIFI_STA], NetworkManager::getHostname());
486505
if(err){
487506
log_e("Could not set hostname! %d", err);
@@ -493,6 +512,16 @@ bool WiFiGenericClass::mode(wifi_mode_t m)
493512
log_e("Could not set mode! %d", err);
494513
return false;
495514
}
515+
516+
if(((m & WIFI_MODE_STA) == 0) && ((cm & WIFI_MODE_STA) != 0)){
517+
// we are disabling STA interface (but AP is ON)
518+
WiFi.STA.onDisable();
519+
}
520+
if(((m & WIFI_MODE_AP) == 0) && ((cm & WIFI_MODE_AP) != 0)){
521+
// we are disabling AP interface (but STA is ON)
522+
WiFi.AP.onDisable();
523+
}
524+
496525
if(_long_range){
497526
if(m & WIFI_MODE_STA){
498527
err = esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_LR);

Diff for: libraries/WiFi/src/WiFiSTA.h

+4
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ class STAClass: public NetworkInterface {
9191
wl_status_t _status;
9292

9393
size_t printDriverInfo(Print & out) const;
94+
95+
friend class WiFiGenericClass;
96+
bool onEnable();
97+
bool onDisable();
9498
};
9599

96100
// ----------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)