Skip to content

Commit

Permalink
samples: wifi: sta: Wait for Wi-Fi readiness
Browse files Browse the repository at this point in the history
Update the sample application to check the Wi-Fi readiness
status using the wifi_ready library.

Signed-off-by: Triveni Danda <[email protected]>
  • Loading branch information
D-Triveni committed Jun 5, 2024
1 parent b8ef80c commit 8a18522
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 18 deletions.
6 changes: 6 additions & 0 deletions samples/wifi/sta/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions samples/wifi/sta/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
105 changes: 87 additions & 18 deletions samples/wifi/sta/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ LOG_MODULE_REGISTER(sta, CONFIG_LOG_DEFAULT_LEVEL);
#include <zephyr/drivers/gpio.h>

#include <net/wifi_mgmt_ext.h>
#include <net/wifi_ready.h>

#include <qspi_if.h>

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

0 comments on commit 8a18522

Please sign in to comment.