1 /** @file wifi_nxp.c
2  *
3  * @brief This file provides Core Wi-Fi definition for wpa supplicant rtos driver.
4  *
5  * Copyright 2008-2024 NXP
6  *
7  *  SPDX-License-Identifier: BSD-3-Clause
8  *
9  */
10 
11 #include <stdlib.h>
12 #include <string.h>
13 
14 #include <osa.h>
15 #include <wm_net.h>
16 #include <wifi.h>
17 #include <wifi-debug.h>
18 
19 #if CONFIG_WPA_SUPP
20 
21 #include <wifi_nxp.h>
22 #include <rtos_wpa_supp_if.h>
23 
24 #include "supp_main.h"
25 
26 static t_u8 wifi_supp_init_done;
27 static struct wifi_nxp_ctx_rtos *g_wifi_if_ctx_rtos = NULL;
28 #if CONFIG_WIFI_NM_HOSTAPD_AP
29 static struct wifi_nxp_ctx_rtos *g_wifi_hapd_if_ctx_rtos = NULL;
30 #endif
31 
32 int wifi_nxp_set_mac_addr(const t_u8 *mac);
33 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
wifi_nxp_wpa_supp_set_mac_addr(void * if_priv,const t_u8 * addr)34 static int wifi_nxp_wpa_supp_set_mac_addr(void *if_priv, const t_u8 *addr)
35 {
36     if (!if_priv || !addr)
37     {
38         supp_e("%s: Invalid params", __func__);
39         return -WM_FAIL;
40     }
41 
42     return wifi_nxp_set_mac_addr(addr);
43 }
44 #endif
45 const rtos_wpa_supp_dev_ops wpa_supp_ops = {
46     .init                     = wifi_nxp_wpa_supp_dev_init,
47     .deinit                   = wifi_nxp_wpa_supp_dev_deinit,
48 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
49     .set_mac_addr             = wifi_nxp_wpa_supp_set_mac_addr,
50     .set_default_scan_ies     = wifi_nxp_wpa_supp_set_default_scan_ies,
51     .sched_scan               = wifi_nxp_wpa_supp_sched_scan,
52     .stop_sched_scan          = wifi_nxp_wpa_supp_stop_sched_scan,
53     .del_key                  = wifi_nxp_wpa_supp_del_key,
54     .set_rekey_info           = wifi_nxp_wpa_supp_set_rekey_info,
55     .set_country              = wifi_nxp_wpa_supp_set_country,
56     .get_country              = wifi_nxp_wpa_supp_get_country,
57     .remain_on_channel        = wifi_nxp_wpa_supp_remain_on_channel,
58     .cancel_remain_on_channel = wifi_nxp_wpa_supp_cancel_remain_on_channel,
59     .get_survey_results       = wifi_nxp_wpa_supp_survey_results_get,
60     .get_modes                = wifi_nxp_wpa_get_modes,
61     .set_modes                = wifi_nxp_hostapd_set_modes,
62     .hapd_send_eapol          = wifi_nxp_hostapd_send_eapol,
63     .set_freq                 = wifi_nxp_hostapd_set_freq,
64     .set_rts                  = wifi_nxp_hostapd_set_rts,
65     .set_frag                 = wifi_nxp_hostapd_set_frag,
66     .set_acl                  = wifi_nxp_hostapd_set_acl,
67 #endif
68     .scan2                    = wifi_nxp_wpa_supp_scan2,
69     .scan_abort               = wifi_nxp_wpa_supp_scan_abort,
70     .get_scan_results2        = wifi_nxp_wpa_supp_scan_results_get,
71     .deauthenticate           = wifi_nxp_wpa_supp_deauthenticate,
72     .authenticate             = wifi_nxp_wpa_supp_authenticate,
73     .associate                = wifi_nxp_wpa_supp_associate,
74     .set_key                  = wifi_nxp_wpa_supp_set_key,
75     .set_supp_port            = wifi_nxp_wpa_supp_set_supp_port,
76     .signal_poll              = wifi_nxp_wpa_supp_signal_poll,
77     .send_mlme                = wifi_nxp_wpa_supp_send_mlme,
78     .get_wiphy                = wifi_nxp_wpa_supp_get_wiphy,
79 #if CONFIG_WIFI_SOFTAP_SUPPORT
80     .init_ap                  = wifi_nxp_wpa_supp_init_ap,
81 #endif
82 #if CONFIG_WPA_SUPP_AP
83     .hapd_init                = wifi_nxp_hostapd_dev_init,
84     .hapd_deinit              = wifi_nxp_hostapd_dev_deinit,
85     .do_acs                   = wifi_nxp_hostapd_do_acs,
86     .set_ap                   = wifi_nxp_hostapd_set_ap,
87     .sta_add                  = wifi_nxp_hostapd_sta_add,
88     .sta_remove               = wifi_nxp_hostapd_sta_remove,
89     .stop_ap                  = wifi_nxp_hostapd_stop_ap,
90 #endif
91 #if CONFIG_WIFI_NM_WPA_SUPPLICANT_DPP
92     .dpp_listen               = wifi_nxp_wpa_dpp_listen,
93 #endif
94 };
95 
wifi_nxp_event_proc_scan_start(void * if_ctx)96 static void wifi_nxp_event_proc_scan_start(void *if_ctx)
97 {
98     wifi_nxp_wpa_supp_event_proc_scan_start(if_ctx);
99 }
100 
wifi_nxp_event_proc_scan_done(void * if_priv,int aborted,int external_scan)101 static void wifi_nxp_event_proc_scan_done(void *if_priv, int aborted, int external_scan)
102 {
103     struct wifi_nxp_ctx_rtos *wifi_if_ctx_rtos = NULL;
104 
105     wifi_if_ctx_rtos = (struct wifi_nxp_ctx_rtos *)if_priv;
106 
107     if (wifi_if_ctx_rtos == NULL)
108     {
109         wifi_e("%s: wifi_if_ctx_rtos is NULL", __func__);
110         return;
111     }
112     wifi_nxp_wpa_supp_event_proc_scan_done(if_priv, aborted, external_scan);
113 }
114 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
wifi_nxp_event_reamin_on_channel(void * if_priv,int cancel_channel)115 static void wifi_nxp_event_reamin_on_channel(void *if_priv, int cancel_channel)
116 {
117     struct wifi_nxp_ctx_rtos *wifi_if_ctx_rtos = NULL;
118 
119     wifi_if_ctx_rtos = (struct wifi_nxp_ctx_rtos *)if_priv;
120 
121     if (wifi_if_ctx_rtos == NULL)
122     {
123         wifi_e("%s: wifi_if_ctx_rtos is NULL", __func__);
124         return;
125     }
126     wifi_nxp_wpa_supp_event_proc_remain_on_channel(if_priv, cancel_channel);
127 }
128 #endif
129 
wifi_nxp_wpa_is_supp_scan_in_progress(void * if_priv)130 static int wifi_nxp_wpa_is_supp_scan_in_progress(void *if_priv)
131 {
132     struct wifi_nxp_ctx_rtos *wifi_if_ctx_rtos = NULL;
133 
134     wifi_if_ctx_rtos = (struct wifi_nxp_ctx_rtos *)if_priv;
135 
136     if (wifi_if_ctx_rtos == NULL)
137     {
138         wifi_e("%s: wifi_if_ctx_rtos is NULL", __func__);
139         return -WM_FAIL;
140     }
141 
142     return wifi_if_ctx_rtos->scan_in_progress;
143 }
144 static const wifi_nxp_callbk_fns_t supp_callbk_fns = {
145     .mac_changed_callbk_fn              = wifi_nxp_wpa_supp_event_proc_mac_changed,
146     .chan_list_changed_callbk_fn        = wifi_nxp_wpa_supp_event_proc_chan_list_changed,
147     .acs_channel_sel_callbk_fn          = wifi_nxp_wpa_supp_event_acs_channel_selected,
148 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
149     .survey_res_callbk_fn               = wifi_nxp_wpa_supp_event_proc_survey_res,
150     .remain_on_channel_callbk_fn        = wifi_nxp_event_reamin_on_channel,
151     .eapol_rx_callbk_fn                 = wifi_nxp_wpa_supp_event_proc_eapol_rx,
152 #endif
153     .dfs_cac_started_callbk_fn          = wifi_nxp_wpa_supp_event_proc_dfs_cac_started,
154     .dfs_cac_finished_callbk_fn         = wifi_nxp_wpa_supp_event_proc_dfs_cac_finished,
155     .ecsa_complete_callbk_fn            = wifi_nxp_wpa_supp_event_proc_ecsa_complete,
156     .scan_start_callbk_fn               = wifi_nxp_event_proc_scan_start,
157     .scan_done_callbk_fn                = wifi_nxp_event_proc_scan_done,
158     .auth_resp_callbk_fn                = wifi_nxp_wpa_supp_event_proc_auth_resp,
159     .assoc_resp_callbk_fn               = wifi_nxp_wpa_supp_event_proc_assoc_resp,
160     .deauth_callbk_fn                   = wifi_nxp_wpa_supp_event_proc_deauth,
161     .disassoc_callbk_fn                 = wifi_nxp_wpa_supp_event_proc_disassoc,
162     .mgmt_tx_status_callbk_fn           = wifi_nxp_wpa_supp_event_mgmt_tx_status,
163     .unprot_mlme_mgmt_rx_callbk_fn      = wifi_nxp_wpa_supp_event_proc_unprot_mgmt,
164     .mgmt_rx_callbk_fn                  = wifi_nxp_wpa_supp_event_proc_mgmt_rx,
165     .get_wiphy_callbk_fn                = wifi_nxp_wpa_supp_event_get_wiphy,
166     .is_supp_scan_in_progress_callbk_fn = wifi_nxp_wpa_is_supp_scan_in_progress,
167     .signal_change_callbk_fn            = wifi_nxp_wpa_supp_event_signal_change,
168 };
169 
wifi_supp_init(void)170 int wifi_supp_init(void)
171 {
172     int ret = -WM_FAIL;
173     char sta_iface_name[NETIF_NAMESIZE];
174 #ifdef CONFIG_WIFI_SOFTAP_SUPPORT
175     char uap_iface_name[NETIF_NAMESIZE];
176 #endif
177     struct netif *iface = NULL;
178 
179     if (wifi_supp_init_done != 0U)
180     {
181         return WM_SUCCESS;
182     }
183 
184     wm_wifi.supp_if_callbk_fns = (wifi_nxp_callbk_fns_t *)&supp_callbk_fns;
185 
186     if (g_wifi_if_ctx_rtos == NULL)
187     {
188         g_wifi_if_ctx_rtos = (struct wifi_nxp_ctx_rtos *)OSA_MemoryAllocate(sizeof(struct wifi_nxp_ctx_rtos));
189 
190         if (!g_wifi_if_ctx_rtos)
191         {
192             wifi_e("Interface ctx alloc failed.");
193             goto out;
194         }
195     }
196 
197     wm_wifi.if_priv = (void *)g_wifi_if_ctx_rtos;
198 
199     iface = net_get_sta_interface();
200 
201     if (iface == NULL)
202     {
203         wifi_e("net_get_sta_interface failed. status code %d", ret);
204         goto out;
205     }
206 
207     (void)net_get_if_name_netif(sta_iface_name, iface);
208 
209 #if CONFIG_WIFI_SOFTAP_SUPPORT
210     if (g_wifi_hapd_if_ctx_rtos == NULL)
211     {
212         g_wifi_hapd_if_ctx_rtos = (struct wifi_nxp_ctx_rtos *)OSA_MemoryAllocate(sizeof(struct wifi_nxp_ctx_rtos));
213 
214         if (!g_wifi_hapd_if_ctx_rtos)
215         {
216             wifi_e("Interface ctx alloc failed.");
217             goto out;
218         }
219     }
220     wm_wifi.hapd_if_priv = (void *)g_wifi_hapd_if_ctx_rtos;
221 
222     iface = net_get_uap_interface();
223 
224     if (iface == NULL)
225     {
226         wifi_e("net_get_uap_interface failed. status code %d", ret);
227         goto out;
228     }
229 
230     (void)net_get_if_name_netif(uap_iface_name, iface);
231 #endif
232 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
233     ret = start_wpa_supplicant(sta_iface_name);
234 
235     if (ret != WM_SUCCESS)
236     {
237         wifi_e("start wpa supplicant failed. status code %d", ret);
238         goto out;
239     }
240 #endif
241 
242     if (ret == WM_SUCCESS)
243     {
244         wifi_supp_init_done = 1U;
245     }
246 
247     return WM_SUCCESS;
248 
249 out:
250     wifi_supp_deinit();
251     return ret;
252 }
253 
wifi_supp_deinit(void)254 void wifi_supp_deinit(void)
255 {
256 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
257     int ret;
258 #endif
259     if (wifi_supp_init_done != 1U)
260     {
261         return;
262     }
263 
264 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
265     ret = stop_wpa_supplicant();
266     if (ret != WM_SUCCESS)
267     {
268         wifi_e("stop wpa supplicant failed. status code %d", ret);
269     }
270 #endif
271 
272 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
273     if (wm_wifi.if_priv)
274     {
275         OSA_MemoryFree(wm_wifi.if_priv);
276         wm_wifi.if_priv = NULL;
277         g_wifi_if_ctx_rtos = NULL;
278     }
279 
280     if (wm_wifi.hapd_if_priv)
281     {
282         OSA_MemoryFree(wm_wifi.hapd_if_priv);
283         wm_wifi.hapd_if_priv = NULL;
284         g_wifi_hapd_if_ctx_rtos = NULL;
285     }
286 #endif
287     wifi_supp_init_done = 0U;
288 }
289 
wifi_nxp_set_mac_addr(const t_u8 * mac)290 int wifi_nxp_set_mac_addr(const t_u8 *mac)
291 {
292     wlan_set_mac_addr((uint8_t *)mac);
293     return WM_SUCCESS;
294 }
295 #endif /* CONFIG_WPA_SUPP */
296