Skip to content

Commit ec8a42d

Browse files
authored
Merge pull request #4017 from anecdata/connect
ESP32-S2 wifi radio: check whether already connected before trying to connect
2 parents e3275be + f72c147 commit ec8a42d

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ jobs:
467467
id: idf-cache
468468
with:
469469
path: ${{ github.workspace }}/.idf_tools
470-
key: ${{ runner.os }}-idf-tools-${{ hashFiles('.git/modules/ports/esp32s2/esp-idf/HEAD') }}-20210121
470+
key: ${{ runner.os }}-idf-tools-${{ hashFiles('.git/modules/ports/esp32s2/esp-idf/HEAD') }}-20210122
471471
- name: Clone IDF submodules
472472
run: |
473473
(cd $IDF_PATH && git submodule update --init)

locale/circuitpython.pot

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3930,6 +3930,10 @@ msgstr ""
39303930
msgid "width must be greater than zero"
39313931
msgstr ""
39323932

3933+
#: ports/esp32s2/common-hal/wifi/Radio.c
3934+
msgid "wifi is not enabled"
3935+
msgstr ""
3936+
39333937
#: shared-bindings/_bleio/Adapter.c
39343938
msgid "window must be <= interval"
39353939
msgstr ""

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled) {
7272
}
7373
if (!self->started && enabled) {
7474
// esp_wifi_start() would default to soft-AP, thus setting it to station
75-
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
75+
start_station(self);
7676
ESP_ERROR_CHECK(esp_wifi_start());
7777
self->started = true;
7878
return;
@@ -89,7 +89,9 @@ mp_obj_t common_hal_wifi_radio_start_scanning_networks(wifi_radio_obj_t *self) {
8989
if (self->current_scan != NULL) {
9090
mp_raise_RuntimeError(translate("Already scanning for wifi networks"));
9191
}
92-
// check enabled
92+
if (!common_hal_wifi_radio_get_enabled(self)) {
93+
mp_raise_RuntimeError(translate("wifi is not enabled"));
94+
}
9395
start_station(self);
9496

9597
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
126128
}
127129

128130
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) {
129-
// check enabled
131+
if (!common_hal_wifi_radio_get_enabled(self)) {
132+
mp_raise_RuntimeError(translate("wifi is not enabled"));
133+
}
134+
135+
EventBits_t bits;
136+
// can't block since both bits are false after wifi_init
137+
// both bits are true after an existing connection stops
138+
bits = xEventGroupWaitBits(self->event_group_handle,
139+
WIFI_CONNECTED_BIT | WIFI_DISCONNECTED_BIT,
140+
pdTRUE,
141+
pdTRUE,
142+
0);
143+
if (((bits & WIFI_CONNECTED_BIT) != 0) &&
144+
!((bits & WIFI_DISCONNECTED_BIT) != 0)) {
145+
return WIFI_RADIO_ERROR_NONE;
146+
}
147+
// explicitly clear bits since xEventGroupWaitBits may have timed out
148+
xEventGroupClearBits(self->event_group_handle, WIFI_CONNECTED_BIT);
149+
xEventGroupClearBits(self->event_group_handle, WIFI_DISCONNECTED_BIT);
130150
start_station(self);
131151

132152
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
157177
self->retries_left = 5;
158178
esp_wifi_connect();
159179

160-
EventBits_t bits;
161180
do {
162181
RUN_BACKGROUND_TASKS;
163182
bits = xEventGroupWaitBits(self->event_group_handle,

0 commit comments

Comments
 (0)