From 1d07d7f637bab7d9f2869726029b3968acd0c773 Mon Sep 17 00:00:00 2001 From: Triveni Danda Date: Fri, 31 May 2024 23:18:57 +0530 Subject: [PATCH 1/4] net: lib: Add wifi_ready library Add wifi_ready library to monitor supplicant events and ensure applications wait for Wi-Fi readiness both at boot time and also at run time. Implements SHEL-2741. Signed-off-by: Triveni Danda Signed-off-by: Chaitanya Tata --- doc/nrf/libraries/networking/wifi_ready.rst | 30 +++ include/net/wifi_ready.h | 75 ++++++++ subsys/net/lib/CMakeLists.txt | 1 + subsys/net/lib/Kconfig | 1 + subsys/net/lib/wifi_ready/CMakeLists.txt | 8 + subsys/net/lib/wifi_ready/Kconfig | 35 ++++ subsys/net/lib/wifi_ready/wifi_ready.c | 202 ++++++++++++++++++++ 7 files changed, 352 insertions(+) create mode 100644 doc/nrf/libraries/networking/wifi_ready.rst create mode 100644 include/net/wifi_ready.h create mode 100644 subsys/net/lib/wifi_ready/CMakeLists.txt create mode 100644 subsys/net/lib/wifi_ready/Kconfig create mode 100644 subsys/net/lib/wifi_ready/wifi_ready.c diff --git a/doc/nrf/libraries/networking/wifi_ready.rst b/doc/nrf/libraries/networking/wifi_ready.rst new file mode 100644 index 000000000000..ceacd025b3ab --- /dev/null +++ b/doc/nrf/libraries/networking/wifi_ready.rst @@ -0,0 +1,30 @@ +.. _lib_wifi_ready: + +Wi-Fi ready +########### + +.. contents:: + :local: + :depth: 2 + +The Wi-Fi ready library manages Wi-FiĀ® readiness for applications by handling supplicant ready and not ready events. + +Overview +******** + +The Wi-Fi ready library informs applications of Wi-Fi readiness by managing supplicant events, indicating when Wi-Fi is available for use. + +Configuration +************* + +To use this library, enable the :kconfig:option:`CONFIG_WIFI_READY_EVENT_HANDLING` Kconfig option. + +API documentation +***************** + +| Header file: :file:`include/net/wifi_ready.h` +| Source files: :file:`subsys/net/lib/wifi_ready` + +.. doxygengroup:: wifi_ready + :project: nrf + :members: diff --git a/include/net/wifi_ready.h b/include/net/wifi_ready.h new file mode 100644 index 000000000000..3db6738e118e --- /dev/null +++ b/include/net/wifi_ready.h @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + + +#ifndef __LIB_WIFI_READY_H__ +#define __LIB_WIFI_READY_H__ + +#include + +/** + * @defgroup wifi_ready Wi-Fi ready library + * @brief Library for handling Wi-Fi ready events. + * + * @{ + */ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Structure for storing a callback function to be called when the Wi-Fi is ready. + */ +typedef struct { + /** Callback function to be called when the Wi-Fi is ready. */ + void (*wifi_ready_cb)(bool wifi_ready); + /** Interface to which the callback is registered. */ + struct net_if *iface; + /** Only for internal use. */ + struct k_work item; +} wifi_ready_callback_t; + +/** + * @brief Register a callback to be called when the Wi-Fi is ready. + * + * @param cb Callback function to be called when the Wi-Fi is ready. + * The callback is called from NET_MGMT thread, so, care should be taken + * to avoid blocking the thread and also stack size should be considered. + * @param iface (optional) Interface to which the callback is registered. + * + * @return 0 if the callback was successfully registered, or a negative error code + * if the callback could not be registered. + * + * @retval -EINVAL if the callback is NULL. + * @retval -ENOMEM if the callback array is full. + * @retval -EALREADY if the callback is already registered. + */ + +int register_wifi_ready_callback(wifi_ready_callback_t cb, struct net_if *iface); +/** + * @brief Unregister a callback that was registered to be called when the Wi-Fi is ready. + * + * @param cb Callback function to be unregistered. + * @param iface (optional) Interface to which the callback is registered. + * + * @return 0 if the callback was successfully unregistered, or a negative error code + * if the callback could not be unregistered. + * + * @retval -EINVAL if the callback is NULL. + * @retval -ENOENT if the callback is not registered. + */ +int unregister_wifi_ready_callback(wifi_ready_callback_t cb, struct net_if *iface); + +#ifdef __cplusplus +} +#endif + +/** + * @} + */ + +#endif /* __LIB_WIFI_READY_H__*/ diff --git a/subsys/net/lib/CMakeLists.txt b/subsys/net/lib/CMakeLists.txt index 7efbb3f915b7..4210ec022851 100644 --- a/subsys/net/lib/CMakeLists.txt +++ b/subsys/net/lib/CMakeLists.txt @@ -31,6 +31,7 @@ add_subdirectory_ifdef(CONFIG_LWM2M_CLIENT_UTILS lwm2m_client_utils) add_subdirectory_ifdef(CONFIG_WIFI_CREDENTIALS wifi_credentials) add_subdirectory_ifdef(CONFIG_SOFTAP_WIFI_PROVISION softap_wifi_provision) add_subdirectory_ifdef(CONFIG_WIFI_MGMT_EXT wifi_mgmt_ext) +add_subdirectory_ifdef(CONFIG_WIFI_READY_LIB wifi_ready) add_subdirectory_ifdef(CONFIG_MQTT_HELPER mqtt_helper) add_subdirectory_ifdef(CONFIG_NRF_PROVISIONING nrf_provisioning) add_subdirectory_ifdef(CONFIG_NRF_MCUMGR_SMP_CLIENT mcumgr_smp_client) diff --git a/subsys/net/lib/Kconfig b/subsys/net/lib/Kconfig index 84cd65120ad6..1c062a10b239 100644 --- a/subsys/net/lib/Kconfig +++ b/subsys/net/lib/Kconfig @@ -43,6 +43,7 @@ rsource "lwm2m_client_utils/Kconfig" rsource "wifi_credentials/Kconfig" rsource "softap_wifi_provision/Kconfig" rsource "wifi_mgmt_ext/Kconfig" +rsource "wifi_ready/Kconfig" rsource "mqtt_helper/Kconfig" rsource "nrf_provisioning/Kconfig" rsource "mcumgr_smp_client/Kconfig" diff --git a/subsys/net/lib/wifi_ready/CMakeLists.txt b/subsys/net/lib/wifi_ready/CMakeLists.txt new file mode 100644 index 000000000000..f757eb5ac460 --- /dev/null +++ b/subsys/net/lib/wifi_ready/CMakeLists.txt @@ -0,0 +1,8 @@ +# +# Copyright (c) 2024 Nordic Semiconductor +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +zephyr_library_named(WIFI_READY_LIB) +zephyr_library_sources(wifi_ready.c) diff --git a/subsys/net/lib/wifi_ready/Kconfig b/subsys/net/lib/wifi_ready/Kconfig new file mode 100644 index 000000000000..cde2a309c889 --- /dev/null +++ b/subsys/net/lib/wifi_ready/Kconfig @@ -0,0 +1,35 @@ +# +# Copyright (c) 2024 Nordic Semiconductor +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +menuconfig WIFI_READY_LIB + bool "Wi-Fi ready event handling library" + select EXPERIMENTAL + depends on WPA_SUPP + depends on NET_MGMT + help + Enable WiFi ready management subsystem that allows the application to + register callbacks that are called when the WiFi is ready to be used and + when the WiFi is not ready to be used. + +if WIFI_READY_LIB + +module = WIFI_READY_LIB +module-str = wifi_ready_event_handling +source "subsys/logging/Kconfig.template.log_config" + +config WIFI_READY_MAX_CALLBACKS + int "Maximum number of Wi-Fi ready callbacks" + default 2 + help + Set the maximum number of Wi-Fi ready callbacks that can be registered + by the application. + +config WIFI_READY_INIT_PRIORITY + int "Wi-Fi ready initialization priority" + default 90 + help + Set the initialization priority of the Wi-Fi ready subsystem. +endif diff --git a/subsys/net/lib/wifi_ready/wifi_ready.c b/subsys/net/lib/wifi_ready/wifi_ready.c new file mode 100644 index 000000000000..cbe21e6e5631 --- /dev/null +++ b/subsys/net/lib/wifi_ready/wifi_ready.c @@ -0,0 +1,202 @@ +/* + * Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + +LOG_MODULE_REGISTER(wifi_ready, CONFIG_WIFI_READY_LIB_LOG_LEVEL); + +static wifi_ready_callback_t wifi_ready_callbacks[CONFIG_WIFI_READY_MAX_CALLBACKS]; +static unsigned char callback_count; +static K_MUTEX_DEFINE(wifi_ready_mutex); + +#define WPA_SUPP_EVENTS (NET_EVENT_WPA_SUPP_READY) | \ + (NET_EVENT_WPA_SUPP_NOT_READY) + +static struct net_mgmt_event_callback net_wpa_supp_cb; + +/* In case Wi-Fi is already ready, call the callbacks */ +static void wifi_ready_work_handler(struct k_work *item); + +static void call_wifi_ready_callbacks(bool ready, struct net_if *iface) +{ + int i; + + k_mutex_lock(&wifi_ready_mutex, K_FOREVER); + for (i = 0; i < ARRAY_SIZE(wifi_ready_callbacks); i++) { + if (wifi_ready_callbacks[i].wifi_ready_cb && + ((wifi_ready_callbacks[i].iface && + (wifi_ready_callbacks[i].iface == iface)) || + !wifi_ready_callbacks[i].iface)) { + wifi_ready_callbacks[i].wifi_ready_cb(ready); + } + } + k_mutex_unlock(&wifi_ready_mutex); +} + +static void wifi_ready_work_handler(struct k_work *item) +{ + wifi_ready_callback_t *cb = CONTAINER_OF(item, wifi_ready_callback_t, item); + + call_wifi_ready_callbacks(true, cb->iface); +} + +static void handle_wpa_supp_event(struct net_if *iface, bool ready) +{ + LOG_DBG("Supplicant event %s for iface %p", + ready ? "ready" : "not ready", iface); + + call_wifi_ready_callbacks(ready, iface); +} + +static void wpa_supp_event_handler(struct net_mgmt_event_callback *cb, + uint32_t mgmt_event, struct net_if *iface) +{ + ARG_UNUSED(cb); + + LOG_DBG("Event received: %d", mgmt_event); + switch (mgmt_event) { + case NET_EVENT_WPA_SUPP_READY: + handle_wpa_supp_event(iface, true); + break; + case NET_EVENT_WPA_SUPP_NOT_READY: + handle_wpa_supp_event(iface, false); + break; + default: + LOG_DBG("Unhandled event (%d)", mgmt_event); + break; + } +} + +int register_wifi_ready_callback(wifi_ready_callback_t cb, struct net_if *iface) +{ + int ret = 0, i; + unsigned int next_avail_idx = CONFIG_WIFI_READY_MAX_CALLBACKS; + struct net_if *wpas_iface = NULL; + + k_mutex_lock(&wifi_ready_mutex, K_FOREVER); + + if (!cb.wifi_ready_cb) { + LOG_ERR("Callback is NULL"); + ret = -EINVAL; + goto out; + } + + for (i = 0; i < ARRAY_SIZE(wifi_ready_callbacks); i++) { + if (wifi_ready_callbacks[i].wifi_ready_cb == NULL) { + next_avail_idx = i; + break; + } + } + + if (next_avail_idx == CONFIG_WIFI_READY_MAX_CALLBACKS) { + LOG_ERR("Reject callback registration, maximum count %d reached", + CONFIG_WIFI_READY_MAX_CALLBACKS); + ret = -ENOMEM; + goto out; + } + + /* Check if callback has already been registered for iface */ + for (i = 0; i < ARRAY_SIZE(wifi_ready_callbacks); i++) { + if (wifi_ready_callbacks[i].iface == iface && + wifi_ready_callbacks[i].wifi_ready_cb == cb.wifi_ready_cb) { + LOG_ERR("Callback has already registered for iface"); + ret = -EALREADY; + goto out; + } + } + + wifi_ready_callbacks[next_avail_idx].wifi_ready_cb = cb.wifi_ready_cb; + wifi_ready_callbacks[next_avail_idx].iface = iface; + + if (++callback_count == 1) { + 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); + } + + if (iface) { + wpas_iface = iface; + } else { + wpas_iface = net_if_get_first_wifi(); + if (!wpas_iface) { + LOG_ERR("Failed to get Wi-Fi interface"); + ret = -ENODEV; + goto out; + } + } + + /* In case Wi-Fi is already ready, call the callback */ + if (wifi_nm_get_instance_iface(wpas_iface)) { + k_work_submit(&wifi_ready_callbacks[next_avail_idx].item); + } + +out: + k_mutex_unlock(&wifi_ready_mutex); + return ret; + +} + + +int unregister_wifi_ready_callback(wifi_ready_callback_t cb, struct net_if *iface) +{ + int ret = 0, i; + bool found = false; + + k_mutex_lock(&wifi_ready_mutex, K_FOREVER); + + if (!cb.wifi_ready_cb) { + LOG_ERR("Callback is NULL"); + ret = -EINVAL; + goto out; + } + + for (i = 0; i < ARRAY_SIZE(wifi_ready_callbacks); i++) { + if (wifi_ready_callbacks[i].iface == iface && + wifi_ready_callbacks[i].wifi_ready_cb == cb.wifi_ready_cb) { + wifi_ready_callbacks[i].wifi_ready_cb = NULL; + wifi_ready_callbacks[i].iface = NULL; + found = true; + } + } + + if (!found) { + ret = -ENOENT; + goto out; + } + + if (--callback_count == 0) { + net_mgmt_del_event_callback(&net_wpa_supp_cb); + } + +out: + if (ret < 0) { + LOG_ERR("Failed to unregister callback: %d", ret); + } + k_mutex_unlock(&wifi_ready_mutex); + return ret; +} + +static int wifi_ready_init(void) +{ + /* Initialize the work items */ + for (int i = 0; i < ARRAY_SIZE(wifi_ready_callbacks); i++) { + k_work_init(&wifi_ready_callbacks[i].item, wifi_ready_work_handler); + } + + return 0; +} + +SYS_INIT(wifi_ready_init, APPLICATION, CONFIG_WIFI_READY_INIT_PRIORITY); From d0152babcaf6c8cf384a9634b206146043d15948 Mon Sep 17 00:00:00 2001 From: Triveni Danda Date: Sat, 1 Jun 2024 10:29:13 +0530 Subject: [PATCH 2/4] samples: wifi: sta: Use Wi-Fi ready library Update the sample application to use newly introduced Wi-Fi ready library, this ensures that application uses Wi-Fi only when it's available both at boot time and run time. Implement demo usage for SHEL-2741 and enable it by default, except for nRF52840DK as it as low RAM. Signed-off-by: Triveni Danda Signed-off-by: Chaitanya Tata --- samples/wifi/sta/Kconfig | 6 + samples/wifi/sta/README.rst | 27 ++++ .../wifi/sta/boards/nrf52840dk_nrf52840.conf | 1 + samples/wifi/sta/prj.conf | 1 + samples/wifi/sta/sample.yaml | 8 + samples/wifi/sta/src/main.c | 139 +++++++++++++++--- 6 files changed, 162 insertions(+), 20 deletions(-) create mode 100644 samples/wifi/sta/boards/nrf52840dk_nrf52840.conf diff --git a/samples/wifi/sta/Kconfig b/samples/wifi/sta/Kconfig index dcef2b3d5e11..2f738998501e 100644 --- a/samples/wifi/sta/Kconfig +++ b/samples/wifi/sta/Kconfig @@ -28,4 +28,10 @@ config STA_CONN_TIMEOUT_SEC Specify the connection timeout, in seconds. This is the overall timeout i.e., time to be waited for a station to connect and get an IP address. DHCP retries should be taken into account when setting this value. If the timeout is set to 0, the connection will not timeout. + +config STA_SAMPLE_START_WIFI_THREAD_STACK_SIZE + int "Stack size for Wi-Fi start thread" + default 4096 + help + Set the stack size for the Wi-Fi start thread. endmenu diff --git a/samples/wifi/sta/README.rst b/samples/wifi/sta/README.rst index e6248490d329..f0f851cc68e5 100644 --- a/samples/wifi/sta/README.rst +++ b/samples/wifi/sta/README.rst @@ -23,6 +23,9 @@ The sample can perform Wi-Fi operations such as connect and disconnect in the 2. Using this sample, the development kit can connect to the specified access point in :abbr:`STA (Station)` mode. +The sample uses the :ref:`lib_wifi_ready` library to check Wi-Fi readiness. +To use the :ref:`lib_wifi_ready` library, enable the :kconfig:option:`CONFIG_WIFI_READY_EVENT_HANDLING` Kconfig option. + User interface ************** @@ -180,6 +183,30 @@ Testing [00:00:07.720,245] sta: RSSI: -57 [00:00:07.720,245] sta: Static IP address: +RPU recovery +************ + +The RPU recovery mechanism is used to recover from the RPU (nRF70) hang. +This feature performs an interface reset (down and up), which triggers a RPU cold boot. +Application's network connection will be lost during the recovery process, and it is application's responsibility to reestablish the network connection. + +Testing +======= + +To test RPU recovery, you must build the sample with :kconfig:option:`CONFIG_SHELL` and :kconfig:option:`CONFIG_NRF700X_UTIL` kconfig options. + +#. Trigger RPU recovery using the following command: + + .. code-block:: console + + wifi_util rpu_recovery_test + + If RPU recovery is triggered, you should see an output similar to the following: + + .. code-block:: console + + RPU recovery triggered + Power management testing ************************ diff --git a/samples/wifi/sta/boards/nrf52840dk_nrf52840.conf b/samples/wifi/sta/boards/nrf52840dk_nrf52840.conf new file mode 100644 index 000000000000..2e3d175efa06 --- /dev/null +++ b/samples/wifi/sta/boards/nrf52840dk_nrf52840.conf @@ -0,0 +1 @@ +CONFIG_WIFI_READY_LIB=n diff --git a/samples/wifi/sta/prj.conf b/samples/wifi/sta/prj.conf index ba3a789b443d..d7bce8345ce7 100644 --- a/samples/wifi/sta/prj.conf +++ b/samples/wifi/sta/prj.conf @@ -8,6 +8,7 @@ CONFIG_WIFI_NRF700X=y # WPA supplicant CONFIG_WPA_SUPP=y +CONFIG_WIFI_READY_LIB=y CONFIG_WIFI_MGMT_EXT=y CONFIG_WIFI_CREDENTIALS=y diff --git a/samples/wifi/sta/sample.yaml b/samples/wifi/sta/sample.yaml index ab874918b2c8..738dd986e0af 100644 --- a/samples/wifi/sta/sample.yaml +++ b/samples/wifi/sta/sample.yaml @@ -10,6 +10,14 @@ tests: - nrf7002dk/nrf5340/cpuapp platform_allow: nrf7002dk/nrf5340/cpuapp tags: ci_build sysbuild + sample.nrf7002.sta.no_wifi_ready: + sysbuild: true + build_only: true + extra_args: CONFIG_WIFI_READY_LIB=n + integration_platforms: + - nrf7002dk/nrf5340/cpuapp + platform_allow: nrf7002dk/nrf5340/cpuapp + tags: ci_build sysbuild sample.nrf7001.sta: sysbuild: true build_only: true diff --git a/samples/wifi/sta/src/main.c b/samples/wifi/sta/src/main.c index c18c40b3c8bf..1a8334d802ad 100644 --- a/samples/wifi/sta/src/main.c +++ b/samples/wifi/sta/src/main.c @@ -25,6 +25,7 @@ LOG_MODULE_REGISTER(sta, CONFIG_LOG_DEFAULT_LEVEL); #include #include +#include #include @@ -52,6 +53,11 @@ static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios); static struct net_mgmt_event_callback wifi_shell_mgmt_cb; static struct net_mgmt_event_callback net_shell_mgmt_cb; +#ifdef CONFIG_WIFI_READY_LIB +static K_SEM_DEFINE(wifi_ready_state_changed_sem, 0, 1); +static bool wifi_ready_status; +#endif /* CONFIG_WIFI_READY_LIB */ + static struct { const struct shell *sh; union { @@ -247,31 +253,13 @@ int bytes_from_str(const char *str, uint8_t *bytes, size_t bytes_len) return 0; } -int main(void) +int start_app(void) { - memset(&context, 0, sizeof(context)); - - net_mgmt_init_event_callback(&wifi_shell_mgmt_cb, - wifi_mgmt_event_handler, - WIFI_SHELL_MGMT_EVENTS); - - net_mgmt_add_event_callback(&wifi_shell_mgmt_cb); - - - net_mgmt_init_event_callback(&net_shell_mgmt_cb, - net_mgmt_event_handler, - NET_EVENT_IPV4_DHCP_BOUND); - - net_mgmt_add_event_callback(&net_shell_mgmt_cb); - - LOG_INF("Starting %s with CPU frequency: %d MHz", CONFIG_BOARD, SystemCoreClock/MHZ(1)); - k_sleep(K_SECONDS(1)); - #if defined(CONFIG_BOARD_NRF7002DK_NRF7001_NRF5340_CPUAPP) || \ defined(CONFIG_BOARD_NRF7002DK_NRF5340_CPUAPP) if (strlen(CONFIG_NRF700X_QSPI_ENCRYPTION_KEY)) { - char key[QSPI_KEY_LEN_BYTES]; int ret; + char key[QSPI_KEY_LEN_BYTES]; ret = bytes_from_str(CONFIG_NRF700X_QSPI_ENCRYPTION_KEY, key, sizeof(key)); if (ret) { @@ -302,6 +290,25 @@ int main(void) CONFIG_NET_CONFIG_MY_IPV4_GW); while (1) { +#ifdef CONFIG_WIFI_READY_LIB + int ret; + + LOG_INF("Waiting for Wi-Fi to be ready"); + ret = k_sem_take(&wifi_ready_state_changed_sem, K_FOREVER); + if (ret) { + LOG_ERR("Failed to take semaphore: %d", ret); + return ret; + } + +check_wifi_ready: + if (!wifi_ready_status) { + LOG_INF("Wi-Fi is not ready"); + /* Perform any cleanup and stop using Wi-Fi and wait for + * Wi-Fi to be ready + */ + continue; + } +#endif /* CONFIG_WIFI_READY_LIB */ wifi_connect(); while (!context.connect_result) { @@ -311,9 +318,101 @@ int main(void) if (context.connected) { cmd_wifi_status(); +#ifdef CONFIG_WIFI_READY_LIB + ret = k_sem_take(&wifi_ready_state_changed_sem, K_FOREVER); + if (ret) { + LOG_ERR("Failed to take semaphore: %d", ret); + return ret; + } + goto check_wifi_ready; +#else k_sleep(K_FOREVER); +#endif /* CONFIG_WIFI_READY_LIB */ } } return 0; } + +#ifdef CONFIG_WIFI_READY_LIB +void start_wifi_thread(void); +#define THREAD_PRIORITY K_PRIO_COOP(CONFIG_NUM_COOP_PRIORITIES - 1) +K_THREAD_DEFINE(start_wifi_thread_id, CONFIG_STA_SAMPLE_START_WIFI_THREAD_STACK_SIZE, + start_wifi_thread, NULL, NULL, NULL, + THREAD_PRIORITY, 0, -1); + +void start_wifi_thread(void) +{ + start_app(); +} + +void wifi_ready_cb(bool wifi_ready) +{ + LOG_DBG("Is Wi-Fi ready?: %s", wifi_ready ? "yes" : "no"); + wifi_ready_status = wifi_ready; + k_sem_give(&wifi_ready_state_changed_sem); +} +#endif /* CONFIG_WIFI_READY_LIB */ + +void net_mgmt_callback_init(void) +{ + memset(&context, 0, sizeof(context)); + + net_mgmt_init_event_callback(&wifi_shell_mgmt_cb, + wifi_mgmt_event_handler, + WIFI_SHELL_MGMT_EVENTS); + + net_mgmt_add_event_callback(&wifi_shell_mgmt_cb); + + net_mgmt_init_event_callback(&net_shell_mgmt_cb, + net_mgmt_event_handler, + NET_EVENT_IPV4_DHCP_BOUND); + + net_mgmt_add_event_callback(&net_shell_mgmt_cb); + + LOG_INF("Starting %s with CPU frequency: %d MHz", CONFIG_BOARD, SystemCoreClock/MHZ(1)); + k_sleep(K_SECONDS(1)); +} + +#ifdef CONFIG_WIFI_READY_LIB +static int register_wifi_ready(void) +{ + int ret = 0; + wifi_ready_callback_t cb; + struct net_if *iface = net_if_get_first_wifi(); + + if (!iface) { + LOG_ERR("Failed to get Wi-Fi interface"); + return -1; + } + + cb.wifi_ready_cb = wifi_ready_cb; + + LOG_DBG("Registering Wi-Fi ready callbacks"); + ret = register_wifi_ready_callback(cb, iface); + if (ret) { + LOG_ERR("Failed to register Wi-Fi ready callbacks %s", strerror(ret)); + return ret; + } + + return ret; +} +#endif /* CONFIG_WIFI_READY_LIB */ + +int main(void) +{ + int ret = 0; + + net_mgmt_callback_init(); + +#ifdef CONFIG_WIFI_READY_LIB + ret = register_wifi_ready(); + if (ret) { + return ret; + } + k_thread_start(start_wifi_thread_id); +#else + start_app(); +#endif /* CONFIG_WIFI_READY_LIB */ + return ret; +} From ba9a2fe2406485894612ad400768bd4dc1472ef8 Mon Sep 17 00:00:00 2001 From: Chaitanya Tata Date: Thu, 6 Jun 2024 13:04:47 +0530 Subject: [PATCH 3/4] doc: changelog: Add an entry for Wi-Fi ready library Add Wi-Fi library to networking libraries section. Signed-off-by: Chaitanya Tata --- .../releases/release-notes-changelog.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst b/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst index d08f870ffd71..cb944e991243 100644 --- a/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst +++ b/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst @@ -898,7 +898,10 @@ Modem libraries Libraries for networking ------------------------ -* Added the :ref:`lib_softap_wifi_provision` library. +* Added: + + * :ref:`lib_softap_wifi_provision` library. + * :ref:`lib_wifi_ready` library. * :ref:`lib_wifi_credentials` library: From f1c2c7b3fa2b6b04a571e0f611f7f749d46b605b Mon Sep 17 00:00:00 2001 From: Triveni Danda Date: Wed, 5 Jun 2024 18:08:46 +0530 Subject: [PATCH 4/4] doc: changelog: Add entry for station sample Add changelog entry for the STA sample to migrate to Wi-Fi ready library. Signed-off-by: Triveni Danda --- .../releases/release-notes-changelog.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst b/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst index cb944e991243..0246b1140e51 100644 --- a/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst +++ b/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst @@ -788,6 +788,10 @@ Wi-Fi samples that used :ref:`zephyr:bluetooth-hci-ipc-sample` or :ref:`zephyr:n * Added the :ref:`wifi_promiscuous_sample` sample that demonstrates how to set Promiscuous mode, establish a connection to an Access Point (AP), analyze incoming Wi-Fi packets, and print packet statistics. +* :ref:`wifi_station_sample` sample: + + * Modified to use the :ref:`lib_wifi_ready` library to manage the Wi-Fi use. + Other samples -------------