From dc794f91c0eea3cca7ed4cf8fb74867c191a0941 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Mon, 13 Jun 2022 15:38:19 -0700 Subject: [PATCH] Speed up auto-wifi with wrong password This adds basic timeout support to connect by preventing subsequent retries if over time. The first connect may still take more than the timeout. --- ports/espressif/common-hal/wifi/Radio.c | 8 ++++++++ supervisor/shared/web_workflow/web_workflow.c | 11 +++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/ports/espressif/common-hal/wifi/Radio.c b/ports/espressif/common-hal/wifi/Radio.c index c914bdd8e1..b6b155a71c 100644 --- a/ports/espressif/common-hal/wifi/Radio.c +++ b/ports/espressif/common-hal/wifi/Radio.c @@ -238,6 +238,10 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t } wifi_config_t *config = &self->sta_config; + size_t timeout_ms = timeout * 1000; + uint32_t start_time = common_hal_time_monotonic_ms(); + uint32_t end_time = start_time + timeout_ms; + EventBits_t bits; // can't block since both bits are false after wifi_init // both bits are true after an existing connection stops @@ -309,6 +313,10 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t pdTRUE, pdTRUE, 0); + // Don't retry anymore if we're over our time budget. + if (self->retries_left > 0 && common_hal_time_monotonic_ms() > end_time) { + self->retries_left = 0; + } } while ((bits & (WIFI_CONNECTED_BIT | WIFI_DISCONNECTED_BIT)) == 0 && !mp_hal_is_interrupted()); if ((bits & WIFI_DISCONNECTED_BIT) != 0) { if (self->last_disconnect_reason == WIFI_REASON_AUTH_FAIL) { diff --git a/supervisor/shared/web_workflow/web_workflow.c b/supervisor/shared/web_workflow/web_workflow.c index dcc79ecf27..bb83048dca 100644 --- a/supervisor/shared/web_workflow/web_workflow.c +++ b/supervisor/shared/web_workflow/web_workflow.c @@ -44,13 +44,17 @@ void supervisor_web_workflow_status(void) { serial_write_compressed(translate("Wi-Fi: ")); if (common_hal_wifi_radio_get_enabled(&common_hal_wifi_radio_obj)) { uint32_t ipv4_address = wifi_radio_get_ipv4_address(&common_hal_wifi_radio_obj); - if (wifi_status != WIFI_RADIO_ERROR_NONE) { + if (wifi_status == WIFI_RADIO_ERROR_AUTH_EXPIRE || + wifi_status == WIFI_RADIO_ERROR_AUTH_FAIL) { + serial_write_compressed(translate("Authentication failure")); + } else if (wifi_status != WIFI_RADIO_ERROR_NONE) { mp_printf(&mp_plat_print, "%d", wifi_status); } else if (ipv4_address == 0) { serial_write_compressed(translate("No IP")); } else { uint8_t *octets = (uint8_t *)&ipv4_address; mp_printf(&mp_plat_print, "%d.%d.%d.%d", octets[0], octets[1], octets[2], octets[3]); + // TODO: Use these unicode to show signal strength: ▂▄▆█ } } else { serial_write_compressed(translate("off")); @@ -75,12 +79,15 @@ void supervisor_start_web_workflow(void) { common_hal_wifi_init(false); common_hal_wifi_radio_set_enabled(&common_hal_wifi_radio_obj, true); + // TODO: Do our own scan so that we can find the channel we want before calling connect. + // Otherwise, connect will do a full slow scan to pick the best AP. + // NUL terminate the strings because dotenv doesn't. ssid[ssid_len] = '\0'; password[password_len] = '\0'; wifi_status = common_hal_wifi_radio_connect( &common_hal_wifi_radio_obj, (uint8_t *)ssid, ssid_len, (uint8_t *)password, password_len, - 0, 1, NULL, 0); + 0, 0.1, NULL, 0); if (wifi_status != WIFI_RADIO_ERROR_NONE) { common_hal_wifi_radio_set_enabled(&common_hal_wifi_radio_obj, false);