@@ -124,22 +124,22 @@ static const char * auth_mode_str(int authmode)
124
124
}
125
125
#endif
126
126
127
- static void _onStaArduinoEvent (arduino_event_id_t event, arduino_event_info_t info )
127
+ static void _onStaArduinoEvent (arduino_event_t *ev )
128
128
{
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){
130
130
return ;
131
131
}
132
132
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 ));
134
134
135
- if (event == ARDUINO_EVENT_WIFI_STA_START) {
135
+ if (ev-> event_id == ARDUINO_EVENT_WIFI_STA_START) {
136
136
_sta_network_if->_setStatus (WL_DISCONNECTED);
137
137
if (esp_wifi_set_ps (WiFi.getSleep ()) != ESP_OK){
138
138
log_e (" esp_wifi_set_ps failed" );
139
139
}
140
- } else if (event == ARDUINO_EVENT_WIFI_STA_STOP) {
140
+ } else if (ev-> event_id == ARDUINO_EVENT_WIFI_STA_STOP) {
141
141
_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) {
143
143
_sta_network_if->_setStatus (WL_IDLE_STATUS);
144
144
if (_sta_network_if->getStatusBits () & ESP_NETIF_WANT_IP6_BIT){
145
145
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
149
149
log_v (" Enabled IPv6 Link Local on %s" , _sta_network_if->desc ());
150
150
}
151
151
}
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 ;
154
154
// Reason 0 causes crash, use reason 1 (UNSPECIFIED) instead
155
155
if (!reason)
156
156
reason = WIFI_REASON_UNSPECIFIED;
@@ -186,18 +186,18 @@ static void _onStaArduinoEvent(arduino_event_id_t event, arduino_event_info_t in
186
186
_sta_network_if->disconnect ();
187
187
_sta_network_if->connect ();
188
188
}
189
- } else if (event == ARDUINO_EVENT_WIFI_STA_GOT_IP) {
189
+ } else if (ev-> event_id == ARDUINO_EVENT_WIFI_STA_GOT_IP) {
190
190
#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 );
194
194
log_d (" STA IP: %u.%u.%u.%u, MASK: %u.%u.%u.%u, GW: %u.%u.%u.%u" ,
195
195
ip[0 ], ip[1 ], ip[2 ], ip[3 ],
196
196
mask[0 ], mask[1 ], mask[2 ], mask[3 ],
197
197
gw[0 ], gw[1 ], gw[2 ], gw[3 ]);
198
198
#endif
199
199
_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) {
201
201
_sta_network_if->_setStatus (WL_IDLE_STATUS);
202
202
}
203
203
}
@@ -288,29 +288,42 @@ bool STAClass::bandwidth(wifi_bandwidth_t bandwidth) {
288
288
return true ;
289
289
}
290
290
291
- bool STAClass::begin (bool tryConnect){
292
-
293
- Network.begin ();
291
+ bool STAClass::onEnable (){
294
292
if (_sta_ev_instance == NULL && esp_event_handler_instance_register (WIFI_EVENT, ESP_EVENT_ANY_ID, &_sta_event_cb, this , &_sta_ev_instance)){
295
293
log_e (" event_handler_instance_register for WIFI_EVENT Failed!" );
296
294
return false ;
297
295
}
298
296
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 */
299
303
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 ;
300
318
}
319
+ return true ;
320
+ }
301
321
322
+ bool STAClass::begin (bool tryConnect){
302
323
if (!WiFi.enableSTA (true )) {
303
324
log_e (" STA enable failed!" );
304
325
return false ;
305
326
}
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
-
314
327
if (tryConnect){
315
328
return connect ();
316
329
}
@@ -322,15 +335,6 @@ bool STAClass::end(){
322
335
log_e (" STA disable failed!" );
323
336
return false ;
324
337
}
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
- }
334
338
return true ;
335
339
}
336
340
0 commit comments