@@ -236,6 +236,11 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
236
236
if (!common_hal_wifi_radio_get_enabled (self )) {
237
237
mp_raise_RuntimeError (translate ("wifi is not enabled" ));
238
238
}
239
+ wifi_config_t * config = & self -> sta_config ;
240
+
241
+ size_t timeout_ms = timeout * 1000 ;
242
+ uint32_t start_time = common_hal_time_monotonic_ms ();
243
+ uint32_t end_time = start_time + timeout_ms ;
239
244
240
245
EventBits_t bits ;
241
246
// can't block since both bits are false after wifi_init
@@ -245,18 +250,37 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
245
250
pdTRUE ,
246
251
pdTRUE ,
247
252
0 );
248
- if (((bits & WIFI_CONNECTED_BIT ) != 0 ) &&
249
- !((bits & WIFI_DISCONNECTED_BIT ) != 0 )) {
250
- return WIFI_RADIO_ERROR_NONE ;
253
+ bool connected = ((bits & WIFI_CONNECTED_BIT ) != 0 ) &&
254
+ !((bits & WIFI_DISCONNECTED_BIT ) != 0 );
255
+ if (connected ) {
256
+ // SSIDs are up to 32 bytes. Assume it is null terminated if it is less.
257
+ if (memcmp (ssid , config -> sta .ssid , ssid_len ) == 0 &&
258
+ (ssid_len == 32 || strlen ((const char * )config -> sta .ssid ) == ssid_len )) {
259
+ // Already connected to the desired network.
260
+ return WIFI_RADIO_ERROR_NONE ;
261
+ } else {
262
+ xEventGroupClearBits (self -> event_group_handle , WIFI_DISCONNECTED_BIT );
263
+ // Trying to switch networks so disconnect first.
264
+ esp_wifi_disconnect ();
265
+ do {
266
+ RUN_BACKGROUND_TASKS ;
267
+ bits = xEventGroupWaitBits (self -> event_group_handle ,
268
+ WIFI_DISCONNECTED_BIT ,
269
+ pdTRUE ,
270
+ pdTRUE ,
271
+ 0 );
272
+ } while ((bits & WIFI_DISCONNECTED_BIT ) == 0 && !mp_hal_is_interrupted ());
273
+ }
251
274
}
252
275
// explicitly clear bits since xEventGroupWaitBits may have timed out
253
276
xEventGroupClearBits (self -> event_group_handle , WIFI_CONNECTED_BIT );
254
277
xEventGroupClearBits (self -> event_group_handle , WIFI_DISCONNECTED_BIT );
255
278
set_mode_station (self , true);
256
279
257
- wifi_config_t * config = & self -> sta_config ;
258
280
memcpy (& config -> sta .ssid , ssid , ssid_len );
259
- config -> sta .ssid [ssid_len ] = 0 ;
281
+ if (ssid_len < 32 ) {
282
+ config -> sta .ssid [ssid_len ] = 0 ;
283
+ }
260
284
memcpy (& config -> sta .password , password , password_len );
261
285
config -> sta .password [password_len ] = 0 ;
262
286
config -> sta .channel = channel ;
@@ -289,6 +313,10 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
289
313
pdTRUE ,
290
314
pdTRUE ,
291
315
0 );
316
+ // Don't retry anymore if we're over our time budget.
317
+ if (self -> retries_left > 0 && common_hal_time_monotonic_ms () > end_time ) {
318
+ self -> retries_left = 0 ;
319
+ }
292
320
} while ((bits & (WIFI_CONNECTED_BIT | WIFI_DISCONNECTED_BIT )) == 0 && !mp_hal_is_interrupted ());
293
321
if ((bits & WIFI_DISCONNECTED_BIT ) != 0 ) {
294
322
if (self -> last_disconnect_reason == WIFI_REASON_AUTH_FAIL ) {
@@ -368,6 +396,14 @@ mp_obj_t common_hal_wifi_radio_get_ipv4_subnet_ap(wifi_radio_obj_t *self) {
368
396
return common_hal_ipaddress_new_ipv4address (self -> ap_ip_info .netmask .addr );
369
397
}
370
398
399
+ uint32_t wifi_radio_get_ipv4_address (wifi_radio_obj_t * self ) {
400
+ if (!esp_netif_is_netif_up (self -> netif )) {
401
+ return 0 ;
402
+ }
403
+ esp_netif_get_ip_info (self -> netif , & self -> ip_info );
404
+ return self -> ip_info .ip .addr ;
405
+ }
406
+
371
407
mp_obj_t common_hal_wifi_radio_get_ipv4_address (wifi_radio_obj_t * self ) {
372
408
if (!esp_netif_is_netif_up (self -> netif )) {
373
409
return mp_const_none ;
0 commit comments