From 95b916d1045e5a5d156a325fec4ad584514b719a Mon Sep 17 00:00:00 2001 From: Sylvio Alves Date: Thu, 27 May 2021 06:34:02 -0300 Subject: [PATCH] drivers: wifi: esp32: fix reconnect issue Device won't reconnect automatically even if AP station is available. This fix adds the carrier event, indicating that network is present again enabling DHCP bound event. Also, internal wifi event callback was added into wifi driver to enable proper event handling. Update west.yml to bring exposed wifi event callback. Signed-off-by: Sylvio Alves Fixes #33843 --- drivers/wifi/esp32/src/esp_wifi_drv.c | 57 +++++++++++++++++++++------ west.yml | 2 +- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/drivers/wifi/esp32/src/esp_wifi_drv.c b/drivers/wifi/esp32/src/esp_wifi_drv.c index 62a65a664c3..c8e0943762d 100644 --- a/drivers/wifi/esp32/src/esp_wifi_drv.c +++ b/drivers/wifi/esp32/src/esp_wifi_drv.c @@ -41,6 +41,23 @@ struct esp32_wifi_runtime { #endif }; +static void esp_wifi_event_task(void); + +K_MSGQ_DEFINE(esp_wifi_msgq, sizeof(system_event_t), 10, 4); +K_THREAD_DEFINE(esp_wifi_event_tid, CONFIG_ESP32_WIFI_EVENT_TASK_STACK_SIZE, + esp_wifi_event_task, NULL, NULL, NULL, CONFIG_ESP32_WIFI_EVENT_TASK_PRIO, 0, 0); + +/* internal wifi library callback function */ +esp_err_t esp_event_send_internal(esp_event_base_t event_base, + int32_t event_id, + void *event_data, + size_t event_data_size, + uint32_t ticks_to_wait) +{ + k_msgq_put(&esp_wifi_msgq, (int32_t *)&event_id, K_FOREVER); + return ESP_OK; +} + static int eth_esp32_send(const struct device *dev, struct net_pkt *pkt) { const int pkt_len = net_pkt_get_len(pkt); @@ -92,18 +109,37 @@ pkt_unref: return ESP_FAIL; } -/* internally used by wifi hal layer */ -void esp_wifi_set_net_state(bool state) +void esp_wifi_event_task(void) { - if (esp32_wifi_iface == NULL) { - LOG_ERR("network interface unavailable"); - return; - } + int32_t event_id; - if (state) { - net_if_up(esp32_wifi_iface); - } else { - net_if_down(esp32_wifi_iface); + while (1) { + k_msgq_get(&esp_wifi_msgq, &event_id, K_FOREVER); + + switch (event_id) { + case ESP32_WIFI_EVENT_STA_START: + LOG_INF("WIFI_EVENT_STA_START"); + net_if_up(esp32_wifi_iface); + break; + case ESP32_WIFI_EVENT_STA_STOP: + LOG_INF("WIFI_EVENT_STA_STOP"); + net_if_down(esp32_wifi_iface); + break; + case ESP32_WIFI_EVENT_STA_CONNECTED: + LOG_INF("WIFI_EVENT_STA_CONNECTED"); + net_eth_carrier_on(esp32_wifi_iface); + break; + case ESP32_WIFI_EVENT_STA_DISCONNECTED: + LOG_INF("WIFI_EVENT_STA_DISCONNECTED"); + net_eth_carrier_off(esp32_wifi_iface); + + if (IS_ENABLED(CONFIG_ESP32_WIFI_STA_RECONNECT)) { + esp_wifi_connect(); + } + break; + default: + break; + } } } @@ -138,7 +174,6 @@ static struct net_stats_eth *eth_esp32_stats(const struct device *dev) static int eth_esp32_dev_init(const struct device *dev) { esp_timer_init(); - esp_event_init(); wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT(); esp_err_t ret = esp_wifi_init(&config); diff --git a/west.yml b/west.yml index 370d7127ae2..5bb4670324d 100644 --- a/west.yml +++ b/west.yml @@ -42,7 +42,7 @@ manifest: path: modules/lib/civetweb - name: hal_espressif west-commands: west/west-commands.yml - revision: 086d305202390589a206523c239e604718759402 + revision: 22e757632677e3579e6f20bb9955fffb2e1b3e1c path: modules/hal/espressif - name: fatfs revision: 1d1fcc725aa1cb3c32f366e0c53d7490d0fe1109