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.
This commit is contained in:
Scott Shawcroft 2022-06-13 15:38:19 -07:00
parent 1a3d2a5b5e
commit dc794f91c0
No known key found for this signature in database
GPG Key ID: 0DFD512649C052DA
2 changed files with 17 additions and 2 deletions

View File

@ -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) {

View File

@ -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);