@@ -334,7 +334,7 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
334334 params .ssid_length = ssid_len ;
335335 params .psk = password ;
336336 params .psk_length = password_len ;
337- params .timeout = (int )timeout ;
337+ params .timeout = 5 ; // (int)timeout;
338338 params .channel = WIFI_CHANNEL_ANY ;
339339 params .security = WIFI_SECURITY_TYPE_NONE ;
340340 if (password_len > 0 ) {
@@ -352,118 +352,64 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
352352 if (bssid_len > 0 ) {
353353 memcpy (params .bssid , bssid , bssid_len );
354354 }
355+ // Check to see if we're already connected
356+ // if we are to the network we want
357+ // then return immediately
358+ // if not
359+ // then disconnect.
355360 printk ("net_mgmt\n" );
356361 int res = net_mgmt (NET_REQUEST_WIFI_CONNECT , self -> sta_netif , & params , sizeof (params ));
357362 printk ("net_mgmt returned\n" );
358363 if (res != 0 ) {
359364 printk ("Failed to start connect to wifi %d\n" , res );
360365 return WIFI_RADIO_ERROR_AUTH_FAIL ;
361366 }
362- printk ("sleeping past timeout %f\n" , timeout );
363- k_sleep (K_SECONDS (timeout + 2 ));
364-
365- printk ("sleeping 30 more seconds\n" );
366- k_sleep (K_SECONDS (30 ));
367- // EventBits_t bits;
368- // // can't block since both bits are false after wifi_init
369- // // both bits are true after an existing connection stops
370- // bits = xEventGroupWaitBits(self->event_group_handle,
371- // WIFI_CONNECTED_BIT | WIFI_DISCONNECTED_BIT,
372- // pdTRUE,
373- // pdTRUE,
374- // 0);
375- // bool connected = ((bits & WIFI_CONNECTED_BIT) != 0) &&
376- // !((bits & WIFI_DISCONNECTED_BIT) != 0);
377- // if (connected) {
378- // // SSIDs are up to 32 bytes. Assume it is null terminated if it is less.
379- // if (memcmp(ssid, config->sta.ssid, ssid_len) == 0 &&
380- // (ssid_len == 32 || strlen((const char *)config->sta.ssid) == ssid_len)) {
381- // // Already connected to the desired network.
382- // return WIFI_RADIO_ERROR_NONE;
383- // } else {
384- // xEventGroupClearBits(self->event_group_handle, WIFI_DISCONNECTED_BIT);
385- // // Trying to switch networks so disconnect first.
386- // esp_wifi_disconnect();
387- // do {
388- // RUN_BACKGROUND_TASKS;
389- // bits = xEventGroupWaitBits(self->event_group_handle,
390- // WIFI_DISCONNECTED_BIT,
391- // pdTRUE,
392- // pdTRUE,
393- // 0);
394- // } while ((bits & WIFI_DISCONNECTED_BIT) == 0 && !mp_hal_is_interrupted());
395- // }
396- // }
397- // // explicitly clear bits since xEventGroupWaitBits may have timed out
398- // xEventGroupClearBits(self->event_group_handle, WIFI_CONNECTED_BIT);
399- // xEventGroupClearBits(self->event_group_handle, WIFI_DISCONNECTED_BIT);
400- // set_mode_station(self, true);
401367
402- // memcpy(&config->sta.ssid, ssid, ssid_len);
403- // if (ssid_len < 32) {
404- // config->sta.ssid[ssid_len] = 0;
405- // }
406- // memcpy(&config->sta.password, password, password_len);
407- // config->sta.password[password_len] = 0;
408- // config->sta.channel = channel;
409- // // From esp_wifi_types.h:
410- // // Generally, station_config.bssid_set needs to be 0; and it needs
411- // // to be 1 only when users need to check the MAC address of the AP
412- // if (bssid_len > 0) {
413- // memcpy(&config->sta.bssid, bssid, bssid_len);
414- // config->sta.bssid[bssid_len] = 0;
415- // config->sta.bssid_set = true;
416- // } else {
417- // config->sta.bssid_set = false;
418- // }
419- // // If channel is 0 (default/unset) and BSSID is not given, do a full scan instead of fast scan
420- // // This will ensure that the best AP in range is chosen automatically
421- // if ((config->sta.bssid_set == 0) && (config->sta.channel == 0)) {
422- // config->sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
423- // } else {
424- // config->sta.scan_method = WIFI_FAST_SCAN;
425- // }
426- // esp_wifi_set_config(ESP_IF_WIFI_STA, config);
427- // self->starting_retries = 5;
428- // self->retries_left = 5;
429- // esp_wifi_connect();
368+ k_poll_event_init (& self -> events [0 ],
369+ K_POLL_TYPE_SEM_AVAILABLE ,
370+ K_POLL_MODE_NOTIFY_ONLY ,
371+ & mp_interrupt_sem );
430372
431- // do {
432- // RUN_BACKGROUND_TASKS;
433- // bits = xEventGroupWaitBits(self->event_group_handle,
434- // WIFI_CONNECTED_BIT | WIFI_DISCONNECTED_BIT,
435- // pdTRUE,
436- // pdTRUE,
437- // 0);
438- // // Don't retry anymore if we're over our time budget.
439- // if (self->retries_left > 0 && common_hal_time_monotonic_ms() > end_time) {
440- // self->retries_left = 0;
441- // }
442- // } while ((bits & (WIFI_CONNECTED_BIT | WIFI_DISCONNECTED_BIT)) == 0 && !mp_hal_is_interrupted());
443-
444- // if ((bits & WIFI_DISCONNECTED_BIT) != 0) {
445- // if (
446- // (self->last_disconnect_reason == WIFI_REASON_AUTH_FAIL) ||
447- // (self->last_disconnect_reason == WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT) ||
448- // (self->last_disconnect_reason == WIFI_REASON_NO_AP_FOUND_W_COMPATIBLE_SECURITY) ||
449- // (self->last_disconnect_reason == WIFI_REASON_NO_AP_FOUND_IN_AUTHMODE_THRESHOLD)
450- // ) {
451- // return WIFI_RADIO_ERROR_AUTH_FAIL;
452- // } else if (self->last_disconnect_reason == WIFI_REASON_NO_AP_FOUND) {
453- // return WIFI_RADIO_ERROR_NO_AP_FOUND;
454- // }
455- // return self->last_disconnect_reason;
456- // } else {
457- // // We're connected, allow us to retry if we get disconnected.
458- // self->retries_left = self->starting_retries;
459- // }
460- return WIFI_RADIO_ERROR_NONE ;
373+ k_poll_signal_init (& self -> done );
374+ k_poll_event_init (& self -> events [1 ],
375+ K_POLL_TYPE_SIGNAL ,
376+ K_POLL_MODE_NOTIFY_ONLY ,
377+ & self -> done );
378+
379+ k_poll (self -> events , ARRAY_SIZE (self -> events ), K_FOREVER );
380+
381+ if (mp_hal_is_interrupted ()) {
382+ // Do we need to disconnect here?
383+ return WIFI_RADIO_ERROR_UNSPECIFIED ;
384+ }
385+
386+ int signaled ;
387+ int result ;
388+ k_poll_signal_check (& self -> done , & signaled , & result );
389+ if (!signaled ) {
390+ return WIFI_RADIO_ERROR_UNSPECIFIED ;
391+ }
392+ printk ("common_hal_wifi_radio_connect done: %d\n" , result );
393+ if (result == WIFI_STATUS_CONN_SUCCESS ) {
394+ printk ("success!\n" );
395+ k_sleep (K_SECONDS (30 ));
396+ return WIFI_RADIO_ERROR_NONE ;
397+ } else if (result == WIFI_STATUS_CONN_FAIL ) {
398+ return WIFI_RADIO_ERROR_UNSPECIFIED ;
399+ } else if (result == WIFI_STATUS_CONN_TIMEOUT ) {
400+ return WIFI_RADIO_ERROR_UNSPECIFIED ;
401+ } else if (result == WIFI_STATUS_CONN_WRONG_PASSWORD ) {
402+ return WIFI_RADIO_ERROR_AUTH_FAIL ;
403+ } else if (result == WIFI_STATUS_CONN_AP_NOT_FOUND ) {
404+ return WIFI_RADIO_ERROR_NO_AP_FOUND ;
405+ }
406+
407+ return WIFI_RADIO_ERROR_UNSPECIFIED ;
461408}
462409
463410bool common_hal_wifi_radio_get_connected (wifi_radio_obj_t * self ) {
464411 printk ("common_hal_wifi_radio_get_connected\n" );
465- // return self->sta_mode && esp_netif_is_netif_up(self->netif);
466- return false;
412+ return net_if_is_up (self -> sta_netif );
467413}
468414
469415mp_obj_t common_hal_wifi_radio_get_ap_info (wifi_radio_obj_t * self ) {
0 commit comments