Skip to content

Commit dc794f9

Browse files
committed
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.
1 parent 1a3d2a5 commit dc794f9

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

ports/espressif/common-hal/wifi/Radio.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
238238
}
239239
wifi_config_t *config = &self->sta_config;
240240

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;
244+
241245
EventBits_t bits;
242246
// can't block since both bits are false after wifi_init
243247
// 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
309313
pdTRUE,
310314
pdTRUE,
311315
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+
}
312320
} while ((bits & (WIFI_CONNECTED_BIT | WIFI_DISCONNECTED_BIT)) == 0 && !mp_hal_is_interrupted());
313321
if ((bits & WIFI_DISCONNECTED_BIT) != 0) {
314322
if (self->last_disconnect_reason == WIFI_REASON_AUTH_FAIL) {

supervisor/shared/web_workflow/web_workflow.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,17 @@ void supervisor_web_workflow_status(void) {
4444
serial_write_compressed(translate("Wi-Fi: "));
4545
if (common_hal_wifi_radio_get_enabled(&common_hal_wifi_radio_obj)) {
4646
uint32_t ipv4_address = wifi_radio_get_ipv4_address(&common_hal_wifi_radio_obj);
47-
if (wifi_status != WIFI_RADIO_ERROR_NONE) {
47+
if (wifi_status == WIFI_RADIO_ERROR_AUTH_EXPIRE ||
48+
wifi_status == WIFI_RADIO_ERROR_AUTH_FAIL) {
49+
serial_write_compressed(translate("Authentication failure"));
50+
} else if (wifi_status != WIFI_RADIO_ERROR_NONE) {
4851
mp_printf(&mp_plat_print, "%d", wifi_status);
4952
} else if (ipv4_address == 0) {
5053
serial_write_compressed(translate("No IP"));
5154
} else {
5255
uint8_t *octets = (uint8_t *)&ipv4_address;
5356
mp_printf(&mp_plat_print, "%d.%d.%d.%d", octets[0], octets[1], octets[2], octets[3]);
57+
// TODO: Use these unicode to show signal strength: ▂▄▆█
5458
}
5559
} else {
5660
serial_write_compressed(translate("off"));
@@ -75,12 +79,15 @@ void supervisor_start_web_workflow(void) {
7579
common_hal_wifi_init(false);
7680
common_hal_wifi_radio_set_enabled(&common_hal_wifi_radio_obj, true);
7781

82+
// TODO: Do our own scan so that we can find the channel we want before calling connect.
83+
// Otherwise, connect will do a full slow scan to pick the best AP.
84+
7885
// NUL terminate the strings because dotenv doesn't.
7986
ssid[ssid_len] = '\0';
8087
password[password_len] = '\0';
8188
wifi_status = common_hal_wifi_radio_connect(
8289
&common_hal_wifi_radio_obj, (uint8_t *)ssid, ssid_len, (uint8_t *)password, password_len,
83-
0, 1, NULL, 0);
90+
0, 0.1, NULL, 0);
8491

8592
if (wifi_status != WIFI_RADIO_ERROR_NONE) {
8693
common_hal_wifi_radio_set_enabled(&common_hal_wifi_radio_obj, false);

0 commit comments

Comments
 (0)