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/prj.conf b/samples/wifi/sta/prj.conf index ba3a789b443d..77f4b416b1af 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_EVENT_HANDLING=y CONFIG_WIFI_MGMT_EXT=y CONFIG_WIFI_CREDENTIALS=y diff --git a/samples/wifi/sta/src/main.c b/samples/wifi/sta/src/main.c index c18c40b3c8bf..bb5048253908 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,10 @@ 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; +K_SEM_DEFINE(wifi_ready_sem, 0, 1); +bool wifi_status; +bool is_suspended; + static struct { const struct shell *sh; union { @@ -247,25 +252,8 @@ 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) @@ -317,3 +305,84 @@ int main(void) return 0; } + +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) +{ + if (k_sem_take(&wifi_ready_sem, K_FOREVER) == 0) { + if (wifi_status) { + start_app(); + } + } +} + +void wifi_ready_cb(bool wifi_ready) +{ + if (wifi_ready) { + wifi_status = true; + LOG_DBG("Wi-Fi is ready"); + if (is_suspended) { + k_thread_resume(start_wifi_thread_id); + } + k_sem_give(&wifi_ready_sem); + } else { + wifi_status = false; + LOG_DBG("Wi-Fi is not ready"); + k_thread_suspend(start_wifi_thread_id); + is_suspended = true; + } +} + +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)); +} + +int main(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; + } + + wifi_status = false; + cb.wifi_ready_cb = wifi_ready_cb; + + LOG_INF("Waiting for Wi-Fi to be ready"); + + ret = register_wifi_ready_callback(cb, iface); + if (ret) { + LOG_ERR("Failed to register Wi-Fi ready callbacks %s", strerror(ret)); + return ret; + } + + net_mgmt_callback_init(); + + k_thread_start(start_wifi_thread_id); + + return ret; +}