Skip to content

drivers: wifi: esp32: Add WiFi event information for station connect and disconnect #88519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 71 additions & 15 deletions drivers/wifi/esp32/src/esp_wifi_drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down