@@ -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
0 commit comments