Skip to content

Commit 039f196

Browse files
glenn20dpgeorge
authored andcommitted
esp32/modnetwork: Fix isconnected() when using static IP config.
Currently <WLAN>.isconnected() always returns True if a static IP is set, regardless of the state of the connection. This patch introduces a new flag 'wifi_sta_connected' which is set in event_handler() when GOT_IP event is received and reset when DISCONNECTED event is received (unless re-connect is successful). isconnected() now simply returns the status of this flag (for STA_IF). The pre-existing flag misleadingly named 'wifi_sta_connected" is also renamed to 'wifi_sta_connect_requested'. Fixes issue adafruit#3837
1 parent a12d046 commit 039f196

1 file changed

Lines changed: 19 additions & 7 deletions

File tree

ports/esp32/modnetwork.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ static bool wifi_started = false;
123123

124124
// Set to "true" if the STA interface is requested to be connected by the
125125
// user, used for automatic reassociation.
126+
static bool wifi_sta_connect_requested = false;
127+
128+
// Set to "true" if the STA interface is connected to wifi and has IP address.
126129
static bool wifi_sta_connected = false;
127130

128131
// This function is called by the system-event task and so runs in a different
@@ -132,8 +135,12 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) {
132135
case SYSTEM_EVENT_STA_START:
133136
ESP_LOGI("wifi", "STA_START");
134137
break;
138+
case SYSTEM_EVENT_STA_CONNECTED:
139+
ESP_LOGI("network", "CONNECTED");
140+
break;
135141
case SYSTEM_EVENT_STA_GOT_IP:
136142
ESP_LOGI("network", "GOT_IP");
143+
wifi_sta_connected = true;
137144
break;
138145
case SYSTEM_EVENT_STA_DISCONNECTED: {
139146
// This is a workaround as ESP32 WiFi libs don't currently
@@ -151,26 +158,33 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) {
151158
break;
152159
case WIFI_REASON_AUTH_FAIL:
153160
message = "\nauthentication failed";
154-
wifi_sta_connected = false;
161+
wifi_sta_connect_requested = false;
155162
break;
156163
default:
157164
// Let other errors through and try to reconnect.
158165
break;
159166
}
160167
ESP_LOGI("wifi", "STA_DISCONNECTED, reason:%d%s", disconn->reason, message);
161168

162-
if (wifi_sta_connected) {
169+
bool reconnected = false;
170+
if (wifi_sta_connect_requested) {
163171
wifi_mode_t mode;
164172
if (esp_wifi_get_mode(&mode) == ESP_OK) {
165173
if (mode & WIFI_MODE_STA) {
166174
// STA is active so attempt to reconnect.
167175
esp_err_t e = esp_wifi_connect();
168176
if (e != ESP_OK) {
169177
ESP_LOGI("wifi", "error attempting to reconnect: 0x%04x", e);
178+
} else {
179+
reconnected = true;
170180
}
171181
}
172182
}
173183
}
184+
if (wifi_sta_connected && !reconnected) {
185+
// If already connected and we fail to reconnect
186+
wifi_sta_connected = false;
187+
}
174188
break;
175189
}
176190
default:
@@ -283,15 +297,15 @@ STATIC mp_obj_t esp_connect(size_t n_args, const mp_obj_t *args) {
283297
MP_THREAD_GIL_EXIT();
284298
ESP_EXCEPTIONS( esp_wifi_connect() );
285299
MP_THREAD_GIL_ENTER();
286-
wifi_sta_connected = true;
300+
wifi_sta_connect_requested = true;
287301

288302
return mp_const_none;
289303
}
290304

291305
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_connect_obj, 1, 7, esp_connect);
292306

293307
STATIC mp_obj_t esp_disconnect(mp_obj_t self_in) {
294-
wifi_sta_connected = false;
308+
wifi_sta_connect_requested = false;
295309
ESP_EXCEPTIONS( esp_wifi_disconnect() );
296310
return mp_const_none;
297311
}
@@ -370,9 +384,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_scan_obj, esp_scan);
370384
STATIC mp_obj_t esp_isconnected(mp_obj_t self_in) {
371385
wlan_if_obj_t *self = MP_OBJ_TO_PTR(self_in);
372386
if (self->if_id == WIFI_IF_STA) {
373-
tcpip_adapter_ip_info_t info;
374-
tcpip_adapter_get_ip_info(WIFI_IF_STA, &info);
375-
return mp_obj_new_bool(info.ip.addr != 0);
387+
return mp_obj_new_bool(wifi_sta_connected);
376388
} else {
377389
wifi_sta_list_t sta;
378390
esp_wifi_ap_get_sta_list(&sta);

0 commit comments

Comments
 (0)