Skip to content

Commit

Permalink
samples: wifi: sta: Use Wi-Fi ready library
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Chaitanya Tata <[email protected]>
  • Loading branch information
D-Triveni committed Jun 6, 2024
1 parent 22650be commit 9ceab9b
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 20 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
2 changes: 2 additions & 0 deletions samples/wifi/sta/boards
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# No RAM for new features
CONFIG_WIFI_READY_LIB=n
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_LIB=y

CONFIG_WIFI_MGMT_EXT=y
CONFIG_WIFI_CREDENTIALS=y
Expand Down
8 changes: 8 additions & 0 deletions samples/wifi/sta/sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
139 changes: 119 additions & 20 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,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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}

0 comments on commit 9ceab9b

Please sign in to comment.