1 /** @file wlan.c
2  *
3  *  @brief  This file provides Core WLAN definition
4  *
5  *  Copyright 2008-2024 NXP
6  *
7  *  SPDX-License-Identifier: BSD-3-Clause
8  *
9  */
10 
11 #include <string.h>
12 #include <wm_utils.h>
13 #include <wlan.h>
14 #include <wifi.h>
15 #include <osa.h>
16 #include <wm_net.h>
17 #include <wifi-debug.h>
18 #include <wlan_11d.h>
19 #if CONFIG_WMSTATS
20 #include <wmstats.h>
21 #endif /* CONFIG_WMSTATS */
22 #include <stdint.h>
23 #ifndef RW610
24 #include <mlan_sdio_api.h>
25 #endif
26 #if (CONFIG_HOST_PMK) || (CONFIG_WPS2)
27 #include <wm_mbedtls_helper_api.h>
28 #include <mbedtls/x509_crt.h>
29 #endif /* (CONFIG_HOST_PMK) || (CONFIG_WPS2) */
30 #ifdef OVERRIDE_CALIBRATION_DATA
31 #include OVERRIDE_CALIBRATION_DATA
32 #else
33 #include <wifi_cal_data_ext.h>
34 #ifdef RW610
35 #include DEFAULT_CALDATA_RW610
36 #endif
37 #endif
38 #ifdef RW610
39 #include "fsl_loader.h"
40 #endif
41 
42 #if CONFIG_SIGMA_AGENT
43 #include "wifi_ping.h"
44 #endif
45 
46 #if CONFIG_HOST_SLEEP
47 #ifdef RW610
48 #include  "fsl_power.h"
49 #endif
50 #endif
51 #if CONFIG_WPS2
52 #include  <wifi_nxp_wps.h>
53 #include  <wps_def.h>
54 #endif
55 #if CONFIG_WPA_SUPP
56 #include <supp_main.h>
57 #include <supp_api.h>
58 #include <wifi_nxp.h>
59 #include "utils/common.h"
60 #include "rtos_wpa_supp_if.h"
61 #if CONFIG_WIFI_SHELL
62 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
63 #include "wpa_cli.h"
64 #endif
65 #endif
66 #endif
67 
68 #if (CONFIG_WPA2_ENTP) || ((CONFIG_WIFI_NM_WPA_SUPPLICANT_CRYPTO_ENTERPRISE || CONFIG_WIFI_NM_HOSTAPD_CRYPTO_ENTERPRISE) && !(CONFIG_WIFI_USB_FILE_ACCESS))
69 #include "ca-cert.h"
70 #include "client-cert.h"
71 #include "client-key.h"
72 #include "dh-param.h"
73 #include "server-cert.h"
74 #include "server-key.h"
75 #endif
76 #include "mlan_decl.h"
77 
78 #if CONFIG_NCP_BRIDGE
79 #include "app_notify.h"
80 #endif
81 
82 #if CONFIG_WIFI_RECOVERY
83 #include "zephyr/net/dhcpv4_server.h"
84 #endif
85 
86 #if (CONFIG_WIFI_IND_RESET) && (CONFIG_WIFI_IND_DNLD)
87 #include "board.h"
88 
89 #if (defined(CPU_MIMXRT1062DVMAA_cm7) || defined(CPU_MIMXRT1062DVL6A_cm7) || defined (CPU_MIMXRT1062DVL6B_cm7))
90 #if defined(SD8978) || defined(SD8987)
91 /* IR-OOB TRIGGER Connect Fly-Wire between J16.1 and J108.4 for 1XK-M2, 1ZM-M2*/
92 #define IR_OUTBAND_TRIGGER_GPIO			GPIO1
93 #define IR_OUTBAND_TRIGGER_GPIO_PIN		(23U)
94 #define IR_OUTBAND_TRIGGER_GPIO_NAME   "GPIO1"
95 //#define IOMUXC_GPIO_IR_OUTBAND_TRIGGER IOMUXC_GPIO_AD_B1_07_GPIO1_IO23
96 #elif defined(SD9177)
97 /* IR-OOB TRIGGER for 2EL-M2, Internal Routing to M2 Slot*/
98 #define IR_OUTBAND_TRIGGER_GPIO			GPIO1
99 #define IR_OUTBAND_TRIGGER_GPIO_PIN		(24U)
100 #define IR_OUTBAND_TRIGGER_GPIO_NAME   "GPIO1"
101 //#define IOMUXC_GPIO_IR_OUTBAND_TRIGGER IOMUXC_GPIO_AD_B1_08_GPIO1_IO24
102 #endif
103 
104 #elif defined(CPU_MIMXRT1176DVMAA_cm7) // For RT1170
105 /* IR OUT-BAND TRIGGER GPIO*/
106 /*Output GPIO J9 PIN2 (IOMUXC_GPIO_DISP_B2_11) for RT1170-EVKA/B*/
107 #define IR_OUTBAND_TRIGGER_GPIO   		GPIO5
108 #define IR_OUTBAND_TRIGGER_GPIO_PIN   	(12U)
109 #define IR_OUTBAND_TRIGGER_GPIO_NAME  	"GPIO5"
110 
111 #endif /* (defined(CPU_MIMXRT1062DVMAA_cm7) || (CPU_MIMXRT1062DVL6A_cm7)) */
112 #endif
113 
114 #define DELAYED_SLP_CFM_DUR 10U
115 #define BAD_MIC_TIMEOUT     (60 * 1000)
116 
117 #if CONFIG_WPA_SUPP
118 #define SUPP_STATUS_TIMEOUT (2 * 1000)
119 #define ROAM_SCAN_TIMEOUT   (60 * 1000)
120 #endif
121 
122 #define WL_ID_CONNECT      "wifi_connect"
123 #define WL_ID_DEEPSLEEP_SM "wlcm_deepsleep_sm"
124 #define WL_ID_WIFI_RSSI    "wifi_rssi"
125 
126 /** Find maximum */
127 #ifndef MAX
128 #define MAX(a, b) ((a) > (b) ? (a) : (b))
129 #endif
130 
131 #if CONFIG_ECSA
132 #define DEF_SWITCH_COUNT 10
133 #endif
134 
135 #if CONFIG_BG_SCAN
136 #define BG_SCAN_LIMIT 3
137 #endif
138 
139 #if UAP_SUPPORT
140 static bool wlan_uap_scan_chan_list_set;
141 #endif
142 
143 #if CONFIG_MEF_CFG
144 wlan_flt_cfg_t g_flt_cfg;
145 #endif
146 #ifdef RW610
147 #if (CONFIG_NXP_MONOLITHIC_WIFI)
148 extern const unsigned char *wlan_fw_bin;
149 extern const unsigned int wlan_fw_bin_len;
150 #else
151 const unsigned char *wlan_fw_bin   = (const unsigned char *)(void *)0;
152 const unsigned int wlan_fw_bin_len = 0;
153 #endif /* CONFIG_NXP_MONOLITHIC_WIFI */
154 extern int nxp_wifi_wlan_event_callback(enum wlan_event_reason reason, void *data);
155 #define wlan_event_callback nxp_wifi_wlan_event_callback
156 #endif
157 
158 static int wifi_wakeup_card_cb(osa_rw_lock_t *plock, unsigned int wait_time);
159 
160 #if CONFIG_WPA2_ENTP
161 extern int wpa2_ent_connect(struct wlan_network *wpa2_network);
162 extern void wpa2_shutdown();
163 #endif
164 
165 #if CONFIG_NCP_BRIDGE
166 /* uap provision callbacks */
167 int (*uap_prov_deinit_cb)(void) = NULL;
168 void (*uap_prov_cleanup_cb)(void) = NULL;
169 #endif
170 
171 #if CONFIG_WMSTATS
172 struct wm_stats g_wm_stats;
173 #endif /* CONFIG_WMSTATS */
174 
175 osa_rw_lock_t sleep_rwlock;
176 
177 #if CONFIG_WMM_UAPSD
178 OSA_SEMAPHORE_HANDLE_DEFINE(uapsd_sem);
179 #endif
180 
181 #if CONFIG_CPU_LOADING
182 #define CPU_LOADING_ACTION_STOP        0
183 #define CPU_LOADING_ACTION_START       1
184 #define CPU_LOADING_STATUS_ONGOING     2
185 #define CPU_LOADING_STATUS_ENDING      3
186 #define CPU_LOADING_STATUS_DEAD        4
187 #define CPU_LOADING_PERIOD             2000
188 #define CPU_LOADING_TASK_NUM           20
189 #define CPU_LOADING_KEEPING            -1
190 
191 #define CONFIG_CPU_LOADING_STACK_SIZE (2048)
192 
193 static void cpu_loading_task(osa_task_param_t arg);
194 
195 /* OSA_TASKS: name, priority, instances, stackSz, useFloat */
196 static OSA_TASK_DEFINE(cpu_loading_task, OSA_PRIORITY_HIGH, 1, CONFIG_CPU_LOADING_STACK_SIZE, 0);
197 
198 static struct
199 {
200     /*The number of tasks.*/
201     uint8_t task_nums;
202     /*The total length of cpu info struct*/
203     uint32_t task_status_len;
204     /*Pointer to buffer of storing cpu Loading info.*/
205     char *cpu_loading_info;
206     /*CPU loading status: CPU_LOADING_STATUS_ENDING / CPU_LOADING_STATUS_ONGOING / CPU_LOADING_STATUS_DEAD*/
207     uint8_t status;
208     /*Index of collecting CPU loading info.*/
209     uint32_t index;
210     /*Remaining time of collecting CPU loading info.*/
211     int sampling_loops;
212     /*The value of timer time out*/
213     uint32_t sampling_period;
214     /*CPU loading timer.*/
215     OSA_TIMER_HANDLE_DEFINE(cpu_loading_timer);
216     /*CPU loading thread.*/
217     OSA_TASK_HANDLE_DEFINE(cpu_loading_task_Handle);
218 
219     /*Array of recording names of tasks.*/
220     char task_name[CPU_LOADING_TASK_NUM][configMAX_TASK_NAME_LEN];
221     /*Array of recording runing time of tasks.*/
222     uint64_t data_cur[CPU_LOADING_TASK_NUM];
223     uint64_t data_pre[CPU_LOADING_TASK_NUM];
224     /*Array of recording the first runing time of tasks.*/
225     uint64_t first_data[CPU_LOADING_TASK_NUM];
226 }cpu_loading;
227 
228 char task_string_name[CPU_LOADING_TASK_NUM][configMAX_TASK_NAME_LEN];
229 
230 #endif
231 
232 #if ((CONFIG_11MC) || (CONFIG_11AZ)) && (CONFIG_WLS_CSI_PROC)
233 OSA_SEMAPHORE_HANDLE_DEFINE(wls_csi_sem);
234 #endif
235 
236 #if CONFIG_WPS2
237 int prov_session_attempt = PROV_NON_SESSION_ATTEMPT;
238 extern WPS_DATA wps_global;
239 #endif
240 
241 #define MAX_EVENTS 20
242 #define CONNECTION_EVENT(r, data) \
243     if (wlan.cb != NULL)          \
244     {                             \
245         (void)wlan.cb(r, data);   \
246     }
247 
248 #ifdef RW610
249 OSA_MUTEX_HANDLE_DEFINE(reset_lock);
250 /* Mon thread */
251 static bool mon_thread_init = 0;
252 #endif
253 
254 #if CONFIG_HOST_SLEEP
255 #if CONFIG_POWER_MANAGER
256 #if !(CONFIG_WIFI_BLE_COEX_APP)
257 int wlan_host_sleep_state = HOST_SLEEP_DISABLE;
258 #else
259 int wlan_host_sleep_state = HOST_SLEEP_PERIODIC;
260 #endif
261 #if CONFIG_UART_INTERRUPT
262 /* This flag is used for Power Manager only.
263  * When using Power Manager, the uart task holds the rxSemaphore and waits
264  * on uart event group to receive input from uart. With this flag, uart task
265  * will have chance to release the lock so that IDLE task can do uart deinit
266  * before entering PM3.
267  * When using suspend mode, no such observations.
268  */
269 bool usart_suspend_flag = false;
270 #endif
271 OSA_TIMER_HANDLE_DEFINE(wake_timer);
272 #endif
273 int is_hs_handshake_done = 0;
274 
275 extern OSA_SEMAPHORE_HANDLE_DEFINE(wakelock);
276 extern int wakeup_by;
277 
278 bool wlan_is_manual = false;
279 #endif
280 
281 #if CONFIG_SCAN_CHANNEL_GAP
282 static t_u16 scan_channel_gap = (t_u16)SCAN_CHANNEL_GAP_VALUE;
283 #endif
284 
285 #if (CONFIG_11K) || (CONFIG_11V)
286 #define NEIGHBOR_REQ_TIMEOUT (60 * 1000)
287 #endif
288 
289 #if CONFIG_11R
290 #if CONFIG_WPA_SUPP
291 #define FT_ROAM_TIMEOUT (20 * 1000)
292 #endif
293 #endif
294 
295 #if CONFIG_POWER_MANAGER
296 #define WAKE_TIMEOUT (5 * 1000)
297 #endif
298 
299 #ifdef RW610
300 #define TEMPERATURE_MON_TIMEOUT (5 * 1000)
301 OSA_TIMER_HANDLE_DEFINE(temperature_mon_timer);
302 #endif
303 
304 enum user_request_type
305 {
306     /* we append our user-generated events to the wifi interface events and
307      * handle them accordingly */
308     CM_STA_USER_REQUEST_CONNECT = WIFI_EVENT_LAST + 1,
309     CM_STA_USER_REQUEST_DISCONNECT,
310     CM_STA_USER_REQUEST_SCAN,
311 #if (CONFIG_11K) || (CONFIG_11V)
312     CM_STA_USER_REQUEST_SET_RSSI_THRESHOLD,
313 #endif
314 #if CONFIG_HOST_SLEEP
315     CM_STA_USER_REQUEST_HS,
316 #endif
317     CM_STA_USER_REQUEST_PS_ENTER,
318     CM_STA_USER_REQUEST_PS_EXIT,
319 #if CONFIG_CPU_LOADING
320     CM_STA_USER_REQUEST_CPU_LOADING,
321 #endif
322     CM_STA_USER_REQUEST_LAST,
323     /* All the STA related request are above and uAP related requests are
324        below */
325     CM_UAP_USER_REQUEST_START,
326     CM_UAP_USER_REQUEST_STOP,
327     CM_UAP_USER_REQUEST_PS_ENTER,
328     CM_UAP_USER_REQUEST_PS_EXIT,
329     CM_UAP_USER_REQUEST_LAST,
330     CM_WLAN_USER_REQUEST_DEINIT,
331     CM_WLAN_USER_REQUEST_SHUTDOWN
332 };
333 
334 static int send_user_request(enum user_request_type request, unsigned int data);
335 
336 enum cm_sta_state
337 {
338     CM_STA_INITIALIZING = 0,
339     CM_STA_IDLE,
340     CM_STA_SCANNING,
341     CM_STA_SCANNING_USER,
342     CM_STA_ASSOCIATING,
343     CM_STA_ASSOCIATED,
344     CM_STA_AUTHENTICATED,
345     CM_STA_REQUESTING_ADDRESS,
346     CM_STA_OBTAINING_ADDRESS,
347     CM_STA_CONNECTED,
348 };
349 
350 enum cm_uap_state
351 {
352     CM_UAP_INITIALIZING = 0,
353     CM_UAP_CONFIGURED,
354     CM_UAP_STARTED,
355     CM_UAP_IP_UP,
356 };
357 
358 static struct wifi_scan_params_t g_wifi_scan_params = {NULL,
359                                                        NULL,
360                                                        {
361                                                            0,
362                                                        },
363                                                        BSS_ANY,
364                                                        60,
365                                                        250};
366 
367 static void wlcmgr_task(osa_task_param_t arg);
368 
369 /* OSA_TASKS: name, priority, instances, stackSz, useFloat */
370 static OSA_TASK_DEFINE(wlcmgr_task, CONFIG_NXP_WIFI_WLCMGR_TASK_PRIO, 1, CONFIG_NXP_WIFI_WLCMGR_TASK_STACK_SIZE, 0);
371 
372 #if CONFIG_WPS2
373 #define CONFIG_WPS_STACK_SIZE (5120)
374 
375 static void wps_task(osa_task_param_t arg);
376 
377 /* OSA_TASKS: name, priority, instances, stackSz, useFloat */
378 static OSA_TASK_DEFINE(wps_task, OSA_PRIORITY_NORMAL, 1, CONFIG_WPS_STACK_SIZE, 0);
379 
380 static int wlcm_wps_callback(enum wps_event event, void *data, uint16_t len);
381 
382 typedef enum
383 {
384     PROV_WPS_NONE,
385     PROV_WPS_STARTED,     /* WPS provisioning started */
386     PROV_WPS_PBC_ENABLED, /* WPS pushbutton attempt enabled */
387     PROV_WPS_PIN_ENABLED, /* WPS PIN attempt enabled */
388     PROV_WPS_SUCCESSFUL,  /* WPS based provisioning successful */
389 } wps_state_t;
390 
391 static struct
392 {
393     wps_state_t wps_state;
394     enum wps_session_command wps_cmd;
395     uint32_t wps_pin;
396 
397     /* WPS thread */
398     OSA_TASK_HANDLE_DEFINE(wps_task_Handle);
399 
400     OSA_SEMAPHORE_HANDLE_DEFINE(wps_scan_done);
401     struct wlan_scan_result wps_res;
402 } wlan_wps;
403 
404 static struct wps_config wps_conf = {
405     .role                    = 1, //WPS_ENROLLEE
406     .pin_generator           = 1,
407     .version                 = 0x20,
408     .version2                = 0x20,
409     .device_name             = "Redfinch",
410     .manufacture             = "NXP",
411     .model_name              = "rw610",
412     .model_number            = "0001",
413     .serial_number           = "0001",
414     .config_methods          = 0x2388,
415     .primary_dev_category    = 01,
416     .primary_dev_subcategory = 01,
417     .rf_bands                = 2,
418     .os_version              = 0xFFFFFFFF,
419     .wps_msg_max_retry       = 5,
420     .wps_msg_timeout         = 5000,
421     .pin_len                 = 8,
422     .wps_callback            = wlcm_wps_callback,
423     .prov_session            = PROV_NON_SESSION_ATTEMPT,
424 };
425 #endif /* CONFIG_WPS2 */
426 #ifdef RW610
427 static void wlcmgr_mon_task(osa_task_param_t arg);
428 
429 /* OSA_TASKS: name, priority, instances, stackSz, useFloat */
430 static OSA_TASK_DEFINE(wlcmgr_mon_task, CONFIG_NXP_WIFI_MON_TASK_PRIO , 1, CONFIG_NXP_WIFI_MON_TASK_STACK_SIZE, 0);
431 
432 /* The monitor thread event queue receives events from the power manager
433  * wlan notifier when idle hook is invoked and host is ready to enter
434  * specific low power mode
435  */
436 OSA_MSGQ_HANDLE_DEFINE(mon_thread_events, MAX_EVENTS, sizeof(struct wlan_message));
437 
438 #endif
439 typedef enum
440 {
441     WLCMGR_INACTIVE,
442     WLCMGR_INIT_DONE,
443     WLCMGR_ACTIVATED,
444     WLCMGR_THREAD_STOPPED,
445     WLCMGR_THREAD_DELETED,
446 } wlcmgr_status_t;
447 
448 static struct
449 {
450     /* This lock enables the scan response data to be accessed by multiple
451      * threads with the expectation that any thread accessing the scan lock may
452      * have to sleep until it is available.  The lock is taken by calling
453      * wlan_scan() and it is released by the WLAN Connection Manager main
454      * thread when the scan response data has been handled and
455      * is therefore free for another user.  This lock must never be taken
456      * in the WLAN Connection Manager main thread and it must only be
457      * released by that thread. The lock count must be 0 or 1. */
458     OSA_SEMAPHORE_HANDLE_DEFINE(scan_lock);
459     bool is_scan_lock;
460 
461     /* The WLAN Connection Manager event queue receives events (command
462      * responses, WiFi events, TCP stack events) from the wifi interface as
463      * well as user requests (connect, disconnect, scan).  This is the main
464      * blocking point for the main thread and acts as the state machine tick.*/
465     OSA_MSGQ_HANDLE_DEFINE(events, MAX_EVENTS, sizeof(struct wifi_message));
466 
467     /* internal state */
468     enum cm_sta_state sta_state, sta_ipv4_state;
469 #if CONFIG_IPV6
470     enum cm_sta_state sta_ipv6_state;
471 #endif
472     enum cm_sta_state sta_return_to;
473     enum cm_uap_state uap_state;
474     enum cm_uap_state uap_return_to;
475     /* store sta mac addr */
476     uint8_t sta_mac[MLAN_MAC_ADDR_LENGTH];
477     /* store uap mac addr */
478 #if UAP_SUPPORT
479     uint8_t uap_mac[MLAN_MAC_ADDR_LENGTH];
480 #endif
481 #if CONFIG_P2P
482     uint8_t wfd_mac[MLAN_MAC_ADDR_LENGTH];
483 #endif
484     /* callbacks */
485     int (*cb)(enum wlan_event_reason reason, void *data);
486     int (*scan_cb)(unsigned int count);
487     void (*event_cb)(int type);
488 
489     /* known networks list */
490     struct wlan_network networks[WLAN_MAX_KNOWN_NETWORKS];
491     int cur_network_idx;
492     int cur_uap_network_idx;
493 
494     unsigned int num_networks;
495     unsigned int scan_count;
496 
497     unsigned int uap_supported_max_sta_num;
498 
499     /* CM thread */
500     OSA_TASK_HANDLE_DEFINE(wlcmgr_task_Handle);
501 
502     unsigned running : 1;
503     unsigned stop_request : 1;
504     wlcmgr_status_t status;
505 
506     /*
507      * Power save state configuration
508      * These are states corresponding to the network that we are currently
509      * connected to. Not relevant, when we are not connected.
510      */
511     bool cm_ieeeps_configured : 1;
512     bool cm_deepsleepps_configured : 1;
513     bool connect_wakelock_taken : 1;
514     unsigned int wakeup_conditions;
515 #if CONFIG_HOST_SLEEP
516     bool is_hs_configured : 1;
517 #endif
518 #if CONFIG_MEF_CFG
519     bool is_mef_enabled : 1;
520 #endif
521 #if (CONFIG_WNM_PS)
522     bool cm_wnmps_configured;
523     t_u16 wnm_sleep_time;
524 #endif
525     wifi_fw_version_ext_t fw_ver_ext;
526 
527 #if CONFIG_WLAN_FAST_PATH
528     /* Data structures for fast path connection */
529     bool auth_cache_valid : 1;
530     bool fast_path_cache_valid : 1;
531     uint8_t fast_path_bss[MLAN_MAC_ADDR_LENGTH];
532     unsigned int fp_network;
533 #endif /* CONFIG_WLAN_FAST_PATH */
534     int uap_rsn_ie_index;
535     bool smart_mode_active : 1;
536 #if CONFIG_WPA_SUPP
537 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
538     OSA_TIMER_HANDLE_DEFINE(supp_status_timer);
539     int status_timeout;
540     bool connect : 1;
541 #endif
542     bool pending_disconnect_request : 1;
543 #if CONFIG_WPA_SUPP_WPS
544     int wps_session_attempt;
545 #endif
546 #endif
547 #if (CONFIG_WPA2_ENTP) || ((CONFIG_WPA_SUPP) && (CONFIG_WPA_SUPP_CRYPTO_ENTERPRISE))
548     t_u8 *ca_cert_data;
549     t_u32 ca_cert_len;
550     t_u8 *client_cert_data;
551     t_u32 client_cert_len;
552     t_u8 *client_key_data;
553     t_u32 client_key_len;
554     t_u8 *ca_cert2_data;
555     t_u32 ca_cert2_len;
556     t_u8 *client_cert2_data;
557     t_u32 client_cert2_len;
558     t_u8 *client_key2_data;
559     t_u32 client_key2_len;
560 #if CONFIG_WPA_SUPP_AP
561 #if CONFIG_WPA_SUPP_CRYPTO_AP_ENTERPRISE
562     t_u8 *dh_data;
563     t_u32 dh_len;
564     t_u8 *server_cert_data;
565     t_u32 server_cert_len;
566     t_u8 *server_key_data;
567     t_u32 server_key_len;
568 #endif
569 #endif
570 #endif
571     OSA_TIMER_HANDLE_DEFINE(assoc_timer);
572     bool assoc_paused : 1;
573     bool pending_assoc_request : 1;
574     bool reassoc_control : 1;
575     bool reassoc_request : 1;
576     unsigned int reassoc_count;
577     bool hs_enabled;
578     bool hs_configured;
579     unsigned int hs_wakeup_condition;
580     wifi_scan_chan_list_t scan_chan_list;
581 #if CONFIG_WPA2_ENTP
582     bool allow_wpa2_enterprise_ap_only : 1;
583 #endif
584     bool hidden_scan_on : 1;
585 #if CONFIG_ROAMING
586     bool roaming_enabled : 1;
587 #endif
588 #if CONFIG_11R
589     bool ft_bss : 1;
590 #endif
591     bool same_ess : 1;
592 #if CONFIG_BG_SCAN
593     unsigned int bgscan_attempt;
594 #endif
595     bool roam_reassoc : 1;
596 #if CONFIG_WIFI_FW_DEBUG
597     void (*wlan_usb_init_cb)(void);
598 #endif
599 #if CONFIG_11R
600 #if CONFIG_WPA_SUPP
601     OSA_TIMER_HANDLE_DEFINE(ft_roam_timer);
602 #endif
603 #endif
604 #if CONFIG_11K
605     bool enable_11k : 1;
606     wlan_rrm_scan_cb_param rrm_scan_cb_param;
607 #endif
608 #if (CONFIG_11K) || (CONFIG_11V)
609     OSA_TIMER_HANDLE_DEFINE(neighbor_req_timer);
610     bool neighbor_req : 1;
611 #endif
612 #if (CONFIG_11K) || (CONFIG_11V)
613     wlan_nlist_report_param nlist_rep_param;
614     wlan_rrm_neighbor_report_t nbr_rpt;
615 #endif
616 #if (CONFIG_11K) || (CONFIG_11V) || (CONFIG_11R) || (CONFIG_ROAMING)
617     uint8_t rssi_low_threshold;
618 #endif
619     uint8_t ind_reset;
620 #if CONFIG_HOST_SLEEP
621     uint8_t hs_dummy_send;
622 #endif
623 #if (CONFIG_WIFI_IND_RESET) && (CONFIG_WIFI_IND_DNLD)
624     uint8_t ir_mode;
625 #endif
626     bool internal : 1;
627 } wlan;
628 
629 OSA_TASK_HANDLE_DEFINE(wlcmgr_mon_task_Handle);
630 bool wlan_in_reset = false;
631 
632 #if CONFIG_CLOUD_KEEP_ALIVE
633 #define MIN_KEEP_ALIVE_ID 0
634 #define MAX_KEEP_ALIVE_ID 4
635 wlan_cloud_keep_alive_t cloud_keep_alive_param[MAX_KEEP_ALIVE_ID];
636 #endif
637 
638 void wlan_wake_up_card(void);
639 
640 #if CONFIG_WLCMGR_DEBUG
dbg_sta_state_name(enum cm_sta_state state)641 static char *dbg_sta_state_name(enum cm_sta_state state)
642 {
643     switch (state)
644     {
645         case CM_STA_INITIALIZING:
646             return "initializing";
647         case CM_STA_IDLE:
648             return "idle";
649         case CM_STA_SCANNING:
650             return "scanning";
651         case CM_STA_SCANNING_USER:
652             return "user scanning";
653         case CM_STA_ASSOCIATING:
654             return "associating";
655         case CM_STA_ASSOCIATED:
656             return "associated";
657         case CM_STA_AUTHENTICATED:
658             return "authenticated";
659         case CM_STA_REQUESTING_ADDRESS:
660             return "requesting address";
661         case CM_STA_OBTAINING_ADDRESS:
662             return "obtaining address";
663         case CM_STA_CONNECTED:
664             return "connected";
665         default:
666             return "unknown";
667     }
668 }
669 
dbg_uap_state_name(enum cm_uap_state state)670 static char *dbg_uap_state_name(enum cm_uap_state state)
671 {
672     switch (state)
673     {
674         case CM_UAP_INITIALIZING:
675             return "initializing";
676         case CM_UAP_CONFIGURED:
677             return "configured";
678         case CM_UAP_STARTED:
679             return "started";
680         case CM_UAP_IP_UP:
681             return "IP configured";
682         default:
683             return "unknown";
684     }
685 }
686 
dbg_lock_info(void)687 static void dbg_lock_info(void)
688 {
689 #if 0
690 	int ret;
691 	char *name;
692 	unsigned long cur_val, suspended_count;
693 	TX_THREAD *first_suspended;
694 	TX_SEMAPHORE *next;
695 
696 	ret = tx_semaphore_info_get(&wlan.scan_lock, &name, &cur_val,
697 				    &first_suspended, &suspended_count, &next);
698 	if (ret) {
699 		wlcm_e("unable to fetch scan lock info");
700 		return;
701 	}
702 
703 	wlcm_d("scan lock info: count=%ld, suspended=%ld", cur_val,
704 	       suspended_count);
705 #endif
706 }
707 #else
708 #define dbg_lock_info(...)
709 #define dbg_sta_state_name(...)
710 #define dbg_uap_state_name(...)
711 #endif /* CONFIG_WLCMGR_DEBUG */
712 
713 /*
714  * Utility Functions
715  */
716 
verify_scan_duration_value(int scan_duration)717 int verify_scan_duration_value(int scan_duration)
718 {
719     if (scan_duration >= 50 && scan_duration <= 500)
720     {
721         return WM_SUCCESS;
722     }
723     return -WM_FAIL;
724 }
725 
verify_scan_channel_value(int channel)726 int verify_scan_channel_value(int channel)
727 {
728     if (channel >= 0 && channel <= 11)
729     {
730         return WM_SUCCESS;
731     }
732     return -WM_FAIL;
733 }
734 
verify_split_scan_delay(int delay)735 int verify_split_scan_delay(int delay)
736 {
737     if (delay >= 30 && delay <= 300)
738     {
739         return WM_SUCCESS;
740     }
741     return -WM_FAIL;
742 }
743 
set_scan_params(struct wifi_scan_params_t * wifi_scan_params)744 int set_scan_params(struct wifi_scan_params_t *wifi_scan_params)
745 {
746     if (!verify_scan_duration_value(wifi_scan_params->scan_duration))
747     {
748         g_wifi_scan_params.scan_duration = wifi_scan_params->scan_duration;
749     }
750     else
751     {
752         wlcm_e("scan duration value invalid!");
753         return -WM_FAIL;
754     }
755     if (!verify_scan_channel_value(wifi_scan_params->channel[0]))
756     {
757         g_wifi_scan_params.channel[0] = wifi_scan_params->channel[0];
758     }
759     else
760     {
761         wlcm_e("scan channel value invalid!");
762         return -WM_FAIL;
763     }
764     if (!verify_split_scan_delay(wifi_scan_params->split_scan_delay))
765     {
766         g_wifi_scan_params.split_scan_delay = wifi_scan_params->split_scan_delay;
767     }
768     else
769     {
770         wlcm_e("split scan delay value invalid!");
771         return -WM_FAIL;
772     }
773 
774     return WM_SUCCESS;
775 }
776 
777 #if CONFIG_RX_ABORT_CFG
wlan_set_get_rx_abort_cfg(struct wlan_rx_abort_cfg * cfg,t_u16 action)778 int wlan_set_get_rx_abort_cfg(struct wlan_rx_abort_cfg *cfg, t_u16 action)
779 {
780     return wifi_set_get_rx_abort_cfg((void *)cfg, action);
781 }
782 #endif
783 
784 #if CONFIG_RX_ABORT_CFG_EXT
wlan_set_rx_abort_cfg_ext(const struct wlan_rx_abort_cfg_ext * cfg)785 int wlan_set_rx_abort_cfg_ext(const struct wlan_rx_abort_cfg_ext *cfg)
786 {
787     return wifi_set_get_rx_abort_cfg_ext((void *)cfg, ACTION_SET);
788 }
789 
wlan_get_rx_abort_cfg_ext(struct wlan_rx_abort_cfg_ext * cfg)790 int wlan_get_rx_abort_cfg_ext(struct wlan_rx_abort_cfg_ext *cfg)
791 {
792     return wifi_set_get_rx_abort_cfg_ext((void *)cfg, ACTION_GET);
793 }
794 #endif
795 
796 #if CONFIG_CCK_DESENSE_CFG
wlan_set_get_cck_desense_cfg(struct wlan_cck_desense_cfg * cfg,t_u16 action)797 int wlan_set_get_cck_desense_cfg(struct wlan_cck_desense_cfg *cfg, t_u16 action)
798 {
799     return wifi_set_get_cck_desense_cfg((void *)cfg, action);
800 }
801 #endif
802 
get_scan_params(struct wifi_scan_params_t * wifi_scan_params)803 int get_scan_params(struct wifi_scan_params_t *wifi_scan_params)
804 {
805     wifi_scan_params->scan_duration    = g_wifi_scan_params.scan_duration;
806     wifi_scan_params->channel[0]       = g_wifi_scan_params.channel[0];
807     wifi_scan_params->split_scan_delay = g_wifi_scan_params.split_scan_delay;
808     return WM_SUCCESS;
809 }
810 
wlan_dhcp_cleanup()811 void wlan_dhcp_cleanup()
812 {
813     net_interface_dhcp_stop(net_get_mlan_handle());
814 }
815 
816 #if CONFIG_HOST_SLEEP
wlan_map_to_wifi_wakeup_condtions(const uint32_t wlan_wakeup_condtions)817 static uint32_t wlan_map_to_wifi_wakeup_condtions(const uint32_t wlan_wakeup_condtions)
818 {
819     uint32_t conditions = 0;
820     if ((wlan_wakeup_condtions & WAKE_ON_UNICAST) != 0U)
821     {
822         conditions |= WIFI_WAKE_ON_UNICAST;
823     }
824 
825     if ((wlan_wakeup_condtions & WAKE_ON_ALL_BROADCAST) != 0U)
826     {
827         conditions |= WIFI_WAKE_ON_ALL_BROADCAST;
828     }
829 
830     if ((wlan_wakeup_condtions & WAKE_ON_MULTICAST) != 0U)
831     {
832         conditions |= WIFI_WAKE_ON_MULTICAST;
833     }
834 
835     if ((wlan_wakeup_condtions & WAKE_ON_ARP_BROADCAST) != 0U)
836     {
837         conditions |= WIFI_WAKE_ON_ARP_BROADCAST;
838     }
839 
840     if ((wlan_wakeup_condtions & WAKE_ON_MAC_EVENT) != 0U)
841     {
842         conditions |= WIFI_WAKE_ON_MAC_EVENT;
843     }
844 
845     if ((wlan_wakeup_condtions & WAKE_ON_MGMT_FRAME) != 0U)
846     {
847         conditions |= WIFI_WAKE_ON_MGMT_FRAME;
848     }
849 
850     return conditions;
851 }
852 #endif
853 
wlan_is_started(void)854 int wlan_is_started(void)
855 {
856     return ((wlan.running == 1) && (wlan.status == WLCMGR_ACTIVATED));
857 }
858 
is_user_scanning(void)859 static bool is_user_scanning(void)
860 {
861     return (wlan.sta_state == CM_STA_SCANNING_USER);
862 }
863 
is_state(enum cm_sta_state state)864 static bool is_state(enum cm_sta_state state)
865 {
866     if (is_user_scanning() == true)
867     {
868         return (wlan.sta_return_to == state);
869     }
870 
871     return (wlan.sta_state == state);
872 }
873 
874 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
is_uap_state(int state)875 static bool is_uap_state(int state)
876 {
877     int curr_state = wifi_nxp_hapd_state();
878 
879     return (state == curr_state);
880 }
881 #else
is_uap_state(enum cm_uap_state state)882 static bool is_uap_state(enum cm_uap_state state)
883 {
884     return (wlan.uap_state == state);
885 }
886 #endif
887 
is_uap_starting(void)888 static bool is_uap_starting(void)
889 {
890 #if UAP_SUPPORT
891 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
892     int state = wifi_nxp_hapd_state();
893 
894     return ((state > HAPD_IFACE_DISABLED) && (state <= HAPD_IFACE_ENABLED));
895 #else
896     return ((wlan.uap_state > CM_UAP_INITIALIZING) && (wlan.uap_state <= CM_UAP_IP_UP));
897 #endif
898 #else
899     return false;
900 #endif
901 }
902 
wlan_get_ipv4_addr(unsigned int * ipv4_addr)903 static int wlan_get_ipv4_addr(unsigned int *ipv4_addr)
904 {
905     return net_get_if_ip_addr(ipv4_addr, net_get_sta_handle());
906 }
907 
908 #if UAP_SUPPORT && (CONFIG_HOST_SLEEP || CONFIG_MEF_CFG)
wlan_get_uap_ipv4_addr(unsigned int * ipv4_addr)909 static int wlan_get_uap_ipv4_addr(unsigned int *ipv4_addr)
910 {
911     return net_get_if_ip_addr(ipv4_addr, net_get_uap_handle());
912 }
913 #endif
914 
915 static int wlan_set_pmfcfg(uint8_t mfpc, uint8_t mfpr);
916 
917 #if CONFIG_HOST_SLEEP
wlan_send_host_sleep_int(uint32_t wake_up_conds,bool is_config)918 static int wlan_send_host_sleep_int(uint32_t wake_up_conds, bool is_config)
919 {
920     int ret = WM_SUCCESS;
921     unsigned int ipv4_addr = 0;
922     enum wlan_bss_type type = WLAN_BSS_TYPE_STA;
923 
924     if (!wlan_is_started())
925     {
926         (void)PRINTF("Wakeup condition configure is not allowed when WIFI is disabled\r\n");
927         return -WM_FAIL;
928     }
929 
930     /* Check if wake_up_conds is valid or not */
931     if (wake_up_conds && (wake_up_conds != HOST_SLEEP_CFG_CANCEL) && (wake_up_conds & 0x20))
932     {
933         (void)PRINTF("Invalid wake_up_conds. Bit 5 is reserved.\r\n");
934         return -WM_FAIL;
935     }
936 
937     if (!is_sta_connected()
938 #if UAP_SUPPORT
939         && !mlan_adap->priv[1]->media_connected
940 #endif
941         )
942     {
943         if ((wake_up_conds & (WAKE_ON_ALL_BROADCAST | WAKE_ON_UNICAST | WAKE_ON_MULTICAST
944                                    | WAKE_ON_ARP_BROADCAST | WAKE_ON_MGMT_FRAME)) != 0)
945         {
946             wlcm_e("Connection on STA or uAP is required for configured bitmap!\r\n");
947             ret = -WM_FAIL;
948             return ret;
949         }
950     }
951 
952     if (wake_up_conds == HOST_SLEEP_CFG_CANCEL)
953     {
954         wlan.hs_enabled = MFALSE;
955         wlan.hs_configured       = MFALSE;
956         wlan.hs_wakeup_condition = wake_up_conds;
957 #if CONFIG_MEF_CFG
958         wlan.is_mef_enabled = MFALSE;
959         (void)memset(&g_flt_cfg, 0, sizeof(wlan_flt_cfg_t));
960         wifi_set_packet_filters(&g_flt_cfg);
961 #endif
962     }
963 #if CONFIG_CLOUD_KEEP_ALIVE
964     else if(is_config == MFALSE)
965     {
966         wlan_start_cloud_keep_alive();
967     }
968 #endif
969 
970 #ifndef RW610
971 #if CONFIG_MEF_CFG
972     if (wake_up_conds == HOST_SLEEP_NO_COND)
973     {
974         wlan.hs_enabled = MTRUE;
975         wlan.hs_wakeup_condition = wake_up_conds;
976         if (g_flt_cfg.nentries == 0 && (is_config == MFALSE))
977         {
978             (void)PRINTF("No user configured MEF entries, use default ARP filters.\r\n");
979             /* User doesn't configure MEF, use default MEF entry */
980             wlan_mef_set_auto_arp(MEF_ACTION_ALLOW_AND_WAKEUP_HOST);
981         }
982         if (wlan.is_mef_enabled == MFALSE)
983         {
984             wlan.is_mef_enabled = MTRUE;
985             wifi_set_packet_filters(&g_flt_cfg);
986         }
987     }
988     else
989 #endif
990 #endif
991     {
992         wlan.hs_enabled = MTRUE;
993         wlan.hs_wakeup_condition = wlan_map_to_wifi_wakeup_condtions(wake_up_conds);
994     }
995 
996     if (is_sta_ipv4_connected() != 0)
997     {
998         ret = wlan_get_ipv4_addr(&ipv4_addr);
999         if (ret != WM_SUCCESS)
1000         {
1001             wlcm_e("HS: cannot get STA IP, check if STA disconnected");
1002             return -WM_FAIL;
1003         }
1004     }
1005 #if UAP_SUPPORT
1006     else if (is_uap_started())
1007     {
1008         ret = wlan_get_uap_ipv4_addr(&ipv4_addr);
1009         if (ret != WM_SUCCESS)
1010         {
1011             wlcm_e("HS: cannot get UAP IP, check if uAP stopped");
1012             return -WM_FAIL;
1013         }
1014         type = WLAN_BSS_TYPE_UAP;
1015     }
1016 #endif
1017     else
1018     {
1019         ipv4_addr = 0;
1020     }
1021 
1022     if (wlan.hs_dummy_send == MFALSE || is_config == MFALSE)
1023     {
1024         ret = wifi_send_hs_cfg_cmd((mlan_bss_type)type, ipv4_addr, HS_CONFIGURE, wlan.hs_wakeup_condition);
1025         if (ret == WM_SUCCESS)
1026         {
1027             if (wake_up_conds != HOST_SLEEP_CFG_CANCEL)
1028             {
1029                 wlan.hs_configured = MTRUE;
1030             }
1031         }
1032         wlan.hs_dummy_send = MTRUE;
1033     }
1034     return ret;
1035 }
1036 
wlan_hs_pre_cfg(void)1037 void wlan_hs_pre_cfg(void)
1038 {
1039     if (wlan.hs_enabled == MTRUE)
1040     {
1041         (void)wlan_send_host_sleep_int(wlan.hs_wakeup_condition, MFALSE);
1042         /** Wait for HS Activate to complete */
1043         OSA_TimeDelay(1000);
1044     }
1045 }
1046 
wlan_hs_post_cfg(void)1047 void wlan_hs_post_cfg(void)
1048 {
1049     uint16_t hs_wakeup_reason;
1050 
1051     if (wlan.hs_enabled == MTRUE)
1052     {
1053         (void)wifi_get_wakeup_reason(&hs_wakeup_reason);
1054 
1055         (void)wifi_print_wakeup_reason(hs_wakeup_reason);
1056     }
1057 }
1058 
wlan_send_host_sleep(uint32_t wake_up_conds)1059 int wlan_send_host_sleep(uint32_t wake_up_conds)
1060 {
1061     if (!wlan.running)
1062     {
1063         return WLAN_ERROR_STATE;
1064     }
1065 
1066     wakelock_get();
1067 
1068     (void)send_user_request(CM_STA_USER_REQUEST_HS, wake_up_conds);
1069 
1070     return WM_SUCCESS;
1071 }
1072 
wlan_get_wakeup_reason(uint16_t * hs_wakeup_reason)1073 int wlan_get_wakeup_reason(uint16_t *hs_wakeup_reason)
1074 {
1075     return wifi_get_wakeup_reason(hs_wakeup_reason);
1076 }
1077 
1078 #endif
1079 
1080 #if CONFIG_HOST_SLEEP
1081 #ifdef RW610
wlan_hs_send_event(int id,void * data)1082 status_t wlan_hs_send_event(int id, void *data)
1083 {
1084     struct wlan_message msg;
1085 
1086     (void)memset(&msg, 0U, sizeof(struct wlan_message));
1087     msg.data = data;
1088     msg.id  = id;
1089     if (OSA_MsgQPut((osa_msgq_handle_t)mon_thread_events, &msg) != KOSA_StatusSuccess)
1090     {
1091         (void)PRINTF("PM: Failed to send msg to queue\r\n");
1092         return -WM_FAIL;
1093     }
1094     return WM_SUCCESS;
1095 }
1096 #endif
1097 
1098 #if CONFIG_POWER_MANAGER
wake_timer_cb(osa_timer_arg_t arg)1099 static void wake_timer_cb(osa_timer_arg_t arg)
1100 {
1101     if(wakelock_isheld())
1102         wakelock_put();
1103 }
1104 #endif
1105 
1106 #if CONFIG_MEF_CFG
wlan_wowlan_config(uint8_t is_mef,t_u32 wake_up_conds)1107 int wlan_wowlan_config(uint8_t is_mef, t_u32 wake_up_conds)
1108 #else
1109 int wlan_wowlan_config(t_u32 wake_up_conds)
1110 #endif
1111 {
1112     int ret = WM_SUCCESS;
1113 
1114     if (!wlan_is_started())
1115     {
1116         (void)PRINTF("Wakeup condition configure is not allowed when WIFI is disabled\r\n");
1117         return -WM_FAIL;
1118     }
1119 
1120     /* Check if wake_up_conds is valid or not */
1121     if (wake_up_conds && (wake_up_conds & 0x20))
1122     {
1123         (void)PRINTF("Invalid wake_up_conds. Bit 5 is reserved.\r\n");
1124         return -WM_FAIL;
1125     }
1126 
1127     if (!is_sta_connected()
1128 #if UAP_SUPPORT
1129         && !mlan_adap->priv[1]->media_connected
1130 #endif
1131         )
1132     {
1133 #if CONFIG_MEF_CFG
1134         if (is_mef)
1135         {
1136             wlcm_e("Connection on STA or uAP is required for MEF configuration\r\n");
1137             ret = -WM_FAIL;
1138             return ret;
1139         }
1140         else
1141 #endif
1142              if ((wake_up_conds & (WAKE_ON_ALL_BROADCAST | WAKE_ON_UNICAST | WAKE_ON_MULTICAST
1143                                    | WAKE_ON_ARP_BROADCAST | WAKE_ON_MGMT_FRAME)) != 0)
1144         {
1145             wlcm_e("Connection on STA or uAP is required for configured bitmap!\r\n");
1146             ret = -WM_FAIL;
1147             return ret;
1148         }
1149     }
1150 
1151 #if CONFIG_MEF_CFG
1152     if (is_mef)
1153     {
1154         wlan.wakeup_conditions = 0;
1155         if (g_flt_cfg.nentries == 0)
1156         {
1157             (void)PRINTF("No user configured MEF entries, use default ARP filters.\r\n");
1158             /* User doesn't configure MEF, use default MEF entry */
1159             wlan_mef_set_auto_arp(MEF_ACTION_ALLOW_AND_WAKEUP_HOST);
1160         }
1161         wifi_set_packet_filters(&g_flt_cfg);
1162     }
1163     else
1164 #endif
1165     {
1166         wlan.wakeup_conditions = wake_up_conds;
1167         if (wake_up_conds & WIFI_WAKE_ON_MGMT_FRAME)
1168         {
1169           /* Set management frame wakeup filter config */
1170           mlan_adap->mgmt_filter[0].action     = 0x3;      /* not discard packet, wakeup host */
1171           mlan_adap->mgmt_filter[0].type       = 0xff;     /* management frames */
1172           mlan_adap->mgmt_filter[0].frame_mask = 0x3C0F;   /* Frame-Mask bits :
1173                                                               : Bit 0 - Association Request(unmask)
1174                                                               : Bit 1 - Association Response(unmask)
1175                                                               : Bit 2 - Re-Association Request(unmask)
1176                                                               : Bit 3 - Re-Association Response(unmask)
1177                                                               : Bit 4 - Probe Request(mask)
1178                                                               : Bit 5 - Probe Response(mask)
1179                                                               : Bit 8 - Beacon Frames(mask)
1180                                                               : Bit 10 - Disassociation(unmask)
1181                                                               : Bit 11 - Authentication(unmask)
1182                                                               : Bit 12 - Deauthentication(unmask)
1183                                                               : Bit 13 - Action Frames(unmask)
1184                                                             */
1185         }
1186 #if CONFIG_MEF_CFG
1187         /* Clear previous MEF entries */
1188         if (g_flt_cfg.nentries != 0)
1189         {
1190             (void)memset(&g_flt_cfg, 0, sizeof(wlan_flt_cfg_t));
1191             wifi_set_packet_filters(&g_flt_cfg);
1192         }
1193 #endif
1194     }
1195 
1196     return ret;
1197 }
1198 
wlan_config_host_sleep(bool is_manual,t_u8 is_periodic)1199 void wlan_config_host_sleep(bool is_manual, t_u8 is_periodic)
1200 {
1201     int ret = 0;
1202 
1203 #if CONFIG_WMM_UAPSD
1204     if (mlan_adap && mlan_adap->pps_uapsd_mode)
1205     {
1206         wlcm_e("Host sleep is not allowed if UAPSD/PPS is activated");
1207         return;
1208     }
1209 #endif
1210     wlan_is_manual = is_manual;
1211     if (!wlan_is_manual)
1212     {
1213 #if CONFIG_POWER_MANAGER
1214 	if (!wlan_is_started())
1215         {
1216             wlcm_e("Host sleep is not allowed when WIFI is disabled\r\n");
1217             return;
1218         }
1219         if (is_periodic)
1220             wlan_host_sleep_state = HOST_SLEEP_PERIODIC;
1221         else
1222             wlan_host_sleep_state = HOST_SLEEP_ONESHOT;
1223 #endif
1224     }
1225     else
1226     {
1227 #if CONFIG_POWER_MANAGER
1228         /* Reset flag and stop timer if manual mode is selected without cancel periodic sleep */
1229         wlan_host_sleep_state = HOST_SLEEP_DISABLE;
1230         if (OSA_TimerIsRunning((osa_timer_handle_t)wake_timer))
1231         {
1232             OSA_TimerDeactivate((osa_timer_handle_t)wake_timer);
1233             wakelock_put();
1234         }
1235 #endif
1236         if (wlan.status == WLCMGR_ACTIVATED)
1237         {
1238 #if CONFIG_HOST_SLEEP
1239             /* Start host sleep handshake here if manual mode is selected */
1240             ret = wlan_send_host_sleep_int(wlan.wakeup_conditions, MFALSE);
1241             if (ret != WM_SUCCESS)
1242             {
1243 #if CONFIG_NCP_BRIDGE
1244                 app_notify_event(APP_EVT_HS_CONFIG, APP_EVT_REASON_FAILURE, NULL, 0);
1245 #endif
1246                 wlcm_e("Error: Failed to config host sleep");
1247                 return;
1248             }
1249 #endif
1250         }
1251     }
1252 }
1253 
wlan_cancel_host_sleep(void)1254 void wlan_cancel_host_sleep(void)
1255 {
1256     int ret = 0;
1257     enum wlan_bss_type type = WLAN_BSS_TYPE_STA;
1258 
1259     if (!wlan_is_started())
1260     {
1261         PRINTF("Wlan not started, can't cancel host sleep\r\n");
1262         return;
1263     }
1264 
1265     if (is_uap_started() != 0)
1266         type = WLAN_BSS_TYPE_UAP;
1267     ret = wifi_cancel_host_sleep((mlan_bss_type)type);
1268     if (ret != WM_SUCCESS)
1269     {
1270         wlcm_e("Error: Failed to send host sleep cancel command");
1271         return;
1272     }
1273 }
1274 
wlan_clear_host_sleep_config(void)1275 void wlan_clear_host_sleep_config(void)
1276 {
1277     wlan_is_manual = MFALSE;
1278 #if CONFIG_POWER_MANAGER
1279     wlan_host_sleep_state = HOST_SLEEP_DISABLE;
1280 #if CONFIG_UART_INTERRUPT
1281     usart_suspend_flag = MFALSE;
1282 #endif
1283     if (OSA_TimerIsRunning((osa_timer_handle_t)wake_timer))
1284     {
1285         OSA_TimerDeactivate((osa_timer_handle_t)wake_timer);
1286         wakelock_put();
1287     }
1288     is_hs_handshake_done = 0;
1289 #endif
1290 #if CONFIG_MEF_CFG
1291     memset(&g_flt_cfg, 0x0, sizeof(wlan_flt_cfg_t));
1292 
1293     if (wlan_is_started())
1294     {
1295 	wifi_set_packet_filters(&g_flt_cfg);
1296     }
1297 #endif
1298     wakeup_by = 0;
1299     wifi_clear_wakeup_reason();
1300     wlan.wakeup_conditions = 0;
1301     wlan.is_hs_configured = MFALSE;
1302 }
1303 #endif
1304 
wlan_send_sleep_confirm(void)1305 static void wlan_send_sleep_confirm(void)
1306 {
1307     enum wlan_bss_type type = WLAN_BSS_TYPE_STA;
1308 
1309     if (is_uap_started() != 0)
1310     {
1311         type = WLAN_BSS_TYPE_UAP;
1312     }
1313 
1314     send_sleep_confirm_command((mlan_bss_type)type);
1315 }
1316 
is_bssid_any(char * b)1317 static int is_bssid_any(char *b)
1318 {
1319     return (int)(!b[0] && !b[1] && !b[2] && !b[3] && !b[4] && !b[5]);
1320 }
1321 
1322 /* Check to see if the security features of our network, 'config', match with
1323  * those of a scan result, 'res' and return 1 if they do, 0 if they do not. */
security_profile_matches(const struct wlan_network * network,const struct wifi_scan_result2 * res)1324 static int security_profile_matches(const struct wlan_network *network, const struct wifi_scan_result2 *res)
1325 {
1326     const struct wlan_network_security *config = &network->security;
1327 
1328     /* No security: just check that the scan result doesn't specify security */
1329     if (config->type == WLAN_SECURITY_NONE)
1330     {
1331 #if CONFIG_WPS2
1332         if (res->wps_session != WPS_SESSION_INACTIVE)
1333             return 1;
1334 #endif
1335 #if CONFIG_DRIVER_OWE
1336         if (res->trans_mode == OWE_TRANS_MODE_OPEN)
1337         {
1338             return res->trans_ssid_len;
1339         }
1340         else if (res->trans_mode == OWE_TRANS_MODE_OWE)
1341         {
1342             return res->WPA_WPA2_WEP.wpa2;
1343         }
1344         else
1345         { /* Do Nothing */
1346         }
1347 #endif
1348 
1349         if (res->WPA_WPA2_WEP.wepStatic || res->WPA_WPA2_WEP.wpa2 || res->WPA_WPA2_WEP.wpa)
1350         {
1351             return WM_SUCCESS;
1352         }
1353 
1354         return 1;
1355     }
1356 
1357     /* WEP mode: if we are using WEP, the AP must use static WEP */
1358     if (config->type == WLAN_SECURITY_WEP_OPEN || config->type == WLAN_SECURITY_WEP_SHARED)
1359     {
1360         if (res->phtcap_ie_present)
1361         {
1362             wlcm_e(
1363                 "As per Wi-Fi Certification WEP "
1364                 "is not used with HT associations "
1365                 "in 11n devices");
1366             return 0;
1367         }
1368         return (int)res->WPA_WPA2_WEP.wepStatic;
1369     }
1370 
1371     /* WPA/WPA2 mode: if we are using WPA/WPA2, the AP must use WPA/WPA2 */
1372     if (config->type == WLAN_SECURITY_WPA_WPA2_MIXED)
1373     {
1374         return (int)(res->WPA_WPA2_WEP.wpa | res->WPA_WPA2_WEP.wpa2);
1375     }
1376 
1377     /* WPA2 mode: if we are using WPA2, the AP must use WPA2 */
1378     if (config->type == WLAN_SECURITY_WPA2)
1379     {
1380         return (int)(res->WPA_WPA2_WEP.wpa2 | res->WPA_WPA2_WEP.wpa2_sha256);
1381 
1382     }
1383 
1384 #if CONFIG_11R
1385     /* WPA2_FT mode: if we are using WPA2, the AP must use WPA2_FT */
1386     if (config->type == WLAN_SECURITY_WPA2_FT)
1387     {
1388         return (int)(res->WPA_WPA2_WEP.ft_psk);
1389 
1390     }
1391 #endif
1392 
1393     /* OWE mode: if we are using OWE, the AP must use OWE */
1394 #if CONFIG_DRIVER_OWE
1395     if (config->type == WLAN_SECURITY_OWE_ONLY)
1396     {
1397         if (res->trans_mode == OWE_TRANS_MODE_OPEN)
1398         {
1399             return res->trans_ssid_len;
1400         }
1401         else
1402         {
1403             return (int)res->WPA_WPA2_WEP.owe;
1404         }
1405     }
1406 #endif
1407 
1408     /* WPA mode: if we are using WPA, the AP must use WPA */
1409     if (config->type == WLAN_SECURITY_WPA)
1410     {
1411         if (res->wpa_ucstCipher.tkip != 0U)
1412         {
1413             wlcm_e(
1414                 "As per Wi-Fi Certification security "
1415                 "combinations \"WPA\" alone is not allowed "
1416                 "security type.");
1417             wlcm_e(
1418                 "Please use WLAN_SECURITY_WPA_WPA2_MIXED "
1419                 "security type to connect to "
1420                 "WPA(TKIP) Only AP.");
1421             return (int)!res->wpa_ucstCipher.tkip;
1422         }
1423         return (int)res->WPA_WPA2_WEP.wpa;
1424     }
1425 
1426 #if CONFIG_WPA2_ENTP
1427     /* WPA2 Enterprise mode: if we are using WPA2 Enterprise,
1428      * the AP must use WPA2 Enterpise */
1429     if (config->type == WLAN_SECURITY_EAP_TLS || config->type == WLAN_SECURITY_EAP_PEAP_MSCHAPV2)
1430         return res->wpa2_entp_IE_exist;
1431 #endif
1432 
1433     if ((config->type == WLAN_SECURITY_WPA3_SAE) || (config->type == WLAN_SECURITY_WPA2_WPA3_SAE_MIXED))
1434     {
1435         uint8_t mfpc, mfpr;
1436 
1437         (void)wlan_get_pmfcfg(&mfpc, &mfpr);
1438 
1439         if (!mfpc && !mfpr)
1440         {
1441             wlcm_e("As per WPA3 SAE Certification, PMF is mandatory.\r\n");
1442             return WM_SUCCESS;
1443         }
1444         if (config->type == WLAN_SECURITY_WPA3_SAE)
1445             return (int)(res->WPA_WPA2_WEP.wpa3_sae);
1446         if (config->type == WLAN_SECURITY_WPA2_WPA3_SAE_MIXED)
1447             return (int)(res->WPA_WPA2_WEP.wpa3_sae | res->WPA_WPA2_WEP.wpa2 | res->WPA_WPA2_WEP.wpa2_sha256);
1448     }
1449     return WM_SUCCESS;
1450 }
1451 
1452 /* Check if 'network' matches the scan result 'res' based on network properties
1453  * and the security profile.  If we found a match, update 'network' with any
1454  * new information discovered when parsing 'res'. 192 We may update the channel,
1455  * BSSID, or SSID but not the security profile. */
network_matches_scan_result(const struct wlan_network * network,const struct wifi_scan_result2 * res,uint8_t * num_channels,wlan_scan_channel_list_t * chan_list)1456 static int network_matches_scan_result(const struct wlan_network *network,
1457                                        const struct wifi_scan_result2 *res,
1458                                        uint8_t *num_channels,
1459                                        wlan_scan_channel_list_t *chan_list)
1460 {
1461     uint8_t null_ssid[IEEEtypes_SSID_SIZE] = {0};
1462     uint16_t idx = 0;
1463 
1464 #if CONFIG_11V
1465     if ((wlan.roam_reassoc == true) && (wlan.nlist_rep_param.nlist_mode == WLAN_NLIST_11V) &&
1466         ((wlan.nlist_rep_param.btm_mode & 0x1C) != 0U))
1467     {
1468         if (memcmp((const void *)network->bssid, (const void *)res->bssid, (size_t)IEEEtypes_ADDRESS_SIZE) == 0)
1469         {
1470             return -WM_FAIL;
1471         }
1472     }
1473 #endif
1474     /* Check basic network information that we know */
1475     if (network->channel_specific && network->channel != res->Channel)
1476     {
1477         wlcm_d("%s: Channel mismatch. Got: %d Expected: %d", network->ssid, res->Channel, network->channel);
1478         return -WM_FAIL;
1479     }
1480     if (network->bssid_specific && memcmp(network->bssid, res->bssid, 6))
1481     {
1482         wlcm_d("%s: bssid mismatch.", network->ssid);
1483         return -WM_FAIL;
1484     }
1485 
1486     if (network->ssid_specific != 0U)
1487     {
1488         if (!wlan.hidden_scan_on && (!memcmp(null_ssid, (const char *)res->ssid, (size_t)res->ssid_len)))
1489         {
1490             chan_list[*num_channels].chan_number = res->Channel;
1491             chan_list[*num_channels].scan_type   = MLAN_SCAN_TYPE_ACTIVE;
1492             chan_list[*num_channels].scan_time   = 150;
1493             (*num_channels)++;
1494         }
1495         if ((res->ssid_len == 0) ||
1496             ((strncmp((const char *)network->ssid, (const char *)res->ssid,
1497                      (size_t)MAX(strlen(network->ssid), (unsigned int)res->ssid_len)) != 0)
1498 #if CONFIG_DRIVER_OWE
1499             && ((res->trans_mode == OWE_TRANS_MODE_OWE) &&
1500                 (strncmp((const char *)network->trans_ssid, (const char *)res->ssid,
1501                          (size_t)MAX(strlen(network->trans_ssid), (unsigned int)res->ssid_len))) != 0)
1502 #endif
1503 #if CONFIG_WLAN_BRIDGE
1504             && (strncmp((const char *)network->bridge_ssid, (const char *)res->ssid,
1505                         (size_t)MAX(strlen(network->bridge_ssid), (unsigned int)res->ssid_len)) != 0)
1506 #endif
1507             ))
1508         {
1509             wlcm_d("ssid mismatch: Got: %s Expected: %s", (char *)res->ssid, network->ssid);
1510             return -WM_FAIL;
1511         }
1512     }
1513 
1514     /* In case of CONFIG_WD_EXTERAL we don't parse and
1515        store the security profile.
1516        Instead the respective IE are stored in network struct as is */
1517     /* Check security information */
1518     if (network->security_specific != 0U)
1519     {
1520         if (!security_profile_matches(network, res))
1521         {
1522             wlcm_d("%s: security profile mismatch", network->ssid);
1523             return -WM_FAIL;
1524         }
1525     }
1526 
1527     if (!(res->WPA_WPA2_WEP.wepStatic || res->WPA_WPA2_WEP.wpa3_sae || res->WPA_WPA2_WEP.wpa2 || res->WPA_WPA2_WEP.wpa || res->WPA_WPA2_WEP.wpa2_sha256) && (network->security.psk_len || network->security.password_len))
1528     {
1529         wlcm_d("%s: security profile mismatch", network->ssid);
1530         return -WM_FAIL;
1531     }
1532 
1533     if (!wifi_11d_is_channel_allowed((int)res->Channel))
1534     {
1535         wlcm_d("%d: Channel not allowed.", res->Channel);
1536         return -WM_FAIL;
1537     }
1538 
1539     if ((res->ap_pwe != network->security.pwe_derivation) && ((res->ap_pwe | network->security.pwe_derivation) == 1))
1540     {
1541         wlcm_d("%d: H2E configuration mismatch", res->ap_pwe);
1542         return -WM_FAIL;
1543     }
1544 #if CONFIG_DRIVER_MBO
1545     if (res->mbo_assoc_disallowed)
1546     {
1547         wlcm_d("%s: MBO Association disallowed.", network->ssid);
1548         return -WM_FAIL;
1549     }
1550 #endif
1551 
1552 #if CONFIG_DRIVER_OWE
1553     wlcm_d("%s: Match successful", res->trans_mode == OWE_TRANS_MODE_OWE ? res->ssid : res->trans_ssid);
1554 #endif
1555     /* If the bss blacklist is not empty, check whether the network is in the blacklist or not. */
1556     /* If yes, skip this network. */
1557     if(mlan_adap->blacklist_bss.num_bssid)
1558     {
1559         for(idx = 0; idx < mlan_adap->blacklist_bss.num_bssid; idx++)
1560         {
1561             if(!memcmp(res->bssid, mlan_adap->blacklist_bss.bssids[idx], MLAN_MAC_ADDR_LENGTH))
1562             {
1563                 wlcm_d("%02X:%02X:%02X:%02X:%02X:%02X : BSSID is not allowed.", res->bssid[0], res->bssid[1], res->bssid[2],
1564                        res->bssid[3], res->bssid[4], res->bssid[5]);
1565                 return WM_SUCCESS;
1566             }
1567         }
1568     }
1569     return WM_SUCCESS;
1570 }
1571 
1572 #if CONFIG_WPA2_ENTP
wpa2_tls_init(struct wlan_network * network)1573 static int wpa2_tls_init(struct wlan_network *network)
1574 {
1575     network->security.wlan_ctx =
1576         wm_mbedtls_ssl_config_new(&network->security.tls_cert, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_VERIFY_NONE);
1577 
1578     if (!network->security.wlan_ctx)
1579     {
1580         wlcm_e("TLS session init failed (ctx failed)");
1581         return -WM_FAIL;
1582     }
1583 
1584     network->security.wlan_ssl = wm_mbedtls_ssl_new(network->security.wlan_ctx, 0, NULL);
1585 
1586     if (!network->security.wlan_ssl)
1587     {
1588         wlcm_e("TLS session init failed (ssl failed)");
1589         wm_mbedtls_ssl_config_free(network->security.wlan_ctx);
1590         network->security.wlan_ctx = NULL;
1591         return -WM_FAIL;
1592     }
1593 
1594     /* Disable SSL application data splitting as FreeRADIUS fails to
1595      * process splitted data
1596      */
1597     mbedtls_ssl_conf_cbc_record_splitting(network->security.wlan_ctx, MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED);
1598 
1599     return WM_SUCCESS;
1600 }
1601 
wpa2_tls_cleanup(struct wlan_network * network,bool force)1602 static void wpa2_tls_cleanup(struct wlan_network *network, bool force)
1603 {
1604     if (force)
1605     {
1606         if (network->security.wlan_ssl)
1607         {
1608             wm_mbedtls_ssl_free(network->security.wlan_ssl);
1609             network->security.wlan_ssl = NULL;
1610             if (network->security.wlan_ctx)
1611             {
1612                 wm_mbedtls_ssl_config_free(network->security.wlan_ctx);
1613                 network->security.wlan_ctx = NULL;
1614             }
1615         }
1616     }
1617 
1618     wpa2_shutdown();
1619     wlan_set_prov_session(PROV_NON_SESSION_ATTEMPT);
1620 }
1621 #endif
1622 
1623 static void wlcm_request_reconnect(enum cm_sta_state *next, struct wlan_network *network);
1624 int load_wep_key(const uint8_t *input, uint8_t *output, uint8_t *output_len, const unsigned max_output_len);
1625 
1626 /* Configure the firmware and PSK Supplicant for the security settings
1627  * specified in 'network'.  For WPA and WPA2 networks, we must chose between
1628  * the older TKIP cipher or the newer CCMP cipher.  We prefer CCMP, however we
1629  * will chose TKIP if the AP doesn't report CCMP support.  CCMP is optional for
1630  * WPA and required for WPA2, however a WPA2 AP may still have it disabled. */
configure_security(struct wlan_network * network,struct wifi_scan_result2 * res)1631 static int configure_security(struct wlan_network *network, struct wifi_scan_result2 *res)
1632 {
1633     int ret = WM_SUCCESS;
1634     switch (network->security.type)
1635     {
1636 #if CONFIG_WPA2_ENTP
1637         case WLAN_SECURITY_EAP_TLS:
1638         case WLAN_SECURITY_EAP_PEAP_MSCHAPV2:
1639             if (network->security.pmk_valid == false)
1640             {
1641                 wlan_set_prov_session(PROV_ENTP_SESSION_ATTEMPT);
1642                 ret = wpa2_tls_init(network);
1643                 if (ret != WM_SUCCESS)
1644                     return -WM_FAIL;
1645             }
1646 
1647             ret                 = wifi_send_enable_supplicant(network->role, network->ssid);
1648             if (ret != WM_SUCCESS)
1649                 return -WM_FAIL;
1650 
1651             break;
1652 #endif
1653         case WLAN_SECURITY_WPA:
1654         case WLAN_SECURITY_WPA2:
1655         case WLAN_SECURITY_WPA_WPA2_MIXED:
1656 #if CONFIG_WPS2
1657             wlan_set_prov_session(PROV_NON_SESSION_ATTEMPT);
1658 #endif
1659             if (network->security.type == WLAN_SECURITY_WPA)
1660             {
1661                 wlcm_d("configuring WPA security");
1662             }
1663             else if (network->security.type == WLAN_SECURITY_WPA2)
1664             {
1665                 wlcm_d("configuring WPA2 security");
1666             }
1667             else
1668             {
1669                 wlcm_d("configuring WPA/WPA2 Mixed security");
1670             }
1671 
1672             /* the handler for the "add PSK" command calls supplicantEnable() so we
1673              * don't do it here
1674              *
1675              * TODO: at this time, the firmware will not generate a specific
1676              * response to the command sent by wifi_add_wpa_psk().  The
1677              * command+response sequence should be worked into the WLAN Connection
1678              * Manager once this is fixed.
1679              *
1680              * TODO: at this time, the PSK Supplicant caches passphrases on the
1681              * SSID however this won't work for networks with a hidden SSID.  The
1682              * WLAN Connection manager will need to be updated once that's
1683              * resolved. */
1684             if (network->security.pmk_valid)
1685             {
1686                 wlcm_d("adding SSID and PMK to supplicant cache");
1687 
1688                 /* Do not pass BSSID here as embedded supplicant fails
1689                  * to derive session keys
1690                  */
1691                 ret = wifi_send_add_wpa_pmk((int)network->role, network->ssid, NULL, network->security.pmk,
1692                                             WLAN_PMK_LENGTH);
1693             }
1694             else
1695             {
1696 #if CONFIG_11R
1697                 if (!wlan.ft_bss)
1698 #endif
1699                 {
1700                     wlcm_d("adding SSID and PSK to supplicant cache");
1701                     ret = wifi_send_add_wpa_psk((int)network->role, network->ssid, network->security.psk,
1702                                                 (unsigned int)network->security.psk_len);
1703                 }
1704             }
1705 
1706             if (ret != WM_SUCCESS)
1707             {
1708                 return -WM_FAIL;
1709             }
1710 #if CONFIG_WLAN_BRIDGE
1711             if (network->bridge_ssid)
1712             {
1713                 if (network->security.bridge_pmk_valid)
1714                 {
1715                     ret = wifi_send_add_wpa_pmk(network->role, network->bridge_ssid, NULL, network->security.bridge_pmk,
1716                                                 WLAN_PMK_LENGTH);
1717                 }
1718                 else
1719                 {
1720                     ret = wifi_send_add_wpa_psk(network->role, network->bridge_ssid, network->security.bridge_psk,
1721                                                 network->security.bridge_psk_len);
1722                 }
1723 
1724                 if (ret != WM_SUCCESS)
1725                     return -WM_FAIL;
1726             }
1727 #endif
1728             break;
1729 #if CONFIG_DRIVER_OWE
1730         case WLAN_SECURITY_OWE_ONLY:
1731             if (res->WPA_WPA2_WEP.owe != 0U)
1732             {
1733                 /** This is dummy command to enable the embedded supplicant in Wi-Fi fimrware, OWE never uses any password */
1734                 wlcm_d("configuring OWE security");
1735                 ret = wifi_send_add_wpa3_password((int)network->role, network->ssid, "12345678",
1736                                                   8U);
1737             }
1738             else
1739             { /* Do Nothing */
1740             }
1741             if (ret != WM_SUCCESS)
1742             {
1743                 return -WM_FAIL;
1744             }
1745             break;
1746 #endif
1747         case WLAN_SECURITY_WPA3_SAE:
1748         case WLAN_SECURITY_WPA2_WPA3_SAE_MIXED:
1749             if (res->WPA_WPA2_WEP.wpa3_sae != 0U)
1750             {
1751                 wlcm_d("configuring WPA3 SAE security");
1752                 wlcm_d("adding SSID and WPA3 SAE PASSWORD to supplicant cache");
1753                 ret = wifi_send_add_wpa3_password((int)network->role, network->ssid, network->security.password,
1754                                                   network->security.password_len);
1755             }
1756             else if (res->WPA_WPA2_WEP.wpa2 != 0U || res->WPA_WPA2_WEP.wpa2_sha256 != 0U)
1757             {
1758                 wlcm_d("configuring WPA2 security");
1759                 wlcm_d("adding SSID and PSK to supplicant cache");
1760                 ret = wifi_send_add_wpa_psk((int)network->role, network->ssid, network->security.password,
1761                                             network->security.password_len);
1762             }
1763             else
1764             { /* Do Nothing */
1765             }
1766             if (ret != WM_SUCCESS)
1767             {
1768                 return -WM_FAIL;
1769             }
1770             break;
1771         case WLAN_SECURITY_WEP_OPEN:
1772         case WLAN_SECURITY_WEP_SHARED:
1773             ret = load_wep_key((const uint8_t *)network->security.psk, (uint8_t *)network->security.psk,
1774                                (uint8_t *)&network->security.psk_len, sizeof(network->security.psk));
1775             if (ret != WM_SUCCESS)
1776             {
1777                 return -WM_E_INVAL;
1778             }
1779 
1780             ret = wifi_set_key(BSS_TYPE_STA, true, 0U, (const uint8_t *)network->security.psk,
1781                                (unsigned)network->security.psk_len, NULL, 0, (const uint8_t *)network->bssid, 0);
1782 
1783             if (ret != WM_SUCCESS)
1784             {
1785                 return -WM_FAIL;
1786             }
1787             break;
1788 
1789         case WLAN_SECURITY_NONE:
1790 #if CONFIG_WPS2
1791             if (network->wps_specific && res->wps_session != WPS_SESSION_INACTIVE)
1792                 wlan_set_prov_session(PROV_WPS_SESSION_ATTEMPT);
1793 #endif
1794             break;
1795         default:
1796             wlcm_d("Unexpected wlan security");
1797             break;
1798     }
1799 
1800     return WM_SUCCESS;
1801 }
1802 
is_running(void)1803 static bool is_running(void)
1804 {
1805     return (wlan.running && wlan.sta_state >= CM_STA_IDLE);
1806 }
1807 
is_sta_connecting(void)1808 static bool is_sta_connecting(void)
1809 {
1810 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
1811     int state = wifi_nxp_supp_state();
1812 
1813     return ((state >= WPA_SCANNING) && (state <= WPA_COMPLETED));
1814 #else
1815     return ((wlan.sta_state > CM_STA_ASSOCIATING) && (wlan.sta_state <= CM_STA_CONNECTED));
1816 #endif
1817 }
1818 
is_sta_idle(void)1819 static bool is_sta_idle(void)
1820 {
1821 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
1822     int state = wifi_nxp_supp_state();
1823 
1824     return (state <= WPA_INACTIVE);
1825 #else
1826     return (wlan.sta_state == CM_STA_IDLE);
1827 #endif
1828 }
1829 
1830 /* Check whether we are allowed to start a user-requested scan right now. */
is_scanning_allowed(void)1831 static bool is_scanning_allowed(void)
1832 {
1833 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
1834     return (is_state(CM_STA_IDLE) || is_state(CM_STA_CONNECTED) || is_state(CM_STA_AUTHENTICATED));
1835 #else
1836     return true;
1837 #endif
1838 }
1839 
is_sta_associated(void)1840 static bool is_sta_associated(void)
1841 {
1842 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
1843     int state = wifi_nxp_supp_state();
1844 
1845     return ((state >= WPA_ASSOCIATED) && (state <= WPA_COMPLETED));
1846 #else
1847     return ((wlan.sta_state >= CM_STA_ASSOCIATED) && (wlan.sta_state <= CM_STA_CONNECTED));
1848 #endif
1849 }
1850 
1851 /*
1852  * Connection Manager actions
1853  */
do_scan(struct wlan_network * network)1854 static void do_scan(struct wlan_network *network)
1855 {
1856     int ret;
1857     uint8_t *bssid = NULL;
1858     char *ssid     = NULL;
1859 #if CONFIG_WLAN_BRIDGE
1860     char *bridge_ssid = NULL;
1861 #endif
1862     unsigned int channel = 0;
1863     IEEEtypes_Bss_t type;
1864     wlan_scan_channel_list_t chan_list[1];
1865     (void)memset((uint8_t *)chan_list, 0x00, sizeof(wlan_scan_channel_list_t) * 1);
1866 
1867     wlcm_d("initiating scan for network \"%s\"", network->name);
1868 
1869     if (network->bssid_specific != 0U)
1870     {
1871         bssid = (uint8_t *)network->bssid;
1872     }
1873     if (network->ssid_specific != 0U)
1874     {
1875         ssid = network->ssid;
1876 #if CONFIG_WLAN_BRIDGE
1877         bridge_ssid = network->bridge_ssid;
1878 #endif
1879     }
1880 #if CONFIG_DRIVER_OWE
1881     if (network->owe_trans_mode == OWE_TRANS_MODE_OPEN)
1882     {
1883         ssid    = network->trans_ssid;
1884         channel = network->channel;
1885     }
1886 #endif
1887     if (network->channel_specific != 0U)
1888     {
1889         channel = network->channel;
1890     }
1891 
1892     switch (network->role)
1893     {
1894         case WLAN_BSS_ROLE_STA:
1895             type = BSS_INFRASTRUCTURE;
1896             break;
1897         default:
1898             type = BSS_INFRASTRUCTURE;
1899             break;
1900     }
1901 
1902     wlan.sta_state = CM_STA_SCANNING;
1903 
1904     /* comment out this, need to check if 11d needs 3 times full channel scan */
1905     /*
1906     if (wrapper_wlan_11d_support_is_enabled() && wlan.scan_count < WLAN_11D_SCAN_LIMIT)
1907     {
1908         ret = wifi_send_scan_cmd((t_u8)g_wifi_scan_params.bss_type, g_wifi_scan_params.bssid, g_wifi_scan_params.ssid,
1909                                  NULL, 0, NULL, 0,
1910 #if CONFIG_SCAN_WITH_RSSIFILTER
1911                                  0,
1912 #endif
1913 #if CONFIG_SCAN_CHANNEL_GAP
1914                                  scan_channel_gap,
1915 #endif
1916                                  false, false);
1917     }
1918     else
1919     */
1920     {
1921         if (channel != 0)
1922         {
1923             chan_list[0].chan_number = (t_u8)channel;
1924             chan_list[0].scan_type   = MLAN_SCAN_TYPE_ACTIVE;
1925             chan_list[0].scan_time   = 120;
1926 #if CONFIG_WLAN_BRIDGE
1927             ret = wifi_send_scan_cmd((t_u8)type, bssid, ssid, bridge_ssid, 1, chan_list, 0,
1928 #if CONFIG_SCAN_CHANNEL_GAP
1929                                      scan_channel_gap,
1930 #endif
1931                                      false, false);
1932 #else
1933 #if CONFIG_SCAN_WITH_RSSIFILTER
1934             ret = wifi_send_scan_cmd((t_u8)type, bssid, ssid, 1, 1, chan_list, 0, 0,
1935 #if CONFIG_SCAN_CHANNEL_GAP
1936                                      scan_channel_gap,
1937 #endif
1938                                      false, false);
1939 #else
1940             ret = wifi_send_scan_cmd((t_u8)type, bssid, ssid, 1, 1, chan_list, 0,
1941 #if CONFIG_SCAN_CHANNEL_GAP
1942                                      scan_channel_gap,
1943 #endif
1944                                      false, false);
1945 #endif
1946 #endif
1947         }
1948         else
1949         {
1950 #if CONFIG_SCAN_WITH_RSSIFILTER
1951             ret = wifi_send_scan_cmd((t_u8)type, bssid, ssid, 1, 0, NULL, 0, 0,
1952 #if CONFIG_SCAN_CHANNEL_GAP
1953                                      scan_channel_gap,
1954 #endif
1955                                      false, false);
1956 #else
1957             ret = wifi_send_scan_cmd((t_u8)type, bssid, ssid, 1, 0, NULL, 0,
1958 #if CONFIG_SCAN_CHANNEL_GAP
1959                                      scan_channel_gap,
1960 #endif
1961                                      false, false);
1962 #endif
1963         }
1964     }
1965     if (ret != 0)
1966     {
1967         (void)wlan_wlcmgr_send_msg(WIFI_EVENT_SCAN_RESULT, WIFI_EVENT_REASON_FAILURE, NULL);
1968         wlcm_e("error: scan failed");
1969     }
1970     else
1971     {
1972         wlan.scan_count++;
1973     }
1974 }
1975 
do_hidden_scan(struct wlan_network * network,uint8_t num_channels,wlan_scan_channel_list_t * chan_list)1976 static void do_hidden_scan(struct wlan_network *network, uint8_t num_channels, wlan_scan_channel_list_t *chan_list)
1977 {
1978     int ret;
1979     uint8_t *bssid = NULL;
1980     char *ssid     = NULL;
1981     IEEEtypes_Bss_t type;
1982 
1983     wlcm_d("initiating scan for hidden network \"%s\"", network->name);
1984 
1985     if (network->bssid_specific != 0U)
1986     {
1987         bssid = (uint8_t *)network->bssid;
1988     }
1989     if (network->ssid_specific != 0U)
1990     {
1991         ssid = network->ssid;
1992     }
1993 
1994     switch (network->role)
1995     {
1996         case WLAN_BSS_ROLE_STA:
1997             type = BSS_INFRASTRUCTURE;
1998             break;
1999         default:
2000             type = BSS_INFRASTRUCTURE;
2001             break;
2002     }
2003 
2004     wlan.sta_state = CM_STA_SCANNING;
2005 
2006 #if CONFIG_SCAN_WITH_RSSIFILTER
2007     ret = wifi_send_scan_cmd((t_u8)type, bssid, ssid, 1, num_channels, chan_list, 0, 0,
2008 #if CONFIG_SCAN_CHANNEL_GAP
2009                              scan_channel_gap,
2010 #endif
2011                              false, true);
2012 #else
2013     ret = wifi_send_scan_cmd((t_u8)type, bssid, ssid, 1, num_channels, chan_list, 0,
2014 #if CONFIG_SCAN_CHANNEL_GAP
2015                              scan_channel_gap,
2016 #endif
2017                              false, true);
2018 #endif
2019     if (ret != 0)
2020     {
2021         (void)wlan_wlcmgr_send_msg(WIFI_EVENT_SCAN_RESULT, WIFI_EVENT_REASON_FAILURE, NULL);
2022         wlcm_e("error: scan failed");
2023     }
2024 }
2025 
2026 static void do_connect_failed(enum wlan_event_reason reason);
2027 
2028 #if !CONFIG_WPA_SUPP
2029 /* Start a connection attempt.  To do this we choose a specific network to scan
2030  * for or the first of our list of known networks. If that network uses WEP
2031  * security, we first issue the WEP configuration command and enter the
2032  * CM_STA_CONFIGURING state, we'll get a response to this command which will
2033  * transition us to the CM_STA_SCANNING state.  Otherwise we issue a scan and
2034  * enter the CM_STA_SCANNING state now. */
do_connect(int netindex)2035 static int do_connect(int netindex)
2036 {
2037 #if CONFIG_WLAN_FAST_PATH
2038     int ret;
2039 #endif /* CONFIG_WLAN_FAST_PATH */
2040 
2041 #if CONFIG_WMSTATS
2042     g_wm_stats.wm_conn_att++;
2043 #endif /* CONFIG_WMSTATS */
2044 
2045     /* try the specified network */
2046     if (wlan.networks[netindex].role != WLAN_BSS_ROLE_STA)
2047     {
2048         return -WM_E_INVAL;
2049     }
2050 
2051 #if CONFIG_DRIVER_OWE
2052     wlan.networks[netindex].owe_trans_mode = 0;
2053 #endif
2054     wlan.cur_network_idx = netindex;
2055     wlan.scan_count      = 0;
2056 
2057 #if CONFIG_WLAN_FAST_PATH
2058     if (((wlan.auth_cache_valid == true) || (wlan.fast_path_cache_valid == true)) &&
2059         (wlan.cur_network_idx == wlan.fp_network))
2060     {
2061         wlcm_d("Fast Path: Taking fast path");
2062 
2063         /* Fast path: Jump directly to associating state */
2064         wlan.sta_state = CM_STA_ASSOCIATING;
2065         ret            = wrapper_wifi_assoc(wlan.fast_path_bss, wlan.networks[wlan.cur_network_idx].security.type,
2066                                  wlan.networks[wlan.cur_network_idx].security.ucstCipher.tkip, 0, false);
2067         if (ret == WM_SUCCESS)
2068             return WM_SUCCESS;
2069 
2070         wlcm_d("Fast path assoc failed. Will try normal assoc");
2071         wlan.auth_cache_valid      = false;
2072         wlan.fast_path_cache_valid = false;
2073     }
2074 
2075 #endif /* CONFIG_WLAN_FAST_PATH */
2076     do_scan(&wlan.networks[netindex]);
2077 
2078     return WM_SUCCESS;
2079 }
2080 #endif
2081 
2082 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT && UAP_SUPPORT
do_start(struct wlan_network * network)2083 static int do_start(struct wlan_network *network)
2084 {
2085     int ret = 0;
2086     uint8_t active_chan_list[40];
2087     uint8_t active_num_chans = 0;
2088     wifi_scan_chan_list_t scan_chan_list;
2089 #if CONFIG_WPA_SUPP
2090     struct netif *netif = net_get_uap_interface();
2091 #else
2092     uint8_t i;
2093 #endif
2094 
2095     if (network->role == WLAN_BSS_ROLE_UAP)
2096     {
2097         if (network->channel_specific == 0U)
2098         {
2099             network->channel = UAP_DEFAULT_CHANNEL;
2100 
2101             if (wlan.running && is_sta_associated())
2102             {
2103                 network->channel = wlan.networks[wlan.cur_network_idx].channel;
2104 #if CONFIG_WPA_SUPP
2105             network->sec_channel_offset = wifi_get_sec_channel_offset(network->channel);
2106             if (network->channel == 14)
2107             {
2108                 wpa_supp_set_ap_bw(netif, 1);
2109             }
2110 #endif
2111             }
2112             else
2113             {
2114                 if (!wlan_uap_scan_chan_list_set)
2115                 {
2116                     wifi_get_active_channel_list(active_chan_list, &active_num_chans,
2117                                                  wlan.networks[wlan.cur_uap_network_idx].acs_band);
2118 
2119                     if (active_num_chans != 0U)
2120                     {
2121                         scan_chan_list.num_of_chan = active_num_chans;
2122                         (void)memcpy((void *)scan_chan_list.chan_number, (const void *)active_chan_list,
2123                                      active_num_chans);
2124                         wlan_uap_set_scan_chan_list(scan_chan_list);
2125                     }
2126                 }
2127             }
2128         }
2129         else
2130         {
2131 #if CONFIG_11AC
2132             t_u8 bandwidth = wifi_uap_get_bandwidth();
2133             if (bandwidth == BANDWIDTH_80MHZ)
2134             {
2135                 if ((wlan.networks[wlan.cur_uap_network_idx].acs_band == 0)
2136 #if CONFIG_WIFI_CAPA
2137                         || ((wlan.networks[wlan.cur_uap_network_idx].acs_band == 1) && (!(network->wlan_capa & (WIFI_SUPPORT_11AX | WIFI_SUPPORT_11AC))))
2138 #endif
2139                    )
2140                 {
2141                     wlcm_e("uAP configured bandwidth not allowed");
2142                     CONNECTION_EVENT(WLAN_REASON_UAP_START_FAILED, NULL);
2143                     wlan.cur_uap_network_idx = -1;
2144                     return -WM_FAIL;
2145                 }
2146             }
2147 #endif
2148 #if CONFIG_WPA_SUPP
2149             network->sec_channel_offset = wifi_get_sec_channel_offset(network->channel);
2150             if (network->channel == 14)
2151             {
2152                 wpa_supp_set_ap_bw(netif, 1);
2153             }
2154 #else
2155             wifi_get_active_channel_list(active_chan_list, &active_num_chans,
2156                                                  wlan.networks[wlan.cur_uap_network_idx].acs_band);
2157 
2158             for (i = 0; i < active_num_chans; i++)
2159             {
2160                 if (network->channel == active_chan_list[i])
2161                 {
2162                     break;
2163                 }
2164             }
2165             if (i == active_num_chans)
2166             {
2167                 wlcm_e("uAP configured channel not allowed");
2168                 CONNECTION_EVENT(WLAN_REASON_UAP_START_FAILED, NULL);
2169                 wlan.cur_uap_network_idx = -1;
2170                 return -WM_FAIL;
2171             }
2172 #endif
2173         }
2174 
2175         wlcm_d("starting our own network");
2176 
2177 #if CONFIG_WIFI_CAPA
2178         if (network->channel == 14)
2179         {
2180             wifi_uap_config_wifi_capa(WIFI_SUPPORT_LEGACY);
2181         }
2182         else
2183         {
2184             wifi_uap_config_wifi_capa(network->wlan_capa);
2185         }
2186 #endif
2187 
2188 #if CONFIG_WPA_SUPP
2189         if (network->bssid_specific == 0U)
2190         {
2191             (void)memcpy(&network->bssid[0], &wlan.uap_mac[0], MLAN_MAC_ADDR_LENGTH);
2192         }
2193 
2194         t_u8 bandwidth = wifi_uap_get_bandwidth();
2195 
2196 #if defined(SD8801) || defined(RW610)
2197         wpa_supp_set_ap_bw(netif, 1);
2198 #else
2199         wpa_supp_set_ap_bw(netif, bandwidth);
2200 #endif
2201         ret = wpa_supp_start_ap(netif, network, 0);
2202 #else
2203         if (network->channel == 14)
2204         {
2205             wifi_uap_set_bandwidth(BANDWIDTH_20MHZ);
2206         }
2207 
2208         ret = wifi_uap_start((mlan_bss_type)network->type, network->ssid,
2209 #if CONFIG_P2P
2210                              wlan.wfd_mac,
2211 #else
2212                              wlan.uap_mac,
2213 #endif
2214                              (int)network->security.type, network->security.key_mgmt, &network->security.psk[0], &network->security.password[0],
2215                              (int)network->channel, wlan.scan_chan_list,
2216                              network->security.pwe_derivation,
2217                              network->security.transition_disable,
2218                              network->security.mfpc,
2219 #if CONFIG_WIFI_DTIM_PERIOD
2220                              network->security.mfpr, network->dtim_period
2221 #else
2222                              network->security.mfpr
2223 #endif
2224         );
2225 #endif
2226         if (ret != WM_SUCCESS)
2227         {
2228             wlcm_e("uAP start failed, giving up");
2229             CONNECTION_EVENT(WLAN_REASON_UAP_START_FAILED, NULL);
2230             wlan.cur_uap_network_idx = -1;
2231             return -WM_FAIL;
2232         }
2233 
2234 #if CONFIG_WPA_SUPP
2235         if (ret == WM_SUCCESS)
2236         {
2237             wlan.connect        = false;
2238             wlan.status_timeout = 0;
2239             (void)OSA_TimerActivate((osa_timer_handle_t)wlan.supp_status_timer);
2240         }
2241 #endif
2242 
2243         wlan.uap_state = CM_UAP_CONFIGURED;
2244     }
2245     return WM_SUCCESS;
2246 }
2247 
do_stop(struct wlan_network * network)2248 static int do_stop(struct wlan_network *network)
2249 {
2250     int ret = WM_SUCCESS;
2251 #if CONFIG_WPA_SUPP
2252     struct netif *netif = net_get_uap_interface();
2253 #endif
2254 
2255     wlcm_d("stopping our own network");
2256 
2257     if (network->role == WLAN_BSS_ROLE_UAP)
2258     {
2259 #if CONFIG_WPA_SUPP
2260         ret = wpa_supp_stop_ap(netif, network);
2261 #else
2262         ret = wifi_uap_stop();
2263 #endif
2264         if (ret != 0)
2265         {
2266             wlcm_e("uAP stop failed, giving up");
2267             CONNECTION_EVENT(WLAN_REASON_UAP_STOP_FAILED, NULL);
2268             return -WM_FAIL;
2269         }
2270         wlan.uap_state           = CM_UAP_INITIALIZING;
2271         wlan_uap_set_bandwidth(UAP_DEFAULT_BANDWIDTH);
2272         wlan_uap_set_beacon_period(UAP_DEFAULT_BEACON_PERIOD);
2273         wlan_uap_set_hidden_ssid(UAP_DEFAULT_HIDDEN_SSID);
2274         wlan.cur_uap_network_idx = -1;
2275     }
2276 
2277     return WM_SUCCESS;
2278 }
2279 #endif /* !CONFIG_WIFI_NM_WPA_SUPPLICANT && UAP_SUPPORT */
2280 
2281 /* A connection attempt has failed for 'reason', decide whether to try to
2282  * connect to another network (in that case, tell the state machine to
2283  * transition to CM_STA_CONFIGURING to try that network) or finish attempting to
2284  * connect by releasing the scan lock and informing the user. */
do_connect_failed(enum wlan_event_reason reason)2285 static void do_connect_failed(enum wlan_event_reason reason)
2286 {
2287 #if (CONFIG_DRIVER_OWE) || (CONFIG_WPA2_ENTP)
2288     struct wlan_network *network = &wlan.networks[wlan.cur_network_idx];
2289 #endif
2290 #if CONFIG_WMSTATS
2291     g_wm_stats.wm_conn_fail++;
2292     switch (reason)
2293     {
2294         case WLAN_REASON_NETWORK_AUTH_FAILED:
2295             g_wm_stats.wm_auth_fail++;
2296             break;
2297         case WLAN_REASON_NETWORK_NOT_FOUND:
2298             g_wm_stats.wm_nwnt_found++;
2299             break;
2300         default:
2301             wlcm_d("Unexpected connect fail reason");
2302             break;
2303     }
2304 #endif /* CONFIG_WMSTATS */
2305 
2306 #if CONFIG_WPA2_ENTP
2307     if (wlan_get_prov_session() == PROV_ENTP_SESSION_ATTEMPT)
2308     {
2309         wpa2_tls_cleanup(network, false);
2310     }
2311 #endif
2312     if (wlan.connect_wakelock_taken)
2313     {
2314 #if CONFIG_HOST_SLEEP
2315         wakelock_put();
2316 #endif
2317         wlan.connect_wakelock_taken = false;
2318     }
2319 
2320 #if CONFIG_DRIVER_OWE
2321     if (network->trans_ssid_specific && network->trans_ssid_len)
2322     {
2323         (void)memset(&network->trans_ssid, 0x00, sizeof(network->trans_ssid));
2324         network->trans_ssid_len = 0;
2325     }
2326 #endif
2327 
2328     wlcm_d("connecting to \"%s\" failed", wlan.networks[wlan.cur_network_idx].name);
2329 
2330     if (wlan.sta_state == CM_STA_SCANNING_USER)
2331     {
2332         wlan.sta_return_to = CM_STA_IDLE;
2333     }
2334 
2335     wlan.sta_state = CM_STA_IDLE;
2336     CONNECTION_EVENT(reason, NULL);
2337 }
2338 
report_scan_results(void)2339 static void report_scan_results(void)
2340 {
2341     unsigned int count;
2342     if (wlan.scan_cb != NULL)
2343     {
2344         if (wifi_get_scan_result_count(&count) != WM_SUCCESS)
2345         {
2346             count = 0;
2347         }
2348         (void)wlan.scan_cb(count);
2349         wlan.scan_cb = NULL;
2350     }
2351 }
2352 
update_network_params(struct wlan_network * network,const struct wifi_scan_result2 * res)2353 static void update_network_params(struct wlan_network *network, const struct wifi_scan_result2 *res)
2354 {
2355     if (!network->security_specific)
2356     {
2357         /* Wildcard: If wildcard security is specified, copy the highest
2358          * security available in the scan result to the configuration
2359          * structure
2360          */
2361         enum wlan_security_type t = WLAN_SECURITY_NONE;
2362         int key_mgmt = WLAN_KEY_MGMT_NONE;
2363 
2364         if ((res->WPA_WPA2_WEP.wpa3_sae != 0U) && (res->WPA_WPA2_WEP.wpa2 != 0U))
2365         {
2366             t = WLAN_SECURITY_WPA2_WPA3_SAE_MIXED;
2367             key_mgmt = WLAN_KEY_MGMT_PSK | WLAN_KEY_MGMT_SAE;
2368         }
2369         else if (res->WPA_WPA2_WEP.wpa3_sae != 0U)
2370         {
2371             t = WLAN_SECURITY_WPA3_SAE;
2372             key_mgmt = WLAN_KEY_MGMT_SAE;
2373         }
2374         else if (res->WPA_WPA2_WEP.wpa2 != 0U)
2375         {
2376             t = WLAN_SECURITY_WPA2;
2377             key_mgmt = WLAN_KEY_MGMT_PSK;
2378         }
2379         else if (res->WPA_WPA2_WEP.wpa != 0U)
2380         {
2381             t = WLAN_SECURITY_WPA_WPA2_MIXED;
2382             key_mgmt = WLAN_KEY_MGMT_PSK;
2383         }
2384         else if (res->WPA_WPA2_WEP.wepStatic != 0U)
2385         {
2386             t = WLAN_SECURITY_WEP_OPEN;
2387         }
2388 #if CONFIG_DRIVER_OWE
2389         else if (res->WPA_WPA2_WEP.wpa2 && res->WPA_WPA2_WEP.owe)
2390         {
2391             t = WLAN_SECURITY_OWE_ONLY;
2392             key_mgmt = WLAN_KEY_MGMT_OWE;
2393         }
2394 #endif
2395         else
2396         {
2397             t = WLAN_SECURITY_NONE;
2398             key_mgmt = WLAN_KEY_MGMT_NONE;
2399         }
2400 
2401         network->security.type = t;
2402         network->security.key_mgmt = key_mgmt;
2403 
2404         if ((network->security.type == WLAN_SECURITY_WPA2) || (network->security.type == WLAN_SECURITY_WPA3_SAE) || (network->security.type == WLAN_SECURITY_WPA2_WPA3_SAE_MIXED))
2405         {
2406             network->security.mfpr = 0;
2407             (void)wlan_set_pmfcfg((t_u8)network->security.mfpc, (t_u8)network->security.mfpr);
2408         }
2409     }
2410 
2411     network->dot11n = res->phtcap_ie_present;
2412 
2413 #if CONFIG_11AC
2414     network->dot11ac = res->pvhtcap_ie_present;
2415 #endif
2416 #if CONFIG_11AX
2417     network->dot11ax = res->phecap_ie_present;
2418 #endif
2419 
2420 #if CONFIG_11R
2421 
2422     network->mdid = res->mdid;
2423 
2424     if (res->WPA_WPA2_WEP.ft_1x != 0U)
2425     {
2426         network->ft_1x = 1;
2427     }
2428     else if (res->WPA_WPA2_WEP.ft_psk != 0U)
2429     {
2430         network->ft_psk = 1;
2431     }
2432     else if (res->WPA_WPA2_WEP.ft_sae != 0U)
2433     {
2434         network->ft_sae = 1;
2435     }
2436     else
2437     {
2438         /* Do nothing */
2439     }
2440 #endif
2441 
2442     /* We have a match based on the criteria we checked, update the known
2443      * network with any additional information that we got from the scan but
2444      * did not know before */
2445     if (!network->channel_specific)
2446     {
2447         network->channel = res->Channel;
2448     }
2449     if (!network->bssid_specific)
2450     {
2451         (void)memcpy((void *)network->bssid, (const void *)res->bssid, MLAN_MAC_ADDR_LENGTH);
2452     }
2453     if (!network->ssid_specific)
2454     {
2455         (void)memcpy((void *)network->ssid, (const void *)res->ssid, (size_t)res->ssid_len);
2456     }
2457 
2458     network->beacon_period = res->beacon_period;
2459     network->dtim_period   = res->dtim_period;
2460 #if CONFIG_DRIVER_OWE
2461     network->owe_trans_mode = res->trans_mode;
2462 
2463     if (res->trans_mode == OWE_TRANS_MODE_OPEN)
2464     {
2465         (void)memset(&network->trans_ssid, 0x00, sizeof(network->trans_ssid));
2466         (void)memcpy((void *)network->trans_ssid, (const void *)res->trans_ssid, res->trans_ssid_len);
2467         network->trans_ssid_len = res->trans_ssid_len;
2468     }
2469 #endif
2470 
2471 #if CONFIG_11K
2472     if (res->neighbor_report_supported == true)
2473     {
2474         network->neighbor_report_supported = true;
2475     }
2476     else
2477     {
2478         network->neighbor_report_supported = false;
2479     }
2480 #endif
2481 
2482 #if CONFIG_11V
2483     if (res->bss_transition_supported == true)
2484     {
2485         network->bss_transition_supported = true;
2486     }
2487     else
2488     {
2489         network->bss_transition_supported = false;
2490     }
2491 #endif
2492 
2493     network->security.is_pmf_required = res->is_pmf_required;
2494 
2495     switch (network->security.type)
2496     {
2497         case WLAN_SECURITY_WPA:
2498             network->security.mcstCipher.tkip = res->wpa_mcstCipher.tkip;
2499             network->security.ucstCipher.tkip = res->wpa_ucstCipher.tkip;
2500             network->security.mcstCipher.ccmp = res->wpa_mcstCipher.ccmp;
2501             network->security.ucstCipher.ccmp = res->wpa_ucstCipher.ccmp;
2502             break;
2503         case WLAN_SECURITY_WPA2:
2504         case WLAN_SECURITY_WPA_WPA2_MIXED:
2505 #if CONFIG_WPA2_ENTP
2506         case WLAN_SECURITY_EAP_TLS:
2507         case WLAN_SECURITY_EAP_PEAP_MSCHAPV2:
2508 #endif
2509             network->security.mcstCipher.ccmp = res->rsn_mcstCipher.ccmp;
2510             network->security.ucstCipher.ccmp = res->rsn_ucstCipher.ccmp;
2511             if (!res->rsn_mcstCipher.ccmp)
2512             {
2513                 network->security.mcstCipher.tkip = res->rsn_mcstCipher.tkip;
2514             }
2515             if (!res->rsn_ucstCipher.ccmp)
2516             {
2517                 network->security.ucstCipher.tkip = res->rsn_ucstCipher.tkip;
2518             }
2519             if (!res->rsn_mcstCipher.tkip && !res->rsn_ucstCipher.tkip && !res->rsn_mcstCipher.ccmp &&
2520                 !res->rsn_ucstCipher.ccmp)
2521             {
2522                 network->security.mcstCipher.ccmp = res->wpa_mcstCipher.ccmp;
2523                 network->security.ucstCipher.ccmp = res->wpa_ucstCipher.ccmp;
2524                 if (!res->wpa_mcstCipher.ccmp)
2525                 {
2526                     network->security.mcstCipher.tkip = res->wpa_mcstCipher.tkip;
2527                 }
2528                 if (!res->wpa_ucstCipher.ccmp)
2529                 {
2530                     network->security.ucstCipher.tkip = res->wpa_ucstCipher.tkip;
2531                 }
2532             }
2533             break;
2534         case WLAN_SECURITY_NONE:
2535             break;
2536         default:
2537             wlcm_d("Unexpected security nw param");
2538             break;
2539     }
2540 }
2541 
start_association(struct wlan_network * network,struct wifi_scan_result2 * res)2542 static int start_association(struct wlan_network *network, struct wifi_scan_result2 *res)
2543 {
2544     int ret                     = WM_SUCCESS;
2545     unsigned int owe_trans_mode = 0;
2546     bool is_ft                  = false;
2547 
2548     wlcm_d("starting association to \"%s\"", network->name);
2549     wlan.roam_reassoc = false;
2550     ret               = configure_security(network, res);
2551     if (ret != 0)
2552     {
2553         wlcm_d("setting security params failed");
2554         do_connect_failed(WLAN_REASON_NETWORK_AUTH_FAILED);
2555         return -WM_FAIL;
2556     }
2557 #if CONFIG_DRIVER_OWE
2558     owe_trans_mode = res->trans_mode;
2559 #endif
2560 #if CONFIG_11R
2561     is_ft       = wlan.ft_bss;
2562     wlan.ft_bss = false;
2563 #endif
2564 
2565     ret = wrapper_wifi_assoc(res->bssid, (int)network->security.type, (bool)network->security.ucstCipher.tkip,
2566                              owe_trans_mode, is_ft);
2567     if (ret != WM_SUCCESS)
2568     {
2569         wlcm_d("association failed");
2570 #if CONFIG_WLAN_FAST_PATH
2571         wlan.auth_cache_valid      = false;
2572         wlan.fast_path_cache_valid = false;
2573 #endif /* CONFIG_WLAN_FAST_PATH */
2574         do_connect_failed(WLAN_REASON_NETWORK_AUTH_FAILED);
2575         return -WM_FAIL;
2576     }
2577 #if CONFIG_WLAN_FAST_PATH
2578     else
2579     {
2580         /* cache the scan result for fast path.
2581          * It will be marked as valid only on
2582          * successful association and authentication */
2583         wlan.auth_cache_valid      = false;
2584         wlan.fast_path_cache_valid = false;
2585         (void)memcpy((void *)wlan.fast_path_bss, (const void *)res->bssid, MLAN_MAC_ADDR_LENGTH);
2586         wlan.fp_network = wlan.cur_network_idx;
2587     }
2588 #endif /* CONFIG_WLAN_FAST_PATH */
2589 
2590     return ret;
2591 }
2592 
2593 #if CONFIG_11V
wlan_send_btm_response(t_u8 * bssid,enum wnm_btm_status_code status)2594 static void wlan_send_btm_response(t_u8 *bssid, enum wnm_btm_status_code status)
2595 {
2596     wlan_send_mgmt_wnm_btm_resp(wlan.nlist_rep_param.dialog_token, status, wlan.nlist_rep_param.dst_addr, wlan.sta_mac,
2597                                 bssid, NULL, 0, wlan.nlist_rep_param.protect);
2598 
2599     memset(&wlan.nlist_rep_param, 0x00, sizeof(wlan_nlist_report_param));
2600 }
2601 #endif
2602 
handle_scan_results(void)2603 static void handle_scan_results(void)
2604 {
2605     unsigned int count;
2606     int ret;
2607     unsigned int i;
2608     struct wifi_scan_result2 *res;
2609     struct wlan_network *network = &wlan.networks[wlan.cur_network_idx];
2610     bool matching_ap_found       = false;
2611     uint8_t num_channels         = 0;
2612     wlan_scan_channel_list_t chan_list[40];
2613 
2614     /* comment out this, need to check if 11d needs 3 times full channel scan */
2615     /*
2616     if (wrapper_wlan_11d_support_is_enabled() && wlan.scan_count < WLAN_11D_SCAN_LIMIT)
2617     {
2618         wlcm_d("11D enabled, re-scanning");
2619         do_scan(network);
2620         return;
2621     }
2622     */
2623 
2624     /*
2625      * We need an allocation here because the lower layer puts all the
2626      * results into the same global buffer. Hence, we need a private
2627      * copy. fixme: Can be removed after this issue is fixed in the
2628      * lower layer.
2629      */
2630 #if !CONFIG_MEM_POOLS
2631     struct wifi_scan_result2 *best_ap = OSA_MemoryAllocate(sizeof(struct wifi_scan_result2));
2632 #else
2633     struct wifi_scan_result2 *best_ap = OSA_MemoryPoolAllocate(buf_512_MemoryPool);
2634 #endif
2635 
2636     if (best_ap == NULL)
2637     {
2638         wlcm_d("%s: Failed to alloc scan result object", __func__);
2639         return;
2640     }
2641 
2642     /* We're associating unless an error occurs, in which case we make a
2643      * decision to rescan (transition to CM_STA_SCANNING) or fail
2644      * (transition to CM_STA_IDLE or a new connection attempt) */
2645     wlan.sta_state = CM_STA_ASSOCIATING;
2646 
2647     ret = wifi_get_scan_result_count(&count);
2648     if (ret != 0)
2649     {
2650         count = 0;
2651     }
2652 
2653     /* 'count' scan results available, is our network there? */
2654     for (i = 0; i < count; i++)
2655     {
2656         ret = wifi_get_scan_result(i, &res);
2657         if (ret == WM_SUCCESS)
2658         {
2659             ret = network_matches_scan_result(network, res, &num_channels, chan_list);
2660             if (ret == WM_SUCCESS)
2661             {
2662                 if (!matching_ap_found)
2663                 {
2664                     /* First matching AP found */
2665                     (void)memcpy((void *)best_ap, (const void *)res, sizeof(struct wifi_scan_result2));
2666                     matching_ap_found = true;
2667                     /*
2668                      * Continue the search. There may be an AP
2669                      * with same name but better RSSI.
2670                      */
2671                     continue;
2672                 }
2673 
2674                 wlcm_d("RSSI: Best AP=%d Result AP=%d", best_ap->RSSI, res->RSSI);
2675                 if (best_ap->RSSI > res->RSSI)
2676                 {
2677                     /*
2678                      * We found a network better that current
2679                      * best_ap
2680                      */
2681                     wlcm_d("Found better AP %s on channel %d", res->ssid, res->Channel);
2682                     /* Assign the new found as curr_best */
2683                     (void)memcpy((void *)best_ap, (const void *)res, sizeof(struct wifi_scan_result2));
2684                 }
2685 
2686                 /* Continue the search */
2687             }
2688         }
2689     }
2690 
2691     if (matching_ap_found)
2692     {
2693         if (wlan.roam_reassoc == true)
2694         {
2695             if (memcmp((const void *)network->bssid, (const void *)best_ap->bssid, (size_t)IEEEtypes_ADDRESS_SIZE) == 0)
2696             {
2697 #if CONFIG_11V
2698                 if (wlan.nlist_rep_param.nlist_mode == WLAN_NLIST_11V)
2699                 {
2700                     wlan_send_btm_response(NULL, WNM_BTM_REJECT_NO_SUITABLE_CANDIDATES);
2701                 }
2702 #endif
2703                 wlan.sta_state    = CM_STA_CONNECTED;
2704                 wlan.roam_reassoc = false;
2705 #if CONFIG_11R
2706                 wlan.ft_bss = false;
2707 #endif
2708 #if !CONFIG_MEM_POOLS
2709                 OSA_MemoryFree((void *)best_ap);
2710 #else
2711                 OSA_MemoryPoolFree(buf_512_MemoryPool, best_ap);
2712 #endif
2713                 return;
2714             }
2715 #if CONFIG_11V
2716             if (wlan.nlist_rep_param.nlist_mode == WLAN_NLIST_11V)
2717             {
2718                 wlan_send_btm_response(best_ap->bssid, WNM_BTM_ACCEPT);
2719             }
2720 #endif
2721         }
2722 
2723         wlan.same_ess = false;
2724 
2725 #if CONFIG_11R
2726         if ((wlan.ft_bss == true) && (network->mdid == best_ap->mdid))
2727         {
2728             wlan.same_ess = true;
2729         }
2730 #endif
2731         wlan.same_ess |= wlan.roam_reassoc;
2732 
2733         update_network_params(network, best_ap);
2734 #if CONFIG_DRIVER_OWE
2735         if (network->owe_trans_mode == OWE_TRANS_MODE_OPEN)
2736         {
2737             wlcm_d("do scan for OWE Transition SSID: %s", network->trans_ssid);
2738 #if !CONFIG_MEM_POOLS
2739             OSA_MemoryFree((void *)best_ap);
2740 #else
2741             OSA_MemoryPoolFree(buf_512_MemoryPool, best_ap);
2742 #endif
2743             do_scan(network);
2744             return;
2745         }
2746         else
2747         {
2748 #endif
2749             ret = start_association(network, best_ap);
2750             if (ret == WM_SUCCESS)
2751             {
2752 #if !CONFIG_MEM_POOLS
2753                 OSA_MemoryFree((void *)best_ap);
2754 #else
2755                 OSA_MemoryPoolFree(buf_512_MemoryPool, best_ap);
2756 #endif
2757                 return;
2758             }
2759 #if CONFIG_DRIVER_OWE
2760         }
2761 #endif
2762     }
2763     else if (num_channels != 0U)
2764     {
2765 #if !CONFIG_MEM_POOLS
2766         OSA_MemoryFree((void *)best_ap);
2767 #else
2768         OSA_MemoryPoolFree(buf_512_MemoryPool, best_ap);
2769 #endif
2770         wlan.hidden_scan_on = true;
2771         do_hidden_scan(network, num_channels, chan_list);
2772         return;
2773     }
2774     else
2775     {
2776         /* Do Nothing */
2777     }
2778 
2779 #if !CONFIG_MEM_POOLS
2780     OSA_MemoryFree((void *)best_ap);
2781 #else
2782     OSA_MemoryPoolFree(buf_512_MemoryPool, best_ap);
2783 #endif
2784 
2785     if (wlan.roam_reassoc == true)
2786     {
2787         wlan.sta_state    = CM_STA_CONNECTED;
2788         wlan.roam_reassoc = false;
2789 #if CONFIG_11R
2790         wlan.ft_bss = false;
2791 #endif
2792         return;
2793     }
2794 
2795 #ifdef RW610
2796     /* If reset is in process, skip re-scan */
2797     if (OSA_MutexLock((osa_mutex_handle_t)reset_lock, 0) != WM_SUCCESS)
2798     {
2799         (void)PRINTF("skip re-scan when reset is in process\r\n");
2800         return;
2801     }
2802     OSA_MutexUnlock((osa_mutex_handle_t)reset_lock);
2803 #endif
2804 
2805     /* We didn't find our network in the scan results set: rescan if we
2806      * have rescan attempts remaining, otherwise give up.
2807      */
2808     if (wlan.scan_count < WLAN_RESCAN_LIMIT)
2809     {
2810         wlcm_d("network not found, re-scanning");
2811         wlan.hidden_scan_on = false;
2812         do_scan(network);
2813     }
2814     else
2815     {
2816         wlcm_d("rescan limit exceeded, giving up");
2817         do_connect_failed(WLAN_REASON_NETWORK_NOT_FOUND);
2818 
2819         if (wlan.reassoc_control)
2820         {
2821             wlcm_request_reconnect(&wlan.sta_state, network);
2822         }
2823     }
2824 }
2825 
wlan_disable_power_save(int action)2826 static void wlan_disable_power_save(int action)
2827 {
2828     switch (action)
2829     {
2830         case WLAN_DEEP_SLEEP:
2831             wlcm_d("stopping deep sleep ps mode");
2832             (void)wifi_exit_deepsleep_power_save();
2833             break;
2834         case WLAN_IEEE:
2835             wlcm_d("stopping IEEE ps mode");
2836             (void)wifi_exit_ieee_power_save();
2837             break;
2838 #if (CONFIG_WNM_PS)
2839         case WLAN_WNM:
2840             wlcm_d("stopping WNM ps mode");
2841             (void)wifi_exit_wnm_power_save();
2842             break;
2843 #endif
2844         default:
2845             wlcm_d("Unexpected disable ps action");
2846             break;
2847     }
2848 }
2849 
wlan_enable_power_save(int action)2850 static void wlan_enable_power_save(int action)
2851 {
2852     switch (action)
2853     {
2854         case WLAN_DEEP_SLEEP:
2855             wlcm_d("starting deep sleep ps mode");
2856             (void)wifi_enter_deepsleep_power_save();
2857             break;
2858         case WLAN_IEEE:
2859             wlcm_d("starting IEEE ps mode");
2860             (void)wifi_enter_ieee_power_save();
2861             break;
2862 #if (CONFIG_WNM_PS)
2863         case WLAN_WNM:
2864             wlcm_d("starting WNM ps mode");
2865             (void)wifi_enter_wnm_power_save(wlan.wnm_sleep_time);
2866             break;
2867 #endif
2868         default:
2869             wlcm_d("Unexpected ps mode");
2870             break;
2871     }
2872 }
2873 
wlcm_process_sleep_event(void)2874 static void wlcm_process_sleep_event(void)
2875 {
2876     wlan_send_sleep_confirm();
2877 }
2878 
wlcm_process_ieeeps_event(struct wifi_message * msg)2879 static void wlcm_process_ieeeps_event(struct wifi_message *msg)
2880 {
2881     ENH_PS_MODES action = (ENH_PS_MODES)(*((uint32_t *)msg->data));
2882     wlcm_d("got msg data :: %x", action);
2883 #if !CONFIG_MEM_POOLS
2884     OSA_MemoryFree(msg->data);
2885 #else
2886     OSA_MemoryPoolFree(buf_32_MemoryPool, msg->data);
2887 #endif
2888 
2889     if (msg->reason == WIFI_EVENT_REASON_SUCCESS)
2890     {
2891         if (action == EN_AUTO_PS)
2892         {
2893             if (!wlan.cm_ieeeps_configured)
2894             {
2895                 CONNECTION_EVENT(WLAN_REASON_PS_ENTER, (void *)WLAN_IEEE);
2896                 wlan.cm_ieeeps_configured = true;
2897             }
2898         }
2899         else if (action == DIS_AUTO_PS)
2900         {
2901             wlan.cm_ieeeps_configured = false;
2902             CONNECTION_EVENT(WLAN_REASON_PS_EXIT, (void *)WLAN_IEEE);
2903         }
2904         else if (action == SLEEP_CONFIRM)
2905         {
2906         }
2907         else
2908         { /* Do Nothing */
2909         }
2910     }
2911 }
2912 
wlcm_process_deepsleep_event(struct wifi_message * msg,enum cm_sta_state * next)2913 static void wlcm_process_deepsleep_event(struct wifi_message *msg, enum cm_sta_state *next)
2914 {
2915     ENH_PS_MODES action = (ENH_PS_MODES)(*((uint32_t *)msg->data));
2916 #if CONFIG_WIFI_PS_DEBUG
2917     wlcm_d("got msg data :: %x", action);
2918 #endif
2919 #if !CONFIG_MEM_POOLS
2920     OSA_MemoryFree(msg->data);
2921 #else
2922     OSA_MemoryPoolFree(buf_32_MemoryPool, msg->data);
2923 #endif
2924 
2925     if (msg->reason == WIFI_EVENT_REASON_SUCCESS)
2926     {
2927         if (action == EN_AUTO_PS)
2928         {
2929             if(!wlan.cm_deepsleepps_configured)
2930             {
2931                 CONNECTION_EVENT(WLAN_REASON_PS_ENTER, (void *)WLAN_DEEP_SLEEP);
2932                 wlan.cm_deepsleepps_configured = true;
2933             }
2934         }
2935         else if (action == DIS_AUTO_PS)
2936         {
2937             wlan.cm_deepsleepps_configured = false;
2938             // CONNECTION_EVENT(WLAN_REASON_INITIALIZED, NULL);
2939             /* Skip ps-exit event for the first time
2940                after waking from PM4+DS. This will ensure
2941                that we do not send ps-exit event until
2942                wlan-init event has been sent */
2943             CONNECTION_EVENT(WLAN_REASON_PS_EXIT, (void *)WLAN_DEEP_SLEEP);
2944         }
2945         else if (action == SLEEP_CONFIRM)
2946         {
2947         }
2948         else
2949         { /* Do Nothing */
2950         }
2951     }
2952 }
2953 
2954 #if (CONFIG_WNM_PS)
wlcm_process_wnmps_event(struct wifi_message * msg)2955 static void wlcm_process_wnmps_event(struct wifi_message *msg)
2956 {
2957     uint16_t action                      = (uint16_t)(*((uint32_t *)msg->data));
2958     wnm_sleep_result_t *wnm_sleep_result = (wnm_sleep_result_t *)&action;
2959 
2960 #if !CONFIG_MEM_POOLS
2961     OSA_MemoryFree(msg->data);
2962 #else
2963     OSA_MemoryPoolFree(buf_32_MemoryPool, msg->data);
2964 #endif
2965 
2966     if (msg->reason == WIFI_EVENT_REASON_SUCCESS)
2967     {
2968         if ((wnm_sleep_result->action == 0) && (wnm_sleep_result->result == 0))
2969         {
2970             wlcm_d("WNM enable success event: action %x result %x", wnm_sleep_result->action, wnm_sleep_result->result);
2971             if (!wlan.cm_wnmps_configured)
2972             {
2973                 CONNECTION_EVENT(WLAN_REASON_PS_ENTER, (void *)WLAN_WNM);
2974                 wlan.cm_wnmps_configured = true;
2975             }
2976         }
2977         else if ((wnm_sleep_result->action == 0) && (wnm_sleep_result->result == 1))
2978         {
2979             wlcm_d("WNM enable fail event: action %x result %x", wnm_sleep_result->action, wnm_sleep_result->result);
2980             wlan.cm_wnmps_configured = false;
2981         }
2982         else if (action == DIS_WNM_PS)
2983         {
2984             wlcm_d("WNM disable success event");
2985             wlan.cm_wnmps_configured = false;
2986             CONNECTION_EVENT(WLAN_REASON_PS_EXIT, (void *)WLAN_WNM);
2987         }
2988         else
2989         {
2990             /* Do nothing */
2991         }
2992     }
2993 }
2994 #endif
2995 
2996 #define WL_ID_STA_DISCONN "sta_disconnected"
2997 
2998 /* fixme: duplicated from legacy. Needs to be removed later. */
2999 #define IEEEtypes_REASON_MIC_FAILURE          14U
3000 #define IEEEtypes_REASON_4WAY_HANDSHK_TIMEOUT 15
3001 #define WPA2_ENTERPRISE_FAILED                0xFF
3002 
wlcm_process_scan_result_event(struct wifi_message * msg,enum cm_sta_state * next)3003 static void wlcm_process_scan_result_event(struct wifi_message *msg, enum cm_sta_state *next)
3004 {
3005     if (msg->reason == WIFI_EVENT_REASON_SUCCESS)
3006     {
3007         wifi_scan_process_results();
3008     }
3009 
3010     if (wlan.sta_state == CM_STA_SCANNING)
3011     {
3012         if (msg->reason == WIFI_EVENT_REASON_SUCCESS)
3013         {
3014             handle_scan_results();
3015         }
3016         *next = wlan.sta_state;
3017         wlcm_d("SM: returned to %s", dbg_sta_state_name(*next));
3018         wlcm_d("releasing scan lock (connect scan)");
3019     }
3020     else if (wlan.sta_state == CM_STA_SCANNING_USER)
3021     {
3022 #if CONFIG_WPA_SUPP
3023 #if CONFIG_11K
3024         if (wlan.enable_11k == 1U)
3025         {
3026             wifi_scan_done(msg);
3027             /*
3028              * Subscribe EVENT_RSSI_LOW if roaming is enabled.
3029              * Do this here in case roaming is not happened or failed in wpa_supplicant.
3030              */
3031 #if CONFIG_ROAMING
3032             wlan_subscribe_rssi_low_event();
3033 #endif
3034         }
3035 #endif
3036 #endif
3037         /*
3038          * zephyr l2 mgmt scan needs scan results to clear scan callback,
3039          * even if scan is failed
3040          */
3041         report_scan_results();
3042         *next = wlan.sta_return_to;
3043         wlcm_d("SM: returned to %s", dbg_sta_state_name(*next));
3044         wlcm_d("releasing scan lock (user scan)");
3045     }
3046     else
3047     {
3048 #if CONFIG_WPA_SUPP
3049         if (wlan.is_scan_lock)
3050         {
3051             wlcm_d("releasing scan lock (connect scan)");
3052             (void)OSA_SemaphorePost((osa_semaphore_handle_t)wlan.scan_lock);
3053             wlan.is_scan_lock = 0;
3054         }
3055 
3056         wifi_scan_done(msg);
3057 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
3058         /*
3059          * Subscribe EVENT_RSSI_LOW if roaming is enabled.
3060          * Do this here in case roaming is not happened or failed in wpa_supplicant.
3061          */
3062 #if CONFIG_ROAMING
3063         wlan_subscribe_rssi_low_event();
3064 #endif
3065         /*
3066         * Zephyr l2 mgmt scan needs call report_scan_results() to clear scan callback.
3067         *
3068         * If wlan.sta_state is modified to non-CM_STA_SCANNING_USER before receiving
3069         * scan report, also should call this function to clear scan callback function.
3070         *
3071         * Otherwise scan_cb can't be cleared and will failed to do l2 mgmt scan next time.
3072         */
3073         if(wlan.scan_cb != NULL)
3074             report_scan_results();
3075 #endif
3076 
3077         return;
3078 #else
3079         if (wlan.roam_reassoc == true)
3080         {
3081             if (wlan.sta_state == CM_STA_CONNECTED)
3082             {
3083                 wlcm_d("SM: returned to %s", dbg_sta_state_name(*next));
3084                 handle_scan_results();
3085 #if CONFIG_ROAMING
3086                 /*
3087                  * Subscribe EVENT_RSSI_LOW if roaming is enabled.
3088                  * Do this here in case roaming is not happened.
3089                  */
3090                 wlan_subscribe_rssi_low_event();
3091 #endif
3092                 *next = wlan.sta_state;
3093                 return;
3094             }
3095         }
3096 #endif
3097     }
3098 
3099     (void)OSA_SemaphorePost((osa_semaphore_handle_t)wlan.scan_lock);
3100     wlan.is_scan_lock = 0;
3101 }
3102 
wlcm_process_sta_addr_config_event(struct wifi_message * msg,enum cm_sta_state * next,struct wlan_network * network)3103 static void wlcm_process_sta_addr_config_event(struct wifi_message *msg,
3104                                                enum cm_sta_state *next,
3105                                                struct wlan_network *network)
3106 {
3107     /* We have a response to our configuration request from the TCP stack.
3108      * If we configured the stack with a static IP address and this was
3109      * successful, we are effectively connected so we can report success
3110      * and proceed to CM_STA_CONNECTED.  If we configured the stack with
3111      * DHCP and the response is successful then the TCP stack is trying to
3112      * obtain an address for us and we should proceed to the
3113      * CM_STA_OBTAINING_ADDRESS state and wait for the
3114      * WIFI_EVENT_DHCP_CONFIG event,
3115      * which will tell us whether or not the DHCP address was
3116      * obtained.  Otherwise, if we got an unsuccessful response then we
3117      * must report a connection error.
3118      *
3119      * This event is only relevant if we are connecting and have requested
3120      * a TCP stack configuration (that is, we are in the
3121      * CM_STA_REQUESTING_ADDRESS state).  Otherwise, we ignore it. */
3122 
3123     if (!is_state(CM_STA_REQUESTING_ADDRESS))
3124     {
3125         if (wlan.connect_wakelock_taken)
3126         {
3127 #if CONFIG_HOST_SLEEP
3128             wakelock_put();
3129 #endif
3130             wlan.connect_wakelock_taken = false;
3131         }
3132         wlcm_d("ignoring TCP configure response");
3133         return;
3134     }
3135     if (msg->reason != WIFI_EVENT_REASON_SUCCESS)
3136     {
3137         do_connect_failed(WLAN_REASON_ADDRESS_FAILED);
3138         *next = wlan.sta_state;
3139         return;
3140     }
3141     void *if_handle = NULL;
3142     switch (network->ip.ipv4.addr_type)
3143     {
3144         case ADDR_TYPE_STATIC:
3145 #if CONFIG_WMSTATS
3146             g_wm_stats.wm_addr_type = 0;
3147 #endif /* CONFIG_WMSTATS */
3148             net_configure_dns((struct net_ip_config *)&network->ip, network->role);
3149             if (network->type == WLAN_BSS_TYPE_STA)
3150             {
3151                 if_handle = net_get_mlan_handle();
3152             }
3153 #if CONFIG_P2P
3154             else if (network->type == WLAN_BSS_TYPE_WIFIDIRECT)
3155                 if_handle = net_get_wfd_handle();
3156 #endif /* CONFIG_P2P */
3157             (void)net_get_if_addr((struct net_ip_config *)&network->ip, if_handle);
3158             wlan.sta_state = CM_STA_CONNECTED;
3159             if (wlan.connect_wakelock_taken)
3160             {
3161 #if CONFIG_HOST_SLEEP
3162                 wakelock_put();
3163 #endif
3164                 wlan.connect_wakelock_taken = false;
3165             }
3166             *next                       = CM_STA_CONNECTED;
3167             wlan.sta_ipv4_state         = CM_STA_CONNECTED;
3168 #if CONFIG_P2P
3169             if (network->security.type != WLAN_SECURITY_NONE)
3170 #endif
3171                 if (wlan.reassoc_control && wlan.reassoc_request)
3172                 {
3173                     wlan.reassoc_count   = 0;
3174                     wlan.reassoc_request = false;
3175                 }
3176             mlan_adap->skip_dfs = false;
3177             CONNECTION_EVENT(WLAN_REASON_SUCCESS, NULL);
3178 #if CONFIG_P2P
3179             wifi_wfd_event(false, false, (void *)1);
3180 #endif
3181             break;
3182         case ADDR_TYPE_DHCP:
3183         case ADDR_TYPE_LLA:
3184 #if CONFIG_WMSTATS
3185             g_wm_stats.wm_addr_type = 1;
3186 #endif /* CONFIG_WMSTATS */
3187             *next               = CM_STA_OBTAINING_ADDRESS;
3188             wlan.sta_ipv4_state = CM_STA_OBTAINING_ADDRESS;
3189             break;
3190         default:
3191             wlcm_d("Unexpected addr type");
3192             break;
3193     } /* end of switch */
3194 #if CONFIG_IPV6
3195     /* Set the ipv6 state to obtaining address */
3196     wlan.sta_ipv6_state = CM_STA_OBTAINING_ADDRESS;
3197 #endif
3198 }
3199 
wlcm_process_channel_switch_ann(enum cm_sta_state * next,struct wlan_network * network)3200 static void wlcm_process_channel_switch_ann(enum cm_sta_state *next, struct wlan_network *network)
3201 {
3202     if (!wifi_is_ecsa_enabled())
3203     {
3204         if (is_state(CM_STA_CONNECTED))
3205         {
3206             wlcm_d("Sending deauth because of channel switch");
3207             set_event_chanswann();
3208             (void)wifi_deauthenticate((uint8_t *)network->bssid);
3209             /*
3210               This function call is already present in
3211               wlan_11h_handle_event_chanswann(). Remove it from here when
3212               that function is used directly.
3213             */
3214             wrapper_wlan_11d_clear_parsedtable();
3215             wrapper_clear_media_connected_event();
3216 #if CONFIG_WLAN_FAST_PATH
3217             /*
3218              * Flush fast path cache. We do not want fast path code to
3219              * push out assoc requests on the same channel on which
3220              * radar interference was detected.
3221              */
3222             wlan.auth_cache_valid      = false;
3223             wlan.fast_path_cache_valid = false;
3224 #endif /* CONFIG_WLAN_FAST_PATH */
3225             clear_event_chanswann();
3226             wlan.sta_state = CM_STA_IDLE;
3227             *next          = CM_STA_IDLE;
3228             CONNECTION_EVENT(WLAN_REASON_CHAN_SWITCH, NULL);
3229         }
3230     }
3231 }
3232 
3233 #if CONFIG_CSI
wlcm_process_csi_status_report(struct wifi_message * msg)3234 void wlcm_process_csi_status_report(struct wifi_message *msg)
3235 {
3236     if (msg->data != NULL)
3237     {
3238         wifi_csi_status_info *pcsi_status = (wifi_csi_status_info *)msg->data;
3239         if (pcsi_status->status == csi_enabled)
3240             (void)PRINTF("csi status report: enable and start csi on channel %d \r\n", pcsi_status->channel);
3241         if (pcsi_status->status == csi_disabled)
3242             (void)PRINTF("csi status report: stop and disable csi\r\n");
3243         if (pcsi_status->status == csiconfig_wrong)
3244             (void)PRINTF("csi status report: channel or bandwidth config wrong\r\n");
3245         if (pcsi_status->status == csiinternal_restart)
3246             (void)PRINTF("csi status report: FW internal restart csi on channel %d \r\n", pcsi_status->channel);
3247         if (pcsi_status->status == csiinternal_stop)
3248             (void)PRINTF("csi status report: FW internal stop csi\r\n");
3249         if (pcsi_status->status == csiinternal_disabled)
3250             (void)PRINTF("csi status report: FW internal stop and disable csi, user should put in csi cmd to enable csi\r\n");
3251 #if !CONFIG_MEM_POOLS
3252         OSA_MemoryFree((void *)msg->data);
3253 #else
3254         OSA_MemoryPoolFree(buf_32_MemoryPool, msg->data);
3255 #endif
3256     }
3257 }
3258 #endif
3259 
3260 #if CONFIG_WPA_SUPP
wlan_get_chan_type(nxp_wifi_ch_switch_info chandef)3261 enum mlan_channel_type wlan_get_chan_type(nxp_wifi_ch_switch_info chandef)
3262 {
3263     switch (chandef.ch_width) {
3264     case CHAN_BAND_WIDTH_20_NOHT:
3265         return CHAN_NO_HT;
3266     case CHAN_BAND_WIDTH_20:
3267         return CHAN_HT20;
3268     case CHAN_BAND_WIDTH_40:
3269         if (chandef.center_freq1 > chandef.center_freq)
3270             return CHAN_HT40PLUS;
3271         return CHAN_HT40MINUS;
3272     default:
3273 
3274         return CHAN_NO_HT;
3275     }
3276 }
3277 
wlan_get_chan_offset(int width,int freq,int cf1,int cf2)3278 int wlan_get_chan_offset(int width, int freq, int cf1, int cf2)
3279 {
3280     int freq1 = 0;
3281 
3282     switch (width)
3283     {
3284         case CHAN_BAND_WIDTH_20_NOHT:
3285         case CHAN_BAND_WIDTH_20:
3286             return 0;
3287         case CHAN_BAND_WIDTH_40:
3288             freq1 = cf1 - 10;
3289             break;
3290         case CHAN_BAND_WIDTH_80:
3291             freq1 = cf1 - 30;
3292             break;
3293         case CHAN_BAND_WIDTH_160:
3294             freq1 = cf1 - 70;
3295             break;
3296         case CHAN_BAND_WIDTH_80P80:
3297             freq1 = cf1 - 30;
3298             break;
3299         default:
3300             /* FIXME: implement this */
3301             return 0;
3302     }
3303 
3304     return (abs(freq - freq1) / 20) % 2 == 0 ? 1 : -1;
3305 }
3306 
wlcm_process_channel_switch_supp(struct wifi_message * msg)3307 static void wlcm_process_channel_switch_supp(struct wifi_message *msg)
3308 {
3309     chan_band_info  pchan_band_info ;
3310     nxp_wifi_ch_switch_info chandef = {0,};
3311     t_u8 band = BAND_2GHZ;
3312     enum mlan_channel_type ch_type = CHAN_NO_HT;
3313     uint8_t bss_type = 0;
3314     pmlan_private pmpriv = NULL;
3315     wifi_ecsa_info *pchan_info = NULL;
3316     t_u8 channel= 0 ;
3317     const chan_freq_power_t *cfp = MNULL;
3318     mlan_adapter *pmadapter = NULL;
3319 
3320     if (wifi_is_ecsa_enabled())
3321     {
3322         if (msg->data != NULL)
3323         {
3324             memset(&pchan_band_info , 0, sizeof(chan_band_info));
3325             pchan_info = (wifi_ecsa_info *)msg->data;
3326             bss_type = pchan_info->bss_type;
3327 
3328 #if !UAP_SUPPORT
3329             if (bss_type > WLAN_BSS_ROLE_STA)
3330             {
3331 #if !CONFIG_MEM_POOLS
3332                 OSA_MemoryFree((void *)msg->data);
3333 #else
3334                 OSA_MemoryPoolFree(buf_32_MemoryPool, msg->data);
3335 #endif
3336                 return;
3337             }
3338 #endif
3339 
3340             pmpriv = mlan_adap->priv[bss_type];
3341             pmadapter = pmpriv->adapter;
3342             channel = pchan_info->channel;
3343 
3344 #if UAP_SUPPORT
3345             if(is_uap_started())
3346             {
3347                 pmpriv->uap_channel = channel;
3348                 pmpriv->uap_state_chan_cb.channel = channel;
3349                 pmpriv->uap_state_chan_cb.band_config = pchan_info->band_config;
3350                 pchan_band_info.is_11n_enabled = pmpriv->is_11n_enabled;
3351             }
3352 #endif
3353 
3354             if (is_sta_connected())
3355             {
3356                 wifi_set_curr_bss_channel(channel);
3357 #define MAX_CHANNEL_BAND_B 14
3358                 if (channel <= MAX_CHANNEL_BAND_B)
3359                     cfp = wlan_find_cfp_by_band_and_channel(pmadapter, BAND_B, channel);
3360 #if CONFIG_5GHz_SUPPORT
3361                 else
3362                     cfp = wlan_find_cfp_by_band_and_channel(pmadapter, BAND_A, channel);
3363 #endif
3364                 if (cfp)
3365                     pmpriv->curr_bss_params.bss_descriptor.freq = cfp->freq;
3366                 else
3367                     pmpriv->curr_bss_params.bss_descriptor.freq = 0;
3368             }
3369 
3370             /* Handle Host-based DFS and non-DFS(normal uap) case */
3371             memcpy((t_u8 *)&pchan_band_info.bandcfg,(t_u8 *)&pchan_info->band_config, sizeof(pchan_info->band_config));
3372 		    pchan_band_info.channel = channel;
3373 #if (CONFIG_11AC)
3374             if (pchan_band_info.bandcfg.chanWidth == CHAN_BW_80MHZ)
3375                 pchan_band_info.center_chan = wlan_get_center_freq_idx(pmpriv, BAND_AAC,channel,CHANNEL_BW_80MHZ);
3376 #endif
3377 
3378             /*Get freq and width info*/
3379             memset(&chandef,0, sizeof(nxp_wifi_ch_switch_info));
3380             chandef.center_freq2 = 0;
3381 
3382             if (pchan_band_info.bandcfg.chanBand == BAND_2GHZ)
3383                 band = BAND_2GHZ;
3384             else if (pchan_band_info.bandcfg.chanBand == BAND_5GHZ)
3385                 band = BAND_5GHZ;
3386 
3387             chandef.center_freq = channel_to_frequency(pchan_band_info.channel, band);
3388 
3389             switch (pchan_band_info.bandcfg.chanWidth)
3390             {
3391                 case CHAN_BW_20MHZ:
3392                     if (pchan_band_info.is_11n_enabled)
3393                         chandef.ch_width = CHAN_BAND_WIDTH_20;
3394                     else
3395                         chandef.ch_width = CHAN_BAND_WIDTH_20_NOHT;
3396                     chandef.center_freq1 = chandef.center_freq;
3397                     break;
3398 
3399                 case CHAN_BW_40MHZ:
3400                     chandef.ch_width = CHAN_BAND_WIDTH_40;
3401                     if (pchan_band_info.bandcfg.chan2Offset == SEC_CHAN_ABOVE)
3402                         chandef.center_freq1 = chandef.center_freq + 10;
3403                     else if (pchan_band_info.bandcfg.chan2Offset == SEC_CHAN_BELOW)
3404                         chandef.center_freq1 = chandef.center_freq - 10;
3405                     break;
3406 
3407 #if (CONFIG_11AC)
3408                 case CHAN_BW_80MHZ:
3409                     chandef.ch_width = CHAN_BAND_WIDTH_80;
3410                     chandef.center_freq1 = channel_to_frequency(pchan_band_info.center_chan, band);
3411                     break;
3412 #endif
3413 
3414                 default:
3415                     break;
3416             }
3417 
3418             /*Get type*/
3419             switch (chandef.ch_width)
3420             {
3421                 case CHAN_BAND_WIDTH_20_NOHT:
3422                 case CHAN_BAND_WIDTH_20:
3423                 case CHAN_BAND_WIDTH_40:
3424                     ch_type = wlan_get_chan_type(chandef);
3425                     break;
3426                 default:
3427                     break;
3428             }
3429 
3430             chandef.ht_enabled = 1;
3431             /*Get ht and ch_offset info*/
3432             switch (ch_type)
3433             {
3434                 case CHAN_NO_HT:
3435                     chandef.ht_enabled = 0;
3436                     break;
3437                 case CHAN_HT20:
3438                     break;
3439                 case CHAN_HT40PLUS:
3440                     chandef.ch_offset = 1;
3441                     break;
3442                 case CHAN_HT40MINUS:
3443                     chandef.ch_offset = -1;
3444                     break;
3445                 default:
3446                     if (chandef.ch_width && chandef.center_freq1)
3447                     {
3448                         /* This can happen for example with VHT80 ch switch */
3449                         chandef.ch_offset = wlan_get_chan_offset(chandef.ch_width, chandef.center_freq, chandef.center_freq1, chandef.center_freq2 ? chandef.center_freq2 : 0);
3450                     }
3451                     else
3452                     {
3453                         PRINTF("Unknown secondary channel information - following channel definition calculations may fail\r\n");
3454                     }
3455                     break;
3456            }
3457 
3458 #if UAP_SUPPORT
3459             if(is_uap_started())
3460             {
3461                 wm_wifi.supp_if_callbk_fns->ecsa_complete_callbk_fn(wm_wifi.hapd_if_priv, &chandef);
3462                 (void)PRINTF("uap switch to channel %d success!\r\n", channel);
3463             }
3464 #endif
3465 
3466             if (is_sta_connected())
3467             {
3468                 wm_wifi.supp_if_callbk_fns->ecsa_complete_callbk_fn(wm_wifi.if_priv, &chandef);
3469                 (void)PRINTF("sta switch to channel %d success!\r\n", channel);
3470             }
3471 #if !CONFIG_MEM_POOLS
3472             OSA_MemoryFree((void *)msg->data);
3473 #else
3474             OSA_MemoryPoolFree(buf_32_MemoryPool, msg->data);
3475 #endif
3476         }
3477 
3478 #if CONFIG_ECSA
3479         wifi_put_ecsa_sem();
3480         set_ecsa_block_tx_flag(false);
3481 #endif
3482     }
3483     else
3484     {
3485         wlcm_d("ECSA not support");
3486         if (msg->data != NULL)
3487         {
3488 #if !CONFIG_MEM_POOLS
3489             OSA_MemoryFree((void *)msg->data);
3490 #else
3491             OSA_MemoryPoolFree(buf_32_MemoryPool, msg->data);
3492 #endif
3493         }
3494     }
3495 }
3496 #else
wlcm_process_channel_switch(struct wifi_message * msg)3497 static void wlcm_process_channel_switch(struct wifi_message *msg)
3498 {
3499     t_u8 channel= 0 ;
3500     wifi_ecsa_info *pchan_info = NULL;
3501 
3502     if (wifi_is_ecsa_enabled())
3503     {
3504         if (msg->data != NULL)
3505         {
3506             pchan_info = (wifi_ecsa_info *)msg->data;
3507             channel= pchan_info->channel;
3508 
3509 #if UAP_SUPPORT
3510             if(is_uap_started())
3511             {
3512                 (void)PRINTF("uap switch to channel %d success!\r\n", channel);
3513                 wlan.networks[wlan.cur_uap_network_idx].channel = channel;
3514             }
3515 #endif
3516 
3517             if (is_sta_connected())
3518             {
3519                 (void)PRINTF("sta switch to channel %d success!\r\n", channel);
3520                 wlan.networks[wlan.cur_network_idx].channel = channel;
3521                 wifi_set_curr_bss_channel(wlan.networks[wlan.cur_network_idx].channel);
3522             }
3523 #if !CONFIG_MEM_POOLS
3524             OSA_MemoryFree((void *)msg->data);
3525 #else
3526             OSA_MemoryPoolFree(buf_32_MemoryPool, msg->data);
3527 #endif
3528         }
3529 
3530 #if CONFIG_ECSA
3531         wifi_put_ecsa_sem();
3532         set_ecsa_block_tx_flag(false);
3533 #endif
3534     }
3535     else
3536     {
3537         wlcm_d("ECSA not support");
3538         if (msg->data != NULL)
3539         {
3540 #if !CONFIG_MEM_POOLS
3541             OSA_MemoryFree((void *)msg->data);
3542 #else
3543             OSA_MemoryPoolFree(buf_32_MemoryPool, msg->data);
3544 #endif
3545         }
3546     }
3547 }
3548 #endif /*End of CONFIG_WPA_SUPP*/
3549 
3550 #if CONFIG_HOST_SLEEP
wlcm_process_hs_config_event(void)3551 static void wlcm_process_hs_config_event(void)
3552 {
3553     /* host sleep config done event received */
3554     int ret = WM_SUCCESS;
3555     unsigned int ipv4_addr = 0;
3556     enum wlan_bss_type type = WLAN_BSS_TYPE_STA;
3557 
3558     if(is_sta_ipv4_connected() != 0)
3559     {
3560         ret = wlan_get_ipv4_addr(&ipv4_addr);
3561         if (ret != WM_SUCCESS)
3562         {
3563             wlcm_e("HS : Cannot get STA IP, check if STA disconnected");
3564             return;
3565         }
3566     }
3567     /* If uap interface is up
3568      * configure host sleep for uap interface
3569      * else confiugre host sleep for station
3570      * interface.
3571      */
3572 #if UAP_SUPPORT
3573     else if (is_uap_started() != 0)
3574     {
3575         ret = wlan_get_uap_ipv4_addr(&ipv4_addr);
3576         if (ret != WM_SUCCESS)
3577         {
3578             wlcm_e("HS: Cannot get UAP IP, check if uAP stopped");
3579             return;
3580         }
3581         type = WLAN_BSS_TYPE_UAP;
3582     }
3583 #endif
3584     else
3585         ipv4_addr = 0;
3586 
3587     (void)wifi_send_hs_cfg_cmd((mlan_bss_type)type, ipv4_addr, (uint16_t)HS_ACTIVATE, 0);
3588 }
3589 #endif
3590 
3591 #if CONFIG_11N
wlcm_process_send_addba(struct wifi_message * msg)3592 static void wlcm_process_send_addba(struct wifi_message *msg)
3593 {
3594     send_add_ba_param_t *addba = (send_add_ba_param_t *)msg->data;
3595 
3596     wlan_send_addba(mlan_adap->priv[addba->interface], addba->tid, addba->peer_mac);
3597 
3598 #if !CONFIG_MEM_POOLS
3599     OSA_MemoryFree(addba);
3600 #else
3601     OSA_MemoryPoolFree(buf_32_MemoryPool, addba);
3602 #endif
3603 }
3604 
wlcm_process_addba_request(struct wifi_message * msg)3605 static void wlcm_process_addba_request(struct wifi_message *msg)
3606 {
3607     if (is_sta_associated() || is_uap_started())
3608     {
3609         (void)wrapper_wlan_cmd_11n_addba_rspgen(msg->data);
3610     }
3611     else
3612     {
3613         wlcm_d("Ignore ADDBA Request event in disconnected state");
3614 #if !CONFIG_MEM_POOLS
3615         OSA_MemoryFree((void *)msg->data);
3616 #else
3617         OSA_MemoryPoolFree(buf_256_MemoryPool, msg->data);
3618 #endif
3619     }
3620 }
3621 
wlcm_process_delba_request(struct wifi_message * msg)3622 static void wlcm_process_delba_request(struct wifi_message *msg)
3623 {
3624     if (is_sta_associated() || is_uap_started())
3625     {
3626         (void)wrapper_wlan_cmd_11n_delba_rspgen(msg->data);
3627     }
3628     else
3629     {
3630         wlcm_d("Ignore DELBA Request event in disconnected state");
3631 #if !CONFIG_MEM_POOLS
3632         OSA_MemoryFree((void *)msg->data);
3633 #else
3634         OSA_MemoryPoolFree(buf_256_MemoryPool, msg->data);
3635 #endif
3636     }
3637 }
3638 
wlcm_process_ba_stream_timeout_request(struct wifi_message * msg)3639 static void wlcm_process_ba_stream_timeout_request(struct wifi_message *msg)
3640 {
3641     if (is_sta_associated() || is_uap_started())
3642     {
3643         (void)wrapper_wlan_cmd_11n_ba_stream_timeout(msg->data);
3644     }
3645     else
3646     {
3647         wlcm_d(
3648             "Ignore BA STREAM TIMEOUT Request"
3649             " event in disconnected state");
3650 #if !CONFIG_MEM_POOLS
3651         OSA_MemoryFree((void *)msg->data);
3652 #else
3653         OSA_MemoryPoolFree(buf_256_MemoryPool, msg->data);
3654 #endif
3655     }
3656 }
3657 #endif
3658 
wlcm_process_association_event(struct wifi_message * msg,enum cm_sta_state * next)3659 static void wlcm_process_association_event(struct wifi_message *msg, enum cm_sta_state *next)
3660 {
3661 #if CONFIG_WPA2_ENTP
3662     int ret;
3663     struct wlan_network *network = &wlan.networks[wlan.cur_network_idx];
3664 #endif
3665 
3666 #if CONFIG_WLAN_FAST_PATH
3667     if (wlan.is_scan_lock)
3668     {
3669         wlcm_d("releasing scan lock for fast path");
3670         (void)OSA_SemaphorePost((osa_semaphore_handle_t)wlan.scan_lock);
3671         wlan.is_scan_lock = 0;
3672     }
3673 #endif
3674 #if CONFIG_WPA_SUPP
3675     if (wlan.is_scan_lock)
3676     {
3677       	wlcm_d("releasing scan lock (connect scan)");
3678         (void)OSA_SemaphorePost((osa_semaphore_handle_t)wlan.scan_lock);
3679        	wlan.is_scan_lock = 0;
3680     }
3681 #endif
3682 
3683     /* We have received a response to the association command.  We may now
3684      * proceed to authenticating if it was successful, otherwise this
3685      * connection attempt has failed.
3686      *
3687      * This event is only relevant if we have sent an association command
3688      * while connecting (that is, we are in the CM_STA_ASSOCIATING state).
3689      * Otherwise, it is ignored. */
3690 
3691 #if !CONFIG_WPA_SUPP
3692     if (!is_state(CM_STA_ASSOCIATING))
3693     {
3694         wlcm_d("ignoring association result event");
3695         return;
3696     }
3697 #endif
3698 
3699     if (msg->reason == WIFI_EVENT_REASON_SUCCESS)
3700     {
3701         wlan.sta_state = CM_STA_ASSOCIATED;
3702         *next          = CM_STA_ASSOCIATED;
3703 
3704 #if CONFIG_WPA2_ENTP
3705         if (wlan_get_prov_session() == PROV_ENTP_SESSION_ATTEMPT)
3706         {
3707             ret = wpa2_ent_connect(network);
3708             if (ret != WM_SUCCESS)
3709             {
3710                 wlcm_e("wpa2_ent_connect failed");
3711             }
3712         }
3713 #endif
3714         wlan.scan_count = 0;
3715     }
3716 #if !CONFIG_WPA_SUPP
3717     else if (wlan.scan_count < WLAN_RESCAN_LIMIT)
3718     {
3719         wlcm_d("association failed, re-scanning");
3720 #if CONFIG_WPA2_ENTP
3721         if (wlan_get_prov_session() == PROV_ENTP_SESSION_ATTEMPT)
3722         {
3723             wpa2_tls_cleanup(network, true);
3724         }
3725 #endif
3726         /*
3727          *  this scan does not hold scan lock as it was already put by wlcmgr task
3728          *  need to check if need to hold scan lock again
3729          */
3730         do_scan(&wlan.networks[wlan.cur_network_idx]);
3731         *next = CM_STA_SCANNING;
3732     }
3733 #endif
3734     else
3735     {
3736 #if CONFIG_WPA2_ENTP
3737         if (wlan_get_prov_session() == PROV_ENTP_SESSION_ATTEMPT)
3738         {
3739             wpa2_tls_cleanup(network, true);
3740         }
3741 #endif
3742 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
3743 #if CONFIG_WPA_SUPP
3744         OSA_TimerDeactivate((osa_timer_handle_t)wlan.supp_status_timer);
3745         wlan.status_timeout = 0;
3746 #endif
3747 #endif
3748 
3749 #if !CONFIG_WPA_SUPP
3750         do_connect_failed(WLAN_REASON_NETWORK_AUTH_FAILED);
3751 
3752         if (wlan.reassoc_control)
3753         {
3754             wlcm_request_reconnect(next, &wlan.networks[wlan.cur_network_idx]);
3755         }
3756 #endif
3757         *next = wlan.sta_state;
3758     }
3759 }
3760 
3761 #if CONFIG_WPA_SUPP
3762 #if CONFIG_AUTO_RECONNECT
3763 
wlcm_process_association_notify_event(struct wifi_message * msg,enum cm_sta_state * next)3764 static void wlcm_process_association_notify_event(struct wifi_message *msg, enum cm_sta_state *next)
3765 {
3766     struct netif *netif = net_get_sta_interface();
3767 
3768     (void)msg;
3769     (void)next;
3770 
3771     if (wlan.roam_reassoc == false)
3772     {
3773         (void)wpa_supp_notify_assoc(netif);
3774     }
3775 }
3776 
3777 #endif
3778 #endif
3779 
wlcm_process_pmk_event(struct wifi_message * msg,enum cm_sta_state * next,struct wlan_network * network)3780 static void wlcm_process_pmk_event(struct wifi_message *msg, enum cm_sta_state *next, struct wlan_network *network)
3781 {
3782     char *bssid = network->bssid;
3783 
3784     if (msg->data != NULL)
3785     {
3786         network->security.pmk_valid = true;
3787         (void)memcpy((void *)network->security.pmk, (const void *)msg->data, WLAN_PMK_LENGTH);
3788         if (network->role == WLAN_BSS_ROLE_STA)
3789         {
3790 #if CONFIG_WPA2_ENTP
3791             if (network->security.type == WLAN_SECURITY_EAP_TLS)
3792             {
3793                 /* OKC-802.1X case, roaming without specifying bssid */
3794                 bssid = NULL;
3795             }
3796 #endif
3797             (void)wifi_send_add_wpa_pmk((int)network->role, network->ssid, bssid, network->security.pmk,
3798                                         WLAN_PMK_LENGTH);
3799         }
3800     }
3801 }
3802 
wlcm_process_authentication_event(struct wifi_message * msg,enum cm_sta_state * next,struct wlan_network * network)3803 static void wlcm_process_authentication_event(struct wifi_message *msg,
3804                                               enum cm_sta_state *next,
3805                                               struct wlan_network *network)
3806 {
3807 #if CONFIG_STA_AUTO_DHCPV4
3808     int ret         = 0;
3809 #endif
3810     void *if_handle = NULL;
3811 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
3812 #if CONFIG_WPA_SUPP
3813     struct netif *netif = net_get_sta_interface();
3814 #endif
3815 #endif
3816 
3817 #if CONFIG_WPS2
3818     if (wlan_get_prov_session() == PROV_WPS_SESSION_ATTEMPT)
3819     {
3820         if(wlan.connect_wakelock_taken)
3821         {
3822 #if CONFIG_HOST_SLEEP
3823             wakelock_put();
3824 #endif
3825             wlan.connect_wakelock_taken = false;
3826         }
3827         return;
3828     }
3829 #endif
3830 
3831     if (!is_sta_associated())
3832     {
3833         if (wlan.connect_wakelock_taken)
3834         {
3835 #if CONFIG_HOST_SLEEP
3836             wakelock_put();
3837 #endif
3838             wlan.connect_wakelock_taken = false;
3839         }
3840         wlcm_d("ignoring authentication event");
3841         return;
3842     }
3843 
3844     if (msg->reason == WIFI_EVENT_REASON_SUCCESS)
3845     {
3846 #if CONFIG_WMSTATS
3847         g_wm_stats.wm_conn_succ++;
3848 #endif /* CONFIG_WMSTATS */
3849 #if CONFIG_WLAN_FAST_PATH
3850         if (wlan.fast_path_cache_valid == true && (wlan.cur_network_idx == wlan.fp_network))
3851         {
3852             if_handle = net_get_mlan_handle();
3853             net_interface_up(if_handle);
3854             // wakelock_put(WL_ID_CONNECT);
3855             // wlan.connect_wakelock_taken = false;
3856             wlan.sta_ipv4_state         = CM_STA_CONNECTED;
3857             *next                       = CM_STA_CONNECTED;
3858 
3859             if (wlan.reassoc_control && wlan.reassoc_request)
3860             {
3861                 wlan.reassoc_count   = 0;
3862                 wlan.reassoc_request = false;
3863             }
3864             mlan_adap->skip_dfs = false;
3865             CONNECTION_EVENT(WLAN_REASON_SUCCESS, NULL);
3866         }
3867         else
3868         {
3869 #endif /* CONFIG_WLAN_FAST_PATH */
3870             if (network->type == WLAN_BSS_TYPE_STA)
3871             {
3872                 if_handle = net_get_mlan_handle();
3873             }
3874 #if CONFIG_P2P
3875             else if (network->type == WLAN_BSS_TYPE_WIFIDIRECT)
3876             {
3877                 if_handle = net_get_wfd_handle();
3878             }
3879             else
3880             {
3881                 /* Do nothing */
3882             }
3883 #endif /* CONFIG_P2P */
3884             CONNECTION_EVENT(WLAN_REASON_AUTH_SUCCESS, NULL);
3885 
3886 #if CONFIG_BG_SCAN
3887             wlan.bgscan_attempt = 0;
3888 #endif
3889 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
3890 #if CONFIG_WPA_SUPP
3891             OSA_TimerDeactivate((osa_timer_handle_t)wlan.supp_status_timer);
3892             wlan.status_timeout = 0;
3893 
3894             wpa_supp_network_status(netif, network);
3895 #endif
3896 #endif
3897 
3898 #if CONFIG_ROAMING
3899             wlan_subscribe_rssi_low_event();
3900 #endif
3901 
3902 #if CONFIG_WPA_SUPP
3903 #if CONFIG_11R
3904             wlan.same_ess = wifi_same_ess_ft();
3905 #endif
3906             wlan.roam_reassoc = false;
3907 #endif
3908             if (wlan.same_ess == true)
3909             {
3910 #if CONFIG_11R
3911                 wlan.ft_bss = false;
3912 #endif
3913                 (void)net_get_if_addr((struct net_ip_config *)&network->ip, if_handle);
3914                 wlan.sta_state      = CM_STA_CONNECTED;
3915                 *next               = CM_STA_CONNECTED;
3916                 wlan.sta_ipv4_state = CM_STA_CONNECTED;
3917 
3918                 if (wlan.reassoc_control && wlan.reassoc_request)
3919                 {
3920                     wlan.reassoc_count   = 0;
3921                     wlan.reassoc_request = false;
3922                 }
3923                 mlan_adap->skip_dfs = false;
3924                 CONNECTION_EVENT(WLAN_REASON_SUCCESS, NULL);
3925                 return;
3926             }
3927 
3928 #if CONFIG_STA_AUTO_DHCPV4
3929             ret = net_configure_address((struct net_ip_config *)&network->ip, if_handle);
3930             if (ret != 0)
3931             {
3932                 wlcm_e("Configure Address failed");
3933                 do_connect_failed(WLAN_REASON_ADDRESS_FAILED);
3934                 *next = wlan.sta_state;
3935             }
3936             else
3937             {
3938                 *next               = CM_STA_REQUESTING_ADDRESS;
3939                 wlan.sta_ipv4_state = CM_STA_REQUESTING_ADDRESS;
3940 #if CONFIG_IPV6
3941                 wlan.sta_ipv6_state = CM_STA_REQUESTING_ADDRESS;
3942 #endif
3943             }
3944 #else
3945             *next               = CM_STA_AUTHENTICATED;
3946             wlan.sta_ipv4_state = CM_STA_AUTHENTICATED;
3947 #if CONFIG_IPV6
3948             wlan.sta_ipv6_state = CM_STA_AUTHENTICATED;
3949 #endif
3950 #endif
3951 
3952 #if CONFIG_WLAN_FAST_PATH
3953             wlan.auth_cache_valid = false;
3954             /* Mark the fast path cache valid after
3955              * successful connection. */
3956             wlcm_d("Fast Path: validating settings");
3957             wlan.fast_path_cache_valid = true;
3958         }
3959 #endif /* CONFIG_WLAN_FAST_PATH */
3960 #if CONFIG_WPA2_ENTP
3961         if (wlan_get_prov_session() == PROV_ENTP_SESSION_ATTEMPT &&
3962             (network->security.type != WLAN_SECURITY_EAP_PEAP_MSCHAPV2))
3963         {
3964             wpa2_tls_cleanup(network, false);
3965         }
3966 #endif
3967     }
3968     else
3969     {
3970 #if CONFIG_WLAN_FAST_PATH
3971         wlan.auth_cache_valid = true;
3972         /* Mark the fast path cache invalid. */
3973         wlan.fast_path_cache_valid = false;
3974 #endif /* CONFIG_WLAN_FAST_PATH */
3975 #if CONFIG_WPA2_ENTP
3976         if (wlan_get_prov_session() == PROV_ENTP_SESSION_ATTEMPT)
3977         {
3978             wpa2_tls_cleanup(network, false);
3979         }
3980         else if (wlan_get_prov_session() == PROV_NON_SESSION_ATTEMPT &&
3981                  network->security.pmk_valid == true &&
3982                  (network->security.type == WLAN_SECURITY_EAP_TLS || network->security.type == WLAN_SECURITY_EAP_PEAP_MSCHAPV2)
3983                 )
3984         {
3985             /* If EAP_TLS re-connected but get auth failed, clear pmk_valid/pmk and enter "PROV_ENTP_SESSION_ATTEMPT" again */
3986             network->security.pmk_valid = false;
3987             memset(network->security.pmk, 0, WLAN_PMK_LENGTH);
3988         }
3989 #endif
3990         if (*((uint16_t *)msg->data) == IEEEtypes_REASON_MIC_FAILURE)
3991         {
3992             wlan.assoc_paused = true;
3993             (void)OSA_TimerActivate((osa_timer_handle_t)wlan.assoc_timer);
3994         }
3995 
3996         if (is_state(CM_STA_REQUESTING_ADDRESS) || is_state(CM_STA_OBTAINING_ADDRESS))
3997         {
3998             void *if_handle = NULL;
3999             /* On Link loss, we need to take down the interface. */
4000             if (network->type == WLAN_BSS_TYPE_STA)
4001             {
4002                 if_handle = net_get_mlan_handle();
4003             }
4004 
4005             if (if_handle != NULL)
4006             {
4007                 /* Forcefully stop dhcp on given interface.
4008                  * net_interface_dhcp_stop internally does nothing
4009                  * if dhcp client is not started.
4010                  */
4011                 net_interface_dhcp_stop(if_handle);
4012                 net_interface_down(if_handle);
4013             }
4014         }
4015 
4016 #if !CONFIG_WPA_SUPP
4017         if (is_state(CM_STA_ASSOCIATED))
4018         {
4019             (void)wifi_deauthenticate((uint8_t *)network->bssid);
4020         }
4021 #endif
4022         wlan.sta_return_to  = CM_STA_IDLE;
4023         wlan.sta_state      = CM_STA_IDLE;
4024         *next               = CM_STA_IDLE;
4025         wlan.sta_ipv4_state = CM_STA_IDLE;
4026 #if CONFIG_IPV6
4027         wlan.sta_ipv6_state = CM_STA_IDLE;
4028 #endif
4029 
4030 #if !CONFIG_WPA_SUPP
4031         do_connect_failed(WLAN_REASON_NETWORK_AUTH_FAILED);
4032 #endif
4033 
4034         if (wlan.reassoc_control)
4035         {
4036             wlcm_request_reconnect(next, network);
4037         }
4038 
4039 #if CONFIG_P2P
4040         wifi_wfd_event(false, false, NULL);
4041 #endif
4042     }
4043 }
4044 
4045 #if (CONFIG_11K) || (CONFIG_11V) || (CONFIG_ROAMING)
4046 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
wlcm_process_rssi_low_event(struct wifi_message * msg)4047 static void wlcm_process_rssi_low_event(struct wifi_message *msg)
4048 {
4049     t_s16 curr_rssi = *(t_s16 *)msg->data;
4050 
4051     wm_wifi.supp_if_callbk_fns->signal_change_callbk_fn(wm_wifi.if_priv, &curr_rssi);
4052 #if !CONFIG_MEM_POOLS
4053     OSA_MemoryFree(msg->data);
4054 #else
4055     OSA_MemoryPoolFree(buf_32_MemoryPool, msg->data);
4056 #endif
4057 }
4058 #else
4059 /** Try to roaming if enabled based on priority:
4060  * 1. 11R roaming (full channel scan)
4061  * 2. 11K roaming
4062  * 3. 11V roaming
4063  * 4. Legacy roaming (full channel scan)
4064  *
4065  * If not trigger roaming, subscribe RSSI low event again
4066  */
wlcm_process_rssi_low_event(struct wifi_message * msg,enum cm_sta_state * next,struct wlan_network * network)4067 static void wlcm_process_rssi_low_event(struct wifi_message *msg, enum cm_sta_state *next, struct wlan_network *network)
4068 {
4069 #if !CONFIG_ROAMING
4070     wlcm_d("wlcm_process_rssi_low_event roaming not support");
4071     return;
4072 #else
4073     bool set_rssi_threshold = false;
4074 #if CONFIG_BG_SCAN || CONFIG_11K || CONFIG_11V
4075     int ret;
4076 #endif
4077 
4078     if (wlan.roaming_enabled == false)
4079     {
4080         wlcm_d("wlcm_process_rssi_low_event roaming disabled");
4081         return;
4082     }
4083 
4084 #if CONFIG_11R
4085     if (wlan.roam_reassoc == false)
4086     {
4087         wlan.ft_bss = false;
4088         if ((network->ft_psk | network->ft_1x | network->ft_sae) == 1U)
4089         {
4090             wlan.ft_bss = true;
4091             wlan.roam_reassoc = true;
4092 
4093 #if CONFIG_BG_SCAN
4094             ret = wifi_config_bgscan_and_rssi(network->ssid);
4095             if (ret == WM_SUCCESS)
4096             {
4097                 wlcm_d("bgscan config successful");
4098                 return;
4099             }
4100 #endif
4101             wlan.roam_reassoc = false;
4102             set_rssi_threshold = true;
4103         }
4104     }
4105     else
4106     {
4107         wlcm_d("11R Roaming already in progress");
4108         (void)wifi_set_rssi_low_threshold(&wlan.rssi_low_threshold);
4109         return;
4110     }
4111 #endif
4112 
4113 #if CONFIG_11K
4114     if (network->neighbor_report_supported == true)
4115     {
4116         ret = wlan_host_11k_neighbor_req((const char *)network->ssid);
4117         if (ret == WM_SUCCESS)
4118         {
4119             wlcm_d("Sent 11K neighbor request");
4120             return;
4121         }
4122         set_rssi_threshold = true;
4123     }
4124 #endif /* CONFIG_11K */
4125 
4126 #if CONFIG_11V
4127     if (network->bss_transition_supported == true)
4128     {
4129         ret = wlan_host_11v_bss_trans_query(0x10);
4130         if (ret == WM_SUCCESS)
4131         {
4132             wlcm_d("Sent 11V bss transition query");
4133             return;
4134         }
4135         set_rssi_threshold = true;
4136     }
4137 #endif /* CONFIG_11V */
4138 
4139     if (wlan.roam_reassoc == false)
4140     {
4141         wlan.roam_reassoc = true;
4142 #if CONFIG_BG_SCAN
4143         ret = wifi_config_bgscan_and_rssi(network->ssid);
4144         if (ret == WM_SUCCESS)
4145         {
4146             wlcm_d("bgscan config successful");
4147             return;
4148         }
4149 #endif
4150         wlan.roam_reassoc = false;
4151         set_rssi_threshold = true;
4152     }
4153     else
4154     {
4155         wlcm_d("Roaming already in progress");
4156         set_rssi_threshold = true;
4157     }
4158 
4159     if (set_rssi_threshold == true)
4160     {
4161         (void)wifi_set_rssi_low_threshold(&wlan.rssi_low_threshold);
4162     }
4163 #endif /* CONFIG_ROAMING */
4164 }
4165 #endif
4166 #endif
4167 
4168 #if (CONFIG_11K) || (CONFIG_11V)
wlan_sort_nlist_channels(wlan_nlist_report_param * pnlist_rep_param)4169 static void wlan_sort_nlist_channels(wlan_nlist_report_param *pnlist_rep_param)
4170 {
4171     t_u8 i, j;
4172 
4173     /* Bubble sort */
4174     for (i = 0; i < pnlist_rep_param->num_channels; i++)
4175     {
4176         for (j = 1; j < pnlist_rep_param->num_channels - i; j++)
4177         {
4178             if ((t_u8)pnlist_rep_param->channels[j - 1] > (t_u8)pnlist_rep_param->channels[j])
4179             {
4180                 SWAP_U8(pnlist_rep_param->channels[j - 1], pnlist_rep_param->channels[j]);
4181             }
4182         }
4183     }
4184 }
4185 
4186 #if CONFIG_WPA_SUPP
4187 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
wlan_11k_roam()4188 int wlan_11k_roam()
4189 {
4190     int ret;
4191     struct wlan_network *network = &wlan.networks[wlan.cur_network_idx];
4192     wlan_nlist_report_param *pnlist_rep_param = (wlan_nlist_report_param *)&wlan.nlist_rep_param;
4193     t_u8 i;
4194     wlan_scan_params_v2_t params;
4195     struct netif *netif = net_get_sta_interface();
4196 
4197     if (is_state(CM_STA_IDLE))
4198     {
4199         (void)PRINTF("Station is not connected\r\n");
4200         return -WM_FAIL;
4201     }
4202 
4203     memset(&params, 0x00, sizeof(wlan_scan_params_v2_t));
4204 
4205     memcpy(params.ssid, network->ssid, strlen(network->ssid));
4206     params.is_ssid = 1;
4207 
4208     params.num_channels = pnlist_rep_param->num_channels;
4209 
4210     for (i = 0; i < pnlist_rep_param->num_channels; i++)
4211     {
4212         params.chan_list[i].chan_number = (t_u8)pnlist_rep_param->channels[i];
4213         params.chan_list[i].scan_type   = MLAN_SCAN_TYPE_ACTIVE;
4214         params.chan_list[i].scan_time   = 60;
4215 
4216     }
4217 
4218     ret = wpa_supp_roam(netif, NULL);
4219     if (ret != WM_SUCCESS)
4220     {
4221         wlcm_e("wlan ft roam failed");
4222         return -WM_FAIL;
4223     }
4224 
4225     ret = wpa_supp_scan(netif, &params);
4226     if (ret != WM_SUCCESS)
4227     {
4228         wlcm_e("wlan ft roam scan failed");
4229         return -WM_FAIL;
4230     }
4231 
4232     return WM_SUCCESS;
4233 }
4234 #endif
wlcm_process_neighbor_list_report_event(struct wifi_message * msg,enum cm_sta_state * next,struct wlan_network * network)4235 static void wlcm_process_neighbor_list_report_event(struct wifi_message *msg,
4236                                                     enum cm_sta_state *next,
4237                                                     struct wlan_network *network)
4238 {
4239     // unsigned int i;
4240     int ret = WM_SUCCESS;
4241     wlan_nlist_report_param *pnlist_rep_param = (wlan_nlist_report_param *)&wlan.nlist_rep_param;
4242 
4243     if (is_state(CM_STA_IDLE) || (pnlist_rep_param == NULL))
4244     {
4245         wlcm_d("ignoring neighbor list report event in idle state");
4246         return;
4247     }
4248 
4249 #if CONFIG_11K
4250     if (pnlist_rep_param->nlist_mode == WLAN_NLIST_11K)
4251     {
4252         wlan.neighbor_req = false;
4253         (void)OSA_TimerDeactivate((osa_timer_handle_t)wlan.neighbor_req_timer);
4254     }
4255 #endif
4256 
4257     wlan_sort_nlist_channels(pnlist_rep_param);
4258 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
4259     ret = wlan_11k_roam();
4260 #endif
4261     if (ret != WM_SUCCESS)
4262     {
4263         wlcm_e("11k roam failed");
4264     }
4265 
4266     memset(&wlan.nlist_rep_param, 0x00, sizeof(wlan_nlist_report_param));
4267 }
4268 #else
wlcm_process_neighbor_list_report_event(struct wifi_message * msg,enum cm_sta_state * next,struct wlan_network * network)4269 static void wlcm_process_neighbor_list_report_event(struct wifi_message *msg,
4270                                                     enum cm_sta_state *next,
4271                                                     struct wlan_network *network)
4272 {
4273     unsigned int i;
4274     int ret;
4275     wlan_scan_channel_list_t chan_list[MAX_NUM_CHANS_IN_NBOR_RPT];
4276     t_u8 *bssid                               = NULL;
4277     wlan_nlist_report_param *pnlist_rep_param = (wlan_nlist_report_param *)msg->data;
4278 
4279     wlan.roam_reassoc = false;
4280 
4281     if (is_state(CM_STA_IDLE) || (pnlist_rep_param == NULL))
4282     {
4283         wlcm_d("ignoring neighbor list report event in idle state");
4284         return;
4285     }
4286 
4287 #if CONFIG_11K
4288     if (pnlist_rep_param->nlist_mode == WLAN_NLIST_11K)
4289     {
4290         wlan.neighbor_req = false;
4291         (void)OSA_TimerDeactivate((osa_timer_handle_t)wlan.neighbor_req_timer);
4292     }
4293 #endif
4294 
4295 #if CONFIG_11V
4296     if ((pnlist_rep_param->nlist_mode == WLAN_NLIST_11V) || (pnlist_rep_param->nlist_mode == WLAN_NLIST_11V_PREFERRED))
4297     {
4298         wlan.neighbor_req = false;
4299         (void)OSA_TimerDeactivate((osa_timer_handle_t)wlan.neighbor_req_timer);
4300     }
4301 #endif
4302 
4303     wlan_sort_nlist_channels(pnlist_rep_param);
4304     memcpy(&wlan.nlist_rep_param, pnlist_rep_param, sizeof(wlan_nlist_report_param));
4305 
4306 #if CONFIG_11V
4307     if (pnlist_rep_param->nlist_mode == WLAN_NLIST_11V_PREFERRED)
4308     {
4309         bssid = pnlist_rep_param->bssid;
4310     }
4311 #endif
4312 
4313     for (i = 0; i < pnlist_rep_param->num_channels; i++)
4314     {
4315         chan_list[i].chan_number = (t_u8)pnlist_rep_param->channels[i];
4316         chan_list[i].scan_type   = MLAN_SCAN_TYPE_ACTIVE;
4317         chan_list[i].scan_time   = 60;
4318         chan_list[i].radio_type  = (chan_list[i].chan_number > 14) ? HostCmd_SCAN_RADIO_TYPE_A : HostCmd_SCAN_RADIO_TYPE_BG;
4319     }
4320 
4321 #if CONFIG_11R
4322     wlan.ft_bss = false;
4323     if ((network->ft_psk | network->ft_1x | network->ft_sae) == 1U)
4324     {
4325         wlan.ft_bss = true;
4326     }
4327 #endif
4328     wlan.roam_reassoc = true;
4329     ret = wifi_send_scan_cmd((t_u8)BSS_INFRASTRUCTURE, bssid, network->ssid, 1, pnlist_rep_param->num_channels,
4330                              chan_list, 0,
4331 #if CONFIG_SCAN_WITH_RSSIFILTER
4332                              0,
4333 #endif
4334 #if CONFIG_SCAN_CHANNEL_GAP
4335                              scan_channel_gap,
4336 #endif
4337                              false, false);
4338     if (ret != WM_SUCCESS)
4339     {
4340         wlcm_e("neighbor list scan failed");
4341 #if CONFIG_11R
4342         wlan.ft_bss = false;
4343 #endif
4344         wlan.roam_reassoc = false;
4345     }
4346 
4347     if (pnlist_rep_param != NULL)
4348     {
4349 #if !CONFIG_MEM_POOLS
4350         OSA_MemoryFree((void *)pnlist_rep_param);
4351 #else
4352         OSA_MemoryPoolFree(buf_128_MemoryPool, pnlist_rep_param);
4353 #endif
4354     }
4355 }
4356 #endif
4357 #endif
4358 
4359 #if CONFIG_11R
4360 #if CONFIG_WPA_SUPP
4361 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
wlan_ft_roam(const t_u8 * bssid,const t_u8 channel)4362 int wlan_ft_roam(const t_u8 *bssid, const t_u8 channel)
4363 {
4364     int ret;
4365     wlan_scan_params_v2_t params;
4366     t_u8 curr_bss[MLAN_MAC_ADDR_LENGTH] = {0};
4367 
4368     struct netif *netif = net_get_sta_interface();
4369 
4370     if (is_state(CM_STA_IDLE))
4371     {
4372         (void)PRINTF("Station is not connected\r\n");
4373         return -WM_FAIL;
4374     }
4375     if (wlan.roam_reassoc == true)
4376     {
4377         (void)PRINTF("Roaming already in progress\r\n");
4378         return WM_SUCCESS;
4379     }
4380 
4381     wlan.roam_reassoc = false;
4382 
4383     memset(&params, 0x00, sizeof(wlan_scan_params_v2_t));
4384 
4385     if (bssid)
4386     {
4387         if (wlan.running && is_sta_associated())
4388         {
4389             memcpy(curr_bss, wlan.networks[wlan.cur_network_idx].bssid, MLAN_MAC_ADDR_LENGTH);
4390             if(memcmp(curr_bss, bssid, MLAN_MAC_ADDR_LENGTH) == 0)
4391             {
4392                 (void)PRINTF("Already connected to this BSS. Skip roaming.\r\n");
4393                 return WM_SUCCESS;
4394             }
4395         }
4396         memcpy(params.bssid, bssid, MLAN_MAC_ADDR_LENGTH);
4397         params.is_bssid = 1;
4398     }
4399 
4400     if (channel)
4401     {
4402         params.num_channels = 1;
4403         params.chan_list[0].chan_number = channel;
4404     }
4405 
4406     ret = wpa_supp_roam(netif, (t_u8 *)bssid);
4407     if (ret != WM_SUCCESS)
4408     {
4409         wlcm_d("wlan ft roam failed");
4410         return -WM_FAIL;
4411     }
4412 
4413     ret = wpa_supp_scan(netif, &params);
4414     if (ret != WM_SUCCESS)
4415     {
4416         wlcm_d("wlan ft roam scan failed");
4417         return -WM_FAIL;
4418     }
4419 
4420     wlan.roam_reassoc = true;
4421 
4422     (void)OSA_TimerActivate((osa_timer_handle_t)wlan.ft_roam_timer);
4423 
4424     (void)PRINTF("Started FT Roaming...\r\n");
4425 
4426     return WM_SUCCESS;
4427 }
4428 #endif
4429 #else
wlan_ft_roam(const t_u8 * bssid,const t_u8 channel)4430 int wlan_ft_roam(const t_u8 *bssid, const t_u8 channel)
4431 {
4432     struct wlan_network *network = &wlan.networks[wlan.cur_network_idx];
4433 
4434     if (is_state(CM_STA_IDLE))
4435     {
4436         (void)PRINTF("Station is not connected\r\n");
4437         return -WM_FAIL;
4438     }
4439 
4440     if ((network->ft_psk | network->ft_1x | network->ft_sae) == 1U)
4441     {
4442         int ret;
4443         wlan_scan_channel_list_t chan_list;
4444 
4445         chan_list.chan_number = channel;
4446         chan_list.scan_type   = MLAN_SCAN_TYPE_ACTIVE;
4447         chan_list.scan_time   = 120;
4448 
4449         wlan.ft_bss       = true;
4450         wlan.roam_reassoc = true;
4451         ret               = wifi_send_scan_cmd((t_u8)BSS_INFRASTRUCTURE, bssid, network->ssid, 1, 1, &chan_list, 0,
4452 #if CONFIG_SCAN_WITH_RSSIFILTER
4453                                  0,
4454 #endif
4455 #if CONFIG_SCAN_CHANNEL_GAP
4456                                  scan_channel_gap,
4457 #endif
4458                                  false, false);
4459         if (ret != WM_SUCCESS)
4460         {
4461             wlcm_e("wlan ft roam scan failed");
4462             wlan.ft_bss       = false;
4463             wlan.roam_reassoc = false;
4464             return -WM_FAIL;
4465         }
4466 
4467         return WM_SUCCESS;
4468     }
4469     else
4470     {
4471         (void)PRINTF("Current associated AP do not support FT BSS transition\r\n");
4472     }
4473 
4474     return -WM_FAIL;
4475 }
4476 #endif
4477 #endif
4478 
4479 #if CONFIG_ECSA
wlan_check_valid_channel_operclass(t_u8 channel,t_u8 oper_class)4480 static int wlan_check_valid_channel_operclass(t_u8 channel, t_u8 oper_class)
4481 {
4482     int ret = 0;
4483     mlan_ioctl_req req;
4484     mlan_ds_misc_cfg *misc = NULL;
4485     mlan_status status     = MLAN_STATUS_SUCCESS;
4486 
4487     (void)memset(&req, 0x00, sizeof(mlan_ioctl_req));
4488 
4489     misc = OSA_MemoryAllocate(sizeof(mlan_ds_misc_cfg));
4490     if (misc == NULL)
4491     {
4492         return -WM_FAIL;
4493     }
4494 
4495     req.bss_index                       = MLAN_BSS_ROLE_UAP;
4496     req.pbuf                            = (t_u8 *)misc;
4497     misc->sub_command                   = MLAN_OID_MISC_OPER_CLASS_CHECK;
4498     req.req_id                          = MLAN_IOCTL_MISC_CFG;
4499     req.action                          = MLAN_ACT_GET;
4500     misc->param.bw_chan_oper.oper_class = oper_class;
4501     misc->param.bw_chan_oper.channel    = channel;
4502 
4503     status = wlan_ops_uap_ioctl(mlan_adap, &req);
4504     if (status != MLAN_STATUS_SUCCESS)
4505     {
4506         PRINTM(MERROR, "Failed to get operclass\n");
4507         OSA_MemoryFree(misc);
4508         return -WM_FAIL;
4509     }
4510 
4511     OSA_MemoryFree(misc);
4512 
4513     return ret;
4514 }
4515 
wlan_set_uap_ecsa_cfg(t_u8 block_tx,t_u8 oper_class,t_u8 channel,t_u8 switch_count,t_u8 band_width,t_u8 ecsa)4516 static int wlan_set_uap_ecsa_cfg(
4517     t_u8 block_tx, t_u8 oper_class, t_u8 channel, t_u8 switch_count, t_u8 band_width, t_u8 ecsa)
4518 {
4519     mlan_private *pmpriv = (mlan_private *)mlan_adap->priv[1];
4520     bool block_tx_flag   = (1 == block_tx) ? true : false;
4521     int ret = WM_SUCCESS;
4522 
4523     if (wlan_11h_radar_detect_required(pmpriv, channel))
4524     {
4525         wlcm_e("Please set non-dfs channel");
4526         return -WM_FAIL;
4527     }
4528 
4529     if ((channel > MAX_CHANNELS_BG) && ISSUPP_NO5G(mlan_adap->fw_cap_ext))
4530     {
4531         wlcm_e("Not support 5G, please set 2G channel");
4532         return -WM_FAIL;
4533     }
4534 
4535     if (wlan_check_channel_by_region_table(pmpriv, channel) == MFALSE)
4536     {
4537         (void)PRINTF("uAP target channel not allowed\n\r");
4538         return -WM_FAIL;
4539     }
4540 
4541     if (is_uap_started() && (!is_sta_connected()))
4542     {
4543         if (oper_class)
4544         {
4545             if (wlan_check_valid_channel_operclass(channel, oper_class))
4546             {
4547                 wlcm_e("Wrong channel switch parameters!");
4548                 return -EINVAL;
4549             }
4550         }
4551 
4552         set_ecsa_block_tx_flag(block_tx_flag);
4553 
4554         if (0 != switch_count)
4555         {
4556             set_ecsa_block_tx_time(switch_count);
4557             ret = wifi_set_ecsa_cfg(block_tx, oper_class, channel, switch_count, band_width, ecsa);
4558         }
4559         else
4560         {
4561             ret = wifi_set_action_ecsa_cfg(block_tx, oper_class, channel, switch_count);
4562         }
4563 
4564         if(WM_SUCCESS != ret)
4565         {
4566             set_ecsa_block_tx_flag(false);
4567         }
4568 
4569         return ret;
4570     }
4571     else
4572     {
4573         wlcm_e("uap isn't up");
4574         return -WM_FAIL;
4575     }
4576 }
4577 
wlan_switch_to_nondfs_channel(void)4578 static void wlan_switch_to_nondfs_channel(void)
4579 {
4580     mlan_private *pmpriv = (mlan_private *)mlan_adap->priv[1];
4581     t_u8 uap_channel     = 0;
4582     t_u8 block_tx        = 1;
4583     t_u8 oper_class      = 0;
4584     t_u8 switch_count    = DEF_SWITCH_COUNT;
4585     t_u8 band_width      = 0;
4586     t_u8 ecsa            = MTRUE;
4587 
4588     if (is_uap_started())
4589     {
4590 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
4591         uap_channel = mlan_adap->priv[1]->uap_channel;
4592 #else
4593         uap_channel = (t_u8)wlan.networks[wlan.cur_uap_network_idx].channel;
4594 #endif
4595 
4596         if (MTRUE == wlan_11h_radar_detect_required(pmpriv, uap_channel))
4597         {
4598             if (MLAN_STATUS_SUCCESS == wlan_get_non_dfs_chan(pmpriv, &uap_channel))
4599             {
4600                 wlan_set_uap_ecsa_cfg(block_tx, oper_class, uap_channel, switch_count, band_width, ecsa);
4601             }
4602             else
4603             {
4604                 return;
4605             }
4606         }
4607     }
4608 }
4609 #endif
4610 
4611 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
wlcm_process_link_loss_event(struct wifi_message * msg,enum cm_sta_state * next,struct wlan_network * network)4612 static void wlcm_process_link_loss_event(struct wifi_message *msg,
4613                                          enum cm_sta_state *next,
4614                                          struct wlan_network *network)
4615 {
4616     /* We're being informed about an asynchronous link loss (ex: beacon loss
4617      * counter detected that the AP is gone, security failure, etc). If we
4618      * were connected, we need to report this event and proceed to idle.
4619      * If we were in the middle of a connection attempt, we need to treat
4620      * this as a connection attempt failure via do_connect_fail() and
4621      * proceed accordingly.
4622      */
4623 #if CONFIG_WMSTATS
4624     g_wm_stats.wm_lloss++;
4625 #endif /* CONFIG_WMSTATS */
4626     if (is_state(CM_STA_IDLE))
4627     {
4628         wlcm_d("ignoring link loss event in idle state");
4629         return;
4630     }
4631 
4632     void *if_handle = NULL;
4633     /* On Link loss, we need to take down the interface. */
4634     if (network->type == WLAN_BSS_TYPE_STA)
4635     {
4636         if_handle = net_get_mlan_handle();
4637     }
4638 
4639     if (if_handle != NULL)
4640     {
4641         /* Forcefully stop dhcp on given interface.
4642          * net_interface_dhcp_stop internally does nothing
4643          * if dhcp client is not started.
4644          */
4645         net_interface_dhcp_stop(if_handle);
4646         net_interface_down(if_handle);
4647     }
4648 
4649     /* If we were connected and lost the link, we must report that now and
4650      * return to CM_STA_IDLE
4651      */
4652     if (is_state(CM_STA_CONNECTED))
4653     {
4654         if (is_user_scanning() != 0)
4655         {
4656             wlan.sta_return_to = CM_STA_IDLE;
4657         }
4658         else
4659         {
4660             *next = CM_STA_IDLE;
4661         }
4662         wlan.sta_state      = CM_STA_IDLE;
4663         wlan.sta_ipv4_state = CM_STA_IDLE;
4664 #if CONFIG_IPV6
4665         wlan.sta_ipv6_state = CM_STA_IDLE;
4666 #endif
4667         if ((int)msg->data == IEEEtypes_REASON_MIC_FAILURE)
4668         {
4669             /* In case of a LINK loss because of bad MIC
4670              * failure, directly send a deauth.
4671              */
4672             wlcm_d(
4673                 "Sending deauth because of"
4674                 " successive bad MIC failures");
4675             (void)wlan_disconnect();
4676             *next = CM_STA_IDLE;
4677         }
4678 
4679         CONNECTION_EVENT(WLAN_REASON_LINK_LOST, NULL);
4680 #if CONFIG_ECSA
4681         wrapper_clear_media_connected_event();
4682         wlan_switch_to_nondfs_channel();
4683 #endif
4684     }
4685     else
4686     {
4687         /* Stop the dhcp timer first after link lost occurs, as the dhcp timer
4688          * callback may lead to that the connection state is out-of-sync with FW */
4689         net_stop_dhcp_timer();
4690 
4691         /* we were attempting a connection and lost the link,
4692          * so treat this as a connection attempt failure
4693          */
4694         switch ((int)msg->data)
4695         {
4696             case IEEEtypes_REASON_4WAY_HANDSHK_TIMEOUT:
4697                 do_connect_failed(WLAN_REASON_NETWORK_AUTH_FAILED);
4698                 break;
4699             default:
4700                 do_connect_failed(WLAN_REASON_NETWORK_AUTH_FAILED);
4701                 break;
4702         }
4703         *next = wlan.sta_state;
4704     }
4705 #if CONFIG_WPA2_ENTP
4706     if (wlan_get_prov_session() == PROV_ENTP_SESSION_ATTEMPT)
4707     {
4708         wpa2_tls_cleanup(network, false);
4709     }
4710 #endif
4711 #if CONFIG_P2P
4712     wifi_wfd_event(false, false, NULL);
4713 #endif
4714 
4715 #if !CONFIG_WPA_SUPP
4716     if (wlan.reassoc_control)
4717     {
4718         wlcm_request_reconnect(next, network);
4719     }
4720 #endif
4721 }
4722 #endif
4723 
4724 #if CONFIG_WLAN_BRIDGE
wlcm_process_network_switch_event(struct wifi_message * msg,enum cm_sta_state * next,struct wlan_network * network)4725 static void wlcm_process_network_switch_event(struct wifi_message *msg,
4726                                               enum cm_sta_state *next,
4727                                               struct wlan_network *network)
4728 {
4729     Event_AutoLink_SW_Node_t *pnewNode = (Event_AutoLink_SW_Node_t *)msg->data;
4730 
4731     char *p = OSA_MemoryAllocate(pnewNode->len_ssid + 1);
4732 
4733     /*print new network info*/
4734     (void)PRINTF("\r\nBssid=");
4735     extern void print_mac(const char *mac);
4736     print_mac((char *)pnewNode->peer_mac_addr);
4737     if (p)
4738     {
4739         (void)memcpy((void *)p, (const void *)pnewNode->ssid, pnewNode->len_ssid);
4740         (void)PRINTF("\r\nSsid=%s\r\n", p);
4741         OSA_MemoryFree(p);
4742     }
4743     (void)PRINTF("channel=%d,chanBand=%d,chanWidth=%d,chan2Offset=%d,scanMode=%d", pnewNode->chanBand.chanNum,
4744                  pnewNode->chanBand.bandConfig.chanBand, pnewNode->chanBand.bandConfig.chanWidth,
4745                  pnewNode->chanBand.bandConfig.chan2Offset, pnewNode->chanBand.bandConfig.scanMode);
4746     (void)PRINTF("\r\nSecurityType=%d", pnewNode->secutype);
4747     (void)PRINTF("\r\nmcstCipher=%d", pnewNode->mcstcipher);
4748     (void)PRINTF("\r\nucstCipher=%d\r\n", pnewNode->ucstcipher);
4749 
4750     OSA_MemoryFree(msg->data);
4751 }
4752 #endif
4753 
wlcm_process_disassoc_event(struct wifi_message * msg,enum cm_sta_state * next,struct wlan_network * network)4754 static void wlcm_process_disassoc_event(struct wifi_message *msg, enum cm_sta_state *next, struct wlan_network *network)
4755 {
4756     /* We're being informed about an asynchronous disassociation from AP
4757      * As were in the middle of a connection attempt, we need to treat
4758      * this as a connection attempt failure via do_connect_fail() and
4759      * proceed accordingly.
4760      */
4761 #if CONFIG_WPA2_ENTP
4762     if (wlan_get_prov_session() == PROV_ENTP_SESSION_ATTEMPT)
4763     {
4764         wpa2_tls_cleanup(network, false);
4765     }
4766     else if (wlan_get_prov_session() == PROV_NON_SESSION_ATTEMPT && is_state(CM_STA_CONNECTED) &&
4767              network->security.pmk_valid == true &&
4768              (network->security.type == WLAN_SECURITY_EAP_TLS || network->security.type == WLAN_SECURITY_EAP_PEAP_MSCHAPV2)
4769             )
4770     {
4771         /* Connects with security EAP_TLS, if AP reset PMK and send disassoc, STA clear pmk_valid/pmk and enter "PROV_ENTP_SESSION_ATTEMPT" again */
4772         network->security.pmk_valid = false;
4773         memset(network->security.pmk, 0, WLAN_PMK_LENGTH);
4774     }
4775 #endif
4776 #if CONFIG_P2P
4777     wifi_wfd_event(false, false, NULL);
4778 #endif
4779     if (is_user_scanning() != 0)
4780     {
4781         wlan.sta_return_to = CM_STA_IDLE;
4782     }
4783     *next          = CM_STA_IDLE;
4784     wlan.sta_state = CM_STA_IDLE;
4785 
4786     do_connect_failed(WLAN_REASON_NETWORK_AUTH_FAILED);
4787 
4788     if (wlan.reassoc_control)
4789     {
4790         wlcm_request_reconnect(next, network);
4791     }
4792 }
4793 
wlcm_process_deauthentication_event(struct wifi_message * msg,enum cm_sta_state * next,struct wlan_network * network)4794 static void wlcm_process_deauthentication_event(struct wifi_message *msg,
4795                                                 enum cm_sta_state *next,
4796                                                 struct wlan_network *network)
4797 {
4798 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
4799     struct netif *netif = net_get_sta_interface();
4800     struct wifi_connect_req_params params = {0};
4801 
4802     supplicant_get_wifi_conn_params(net_if_get_device((void *)netif), &params);
4803 #endif
4804 
4805 #if CONFIG_WPA2_ENTP
4806     if (wlan_get_prov_session() == PROV_ENTP_SESSION_ATTEMPT)
4807     {
4808         wpa2_tls_cleanup(network, false);
4809     }
4810 #endif
4811 #if CONFIG_P2P
4812     wifi_wfd_event(false, false, NULL);
4813 #endif
4814 #if CONFIG_WPA_SUPP
4815     if (
4816 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
4817         ((params.security == WIFI_SECURITY_TYPE_SAE)
4818         || (params.security == WIFI_SECURITY_TYPE_SAE_H2E)
4819         || (params.security == WIFI_SECURITY_TYPE_SAE_AUTO))
4820 #else
4821         (network->security.type == WLAN_SECURITY_WPA3_SAE ||
4822          network->security.type == WLAN_SECURITY_WPA3_SAE_EXT_KEY)
4823 #endif
4824          && (msg->reason == WLAN_REASON_PREV_AUTH_NOT_VALID ||
4825         msg->reason == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
4826         msg->reason == WLAN_REASON_INVALID_IE))
4827     {
4828         /*
4829          *  Clear pmksa cache in case AP wants to redo sae auth.
4830          *  But send successful assoc resp and send deauth right after.
4831          *  Then we will always using pmksa cache if it exists.
4832          */
4833         wlan_pmksa_flush();
4834     }
4835 
4836 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
4837     wlan_handle_disconnect_event(mlan_adap->priv[0]);
4838 
4839 #if UAP_SUPPORT
4840     if(is_uap_started())
4841     {
4842         while (is_sta_connected())
4843             OSA_TimeDelay(100);
4844         wlan_switch_to_nondfs_channel();
4845     }
4846 #endif
4847 #endif
4848 #endif
4849 }
4850 
wlcm_process_net_dhcp_config(struct wifi_message * msg,enum cm_sta_state * next,struct wlan_network * network)4851 static void wlcm_process_net_dhcp_config(struct wifi_message *msg,
4852                                          enum cm_sta_state *next,
4853                                          struct wlan_network *network)
4854 {
4855     /* We have received a response from the embedded DHCP client.  If
4856      * successful, we now have a DHCP lease and can update our IP address
4857      * information and procceed to the CM_STA_CONNECTED state, otherwise we
4858      * failed to obtain a DHCP lease and report that we failed to connect.
4859      *
4860      * This event is only relevant if we are attempting a connection and
4861      * are in the CM_STA_OBTAINING_ADDRESS state (due to configuring the TCP
4862      * stack with DHCP), otherwise we ignore it. */
4863 
4864     void *if_handle = NULL;
4865     // wakelock_put(WL_ID_CONNECT);
4866     // wlan.connect_wakelock_taken = false;
4867     if (wlan.sta_ipv4_state == CM_STA_OBTAINING_ADDRESS)
4868     {
4869         if (wlan.connect_wakelock_taken)
4870         {
4871 #if CONFIG_HOST_SLEEP
4872             wakelock_put();
4873 #endif
4874             wlan.connect_wakelock_taken = false;
4875         }
4876         if (msg->reason != WIFI_EVENT_REASON_SUCCESS)
4877         {
4878 #if CONFIG_WMSTATS
4879             g_wm_stats.wm_dhcp_fail++;
4880 #endif /* CONFIG_WMSTATS */
4881             wlcm_d("got event: DHCP failure");
4882 #if CONFIG_IPV6
4883             if (wlan.sta_ipv6_state != CM_STA_CONNECTED)
4884             {
4885 #endif
4886 #if CONFIG_WLAN_FAST_PATH
4887                 /* Mark the fast path cache invalid. */
4888                 wlan.auth_cache_valid      = false;
4889                 wlan.fast_path_cache_valid = false;
4890 #endif /* CONFIG_WLAN_FAST_PATH */
4891 
4892                 do_connect_failed(WLAN_REASON_ADDRESS_FAILED);
4893 
4894                 if (wlan.reassoc_control)
4895                 {
4896                     wlcm_request_reconnect(next, network);
4897                 }
4898 
4899                 *next = wlan.sta_state;
4900 #if CONFIG_IPV6
4901             }
4902             else if (wlan.sta_ipv6_state == CM_STA_CONNECTED)
4903             {
4904                 wlan.sta_state = CM_STA_CONNECTED;
4905                 *next          = CM_STA_CONNECTED;
4906                 if (network->type == WLAN_BSS_TYPE_STA)
4907                 {
4908                     if_handle = net_get_mlan_handle();
4909                 }
4910 
4911                 net_interface_up(if_handle);
4912                 mlan_adap->skip_dfs = false;
4913                 CONNECTION_EVENT(WLAN_REASON_SUCCESS, NULL);
4914             }
4915             else
4916             {
4917                 /* Do Nothing */
4918             }
4919 #endif
4920             return;
4921         }
4922         char ip[16];
4923 #if CONFIG_WMSTATS
4924         g_wm_stats.wm_dhcp_succ++;
4925 #endif /* CONFIG_WMSTATS */
4926         wlcm_d("got event: DHCP success");
4927         net_stop_dhcp_timer();
4928         net_configure_dns((struct net_ip_config *)&network->ip, network->role);
4929         if (network->type == WLAN_BSS_TYPE_STA)
4930         {
4931             if_handle = net_get_mlan_handle();
4932         }
4933 #if CONFIG_P2P
4934         else if (network->type == WLAN_BSS_TYPE_WIFIDIRECT)
4935             if_handle = net_get_wfd_handle();
4936 #endif /* CONFIG_P2P */
4937         (void)net_get_if_addr((struct net_ip_config *)&network->ip, if_handle);
4938         // net_inet_ntoa(network->ip.ipv4.address, ip);
4939         wlan.sta_state      = CM_STA_CONNECTED;
4940         *next               = CM_STA_CONNECTED;
4941         wlan.sta_ipv4_state = CM_STA_CONNECTED;
4942 
4943 #if CONFIG_WPS2
4944         wlan_set_prov_session(PROV_NON_SESSION_ATTEMPT);
4945 #endif
4946         if (wlan.reassoc_control && wlan.reassoc_request)
4947         {
4948             wlan.reassoc_count   = 0;
4949             wlan.reassoc_request = false;
4950         }
4951         mlan_adap->skip_dfs = false;
4952         CONNECTION_EVENT(WLAN_REASON_SUCCESS, &ip);
4953 
4954 #if CONFIG_P2P
4955         wifi_wfd_event(false, false, (void *)1);
4956 #endif
4957     }
4958     else
4959     {
4960         wlcm_d("got event from dhcp client %d", msg->reason);
4961         if (msg->reason != WIFI_EVENT_REASON_SUCCESS)
4962         {
4963 #if CONFIG_WMSTATS
4964             g_wm_stats.wm_leas_fail++;
4965 #endif      /* CONFIG_WMSTATS */
4966             /* There was some problem with dhcp lease
4967              * renewal, so just disconnect from wlan.
4968              */
4969 
4970 #if CONFIG_IPV6
4971             /* Do not disconnect-connect if the STA interface has
4972              * obtained IPv6 address */
4973             if (wlan.sta_ipv6_state != CM_STA_CONNECTED)
4974             {
4975 #endif
4976                 if (wlan.sta_ipv4_state == CM_STA_CONNECTED)
4977                 {
4978                     wlcm_d("Lease renewal failed");
4979                     CONNECTION_EVENT(WLAN_REASON_ADDRESS_FAILED, NULL);
4980                     *next               = CM_STA_AUTHENTICATED;
4981                     wlan.sta_ipv4_state = CM_STA_AUTHENTICATED;
4982                 }
4983 #if CONFIG_IPV6
4984             }
4985 #endif
4986             return;
4987         }
4988 #if CONFIG_WMSTATS
4989         g_wm_stats.wm_leas_succ++;
4990 #endif /* CONFIG_WMSTATS */
4991         /* Successful in getting ip address, so update
4992          * local wlan-info params */
4993         wlcm_d("update wlan-info params");
4994         if (network->type == WLAN_BSS_TYPE_STA)
4995         {
4996             if_handle = net_get_mlan_handle();
4997         }
4998 #if CONFIG_P2P
4999         else if (network->type == WLAN_BSS_TYPE_WIFIDIRECT)
5000         {
5001             if_handle = net_get_wfd_handle();
5002         }
5003         else
5004         {
5005             /*Do nothing*/
5006         }
5007 #endif /* CONFIG_P2P */
5008         (void)net_get_if_addr((struct net_ip_config *)&network->ip, if_handle);
5009         CONNECTION_EVENT(WLAN_REASON_ADDRESS_SUCCESS, NULL);
5010         wlan.sta_state      = CM_STA_CONNECTED;
5011         *next               = CM_STA_CONNECTED;
5012         wlan.sta_ipv4_state = CM_STA_CONNECTED;
5013     }
5014 }
5015 
5016 #if CONFIG_IPV6
wlcm_process_net_ipv6_config(struct wifi_message * msg,enum cm_sta_state * next,struct wlan_network * network)5017 static void wlcm_process_net_ipv6_config(struct wifi_message *msg,
5018                                          enum cm_sta_state *next,
5019                                          struct wlan_network *network)
5020 {
5021     void *if_handle = net_get_mlan_handle();
5022     int i, found = 0;
5023     if (network->type != WLAN_BSS_TYPE_STA || (if_handle == NULL))
5024     {
5025         return;
5026     }
5027 
5028     net_get_if_ipv6_addr((struct net_ip_config *)&network->ip, if_handle);
5029     for (i = 0; i < CONFIG_MAX_IPV6_ADDRESSES && i < network->ip.ipv6_count; i++)
5030     {
5031         if ((network->ip.ipv6[i].addr_state == NET_ADDR_TENTATIVE) ||
5032                 (network->ip.ipv6[i].addr_state == NET_ADDR_PREFERRED))
5033         {
5034             found++;
5035             /* Not considering link-local address as of now */
5036             if (wlan.sta_ipv6_state != CM_STA_CONNECTED)
5037             {
5038                 wlan.sta_ipv6_state = CM_STA_CONNECTED;
5039 
5040                 if (wlan.reassoc_control && wlan.reassoc_request)
5041                 {
5042                     wlan.reassoc_count   = 0;
5043                     wlan.reassoc_request = false;
5044                 }
5045             }
5046         }
5047     }
5048 
5049     if (wlan.sta_ipv6_state >= CM_STA_OBTAINING_ADDRESS && !found)
5050     {
5051         /* If the state is either obtaining address or connected and
5052          * if none of the IP addresses is preferred, then
5053          * change/maintain the state to obtaining address */
5054         wlan.sta_ipv6_state = CM_STA_OBTAINING_ADDRESS;
5055     }
5056 }
5057 #endif /* CONFIG_IPV6 */
5058 
wlan_rx_mgmt_indication(const enum wlan_bss_type bss_type,const uint32_t mgmt_subtype_mask,int (* rx_mgmt_callback)(const enum wlan_bss_type bss_type,const wlan_mgmt_frame_t * frame,const size_t len))5059 int wlan_rx_mgmt_indication(const enum wlan_bss_type bss_type,
5060                             const uint32_t mgmt_subtype_mask,
5061                             int (*rx_mgmt_callback)(const enum wlan_bss_type bss_type,
5062                                                     const wlan_mgmt_frame_t *frame,
5063                                                     const size_t len))
5064 {
5065 #ifdef MGMT_RX
5066     if (mgmt_subtype_mask)
5067         rx_mgmt_register_callback(rx_mgmt_callback);
5068     else
5069         rx_mgmt_deregister_callback();
5070 #endif
5071 
5072     return wifi_set_rx_mgmt_indication(bss_type, mgmt_subtype_mask);
5073 }
5074 
5075 #if CONFIG_WPA_SUPP
5076 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
wlcm_process_scan_failed()5077 static void wlcm_process_scan_failed()
5078 {
5079     if (wlan.pending_disconnect_request == true)
5080     {
5081         wlan_disconnect();
5082     }
5083 }
5084 #endif
5085 #define IEEEtypes_REASON_DEAUTH_LEAVING     3
5086 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
wlcm_process_disconnected()5087 static void wlcm_process_disconnected()
5088 {
5089     (void)wifi_event_completion(WIFI_EVENT_LINK_LOSS, WIFI_EVENT_REASON_FAILURE, (void *)IEEEtypes_REASON_DEAUTH_LEAVING);
5090 }
5091 #endif
5092 #if CONFIG_11K
5093 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
wlan_parse_neighbor_report_response(const char * nbr_response,wlan_rrm_neighbor_report_t * nbr_rpt)5094 static void wlan_parse_neighbor_report_response(const char *nbr_response, wlan_rrm_neighbor_report_t *nbr_rpt)
5095 {
5096     char event[32] = {0};
5097     char bssid[32] = {0};
5098     char info[32]  = {0};
5099     int op_class,channel,phy_type;
5100 
5101     if (!nbr_response || nbr_response[0] == '\0')
5102     {
5103         wlcm_e("11K: Neighbor response is NULL");
5104         return;
5105     }
5106     else if (!nbr_rpt)
5107     {
5108         wlcm_e("11K: Input Neighbor report is NULL");
5109         return;
5110     }
5111 
5112 // Sample Response Pattern
5113 //<3>RRM-NEIGHBOR-REP-RECEIVED bssid=ec:aa:a0:81:7f:20 info=0x1801 op_class=0 chan=153 phy_type=1 lci=0100080010000000000000000000000000000000000406000000000000060101 civic=02000b0000ed000000
5114 
5115     if (sscanf(nbr_response,"%s bssid=%s info=%s op_class=%d chan=%d phy_type=%d", event, bssid, info, &op_class, &channel, &phy_type) == 6)
5116     {
5117         int i;
5118         int match  = 0;
5119         size_t len = 0;
5120 
5121         // Check if the bssid is already present in list
5122 
5123         for (i = 0; i < nbr_rpt->neighbor_cnt; i++)
5124         {
5125             if (strncmp((const char *)nbr_rpt->neighbor_ap[i].bssid, bssid, 32) == 0)
5126             {
5127                 match = 1;
5128                 break;
5129             }
5130 
5131             if (nbr_rpt->neighbor_ap[i].channel == channel)
5132             {
5133                 match = 1;
5134                 break;
5135             }
5136         }
5137 
5138         if (!match && (nbr_rpt->neighbor_cnt < MAX_NEIGHBOR_AP_LIMIT))
5139         {
5140             strncpy((char *)nbr_rpt->neighbor_ap[nbr_rpt->neighbor_cnt].bssid, bssid, sizeof(nbr_rpt->neighbor_ap->bssid));
5141             len = strlen(bssid);
5142             nbr_rpt->neighbor_ap[nbr_rpt->neighbor_cnt].bssid[len] = (t_u8)'\0';
5143 
5144             strncpy((char *)nbr_rpt->neighbor_ap[nbr_rpt->neighbor_cnt].bssidInfo, info, sizeof(nbr_rpt->neighbor_ap->bssidInfo));
5145             len = strlen(info);
5146             nbr_rpt->neighbor_ap[nbr_rpt->neighbor_cnt].bssidInfo[len] = (t_u8)'\0';
5147 
5148             nbr_rpt->neighbor_ap[nbr_rpt->neighbor_cnt].channel = channel;
5149             wlan.nlist_rep_param.channels[wlan.nlist_rep_param.num_channels] = channel;
5150             nbr_rpt->neighbor_ap[nbr_rpt->neighbor_cnt].op_class = op_class;
5151             nbr_rpt->neighbor_ap[nbr_rpt->neighbor_cnt].phy_type = phy_type;
5152             //nbr_rpt->neighbor_ap[nbr_rpt->neighbor_cnt].freq = wifi_getRadioFrequencyFromChannel(channel);
5153 
5154             wlcm_d("RRM Neighbor[%d] - Bssid = %s , Info = %s, op_class=%d, Channel = %d, Phy_Type = %d, Freq=%d ", nbr_rpt->neighbor_cnt, nbr_rpt->neighbor_ap[nbr_rpt->neighbor_cnt].bssid, nbr_rpt->neighbor_ap[nbr_rpt->neighbor_cnt].bssidInfo, nbr_rpt->neighbor_ap[nbr_rpt->neighbor_cnt].op_class, nbr_rpt->neighbor_ap[nbr_rpt->neighbor_cnt].channel,nbr_rpt->neighbor_ap[nbr_rpt->neighbor_cnt].phy_type,nbr_rpt->neighbor_ap[nbr_rpt->neighbor_cnt].freq);
5155 
5156             nbr_rpt->neighbor_cnt += 1;
5157             wlan.nlist_rep_param.num_channels += 1;
5158 
5159             wlan.nlist_rep_param.nlist_mode = WLAN_NLIST_11K;
5160         }
5161         else if (match)
5162         {
5163             wlcm_d("BSSID already present in neighbor list, Skipping %s ",bssid);
5164         }
5165         else
5166         {
5167             wlcm_d("Maximum neighbors added to list, Skipping.");
5168         }
5169     }
5170     else
5171     {
5172         wlcm_d("Failed to Parse Neighbor Report - Skipping entry\n");
5173     }
5174 }
5175 #endif
5176 #endif
5177 
5178 #if CONFIG_WPA_SUPP
5179 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
wlcm_process_add_unspecified_network(const char * name)5180 static int wlcm_process_add_unspecified_network(const char *name)
5181 {
5182     int ret, i;
5183     struct wlan_network *network;
5184     size_t len       = 0;
5185     const char *ssid = "w";
5186 
5187     network = OSA_MemoryAllocate(sizeof(struct wlan_network));
5188 
5189     if (network == NULL)
5190     {
5191         wlcm_d("%s: Failed to alloc wlan_network network", __func__);
5192         return -WM_FAIL;
5193     }
5194 
5195     (void)memset(network, 0, sizeof(struct wlan_network));
5196     (void)memcpy(network->name, name, strlen(name));
5197     len = strlen(name);
5198     network->name[len] = '\0';
5199     (void)memcpy(network->ssid, ssid, strlen(ssid));
5200 
5201     network->ip.ipv4.addr_type = ADDR_TYPE_DHCP;
5202 
5203     ret = wlan_add_network(network);
5204 
5205     OSA_MemoryFree(network);
5206 
5207     if (ret != WM_SUCCESS)
5208     {
5209         wlcm_e("Failed to add wps network");
5210         return ret;
5211     }
5212     for (i = 0; i < ARRAY_SIZE(wlan.networks); i++)
5213     {
5214         if (wlan.networks[i].name[0] != '\0' && strlen(wlan.networks[i].name) == len &&
5215                 !strncmp(wlan.networks[i].name, name, len))
5216         {
5217             wlan.cur_network_idx = i;
5218             break;
5219         }
5220     }
5221     return WM_SUCCESS;
5222 }
5223 #endif
5224 #endif
5225 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
wpa_supplicant_msg_cb(const char * buf,size_t len)5226 static void wpa_supplicant_msg_cb(const char *buf, size_t len)
5227 {
5228     const char *s;
5229     unsigned char is_11n_enabled;
5230     int ret;
5231 #if CONFIG_WIFI_NM_HOSTAPD_AP
5232     struct netif *netif = net_get_uap_interface();
5233 #endif
5234     struct netif *sta_netif = net_get_sta_interface();
5235     struct wlan_network *network = &wlan.networks[wlan.cur_network_idx];
5236 
5237     wlcm_d("%s: %s", __func__, buf);
5238 
5239     if (strstr(buf, WPA_EVENT_SCAN_FAILED))
5240     {
5241         wlcm_process_scan_failed();
5242     }
5243     else if (strstr(buf, WPA_EVENT_DISCONNECTED))
5244     {
5245         wlcm_process_disconnected();
5246         wlan_handle_disconnect_event(mlan_adap->priv[0]);
5247     }
5248     else if (strstr(buf, WPA_EVENT_NETWORK_NOT_FOUND))
5249     {
5250         wlcm_d("No suitable network was found");
5251 
5252         if (wlan.roam_reassoc == true)
5253         {
5254 #if (CONFIG_11K) || (CONFIG_11V) || (CONFIG_ROAMING)
5255             (void)wifi_set_rssi_low_threshold(&wlan.rssi_low_threshold);
5256             wlan.roam_reassoc = false;
5257 #endif
5258         }
5259         else
5260         {
5261             wlan.scan_count++;
5262 
5263             do_connect_failed(WLAN_REASON_NETWORK_NOT_FOUND);
5264 
5265             if (wlan.scan_count > WLAN_RESCAN_LIMIT)
5266             {
5267                 wlan.cur_network_idx = -1;
5268                 (void)wpa_supp_disable(sta_netif, network);
5269             }
5270         }
5271     }
5272     else if (strstr(buf, WPA_EVENT_AUTH_REJECT))
5273     {
5274         wlcm_d("Authentication rejected during connection attempt");
5275         if ((wlan.roam_reassoc != true) && (!(strlen(buf) > strlen(WPA_EVENT_AUTH_REJECT))))
5276         {
5277             (void)send_user_request(CM_STA_USER_REQUEST_CONNECT, wlan.cur_network_idx);
5278         }
5279         wlan.roam_reassoc = false;
5280     }
5281 #if CONFIG_WPA_SUPP_AP
5282     else if (strstr(buf, AP_STA_CONNECTED))
5283     {
5284         wlcm_d("AP: Station connected");
5285         t_u8 addr[MLAN_MAC_ADDR_LENGTH];
5286 
5287         s = strchr(buf, ' ');
5288         if (s == NULL)
5289             return;
5290 
5291         if (hwaddr_aton(s + 1, addr))
5292             return;
5293 
5294         ret = wpa_supp_get_sta_info(netif, addr, &is_11n_enabled);
5295         if (ret != 0)
5296             return;
5297 
5298         wifi_uap_client_assoc(addr, is_11n_enabled);
5299 
5300         CONNECTION_EVENT(WLAN_REASON_UAP_CLIENT_CONN, addr);
5301     }
5302     if (strstr(buf, AP_STA_DISCONNECTED))
5303     {
5304         wlcm_d("AP: Station dis-connected");
5305 
5306         wifi_uap_client_disassoc_t disassoc_resp;
5307 
5308         disassoc_resp.reason_code = 0;
5309 
5310         s = strchr(buf, ' ');
5311         if (s == NULL)
5312             return;
5313 
5314         if (hwaddr_aton(s + 1, disassoc_resp.sta_addr))
5315             return;
5316 
5317         wifi_uap_client_deauth(disassoc_resp.sta_addr);
5318 
5319         CONNECTION_EVENT(WLAN_REASON_UAP_CLIENT_DISSOC, (void *)&disassoc_resp);
5320     }
5321 #endif
5322 #if CONFIG_WPA_SUPP_WPS
5323     else if (strstr(buf, WPS_EVENT_OVERLAP))
5324     {
5325         wlcm_d("WPS overlap detected in PBC mode");
5326     }
5327     else if (strstr(buf, WPS_EVENT_AP_AVAILABLE_PBC))
5328     {
5329         wlcm_d("Available WPS AP with active PBC found in scan results");
5330     }
5331     else if (strstr(buf, WPS_EVENT_AP_AVAILABLE_PIN))
5332     {
5333         wlcm_d("Available WPS AP with recently selected PIN registrar found in scan results");
5334     }
5335     else if (strstr(buf, WPS_EVENT_AP_AVAILABLE))
5336     {
5337         wlcm_d("Available WPS AP found in scan results");
5338     }
5339     else if (strstr(buf, WPS_EVENT_CRED_RECEIVED))
5340     {
5341         wlcm_d("A new credential received");
5342     }
5343     else if (strstr(buf, WPS_EVENT_DISABLE))
5344     {
5345         wlcm_d("PBC mode was disabled");
5346         wlan.wps_session_attempt = 0;
5347     }
5348     else if (strstr(buf, WPS_EVENT_ACTIVE))
5349     {
5350         wlcm_d("PBC mode was activated");
5351         wlan.wps_session_attempt = 1;
5352     }
5353     else if (strstr(buf, WPS_EVENT_PIN_ACTIVE))
5354     {
5355         wlcm_d("PIN mode was activated");
5356         wlan.wps_session_attempt = 1;
5357     }
5358     else if (strstr(buf, WPS_EVENT_TIMEOUT))
5359     {
5360         wlcm_d("WPS enrollment attempt timed out and was terminated");
5361         wlan.wps_session_attempt = 0;
5362     }
5363     else if (strstr(buf, WPS_EVENT_FAIL))
5364     {
5365         wlcm_d("WPS registration failed after M2/M2D");
5366         wlan.wps_session_attempt = 0;
5367     }
5368     else if (strstr(buf, WPS_EVENT_SUCCESS))
5369     {
5370         wlcm_d("WPS registration completed successfully");
5371         if (wlan.wps_session_attempt)
5372         {
5373             if (wlcm_process_add_unspecified_network("wps_network") == WM_SUCCESS)
5374             {
5375                 wlan.wps_session_attempt = 0;
5376             }
5377         }
5378     }
5379     else
5380 #endif
5381 #if CONFIG_11K
5382         if (strstr(buf, RRM_EVENT_NEIGHBOR_REP_RXED))
5383     {
5384         wlcm_d("11K RRM event neighbor response received");
5385         wlan_parse_neighbor_report_response(buf, &wlan.nbr_rpt);
5386     }
5387 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
5388     else if (strstr(buf, RRM_EVENT_NEIGHBOR_REP_COMPLETED))
5389     {
5390         if (wlan.nbr_rpt.neighbor_cnt != 0U)
5391         {
5392             memset(&wlan.nbr_rpt, 0x00, sizeof(wlan_rrm_neighbor_report_t));
5393             (void)wifi_event_completion(WIFI_EVENT_NLIST_REPORT, WIFI_EVENT_REASON_SUCCESS, NULL);
5394         }
5395     }
5396 #endif
5397     else if (strstr(buf, RRM_EVENT_NEIGHBOR_REP_FAILED))
5398     {
5399         wlcm_d("11K RRM event neighbor report request failed");
5400     }
5401     else
5402 #endif
5403 #if CONFIG_WPA_SUPP_DPP
5404         if (strstr(buf, DPP_EVENT_CONF_RECEIVED))
5405     {
5406         if (!is_uap_started())
5407         {
5408             wlcm_d("DPP starts to new a network profile");
5409             (void)wlcm_process_add_unspecified_network("dpp_network");
5410         }
5411     }
5412     else if (strstr(buf, DPP_EVENT_CONFOBJ_AKM))
5413     {
5414         unsigned int network_idx = -1;
5415 
5416         if (is_uap_started())
5417         {
5418             network_idx = wlan.cur_uap_network_idx;
5419         }
5420         else
5421         {
5422             network_idx = wlan.cur_network_idx;
5423         }
5424         /* Only for STA associate */
5425         mlan_adap->priv[0]->is_dpp_connect = MTRUE;
5426         if (network_idx < WLAN_MAX_KNOWN_NETWORKS)
5427         {
5428             struct wlan_network_security *security = &(wlan.networks[network_idx].security);
5429             const char *pos = buf + sizeof(DPP_EVENT_CONFOBJ_AKM) - 1;
5430             security->pmk_valid = false;
5431             security->type = WLAN_SECURITY_NONE;
5432             if (memcmp("psk", pos, strlen("psk")) == 0)
5433             {
5434                 security->type     = WLAN_SECURITY_WPA2;
5435                 security->key_mgmt = WLAN_KEY_MGMT_PSK;
5436             }
5437             else if (memcmp("sae", pos, strlen("sae")) == 0)
5438             {
5439                 security->type     = WLAN_SECURITY_WPA3_SAE;
5440                 security->key_mgmt = WLAN_KEY_MGMT_SAE;
5441             }
5442             else if ((memcmp("psk-sae", pos, strlen("psk-sae")) == 0) ||
5443                      (memcmp("psk+sae", pos, strlen("psk+sae")) == 0))
5444             {
5445                 security->type = WLAN_SECURITY_WPA2_WPA3_SAE_MIXED;
5446                 security->key_mgmt = WLAN_KEY_MGMT_PSK | WLAN_KEY_MGMT_SAE;
5447             }
5448             else if ((memcmp("sae-dpp", pos, strlen("sae-dpp")) == 0) ||
5449                      (memcmp("dpp+sae", pos, strlen("dpp+sae")) == 0))
5450             {
5451                 security->type        = WLAN_SECURITY_WPA3_SAE;
5452                 security->key_mgmt = WLAN_KEY_MGMT_DPP | WLAN_KEY_MGMT_SAE;
5453             }
5454             else if ((memcmp("psk-sae-dpp", pos, strlen("psk-sae-dpp")) == 0) ||
5455                      (memcmp("dpp+psk+sae", pos, strlen("dpp+psk+sae")) == 0))
5456             {
5457                 security->type = WLAN_SECURITY_WPA2_WPA3_SAE_MIXED;
5458                 security->key_mgmt = WLAN_KEY_MGMT_DPP | WLAN_KEY_MGMT_PSK | WLAN_KEY_MGMT_SAE;
5459             }
5460             else if (memcmp("dpp", pos, strlen("dpp")) == 0)
5461             {
5462                 security->type = WLAN_SECURITY_DPP;
5463                 security->key_mgmt = WLAN_KEY_MGMT_DPP;
5464             }
5465             else if (memcmp("dot1x", pos, strlen("dot1x")) == 0)
5466             {
5467 #if CONFIG_EAP_TLS
5468                 security->type = WLAN_SECURITY_EAP_TLS_SHA256;
5469 #endif
5470                 security->key_mgmt = WLAN_KEY_MGMT_IEEE8021X;
5471             }
5472             else
5473             {
5474                 wlcm_e("DPP AKM type(%s) unknown!", pos);
5475                 return ;
5476             }
5477             security->pairwise_cipher = 0x10; /* CCMP */
5478             security->mfpc = 1;
5479             security->mfpr = 1;
5480             wlan_set_pmfcfg(security->mfpc, security->mfpr);
5481         }
5482     }
5483     else if (strstr(buf, DPP_EVENT_CONFOBJ_SSID))
5484     {
5485         unsigned int network_idx = -1;
5486 
5487         if (is_uap_started())
5488         {
5489             network_idx = wlan.cur_uap_network_idx;
5490         }
5491         else
5492         {
5493             network_idx = wlan.cur_network_idx;
5494         }
5495         if (network_idx < WLAN_MAX_KNOWN_NETWORKS)
5496         {
5497             const char *pos = buf + sizeof(DPP_EVENT_CONFOBJ_SSID) - 1;
5498             if (strlen(pos) < IEEEtypes_SSID_SIZE)
5499             {
5500                 (void)memcpy(wlan.networks[network_idx].ssid, pos, strlen(pos));
5501             }
5502         }
5503     }
5504     else if (strstr(buf, DPP_EVENT_CONFOBJ_PASS))
5505     {
5506         unsigned int network_idx = -1;
5507 
5508         if (is_uap_started())
5509         {
5510             network_idx = wlan.cur_uap_network_idx;
5511         }
5512         else
5513         {
5514             network_idx = wlan.cur_network_idx;
5515         }
5516         if (network_idx < WLAN_MAX_KNOWN_NETWORKS)
5517         {
5518             char psk[WLAN_PSK_MAX_LENGTH];
5519             unsigned int hex_len = 0;
5520             struct wlan_network_security *security = &(wlan.networks[network_idx].security);
5521             const char *pos = buf + sizeof(DPP_EVENT_CONFOBJ_PASS) - 1;
5522 
5523             hex_len = strlen(pos);
5524             if (hex_len <= (WLAN_PSK_MAX_LENGTH * 2))
5525             {
5526                 memset(psk, 0, sizeof(psk));
5527                 hexstr2bin(pos, (unsigned char *)psk, hex_len/2);
5528                 security->psk_len = strlen(psk);
5529                 (void)strcpy(security->psk, psk);
5530                 security->password_len = strlen(psk);
5531                 (void)strcpy(security->password, psk);
5532             }
5533         }
5534     }
5535     else if (strstr(buf, DPP_EVENT_CONFOBJ_PSK))
5536     {
5537         unsigned int network_idx = -1;
5538 
5539         if (is_uap_started())
5540         {
5541             network_idx = wlan.cur_uap_network_idx;
5542         }
5543         else
5544         {
5545             network_idx = wlan.cur_network_idx;
5546         }
5547         if (network_idx < WLAN_MAX_KNOWN_NETWORKS)
5548         {
5549             char pmk[WLAN_PMK_LENGTH];
5550             unsigned int hex_len = 0;
5551             struct wlan_network_security *security = &(wlan.networks[network_idx].security);
5552             const char *pos = buf + sizeof(DPP_EVENT_CONFOBJ_PSK) - 1;
5553 
5554             hex_len = strlen(pos);
5555             if (hex_len <= (WLAN_PMK_LENGTH * 2))
5556             {
5557                 memset(pmk, 0, sizeof(pmk));
5558                 hexstr2bin(pos, (unsigned char *)pmk, hex_len/2);
5559                 security->pmk_valid = true;
5560                 (void)memcpy(security->pmk, pmk, WLAN_PMK_LENGTH);
5561             }
5562         }
5563     }
5564     else if (strstr(buf, DPP_EVENT_CONNECTOR))
5565     {
5566         unsigned int network_idx = -1;
5567 
5568         if (is_uap_started())
5569         {
5570             network_idx = wlan.cur_uap_network_idx;
5571         }
5572         else
5573         {
5574             network_idx = wlan.cur_network_idx;
5575         }
5576         if (network_idx < WLAN_MAX_KNOWN_NETWORKS)
5577         {
5578             struct wlan_network_security *security = &(wlan.networks[network_idx].security);
5579             const char *pos = buf + sizeof(DPP_EVENT_CONNECTOR) - 1;
5580 
5581             if (security->dpp_connector)
5582             {
5583                 OSA_MemoryFree(security->dpp_connector);
5584             }
5585             security->dpp_connector = OSA_MemoryAllocate(strlen(pos) + 1);
5586             if (security->dpp_connector == NULL)
5587             {
5588                 wlcm_e("Allocate %s memory failed!", DPP_EVENT_CONNECTOR);
5589                 return ;
5590             }
5591             (void)memcpy(security->dpp_connector, pos, strlen(pos));
5592         }
5593     }
5594     else if (strstr(buf, DPP_EVENT_C_SIGN_KEY))
5595     {
5596         unsigned int network_idx = -1;
5597 
5598         if (is_uap_started())
5599         {
5600             network_idx = wlan.cur_uap_network_idx;
5601         }
5602         else
5603         {
5604             network_idx = wlan.cur_network_idx;
5605         }
5606         if (network_idx < WLAN_MAX_KNOWN_NETWORKS)
5607         {
5608             struct wlan_network_security *security = &(wlan.networks[network_idx].security);
5609             const char *pos = buf + sizeof(DPP_EVENT_C_SIGN_KEY) - 1;
5610 
5611             if (security->dpp_c_sign_key)
5612             {
5613                 OSA_MemoryFree(security->dpp_c_sign_key);
5614             }
5615             security->dpp_c_sign_key = OSA_MemoryAllocate(strlen(pos) + 1);
5616             if (security->dpp_c_sign_key == NULL)
5617             {
5618                 wlcm_e("Allocate %s memory failed!", DPP_EVENT_C_SIGN_KEY);
5619                 return ;
5620             }
5621             (void)memcpy(security->dpp_c_sign_key, pos, strlen(pos));
5622         }
5623     }
5624     else if (strstr(buf, DPP_EVENT_NET_ACCESS_KEY))
5625     {
5626         unsigned int network_idx = -1;
5627 
5628         if (is_uap_started())
5629         {
5630             network_idx = wlan.cur_uap_network_idx;
5631         }
5632         else
5633         {
5634             network_idx = wlan.cur_network_idx;
5635         }
5636         if (network_idx < WLAN_MAX_KNOWN_NETWORKS)
5637         {
5638             struct wlan_network_security *security = &(wlan.networks[network_idx].security);
5639             const char *pos = buf + sizeof(DPP_EVENT_NET_ACCESS_KEY) - 1;
5640 
5641             if (security->dpp_net_access_key)
5642             {
5643                 OSA_MemoryFree(security->dpp_net_access_key);
5644             }
5645             security->dpp_net_access_key = OSA_MemoryAllocate(strlen(pos) + 1);
5646             if (security->dpp_net_access_key == NULL)
5647             {
5648                 wlcm_e("Allocate %s memory failed!", DPP_EVENT_NET_ACCESS_KEY);
5649                 return ;
5650             }
5651             (void)memcpy(security->dpp_net_access_key, pos, strlen(pos));
5652             if (is_uap_started())
5653             {
5654                 wlcm_d("DPP reload uap");
5655                 wlan_enable_uap_11d(MTRUE);
5656                 wpa_supp_start_ap(netif, &wlan.networks[network_idx], 1);
5657             }
5658         }
5659     }
5660     else if (strstr(buf, DPP_EVENT_INTRO))
5661     {
5662         wlan_deepsleepps_on();
5663         wlan_ieeeps_on(wlan.wakeup_conditions);
5664     }
5665     else
5666 #endif
5667     {
5668     }
5669 }
5670 #endif
5671 #endif
5672 
5673 #define MAX_RETRY_TICKS 50
5674 
wlcm_process_init_params()5675 static void wlcm_process_init_params()
5676 {
5677     wlan.cm_ieeeps_configured = false;
5678 
5679     wlan.cm_deepsleepps_configured = false;
5680 
5681 #if (CONFIG_WNM_PS)
5682     wlan.cm_wnmps_configured = false;
5683     wlan.wnm_sleep_time      = 0;
5684 #endif
5685 #if (CONFIG_11K) || (CONFIG_11V)
5686     memset(&wlan.nlist_rep_param, 0x00, sizeof(wlan_nlist_report_param));
5687 #endif
5688 
5689 #if CONFIG_BG_SCAN
5690     wlan.bgscan_attempt = 0;
5691 #endif
5692 
5693     wlan.cur_network_idx     = -1;
5694     wlan.cur_uap_network_idx = -1;
5695 }
5696 
wlcm_process_init(enum cm_sta_state * next)5697 static void wlcm_process_init(enum cm_sta_state *next)
5698 {
5699     int ret;
5700     (void)ret;
5701 
5702     wlan.sta_state = CM_STA_IDLE;
5703     *next          = CM_STA_IDLE;
5704 
5705     wlcm_process_init_params();
5706 
5707 #if CONFIG_P2P
5708     /* This call is made to initiate WFD, We are not interested
5709      * in the response since mac addr is already populated in
5710      * wlan_start
5711      */
5712     wifi_get_wfd_mac_address();
5713 #endif
5714 
5715 #if OTP_CHANINFO
5716     (void)wifi_get_fw_region_and_cfp_tables();
5717 #endif
5718 
5719 #if UAP_SUPPORT
5720     (void)wifi_get_uap_max_clients(&wlan.uap_supported_max_sta_num);
5721 #endif
5722 
5723     (void)wrapper_wlan_cmd_get_hw_spec();
5724 
5725 #ifndef RW610
5726     wlan_ed_mac_ctrl_t wlan_ed_mac_ctrl = {
5727         0x01,
5728         CONFIG_NXP_WIFI_ED_OFFSET_2G
5729 #if CONFIG_5GHz_SUPPORT
5730         ,
5731         0x01,
5732         CONFIG_NXP_WIFI_ED_OFFSET_5G
5733 #endif
5734     };
5735     (void)wlan_set_ed_mac_mode(wlan_ed_mac_ctrl);
5736     (void)wlan_set_uap_ed_mac_mode(wlan_ed_mac_ctrl);
5737 #endif
5738 
5739     (void)wifi_enable_ecsa_support();
5740 
5741     //		uint16_t ant = 1; //board_antenna_select();
5742     //                if (board_antenna_switch_ctrl())
5743     //			rfctrl_set_config(ant);
5744 
5745 #ifdef STREAM_2X2
5746     uint8_t tx_antenna = 3, rx_antenna = 3;
5747 
5748     ret = wifi_set_antenna(tx_antenna, rx_antenna);
5749     if (ret != WM_SUCCESS)
5750     {
5751         wlcm_e("Failed to set antenna configuration");
5752         return;
5753     }
5754     wlcm_d("Antenna selected: tx: %d, rx:%x", tx_antenna, rx_antenna);
5755 #if 0
5756     uint16_t httxcfg = 0x1FF;
5757 
5758     ret = wifi_set_11n_cfg(httxcfg);
5759     if (ret != WM_SUCCESS) {
5760 	    wlcm_e("Failed to set 11n ht tx configuration");
5761 	    return;
5762     }
5763 #endif
5764 
5765 #if CONFIG_11AC
5766     uint32_t vhtcap     = 0x33D179B0;
5767     uint16_t tx_mcs_map = 0xfffa;
5768     uint16_t rx_mcs_map = 0xfffa;
5769 
5770     ret = wifi_set_11ac_cfg(vhtcap, tx_mcs_map, rx_mcs_map);
5771     if (ret != WM_SUCCESS)
5772     {
5773         wlcm_e("Failed to set 11ac configuration");
5774         return;
5775     }
5776 #endif
5777 #else
5778 #if defined(SD8801) || defined(SD8978) || defined(SD8987)
5779     uint32_t ant           = 1;
5780     uint16_t evaluate_time = 0x1770;
5781 
5782     ret = wifi_set_antenna(ant, evaluate_time);
5783     if (ret != WM_SUCCESS)
5784     {
5785         wlcm_d("Failed to set antenna configuration");
5786     }
5787     else
5788     {
5789         wlcm_d("Antenna selected: %d", ant);
5790     }
5791 #endif /* defined(SD8801, 8978, 8987) */
5792 #endif
5793 
5794     wifi_set_packet_retry_count(MAX_RETRY_TICKS);
5795 
5796 #if defined(SD8978) || defined(SD8987) || defined(SD8997) || defined(SD9097) || defined(SD9098) || defined(SD9177)
5797 
5798     wifi_set_txbfcap(0x19e74608);
5799 
5800     /*Enabling 20/40MHz enable(bit 1)
5801      * enabling Short GI in 40 Mhz(bit 6)
5802      * and 20MHz(bit 5),
5803      * Reserved bits to be set to 1 (Bits 2,3)*/
5804     uint16_t httxcfg = 0x6E; // TODO need to add defines for this setting
5805 
5806     ret = wlan_set_httxcfg(httxcfg);
5807     if (ret != WM_SUCCESS)
5808     {
5809         wlcm_e("Failed to set HT TX configuration");
5810         return;
5811     }
5812 
5813     wlan_uap_set_httxcfg(httxcfg);
5814 #endif
5815 
5816 #if CONFIG_11K
5817     (void)wifi_host_11k_cfg(1);
5818 #endif
5819 #if CONFIG_11K_OFFLOAD
5820     (void)wifi_11k_cfg(1);
5821     (void)wifi_11h_enable();
5822 #endif
5823 #if (CONFIG_11K) || (CONFIG_11V) || (CONFIG_1AS)
5824 #if !CONFIG_WPA_SUPP
5825     (void)wlan_rx_mgmt_indication(WLAN_BSS_TYPE_STA, WLAN_MGMT_ACTION, NULL);
5826 #endif
5827 #endif
5828 
5829 #if !CONFIG_WPA_SUPP
5830 #if CONFIG_DRIVER_MBO
5831     wifi_host_mbo_cfg(1);
5832 #endif
5833 #endif
5834 
5835 #if CONFIG_WIFI_AUTO_POWER_SAVE
5836     wlan_deepsleepps_on();
5837     wifi_set_power_save_mode();
5838     wlan_ieeeps_on(wlan.wakeup_conditions);
5839 #endif
5840 
5841     wlan_set_11d_state(WLAN_BSS_TYPE_UAP, 1);
5842     wlan_set_11d_state(WLAN_BSS_TYPE_STA, 1);
5843 }
5844 
wlcm_process_net_if_config_event(struct wifi_message * msg,enum cm_sta_state * next)5845 static void wlcm_process_net_if_config_event(struct wifi_message *msg, enum cm_sta_state *next)
5846 {
5847 #if CONFIG_WPA_SUPP
5848 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
5849     struct netif *netif = net_get_sta_interface();
5850 #endif
5851     int ret;
5852 #endif
5853 
5854     if (wlan.sta_state != CM_STA_INITIALIZING)
5855     {
5856         wlcm_d("ignoring TCP configure response");
5857         return;
5858     }
5859 
5860     if (msg->reason != WIFI_EVENT_REASON_SUCCESS)
5861     {
5862         wlcm_e("Interfaces init failed");
5863         CONNECTION_EVENT(WLAN_REASON_INITIALIZATION_FAILED, NULL);
5864         /* stay here until user re-inits */
5865         *next = CM_STA_INITIALIZING;
5866         return;
5867     }
5868 #if CONFIG_WPA_SUPP
5869 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
5870     ret = wpa_supp_init(wpa_supplicant_msg_cb);
5871     if (ret != 0)
5872     {
5873         wlcm_e("wpa_supp_init failed. status code %d", ret);
5874         return;
5875     }
5876 #endif
5877     ret = wifi_supp_init();
5878     if (ret != 0)
5879     {
5880         wlcm_e("wifi_supp_init failed. status code %d", ret);
5881         return;
5882     }
5883     wifi_set_rx_mgmt_indication(WLAN_BSS_ROLE_STA, WLAN_MGMT_ACTION);
5884 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
5885     ret = wpa_supp_status(netif);
5886     if (ret != 0)
5887     {
5888         wlcm_e("wpa_supp_status failed. status code %d", ret);
5889         return;
5890     }
5891 
5892     /* ret = monitor_start();
5893     if (ret != 0)
5894     {
5895         wlcm_e("monitor_start failed. status code %d", ret);
5896         return;
5897     } */
5898 #endif
5899 #endif
5900 
5901     wlcm_process_init(next);
5902 }
5903 
5904 static void wlcm_request_disconnect(enum cm_sta_state *next, struct wlan_network *curr_nw);
5905 
5906 #if (CONFIG_WIFI_IND_RESET) && (CONFIG_WIFI_IND_DNLD)
wlcm_process_fw_hang_event(struct wifi_message * msg,enum cm_sta_state * next)5907 static void wlcm_process_fw_hang_event(struct wifi_message *msg, enum cm_sta_state *next)
5908 {
5909 #if CONFIG_WPA_SUPP
5910     struct netif *netif = net_get_sta_interface();
5911 #endif
5912 
5913     (void)msg;
5914 
5915     CONNECTION_EVENT(WLAN_REASON_FW_HANG, NULL);
5916 
5917     if (wlan.sta_state > CM_STA_IDLE)
5918     {
5919 #if CONFIG_WPA_SUPP
5920         wpa_supp_disconnect(netif);
5921 #endif
5922         wlcm_request_disconnect(next, &wlan.networks[wlan.cur_network_idx]);
5923         wlan_dhcp_cleanup();
5924     }
5925 
5926 #if UAP_SUPPORT
5927     if (wlan.uap_state > CM_UAP_INITIALIZING)
5928     {
5929         (void)do_stop(&wlan.networks[wlan.cur_uap_network_idx]);
5930     }
5931 #endif
5932 }
5933 
wlcm_process_fw_reset_event(struct wifi_message * msg,enum cm_sta_state * next)5934 static void wlcm_process_fw_reset_event(struct wifi_message *msg, enum cm_sta_state *next)
5935 {
5936     (void)msg;
5937     (void)next;
5938 
5939     wlan.ind_reset = 1;
5940 
5941     wlcm_process_init(next);
5942 
5943     CONNECTION_EVENT(WLAN_REASON_FW_RESET, NULL);
5944 }
5945 #endif
5946 
5947 #if UAP_SUPPORT
5948 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
wlan_notify_uap_chan_switch(t_u8 channel)5949 static void wlan_notify_uap_chan_switch(t_u8 channel)
5950 {
5951     wifi_ecsa_info *pecsa_info = NULL;
5952 
5953 #if !CONFIG_MEM_POOLS
5954      pecsa_info = OSA_MemoryAllocate(sizeof(wifi_ecsa_info));
5955 #else
5956      pecsa_info = OSA_MemoryPoolAllocate(buf_32_MemoryPool);
5957 #endif
5958      if (!pecsa_info)
5959      {
5960          wlcm_w("No mem. Cannot process new channel from channel switch");
5961          return;
5962      }
5963      pecsa_info->bss_type    = WLAN_BSS_TYPE_UAP;
5964      pecsa_info->channel     = channel;
5965      pecsa_info->band_config = (channel > 14) ? BAND_5GHZ: BAND_2GHZ;
5966 
5967     if (wifi_event_completion(WIFI_EVENT_CHAN_SWITCH, WIFI_EVENT_REASON_SUCCESS, pecsa_info) != WM_SUCCESS)
5968     {
5969         /* If fail to send message on queue, free allocated memory ! */
5970 #if !CONFIG_MEM_POOLS
5971         OSA_MemoryFree((void *)pecsa_info);
5972 #else
5973         OSA_MemoryPoolFree(buf_32_MemoryPool, pecsa_info);
5974 #endif
5975     }
5976 }
5977 #endif
5978 
uap_state_machine(struct wifi_message * msg)5979 static enum cm_uap_state uap_state_machine(struct wifi_message *msg)
5980 {
5981     struct wlan_network *network = NULL;
5982     enum cm_uap_state next       = wlan.uap_state;
5983     void *if_handle              = NULL;
5984 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
5985     int ret                      = 0;
5986 #endif
5987 #if CONFIG_WPA_SUPP
5988     struct netif *netif = net_get_uap_interface();
5989 #endif
5990 
5991     network = &wlan.networks[wlan.cur_uap_network_idx];
5992 
5993     switch (msg->event)
5994     {
5995         case CM_UAP_USER_REQUEST_START:
5996             wlan.cur_uap_network_idx = (int)msg->data;
5997             wlan.scan_count          = 0;
5998 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
5999             (void)do_start(&wlan.networks[wlan.cur_uap_network_idx]);
6000 #endif
6001             next = wlan.uap_state;
6002             break;
6003         case CM_UAP_USER_REQUEST_STOP:
6004 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
6005             supplicant_ap_disable(net_if_get_device((void *)netif));
6006 #else
6007             if (wlan.uap_state < CM_UAP_CONFIGURED)
6008             {
6009                 break;
6010             }
6011 
6012             (void)do_stop(&wlan.networks[wlan.cur_uap_network_idx]);
6013             next = wlan.uap_state;
6014 #if CONFIG_WPA_SUPP
6015             if (wlan.status_timeout)
6016             {
6017                 OSA_TimerDeactivate((osa_timer_handle_t)wlan.supp_status_timer);
6018                 wlan.status_timeout = 0;
6019                 CONNECTION_EVENT(WLAN_REASON_UAP_START_FAILED, NULL);
6020             }
6021 #endif
6022 #endif
6023             break;
6024         case WIFI_EVENT_UAP_STARTED:
6025 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
6026             if (msg->reason == WIFI_EVENT_REASON_SUCCESS)
6027             {
6028                 t_u8 sta_channel = mlan_adap->priv[0]->curr_bss_params.bss_descriptor.channel;
6029                 t_u8 uap_channel = mlan_adap->priv[1]->uap_channel;
6030 
6031                 CONNECTION_EVENT(WLAN_REASON_UAP_SUCCESS, NULL);
6032                 if(is_sta_connected() && uap_channel != sta_channel)
6033                 {
6034                     while(!is_uap_started())
6035                         OSA_TimeDelay(10);
6036                     PRINTF("Station has connection on channel %d, switch to this channel\r\n", sta_channel);
6037                     wlan_notify_uap_chan_switch(sta_channel);
6038                 }
6039             }
6040 #else
6041             if (!is_uap_state(CM_UAP_CONFIGURED))
6042             {
6043                 wlcm_w(
6044                     "Ignoring address config event as uap not "
6045                     "in configured state");
6046                 break;
6047             }
6048 
6049             if (msg->reason == WIFI_EVENT_REASON_SUCCESS)
6050             {
6051                 if (network->type == WLAN_BSS_TYPE_UAP)
6052                 {
6053                     (void)memcpy((void *)&network->bssid[0], (const void *)&wlan.uap_mac[0], 6);
6054                     if_handle = net_get_uap_handle();
6055                 }
6056 #if CONFIG_P2P
6057                 else if (network->type == WLAN_BSS_TYPE_WIFIDIRECT)
6058                 {
6059                     (void)memcpy((void *)&network->bssid[0], (const void *)&wlan.wfd_mac[0], 6);
6060                     if_handle = net_get_wfd_handle();
6061                 }
6062 #endif /* CONFIG_P2P */
6063 #if CONFIG_WPA_SUPP
6064                 OSA_TimerDeactivate((osa_timer_handle_t)wlan.supp_status_timer);
6065                 wlan.status_timeout = 0;
6066 
6067                 wpa_supp_network_status(netif, network);
6068 #endif
6069                 ret = net_configure_address((struct net_ip_config *)&network->ip, if_handle);
6070                 if (ret != 0)
6071                 {
6072                     wlcm_e("TCP/IP stack setup failed");
6073                     CONNECTION_EVENT(WLAN_REASON_ADDRESS_FAILED, NULL);
6074                 }
6075                 else
6076                 {
6077                     next = CM_UAP_STARTED;
6078                 }
6079             }
6080             else
6081             {
6082                 CONNECTION_EVENT(WLAN_REASON_UAP_START_FAILED, NULL);
6083             }
6084 #endif
6085             break;
6086         case WIFI_EVENT_UAP_CLIENT_ASSOC:
6087 #if CONFIG_WIFI_UAP_WORKAROUND_STICKY_TIM
6088             if (network->type == WLAN_BSS_TYPE_UAP)
6089             {
6090                 wifi_uap_enable_sticky_bit(msg->data);
6091             }
6092 #endif /* CONFIG_WIFI_UAP_WORKAROUND_STICKY_TIM */
6093             CONNECTION_EVENT(WLAN_REASON_UAP_CLIENT_ASSOC, msg->data);
6094             /* This was allocated by the sender */
6095 #if !CONFIG_MEM_POOLS
6096             OSA_MemoryFree(msg->data);
6097 #else
6098             OSA_MemoryPoolFree(buf_32_MemoryPool, msg->data);
6099 #endif
6100             break;
6101         case WIFI_EVENT_UAP_CLIENT_CONN:
6102             CONNECTION_EVENT(WLAN_REASON_UAP_CLIENT_CONN, msg->data);
6103             /* This was allocated by the sender */
6104 #if !CONFIG_MEM_POOLS
6105             OSA_MemoryFree(msg->data);
6106 #else
6107             OSA_MemoryPoolFree(buf_32_MemoryPool, msg->data);
6108 #endif
6109             break;
6110         case WIFI_EVENT_UAP_CLIENT_DEAUTH:
6111 #ifdef CONFIG_WPA_SUPP_AP
6112             {
6113                 wlan_uap_client_disassoc_t *disassoc_resp = msg->data;
6114                 /* BIT 14 indicate deauth is initiated by FW */
6115                 if(!(disassoc_resp->reason_code & MBIT(14)))
6116                 {
6117                     wifi_nxp_sta_remove(disassoc_resp->sta_addr);
6118                 }
6119             }
6120 #else
6121             CONNECTION_EVENT(WLAN_REASON_UAP_CLIENT_DISSOC, msg->data);
6122 #endif
6123             /* This was allocated by the sender */
6124 #if !CONFIG_MEM_POOLS
6125             OSA_MemoryFree(msg->data);
6126 #else
6127             OSA_MemoryPoolFree(buf_32_MemoryPool, msg->data);
6128 #endif
6129             break;
6130         case WIFI_EVENT_UAP_STOPPED:
6131             CONNECTION_EVENT(WLAN_REASON_UAP_STOPPED, NULL);
6132             break;
6133         case WIFI_EVENT_UAP_NET_ADDR_CONFIG:
6134             if (!is_uap_state(CM_UAP_STARTED))
6135             {
6136                 wlcm_d(
6137                     "Ignoring address config event as uap not "
6138                     "in started state");
6139                 break;
6140             }
6141 
6142             if (msg->reason == WIFI_EVENT_REASON_SUCCESS && network->ip.ipv4.addr_type == ADDR_TYPE_STATIC)
6143             {
6144                 if (network->type == WLAN_BSS_TYPE_UAP)
6145                 {
6146                     if_handle = net_get_uap_handle();
6147                 }
6148 #if CONFIG_P2P
6149                 else if (network->type == WLAN_BSS_TYPE_WIFIDIRECT)
6150                 {
6151                     if_handle = net_get_wfd_handle();
6152                 }
6153                 else
6154                 {
6155                     /*Do Nothing*/
6156                 }
6157 #endif /* CONFIG_P2P */
6158 
6159                 (void)net_get_if_addr((struct net_ip_config *)&network->ip, if_handle);
6160                 /* UAP case set dns same as gateway */
6161                 network->ip.ipv4.dns1 = network->ip.ipv4.gw;
6162                 network->ip.ipv4.dns2 = 0;
6163 #if CONFIG_IPV6
6164                 (void)net_get_if_ipv6_addr((struct net_ip_config *)&network->ip, if_handle);
6165 #endif
6166                 next = CM_UAP_IP_UP;
6167                 CONNECTION_EVENT(WLAN_REASON_UAP_SUCCESS, NULL);
6168             }
6169             else
6170             {
6171                 CONNECTION_EVENT(WLAN_REASON_ADDRESS_FAILED, NULL);
6172                 next = CM_UAP_INITIALIZING;
6173             }
6174             break;
6175 #if CONFIG_WMM
6176         case WIFI_EVENT_UAP_TX_DATA_PAUSE:
6177             wifi_uap_handle_event_data_pause(msg->data);
6178 #if !CONFIG_MEM_POOLS
6179             OSA_MemoryFree(msg->data);
6180 #else
6181             OSA_MemoryPoolFree(buf_32_MemoryPool, msg->data);
6182 #endif
6183             break;
6184 #endif
6185         default:
6186             wlcm_w("got unknown message  UAP  : %d", msg->event);
6187             break;
6188     }
6189     return next;
6190 }
6191 #endif /* UAP_SUPPORT */
6192 
wlcm_request_scan(struct wifi_message * msg,enum cm_sta_state * next)6193 static void wlcm_request_scan(struct wifi_message *msg, enum cm_sta_state *next)
6194 {
6195     char ssid[(MLAN_MAX_SSID_LENGTH + 1) * MRVDRV_MAX_SSID_LIST_LENGTH]  = {0};
6196     uint8_t ssid_num = 0, ssid_off = 0;
6197     if (msg->data == NULL)
6198     {
6199         wlcm_w("ignoring scan request with NULL scan params");
6200         wlcm_d("releasing scan lock");
6201         (void)OSA_SemaphorePost((osa_semaphore_handle_t)wlan.scan_lock);
6202         wlan.is_scan_lock = 0;
6203         return;
6204     }
6205 
6206     wlan_scan_params_v2_t *wlan_scan_param = (wlan_scan_params_v2_t *)msg->data;
6207 
6208     if ((!is_scanning_allowed())
6209 #if CONFIG_WPS2
6210         || (wlan_get_prov_session() != PROV_NON_SESSION_ATTEMPT)
6211 #endif
6212     )
6213     {
6214         wlcm_w("ignoring scan result in invalid state");
6215         wlcm_d("releasing scan lock");
6216         /* Free allocated wifi scan parameters */
6217 #if !CONFIG_MEM_POOLS
6218         OSA_MemoryFree(wlan_scan_param);
6219 #else
6220         OSA_MemoryPoolFree(buf_128_MemoryPool, wlan_scan_param);
6221 #endif
6222         (void)OSA_SemaphorePost((osa_semaphore_handle_t)wlan.scan_lock);
6223         wlan.is_scan_lock = 0;
6224         return;
6225     }
6226 
6227     memcpy(ssid+ssid_off, wlan_scan_param->ssid[0], strlen(wlan_scan_param->ssid[0]));
6228     ssid_off += strlen(wlan_scan_param->ssid[0]);
6229     ssid[ssid_off] = '\0';
6230     ssid_off++;
6231     ssid_num++;
6232 #if CONFIG_COMBO_SCAN
6233     if (strlen(wlan_scan_param->ssid[1]))
6234     {
6235         memcpy(ssid+ssid_off, wlan_scan_param->ssid[1], strlen(wlan_scan_param->ssid[1]));
6236         ssid_off += strlen(wlan_scan_param->ssid[1]);
6237         ssid[ssid_off] = '\0';
6238         ssid_num++;
6239     }
6240 #endif
6241 #if CONFIG_SCAN_CHANNEL_GAP
6242     if (is_uap_started() || is_sta_connected())
6243         wlan_scan_param->scan_chan_gap = scan_channel_gap;
6244     else
6245         wlan_scan_param->scan_chan_gap = 0;
6246 #endif
6247 
6248 #if CONFIG_WPA_SUPP
6249 #if CONFIG_11K
6250     if (wlan.enable_11k == 1U)
6251     {
6252         wm_wifi.wpa_supp_scan = true;
6253         wm_wifi.external_scan = true;
6254     }
6255 #endif
6256 #endif
6257 
6258     wlcm_d("initiating wlan-scan (return to %s)", dbg_sta_state_name(wlan.sta_state));
6259 
6260     wlan.scan_cb       = (int (*)(unsigned int count))(wlan_scan_param->cb);
6261 
6262     int ret = wifi_send_scan_cmd((t_u8)g_wifi_scan_params.bss_type, wlan_scan_param->bssid,
6263                                  ssid, ssid_num,
6264                                  wlan_scan_param->num_channels, wlan_scan_param->chan_list, wlan_scan_param->num_probes,
6265 #if CONFIG_SCAN_WITH_RSSIFILTER
6266                                  wlan_scan_param->rssi_threshold,
6267 #endif
6268 #if CONFIG_SCAN_CHANNEL_GAP
6269                                  wlan_scan_param->scan_chan_gap,
6270 #endif
6271                                  false, false);
6272     if (ret != WM_SUCCESS)
6273     {
6274         wlcm_e("wifi send scan cmd failed");
6275         (void)wlan.scan_cb(0);
6276         wlan.scan_cb = NULL;
6277         *next = wlan.sta_state;
6278 #if CONFIG_WPA_SUPP
6279         wm_wifi.wpa_supp_scan = false;
6280         wm_wifi.external_scan = false;
6281 #endif
6282         wlcm_d("releasing scan lock");
6283         (void)OSA_SemaphorePost((osa_semaphore_handle_t)wlan.scan_lock);
6284         wlan.is_scan_lock = 0;
6285     }
6286     else
6287     {
6288         wlan.sta_return_to = wlan.sta_state;
6289         *next              = CM_STA_SCANNING_USER;
6290     }
6291     /* Free allocated wifi scan parameters */
6292 #if !CONFIG_MEM_POOLS
6293     OSA_MemoryFree(wlan_scan_param);
6294 #else
6295     OSA_MemoryPoolFree(buf_128_MemoryPool, wlan_scan_param);
6296 #endif
6297 }
6298 
wlcm_deinit(int action)6299 static void wlcm_deinit(int action)
6300 {
6301     if ((wlan.status != WLCMGR_ACTIVATED) && (wlan.status != WLCMGR_INIT_DONE))
6302     {
6303         wlcm_e("cannot deinit wlcmgr. unexpected status: %d\n\r", wlan.status);
6304         return;
6305     }
6306 
6307     wifi_scan_stop();
6308     wifi_deinit();
6309 
6310     wlan.status = WLCMGR_INACTIVE;
6311 }
6312 
wlcm_request_disconnect(enum cm_sta_state * next,struct wlan_network * curr_nw)6313 static void wlcm_request_disconnect(enum cm_sta_state *next, struct wlan_network *curr_nw)
6314 {
6315     void *if_handle = NULL;
6316     /* On disconnect request, we need to take down the interface.
6317        This is required to fix bug # 52964 */
6318     if (curr_nw->type == WLAN_BSS_TYPE_STA)
6319     {
6320         if_handle = net_get_mlan_handle();
6321     }
6322 #if CONFIG_P2P
6323     else if (curr_nw->type == WLAN_BSS_TYPE_WIFIDIRECT)
6324     {
6325         if_handle = net_get_wfd_handle();
6326     }
6327     else
6328     {
6329         /* Do Nothing */
6330     }
6331 #endif /* CONFIG_P2P */
6332     if (if_handle == NULL)
6333     {
6334 #if CONFIG_NCP_BRIDGE
6335         CONNECTION_EVENT(WLAN_REASON_USER_DISCONNECT, (void *)(-WM_FAIL));
6336 #endif
6337 #if CONFIG_HOST_SLEEP
6338         wakelock_put();
6339 #endif
6340         wlcm_w("No interface is up\r\n");
6341         return;
6342     }
6343     net_stop_dhcp_timer();
6344     /* Forcefully stop dhcp on given interface.
6345      * net_interface_dhcp_stop internally does nothing
6346      * if dhcp client is not started.
6347      */
6348     net_interface_dhcp_stop(if_handle);
6349     net_interface_down(if_handle);
6350 
6351     if (
6352 #if CONFIG_WPS2
6353         (wlan_get_prov_session() == PROV_NON_SESSION_ATTEMPT) &&
6354 #endif
6355         (wlan.sta_state < CM_STA_IDLE || is_state(CM_STA_IDLE)))
6356     {
6357 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
6358 #if CONFIG_WPA_SUPP
6359         if (wlan.status_timeout)
6360         {
6361             OSA_TimerDeactivate((osa_timer_handle_t)wlan.supp_status_timer);
6362             wlan.status_timeout = 0;
6363             CONNECTION_EVENT(WLAN_REASON_USER_DISCONNECT, NULL);
6364         }
6365         else
6366 #endif
6367 #endif
6368         {
6369 #if CONFIG_NCP_BRIDGE
6370             CONNECTION_EVENT(WLAN_REASON_USER_DISCONNECT, (void *)(-WM_FAIL));
6371 #endif
6372         }
6373         if (wlan.connect_wakelock_taken)
6374         {
6375             wlan.connect_wakelock_taken = false;
6376         }
6377 
6378 #if CONFIG_HOST_SLEEP
6379         wakelock_put();
6380 #endif
6381         wlan.cur_network_idx = -1;
6382         return;
6383     }
6384 
6385     if (is_user_scanning() && wlan.sta_return_to != CM_STA_IDLE)
6386     {
6387         /* we're in a user scan: we'll need to disconnect and
6388          * return to the idle state once this scan completes */
6389         if (wlan.sta_return_to >= CM_STA_ASSOCIATING)
6390         {
6391 #if !CONFIG_WPA_SUPP
6392             (void)wifi_deauthenticate((uint8_t *)curr_nw->bssid);
6393 #endif
6394             wlan.sta_return_to  = CM_STA_IDLE;
6395             *next               = CM_STA_IDLE;
6396             wlan.sta_ipv4_state = CM_STA_IDLE;
6397 #if CONFIG_IPV6
6398             wlan.sta_ipv6_state = CM_STA_IDLE;
6399 #endif
6400         }
6401     }
6402     else if (wlan.sta_state >= CM_STA_ASSOCIATING)
6403     {
6404         /* if we aren't idle or user scanning,
6405          * we'll need to actually
6406          * disconnect */
6407 #if !CONFIG_WPA_SUPP
6408         (void)wifi_deauthenticate((uint8_t *)curr_nw->bssid);
6409 #endif
6410         if (wlan.is_scan_lock)
6411         {
6412             wlcm_d("releasing scan lock");
6413             (void)OSA_SemaphorePost((osa_semaphore_handle_t)wlan.scan_lock);
6414             wlan.is_scan_lock = 0;
6415         }
6416         wlan.sta_return_to  = CM_STA_IDLE;
6417         wlan.sta_state      = CM_STA_IDLE;
6418         *next               = CM_STA_IDLE;
6419         wlan.sta_ipv4_state = CM_STA_IDLE;
6420 #if CONFIG_IPV6
6421         wlan.sta_ipv6_state = CM_STA_IDLE;
6422 #endif
6423     }
6424     else if (wlan.sta_state == CM_STA_SCANNING)
6425     {
6426         wlan.sta_state      = CM_STA_IDLE;
6427         *next               = CM_STA_IDLE;
6428         wlan.sta_ipv4_state = CM_STA_IDLE;
6429 #if CONFIG_IPV6
6430         wlan.sta_ipv6_state = CM_STA_IDLE;
6431 #endif
6432     }
6433     else
6434     { /* Do Nothing */
6435     }
6436     wlan.cur_network_idx =-1;
6437 
6438 #if CONFIG_WPS2
6439     if (wlan_get_prov_session() == PROV_WPS_SESSION_ATTEMPT)
6440     {
6441         CONNECTION_EVENT(WLAN_REASON_WPS_DISCONNECT, NULL);
6442         wlan_set_prov_session(PROV_NON_SESSION_ATTEMPT);
6443     }
6444     else
6445 #endif
6446 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
6447 #if CONFIG_WPA_SUPP
6448         if (wlan.status_timeout)
6449     {
6450 #if CONFIG_WPA_SUPP_WPS
6451         wlan.wps_session_attempt = 0;
6452 #endif
6453         OSA_TimerDeactivate((osa_timer_handle_t)wlan.supp_status_timer);
6454         wlan.status_timeout = 0;
6455     }
6456 #endif
6457 #endif
6458     CONNECTION_EVENT(WLAN_REASON_USER_DISCONNECT, NULL);
6459 
6460     if (wlan.reassoc_control && wlan.reassoc_request)
6461     {
6462         wlan.scan_count      = WLAN_RESCAN_LIMIT;
6463         wlan.reassoc_count   = WLAN_RECONNECT_LIMIT;
6464         wlan.reassoc_request = false;
6465     }
6466 
6467     if (wlan.connect_wakelock_taken)
6468     {
6469 #if CONFIG_HOST_SLEEP
6470         wakelock_put();
6471 #endif
6472         wlan.connect_wakelock_taken = false;
6473     }
6474 #if CONFIG_HOST_SLEEP
6475      /* The wakelock will be taken when user issue disconnect command and this time flag wlan.connect_wakelock_taken is false.
6476       * Release wakelock when disconnect process is done without check.
6477       */
6478     wakelock_put();
6479 #endif
6480 }
6481 
wlcm_request_connect(struct wifi_message * msg,enum cm_sta_state * next,struct wlan_network * network)6482 static void wlcm_request_connect(struct wifi_message *msg, enum cm_sta_state *next, struct wlan_network *network)
6483 {
6484     int ret = WM_SUCCESS;
6485     struct wlan_network *new_network = &wlan.networks[(unsigned int)msg->data];
6486 #if CONFIG_WPA_SUPP
6487 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
6488     struct netif *netif = net_get_sta_interface();
6489 #endif
6490 #endif
6491 
6492 #if CONFIG_HOST_SLEEP
6493     if (!wlan.connect_wakelock_taken)
6494     {
6495         wakelock_get();
6496         wlan.connect_wakelock_taken = true;
6497     }
6498 #endif
6499 #if CONFIG_WLAN_FAST_PATH
6500     /* Mark the fast path cache invalid. */
6501     if ((int)msg->data != wlan.cur_network_idx)
6502         wlan.auth_cache_valid = false;
6503 #endif /* CONFIG_WLAN_FAST_PATH */
6504 
6505     (void)wlan_set_pmfcfg((t_u8)new_network->security.mfpc, (t_u8)new_network->security.mfpr);
6506 
6507     if ((wlan.roam_reassoc == false) && (wlan.sta_state >= CM_STA_ASSOCIATING))
6508     {
6509         if (new_network->role == WLAN_BSS_ROLE_STA)
6510         {
6511             wlcm_d(
6512                 "deauthenticating before"
6513                 " attempting new connection");
6514             (void)wifi_deauthenticate((uint8_t *)network->bssid);
6515         }
6516     }
6517 
6518     wlcm_d("starting %s to network: %d", wlan.roam_reassoc == false ? "connection" : "reassociation", (int)msg->data);
6519 
6520 #if !CONFIG_WPA_SUPP
6521     ret = do_connect((int)msg->data);
6522 #else
6523     wlan.scan_count      = 0;
6524     wlan.cur_network_idx = (int)msg->data;
6525 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
6526     if (wlan.roam_reassoc == true)
6527     {
6528         ret                  = wpa_supp_reassociate(netif);
6529     }
6530     else
6531     {
6532         ret                  = wpa_supp_connect(netif, new_network);
6533     }
6534 #endif
6535 #endif
6536 
6537     /* Release the connect scan lock if do_connect fails,
6538      * in successful case it gets freed in scan result event.
6539      */
6540     if (ret != WM_SUCCESS)
6541     {
6542         if (wlan.is_scan_lock)
6543         {
6544             wlcm_d("releasing scan lock (connect scan)");
6545             (void)OSA_SemaphorePost((osa_semaphore_handle_t)wlan.scan_lock);
6546             wlan.is_scan_lock = 0;
6547         }
6548         wlan.cur_network_idx = -1;
6549         CONNECTION_EVENT(WLAN_REASON_CONNECT_FAILED, NULL);
6550     }
6551 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
6552 #if CONFIG_WPA_SUPP
6553     if (ret == WM_SUCCESS)
6554     {
6555         wlan.sta_state      = CM_STA_ASSOCIATING;
6556         wlan.connect        = true;
6557         wlan.status_timeout = 0;
6558         (void)OSA_TimerActivate((osa_timer_handle_t)wlan.supp_status_timer);
6559     }
6560 #endif
6561 #endif
6562 
6563     *next = wlan.sta_state;
6564 }
6565 
wlcm_request_reconnect(enum cm_sta_state * next,struct wlan_network * network)6566 static void wlcm_request_reconnect(enum cm_sta_state *next, struct wlan_network *network)
6567 {
6568     struct wifi_message msg;
6569     msg.event  = 0;
6570     msg.reason = WIFI_EVENT_REASON_SUCCESS;
6571     msg.data   = (void *)(wlan.cur_network_idx);
6572 
6573     wlcm_d("Reconnect in progress ...");
6574 
6575     wlan.reassoc_request = true;
6576 
6577     if (wlan.reassoc_count < WLAN_RECONNECT_LIMIT)
6578     {
6579         wlcm_d("Reconnect attempt # %d", wlan.reassoc_count + 1);
6580         wlan.reassoc_count++;
6581         wlcm_request_connect(&msg, next, network);
6582     }
6583     else
6584     {
6585         wlcm_d("Reconnection failed. Giving up.");
6586         wlan.reassoc_request = false;
6587         wlan.reassoc_count   = 0;
6588 
6589         wlan.cur_network_idx = -1;
6590         CONNECTION_EVENT(WLAN_REASON_CONNECT_FAILED, NULL);
6591 
6592         wlcm_d("Disconnecting ... ");
6593         (void)wlan_disconnect();
6594     }
6595 }
6596 
6597 #if CONFIG_BG_SCAN
wifi_process_bg_scan_stopped(struct wifi_message * msg)6598 static void wifi_process_bg_scan_stopped(struct wifi_message *msg)
6599 {
6600     (void)msg;
6601 
6602     if (wlan.roam_reassoc && wlan.bgscan_attempt < BG_SCAN_LIMIT)
6603     {
6604         wlan.bgscan_attempt++;
6605         wlan.roam_reassoc = false;
6606         (void)wifi_event_completion(WIFI_EVENT_RSSI_LOW, WIFI_EVENT_REASON_SUCCESS, NULL);
6607     }
6608     else
6609     {
6610         wlan.bgscan_attempt = 0;
6611         wlan.roam_reassoc = false;
6612         PRINTF("Soft Roam: AP with better RSSI not found");
6613         CONNECTION_EVENT(WLAN_REASON_BGSCAN_NETWORK_NOT_FOUND, NULL);
6614     }
6615 }
6616 
wlcm_process_bg_scan_report(void)6617 static void wlcm_process_bg_scan_report(void)
6618 {
6619     wifi_send_scan_query();
6620 #if (CONFIG_11K) || (CONFIG_11V) || (CONFIG_ROAMING)
6621     /* Set rssi low threshold and subscribe rssi low event again */
6622     (void)wifi_set_rssi_low_threshold(&wlan.rssi_low_threshold);
6623 #endif
6624 }
6625 #endif
6626 
wlcm_process_get_hw_spec_event(void)6627 static void wlcm_process_get_hw_spec_event(void)
6628 {
6629 #if CONFIG_WMM
6630     (void)wifi_wmm_init();
6631 #endif
6632 
6633 #if CONFIG_WIFI_SMOKE_TESTS
6634     extern int initNetwork(void);
6635     /* network enet init */
6636     int ret = initNetwork();
6637     if (ret != WM_SUCCESS)
6638     {
6639         PRINTF("FAILED to init network (ret=%d). Reboot the board and try again.\r\n", ret);
6640     }
6641 #endif
6642 
6643 #if CONFIG_SIGMA_AGENT
6644     extern int sigma_agent_init(void);
6645 
6646     (void)sigma_agent_init();
6647 #endif
6648 
6649     /* Set Tx Power Limits in Wi-Fi firmware */
6650     (void)wlan_set_wwsm_txpwrlimit();
6651 
6652     if (wlan.ind_reset == 0)
6653     {
6654         CONNECTION_EVENT(WLAN_REASON_INITIALIZED, NULL);
6655     }
6656 #if CONFIG_WIFI_IND_RESET
6657     wlan.ind_reset = 0;
6658 #endif
6659 }
6660 
wlcm_process_mgmt_frame(void * data)6661 static void wlcm_process_mgmt_frame(void *data)
6662 {
6663     RxPD *rxpd                   = (RxPD *)(net_stack_buffer_get_payload(data));
6664     wlan_mgmt_pkt *pmgmt_pkt_hdr = NULL;
6665 
6666     pmgmt_pkt_hdr          = (wlan_mgmt_pkt *)(void *)((uint8_t *)rxpd + rxpd->rx_pkt_offset);
6667     pmgmt_pkt_hdr->frm_len = wlan_le16_to_cpu(pmgmt_pkt_hdr->frm_len);
6668     if ((pmgmt_pkt_hdr->wlan_header.frm_ctl & (t_u16)IEEE80211_FC_MGMT_FRAME_TYPE_MASK) == (t_u16)0U)
6669     {
6670         // coverity[overrun-buffer-val:SUPPRESS]
6671 		(void)wlan_process_802dot11_mgmt_pkt(
6672             mlan_adap->priv[0], (t_u8 *)&pmgmt_pkt_hdr->wlan_header,
6673             pmgmt_pkt_hdr->frm_len + sizeof(wlan_mgmt_pkt) - sizeof(pmgmt_pkt_hdr->frm_len), rxpd);
6674     }
6675 }
6676 
6677 #if (CONFIG_11MC) || (CONFIG_11AZ)
wlcm_process_ftm_complete_event()6678 static int wlcm_process_ftm_complete_event()
6679 {
6680 	return wifi_process_wlc_ftm_event();
6681 }
6682 
6683 #if CONFIG_WLS_CSI_PROC
wlcm_process_wls_csi_event(void * p_data)6684 static int wlcm_process_wls_csi_event(void *p_data)
6685 {
6686 	return wifi_process_wls_csi_event(p_data);
6687 }
6688 
wifi_get_wls_csi_sem(void)6689 int wifi_get_wls_csi_sem(void)
6690 {
6691     osa_status_t status = KOSA_StatusSuccess;
6692 
6693     status = OSA_SemaphoreWait((osa_semaphore_handle_t)wls_csi_sem, osaWaitForever_c);
6694 
6695     if (status != KOSA_StatusSuccess)
6696     {
6697         return -WM_FAIL;
6698     }
6699 
6700     return WM_SUCCESS;
6701 }
6702 
wifi_put_wls_csi_sem(void)6703 int wifi_put_wls_csi_sem(void)
6704 {
6705     osa_status_t status = KOSA_StatusSuccess;
6706 
6707     status = OSA_SemaphorePost((osa_semaphore_handle_t)wls_csi_sem);
6708 
6709     if (status != KOSA_StatusSuccess)
6710     {
6711         return -WM_FAIL;
6712     }
6713 
6714     return WM_SUCCESS;
6715 }
6716 #endif
6717 
6718 #endif
6719 
wlcm_process_region_power_cfg(struct wifi_message * msg)6720 static void wlcm_process_region_power_cfg(struct wifi_message *msg)
6721 {
6722     t_u8 *country_code = (t_u8 *)msg->data;
6723 
6724 #if CONFIG_COMPRESS_TX_PWTBL
6725     t_u8 region_code;
6726 
6727     region_code = region_string_2_region_code(country_code);
6728     wlan_set_rg_power_cfg(region_code);
6729 #endif
6730 
6731     OSA_MemoryFree(country_code);
6732 }
6733 
6734 #if (CONFIG_11K) || (CONFIG_11V)
wlcm_set_rssi_low_threshold(enum cm_sta_state * next,struct wlan_network * curr_nw)6735 static void wlcm_set_rssi_low_threshold(enum cm_sta_state *next, struct wlan_network *curr_nw)
6736 {
6737     (void)next;
6738     (void)curr_nw;
6739 
6740     (void)wifi_set_rssi_low_threshold(&wlan.rssi_low_threshold);
6741 }
6742 #endif
6743 
6744 #if CONFIG_HOST_SLEEP
wlcm_send_host_sleep(struct wifi_message * msg,enum cm_sta_state * next,struct wlan_network * network)6745 static void wlcm_send_host_sleep(struct wifi_message *msg, enum cm_sta_state *next, struct wlan_network *network)
6746 {
6747     uint32_t wake_up_conds = (uint32_t)msg->data;
6748 
6749     (void)next;
6750     (void)network;
6751 
6752     (void)wlan_send_host_sleep_int(wake_up_conds, MTRUE);
6753 }
6754 #endif
6755 
6756 #if CONFIG_CPU_LOADING
wlan_cpu_loading_info_display(void)6757 static void wlan_cpu_loading_info_display(void)
6758 {
6759     uint64_t total_runtime = 0;
6760     uint64_t task_runtime[CPU_LOADING_TASK_NUM] = {0};
6761     float task_runtime_percentage[CPU_LOADING_TASK_NUM] = {0};
6762     uint8_t task_index = 0, i = 0;
6763     uint32_t collect_time = 0;
6764     char cpu_loading_task_name[] = "cpu_loading_task";
6765 
6766     for(i = 0; i < cpu_loading.task_nums; i++) //Don't calculate cpu info of cpu_loading_thread task.
6767     {
6768         if(!memcmp(cpu_loading_task_name, cpu_loading.task_name[i], strlen(cpu_loading_task_name)))
6769             continue;
6770 
6771         if(cpu_loading.status == CPU_LOADING_STATUS_ONGOING)
6772             task_runtime[i] = cpu_loading.data_cur[i] - cpu_loading.data_pre[i];
6773         else
6774             task_runtime[i] = cpu_loading.data_cur[i] - cpu_loading.first_data[i];
6775 
6776         total_runtime += task_runtime[i];
6777     }
6778 
6779     collect_time = ((cpu_loading.index - 1) * cpu_loading.sampling_period) /1000;
6780     (void)PRINTF("\r\n");
6781     if(cpu_loading.status != CPU_LOADING_STATUS_ENDING)
6782         (void)PRINTF("CPU loading: %ds ~ %ds \r\n", (collect_time - cpu_loading.sampling_period /1000) + 1, collect_time);
6783     else
6784         (void)PRINTF("Total CPU loading info in previous %d seconds\r\n", cpu_loading.index * cpu_loading.sampling_period / 1000);
6785 
6786     (void)PRINTF("taskName             \t\tPercentage\r\n");
6787     for(int i = 0; i < cpu_loading.task_nums; i++)
6788     {
6789         if(!memcmp(cpu_loading_task_name, cpu_loading.task_name[i], strlen(cpu_loading_task_name)))
6790             continue;
6791         task_runtime_percentage[i] = (float)(((float)(task_runtime[i]) / total_runtime) * 100);
6792         (void)PRINTF("%s \t\t%6.2f%%\r\n", task_string_name[i], task_runtime_percentage[i]);
6793     }
6794 }
6795 
wlan_cpu_loading_stop()6796 static int wlan_cpu_loading_stop()
6797 {
6798     cpu_loading.status = CPU_LOADING_STATUS_ENDING;
6799     wlan_cpu_loading_info_display();
6800 
6801     cpu_loading.index = 0;
6802 
6803     (void)send_user_request(CM_STA_USER_REQUEST_CPU_LOADING, 0); // Notify wlcmgr task to destory cpu_loading_thread task.
6804 
6805     return WM_SUCCESS;
6806 }
6807 
wlan_cpu_loading_request()6808 static void wlan_cpu_loading_request()
6809 {
6810     osa_status_t status;
6811 
6812     status = OSA_TimerDestroy((osa_timer_handle_t)cpu_loading.cpu_loading_timer);
6813     if (status != KOSA_StatusSuccess)
6814     {
6815         (void)PRINTF("Failed to delete cpu loading timer: %d.\r\n", ret);
6816     }
6817 
6818     OSA_MemoryFree(cpu_loading.cpu_loading_info);
6819 
6820     status = OSA_TaskDestroy((osa_task_handle_t)cpu_loading.cpu_loading_task_Handle);
6821     if (status != KOSA_StatusSuccess)
6822     {
6823         (void)PRINTF("Failed to delete cpu_loading_task: %d.\r\n", ret);
6824     }
6825 
6826     cpu_loading.status = CPU_LOADING_STATUS_DEAD;
6827 
6828     (void)PRINTF("Success to stop CPU loading test.\r\n");
6829 }
6830 #endif
6831 
6832 /*
6833  * Event Handlers
6834  */
6835 
handle_message(struct wifi_message * msg)6836 static enum cm_sta_state handle_message(struct wifi_message *msg)
6837 {
6838     enum cm_sta_state next       = wlan.sta_state;
6839     struct wlan_network *network = NULL;
6840 #if CONFIG_WPA_SUPP
6841 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
6842     int ret = WM_SUCCESS;
6843 #endif
6844     struct netif *netif = net_get_sta_interface();
6845 #endif
6846 
6847     network = &wlan.networks[wlan.cur_network_idx];
6848 
6849     switch (msg->event)
6850     {
6851         case CM_STA_USER_REQUEST_CONNECT:
6852             wlan.pending_assoc_request = false;
6853             if (!wlan.assoc_paused)
6854             {
6855                 wlcm_request_connect(msg, &next, network);
6856             }
6857             else
6858             {
6859                 wlan.pending_assoc_request = true;
6860             }
6861             break;
6862 
6863         case CM_STA_USER_REQUEST_DISCONNECT:
6864 #if CONFIG_WPA_SUPP
6865 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
6866             wlan.pending_disconnect_request = false;
6867             ret = wpa_supp_abort_scan(netif);
6868             if (ret == WM_SUCCESS)
6869             {
6870                 wlan.pending_disconnect_request = true;
6871             }
6872             wpa_supp_disconnect(netif);
6873 #else
6874             supplicant_disconnect(net_if_get_device((void *)netif));
6875 #endif
6876 #endif
6877             if (wlan.cur_network_idx >= WLAN_MAX_KNOWN_NETWORKS)
6878             {
6879 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
6880 #if CONFIG_HOST_SLEEP
6881                 wakelock_put();
6882 #endif
6883 #endif
6884                 break;
6885             }
6886 
6887             wlcm_request_disconnect(&next, network);
6888             break;
6889 
6890         case CM_STA_USER_REQUEST_SCAN:
6891             wlcm_request_scan(msg, &next);
6892             break;
6893 #if (CONFIG_11K) || (CONFIG_11V)
6894         case CM_STA_USER_REQUEST_SET_RSSI_THRESHOLD:
6895             wlcm_set_rssi_low_threshold(&next, network);
6896             break;
6897 #endif
6898 #if CONFIG_HOST_SLEEP
6899         case CM_STA_USER_REQUEST_HS:
6900             wlcm_send_host_sleep(msg, &next, network);
6901             break;
6902 #endif
6903         case CM_STA_USER_REQUEST_PS_ENTER:
6904 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
6905             int state = wifi_nxp_supp_state();
6906 
6907             if ((state >= WPA_SCANNING) && (state < WPA_COMPLETED))
6908 #else
6909             if (wlan.sta_state >= CM_STA_SCANNING && wlan.sta_state <= CM_STA_OBTAINING_ADDRESS)
6910 #endif
6911             {
6912                 wlcm_w("ignoring ps enter in invalid state");
6913                 wlcm_e("Error entering power save mode");
6914                 break;
6915             }
6916             if (msg->data == NULL)
6917             {
6918                 wlcm_w("ignoring ps enter request with NULL ps mode");
6919                 wlcm_e("entering power save mode");
6920                 break;
6921             }
6922             wlan_enable_power_save((int)msg->data);
6923             break;
6924         case CM_STA_USER_REQUEST_PS_EXIT:
6925             if (msg->data == NULL)
6926             {
6927                 wlcm_w("ignoring ps exit request with NULL ps mode");
6928                 break;
6929             }
6930             wlan_disable_power_save((int)msg->data);
6931             break;
6932 #if CONFIG_CPU_LOADING
6933         case CM_STA_USER_REQUEST_CPU_LOADING:
6934             wlan_cpu_loading_request();
6935             break;
6936 #endif
6937         case WIFI_EVENT_SCAN_START:
6938 #if CONFIG_WPA_SUPP
6939             wifi_scan_start(msg);
6940 #endif
6941             break;
6942         case WIFI_EVENT_SCAN_RESULT:
6943             wlcm_d("got event: scan result");
6944             wlcm_process_scan_result_event(msg, &next);
6945             break;
6946 
6947 #if (CONFIG_WIFI_IND_RESET) && (CONFIG_WIFI_IND_DNLD)
6948         case WIFI_EVENT_FW_HANG:
6949             wlcm_d("got event: fw hang");
6950             wlcm_process_fw_hang_event(msg, &next);
6951             break;
6952         case WIFI_EVENT_FW_RESET:
6953             wlcm_d("got event: fw reset");
6954             wlcm_process_fw_reset_event(msg, &next);
6955             break;
6956 #endif
6957 
6958 #if CONFIG_WPA_SUPP
6959         case WIFI_EVENT_SURVEY_RESULT_GET:
6960             wifi_survey_result_get(msg);
6961             break;
6962 #endif
6963 
6964         case WIFI_EVENT_ASSOCIATION:
6965             wlcm_d("got event: association result: %s",
6966                     msg->reason == WIFI_EVENT_REASON_SUCCESS ? "success" : "failure");
6967             if (msg->reason == WIFI_EVENT_REASON_SUCCESS)
6968             {
6969                 CONNECTION_EVENT(WLAN_REASON_ASSOC_SUCCESS, NULL);
6970             }
6971             wlcm_process_association_event(msg, &next);
6972             break;
6973 
6974 #if CONFIG_WPA_SUPP
6975 #if CONFIG_AUTO_RECONNECT
6976         case WIFI_EVENT_ASSOCIATION_NOTIFY:
6977             wlcm_d("got event: association notify: %s",
6978                     msg->reason == WIFI_EVENT_REASON_SUCCESS ? "success" : "failure");
6979 
6980             wlcm_process_association_notify_event(msg, &next);
6981             break;
6982 #endif
6983 #endif
6984 
6985         case WIFI_EVENT_PMK:
6986             wlcm_d("got event: PMK result: %s", msg->reason == WIFI_EVENT_REASON_SUCCESS ? "success" : "failure");
6987             if (wlan.cur_network_idx >= WLAN_MAX_KNOWN_NETWORKS)
6988                 break;
6989 
6990             wlcm_process_pmk_event(msg, &next, network);
6991             break;
6992             /* We have received a event from firmware whether
6993              * authentication with given wireless credentials was successful
6994              * or not. If successful, we are authenticated and can proceed
6995              * to IP-level setup by configuring the TCP stack for our
6996              * desired address and transitioning to the
6997              * CM_STA_REQUESTING_ADDRESS state where we wait for the TCP
6998              * stack configuration response.  Otherwise we have failed to
6999              * connect to the network.
7000              */
7001         case WIFI_EVENT_AUTHENTICATION:
7002             wlcm_d("got event: authentication result: %s",
7003                     msg->reason == WIFI_EVENT_REASON_SUCCESS ? "success" : "failure");
7004             if(msg->reason == WIFI_EVENT_REASON_FAILURE)
7005             {
7006 #if CONFIG_ECSA
7007                 wrapper_clear_media_connected_event();
7008                 wlan_switch_to_nondfs_channel();
7009 #endif
7010             }
7011             if (wlan.cur_network_idx >= WLAN_MAX_KNOWN_NETWORKS)
7012                 break;
7013 
7014             wlcm_process_authentication_event(msg, &next, network);
7015             break;
7016         case WIFI_EVENT_LINK_LOSS:
7017             wlcm_d("got event: link loss, code=%d", (int)msg->data);
7018 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
7019             if (is_sta_connected())
7020             {
7021                 CONNECTION_EVENT(WLAN_REASON_LINK_LOST, NULL);
7022 #if CONFIG_ECSA
7023                 wrapper_clear_media_connected_event();
7024                 wlan_switch_to_nondfs_channel();
7025 #endif
7026             }
7027 #else
7028             if (wlan.cur_network_idx >= WLAN_MAX_KNOWN_NETWORKS)
7029                 break;
7030 
7031             wlcm_process_link_loss_event(msg, &next, network);
7032 #endif
7033             break;
7034         case WIFI_EVENT_RSSI_LOW:
7035             wlcm_d("got event: rssi low");
7036 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
7037             wlcm_process_rssi_low_event(msg);
7038 #else
7039             if (wlan.cur_network_idx >= WLAN_MAX_KNOWN_NETWORKS)
7040                 break;
7041 #if (CONFIG_11K) || (CONFIG_11V) || (CONFIG_ROAMING)
7042             wlcm_process_rssi_low_event(msg, &next, network);
7043 #else
7044             CONNECTION_EVENT(WLAN_REASON_RSSI_LOW, NULL);
7045 #endif
7046 #endif
7047             break;
7048 #if CONFIG_SUBSCRIBE_EVENT_SUPPORT
7049         case WIFI_EVENT_RSSI_HIGH:
7050             wlcm_d("got event: RSSI high");
7051             CONNECTION_EVENT(WLAN_REASON_RSSI_HIGH, NULL);
7052             break;
7053         case WIFI_EVENT_SNR_LOW:
7054             wlcm_d("got event: SNR low");
7055             CONNECTION_EVENT(WLAN_REASON_SNR_LOW, NULL);
7056             break;
7057         case WIFI_EVENT_SNR_HIGH:
7058             wlcm_d("got event: SNR high");
7059             CONNECTION_EVENT(WLAN_REASON_SNR_HIGH, NULL);
7060             break;
7061         case WIFI_EVENT_MAX_FAIL:
7062             wlcm_d("got event: MAX fail");
7063             CONNECTION_EVENT(WLAN_REASON_MAX_FAIL, NULL);
7064             break;
7065         case WIFI_EVENT_BEACON_MISSED:
7066             wlcm_d("got event: Beacon missed");
7067             CONNECTION_EVENT(WLAN_REASON_BEACON_MISSED, NULL);
7068             break;
7069         case WIFI_EVENT_DATA_RSSI_LOW:
7070             wlcm_d("got event: DATA_RSSI low");
7071             CONNECTION_EVENT(WLAN_REASON_DATA_RSSI_LOW, NULL);
7072             break;
7073         case WIFI_EVENT_DATA_RSSI_HIGH:
7074             wlcm_d("got event: DATA_RSSI high");
7075             CONNECTION_EVENT(WLAN_REASON_DATA_RSSI_HIGH, NULL);
7076             break;
7077         case WIFI_EVENT_DATA_SNR_LOW:
7078             wlcm_d("got event: DATA_SNR low");
7079             CONNECTION_EVENT(WLAN_REASON_DATA_SNR_LOW, NULL);
7080             break;
7081         case WIFI_EVENT_DATA_SNR_HIGH:
7082             wlcm_d("got event: DATA_SNR high");
7083             CONNECTION_EVENT(WLAN_REASON_DATA_SNR_HIGH, NULL);
7084             break;
7085         case WIFI_EVENT_FW_LINK_QUALITY:
7086             wlcm_d("got event: LINK_QUALITY");
7087             CONNECTION_EVENT(WLAN_REASON_LINK_QUALITY, NULL);
7088             break;
7089         case WIFI_EVENT_FW_PRE_BCN_LOST:
7090             wlcm_d("got event: PRE_BEACON_LOST");
7091             CONNECTION_EVENT(WLAN_REASON_PRE_BEACON_LOST, NULL);
7092             break;
7093 #endif
7094 #if CONFIG_HOST_SLEEP
7095         case WIFI_EVENT_HS_ACTIVATED:
7096         case WIFI_EVENT_SLEEP_CONFIRM_DONE:
7097             if (wlan.hs_configured == MTRUE)
7098             {
7099                 wlan.hs_configured = MFALSE;
7100 #if CONFIG_POWER_MANAGER
7101                 if (!wlan_is_manual)
7102                 {
7103                     is_hs_handshake_done = WLAN_HOSTSLEEP_SUCCESS;
7104 #ifdef RW610
7105                     (void)OSA_TimerDeactivate((osa_timer_handle_t)temperature_mon_timer);
7106 #endif
7107                 }
7108 #endif
7109             }
7110             break;
7111 #endif
7112 #if (CONFIG_11K) || (CONFIG_11V)
7113         case WIFI_EVENT_NLIST_REPORT:
7114             wlcm_d("got event: neighbor list report");
7115             if (wlan.cur_network_idx >= WLAN_MAX_KNOWN_NETWORKS)
7116                 break;
7117 
7118             wlcm_process_neighbor_list_report_event(msg, &next, network);
7119             break;
7120 #endif
7121 #if CONFIG_WLAN_BRIDGE
7122         case WIFI_EVENT_AUTOLINK_NETWORK_SWITCHED:
7123             wlcm_d("got event: auto link switch network");
7124             wlcm_process_network_switch_event(msg, &next, network);
7125             break;
7126 #endif
7127         case WIFI_EVENT_DISASSOCIATION:
7128             wlcm_d("got event: disassociation, code=%d", (int)(msg->data));
7129 #if CONFIG_ECSA
7130             wrapper_clear_media_connected_event();
7131             wlan_switch_to_nondfs_channel();
7132 #endif
7133             if (wlan.cur_network_idx >= WLAN_MAX_KNOWN_NETWORKS)
7134                 break;
7135 
7136             wlcm_process_disassoc_event(msg, &next, network);
7137             break;
7138 
7139         case WIFI_EVENT_DEAUTHENTICATION:
7140             wlcm_d("got event: deauthentication");
7141 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
7142             CONNECTION_EVENT(WLAN_REASON_DISCONNECTED, NULL);
7143 #endif
7144             wlcm_process_deauthentication_event(msg, &next, network);
7145             break;
7146 
7147         case WIFI_EVENT_NET_STA_ADDR_CONFIG:
7148             wlcm_d("got event: TCP configured");
7149             if (wlan.cur_network_idx >= WLAN_MAX_KNOWN_NETWORKS)
7150                 break;
7151 
7152             wlcm_process_sta_addr_config_event(msg, &next, network);
7153             break;
7154 
7155         case WIFI_EVENT_GET_HW_SPEC:
7156             wlcm_process_get_hw_spec_event();
7157             break;
7158 
7159         case WIFI_EVENT_NET_INTERFACE_CONFIG:
7160             wlcm_d("got event: Interfaces configured");
7161             wlcm_process_net_if_config_event(msg, &next);
7162             break;
7163 
7164         case WIFI_EVENT_NET_DHCP_CONFIG:
7165             if (wlan.cur_network_idx >= WLAN_MAX_KNOWN_NETWORKS)
7166                 break;
7167 
7168             wlcm_process_net_dhcp_config(msg, &next, network);
7169             break;
7170 #if CONFIG_IPV6
7171         case WIFI_EVENT_NET_IPV6_CONFIG:
7172             wlcm_d("got event: net ipv6 config");
7173             if (wlan.cur_network_idx >= WLAN_MAX_KNOWN_NETWORKS)
7174                 break;
7175 
7176             wlcm_process_net_ipv6_config(msg, &next, network);
7177             break;
7178 #endif /* CONFIG_IPV6 */
7179 
7180         case WIFI_EVENT_CHAN_SWITCH_ANN:
7181             wlcm_d("got event: channel switch announcement");
7182             if (wlan.cur_network_idx >= WLAN_MAX_KNOWN_NETWORKS)
7183                 break;
7184 
7185             wlcm_process_channel_switch_ann(&next, network);
7186             break;
7187         case WIFI_EVENT_CHAN_SWITCH:
7188             wlcm_d("got event: channel switch");
7189 #if CONFIG_WPA_SUPP
7190             wlcm_process_channel_switch_supp(msg);
7191 #else
7192             wlcm_process_channel_switch(msg);
7193 #endif
7194             break;
7195 #if CONFIG_CSI
7196         case WIFI_EVENT_CSI_STATUS:
7197             wlcm_d("got event: csi status report");
7198             wlcm_process_csi_status_report(msg);
7199             break;
7200 #endif
7201 
7202         case WIFI_EVENT_SLEEP:
7203 #if CONFIG_WIFI_PS_DEBUG
7204             wlcm_d("got event: sleep");
7205 #endif
7206             wlcm_process_sleep_event();
7207             break;
7208 
7209         case WIFI_EVENT_IEEE_PS:
7210 #if CONFIG_WIFI_PS_DEBUG
7211             wlcm_d("got event: IEEE ps result: %s", msg->reason == WIFI_EVENT_REASON_SUCCESS ? "success" : "failure");
7212 #endif
7213             wlcm_process_ieeeps_event(msg);
7214             break;
7215 
7216         case WIFI_EVENT_DEEP_SLEEP:
7217 #if CONFIG_WIFI_PS_DEBUG
7218             wlcm_d("got event: deep sleep result: %s",
7219                     msg->reason == WIFI_EVENT_REASON_SUCCESS ? "success" : "failure");
7220 #endif
7221             wlcm_process_deepsleep_event(msg, &next);
7222 
7223             break;
7224 #if (CONFIG_WNM_PS)
7225         case WIFI_EVENT_WNM_PS:
7226 #if CONFIG_WIFI_PS_DEBUG
7227             wlcm_d("got event: WNM ps result: %s", msg->reason == WIFI_EVENT_REASON_SUCCESS ? "success" : "failure");
7228 #endif
7229             wlcm_process_wnmps_event(msg);
7230             break;
7231 #endif
7232         case WIFI_EVENT_IEEE_DEEP_SLEEP:
7233 #if CONFIG_WIFI_PS_DEBUG
7234             wlcm_d("got event: IEEE deep sleep result: %s",
7235                     msg->reason == WIFI_EVENT_REASON_SUCCESS ? "success" : "failure");
7236 #endif
7237 #if !CONFIG_MEM_POOLS
7238             OSA_MemoryFree(msg->data);
7239 #else
7240             OSA_MemoryPoolFree(buf_32_MemoryPool, msg->data);
7241 #endif
7242             break;
7243         case WIFI_EVENT_WNM_DEEP_SLEEP:
7244 #if CONFIG_WIFI_PS_DEBUG
7245             wlcm_d("got event: WNM deep sleep result: %s",
7246                     msg->reason == WIFI_EVENT_REASON_SUCCESS ? "success" : "failure");
7247 #endif
7248 #if !CONFIG_MEM_POOLS
7249             OSA_MemoryFree(msg->data);
7250 #else
7251             OSA_MemoryPoolFree(buf_32_MemoryPool, msg->data);
7252 #endif
7253             break;
7254 #if CONFIG_HOST_SLEEP
7255         case WIFI_EVENT_HS_CONFIG:
7256             if (wlan.hs_configured == MTRUE)
7257             {
7258                 wlcm_process_hs_config_event();
7259             }
7260             break;
7261 #endif
7262 #if CONFIG_11N
7263         case WIFI_EVENT_11N_SEND_ADDBA:
7264             wlcm_process_send_addba(msg);
7265             break;
7266         case WIFI_EVENT_11N_RECV_ADDBA:
7267             wlcm_process_addba_request(msg);
7268             break;
7269         case WIFI_EVENT_11N_DELBA:
7270             wlcm_process_delba_request(msg);
7271             break;
7272         case WIFI_EVENT_11N_BA_STREAM_TIMEOUT:
7273             wlcm_process_ba_stream_timeout_request(msg);
7274             break;
7275         case WIFI_EVENT_11N_AGGR_CTRL:
7276             wlcm_d("AGGR_CTRL ignored for now");
7277             break;
7278 #endif /* CONFIG_11N */
7279         case WIFI_EVENT_STA_MAC_ADDR_CONFIG:
7280             if (msg->data != NULL)
7281             {
7282                 (void)memcpy((void *)&wlan.sta_mac[0], (const void *)msg->data, MLAN_MAC_ADDR_LENGTH);
7283 #if !CONFIG_MEM_POOLS
7284                 OSA_MemoryFree(msg->data);
7285 #else
7286                 OSA_MemoryPoolFree(buf_32_MemoryPool, msg->data);
7287 #endif
7288             }
7289             break;
7290 #if UAP_SUPPORT
7291         case WIFI_EVENT_UAP_MAC_ADDR_CONFIG:
7292             if (msg->data != NULL)
7293             {
7294                 (void)memcpy((void *)&wlan.uap_mac[0], (const void *)msg->data, MLAN_MAC_ADDR_LENGTH);
7295 #if !CONFIG_MEM_POOLS
7296                 OSA_MemoryFree(msg->data);
7297 #else
7298                 OSA_MemoryPoolFree(buf_32_MemoryPool, msg->data);
7299 #endif
7300             }
7301             break;
7302 #endif
7303 #if CONFIG_BG_SCAN
7304         case WIFI_EVENT_BG_SCAN_STOPPED:
7305             wlcm_d("got event: BG scan stopped");
7306             wifi_process_bg_scan_stopped(msg);
7307             break;
7308         case WIFI_EVENT_BG_SCAN_REPORT:
7309             wlcm_d("got event: BG scan report");
7310             wlcm_process_bg_scan_report();
7311             break;
7312 #endif
7313         case WIFI_EVENT_MGMT_FRAME:
7314             wlcm_d("got event: management frame");
7315             wlcm_process_mgmt_frame(msg->data);
7316             next = wlan.sta_state;
7317             net_stack_buffer_free(msg->data);
7318             break;
7319 #if CONFIG_WPA_SUPP
7320         case WIFI_EVENT_REMAIN_ON_CHANNEL:
7321             wifi_process_remain_on_channel(msg);
7322             break;
7323         case WIFI_EVENT_MGMT_TX_STATUS:
7324             wifi_process_mgmt_tx_status(msg);
7325             break;
7326 #endif
7327 #if (CONFIG_11MC) || (CONFIG_11AZ)
7328         case WIFI_EVENT_FTM_COMPLETE:
7329             wlcm_d("got event: continue to ftm or stop");
7330             wlcm_process_ftm_complete_event();
7331             break;
7332 #if CONFIG_WLS_CSI_PROC
7333         case WIFI_EVENT_WLS_CSI:
7334             wlcm_d("got event: receive WLS csi data");
7335             wlcm_process_wls_csi_event(msg->data);
7336             break;
7337 #endif
7338 #endif
7339         case WIFI_EVENT_REGION_POWER_CFG:
7340             wlcm_process_region_power_cfg(msg);
7341             break;
7342 #if CONFIG_WMM
7343         case WIFI_EVENT_TX_DATA_PAUSE:
7344             wifi_sta_handle_event_data_pause(msg->data);
7345 #if !CONFIG_MEM_POOLS
7346             OSA_MemoryFree(msg->data);
7347 #else
7348             OSA_MemoryPoolFree(buf_32_MemoryPool, msg->data);
7349 #endif
7350             break;
7351 #endif
7352         default:
7353             wlcm_w("got unknown message: %d", msg->event);
7354             break;
7355     }
7356 
7357     return next;
7358 }
7359 
is_uap_msg(struct wifi_message * msg)7360 static bool is_uap_msg(struct wifi_message *msg)
7361 {
7362     return (((msg->event >= CM_UAP_USER_REQUEST_START) && (msg->event < CM_WLAN_USER_REQUEST_DEINIT)) ||
7363             (msg->event <= WIFI_EVENT_UAP_LAST));
7364 }
7365 
7366 /*
7367  * Main Thread: the WLAN Connection Manager event queue handler and state
7368  * machine.
7369  */
wlcmgr_task(void * data)7370 static void wlcmgr_task(void *data)
7371 {
7372     osa_status_t status;
7373     struct wifi_message msg;
7374     enum cm_sta_state next_sta_state;
7375 #if UAP_SUPPORT
7376     enum cm_uap_state next_uap_state;
7377 #endif
7378 
7379     (void)memset((void *)&msg, 0, sizeof(struct wifi_message));
7380 
7381     /* Wait for all the data structures to be created */
7382     while (!wlan.running)
7383     {
7384         OSA_TimeDelay(500);
7385     }
7386 
7387     (void)net_wlan_init();
7388 
7389     while (true)
7390     {
7391         status = OSA_MsgQGet((osa_msgq_handle_t)wlan.events, &msg, osaWaitForever_c);
7392         if ((wlan.stop_request != 0U) && (msg.event == (uint16_t)CM_WLAN_USER_REQUEST_SHUTDOWN))
7393         {
7394             wlcm_d("Received shutdown request");
7395             wlan.status = WLCMGR_THREAD_STOPPED;
7396             while (true)
7397             {
7398                 OSA_TimeDelay(60000);
7399             }
7400         }
7401 
7402         if (status == KOSA_StatusSuccess)
7403         {
7404 #if !CONFIG_WIFI_PS_DEBUG
7405             if (msg.event != WIFI_EVENT_SLEEP && msg.event != WIFI_EVENT_IEEE_PS &&
7406                     msg.event != WIFI_EVENT_DEEP_SLEEP && msg.event != WIFI_EVENT_IEEE_DEEP_SLEEP)
7407             {
7408                 wlcm_d("got wifi message: %d %d %p", msg.event, msg.reason, msg.data);
7409             }
7410 #else
7411             wlcm_d("got wifi message: %d %d %p", msg.event, msg.reason, msg.data);
7412 #endif
7413 
7414             if (is_uap_msg(&msg) != 0)
7415             {
7416 #if UAP_SUPPORT
7417                 /* uAP related msg */
7418                 next_uap_state = uap_state_machine(&msg);
7419                 if (wlan.uap_state == next_uap_state)
7420                 {
7421                     continue;
7422                 }
7423 
7424                 wlcm_d("SM uAP %s -> %s", dbg_uap_state_name(wlan.uap_state), dbg_uap_state_name(next_uap_state));
7425                 wlan.uap_state = next_uap_state;
7426 #else
7427                 wlcm_w("UAP feature disabled recv wlcm msg %d", msg.event);
7428 #endif
7429             }
7430             else if (msg.event == (uint16_t)CM_WLAN_USER_REQUEST_DEINIT)
7431             {
7432                 wlcm_deinit((int)msg.data);
7433             }
7434             else
7435             {
7436                 /* STA related msg */
7437                 next_sta_state = handle_message(&msg);
7438                 if (wlan.sta_state == next_sta_state)
7439                 {
7440                     continue;
7441                 }
7442 
7443                 wlcm_d("SM STA %s -> %s", dbg_sta_state_name(wlan.sta_state), dbg_sta_state_name(next_sta_state));
7444                 wlan.sta_state = next_sta_state;
7445             }
7446         }
7447         else
7448         {
7449             wlcm_d("SM queue recv Timed out ");
7450         }
7451     }
7452 }
7453 
7454 #if CONFIG_WPS2
7455 /* WLAN Connection Manager scan results callback */
prov_wps_scan_results(unsigned int count)7456 static int prov_wps_scan_results(unsigned int count)
7457 {
7458     int i;
7459     int err;
7460 
7461     if (count == 0)
7462     {
7463         OSA_SemaphorePost((osa_semaphore_handle_t)wlan_wps.wps_scan_done);
7464         return 0;
7465     }
7466 
7467     for (i = 0; i < count; i++)
7468     {
7469         err = wlan_get_scan_result(i, &wlan_wps.wps_res);
7470         if (err != 0)
7471         {
7472             wlcm_e("Error: can't get scan res %d", i);
7473             continue;
7474         }
7475 
7476         if ((wlan_wps.wps_res.wps_session == WPS_SESSION_PBC) || (wlan_wps.wps_res.wps_session == WPS_SESSION_PIN))
7477             break;
7478     }
7479 
7480     OSA_SemaphorePost((osa_semaphore_handle_t)wlan_wps.wps_scan_done);
7481 
7482     return 0;
7483 }
7484 
wps_task(void * data)7485 static void wps_task(void *data)
7486 {
7487     osa_status_t status;
7488 
7489     status = OSA_SemaphoreCreateBinary((osa_semaphore_handle_t)wlan_wps.wps_scan_done);
7490     if (status != KOSA_StatusSuccess)
7491     {
7492         wlcm_e("Failed to create WPS scan semaphore");
7493     }
7494 
7495     while (1)
7496     {
7497         OSA_TimeDelay(500);
7498         if ((wlan_wps.wps_state == PROV_WPS_PBC_ENABLED) || (wlan_wps.wps_state == PROV_WPS_PIN_ENABLED))
7499         {
7500             int i = 5;
7501 
7502             wlcm_d("WPS is enabled");
7503             if (is_uap_started())
7504             {
7505                 wlan_wps.wps_state = PROV_WPS_STARTED;
7506                 wps_conf.role = 2; //WPS_REGISTRAR
7507                 wlan_set_prov_session(PROV_WPS_SESSION_ATTEMPT);
7508                 wps_connect(wlan_wps.wps_cmd, wlan_wps.wps_pin, &wlan_wps.wps_res);
7509             }
7510             else
7511             {
7512                 while (i)
7513                 {
7514                     if (wlan_scan(prov_wps_scan_results) != 0)
7515                     {
7516                         (void)PRINTF("Error: scan request failed, sta_state");
7517 #if CONFIG_WLCMGR_DEBUG
7518                         (void)PRINTF("(%s)", dbg_sta_state_name(wlan.sta_state));
7519 #else
7520                         (void)PRINTF("(%d)", wlan.sta_state);
7521 #endif
7522                         (void)PRINTF(" is not idle/connected\r\n");
7523                         (void)PRINTF("Wait or disconnect network\r\n");
7524                         break;
7525                     }
7526                     OSA_SemaphoreWait((osa_semaphore_handle_t)wlan_wps.wps_scan_done, osaWaitForever_c);
7527                     if ((wlan_wps.wps_res.wps_session == WPS_SESSION_PIN) ||
7528                         (wlan_wps.wps_res.wps_session == WPS_SESSION_PBC))
7529                     {
7530                         wlcm_d("WPS Session from AP started");
7531                         wlan_wps.wps_state = PROV_WPS_STARTED;
7532                         wlan_set_prov_session(PROV_WPS_SESSION_ATTEMPT);
7533                         wps_connect(wlan_wps.wps_cmd, wlan_wps.wps_pin, &wlan_wps.wps_res);
7534                         break;
7535                     }
7536                     i--;
7537                 }
7538             }
7539 
7540             wlan_wps.wps_state = PROV_WPS_NONE;
7541             memset(&wlan_wps.wps_res, 0, sizeof(wlan_wps.wps_res));
7542         }
7543     }
7544 }
7545 #endif
7546 /*
7547  * WLAN API
7548  */
7549 
send_user_request(enum user_request_type request,unsigned int data)7550 static int send_user_request(enum user_request_type request, unsigned int data)
7551 {
7552     struct wifi_message msg;
7553 
7554     msg.event  = (uint16_t)request;
7555     msg.reason = WIFI_EVENT_REASON_SUCCESS;
7556     msg.data   = (void *)data;
7557 
7558     if (OSA_MsgQPut((osa_msgq_handle_t)wlan.events, &msg) == KOSA_StatusSuccess)
7559     {
7560         return WM_SUCCESS;
7561     }
7562 
7563     return -WM_FAIL;
7564 }
7565 
copy_network(struct wlan_network * dst,struct wlan_network * src)7566 static void copy_network(struct wlan_network *dst, struct wlan_network *src)
7567 {
7568     (void)memcpy((void *)dst, (const void *)src, sizeof(struct wlan_network));
7569     /* Omit any information that was dynamically
7570      * learned from the network so that users can
7571      * see which parameters were actually
7572      * configured by the user.
7573      */
7574     if (!src->bssid_specific)
7575     {
7576         (void)memset(dst->bssid, 0, IEEEtypes_ADDRESS_SIZE);
7577     }
7578     if (!src->ssid_specific)
7579     {
7580         (void)memset(dst->ssid, 0, IEEEtypes_SSID_SIZE);
7581     }
7582     if (!src->channel_specific)
7583     {
7584         dst->channel = 0;
7585     }
7586     if (src->ip.ipv4.addr_type != 0U)
7587     {
7588         dst->ip.ipv4.address = 0;
7589         dst->ip.ipv4.gw      = 0;
7590         dst->ip.ipv4.netmask = 0;
7591         dst->ip.ipv4.dns1    = 0;
7592         dst->ip.ipv4.dns2    = 0;
7593     }
7594 }
7595 
wifi_wakeup_card_cb(osa_rw_lock_t * plock,unsigned int wait_time)7596 static int wifi_wakeup_card_cb(osa_rw_lock_t *plock, unsigned int wait_time)
7597 {
7598     osa_status_t status = OSA_SemaphoreWait((osa_semaphore_handle_t)plock->rw_lock, 0);
7599     if (status != KOSA_StatusSuccess)
7600     {
7601         wlan_wake_up_card();
7602         status = OSA_SemaphoreWait((osa_semaphore_handle_t)plock->rw_lock, wait_time);
7603     }
7604 
7605     if (status != KOSA_StatusSuccess)
7606     {
7607         return -WM_FAIL;
7608     }
7609 
7610     return WM_SUCCESS;
7611 }
7612 
wlan_init(const uint8_t * fw_start_addr,const size_t size)7613 int wlan_init(const uint8_t *fw_start_addr, const size_t size)
7614 {
7615     int ret;
7616 #if (CONFIG_WMM_UAPSD) || (CONFIG_HOST_SLEEP)
7617     osa_status_t status;
7618 #endif
7619 
7620     if (wlan.status != WLCMGR_INACTIVE)
7621     {
7622         return WM_SUCCESS;
7623     }
7624 
7625 #if CONFIG_MEM_POOLS
7626     ret = mem_pool_init();
7627     if (ret != WM_SUCCESS)
7628     {
7629         wlcm_e("Failed to init Memory Pools");
7630         return ret;
7631     }
7632 #endif
7633 
7634 #ifdef OVERRIDE_CALIBRATION_DATA
7635     wlan_set_cal_data(ext_cal_data, sizeof(ext_cal_data));
7636 #else
7637 #ifndef RW610
7638     wlan_set_cal_data(int_cal_data, sizeof(int_cal_data));
7639 #else
7640     wlan_set_cal_data(cal_data_rw610, sizeof(cal_data_rw610));
7641 #endif
7642 #endif
7643 
7644 #if (CONFIG_HOST_PMK) || (CONFIG_WPS2)
7645     wm_mbedtls_lib_init();
7646 #endif /* (CONFIG_HOST_PMK) || (CONFIG_WPS2) */
7647 
7648     ret = OSA_RWLockCreateWithCB(&sleep_rwlock, "sleep_mutex", "sleep_rwlock", wifi_wakeup_card_cb);
7649     if (ret != WM_SUCCESS)
7650     {
7651         wifi_e("Create sleep cmd lock failed");
7652         return ret;
7653     }
7654 
7655 #if CONFIG_WMM_UAPSD
7656     status = OSA_SemaphoreCreateBinary((osa_semaphore_handle_t)uapsd_sem);
7657     if (status != KOSA_StatusSuccess)
7658     {
7659         wifi_e("Create uapsd sem failed");
7660         return ret;
7661     }
7662     OSA_SemaphorePost((osa_semaphore_handle_t)uapsd_sem);
7663 #endif
7664 #if CONFIG_HOST_SLEEP
7665     status = OSA_SemaphoreCreate((osa_semaphore_handle_t)wakelock, 0);
7666     if (status != KOSA_StatusSuccess)
7667     {
7668         wifi_e("Failed to create wake-lock semaphore");
7669         return ret;
7670     }
7671 #endif
7672 
7673     ret = wifi_init(fw_start_addr, size);
7674     if (ret != 0)
7675     {
7676         wlcm_e("wifi_init failed. status code %d", ret);
7677         return ret;
7678     }
7679 
7680     wlan.status = WLCMGR_INIT_DONE;
7681     wifi_mac_addr_t mac_addr;
7682 
7683     ret = wifi_get_device_mac_addr(&mac_addr);
7684 
7685     if (ret != WM_SUCCESS)
7686     {
7687         wlcm_e("Failed to get sta mac address");
7688         return ret;
7689     }
7690 
7691 #if UAP_SUPPORT
7692     wifi_mac_addr_t mac_addr_uap;
7693     ret = wifi_get_device_uap_mac_addr(&mac_addr_uap);
7694     if (ret != WM_SUCCESS)
7695     {
7696         wlcm_e("Failed to get uap mac address");
7697         return ret;
7698     }
7699 
7700     (void)memcpy((void *)&wlan.uap_mac[0], (const void *)mac_addr_uap.mac, MLAN_MAC_ADDR_LENGTH);
7701 #endif
7702     (void)memcpy((void *)&wlan.sta_mac[0], (const void *)mac_addr.mac, MLAN_MAC_ADDR_LENGTH);
7703     (void)PRINTF("STA MAC Address: ");
7704     print_mac((const char *)&wlan.sta_mac);
7705     (void)PRINTF("\r\n");
7706 #if CONFIG_P2P
7707     (void)memcpy((void *)&wlan.wfd_mac[0], (const void *)mac_addr.mac, MLAN_MAC_ADDR_LENGTH);
7708     wlan.wfd_mac[0] |= (0x01 << 1);
7709 #endif
7710 
7711     ret = wifi_get_device_firmware_version_ext(&wlan.fw_ver_ext);
7712     if (ret != WM_SUCCESS)
7713     {
7714         wlcm_e("Failed to get verext");
7715         return ret;
7716     }
7717     wlcm_d("WLAN FW ext_version: %s", wlan.fw_ver_ext.version_str);
7718 
7719 #if CONFIG_WIFI_FW_DEBUG
7720     if (wlan.wlan_usb_init_cb != NULL)
7721         wlan.wlan_usb_init_cb();
7722     else
7723         wifi_d("USB init callback is not registered");
7724 #endif
7725     return ret;
7726 }
7727 
wlan_deinit(int action)7728 void wlan_deinit(int action)
7729 {
7730     if (wlan.running != 0U)
7731     {
7732         (void)send_user_request(CM_WLAN_USER_REQUEST_DEINIT, (unsigned int)action);
7733     }
7734     else
7735     {
7736         wlcm_deinit(action);
7737     }
7738 #ifndef RW610
7739     OSA_RWLockDestroy(&sleep_rwlock);
7740 #endif
7741 }
7742 
7743 #if CONFIG_WPS2
wlcm_wps_callback(enum wps_event event,void * data,uint16_t len)7744 static int wlcm_wps_callback(enum wps_event event, void *data, uint16_t len)
7745 {
7746     int ret                  = WM_SUCCESS;
7747     struct wlan_network *net = (struct wlan_network *)data;
7748 
7749     wlcm_d("WPS EVENT = %d data = %p len=%d", event, data, len);
7750 
7751     if (event == WPS_STARTED) {
7752         wifi_send_wps_cfg_cmd(1); /* Notify wifidriver that wps session has started */
7753     }
7754     else if (event == WPS_SESSION_SUCCESSFUL)
7755     {
7756         if (data == NULL)
7757         {
7758             wifi_send_wps_cfg_cmd(0); /* Notify wifidriver that wps session end */
7759             if (wps_conf.role != 2) /* if not WPS_REGISTRAR */
7760             {
7761                 wlcm_e("Invalid data for WPS SESSION SUCCESSFUL");
7762                 return -WM_FAIL;
7763             }
7764         }
7765         if (len == sizeof(struct wlan_network))
7766         {
7767             /* It's WPS SESSION */
7768             wifi_send_wps_cfg_cmd(0); /* Notify wifidriver that wps session end */
7769             ret = wlan_add_network(net);
7770 
7771             if (ret != WM_SUCCESS)
7772             {
7773                 wlcm_d("Adding network failed");
7774                 return ret;
7775             }
7776 #if CONFIG_NCP_BRIDGE
7777             CONNECTION_EVENT(WLAN_REASON_WPS_SESSION_DONE, net);
7778 #endif
7779             ret = wlan_connect(net->name);
7780             if (ret != WM_SUCCESS)
7781             {
7782                 wlcm_d("Connecting to network failed");
7783                 return ret;
7784             }
7785         }
7786         else
7787         {
7788             /* It's ENTP SESSION */
7789             ret = wlan_wlcmgr_send_msg(WIFI_EVENT_PMK, WIFI_EVENT_REASON_SUCCESS, data);
7790         }
7791 
7792     }
7793     else if (event == WPS_SESSION_TIMEOUT || event == WPS_SESSION_FAILED)
7794     {
7795         wifi_send_wps_cfg_cmd(0); /* Notify wifidriver that wps session end */
7796         ret =
7797             wlan_wlcmgr_send_msg(WIFI_EVENT_AUTHENTICATION, WIFI_EVENT_REASON_FAILURE, (void *)WPA2_ENTERPRISE_FAILED);
7798     }
7799 
7800     return ret;
7801 }
7802 #endif
7803 
assoc_timer_cb(osa_timer_arg_t arg)7804 static void assoc_timer_cb(osa_timer_arg_t arg)
7805 {
7806     wlan.assoc_paused = false;
7807     if (wlan.pending_assoc_request)
7808     {
7809         (void)send_user_request(CM_STA_USER_REQUEST_CONNECT, 0);
7810     }
7811 }
7812 
7813 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
7814 #if CONFIG_WPA_SUPP
supp_status_timer_cb(osa_timer_arg_t arg)7815 static void supp_status_timer_cb(osa_timer_arg_t arg)
7816 {
7817     int ret;
7818 
7819 #if CONFIG_WPA_SUPP_CRYPTO_ENTERPRISE
7820     if (wlan.status_timeout == 60)
7821 #else
7822     if (wlan.status_timeout == 40)
7823 #endif
7824     {
7825         ret = wpa_supp_req_status(wlan.connect ? CONNECT : START);
7826 
7827         if (ret != WM_SUCCESS)
7828         {
7829             (void)send_user_request(wlan.connect ? CM_STA_USER_REQUEST_DISCONNECT : CM_UAP_USER_REQUEST_STOP, 0);
7830         }
7831     }
7832 
7833     wlan.status_timeout++;
7834 }
7835 #endif
7836 #endif
7837 
7838 #if (CONFIG_11K) || (CONFIG_11V)
neighbor_req_timer_cb(osa_timer_arg_t arg)7839 static void neighbor_req_timer_cb(osa_timer_arg_t arg)
7840 {
7841     if (wlan.neighbor_req == true)
7842     {
7843         wlan.neighbor_req = false;
7844         (void)send_user_request(CM_STA_USER_REQUEST_SET_RSSI_THRESHOLD, 0);
7845     }
7846 }
7847 #endif
7848 
7849 #if CONFIG_11R
7850 #if CONFIG_WPA_SUPP
ft_roam_timer_cb(osa_timer_arg_t arg)7851 static void ft_roam_timer_cb(osa_timer_arg_t arg)
7852 {
7853     wlan.roam_reassoc = false;
7854 }
7855 #endif
7856 #endif
7857 
7858 #ifdef RW610
temperature_mon_cb(osa_timer_arg_t arg)7859 static void temperature_mon_cb(osa_timer_arg_t arg)
7860 {
7861 #if CONFIG_WIFI_RECOVERY
7862     if (wifi_recovery_enable || wifi_fw_is_hang())
7863     {
7864         struct wlan_message msg;
7865         (void)memset(&msg, 0U, sizeof(struct wlan_message));
7866         msg.data = NULL;
7867         msg.id  = WIFI_RECOVERY_REQ;
7868         if (OSA_MsgQPut((osa_msgq_handle_t)mon_thread_events, &msg) != KOSA_StatusSuccess)
7869         {
7870             (void)PRINTF("Failed to send wifi recovery msg to queue\r\n");
7871         }
7872         return;
7873     }
7874 #endif
7875     /*
7876      *  get CAU module temperature and write to firmware SMU in every 5s
7877      *  can also read FW power status by REG PMU->WLAN_CTRL 0x4003_1068
7878      *  bit[3:2] == 3 means FW is in sleep status
7879      */
7880     if ((mlan_adap != NULL) && (mlan_adap->ps_state == PS_STATE_AWAKE))
7881     {
7882         wifi_cau_temperature_write_to_firmware();
7883     }
7884 }
7885 #endif
7886 
wlan_wait_wlmgr_ready()7887 static void wlan_wait_wlmgr_ready()
7888 {
7889     while (wlan.sta_state == CM_STA_INITIALIZING)
7890     {
7891         /* wait for wlmgr ready */
7892         OSA_TimeDelay(50);
7893     }
7894 }
7895 
wlan_start(int (* cb)(enum wlan_event_reason reason,void * data))7896 int wlan_start(int (*cb)(enum wlan_event_reason reason, void *data))
7897 {
7898 #ifdef RW610
7899     static bool reset_mutex_init = 0;
7900 #endif
7901     int ret;
7902     osa_status_t status;
7903 
7904     if (!((wlan.status == WLCMGR_INIT_DONE) || (wlan.status == WLCMGR_INACTIVE)))
7905     {
7906         wlcm_e("cannot start wlcmgr. unexpected status: %d", wlan.status);
7907         return WLAN_ERROR_STATE;
7908     }
7909 
7910     if (cb == NULL)
7911     {
7912         return -WM_E_INVAL;
7913     }
7914 
7915     if (wlan.running != 0U)
7916     {
7917         return WLAN_ERROR_STATE;
7918     }
7919 
7920     wlan.sta_state = CM_STA_INITIALIZING;
7921 
7922     wlan.sta_return_to = CM_STA_IDLE;
7923     wlan.uap_state     = CM_UAP_INITIALIZING;
7924     wlan.uap_return_to = CM_UAP_INITIALIZING;
7925 
7926 #if CONFIG_WIFI_STA_RECONNECT
7927     wlan.reassoc_control = true;
7928 #endif
7929     wlan.hidden_scan_on  = false;
7930 
7931     wlcm_process_init_params();
7932 
7933 #if (CONFIG_11K) || (CONFIG_11V) || (CONFIG_ROAMING)
7934     wlan.rssi_low_threshold = 70;
7935 #endif
7936 
7937 #ifdef RW610
7938 #if (CONFIG_WIFI_BLE_COEX_APP) && (CONFIG_WIFI_BLE_COEX_APP == 1)
7939     wlan.wakeup_conditions = (unsigned int)WAKE_ON_UNICAST | (unsigned int)WAKE_ON_MAC_EVENT |
7940                              (unsigned int)WAKE_ON_MULTICAST | (unsigned int)WAKE_ON_ARP_BROADCAST;
7941 #else
7942     wlan.wakeup_conditions = 0;
7943 #endif
7944 #else
7945     wlan.wakeup_conditions = (unsigned int)WAKE_ON_UNICAST | (unsigned int)WAKE_ON_MAC_EVENT |
7946                              (unsigned int)WAKE_ON_MULTICAST | (unsigned int)WAKE_ON_ARP_BROADCAST;
7947 #endif
7948 
7949     wlan.num_networks = 0;
7950     (void)memset(&wlan.networks[0], 0, sizeof(wlan.networks));
7951     (void)memset(&wlan.scan_chan_list, 0, sizeof(wifi_scan_chan_list_t));
7952     wlan.scan_count = 0;
7953     wlan.cb         = cb;
7954     wlan.scan_cb    = NULL;
7955     wlan.internal = 0;
7956 
7957 #if CONFIG_WPA_SUPP
7958 #if CONFIG_WPA_SUPP_CRYPTO_ENTERPRISE
7959     wlan.ca_cert_data     = NULL;
7960     wlan.ca_cert_len      = 0;
7961     wlan.client_cert_data = NULL;
7962     wlan.client_cert_len  = 0;
7963     wlan.client_key_data  = NULL;
7964     wlan.client_key_len   = 0;
7965 #if CONFIG_WPA_SUPP_AP
7966 #if CONFIG_WPA_SUPP_CRYPTO_AP_ENTERPRISE
7967     wlan.dh_data          = NULL;
7968     wlan.dh_len           = 0;
7969     wlan.server_cert_data = NULL;
7970     wlan.server_cert_len  = 0;
7971     wlan.server_key_data  = NULL;
7972     wlan.server_key_len   = 0;
7973 #endif
7974 #endif
7975 #endif
7976 #endif
7977 
7978     status = OSA_MsgQCreate((osa_msgq_handle_t)wlan.events, MAX_EVENTS, sizeof(struct wifi_message));
7979     if (status != KOSA_StatusSuccess)
7980     {
7981         wlcm_e("unable to create event queue: %d", status);
7982         return -WM_FAIL;
7983     }
7984 
7985     ret = wifi_register_event_queue((osa_msgq_handle_t)wlan.events);
7986 
7987     if (ret != WM_SUCCESS)
7988     {
7989         wlcm_e("unable to register event queue");
7990         OSA_MsgQDestroy((osa_msgq_handle_t)wlan.events);
7991         return -WM_FAIL;
7992     }
7993 
7994     status = OSA_TaskCreate((osa_task_handle_t)wlan.wlcmgr_task_Handle, OSA_TASK(wlcmgr_task), NULL);
7995     if (status != KOSA_StatusSuccess)
7996     {
7997         wlan.cb = NULL;
7998         (void)wifi_unregister_event_queue(&wlan.events);
7999         OSA_MsgQDestroy((osa_msgq_handle_t)wlan.events);
8000         return -WM_FAIL;
8001     }
8002 
8003 #if ((CONFIG_11MC) || (CONFIG_11AZ)) && (CONFIG_WLS_CSI_PROC)
8004     status = OSA_SemaphoreCreateBinary((osa_semaphore_handle_t)wls_csi_sem) ;
8005     if (status != KOSA_StatusSuccess)
8006     {
8007         wlcm_e("unable to create wls csi lock: %d", status);
8008         return -WM_FAIL;
8009     }
8010     OSA_SemaphorePost((osa_semaphore_handle_t)wls_csi_sem);
8011 #endif
8012 
8013     if (OSA_SemaphoreCreateBinary((osa_semaphore_handle_t)wlan.scan_lock) != KOSA_StatusSuccess)
8014     {
8015         (void)wifi_unregister_event_queue(&wlan.events);
8016         OSA_MsgQDestroy((osa_msgq_handle_t)wlan.events);
8017         OSA_TaskDestroy((osa_task_handle_t)wlan.wlcmgr_task_Handle);
8018         return -WM_FAIL;
8019     }
8020     OSA_SemaphorePost((osa_semaphore_handle_t)wlan.scan_lock);
8021 
8022 #ifdef RW610
8023     if (!reset_mutex_init)
8024     {
8025         status = OSA_MutexCreate((osa_mutex_handle_t)reset_lock);
8026         if (status != KOSA_StatusSuccess)
8027         {
8028             wlan.cb = NULL;
8029             wifi_unregister_event_queue(&wlan.events);
8030             OSA_MsgQDestroy((osa_msgq_handle_t)wlan.events);
8031             OSA_TaskDestroy((osa_task_handle_t)wlan.wlcmgr_task_Handle);
8032             OSA_SemaphoreDestroy((osa_semaphore_handle_t)wlan.scan_lock);
8033             return -WM_FAIL;
8034         }
8035         reset_mutex_init = 1;
8036     }
8037     if (!mon_thread_init)
8038     {
8039         status = OSA_MsgQCreate((osa_msgq_handle_t)mon_thread_events, MAX_EVENTS, sizeof(struct wlan_message));
8040         if (status != KOSA_StatusSuccess)
8041         {
8042             wlcm_e("unable to create event queue: %d", status);
8043             return -WM_FAIL;
8044         }
8045 #if ((defined(APP_LOWPOWER_ENABLED) && (APP_LOWPOWER_ENABLED == 1)) && \
8046      ((CONFIG_WIFI_BLE_COEX_APP) && (CONFIG_WIFI_BLE_COEX_APP == 1)))
8047         /* For coex app, only register wlan notify callback when APP_LOWPOWER_ENABLED == 1 */
8048 #if CONFIG_HOST_SLEEP
8049 #if CONFIG_POWER_MANAGER
8050         PM_RegisterNotify(kPM_NotifyGroup0, &wlan_notify);
8051 #endif
8052 #endif
8053 #endif
8054         /* Host sleep hanshake will be done in IDLE task and infinite
8055          * while loop is added to wait for hankshake complete to
8056          * prevent IDLE task from entering suspend state.
8057          * If mon_thread using same priority of IDLE task, then the
8058          * mon_thread task could not be scheduled as we did not
8059          * enabled time slice.
8060          */
8061         status = OSA_TaskCreate((osa_task_handle_t)wlcmgr_mon_task_Handle, OSA_TASK(wlcmgr_mon_task), NULL);
8062         if (status != KOSA_StatusSuccess)
8063         {
8064             wlan.cb = NULL;
8065             wifi_unregister_event_queue(&wlan.events);
8066             OSA_MsgQDestroy((osa_msgq_handle_t)wlan.events);
8067             OSA_TaskDestroy((osa_task_handle_t)wlan.wlcmgr_task_Handle);
8068             OSA_SemaphoreDestroy((osa_semaphore_handle_t)wlan.scan_lock);
8069             OSA_MutexDestroy((osa_mutex_handle_t)reset_lock);
8070             return -WM_FAIL;
8071         }
8072         mon_thread_init = 1;
8073     }
8074     wifi_cau_temperature_enable();
8075     status = OSA_TimerCreate((osa_timer_handle_t)temperature_mon_timer, TEMPERATURE_MON_TIMEOUT,
8076                              &temperature_mon_cb, NULL, KOSA_TimerPeriodic, OSA_TIMER_AUTO_ACTIVATE);
8077     if (status != KOSA_StatusSuccess)
8078     {
8079         wlcm_e("Unable to create temperature monitor timer");
8080     }
8081 #endif
8082 
8083     wlan.running = 1;
8084 
8085     wlan.status = WLCMGR_ACTIVATED;
8086 
8087 #if CONFIG_WPS2
8088     wlcm_d("WPS started");
8089     wps_start(&wps_conf);
8090 
8091     status = OSA_TaskCreate((osa_task_handle_t)wlan_wps.wps_task_Handle, OSA_TASK(wps_task), NULL);
8092     if (status != KOSA_StatusSuccess)
8093     {
8094         wlan.cb = NULL;
8095         wifi_unregister_event_queue(&wlan.events);
8096         OSA_MsgQDestroy((osa_msgq_handle_t)wlan.events);
8097         OSA_TaskDestroy((osa_task_handle_t)wlan.wlcmgr_task_Handle);
8098         OSA_SemaphoreDestroy((osa_semaphore_handle_t)wlan.scan_lock);
8099         OSA_MutexDestroy((osa_mutex_handle_t)reset_lock);
8100         return -WM_FAIL;
8101     }
8102 #endif
8103 
8104 #if CONFIG_CPU_LOADING
8105     cpu_loading.status = CPU_LOADING_STATUS_DEAD;
8106     cpu_loading.sampling_period = CPU_LOADING_PERIOD;
8107 #endif
8108 
8109     status = OSA_TimerCreate((osa_timer_handle_t)wlan.assoc_timer, MSEC_TO_TICK(BAD_MIC_TIMEOUT), &assoc_timer_cb, NULL,
8110                           KOSA_TimerOnce, OSA_TIMER_NO_ACTIVATE);
8111     if (status != KOSA_StatusSuccess)
8112     {
8113         wlcm_e("Unable to create unicast bad mic timer");
8114         return -WM_FAIL;
8115     }
8116 
8117 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
8118 #if CONFIG_WPA_SUPP
8119     status = OSA_TimerCreate((osa_timer_handle_t)wlan.supp_status_timer, MSEC_TO_TICK(SUPP_STATUS_TIMEOUT),
8120                           &supp_status_timer_cb, NULL, KOSA_TimerPeriodic, OSA_TIMER_NO_ACTIVATE);
8121     if (status != KOSA_StatusSuccess)
8122     {
8123         wlcm_e("Unable to create supp status timer");
8124         return -WM_FAIL;
8125     }
8126 #endif
8127 #endif
8128 
8129 #if CONFIG_11K
8130 
8131     memset(&wlan.nbr_rpt, 0x00, sizeof(wlan_rrm_neighbor_report_t));
8132 #endif
8133 
8134 #if (CONFIG_11K) || (CONFIG_11V)
8135     status = OSA_TimerCreate((osa_timer_handle_t)wlan.neighbor_req_timer, MSEC_TO_TICK(NEIGHBOR_REQ_TIMEOUT),
8136                           &neighbor_req_timer_cb, NULL, KOSA_TimerOnce, OSA_TIMER_NO_ACTIVATE);
8137     if (status != KOSA_StatusSuccess)
8138     {
8139         wlcm_e("Unable to create neighbor request timer");
8140         return ret;
8141     }
8142 #endif
8143 
8144 #if CONFIG_11R
8145 #if CONFIG_WPA_SUPP
8146     status = OSA_TimerCreate((osa_timer_handle_t)wlan.ft_roam_timer, MSEC_TO_TICK(FT_ROAM_TIMEOUT),
8147                           &ft_roam_timer_cb, NULL, KOSA_TimerOnce, OSA_TIMER_NO_ACTIVATE);
8148     if (status != KOSA_StatusSuccess)
8149     {
8150         wlcm_e("Unable to create ft roam timer");
8151         return -WM_FAIL;
8152     }
8153 #endif
8154 #endif
8155 
8156 #if (CONFIG_WIFI_IND_RESET) && (CONFIG_WIFI_IND_DNLD)
8157 #ifdef IR_OUTBAND_TRIGGER_GPIO
8158     gpio_pin_config_t out_config = {kGPIO_DigitalOutput, 1, kGPIO_NoIntmode};
8159 
8160 #if defined(IOMUXC_GPIO_IR_OUTBAND_TRIGGER)
8161     IOMUXC_SetPinMux(IOMUXC_GPIO_IR_OUTBAND_TRIGGER, /* GPIO_AD_B0_10 is configured as GPIO1_IO10 */
8162                      0U);
8163 #endif
8164     GPIO_PinInit(IR_OUTBAND_TRIGGER_GPIO, IR_OUTBAND_TRIGGER_GPIO_PIN, &out_config);
8165 #endif
8166 
8167 #endif
8168 
8169     wlan_wait_wlmgr_ready();
8170 
8171 #if CONFIG_WIFI_SHELL
8172 #if CONFIG_WIFI_RF_TEST_MODE
8173     ret = wlan_test_mode_cli_init();
8174     if (ret != WM_SUCCESS)
8175     {
8176         PRINTF("Failed to initialize WLAN RF test mode CLIs\r\n");
8177         return 0;
8178     }
8179 #endif
8180 #if !(CONFIG_WIFI_RF_TEST_MODE) || (CONFIG_NXP_RW610)
8181     ret = wlan_basic_cli_init();
8182     if (ret != WM_SUCCESS)
8183     {
8184         PRINTF("Failed to initialize BASIC WLAN CLIs\r\n");
8185         return 0;
8186     }
8187     ret = wlan_cli_init();
8188     if (ret != WM_SUCCESS)
8189     {
8190         PRINTF("Failed to initialize WLAN CLIs\r\n");
8191         return 0;
8192     }
8193 #if CONFIG_SIGMA_AGENT
8194     ret = ping_cli_init();
8195     if (ret != WM_SUCCESS)
8196     {
8197         PRINTF("Failed to initialize PING CLI\r\n");
8198         return 0;
8199     }
8200 #endif
8201     ret = wlan_enhanced_cli_init();
8202     if (ret != WM_SUCCESS)
8203     {
8204         PRINTF("Failed to initialize WLAN Enhanced CLIs\r\n");
8205         return 0;
8206     }
8207 #if CONFIG_WPA_SUPP
8208 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
8209     ret = wpa_cli_init();
8210     if (ret != WM_SUCCESS)
8211     {
8212         PRINTF("Failed to initialize WPA SUPP CLIs\r\n");
8213         return 0;
8214     }
8215 #endif
8216 #endif
8217 #endif
8218 #endif
8219 
8220     return WM_SUCCESS;
8221 }
8222 
wlan_stop(void)8223 int wlan_stop(void)
8224 {
8225     osa_status_t status = KOSA_StatusIdle;
8226     int ret = WM_SUCCESS;
8227 #ifndef RW610
8228     int total_wait_time = 1000; /* millisecs */
8229     int check_interval  = 200;  /* millisecs */
8230     int num_iterations  = total_wait_time / check_interval;
8231 #endif
8232     if (wlan.status != WLCMGR_ACTIVATED)
8233     {
8234         wlcm_e("cannot stop wlcmgr. unexpected status: %d", wlan.status);
8235         return WLAN_ERROR_STATE;
8236     }
8237 
8238     if (!wlan.running)
8239     {
8240         wlcm_e("cannot stop wlcmgr. unexpected wlan.running: %d", wlan.running);
8241         return WLAN_ERROR_STATE;
8242     }
8243 #if OTP_CHANINFO
8244     wifi_free_fw_region_and_cfp_tables();
8245 #endif
8246 
8247 #ifndef RW610
8248     wlan.running = 0;
8249     wlan.scan_cb = NULL;
8250 
8251 #if CONFIG_WPA_SUPP
8252 
8253     wifi_supp_deinit();
8254 
8255     ret = wpa_supp_deinit();
8256     if (ret != 0)
8257     {
8258         wlcm_e("wpa_supp_deinit failed. status code %d", ret);
8259         return WLAN_ERROR_STATE;
8260     }
8261 #endif
8262 
8263     /* We need to wait for scan_lock as wifi scan might have been
8264      * scheduled, so it must be completed before deleting cm_main_thread
8265      * here. Otherwise deadlock situation might arrive as both of them
8266      * share command_lock semaphore.
8267      */
8268     status = OSA_SemaphoreWait((osa_semaphore_handle_t)wlan.scan_lock, osaWaitForever_c);
8269     if (status != KOSA_StatusSuccess)
8270     {
8271         wlcm_w("failed to get scan lock: %d.", ret);
8272         return WLAN_ERROR_STATE;
8273     }
8274 #else
8275 #if CONFIG_WIFI_RECOVERY && UAP_SUPPORT
8276     /* If CONFIG_WIFI_RECOVERY is defined, 0xb2 CMD will be skipped, but dhcp_server_stop()
8277      * is called in 0xb2 CMD response. So it needs to be called here to stop DHCP server
8278      */
8279     if (wifi_recovery_enable)
8280         net_dhcpv4_server_stop((struct net_if *)net_get_uap_interface());
8281 #endif
8282 #endif
8283 
8284     status = OSA_SemaphoreDestroy((osa_semaphore_handle_t)wlan.scan_lock);
8285     if (status != KOSA_StatusSuccess)
8286     {
8287         wlcm_w("failed to delete scan lock: %d.", ret);
8288         return WLAN_ERROR_STATE;
8289     }
8290     wlan.is_scan_lock = 0;
8291 
8292     wlan.scan_cb = NULL;
8293 
8294     status = OSA_TimerDestroy((osa_timer_handle_t)wlan.assoc_timer);
8295     if (status != KOSA_StatusSuccess)
8296     {
8297         wlcm_w("failed to delete assoc timer: %d.", ret);
8298         return WLAN_ERROR_STATE;
8299     }
8300     wlan.scan_cb = NULL;
8301 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
8302 #if CONFIG_WPA_SUPP
8303     status = OSA_TimerDestroy((osa_timer_handle_t)wlan.supp_status_timer);
8304     if (status != KOSA_StatusSuccess)
8305     {
8306         wlcm_w("failed to delete supp status timer: %d.", ret);
8307         return WLAN_ERROR_STATE;
8308     }
8309 #endif
8310 #endif
8311 
8312 #if (CONFIG_11K) || (CONFIG_11V)
8313     status = OSA_TimerDestroy((osa_timer_handle_t)wlan.neighbor_req_timer);
8314     if (status != KOSA_StatusSuccess)
8315     {
8316         wlcm_w("failed to delete neighbor req timer: %d.", ret);
8317         return WLAN_ERROR_STATE;
8318     }
8319 #endif
8320 
8321 #if CONFIG_11R
8322 #if CONFIG_WPA_SUPP
8323     status = OSA_TimerDestroy((osa_timer_handle_t)wlan.ft_roam_timer);
8324     if (status != KOSA_StatusSuccess)
8325     {
8326         wlcm_w("failed to delete ft roam timer: %d.", ret);
8327         return WLAN_ERROR_STATE;
8328     }
8329 #endif
8330 #endif
8331 
8332 #ifdef RW610
8333     status = OSA_TimerDestroy((osa_timer_handle_t)temperature_mon_timer);
8334     if (status != KOSA_StatusSuccess)
8335     {
8336         wlcm_w("failed to delete temperature monitor timer: %d.", ret);
8337         return WLAN_ERROR_STATE;
8338     }
8339 #else
8340     /* We need to tell the AP that we're going away, however we've already
8341      * stopped the main thread so we can't do this by means of the state
8342      * machine.  Unregister from the wifi interface and explicitly send a
8343      * deauth request and then proceed to tearing the main thread down. */
8344 
8345     /* Set stop_request and wait for wlcmgr thread to acknowledge it */
8346     wlan.stop_request = (uint8_t) true;
8347 
8348     wlcm_d("Sent wlcmgr shutdown request. Current State: %d", wlan.status);
8349 
8350     (void)send_user_request(CM_WLAN_USER_REQUEST_SHUTDOWN, 0);
8351 
8352     --num_iterations;
8353     while (wlan.status != WLCMGR_THREAD_STOPPED && num_iterations)
8354     {
8355         --num_iterations;
8356         OSA_TimeDelay((uint32_t)check_interval);
8357     }
8358 
8359     if (wlan.status != WLCMGR_THREAD_STOPPED && !num_iterations)
8360     {
8361         wlcm_d("Timed out waiting for wlcmgr to stop\r\n");
8362         wlcm_d("Forcing halt for wlcmgr thread\r\n");
8363         /* Reinitiailize variable states */
8364         wlan.status = WLCMGR_THREAD_STOPPED;
8365     }
8366 
8367     wlan.stop_request = (uint8_t) false;
8368 #endif
8369     if (wm_wifi.wlc_mgr_event_queue)
8370     {
8371         ret = wifi_unregister_event_queue(&wlan.events);
8372 
8373         if (ret != WM_SUCCESS)
8374         {
8375             wlcm_w("failed to unregister wifi event queue: %d", ret);
8376             return WLAN_ERROR_STATE;
8377         }
8378     }
8379 
8380     status = OSA_MsgQDestroy((osa_msgq_handle_t)wlan.events);
8381 
8382     if (status != KOSA_StatusSuccess)
8383     {
8384         wlcm_w("failed to delete event queue: %d", ret);
8385         return WLAN_ERROR_STATE;
8386     }
8387 
8388 #if CONFIG_HOST_SLEEP
8389     OSA_SemaphoreDestroy((osa_semaphore_handle_t)wakelock);
8390 #endif
8391 #if ((CONFIG_11MC) || (CONFIG_11AZ)) && (CONFIG_WLS_CSI_PROC)
8392     (void)OSA_SemaphoreDestroy((osa_semaphore_handle_t)wls_csi_sem);
8393 #endif
8394 
8395 #ifndef RW610
8396     if (wlan.sta_state > CM_STA_ASSOCIATING)
8397     {
8398         (void)wifi_deauthenticate((uint8_t *)wlan.networks[wlan.cur_network_idx].bssid);
8399         wlan.sta_return_to = CM_STA_IDLE;
8400     }
8401     if (wlan.uap_state > CM_UAP_CONFIGURED)
8402     {
8403         (void)wifi_uap_stop();
8404 //        (void)dhcp_server_stop();
8405     }
8406 
8407     status = OSA_TaskDestroy((osa_task_handle_t)wlan.wlcmgr_task_Handle);
8408 
8409     if (status != KOSA_StatusSuccess)
8410     {
8411         wlcm_w("failed to terminate thread: %d", ret);
8412         return WLAN_ERROR_STATE;
8413     }
8414 
8415     (void)net_wlan_deinit();
8416 
8417     wlan.status = WLCMGR_INIT_DONE;
8418     wlcm_d("WLCMGR thread deleted\n\r");
8419 #else
8420     wlan.running = 0;
8421     wlan.status  = WLCMGR_INACTIVE;
8422     memset(&wlan, 0x00, sizeof(wlan));
8423 
8424     wifi_deinit();
8425 
8426     OSA_RWLockDestroy(&sleep_rwlock);
8427 #endif
8428 
8429 #if CONFIG_WMM_UAPSD
8430     OSA_SemaphoreDestroy((osa_semaphore_handle_t)uapsd_sem);
8431 #endif
8432     return ret;
8433 }
8434 
wlan_initialize_uap_network(struct wlan_network * net)8435 void wlan_initialize_uap_network(struct wlan_network *net)
8436 {
8437     (void)memset(net, 0, sizeof(struct wlan_network));
8438     /* Set profile name */
8439     (void)strcpy(net->name, "uap-network");
8440     /* Set channel selection to auto (0) */
8441     net->channel = 0;
8442     /* Set network type to uAP */
8443     net->type = WLAN_BSS_TYPE_UAP;
8444     /* Set network role to uAP */
8445     net->role = WLAN_BSS_ROLE_UAP;
8446     /* Specify address type as static assignment */
8447     net->ip.ipv4.addr_type = ADDR_TYPE_STATIC;
8448 }
8449 
wlan_initialize_sta_network(struct wlan_network * net)8450 void wlan_initialize_sta_network(struct wlan_network *net)
8451 {
8452     (void)memset(net, 0, sizeof(struct wlan_network));
8453     /* Set profile name */
8454     (void)strcpy(net->name, "sta-network");
8455     /* Set channel selection to auto (0) */
8456     net->channel = 0;
8457     /* Set network type to sta */
8458     net->type = WLAN_BSS_TYPE_STA;
8459     /* Set network role to sta */
8460     net->role = WLAN_BSS_ROLE_STA;
8461     /* Specify address type as dynamic assignment */
8462     net->ip.ipv4.addr_type = ADDR_TYPE_DHCP;
8463 }
8464 
isHexNumber(const char * str,const uint8_t len)8465 static bool isHexNumber(const char *str, const uint8_t len)
8466 {
8467     for (int i = 0; i < len; ++i)
8468     {
8469         if (('0' > str[i] || '9' < str[i]) && ('A' > str[i] || 'F' < str[i]) && ('a' > str[i] || 'f' < str[i]))
8470         {
8471             return false;
8472         }
8473     }
8474 
8475     return true;
8476 }
8477 
wlan_is_key_valid(struct wlan_network * network)8478 static bool wlan_is_key_valid(struct wlan_network *network)
8479 {
8480     enum wlan_security_type type = network->security.type;
8481     bool valid                   = true;
8482 
8483     switch (type)
8484     {
8485         case WLAN_SECURITY_WPA:
8486         case WLAN_SECURITY_WPA2:
8487         case WLAN_SECURITY_WPA_WPA2_MIXED:
8488 #if CONFIG_11R
8489         case WLAN_SECURITY_WPA2_FT:
8490 #endif
8491             /* check the length of PSK phrase */
8492             if (network->security.psk_len < WLAN_PSK_MIN_LENGTH || network->security.psk_len >= WLAN_PSK_MAX_LENGTH)
8493             {
8494                 wlcm_e(
8495                     "Invalid passphrase length %d "
8496                     "(expected ASCII characters: 8..63)",
8497                     network->security.psk_len);
8498                 return false;
8499             }
8500             if ((network->security.psk_len == WLAN_PSK_MAX_LENGTH - 1) &&
8501                 (isHexNumber(network->security.psk, network->security.psk_len) == false))
8502             {
8503                 wlcm_e(
8504                     "Invalid hexadecimal digits psk"
8505                     "(expected Hexadecimal digits: 64)");
8506                 return false;
8507             }
8508             break;
8509         case WLAN_SECURITY_WPA2_WPA3_SAE_MIXED:
8510             /* check the length of PSK phrase */
8511             if (network->security.psk_len < WLAN_PSK_MIN_LENGTH || network->security.psk_len >= WLAN_PSK_MAX_LENGTH)
8512             {
8513                 wlcm_e(
8514                     "Invalid passphrase length %d "
8515                     "(expected ASCII characters: 8..63)",
8516                     network->security.psk_len);
8517                 return false;
8518             }
8519             if ((network->security.psk_len == WLAN_PSK_MAX_LENGTH - 1) &&
8520                 (isHexNumber(network->security.psk, network->security.psk_len) == false))
8521             {
8522                 wlcm_e(
8523                     "Invalid hexadecimal digits psk"
8524                     "(expected Hexadecimal digits: 64)");
8525                 return false;
8526             }
8527         case WLAN_SECURITY_WPA3_SAE:
8528 #if CONFIG_WPA_SUPP
8529 #if CONFIG_11R
8530         case WLAN_SECURITY_WPA3_FT_SAE:
8531 #endif
8532 #endif
8533         case WLAN_SECURITY_WPA3_SAE_EXT_KEY:
8534             if (network->security.password_len < WLAN_PASSWORD_MIN_LENGTH ||
8535                 network->security.password_len > WLAN_PASSWORD_MAX_LENGTH)
8536             {
8537                 wlcm_e("Invalid password length %d (expected 8..255)", network->security.password_len);
8538                 return false;
8539             }
8540             break;
8541         case WLAN_SECURITY_NONE:
8542         case WLAN_SECURITY_WILDCARD:
8543 #if CONFIG_DRIVER_OWE
8544         case WLAN_SECURITY_OWE_ONLY:
8545 #endif
8546 #if CONFIG_WPA2_ENTP
8547         case WLAN_SECURITY_EAP_TLS:
8548 #elif CONFIG_WPA_SUPP
8549 #if CONFIG_WPA_SUPP_CRYPTO_ENTERPRISE
8550 #if CONFIG_EAP_TLS
8551         case WLAN_SECURITY_EAP_TLS:
8552         case WLAN_SECURITY_EAP_TLS_SHA256:
8553 #if CONFIG_11R
8554         case WLAN_SECURITY_EAP_TLS_FT:
8555         case WLAN_SECURITY_EAP_TLS_FT_SHA384:
8556 #endif
8557 #endif
8558 #if CONFIG_EAP_TTLS
8559         case WLAN_SECURITY_EAP_TTLS:
8560 #if CONFIG_EAP_MSCHAPV2
8561         case WLAN_SECURITY_EAP_TTLS_MSCHAPV2:
8562 #endif
8563 #endif
8564 #if CONFIG_EAP_PEAP
8565 #if CONFIG_EAP_MSCHAPV2
8566         case WLAN_SECURITY_EAP_PEAP_MSCHAPV2:
8567 #endif
8568 #if CONFIG_EAP_TLS
8569         case WLAN_SECURITY_EAP_PEAP_TLS:
8570 #endif
8571 #if CONFIG_EAP_GTC
8572         case WLAN_SECURITY_EAP_PEAP_GTC:
8573 #endif
8574 #endif
8575 #if CONFIG_EAP_SIM
8576         case WLAN_SECURITY_EAP_SIM:
8577 #endif
8578 #if CONFIG_EAP_AKA
8579         case WLAN_SECURITY_EAP_AKA:
8580 #endif
8581 #if CONFIG_EAP_AKA_PRIME
8582         case WLAN_SECURITY_EAP_AKA_PRIME:
8583 #endif
8584 #if CONFIG_EAP_FAST
8585 #if CONFIG_EAP_MSCHAPV2
8586         case WLAN_SECURITY_EAP_FAST_MSCHAPV2:
8587 #endif
8588 #if CONFIG_EAP_GTC
8589         case WLAN_SECURITY_EAP_FAST_GTC:
8590 #endif
8591 #endif
8592 #endif
8593 #endif
8594 #if CONFIG_PEAP_MSCHAPV2
8595         case WLAN_SECURITY_EAP_PEAP_MSCHAPV2:
8596 #endif
8597             valid = true;
8598             break;
8599         case WLAN_SECURITY_WEP_OPEN:
8600         case WLAN_SECURITY_WEP_SHARED:
8601             valid = false;
8602             break;
8603         default:
8604 
8605             valid = false;
8606             break;
8607     }
8608 
8609     if (valid == false)
8610     {
8611         return false;
8612     }
8613     else
8614     {
8615         return true;
8616     }
8617 }
8618 
8619 #if CONFIG_WPA_SUPP
8620 #if CONFIG_WPA_SUPP_CRYPTO_ENTERPRISE
8621 #if CONFIG_EAP_TLS
wlan_is_eap_tls_security(enum wlan_security_type security)8622 static bool wlan_is_eap_tls_security(enum wlan_security_type security)
8623 {
8624     if ((security == WLAN_SECURITY_EAP_TLS) || (security == WLAN_SECURITY_EAP_TLS_SHA256)
8625 #if CONFIG_11R
8626         || (security == WLAN_SECURITY_EAP_TLS_FT) || (security == WLAN_SECURITY_EAP_TLS_FT_SHA384)
8627 #endif
8628        )
8629         return true;
8630     return false;
8631 }
8632 #endif
8633 
8634 #if CONFIG_EAP_TTLS
wlan_is_eap_ttls_security(enum wlan_security_type security)8635 static bool wlan_is_eap_ttls_security(enum wlan_security_type security)
8636 {
8637     if (security == WLAN_SECURITY_EAP_TTLS)
8638         return true;
8639 #if CONFIG_EAP_MSCHAPV2
8640     if (security == WLAN_SECURITY_EAP_TTLS_MSCHAPV2)
8641         return true;
8642 #endif
8643 
8644     return false;
8645 }
8646 #endif
8647 
8648 #if CONFIG_EAP_PEAP
wlan_is_eap_peap_security(enum wlan_security_type security)8649 static bool wlan_is_eap_peap_security(enum wlan_security_type security)
8650 {
8651 #if CONFIG_EAP_MSCHAPV2
8652     if (security == WLAN_SECURITY_EAP_PEAP_MSCHAPV2)
8653         return true;
8654 #endif
8655 #if CONFIG_EAP_GTC
8656     if (security == WLAN_SECURITY_EAP_PEAP_GTC)
8657         return true;
8658 #endif
8659 #if CONFIG_EAP_TLS
8660     if (security == WLAN_SECURITY_EAP_PEAP_TLS)
8661         return true;
8662 #endif
8663     return false;
8664 }
8665 #endif
8666 
wlan_is_eap_fast_security(enum wlan_security_type security)8667 static bool wlan_is_eap_fast_security(enum wlan_security_type security)
8668 {
8669 #if CONFIG_EAP_MSCHAPV2
8670     if (security == WLAN_SECURITY_EAP_FAST_MSCHAPV2)
8671         return true;
8672 #endif
8673 #if CONFIG_EAP_GTC
8674     if (security == WLAN_SECURITY_EAP_FAST_GTC)
8675         return true;
8676 #endif
8677     return false;
8678 }
8679 
wlan_is_skip_cert_cfg(enum wlan_security_type security)8680 static bool wlan_is_skip_cert_cfg(enum wlan_security_type security)
8681 {
8682 #if CONFIG_EAP_MSCHAPV2
8683     if (security == WLAN_SECURITY_EAP_TTLS_MSCHAPV2)
8684         return true;
8685 
8686     if (security == WLAN_SECURITY_EAP_PEAP_MSCHAPV2)
8687         return true;
8688 #endif
8689 
8690     return false;
8691 }
8692 
8693 #endif
8694 #endif
8695 
wlan_key_mgmt_wpa_psk(int akm)8696 static int wlan_key_mgmt_wpa_psk(int akm)
8697 {
8698     int rakm = (WLAN_KEY_MGMT_PSK | WLAN_KEY_MGMT_FT_PSK | WLAN_KEY_MGMT_PSK_SHA256);
8699 
8700     if (akm == 0)
8701     {
8702         return 0;
8703     }
8704 
8705     akm &= ~(WLAN_KEY_MGMT_PSK | WLAN_KEY_MGMT_FT_PSK | WLAN_KEY_MGMT_PSK_SHA256);
8706 
8707     return (!akm && rakm);
8708 }
8709 
8710 #if CONFIG_11R
wlan_key_mgmt_ft_psk(int akm)8711 static int wlan_key_mgmt_ft_psk(int akm)
8712 {
8713     int rakm = WLAN_KEY_MGMT_FT_PSK;
8714 
8715     if (akm == 0)
8716     {
8717         return 0;
8718     }
8719 
8720     akm &= ~WLAN_KEY_MGMT_FT_PSK;
8721 
8722     return (!akm && rakm);
8723 }
8724 #endif
8725 
wlan_key_mgmt_sae(int akm)8726 static int wlan_key_mgmt_sae(int akm)
8727 {
8728     int rakm;
8729 
8730     if (akm == 0)
8731     {
8732         return 0;
8733     }
8734 
8735     rakm = (
8736 #if CONFIG_WPA_SUPP_DPP
8737             WLAN_KEY_MGMT_DPP |
8738 #endif
8739             WLAN_KEY_MGMT_SAE |
8740             WLAN_KEY_MGMT_SAE_EXT_KEY);
8741 
8742     akm &= ~(
8743 #if CONFIG_WPA_SUPP_DPP
8744             WLAN_KEY_MGMT_DPP |
8745 #endif
8746             WLAN_KEY_MGMT_SAE |
8747             WLAN_KEY_MGMT_SAE_EXT_KEY);
8748 
8749     return (!akm && rakm);
8750 }
8751 
8752 #if CONFIG_WPA_SUPP
8753 #if CONFIG_11R
wlan_key_mgmt_ft_sae(int akm)8754 static int wlan_key_mgmt_ft_sae(int akm)
8755 {
8756     int rakm =~WLAN_KEY_MGMT_FT_SAE;
8757 
8758     if (akm == 0)
8759     {
8760         return 0;
8761     }
8762 
8763     akm &= ~WLAN_KEY_MGMT_FT_SAE;
8764 
8765     return (!akm && rakm);
8766 }
8767 #endif
8768 #endif
8769 
wlan_key_mgmt_wpa_psk_sae(int akm)8770 static int wlan_key_mgmt_wpa_psk_sae(int akm)
8771 {
8772     int rakm;
8773 
8774     if (akm == 0)
8775     {
8776         return 0;
8777     }
8778 
8779     rakm = (
8780 #if CONFIG_WPA_SUPP_DPP
8781                 WLAN_KEY_MGMT_DPP |
8782 #endif
8783                 WLAN_KEY_MGMT_PSK | WLAN_KEY_MGMT_PSK_SHA256 | WLAN_KEY_MGMT_SAE);
8784 
8785     akm &= ~(
8786 #if CONFIG_WPA_SUPP_DPP
8787             WLAN_KEY_MGMT_DPP |
8788 #endif
8789             WLAN_KEY_MGMT_PSK | WLAN_KEY_MGMT_PSK_SHA256 | WLAN_KEY_MGMT_SAE);
8790 
8791     return (!akm && rakm);
8792 }
8793 
8794 #if CONFIG_DRIVER_OWE
wlan_key_mgmt_owe(int akm)8795 static int wlan_key_mgmt_owe(int akm)
8796 {
8797     int rakm = WLAN_KEY_MGMT_OWE;
8798 
8799     if (akm == 0)
8800     {
8801         return 0;
8802     }
8803 
8804     akm &= ~WLAN_KEY_MGMT_OWE;
8805 
8806     return (!akm && rakm);
8807 }
8808 #endif
8809 
8810 #if CONFIG_WPA_SUPP_DPP
wlan_key_mgmt_dpp(int akm)8811 static int wlan_key_mgmt_dpp(int akm)
8812 {
8813     int rakm = WLAN_KEY_MGMT_DPP;
8814 
8815     if (akm == 0)
8816     {
8817         return 0;
8818     }
8819 
8820     akm &= ~WLAN_KEY_MGMT_DPP;
8821 
8822     return (!akm && rakm);
8823 }
8824 #endif
8825 
wlan_add_network(struct wlan_network * network)8826 int wlan_add_network(struct wlan_network *network)
8827 {
8828     int pos = -1;
8829     int i;
8830     unsigned int len;
8831 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
8832     int ret = 0;
8833 #endif
8834     if (!wlan.running)
8835     {
8836         return WLAN_ERROR_STATE;
8837     }
8838 
8839 #if CONFIG_WPA_SUPP
8840     struct netif *netif = net_get_sta_interface();
8841 #endif
8842 
8843     if (network == NULL)
8844     {
8845         return -WM_E_INVAL;
8846     }
8847 
8848     if ((network->channel > MAX_CHANNELS_BG) && ISSUPP_NO5G(mlan_adap->fw_cap_ext))
8849     {
8850         wlcm_e("Not support 5G, please set 2G channel");
8851         return -WM_E_INVAL;
8852     }
8853     if ((network->acs_band == 1) && ISSUPP_NO5G(mlan_adap->fw_cap_ext))
8854     {
8855         wlcm_e("Not support 5G, please not set acs_band 1");
8856         return -WM_E_INVAL;
8857     }
8858 
8859     if (network->role == WLAN_BSS_ROLE_STA)
8860     {
8861         if (is_running() && !is_state(CM_STA_IDLE) && !is_sta_associated())
8862         {
8863             return WLAN_ERROR_STATE;
8864         }
8865     }
8866 
8867 #if !UAP_SUPPORT
8868     if (network->role != WLAN_BSS_ROLE_STA)
8869     {
8870         wlcm_e("wlan_add_network UAP not supported");
8871         return -WM_E_INVAL;
8872     }
8873 #endif
8874 
8875     /* make sure that the network name length is acceptable */
8876     len = strlen(network->name);
8877     if (len < WLAN_NETWORK_NAME_MIN_LENGTH || len >= WLAN_NETWORK_NAME_MAX_LENGTH)
8878     {
8879         wlcm_e("name length is out of bounds");
8880         return -WM_E_INVAL;
8881     }
8882 
8883     /* make sure that either the SSID or BSSID field is present */
8884     if (network->ssid[0] == '\0' && is_bssid_any(network->bssid))
8885     {
8886         wlcm_e("SSID or BSSID is required");
8887         return -WM_E_INVAL;
8888     }
8889 
8890     if ((network->role == WLAN_BSS_ROLE_UAP) && (network->ip.ipv4.gw != network->ip.ipv4.address))
8891     {
8892         wlcm_e("IP address and Default gateway must be same for uAP");
8893         return -WM_E_INVAL;
8894     }
8895 
8896     /* Always set PMF capable bt default */
8897     network->security.mfpc = 1;
8898 
8899     if (((network->role == WLAN_BSS_ROLE_UAP) || (network->role == WLAN_BSS_ROLE_STA)) &&
8900         ((network->security.type == WLAN_SECURITY_WPA3_SAE)
8901         || (network->security.type == WLAN_SECURITY_WPA3_SAE_EXT_KEY)
8902 #if CONFIG_DRIVER_OWE
8903          || (network->security.type == WLAN_SECURITY_OWE_ONLY)
8904 #endif
8905          ))
8906     {
8907         network->security.mfpr = 1;
8908     }
8909 
8910 #if CONFIG_WPA2_ENTP
8911     /* make sure that if in policy wireless connection is allowed
8912      * only with WPA2 Enterprise AP then add the network
8913      */
8914     if ((network->role == WLAN_BSS_ROLE_STA) &&
8915         ((network->security.type != WLAN_SECURITY_EAP_TLS) ||
8916          (network->security.type != WLAN_SECURITY_EAP_PEAP_MSCHAPV2)) &&
8917         wlan.allow_wpa2_enterprise_ap_only)
8918     {
8919         return -WM_E_INVAL;
8920     }
8921 #endif
8922 
8923 #if CONFIG_WPA_SUPP
8924 #if CONFIG_WPA_SUPP_CRYPTO_ENTERPRISE
8925     if ((is_ep_valid_security(network->security.type)) && ((network->security.wpa3_sb == 1U) || (network->security.wpa3_sb_192 == 1U)))
8926     {
8927         network->security.mfpr = 1;
8928     }
8929 #endif
8930 #endif
8931 
8932 #if CONFIG_11R
8933     if ((network->role == WLAN_BSS_ROLE_STA) &&
8934             ((network->security.type == WLAN_SECURITY_WPA2_FT)
8935 #if CONFIG_WPA_SUPP
8936              || (network->security.type == WLAN_SECURITY_WPA3_FT_SAE)
8937 #endif
8938             ) && (network->channel != 0U))
8939     {
8940         wlcm_e("Specific channel not allowed in FT security");
8941         return -WM_E_INVAL;
8942     }
8943 #endif
8944 
8945     if (!is_valid_security(network->security.type))
8946     {
8947         wlcm_e("Invalid security type is configured");
8948         return -WM_E_INVAL;
8949     }
8950 
8951     if (network->security.key_mgmt == 0)
8952     {
8953         if (network->security.type == WLAN_SECURITY_NONE)
8954         {
8955             network->security.key_mgmt = WLAN_KEY_MGMT_NONE;
8956         }
8957         else if ((network->security.type == WLAN_SECURITY_WPA) || (network->security.type == WLAN_SECURITY_WPA2) || (network->security.type == WLAN_SECURITY_WPA_WPA2_MIXED))
8958         {
8959             network->security.key_mgmt = WLAN_KEY_MGMT_PSK;
8960         }
8961         else if (network->security.type == WLAN_SECURITY_WPA3_SAE)
8962         {
8963             network->security.key_mgmt = WLAN_KEY_MGMT_SAE;
8964         }
8965         else if (network->security.type == WLAN_SECURITY_WPA3_SAE_EXT_KEY)
8966         {
8967             network->security.key_mgmt = WLAN_KEY_MGMT_SAE_EXT_KEY;
8968         }
8969 #if CONFIG_DRIVER_OWE
8970         else if (network->security.type == WLAN_SECURITY_OWE_ONLY)
8971         {
8972             network->security.key_mgmt = WLAN_KEY_MGMT_OWE;
8973         }
8974 #endif
8975 #if CONFIG_11R
8976         else if (network->security.type == WLAN_SECURITY_WPA2_FT)
8977         {
8978             network->security.key_mgmt = WLAN_KEY_MGMT_FT_PSK;
8979         }
8980 #if CONFIG_WPA_SUPP
8981         else if (network->security.type == WLAN_SECURITY_WPA3_FT_SAE)
8982         {
8983             network->security.key_mgmt = WLAN_KEY_MGMT_FT_SAE;
8984         }
8985 #endif
8986 #endif
8987         else if (network->security.type == WLAN_SECURITY_WPA2_WPA3_SAE_MIXED)
8988         {
8989             network->security.key_mgmt = WLAN_KEY_MGMT_PSK | WLAN_KEY_MGMT_SAE;
8990         }
8991     }
8992 
8993     if ((network->role == WLAN_BSS_ROLE_UAP) || (network->role == WLAN_BSS_ROLE_STA))
8994     {
8995         if (network->role == WLAN_BSS_ROLE_STA)
8996         {
8997             if ((network->security.type == WLAN_SECURITY_WPA2) && (network->security.mfpc))
8998             {
8999                 network->security.key_mgmt |= WLAN_KEY_MGMT_PSK_SHA256;
9000             }
9001             if ((network->security.type == WLAN_SECURITY_WPA_WPA2_MIXED) && (network->security.mfpr))
9002             {
9003                 network->security.key_mgmt |= WLAN_KEY_MGMT_PSK_SHA256;
9004             }
9005 	}
9006         if (network->role == WLAN_BSS_ROLE_UAP)
9007         {
9008             if ((network->security.type == WLAN_SECURITY_WPA2) && (network->security.mfpr))
9009             {
9010                 network->security.key_mgmt |= WLAN_KEY_MGMT_PSK_SHA256;
9011             }
9012         }
9013     }
9014 
9015     if (((network->role == WLAN_BSS_ROLE_UAP) || (network->role == WLAN_BSS_ROLE_STA)) &&
9016         ((((network->security.type == WLAN_SECURITY_WPA) || (network->security.type == WLAN_SECURITY_WPA2) || (network->security.type == WLAN_SECURITY_WPA_WPA2_MIXED)) &&
9017         (!wlan_key_mgmt_wpa_psk(network->security.key_mgmt)))
9018 #if CONFIG_11R
9019         || ((network->security.type == WLAN_SECURITY_WPA2_FT) && (!wlan_key_mgmt_ft_psk(network->security.key_mgmt)))
9020 #endif
9021         || ((network->security.type == WLAN_SECURITY_WPA3_SAE) && (!wlan_key_mgmt_sae(network->security.key_mgmt)))
9022         || ((network->security.type == WLAN_SECURITY_WPA3_SAE_EXT_KEY) && (!wlan_key_mgmt_sae(network->security.key_mgmt)))
9023 #if CONFIG_WPA_SUPP
9024 #if CONFIG_11R
9025         || ((network->security.type == WLAN_SECURITY_WPA3_FT_SAE) && (!wlan_key_mgmt_ft_sae(network->security.key_mgmt)))
9026 #endif
9027 #endif
9028         || ((network->security.type == WLAN_SECURITY_WPA2_WPA3_SAE_MIXED) && (!wlan_key_mgmt_wpa_psk_sae(network->security.key_mgmt)))
9029 #if CONFIG_DRIVER_OWE
9030         || ((network->security.type == WLAN_SECURITY_OWE_ONLY) && (!wlan_key_mgmt_owe(network->security.key_mgmt)))
9031 #endif
9032 #if CONFIG_WPA_SUPP_DPP
9033         || ((network->security.type == WLAN_SECURITY_DPP) && (!wlan_key_mgmt_dpp(network->security.key_mgmt)))
9034 #endif
9035         ))
9036     {
9037         wlcm_e("Invalid security/key mgmt is configured");
9038         return -WM_E_INVAL;
9039     }
9040 
9041     if (((network->role == WLAN_BSS_ROLE_UAP) || (network->role == WLAN_BSS_ROLE_STA)) &&
9042         (network->security.key_mgmt == WLAN_KEY_MGMT_PSK_SHA256) &&
9043         (!network->security.mfpc && !network->security.mfpr))
9044     {
9045         wlcm_e("MFP is not configured");
9046         return -WM_E_INVAL;
9047     }
9048 
9049     if (((network->role == WLAN_BSS_ROLE_UAP) || (network->role == WLAN_BSS_ROLE_STA)) &&
9050         (network->security.type == WLAN_SECURITY_WPA2_WPA3_SAE_MIXED) &&
9051         (!(network->security.mfpc && !network->security.mfpr)))
9052     {
9053         wlcm_e("MFP capable only is allowed");
9054         return -WM_E_INVAL;
9055     }
9056 
9057     if (wlan_is_key_valid(network) == false)
9058     {
9059         wlcm_e("Invalid passphrase/password is configured");
9060         return -WM_E_INVAL;
9061     }
9062 
9063     /* Make sure network type is set correctly if not
9064      * set correct values as per role*/
9065     if ((network->type == WLAN_BSS_TYPE_STA) || (network->type == WLAN_BSS_TYPE_ANY))
9066     {
9067         if (network->role == WLAN_BSS_ROLE_UAP)
9068         {
9069             network->type = WLAN_BSS_TYPE_UAP;
9070 #if CONFIG_WIFI_NM_HOSTAPD_AP
9071             netif = net_get_uap_interface();
9072 #endif
9073         }
9074         else if (network->role == WLAN_BSS_ROLE_STA)
9075         {
9076             network->type = WLAN_BSS_TYPE_STA;
9077 #if CONFIG_WPA_SUPP
9078             netif = net_get_sta_interface();
9079 #endif
9080         }
9081         else
9082         { /* Do Nothing */
9083         }
9084     }
9085 
9086     if (network->role == WLAN_BSS_ROLE_UAP)
9087     {
9088 #if CONFIG_WIFI_CAPA
9089         if (network->channel != 14)
9090         {
9091         /* If no capability was configured, set capa up to 11ax by default */
9092         if (!network->wlan_capa)
9093             network->wlan_capa =
9094 #if CONFIG_11AX
9095                 WIFI_SUPPORT_11AX |
9096 #endif
9097 #if CONFIG_11AC
9098                 WIFI_SUPPORT_11AC |
9099 #endif
9100                 WIFI_SUPPORT_11N | WIFI_SUPPORT_LEGACY;
9101         }
9102         else
9103         {
9104             network->wlan_capa = WIFI_SUPPORT_LEGACY;
9105         }
9106 
9107 #if CONFIG_11AX
9108         if (network->wlan_capa & WIFI_SUPPORT_11AX)
9109         {
9110             network->dot11ax = 1;
9111         }
9112 #endif
9113 #if CONFIG_11AC
9114         if (network->wlan_capa & WIFI_SUPPORT_11AC)
9115         {
9116             network->dot11ac = 1;
9117         }
9118 #endif
9119         if (network->wlan_capa & WIFI_SUPPORT_11N)
9120         {
9121             network->dot11n = 1;
9122         }
9123 #else
9124         if (network->channel != 14)
9125         {
9126 #if CONFIG_11AX
9127         network->dot11ax = 1;
9128 #endif
9129 #if CONFIG_11AC
9130         network->dot11ac = 1;
9131 #endif
9132         network->dot11n = 1;
9133         }
9134 #endif
9135     }
9136 
9137 #if CONFIG_WPA_SUPP
9138 #if CONFIG_WPA_SUPP_AP
9139     if (network->role == WLAN_BSS_ROLE_UAP)
9140     {
9141 
9142         if (network->ht_capab == 0)
9143         {
9144             network->ht_capab = wifi_get_default_ht_capab();
9145         }
9146 #if CONFIG_11AC
9147         if (network->vht_capab == 0)
9148         {
9149             network->vht_capab = wifi_get_default_vht_capab();
9150         }
9151         if (network->vht_oper_chwidth == 0)
9152         {
9153             network->vht_oper_chwidth = 1;
9154         }
9155 #endif
9156 #if CONFIG_11AX
9157         if (network->he_oper_chwidth == 0)
9158         {
9159             network->he_oper_chwidth = 1;
9160         }
9161 #endif
9162     }
9163 #endif
9164     if (network->security.sae_groups == NULL)
9165     {
9166         network->security.sae_groups = wlan_string_dup("19");
9167     }
9168 #if CONFIG_DRIVER_OWE
9169     if (network->security.owe_groups == NULL)
9170     {
9171         network->security.owe_groups = wlan_string_dup("19");
9172     }
9173 #endif
9174     if (network->security.group_cipher == 0)
9175     {
9176 #if CONFIG_WPA_SUPP_CRYPTO_ENTERPRISE
9177         if (network->security.wpa3_sb_192 == 1U)
9178         {
9179             network->security.group_cipher = WLAN_CIPHER_GCMP_256;
9180         }
9181         else if (network->security.wpa3_sb == 1U)
9182         {
9183             network->security.group_cipher = WLAN_CIPHER_CCMP_256;
9184         }
9185         else
9186 #endif
9187         {
9188             network->security.group_cipher = WLAN_CIPHER_CCMP;
9189         }
9190     }
9191     else
9192     {
9193 #if CONFIG_WPA_SUPP_CRYPTO_ENTERPRISE
9194         if (network->security.wpa3_sb_192 == 1U)
9195         {
9196             if (network->security.group_cipher != WLAN_CIPHER_GCMP_256)
9197             {
9198                 wlcm_e("Group cipher configuration not allowed");
9199                 goto INVAL;
9200             }
9201         }
9202         else if (network->security.wpa3_sb == 1U)
9203         {
9204             if ((network->security.group_cipher != WLAN_CIPHER_GCMP_256) && (network->security.group_cipher != WLAN_CIPHER_CCMP_256) && (network->security.group_cipher != WLAN_CIPHER_GCMP) && (network->security.group_cipher != WLAN_CIPHER_CCMP))
9205             {
9206                 wlcm_e("Group cipher configuration not allowed");
9207                 goto INVAL;
9208             }
9209         }
9210         else
9211 #endif
9212             if (network->security.type != WLAN_SECURITY_NONE)
9213         {
9214                 if ((network->security.group_cipher != WLAN_CIPHER_CCMP) && (network->security.group_cipher != WLAN_CIPHER_TKIP))
9215                 {
9216                     wlcm_e("Group cipher configuration not allowed");
9217                     goto INVAL;
9218                 }
9219         }
9220         else
9221         {
9222             wlcm_e("Group cipher configuration not allowed");
9223             goto INVAL;
9224         }
9225     }
9226 
9227     if (network->security.pairwise_cipher == 0)
9228     {
9229 #if CONFIG_WPA_SUPP_CRYPTO_ENTERPRISE
9230         if (network->security.wpa3_sb_192 == 1U)
9231         {
9232             network->security.pairwise_cipher = WLAN_CIPHER_GCMP_256;
9233         }
9234         else if (network->security.wpa3_sb == 1U)
9235         {
9236             network->security.pairwise_cipher = WLAN_CIPHER_CCMP_256;
9237         }
9238         else
9239 #endif
9240         {
9241             network->security.pairwise_cipher = WLAN_CIPHER_CCMP;
9242         }
9243     }
9244     else
9245     {
9246 #if CONFIG_WPA_SUPP_CRYPTO_ENTERPRISE
9247         if (network->security.wpa3_sb_192 == 1U)
9248         {
9249             if (network->security.pairwise_cipher != WLAN_CIPHER_GCMP_256)
9250             {
9251                 wlcm_e("Pairwise cipher configuration not allowed");
9252                 goto INVAL;
9253             }
9254         }
9255         else if (network->security.wpa3_sb == 1U)
9256         {
9257             if ((network->security.pairwise_cipher != WLAN_CIPHER_GCMP_256) && (network->security.pairwise_cipher != WLAN_CIPHER_CCMP_256) && (network->security.pairwise_cipher != WLAN_CIPHER_GCMP) && (network->security.pairwise_cipher != WLAN_CIPHER_CCMP))
9258             {
9259                 wlcm_e("Pairwise cipher configuration not allowed");
9260                 goto INVAL;
9261             }
9262         }
9263         else
9264 #endif
9265             if (network->security.type != WLAN_SECURITY_NONE)
9266         {
9267                 if ((network->security.pairwise_cipher != WLAN_CIPHER_CCMP) && (network->security.pairwise_cipher != WLAN_CIPHER_TKIP))
9268                 {
9269                     wlcm_e("Pairwise cipher configuration not allowed");
9270                     goto INVAL;
9271                 }
9272         }
9273         else
9274         {
9275             wlcm_e("Pairwise cipher configuration not allowed");
9276             goto INVAL;
9277         }
9278     }
9279 
9280     if (network->security.group_mgmt_cipher == 0)
9281     {
9282 #if CONFIG_WPA_SUPP_CRYPTO_ENTERPRISE
9283         if (network->security.wpa3_sb_192 == 1U)
9284         {
9285             network->security.group_mgmt_cipher = WLAN_CIPHER_BIP_GMAC_256;
9286         }
9287         else if (network->security.wpa3_sb == 1U)
9288         {
9289             network->security.group_mgmt_cipher = WLAN_CIPHER_BIP_CMAC_256;
9290         }
9291         else
9292 #endif
9293         {
9294             network->security.group_mgmt_cipher = WLAN_CIPHER_AES_128_CMAC;
9295         }
9296     }
9297     else
9298     {
9299 #if CONFIG_WPA_SUPP_CRYPTO_ENTERPRISE
9300         if (network->security.wpa3_sb_192 == 1U)
9301         {
9302             if (network->security.group_mgmt_cipher != WLAN_CIPHER_BIP_GMAC_256)
9303             {
9304                 wlcm_e("Group mgmt cipher configuration not allowed");
9305                 goto INVAL;
9306             }
9307         }
9308         else if (network->security.wpa3_sb == 1U)
9309         {
9310             if ((network->security.group_mgmt_cipher != WLAN_CIPHER_BIP_GMAC_256) && (network->security.group_mgmt_cipher != WLAN_CIPHER_BIP_CMAC_256) && (network->security.group_mgmt_cipher != WLAN_CIPHER_BIP_GMAC_128) && (network->security.group_mgmt_cipher != WLAN_CIPHER_AES_128_CMAC))
9311             {
9312                 wlcm_e("Group mgmt cipher configuration not allowed");
9313                 goto INVAL;
9314             }
9315         }
9316         else
9317 #endif
9318             if (network->security.type != WLAN_SECURITY_NONE)
9319         {
9320                 if (network->security.group_mgmt_cipher != WLAN_CIPHER_AES_128_CMAC)
9321                 {
9322                     wlcm_e("Group mgmt cipher configuration not allowed");
9323                     goto INVAL;
9324                 }
9325         }
9326         else
9327         {
9328             wlcm_e("Group mgmt cipher configuration not allowed");
9329             goto INVAL;
9330         }
9331     }
9332 
9333 #if CONFIG_WPA_SUPP_CRYPTO_ENTERPRISE
9334     if (
9335 #if CONFIG_EAP_TLS
9336             (wlan_is_eap_tls_security(network->security.type)) ||
9337 #endif
9338 #if CONFIG_EAP_TTLS
9339             (wlan_is_eap_ttls_security(network->security.type)) ||
9340 #endif
9341 #if CONFIG_EAP_PEAP
9342             (wlan_is_eap_peap_security(network->security.type)) ||
9343 #endif
9344 #if CONFIG_EAP_FAST
9345             (wlan_is_eap_fast_security(network->security.type)) ||
9346 #endif
9347             false)
9348     {
9349 #if CONFIG_WPA_SUPP_AP
9350 #if CONFIG_WPA_SUPP_CRYPTO_AP_ENTERPRISE
9351         if (network->role == WLAN_BSS_ROLE_UAP)
9352         {
9353             /* Specify CA certificate */
9354             network->security.ca_cert_len =
9355                 wlan_get_entp_cert_files(FILE_TYPE_ENTP_CA_CERT, &network->security.ca_cert_data);
9356             if (network->security.ca_cert_len == 0)
9357             {
9358                 wlan_free_entp_cert_files();
9359                 wlcm_e("CA cert is not configured");
9360                 goto INVAL;
9361             }
9362 
9363             /* Specify Server certificate */
9364             network->security.server_cert_len =
9365                 wlan_get_entp_cert_files(FILE_TYPE_ENTP_SERVER_CERT, &network->security.server_cert_data);
9366             if (network->security.server_cert_len == 0)
9367             {
9368                 wlan_free_entp_cert_files();
9369 #if CONFIG_WIFI_USB_FILE_ACCESS
9370                 OSA_MemoryFree(network->security.ca_cert_data);
9371 #endif
9372                 wlcm_e("Server cert is not configured");
9373                 goto INVAL;
9374             }
9375             /* Specify Server key */
9376             network->security.server_key_len =
9377                 wlan_get_entp_cert_files(FILE_TYPE_ENTP_SERVER_KEY, &network->security.server_key_data);
9378             if (network->security.server_key_len == 0)
9379             {
9380                 wlan_free_entp_cert_files();
9381 #if CONFIG_WIFI_USB_FILE_ACCESS
9382                 OSA_MemoryFree(network->security.ca_cert_data);
9383                 OSA_MemoryFree(network->security.server_cert_data);
9384 #endif
9385                 wlcm_e("Server key is not configured");
9386                 goto INVAL;
9387             }
9388             /* Specify DH params */
9389             network->security.dh_len = wlan_get_entp_cert_files(FILE_TYPE_ENTP_DH_PARAMS, &network->security.dh_data);
9390             if (network->security.dh_len == 0)
9391             {
9392                 wlan_free_entp_cert_files();
9393 #if CONFIG_WIFI_USB_FILE_ACCESS
9394                 OSA_MemoryFree(network->security.ca_cert_data);
9395                 OSA_MemoryFree(network->security.server_cert_data);
9396                 OSA_MemoryFree(network->security.server_key_data);
9397 #endif
9398                 wlcm_e("DH params are not configured");
9399                 goto INVAL;
9400             }
9401 
9402 #if CONFIG_EAP_FAST
9403             if (wlan_is_eap_fast_security(network->security.type))
9404             {
9405                 if (strlen(network->security.pac_opaque_encr_key) != (PAC_OPAQUE_ENCR_KEY_MAX_LENGTH - 1))
9406                 {
9407                     wlcm_e("Invalid PAC Opaque Encryption key");
9408                     goto INVAL;
9409                 }
9410                 if (strlen(network->security.a_id) != (A_ID_MAX_LENGTH - 1))
9411                 {
9412                     wlcm_e("Invalid authority identity(a_id)");
9413                     goto INVAL;
9414                 }
9415             }
9416 #endif
9417         }
9418         else
9419 #endif
9420 #endif
9421         {
9422             if (false == wlan_is_skip_cert_cfg(network->security.type))
9423             {
9424                 /* Specify CA certificate */
9425                 network->security.ca_cert_len =
9426                     wlan_get_entp_cert_files(FILE_TYPE_ENTP_CA_CERT, &network->security.ca_cert_data);
9427                 if (network->security.ca_cert_len == 0)
9428                 {
9429                     wlan_free_entp_cert_files();
9430                     wlcm_e("CA cert is not configured");
9431                     goto INVAL;
9432                 }
9433 
9434                 /* Specify Client certificate */
9435                 network->security.client_cert_len =
9436                     wlan_get_entp_cert_files(FILE_TYPE_ENTP_CLIENT_CERT, &network->security.client_cert_data);
9437                 if (network->security.client_cert_len == 0)
9438                 {
9439                     wlan_free_entp_cert_files();
9440 #if CONFIG_WIFI_USB_FILE_ACCESS
9441                     OSA_MemoryFree(network->security.ca_cert_data);
9442 #endif
9443                     wlcm_e("Client cert is not configured");
9444                     goto INVAL;
9445                 }
9446                 /* Specify Client key */
9447                 network->security.client_key_len =
9448                     wlan_get_entp_cert_files(FILE_TYPE_ENTP_CLIENT_KEY, &network->security.client_key_data);
9449                 if (network->security.client_key_len == 0)
9450                 {
9451                     wlan_free_entp_cert_files();
9452 #if CONFIG_WIFI_USB_FILE_ACCESS
9453                     OSA_MemoryFree(network->security.ca_cert_data);
9454                     OSA_MemoryFree(network->security.client_cert_data);
9455 #endif
9456                     wlcm_e("Client key is not configured");
9457                     goto INVAL;
9458                 }
9459             }
9460         }
9461 #ifdef CONFIG_EAP_TTLS
9462         if (WLAN_SECURITY_EAP_TTLS == network->security.type)
9463         {
9464             if (network->role == WLAN_BSS_ROLE_STA)
9465             {
9466 
9467                 /* Specify CA certificate2 */
9468                 network->security.ca_cert2_len =
9469                     wlan_get_entp_cert_files(FILE_TYPE_ENTP_CA_CERT2, &network->security.ca_cert2_data);
9470                 if (network->security.ca_cert2_len == 0)
9471                 {
9472                     wlan_free_entp_cert_files();
9473 #if CONFIG_WIFI_USB_FILE_ACCESS
9474                     OSA_MemoryFree(network->security.ca_cert_data);
9475                     OSA_MemoryFree(network->security.client_cert_data);
9476                     OSA_MemoryFree(network->security.client_key_data);
9477 #endif
9478                     wlcm_e("CA cert2 is not configured");
9479                     goto INVAL;
9480                 }
9481                 /* Specify Client certificate2 */
9482                 network->security.client_cert2_len =
9483                     wlan_get_entp_cert_files(FILE_TYPE_ENTP_CLIENT_CERT, &network->security.client_cert2_data);
9484                 if (network->security.client_cert2_len == 0)
9485                 {
9486                     wlan_free_entp_cert_files();
9487 #if CONFIG_WIFI_USB_FILE_ACCESS
9488                     OSA_MemoryFree(network->security.ca_cert_data);
9489                     OSA_MemoryFree(network->security.client_cert_data);
9490                     OSA_MemoryFree(network->security.client_key_data);
9491                     OSA_MemoryFree(network->security.ca_cert2_data);
9492 #endif
9493                     wlcm_e("Client cert2 is not configured");
9494                     goto INVAL;
9495                 }
9496                 /* Specify Client key2 */
9497                 network->security.client_key2_len =
9498                     wlan_get_entp_cert_files(FILE_TYPE_ENTP_CLIENT_KEY, &network->security.client_key2_data);
9499                 if (network->security.client_key_len == 0)
9500                 {
9501                     wlan_free_entp_cert_files();
9502 #if CONFIG_WIFI_USB_FILE_ACCESS
9503                     OSA_MemoryFree(network->security.ca_cert_data);
9504                     OSA_MemoryFree(network->security.client_cert_data);
9505                     OSA_MemoryFree(network->security.client_key_data);
9506                     OSA_MemoryFree(network->security.ca_cert2_data);
9507                     OSA_MemoryFree(network->security.client_cert2_data);
9508 #endif
9509                     wlcm_e("Client key2 is not configured");
9510                     goto INVAL;
9511                 }
9512             }
9513         }
9514 #endif
9515     }
9516 #endif
9517 #endif
9518     /* Find a slot for the new network but check all existing networks in
9519      * case the new one has a duplicate name, which is not allowed. */
9520     for (i = 0; i < ARRAY_SIZE(wlan.networks); i++)
9521     {
9522         if (wlan.networks[i].name[0] != '\0')
9523         {
9524             if (strlen(wlan.networks[i].name) == len && !strncmp(wlan.networks[i].name, network->name, len))
9525             {
9526                 goto INVAL;
9527             }
9528         }
9529         else if (pos == -1)
9530         {
9531             pos = i;
9532         }
9533         else
9534         { /* Do Nothing */
9535         }
9536     }
9537 
9538     if (pos < 0)
9539     {
9540         if (network->security.sae_groups)
9541         {
9542             OSA_MemoryFree(network->security.sae_groups);
9543             network->security.sae_groups = NULL;
9544         }
9545 #if CONFIG_DRIVER_OWE
9546         if (network->security.owe_groups)
9547         {
9548             OSA_MemoryFree(network->security.owe_groups);
9549             network->security.owe_groups = NULL;
9550         }
9551 #endif
9552         return -WM_E_NOMEM;
9553     }
9554 
9555     wlan.networks[pos].dtim_period = network->dtim_period;
9556     wlan.networks[pos].acs_band    = network->acs_band;
9557     /* save and set private fields */
9558     (void)memcpy((void *)&wlan.networks[pos], (const void *)network, sizeof(struct wlan_network));
9559     wlan.networks[pos].ssid_specific    = (uint8_t)(network->ssid[0] != '\0');
9560     wlan.networks[pos].bssid_specific   = (uint8_t)!is_bssid_any(network->bssid);
9561     wlan.networks[pos].channel_specific = (uint8_t)(network->channel != 0U);
9562     if (wlan.networks[pos].channel_specific == 1U)
9563     {
9564 #if CONFIG_5GHz_SUPPORT
9565         if (network->channel > MAX_CHANNELS_BG)
9566         {
9567             wlan.networks[pos].acs_band    = 1;
9568         }
9569         else
9570 #endif
9571         {
9572             wlan.networks[pos].acs_band    = 0;
9573         }
9574     }
9575 
9576     if (network->security.type != WLAN_SECURITY_WILDCARD)
9577     {
9578         wlan.networks[pos].security_specific = 1;
9579     }
9580 
9581 #if !CONFIG_WPA_SUPP
9582     if ((network->role == WLAN_BSS_ROLE_STA) &&
9583         (network->security.type != WLAN_SECURITY_NONE && network->security.type != WLAN_SECURITY_WEP_OPEN))
9584     {
9585         ret = wifi_send_clear_wpa_psk((int)network->role, network->ssid);
9586         if (ret != WM_SUCCESS)
9587         {
9588             return WLAN_ERROR_ACTION;
9589         }
9590 #if CONFIG_WLAN_BRIDGE
9591         if (network->bridge_ssid)
9592         {
9593             ret = wifi_send_clear_wpa_psk((int)network->role, network->bridge_ssid);
9594             if (ret != WM_SUCCESS)
9595                 return WLAN_ERROR_ACTION;
9596         }
9597 #endif
9598 #if CONFIG_HOST_PMK
9599         if ((network->security.type != WLAN_SECURITY_OWE_ONLY) && (network->security.type != WLAN_SECURITY_WPA3_SAE))
9600         {
9601             ret = mrvl_generate_psk(network->ssid, strlen(network->ssid), network->security.psk,
9602                                     wlan.networks[pos].security.pmk);
9603 
9604             if (ret == WM_SUCCESS)
9605                 wlan.networks[pos].security.pmk_valid = true;
9606         }
9607 
9608 #if CONFIG_WLAN_BRIDGE
9609         if (network->bridge_ssid)
9610         {
9611             ret = mrvl_generate_psk(network->bridge_ssid, strlen(network->bridge_ssid), network->security.bridge_psk,
9612                                     wlan.networks[pos].security.bridge_pmk);
9613 
9614             if (ret == WM_SUCCESS)
9615                 wlan.networks[pos].security.bridge_pmk_valid = true;
9616         }
9617 #endif
9618 #endif
9619     }
9620 #endif
9621 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
9622 #if CONFIG_WPA_SUPP
9623 #if CONFIG_WPA_SUPP_WPS
9624     if (!wlan.wps_session_attempt)
9625     {
9626 #endif
9627         ret = wpa_supp_add_network(netif, &wlan.networks[pos]);
9628         if (ret < 0)
9629         {
9630             wlan_remove_network(wlan.networks[pos].name);
9631             return -WM_E_NOMEM;
9632         }
9633 #if CONFIG_WPA_SUPP_WPS
9634     }
9635 #endif
9636 #endif
9637 #endif
9638     wlan.num_networks++;
9639 #if CONFIG_WLAN_FAST_PATH
9640     /* On station network addition,
9641      * flush the FP cache since the index changes */
9642     if (network->role == WLAN_BSS_ROLE_STA)
9643     {
9644         wlan.auth_cache_valid      = false;
9645         wlan.fast_path_cache_valid = false;
9646     }
9647 #endif /* CONFIG_WLAN_FAST_PATH */
9648 
9649     return WM_SUCCESS;
9650 
9651 INVAL:
9652     if (network->security.sae_groups)
9653     {
9654         OSA_MemoryFree(network->security.sae_groups);
9655         network->security.sae_groups = NULL;
9656     }
9657 #if CONFIG_DRIVER_OWE
9658     if (network->security.owe_groups)
9659     {
9660         OSA_MemoryFree(network->security.owe_groups);
9661         network->security.owe_groups = NULL;
9662     }
9663 #endif
9664     return -WM_E_INVAL;
9665 }
9666 
9667 #if CONFIG_WIFI_CAPA
wlan_check_11n_capa(unsigned int channel)9668 uint8_t wlan_check_11n_capa(unsigned int channel)
9669 {
9670     uint8_t enable_11n = false;
9671     uint16_t fw_bands  = 0U;
9672 
9673     wifi_get_fw_info(MLAN_BSS_TYPE_UAP, &fw_bands);
9674 
9675     if (channel > 14 && (fw_bands & BAND_AN))
9676     {
9677         enable_11n = true;
9678     }
9679     else if (channel <= 14 && (fw_bands & BAND_GN))
9680     {
9681         enable_11n = true;
9682     }
9683     return enable_11n;
9684 }
9685 
wlan_check_11ac_capa(unsigned int channel)9686 uint8_t wlan_check_11ac_capa(unsigned int channel)
9687 {
9688     uint8_t enable_11ac = false;
9689     uint16_t fw_bands   = 0U;
9690 
9691     wifi_get_fw_info(MLAN_BSS_TYPE_UAP, &fw_bands);
9692 
9693 #if CONFIG_11AC
9694     if (channel > 14 && (fw_bands & BAND_AAC))
9695     {
9696         enable_11ac = true;
9697     }
9698     else if (channel <= 14 && (fw_bands & BAND_GAC))
9699     {
9700         enable_11ac = true;
9701     }
9702 #endif
9703     return enable_11ac;
9704 }
9705 
wlan_check_11ax_capa(unsigned int channel)9706 uint8_t wlan_check_11ax_capa(unsigned int channel)
9707 {
9708     uint8_t enable_11ax = false;
9709     uint16_t fw_bands   = 0U;
9710 
9711     wifi_get_fw_info(MLAN_BSS_TYPE_UAP, &fw_bands);
9712 
9713 #if CONFIG_11AX
9714     if (channel > 14 && (fw_bands & BAND_AAX))
9715     {
9716         enable_11ax = true;
9717     }
9718     else if (channel <= 14 && (fw_bands & BAND_GAX))
9719     {
9720         enable_11ax = true;
9721     }
9722 #endif
9723     return enable_11ax;
9724 }
9725 #endif
9726 
wlan_remove_network(const char * name)9727 int wlan_remove_network(const char *name)
9728 {
9729     unsigned int len, i;
9730 #if CONFIG_WPA_SUPP
9731     struct netif *netif = net_get_sta_interface();
9732 #endif
9733 
9734     if (!is_running())
9735     {
9736         return WLAN_ERROR_STATE;
9737     }
9738 
9739     if (name == NULL)
9740     {
9741         return -WM_E_INVAL;
9742     }
9743 
9744     len = strlen(name);
9745 
9746     /* find the first network whose name matches and clear it out */
9747     for (i = 0; i < ARRAY_SIZE(wlan.networks); i++)
9748     {
9749         if (wlan.networks[i].name[0] != '\0' && strlen(wlan.networks[i].name) == len &&
9750             !strncmp(wlan.networks[i].name, name, len))
9751         {
9752             if (false == wlan_in_reset)
9753             {
9754                 if (wlan.running && wlan.cur_network_idx == i)
9755                 {
9756 #if CONFIG_WLAN_FAST_PATH
9757                     /* On station network removal,
9758                      * flush the FP cache since the index changes */
9759                     wlan.auth_cache_valid      = false;
9760                     wlan.fast_path_cache_valid = false;
9761 #endif /* CONFIG_WLAN_FAST_PATH */
9762                     return WLAN_ERROR_STATE;
9763                 }
9764                 if (wlan.cur_uap_network_idx == i)
9765                 {
9766                     return WLAN_ERROR_STATE;
9767                 }
9768             }
9769 #if CONFIG_WPA2_ENTP
9770             if (wlan.networks[i].security.tls_cert.ca_chain)
9771             {
9772                 wm_mbedtls_free_cert(wlan.networks[i].security.tls_cert.ca_chain);
9773             }
9774             if (wlan.networks[i].security.tls_cert.own_cert)
9775             {
9776                 wm_mbedtls_free_cert(wlan.networks[i].security.tls_cert.own_cert);
9777             }
9778             if (wlan.networks[i].security.tls_cert.own_key)
9779             {
9780                 wm_mbedtls_free_key(wlan.networks[i].security.tls_cert.own_key);
9781             }
9782 #endif
9783 #if CONFIG_WPA_SUPP
9784             if (wlan.networks[i].role == WLAN_BSS_ROLE_STA)
9785             {
9786                 netif = net_get_sta_interface();
9787             }
9788             else if (wlan.networks[i].role == WLAN_BSS_ROLE_UAP)
9789             {
9790 #if CONFIG_WIFI_NM_HOSTAPD_AP
9791                 netif = net_get_uap_interface();
9792 #endif
9793             }
9794             else
9795             {
9796                 /* Do nothing */
9797             }
9798 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
9799             wpa_supp_remove_network(netif, &wlan.networks[i]);
9800 #endif
9801 
9802             if (wlan.networks[i].security.sae_groups)
9803             {
9804                 OSA_MemoryFree(wlan.networks[i].security.sae_groups);
9805                 wlan.networks[i].security.sae_groups = NULL;
9806             }
9807 #if CONFIG_DRIVER_OWE
9808             if (wlan.networks[i].security.owe_groups)
9809             {
9810                 OSA_MemoryFree(wlan.networks[i].security.owe_groups);
9811                 wlan.networks[i].security.owe_groups = NULL;
9812             }
9813 #endif
9814 #if CONFIG_WPA_SUPP_CRYPTO_ENTERPRISE
9815 #if CONFIG_WIFI_USB_FILE_ACCESS
9816             if (wlan.networks[i].role == WLAN_BSS_ROLE_STA)
9817             {
9818                 if (wlan.networks[i].security.ca_cert_data)
9819                 {
9820                     OSA_MemoryFree(wlan.networks[i].security.ca_cert_data);
9821                 }
9822                 if (wlan.networks[i].security.client_cert_data)
9823                 {
9824                     OSA_MemoryFree(wlan.networks[i].security.client_cert_data);
9825                 }
9826                 if (wlan.networks[i].security.client_key_data)
9827                 {
9828                     OSA_MemoryFree(wlan.networks[i].security.client_key_data);
9829                 }
9830                 if (wlan.networks[i].security.ca_cert2_data)
9831                 {
9832                     OSA_MemoryFree(wlan.networks[i].security.ca_cert2_data);
9833                 }
9834                 if (wlan.networks[i].security.client_cert2_data)
9835                 {
9836                     OSA_MemoryFree(wlan.networks[i].security.client_cert2_data);
9837                 }
9838                 if (wlan.networks[i].security.client_key2_data)
9839                 {
9840                     OSA_MemoryFree(wlan.networks[i].security.client_key2_data);
9841                 }
9842             }
9843 #if CONFIG_WPA_SUPP_AP
9844 #if CONFIG_WPA_SUPP_CRYPTO_AP_ENTERPRISE
9845             else if (wlan.networks[i].role == WLAN_BSS_ROLE_UAP)
9846             {
9847                 if (wlan.networks[i].security.ca_cert_data)
9848                 {
9849                     OSA_MemoryFree(wlan.networks[i].security.ca_cert_data);
9850                 }
9851                 if (wlan.networks[i].security.server_cert_data)
9852                 {
9853                     OSA_MemoryFree(wlan.networks[i].security.server_cert_data);
9854                 }
9855                 if (wlan.networks[i].security.server_key_data)
9856                 {
9857                     OSA_MemoryFree(wlan.networks[i].security.server_key_data);
9858                 }
9859             }
9860 #endif
9861 #endif
9862 #endif
9863 #endif
9864 #if CONFIG_WPA_SUPP_DPP
9865             if (wlan.networks[i].security.dpp_connector)
9866             {
9867                 OSA_MemoryFree(wlan.networks[i].security.dpp_connector);
9868             }
9869             if (wlan.networks[i].security.dpp_c_sign_key)
9870             {
9871                 OSA_MemoryFree(wlan.networks[i].security.dpp_c_sign_key);
9872             }
9873             if (wlan.networks[i].security.dpp_net_access_key)
9874             {
9875                 OSA_MemoryFree(wlan.networks[i].security.dpp_net_access_key);
9876             }
9877 #endif
9878 #endif
9879             (void)memset(&wlan.networks[i], 0, sizeof(struct wlan_network));
9880             wlan.num_networks--;
9881             return WM_SUCCESS;
9882         }
9883     }
9884     /* network name wasn't found */
9885     return -WM_E_INVAL;
9886 }
9887 
wlan_get_network_count(unsigned int * count)9888 int wlan_get_network_count(unsigned int *count)
9889 {
9890     if (count == NULL)
9891     {
9892         return -WM_E_INVAL;
9893     }
9894 
9895     *count = wlan.num_networks;
9896     return WM_SUCCESS;
9897 }
9898 
wlan_get_current_network(struct wlan_network * network)9899 int wlan_get_current_network(struct wlan_network *network)
9900 {
9901     short rssi;
9902     int snr;
9903 
9904     if (network == NULL)
9905     {
9906         return -WM_E_INVAL;
9907     }
9908 
9909     if (wlan.running && is_sta_associated())
9910     {
9911         (void)memcpy((void *)network, (const void *)&wlan.networks[wlan.cur_network_idx], sizeof(struct wlan_network));
9912 
9913         wlan_get_current_signal_strength(&rssi, &snr);
9914 
9915         network->rssi = rssi;
9916 
9917         return WM_SUCCESS;
9918     }
9919 
9920     return WLAN_ERROR_STATE;
9921 }
9922 
wlan_get_current_network_ssid(char * ssid)9923 int wlan_get_current_network_ssid(char *ssid)
9924 {
9925     if (ssid == NULL)
9926     {
9927         return -WM_E_INVAL;
9928     }
9929 
9930     if (wlan.running && is_sta_associated())
9931     {
9932         (void)memcpy((void *)ssid, (const void *)&wlan.networks[wlan.cur_network_idx].ssid, IEEEtypes_SSID_SIZE + 1);
9933 
9934         return WM_SUCCESS;
9935     }
9936 
9937     return WLAN_ERROR_STATE;
9938 }
9939 
wlan_get_current_network_bssid(char * bssid)9940 int wlan_get_current_network_bssid(char *bssid)
9941 {
9942     if (bssid == NULL)
9943     {
9944         return -WM_E_INVAL;
9945     }
9946 
9947 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
9948     struct netif *netif = net_get_sta_interface();
9949     struct wifi_iface_status status;
9950 
9951     memset(&status, 0x0, sizeof(status));
9952     supplicant_status(net_if_get_device((void *)netif), &status);
9953     memcpy(bssid, status.bssid, MLAN_MAC_ADDR_LENGTH);
9954     return WM_SUCCESS;
9955 #else
9956     if (wlan.running && is_sta_associated())
9957     {
9958         (void)memcpy((void *)bssid, (const void *)&wlan.networks[wlan.cur_network_idx].bssid, IEEEtypes_ADDRESS_SIZE);
9959 
9960         return WM_SUCCESS;
9961     }
9962 
9963     return WLAN_ERROR_STATE;
9964 #endif
9965 }
9966 
wlan_get_current_uap_network(struct wlan_network * network)9967 int wlan_get_current_uap_network(struct wlan_network *network)
9968 {
9969     if (network == NULL)
9970     {
9971         return -WM_E_INVAL;
9972     }
9973 
9974     if (wlan.running && (is_uap_state(CM_UAP_IP_UP) || is_uap_state(CM_UAP_STARTED)))
9975     {
9976         (void)memcpy((void *)network, (const void *)&wlan.networks[wlan.cur_uap_network_idx],
9977                      sizeof(struct wlan_network));
9978         return WM_SUCCESS;
9979     }
9980     return WLAN_ERROR_STATE;
9981 }
9982 
wlan_get_current_uap_network_ssid(char * ssid)9983 int wlan_get_current_uap_network_ssid(char *ssid)
9984 {
9985     if (ssid == NULL)
9986     {
9987         return -WM_E_INVAL;
9988     }
9989 
9990     if (wlan.running && (is_uap_state(CM_UAP_IP_UP) || is_uap_state(CM_UAP_STARTED)))
9991     {
9992         (void)memcpy((void *)ssid, (const void *)&wlan.networks[wlan.cur_uap_network_idx].ssid, IEEEtypes_SSID_SIZE + 1);
9993 
9994         return WM_SUCCESS;
9995     }
9996 
9997     return WLAN_ERROR_STATE;
9998 }
9999 
10000 #if CONFIG_SCAN_WITH_RSSIFILTER
wlan_set_rssi_threshold(int rssithr)10001 int wlan_set_rssi_threshold(int rssithr)
10002 {
10003     if (rssithr)
10004         wlan.networks[wlan.cur_network_idx].rssi_threshold = (rssithr < 0 ? rssithr : 0);
10005 
10006     return WM_SUCCESS;
10007 }
10008 #endif
10009 
is_uap_started(void)10010 bool is_uap_started(void)
10011 {
10012 #if UAP_SUPPORT
10013 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
10014     int state = wifi_nxp_hapd_state();
10015 
10016     return (state == HAPD_IFACE_ENABLED);
10017 #else
10018     return is_uap_state(CM_UAP_IP_UP);
10019 #endif
10020 #else
10021     return false;
10022 #endif
10023 }
10024 
is_sta_connected(void)10025 bool is_sta_connected(void)
10026 {
10027 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
10028     int state = wifi_nxp_supp_state();
10029 
10030     return (state == WPA_COMPLETED);
10031 #else
10032     return (wlan.sta_state == CM_STA_CONNECTED);
10033 #endif
10034 }
10035 
is_sta_ipv4_connected(void)10036 bool is_sta_ipv4_connected(void)
10037 {
10038 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
10039     int ip = 0;
10040     net_get_if_ip_addr(&ip, net_get_sta_handle());
10041     return (ip == 0 ? false : true);
10042 #else
10043     return (wlan.sta_ipv4_state == CM_STA_CONNECTED);
10044 #endif
10045 }
10046 
10047 #if CONFIG_IPV6
is_sta_ipv6_connected(void)10048 bool is_sta_ipv6_connected(void)
10049 {
10050     return (wlan.sta_ipv6_state == CM_STA_CONNECTED);
10051 }
10052 #endif
10053 
wlan_get_network(unsigned int index,struct wlan_network * network)10054 int wlan_get_network(unsigned int index, struct wlan_network *network)
10055 {
10056     unsigned int i;
10057     int pos = -1;
10058 
10059     if (network == NULL || index > ARRAY_SIZE(wlan.networks))
10060     {
10061         return -WM_E_INVAL;
10062     }
10063 
10064     for (i = 0; i < ARRAY_SIZE(wlan.networks); i++)
10065     {
10066         ++pos;
10067         if (wlan.networks[i].name[0] != '\0' && pos == (int)index)
10068         {
10069             copy_network(network, &wlan.networks[i]);
10070             return WM_SUCCESS;
10071         }
10072     }
10073 
10074     return -WM_E_INVAL;
10075 }
10076 
wlan_get_current_nf(void)10077 int wlan_get_current_nf(void)
10078 {
10079     return -g_data_nf_last;
10080 }
10081 
wlan_get_current_signal_strength(short * rssi,int * snr)10082 int wlan_get_current_signal_strength(short *rssi, int *snr)
10083 {
10084     wifi_rssi_info_t rssi_info;
10085     (void)wifi_send_rssi_info_cmd(&rssi_info);
10086 
10087     *snr  = rssi_info.bcn_rssi_last - rssi_info.bcn_nf_last;
10088     *rssi = rssi_info.bcn_rssi_last;
10089     return WM_SUCCESS;
10090 }
10091 
wlan_get_average_signal_strength(short * rssi,int * snr)10092 int wlan_get_average_signal_strength(short *rssi, int *snr)
10093 {
10094     wifi_rssi_info_t rssi_info;
10095     (void)wifi_send_rssi_info_cmd(&rssi_info);
10096 
10097     *snr  = rssi_info.bcn_snr_avg;
10098     *rssi = rssi_info.bcn_rssi_avg;
10099     return WM_SUCCESS;
10100 }
10101 
wlan_get_current_rssi(short * rssi)10102 int wlan_get_current_rssi(short *rssi)
10103 {
10104     g_rssi = (uint8_t)(g_data_snr_last - g_data_nf_last);
10105     *rssi  = (short)(g_rssi - 256U);
10106     return WM_SUCCESS;
10107 }
10108 
wlan_get_network_byname(char * name,struct wlan_network * network)10109 int wlan_get_network_byname(char *name, struct wlan_network *network)
10110 {
10111     unsigned int i;
10112 
10113     if (network == NULL || name == NULL)
10114     {
10115         return -WM_E_INVAL;
10116     }
10117 
10118     for (i = 0; i < ARRAY_SIZE(wlan.networks); i++)
10119     {
10120         if (wlan.networks[i].name[0] != '\0' && !strcmp(wlan.networks[i].name, name))
10121         {
10122             copy_network(network, &wlan.networks[i]);
10123             return WM_SUCCESS;
10124         }
10125     }
10126 
10127     return -WM_E_INVAL;
10128 }
10129 
wlan_set_network_ip_byname(char * name,struct wlan_ip_config * ip)10130 int wlan_set_network_ip_byname(char *name, struct wlan_ip_config *ip)
10131 {
10132     unsigned int i;
10133 
10134     if (ip == NULL || name == NULL)
10135     {
10136         return -WM_E_INVAL;
10137     }
10138 
10139     for (i = 0; i < ARRAY_SIZE(wlan.networks); i++)
10140     {
10141         if (wlan.networks[i].name[0] != '\0' && !strcmp(wlan.networks[i].name, name))
10142         {
10143             memcpy(&(wlan.networks[i].ip), ip, sizeof(struct wlan_ip_config));
10144             return WM_SUCCESS;
10145         }
10146     }
10147 
10148     return -WM_E_INVAL;
10149 }
10150 
wlan_remove_all_network_profiles(void)10151 int wlan_remove_all_network_profiles(void)
10152 {
10153     unsigned int i;
10154     int ret;
10155 
10156     for (i = 0; i < ARRAY_SIZE(wlan.networks); i++)
10157     {
10158         if (wlan.networks[i].name[0] != '\0')
10159         {
10160             ret = wlan_remove_network(wlan.networks[i].name);
10161             if (ret != WM_SUCCESS)
10162             {
10163                 return -WM_E_INVAL;
10164             }
10165         }
10166     }
10167 
10168     return WM_SUCCESS;
10169 }
10170 
wlan_disconnect(void)10171 int wlan_disconnect(void)
10172 {
10173     if (!wlan.running)
10174     {
10175         return WLAN_ERROR_STATE;
10176     }
10177 
10178 #if CONFIG_HOST_SLEEP
10179     wakelock_get();
10180 #endif
10181 
10182     (void)send_user_request(CM_STA_USER_REQUEST_DISCONNECT, 0);
10183 
10184     /*Wait for sta to enter the disconnect state, and then send ECSA cmd*/
10185     /* zephyr also need this delay to remove network with same name */
10186     OSA_TimeDelay(1000);
10187 #if CONFIG_ECSA
10188     wrapper_clear_media_connected_event();
10189     wlan_switch_to_nondfs_channel();
10190 #endif
10191 
10192     return WM_SUCCESS;
10193 }
10194 
wlan_connect(char * name)10195 int wlan_connect(char *name)
10196 {
10197     unsigned int len = name != NULL ? strlen(name) : 0U;
10198     int i            = 0, ret;
10199 
10200     if (!wlan.running)
10201     {
10202         return WLAN_ERROR_STATE;
10203     }
10204 
10205     if (wlan.num_networks == 0U || len == 0U)
10206     {
10207         return -WM_E_INVAL;
10208     }
10209 
10210 #if CONFIG_WPA_SUPP
10211 #if CONFIG_WPA_SUPP_WPS
10212     if (wlan.wps_session_attempt)
10213     {
10214         wlcm_d("WPS session is in progress");
10215         return WLAN_ERROR_STATE;
10216     }
10217 #endif
10218 #endif
10219 
10220     wlan.roam_reassoc = false;
10221 
10222     /* connect to a specific network */
10223     for (i = 0; i < ARRAY_SIZE(wlan.networks); i++)
10224     {
10225         if (wlan.networks[i].name[0] != '\0' && strlen(wlan.networks[i].name) == len &&
10226             !strncmp(wlan.networks[i].name, name, len))
10227         {
10228             switch (wlan.networks[i].role)
10229             {
10230                 case MLAN_BSS_ROLE_UAP:
10231                     wlcm_e("Invalid bss role. Bss role is uap.");
10232                     ret = WLAN_ERROR_PARAM;
10233                     break;
10234                 case MLAN_BSS_ROLE_ANY:
10235                     wlcm_e("Invalid bss role. Bss role is any.");
10236                     ret = WLAN_ERROR_PARAM;
10237                     break;
10238                 default:
10239                     ret = WLAN_ERROR_NONE;
10240                     break;
10241             }
10242 
10243             if(ret != WLAN_ERROR_NONE)
10244                 return ret;
10245 
10246             wlcm_d("taking the scan lock (connect scan)");
10247             dbg_lock_info();
10248             ret = OSA_SemaphoreWait((osa_semaphore_handle_t)wlan.scan_lock, osaWaitForever_c);
10249             if (ret != WM_SUCCESS)
10250             {
10251                 wlcm_e("failed to get scan lock: 0x%X", ret);
10252                 return WLAN_ERROR_ACTION;
10253             }
10254             wlcm_d("got the scan lock (connect scan)");
10255             wlan.is_scan_lock = 1;
10256             /* Reset reassoc count as this is set to WLAN_RECONNECT_LIMIT
10257              * during disconnect */
10258             wlan.reassoc_count = 0;
10259 
10260             return send_user_request(CM_STA_USER_REQUEST_CONNECT, i);
10261         }
10262     }
10263 
10264     /* specified network was not found */
10265     return -WM_E_INVAL;
10266 }
10267 
wlan_connect_opt(char * name,bool skip_dfs)10268 int wlan_connect_opt(char *name, bool skip_dfs)
10269 {
10270     int ret = 0;
10271 
10272     mlan_adap->skip_dfs = false;
10273     if(skip_dfs)
10274         mlan_adap->skip_dfs = true;
10275 
10276     ret = wlan_connect(name);
10277     if(ret != WM_SUCCESS)
10278     {
10279         mlan_adap->skip_dfs = false;
10280     }
10281 
10282     return ret;
10283 }
10284 
wlan_reassociate(void)10285 int wlan_reassociate(void)
10286 {
10287     int ret;
10288 
10289     if (!wlan.running)
10290     {
10291         return WLAN_ERROR_STATE;
10292     }
10293 
10294     if (wlan.num_networks == 0U)
10295     {
10296         return -WM_E_INVAL;
10297     }
10298 
10299     if (!is_sta_connected())
10300     {
10301         wlcm_d("Error: sta connection is required before sending reassociate request");
10302         return WLAN_ERROR_STATE;
10303     }
10304 
10305 #if CONFIG_WPA_SUPP
10306 #if CONFIG_WPA_SUPP_WPS
10307     if (wlan.wps_session_attempt)
10308     {
10309         wlcm_d("WPS session is in progress");
10310         return WLAN_ERROR_STATE;
10311     }
10312 #endif
10313 #endif
10314 
10315     wlcm_d("taking the scan lock (reassociate scan)");
10316     dbg_lock_info();
10317     ret = OSA_SemaphoreWait((osa_semaphore_handle_t)wlan.scan_lock, osaWaitForever_c);
10318     if (ret != WM_SUCCESS)
10319     {
10320         wlcm_e("failed to get scan lock: 0x%X", ret);
10321         return WLAN_ERROR_ACTION;
10322     }
10323     wlcm_d("got the scan lock (reassociate scan)");
10324     wlan.is_scan_lock = 1;
10325 
10326     wlan.roam_reassoc = true;
10327 
10328     ret = send_user_request(CM_STA_USER_REQUEST_CONNECT, wlan.cur_network_idx);
10329     if (ret != WM_SUCCESS)
10330     {
10331         wlcm_d("Error: Reassociate failed");
10332         wlan.roam_reassoc = false;
10333         (void)OSA_SemaphorePost((osa_semaphore_handle_t)wlan.scan_lock);
10334         wlan.is_scan_lock = 0;
10335     }
10336 
10337     return ret;
10338 }
10339 
wlan_start_network(const char * name)10340 int wlan_start_network(const char *name)
10341 {
10342 #if UAP_SUPPORT
10343     unsigned int i;
10344     unsigned int len;
10345 
10346     if (name == NULL)
10347     {
10348         return -WM_E_INVAL;
10349     }
10350 
10351     len = strlen(name);
10352     if (len == 0 || wlan.num_networks == 0U)
10353     {
10354         return -WM_E_INVAL;
10355     }
10356 
10357     if (is_uap_started() != 0)
10358     {
10359         wlcm_e(
10360             "%s network cannot be started, "
10361             "as the uAP is already running",
10362             name);
10363         return WLAN_ERROR_STATE;
10364     }
10365 
10366     for (i = 0; i < ARRAY_SIZE(wlan.networks); i++)
10367     {
10368         if (wlan.networks[i].name[0] != '\0' && strlen(wlan.networks[i].name) == len &&
10369             !strncmp(wlan.networks[i].name, name, len) && (wlan.networks[i].role == WLAN_BSS_ROLE_UAP) &&
10370             wlan.networks[i].ssid_specific)
10371         {
10372 #if CONFIG_MULTI_CHAN
10373             /* when multi-channel is enabled, uap and sta can start on different channel */
10374             if (wifi_get_mc_policy() == 0)
10375 #endif
10376             {
10377                 if ((wlan.networks[i].channel_specific) && (wlan.networks[i].channel != 0))
10378                 {
10379                     wlcm_w(
10380                         "NOTE: uAP will automatically switch to"
10381                         " the channel that station is on.");
10382                     if(is_sta_connected())
10383                         wlan.networks[i].channel = wlan.networks[wlan.cur_network_idx].channel;
10384                 }
10385             }
10386             if (wlan.networks[i].role == WLAN_BSS_ROLE_UAP)
10387             {
10388                 return send_user_request(CM_UAP_USER_REQUEST_START, i);
10389             }
10390         }
10391     }
10392 
10393     /* specified network was not found */
10394     return -WM_E_INVAL;
10395 #else
10396     wlcm_e("wlan_start_network UAP not supported");
10397     return -WM_E_NODEV;
10398 #endif /* UAP_SUPPORT */
10399 }
10400 
wlan_stop_network(const char * name)10401 int wlan_stop_network(const char *name)
10402 {
10403 #if UAP_SUPPORT
10404     unsigned int i;
10405     unsigned int len;
10406 
10407     if (name == NULL)
10408     {
10409         return -WM_E_INVAL;
10410     }
10411 
10412     len = strlen(name);
10413     if (len == 0U || wlan.num_networks == 0U)
10414     {
10415         return -WM_E_INVAL;
10416     }
10417 
10418     wlan_uap_scan_chan_list_set = false;
10419     /* Search for matching SSID
10420      * If found send stop request
10421      */
10422     for (i = 0; i < ARRAY_SIZE(wlan.networks); i++)
10423     {
10424         if ((wlan.networks[i].name[0] == '\0') || (strlen(wlan.networks[i].name) != len) ||
10425             (strncmp(wlan.networks[i].name, name, len)) != 0)
10426         {
10427             continue;
10428         }
10429 
10430         if (wlan.networks[i].role == WLAN_BSS_ROLE_UAP && wlan.networks[i].ssid_specific)
10431         {
10432             net_interface_down(net_get_uap_handle());
10433             return send_user_request(CM_UAP_USER_REQUEST_STOP, i);
10434         }
10435     } /* end of loop */
10436     /* specified network was not found */
10437     return -WM_E_INVAL;
10438 #else
10439     wlcm_e("wlan_stop_network UAP not supported");
10440     return -WM_E_NODEV;
10441 #endif /* UAP_SUPPORT */
10442 }
10443 
10444 #if defined(RW610)
wlan_remove_all_networks(void)10445 int wlan_remove_all_networks(void)
10446 {
10447     void *intrfc_handle = NULL;
10448 
10449     /* No need to remove net interfaces here, as they are added only once.
10450      * Moreover, removing and adding net interface will increase netif_num cumulatively,
10451      * which will mismatch with "ua2" during creating dhcpd.
10452      */
10453     wlan_in_reset = true;
10454     wlan_remove_all_network_profiles();
10455 
10456     intrfc_handle = net_get_sta_handle();
10457     net_interface_down(intrfc_handle);
10458 
10459 #if UAP_SUPPORT
10460     intrfc_handle = net_get_uap_handle();
10461     net_interface_down(intrfc_handle);
10462     /* wait for mgmt_event handled */
10463     OSA_TimeDelay(500);
10464 #endif
10465 
10466     return WM_SUCCESS;
10467 }
10468 
10469 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
wlan_enable_all_networks(void)10470 int wlan_enable_all_networks(void)
10471 {
10472     void *intrfc_handle = NULL;
10473 
10474     intrfc_handle = net_get_sta_handle();
10475     net_interface_up(intrfc_handle);
10476 
10477 #if UAP_SUPPORT
10478     intrfc_handle = net_get_uap_handle();
10479     net_interface_up(intrfc_handle);
10480 #endif
10481     return WM_SUCCESS;
10482 }
10483 #endif
wlan_destroy_all_tasks(void)10484 void wlan_destroy_all_tasks(void)
10485 {
10486     OSA_LockSchedule();
10487 
10488     /* Destroy cm_main thread */
10489     OSA_TaskDestroy((osa_task_handle_t)wlan.wlcmgr_task_Handle);
10490 
10491 #if CONFIG_WPS2
10492     /* Destroy wps_main thread */
10493     OSA_TaskDestroy((osa_task_handle_t)wlan_wps.wps_task_Handle);
10494     OSA_SemaphoreDestroy((osa_semaphore_handle_t)wlan_wps.wps_scan_done);
10495 #endif
10496 
10497     /* Destroy wifidriver thread */
10498     wifi_destroy_wifidriver_tasks();
10499 
10500     OSA_UnlockSchedule();
10501 }
10502 
wlan_imu_get_task_lock(void)10503 int wlan_imu_get_task_lock(void)
10504 {
10505     return wifi_imu_get_task_lock();
10506 }
10507 
wlan_imu_put_task_lock(void)10508 int wlan_imu_put_task_lock(void)
10509 {
10510     return wifi_imu_put_task_lock();
10511 }
10512 
wlan_reset(cli_reset_option ResetOption)10513 void wlan_reset(cli_reset_option ResetOption)
10514 {
10515     if (OSA_MutexLock((osa_mutex_handle_t)reset_lock, 0) != WM_SUCCESS)
10516     {
10517         PRINTF("already in process...\r\n");
10518         return;
10519     }
10520 
10521     if (ResetOption == CLI_DISABLE_WIFI || ResetOption == CLI_RESET_WIFI)
10522     {
10523         PRINTF("--- Disable WiFi ---\r\n");
10524         if (wlan_is_started())
10525         {
10526 #if CONFIG_HOST_SLEEP
10527 #if CONFIG_POWER_MANAGER
10528             /* Reset host sleep state flag first */
10529             wlan_host_sleep_state = HOST_SLEEP_DISABLE;
10530 #endif
10531 #endif
10532 #if (CONFIG_WPA_SUPP) && (CONFIG_UAP_STA_MAC_ADDR_FILTER)
10533         wlan_set_sta_mac_filter(0, 0, NULL);
10534 #endif
10535             /*Disconnect form AP if station is associated with an AP.*/
10536             if (is_sta_connecting())
10537             {
10538                 wlan_disconnect();
10539                 while (!is_sta_idle())
10540                 {
10541                     OSA_TimeDelay(1000);
10542                 }
10543             }
10544 
10545             /*Stop current uAP if uAP is started.*/
10546 #if UAP_SUPPORT
10547 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
10548             if (is_uap_started())
10549             {
10550                 send_user_request(CM_UAP_USER_REQUEST_STOP, 0);
10551                 while(!is_uap_state(HAPD_IFACE_DISABLED))
10552                 {
10553                      OSA_TimeDelay(1000);
10554                 }
10555             }
10556 #else
10557             if (wlan.uap_state > CM_UAP_CONFIGURED)
10558             {
10559                 wlan_stop_network(wlan.networks[wlan.cur_uap_network_idx].name);
10560                 while (wlan.uap_state != CM_UAP_INITIALIZING)
10561                 {
10562                     OSA_TimeDelay(1000);
10563                 }
10564             }
10565 #endif
10566 #endif
10567 #if CONFIG_CPU_LOADING
10568             if(cpu_loading.status != CPU_LOADING_STATUS_DEAD)
10569                 wlan_cpu_loading_stop();
10570             while(cpu_loading.status != CPU_LOADING_STATUS_DEAD)
10571             {
10572                 OSA_TimeDelay(50);
10573             }
10574 #endif
10575             /* Block TX data */
10576             wifi_set_tx_status(WIFI_DATA_BLOCK);
10577             /* Block RX data */
10578             wifi_set_rx_status(WIFI_DATA_BLOCK);
10579 
10580 #if CONFIG_NCP_BRIDGE
10581             /* Stop uap provisioning if it started */
10582             if (uap_prov_deinit_cb)
10583                 uap_prov_deinit_cb();
10584 #endif
10585             /* Stop and Remove all network interfaces */
10586             wlan_remove_all_networks();
10587 
10588             (void)net_wlan_deinit();
10589 
10590             wifi_scan_stop();
10591             mlan_adap->skip_dfs = false;
10592             if (!wifi_fw_is_hang())
10593                 wifi_send_shutdown_cmd();
10594 
10595 #if CONFIG_WPA_SUPP
10596             wifi_supp_deinit();
10597 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
10598             wpa_supp_deinit();
10599 #endif
10600 #endif
10601 
10602             /* wait for imu task done */
10603             wlan_imu_get_task_lock();
10604             /* Destroy all tasks before touch the global vars */
10605             wlan_destroy_all_tasks();
10606 #if CONFIG_NCP_BRIDGE
10607             if (uap_prov_cleanup_cb)
10608                 uap_prov_cleanup_cb();
10609 #endif
10610 #if CONFIG_WPS2
10611             wps_stop();
10612 #endif /* CONFIG_WPS2 */
10613 
10614 #if CONFIG_WPA2_ENTP
10615             wlan_free_entp_cert_files();
10616 #endif
10617 
10618             wlan_imu_put_task_lock();
10619             /* Clear wlcmgr */
10620             wlan_stop();
10621         }
10622         power_off_device(LOAD_WIFI_FIRMWARE);
10623     }
10624 
10625     if (ResetOption == CLI_ENABLE_WIFI || ResetOption == CLI_RESET_WIFI)
10626     {
10627         PRINTF("--- Enable WiFi ---\r\n");
10628         if (!wlan_is_started())
10629         {
10630             PRINTF("Initialize WLAN Driver\r\n");
10631             /* Initialize WIFI Driver */
10632             if (WM_SUCCESS != (wlan_init(wlan_fw_bin, wlan_fw_bin_len)))
10633             {
10634                 wlcm_e("wlan init failed\r\n");
10635                 OSA_MutexUnlock((osa_mutex_handle_t)reset_lock);
10636                 assert(0);
10637                 return;
10638             }
10639 
10640             if (WM_SUCCESS != (wlan_start(wlan_event_callback)))
10641             {
10642                 wlcm_e("wlan start failed\r\n");
10643                 OSA_MutexUnlock((osa_mutex_handle_t)reset_lock);
10644                 return;
10645             }
10646 
10647             /* update the netif hwaddr after reset */
10648 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
10649             wlan_set_mac_addr(&wlan.sta_mac[0]);
10650             wlan_enable_all_networks();
10651 #else
10652 #if UAP_SUPPORT
10653             net_wlan_set_mac_address(&wlan.sta_mac[0], &wlan.uap_mac[0]);
10654 #else
10655             net_wlan_set_mac_address(&wlan.sta_mac[0], NULL);
10656 #endif
10657 #endif
10658             /* Unblock TX data */
10659             wifi_set_tx_status(WIFI_DATA_RUNNING);
10660             /* Unblock RX data */
10661             wifi_set_rx_status(WIFI_DATA_RUNNING);
10662             wifi_tx_block_cnt = 0;
10663             wifi_rx_block_cnt = 0;
10664 #if (CONFIG_WIFI_BLE_COEX_APP) && (CONFIG_WIFI_BLE_COEX_APP == 1)
10665 #if CONFIG_HOST_SLEEP
10666 #if CONFIG_POWER_MANAGER
10667             /* Re-enable host sleep for coex app */
10668             wlan_host_sleep_state = HOST_SLEEP_PERIODIC;
10669 #endif
10670 #endif
10671 #endif
10672         }
10673     }
10674 
10675     OSA_MutexUnlock((osa_mutex_handle_t)reset_lock);
10676 
10677     wlan_in_reset = false;
10678     PRINTF("--- Done ---\r\n");
10679 }
10680 
wlcmgr_mon_task(void * data)10681 static void wlcmgr_mon_task(void * data)
10682 {
10683 #if CONFIG_HOST_SLEEP
10684     int ret = 0;
10685 #endif
10686     osa_status_t status;
10687     struct wlan_message msg;
10688 
10689 #if CONFIG_POWER_MANAGER
10690     status = OSA_TimerCreate((osa_timer_handle_t)wake_timer, MSEC_TO_TICK(WAKE_TIMEOUT),
10691                           &wake_timer_cb, NULL, KOSA_TimerOnce, OSA_TIMER_NO_ACTIVATE);
10692     if (status != KOSA_StatusSuccess)
10693     {
10694         wlcm_e("Unable to create wake timer");
10695     }
10696 #endif
10697     while (1)
10698     {
10699         status = OSA_MsgQGet((osa_msgq_handle_t)mon_thread_events, &msg, osaWaitForever_c);
10700         if (status == KOSA_StatusSuccess)
10701         {
10702             /*Elements of wlan is not avaliable during wlan reset, so wait ending of wlan reset*/
10703             while(wlan_in_reset)
10704                 OSA_TimeDelay(10);
10705 #if CONFIG_HOST_SLEEP
10706              wlcm_d("got mon thread event: %d", msg.id);
10707             if (msg.id == HOST_SLEEP_HANDSHAKE)
10708             {
10709                 ret = wlan_send_host_sleep_int(wlan.wakeup_conditions, MFALSE);
10710                 if (ret != WM_SUCCESS)
10711                 {
10712                    is_hs_handshake_done = WLAN_HOSTSLEEP_FAIL;
10713                 }
10714             }
10715             else if (msg.id == HOST_SLEEP_EXIT)
10716             {
10717 #if CONFIG_POWER_MANAGER
10718                 if(!wlan_is_manual && wlan_host_sleep_state == HOST_SLEEP_PERIODIC)
10719                 {
10720                     wakelock_get();
10721                     (void)OSA_TimerActivate((osa_timer_handle_t)wake_timer);
10722                 }
10723 #endif
10724                 wlan_cancel_host_sleep();
10725                 /* Check fw status and write temperature to firmware after waking up */
10726                 temperature_mon_cb(NULL);
10727                 (void)OSA_TimerActivate((osa_timer_handle_t)temperature_mon_timer);
10728             }
10729 #endif
10730 #if CONFIG_WIFI_RECOVERY
10731             else if (msg.id == WIFI_RECOVERY_REQ)
10732             {
10733                 CONNECTION_EVENT(WLAN_REASON_FW_HANG, NULL);
10734                 wlan_reset(CLI_RESET_WIFI);
10735                 wifi_recovery_cnt ++;
10736             }
10737 #endif
10738         }
10739         else
10740         {
10741             wlcm_e("Failed to get events from monitor task queue");
10742         }
10743     }
10744 }
10745 #endif // RW610
10746 
10747 #if CONFIG_NCP_BRIDGE
wlan_stop_all_networks(void)10748 int wlan_stop_all_networks(void)
10749 {
10750     wifi_scan_stop();
10751 
10752     net_interface_down(net_get_sta_handle());
10753     wlan_disconnect();
10754 
10755 #if UAP_SUPPORT
10756     net_interface_down(net_get_uap_handle());
10757     send_user_request(CM_UAP_USER_REQUEST_STOP, 0);
10758 #endif
10759 
10760     return WM_SUCCESS;
10761 }
10762 
wlan_register_uap_prov_deinit_cb(int (* cb)(void))10763 void wlan_register_uap_prov_deinit_cb(int (*cb)(void))
10764 {
10765     uap_prov_deinit_cb = cb;
10766 }
10767 
wlan_register_uap_prov_cleanup_cb(void (* cb)(void))10768 void wlan_register_uap_prov_cleanup_cb(void (*cb)(void))
10769 {
10770     uap_prov_cleanup_cb = cb;
10771 }
10772 #endif
10773 
wlan_get_scan_result(unsigned int index,struct wlan_scan_result * res)10774 int wlan_get_scan_result(unsigned int index, struct wlan_scan_result *res)
10775 {
10776     struct wifi_scan_result2 *desc;
10777 
10778     if (res == NULL)
10779     {
10780         return -WM_E_INVAL;
10781     }
10782 
10783     if (!is_running())
10784     {
10785         return WLAN_ERROR_STATE;
10786     }
10787 
10788     if (wifi_get_scan_result(index, &desc) != 0)
10789     {
10790         return -WM_FAIL;
10791     }
10792 
10793     (void)memset(res, 0, sizeof(struct wlan_scan_result));
10794 
10795     (void)memcpy((void *)&res->bssid[0], (const void *)&desc->bssid[0], sizeof(res->bssid));
10796     (void)memcpy((void *)&res->ssid[0], (const void *)((char *)&desc->ssid[0]), (size_t)desc->ssid_len);
10797     res->ssid[desc->ssid_len] = (char)0;
10798     res->ssid_len             = (size_t)desc->ssid_len;
10799     res->channel              = desc->Channel;
10800     res->beacon_period        = desc->beacon_period;
10801     res->dtim_period          = desc->dtim_period;
10802 
10803     if (!desc->is_ibss_bit_set)
10804     {
10805         res->role = WLAN_BSS_ROLE_STA;
10806     }
10807     res->dot11n = (uint8_t)desc->phtcap_ie_present;
10808 #if CONFIG_11AC
10809     res->dot11ac = (uint8_t)desc->pvhtcap_ie_present;
10810 #endif
10811 #if CONFIG_11AX
10812     res->dot11ax = (uint8_t)desc->phecap_ie_present;
10813 #endif
10814 
10815     res->wmm = (uint8_t)desc->wmm_ie_present;
10816 #if (CONFIG_WPA_SUPP_WPS)
10817     if (desc->wps_IE_exist == true)
10818     {
10819         res->wps         = desc->wps_IE_exist;
10820         res->wps_session = desc->wps_session;
10821     }
10822 #elif (CONFIG_WPS2)
10823     if (desc->wps_IE_exist == true)
10824         res->wps = desc->wps_IE_exist;
10825     res->wps_session = desc->wps_session;
10826 #endif
10827     if (desc->WPA_WPA2_WEP.wpa2_entp != 0U)
10828     {
10829         res->wpa2_entp = 1;
10830     }
10831     else if (desc->WPA_WPA2_WEP.wpa3_entp  != 0U)
10832     {
10833         res->wpa3_entp  = 1;
10834     }
10835     else if (desc->WPA_WPA2_WEP.wpa3_1x_sha256 != 0U)
10836     {
10837         res->wpa3_1x_sha256 = 1;
10838     }
10839     else if (desc->WPA_WPA2_WEP.wpa3_1x_sha384 != 0U)
10840     {
10841         res->wpa3_1x_sha384 = 1;
10842     }
10843     else
10844     {
10845         if (desc->WPA_WPA2_WEP.wpa != 0U)
10846         {
10847             res->wpa = 1;
10848         }
10849         if (desc->WPA_WPA2_WEP.wpa3_sae != 0U)
10850         {
10851             res->wpa3_sae = 1;
10852         }
10853         if (desc->WPA_WPA2_WEP.owe != 0U)
10854         {
10855 #if CONFIG_DRIVER_OWE
10856             res->owe = 1;
10857 #endif
10858         }
10859         if (desc->WPA_WPA2_WEP.wpa2 != 0U)
10860         {
10861             res->wpa2 = 1;
10862         }
10863         if (desc->WPA_WPA2_WEP.wpa2_sha256 != 0U)
10864         {
10865             res->wpa2_sha256 = 1;
10866         }
10867         if ((desc->WPA_WPA2_WEP.wepStatic != 0U) || (desc->WPA_WPA2_WEP.wepDynamic != 0U))
10868         {
10869             res->wep = 1;
10870         }
10871     }
10872 #if CONFIG_11R
10873     if (res->wpa2_entp != 0U)
10874     {
10875         res->ft_1x = (t_u8)desc->WPA_WPA2_WEP.ft_1x;
10876     }
10877     if (res->wpa2 != 0U)
10878     {
10879         res->ft_psk = (t_u8)desc->WPA_WPA2_WEP.ft_psk;
10880     }
10881     if (res->wpa3_sae != 0U)
10882     {
10883         res->ft_sae = (t_u8)desc->WPA_WPA2_WEP.ft_sae;
10884     }
10885     if (res->wpa3_1x_sha384 != 0U)
10886     {
10887         res->ft_1x_sha384 = (t_u8)desc->WPA_WPA2_WEP.ft_1x_sha384;
10888     }
10889 #endif
10890     res->rssi = desc->RSSI;
10891 
10892     (void)memcpy((void *)&res->trans_bssid[0], (const void *)&desc->trans_bssid[0], sizeof(res->trans_bssid));
10893     (void)memcpy((void *)&res->trans_ssid[0], (const void *)((char *)&desc->trans_ssid[0]),
10894                  (unsigned int)desc->trans_ssid_len);
10895     res->trans_ssid[desc->trans_ssid_len] = (char)0;
10896     res->trans_ssid_len                   = (unsigned int)desc->trans_ssid_len;
10897 
10898 #if CONFIG_11K
10899     if (desc->neighbor_report_supported == true)
10900     {
10901         res->neighbor_report_supported = true;
10902     }
10903     else
10904     {
10905         res->neighbor_report_supported = false;
10906     }
10907 #endif
10908 
10909 #if CONFIG_11V
10910     if (desc->bss_transition_supported == true)
10911     {
10912         res->bss_transition_supported = true;
10913     }
10914     else
10915     {
10916         res->bss_transition_supported = false;
10917     }
10918 #endif
10919 
10920     res->ap_mfpc = desc->ap_mfpc;
10921     res->ap_mfpr = desc->ap_mfpr;
10922     res->ap_pwe = desc->ap_pwe;
10923 
10924     return WM_SUCCESS;
10925 }
10926 
wlan_set_cal_data(const uint8_t * cal_data,const unsigned int cal_data_size)10927 void wlan_set_cal_data(const uint8_t *cal_data, const unsigned int cal_data_size)
10928 {
10929     if (cal_data_size > 1)
10930     {
10931         wifi_set_cal_data(cal_data, cal_data_size);
10932     }
10933 }
10934 
wlan_set_mac_addr(uint8_t * mac)10935 int wlan_set_mac_addr(uint8_t *mac)
10936 {
10937     if (is_uap_starting() || is_sta_connecting() || !mac)
10938     {
10939         return -WM_FAIL;
10940     }
10941 
10942     if (wlan.status == WLCMGR_INIT_DONE || wlan.status == WLCMGR_ACTIVATED)
10943     {
10944 #if UAP_SUPPORT
10945         uint8_t ap_mac[MLAN_MAC_ADDR_LENGTH];
10946 
10947         (void)memcpy(ap_mac, mac, MLAN_MAC_ADDR_LENGTH);
10948         ap_mac[0] |= 2;
10949         ap_mac[4] += 1;
10950 
10951         net_wlan_set_mac_address((unsigned char *)mac, (unsigned char *)ap_mac);
10952 #else
10953         net_wlan_set_mac_address((unsigned char *)mac, NULL);
10954 #endif
10955         /* save the sta mac */
10956         _wifi_set_mac_addr(mac, MLAN_BSS_TYPE_STA);
10957         (void)memcpy(&wlan.sta_mac[0], mac, MLAN_MAC_ADDR_LENGTH);
10958 #if UAP_SUPPORT
10959         /* save the uap mac */
10960         _wifi_set_mac_addr(&ap_mac[0], MLAN_BSS_TYPE_UAP);
10961         (void)memcpy(&wlan.uap_mac[0], &ap_mac[0], MLAN_MAC_ADDR_LENGTH);
10962 #endif
10963     }
10964     else
10965     {
10966         wifi_set_mac_addr(mac);
10967     }
10968 #if CONFIG_WPS2
10969     (void)memcpy(wps_global.my_mac_addr, mac, MLAN_MAC_ADDR_LENGTH);
10970     (void)memcpy(wps_global.l2->my_mac_addr, mac, MLAN_MAC_ADDR_LENGTH);
10971 #endif
10972 
10973     return WM_SUCCESS;
10974 }
10975 
wlan_set_uap_mac_addr(uint8_t * mac)10976 int wlan_set_uap_mac_addr(uint8_t *mac)
10977 {
10978 #if UAP_SUPPORT
10979     /* Only suppoprt unicast mac */
10980     if (mac[0] & 0x01)
10981     {
10982         return -WM_FAIL;
10983     }
10984 
10985     if (is_uap_starting())
10986     {
10987         return -WM_FAIL;
10988     }
10989 
10990     if (memcmp(mac, &wlan.sta_mac[0], MLAN_MAC_ADDR_LENGTH) == 0)
10991     {
10992         return -WM_FAIL;
10993     }
10994 
10995     if (wlan.status == WLCMGR_INIT_DONE || wlan.status == WLCMGR_ACTIVATED)
10996     {
10997         net_wlan_set_mac_address(NULL, (unsigned char *)mac);
10998 
10999         _wifi_set_mac_addr(mac, MLAN_BSS_TYPE_UAP);
11000 
11001         /* save the uap mac */
11002         (void)memcpy(&wlan.uap_mac[0], mac, MLAN_MAC_ADDR_LENGTH);
11003     }
11004     else
11005     {
11006         wifi_set_mac_addr(mac);
11007     }
11008 
11009     return WM_SUCCESS;
11010 #else
11011     (void)mac;
11012     return WM_SUCCESS;
11013 #endif
11014 }
11015 
wlan_set_sta_mac_addr(uint8_t * mac)11016 int wlan_set_sta_mac_addr(uint8_t *mac)
11017 {
11018     /* Only suppoprt unicast mac */
11019     if (mac[0] & 0x01)
11020     {
11021         return -WM_FAIL;
11022     }
11023 
11024     if (is_sta_connecting())
11025     {
11026         return -WM_FAIL;
11027     }
11028 
11029 #if UAP_SUPPORT
11030     if (memcmp(mac, &wlan.uap_mac[0], MLAN_MAC_ADDR_LENGTH) == 0)
11031     {
11032         return -WM_FAIL;
11033     }
11034 #endif
11035 
11036     if (wlan.status == WLCMGR_INIT_DONE || wlan.status == WLCMGR_ACTIVATED)
11037     {
11038         net_wlan_set_mac_address((unsigned char *)mac, NULL);
11039 
11040         _wifi_set_mac_addr(mac, MLAN_BSS_TYPE_STA);
11041 
11042         /* save the sta mac */
11043         (void)memcpy(&wlan.sta_mac[0], mac, MLAN_MAC_ADDR_LENGTH);
11044     }
11045     else
11046     {
11047         wifi_set_mac_addr(mac);
11048     }
11049 
11050     return WM_SUCCESS;
11051 }
11052 
wlan_scan(int (* cb)(unsigned int count))11053 int wlan_scan(int (*cb)(unsigned int count))
11054 {
11055     int ret;
11056 
11057     wlan_scan_params_v2_t wlan_scan_param;
11058 
11059     (void)memset(&wlan_scan_param, 0, sizeof(wlan_scan_params_v2_t));
11060 
11061     wlan_scan_param.cb = cb;
11062 
11063     ret = wlan_scan_with_opt(wlan_scan_param);
11064 
11065     return ret;
11066 }
11067 
wlan_pscan(int (* cb)(unsigned int count))11068 static int wlan_pscan(int (*cb)(unsigned int count))
11069 {
11070     struct wlan_network* network = NULL;
11071     wlan_scan_params_v2_t wlan_scan_param;
11072     int ret;
11073 
11074     if (wlan.running && is_sta_associated())
11075     {
11076         network = &wlan.networks[wlan.cur_network_idx];
11077     }
11078 
11079     if (network == NULL)
11080     {
11081         wlcm_e("cannot get network info");
11082         return -WM_FAIL;
11083     }
11084 
11085     (void)memset(&wlan_scan_param, 0, sizeof(wlan_scan_params_v2_t));
11086 
11087     wlan_scan_param.cb = cb;
11088 
11089     (void)memcpy((void *)wlan_scan_param.bssid, (const void *)network->bssid, MLAN_MAC_ADDR_LENGTH);
11090 
11091     (void)memcpy((void *)wlan_scan_param.ssid, (const void *)network->ssid, strlen(network->ssid));
11092 
11093     wlan_scan_param.num_channels = 1;
11094 
11095     wlan_scan_param.chan_list[0].chan_number = (t_u8)network->channel;
11096     wlan_scan_param.chan_list[0].scan_type   = MLAN_SCAN_TYPE_PASSIVE;
11097     wlan_scan_param.chan_list[0].scan_time   = 200;
11098 
11099     ret = wlan_scan_with_opt(wlan_scan_param);
11100 
11101     return ret;
11102 }
11103 
wlan_scan_with_opt(wlan_scan_params_v2_t t_wlan_scan_param)11104 int wlan_scan_with_opt(wlan_scan_params_v2_t t_wlan_scan_param)
11105 {
11106     int ret;
11107     uint32_t chan_idx = 0;
11108     uint32_t t_idx = 0;
11109     wlan_scan_params_v2_t *wlan_scan_param = NULL;
11110 
11111     if (t_wlan_scan_param.cb == NULL)
11112     {
11113         return -WM_E_INVAL;
11114     }
11115 
11116     if (!is_running() || !is_scanning_allowed())
11117     {
11118         return WLAN_ERROR_STATE;
11119     }
11120 
11121 #if !CONFIG_MEM_POOLS
11122     wlan_scan_param = (wlan_scan_params_v2_t *)OSA_MemoryAllocate(sizeof(wlan_scan_params_v2_t));
11123 #else
11124      wlan_scan_param = (wlan_scan_params_v2_t *)OSA_MemoryPoolAllocate(buf_128_MemoryPool);
11125 #endif
11126 
11127     if (wlan_scan_param == NULL)
11128     {
11129         return -WM_E_NOMEM;
11130     }
11131 
11132     (void)memcpy((void *)wlan_scan_param, (const void *)&t_wlan_scan_param, sizeof(wlan_scan_params_v2_t));
11133 
11134     if (t_wlan_scan_param.num_channels > 0)
11135     {
11136         wlan_scan_param->num_channels = 0;
11137         (void)memset((void *)&wlan_scan_param->chan_list[0], 0x0, MAX_CHANNEL_LIST * sizeof(wifi_scan_channel_list_t));
11138 #if defined(RW610) && (CONFIG_ANT_DETECT)
11139         for (t_idx = 0; t_idx < ANT_DETECT_MAX_CHANNEL_LIST && t_wlan_scan_param.chan_list[t_idx].chan_number; t_idx++)
11140 #else
11141         for (t_idx = 0; t_idx < MAX_CHANNEL_LIST && t_wlan_scan_param.chan_list[t_idx].chan_number; t_idx++)
11142 #endif
11143         {
11144             if(!wlan_check_channel_by_region_table((mlan_private *)mlan_adap->priv[0], t_wlan_scan_param.chan_list[t_idx].chan_number))
11145             {
11146                 continue;
11147             }
11148             (void)memcpy((void *)&wlan_scan_param->chan_list[chan_idx], (const void *)&t_wlan_scan_param.chan_list[t_idx], sizeof(wifi_scan_channel_list_t));
11149             chan_idx++;
11150         }
11151         wlan_scan_param->num_channels = chan_idx;
11152 
11153         if (chan_idx == 0 && t_wlan_scan_param.num_channels > 0)
11154         {
11155             wlcm_e("no valid channel to scan");
11156 #if !CONFIG_MEM_POOLS
11157             OSA_MemoryFree(wlan_scan_param);
11158 #else
11159             OSA_MemoryPoolFree(buf_128_MemoryPool, wlan_scan_param);
11160 #endif
11161 
11162             return -WM_E_INVAL;
11163         }
11164     }
11165 
11166     wlcm_d("taking the scan lock (user scan)");
11167     dbg_lock_info();
11168     ret = OSA_SemaphoreWait((osa_semaphore_handle_t)wlan.scan_lock, osaWaitForever_c);
11169     if (ret != WM_SUCCESS)
11170     {
11171         wlcm_e("failed to get scan lock: 0x%X", ret);
11172 #if !CONFIG_MEM_POOLS
11173         OSA_MemoryFree(wlan_scan_param);
11174 #else
11175         OSA_MemoryPoolFree(buf_128_MemoryPool, wlan_scan_param);
11176 #endif
11177         return -WM_FAIL;
11178     }
11179     wlcm_d("got the scan lock (user scan)");
11180     wlan.is_scan_lock = 1;
11181 
11182     ret = send_user_request(CM_STA_USER_REQUEST_SCAN, (unsigned int)wlan_scan_param);
11183 
11184     if (ret != WM_SUCCESS)
11185     {
11186 #if !CONFIG_MEM_POOLS
11187         OSA_MemoryFree(wlan_scan_param);
11188 #else
11189         OSA_MemoryPoolFree(buf_128_MemoryPool, wlan_scan_param);
11190 #endif
11191     }
11192 
11193     return ret;
11194 }
11195 
wlan_get_connection_state(enum wlan_connection_state * state)11196 int wlan_get_connection_state(enum wlan_connection_state *state)
11197 {
11198     enum cm_sta_state cur;
11199 
11200     if (state == NULL)
11201     {
11202         return -WM_E_INVAL;
11203     }
11204 
11205     if (!is_running())
11206     {
11207         return WLAN_ERROR_STATE;
11208     }
11209 
11210     if (is_user_scanning() != 0)
11211     {
11212         cur = wlan.sta_return_to;
11213     }
11214     else
11215     {
11216         cur = wlan.sta_state;
11217     }
11218 
11219     switch (cur)
11220     {
11221         case CM_STA_IDLE:
11222             *state = WLAN_DISCONNECTED;
11223             break;
11224         case CM_STA_SCANNING:
11225             *state = WLAN_SCANNING;
11226             break;
11227         case CM_STA_ASSOCIATING:
11228             *state = WLAN_ASSOCIATING;
11229             break;
11230         case CM_STA_ASSOCIATED:
11231             *state = WLAN_ASSOCIATED;
11232             break;
11233         case CM_STA_AUTHENTICATED:
11234             *state = WLAN_AUTHENTICATED;
11235             break;
11236         case CM_STA_REQUESTING_ADDRESS:
11237         case CM_STA_OBTAINING_ADDRESS:
11238             *state = WLAN_CONNECTING;
11239             break;
11240         case CM_STA_CONNECTED:
11241             *state = WLAN_CONNECTED;
11242             break;
11243         default:
11244             *state = WLAN_DISCONNECTED;
11245             break;
11246     }
11247     return WM_SUCCESS;
11248 }
11249 
wlan_get_ps_mode(enum wlan_ps_mode * ps_mode)11250 int wlan_get_ps_mode(enum wlan_ps_mode *ps_mode)
11251 {
11252     if (ps_mode == NULL)
11253     {
11254         return -WM_E_INVAL;
11255     }
11256 
11257     *ps_mode = WLAN_ACTIVE;
11258 
11259     if (wlan.cm_ieeeps_configured && wlan.cm_deepsleepps_configured)
11260     {
11261         if (is_state(CM_STA_CONNECTED))
11262         {
11263             *ps_mode = WLAN_IEEE;
11264         }
11265         else
11266         {
11267             *ps_mode = WLAN_DEEP_SLEEP;
11268         }
11269     }
11270 #if CONFIG_WNM_PS
11271     else if (wlan.cm_wnmps_configured && wlan.cm_deepsleepps_configured)
11272     {
11273         if (is_state(CM_STA_CONNECTED))
11274         {
11275             *ps_mode = WLAN_WNM;
11276         }
11277         else
11278         {
11279             *ps_mode = WLAN_DEEP_SLEEP;
11280         }
11281     }
11282     else if (wlan.cm_wnmps_configured)
11283     {
11284         *ps_mode = WLAN_WNM;
11285     }
11286 #endif
11287     else if (wlan.cm_deepsleepps_configured)
11288     {
11289         *ps_mode = WLAN_DEEP_SLEEP;
11290     }
11291     else if (wlan.cm_ieeeps_configured)
11292     {
11293         *ps_mode = WLAN_IEEE;
11294     }
11295     else
11296     { /* Do Nothing */
11297     }
11298 
11299     return WM_SUCCESS;
11300 }
11301 
wlan_get_ps_mode_cfg(uint8_t * ps_mode_cfg)11302 int wlan_get_ps_mode_cfg(uint8_t *ps_mode_cfg)
11303 {
11304     if (ps_mode_cfg == NULL)
11305     {
11306         return -WM_E_INVAL;
11307     }
11308 
11309     *ps_mode_cfg = (wlan.cm_ieeeps_configured | (wlan.cm_deepsleepps_configured << 1)
11310 #if CONFIG_WNM_PS
11311     | (wlan.cm_wnmps_configured << 2)
11312 #endif
11313     );
11314 
11315     return WM_SUCCESS;
11316 }
11317 
wlan_get_uap_connection_state(enum wlan_connection_state * state)11318 int wlan_get_uap_connection_state(enum wlan_connection_state *state)
11319 {
11320     enum cm_uap_state cur;
11321 
11322     if (state == NULL)
11323     {
11324         return -WM_E_INVAL;
11325     }
11326 
11327     if (!is_running())
11328     {
11329         return WLAN_ERROR_STATE;
11330     }
11331 
11332     cur = wlan.uap_state;
11333 
11334     switch (cur)
11335     {
11336         case CM_UAP_INITIALIZING:
11337         case CM_UAP_CONFIGURED:
11338             *state = WLAN_UAP_STOPPED;
11339             break;
11340         case CM_UAP_STARTED:
11341         case CM_UAP_IP_UP:
11342             *state = WLAN_UAP_STARTED;
11343             break;
11344         default:
11345             *state = WLAN_UAP_STOPPED;
11346             break;
11347     }
11348     return WM_SUCCESS;
11349 }
11350 
wlan_get_address(struct wlan_ip_config * addr)11351 int wlan_get_address(struct wlan_ip_config *addr)
11352 {
11353     void *if_handle = NULL;
11354     if (addr == NULL)
11355     {
11356         return -WM_E_INVAL;
11357     }
11358 
11359     if (!is_running() || !is_state(CM_STA_CONNECTED))
11360     {
11361         return WLAN_ERROR_STATE;
11362     }
11363 
11364     if_handle = net_get_mlan_handle();
11365     if (net_get_if_addr((struct net_ip_config *)addr, if_handle) != 0)
11366     {
11367         return -WM_FAIL;
11368     }
11369 
11370 #if CONFIG_IPV6
11371     if (net_get_if_ipv6_addr((struct net_ip_config *)addr, if_handle) != 0)
11372     {
11373         return -WM_FAIL;
11374     }
11375 #endif
11376 
11377     return WM_SUCCESS;
11378 }
11379 
wlan_get_uap_address(struct wlan_ip_config * addr)11380 int wlan_get_uap_address(struct wlan_ip_config *addr)
11381 {
11382 #if UAP_SUPPORT
11383     void *if_handle = NULL;
11384     if (addr == NULL)
11385     {
11386         return -WM_E_INVAL;
11387     }
11388     if (!is_running() || !is_uap_state(CM_UAP_IP_UP))
11389     {
11390         return WLAN_ERROR_STATE;
11391     }
11392 
11393     if_handle = net_get_uap_handle();
11394     if (net_get_if_addr((struct net_ip_config *)addr, if_handle) != 0)
11395     {
11396         return -WM_FAIL;
11397     }
11398     return WM_SUCCESS;
11399 #else
11400     return -WM_E_NODEV;
11401 #endif
11402 }
11403 
wlan_get_uap_channel(int * channel)11404 int wlan_get_uap_channel(int *channel)
11405 {
11406 #if UAP_SUPPORT
11407     if (channel != NULL)
11408     {
11409         *channel = 0;
11410     }
11411     else
11412     {
11413         return (int)(-WM_E_INVAL);
11414     }
11415 
11416     return wifi_get_uap_channel(channel);
11417 #else
11418     return -WM_E_NODEV;
11419 #endif
11420 }
11421 
11422 #if CONFIG_P2P
wlan_get_wfd_address(struct wlan_ip_config * addr)11423 int wlan_get_wfd_address(struct wlan_ip_config *addr)
11424 {
11425     void *if_handle = NULL;
11426     if (addr == NULL)
11427         return -WM_E_INVAL;
11428     if (!is_running())
11429         return WLAN_ERROR_STATE;
11430 
11431     if_handle = net_get_wfd_handle();
11432     if (net_get_if_addr((struct net_ip_config *)addr, if_handle))
11433         return -WM_FAIL;
11434     return WM_SUCCESS;
11435 }
11436 #endif
11437 
wlan_get_mac_address(unsigned char * dest)11438 int wlan_get_mac_address(unsigned char *dest)
11439 {
11440     if (!dest)
11441         return -WM_E_INVAL;
11442     (void)memset(dest, 0, MLAN_MAC_ADDR_LENGTH);
11443     (void)memcpy(dest, &wlan.sta_mac[0], MLAN_MAC_ADDR_LENGTH);
11444     return WM_SUCCESS;
11445 }
11446 
wlan_get_mac_address_uap(unsigned char * dest)11447 int wlan_get_mac_address_uap(unsigned char *dest)
11448 {
11449 #if UAP_SUPPORT
11450     if (!dest)
11451         return -WM_E_INVAL;
11452     (void)memset(dest, 0, MLAN_MAC_ADDR_LENGTH);
11453     (void)memcpy(dest, &wlan.uap_mac[0], MLAN_MAC_ADDR_LENGTH);
11454     return WM_SUCCESS;
11455 #else
11456     return -WM_E_NODEV;
11457 #endif
11458 }
11459 
11460 #if CONFIG_P2P
wlan_get_wfd_mac_address(unsigned char * dest)11461 int wlan_get_wfd_mac_address(unsigned char *dest)
11462 {
11463     if (dest == NULL)
11464         return -WM_E_INVAL;
11465     (void)memset((void *)dest, 0, MLAN_MAC_ADDR_LENGTH);
11466     (void)memcpy((void *)dest, (const void *)&wlan.wfd_mac[0], MLAN_MAC_ADDR_LENGTH);
11467     return WM_SUCCESS;
11468 }
11469 #endif
11470 
wlan_wake_up_card(void)11471 void wlan_wake_up_card(void)
11472 {
11473     uint32_t resp;
11474 
11475     wifi_wake_up_card(&resp);
11476 }
11477 
wlan_set_ieeeps_cfg(struct wlan_ieeeps_config * ps_cfg)11478 int wlan_set_ieeeps_cfg(struct wlan_ieeeps_config *ps_cfg)
11479 {
11480     wlan_configure_null_pkt_interval(ps_cfg->ps_null_interval);
11481 
11482     return send_user_request(CM_STA_USER_REQUEST_PS_ENTER, WLAN_IEEE);
11483 }
11484 
wlan_configure_listen_interval(int listen_interval)11485 void wlan_configure_listen_interval(int listen_interval)
11486 {
11487     wifi_configure_listen_interval(listen_interval);
11488 }
11489 
wlan_configure_delay_to_ps(unsigned int timeout_ms)11490 void wlan_configure_delay_to_ps(unsigned int timeout_ms)
11491 {
11492     wifi_configure_delay_to_ps(timeout_ms);
11493 }
11494 
wlan_get_listen_interval(void)11495 unsigned short wlan_get_listen_interval(void)
11496 {
11497     return wifi_get_listen_interval();
11498 }
11499 
wlan_get_delay_to_ps(void)11500 unsigned int wlan_get_delay_to_ps(void)
11501 {
11502     return wifi_get_delay_to_ps();
11503 }
11504 
wlan_is_power_save_enabled()11505 bool wlan_is_power_save_enabled()
11506 {
11507     if (wlan.cm_ieeeps_configured
11508 #if CONFIG_WNM_PS
11509         || wlan.cm_wnmps_configured
11510 #endif
11511         || wlan.cm_deepsleepps_configured)
11512     {
11513         return true;
11514     }
11515 
11516     return false;
11517 }
11518 
wlan_configure_null_pkt_interval(int time_in_secs)11519 void wlan_configure_null_pkt_interval(int time_in_secs)
11520 {
11521     wifi_configure_null_pkt_interval((unsigned int)time_in_secs);
11522 }
11523 
wlan_ieeeps_on(unsigned int wakeup_conditions)11524 int wlan_ieeeps_on(unsigned int wakeup_conditions)
11525 {
11526     enum wlan_connection_state state;
11527 
11528     if ((!wlan.running) || (wlan_get_uap_connection_state(&state) != 0) || (state == WLAN_UAP_STARTED))
11529     {
11530         return -WM_FAIL;
11531     }
11532 
11533     if (wlan.cm_ieeeps_configured
11534 #if CONFIG_WNM_PS
11535         || wlan.cm_wnmps_configured
11536 #endif
11537        )
11538     {
11539 #if CONFIG_WNM_PS
11540         if (wlan.cm_wnmps_configured == true)
11541         {
11542             wlcm_d("wnm ps already enabled: %d, ieee ps could not be enabled", wlan.cm_wnmps_configured);
11543             return -WM_FAIL;
11544         }
11545         else
11546 #endif
11547         {
11548             wlcm_d("ieee ps already enabled");
11549         }
11550 
11551         CONNECTION_EVENT(WLAN_REASON_PS_ENTER, (void *)WLAN_IEEE);
11552         return WM_SUCCESS;
11553     }
11554 
11555     return send_user_request(CM_STA_USER_REQUEST_PS_ENTER, WLAN_IEEE);
11556 }
11557 
wlan_ieeeps_off(void)11558 int wlan_ieeeps_off(void)
11559 {
11560     if (wlan.cm_ieeeps_configured)
11561     {
11562         return send_user_request(CM_STA_USER_REQUEST_PS_EXIT, WLAN_IEEE);
11563     }
11564 
11565     CONNECTION_EVENT(WLAN_REASON_PS_EXIT, (void *)WLAN_IEEE);
11566     return WM_SUCCESS;
11567 }
11568 
11569 #if (CONFIG_WNM_PS)
wlan_wnmps_on(unsigned int wakeup_conditions,t_u16 wnm_sleep_time)11570 int wlan_wnmps_on(unsigned int wakeup_conditions, t_u16 wnm_sleep_time)
11571 {
11572     enum wlan_connection_state state;
11573 
11574     if ((!wlan.running) || (wlan_get_uap_connection_state(&state) != 0) || (state == WLAN_UAP_STARTED))
11575     {
11576         return -WM_FAIL;
11577     }
11578 
11579     if (wlan.cm_ieeeps_configured || wlan.cm_wnmps_configured)
11580     {
11581         if (wlan.cm_ieeeps_configured == true)
11582         {
11583             wlcm_d("ieee ps already enabled: %d, wnm ps could not be enabled", wlan.cm_ieeeps_configured);
11584             return -WM_FAIL;
11585         }
11586         else
11587         {
11588             wlcm_d("wnm ps already enabled");
11589         }
11590 
11591         CONNECTION_EVENT(WLAN_REASON_PS_ENTER, (void *)WLAN_WNM);
11592         return WM_SUCCESS;
11593     }
11594 
11595     wlan.wakeup_conditions   = wakeup_conditions;
11596     wlan.wnm_sleep_time      = wnm_sleep_time;
11597     return send_user_request(CM_STA_USER_REQUEST_PS_ENTER, WLAN_WNM);
11598 }
11599 
wlan_wnmps_off()11600 int wlan_wnmps_off()
11601 {
11602     if (wlan.cm_wnmps_configured)
11603     {
11604         return send_user_request(CM_STA_USER_REQUEST_PS_EXIT, WLAN_WNM);
11605     }
11606 
11607     CONNECTION_EVENT(WLAN_REASON_PS_EXIT, (void *)WLAN_WNM);
11608     return WM_SUCCESS;
11609 }
11610 #endif
11611 
wlan_deepsleepps_on(void)11612 int wlan_deepsleepps_on(void)
11613 {
11614     enum wlan_connection_state state;
11615 
11616     if ((!wlan.running) || (wlan_get_uap_connection_state(&state) != 0) || (state == WLAN_UAP_STARTED))
11617     {
11618         return -WM_FAIL;
11619     }
11620 
11621     if (wlan.cm_deepsleepps_configured)
11622     {
11623         CONNECTION_EVENT(WLAN_REASON_PS_ENTER, (void *)WLAN_DEEP_SLEEP);
11624         return WM_SUCCESS;
11625     }
11626 
11627     return send_user_request(CM_STA_USER_REQUEST_PS_ENTER, WLAN_DEEP_SLEEP);
11628 }
11629 
wlan_deepsleepps_off(void)11630 int wlan_deepsleepps_off(void)
11631 {
11632     if (wlan.cm_deepsleepps_configured)
11633     {
11634         return send_user_request(CM_STA_USER_REQUEST_PS_EXIT, WLAN_DEEP_SLEEP);
11635     }
11636 
11637     CONNECTION_EVENT(WLAN_REASON_PS_EXIT, (void *)WLAN_DEEP_SLEEP);
11638     return WM_SUCCESS;
11639 }
11640 
11641 #if CONFIG_WPS2
wlan_start_wps_pbc()11642 int wlan_start_wps_pbc()
11643 {
11644     wlan_wps.wps_state = PROV_WPS_PBC_ENABLED;
11645     wlan_wps.wps_cmd   = CMD_WPS_PBC;
11646     wlan_wps.wps_pin   = 0;
11647 
11648     return WM_SUCCESS;
11649 }
11650 
wlan_wps_generate_pin(uint32_t * pin)11651 void wlan_wps_generate_pin(uint32_t *pin)
11652 {
11653     wps_generate_pin(pin);
11654 }
11655 
wlan_start_wps_pin(uint32_t pin)11656 int wlan_start_wps_pin(uint32_t pin)
11657 {
11658     if (wps_validate_pin(pin) != WM_SUCCESS)
11659     {
11660         wlcm_d("WPS PIN validation failed for %d", pin);
11661         return -WM_FAIL;
11662     }
11663 
11664     wlan_wps.wps_state = PROV_WPS_PIN_ENABLED;
11665     wlan_wps.wps_cmd   = CMD_WPS_PIN;
11666     wlan_wps.wps_pin   = pin;
11667 
11668     return WM_SUCCESS;
11669 }
11670 #endif
11671 
11672 #ifdef STREAM_2X2
wlan_set_current_ant(uint8_t tx_antenna,uint8_t rx_antenna)11673 int wlan_set_current_ant(uint8_t tx_antenna, uint8_t rx_antenna)
11674 {
11675     int rv = wifi_set_antenna(tx_antenna, rx_antenna);
11676     if (rv != WM_SUCCESS)
11677     {
11678         wlcm_e("Unable to set antenna");
11679         return WLAN_ERROR_STATE;
11680     }
11681 
11682     return WM_SUCCESS;
11683 }
11684 #else
11685 #ifndef RW610
wlan_set_antcfg(uint32_t ant,uint16_t evaluate_time)11686 int wlan_set_antcfg(uint32_t ant, uint16_t evaluate_time)
11687 {
11688     int rv = wifi_set_antenna(ant, evaluate_time);
11689     if (rv != WM_SUCCESS)
11690     {
11691         wlcm_e("Unable to set antenna");
11692         return WLAN_ERROR_STATE;
11693     }
11694 
11695     return WM_SUCCESS;
11696 }
11697 
wlan_get_antcfg(uint32_t * ant,uint16_t * evaluate_time,uint16_t * current_antenna)11698 int wlan_get_antcfg(uint32_t *ant, uint16_t *evaluate_time, uint16_t *current_antenna)
11699 {
11700     int rv = wifi_get_antenna((unsigned int *)ant, evaluate_time, current_antenna);
11701     if (rv != WM_SUCCESS)
11702     {
11703         wlcm_e("Unable to get current antenna");
11704         return WLAN_ERROR_STATE;
11705     }
11706 
11707     return WM_SUCCESS;
11708 }
11709 #else
wlan_set_antcfg(uint32_t ant,uint16_t evaluate_time,uint8_t evaluate_mode)11710 int wlan_set_antcfg(uint32_t ant, uint16_t evaluate_time, uint8_t evaluate_mode)
11711 {
11712     int rv = wifi_set_antenna(ant, evaluate_time, evaluate_mode);
11713     if (rv != WM_SUCCESS)
11714     {
11715         wlcm_e("Unable to set antenna");
11716         return WLAN_ERROR_STATE;
11717     }
11718 
11719     return WM_SUCCESS;
11720 }
11721 
wlan_get_antcfg(uint32_t * ant,uint16_t * evaluate_time,uint8_t * evaluate_mode,uint16_t * current_antenna)11722 int wlan_get_antcfg(uint32_t *ant, uint16_t *evaluate_time, uint8_t *evaluate_mode, uint16_t *current_antenna)
11723 {
11724     int rv = wifi_get_antenna((unsigned int *)ant, evaluate_time, evaluate_mode, current_antenna);
11725     if (rv != WM_SUCCESS)
11726     {
11727         wlcm_e("Unable to get current antenna");
11728         return WLAN_ERROR_STATE;
11729     }
11730 
11731     return WM_SUCCESS;
11732 }
11733 #endif /*RW610*/
11734 
11735 #endif
11736 
wlan_wlcmgr_send_msg(enum wifi_event event,enum wifi_event_reason reason,void * data)11737 int wlan_wlcmgr_send_msg(enum wifi_event event, enum wifi_event_reason reason, void *data)
11738 {
11739     struct wifi_message msg;
11740 
11741     msg.event  = (uint16_t)event;
11742     msg.reason = reason;
11743     msg.data   = (void *)data;
11744 
11745     if (OSA_MsgQPut((osa_msgq_handle_t)wlan.events, &msg) == KOSA_StatusSuccess)
11746     {
11747         return WM_SUCCESS;
11748     }
11749 
11750     return -WM_FAIL;
11751 }
11752 
11753 /*
11754   This function validates input string for a valid WEP key, converts
11755   it to appropriate format
11756 
11757   Returns positive integer equal to size of the output string
11758   for successful operation or -WM_FAIL in case of failure to convert. Note
11759   that the output string is not NULL terminated.
11760  */
load_wep_key(const uint8_t * input,uint8_t * output,uint8_t * output_len,const unsigned max_output_len)11761 int load_wep_key(const uint8_t *input, uint8_t *output, uint8_t *output_len, const unsigned max_output_len)
11762 {
11763     if (input == NULL || output == NULL)
11764     {
11765         return -WM_FAIL;
11766     }
11767 
11768     unsigned len = *output_len;
11769 
11770     /* fixme: add macros here after mlan integration */
11771     if (len == 10U || len == 26U)
11772     {
11773         /* Looks like this is an hexadecimal key */
11774         int ret = (int)wm_hex2bin(input, output, max_output_len);
11775         if (ret == 0)
11776         {
11777             return -WM_FAIL;
11778         }
11779 
11780         len = len / 2U;
11781     }
11782     else if (len == 5U || len == 13U)
11783     {
11784         /* Looks like this is ASCII key  */
11785         if (len > max_output_len)
11786         {
11787             return -WM_FAIL;
11788         }
11789 
11790         (void)memcpy((void *)output, (const void *)input, len);
11791     }
11792     else
11793     {
11794         return -WM_FAIL;
11795     }
11796 
11797     *output_len = (uint8_t)len;
11798 
11799     return WM_SUCCESS;
11800 }
11801 
get_split_scan_delay_ms(void)11802 int get_split_scan_delay_ms(void)
11803 {
11804     return g_wifi_scan_params.split_scan_delay;
11805 }
11806 
wlan_get_firmware_version_ext(void)11807 char *wlan_get_firmware_version_ext(void)
11808 {
11809     return wlan.fw_ver_ext.version_str;
11810 }
11811 
wlan_get_uap_supported_max_clients(void)11812 unsigned int wlan_get_uap_supported_max_clients(void)
11813 {
11814     return wlan.uap_supported_max_sta_num;
11815 }
11816 
wlan_get_uap_max_clients(unsigned int * max_sta_num)11817 int wlan_get_uap_max_clients(unsigned int *max_sta_num)
11818 {
11819 #if UAP_SUPPORT
11820     return wifi_get_uap_max_clients(max_sta_num);
11821 #else
11822     return -WM_E_NODEV;
11823 #endif
11824 }
11825 
wlan_set_uap_max_clients(unsigned int max_sta_num)11826 int wlan_set_uap_max_clients(unsigned int max_sta_num)
11827 {
11828 #if UAP_SUPPORT
11829     int ret = -WM_FAIL;
11830 
11831     if (is_uap_started() != 0)
11832     {
11833         wlcm_e(
11834                 "Cannot set the max station number "
11835                 "as the uAP is already running");
11836         return -WM_FAIL;
11837     }
11838     else if (max_sta_num > wlan.uap_supported_max_sta_num)
11839     {
11840         wlcm_e(
11841                 "Maximum supported station number "
11842                 "limit is = %d",
11843                 wlan.uap_supported_max_sta_num);
11844         return -WM_FAIL;
11845     }
11846     else
11847     {
11848         ret = wifi_set_uap_max_clients(&max_sta_num);
11849 
11850         if (ret != WM_SUCCESS)
11851         {
11852             return ret;
11853         }
11854 
11855         return ret;
11856     }
11857 #else
11858     return -WM_E_NODEV;
11859 #endif
11860 }
11861 
wlan_get_mgmt_ie(enum wlan_bss_type bss_type,IEEEtypes_ElementId_t index,void * buf,unsigned int * buf_len)11862 int wlan_get_mgmt_ie(enum wlan_bss_type bss_type, IEEEtypes_ElementId_t index, void *buf, unsigned int *buf_len)
11863 {
11864     return wifi_get_mgmt_ie((mlan_bss_type)bss_type, index, buf, buf_len);
11865 }
11866 
wlan_set_mgmt_ie(enum wlan_bss_type bss_type,IEEEtypes_ElementId_t id,void * buf,unsigned int buf_len)11867 int wlan_set_mgmt_ie(enum wlan_bss_type bss_type, IEEEtypes_ElementId_t id, void *buf, unsigned int buf_len)
11868 {
11869     return wifi_set_mgmt_ie((mlan_bss_type)bss_type, id, buf, buf_len);
11870 }
11871 
11872 #ifdef SD8801
wlan_get_ext_coex_stats(wlan_ext_coex_stats_t * ext_coex_stats)11873 int wlan_get_ext_coex_stats(wlan_ext_coex_stats_t *ext_coex_stats)
11874 {
11875     return wifi_get_ext_coex_stats(ext_coex_stats);
11876 }
11877 
wlan_set_ext_coex_config(const wlan_ext_coex_config_t ext_coex_config)11878 int wlan_set_ext_coex_config(const wlan_ext_coex_config_t ext_coex_config)
11879 {
11880     return wifi_set_ext_coex_config(&ext_coex_config);
11881 }
11882 #endif
11883 
wlan_clear_mgmt_ie(enum wlan_bss_type bss_type,IEEEtypes_ElementId_t index,int mgmt_bitmap_index)11884 int wlan_clear_mgmt_ie(enum wlan_bss_type bss_type, IEEEtypes_ElementId_t index, int mgmt_bitmap_index)
11885 {
11886     return wifi_clear_mgmt_ie((mlan_bss_type)bss_type, index, mgmt_bitmap_index);
11887 }
11888 
wlan_set_txbfcap(unsigned int tx_bf_cap)11889 int wlan_set_txbfcap(unsigned int tx_bf_cap)
11890 {
11891     return wifi_set_txbfcap(tx_bf_cap);
11892 }
11893 
wlan_set_htcapinfo(unsigned int htcapinfo)11894 int wlan_set_htcapinfo(unsigned int htcapinfo)
11895 {
11896     return wifi_set_htcapinfo(htcapinfo);
11897 }
11898 
wlan_set_httxcfg(unsigned short httxcfg)11899 int wlan_set_httxcfg(unsigned short httxcfg)
11900 {
11901     return wifi_set_httxcfg(httxcfg);
11902 }
11903 
wlan_set_txratecfg(wlan_ds_rate ds_rate,mlan_bss_type bss_type)11904 int wlan_set_txratecfg(wlan_ds_rate ds_rate, mlan_bss_type bss_type)
11905 {
11906     return wifi_set_txratecfg(ds_rate, bss_type);
11907 }
11908 
wlan_get_txratecfg(wlan_ds_rate * ds_rate,mlan_bss_type bss_type)11909 int wlan_get_txratecfg(wlan_ds_rate *ds_rate, mlan_bss_type bss_type)
11910 {
11911     int ret;
11912 
11913     ret = wifi_get_txratecfg(ds_rate, bss_type);
11914 
11915     if (ret != WM_SUCCESS)
11916     {
11917         return ret;
11918     }
11919 
11920     ret = wifi_get_data_rate(ds_rate, bss_type);
11921 
11922     if (ret != WM_SUCCESS)
11923     {
11924         return ret;
11925     }
11926 
11927     return WM_SUCCESS;
11928 }
11929 
wlan_set_sta_tx_power(t_u32 power_level)11930 int wlan_set_sta_tx_power(t_u32 power_level)
11931 {
11932     return wifi_set_tx_power(power_level);
11933 }
11934 
wlan_get_sta_tx_power(t_u32 * power_level)11935 int wlan_get_sta_tx_power(t_u32 *power_level)
11936 {
11937     return wifi_get_tx_power(power_level);
11938 }
11939 
11940 #if CONFIG_COMPRESS_TX_PWTBL
wlan_set_region_power_cfg(const t_u8 * data,t_u16 len)11941 int wlan_set_region_power_cfg(const t_u8 *data, t_u16 len)
11942 {
11943     return wifi_set_region_power_cfg(data, len);
11944 }
11945 #endif
11946 
wlan_set_chanlist_and_txpwrlimit(wlan_chanlist_t * chanlist,wlan_txpwrlimit_t * txpwrlimit)11947 int wlan_set_chanlist_and_txpwrlimit(wlan_chanlist_t *chanlist, wlan_txpwrlimit_t *txpwrlimit)
11948 {
11949     int ret = WM_SUCCESS;
11950 
11951     ret = wlan_set_chanlist(chanlist);
11952     if (ret != WM_SUCCESS)
11953     {
11954         wlcm_e("Cannot set Channel List");
11955         return ret;
11956     }
11957 
11958     ret = wlan_set_txpwrlimit(txpwrlimit);
11959     if (ret != WM_SUCCESS)
11960     {
11961         wlcm_e("Cannot set Tx Power");
11962     }
11963 
11964     return ret;
11965 }
11966 
wlan_set_chanlist(wlan_chanlist_t * chanlist)11967 int wlan_set_chanlist(wlan_chanlist_t *chanlist)
11968 {
11969     if (chanlist != NULL)
11970     {
11971         return wifi_set_chanlist(chanlist);
11972     }
11973 
11974     return -WM_FAIL;
11975 }
11976 
wlan_get_chanlist(wlan_chanlist_t * chanlist)11977 int wlan_get_chanlist(wlan_chanlist_t *chanlist)
11978 {
11979     if (chanlist != NULL)
11980     {
11981         return wifi_get_chanlist(chanlist);
11982     }
11983 
11984     return -WM_FAIL;
11985 }
11986 
wlan_set_txpwrlimit(wlan_txpwrlimit_t * txpwrlimit)11987 int wlan_set_txpwrlimit(wlan_txpwrlimit_t *txpwrlimit)
11988 {
11989     if (txpwrlimit != NULL)
11990     {
11991         return wifi_set_txpwrlimit(txpwrlimit);
11992     }
11993 
11994     return -WM_FAIL;
11995 }
11996 
wlan_get_txpwrlimit(wifi_SubBand_t subband,wifi_txpwrlimit_t * txpwrlimit)11997 int wlan_get_txpwrlimit(wifi_SubBand_t subband, wifi_txpwrlimit_t *txpwrlimit)
11998 {
11999     if (txpwrlimit != NULL)
12000     {
12001         (void)memset(txpwrlimit, 0x00, sizeof(wifi_txpwrlimit_t));
12002         return wifi_get_txpwrlimit(subband, txpwrlimit);
12003     }
12004 
12005     return -WM_FAIL;
12006 }
12007 
12008 #if WLAN_LOW_POWER_ENABLE
wlan_enable_low_pwr_mode(void)12009 int wlan_enable_low_pwr_mode(void)
12010 {
12011     if (wlan.status == WLCMGR_INACTIVE)
12012     {
12013         wifi_enable_low_pwr_mode();
12014         return WM_SUCCESS;
12015     }
12016     else
12017         return -WM_FAIL;
12018 }
12019 #endif
12020 
wlan_set_reassoc_control(bool reassoc_control)12021 void wlan_set_reassoc_control(bool reassoc_control)
12022 {
12023     wlan.reassoc_control = reassoc_control;
12024     wlcm_d("Reassoc control %s", reassoc_control ? "enabled" : "disabled");
12025 }
12026 
wlan_set_ed_mac_mode(wlan_ed_mac_ctrl_t wlan_ed_mac_ctrl)12027 int wlan_set_ed_mac_mode(wlan_ed_mac_ctrl_t wlan_ed_mac_ctrl)
12028 {
12029     return wifi_set_ed_mac_mode(&wlan_ed_mac_ctrl, MLAN_BSS_TYPE_STA);
12030 }
12031 
wlan_get_ed_mac_mode(wlan_ed_mac_ctrl_t * wlan_ed_mac_ctrl)12032 int wlan_get_ed_mac_mode(wlan_ed_mac_ctrl_t *wlan_ed_mac_ctrl)
12033 {
12034     return wifi_get_ed_mac_mode(wlan_ed_mac_ctrl, MLAN_BSS_TYPE_STA);
12035 }
12036 
wlan_set_uap_ed_mac_mode(wlan_ed_mac_ctrl_t wlan_ed_mac_ctrl)12037 int wlan_set_uap_ed_mac_mode(wlan_ed_mac_ctrl_t wlan_ed_mac_ctrl)
12038 {
12039     return wifi_set_ed_mac_mode(&wlan_ed_mac_ctrl, MLAN_BSS_TYPE_UAP);
12040 }
12041 
wlan_get_uap_ed_mac_mode(wlan_ed_mac_ctrl_t * wlan_ed_mac_ctrl)12042 int wlan_get_uap_ed_mac_mode(wlan_ed_mac_ctrl_t *wlan_ed_mac_ctrl)
12043 {
12044     return wifi_get_ed_mac_mode(wlan_ed_mac_ctrl, MLAN_BSS_TYPE_UAP);
12045 }
12046 
wlan_get_11d_enable_status(void)12047 bool wlan_get_11d_enable_status(void)
12048 {
12049     return wrapper_wlan_11d_support_is_enabled();
12050 }
12051 
wlan_remain_on_channel(const enum wlan_bss_type bss_type,const bool status,const uint8_t channel,const uint32_t duration)12052 int wlan_remain_on_channel(const enum wlan_bss_type bss_type,
12053                            const bool status,
12054                            const uint8_t channel,
12055                            const uint32_t duration)
12056 {
12057     wifi_remain_on_channel_t roc;
12058 
12059     (void)memset(&roc, 0x00, sizeof(wifi_remain_on_channel_t));
12060 
12061     roc.remove = (uint16_t)!status;
12062 
12063     roc.channel = channel;
12064 
12065     roc.remain_period = duration;
12066 
12067 #if CONFIG_5GHz_SUPPORT
12068     if (channel > 14)
12069     {
12070         roc.bandcfg = 1;
12071     }
12072 #endif
12073 
12074     return wifi_send_remain_on_channel_cmd((uint32_t)bss_type, &roc);
12075 }
12076 
wlan_get_otp_user_data(uint8_t * buf,uint16_t len)12077 int wlan_get_otp_user_data(uint8_t *buf, uint16_t len)
12078 {
12079     if (buf == NULL)
12080     {
12081         return -WM_E_INVAL;
12082     }
12083 
12084     return wifi_get_otp_user_data(buf, len);
12085 }
12086 
12087 #if CONFIG_WIFI_GET_LOG
wlan_get_log(wlan_pkt_stats_t * stats)12088 int wlan_get_log(wlan_pkt_stats_t *stats)
12089 {
12090     if (!stats)
12091         return -WM_E_INVAL;
12092 
12093     return wifi_get_log(stats, MLAN_BSS_TYPE_STA);
12094 }
12095 
wlan_uap_get_log(wlan_pkt_stats_t * stats)12096 int wlan_uap_get_log(wlan_pkt_stats_t *stats)
12097 {
12098     if (!stats)
12099         return -WM_E_INVAL;
12100 
12101     return wifi_get_log(stats, MLAN_BSS_TYPE_UAP);
12102 }
12103 #endif
12104 
wlan_get_cal_data(wlan_cal_data_t * cal_data)12105 int wlan_get_cal_data(wlan_cal_data_t *cal_data)
12106 {
12107     if (cal_data == NULL)
12108     {
12109         return -WM_E_INVAL;
12110     }
12111 
12112     return wifi_get_cal_data(cal_data);
12113 }
12114 
12115 #if CONFIG_AUTO_RECONNECT
wlan_auto_reconnect_enable(wlan_auto_reconnect_config_t auto_reconnect_config)12116 int wlan_auto_reconnect_enable(wlan_auto_reconnect_config_t auto_reconnect_config)
12117 {
12118     if (is_sta_connected())
12119     {
12120         wlcm_e("Can not enable auto reconnect in connected state");
12121         return -WM_E_INVAL;
12122     }
12123 
12124     return wifi_auto_reconnect_enable(auto_reconnect_config);
12125 }
12126 
wlan_auto_reconnect_disable(void)12127 int wlan_auto_reconnect_disable(void)
12128 {
12129     return wifi_auto_reconnect_disable();
12130 }
12131 
wlan_get_auto_reconnect_config(wlan_auto_reconnect_config_t * auto_reconnect_config)12132 int wlan_get_auto_reconnect_config(wlan_auto_reconnect_config_t *auto_reconnect_config)
12133 {
12134     if (auto_reconnect_config == NULL)
12135     {
12136         return -WM_E_INVAL;
12137     }
12138 
12139     return wifi_get_auto_reconnect_config(auto_reconnect_config);
12140 }
12141 #endif
12142 
wlan_get_tsf(uint32_t * tsf_high,uint32_t * tsf_low)12143 int wlan_get_tsf(uint32_t *tsf_high, uint32_t *tsf_low)
12144 {
12145     return wifi_get_tsf(tsf_high, tsf_low);
12146 }
12147 
wlan_tcp_keep_alive(wlan_tcp_keep_alive_t * tcp_keep_alive)12148 int wlan_tcp_keep_alive(wlan_tcp_keep_alive_t *tcp_keep_alive)
12149 {
12150     int ret;
12151     unsigned int ipv4_addr;
12152 
12153     ret = wlan_get_ipv4_addr(&ipv4_addr);
12154     if (ret != WM_SUCCESS)
12155     {
12156         wlcm_e("Cannot get IP");
12157         return -WM_FAIL;
12158     }
12159 
12160     return wifi_tcp_keep_alive(tcp_keep_alive, wlan.sta_mac, ipv4_addr);
12161 }
12162 
12163 #if CONFIG_NAT_KEEP_ALIVE
wlan_nat_keep_alive(wlan_nat_keep_alive_t * nat_keep_alive)12164 int wlan_nat_keep_alive(wlan_nat_keep_alive_t *nat_keep_alive)
12165 {
12166     int ret;
12167     unsigned int ipv4_addr;
12168     t_u16 src_port = 4500;
12169 
12170     ret = wlan_get_ipv4_addr(&ipv4_addr);
12171     if (ret != WM_SUCCESS)
12172     {
12173         wlcm_e("Cannot get IP");
12174         return -WM_FAIL;
12175     }
12176 
12177     return wifi_nat_keep_alive(nat_keep_alive, wlan.sta_mac, ipv4_addr, src_port);
12178 }
12179 #endif
12180 
12181 #if CONFIG_CLOUD_KEEP_ALIVE
12182 /* Here the length of ip_packet for test is 40 */
12183 static t_u16 pkt_len_default = 40;
12184 /* ip packet content */
12185 static t_u8 packet_default[40] = {0x45, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x00, 0xff, 0x06, 0x00, 0x00, 0xc0, 0xa8,
12186                                   0x00, 0x7c, 0xc0, 0xa8, 0x00, 0x8a, 0xc0, 0x03, 0x22, 0xb7, 0xb0, 0xb6, 0x60, 0x9f,
12187                                   0x42, 0xdd, 0x9e, 0x1e, 0x50, 0x18, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00};
12188 
wlan_save_cloud_keep_alive_params(wlan_cloud_keep_alive_t * cloud_keep_alive,t_u16 src_port,t_u16 dst_port,t_u32 seq_number,t_u32 ack_number,t_u8 enable)12189 int wlan_save_cloud_keep_alive_params(wlan_cloud_keep_alive_t *cloud_keep_alive,
12190                                       t_u16 src_port,
12191                                       t_u16 dst_port,
12192                                       t_u32 seq_number,
12193                                       t_u32 ack_number,
12194                                       t_u8 enable)
12195 {
12196     if (enable)
12197     {
12198         if (cloud_keep_alive == NULL)
12199         {
12200             return -WM_E_INVAL;
12201         }
12202 
12203         if (!is_sta_connected())
12204         {
12205             wlcm_e("Can not start cloud keep alive in disconnected state");
12206             return -WM_E_INVAL;
12207         }
12208 
12209         if (cloud_keep_alive->mkeep_alive_id >= MAX_KEEP_ALIVE_ID)
12210         {
12211             wlcm_e("Invalid keep alive id");
12212             return -WM_E_INVAL;
12213         }
12214 
12215         /* Get source mac address */
12216         uint8_t sta_mac[MLAN_MAC_ADDR_LENGTH];
12217         if (wlan_get_mac_address(sta_mac))
12218         {
12219             wlcm_e("Unable to retrieve MAC address\r\n");
12220         }
12221         (void)memcpy(cloud_keep_alive->src_mac, sta_mac, MLAN_MAC_ADDR_LENGTH);
12222 
12223         /* Get source ip */
12224         int ret = -WM_FAIL;
12225         ret     = wlan_get_ipv4_addr(&cloud_keep_alive->src_ip);
12226         if (ret != WM_SUCCESS)
12227         {
12228             wlcm_e("Cannot get IP");
12229         }
12230         (void)memcpy(&cloud_keep_alive_param[cloud_keep_alive->mkeep_alive_id], cloud_keep_alive, sizeof(wlan_cloud_keep_alive_t));
12231     }
12232     else
12233     {
12234         int i;
12235 
12236         for (i = 0; i < MAX_KEEP_ALIVE_ID; i++)
12237         {
12238             cloud_keep_alive = &cloud_keep_alive_param[i];
12239 
12240             if (cloud_keep_alive && cloud_keep_alive->enable && (ntohs(dst_port) == cloud_keep_alive->dst_port))
12241             {
12242                 /* Copy source port, destination port, sequece number, ack number, window size for keep alive.
12243                    IP header checksum and TCP pesudo header checksum are calculated by WLAN FW */
12244                 (void)memcpy(packet_default + 12, &cloud_keep_alive->src_ip, sizeof(cloud_keep_alive->src_ip));
12245                 (void)memcpy(packet_default + 16, &cloud_keep_alive->dst_ip, sizeof(cloud_keep_alive->dst_ip));
12246                 (void)memcpy(packet_default + 20, &src_port, sizeof(src_port));
12247                 (void)memcpy(packet_default + 22, &dst_port, sizeof(dst_port));
12248                 (void)memcpy(packet_default + 24, &seq_number, sizeof(seq_number));
12249                 (void)memcpy(packet_default + 28, &ack_number, sizeof(ack_number));
12250 
12251                 cloud_keep_alive->pkt_len = pkt_len_default;
12252                 (void)memcpy(cloud_keep_alive->packet, packet_default, cloud_keep_alive->pkt_len);
12253 
12254                 cloud_keep_alive->cached = MTRUE;
12255             }
12256         }
12257     }
12258 
12259     return WM_SUCCESS;
12260 }
12261 
wlan_cloud_keep_alive_enabled(t_u32 dst_ip,t_u16 dst_port)12262 int wlan_cloud_keep_alive_enabled(t_u32 dst_ip, t_u16 dst_port)
12263 {
12264     int i;
12265     wifi_cloud_keep_alive_t *cloud_keep_alive = NULL;
12266 
12267     for (i = 0; i < MAX_KEEP_ALIVE_ID; i++)
12268     {
12269         cloud_keep_alive = &cloud_keep_alive_param[i];
12270         if (cloud_keep_alive && (cloud_keep_alive->dst_ip == dst_ip) && (cloud_keep_alive->dst_port == dst_port))
12271         {
12272             return 1;
12273         }
12274     }
12275 
12276     return 0;
12277 }
12278 
wlan_start_cloud_keep_alive(void)12279 int wlan_start_cloud_keep_alive(void)
12280 {
12281     int i;
12282     wifi_cloud_keep_alive_t *cloud_keep_alive = NULL;
12283 
12284     for (i = 0; i < MAX_KEEP_ALIVE_ID; i++)
12285     {
12286         cloud_keep_alive = &cloud_keep_alive_param[i];
12287         if (cloud_keep_alive && cloud_keep_alive->cached && cloud_keep_alive->enable)
12288         {
12289             cloud_keep_alive->cached = MFALSE;
12290             wifi_cloud_keep_alive(cloud_keep_alive, MLAN_ACT_SET, NULL);
12291         }
12292     }
12293 
12294     return WM_SUCCESS;
12295 }
12296 
wlan_stop_cloud_keep_alive(wlan_cloud_keep_alive_t * cloud_keep_alive)12297 int wlan_stop_cloud_keep_alive(wlan_cloud_keep_alive_t *cloud_keep_alive)
12298 {
12299     t_u8 enable = MTRUE;
12300 
12301     if (cloud_keep_alive == NULL)
12302         return -WM_E_INVAL;
12303 
12304     wifi_cloud_keep_alive(cloud_keep_alive, MLAN_ACT_GET, &enable);
12305 
12306     if (enable == MFALSE)
12307     {
12308         wlcm_e("Cloud keep alive is already stoped");
12309         return 0;
12310     }
12311     return wifi_cloud_keep_alive(cloud_keep_alive, MLAN_ACT_SET, NULL);
12312 }
12313 #endif
12314 
wlan_get_beacon_period(void)12315 uint16_t wlan_get_beacon_period(void)
12316 {
12317     struct wlan_network* network = NULL;
12318 
12319     if (wlan.running && is_sta_associated())
12320     {
12321         network = &wlan.networks[wlan.cur_network_idx];
12322     }
12323 
12324     if (network == NULL)
12325     {
12326         wlcm_e("cannot get network info");
12327         return 0;
12328     }
12329 
12330     return network->beacon_period;
12331 }
12332 
12333 OSA_SEMAPHORE_HANDLE_DEFINE(wlan_dtim_sem);
12334 
12335 static uint8_t dtim_period;
12336 
pscan_cb(unsigned int count)12337 static int pscan_cb(unsigned int count)
12338 {
12339     struct wlan_scan_result res;
12340     unsigned int i;
12341     int err;
12342 
12343     dtim_period = 0;
12344 
12345     if (count == 0U)
12346     {
12347         (void)PRINTF("networks not found\r\n");
12348         (void)OSA_SemaphorePost((osa_semaphore_handle_t)wlan_dtim_sem);
12349         return 0;
12350     }
12351 
12352     for (i = 0; i < count; i++)
12353     {
12354         err = wlan_get_scan_result(i, &res);
12355         if (err != 0)
12356         {
12357             (void)PRINTF("Error: can't get scan res %d\r\n", i);
12358             continue;
12359         }
12360 
12361         dtim_period = res.dtim_period;
12362     }
12363 
12364     (void)OSA_SemaphorePost((osa_semaphore_handle_t)wlan_dtim_sem);
12365 
12366     return 0;
12367 }
12368 
wlan_get_dtim_period(void)12369 uint8_t wlan_get_dtim_period(void)
12370 {
12371     osa_status_t status;
12372 
12373     status = OSA_SemaphoreCreateBinary((osa_semaphore_handle_t)wlan_dtim_sem);
12374     if (status != KOSA_StatusSuccess)
12375     {
12376         return 0;
12377     }
12378 
12379     if (wlan_pscan(pscan_cb) != 0)
12380     {
12381         (void)PRINTF("Error: scan request failed\r\n");
12382         (void)OSA_SemaphorePost((osa_semaphore_handle_t)wlan_dtim_sem);
12383         (void)OSA_SemaphoreDestroy((osa_semaphore_handle_t)wlan_dtim_sem);
12384         return 0;
12385     }
12386 
12387     /* Wait till scan for DTIM is complete */
12388     /*TODO:This need to be handled in better way. */
12389     if (OSA_SemaphoreWait((osa_semaphore_handle_t)wlan_dtim_sem, 500) != KOSA_StatusSuccess)
12390     {
12391         wlcm_e("Do not call this API from wlan event handler\r\n");
12392         dtim_period = 0;
12393     }
12394     (void)OSA_SemaphoreDestroy((osa_semaphore_handle_t)wlan_dtim_sem);
12395 
12396     return dtim_period;
12397 }
12398 
wlan_get_data_rate(wlan_ds_rate * ds_rate,mlan_bss_type bss_type)12399 int wlan_get_data_rate(wlan_ds_rate *ds_rate, mlan_bss_type bss_type)
12400 {
12401     return wifi_get_data_rate(ds_rate, bss_type);
12402 }
12403 
wlan_set_pmfcfg(uint8_t mfpc,uint8_t mfpr)12404 static int wlan_set_pmfcfg(uint8_t mfpc, uint8_t mfpr)
12405 {
12406     if (!mfpc && mfpr)
12407     {
12408         return -WM_FAIL;
12409     }
12410 
12411     return wifi_set_pmfcfg(mfpc, mfpr);
12412 }
12413 
wlan_get_pmfcfg(uint8_t * mfpc,uint8_t * mfpr)12414 int wlan_get_pmfcfg(uint8_t *mfpc, uint8_t *mfpr)
12415 {
12416     return wifi_get_pmfcfg(mfpc, mfpr);
12417 }
12418 
wlan_uap_get_pmfcfg(uint8_t * mfpc,uint8_t * mfpr)12419 int wlan_uap_get_pmfcfg(uint8_t *mfpc, uint8_t *mfpr)
12420 {
12421 #if UAP_SUPPORT
12422     if (is_uap_started())
12423     {
12424         return wifi_uap_get_pmfcfg(mfpc, mfpr);
12425     }
12426 #endif
12427     return -WM_FAIL;
12428 }
12429 
12430 #if CONFIG_TBTT_OFFSET
wlan_get_tbtt_offset(wlan_tbtt_offset_t * tbtt_offset)12431 int wlan_get_tbtt_offset(wlan_tbtt_offset_t *tbtt_offset)
12432 {
12433     return wifi_get_tbtt_offset(tbtt_offset);
12434 }
12435 #endif
12436 
wlan_set_packet_filters(wlan_flt_cfg_t * flt_cfg)12437 int wlan_set_packet_filters(wlan_flt_cfg_t *flt_cfg)
12438 {
12439     return wifi_set_packet_filters(flt_cfg);
12440 }
12441 
wlan_set_auto_arp(void)12442 int wlan_set_auto_arp(void)
12443 {
12444     int ret;
12445     unsigned int ipv4_addr;
12446     wlan_flt_cfg_t flt_cfg;
12447 
12448     ret = wlan_get_ipv4_addr(&ipv4_addr);
12449     if (ret != WM_SUCCESS)
12450     {
12451         wlcm_e("Cannot get IP");
12452         return -WM_FAIL;
12453     }
12454 
12455     (void)memset(&flt_cfg, 0, sizeof(wlan_flt_cfg_t));
12456 
12457     flt_cfg.criteria = CRITERIA_BROADCAST;
12458     flt_cfg.nentries = 1;
12459 
12460     flt_cfg.mef_entry[0].mode   = MEF_MODE_HOST_SLEEP; // MBIT(0);
12461     flt_cfg.mef_entry[0].action = MEF_AUTO_ARP;
12462 
12463     flt_cfg.mef_entry[0].filter_num = 3;
12464 
12465     flt_cfg.mef_entry[0].filter_item[0].type         = TYPE_BYTE_EQ;
12466     flt_cfg.mef_entry[0].filter_item[0].repeat       = 6;
12467     flt_cfg.mef_entry[0].filter_item[0].offset       = 0;
12468     flt_cfg.mef_entry[0].filter_item[0].num_byte_seq = 1;
12469     flt_cfg.mef_entry[0].filter_item[0].byte_seq[0]  = 0xff;
12470     flt_cfg.mef_entry[0].rpn[1]                      = RPN_TYPE_AND;
12471 
12472     flt_cfg.mef_entry[0].filter_item[1].type         = TYPE_BYTE_EQ;
12473     flt_cfg.mef_entry[0].filter_item[1].repeat       = 1;
12474     flt_cfg.mef_entry[0].filter_item[1].offset       = 20;
12475     flt_cfg.mef_entry[0].filter_item[1].num_byte_seq = 2;
12476     (void)memcpy((void *)flt_cfg.mef_entry[0].filter_item[1].byte_seq, (const void *)"\x08\x06", 2);
12477     flt_cfg.mef_entry[0].rpn[2] = RPN_TYPE_AND;
12478 
12479     flt_cfg.mef_entry[0].filter_item[2].type         = TYPE_BYTE_EQ;
12480     flt_cfg.mef_entry[0].filter_item[2].repeat       = 1;
12481     flt_cfg.mef_entry[0].filter_item[2].offset       = 46;
12482     flt_cfg.mef_entry[0].filter_item[2].num_byte_seq = 4;
12483     (void)memcpy((void *)flt_cfg.mef_entry[0].filter_item[2].byte_seq, (const void *)&ipv4_addr, 4);
12484 
12485     return wifi_set_packet_filters(&flt_cfg);
12486 }
12487 
12488 #if !CONFIG_WPA_SUPP
is_broadcast_ether_addr(const t_u8 * addr)12489 static inline bool is_broadcast_ether_addr(const t_u8 *addr)
12490 {
12491     return (*(const t_u16 *)(addr + 0) & *(const t_u16 *)(addr + 2) & *(const t_u16 *)(addr + 4)) == 0xffff;
12492 }
12493 #endif
12494 
12495 /**
12496  * @brief is_wowlan_pattern_supported
12497  *
12498  * @param priv                 A pointer to moal_private
12499  * @param pat                 A pointer to wowlan pattern
12500  * @param byte_seq       A pointer to byte_seq
12501  *
12502  * @return                      1 -- support, 0 -- not support
12503  */
is_wowlan_pattern_supported(wifi_wowlan_pattern_t * pat,t_u8 * byte_seq)12504 static t_bool is_wowlan_pattern_supported(wifi_wowlan_pattern_t *pat, t_u8 *byte_seq)
12505 {
12506     int j, k, valid_byte_cnt = 0;
12507     t_bool dont_care_byte = false;
12508 
12509     for (j = 0; j < DIV_ROUND_UP(pat->pattern_len, 8); j++)
12510     {
12511         for (k = 0; k < 8; k++)
12512         {
12513             if (pat->mask[j] & 1 << k)
12514             {
12515                 (void)memcpy((void *)(byte_seq + valid_byte_cnt), (const void *)&pat->pattern[j * 8 + k], 1);
12516                 valid_byte_cnt++;
12517                 if (dont_care_byte)
12518                     return false;
12519             }
12520             else
12521             {
12522                 if (valid_byte_cnt)
12523                     dont_care_byte = true;
12524             }
12525 
12526             if (valid_byte_cnt > MAX_NUM_BYTE_SEQ)
12527                 return false;
12528         }
12529     }
12530 
12531     byte_seq[MAX_NUM_BYTE_SEQ] = valid_byte_cnt;
12532 
12533     return true;
12534 }
12535 
wlan_wowlan_cfg_ptn_match(wlan_wowlan_ptn_cfg_t * ptn_cfg)12536 int wlan_wowlan_cfg_ptn_match(wlan_wowlan_ptn_cfg_t *ptn_cfg)
12537 {
12538     wlan_flt_cfg_t flt_cfg;
12539     wifi_mef_entry_t *mef_entry;
12540     t_u8 byte_seq[MAX_NUM_BYTE_SEQ + 1];
12541     const t_u8 ipv4_mc_mac[] = {0x33, 0x33};
12542     const t_u8 ipv6_mc_mac[] = {0x01, 0x00, 0x5e};
12543     int filt_num = 0, i = 0, ret = 0;
12544     t_bool first_pat = true;
12545     memset(&flt_cfg, 0, sizeof(flt_cfg));
12546     //  mef_cfg.mef_act_type = MEF_ACT_WOWLAN;
12547     mef_entry = &flt_cfg.mef_entry[0];
12548 
12549     mef_entry->mode   = MEF_MODE_HOST_SLEEP;
12550     mef_entry->action = MEF_ACTION_ALLOW_AND_WAKEUP_HOST;
12551 
12552     for (i = 0; i < ptn_cfg->n_patterns; i++)
12553     {
12554         memset(byte_seq, 0, sizeof(byte_seq));
12555         if (!is_wowlan_pattern_supported(&ptn_cfg->patterns[i], byte_seq))
12556         {
12557             PRINTM(MERROR, "Pattern not supported\n");
12558             ret = -EOPNOTSUPP;
12559             return ret;
12560         }
12561         if (!ptn_cfg->patterns[i].pkt_offset)
12562         {
12563             if (!(byte_seq[0] & 0x01) && (byte_seq[MAX_NUM_BYTE_SEQ] == 1))
12564             {
12565                 flt_cfg.criteria |= CRITERIA_UNICAST;
12566                 continue;
12567             }
12568             else if (is_broadcast_ether_addr(byte_seq))
12569             {
12570                 flt_cfg.criteria |= CRITERIA_BROADCAST;
12571                 continue;
12572             }
12573             else if ((!memcmp(byte_seq, ipv4_mc_mac, 2) && (byte_seq[MAX_NUM_BYTE_SEQ] == 2)) ||
12574                      (!memcmp(byte_seq, ipv6_mc_mac, 3) && (byte_seq[MAX_NUM_BYTE_SEQ] == 3)))
12575             {
12576                 flt_cfg.criteria |= CRITERIA_MULTICAST;
12577                 continue;
12578             }
12579         }
12580 
12581         //    mef_entry->filter_item[filt_num].fill_flag =
12582         //          (FILLING_TYPE | FILLING_REPEAT | FILLING_BYTE_SEQ |
12583         //         FILLING_OFFSET);
12584         mef_entry->filter_item[filt_num].repeat = 1;
12585         mef_entry->filter_item[filt_num].offset = ptn_cfg->patterns[i].pkt_offset;
12586         (void)memcpy((void *)mef_entry->filter_item[filt_num].byte_seq, (const void *)byte_seq, MAX_NUM_BYTE_SEQ);
12587         mef_entry->filter_item[filt_num].num_byte_seq = byte_seq[MAX_NUM_BYTE_SEQ];
12588         mef_entry->filter_item[filt_num].type         = TYPE_BYTE_EQ;
12589         if (first_pat)
12590             first_pat = false;
12591         else
12592             mef_entry->rpn[filt_num] = RPN_TYPE_OR;
12593 
12594         filt_num++;
12595     }
12596     if (ptn_cfg->enable)
12597     {
12598         //   (void)memset(&flt_cfg, 0, sizeof(wlan_flt_cfg_t));
12599 
12600         flt_cfg.criteria = CRITERIA_UNICAST | CRITERIA_BROADCAST | CRITERIA_MULTICAST;
12601         flt_cfg.nentries = 1;
12602 
12603         flt_cfg.mef_entry[0].mode   = MEF_MODE_HOST_SLEEP;
12604         flt_cfg.mef_entry[0].action = MEF_ACTION_ALLOW_AND_WAKEUP_HOST;
12605 
12606         flt_cfg.mef_entry[0].filter_num = 2;
12607 
12608         flt_cfg.mef_entry[0].filter_item[filt_num].type         = TYPE_BYTE_EQ;
12609         flt_cfg.mef_entry[0].filter_item[filt_num].repeat       = 16;
12610         flt_cfg.mef_entry[0].filter_item[filt_num].offset       = 56;
12611         flt_cfg.mef_entry[0].filter_item[filt_num].num_byte_seq = MLAN_MAC_ADDR_LENGTH;
12612         (void)memcpy((void *)flt_cfg.mef_entry[0].filter_item[filt_num].byte_seq, (const void *)wlan.sta_mac,
12613                      MLAN_MAC_ADDR_LENGTH);
12614         if (filt_num)
12615             flt_cfg.mef_entry[0].rpn[filt_num] = RPN_TYPE_OR;
12616         filt_num++;
12617         // flt_cfg.mef_entry.filter_item[1].fill_flag	  = (FILLING_TYPE | FILLING_REPEAT | FILLING_BYTE_SEQ |
12618         // FILLING_OFFSET);
12619         flt_cfg.mef_entry[0].filter_item[filt_num].type         = TYPE_BYTE_EQ;
12620         flt_cfg.mef_entry[0].filter_item[filt_num].repeat       = 16;
12621         flt_cfg.mef_entry[0].filter_item[filt_num].offset       = 28;
12622         flt_cfg.mef_entry[0].filter_item[filt_num].num_byte_seq = MLAN_MAC_ADDR_LENGTH;
12623         (void)memcpy((void *)flt_cfg.mef_entry[0].filter_item[filt_num].byte_seq, (const void *)wlan.sta_mac,
12624                      MLAN_MAC_ADDR_LENGTH);
12625         if (filt_num)
12626             flt_cfg.mef_entry[0].rpn[filt_num] = RPN_TYPE_OR;
12627         filt_num++;
12628     }
12629     flt_cfg.mef_entry[0].filter_num = filt_num;
12630     return wifi_set_packet_filters(&flt_cfg);
12631 }
12632 
12633 #if CONFIG_AUTO_PING
wlan_set_auto_ping(void)12634 int wlan_set_auto_ping(void)
12635 {
12636     wlan_flt_cfg_t flt_cfg;
12637 
12638     (void)memset(&flt_cfg, 0, sizeof(wlan_flt_cfg_t));
12639 
12640     flt_cfg.criteria = (MBIT(0) | MBIT(1));
12641     flt_cfg.nentries = 1;
12642 
12643     flt_cfg.mef_entry[0].mode   = MBIT(0);
12644     flt_cfg.mef_entry[0].action = 0x20;
12645 
12646     flt_cfg.mef_entry[0].filter_num = 2;
12647 
12648     flt_cfg.mef_entry[0].filter_item[0].type         = TYPE_BYTE_EQ;
12649     flt_cfg.mef_entry[0].filter_item[0].repeat       = 1;
12650     flt_cfg.mef_entry[0].filter_item[0].offset       = IPV4_PKT_OFFSET;
12651     flt_cfg.mef_entry[0].filter_item[0].num_byte_seq = 2;
12652     (void)memcpy((void *)flt_cfg.mef_entry[0].filter_item[0].byte_seq, (const void *)"\x08\x00", 2);
12653     flt_cfg.mef_entry[0].rpn[1] = RPN_TYPE_AND;
12654 
12655     flt_cfg.mef_entry[0].filter_item[1].type      = TYPE_DNUM_EQ;
12656     flt_cfg.mef_entry[0].filter_item[1].pattern   = ICMP_OF_IP_PROTOCOL;
12657     flt_cfg.mef_entry[0].filter_item[1].offset    = IP_PROTOCOL_OFFSET;
12658     flt_cfg.mef_entry[0].filter_item[1].num_bytes = 1;
12659 
12660     return wifi_set_packet_filters(&flt_cfg);
12661 }
12662 #endif /* CONFIG_AUTO_PING */
12663 
wlan_set_ipv6_ns_offload(void)12664 int wlan_set_ipv6_ns_offload(void)
12665 {
12666     wlan_flt_cfg_t flt_cfg;
12667 
12668     (void)memset(&flt_cfg, 0, sizeof(wlan_flt_cfg_t));
12669 
12670     flt_cfg.criteria = (MBIT(1) | MBIT(3));
12671     flt_cfg.nentries = 1;
12672 
12673     flt_cfg.mef_entry[0].mode   = MBIT(0);
12674     flt_cfg.mef_entry[0].action = 0x40;
12675 
12676     flt_cfg.mef_entry[0].filter_num = 2;
12677 
12678     flt_cfg.mef_entry[0].filter_item[0].type         = TYPE_BYTE_EQ;
12679     flt_cfg.mef_entry[0].filter_item[0].repeat       = 1;
12680     flt_cfg.mef_entry[0].filter_item[0].offset       = 20;
12681     flt_cfg.mef_entry[0].filter_item[0].num_byte_seq = 2;
12682     (void)memcpy((void *)flt_cfg.mef_entry[0].filter_item[0].byte_seq, (const void *)"\x86\xdd", 2);
12683     flt_cfg.mef_entry[0].rpn[1] = RPN_TYPE_AND;
12684 
12685     flt_cfg.mef_entry[0].filter_item[1].type         = TYPE_BYTE_EQ;
12686     flt_cfg.mef_entry[0].filter_item[1].repeat       = 1;
12687     flt_cfg.mef_entry[0].filter_item[1].offset       = 62;
12688     flt_cfg.mef_entry[0].filter_item[1].num_byte_seq = 1;
12689     (void)memcpy((void *)flt_cfg.mef_entry[0].filter_item[1].byte_seq, (const void *)"\x87", 1);
12690 
12691     return wifi_set_packet_filters(&flt_cfg);
12692 }
12693 
wlan_get_current_bssid(uint8_t * bssid)12694 int wlan_get_current_bssid(uint8_t *bssid)
12695 {
12696     struct wlan_network* network = NULL;
12697 
12698     if (wlan.running && is_sta_associated())
12699     {
12700         network = &wlan.networks[wlan.cur_network_idx];
12701     }
12702 
12703     if (network == NULL)
12704     {
12705         wlcm_e("cannot get network info");
12706         return -WM_FAIL;
12707     }
12708     if (bssid != NULL)
12709     {
12710         (void)memcpy((void *)bssid, (const void *)network->bssid, IEEEtypes_ADDRESS_SIZE);
12711         return WM_SUCCESS;
12712     }
12713 
12714     return -WM_FAIL;
12715 }
12716 
wlan_get_current_channel(void)12717 uint8_t wlan_get_current_channel(void)
12718 {
12719     struct wlan_network* network = NULL;
12720 
12721     if (wlan.running && is_sta_associated())
12722     {
12723         network = &wlan.networks[wlan.cur_network_idx];
12724     }
12725 
12726     if (network == NULL)
12727     {
12728         wlcm_e("cannot get network info");
12729         return 0;
12730     }
12731 
12732     return (uint8_t)network->channel;
12733 }
12734 
wlan_sta_ampdu_tx_enable(void)12735 void wlan_sta_ampdu_tx_enable(void)
12736 {
12737     wifi_sta_ampdu_tx_enable();
12738 }
12739 
wlan_sta_ampdu_tx_disable(void)12740 void wlan_sta_ampdu_tx_disable(void)
12741 {
12742     wifi_sta_ampdu_tx_disable();
12743 }
12744 
wlan_sta_ampdu_rx_enable(void)12745 void wlan_sta_ampdu_rx_enable(void)
12746 {
12747     wifi_sta_ampdu_rx_enable();
12748 }
12749 
wlan_sta_ampdu_rx_disable(void)12750 void wlan_sta_ampdu_rx_disable(void)
12751 {
12752     wifi_sta_ampdu_rx_disable();
12753 }
12754 
wlan_uap_ampdu_tx_enable(void)12755 void wlan_uap_ampdu_tx_enable(void)
12756 {
12757     wifi_uap_ampdu_tx_enable();
12758 }
12759 
wlan_uap_ampdu_tx_disable(void)12760 void wlan_uap_ampdu_tx_disable(void)
12761 {
12762     wifi_uap_ampdu_tx_disable();
12763 }
12764 
wlan_uap_ampdu_rx_enable(void)12765 void wlan_uap_ampdu_rx_enable(void)
12766 {
12767     wifi_uap_ampdu_rx_enable();
12768 }
12769 
wlan_uap_ampdu_rx_disable(void)12770 void wlan_uap_ampdu_rx_disable(void)
12771 {
12772     wifi_uap_ampdu_rx_disable();
12773 }
12774 
12775 #if CONFIG_WIFI_AMPDU_CTRL
wlan_sta_ampdu_tx_enable_per_tid(t_u8 tid)12776 void wlan_sta_ampdu_tx_enable_per_tid(t_u8 tid)
12777 {
12778     wifi_sta_ampdu_tx_enable_per_tid(tid);
12779 }
12780 
wlan_sta_ampdu_rx_enable_per_tid(t_u8 tid)12781 void wlan_sta_ampdu_rx_enable_per_tid(t_u8 tid)
12782 {
12783     wifi_sta_ampdu_rx_enable_per_tid(tid);
12784 }
12785 
wlan_uap_ampdu_tx_enable_per_tid(t_u8 tid)12786 void wlan_uap_ampdu_tx_enable_per_tid(t_u8 tid)
12787 {
12788     wifi_uap_ampdu_tx_enable_per_tid(tid);
12789 }
12790 
wlan_uap_ampdu_rx_enable_per_tid(t_u8 tid)12791 void wlan_uap_ampdu_rx_enable_per_tid(t_u8 tid)
12792 {
12793     wifi_uap_ampdu_rx_enable_per_tid(tid);
12794 }
12795 #endif
12796 
wlan_uap_set_scan_chan_list(wifi_scan_chan_list_t scan_chan_list)12797 void wlan_uap_set_scan_chan_list(wifi_scan_chan_list_t scan_chan_list)
12798 {
12799 #if UAP_SUPPORT
12800     wlan_uap_scan_chan_list_set = true;
12801     (void)memcpy((void *)&wlan.scan_chan_list, (const void *)&scan_chan_list, sizeof(wifi_scan_chan_list_t));
12802 #endif
12803 }
12804 
wlan_uap_set_beacon_period(const uint16_t beacon_period)12805 void wlan_uap_set_beacon_period(const uint16_t beacon_period)
12806 {
12807 #if UAP_SUPPORT
12808     wifi_uap_set_beacon_period(beacon_period);
12809 #endif
12810 }
12811 
wlan_uap_set_bandwidth(const uint8_t bandwidth)12812 int wlan_uap_set_bandwidth(const uint8_t bandwidth)
12813 {
12814 #if UAP_SUPPORT
12815 #if defined(RW610) || defined(SD8801)
12816     return WM_SUCCESS;
12817 #else
12818     return wifi_uap_set_bandwidth(bandwidth);
12819 #endif
12820 #else
12821     return -WM_E_NODEV;
12822 #endif
12823 }
12824 
wlan_uap_get_bandwidth(uint8_t * bandwidth)12825 int wlan_uap_get_bandwidth(uint8_t *bandwidth)
12826 {
12827 #if UAP_SUPPORT
12828     *bandwidth = wifi_uap_get_bandwidth();
12829     if (*bandwidth == BANDWIDTH_20MHZ || *bandwidth == BANDWIDTH_40MHZ
12830 #if CONFIG_11AC
12831         || *bandwidth == BANDWIDTH_80MHZ
12832 #endif
12833     )
12834     {
12835         return WM_SUCCESS;
12836     }
12837     else
12838     {
12839         return -WM_FAIL;
12840     }
12841 #else
12842     return -WM_E_NODEV;
12843 #endif
12844 }
12845 
wlan_uap_set_hidden_ssid(const t_u8 hidden_ssid)12846 int wlan_uap_set_hidden_ssid(const t_u8 hidden_ssid)
12847 {
12848 #if UAP_SUPPORT
12849     if (hidden_ssid != 0 && hidden_ssid != 1 && hidden_ssid != 2)
12850     {
12851         return -WM_FAIL;
12852     }
12853 
12854     if (is_uap_started())
12855     {
12856         (void)PRINTF("Pls set hidden_ssid before start uAP.\r\n");
12857         return -WM_FAIL;
12858     }
12859 
12860     wifi_uap_set_hidden_ssid(hidden_ssid);
12861 
12862     return WM_SUCCESS;
12863 #else
12864     return -WM_E_NODEV;
12865 #endif
12866 }
12867 
wlan_uap_ctrl_deauth(const bool enable)12868 void wlan_uap_ctrl_deauth(const bool enable)
12869 {
12870 #if UAP_SUPPORT
12871     (void)wifi_uap_ctrl_deauth(enable);
12872 #endif
12873 }
12874 
wlan_uap_set_ecsa(void)12875 void wlan_uap_set_ecsa(void)
12876 {
12877 #if UAP_SUPPORT
12878     wifi_uap_set_ecsa();
12879 #endif
12880 }
12881 
wlan_uap_set_htcapinfo(const uint16_t ht_cap_info)12882 void wlan_uap_set_htcapinfo(const uint16_t ht_cap_info)
12883 {
12884 #if UAP_SUPPORT
12885     wifi_uap_set_htcapinfo(ht_cap_info);
12886 #endif
12887 }
12888 
wlan_uap_set_httxcfg(unsigned short httxcfg)12889 void wlan_uap_set_httxcfg(unsigned short httxcfg)
12890 {
12891 #if UAP_SUPPORT
12892     wifi_uap_set_httxcfg(httxcfg);
12893 #endif
12894 }
12895 
12896 #if CONFIG_WIFI_RTS_THRESHOLD
wlan_set_rts(int rts)12897 int wlan_set_rts(int rts)
12898 {
12899     return wifi_set_rts(rts, MLAN_BSS_TYPE_STA);
12900 }
12901 
wlan_set_uap_rts(int rts)12902 int wlan_set_uap_rts(int rts)
12903 {
12904     return wifi_set_rts(rts, MLAN_BSS_TYPE_UAP);
12905 }
12906 #endif
12907 
12908 #if CONFIG_WIFI_FRAG_THRESHOLD
wlan_set_frag(int frag)12909 int wlan_set_frag(int frag)
12910 {
12911     return wifi_set_frag(frag, MLAN_BSS_TYPE_STA);
12912 }
12913 
wlan_set_uap_frag(int frag)12914 int wlan_set_uap_frag(int frag)
12915 {
12916     return wifi_set_frag(frag, MLAN_BSS_TYPE_UAP);
12917 }
12918 #endif
12919 
12920 #if CONFIG_11K
_wlan_rrm_scan_cb(unsigned int count)12921 int _wlan_rrm_scan_cb(unsigned int count)
12922 {
12923     t_u16 i;
12924     t_u8 *rep_buf = NULL;
12925     t_u8 *buf_pos = NULL;
12926     /* The sufficient size is the length including reporting frame body */
12927     t_u16 suffi_len           = 250U;
12928     t_u32 pos_last_indication = 0U;
12929     bool match_ap_found       = false;
12930     int meas_report_len       = 0;
12931 
12932     /* process scan result */
12933 #if !CONFIG_MEM_POOLS
12934     rep_buf = (t_u8 *)OSA_MemoryAllocate(BEACON_REPORT_BUF_SIZE);
12935 #else
12936     rep_buf = (t_u8 *)OSA_MemoryPoolAllocate(buf_1536_MemoryPool);
12937 #endif
12938 
12939     if (rep_buf == NULL)
12940     {
12941         PRINTM(MERROR, "Cannot allocate memory for report buffer");
12942         return -1;
12943     }
12944 
12945     (void)memset(rep_buf, 0, BEACON_REPORT_BUF_SIZE);
12946     buf_pos = rep_buf;
12947     for (i = 0; i < count; i++)
12948     {
12949         if (wlan_rrm_matched_ap_found(&wlan.rrm_scan_cb_param.rep_data, &mlan_adap->pscan_table[i]))
12950         {
12951             wlan_add_rm_beacon_report(&wlan.rrm_scan_cb_param.rep_data, &mlan_adap->pscan_table[i], &buf_pos,
12952                                       BEACON_REPORT_BUF_SIZE - (buf_pos - rep_buf), &pos_last_indication);
12953         }
12954 
12955         /* If current rep_buf is not enough and still have AP not added, just send the report */
12956         if ((buf_pos + suffi_len - rep_buf > BEACON_REPORT_BUF_SIZE) && (i < count - 1U) &&
12957             wlan_rrm_matched_ap_found(&wlan.rrm_scan_cb_param.rep_data, &mlan_adap->pscan_table[i + 1U]))
12958         {
12959             match_ap_found  = 1;
12960             meas_report_len = buf_pos - rep_buf;
12961             /* send beacon report, not the last one */
12962             wlan_send_mgmt_rm_beacon_report(wlan.rrm_scan_cb_param.dialog_tok, wlan.sta_mac,
12963                                             wlan.rrm_scan_cb_param.dst_addr, rep_buf, (t_u32)meas_report_len,
12964                                             (bool)wlan.rrm_scan_cb_param.protect);
12965             /* Prepare for the next beacon report */
12966             (void)memset(rep_buf, 0, BEACON_REPORT_BUF_SIZE);
12967             buf_pos = rep_buf;
12968         }
12969 
12970         /* Last AP in scan table, and matched AP found */
12971         if ((i == count - 1U) && (buf_pos > rep_buf))
12972         {
12973             match_ap_found = 1;
12974             /* Update last indication, the last one */
12975             if (wlan.rrm_scan_cb_param.rep_data.last_ind != 0U && pos_last_indication != 0U)
12976             {
12977                 *(char *)pos_last_indication = (char)1U;
12978             }
12979             meas_report_len = buf_pos - rep_buf;
12980             /* send beacon report, the last one */
12981             wlan_send_mgmt_rm_beacon_report(wlan.rrm_scan_cb_param.dialog_tok, wlan.sta_mac,
12982                                             wlan.rrm_scan_cb_param.dst_addr, rep_buf, (t_u32)meas_report_len,
12983                                             (bool)wlan.rrm_scan_cb_param.protect);
12984         }
12985     }
12986 
12987     /* If no matched AP found, no beacon report detail */
12988     if (!match_ap_found)
12989     {
12990         *buf_pos++ = (t_u8)MEASURE_REPORT;
12991         /* Tag length */
12992         *buf_pos++      = 3;
12993         *buf_pos++      = wlan.rrm_scan_cb_param.rep_data.token;
12994         *buf_pos++      = WLAN_RRM_REPORT_MODE_ACCEPT;
12995         *buf_pos++      = WLAN_RRM_MEASURE_TYPE_BEACON;
12996         meas_report_len = buf_pos - rep_buf;
12997         /* send beacon report */
12998         wlan_send_mgmt_rm_beacon_report(wlan.rrm_scan_cb_param.dialog_tok, wlan.sta_mac,
12999                                         wlan.rrm_scan_cb_param.dst_addr, rep_buf, (t_u32)meas_report_len,
13000                                         (bool)wlan.rrm_scan_cb_param.protect);
13001     }
13002 #if !CONFIG_MEM_POOLS
13003     OSA_MemoryFree(rep_buf);
13004 #else
13005     OSA_MemoryPoolFree(buf_1536_MemoryPool, rep_buf);
13006 #endif
13007 
13008     return 0;
13009 }
13010 
wlan_rrm_request_scan(wlan_scan_params_v2_t * wlan_scan_param,wlan_rrm_scan_cb_param * scan_cb_param)13011 void wlan_rrm_request_scan(wlan_scan_params_v2_t *wlan_scan_param, wlan_rrm_scan_cb_param *scan_cb_param)
13012 {
13013     char ssid[(MLAN_MAX_SSID_LENGTH + 1) * MRVDRV_MAX_SSID_LIST_LENGTH]  = {0};
13014     uint8_t ssid_num = 0, ssid_off = 0;
13015     memcpy(ssid+ssid_off, wlan_scan_param->ssid[0], strlen(wlan_scan_param->ssid[0]));
13016     ssid_off += strlen(wlan_scan_param->ssid[0]);
13017     ssid[ssid_off] = '\0';
13018     ssid_off++;
13019     ssid_num++;
13020 #if CONFIG_COMBO_SCAN
13021     if (strlen(wlan_scan_param->ssid[1]))
13022     {
13023         memcpy(ssid+ssid_off, wlan_scan_param->ssid[1], strlen(wlan_scan_param->ssid[1]));
13024         ssid_off += strlen(wlan_scan_param->ssid[1]);
13025         ssid[ssid_off] = '\0';
13026         ssid_num++;
13027     }
13028 #endif
13029 
13030     if (wlan_scan_param == NULL || scan_cb_param == NULL)
13031     {
13032         wlcm_d("ignoring scan request with NULL scan or cb params");
13033         return;
13034     }
13035     if (!is_scanning_allowed())
13036     {
13037         wlcm_d("ignoring scan request in invalid state");
13038         return;
13039     }
13040 
13041     (void)memcpy(&wlan.rrm_scan_cb_param, scan_cb_param, sizeof(wlan_rrm_scan_cb_param));
13042 
13043 #if CONFIG_SCAN_CHANNEL_GAP
13044     if (is_uap_started() || is_sta_connected())
13045         wlan_scan_param->scan_chan_gap = scan_channel_gap;
13046     else
13047         wlan_scan_param->scan_chan_gap = (t_u16)0U;
13048 #endif
13049 
13050     int ret = wifi_send_scan_cmd((t_u8)BSS_ANY, wlan_scan_param->bssid,
13051                                  ssid, ssid_num,
13052                                  wlan_scan_param->num_channels, wlan_scan_param->chan_list, wlan_scan_param->num_probes,
13053 #if CONFIG_SCAN_WITH_RSSIFILTER
13054                                  wlan_scan_param->rssi_threshold,
13055 #endif
13056 #if CONFIG_SCAN_CHANNEL_GAP
13057                                  wlan_scan_param->scan_chan_gap,
13058 #endif
13059                                  false, false);
13060     if (ret == WM_SUCCESS)
13061     {
13062         wlan.scan_cb       = (int (*)(unsigned int count))(wlan_scan_param->cb);
13063         wlan.sta_return_to = wlan.sta_state;
13064         wlan.sta_state     = CM_STA_SCANNING_USER;
13065     }
13066     else
13067     {
13068         wlcm_e("wifi send scan cmd failed");
13069     }
13070 }
13071 #endif
13072 
13073 #if CONFIG_11K_OFFLOAD
wlan_11k_cfg(int enable_11k)13074 int wlan_11k_cfg(int enable_11k)
13075 {
13076     return wifi_11k_cfg(enable_11k);
13077 }
13078 
wlan_11k_neighbor_req(void)13079 int wlan_11k_neighbor_req(void)
13080 {
13081     return wifi_11k_neighbor_req();
13082 }
13083 #endif
13084 
13085 #if CONFIG_SCAN_CHANNEL_GAP
wlan_set_scan_channel_gap(unsigned scan_chan_gap)13086 void wlan_set_scan_channel_gap(unsigned scan_chan_gap)
13087 {
13088     scan_channel_gap = (t_u16)scan_chan_gap;
13089     return;
13090 }
13091 #endif
13092 
13093 #if CONFIG_11K
wlan_host_11k_cfg(int enable_11k)13094 int wlan_host_11k_cfg(int enable_11k)
13095 {
13096 #if CONFIG_WPA_SUPP
13097     wlan.enable_11k = enable_11k;
13098     return WM_SUCCESS;
13099 #else
13100     int ret = -WM_FAIL;
13101     mlan_private *pmpriv = (mlan_private *)mlan_adap->priv[0];
13102 
13103     ret = wifi_host_11k_cfg(enable_11k);
13104     wlan_set_host_11k_status(pmpriv->enable_host_11k);
13105 
13106     return ret;
13107 #endif
13108 }
13109 
wlan_set_host_11k_status(int enable_11k)13110 int wlan_set_host_11k_status(int enable_11k)
13111 {
13112     if (enable_11k)
13113         wlan.enable_11k = true;
13114     else
13115         wlan.enable_11k = false;
13116 
13117     return 0;
13118 }
13119 
wlan_get_host_11k_status(void)13120 bool wlan_get_host_11k_status(void)
13121 {
13122     return wlan.enable_11k;
13123 }
13124 
wlan_host_11k_neighbor_req(const char * ssid)13125 int wlan_host_11k_neighbor_req(const char *ssid)
13126 {
13127     int ret = -WM_FAIL;
13128 #if CONFIG_WPA_SUPP
13129 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
13130     struct netif *netif = net_get_sta_interface();
13131 #endif
13132 #endif
13133 
13134     if (!is_sta_connected())
13135     {
13136         wlcm_d("Error: sta connection is required before sending neighbor report req");
13137         return -WM_FAIL;
13138     }
13139 
13140 #if CONFIG_WPA_SUPP
13141 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
13142     ret = wpa_supp_send_neighbor_rep(netif, (char *)ssid, 0, 0);
13143 #endif
13144 #else
13145     ret = wifi_host_11k_neighbor_req(ssid);
13146 #endif
13147     if (ret == WM_SUCCESS)
13148     {
13149         wlan.neighbor_req = true;
13150         (void)OSA_TimerActivate((osa_timer_handle_t)wlan.neighbor_req_timer);
13151     }
13152     return ret;
13153 }
13154 #endif
13155 
13156 #if CONFIG_11V
wlan_host_11v_bss_trans_query(t_u8 query_reason)13157 int wlan_host_11v_bss_trans_query(t_u8 query_reason)
13158 {
13159 #if CONFIG_WPA_SUPP
13160 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
13161     struct netif *netif = net_get_sta_interface();
13162 #endif
13163 #else
13164     int ret = -WM_FAIL;
13165 #endif
13166 
13167     if (!is_sta_connected())
13168     {
13169         wlcm_d("Error: sta connection is required before sending bss transition query");
13170         return -WM_FAIL;
13171     }
13172 
13173 #if CONFIG_WPA_SUPP
13174 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
13175     return wpa_supp_send_btm_query(netif, query_reason);
13176 #else
13177     return 0;
13178 #endif
13179 #else
13180     ret = wifi_host_11v_bss_trans_query(query_reason);
13181     if (ret == WM_SUCCESS)
13182     {
13183         wlan.neighbor_req = true;
13184         (void)OSA_TimerActivate((osa_timer_handle_t)wlan.neighbor_req_timer);
13185     }
13186     return ret;
13187 #endif
13188 }
13189 #endif
13190 
13191 #if !CONFIG_WPA_SUPP
13192 #if CONFIG_DRIVER_MBO
wlan_host_mbo_cfg(int enable_mbo)13193 int wlan_host_mbo_cfg(int enable_mbo)
13194 {
13195     return wifi_host_mbo_cfg(enable_mbo);
13196 }
13197 
wlan_mbo_peferch_cfg(t_u8 ch0,t_u8 pefer0,t_u8 ch1,t_u8 pefer1)13198 int wlan_mbo_peferch_cfg(t_u8 ch0, t_u8 pefer0, t_u8 ch1, t_u8 pefer1)
13199 {
13200     uint8_t ap_addr[IEEEtypes_ADDRESS_SIZE];
13201     if (is_sta_connected())
13202     {
13203         (void)wlan_get_current_bssid(ap_addr);
13204         return wifi_mbo_send_preferch_wnm(wlan.sta_mac, (t_u8 *)ap_addr, ch0, pefer0, ch1, pefer1);
13205     }
13206     else
13207     {
13208         return wifi_mbo_preferch_cfg(ch0, pefer0, ch1, pefer1);
13209     }
13210 }
13211 #endif
13212 #endif
13213 
13214 #if (CONFIG_11MC) || (CONFIG_11AZ)
wlan_ftm_start_stop(const t_u16 action,const t_u8 loop_cnt,const t_u8 * mac,const t_u8 channel)13215 int wlan_ftm_start_stop(const t_u16 action, const t_u8 loop_cnt, const t_u8 *mac, const t_u8 channel)
13216 {
13217 
13218 	return wifi_ftm_start_stop(action, loop_cnt, mac, channel);
13219 }
13220 
wlan_ftm_cfg(const t_u8 protocol,ranging_11az_cfg_t * ftm_ranging_cfg)13221 int wlan_ftm_cfg(const t_u8 protocol, ranging_11az_cfg_t *ftm_ranging_cfg)
13222 {
13223 	return wifi_ftm_cfg(protocol, ftm_ranging_cfg);
13224 }
13225 
wlan_ftm_11mc_cfg(ftm_11mc_nego_cfg_t * ftm_11mc_nego_cfg)13226 int wlan_ftm_11mc_cfg(ftm_11mc_nego_cfg_t *ftm_11mc_nego_cfg)
13227 {
13228 	return wifi_ftm_11mc_cfg(ftm_11mc_nego_cfg);
13229 }
13230 
wlan_ftm_location_cfg(location_cfg_info_t * ftm_location_cfg)13231 int wlan_ftm_location_cfg(location_cfg_info_t *ftm_location_cfg)
13232 {
13233 	return wifi_ftm_location_cfg(ftm_location_cfg);
13234 }
13235 
wlan_ftm_civic_cfg(location_civic_rep_t * ftm_civic_cfg)13236 int wlan_ftm_civic_cfg(location_civic_rep_t *ftm_civic_cfg)
13237 {
13238 	return wifi_ftm_civic_cfg(ftm_civic_cfg);
13239 }
13240 #endif
13241 
13242 #if CONFIG_WPA_SUPP
13243 #if (CONFIG_11AX && defined(CONFIG_MBO))
wlan_mbo_peferch_cfg(const char * non_pref_chan)13244 int wlan_mbo_peferch_cfg(const char *non_pref_chan)
13245 {
13246 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
13247     struct netif *netif = net_get_sta_interface();
13248 
13249     return wpa_supp_mbo_update_non_pref_chan(netif, non_pref_chan);
13250 #else
13251     return 0;
13252 #endif
13253 }
13254 
wlan_mbo_set_cell_capa(t_u8 cell_capa)13255 int wlan_mbo_set_cell_capa(t_u8 cell_capa)
13256 {
13257 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
13258     struct netif *netif = net_get_sta_interface();
13259 
13260     if (cell_capa != 1 && cell_capa != 2 && cell_capa != 3)
13261     {
13262         return -WM_E_PERM;
13263     }
13264 
13265     return wpa_supp_mbo_set_cell_capa(netif, cell_capa);
13266 #else
13267     return 0;
13268 #endif
13269 }
13270 
wlan_mbo_set_oce(t_u8 oce)13271 int wlan_mbo_set_oce(t_u8 oce)
13272 {
13273 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
13274     struct netif *netif = net_get_sta_interface();
13275 
13276     if (oce != 1 && oce != 2)
13277     {
13278         return -WM_E_PERM;
13279     }
13280 
13281     return wpa_supp_mbo_set_oce(netif, oce);
13282 #else
13283     return 0;
13284 #endif
13285 }
13286 #endif
13287 
wlan_set_okc(t_u8 okc)13288 int wlan_set_okc(t_u8 okc)
13289 {
13290 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
13291     struct netif *netif = net_get_sta_interface();
13292 
13293     if (okc != 0 && okc != 1)
13294     {
13295         return -WM_E_PERM;
13296     }
13297 
13298     return wpa_supp_set_okc(netif, okc);
13299 #else
13300     return 0;
13301 #endif
13302 }
13303 
wlan_pmksa_list(char * buf,size_t buflen)13304 int wlan_pmksa_list(char *buf, size_t buflen)
13305 {
13306 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
13307     struct netif *netif = net_get_sta_interface();
13308 
13309     return wpa_supp_pmksa_list(netif, buf, buflen);
13310 #else
13311     return 0;
13312 #endif
13313 }
13314 
wlan_pmksa_flush(void)13315 int wlan_pmksa_flush(void)
13316 {
13317     struct netif *netif = net_get_sta_interface();
13318 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
13319     return wpa_supp_pmksa_flush(netif);
13320 #else
13321     return supplicant_pmksa_flush(net_if_get_device((void *)netif));
13322 #endif
13323 }
13324 
wlan_set_scan_interval(int scan_int)13325 int wlan_set_scan_interval(int scan_int)
13326 {
13327 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
13328     struct netif *netif = net_get_sta_interface();
13329 
13330     return wpa_supp_set_scan_interval(netif, scan_int);
13331 #else
13332     return 0;
13333 #endif
13334 }
13335 #endif
13336 
13337 #if CONFIG_UAP_STA_MAC_ADDR_FILTER
wlan_set_sta_mac_filter(int filter_mode,int mac_count,unsigned char * mac_addr)13338 int wlan_set_sta_mac_filter(int filter_mode, int mac_count, unsigned char *mac_addr)
13339 {
13340 #if CONFIG_WPA_SUPP
13341     return wlan_host_set_sta_mac_filter(filter_mode, mac_count, mac_addr);
13342 #else
13343     return wifi_set_sta_mac_filter(filter_mode, mac_count, mac_addr);
13344 #endif
13345 }
13346 #endif
13347 
13348 #if CONFIG_WPA2_ENTP
wlan_enable_wpa2_enterprise_ap_only(void)13349 void wlan_enable_wpa2_enterprise_ap_only(void)
13350 {
13351     wlan.allow_wpa2_enterprise_ap_only = true;
13352     wifi_scan_enable_wpa2_enterprise_ap_only();
13353 }
13354 #endif
13355 
wlan_version_extended(void)13356 void wlan_version_extended(void)
13357 {
13358 #if SDK_DEBUGCONSOLE != DEBUGCONSOLE_DISABLE
13359     char *version_str;
13360 
13361     version_str = wlan_get_firmware_version_ext();
13362 
13363     (void)PRINTF("WLAN Driver Version   : %s\r\n", WLAN_DRV_VERSION);
13364     (void)PRINTF("WLAN Firmware Version : %s\r\n", version_str);
13365 #endif
13366 }
13367 
13368 #if CONFIG_WIFI_TX_PER_TRACK
wlan_set_tx_pert(struct wlan_tx_pert_info * tx_pert,mlan_bss_type bss_type)13369 void wlan_set_tx_pert(struct wlan_tx_pert_info *tx_pert, mlan_bss_type bss_type)
13370 {
13371     int ret = WM_SUCCESS;
13372 
13373     ret = wifi_set_tx_pert((void *)tx_pert, bss_type);
13374     if (ret != WM_SUCCESS)
13375         (void)PRINTF("Failed to set tx per tracking.\r\n");
13376     return;
13377 }
13378 #endif
13379 
13380 #if CONFIG_TX_RX_HISTOGRAM
wlan_set_txrx_histogram(struct wlan_txrx_histogram_info * txrx_histogram,t_u8 * data)13381 void wlan_set_txrx_histogram(struct wlan_txrx_histogram_info *txrx_histogram, t_u8 *data)
13382 {
13383     int ret = WM_SUCCESS;
13384 
13385     wifi_set_txrx_histogram((void *)txrx_histogram, data);
13386     if (ret != WM_SUCCESS)
13387         (void)PRINTF("Failed to set txrx histogram config.\r\n");
13388     return;
13389 }
13390 #endif
13391 
13392 #if CONFIG_ROAMING
wlan_set_roaming(const int enable,const uint8_t rssi_low_threshold)13393 int wlan_set_roaming(const int enable, const uint8_t rssi_low_threshold)
13394 {
13395 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
13396 #if CONFIG_WPA_SUPP
13397     struct netif *netif = net_get_sta_interface();
13398 #endif
13399 #endif
13400 
13401     wlan.roaming_enabled = enable;
13402 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
13403 #if CONFIG_WPA_SUPP
13404     wpa_supp_set_okc(netif, wlan.roaming_enabled == true ? 0 : 1);
13405 #endif
13406 #endif
13407     wlan.rssi_low_threshold = rssi_low_threshold;
13408 
13409     return wifi_config_roaming(enable, &wlan.rssi_low_threshold);
13410 }
13411 
wlan_get_roaming_status(void)13412 int wlan_get_roaming_status(void)
13413 {
13414     return wlan.roaming_enabled;
13415 }
13416 
wlan_subscribe_rssi_low_event(void)13417 void wlan_subscribe_rssi_low_event(void)
13418 {
13419     if(wlan.roaming_enabled)
13420     {
13421         wifi_set_rssi_low_threshold(&wlan.rssi_low_threshold);
13422     }
13423 }
13424 #endif
13425 
13426 #if CONFIG_WIFI_MEM_ACCESS
wlan_mem_access(uint16_t action,uint32_t addr,uint32_t * value)13427 int wlan_mem_access(uint16_t action, uint32_t addr, uint32_t *value)
13428 {
13429     return wifi_mem_access(action, addr, value);
13430 }
13431 #endif
13432 
13433 #if CONFIG_WIFI_BOOT_SLEEP
wlan_boot_sleep(uint16_t action,uint16_t * enable)13434 int wlan_boot_sleep(uint16_t action, uint16_t *enable)
13435 {
13436 
13437     if ((*enable != 0) && (*enable != 1))
13438     {
13439         return -WM_FAIL;
13440     }
13441 
13442     return wifi_boot_sleep(action, enable);
13443 }
13444 #endif
13445 
13446 #if CONFIG_RF_TEST_MODE
13447 
wlan_set_rf_test_mode(void)13448 int wlan_set_rf_test_mode(void)
13449 {
13450     wlan_ieeeps_off();
13451     wlan_deepsleepps_off();
13452     return wifi_set_rf_test_mode();
13453 }
13454 
wlan_unset_rf_test_mode(void)13455 int wlan_unset_rf_test_mode(void)
13456 {
13457     (void)wifi_unset_rf_test_mode();
13458 #if CONFIG_WIFI_AUTO_POWER_SAVE
13459     wlan_deepsleepps_on();
13460     wlan_ieeeps_on(wlan.wakeup_conditions);
13461 #endif
13462     return WM_SUCCESS;
13463 }
13464 
wlan_set_rf_channel(const uint8_t channel)13465 int wlan_set_rf_channel(const uint8_t channel)
13466 {
13467     return wifi_set_rf_channel(channel);
13468 }
13469 
wlan_set_rf_radio_mode(const uint8_t mode)13470 int wlan_set_rf_radio_mode(const uint8_t mode)
13471 {
13472     return wifi_set_rf_radio_mode(mode);
13473 }
13474 
wlan_get_rf_channel(uint8_t * channel)13475 int wlan_get_rf_channel(uint8_t *channel)
13476 {
13477     if (channel != NULL)
13478         return wifi_get_rf_channel(channel);
13479 
13480     return -WM_FAIL;
13481 }
13482 
wlan_get_rf_radio_mode(uint8_t * mode)13483 int wlan_get_rf_radio_mode(uint8_t *mode)
13484 {
13485     if (mode)
13486         return wifi_get_rf_radio_mode(mode);
13487 
13488     return -WM_FAIL;
13489 }
13490 
wlan_set_rf_bandwidth(const uint8_t bandwidth)13491 int wlan_set_rf_bandwidth(const uint8_t bandwidth)
13492 {
13493     return wifi_set_rf_bandwidth(bandwidth);
13494 }
13495 
wlan_set_rf_band(const uint8_t band)13496 int wlan_set_rf_band(const uint8_t band)
13497 {
13498     return wifi_set_rf_band(band);
13499 }
13500 
wlan_get_rf_band(uint8_t * band)13501 int wlan_get_rf_band(uint8_t *band)
13502 {
13503     if (band != NULL)
13504         return wifi_get_rf_band(band);
13505 
13506     return -WM_FAIL;
13507 }
13508 
wlan_get_rf_bandwidth(uint8_t * bandwidth)13509 int wlan_get_rf_bandwidth(uint8_t *bandwidth)
13510 {
13511     if (bandwidth != NULL)
13512         return wifi_get_rf_bandwidth(bandwidth);
13513 
13514     return -WM_FAIL;
13515 }
13516 
wlan_get_rf_per(uint32_t * rx_tot_pkt_count,uint32_t * rx_mcast_bcast_count,uint32_t * rx_pkt_fcs_error)13517 int wlan_get_rf_per(uint32_t *rx_tot_pkt_count, uint32_t *rx_mcast_bcast_count, uint32_t *rx_pkt_fcs_error)
13518 {
13519     if ((rx_tot_pkt_count != NULL) && (rx_mcast_bcast_count != NULL) && (rx_pkt_fcs_error != NULL))
13520         return wifi_get_rf_per(rx_tot_pkt_count, rx_mcast_bcast_count, rx_pkt_fcs_error);
13521 
13522     return -WM_FAIL;
13523 }
13524 
wlan_set_rf_tx_cont_mode(const uint32_t enable_tx,const uint32_t cw_mode,const uint32_t payload_pattern,const uint32_t cs_mode,const uint32_t act_sub_ch,const uint32_t tx_rate)13525 int wlan_set_rf_tx_cont_mode(const uint32_t enable_tx,
13526                              const uint32_t cw_mode,
13527                              const uint32_t payload_pattern,
13528                              const uint32_t cs_mode,
13529                              const uint32_t act_sub_ch,
13530                              const uint32_t tx_rate)
13531 {
13532     return wifi_set_rf_tx_cont_mode(enable_tx, cw_mode, payload_pattern, cs_mode, act_sub_ch, tx_rate);
13533 }
13534 
wlan_set_rf_tx_antenna(const uint8_t antenna)13535 int wlan_set_rf_tx_antenna(const uint8_t antenna)
13536 {
13537     return wifi_set_rf_tx_antenna(antenna);
13538 }
13539 
wlan_get_rf_tx_antenna(uint8_t * antenna)13540 int wlan_get_rf_tx_antenna(uint8_t *antenna)
13541 {
13542     if (antenna != NULL)
13543         return wifi_get_rf_tx_antenna(antenna);
13544 
13545     return -WM_FAIL;
13546 }
13547 
wlan_set_rf_rx_antenna(const uint8_t antenna)13548 int wlan_set_rf_rx_antenna(const uint8_t antenna)
13549 {
13550     return wifi_set_rf_rx_antenna(antenna);
13551 }
13552 
wlan_get_rf_rx_antenna(uint8_t * antenna)13553 int wlan_get_rf_rx_antenna(uint8_t *antenna)
13554 {
13555     if (antenna != NULL)
13556         return wifi_get_rf_rx_antenna(antenna);
13557 
13558     return -WM_FAIL;
13559 }
13560 
wlan_set_rf_tx_power(const uint32_t power,const uint8_t mod,const uint8_t path_id)13561 int wlan_set_rf_tx_power(const uint32_t power, const uint8_t mod, const uint8_t path_id)
13562 {
13563     return wifi_set_rf_tx_power(power, mod, path_id);
13564 }
13565 
wlan_cfg_rf_he_tb_tx(uint16_t enable,uint16_t qnum,uint16_t aid,uint16_t axq_mu_timer,int16_t tx_power)13566 int wlan_cfg_rf_he_tb_tx(uint16_t enable,uint16_t qnum,uint16_t aid,uint16_t axq_mu_timer,int16_t tx_power)
13567 {
13568 	return wifi_cfg_rf_he_tb_tx(enable, qnum, aid,axq_mu_timer,tx_power);
13569 }
13570 
wlan_rf_trigger_frame_cfg(uint32_t Enable_tx,uint32_t Standalone_hetb,uint8_t FRAME_CTRL_TYPE,uint8_t FRAME_CTRL_SUBTYPE,uint16_t FRAME_DURATION,uint64_t TriggerType,uint64_t UlLen,uint64_t MoreTF,uint64_t CSRequired,uint64_t UlBw,uint64_t LTFType,uint64_t LTFMode,uint64_t LTFSymbol,uint64_t UlSTBC,uint64_t LdpcESS,uint64_t ApTxPwr,uint64_t PreFecPadFct,uint64_t PeDisambig,uint64_t SpatialReuse,uint64_t Doppler,uint64_t HeSig2,uint32_t AID12,uint32_t RUAllocReg,uint32_t RUAlloc,uint32_t UlCodingType,uint32_t UlMCS,uint32_t UlDCM,uint32_t SSAlloc,uint8_t UlTargetRSSI,uint8_t MPDU_MU_SF,uint8_t TID_AL,uint8_t AC_PL,uint8_t Pref_AC)13571 int wlan_rf_trigger_frame_cfg(uint32_t Enable_tx,uint32_t Standalone_hetb,uint8_t FRAME_CTRL_TYPE,
13572                               uint8_t FRAME_CTRL_SUBTYPE,uint16_t FRAME_DURATION,uint64_t TriggerType,
13573 							  uint64_t UlLen,uint64_t MoreTF,uint64_t CSRequired,uint64_t UlBw,
13574 							  uint64_t LTFType,uint64_t LTFMode,uint64_t LTFSymbol,uint64_t UlSTBC,
13575 							  uint64_t LdpcESS,uint64_t ApTxPwr,uint64_t PreFecPadFct,
13576 							  uint64_t PeDisambig,uint64_t SpatialReuse,uint64_t Doppler,
13577 							  uint64_t HeSig2,uint32_t AID12,uint32_t RUAllocReg,uint32_t RUAlloc,
13578 							  uint32_t UlCodingType,uint32_t UlMCS,uint32_t UlDCM,uint32_t SSAlloc,
13579 							  uint8_t UlTargetRSSI,uint8_t MPDU_MU_SF,uint8_t TID_AL,uint8_t AC_PL,
13580 							  uint8_t Pref_AC)
13581 {
13582 	return wifi_rf_trigger_frame_cfg(Enable_tx,Standalone_hetb,FRAME_CTRL_TYPE,
13583                                      FRAME_CTRL_SUBTYPE,FRAME_DURATION,TriggerType,
13584 			                         UlLen,MoreTF,CSRequired,UlBw,LTFType,LTFMode,
13585 			                         LTFSymbol,UlSTBC,LdpcESS,ApTxPwr,PreFecPadFct,
13586 			                         PeDisambig,SpatialReuse,Doppler,HeSig2,AID12,
13587 			                         RUAllocReg,RUAlloc,UlCodingType,UlMCS,UlDCM,
13588 			                         SSAlloc,UlTargetRSSI,MPDU_MU_SF,TID_AL,AC_PL,
13589 			                         Pref_AC);
13590 }
13591 
wlan_set_rf_tx_frame(const uint32_t enable,const uint32_t data_rate,const uint32_t frame_pattern,const uint32_t frame_length,const uint16_t adjust_burst_sifs,const uint32_t burst_sifs_in_us,const uint32_t short_preamble,const uint32_t act_sub_ch,const uint32_t short_gi,const uint32_t adv_coding,const uint32_t tx_bf,const uint32_t gf_mode,const uint32_t stbc,const uint8_t * bssid)13592 int wlan_set_rf_tx_frame(const uint32_t enable,
13593                          const uint32_t data_rate,
13594                          const uint32_t frame_pattern,
13595                          const uint32_t frame_length,
13596                          const uint16_t adjust_burst_sifs,
13597                          const uint32_t burst_sifs_in_us,
13598                          const uint32_t short_preamble,
13599                          const uint32_t act_sub_ch,
13600                          const uint32_t short_gi,
13601                          const uint32_t adv_coding,
13602                          const uint32_t tx_bf,
13603                          const uint32_t gf_mode,
13604                          const uint32_t stbc,
13605                          const uint8_t *bssid)
13606 {
13607     return wifi_set_rf_tx_frame(enable, data_rate, frame_pattern, frame_length, adjust_burst_sifs, burst_sifs_in_us,
13608                                 short_preamble, act_sub_ch, short_gi, adv_coding, tx_bf, gf_mode, stbc, bssid);
13609 }
13610 
wlan_set_rf_otp_mac_addr(uint8_t * mac)13611 int wlan_set_rf_otp_mac_addr(uint8_t *mac)
13612 {
13613     return wifi_set_rf_otp_mac_addr(mac);
13614 }
13615 
wlan_get_rf_otp_mac_addr(uint8_t * mac)13616 int wlan_get_rf_otp_mac_addr(uint8_t *mac)
13617 {
13618     if (mac != NULL)
13619       return wifi_get_rf_otp_mac_addr(mac);
13620 
13621     return -WM_FAIL;
13622 }
13623 
wlan_set_rf_otp_cal_data(const uint8_t * cal_data,uint32_t cal_data_len)13624 int wlan_set_rf_otp_cal_data(const uint8_t *cal_data, uint32_t cal_data_len)
13625 {
13626     return wifi_set_rf_otp_cal_data(cal_data, cal_data_len);
13627 }
13628 
wlan_get_rf_otp_cal_data(uint8_t * cal_data)13629 int wlan_get_rf_otp_cal_data(uint8_t *cal_data)
13630 {
13631     if (cal_data != NULL)
13632       return wifi_get_rf_otp_cal_data(cal_data);
13633 
13634     return -WM_FAIL;
13635 }
13636 #endif
13637 #if CONFIG_WIFI_FW_DEBUG
wlan_register_fw_dump_cb(void (* wlan_usb_init_cb)(void),int (* wlan_usb_mount_cb)(),int (* wlan_usb_file_open_cb)(char * test_file_name),int (* wlan_usb_file_write_cb)(uint8_t * data,size_t data_len),int (* wlan_usb_file_close_cb)())13638 void wlan_register_fw_dump_cb(void (*wlan_usb_init_cb)(void),
13639                               int (*wlan_usb_mount_cb)(),
13640                               int (*wlan_usb_file_open_cb)(char *test_file_name),
13641                               int (*wlan_usb_file_write_cb)(uint8_t *data, size_t data_len),
13642                               int (*wlan_usb_file_close_cb)())
13643 {
13644     wlan.wlan_usb_init_cb = wlan_usb_init_cb;
13645     wifi_register_fw_dump_cb(wlan_usb_mount_cb, wlan_usb_file_open_cb, wlan_usb_file_write_cb, wlan_usb_file_close_cb);
13646 }
13647 #endif
13648 
13649 #if CONFIG_WMM
wlan_wmm_tx_stats_dump(int bss_type)13650 void wlan_wmm_tx_stats_dump(int bss_type)
13651 {
13652     wifi_wmm_tx_stats_dump(bss_type);
13653 }
13654 #endif
13655 
wlan_send_hostcmd(const void * cmd_buf,uint32_t cmd_buf_len,void * host_resp_buf,uint32_t resp_buf_len,uint32_t * reqd_resp_len)13656 int wlan_send_hostcmd(
13657     const void *cmd_buf, uint32_t cmd_buf_len, void *host_resp_buf, uint32_t resp_buf_len, uint32_t *reqd_resp_len)
13658 {
13659     if ((cmd_buf == NULL) || (host_resp_buf == NULL) || (reqd_resp_len == NULL))
13660     {
13661         return (int)WM_E_NOMEM;
13662     }
13663     if (!cmd_buf_len || !resp_buf_len)
13664     {
13665         return (int)WM_E_INVAL;
13666     }
13667 
13668     return wifi_send_hostcmd(cmd_buf, cmd_buf_len, host_resp_buf, resp_buf_len, reqd_resp_len);
13669 }
13670 
13671 #if CONFIG_11AX
wlan_enable_disable_htc(uint8_t option)13672 int wlan_enable_disable_htc(uint8_t option)
13673 {
13674     int ret                 = -WM_FAIL;
13675     uint8_t send_htc_set[]  = {0x8b, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x24, 0x01, 0x01, 0x00, 0x00, 0x00};
13676     uint8_t debug_resp_buf[32] = {0};
13677     uint32_t reqd_len       = 0;
13678 
13679     send_htc_set[12] = option;
13680 
13681     ret = wlan_send_hostcmd(send_htc_set, sizeof(send_htc_set) / sizeof(uint8_t), debug_resp_buf, sizeof(debug_resp_buf),
13682                             &reqd_len);
13683 
13684     return ret;
13685 }
13686 
13687 #if CONFIG_WIFI_HTC_DEBUG
wlan_send_debug_htc(const uint8_t count,const uint8_t vht,const uint8_t he,const uint8_t rxNss,const uint8_t channelWidth,const uint8_t ulMuDisable,const uint8_t txNSTS,const uint8_t erSuDisable,const uint8_t dlResoundRecomm,const uint8_t ulMuDataDisable)13688 int wlan_send_debug_htc(const uint8_t count,
13689 		const uint8_t vht,
13690 		const uint8_t he,
13691 		const uint8_t rxNss,
13692 		const uint8_t channelWidth,
13693 		const uint8_t ulMuDisable,
13694 		const uint8_t txNSTS,
13695 		const uint8_t erSuDisable,
13696 		const uint8_t dlResoundRecomm,
13697 		const uint8_t ulMuDataDisable)
13698 {
13699     int ret           = -WM_FAIL;
13700     int i;
13701     uint8_t debug_cmd_buf[] = {0x8b, 0x00, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x11, 0x01, 0x40, 0x01, 0x01, 0x00,
13702 			0x02, 0x00, 0x00, 0x00, 0x00, 0x00};
13703 
13704     uint8_t debug_resp_buf[32] = {0};
13705     uint32_t reqd_len = 0;
13706 
13707     (void)memset(debug_resp_buf, 0, sizeof(debug_resp_buf));
13708 
13709     debug_cmd_buf[12] = count;
13710     debug_cmd_buf[13] = vht;
13711     debug_cmd_buf[14] = he;
13712     debug_cmd_buf[15] = rxNss;
13713     debug_cmd_buf[16] = channelWidth;
13714     debug_cmd_buf[17] = ulMuDisable;
13715     debug_cmd_buf[18] = txNSTS;
13716     debug_cmd_buf[19] = erSuDisable;
13717     debug_cmd_buf[20] = dlResoundRecomm;
13718     debug_cmd_buf[21] = ulMuDataDisable;
13719 
13720     ret = wlan_send_hostcmd(debug_cmd_buf, sizeof(debug_cmd_buf) / sizeof(uint8_t), debug_resp_buf, sizeof(debug_resp_buf),
13721                             &reqd_len);
13722     if (ret == WM_SUCCESS)
13723     {
13724         (void)PRINTF("Hostcmd success, response is\r\n");
13725         for (i = 0; i < reqd_len; i++)
13726             (void)PRINTF("%x\t", debug_resp_buf[i]);
13727     }
13728     else
13729     {
13730         (void)PRINTF("Hostcmd failed error: %d", ret);
13731     }
13732     return ret;
13733 }
13734 #endif
13735 
wlan_set_11ax_tx_omi(const t_u8 interface,const t_u16 tx_omi,const t_u8 tx_option,const t_u8 num_data_pkts)13736 int wlan_set_11ax_tx_omi(const t_u8 interface, const t_u16 tx_omi, const t_u8 tx_option, const t_u8 num_data_pkts)
13737 {
13738     if (interface == MLAN_BSS_TYPE_STA)
13739     {
13740 
13741         if (num_data_pkts > 16)
13742         {
13743             (void)PRINTF("Minimum value of num_data_pkts should be 1 and maximum should be 16");
13744             return -WM_FAIL;
13745         }
13746 
13747         if (is_sta_connected())
13748         {
13749             return wifi_set_11ax_tx_omi(MLAN_BSS_TYPE_STA, tx_omi, tx_option, num_data_pkts);
13750         }
13751         else
13752         {
13753             wifi_d("STA not connected");
13754             return -WM_FAIL;
13755         }
13756     }
13757     else if (interface == MLAN_BSS_TYPE_UAP)
13758     {
13759         if (is_uap_started())
13760         {
13761             return wifi_set_11ax_tx_omi(MLAN_BSS_TYPE_UAP, tx_omi, tx_option, num_data_pkts);
13762         }
13763         else
13764         {
13765             wifi_d("uAP not started");
13766             return -WM_FAIL;
13767         }
13768     }
13769     else
13770     {
13771         wifi_d("Interface not supported");
13772         return -WM_FAIL;
13773     }
13774 }
13775 
wlan_set_11ax_tol_time(const t_u32 tol_time)13776 int wlan_set_11ax_tol_time(const t_u32 tol_time)
13777 {
13778     if (tol_time < 1 || tol_time > 3600)
13779     {
13780         wlcm_d("Error: invalid tolerance time value, range[[1..3600]].");
13781         return -WM_FAIL;
13782         ;
13783     }
13784 
13785     if (is_sta_connecting())
13786     {
13787         wlcm_d("Pls set OBSS Tolerance Time value before connecting to AP.");
13788         return -WM_FAIL;
13789     }
13790 
13791     return wifi_set_11ax_tol_time(tol_time);
13792 }
13793 
wlan_set_11ax_rutxpowerlimit(const void * rutx_pwr_cfg,uint32_t rutx_pwr_cfg_len)13794 int wlan_set_11ax_rutxpowerlimit(const void *rutx_pwr_cfg, uint32_t rutx_pwr_cfg_len)
13795 {
13796     if (rutx_pwr_cfg != NULL)
13797     {
13798         return wifi_set_11ax_rutxpowerlimit(rutx_pwr_cfg, rutx_pwr_cfg_len);
13799     }
13800 
13801     return -WM_FAIL;
13802 }
13803 
wlan_set_11ax_rutxpowerlimit_legacy(const wifi_rutxpwrlimit_t * ru_pwr_cfg)13804 int wlan_set_11ax_rutxpowerlimit_legacy(const wifi_rutxpwrlimit_t *ru_pwr_cfg)
13805 {
13806     if (ru_pwr_cfg != NULL)
13807     {
13808         return wifi_set_11ax_rutxpowerlimit_legacy(ru_pwr_cfg);
13809     }
13810 
13811     return -WM_FAIL;
13812 }
13813 
wlan_get_11ax_rutxpowerlimit_legacy(wifi_rutxpwrlimit_t * ru_pwr_cfg)13814 int wlan_get_11ax_rutxpowerlimit_legacy(wifi_rutxpwrlimit_t *ru_pwr_cfg)
13815 {
13816     if (ru_pwr_cfg != NULL)
13817     {
13818         (void)memset(ru_pwr_cfg, 0x00, sizeof(wlan_rutxpwrlimit_t));
13819         return wifi_get_11ax_rutxpowerlimit_legacy(ru_pwr_cfg);
13820     }
13821 
13822     return -WM_FAIL;
13823 }
13824 
13825 /* cfg tables for 11axcfg and twt commands to FW */
13826 static wlan_11ax_config_t g_11ax_cfg_default[] = {
13827     {/* band */
13828      0x03,
13829      /* HE cap */
13830      0x00ff,                               // ID
13831      0x0018,                               // Length
13832      0x23,                                 // he capability id
13833      {0x03, 0x08, 0x00, 0x82, 0x00, 0x00}, // HE MAC capability info
13834      {0x40, 0x50, 0x42, 0x49, 0x0d, 0x00, 0x20, 0x1e, 0x17, 0x31,
13835       0x00},                   // HE PHY capability info
13836      {0xfd, 0xff, 0xfd, 0xff}, // Tx Rx HE-MCS NSS support
13837      {0x88, 0x1f, 0x00, 0x00}}};
13838 
wlan_set_11ax_cfg(wlan_11ax_config_t * ax_config)13839 int wlan_set_11ax_cfg(wlan_11ax_config_t *ax_config)
13840 {
13841     return wifi_set_11ax_cfg(ax_config);
13842 }
13843 
wlan_get_11ax_cfg(void)13844 wlan_11ax_config_t *wlan_get_11ax_cfg(void)
13845 {
13846     return g_11ax_cfg_default;
13847 }
13848 
13849 #if CONFIG_11AX_TWT
13850 static wlan_btwt_config_t g_btwt_cfg_default[] = {{/* action */
13851                                                    0x0001,
13852                                                    /* sub_id */
13853                                                    0x0125,
13854                                                    /* btwt_cfg */
13855                                                    0x40, 0x04, 0x0063, 0x0270,
13856                                                    0x0a, 0x05}};
13857 
wlan_set_btwt_cfg(const wlan_btwt_config_t * btwt_config)13858 int wlan_set_btwt_cfg(const wlan_btwt_config_t *btwt_config)
13859 {
13860     return wifi_set_btwt_cfg(btwt_config);
13861 }
13862 
wlan_get_btwt_cfg(void)13863 wlan_btwt_config_t *wlan_get_btwt_cfg(void)
13864 {
13865     return g_btwt_cfg_default;
13866 }
13867 
13868 static wlan_twt_setup_config_t g_twt_setup_cfg_default[] = {{
13869     0x01,   // implicit
13870     0x00,   // unannounced
13871     0x00,   // Non-Trigger
13872     0x00,   // info enabled
13873     0x00,   // indv TWT
13874     0x40,   // wakeup dur
13875     0x00,   // FID
13876     0x01,   // FW not tweak
13877     0x0a,   // exponent
13878     0x0200, // mantissa  200TU
13879     0x00,   // REQ TWT
13880     0x00,   // state
13881     0x003c  // bcn miss=60s
13882 }};
13883 
13884 /* Below macros are defined as in FW under dot11ax_twt.c */
13885 #define TWT_EARLY_WAKEUP_ADJUSTMENT 1000                                // us
13886 #define TWT_SLEEP_MIN               (756 + TWT_EARLY_WAKEUP_ADJUSTMENT) // us
wlan_set_twt_setup_cfg(const wlan_twt_setup_config_t * twt_setup)13887 int wlan_set_twt_setup_cfg(const wlan_twt_setup_config_t *twt_setup)
13888 {
13889     if (((twt_setup->twt_mantissa << twt_setup->twt_exponent) - (twt_setup->twt_wakeup_duration * 256)) < TWT_SLEEP_MIN)
13890     {
13891         wlcm_e("TWT interval is : %u us", twt_setup->twt_mantissa << twt_setup->twt_exponent);
13892         wlcm_e("Wakeup duration (WD) value is : %u us", twt_setup->twt_wakeup_duration * 256);
13893         wlcm_e("Minimum sleep time (Interval - WD) should be greater than: %u us", TWT_SLEEP_MIN);
13894         return -WM_FAIL;
13895     }
13896     return wifi_set_twt_setup_cfg(twt_setup);
13897 }
13898 
wlan_get_twt_setup_cfg(void)13899 wlan_twt_setup_config_t * wlan_get_twt_setup_cfg(void)
13900 {
13901     return g_twt_setup_cfg_default;
13902 }
13903 
13904 static wlan_twt_teardown_config_t g_twt_teardown_cfg_default[] = {
13905     {0x00, 0x00, 0x00}};
13906 
wlan_set_twt_teardown_cfg(const wlan_twt_teardown_config_t * teardown_config)13907 int wlan_set_twt_teardown_cfg(const wlan_twt_teardown_config_t *teardown_config)
13908 {
13909     return wifi_set_twt_teardown_cfg(teardown_config);
13910 }
13911 
wlan_get_twt_teardown_cfg(void)13912 wlan_twt_teardown_config_t * wlan_get_twt_teardown_cfg(void)
13913 {
13914     return g_twt_teardown_cfg_default;
13915 }
13916 
wlan_get_twt_report(wlan_twt_report_t * twt_report)13917 int wlan_get_twt_report(wlan_twt_report_t *twt_report)
13918 {
13919     return wifi_get_twt_report(twt_report);
13920 }
13921 
wlan_twt_information(wlan_twt_information_t * twt_information)13922 int wlan_twt_information(wlan_twt_information_t *twt_information)
13923 {
13924     return wifi_twt_information(twt_information);
13925 }
13926 #endif /* CONFIG_11AX_TWT */
13927 #endif /* CONFIG_11AX */
13928 
13929 #if CONFIG_WIFI_CLOCKSYNC
wlan_get_tsf_info(wlan_tsf_info_t * tsf_info)13930 int wlan_get_tsf_info(wlan_tsf_info_t *tsf_info)
13931 {
13932     return wifi_get_tsf_info(tsf_info);
13933 }
wlan_set_clocksync_cfg(const wlan_clock_sync_gpio_tsf_t * tsf_latch)13934 int wlan_set_clocksync_cfg(const wlan_clock_sync_gpio_tsf_t *tsf_latch)
13935 {
13936     return wifi_set_clocksync_cfg(tsf_latch, (mlan_bss_type)WLAN_BSS_TYPE_STA);
13937 }
13938 #endif /* CONFIG_WIFI_CLOCKSYNC */
13939 
13940 #if CONFIG_WIFI_EU_CRYPTO
wlan_set_crypto_RC4_encrypt(const t_u8 * Key,const t_u16 KeyLength,const t_u8 * KeyIV,const t_u16 KeyIVLength,t_u8 * Data,t_u16 * DataLength)13941 int wlan_set_crypto_RC4_encrypt(
13942     const t_u8 *Key, const t_u16 KeyLength, const t_u8 *KeyIV, const t_u16 KeyIVLength, t_u8 *Data, t_u16 *DataLength)
13943 {
13944     t_u16 rc4_key_length   = EU_CRYPTO_KEY_MAX_LENGTH;
13945     t_u16 rc4_keyiv_length = EU_CRYPTO_KEYIV_MAX_LENGTH;
13946     t_u16 rc4_data_length  = EU_CRYPTO_DATA_MAX_LENGTH;
13947 
13948 #if defined(SD8801)
13949     return -WM_E_PERM;
13950 #endif
13951 
13952     if (!wlan_is_started())
13953     {
13954         (void)PRINTF("Must enable Wi-Fi firstly\r\n");
13955         return -WM_FAIL;
13956     }
13957 
13958     if (KeyLength > rc4_key_length)
13959         return -WM_FAIL;
13960     if (KeyIVLength > rc4_keyiv_length)
13961         return -WM_FAIL;
13962     if (*DataLength > rc4_data_length)
13963         return -WM_FAIL;
13964     EU_Crypto Crypto_RC4_Param;
13965     Crypto_RC4_Param.KeyIVLength = KeyIVLength;
13966     memcpy(Crypto_RC4_Param.KeyIV, KeyIV, KeyIVLength);
13967     Crypto_RC4_Param.KeyLength = KeyLength;
13968     memcpy(Crypto_RC4_Param.Key, Key, KeyLength);
13969     Crypto_RC4_Param.DataLength = DataLength;
13970     Crypto_RC4_Param.Data       = Data;
13971 
13972     t_u16 EncDec = 1;
13973 
13974     return wifi_set_eu_crypto(&Crypto_RC4_Param, CRYPTO_RC4, EncDec);
13975 }
13976 
wlan_set_crypto_RC4_decrypt(const t_u8 * Key,const t_u16 KeyLength,const t_u8 * KeyIV,const t_u16 KeyIVLength,t_u8 * Data,t_u16 * DataLength)13977 int wlan_set_crypto_RC4_decrypt(
13978     const t_u8 *Key, const t_u16 KeyLength, const t_u8 *KeyIV, const t_u16 KeyIVLength, t_u8 *Data, t_u16 *DataLength)
13979 {
13980     t_u16 rc4_key_length   = EU_CRYPTO_KEY_MAX_LENGTH;
13981     t_u16 rc4_keyiv_length = EU_CRYPTO_KEYIV_MAX_LENGTH;
13982     t_u16 rc4_data_length  = EU_CRYPTO_DATA_MAX_LENGTH;
13983 
13984 #if defined(SD8801)
13985     return -WM_E_PERM;
13986 #endif
13987 
13988     if (!wlan_is_started())
13989     {
13990         (void)PRINTF("Must enable Wi-Fi firstly\r\n");
13991         return -WM_FAIL;
13992     }
13993 
13994     if (KeyLength > rc4_key_length)
13995         return -WM_FAIL;
13996     if (KeyIVLength > rc4_keyiv_length)
13997         return -WM_FAIL;
13998     if (*DataLength > rc4_data_length)
13999         return -WM_FAIL;
14000     EU_Crypto Crypto_RC4_Param;
14001     Crypto_RC4_Param.KeyIVLength = KeyIVLength;
14002     memcpy(Crypto_RC4_Param.KeyIV, KeyIV, KeyIVLength);
14003     Crypto_RC4_Param.KeyLength = KeyLength;
14004     memcpy(Crypto_RC4_Param.Key, Key, KeyLength);
14005     Crypto_RC4_Param.DataLength = DataLength;
14006     Crypto_RC4_Param.Data       = Data;
14007 
14008     t_u16 EncDec = 0;
14009 
14010     return wifi_set_eu_crypto(&Crypto_RC4_Param, CRYPTO_RC4, EncDec);
14011 }
14012 
wlan_set_crypto_AES_ECB_encrypt(const t_u8 * Key,const t_u16 KeyLength,const t_u8 * KeyIV,const t_u16 KeyIVLength,t_u8 * Data,t_u16 * DataLength)14013 int wlan_set_crypto_AES_ECB_encrypt(
14014     const t_u8 *Key, const t_u16 KeyLength, const t_u8 *KeyIV, const t_u16 KeyIVLength, t_u8 *Data, t_u16 *DataLength)
14015 {
14016     t_u16 aes_ecb_key_length   = EU_CRYPTO_KEY_MAX_LENGTH;
14017     t_u16 aes_ecb_keyiv_length = EU_CRYPTO_KEYIV_MAX_LENGTH;
14018     t_u16 aes_ecb_data_length  = EU_CRYPTO_DATA_MAX_LENGTH;
14019 
14020 #if defined(SD8801)
14021     return -WM_E_PERM;
14022 #endif
14023 
14024     if (!wlan_is_started())
14025     {
14026         (void)PRINTF("Must enable Wi-Fi firstly\r\n");
14027         return -WM_FAIL;
14028     }
14029 
14030     if (KeyLength > aes_ecb_key_length)
14031         return -WM_FAIL;
14032     if (KeyIVLength > aes_ecb_keyiv_length)
14033         return -WM_FAIL;
14034     if (*DataLength > aes_ecb_data_length)
14035         return -WM_FAIL;
14036     EU_Crypto Crypto_AES_ECB_Param;
14037     Crypto_AES_ECB_Param.KeyIVLength = KeyIVLength;
14038     memcpy(Crypto_AES_ECB_Param.KeyIV, KeyIV, KeyIVLength);
14039     Crypto_AES_ECB_Param.KeyLength = KeyLength;
14040     memcpy(Crypto_AES_ECB_Param.Key, Key, KeyLength);
14041     Crypto_AES_ECB_Param.DataLength = DataLength;
14042     Crypto_AES_ECB_Param.Data       = Data;
14043 
14044     t_u16 EncDec = 1;
14045 
14046     return wifi_set_eu_crypto(&Crypto_AES_ECB_Param, CRYPTO_AES_ECB, EncDec);
14047 }
14048 
wlan_set_crypto_AES_ECB_decrypt(const t_u8 * Key,const t_u16 KeyLength,const t_u8 * KeyIV,const t_u16 KeyIVLength,t_u8 * Data,t_u16 * DataLength)14049 int wlan_set_crypto_AES_ECB_decrypt(
14050     const t_u8 *Key, const t_u16 KeyLength, const t_u8 *KeyIV, const t_u16 KeyIVLength, t_u8 *Data, t_u16 *DataLength)
14051 {
14052     t_u16 aes_ecb_key_length   = EU_CRYPTO_KEY_MAX_LENGTH;
14053     t_u16 aes_ecb_keyiv_length = EU_CRYPTO_KEYIV_MAX_LENGTH;
14054     t_u16 aes_ecb_data_length  = EU_CRYPTO_DATA_MAX_LENGTH;
14055 
14056 #if defined(SD8801)
14057     return -WM_E_PERM;
14058 #endif
14059 
14060     if (!wlan_is_started())
14061     {
14062         (void)PRINTF("Must enable Wi-Fi firstly\r\n");
14063         return -WM_FAIL;
14064     }
14065 
14066     if (KeyLength > aes_ecb_key_length)
14067         return -WM_FAIL;
14068     if (KeyIVLength > aes_ecb_keyiv_length)
14069         return -WM_FAIL;
14070     if (*DataLength > aes_ecb_data_length)
14071         return -WM_FAIL;
14072     EU_Crypto Crypto_AES_ECB_Param;
14073     Crypto_AES_ECB_Param.KeyIVLength = KeyIVLength;
14074     memcpy(Crypto_AES_ECB_Param.KeyIV, KeyIV, KeyIVLength);
14075     Crypto_AES_ECB_Param.KeyLength = KeyLength;
14076     memcpy(Crypto_AES_ECB_Param.Key, Key, KeyLength);
14077     Crypto_AES_ECB_Param.DataLength = DataLength;
14078     Crypto_AES_ECB_Param.Data       = Data;
14079 
14080     t_u16 EncDec = 0;
14081 
14082     return wifi_set_eu_crypto(&Crypto_AES_ECB_Param, CRYPTO_AES_ECB, EncDec);
14083 }
14084 
wlan_set_crypto_AES_WRAP_encrypt(const t_u8 * Key,const t_u16 KeyLength,const t_u8 * KeyIV,const t_u16 KeyIVLength,t_u8 * Data,t_u16 * DataLength)14085 int wlan_set_crypto_AES_WRAP_encrypt(
14086     const t_u8 *Key, const t_u16 KeyLength, const t_u8 *KeyIV, const t_u16 KeyIVLength, t_u8 *Data, t_u16 *DataLength)
14087 {
14088     t_u16 aes_wrap_key_length   = EU_CRYPTO_KEY_MAX_LENGTH;
14089     t_u16 aes_wrap_keyiv_length = EU_CRYPTO_KEYIV_MAX_LENGTH;
14090     t_u16 aes_wrap_data_length  = EU_CRYPTO_DATA_MAX_LENGTH;
14091 
14092 #if defined(SD8801)
14093     return -WM_E_PERM;
14094 #endif
14095 
14096     if (!wlan_is_started())
14097     {
14098         (void)PRINTF("Must enable Wi-Fi firstly\r\n");
14099         return -WM_FAIL;
14100     }
14101 
14102     if (KeyLength > aes_wrap_key_length)
14103         return -WM_FAIL;
14104     if (KeyIVLength > aes_wrap_keyiv_length)
14105         return -WM_FAIL;
14106     if (*DataLength > aes_wrap_data_length)
14107         return -WM_FAIL;
14108 
14109     EU_Crypto Crypto_AES_WRAP_Param;
14110     Crypto_AES_WRAP_Param.KeyIVLength = KeyIVLength;
14111     memcpy(Crypto_AES_WRAP_Param.KeyIV, KeyIV, KeyIVLength);
14112     Crypto_AES_WRAP_Param.KeyLength = KeyLength;
14113     memcpy(Crypto_AES_WRAP_Param.Key, Key, KeyLength);
14114     Crypto_AES_WRAP_Param.DataLength = DataLength;
14115     Crypto_AES_WRAP_Param.Data       = Data;
14116 
14117     t_u16 EncDec = 1;
14118 
14119     return wifi_set_eu_crypto(&Crypto_AES_WRAP_Param, CRYPTO_AES_WRAP, EncDec);
14120 }
14121 
wlan_set_crypto_AES_WRAP_decrypt(const t_u8 * Key,const t_u16 KeyLength,const t_u8 * KeyIV,const t_u16 KeyIVLength,t_u8 * Data,t_u16 * DataLength)14122 int wlan_set_crypto_AES_WRAP_decrypt(
14123     const t_u8 *Key, const t_u16 KeyLength, const t_u8 *KeyIV, const t_u16 KeyIVLength, t_u8 *Data, t_u16 *DataLength)
14124 {
14125     t_u16 aes_wrap_key_length   = EU_CRYPTO_KEY_MAX_LENGTH;
14126     t_u16 aes_wrap_keyiv_length = EU_CRYPTO_KEYIV_MAX_LENGTH;
14127     t_u16 aes_wrap_data_length  = EU_CRYPTO_DATA_MAX_LENGTH;
14128 
14129 #if defined(SD8801)
14130     return -WM_E_PERM;
14131 #endif
14132 
14133     if (!wlan_is_started())
14134     {
14135         (void)PRINTF("Must enable Wi-Fi firstly\r\n");
14136         return -WM_FAIL;
14137     }
14138 
14139     if (KeyLength > aes_wrap_key_length)
14140         return -WM_FAIL;
14141     if (KeyIVLength > aes_wrap_keyiv_length)
14142         return -WM_FAIL;
14143     if (*DataLength > aes_wrap_data_length)
14144         return -WM_FAIL;
14145     EU_Crypto Crypto_AES_WRAP_Param;
14146     Crypto_AES_WRAP_Param.KeyIVLength = KeyIVLength;
14147     memcpy(Crypto_AES_WRAP_Param.KeyIV, KeyIV, KeyIVLength);
14148     Crypto_AES_WRAP_Param.KeyLength = KeyLength;
14149     memcpy(Crypto_AES_WRAP_Param.Key, Key, KeyLength);
14150     Crypto_AES_WRAP_Param.DataLength = DataLength;
14151     Crypto_AES_WRAP_Param.Data       = Data;
14152 
14153     t_u16 EncDec = 0;
14154 
14155     return wifi_set_eu_crypto(&Crypto_AES_WRAP_Param, CRYPTO_AES_WRAP, EncDec);
14156 }
14157 
wlan_set_crypto_AES_CCMP_encrypt(const t_u8 * Key,const t_u16 KeyLength,const t_u8 * AAD,const t_u16 AADLength,const t_u8 * Nonce,const t_u16 NonceLength,t_u8 * Data,t_u16 * DataLength)14158 int wlan_set_crypto_AES_CCMP_encrypt(const t_u8 *Key,
14159                                      const t_u16 KeyLength,
14160                                      const t_u8 *AAD,
14161                                      const t_u16 AADLength,
14162                                      const t_u8 *Nonce,
14163                                      const t_u16 NonceLength,
14164                                      t_u8 *Data,
14165                                      t_u16 *DataLength)
14166 {
14167     t_u16 aes_ccmp_key_length   = EU_CRYPTO_KEY_MAX_LENGTH;
14168     t_u16 aes_ccmp_AAD_length   = EU_CRYPTO_AAD_MAX_LENGTH;
14169     t_u16 aes_ccmp_Nonce_length = EU_CRYPTO_NONCE_MAX_LENGTH;
14170     t_u16 aes_ccmp_data_length  = EU_CRYPTO_DATA_MAX_LENGTH;
14171 
14172 #if defined(SD8801)
14173     return -WM_E_PERM;
14174 #endif
14175 #if defined(SD8978)
14176     if (KeyLength == aes_ccmp_key_length)
14177     {
14178         return -WM_E_PERM;
14179     }
14180 #endif
14181 
14182     if (!wlan_is_started())
14183     {
14184         (void)PRINTF("Must enable Wi-Fi firstly\r\n");
14185         return -WM_FAIL;
14186     }
14187 
14188     if (KeyLength > aes_ccmp_key_length)
14189         return -WM_FAIL;
14190     if (AADLength > aes_ccmp_AAD_length)
14191         return -WM_FAIL;
14192     if (NonceLength > aes_ccmp_Nonce_length)
14193         return -WM_FAIL;
14194     if (*DataLength > aes_ccmp_data_length)
14195         return -WM_FAIL;
14196     EU_Crypto Crypto_AES_CCMP_Param;
14197     Crypto_AES_CCMP_Param.AADLength = AADLength;
14198     memcpy(Crypto_AES_CCMP_Param.AAD, AAD, AADLength);
14199     Crypto_AES_CCMP_Param.NonceLength = NonceLength;
14200     memcpy(Crypto_AES_CCMP_Param.Nonce, Nonce, NonceLength);
14201     Crypto_AES_CCMP_Param.KeyLength = KeyLength;
14202     memcpy(Crypto_AES_CCMP_Param.Key, Key, KeyLength);
14203     Crypto_AES_CCMP_Param.DataLength = DataLength;
14204     Crypto_AES_CCMP_Param.Data       = Data;
14205 
14206     t_u16 EncDec = 1;
14207 
14208     return wifi_set_eu_crypto(&Crypto_AES_CCMP_Param, CRYPTO_AES_CCMP, EncDec);
14209 }
14210 
wlan_set_crypto_AES_CCMP_decrypt(const t_u8 * Key,const t_u16 KeyLength,const t_u8 * AAD,const t_u16 AADLength,const t_u8 * Nonce,const t_u16 NonceLength,t_u8 * Data,t_u16 * DataLength)14211 int wlan_set_crypto_AES_CCMP_decrypt(const t_u8 *Key,
14212                                      const t_u16 KeyLength,
14213                                      const t_u8 *AAD,
14214                                      const t_u16 AADLength,
14215                                      const t_u8 *Nonce,
14216                                      const t_u16 NonceLength,
14217                                      t_u8 *Data,
14218                                      t_u16 *DataLength)
14219 {
14220     t_u16 aes_ccmp_key_length   = EU_CRYPTO_KEY_MAX_LENGTH;
14221     t_u16 aes_ccmp_AAD_length   = EU_CRYPTO_AAD_MAX_LENGTH;
14222     t_u16 aes_ccmp_Nonce_length = EU_CRYPTO_NONCE_MAX_LENGTH;
14223     t_u16 aes_ccmp_data_length  = EU_CRYPTO_DATA_MAX_LENGTH;
14224 
14225 #if defined(SD8801)
14226     return -WM_E_PERM;
14227 #endif
14228 
14229 #if defined(SD8978)
14230     if (KeyLength == aes_ccmp_key_length)
14231     {
14232         return -WM_E_PERM;
14233     }
14234 #endif
14235 
14236     if (!wlan_is_started())
14237     {
14238         (void)PRINTF("Must enable Wi-Fi firstly\r\n");
14239         return -WM_FAIL;
14240     }
14241 
14242     if (KeyLength > aes_ccmp_key_length)
14243         return -WM_FAIL;
14244     if (AADLength > aes_ccmp_AAD_length)
14245         return -WM_FAIL;
14246     if (NonceLength > aes_ccmp_Nonce_length)
14247         return -WM_FAIL;
14248     if (*DataLength > aes_ccmp_data_length)
14249         return -WM_FAIL;
14250     EU_Crypto Crypto_AES_CCMP_Param;
14251     Crypto_AES_CCMP_Param.AADLength = AADLength;
14252     memcpy(Crypto_AES_CCMP_Param.AAD, AAD, AADLength);
14253     Crypto_AES_CCMP_Param.NonceLength = NonceLength;
14254     memcpy(Crypto_AES_CCMP_Param.Nonce, Nonce, NonceLength);
14255     Crypto_AES_CCMP_Param.KeyLength = KeyLength;
14256     memcpy(Crypto_AES_CCMP_Param.Key, Key, KeyLength);
14257     Crypto_AES_CCMP_Param.DataLength = DataLength;
14258     Crypto_AES_CCMP_Param.Data       = Data;
14259 
14260     t_u16 EncDec = 0;
14261 
14262     return wifi_set_eu_crypto(&Crypto_AES_CCMP_Param, CRYPTO_AES_CCMP, EncDec);
14263 }
14264 
wlan_set_crypto_AES_GCMP_encrypt(const t_u8 * Key,const t_u16 KeyLength,const t_u8 * AAD,const t_u16 AADLength,const t_u8 * Nonce,const t_u16 NonceLength,t_u8 * Data,t_u16 * DataLength)14265 int wlan_set_crypto_AES_GCMP_encrypt(const t_u8 *Key,
14266                                      const t_u16 KeyLength,
14267                                      const t_u8 *AAD,
14268                                      const t_u16 AADLength,
14269                                      const t_u8 *Nonce,
14270                                      const t_u16 NonceLength,
14271                                      t_u8 *Data,
14272                                      t_u16 *DataLength)
14273 {
14274 #if defined(SD8801) || defined(SD8978)
14275     return -WM_E_PERM;
14276 #else
14277     t_u16 aes_gcmp_key_length   = EU_CRYPTO_KEY_MAX_LENGTH;
14278     t_u16 aes_gcmp_AAD_length   = EU_CRYPTO_AAD_MAX_LENGTH;
14279     t_u16 aes_gcmp_Nonce_length = EU_CRYPTO_NONCE_MAX_LENGTH;
14280     t_u16 aes_gcmp_data_length  = EU_CRYPTO_DATA_MAX_LENGTH;
14281 
14282     if (!wlan_is_started())
14283     {
14284         (void)PRINTF("Must enable Wi-Fi firstly\r\n");
14285         return -WM_FAIL;
14286     }
14287 
14288     if (KeyLength > aes_gcmp_key_length)
14289         return -WM_FAIL;
14290     if (AADLength > aes_gcmp_AAD_length)
14291         return -WM_FAIL;
14292     if (NonceLength > aes_gcmp_Nonce_length)
14293         return -WM_FAIL;
14294     if (*DataLength > aes_gcmp_data_length)
14295         return -WM_FAIL;
14296     EU_Crypto Crypto_AES_GCMP_Param;
14297     Crypto_AES_GCMP_Param.AADLength = AADLength;
14298     memcpy(Crypto_AES_GCMP_Param.AAD, AAD, AADLength);
14299     Crypto_AES_GCMP_Param.NonceLength = NonceLength;
14300     memcpy(Crypto_AES_GCMP_Param.Nonce, Nonce, NonceLength);
14301     Crypto_AES_GCMP_Param.KeyLength = KeyLength;
14302     memcpy(Crypto_AES_GCMP_Param.Key, Key, KeyLength);
14303     Crypto_AES_GCMP_Param.DataLength = DataLength;
14304     Crypto_AES_GCMP_Param.Data       = Data;
14305 
14306     t_u16 EncDec = 1;
14307 
14308     return wifi_set_eu_crypto(&Crypto_AES_GCMP_Param, CRYPTO_AES_GCMP, EncDec);
14309 #endif
14310 }
14311 
wlan_set_crypto_AES_GCMP_decrypt(const t_u8 * Key,const t_u16 KeyLength,const t_u8 * AAD,const t_u16 AADLength,const t_u8 * Nonce,const t_u16 NonceLength,t_u8 * Data,t_u16 * DataLength)14312 int wlan_set_crypto_AES_GCMP_decrypt(const t_u8 *Key,
14313                                      const t_u16 KeyLength,
14314                                      const t_u8 *AAD,
14315                                      const t_u16 AADLength,
14316                                      const t_u8 *Nonce,
14317                                      const t_u16 NonceLength,
14318                                      t_u8 *Data,
14319                                      t_u16 *DataLength)
14320 {
14321 #if defined(SD8801) || defined(SD8978)
14322     return -WM_E_PERM;
14323 #else
14324     t_u16 aes_gcmp_key_length   = EU_CRYPTO_KEY_MAX_LENGTH;
14325     t_u16 aes_gcmp_AAD_length   = EU_CRYPTO_AAD_MAX_LENGTH;
14326     t_u16 aes_gcmp_Nonce_length = EU_CRYPTO_NONCE_MAX_LENGTH;
14327     t_u16 aes_gcmp_data_length  = EU_CRYPTO_DATA_MAX_LENGTH;
14328 
14329     if (!wlan_is_started())
14330     {
14331         (void)PRINTF("Must enable Wi-Fi firstly\r\n");
14332         return -WM_FAIL;
14333     }
14334 
14335     if (KeyLength > aes_gcmp_key_length)
14336         return -WM_FAIL;
14337     if (AADLength > aes_gcmp_AAD_length)
14338         return -WM_FAIL;
14339     if (NonceLength > aes_gcmp_Nonce_length)
14340         return -WM_FAIL;
14341     if (*DataLength > aes_gcmp_data_length)
14342         return -WM_FAIL;
14343     EU_Crypto Crypto_AES_GCMP_Param;
14344     Crypto_AES_GCMP_Param.AADLength = AADLength;
14345     memcpy(Crypto_AES_GCMP_Param.AAD, AAD, AADLength);
14346     Crypto_AES_GCMP_Param.NonceLength = NonceLength;
14347     memcpy(Crypto_AES_GCMP_Param.Nonce, Nonce, NonceLength);
14348     Crypto_AES_GCMP_Param.KeyLength = KeyLength;
14349     memcpy(Crypto_AES_GCMP_Param.Key, Key, KeyLength);
14350     Crypto_AES_GCMP_Param.DataLength = DataLength;
14351     Crypto_AES_GCMP_Param.Data       = Data;
14352 
14353     t_u16 EncDec = 0;
14354 
14355     return wifi_set_eu_crypto(&Crypto_AES_GCMP_Param, CRYPTO_AES_GCMP, EncDec);
14356 #endif
14357 }
14358 #endif /* CONFIG_WIFI_EU_CRYPTO */
14359 
14360 #if CONFIG_HEAP_DEBUG
wlan_show_os_mem_stat(void)14361 void wlan_show_os_mem_stat(void)
14362 {
14363     wifi_show_os_mem_stat();
14364 }
14365 #endif
14366 
14367 #if CONFIG_MULTI_CHAN
wlan_set_multi_chan_status(const int status)14368 int wlan_set_multi_chan_status(const int status)
14369 {
14370     return wifi_set_mc_policy(status);
14371 }
14372 
wlan_get_multi_chan_status(int * status)14373 int wlan_get_multi_chan_status(int *status)
14374 {
14375     (*status) = wifi_get_mc_policy();
14376     return WM_SUCCESS;
14377 }
14378 
wlan_set_drcs_cfg(const wlan_drcs_cfg_t * drcs_cfg,const int num)14379 int wlan_set_drcs_cfg(const wlan_drcs_cfg_t *drcs_cfg, const int num)
14380 {
14381     return wifi_set_mc_cfg_ext((wifi_drcs_cfg_t *)drcs_cfg, num);
14382 }
14383 
wlan_get_drcs_cfg(wlan_drcs_cfg_t * drcs_cfg,int num)14384 int wlan_get_drcs_cfg(wlan_drcs_cfg_t *drcs_cfg, int num)
14385 {
14386     return wifi_get_mc_cfg_ext((wifi_drcs_cfg_t *)drcs_cfg, num);
14387 }
14388 #endif
14389 
14390 #if CONFIG_WPS2
wlan_set_prov_session(int session)14391 void wlan_set_prov_session(int session)
14392 {
14393     prov_session_attempt = session;
14394     wps_conf.prov_session = session;
14395 }
14396 
wlan_get_prov_session(void)14397 int wlan_get_prov_session(void)
14398 {
14399     return prov_session_attempt;
14400 }
14401 #endif
14402 
14403 #if CONFIG_1AS
wlan_get_fw_timestamp(wlan_correlated_time_t * time)14404 int wlan_get_fw_timestamp(wlan_correlated_time_t *time)
14405 {
14406     return wifi_get_fw_timestamp((wifi_correlated_time_t *)time);
14407 }
14408 
wlan_start_timing_measurement(int bss_type,t_u8 * peer_mac,uint8_t num_of_tm)14409 int wlan_start_timing_measurement(int bss_type, t_u8 *peer_mac, uint8_t num_of_tm)
14410 {
14411     return wifi_start_timing_measurement(bss_type, peer_mac, num_of_tm);
14412 }
14413 
wlan_end_timing_measurement(wlan_dot1as_info_t * info)14414 void wlan_end_timing_measurement(wlan_dot1as_info_t *info)
14415 {
14416     /* Do nothing for now */
14417 }
14418 
wlan_request_timing_measurement(int bss_type,t_u8 * peer_mac,t_u8 trigger)14419 void wlan_request_timing_measurement(int bss_type, t_u8 *peer_mac, t_u8 trigger)
14420 {
14421     wifi_request_timing_measurement(bss_type, peer_mac, trigger);
14422 }
14423 
wlan_report_timing_measurement(wlan_dot1as_info_t * info)14424 void wlan_report_timing_measurement(wlan_dot1as_info_t *info)
14425 {
14426     /* Do nothing for now */
14427 }
14428 #endif
14429 
14430 #if CONFIG_ECSA
14431 
wlan_uap_set_ecsa_cfg(t_u8 block_tx,t_u8 oper_class,t_u8 channel,t_u8 switch_count,t_u8 band_width)14432 int wlan_uap_set_ecsa_cfg(t_u8 block_tx, t_u8 oper_class, t_u8 channel, t_u8 switch_count, t_u8 band_width)
14433 {
14434     t_u8 ecsa = MFALSE;
14435     int ret   = 0;
14436 
14437     ret = wlan_set_uap_ecsa_cfg(block_tx, oper_class, channel, switch_count, band_width, ecsa);
14438 
14439     return ret;
14440 }
14441 
14442 #endif
14443 #if CONFIG_11AX
14444 #if CONFIG_MMSF
wlan_set_mmsf(const t_u8 enable,const t_u8 Density,const t_u8 MMSF)14445 int wlan_set_mmsf(const t_u8 enable, const t_u8 Density, const t_u8 MMSF)
14446 {
14447     return wifi_mmsf_cfg(ACTION_SET, (t_u8 *)&enable, (t_u8 *)&Density, (t_u8 *)&MMSF);
14448 }
14449 
wlan_get_mmsf(t_u8 * enable,t_u8 * Density,t_u8 * MMSF)14450 int wlan_get_mmsf(t_u8 *enable, t_u8 *Density, t_u8 *MMSF)
14451 {
14452     return wifi_mmsf_cfg(ACTION_GET, enable, Density, MMSF);
14453 }
14454 #endif
14455 #endif
14456 
14457 #if CONFIG_WIFI_RECOVERY
wlan_recovery_test(void)14458 int wlan_recovery_test(void)
14459 {
14460     return wifi_recovery_test();
14461 }
14462 #endif
14463 
14464 #if CONFIG_SUBSCRIBE_EVENT_SUPPORT
14465 /**
14466  *  @brief This function subscribe event to firmware.
14467  *
14468  *  @param sbitmap    A pointer to specific event from user.
14469  *  @param thresh_value     A pointer to value from user.
14470  *  @param freq      A pointer to freq from user.
14471  *
14472  *  @return             MLAN_STATUS_SUCCESS, MLAN_STATUS_FAILURE or WM_E_INVAL
14473  */
wlan_set_subscribe_event(unsigned int event_id,unsigned int thresh_value,unsigned int freq)14474 int wlan_set_subscribe_event(unsigned int event_id, unsigned int thresh_value, unsigned int freq)
14475 {
14476     int ret = WM_E_INVAL;
14477     switch (event_id)
14478     {
14479         case EVENT_SUB_RSSI_LOW:
14480             ret = wifi_set_threshold_rssi_low(mlan_adap->priv[0], thresh_value, freq);
14481             break;
14482         case EVENT_SUB_RSSI_HIGH:
14483             ret = wifi_set_threshold_rssi_high(mlan_adap->priv[0], thresh_value, freq);
14484             break;
14485         case EVENT_SUB_SNR_LOW:
14486             ret = wifi_set_threshold_snr_low(mlan_adap->priv[0], thresh_value, freq);
14487             break;
14488         case EVENT_SUB_SNR_HIGH:
14489             ret = wifi_set_threshold_snr_high(mlan_adap->priv[0], thresh_value, freq);
14490             break;
14491         case EVENT_SUB_MAX_FAIL:
14492             ret = wifi_set_threshold_max_fail(mlan_adap->priv[0], thresh_value, freq);
14493             break;
14494         case EVENT_SUB_BEACON_MISSED:
14495             ret = wifi_set_threshold_beacon_miss(mlan_adap->priv[0], thresh_value, freq);
14496             break;
14497         case EVENT_SUB_DATA_RSSI_LOW:
14498             ret = wifi_set_threshold_data_rssi_low(mlan_adap->priv[0], thresh_value, freq);
14499             break;
14500         case EVENT_SUB_DATA_RSSI_HIGH:
14501             ret = wifi_set_threshold_data_rssi_high(mlan_adap->priv[0], thresh_value, freq);
14502             break;
14503         case EVENT_SUB_DATA_SNR_LOW:
14504             ret = wifi_set_threshold_data_snr_low(mlan_adap->priv[0], thresh_value, freq);
14505             break;
14506         case EVENT_SUB_DATA_SNR_HIGH:
14507             ret = wifi_set_threshold_data_snr_high(mlan_adap->priv[0], thresh_value, freq);
14508             break;
14509         case EVENT_SUB_PRE_BEACON_LOST:
14510             ret = wifi_set_threshold_pre_beacon_lost(mlan_adap->priv[0], thresh_value, freq);
14511             break;
14512         default:
14513             ret = WM_E_INVAL;
14514             break;
14515     }
14516     return ret;
14517 }
14518 
wlan_get_subscribe_event(wlan_ds_subscribe_evt * sub_evt)14519 int wlan_get_subscribe_event(wlan_ds_subscribe_evt *sub_evt)
14520 {
14521     int ret = WM_E_INVAL;
14522     mlan_ds_subscribe_evt msub_evt;
14523     memset(&msub_evt, 0, sizeof(msub_evt));
14524     ret = wifi_get_subscribe_event(mlan_adap->priv[0], &msub_evt);
14525     memcpy((t_u8 *)sub_evt, (t_u8 *)&msub_evt.evt_bitmap, sizeof(wlan_ds_subscribe_evt));
14526     return ret;
14527 }
14528 
wlan_clear_subscribe_event(unsigned int event_id)14529 int wlan_clear_subscribe_event(unsigned int event_id)
14530 {
14531     /*bitmap parameter analyse*/
14532     int evt_bitmap = 0;
14533     switch (event_id)
14534     {
14535         case EVENT_SUB_RSSI_LOW:
14536             evt_bitmap = SUBSCRIBE_EVT_RSSI_LOW;
14537             break;
14538         case EVENT_SUB_RSSI_HIGH:
14539             evt_bitmap = SUBSCRIBE_EVT_RSSI_HIGH;
14540             break;
14541         case EVENT_SUB_SNR_LOW:
14542             evt_bitmap = SUBSCRIBE_EVT_SNR_LOW;
14543             break;
14544         case EVENT_SUB_SNR_HIGH:
14545             evt_bitmap = SUBSCRIBE_EVT_SNR_HIGH;
14546             break;
14547         case EVENT_SUB_MAX_FAIL:
14548             evt_bitmap = SUBSCRIBE_EVT_MAX_FAIL;
14549             break;
14550         case EVENT_SUB_BEACON_MISSED:
14551             evt_bitmap = SUBSCRIBE_EVT_BEACON_MISSED;
14552             break;
14553         case EVENT_SUB_DATA_RSSI_LOW:
14554             evt_bitmap = SUBSCRIBE_EVT_DATA_RSSI_LOW;
14555             break;
14556         case EVENT_SUB_DATA_RSSI_HIGH:
14557             evt_bitmap = SUBSCRIBE_EVT_DATA_RSSI_HIGH;
14558             break;
14559         case EVENT_SUB_DATA_SNR_LOW:
14560             evt_bitmap = SUBSCRIBE_EVT_DATA_SNR_LOW;
14561             break;
14562         case EVENT_SUB_DATA_SNR_HIGH:
14563             evt_bitmap = SUBSCRIBE_EVT_DATA_SNR_HIGH;
14564             break;
14565         case EVENT_SUB_LINK_QUALITY:
14566             evt_bitmap = SUBSCRIBE_EVT_LINK_QUALITY;
14567             break;
14568         case EVENT_SUB_PRE_BEACON_LOST:
14569             evt_bitmap = SUBSCRIBE_EVT_PRE_BEACON_LOST;
14570             break;
14571         default:
14572             return WM_E_INVAL;
14573             break;
14574     }
14575     return wifi_clear_subscribe_event(mlan_adap->priv[0], evt_bitmap);
14576 }
14577 
wlan_set_threshold_link_quality(unsigned int event_id,unsigned int link_snr,unsigned int link_snr_freq,unsigned int link_rate,unsigned int link_rate_freq,unsigned int link_tx_latency,unsigned int link_tx_lantency_freq)14578 int wlan_set_threshold_link_quality(unsigned int event_id,
14579                                     unsigned int link_snr,
14580                                     unsigned int link_snr_freq,
14581                                     unsigned int link_rate,
14582                                     unsigned int link_rate_freq,
14583                                     unsigned int link_tx_latency,
14584                                     unsigned int link_tx_lantency_freq)
14585 {
14586     if (event_id == EVENT_SUB_LINK_QUALITY)
14587         return wifi_set_threshold_link_quality(mlan_adap->priv[0], link_snr, link_snr_freq, link_rate, link_rate_freq,
14588                                                link_tx_latency, link_tx_lantency_freq);
14589     return WM_E_INVAL;
14590 }
14591 #endif
14592 
14593 #if CONFIG_WIFI_REG_ACCESS
wlan_reg_access(wifi_reg_t type,uint16_t action,uint32_t offset,uint32_t * value)14594 int wlan_reg_access(wifi_reg_t type, uint16_t action, uint32_t offset, uint32_t *value)
14595 {
14596     return wifi_reg_access(type, action, offset, value);
14597 }
14598 #endif
14599 
14600 #if CONFIG_WMM_UAPSD
14601 static t_u8 uapsd_qos_info             = WMM_UAPSD_QOS_INFO;
14602 static unsigned int uapsd_sleep_period = WMM_UAPSD_SLEEP_PERIOD;
wlan_wmm_uapsd_qosinfo(t_u8 * qos_info,t_u8 action)14603 int wlan_wmm_uapsd_qosinfo(t_u8 *qos_info, t_u8 action)
14604 {
14605     int ret = WM_SUCCESS;
14606 
14607     ret = wifi_wmm_qos_cfg(qos_info, action);
14608     if (ret == WM_SUCCESS && action == ACTION_SET)
14609         uapsd_qos_info = *qos_info;
14610 
14611     return ret;
14612 }
14613 
wlan_set_wmm_uapsd(t_u8 uapsd_enable)14614 int wlan_set_wmm_uapsd(t_u8 uapsd_enable)
14615 {
14616 #if !CONFIG_WNM_PS
14617     unsigned int condition = 0;
14618 #endif
14619 
14620     if (!is_uap_state(CM_UAP_INITIALIZING) || is_sta_connecting())
14621     {
14622         (void)PRINTF("Failed to enable/disable UAPSD, because uAP is up/STA is connecting\n");
14623         return -WM_FAIL;
14624     }
14625 
14626     if (uapsd_enable)
14627     {
14628         (void)wifi_wmm_qos_cfg(&uapsd_qos_info, 1);
14629         (void)wifi_sleep_period(&uapsd_sleep_period, 1);
14630 #if !CONFIG_WNM_PS
14631         (void)wlan_ieeeps_on(condition);
14632 #endif
14633     }
14634     else
14635     {
14636         t_u8 qos_info       = 0;
14637         unsigned int period = 0;
14638         (void)wifi_wmm_qos_cfg(&qos_info, 1);
14639         (void)wifi_sleep_period(&period, 1);
14640         (void)wlan_ieeeps_off();
14641     }
14642 	return WM_SUCCESS;
14643 }
wlan_sleep_period(unsigned int * sleep_period,t_u8 action)14644 int wlan_sleep_period(unsigned int *sleep_period, t_u8 action)
14645 {
14646     int ret = WM_SUCCESS;
14647 
14648     ret = wifi_sleep_period(sleep_period, action);
14649     if (ret == WM_SUCCESS && action == ACTION_SET)
14650         uapsd_sleep_period = *sleep_period;
14651 
14652     return ret;
14653 }
14654 
wlan_is_wmm_uapsd_enabled(void)14655 t_u8 wlan_is_wmm_uapsd_enabled(void)
14656 {
14657     return (mlan_adap ? mlan_adap->pps_uapsd_mode : false);
14658 }
14659 #endif
14660 
14661 #if CONFIG_TX_AMPDU_PROT_MODE
wlan_tx_ampdu_prot_mode(tx_ampdu_prot_mode_para * prot_mode,t_u16 action)14662 int wlan_tx_ampdu_prot_mode(tx_ampdu_prot_mode_para *prot_mode, t_u16 action)
14663 {
14664     return wifi_tx_ampdu_prot_mode(prot_mode, action);
14665 }
14666 #endif
14667 
14668 #if CONFIG_MEF_CFG
wlan_mef_set_auto_arp(t_u8 mef_action)14669 int wlan_mef_set_auto_arp(t_u8 mef_action)
14670 {
14671     int ret, index;
14672     unsigned int ipv4_addr[2];
14673     int ipv4_addr_num = 0;
14674     int filter_num = 0;
14675 
14676     if(!is_sta_ipv4_connected() && !is_uap_started())
14677     {
14678         wlcm_e("No connection on STA and uAP is not activated.");
14679         wlcm_e("Should at least meet one condition.");
14680         return -WM_E_PERM;
14681     }
14682 
14683     if (g_flt_cfg.nentries >= MAX_NUM_ENTRIES)
14684     {
14685         wlcm_e("Number of MEF entries(%d) exceeds limit(8)!", g_flt_cfg.nentries);
14686         return -WM_FAIL;
14687     }
14688 
14689     (void)memset(ipv4_addr, 0x0, sizeof(ipv4_addr));
14690     if(is_sta_ipv4_connected() != 0)
14691     {
14692         ret = wlan_get_ipv4_addr(&ipv4_addr[0]);
14693         if (ret != WM_SUCCESS)
14694         {
14695             wlcm_e("Cannot get STA IP");
14696             return -WM_FAIL;
14697         }
14698         ipv4_addr_num++;
14699     }
14700 #if UAP_SUPPORT
14701     if(is_uap_started() != 0)
14702     {
14703         ret = wlan_get_uap_ipv4_addr(&ipv4_addr[1]);
14704         if (ret != WM_SUCCESS)
14705         {
14706             wlcm_e("Cannot get UAP IP");
14707             return -WM_FAIL;
14708         }
14709         ipv4_addr_num++;
14710     }
14711 #endif
14712     index = g_flt_cfg.nentries;
14713     g_flt_cfg.criteria |= (CRITERIA_BROADCAST | CRITERIA_UNICAST);
14714     g_flt_cfg.nentries++;
14715 
14716     g_flt_cfg.mef_entry[index].mode                        = MEF_MODE_HOST_SLEEP;
14717     g_flt_cfg.mef_entry[index].action                      = (MEF_AUTO_ARP | (mef_action & 0xF));
14718     g_flt_cfg.mef_entry[index].filter_num                  = 1;
14719     g_flt_cfg.mef_entry[index].filter_item[0].type         = TYPE_BYTE_EQ;
14720     g_flt_cfg.mef_entry[index].filter_item[0].repeat       = 1;
14721     g_flt_cfg.mef_entry[index].filter_item[0].offset       = IPV4_PKT_OFFSET;
14722     g_flt_cfg.mef_entry[index].filter_item[0].num_byte_seq = 2;
14723     (void)memcpy(g_flt_cfg.mef_entry[index].filter_item[0].byte_seq, "\x08\x06", 2);
14724     g_flt_cfg.mef_entry[index].rpn[1] = RPN_TYPE_AND;
14725 
14726     if(is_sta_ipv4_connected() != 0)
14727     {
14728         g_flt_cfg.mef_entry[index].filter_num++;
14729         filter_num = g_flt_cfg.mef_entry[index].filter_num;
14730         g_flt_cfg.mef_entry[index].filter_item[filter_num - 1].type         = TYPE_BYTE_EQ;
14731         g_flt_cfg.mef_entry[index].filter_item[filter_num - 1].repeat       = 1;
14732         g_flt_cfg.mef_entry[index].filter_item[filter_num - 1].offset       = 46;
14733         g_flt_cfg.mef_entry[index].filter_item[filter_num - 1].num_byte_seq = 4;
14734         (void)memcpy(g_flt_cfg.mef_entry[index].filter_item[filter_num - 1].byte_seq,
14735                      &ipv4_addr[0], 4); // STA IP address
14736     }
14737 
14738     if(is_uap_started() != 0)
14739     {
14740         g_flt_cfg.mef_entry[index].filter_num++;
14741         filter_num = g_flt_cfg.mef_entry[index].filter_num;
14742         if(ipv4_addr_num == 2)
14743             g_flt_cfg.mef_entry[index].rpn[filter_num] = RPN_TYPE_OR;
14744         g_flt_cfg.mef_entry[index].filter_item[filter_num - 1].type         = TYPE_BYTE_EQ;
14745         g_flt_cfg.mef_entry[index].filter_item[filter_num - 1].repeat       = 1;
14746         g_flt_cfg.mef_entry[index].filter_item[filter_num - 1].offset       = 46;
14747         g_flt_cfg.mef_entry[index].filter_item[filter_num - 1].num_byte_seq = 4;
14748         (void)memcpy(g_flt_cfg.mef_entry[index].filter_item[filter_num - 1].byte_seq,
14749                      &ipv4_addr[1], 4); // UAP IP address
14750     }
14751 
14752     return WM_SUCCESS;
14753 }
14754 
wlan_mef_set_auto_ping(t_u8 mef_action)14755 int wlan_mef_set_auto_ping(t_u8 mef_action)
14756 {
14757     int ret, index;
14758     unsigned int ipv4_addr[2];
14759     int ipv4_addr_num = 0;
14760     int filter_num = 0;
14761 
14762     if(!is_sta_ipv4_connected() && !is_uap_started())
14763     {
14764         wlcm_e("No connection on STA and uAP is not activated.");
14765         wlcm_e("Should at least meet one condition.");
14766         return -WM_E_PERM;
14767     }
14768 
14769     if (g_flt_cfg.nentries >= MAX_NUM_ENTRIES)
14770     {
14771         wlcm_e("Number of MEF entries(%d) exceeds limit(8)!", g_flt_cfg.nentries);
14772         return -WM_FAIL;
14773     }
14774 
14775     (void)memset(ipv4_addr, 0x0, sizeof(ipv4_addr));
14776     if(is_sta_ipv4_connected() != 0)
14777     {
14778         ret = wlan_get_ipv4_addr(&ipv4_addr[0]);
14779         if (ret != WM_SUCCESS)
14780         {
14781             wlcm_e("Cannot get STA IP");
14782             return -WM_FAIL;
14783         }
14784         ipv4_addr_num++;
14785     }
14786 #if UAP_SUPPORT
14787     if(is_uap_started() != 0)
14788     {
14789         ret = wlan_get_uap_ipv4_addr(&ipv4_addr[1]);
14790         if (ret != WM_SUCCESS)
14791         {
14792             wlcm_e("Cannot get UAP IP");
14793             return -WM_FAIL;
14794         }
14795         ipv4_addr_num++;
14796     }
14797 #endif
14798     index = g_flt_cfg.nentries;
14799     g_flt_cfg.criteria |= (CRITERIA_BROADCAST | CRITERIA_UNICAST);
14800     g_flt_cfg.nentries++;
14801     g_flt_cfg.mef_entry[index].mode                        = MEF_MODE_HOST_SLEEP;
14802     g_flt_cfg.mef_entry[index].action                      = (MEF_AUTO_PING | (mef_action & 0xF));
14803     g_flt_cfg.mef_entry[index].filter_num                  = 2;
14804     g_flt_cfg.mef_entry[index].filter_item[0].type         = TYPE_BYTE_EQ;
14805     g_flt_cfg.mef_entry[index].filter_item[0].repeat       = 1;
14806     g_flt_cfg.mef_entry[index].filter_item[0].offset       = IPV4_PKT_OFFSET;
14807     g_flt_cfg.mef_entry[index].filter_item[0].num_byte_seq = 2;
14808     (void)memcpy(g_flt_cfg.mef_entry[index].filter_item[0].byte_seq, "\x08\x00", 2);
14809     g_flt_cfg.mef_entry[index].rpn[1] = RPN_TYPE_AND;
14810 
14811     g_flt_cfg.mef_entry[index].filter_item[1].type      = TYPE_DNUM_EQ;
14812     g_flt_cfg.mef_entry[index].filter_item[1].pattern   = ICMP_OF_IP_PROTOCOL;
14813     g_flt_cfg.mef_entry[index].filter_item[1].offset    = IP_PROTOCOL_OFFSET;
14814     g_flt_cfg.mef_entry[index].filter_item[1].num_bytes = 1;
14815     g_flt_cfg.mef_entry[index].rpn[2]                   = RPN_TYPE_AND;
14816 
14817     if(is_sta_ipv4_connected() != 0)
14818     {
14819         g_flt_cfg.mef_entry[index].filter_num++;
14820         filter_num = g_flt_cfg.mef_entry[index].filter_num;
14821         g_flt_cfg.mef_entry[index].filter_item[filter_num - 1].type         = TYPE_BYTE_EQ;
14822         g_flt_cfg.mef_entry[index].filter_item[filter_num - 1].repeat       = 1;
14823         g_flt_cfg.mef_entry[index].filter_item[filter_num - 1].offset       = 38;
14824         g_flt_cfg.mef_entry[index].filter_item[filter_num - 1].num_byte_seq = 4;
14825         (void)memcpy(g_flt_cfg.mef_entry[index].filter_item[filter_num - 1].byte_seq,
14826                      &ipv4_addr[0], 4); //STA IP address
14827     }
14828 
14829     if(is_uap_started() != 0)
14830     {
14831         g_flt_cfg.mef_entry[index].filter_num++;
14832         filter_num = g_flt_cfg.mef_entry[index].filter_num;
14833         if(ipv4_addr_num == 2)
14834             g_flt_cfg.mef_entry[index].rpn[filter_num - 1] = RPN_TYPE_OR;
14835         g_flt_cfg.mef_entry[index].filter_item[filter_num - 1].type         = TYPE_BYTE_EQ;
14836         g_flt_cfg.mef_entry[index].filter_item[filter_num - 1].repeat       = 1;
14837         g_flt_cfg.mef_entry[index].filter_item[filter_num - 1].offset       = 38;
14838         g_flt_cfg.mef_entry[index].filter_item[filter_num - 1].num_byte_seq = 4;
14839         (void)memcpy(g_flt_cfg.mef_entry[index].filter_item[filter_num - 1].byte_seq,
14840                      &ipv4_addr[1], 4); // UAP IP address
14841     }
14842 
14843     return WM_SUCCESS;
14844 }
14845 
wlan_set_ipv6_ns_mef(t_u8 mef_action)14846 int wlan_set_ipv6_ns_mef(t_u8 mef_action)
14847 {
14848 	int index;
14849 
14850     if (g_flt_cfg.nentries >= MAX_NUM_ENTRIES)
14851     {
14852         wlcm_e("Number of MEF entries(%d) exceeds limit(8)!", g_flt_cfg.nentries);
14853         return -WM_FAIL;
14854     }
14855 
14856     index = g_flt_cfg.nentries;
14857     g_flt_cfg.criteria |= (CRITERIA_UNICAST | CRITERIA_MULTICAST);
14858     g_flt_cfg.nentries++;
14859     g_flt_cfg.mef_entry[index].mode = MEF_MODE_HOST_SLEEP;
14860     g_flt_cfg.mef_entry[index].action = (MEF_NS_RESP| (mef_action & 0xF));
14861     g_flt_cfg.mef_entry[index].filter_num = 2;
14862 
14863 	g_flt_cfg.mef_entry[index].filter_item[0].fill_flag = (FILLING_TYPE | FILLING_REPEAT | FILLING_OFFSET | FILLING_BYTE_SEQ);
14864     g_flt_cfg.mef_entry[index].filter_item[0].type         = TYPE_BYTE_EQ;
14865     g_flt_cfg.mef_entry[index].filter_item[0].repeat       = 1;
14866     g_flt_cfg.mef_entry[index].filter_item[0].offset       = IPV4_PKT_OFFSET;
14867     g_flt_cfg.mef_entry[index].filter_item[0].num_byte_seq = 2;
14868     (void)memcpy(g_flt_cfg.mef_entry[index].filter_item[0].byte_seq, "\x86\xdd", 2);
14869     g_flt_cfg.mef_entry[index].rpn[1] = RPN_TYPE_AND;
14870 
14871 	g_flt_cfg.mef_entry[index].filter_item[1].fill_flag = (FILLING_TYPE | FILLING_REPEAT | FILLING_OFFSET | FILLING_BYTE_SEQ);
14872     g_flt_cfg.mef_entry[index].filter_item[1].type         = TYPE_BYTE_EQ;
14873     g_flt_cfg.mef_entry[index].filter_item[1].repeat       = 1;
14874     g_flt_cfg.mef_entry[index].filter_item[1].offset       = 62;
14875     g_flt_cfg.mef_entry[index].filter_item[1].num_byte_seq = 1;
14876     (void)memcpy(g_flt_cfg.mef_entry[index].filter_item[1].byte_seq, "\x87", 1);
14877 
14878     return WM_SUCCESS;
14879 }
14880 
wlan_mef_set_multicast(t_u8 mef_action)14881 int wlan_mef_set_multicast(t_u8 mef_action)
14882 {
14883     t_u32 index = 0;
14884 
14885     if (g_flt_cfg.nentries >= MAX_NUM_ENTRIES)
14886     {
14887         wlcm_e("Number of MEF entries(%d) exceeds limit(8)!", g_flt_cfg.nentries);
14888         return -WM_FAIL;
14889     }
14890     index = g_flt_cfg.nentries;
14891     g_flt_cfg.criteria |= (CRITERIA_MULTICAST | CRITERIA_UNICAST);
14892     g_flt_cfg.nentries++;
14893 
14894     g_flt_cfg.mef_entry[index].mode                        = MEF_MODE_HOST_SLEEP;
14895     g_flt_cfg.mef_entry[index].action                      = mef_action;
14896     g_flt_cfg.mef_entry[index].filter_num                  = 2;
14897     g_flt_cfg.mef_entry[index].filter_item[0].type         = TYPE_BIT_EQ;
14898     g_flt_cfg.mef_entry[index].filter_item[0].offset       = 0;
14899     g_flt_cfg.mef_entry[index].filter_item[0].num_byte_seq = 1;
14900     g_flt_cfg.mef_entry[index].filter_item[0].byte_seq[0]  = 0x01;
14901     g_flt_cfg.mef_entry[index].filter_item[0].num_mask_seq = 1;
14902     g_flt_cfg.mef_entry[index].filter_item[0].mask_seq[0]  = 0x01;
14903     g_flt_cfg.mef_entry[index].rpn[1]                      = RPN_TYPE_OR;
14904 
14905     g_flt_cfg.mef_entry[index].filter_item[1].type         = TYPE_BIT_EQ;
14906     g_flt_cfg.mef_entry[index].filter_item[1].offset       = 38;
14907     g_flt_cfg.mef_entry[index].filter_item[1].num_byte_seq = 1;
14908     g_flt_cfg.mef_entry[index].filter_item[1].byte_seq[0]  = 0xE0;
14909     g_flt_cfg.mef_entry[index].filter_item[1].num_mask_seq = 1;
14910     g_flt_cfg.mef_entry[index].filter_item[1].mask_seq[0]  = 0xF0;
14911 
14912     return WM_SUCCESS;
14913 }
14914 
wlan_config_mef(int type,t_u8 mef_action)14915 int wlan_config_mef(int type, t_u8 mef_action)
14916 {
14917     int ret = -WM_FAIL;
14918 
14919     if (!wlan_is_started())
14920     {
14921         (void)PRINTF("MEF configure is not allowed when WIFI is disabled\r\n");
14922         return -WM_FAIL;
14923     }
14924 
14925     switch (type)
14926     {
14927         case MEF_TYPE_DELETE:
14928             (void)memset(&g_flt_cfg, 0, sizeof(wlan_flt_cfg_t));
14929             ret = wifi_set_packet_filters(&g_flt_cfg);
14930             if(ret == WM_SUCCESS)
14931                 (void)PRINTF("delete all MEF entries Successful\n\r");
14932             else
14933                 (void)PRINTF("delete all MEF entries Failed\n\r");
14934             break;
14935         case MEF_TYPE_PING:
14936             ret = wlan_mef_set_auto_ping(mef_action);
14937             if (ret == WM_SUCCESS)
14938                 (void)PRINTF("Add ping MEF entry successful\n\r");
14939             else
14940                 (void)PRINTF("Add ping MEF entry Failed\n\r");
14941             break;
14942         case MEF_TYPE_ARP:
14943             ret = wlan_mef_set_auto_arp(mef_action);
14944             if (ret == WM_SUCCESS)
14945             {
14946                 (void)PRINTF("Add ARP MEF entry successful\n\r");
14947             }
14948             else
14949                 (void)PRINTF("Add ARP MEF entry Failed\n\r");
14950             break;
14951         case MEF_TYPE_MULTICAST:
14952             ret = wlan_mef_set_multicast(mef_action);
14953             if (ret == WM_SUCCESS)
14954                 (void)PRINTF("Add multicast MEF entry successful\n\r");
14955             else
14956                 (void)PRINTF("Add multicast MEF entry Failed\n\r");
14957             break;
14958         case MEF_TYPE_IPV6_NS:
14959             ret = wlan_set_ipv6_ns_mef(mef_action);
14960             if (ret == WM_SUCCESS)
14961                 (void)PRINTF("Add ns MEF entry successful\n\r");
14962             else
14963                 (void)PRINTF("Add ns MEF entry Failed\n\r");
14964             break;
14965         default:
14966             (void)PRINTF("Error: unknown MEF type:%d", type);
14967             break;
14968     }
14969 
14970     return ret;
14971 }
14972 #endif
14973 
14974 #if CONFIG_CSI
wlan_register_csi_user_callback(int (* csi_data_recv_callback)(void * buffer,size_t len))14975 int wlan_register_csi_user_callback(int (*csi_data_recv_callback)(void *buffer, size_t len))
14976 {
14977     return register_csi_user_callback(csi_data_recv_callback);
14978 }
14979 
wlan_unregister_csi_user_callback(void)14980 int wlan_unregister_csi_user_callback(void)
14981 {
14982     return unregister_csi_user_callback();
14983 }
14984 
wlan_csi_cfg(wlan_csi_config_params_t * csi_params)14985 int wlan_csi_cfg(wlan_csi_config_params_t *csi_params)
14986 {
14987     int ret = WM_SUCCESS;
14988 
14989     if (csi_params->csi_enable == 1)
14990     {
14991         csi_local_buff_init();
14992     }
14993     else
14994     {
14995         ret = unregister_csi_user_callback();
14996     }
14997 
14998     ret = wifi_csi_cfg(csi_params);
14999 
15000     return ret;
15001 }
15002 #endif
15003 
15004 #if (CONFIG_11K) || (CONFIG_11V) || (CONFIG_11R) || (CONFIG_ROAMING)
wlan_set_rssi_low_threshold(uint8_t threshold)15005 void wlan_set_rssi_low_threshold(uint8_t threshold)
15006 {
15007     wlan.rssi_low_threshold = threshold;
15008 
15009     if (is_sta_connected())
15010     {
15011 #if CONFIG_ROAMING
15012         if (wlan.roaming_enabled == true)
15013         {
15014             (void)wifi_config_roaming(true, &wlan.rssi_low_threshold);
15015         }
15016         else
15017 #endif
15018         {
15019             (void)wifi_set_rssi_low_threshold(&wlan.rssi_low_threshold);
15020         }
15021     }
15022 }
15023 #endif
15024 
15025 #if CONFIG_WPA_SUPP
15026 #if CONFIG_WPA_SUPP_WPS
wlan_remove_wps_network(void)15027 static int wlan_remove_wps_network(void)
15028 {
15029     unsigned int len, i;
15030     int ret = -WM_E_INVAL;
15031     struct netif *netif = net_get_sta_interface();
15032 
15033     ret = wpa_supp_cancel_scan(netif);
15034     /* find the first network whose name matches and clear it out */
15035     for (i = 0; i < ARRAY_SIZE(wlan.networks); i++)
15036     {
15037         if (wlan.networks[i].wps_network)
15038             ret = wpa_supp_remove_network(netif, &wlan.networks[i]);
15039     }
15040     return ret;
15041 }
15042 
wlan_start_wps_pbc(void)15043 int wlan_start_wps_pbc(void)
15044 {
15045     int ret = -WM_FAIL;
15046     struct netif *netif = net_get_sta_interface();
15047 
15048     if (wlan.wps_session_attempt)
15049     {
15050         wlcm_d("WPS session is already in progress");
15051         return ret;
15052     }
15053     wlan_remove_wps_network();
15054     ret = wpa_supp_start_wps_pbc(netif, 0);
15055 
15056     if (ret == -2)
15057     {
15058         wlcm_e("WPS PBC overlap detected");
15059     }
15060     return ret;
15061 }
15062 
wlan_wps_generate_pin(uint32_t * pin)15063 void wlan_wps_generate_pin(uint32_t *pin)
15064 {
15065     struct netif *netif = net_get_sta_interface();
15066 
15067     wpa_supp_wps_generate_pin(netif, (unsigned int *)pin);
15068 }
15069 
wlan_start_wps_pin(const char * pin)15070 int wlan_start_wps_pin(const char *pin)
15071 {
15072     struct netif *netif = net_get_sta_interface();
15073 
15074     if (wlan.wps_session_attempt)
15075     {
15076         wlcm_d("WPS session is already in progress");
15077         return -WM_FAIL;
15078     }
15079 
15080     if (wpa_supp_wps_pin_valid(netif, (const unsigned char *)pin) != WM_SUCCESS)
15081     {
15082         wlcm_d("WPS PIN validation failed for %s", pin);
15083         return -WM_FAIL;
15084     }
15085     wlan_remove_wps_network();
15086     return wpa_supp_start_wps_pin(netif, pin, 0);
15087 }
15088 
wlan_wps_cancel(void)15089 int wlan_wps_cancel(void)
15090 {
15091     struct netif *netif = net_get_sta_interface();
15092 
15093     if (wlan.wps_session_attempt == 0)
15094     {
15095         return WM_SUCCESS;
15096     }
15097 
15098     return wpa_supp_cancel_wps(netif, 0);
15099 }
15100 
15101 #if CONFIG_WPA_SUPP_AP
wlan_start_ap_wps_pbc(void)15102 int wlan_start_ap_wps_pbc(void)
15103 {
15104     struct netif *netif = net_get_uap_interface();
15105 
15106     if (is_uap_started() == 0)
15107     {
15108         wlcm_e("Cannot Start WPS PBC as uAP is not running");
15109         return -WM_FAIL;
15110     }
15111 
15112     return wpa_supp_start_wps_pbc(netif, 1);
15113 }
15114 
wlan_start_ap_wps_pin(const char * pin)15115 int wlan_start_ap_wps_pin(const char *pin)
15116 {
15117     struct netif *netif = net_get_uap_interface();
15118 
15119     if (is_uap_started() == 0)
15120     {
15121         wlcm_e("Cannot Start WPS PIN as uAP is not running");
15122         return -WM_FAIL;
15123     }
15124 
15125     if (wpa_supp_wps_pin_valid(netif, (const unsigned char *)pin) != WM_SUCCESS)
15126     {
15127         wlcm_d("WPS PIN validation failed for %s", pin);
15128         return -WM_FAIL;
15129     }
15130 
15131     return wpa_supp_start_wps_pin(netif, pin, 1);
15132 }
15133 
wlan_wps_ap_cancel(void)15134 int wlan_wps_ap_cancel(void)
15135 {
15136     struct netif *netif = net_get_uap_interface();
15137 
15138     return wpa_supp_cancel_wps(netif, 1);
15139 }
15140 #endif
15141 #endif
15142 #endif
15143 
15144 #if (CONFIG_WPA2_ENTP) || (CONFIG_WPA_SUPP_CRYPTO_ENTERPRISE)
15145 #if CONFIG_WIFI_USB_FILE_ACCESS
wlan_entp_cert_cleanup()15146 static void wlan_entp_cert_cleanup()
15147 {
15148     if (wlan.ca_cert_data != NULL)
15149     {
15150         OSA_MemoryFree(wlan.ca_cert_data);
15151     }
15152     if (wlan.client_cert_data != NULL)
15153     {
15154         OSA_MemoryFree(wlan.client_cert_data);
15155     }
15156     if (wlan.client_key_data != NULL)
15157     {
15158         OSA_MemoryFree(wlan.client_key_data);
15159     }
15160     if (wlan.ca_cert2_data != NULL)
15161     {
15162         OSA_MemoryFree(wlan.ca_cert2_data);
15163     }
15164     if (wlan.client_cert2_data != NULL)
15165     {
15166         OSA_MemoryFree(wlan.client_cert2_data);
15167     }
15168     if (wlan.client_key2_data != NULL)
15169     {
15170         OSA_MemoryFree(wlan.client_key2_data);
15171     }
15172 
15173 #if CONFIG_WPA_SUPP_AP
15174 #if CONFIG_WPA_SUPP_CRYPTO_AP_ENTERPRISE
15175     if (wlan.dh_data != NULL)
15176     {
15177         OSA_MemoryFree(wlan.dh_data);
15178     }
15179     if (wlan.server_cert_data != NULL)
15180     {
15181         OSA_MemoryFree(wlan.server_cert_data);
15182     }
15183     if (wlan.server_key_data != NULL)
15184     {
15185         OSA_MemoryFree(wlan.server_key_data);
15186     }
15187 #endif
15188 #endif
15189 }
15190 
wlan_set_entp_cert_files(int cert_type,t_u8 * data,t_u32 data_len)15191 int wlan_set_entp_cert_files(int cert_type, t_u8 *data, t_u32 data_len)
15192 {
15193     if (cert_type == FILE_TYPE_ENTP_CA_CERT)
15194     {
15195         wlan.ca_cert_data = OSA_MemoryAllocate(data_len);
15196         if (!wlan.ca_cert_data)
15197         {
15198             wlan_entp_cert_cleanup();
15199             wlcm_e("CA Cert malloc failed");
15200             return -WM_FAIL;
15201         }
15202         memcpy(wlan.ca_cert_data, data, data_len);
15203         wlan.ca_cert_len = data_len;
15204     }
15205     else if (cert_type == FILE_TYPE_ENTP_CLIENT_CERT)
15206     {
15207         wlan.client_cert_data = OSA_MemoryAllocate(data_len);
15208         if (!wlan.client_cert_data)
15209         {
15210             wlan_entp_cert_cleanup();
15211             wlcm_e("Client Cert malloc failed");
15212             return -WM_FAIL;
15213         }
15214         memcpy(wlan.client_cert_data, data, data_len);
15215         wlan.client_cert_len = data_len;
15216     }
15217     else if (cert_type == FILE_TYPE_ENTP_CLIENT_KEY)
15218     {
15219         wlan.client_key_data = OSA_MemoryAllocate(data_len);
15220         if (!wlan.client_key_data)
15221         {
15222             wlan_entp_cert_cleanup();
15223             wlcm_e("Client Key malloc failed");
15224             return -WM_FAIL;
15225         }
15226         memcpy(wlan.client_key_data, data, data_len);
15227         wlan.client_key_len = data_len;
15228     }
15229     else if (cert_type == FILE_TYPE_ENTP_CA_CERT2)
15230     {
15231         wlan.ca_cert2_data = OSA_MemoryAllocate(data_len);
15232         if (!wlan.ca_cert2_data)
15233         {
15234             wlan_entp_cert_cleanup();
15235             wlcm_e("CA Cert2 malloc failed");
15236             return -WM_FAIL;
15237         }
15238         memcpy(wlan.ca_cert2_data, data, data_len);
15239         wlan.ca_cert2_len = data_len;
15240     }
15241     else if (cert_type == FILE_TYPE_ENTP_CLIENT_CERT2)
15242     {
15243         wlan.client_cert2_data = OSA_MemoryAllocate(data_len);
15244         if (!wlan.client_cert2_data)
15245         {
15246             wlan_entp_cert_cleanup();
15247             wlcm_e("Client Cert2 malloc failed");
15248             return -WM_FAIL;
15249         }
15250         memcpy(wlan.client_cert2_data, data, data_len);
15251         wlan.client_cert2_len = data_len;
15252     }
15253     else if (cert_type == FILE_TYPE_ENTP_CLIENT_KEY2)
15254     {
15255         wlan.client_key2_data = OSA_MemoryAllocate(data_len);
15256         if (!wlan.client_key2_data)
15257         {
15258             wlan_entp_cert_cleanup();
15259             wlcm_e("Client Key2 malloc failed");
15260             return -WM_FAIL;
15261         }
15262         memcpy(wlan.client_key2_data, data, data_len);
15263         wlan.client_key2_len = data_len;
15264     }
15265 #if CONFIG_WPA_SUPP_AP
15266 #if CONFIG_WPA_SUPP_CRYPTO_AP_ENTERPRISE
15267     else if (cert_type == FILE_TYPE_ENTP_DH_PARAMS)
15268     {
15269         wlan.dh_data = OSA_MemoryAllocate(data_len);
15270         if (!wlan.dh_data)
15271         {
15272             wlan_entp_cert_cleanup();
15273             wlcm_e("DH params malloc failed");
15274             return -WM_FAIL;
15275         }
15276         memcpy(wlan.dh_data, data, data_len);
15277         wlan.dh_len = data_len;
15278     }
15279 #endif
15280 #endif
15281 #if CONFIG_WPA_SUPP_AP
15282 #if CONFIG_WPA_SUPP_CRYPTO_AP_ENTERPRISE
15283     else if (cert_type == FILE_TYPE_ENTP_SERVER_CERT)
15284     {
15285         wlan.server_cert_data = OSA_MemoryAllocate(data_len);
15286         if (!wlan.server_cert_data)
15287         {
15288             wlan_entp_cert_cleanup();
15289             wlcm_e("Server Cert malloc failed");
15290             return -WM_FAIL;
15291         }
15292         memcpy(wlan.server_cert_data, data, data_len);
15293         wlan.server_cert_len = data_len;
15294     }
15295     else if (cert_type == FILE_TYPE_ENTP_SERVER_KEY)
15296     {
15297         wlan.server_key_data = OSA_MemoryAllocate(data_len);
15298         if (!wlan.server_key_data)
15299         {
15300             wlan_entp_cert_cleanup();
15301             wlcm_e("Server Key malloc failed");
15302             return -WM_FAIL;
15303         }
15304         memcpy(wlan.server_key_data, data, data_len);
15305         wlan.server_key_len = data_len;
15306     }
15307 #endif
15308 #endif
15309 #if CONFIG_WPA_SUPP_AP
15310 #if CONFIG_WPA_SUPP_CRYPTO_AP_ENTERPRISE
15311     else if (cert_type == FILE_TYPE_ENTP_SERVER_CERT)
15312     {
15313         wlan.server_cert_data = OSA_MemoryAllocate(data_len);
15314         if (!wlan.server_cert_data)
15315         {
15316             wlan_entp_cert_cleanup();
15317             wlcm_e("Server Cert malloc failed");
15318             return -WM_FAIL;
15319         }
15320         memcpy(wlan.server_cert_data, data, data_len);
15321         wlan.server_cert_len = data_len;
15322     }
15323     else if (cert_type == FILE_TYPE_ENTP_SERVER_KEY)
15324     {
15325         wlan.server_key_data = OSA_MemoryAllocate(data_len);
15326         if (!wlan.server_key_data)
15327         {
15328             wlan_entp_cert_cleanup();
15329             wlcm_e("Server Key malloc failed");
15330             return -WM_FAIL;
15331         }
15332         memcpy(wlan.server_key_data, data, data_len);
15333         wlan.server_key_len = data_len;
15334     }
15335     else if (cert_type == FILE_TYPE_ENTP_DH_PARAMS)
15336     {
15337         wlan.dh_data = OSA_MemoryAllocate(data_len);
15338         if (!wlan.dh_data)
15339         {
15340             wlan_entp_cert_cleanup();
15341             wlcm_e("DH params malloc failed");
15342             return -WM_FAIL;
15343         }
15344         memcpy(wlan.dh_data, data, data_len);
15345         wlan.dh_len = data_len;
15346     }
15347 #endif
15348 #endif
15349     else
15350     {
15351         wlcm_e("Invalid file type");
15352         return -WM_FAIL;
15353     }
15354 
15355     return WM_SUCCESS;
15356 }
15357 #endif
15358 
wlan_get_entp_cert_files(int cert_type,t_u8 ** data)15359 t_u32 wlan_get_entp_cert_files(int cert_type, t_u8 **data)
15360 {
15361     int len = 0;
15362     if (cert_type == FILE_TYPE_ENTP_CA_CERT)
15363     {
15364         *data = wlan.ca_cert_data;
15365         len   = wlan.ca_cert_len;
15366 #if !CONFIG_WIFI_USB_FILE_ACCESS
15367         if (!wlan.ca_cert_data)
15368         {
15369             *data = (t_u8 *)ca_der;
15370             len   = ca_der_len;
15371         }
15372 #endif
15373         wlan.ca_cert_data = NULL;
15374     }
15375     else if (cert_type == FILE_TYPE_ENTP_CLIENT_CERT)
15376     {
15377         *data = wlan.client_cert_data;
15378         len   = wlan.client_cert_len;
15379 #if !CONFIG_WIFI_USB_FILE_ACCESS
15380         if (!wlan.client_cert_data)
15381         {
15382             *data = (t_u8 *)client_der;
15383             len   = client_der_len;
15384         }
15385 #endif
15386         wlan.client_cert_data = NULL;
15387     }
15388     else if (cert_type == FILE_TYPE_ENTP_CLIENT_KEY)
15389     {
15390         *data = wlan.client_key_data;
15391         len   = wlan.client_key_len;
15392 #if !CONFIG_WIFI_USB_FILE_ACCESS
15393         if (!wlan.client_key_data)
15394         {
15395             *data = (t_u8 *)client_key_der;
15396             len   = client_key_der_len;
15397         }
15398 #endif
15399         wlan.client_key_data = NULL;
15400     }
15401     if (cert_type == FILE_TYPE_ENTP_CA_CERT2)
15402     {
15403         *data = wlan.ca_cert2_data;
15404         len   = wlan.ca_cert2_len;
15405 #if !CONFIG_WIFI_USB_FILE_ACCESS
15406         if (!wlan.ca_cert2_data)
15407         {
15408             *data = (t_u8 *)ca_der;
15409             len   = ca_der_len;
15410         }
15411 #endif
15412         wlan.ca_cert2_data = NULL;
15413     }
15414     else if (cert_type == FILE_TYPE_ENTP_CLIENT_CERT2)
15415     {
15416         *data = wlan.client_cert2_data;
15417         len   = wlan.client_cert2_len;
15418 #if !CONFIG_WIFI_USB_FILE_ACCESS
15419         if (!wlan.client_cert2_data)
15420         {
15421             *data = (t_u8 *)client_der;
15422             len   = client_der_len;
15423         }
15424 #endif
15425         wlan.client_cert2_data = NULL;
15426     }
15427     else if (cert_type == FILE_TYPE_ENTP_CLIENT_KEY2)
15428     {
15429         *data = wlan.client_key2_data;
15430         len   = wlan.client_key2_len;
15431 #if !CONFIG_WIFI_USB_FILE_ACCESS
15432         if (!wlan.client_key2_data)
15433         {
15434             *data = (t_u8 *)client_key_der;
15435             len   = client_key_der_len;
15436         }
15437 #endif
15438         wlan.client_key2_data = NULL;
15439     }
15440 #if CONFIG_WPA_SUPP_AP
15441 #if CONFIG_WPA_SUPP_CRYPTO_AP_ENTERPRISE
15442     else if (cert_type == FILE_TYPE_ENTP_DH_PARAMS)
15443     {
15444         *data = wlan.dh_data;
15445         len   = wlan.dh_len;
15446 #if !CONFIG_WIFI_USB_FILE_ACCESS
15447         if (!wlan.dh_data)
15448         {
15449             *data = (t_u8 *)dh_der;
15450             len   = dh_der_len;
15451         }
15452 #endif
15453         wlan.dh_data = NULL;
15454     }
15455 #endif
15456 #endif
15457 #if CONFIG_WPA_SUPP_AP
15458 #if CONFIG_WPA_SUPP_CRYPTO_AP_ENTERPRISE
15459     else if (cert_type == FILE_TYPE_ENTP_SERVER_CERT)
15460     {
15461         *data = wlan.server_cert_data;
15462         len   = wlan.server_cert_len;
15463 #if !CONFIG_WIFI_USB_FILE_ACCESS
15464         if (!wlan.server_cert_data)
15465         {
15466             *data = (t_u8 *)server_der;
15467             len   = server_der_len;
15468         }
15469 #endif
15470         wlan.server_cert_data = NULL;
15471     }
15472     else if (cert_type == FILE_TYPE_ENTP_SERVER_KEY)
15473     {
15474         *data = wlan.server_key_data;
15475         len   = wlan.server_key_len;
15476 #if !CONFIG_WIFI_USB_FILE_ACCESS
15477         if (!wlan.server_key_data)
15478         {
15479             *data = (t_u8 *)server_key_der;
15480             len   = server_key_der_len;
15481         }
15482 #endif
15483         wlan.server_key_data = NULL;
15484     }
15485 #endif
15486 #endif
15487 
15488     return len;
15489 }
15490 
wlan_free_entp_cert_files(void)15491 void wlan_free_entp_cert_files(void)
15492 {
15493 #if CONFIG_WIFI_USB_FILE_ACCESS
15494     if (wlan.ca_cert_data != NULL)
15495     {
15496         wlan.ca_cert_data = NULL;
15497         wlan.ca_cert_len  = 0;
15498     }
15499     if (wlan.client_cert_data != NULL)
15500     {
15501         wlan.client_cert_data = NULL;
15502         wlan.client_cert_len  = 0;
15503     }
15504     if (wlan.client_key_data != NULL)
15505     {
15506         wlan.client_key_data = NULL;
15507         wlan.client_key_len  = 0;
15508     }
15509     if (wlan.ca_cert2_data != NULL)
15510     {
15511         wlan.ca_cert2_data = NULL;
15512         wlan.ca_cert2_len  = 0;
15513     }
15514     if (wlan.client_cert2_data != NULL)
15515     {
15516         wlan.client_cert2_data = NULL;
15517         wlan.client_cert2_len  = 0;
15518     }
15519     if (wlan.client_key2_data != NULL)
15520     {
15521         wlan.client_key2_data = NULL;
15522         wlan.client_key2_len  = 0;
15523     }
15524 #if CONFIG_WPA_SUPP_AP
15525 #if CONFIG_WPA_SUPP_CRYPTO_AP_ENTERPRISE
15526     if (wlan.dh_data != NULL)
15527     {
15528         wlan.dh_data = NULL;
15529         wlan.dh_len  = 0;
15530     }
15531     if (wlan.server_cert_data != NULL)
15532     {
15533         wlan.server_cert_data = NULL;
15534         wlan.server_cert_len  = 0;
15535     }
15536     if (wlan.server_key_data != NULL)
15537     {
15538         wlan.server_key_data = NULL;
15539         wlan.server_key_len  = 0;
15540     }
15541 #endif
15542 #endif
15543 #endif
15544 }
15545 #endif
15546 
15547 #if CONFIG_NET_MONITOR
wlan_register_monitor_user_callback(int (* monitor_data_recv_callback)(void * buffer,t_u16 data_len))15548 void wlan_register_monitor_user_callback(int (*monitor_data_recv_callback)(void *buffer, t_u16 data_len))
15549 {
15550     register_monitor_user_callback(monitor_data_recv_callback);
15551 }
15552 
wlan_deregister_net_monitor_user_callback()15553 void wlan_deregister_net_monitor_user_callback()
15554 {
15555     deregister_monitor_user_callback();
15556 }
15557 
wlan_net_monitor_cfg(wlan_net_monitor_t * monitor)15558 int wlan_net_monitor_cfg(wlan_net_monitor_t *monitor)
15559 {
15560     bool flag = false;
15561 
15562     if (is_sta_connected() || is_uap_started())
15563     {
15564         (void)PRINTF("down the uap and disconnet sta first\n\r");
15565         return WM_FAIL;
15566     }
15567 
15568     flag = (1 == monitor->monitor_activity) ? true : false;
15569     set_monitor_flag(flag);
15570     return wifi_net_monitor_cfg(monitor);
15571 }
15572 #endif
15573 
15574 #if CONFIG_TSP
wlan_get_tsp_cfg(t_u16 * enable,t_u32 * back_off,t_u32 * highThreshold,t_u32 * lowThreshold,t_u32 * dutycycstep,t_u32 * dutycycmin,int * highthrtemp,int * lowthrtemp,int * currCAUTemp,int * currRFUTemp)15575 int wlan_get_tsp_cfg(t_u16 *enable,
15576                      t_u32 *back_off,
15577                      t_u32 *highThreshold,
15578                      t_u32 *lowThreshold,
15579                      t_u32 *dutycycstep,
15580                      t_u32 *dutycycmin,
15581                      int *highthrtemp,
15582                      int *lowthrtemp,
15583                      int *currCAUTemp,
15584                      int *currRFUTemp)
15585 {
15586     t_u16 action = 0;
15587 
15588     return wifi_tsp_cfg(action, enable, back_off, highThreshold, lowThreshold, dutycycstep, dutycycmin, highthrtemp, lowthrtemp, currCAUTemp, currRFUTemp);
15589 }
wlan_set_tsp_cfg(t_u16 enable,t_u32 back_off,t_u32 highThreshold,t_u32 lowThreshold,t_u32 dutycycstep,t_u32 dutycycmin,int highthrtemp,int lowthrtemp)15590 int wlan_set_tsp_cfg(t_u16 enable,
15591                      t_u32 back_off,
15592                      t_u32 highThreshold,
15593                      t_u32 lowThreshold,
15594                      t_u32 dutycycstep,
15595                      t_u32 dutycycmin,
15596                      int highthrtemp,
15597                      int lowthrtemp)
15598 {
15599     t_u16 action = 1;
15600 
15601     return wifi_tsp_cfg(action, &enable, &back_off, &highThreshold, &lowThreshold, &dutycycstep, &dutycycmin, &highthrtemp, &lowthrtemp, NULL, NULL);
15602 }
15603 #endif
15604 
15605 #if STA_SUPPORT
wlan_get_signal_info(wlan_rssi_info_t * signal)15606 int wlan_get_signal_info(wlan_rssi_info_t *signal)
15607 {
15608     return wifi_send_rssi_info_cmd(signal);
15609 }
15610 #endif
15611 
wlan_set_bandcfg(wlan_bandcfg_t * bandcfg)15612 int wlan_set_bandcfg(wlan_bandcfg_t *bandcfg)
15613 {
15614     return wifi_get_set_bandcfg(bandcfg, MLAN_ACT_SET);
15615 }
15616 
wlan_get_bandcfg(wlan_bandcfg_t * bandcfg)15617 int wlan_get_bandcfg(wlan_bandcfg_t *bandcfg)
15618 {
15619     return wifi_get_set_bandcfg(bandcfg, MLAN_ACT_GET);
15620 }
15621 
15622 #if CONFIG_TURBO_MODE
wlan_get_turbo_mode(t_u8 * mode)15623 int wlan_get_turbo_mode(t_u8 *mode)
15624 {
15625     return wifi_get_turbo_mode(mode);
15626 }
15627 
wlan_get_uap_turbo_mode(t_u8 * mode)15628 int wlan_get_uap_turbo_mode(t_u8 *mode)
15629 {
15630     return wifi_get_uap_turbo_mode(mode);
15631 }
15632 
wlan_set_turbo_mode(t_u8 mode)15633 int wlan_set_turbo_mode(t_u8 mode)
15634 {
15635     return wifi_set_turbo_mode(mode);
15636 }
15637 
wlan_set_uap_turbo_mode(t_u8 mode)15638 int wlan_set_uap_turbo_mode(t_u8 mode)
15639 {
15640     return wifi_set_uap_turbo_mode(mode);
15641 }
15642 #endif
15643 
wlan_set_ps_cfg(t_u16 multiple_dtims,t_u16 bcn_miss_timeout,t_u16 local_listen_interval,t_u16 adhoc_wake_period,t_u16 mode,t_u16 delay_to_ps)15644 void wlan_set_ps_cfg(t_u16 multiple_dtims,
15645                      t_u16 bcn_miss_timeout,
15646                      t_u16 local_listen_interval,
15647                      t_u16 adhoc_wake_period,
15648                      t_u16 mode,
15649                      t_u16 delay_to_ps)
15650 {
15651     wifi_set_ps_cfg(multiple_dtims, bcn_miss_timeout, local_listen_interval, adhoc_wake_period, mode, delay_to_ps);
15652 }
15653 
15654 #if (CONFIG_IPS)
wlan_set_ips(int option)15655 int wlan_set_ips(int option)
15656 {
15657     return wifi_set_ips_config(MLAN_BSS_TYPE_STA, option);
15658 }
15659 #endif
15660 
wlan_set_country_code(const char * alpha2)15661 int wlan_set_country_code(const char *alpha2)
15662 {
15663     int ret;
15664     t_u8 region_code_rw610;
15665     unsigned char country3 = 0x20;
15666     char country_code[COUNTRY_CODE_LEN] = {0};
15667 #ifndef RW610
15668     char region_code[COUNTRY_CODE_LEN] = {0};
15669     const char *wlan_region_code       = NULL;
15670 
15671     wlan_region_code = wlan_get_wlan_region_code();
15672 
15673     region_code[0] = alpha2[0];
15674     region_code[1] = alpha2[1];
15675 
15676     if (strstr(wlan_region_code, region_code) == NULL)
15677     {
15678         wlcm_d("Region %s is configured, re-config not allowed", wlan_region_code);
15679         return -WM_FAIL;
15680     }
15681 #endif
15682 
15683     if ((alpha2[2] == 0x4f) || (alpha2[2] == 0x49) || (alpha2[2] == 0x58) || (alpha2[2] == 0x04))
15684     {
15685         country3 = alpha2[2];
15686     }
15687 
15688     country_code[0] = alpha2[0];
15689     country_code[1] = alpha2[1];
15690     country_code[2] = country3;
15691 
15692     ret = wlan_11d_region_2_code(mlan_adap, (t_u8 *)country_code, &region_code_rw610);
15693     if(ret != WM_SUCCESS)
15694     {
15695         wlcm_e("%s: Invalid country code.",country_code);
15696         return ret;
15697     }
15698 
15699     ret = wifi_set_country_code(country_code);
15700     if (ret != WM_SUCCESS)
15701         return ret;
15702 
15703 #if defined(RW610) && (CONFIG_COMPRESS_TX_PWTBL)
15704     ret = wlan_set_rg_power_cfg(region_code_rw610);
15705     if (ret != WM_SUCCESS)
15706     {
15707         return -WM_FAIL;
15708     }
15709 #endif
15710 
15711 #if defined(RW610) && ((CONFIG_COMPRESS_RU_TX_PWTBL) && (CONFIG_11AX))
15712     ret = wlan_set_ru_power_cfg(region_code_rw610);
15713     if (ret != WM_SUCCESS)
15714     {
15715         return -WM_FAIL;
15716     }
15717 #endif
15718 
15719     return ret;
15720 }
15721 
wlan_set_country_ie_ignore(uint8_t * ignore)15722 int wlan_set_country_ie_ignore(uint8_t *ignore)
15723 {
15724     return wifi_set_country_ie_ignore(ignore);
15725 }
15726 
wlan_set_region_code(unsigned int region_code)15727 int wlan_set_region_code(unsigned int region_code)
15728 {
15729     char *country;
15730 
15731     if ((region_code == 0x40) || (region_code == 0x41) || (region_code == 0xFE))
15732     {
15733         (void)PRINTF("Region code 0XFF is used for Japan to support channels of both 2.4GHz band and 5GHz band.\r\n");
15734         return -WM_FAIL;
15735     }
15736 
15737     country = (char *)wlan_11d_code_2_region(mlan_adap, (unsigned char)region_code);
15738     return wlan_set_country_code(country);
15739 }
15740 
wlan_get_region_code(unsigned int * region_code)15741 int wlan_get_region_code(unsigned int *region_code)
15742 {
15743     return wifi_get_region_code(region_code);
15744 }
15745 
wlan_set_11d_state(int bss_type,int state)15746 int wlan_set_11d_state(int bss_type, int state)
15747 {
15748     if (bss_type == WLAN_BSS_TYPE_UAP)
15749     {
15750 #if UAP_SUPPORT
15751         return wlan_enable_uap_11d(state);
15752 #else
15753         return -WM_E_NODEV;
15754 #endif
15755     }
15756     else
15757     {
15758         return wlan_enable_11d(state);
15759     }
15760 }
15761 
15762 #if CONFIG_COEX_DUTY_CYCLE
wlan_single_ant_duty_cycle(t_u16 enable,t_u16 nbTime,t_u16 wlanTime)15763 int wlan_single_ant_duty_cycle(t_u16 enable, t_u16 nbTime, t_u16 wlanTime)
15764 {
15765     return wifi_single_ant_duty_cycle(enable, nbTime, wlanTime);
15766 }
15767 
wlan_dual_ant_duty_cycle(t_u16 enable,t_u16 nbTime,t_u16 wlanTime,t_u16 wlanBlockTime)15768 int wlan_dual_ant_duty_cycle(t_u16 enable, t_u16 nbTime, t_u16 wlanTime, t_u16 wlanBlockTime)
15769 {
15770     return wifi_dual_ant_duty_cycle(enable, nbTime, wlanTime, wlanBlockTime);
15771 }
15772 #endif
15773 
15774 #if CONFIG_EXTERNAL_COEX_PTA
wlan_external_coex_pta_cfg(ext_coex_pta_cfg coex_pta_config)15775 int wlan_external_coex_pta_cfg(ext_coex_pta_cfg coex_pta_config)
15776 {
15777     return wifi_external_coex_pta_cfg(coex_pta_config);
15778 }
15779 #endif
15780 
15781 #if CONFIG_IMD3_CFG
wlan_imd3_cfg(t_u8 imd3_value)15782 int wlan_imd3_cfg(t_u8 imd3_value)
15783 {
15784     return wifi_imd3_cfg(imd3_value);
15785 }
15786 #endif
15787 
15788 #if CONFIG_WPA_SUPP
15789 #if CONFIG_UAP_STA_MAC_ADDR_FILTER
wlan_host_set_sta_mac_filter(int filter_mode,int mac_count,unsigned char * mac_addr)15790 int wlan_host_set_sta_mac_filter(int filter_mode, int mac_count, unsigned char *mac_addr)
15791 {
15792 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
15793     int ret = 0;
15794     struct netif *uap_netif = net_get_uap_interface();
15795     ret = wpa_supp_set_mac_acl(uap_netif, filter_mode, mac_count, mac_addr);
15796     if (ret < 0)
15797         return -WM_FAIL;
15798     else
15799 #endif
15800         return WM_SUCCESS;
15801 }
15802 #endif
15803 #endif
15804 
15805 #if (CONFIG_WIFI_IND_RESET) && (CONFIG_WIFI_IND_DNLD)
wlan_set_indrst_cfg(const wlan_indrst_cfg_t * indrst_cfg)15806 int wlan_set_indrst_cfg(const wlan_indrst_cfg_t *indrst_cfg)
15807 {
15808     wlan.ir_mode = indrst_cfg->ir_mode;
15809 
15810     return wifi_set_indrst_cfg(indrst_cfg, (mlan_bss_type)WLAN_BSS_TYPE_STA);
15811 }
15812 
wlan_get_indrst_cfg(wlan_indrst_cfg_t * indrst_cfg)15813 int wlan_get_indrst_cfg(wlan_indrst_cfg_t *indrst_cfg)
15814 {
15815     return wifi_get_indrst_cfg(indrst_cfg, (mlan_bss_type)WLAN_BSS_TYPE_STA);
15816 }
15817 
wlan_trigger_oob_ind_reset()15818 static int wlan_trigger_oob_ind_reset()
15819 {
15820     (void)wlan_ieeeps_off();
15821 
15822     OSA_TimeDelay(1000);
15823 
15824     (void)wlan_deepsleepps_off();
15825 
15826     OSA_TimeDelay(1000);
15827 
15828 #ifdef IR_OUTBAND_TRIGGER_GPIO
15829     GPIO_PinWrite(IR_OUTBAND_TRIGGER_GPIO, IR_OUTBAND_TRIGGER_GPIO_PIN, 0);
15830 
15831     OSA_TimeDelay(10);
15832 
15833     GPIO_PinWrite(IR_OUTBAND_TRIGGER_GPIO, IR_OUTBAND_TRIGGER_GPIO_PIN, 1);
15834 #endif
15835 
15836     return wifi_trigger_oob_indrst();
15837 }
15838 
wlan_independent_reset()15839 int wlan_independent_reset()
15840 {
15841     if (wlan.ir_mode == 1)
15842     {
15843         return wlan_trigger_oob_ind_reset();
15844     }
15845     else if (wlan.ir_mode == 2)
15846     {
15847         return wifi_test_independent_reset();
15848     }
15849 
15850     return -WM_FAIL;
15851 }
15852 #endif
15853 
15854 #if CONFIG_INACTIVITY_TIMEOUT_EXT
wlan_sta_inactivityto(wlan_inactivity_to_t * inac_to,t_u16 action)15855 int wlan_sta_inactivityto(wlan_inactivity_to_t *inac_to, t_u16 action)
15856 {
15857     return wifi_sta_inactivityto(inac_to, action);
15858 }
15859 #endif
15860 
15861 #if CONFIG_CPU_LOADING
15862 
wlan_cpu_loading_record_data(void)15863 static void wlan_cpu_loading_record_data(void)
15864 {
15865     memset(cpu_loading.cpu_loading_info, 0, cpu_loading.task_status_len);
15866 
15867     char run_task_name[configMAX_TASK_NAME_LEN];
15868     char cpu_run_data[20];
15869     unsigned int value;
15870     int task_name_index = 0, task_time_index = 0, index = 0, task_index = 0;
15871 
15872     OSA_GetRuntimeStats(cpu_loading.cpu_loading_info);
15873 
15874     uint32_t len_data = strlen(cpu_loading.cpu_loading_info);
15875     do
15876     {
15877         memset(run_task_name, 0, strlen(run_task_name));
15878         /*Record task name*/
15879         do
15880         {
15881             if(cpu_loading.cpu_loading_info[index] == ' ' && cpu_loading.cpu_loading_info[index + 1] == ' ') // Complete name parsing
15882                 break;
15883             else
15884                 run_task_name[task_name_index++] = cpu_loading.cpu_loading_info[index++];
15885         }while(index < len_data);
15886 
15887         do      //Filter out padding spaces between task names and run time values.
15888         {
15889             if(cpu_loading.cpu_loading_info[index++] == '\t')
15890                 break;
15891         } while (index < len_data);
15892 
15893         /*Record task run time*/
15894         do
15895         {
15896             if(cpu_loading.cpu_loading_info[index] < '0' || cpu_loading.cpu_loading_info[index] > '9')
15897                 break;
15898             cpu_run_data[task_time_index++] = cpu_loading.cpu_loading_info[index++];
15899         }while(index < len_data);
15900 
15901         cpu_run_data[task_time_index] = '\0';
15902         get_uint(cpu_run_data, &value, strlen(cpu_run_data));
15903 
15904         if(cpu_loading.index > 0)
15905         {
15906             for(int i = 0; i < cpu_loading.task_nums; i++)  // To collect CPU loading info according to fixed task name sequence.
15907             {
15908                 if(!strcmp(cpu_loading.task_name[i], run_task_name))
15909                 {
15910                     cpu_loading.data_pre[i] = cpu_loading.data_cur[i];
15911                     cpu_loading.data_cur[i] = value;
15912                     break;
15913                 }
15914             }
15915         }
15916         else
15917         {
15918             memset(task_string_name[task_index],' ', configMAX_TASK_NAME_LEN);
15919             task_string_name[task_index][configMAX_TASK_NAME_LEN -1] = '\0';
15920 
15921             memcpy(cpu_loading.task_name[task_index], run_task_name, strlen(run_task_name));
15922             memcpy(task_string_name[task_index], run_task_name, strlen(run_task_name));
15923             cpu_loading.data_pre[task_index] = value;
15924             cpu_loading.data_cur[task_index] = value;
15925             cpu_loading.first_data[task_index] = value;
15926 
15927             cpu_loading.task_name[task_index][strlen(run_task_name)] = '\0';
15928         }
15929 
15930         /*Filter percentage value*/
15931         do
15932         {
15933             if(cpu_loading.cpu_loading_info[index] == '\r' && cpu_loading.cpu_loading_info[index + 1] == '\n')
15934             {
15935                 index += 2;
15936                 break;
15937             }
15938 
15939             index++;
15940         }while((index < len_data));
15941 
15942         task_time_index = 0;
15943         task_name_index = 0;
15944         task_index ++;
15945 
15946     }while (index < len_data);
15947 
15948     cpu_loading.index ++;
15949     cpu_loading.sampling_loops --;
15950 }
15951 
cpu_loading_task(osa_task_param_t arg)15952 static void cpu_loading_task(osa_task_param_t arg)
15953 {
15954     for(;;)
15955     {
15956         /* Wait till cpu loading timer time out. */
15957         (void)os_event_notify_get(OS_WAIT_FOREVER);
15958 
15959         if(cpu_loading.sampling_loops == 0)
15960         {
15961             wlan_cpu_loading_stop();
15962         }
15963         else
15964         {
15965             wlan_cpu_loading_record_data();
15966             if(cpu_loading.index > 1)
15967                 wlan_cpu_loading_info_display();
15968         }
15969     }
15970 
15971     OSA_ThreadSelfComplete(NULL);
15972 }
15973 
cpu_loading_cb(osa_timer_arg_t arg)15974 static void cpu_loading_cb(osa_timer_arg_t arg)
15975 {
15976     (void)os_event_notify_put(cpu_loading.cpu_loading_thread);
15977 }
15978 
wlan_cpu_loading_start(uint32_t number,uint8_t period)15979 static int wlan_cpu_loading_start(uint32_t number, uint8_t period)
15980 {
15981     int ret;
15982     osa_status_t status;
15983 
15984     if(cpu_loading.status == CPU_LOADING_STATUS_DEAD)
15985     {
15986         memset(&cpu_loading, 0, sizeof(cpu_loading));
15987         if(period == 0)
15988             cpu_loading.sampling_period = CPU_LOADING_PERIOD;
15989         else
15990             cpu_loading.sampling_period = period * (CPU_LOADING_PERIOD / 2);
15991 
15992         status = OSA_TimerCreate((osa_timer_handle_t)cpu_loading.cpu_loading_timer, MSEC_TO_TICK(cpu_loading.sampling_period),
15993                           &cpu_loading_cb, NULL, KOSA_TimerPeriodic, OSA_TIMER_NO_ACTIVATE);
15994         if (status != KOSA_StatusSuccess)
15995         {
15996             (void)PRINTF("Unable to create cpu loading timer.\r\n");
15997             return -WM_FAIL;
15998         }
15999 
16000         status = OSA_TaskCreate((osa_task_handle_t)cpu_loading.cpu_loading_task_Handle, OSA_TASK(cpu_loading_task), NULL);
16001         if (status != KOSA_StatusSuccess)
16002         {
16003             (void)PRINTF("Unable to create cpu loading thread.\r\n");
16004             return -WM_FAIL;
16005         }
16006 
16007         os_get_num_of_tasks(&cpu_loading.task_nums);
16008         cpu_loading.task_status_len = cpu_loading.task_nums * sizeof(TaskStatus_t);
16009         cpu_loading.cpu_loading_info = (char *)OSA_MemoryAllocate(cpu_loading.task_status_len);
16010         if (cpu_loading.cpu_loading_info == NULL)
16011         {
16012             (void)PRINTF("%s: Failed to alloc cpu loading info\r\n", __func__);
16013             return -WM_FAIL;
16014         }
16015 
16016         cpu_loading.index = 0;
16017         if(number != 0)
16018             cpu_loading.sampling_loops = number;
16019         else
16020             cpu_loading.sampling_loops = CPU_LOADING_KEEPING;
16021 
16022         cpu_loading.status = CPU_LOADING_STATUS_ONGOING;
16023 
16024         memset(cpu_loading.data_cur, 0, sizeof(cpu_loading.data_cur));
16025         memset(cpu_loading.data_pre, 0, sizeof(cpu_loading.data_pre));
16026         (void)OSA_TimerActivate((osa_timer_handle_t)cpu_loading.cpu_loading_timer);
16027         return WM_SUCCESS;
16028     }
16029     else
16030     {
16031         wlcm_e("Unable to start cpu loading timer, pls stop the previous cpu loading test firstly.");
16032         return -WM_FAIL;
16033     }
16034 }
16035 
wlan_cpu_loading(uint8_t start,uint32_t number,uint8_t period)16036 int wlan_cpu_loading(uint8_t start, uint32_t number, uint8_t period)
16037 {
16038     int ret;
16039     if(start == CPU_LOADING_ACTION_STOP)
16040     {
16041         if(cpu_loading.status == CPU_LOADING_STATUS_DEAD)
16042         {
16043             (void)PRINTF("Collecting CPU loading info has already ended.\r\n");
16044             return WM_SUCCESS;
16045         }
16046         else
16047         {
16048             cpu_loading.sampling_loops = 0;
16049             OSA_TimerChange((osa_timer_handle_t)cpu_loading.cpu_loading_timer, 100, 0); // Chages value of cpu loading timer to stop cpu loading test quickly.
16050             if(cpu_loading.status != CPU_LOADING_STATUS_DEAD)
16051             {
16052                 OSA_TimeDelay(50);
16053             }
16054             return WM_SUCCESS;
16055         }
16056     }
16057     else
16058     {
16059         return wlan_cpu_loading_start(number, period);
16060     }
16061 }
16062 
16063 #endif
16064 
16065 #if CONFIG_AUTO_NULL_TX
wlan_auto_null_tx(wlan_auto_null_tx_t * auto_null_tx,mlan_bss_type bss_type)16066 int wlan_auto_null_tx(wlan_auto_null_tx_t *auto_null_tx, mlan_bss_type bss_type)
16067 {
16068     if (auto_null_tx == NULL)
16069     {
16070         return -WM_E_INVAL;
16071     }
16072 
16073     return wifi_auto_null_tx(auto_null_tx, bss_type);
16074 }
16075 #endif
16076 
16077 #ifdef RW610
wlan_get_temperature()16078 int32_t wlan_get_temperature()
16079 {
16080     return wifi_get_temperature();
16081 }
16082 #endif
16083 
wlan_string_dup(const char * s)16084 char *wlan_string_dup(const char *s)
16085 {
16086     char *snew = (char *)OSA_MemoryAllocate(strlen(s) + 1);
16087     if (snew)
16088         (void)strcpy(snew, s);
16089     return snew;
16090 }
16091 
wlan_get_board_type()16092 uint32_t wlan_get_board_type()
16093 {
16094     uint32_t board_type = 0xff;
16095 
16096 #ifdef RW610
16097     board_type = wifi_get_board_type();
16098 #endif
16099 
16100     return board_type;
16101 }
16102 
16103 #if UAP_SUPPORT
wlan_uap_disconnect_sta(uint8_t * sta_addr)16104 int wlan_uap_disconnect_sta(uint8_t *sta_addr)
16105 {
16106     int ret = WM_SUCCESS;
16107 #if !CONFIG_WPA_SUPP
16108     t_u16 reason_code = WLAN_REASON_CODE_PREV_AUTH_NOT_VALID;
16109 #endif
16110 #if CONFIG_WPA_SUPP
16111 #if !CONFIG_WIFI_NM_WPA_SUPPLICANT
16112     struct netif *netif = net_get_uap_interface();
16113     ret = wpa_supp_deauth_sta(netif, sta_addr);
16114 #endif
16115 #else
16116     ret = wifi_sta_deauth(sta_addr, reason_code);
16117 #endif
16118 
16119     if(ret != WM_SUCCESS)
16120     {
16121         (void)PRINTF("Error: Failed to disconnect sta.\r\n");
16122     }
16123 
16124     return ret;
16125 }
16126 
wlan_uap_set_sta_ageout_timer(uint32_t sta_ageout_time)16127 int wlan_uap_set_sta_ageout_timer(uint32_t sta_ageout_time)
16128 {
16129     int ret = WM_SUCCESS;
16130 
16131     if (is_uap_started() != 0)
16132     {
16133         wlcm_e(
16134             "Cannot set the max inactivity duration for stations "
16135             "as the uAP is already running");
16136         return -WM_FAIL;
16137     }
16138 
16139     ret = wifi_uap_sta_ageout_timer_getset(ACTION_SET, (uint32_t *)&sta_ageout_time);
16140     if (ret != WM_SUCCESS)
16141     {
16142         wlcm_e("Failed to set STA ageout timer");
16143         return ret;
16144     }
16145 
16146     ret = wifi_uap_ps_sta_ageout_timer_getset(ACTION_SET, (uint32_t *)&sta_ageout_time);
16147     if (ret != WM_SUCCESS)
16148     {
16149         wlcm_e("Failed to set PS STA ageout timer");
16150         return ret;
16151     }
16152 
16153     return ret;
16154 }
16155 #endif
16156 
16157 #if CONFIG_WIFI_NM_WPA_SUPPLICANT
wlan_supp_dpp_listen(int bss_type,int enable)16158 int wlan_supp_dpp_listen(int bss_type, int enable)
16159 {
16160     if (bss_type == WLAN_BSS_ROLE_STA)
16161     {
16162         if (enable)
16163         {
16164             wifi_set_rx_mgmt_indication(bss_type, WLAN_MGMT_ACTION);
16165             wlan_ieeeps_off();
16166             wlan_deepsleepps_off();
16167         }
16168         else
16169         {
16170             wlan_ieeeps_on(wlan.wakeup_conditions);
16171             wlan_deepsleepps_on();
16172         }
16173     }
16174     return 0;
16175 }
16176 #endif
16177