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/boards b/samples/wifi/sta/boards new file mode 100644 index 000000000000..d5dc2cb8c61c --- /dev/null +++ b/samples/wifi/sta/boards @@ -0,0 +1,2 @@ +# No RAM for new features +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; +}