Skip to content

Commit

Permalink
WPA supp ready: better implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Chaitanya Tata <[email protected]>
  • Loading branch information
krish2718 committed Feb 26, 2024
1 parent 489eeb5 commit 389d831
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 43 deletions.
22 changes: 14 additions & 8 deletions zephyr/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ module-help = Sets log level for WFA Quick Track
source "subsys/logging/Kconfig.template.log_config"

config WFA_QT_CONTROL_APP
# Need full POSIX from libc, Zephyr's POSIX support is only partial
depends on !POSIX_API
select PTHREAD_IPC
bool "WFA Quicktrack control app"
# Need full POSIX from libc, Zephyr's POSIX support is only partial
depends on !POSIX_API
select PTHREAD_IPC
bool "WFA Quicktrack control app"

config WFA_QT_THREAD_STACK_SIZE
int "WFA QT thread stack size"
default 4096
help
Set the stack size for WFA QT thread.
int "WFA QT thread stack size"
default 4096
help
Set the stack size for WFA QT thread.

config WPAS_READY_TIMEOUT_MS
int "WPA supplicant ready timeout (ms)"
default 10000
help
Set the timeout for WPA supplicant to be ready.
59 changes: 24 additions & 35 deletions zephyr/src/wpas_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,55 +21,46 @@ LOG_MODULE_REGISTER(wpas_event, CONFIG_WFA_QT_LOG_LEVEL);
#define WPA_SUPP_EVENTS (NET_EVENT_WPA_SUPP_READY)

static struct net_mgmt_event_callback net_wpa_supp_cb;
struct wpa_supplicant *wpa_s = NULL;

K_SEM_DEFINE(wpa_supp_ready_sem, 0, 1);

#define DEFAULT_WIFI_IFACE "wlan0"

static void handle_wpa_supp_ready(struct net_mgmt_event_callback *cb)
{
int retry_count = 0;

retry:
wpa_s = z_wpas_get_handle_by_ifname("wlan0");
if (!wpa_s && retry_count++ < 5) {
LOG_ERR("%s: Unable to get wpa_s handle for %s\n",
__func__, "wlan0");
goto retry;
}

if (!wpa_s) {
LOG_ERR("%s: Unable to get wpa_s handle for %s\n",
__func__, "wlan0");
}

if (wpa_s) {
LOG_INF("Supplicant is ready");
k_sem_give(&wpa_supp_ready_sem);
}

k_sem_give(&wpa_supp_ready_sem);
}

static void wpa_supp_event_handler(struct net_mgmt_event_callback *cb,
uint32_t mgmt_event, struct net_if *iface)
static void wpa_supp_event_handler(struct net_mgmt_event_callback *cb,
uint32_t mgmt_event, struct net_if *iface)
{
switch (mgmt_event) {
case NET_EVENT_WPA_SUPP_READY:
/* TODO: Handle other events */
switch (mgmt_event) {
case NET_EVENT_WPA_SUPP_READY:
handle_wpa_supp_ready(cb);
break;
default:
break;
}
break;
default:
LOG_DBG("Unhandled event (%d)", mgmt_event);
break;
}
}

int wait_for_wpa_s_ready(void)
{
struct wpa_supplicant *wpa_s = z_wpas_get_handle_by_ifname(DEFAULT_WIFI_IFACE);

if (wpa_s) {
return 0;
}

k_sem_take(&wpa_supp_ready_sem, K_FOREVER);
k_sem_take(&wpa_supp_ready_sem, K_MSEC(CONFIG_WPAS_READY_TIMEOUT_MS));

wpa_s = z_wpas_get_handle_by_ifname(DEFAULT_WIFI_IFACE);
if (!wpa_s) {
return -1;
}

/* Check for ctrl_iface initialization */
/* Belts and braces: Check for ctrl_iface initialization */
if (wpa_s->ctrl_iface->sock_pair[0] < 0) {
return -1;
}
Expand All @@ -79,10 +70,8 @@ int wait_for_wpa_s_ready(void)

int wpa_supp_events_register(void)
{
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);
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);

return 0;
}

0 comments on commit 389d831

Please sign in to comment.