Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32-S2 wifi radio: check whether already connected before trying to connect #4017

Merged
merged 6 commits into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -3906,6 +3906,10 @@ msgstr ""
msgid "width must be greater than zero"
msgstr ""

#: ports/esp32s2/common-hal/wifi/Radio.c
msgid "wifi is not enabled"
msgstr ""

#: shared-bindings/_bleio/Adapter.c
msgid "window must be <= interval"
msgstr ""
Expand Down
27 changes: 23 additions & 4 deletions ports/esp32s2/common-hal/wifi/Radio.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled) {
}
if (!self->started && enabled) {
// esp_wifi_start() would default to soft-AP, thus setting it to station
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
start_station(self);
ESP_ERROR_CHECK(esp_wifi_start());
self->started = true;
return;
Expand All @@ -89,7 +89,9 @@ mp_obj_t common_hal_wifi_radio_start_scanning_networks(wifi_radio_obj_t *self) {
if (self->current_scan != NULL) {
mp_raise_RuntimeError(translate("Already scanning for wifi networks"));
}
// check enabled
if (!common_hal_wifi_radio_get_enabled(self)) {
mp_raise_RuntimeError(translate("wifi is not enabled"));
}
start_station(self);

wifi_scannednetworks_obj_t *scan = m_new_obj(wifi_scannednetworks_obj_t);
Expand Down Expand Up @@ -126,7 +128,25 @@ void common_hal_wifi_radio_set_hostname(wifi_radio_obj_t *self, const char *host
}

wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t* ssid, size_t ssid_len, uint8_t* password, size_t password_len, uint8_t channel, mp_float_t timeout, uint8_t* bssid, size_t bssid_len) {
// check enabled
if (!common_hal_wifi_radio_get_enabled(self)) {
mp_raise_RuntimeError(translate("wifi is not enabled"));
}

EventBits_t bits;
// can't block since both bits are false after wifi_init
// both bits are true after an existing connection stops
bits = xEventGroupWaitBits(self->event_group_handle,
WIFI_CONNECTED_BIT | WIFI_DISCONNECTED_BIT,
pdTRUE,
pdTRUE,
0);
if (((bits & WIFI_CONNECTED_BIT) != 0) &&
!((bits & WIFI_DISCONNECTED_BIT) != 0)) {
return WIFI_RADIO_ERROR_NONE;
}
// explicitly clear bits since xEventGroupWaitBits may have timed out
xEventGroupClearBits(self->event_group_handle, WIFI_CONNECTED_BIT);
xEventGroupClearBits(self->event_group_handle, WIFI_DISCONNECTED_BIT);
start_station(self);

wifi_config_t* config = &self->sta_config;
Expand Down Expand Up @@ -157,7 +177,6 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
self->retries_left = 5;
esp_wifi_connect();

EventBits_t bits;
do {
RUN_BACKGROUND_TASKS;
bits = xEventGroupWaitBits(self->event_group_handle,
Expand Down