Skip to content

Commit cb66c24

Browse files
committed
drivers: wifi: esp32: Add WiFi events for station connect and disconnect
Added events for when the station iface is used to connect and disconnect. These events will feed the NET_EVENT_WIFI_CONNECT_RESULT and NET_EVENT_WIFI_DISCONNECT_RESULT events to send information to applications making use of them Signed-off-by: Humphrey Chiramba <[email protected]>
1 parent 9c9c080 commit cb66c24

File tree

1 file changed

+71
-15
lines changed

1 file changed

+71
-15
lines changed

drivers/wifi/esp32/src/esp_wifi_drv.c

+71-15
Original file line numberDiff line numberDiff line change
@@ -307,35 +307,91 @@ static void esp_wifi_handle_sta_connect_event(void *event_data)
307307
#endif
308308
}
309309

310-
static void esp_wifi_handle_sta_disconnect_event(void *event_data)
310+
static int handle_disconnect_event_while_not_connected(wifi_event_sta_disconnected_t *event)
311311
{
312-
wifi_event_sta_disconnected_t *event = (wifi_event_sta_disconnected_t *)event_data;
312+
int status = 0;
313313

314-
if (esp32_data.state == ESP32_STA_CONNECTED) {
315-
#if defined(CONFIG_ESP32_WIFI_STA_AUTO_DHCPV4)
316-
net_dhcpv4_stop(esp32_wifi_iface);
317-
#endif
318-
wifi_mgmt_raise_disconnect_result_event(esp32_wifi_iface, 0);
319-
} else {
320-
wifi_mgmt_raise_disconnect_result_event(esp32_wifi_iface, -1);
321-
}
322-
323-
LOG_DBG("Disconnect reason: %d", event->reason);
324314
switch (event->reason) {
325-
case WIFI_REASON_AUTH_EXPIRE:
315+
case WIFI_REASON_ROAMING:
316+
LOG_DBG("Roaming");
317+
break;
318+
case WIFI_REASON_ASSOC_LEAVE:
319+
LOG_DBG("Disconnect Requested");
320+
break;
326321
case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT:
327-
case WIFI_REASON_AUTH_FAIL:
328322
case WIFI_REASON_HANDSHAKE_TIMEOUT:
329-
case WIFI_REASON_MIC_FAILURE:
330323
LOG_DBG("STA Auth Error");
324+
status = WIFI_STATUS_CONN_WRONG_PASSWORD;
325+
break;
326+
case WIFI_REASON_DISCONN_INACTIVITY:
327+
case WIFI_REASON_TIMEOUT:
328+
LOG_DBG("STA Connection Timeout");
329+
status = WIFI_STATUS_CONN_TIMEOUT;
331330
break;
332331
case WIFI_REASON_NO_AP_FOUND:
333332
LOG_DBG("AP Not found");
333+
status = WIFI_STATUS_CONN_AP_NOT_FOUND;
334+
break;
335+
default:
336+
LOG_DBG("Generic Failure");
337+
status = WIFI_STATUS_CONN_FAIL;
338+
break;
339+
}
340+
341+
return status;
342+
}
343+
344+
static int handle_disconnect_event_while_connected(wifi_event_sta_disconnected_t *event)
345+
{
346+
#if defined(CONFIG_ESP32_WIFI_STA_AUTO_DHCPV4)
347+
net_dhcpv4_stop(esp32_wifi_iface);
348+
#endif
349+
350+
int status = 0;
351+
352+
switch (event->reason) {
353+
case WIFI_REASON_ROAMING:
354+
LOG_DBG("Roaming");
355+
break;
356+
case WIFI_REASON_AUTH_LEAVE:
357+
LOG_DBG("AP Requested Disconnect");
358+
status = WIFI_REASON_DISCONN_AP_LEAVING;
359+
break;
360+
case WIFI_REASON_ASSOC_LEAVE:
361+
LOG_DBG("Disconnect Was Requested");
362+
status = WIFI_REASON_DISCONN_USER_REQUEST;
363+
break;
364+
case WIFI_REASON_AUTH_EXPIRE:
365+
LOG_DBG("AP not active");
366+
status = WIFI_REASON_DISCONN_INACTIVITY;
334367
break;
335368
default:
369+
LOG_DBG("Generic Failure");
370+
status = WIFI_REASON_DISCONN_UNSPECIFIED;
336371
break;
337372
}
338373

374+
return status;
375+
}
376+
377+
static void esp_wifi_handle_sta_disconnect_event(void *event_data)
378+
{
379+
wifi_event_sta_disconnected_t *event = (wifi_event_sta_disconnected_t *)event_data;
380+
381+
LOG_DBG("Disconnect reason: %d", event->reason);
382+
383+
int disconn_status = 0;
384+
385+
if (esp32_data.state == ESP32_STA_CONNECTED) {
386+
disconn_status = handle_disconnect_event_while_connected(event);
387+
wifi_mgmt_raise_disconnect_result_event(esp32_wifi_iface, disconn_status);
388+
} else {
389+
disconn_status = handle_disconnect_event_while_not_connected(event);
390+
wifi_mgmt_raise_connect_result_event(esp32_wifi_iface, disconn_status);
391+
}
392+
393+
wifi_mgmt_raise_disconnect_complete_event(esp32_wifi_iface, WIFI_REASON_DISCONN_SUCCESS);
394+
339395
if (IS_ENABLED(CONFIG_ESP32_WIFI_STA_RECONNECT) &&
340396
(event->reason != WIFI_REASON_ASSOC_LEAVE)) {
341397
esp32_data.state = ESP32_STA_CONNECTING;

0 commit comments

Comments
 (0)