Skip to content

Commit a01ff65

Browse files
committed
fix case of connecting when wifi is stopped
1 parent c524900 commit a01ff65

File tree

1 file changed

+13
-3
lines changed
  • ports/esp32s2/common-hal/wifi

1 file changed

+13
-3
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,25 @@ void common_hal_wifi_radio_set_hostname(wifi_radio_obj_t *self, const char *host
126126
}
127127

128128
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+
if (!common_hal_wifi_radio_get_enabled(self)) {
130+
mp_raise_RuntimeError(translate("Can't connect when wifi is not enabled"));
131+
}
132+
129133
EventBits_t bits;
134+
// can't block since both bits are false after wifi_init
135+
// both bits are true after an existing connection stops
130136
bits = xEventGroupWaitBits(self->event_group_handle,
131137
WIFI_CONNECTED_BIT | WIFI_DISCONNECTED_BIT,
132138
pdTRUE,
133139
pdTRUE,
134140
0);
135-
if ((bits & WIFI_CONNECTED_BIT) != 0) {
136-
return WIFI_RADIO_ERROR_NONE;
137-
}
141+
if (((bits & WIFI_CONNECTED_BIT) != 0) &&
142+
!((bits & WIFI_DISCONNECTED_BIT) != 0)) {
143+
return WIFI_RADIO_ERROR_NONE;
144+
}
145+
// explicitly clear bits since xEventGroupWaitBits may have timed out
146+
xEventGroupClearBits(self->event_group_handle, WIFI_CONNECTED_BIT);
147+
xEventGroupClearBits(self->event_group_handle, WIFI_DISCONNECTED_BIT);
138148
start_station(self);
139149

140150
wifi_config_t* config = &self->sta_config;

0 commit comments

Comments
 (0)