summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Halbert <halbert@adafruit.com>2021-01-22 05:48:18 -0500
committerGitHub <noreply@github.com>2021-01-22 05:48:18 -0500
commitec8a42d7c9f2cf8d66d6a1b7d87f62cc4800f40a (patch)
treea81a0c0c9a8c5f203ead2fa98f33bea42cdd69f0
parente3275be8b1ad28d0296733876cf119f134680e81 (diff)
parentf72c1474c50b8c3b3c3d5459df60bfcb63c1c057 (diff)
Merge pull request #4017 from anecdata/connect
ESP32-S2 wifi radio: check whether already connected before trying to connect
-rw-r--r--.github/workflows/build.yml2
-rw-r--r--locale/circuitpython.pot4
-rw-r--r--ports/esp32s2/common-hal/wifi/Radio.c27
3 files changed, 28 insertions, 5 deletions
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index cc5ad4f7b..0dbccdf4d 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -467,7 +467,7 @@ jobs:
id: idf-cache
with:
path: ${{ github.workspace }}/.idf_tools
- key: ${{ runner.os }}-idf-tools-${{ hashFiles('.git/modules/ports/esp32s2/esp-idf/HEAD') }}-20210121
+ key: ${{ runner.os }}-idf-tools-${{ hashFiles('.git/modules/ports/esp32s2/esp-idf/HEAD') }}-20210122
- name: Clone IDF submodules
run: |
(cd $IDF_PATH && git submodule update --init)
diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot
index 259cd34d4..3ffc31cc6 100644
--- a/locale/circuitpython.pot
+++ b/locale/circuitpython.pot
@@ -3930,6 +3930,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 ""
diff --git a/ports/esp32s2/common-hal/wifi/Radio.c b/ports/esp32s2/common-hal/wifi/Radio.c
index 930230088..574bab0a1 100644
--- a/ports/esp32s2/common-hal/wifi/Radio.c
+++ b/ports/esp32s2/common-hal/wifi/Radio.c
@@ -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;
@@ -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);
@@ -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;
@@ -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,