From 87bc1ba4bbfd8da11b3ce4d9680910d5287d445a Mon Sep 17 00:00:00 2001 From: Triveni Danda Date: Thu, 22 Feb 2024 20:36:23 +0530 Subject: [PATCH] samples: wifi: wfa-qt: Add supplicant events Add changes to handle supplicant events. Signed-off-by: Triveni Danda --- samples/wifi/wfa_qt_app/CMakeLists.txt | 6 +- samples/wifi/wfa_qt_app/src/events.c | 89 +++++++++++++++++++++++ samples/wifi/wfa_qt_app/src/qt_app_main.c | 7 ++ 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 samples/wifi/wfa_qt_app/src/events.c diff --git a/samples/wifi/wfa_qt_app/CMakeLists.txt b/samples/wifi/wfa_qt_app/CMakeLists.txt index 9689236a22e0..065d4b27a410 100644 --- a/samples/wifi/wfa_qt_app/CMakeLists.txt +++ b/samples/wifi/wfa_qt_app/CMakeLists.txt @@ -9,4 +9,8 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(nrf_wfa_qt_app) -target_sources(app PRIVATE src/qt_app_main.c) +target_compile_definitions(app PRIVATE -DCONFIG_CTRL_IFACE_ZEPHYR=y) +target_sources(app PRIVATE + src/events.c + src/qt_app_main.c) + diff --git a/samples/wifi/wfa_qt_app/src/events.c b/samples/wifi/wfa_qt_app/src/events.c new file mode 100644 index 000000000000..7797bf5a14e8 --- /dev/null +++ b/samples/wifi/wfa_qt_app/src/events.c @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +/** @file + * @brief WPA SUPP events + */ + +#include +#include +#include +#include +#include +#include + +#define WPA_SUPP_EVENTS (NET_EVENT_WPA_SUPP_READY | \ + NET_EVENT_WPA_SUPP_NOT_READY | \ + NET_EVENT_WPA_SUPP_IFACE_ADDED | \ + NET_EVENT_WPA_SUPP_CMD_IFACE_REMOVING | \ + NET_EVENT_WPA_SUPP_CMD_IFACE_REMOVED) + +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) { + printf("%s: Unable to get wpa_s handle for %s\n", + __func__, "wlan0"); + goto retry; + } + + if (!wpa_s) { + printf("%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: + printf("Supplicant is ready"); + handle_wpa_supp_ready(cb); + break; + case NET_EVENT_WPA_SUPP_NOT_READY: + printf("Supplicant is not ready"); + break; + case NET_EVENT_WPA_SUPP_IFACE_ADDED: + printf("Interface added"); + break; + case NET_EVENT_WPA_SUPP_CMD_IFACE_REMOVING: + printf("Interface is removing"); + break; + case NET_EVENT_WPA_SUPP_CMD_IFACE_REMOVED: + printf("Interface removed"); + break; + default: + break; + } +} + +int wpa_supp_events_init(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); + + 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; +} diff --git a/samples/wifi/wfa_qt_app/src/qt_app_main.c b/samples/wifi/wfa_qt_app/src/qt_app_main.c index b5c6ff2a2e9d..c4751b0713ad 100644 --- a/samples/wifi/wfa_qt_app/src/qt_app_main.c +++ b/samples/wifi/wfa_qt_app/src/qt_app_main.c @@ -33,9 +33,12 @@ int init_usb(void) } #endif /* CONFIG_USB_DEVICE_STACK */ +int check_events(void); + int main(void) { struct in_addr addr; + int ret; #if defined(CLOCK_FEATURE_HFCLK_DIVIDE_PRESENT) || NRF_CLOCK_HAS_HFCLK192M /* For now hardcode to 128MHz */ @@ -133,6 +136,10 @@ int main(void) net_config_init_app(dev, "Initializing network"); #endif + ret = wpa_supp_events_init(); + if (ret) { + return -1; + } return 0; }