|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +/** @file |
| 8 | + * @brief WPA SUPP events |
| 9 | + */ |
| 10 | + |
| 11 | +#include <stdio.h> |
| 12 | +#include <zephyr/net/net_event.h> |
| 13 | +#include <ctrl_iface_zephyr.h> |
| 14 | +#include <supp_events.h> |
| 15 | +#include <utils.h> |
| 16 | +#include <supp_main.h> |
| 17 | +#include <zephyr/logging/log.h> |
| 18 | +#include <zephyr/kernel.h> |
| 19 | +LOG_MODULE_REGISTER(wpas_event, CONFIG_WFA_QT_LOG_LEVEL); |
| 20 | + |
| 21 | +#define WPA_SUPP_EVENTS (NET_EVENT_WPA_SUPP_READY) |
| 22 | + |
| 23 | +static struct net_mgmt_event_callback net_wpa_supp_cb; |
| 24 | + |
| 25 | +K_SEM_DEFINE(wpa_supp_ready_sem, 0, 1); |
| 26 | + |
| 27 | +#define DEFAULT_WIFI_IFACE "wlan0" |
| 28 | + |
| 29 | +static void handle_wpa_supp_ready(struct net_mgmt_event_callback *cb) |
| 30 | +{ |
| 31 | + k_sem_give(&wpa_supp_ready_sem); |
| 32 | +} |
| 33 | + |
| 34 | +static void wpa_supp_event_handler(struct net_mgmt_event_callback *cb, |
| 35 | + uint32_t mgmt_event, struct net_if *iface) |
| 36 | +{ |
| 37 | + /* TODO: Handle other events */ |
| 38 | + switch (mgmt_event) { |
| 39 | + case NET_EVENT_WPA_SUPP_READY: |
| 40 | + handle_wpa_supp_ready(cb); |
| 41 | + break; |
| 42 | + default: |
| 43 | + LOG_DBG("Unhandled event (%d)", mgmt_event); |
| 44 | + break; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +int wait_for_wpa_s_ready(void) |
| 49 | +{ |
| 50 | + struct wpa_supplicant *wpa_s = z_wpas_get_handle_by_ifname(DEFAULT_WIFI_IFACE); |
| 51 | + |
| 52 | + if (wpa_s) { |
| 53 | + LOG_INF("Supplicant is ready"); |
| 54 | + return 0; |
| 55 | + } |
| 56 | + |
| 57 | + k_sem_take(&wpa_supp_ready_sem, K_MSEC(CONFIG_WPAS_READY_TIMEOUT_MS)); |
| 58 | + |
| 59 | + wpa_s = z_wpas_get_handle_by_ifname(DEFAULT_WIFI_IFACE); |
| 60 | + if (!wpa_s) { |
| 61 | + LOG_INF("Supplicant is not ready"); |
| 62 | + return -1; |
| 63 | + } |
| 64 | + |
| 65 | + /* Belts and braces: Check for ctrl_iface initialization */ |
| 66 | + if (wpa_s->ctrl_iface->sock_pair[0] < 0) { |
| 67 | + LOG_INF("Control iface is not ready"); |
| 68 | + return -1; |
| 69 | + } |
| 70 | + |
| 71 | + return 0; |
| 72 | +} |
| 73 | + |
| 74 | +int wpa_supp_events_register(void) |
| 75 | +{ |
| 76 | + net_mgmt_init_event_callback(&net_wpa_supp_cb, wpa_supp_event_handler, WPA_SUPP_EVENTS); |
| 77 | + net_mgmt_add_event_callback(&net_wpa_supp_cb); |
| 78 | + |
| 79 | + return 0; |
| 80 | +} |
0 commit comments