From cb66c24d5e46d761a1b719c741daee2f443e4caf Mon Sep 17 00:00:00 2001 From: Humphrey Chiramba Date: Wed, 23 Apr 2025 18:29:24 +0000 Subject: [PATCH] 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 --- drivers/wifi/esp32/src/esp_wifi_drv.c | 86 ++++++++++++++++++++++----- 1 file changed, 71 insertions(+), 15 deletions(-) diff --git a/drivers/wifi/esp32/src/esp_wifi_drv.c b/drivers/wifi/esp32/src/esp_wifi_drv.c index 06cc67813872..43c6a378c76a 100644 --- a/drivers/wifi/esp32/src/esp_wifi_drv.c +++ b/drivers/wifi/esp32/src/esp_wifi_drv.c @@ -307,35 +307,91 @@ static void esp_wifi_handle_sta_connect_event(void *event_data) #endif } -static void esp_wifi_handle_sta_disconnect_event(void *event_data) +static int handle_disconnect_event_while_not_connected(wifi_event_sta_disconnected_t *event) { - wifi_event_sta_disconnected_t *event = (wifi_event_sta_disconnected_t *)event_data; + int status = 0; - if (esp32_data.state == ESP32_STA_CONNECTED) { -#if defined(CONFIG_ESP32_WIFI_STA_AUTO_DHCPV4) - net_dhcpv4_stop(esp32_wifi_iface); -#endif - wifi_mgmt_raise_disconnect_result_event(esp32_wifi_iface, 0); - } else { - wifi_mgmt_raise_disconnect_result_event(esp32_wifi_iface, -1); - } - - LOG_DBG("Disconnect reason: %d", event->reason); switch (event->reason) { - case WIFI_REASON_AUTH_EXPIRE: + case WIFI_REASON_ROAMING: + LOG_DBG("Roaming"); + break; + case WIFI_REASON_ASSOC_LEAVE: + LOG_DBG("Disconnect Requested"); + break; case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT: - case WIFI_REASON_AUTH_FAIL: case WIFI_REASON_HANDSHAKE_TIMEOUT: - case WIFI_REASON_MIC_FAILURE: LOG_DBG("STA Auth Error"); + status = WIFI_STATUS_CONN_WRONG_PASSWORD; + break; + case WIFI_REASON_DISCONN_INACTIVITY: + case WIFI_REASON_TIMEOUT: + LOG_DBG("STA Connection Timeout"); + status = WIFI_STATUS_CONN_TIMEOUT; break; case WIFI_REASON_NO_AP_FOUND: LOG_DBG("AP Not found"); + status = WIFI_STATUS_CONN_AP_NOT_FOUND; + break; + default: + LOG_DBG("Generic Failure"); + status = WIFI_STATUS_CONN_FAIL; + break; + } + + return status; +} + +static int handle_disconnect_event_while_connected(wifi_event_sta_disconnected_t *event) +{ +#if defined(CONFIG_ESP32_WIFI_STA_AUTO_DHCPV4) + net_dhcpv4_stop(esp32_wifi_iface); +#endif + + int status = 0; + + switch (event->reason) { + case WIFI_REASON_ROAMING: + LOG_DBG("Roaming"); + break; + case WIFI_REASON_AUTH_LEAVE: + LOG_DBG("AP Requested Disconnect"); + status = WIFI_REASON_DISCONN_AP_LEAVING; + break; + case WIFI_REASON_ASSOC_LEAVE: + LOG_DBG("Disconnect Was Requested"); + status = WIFI_REASON_DISCONN_USER_REQUEST; + break; + case WIFI_REASON_AUTH_EXPIRE: + LOG_DBG("AP not active"); + status = WIFI_REASON_DISCONN_INACTIVITY; break; default: + LOG_DBG("Generic Failure"); + status = WIFI_REASON_DISCONN_UNSPECIFIED; break; } + return status; +} + +static void esp_wifi_handle_sta_disconnect_event(void *event_data) +{ + wifi_event_sta_disconnected_t *event = (wifi_event_sta_disconnected_t *)event_data; + + LOG_DBG("Disconnect reason: %d", event->reason); + + int disconn_status = 0; + + if (esp32_data.state == ESP32_STA_CONNECTED) { + disconn_status = handle_disconnect_event_while_connected(event); + wifi_mgmt_raise_disconnect_result_event(esp32_wifi_iface, disconn_status); + } else { + disconn_status = handle_disconnect_event_while_not_connected(event); + wifi_mgmt_raise_connect_result_event(esp32_wifi_iface, disconn_status); + } + + wifi_mgmt_raise_disconnect_complete_event(esp32_wifi_iface, WIFI_REASON_DISCONN_SUCCESS); + if (IS_ENABLED(CONFIG_ESP32_WIFI_STA_RECONNECT) && (event->reason != WIFI_REASON_ASSOC_LEAVE)) { esp32_data.state = ESP32_STA_CONNECTING;