|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/kernel.h> |
| 8 | +#include <zephyr/init.h> |
| 9 | +#include <zephyr/logging/log.h> |
| 10 | +#include <zephyr/net/net_event.h> |
| 11 | + |
| 12 | +#include <stdio.h> |
| 13 | +#include <stdlib.h> |
| 14 | + |
| 15 | +#include <net/wifi_ready.h> |
| 16 | +#include <supp_events.h> |
| 17 | +#include <zephyr/net/wifi_nm.h> |
| 18 | + |
| 19 | +LOG_MODULE_REGISTER(wifi_ready, CONFIG_WIFI_READY_LIB_LOG_LEVEL); |
| 20 | + |
| 21 | +static wifi_ready_callback_t wifi_ready_callbacks[CONFIG_WIFI_READY_MAX_CALLBACKS]; |
| 22 | +static unsigned char callback_count; |
| 23 | +static K_MUTEX_DEFINE(wifi_ready_mutex); |
| 24 | + |
| 25 | +#define WPA_SUPP_EVENTS (NET_EVENT_WPA_SUPP_READY) | \ |
| 26 | + (NET_EVENT_WPA_SUPP_NOT_READY) |
| 27 | + |
| 28 | +static struct net_mgmt_event_callback net_wpa_supp_cb; |
| 29 | + |
| 30 | +/* In case Wi-Fi is already ready, call the callbacks */ |
| 31 | +static void wifi_ready_work_handler(struct k_work *item); |
| 32 | + |
| 33 | +static void call_wifi_ready_callbacks(bool ready, struct net_if *iface) |
| 34 | +{ |
| 35 | + int i; |
| 36 | + |
| 37 | + k_mutex_lock(&wifi_ready_mutex, K_FOREVER); |
| 38 | + for (i = 0; i < ARRAY_SIZE(wifi_ready_callbacks); i++) { |
| 39 | + if (wifi_ready_callbacks[i].wifi_ready_cb && |
| 40 | + ((wifi_ready_callbacks[i].iface && |
| 41 | + (wifi_ready_callbacks[i].iface == iface)) || |
| 42 | + !wifi_ready_callbacks[i].iface)) { |
| 43 | + wifi_ready_callbacks[i].wifi_ready_cb(ready); |
| 44 | + } |
| 45 | + } |
| 46 | + k_mutex_unlock(&wifi_ready_mutex); |
| 47 | +} |
| 48 | + |
| 49 | +static void wifi_ready_work_handler(struct k_work *item) |
| 50 | +{ |
| 51 | + wifi_ready_callback_t *cb = CONTAINER_OF(item, wifi_ready_callback_t, item); |
| 52 | + |
| 53 | + call_wifi_ready_callbacks(true, cb->iface); |
| 54 | +} |
| 55 | + |
| 56 | +static void handle_wpa_supp_event(struct net_if *iface, bool ready) |
| 57 | +{ |
| 58 | + LOG_DBG("Supplicant event %s for iface %p", |
| 59 | + ready ? "ready" : "not ready", iface); |
| 60 | + |
| 61 | + call_wifi_ready_callbacks(ready, iface); |
| 62 | +} |
| 63 | + |
| 64 | +static void wpa_supp_event_handler(struct net_mgmt_event_callback *cb, |
| 65 | + uint32_t mgmt_event, struct net_if *iface) |
| 66 | +{ |
| 67 | + ARG_UNUSED(cb); |
| 68 | + |
| 69 | + LOG_DBG("Event received: %d", mgmt_event); |
| 70 | + switch (mgmt_event) { |
| 71 | + case NET_EVENT_WPA_SUPP_READY: |
| 72 | + handle_wpa_supp_event(iface, true); |
| 73 | + break; |
| 74 | + case NET_EVENT_WPA_SUPP_NOT_READY: |
| 75 | + handle_wpa_supp_event(iface, false); |
| 76 | + break; |
| 77 | + default: |
| 78 | + LOG_DBG("Unhandled event (%d)", mgmt_event); |
| 79 | + break; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +int register_wifi_ready_callback(wifi_ready_callback_t cb, struct net_if *iface) |
| 84 | +{ |
| 85 | + int ret = 0, i; |
| 86 | + unsigned int next_avail_idx = CONFIG_WIFI_READY_MAX_CALLBACKS; |
| 87 | + struct net_if *wpas_iface = NULL; |
| 88 | + |
| 89 | + k_mutex_lock(&wifi_ready_mutex, K_FOREVER); |
| 90 | + |
| 91 | + if (!cb.wifi_ready_cb) { |
| 92 | + LOG_ERR("Callback is NULL"); |
| 93 | + ret = -EINVAL; |
| 94 | + goto out; |
| 95 | + } |
| 96 | + |
| 97 | + for (i = 0; i < ARRAY_SIZE(wifi_ready_callbacks); i++) { |
| 98 | + if (wifi_ready_callbacks[i].wifi_ready_cb == NULL) { |
| 99 | + next_avail_idx = i; |
| 100 | + break; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if (next_avail_idx == CONFIG_WIFI_READY_MAX_CALLBACKS) { |
| 105 | + LOG_ERR("Reject callback registration, maximum count %d reached", |
| 106 | + CONFIG_WIFI_READY_MAX_CALLBACKS); |
| 107 | + ret = -ENOMEM; |
| 108 | + goto out; |
| 109 | + } |
| 110 | + |
| 111 | + /* Check if callback has already been registered for iface */ |
| 112 | + for (i = 0; i < ARRAY_SIZE(wifi_ready_callbacks); i++) { |
| 113 | + if (wifi_ready_callbacks[i].iface == iface && |
| 114 | + wifi_ready_callbacks[i].wifi_ready_cb == cb.wifi_ready_cb) { |
| 115 | + LOG_ERR("Callback has already registered for iface"); |
| 116 | + ret = -EALREADY; |
| 117 | + goto out; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + wifi_ready_callbacks[next_avail_idx].wifi_ready_cb = cb.wifi_ready_cb; |
| 122 | + wifi_ready_callbacks[next_avail_idx].iface = iface; |
| 123 | + |
| 124 | + if (++callback_count == 1) { |
| 125 | + net_mgmt_init_event_callback(&net_wpa_supp_cb, |
| 126 | + wpa_supp_event_handler, WPA_SUPP_EVENTS); |
| 127 | + net_mgmt_add_event_callback(&net_wpa_supp_cb); |
| 128 | + } |
| 129 | + |
| 130 | + if (iface) { |
| 131 | + wpas_iface = iface; |
| 132 | + } else { |
| 133 | + wpas_iface = net_if_get_first_wifi(); |
| 134 | + if (!wpas_iface) { |
| 135 | + LOG_ERR("Failed to get Wi-Fi interface"); |
| 136 | + ret = -ENODEV; |
| 137 | + goto out; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /* In case Wi-Fi is already ready, call the callback */ |
| 142 | + if (wifi_nm_get_instance_iface(wpas_iface)) { |
| 143 | + k_work_submit(&wifi_ready_callbacks[next_avail_idx].item); |
| 144 | + } |
| 145 | + |
| 146 | +out: |
| 147 | + k_mutex_unlock(&wifi_ready_mutex); |
| 148 | + return ret; |
| 149 | + |
| 150 | +} |
| 151 | + |
| 152 | + |
| 153 | +int unregister_wifi_ready_callback(wifi_ready_callback_t cb, struct net_if *iface) |
| 154 | +{ |
| 155 | + int ret = 0, i; |
| 156 | + bool found = false; |
| 157 | + |
| 158 | + k_mutex_lock(&wifi_ready_mutex, K_FOREVER); |
| 159 | + |
| 160 | + if (!cb.wifi_ready_cb) { |
| 161 | + LOG_ERR("Callback is NULL"); |
| 162 | + ret = -EINVAL; |
| 163 | + goto out; |
| 164 | + } |
| 165 | + |
| 166 | + for (i = 0; i < ARRAY_SIZE(wifi_ready_callbacks); i++) { |
| 167 | + if (wifi_ready_callbacks[i].iface == iface && |
| 168 | + wifi_ready_callbacks[i].wifi_ready_cb == cb.wifi_ready_cb) { |
| 169 | + wifi_ready_callbacks[i].wifi_ready_cb = NULL; |
| 170 | + wifi_ready_callbacks[i].iface = NULL; |
| 171 | + found = true; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + if (!found) { |
| 176 | + ret = -ENOENT; |
| 177 | + goto out; |
| 178 | + } |
| 179 | + |
| 180 | + if (--callback_count == 0) { |
| 181 | + net_mgmt_del_event_callback(&net_wpa_supp_cb); |
| 182 | + } |
| 183 | + |
| 184 | +out: |
| 185 | + if (ret < 0) { |
| 186 | + LOG_ERR("Failed to unregister callback: %d", ret); |
| 187 | + } |
| 188 | + k_mutex_unlock(&wifi_ready_mutex); |
| 189 | + return ret; |
| 190 | +} |
| 191 | + |
| 192 | +static int wifi_ready_init(void) |
| 193 | +{ |
| 194 | + /* Initialize the work items */ |
| 195 | + for (int i = 0; i < ARRAY_SIZE(wifi_ready_callbacks); i++) { |
| 196 | + k_work_init(&wifi_ready_callbacks[i].item, wifi_ready_work_handler); |
| 197 | + } |
| 198 | + |
| 199 | + return 0; |
| 200 | +} |
| 201 | + |
| 202 | +SYS_INIT(wifi_ready_init, APPLICATION, CONFIG_WIFI_READY_INIT_PRIORITY); |
0 commit comments