From 7583c224ae77934c10a16dc164b1ecc028e242ff Mon Sep 17 00:00:00 2001 From: Triveni Danda Date: Mon, 26 Feb 2024 18:22:08 +0530 Subject: [PATCH] wfa-qt-control-app: Handle supplicant event Implement imperative check for supplicant readiness prior to QT thread initiation. Signed-off-by: Triveni Danda --- zephyr/CMakeLists.txt | 1 + zephyr/src/main.c | 9 +++++ zephyr/src/wpa_s_events.c | 82 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 zephyr/src/wpa_s_events.c diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt index 599ca3c..f9a9ccc 100644 --- a/zephyr/CMakeLists.txt +++ b/zephyr/CMakeLists.txt @@ -32,6 +32,7 @@ zephyr_include_directories( zephyr_library_sources( # Zephyr's port of the Indigo API + ${SOURCES_BASE}/zephyr/src/wpa_s_events.c ${SOURCES_BASE}/zephyr/src/main.c ${SOURCES_BASE}/zephyr/src/indigo_api_callback_dut.c ${SOURCES_BASE}/zephyr/src/vendor_specific_dut.c diff --git a/zephyr/src/main.c b/zephyr/src/main.c index f71b1b6..87ebaa2 100755 --- a/zephyr/src/main.c +++ b/zephyr/src/main.c @@ -19,6 +19,8 @@ LOG_MODULE_REGISTER(wfa_qt, CONFIG_WFA_QT_LOG_LEVEL); int control_socket_init(int port); void qt_main(void); +int wpa_supp_events_register(void); +int wait_for_wpa_s_ready(void); K_THREAD_DEFINE(qt_main_tid, CONFIG_WFA_QT_THREAD_STACK_SIZE, qt_main, @@ -36,6 +38,13 @@ static void print_welcome() { void qt_main(void) { int service_socket = -1; + int ret; + + ret = wpa_supp_events_register(); + ret = wait_for_wpa_s_ready(); + if (ret < 0) { + LOG_DBG("Control interface is not initialized"); + } /* Welcome message */ print_welcome(); diff --git a/zephyr/src/wpa_s_events.c b/zephyr/src/wpa_s_events.c new file mode 100644 index 0000000..353797c --- /dev/null +++ b/zephyr/src/wpa_s_events.c @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +/** @file + * @brief WPA SUPP events + */ + +#include +#include +#include +#include +#include +#include +#include +#include +LOG_MODULE_REGISTER(wpa_s_event, CONFIG_WFA_QT_LOG_LEVEL); + +#define WPA_SUPP_EVENTS (NET_EVENT_WPA_SUPP_READY) + +static struct net_mgmt_event_callback net_wpa_supp_cb; +struct wpa_supplicant *wpa_s = NULL; + +K_SEM_DEFINE(wpa_supp_ready_sem, 0, 1); + +static void handle_wpa_supp_ready(struct net_mgmt_event_callback *cb) +{ + int retry_count = 0; + +retry: + wpa_s = z_wpas_get_handle_by_ifname("wlan0"); + if (!wpa_s && retry_count++ < 5) { + LOG_ERR("%s: Unable to get wpa_s handle for %s\n", + __func__, "wlan0"); + goto retry; + } + + if (!wpa_s) { + LOG_ERR("%s: Unable to get wpa_s handle for %s\n", + __func__, "wlan0"); + } + + k_sem_give(&wpa_supp_ready_sem); + +} + +static void wpa_supp_event_handler(struct net_mgmt_event_callback *cb, + uint32_t mgmt_event, struct net_if *iface) +{ + switch (mgmt_event) { + case NET_EVENT_WPA_SUPP_READY: + LOG_INF("Supplicant is ready"); + handle_wpa_supp_ready(cb); + break; + default: + break; + } +} + +int wait_for_wpa_s_ready(void) +{ + k_sem_take(&wpa_supp_ready_sem, K_FOREVER); + + /* Check for ctrl_iface initialization */ + if (wpa_s->ctrl_iface->sock_pair[0] < 0) { + return -1; + } + + return 0; +} + +int wpa_supp_events_register(void) +{ + net_mgmt_init_event_callback(&net_wpa_supp_cb, + wpa_supp_event_handler, + WPA_SUPP_EVENTS); + net_mgmt_add_event_callback(&net_wpa_supp_cb); + + return 0; +}