From 34bb7af431e0faf89a9100638d8c919236e04225 Mon Sep 17 00:00:00 2001 From: Kapil Gupta Date: Thu, 28 Jul 2022 16:46:39 +0530 Subject: [PATCH] wpa_supplicant: Run eloop timer in ppTask context Currently eloop runs in timer context which may cause some inconsistent behavior. Add changes to run eloop in ppTask context --- components/wpa_supplicant/port/eloop.c | 20 +++++++++++++++++++- components/wpa_supplicant/test/test_eloop.c | 21 ++++++++++++++------- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/components/wpa_supplicant/port/eloop.c b/components/wpa_supplicant/port/eloop.c index 9e149d599a..4b2f598af1 100644 --- a/components/wpa_supplicant/port/eloop.c +++ b/components/wpa_supplicant/port/eloop.c @@ -16,6 +16,7 @@ #include "common.h" #include "list.h" #include "eloop.h" +#include "esp_wifi_driver.h" struct eloop_timeout { struct dl_list list; @@ -42,12 +43,29 @@ static void *eloop_data_lock = NULL; static struct eloop_data eloop; +static int eloop_run_wrapper(void *data) +{ + eloop_run(); + return 0; +} + +static void eloop_run_timer(void) +{ + /* Execute timers in pptask context to make it thread safe */ + wifi_ipc_config_t cfg; + + cfg.fn = eloop_run_wrapper; + cfg.arg = NULL; + cfg.arg_size = 0; + esp_wifi_ipc_internal(&cfg, false); +} + int eloop_init(void) { os_memset(&eloop, 0, sizeof(eloop)); dl_list_init(&eloop.timeout); os_timer_disarm(&eloop.eloop_timer); - os_timer_setfn(&eloop.eloop_timer, (ETSTimerFunc *)eloop_run, NULL); + os_timer_setfn(&eloop.eloop_timer, (ETSTimerFunc *)eloop_run_timer, NULL); eloop_data_lock = os_recursive_mutex_create(); diff --git a/components/wpa_supplicant/test/test_eloop.c b/components/wpa_supplicant/test/test_eloop.c index 5b5fc722d9..a10f3287c6 100644 --- a/components/wpa_supplicant/test/test_eloop.c +++ b/components/wpa_supplicant/test/test_eloop.c @@ -54,14 +54,20 @@ extern const wifi_osi_funcs_t *wifi_funcs; /* Check if eloop runs its timers correctly & in correct order */ TEST_CASE("Test eloop timers run", "[eloop]") { + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + cfg.nvs_enable = false; + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + TEST_ESP_OK(esp_wifi_stop()); + TEST_ESP_OK(esp_wifi_deinit()); + /* Reset memory stats since some is leaked during the first initialization */ + test_utils_record_free_mem(); + int execution_order[6] = {1, 5, 3, 0, 2, 4}; int index[6] = {0,1,2,3,4,5}; + t = 0; - wifi_funcs = WIFI_OSI_FUNCS_INITIALIZER(); - if (!wifi_funcs) { - TEST_ASSERT(1); - } - eloop_init(); + /* We need pptask to run eloop, wifi init will do that */ + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); os_get_reltime(&ts); for (int i = 0; i < 6; i++) { eloop_register_timeout(timeouts_sec[i], timeouts_usec[i], @@ -70,8 +76,9 @@ TEST_CASE("Test eloop timers run", "[eloop]") /* wait for all timers to run */ os_sleep(20, 0); - t = 0; /* check the execution order, this will also check whether they were fired at correct time */ TEST_ASSERT(memcmp(execution_order, executed_order, 6*sizeof(int)) == 0); - eloop_destroy(); + TEST_ESP_OK(esp_wifi_stop()); + TEST_ESP_OK(esp_wifi_deinit()); + os_sleep(3, 0); }