Skip to content

Commit dbced75

Browse files
glenn20dpgeorge
authored andcommitted
esp32/network_wlan: Wait for STA/AP START/STOP event in wlan.active.
This is a fix for commit bccbaa9: - Should only wait for WIFI_EVENT_STA_START when invoked on the STA_IF interface. - The WIFI_EVENT_STA_START event is generated every time the STA_IF interface is set active(True) and it was previously inactive, ie. not only after calling esp_wifi_start(). - Also wait for WIFI_EVENT_STA_STOP when deactivating the interface. - Also wait for relevant AP events. Fixes issue #11910. Signed-off-by: Glenn Moloney <glenn.moloney@gmail.com> Signed-off-by: Damien George <damien@micropython.org>
1 parent 2c67671 commit dbced75

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

ports/esp32/modnetwork.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ typedef struct _base_if_obj_t {
4444
mp_obj_base_t base;
4545
esp_interface_t if_id;
4646
esp_netif_t *netif;
47+
volatile bool active;
4748
} base_if_obj_t;
4849

4950
extern const mp_obj_type_t esp_network_wlan_type;

ports/esp32/network_lan.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
typedef struct _lan_if_obj_t {
4949
base_if_obj_t base;
5050
bool initialized;
51-
bool active;
5251
int8_t mdc_pin;
5352
int8_t mdio_pin;
5453
int8_t phy_power_pin;
@@ -295,7 +294,7 @@ STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar
295294

296295
esp_err_t esp_err = esp_eth_driver_install(&config, &self->eth_handle);
297296
if (esp_err == ESP_OK) {
298-
self->active = false;
297+
self->base.active = false;
299298
self->initialized = true;
300299
} else {
301300
if (esp_err == ESP_ERR_INVALID_ARG) {
@@ -322,19 +321,19 @@ STATIC mp_obj_t lan_active(size_t n_args, const mp_obj_t *args) {
322321

323322
if (n_args > 1) {
324323
if (mp_obj_is_true(args[1])) {
325-
self->active = (esp_eth_start(self->eth_handle) == ESP_OK);
326-
if (!self->active) {
324+
self->base.active = (esp_eth_start(self->eth_handle) == ESP_OK);
325+
if (!self->base.active) {
327326
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ethernet enable failed"));
328327
}
329328
} else {
330-
self->active = !(esp_eth_stop(self->eth_handle) == ESP_OK);
331-
if (self->active) {
329+
self->base.active = !(esp_eth_stop(self->eth_handle) == ESP_OK);
330+
if (self->base.active) {
332331
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ethernet disable failed"));
333332
}
334333
}
335334
}
336335

337-
return mp_obj_new_bool(self->active);
336+
return mp_obj_new_bool(self->base.active);
338337
}
339338
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(lan_active_obj, 1, 2, lan_active);
340339

@@ -345,7 +344,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_status_obj, lan_status);
345344

346345
STATIC mp_obj_t lan_isconnected(mp_obj_t self_in) {
347346
lan_if_obj_t *self = MP_OBJ_TO_PTR(self_in);
348-
return self->active ? mp_obj_new_bool(self->phy->get_link(self->phy) == ETH_LINK_UP) : mp_const_false;
347+
return self->base.active ? mp_obj_new_bool(self->phy->get_link(self->phy) == ETH_LINK_UP) : mp_const_false;
349348
}
350349
STATIC MP_DEFINE_CONST_FUN_OBJ_1(lan_isconnected_obj, lan_isconnected);
351350

ports/esp32/network_wlan.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,22 @@ static bool mdns_initialised = false;
7676
#endif
7777

7878
static uint8_t conf_wifi_sta_reconnects = 0;
79-
static volatile uint8_t wifi_sta_reconnects;
79+
static uint8_t wifi_sta_reconnects;
8080

8181
// This function is called by the system-event task and so runs in a different
8282
// thread to the main MicroPython task. It must not raise any Python exceptions.
8383
static void network_wlan_wifi_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
8484
switch (event_id) {
8585
case WIFI_EVENT_STA_START:
8686
ESP_LOGI("wifi", "STA_START");
87+
wlan_sta_obj.active = true;
8788
wifi_sta_reconnects = 0;
8889
break;
8990

91+
case WIFI_EVENT_STA_STOP:
92+
wlan_sta_obj.active = false;
93+
break;
94+
9095
case WIFI_EVENT_STA_CONNECTED:
9196
ESP_LOGI("network", "CONNECTED");
9297
break;
@@ -140,6 +145,15 @@ static void network_wlan_wifi_event_handler(void *event_handler_arg, esp_event_b
140145
}
141146
break;
142147
}
148+
149+
case WIFI_EVENT_AP_START:
150+
wlan_ap_obj.active = true;
151+
break;
152+
153+
case WIFI_EVENT_AP_STOP:
154+
wlan_ap_obj.active = false;
155+
break;
156+
143157
default:
144158
break;
145159
}
@@ -184,10 +198,12 @@ void esp_initialise_wifi(void) {
184198
wlan_sta_obj.base.type = &esp_network_wlan_type;
185199
wlan_sta_obj.if_id = ESP_IF_WIFI_STA;
186200
wlan_sta_obj.netif = esp_netif_create_default_wifi_sta();
201+
wlan_sta_obj.active = false;
187202

188203
wlan_ap_obj.base.type = &esp_network_wlan_type;
189204
wlan_ap_obj.if_id = ESP_IF_WIFI_AP;
190205
wlan_ap_obj.netif = esp_netif_create_default_wifi_ap();
206+
wlan_ap_obj.active = false;
191207

192208
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
193209
ESP_LOGD("modnetwork", "Initializing WiFi");
@@ -237,16 +253,15 @@ STATIC mp_obj_t network_wlan_active(size_t n_args, const mp_obj_t *args) {
237253
} else {
238254
esp_exceptions(esp_wifi_set_mode(mode));
239255
if (!wifi_started) {
240-
// WIFI_EVENT_STA_START must be received before esp_wifi_connect() can be called.
241-
// Use the `wifi_sta_reconnects` variable to detect that event.
242-
wifi_sta_reconnects = 1;
243256
esp_exceptions(esp_wifi_start());
244257
wifi_started = true;
245-
while (wifi_sta_reconnects != 0) {
246-
MICROPY_EVENT_POLL_HOOK;
247-
}
248258
}
249259
}
260+
261+
// Wait for the interface to be in the correct state.
262+
while (self->active != active) {
263+
MICROPY_EVENT_POLL_HOOK;
264+
}
250265
}
251266

252267
return (mode & bit) ? mp_const_true : mp_const_false;

0 commit comments

Comments
 (0)