• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  * Description: soc_wifi_wpa_service_api.c
15  */
16 
17 #include "soc_osal.h"
18 #include "soc_wifi_api.h"
19 #include "service_wifi_api.h"
20 #include "wifi_device.h"
21 #include "wifi_p2p.h"
22 #include "soc_wifi_p2p_api.h"
23 #include "securec.h"
24 #include "wifi_hotspot.h"
25 #include "wifi_alg.h"
26 #include "service_event.h"
27 #include "lwip/netifapi.h"
28 #include "lwip/dns.h"
29 #include "mac_addr.h"
30 #include "tsensor.h"
31 #if defined(CONFIG_RST_SUPPORT) || defined(CONFIG_PLAT_DFR_SUPPORT)
32 #include "plat_service.h"
33 #ifdef _PRE_WLAN_FEATURE_CENTRALIZE
34 #ifdef BOARD_FPGA_WIFI
35 #include "abb_config.h"
36 #include "mpw0_poweron.h"
37 #endif // BOARD_FPGA_WIFI
38 #endif // _PRE_WLAN_FEATURE_CENTRALIZE
39 #endif // defined(CONFIG_RST_SUPPORT) || defined(CONFIG_PLAT_DFR_SUPPORT)
40 
41 #define OAL_STATIC
42 
43 #ifdef BUILD_UT
44 #define OSAL_STATIC
45 #else
46 #define OSAL_STATIC static
47 #endif
48 
49 #define service_is_freqs(freqs) (((freqs) == 2412 || (freqs) == 2417 || (freqs) == 2422 || (freqs) == 2427 || \
50                                   (freqs) == 2432 || (freqs) == 2437 || (freqs) == 2442 || (freqs) == 2447 || \
51                                   (freqs) == 2452 || (freqs) == 2457 || (freqs) == 2462 || (freqs) == 2467 || \
52                                   (freqs) == 2472 || (freqs) == 2484) ? (freqs) : 0)
53 #define service_char_to_upper(ch)   (((ch) >= 'a' && (ch) <= 'z') ? ((ch) - 0x20) : (ch))
54 
55 #define service_check_hex(ch)     ((((ch) >= '0') && ((ch) <= '9')) || (((ch) >= 'A') && ((ch) <= 'F')) || \
56                                   (((ch) >= 'a') && ((ch) <= 'f')) ? 1 : 0)
57 
58 #ifdef __cplusplus
59 #if __cplusplus
60 extern "C" {
61 #endif
62 #endif
63 
64 #define WIFI_ACTIVE_VAP_MAX_NUM   2
65 #define WIFI_MAC_CONTRY_CODE_LEN 3
66 #define SERVICE_P2P_MAX_FIND_TIME 120
67 #define SERVICE_P2P_MIN_FIND_TIME 5
68 #define SERVICE_P2P_MAX_FIND_NUM  32
69 #define SERVICE_P2P_MAX_GC_NUM  4
70 #define SERVICE_P2P_DEFAULT_INTENT 7
71 #define SERVICE_P2P_MAX_INTENT 15
72 #define SERVICE_P2P_MAX_LISTEN_TIME 65535
73 #define SERVICE_BASE_FREQS 2412
74 #define SERVICE_MIN_PRIO 10
75 #define SERVICE_MIN_STACK 4096
76 #define SERVICE_FREQS_INTERVAL 5
77 #define SERVICE_LINKLOSS_PROBEREQ_RATIO_MIN 1
78 #define SERVICE_LINKLOSS_PROBEREQ_RATIO_MAX 10
79 #define SERVICE_LINKLOSS_THRESHOLD_MIN 50
80 #define SERVICE_LINKLOSS_THRESHOLD_MAX 500
81 #define WIFI_SENDPKT_MIN_LEN 10
82 #define WIFI_SENDPKT_MAX_LEN 1400
83 #define SERVICE_MAX_PERIOD_INTERVAL 65535
84 #define WIFI_CONN_SCAN_INTERVAL 5000
85 #define WIFI_USER_INPUT_KEY_LEN (WIFI_MAX_KEY_LEN + 2) /* 关联时临时保存用户输入的密码信息,长度 = 64 + \0 + 2个双引号 */
86 
87 #define service_is_freqs(freqs) (((freqs) == 2412 || (freqs) == 2417 || (freqs) == 2422 || (freqs) == 2427 || \
88                                   (freqs) == 2432 || (freqs) == 2437 || (freqs) == 2442 || (freqs) == 2447 || \
89                                   (freqs) == 2452 || (freqs) == 2457 || (freqs) == 2462 || (freqs) == 2467 || \
90                                   (freqs) == 2472 || (freqs) == 2484) ? (freqs) : 0)
91 
92 /*****************************************************************************
93  全局变量定义
94 *****************************************************************************/
95 softap_config_stru        g_softap_config = {0};
96 softap_config_advance_stru g_softap_advance_config = {100, 2, 86400, 1, 1, WIFI_MODE_11B_G_N_AX};
97 
98 char                 g_sta_ifname[WIFI_IFNAME_MAX_SIZE + 1];
99 char                 g_hotspot_ifname[WIFI_IFNAME_MAX_SIZE + 1];
100 char                 g_p2p_ifname[WIFI_IFNAME_MAX_SIZE + 1];
101 int                  g_drop_disconnect = 0;
102 int                  g_sta_enble_flag = 0;
103 int                  g_softap_enble_flag = 0;
104 int                  g_p2p_enable_flag = 0;
105 int                  g_softap_hidden = 1;
106 dl_list              g_api_cb_node;
107 int                  g_api_cb_init = 0;
108 int                  g_p2p_isgo = 0;
109 #ifdef _PRE_WLAN_FEATURE_CSI
110 int                  g_csi_enable_flag = 0;
111 #endif
112 /* 上一次扫描上报的时间 */
113 static uint64_t g_sta_last_scan_time_stamp_ms = 0;
114 /* 这是否是一个由关联触发的扫描 */
115 OAL_STATIC int g_sta_conn_req_flag = 0;
116 /* 关联请求信息 */
117 static wifi_sta_config_stru g_sta_conn_config = {0};
118 #if defined(LWIP_IPV4) && defined(LWIP_IPV6) && (LWIP_IPV4) && (LWIP_IPV6)
119 __attribute__((weak)) err_t netifapi_netif_add_ip6_address(struct netif *netif, ip_addr_t *ipaddr);
120 #endif
121 static errcode_t wifi_sta_connect_part2(void);
122 /*****************************************************************************
123  函数实现
124 *****************************************************************************/
service_check_api_cb(uint32_t line_num)125 static uint32_t service_check_api_cb(uint32_t line_num)
126 {
127     if (g_api_cb_init == 0) {
128         service_error_log1(SERVICE_ERROR, "Srv:%d:cd is not init", line_num);
129         return ERRCODE_FAIL;
130     }
131     if (list_empty(&g_api_cb_node) == 1) {
132         service_error_log1(SERVICE_ERROR, "Srv:%d:cb list is null", line_num);
133         return ERRCODE_FAIL;
134     }
135     return ERRCODE_SUCC;
136 }
137 
send_broadcast_connected_change_for_sta(int state,const wifi_linked_info_stru * connect_para,int32_t reason_code)138 OSAL_STATIC void send_broadcast_connected_change_for_sta(int state, const wifi_linked_info_stru *connect_para,
139     int32_t reason_code)
140 {
141     if (service_check_api_cb(__LINE__) == ERRCODE_FAIL) {
142         return;
143     }
144     service_event_cb *node = NULL;
145     dl_list_for_each_entry(node, &g_api_cb_node, service_event_cb, node) {
146         if (node != NULL && node->service_cb.wifi_event_connection_changed != NULL) {
147             node->service_cb.wifi_event_connection_changed(state, connect_para, reason_code);
148         }
149     }
150 }
151 
send_broadcast_sta_connected_for_ap(const wifi_sta_info_stru * sta_info)152 OSAL_STATIC void send_broadcast_sta_connected_for_ap(const wifi_sta_info_stru *sta_info)
153 {
154     if (service_check_api_cb(__LINE__) == ERRCODE_FAIL) {
155         return;
156     }
157     service_event_cb *node = NULL;
158     dl_list_for_each_entry(node, &g_api_cb_node, service_event_cb, node) {
159         if (node != NULL && node->service_cb.wifi_event_softap_sta_join != NULL) {
160             node->service_cb.wifi_event_softap_sta_join(sta_info);
161         }
162     }
163 }
164 
send_broadcast_sta_leave_for_ap(const wifi_sta_info_stru * sta_info)165 OSAL_STATIC void send_broadcast_sta_leave_for_ap(const wifi_sta_info_stru *sta_info)
166 {
167     if (service_check_api_cb(__LINE__) == ERRCODE_FAIL) {
168         return;
169     }
170     service_event_cb *node = NULL;
171     dl_list_for_each_entry(node, &g_api_cb_node, service_event_cb, node) {
172         if (node != NULL && node->service_cb.wifi_event_softap_sta_leave != NULL) {
173             node->service_cb.wifi_event_softap_sta_leave(sta_info);
174         }
175     }
176 }
177 
178 #ifdef CONFIG_P2P_SUPPORT
send_broadcast_sta_leave_for_p2p(const p2p_client_info_stru * gc_info)179 static void send_broadcast_sta_leave_for_p2p(const p2p_client_info_stru *gc_info)
180 {
181     if (service_check_api_cb(__LINE__) == ERRCODE_FAIL) {
182         return;
183     }
184     service_event_cb *node = NULL;
185     dl_list_for_each_entry(node, &g_api_cb_node, service_event_cb, node) {
186         if (node != NULL && node->service_cb.wifi_event_p2p_go_connection_changed != NULL) {
187             node->service_cb.wifi_event_p2p_go_connection_changed(WIFI_STATE_NOT_AVALIABLE, gc_info);
188         }
189     }
190 }
191 
send_broadcast_sta_connected_for_p2p(const p2p_client_info_stru * gc_info)192 static void send_broadcast_sta_connected_for_p2p(const p2p_client_info_stru *gc_info)
193 {
194     if (service_check_api_cb(__LINE__) == ERRCODE_FAIL) {
195         return;
196     }
197     service_event_cb *node = NULL;
198     dl_list_for_each_entry(node, &g_api_cb_node, service_event_cb, node) {
199         if (node != NULL && node->service_cb.wifi_event_p2p_go_connection_changed != NULL) {
200             node->service_cb.wifi_event_p2p_go_connection_changed(WIFI_STATE_AVALIABLE, gc_info);
201         }
202     }
203 }
204 #endif /* CONFIG_P2P_SUPPORT */
205 
wifi_is_need_psk(wifi_security_enum security_type)206 int32_t wifi_is_need_psk(wifi_security_enum security_type)
207 {
208     if ((security_type != WIFI_SEC_TYPE_OPEN) &&
209         (security_type != WIFI_SEC_TYPE_OWE)) {
210         return 1;
211     }
212     return 0;
213 }
214 
wifi_get_timestamp_ms(void)215 OSAL_STATIC uint64_t wifi_get_timestamp_ms(void)
216 {
217     osal_timeval tv;
218     osal_gettimeofday(&tv);
219     return (uint64_t)(((int64_t)tv.tv_sec) * 1000000 + (int64_t)tv.tv_usec) >> 10;  /* 秒与微秒的转换需乘1000000 */
220 }
221 
send_broadcast_scan_done(int state,const event_wifi_scan_done * wifi_scan_done)222 OSAL_STATIC void send_broadcast_scan_done(int state, const event_wifi_scan_done *wifi_scan_done)
223 {
224     g_sta_last_scan_time_stamp_ms = wifi_get_timestamp_ms();
225     /* 如果当前是一个由关联触发的扫描,走完下半段 */
226     if (g_sta_conn_req_flag == 1) {
227         service_error_log1(SERVICE_ERROR, "Srv:%d:scan triggered by connect!", __LINE__);
228         wifi_sta_connect_part2();
229         return;
230     }
231     if (g_api_cb_init == 0) {
232         service_error_log1(SERVICE_ERROR, "Srv:%d:cb is not init", __LINE__);
233         return;
234     }
235     if (list_empty(&g_api_cb_node) == 1 ||
236         memcmp(wifi_scan_done->ifname, g_sta_ifname, strlen(wifi_scan_done->ifname)) != 0) {
237         service_error_log1(SERVICE_ERROR, "Srv:%d:cb list is null", __LINE__);
238         return;
239     }
240     service_event_cb *node = NULL;
241     dl_list_for_each_entry(node, &g_api_cb_node, service_event_cb, node) {
242         if (node != NULL && node->service_cb.wifi_event_scan_state_changed != NULL) {
243             node->service_cb.wifi_event_scan_state_changed(state, wifi_scan_done->bss_num);
244         }
245     }
246 }
247 
248 #ifdef CONFIG_P2P_SUPPORT
send_broadcast_connected_change_for_p2p(int state,const p2p_status_info_stru * p2p_status)249 static void send_broadcast_connected_change_for_p2p(int state, const p2p_status_info_stru *p2p_status)
250 {
251     if (service_check_api_cb(__LINE__) == ERRCODE_FAIL) {
252         return;
253     }
254     service_event_cb *node = NULL;
255     dl_list_for_each_entry(node, &g_api_cb_node, service_event_cb, node) {
256         if (node != NULL && node->service_cb.wifi_event_p2p_gc_connection_changed != NULL) {
257             node->service_cb.wifi_event_p2p_gc_connection_changed(state, p2p_status);
258         }
259     }
260 }
261 #endif /* CONFIG_P2P_SUPPORT */
262 
send_broadcast_ap_state_change_for_ap(int state)263 OSAL_STATIC void send_broadcast_ap_state_change_for_ap(int state)
264 {
265     if (service_check_api_cb(__LINE__) == ERRCODE_FAIL) {
266         return;
267     }
268     service_event_cb *node = NULL;
269     dl_list_for_each_entry(node, &g_api_cb_node, service_event_cb, node) {
270         if (node != NULL && node->service_cb.wifi_event_softap_state_changed != NULL) {
271             node->service_cb.wifi_event_softap_state_changed(state);
272         }
273     }
274 }
275 
276 #ifdef CONFIG_P2P_SUPPORT
send_broadcast_ap_state_change_for_p2p(int state)277 static void send_broadcast_ap_state_change_for_p2p(int state)
278 {
279     if (service_check_api_cb(__LINE__) == ERRCODE_FAIL) {
280         return;
281     }
282     service_event_cb *node = NULL;
283     dl_list_for_each_entry(node, &g_api_cb_node, service_event_cb, node) {
284         if (node != NULL && node->service_cb.wifi_event_p2p_go_start != NULL) {
285             node->service_cb.wifi_event_p2p_go_start(state);
286         }
287     }
288 }
289 
send_broadcast_p2p_recive_req(const unsigned char * p2p_dev_addr,char wps_mode)290 static void send_broadcast_p2p_recive_req(const unsigned char *p2p_dev_addr, char wps_mode)
291 {
292     if (service_check_api_cb(__LINE__) == ERRCODE_FAIL) {
293         return;
294     }
295     service_event_cb *node = NULL;
296     dl_list_for_each_entry(node, &g_api_cb_node, service_event_cb, node) {
297         if (node != NULL && node->service_cb.wifi_event_p2p_receive_connect != NULL) {
298             node->service_cb.wifi_event_p2p_receive_connect(p2p_dev_addr, wps_mode);
299         }
300     }
301 }
302 
send_broadcast_p2p_go_neg_success(const event_p2p_go_neg_success * go_neg_success)303 static void send_broadcast_p2p_go_neg_success(const event_p2p_go_neg_success *go_neg_success)
304 {
305     if (service_check_api_cb(__LINE__) == ERRCODE_FAIL) {
306         return;
307     }
308     service_event_cb *node = NULL;
309     dl_list_for_each_entry(node, &g_api_cb_node, service_event_cb, node) {
310         if (node != NULL && node->service_cb.wifi_event_p2p_go_neg_result != NULL) {
311             node->service_cb.wifi_event_p2p_go_neg_result(WIFI_STATE_AVALIABLE, go_neg_success->role_go);
312         }
313     }
314 }
315 
send_broadcast_p2p_go_neg_fail(const event_p2p_go_neg_failure * p2p_go_neg_failure)316 static void send_broadcast_p2p_go_neg_fail(const event_p2p_go_neg_failure *p2p_go_neg_failure)
317 {
318     if (service_check_api_cb(__LINE__) == ERRCODE_FAIL) {
319         return;
320     }
321     service_event_cb *node = NULL;
322     dl_list_for_each_entry(node, &g_api_cb_node, service_event_cb, node) {
323         if (node != NULL && node->service_cb.wifi_event_p2p_go_neg_result != NULL) {
324             node->service_cb.wifi_event_p2p_go_neg_result(WIFI_STATE_NOT_AVALIABLE, WIFI_STATE_NOT_AVALIABLE);
325         }
326     }
327 }
328 #endif /* CONFIG_P2P_SUPPORT */
329 
send_broadcast_connected(const event_wifi_connected * wifi_connected)330 OSAL_STATIC void send_broadcast_connected(const event_wifi_connected *wifi_connected)
331 {
332     wifi_linked_info_stru connect_para = {0};
333 #ifdef CONFIG_P2P_SUPPORT
334     p2p_status_info_stru p2p_status = {0};
335 #endif /* CONFIG_P2P_SUPPORT */
336     if (memcmp(wifi_connected->ifname, g_sta_ifname, strlen(wifi_connected->ifname)) == 0) {
337         wifi_sta_get_ap_info(&connect_para);
338         send_broadcast_connected_change_for_sta(WIFI_STATE_AVALIABLE, &connect_para, 0);
339 #ifdef CONFIG_P2P_SUPPORT
340     } else if (memcmp(wifi_connected->ifname, g_p2p_ifname, strlen(wifi_connected->ifname)) == 0) {
341         wifi_p2p_get_connect_info(&p2p_status);
342         send_broadcast_connected_change_for_p2p(WIFI_STATE_AVALIABLE, &p2p_status);
343 #endif /* CONFIG_P2P_SUPPORT */
344     }
345 }
346 
347 /* 转换reason code, 如果是被对端断开的, bit 15置为1 */
wifi_convert_disconnect_reason_code(const event_wifi_disconnected * wifi_disconnected)348 OSAL_STATIC int32_t wifi_convert_disconnect_reason_code(const event_wifi_disconnected *wifi_disconnected)
349 {
350     int32_t reason_code = wifi_disconnected->reason_code;
351 
352     if ((wifi_disconnected->locally_generated == 1) || (reason_code >= WIFI_MAC_REPORT_DISCONNECT_OFFSET)) {
353         return reason_code;
354     }
355     /* 对端主动断开的, bit 15置为1 */
356     reason_code |= (int32_t)(1 << 15);
357     return reason_code;
358 }
359 
send_broadcast_disconnected(const event_wifi_disconnected * wifi_disconnected)360 OSAL_STATIC void send_broadcast_disconnected(const event_wifi_disconnected *wifi_disconnected)
361 {
362     wifi_linked_info_stru disconnect_para = {0};
363 #ifdef CONFIG_P2P_SUPPORT
364     p2p_status_info_stru p2p_status = {0};
365 #endif /* CONFIG_P2P_SUPPORT */
366     if (g_drop_disconnect == 1) {
367         g_drop_disconnect = 0;
368         return;
369     }
370 
371     if (memcmp(wifi_disconnected->ifname, g_sta_ifname, strlen(wifi_disconnected->ifname)) == 0) {
372         if (memcpy_s(disconnect_para.bssid, sizeof(disconnect_para.bssid),
373             wifi_disconnected->bssid, sizeof(wifi_disconnected->bssid)) != EOK) {
374             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
375             return;
376         }
377         disconnect_para.conn_state = WIFI_DISCONNECTED;
378         disconnect_para.wpa_state = wifi_disconnected->wpa_state;
379         send_broadcast_connected_change_for_sta(WIFI_STATE_NOT_AVALIABLE, &disconnect_para,
380             wifi_convert_disconnect_reason_code(wifi_disconnected));
381 #ifdef CONFIG_P2P_SUPPORT
382     } else if (memcmp(wifi_disconnected->ifname, g_p2p_ifname, strlen(wifi_disconnected->ifname)) == 0) {
383         if (memcpy_s(p2p_status.group_bssid, sizeof(p2p_status.group_bssid),
384             wifi_disconnected->bssid, sizeof(wifi_disconnected->bssid)) != EOK) {
385             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
386             return;
387         }
388         send_broadcast_connected_change_for_p2p(WIFI_STATE_NOT_AVALIABLE, &p2p_status);
389 #endif /* CONFIG_P2P_SUPPORT */
390     }
391 }
392 
send_broadcast_ap_state_change(const char * ifname,int state)393 OSAL_STATIC void send_broadcast_ap_state_change(const char *ifname, int state)
394 {
395     if (memcmp(ifname, g_hotspot_ifname, strlen(ifname)) == 0) {
396         g_softap_enble_flag = state;
397         send_broadcast_ap_state_change_for_ap(state);
398 #ifdef CONFIG_P2P_SUPPORT
399     } else if (memcmp(ifname, g_p2p_ifname, strlen(ifname)) == 0) {
400         g_p2p_isgo = state;
401         send_broadcast_ap_state_change_for_p2p(state);
402 #endif /* CONFIG_P2P_SUPPORT */
403     }
404 }
405 
send_broadcast_sta_connected(const event_ap_sta_connected * ap_sta_connected)406 OSAL_STATIC void send_broadcast_sta_connected(const event_ap_sta_connected *ap_sta_connected)
407 {
408     wifi_sta_info_stru sta_info[WIFI_DEFAULT_MAX_NUM_STA] = {0};
409     int sta_num = WIFI_DEFAULT_MAX_NUM_STA;
410     int i;
411 #ifdef CONFIG_P2P_SUPPORT
412     p2p_client_info_stru gc_info[SERVICE_P2P_MAX_GC_NUM] = {0};
413     int p2p_num = SERVICE_P2P_MAX_GC_NUM;
414 #endif /* CONFIG_P2P_SUPPORT */
415     if (memcmp(ap_sta_connected->ifname, g_hotspot_ifname, strlen(ap_sta_connected->ifname)) == 0) {
416         wifi_softap_get_sta_list(sta_info, &sta_num);
417         for (i = 0; i < sta_num; i++) {
418             if ((memcmp(sta_info[i].mac_addr, ap_sta_connected->addr, sizeof(ap_sta_connected->addr))) == 0) {
419                 send_broadcast_sta_connected_for_ap(&sta_info[i]);
420                 return;
421             }
422         }
423 #ifdef CONFIG_P2P_SUPPORT
424     } else if (memcmp(ap_sta_connected->ifname, g_p2p_ifname, strlen(ap_sta_connected->ifname)) == 0) {
425         wifi_p2p_go_get_gc_info(gc_info, &p2p_num);
426         for (i = 0; i < p2p_num; i++) {
427             if ((memcmp(gc_info[i].gc_bssid, ap_sta_connected->addr, sizeof(ap_sta_connected->addr))) == 0) {
428                 send_broadcast_sta_connected_for_p2p(&gc_info[i]);
429                 return;
430             }
431         }
432 #endif /* CONFIG_P2P_SUPPORT */
433     }
434 }
435 
send_broadcast_sta_leave(const event_ap_sta_disconnected * ap_sta_disconnected)436 OSAL_STATIC void send_broadcast_sta_leave(const event_ap_sta_disconnected *ap_sta_disconnected)
437 {
438     wifi_sta_info_stru sta_info = {0};
439 #ifdef CONFIG_P2P_SUPPORT
440     p2p_client_info_stru gc_info = {0};
441 #endif /* CONFIG_P2P_SUPPORT */
442     if (memcmp(ap_sta_disconnected->ifname, g_hotspot_ifname, strlen(ap_sta_disconnected->ifname)) == 0) {
443         if ((memcpy_s(sta_info.mac_addr, sizeof(sta_info.mac_addr), ap_sta_disconnected->addr,
444             sizeof(ap_sta_disconnected->addr))) != EOK) {
445             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
446             return;
447         }
448         send_broadcast_sta_leave_for_ap(&sta_info);
449 #ifdef CONFIG_P2P_SUPPORT
450     } else if (memcmp(ap_sta_disconnected->ifname, g_p2p_ifname, strlen(ap_sta_disconnected->ifname)) == 0) {
451         if ((memcpy_s(gc_info.gc_bssid, sizeof(gc_info.gc_bssid), ap_sta_disconnected->addr,
452             sizeof(ap_sta_disconnected->addr))) != EOK) {
453             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
454             return;
455         }
456         send_broadcast_sta_leave_for_p2p(&gc_info);
457 #endif /* CONFIG_P2P_SUPPORT */
458     }
459 }
460 
461 #ifdef CONFIG_P2P_SUPPORT
send_broadcast_p2p_go_neg_req(const event_p2p_go_neg_request * p2p_go_neg_request)462 OSAL_STATIC void send_broadcast_p2p_go_neg_req(const event_p2p_go_neg_request *p2p_go_neg_request)
463 {
464     char wps_mode = WIFI_WPS_BUTT;
465     if (p2p_go_neg_request->dev_passwd_id == 0x1 || p2p_go_neg_request->dev_passwd_id == 0x5) {
466         wps_mode = WIFI_WPS_PIN;
467     } else if (p2p_go_neg_request->dev_passwd_id == 0x4) {
468         wps_mode = WIFI_WPS_PBC;
469     }
470     send_broadcast_p2p_recive_req(p2p_go_neg_request->addr, wps_mode);
471 }
472 #endif /* CONFIG_P2P_SUPPORT */
473 
474 #ifdef CONFIG_WPS_SUPPORT
send_broadcast_wps_result(int state,const char * ifname)475 OSAL_STATIC void send_broadcast_wps_result(int state, const char *ifname)
476 {
477     wifi_if_type_enum type = IFTYPE_STA;
478     ext_wifi_p2p_status_info p2p_status;
479     if (service_check_api_cb(__LINE__) == ERRCODE_FAIL) {
480         return;
481     }
482     if (memcmp(ifname, g_hotspot_ifname, strlen(ifname)) == 0) {
483         type = IFTYPE_AP;
484     } else if (memcmp(ifname, g_sta_ifname, strlen(ifname)) == 0) {
485         type = IFTYPE_STA;
486 #ifdef CONFIG_P2P_SUPPORT
487     } else if (memcmp(ifname, g_p2p_ifname, strlen(ifname)) == 0) {
488         uapi_wifi_p2p_status(&p2p_status);
489         if ((p2p_status.p2p_state) == EXT_WIFI_P2P_MODE_GC) {
490             type = IFTYPE_P2P_CLIENT;
491         } else if ((p2p_status.p2p_state) == EXT_WIFI_P2P_MODE_GO) {
492             type = IFTYPE_P2P_GO;
493         } else if ((p2p_status.p2p_state) == EXT_WIFI_P2P_DEVICE_ONLY) {
494             type = IFTYPE_P2P_DEVICE;
495         }
496 #endif
497     }
498     if ((type == IFTYPE_STA || type == IFTYPE_P2P_CLIENT) && state == WIFI_STATE_AVALIABLE) {
499         g_drop_disconnect = 1;
500     }
501     service_event_cb *node = NULL;
502     dl_list_for_each_entry(node, &g_api_cb_node, service_event_cb, node) {
503         if (node != NULL && node->service_cb.wifi_event_wps_result != NULL) {
504             node->service_cb.wifi_event_wps_result(state, type);
505         }
506     }
507 }
508 #endif
509 
510 #ifdef CONFIG_P2P_SUPPORT
send_broadcast_p2p_invitation_recive(const event_p2p_invite_recieve * p2p_invite_recieve)511 static void send_broadcast_p2p_invitation_recive(const event_p2p_invite_recieve *p2p_invite_recieve)
512 {
513     if (p2p_invite_recieve->unknow_network != 1) {
514         service_error_log1(SERVICE_ERROR, "Srv:%d:network is saved", __LINE__);
515         return;
516     }
517     send_broadcast_p2p_recive_req(p2p_invite_recieve->go_dev_addr, WIFI_WPS_PBC);
518 }
519 
send_broadcast_p2p_invitation_result(const event_p2p_invite_result * p2p_invite_result)520 static void send_broadcast_p2p_invitation_result(const event_p2p_invite_result *p2p_invite_result)
521 {
522     if (service_check_api_cb(__LINE__) == ERRCODE_FAIL) {
523         return;
524     }
525     service_event_cb *node = NULL;
526     dl_list_for_each_entry(node, &g_api_cb_node, service_event_cb, node) {
527         if (node != NULL && node->service_cb.wifi_event_p2p_invitation_result != NULL) {
528             node->service_cb.wifi_event_p2p_invitation_result(p2p_invite_result->status == 1 ?
529                 WIFI_STATE_AVALIABLE : WIFI_STATE_NOT_AVALIABLE);
530         }
531     }
532 }
533 
p2p_recive_pbc_req(void)534 static void p2p_recive_pbc_req(void)
535 {
536     int ret;
537     service_error_log2(SERVICE_ERROR, "Srv:%d:[%d][1:is_go 0:not_go]", __LINE__, g_p2p_isgo);
538     if (g_p2p_isgo == WIFI_STATE_AVALIABLE) {
539         ret = uapi_wifi_p2p_stop_find();
540         if (ret != EXT_WIFI_OK) {
541             service_error_log1(SERVICE_ERROR, "Srv:%d:stop p2p find fail", __LINE__);
542             return;
543         }
544         ret = uapi_wifi_p2p_wps_pbc();
545         if (ret != EXT_WIFI_OK) {
546             service_error_log1(SERVICE_ERROR, "Srv:%d:start wps pbc fail", __LINE__);
547             return;
548         }
549     }
550 }
551 #endif /* CONFIG_P2P_SUPPORT */
552 
wpa_event_cb_handle(const ext_wifi_event * event)553 OSAL_STATIC void wpa_event_cb_handle(const ext_wifi_event *event)
554 {
555     if (event == NULL) {
556         service_error_log1(SERVICE_ERROR, "Srv:%d:input event is null", __LINE__);
557         return;
558     }
559     service_error_log2(SERVICE_ERROR, "Srv:%d:recive event = %d", __LINE__, event->event);
560     switch (event->event) {
561         case EXT_WIFI_EVT_SCAN_DONE:
562             send_broadcast_scan_done(WIFI_STATE_AVALIABLE, &event->info.wifi_scan_done);
563             break;
564         case EXT_WIFI_EVT_CONNECTED:
565             send_broadcast_connected(&event->info.wifi_connected);
566             break;
567         case EXT_WIFI_EVT_DISCONNECTED:
568             send_broadcast_disconnected(&event->info.wifi_disconnected);
569             break;
570         case EXT_WIFI_EVT_AP_START:
571             send_broadcast_ap_state_change(event->info.wifi_ap_start.ifname, WIFI_STATE_AVALIABLE);
572             break;
573         case EXT_WIFI_EVT_AP_DISABLE:
574             send_broadcast_ap_state_change(event->info.wifi_ap_fail.ifname, WIFI_STATE_NOT_AVALIABLE);
575             break;
576         case EXT_WIFI_EVT_STA_CONNECTED:
577             send_broadcast_sta_connected(&event->info.ap_sta_connected);
578             break;
579         case EXT_WIFI_EVT_STA_DISCONNECTED:
580             send_broadcast_sta_leave(&event->info.ap_sta_disconnected);
581             break;
582 #ifdef CONFIG_P2P_SUPPORT
583         case EXT_WIFI_EVT_P2P_GO_NEG_REQUEST:
584             send_broadcast_p2p_go_neg_req(&event->info.p2p_go_neg_request);
585             break;
586         case EXT_WIFI_EVT_P2P_GO_NEG_SUCCESS:
587             send_broadcast_p2p_go_neg_success(&event->info.p2p_go_neg_success);
588             break;
589         case EXT_WIFI_EVT_P2P_GO_NEG_FAILURE:
590             send_broadcast_p2p_go_neg_fail(&event->info.p2p_go_neg_failure);
591             break;
592 #endif /* CONFIG_P2P_SUPPORT */
593 #ifdef CONFIG_WPS_SUPPORT
594         case EXT_WIFI_EVT_WPS_SUCCESS:
595             send_broadcast_wps_result(WIFI_STATE_AVALIABLE, event->info.wifi_wps_success.ifname);
596             break;
597         case EXT_WIFI_EVT_WPS_TIMEOUT:
598             send_broadcast_wps_result(WIFI_STATE_NOT_AVALIABLE, event->info.wifi_wps_timeout.ifname);
599             break;
600         case EXT_WIFI_EVT_WPS_FAIL:
601             send_broadcast_wps_result(WIFI_STATE_NOT_AVALIABLE, event->info.wifi_wps_fail.ifname);
602             break;
603 #endif /* CONFIG_WPS_SUPPORT */
604 #ifdef CONFIG_P2P_SUPPORT
605         case EXT_WIFI_EVT_P2P_INVITATION_RECIEVE:
606             send_broadcast_p2p_invitation_recive(&event->info.p2p_invite_recieve);
607             break;
608         case EXT_WIFI_EVT_P2P_INVITATION_RESULT:
609             send_broadcast_p2p_invitation_result(&event->info.p2p_invite_result);
610             break;
611         case EXT_WIFI_EVT_P2P_PROV_DISC_PBC_REQ:
612             p2p_recive_pbc_req();
613             break;
614 #endif /* CONFIG_P2P_SUPPORT */
615         default:
616             service_error_log1(SERVICE_ERROR, "Srv:%d:event is unknown", __LINE__);
617             return;
618     }
619 }
620 
register_callback(void)621 static int register_callback(void)
622 {
623     int ret;
624     ret = uapi_wifi_config_callback(0, SERVICE_MIN_PRIO, SERVICE_MIN_STACK);
625     if (ret != 0) {
626         return ret;
627     }
628     ret = uapi_wifi_register_event_callback(wpa_event_cb_handle);
629     return ret;
630 }
631 
find_wifi_event(const wifi_event_stru * event)632 static service_event_cb *find_wifi_event(const wifi_event_stru *event)
633 {
634     service_event_cb *node = NULL;
635     dl_list_for_each_entry(node, &g_api_cb_node, service_event_cb, node) {
636         if (node != NULL && memcmp(&node->service_cb, event, sizeof(wifi_event_stru)) == 0) {
637             return node;
638         }
639     }
640     return NULL;
641 }
642 
wifi_register_event_cb(const wifi_event_stru * event)643 errcode_t wifi_register_event_cb(const wifi_event_stru *event)
644 {
645     if (event == NULL) {
646         service_error_log1(SERVICE_ERROR, "Srv:%d:input is inlegal", __LINE__);
647         return ERROR_WIFI_INVALID_ARGS;
648     }
649 
650     if (g_api_cb_init == 0) {
651         list_init(&g_api_cb_node);
652         g_api_cb_init = 1;
653     }
654     if (find_wifi_event(event) != NULL) {
655         service_error_log1(SERVICE_ERROR, "Srv:%d:this cb is already register", __LINE__);
656         return ERRCODE_SUCC;
657     }
658     service_event_cb *node = (service_event_cb *)malloc(sizeof(service_event_cb));
659     if (node == NULL) {
660         service_error_log1(SERVICE_ERROR, "Srv:%d:malloc fail", __LINE__);
661         return ERROR_WIFI_UNKNOWN;
662     }
663     if (memcpy_s(&node->service_cb, sizeof(wifi_event_stru), event, sizeof(wifi_event_stru)) != EOK) {
664         service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
665         free(node);
666         return ERROR_WIFI_UNKNOWN;
667     }
668 
669     list_tail_insert(&g_api_cb_node, &node->node);
670     return ERRCODE_SUCC;
671 }
672 
wifi_unregister_event_cb(const wifi_event_stru * event)673 errcode_t wifi_unregister_event_cb(const wifi_event_stru *event)
674 {
675     if (event == NULL) {
676         service_error_log1(SERVICE_ERROR, "Srv:%d:input is inlegal", __LINE__);
677         return ERROR_WIFI_INVALID_ARGS;
678     }
679     if (g_api_cb_init == 0) {
680         service_error_log1(SERVICE_ERROR, "Srv:%d:cb is not init", __LINE__);
681         return ERROR_WIFI_UNKNOWN;
682     }
683     service_event_cb *node = NULL;
684     dl_list_for_each_entry(node, &g_api_cb_node, service_event_cb, node) {
685         if (node != NULL && memcmp(&node->service_cb, event, sizeof(wifi_event_stru)) == 0) {
686             list_delete_node(&node->node);
687             free(node);
688             return ERRCODE_SUCC;
689         }
690     }
691     service_error_log1(SERVICE_ERROR, "Srv:%d:no such cb", __LINE__);
692     return ERROR_WIFI_INVALID_ARGS;
693 }
694 
service_addr_precheck(const unsigned char * addr)695 OSAL_STATIC wifi_return_code service_addr_precheck(const unsigned char *addr)
696 {
697     // 对0、1、2、3、4、5每一字节进行非0判断
698     if ((addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]) == 0) {
699         return ERROR_WIFI_INVALID_ARGS;
700     }
701     // 对0、1、2、3、4、5每一字节进行非FF判断
702     if ((addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xFF) {
703         return ERROR_WIFI_INVALID_ARGS;
704     }
705 
706     return ERRCODE_SUCC;
707 }
708 
service_wifi_and_sta_check(void)709 static wifi_return_code service_wifi_and_sta_check(void)
710 {
711     if (wifi_is_sta_enabled() != 1) {
712         service_error_log1(SERVICE_ERROR, "Srv:%d:sta is not enabled", __LINE__);
713         return ERROR_WIFI_IFACE_INVALID;
714     }
715     return ERRCODE_SUCC;
716 }
717 
service_wifi_and_ap_check(void)718 static wifi_return_code service_wifi_and_ap_check(void)
719 {
720     if (uapi_wifi_get_init_status() != 1) {
721         service_error_log1(SERVICE_ERROR, "Srv:%d:WiFi is not initialized", __LINE__);
722         return ERROR_WIFI_NOT_STARTED;
723     }
724 
725     if (g_softap_enble_flag != 1) {
726         service_error_log1(SERVICE_ERROR, "Srv:%d:ap is not enabled", __LINE__);
727         return ERROR_WIFI_IFACE_INVALID;
728     }
729     return ERRCODE_SUCC;
730 }
731 
service_check_wifi_device_config(const wifi_sta_config_stru * config)732 static wifi_return_code service_check_wifi_device_config(const wifi_sta_config_stru *config)
733 {
734     if (config == NULL) {
735         service_error_log1(SERVICE_ERROR, "Srv:%d: config is NULL", __LINE__);
736         return ERROR_WIFI_INVALID_ARGS;
737     }
738 
739     if (config->ip_type >= UNKNOWN) {
740         service_error_log1(SERVICE_ERROR, "Srv:%d: ip_type is invalid", __LINE__);
741         return ERROR_WIFI_INVALID_ARGS;
742     }
743 
744     if (((strlen(config->ssid) == 0) || (strlen(config->ssid) > EXT_WIFI_MAX_SSID_LEN)) &&
745         (service_addr_precheck(config->bssid) != ERRCODE_SUCC)) {
746         service_error_log1(SERVICE_ERROR, "Srv:%d: ssid and bssid are invalid", __LINE__);
747         return ERROR_WIFI_INVALID_ARGS;
748     }
749 
750     if (config->security_type == WIFI_SEC_TYPE_INVALID ||
751 #ifndef CONFIG_OWE
752         config->security_type == WIFI_SEC_TYPE_OWE ||
753 #endif /* CONFIG_OWE */
754         config->security_type == WIFI_SEC_TYPE_WAPI_CERT) {
755         service_error_log1(SERVICE_ERROR, "Srv:%d: securitytype is not supported", __LINE__);
756         return ERROR_WIFI_INVALID_ARGS;
757     }
758 
759     if ((wifi_is_need_psk(config->security_type) == 1) && (strlen(config->pre_shared_key) == 0 ||
760         strlen(config->pre_shared_key) > EXT_WIFI_MAX_KEY_LEN)) {
761         service_error_log1(SERVICE_ERROR, "Srv:%d: securitytype is invalid", __LINE__);
762         return ERROR_WIFI_INVALID_ARGS;
763     }
764 
765     if (config->wifi_psk_type != 0 && config->wifi_psk_type != 1) {
766         service_error_log1(SERVICE_ERROR, "Srv:%d:wifi_psk_type is invalid", __LINE__);
767         return ERROR_WIFI_INVALID_ARGS;
768     }
769 
770     return ERRCODE_SUCC;
771 }
772 
service_hex_to_asic(const unsigned char * hex,unsigned char * asic,int hex_len)773 static wifi_return_code service_hex_to_asic(const unsigned char* hex, unsigned char* asic, int hex_len)
774 {
775     int j = 0;
776     int check_hex1, check_hex2;
777     unsigned char high_byte, low_byte;
778 
779     for (int i = 0; i < hex_len; i += 2) {   // 2个字节合为一个asic码
780         check_hex1 = service_check_hex(hex[i]);
781         check_hex2 = service_check_hex(hex[i + 1]);
782         if (check_hex1 != 1 || check_hex2 != 1) {
783             service_error_log1(SERVICE_ERROR, "Srv:%d:hex is invalid", __LINE__);
784             return ERROR_WIFI_INVALID_ARGS;
785         }
786         high_byte = service_char_to_upper(hex[i]);
787         low_byte = service_char_to_upper(hex[i + 1]);
788 
789         if (high_byte > 0x39) {
790             high_byte -= 0x37;           // 这里判断的是将A-F转换为0XA到0XF ,查看ASCII 表,可以看到从字符转换到hex之间的 差值
791         } else {
792             high_byte -= 0x30;           // 这里是将0-9转换为0x0-0x9  同样查看ASCII表,可以看到从字符准换到hex之间的差值。
793         }
794 
795         if (low_byte > 0x39) {
796             low_byte -= 0x37;
797         } else {
798             low_byte -= 0x30;
799         }
800 
801         asic[j] = (high_byte << 4) | low_byte;  // 右移4位
802         j++;
803     }
804 
805     asic[hex_len / 2] = '\0';               // asic最后的长度为hex_len/2
806     return ERRCODE_SUCC;
807 }
808 
service_check_wep_key(const char * pre_shared_key,int8_t wifi_psk_type,char * wep_key)809 static wifi_return_code service_check_wep_key(const char *pre_shared_key, int8_t wifi_psk_type, char *wep_key)
810 {
811     uint32_t key_len = strlen(pre_shared_key);
812     int32_t  ret;
813     if (wifi_psk_type == 0) {
814         // WEP的5byte和13byte固定是ascii码模式
815         if (key_len != 5 && key_len != 13) {
816             service_error_log1(SERVICE_ERROR, "Srv:%d: wep key invalid", __LINE__);
817             return ERROR_WIFI_INVALID_ARGS;
818         }
819     } else if (wifi_psk_type == 1) {
820         // 10byte和26byte固定是HEX模式
821         if (key_len != 10 && key_len != 26) {
822             service_error_log1(SERVICE_ERROR, "Srv:%d: wep key invalid", __LINE__);
823             return ERROR_WIFI_INVALID_ARGS;
824         }
825         ret = service_hex_to_asic(pre_shared_key, wep_key, (int32_t)key_len);
826         if (ret != ERRCODE_SUCC) {
827             service_error_log1(SERVICE_ERROR, "Srv:%d:service_hex_to_asic failed", __LINE__);
828             return ret;
829         }
830     }
831     return ERRCODE_SUCC;
832 }
833 
service_check_wpa_key(const char * pre_shared_key,int8_t wifi_psk_type)834 static wifi_return_code service_check_wpa_key(const char *pre_shared_key, int8_t wifi_psk_type)
835 {
836     unsigned long  key_len = strlen(pre_shared_key);
837     if (wifi_psk_type == 0) {
838         // wpa、wpa2、wpa/wpa2wpa2/wpa3个人级认证,8~63byte默认是ASCII
839         if (key_len < 8 || key_len > 63) {
840             service_error_log1(SERVICE_ERROR, "Srv:%d: wpa key invalid", __LINE__);
841             return ERROR_WIFI_INVALID_ARGS;
842         }
843     } else if (wifi_psk_type == 1) {
844         // wpa、wpa2、wpa/wpa2wpa2/wpa3个人级认证, 64byte默认是HEX
845         if (key_len != 64) {
846             service_error_log1(SERVICE_ERROR, "Srv:%d: wpa key invalid", __LINE__);
847             return ERROR_WIFI_INVALID_ARGS;
848         }
849     }
850     return ERRCODE_SUCC;
851 }
852 
service_check_sae_key(const char * pre_shared_key,int8_t wifi_psk_type)853 static wifi_return_code service_check_sae_key(const char *pre_shared_key, int8_t wifi_psk_type)
854 {
855     unsigned long  key_len = strlen(pre_shared_key);
856     if (wifi_psk_type == 0) {
857         // SAE个人级认证:8~64byte默认是ASCII
858         if (key_len < 8 || key_len > 64) {
859             service_error_log1(SERVICE_ERROR, "Srv:%d: sae key invalid", __LINE__);
860             return ERROR_WIFI_INVALID_ARGS;
861         }
862     } else if (wifi_psk_type == 1) {
863         service_error_log1(SERVICE_ERROR, "Srv:%d: wpa3 does not support hex", __LINE__);
864         return ERROR_WIFI_INVALID_ARGS;
865     }
866     return ERRCODE_SUCC;
867 }
868 
service_check_wapi_key(const char * pre_shared_key,int8_t wifi_psk_type)869 static wifi_return_code service_check_wapi_key(const char *pre_shared_key, int8_t wifi_psk_type)
870 {
871     unsigned long  key_len = strlen(pre_shared_key);
872     if (wifi_psk_type == 0) {
873         if (key_len < 8 || key_len > 64) {  /* wapi个人级认证:ASCII默认长度为8-64byte */
874             service_error_log1(SERVICE_ERROR, "Srv:%d: wapi key invalid", __LINE__);
875             return ERROR_WIFI_INVALID_ARGS;
876         }
877     } else if (wifi_psk_type == 1) {
878         if (key_len < 8 || key_len > 32) {  /* wapi个人级认证:HEX默认长度为8-32byte */
879             service_error_log1(SERVICE_ERROR, "Srv:%d: wapi key invalid", __LINE__);
880             return ERROR_WIFI_INVALID_ARGS;
881         }
882     }
883     return ERRCODE_SUCC;
884 }
885 
service_check_pre_shared_key(const char * pre_shared_key,int security_type,int8_t wifi_psk_type,char * wep_key)886 static wifi_return_code service_check_pre_shared_key(const char *pre_shared_key, int security_type,
887     int8_t wifi_psk_type, char *wep_key)
888 {
889     int ret;
890 
891     if (security_type == WIFI_SEC_TYPE_WEP || security_type == WIFI_SEC_TYPE_WEP_OPEN) {
892         ret = service_check_wep_key(pre_shared_key, wifi_psk_type, wep_key);
893         if (ret != ERRCODE_SUCC) {
894             service_error_log1(SERVICE_ERROR, "Srv:%d:wep key check failed", __LINE__);
895             return ret;
896         }
897     }
898 
899     if (security_type == WIFI_SEC_TYPE_WPA2PSK || security_type == WIFI_SEC_TYPE_WPA2_WPA_PSK_MIX ||
900         security_type == WIFI_SEC_TYPE_WPAPSK) {
901         ret = service_check_wpa_key(pre_shared_key, wifi_psk_type);
902         if (ret != ERRCODE_SUCC) {
903             service_error_log1(SERVICE_ERROR, "Srv:%d:wpa key check failed", __LINE__);
904             return ret;
905         }
906     }
907 
908     if (security_type == WIFI_SEC_TYPE_SAE || security_type == WIFI_SEC_TYPE_WPA3_WPA2_PSK_MIX) {
909         ret = service_check_sae_key(pre_shared_key, wifi_psk_type);
910         if (ret != ERRCODE_SUCC) {
911             service_error_log1(SERVICE_ERROR, "Srv:%d:sae key check failed", __LINE__);
912             return ret;
913         }
914     }
915 
916     if (security_type == WIFI_SEC_TYPE_WAPI_PSK) {
917         ret = service_check_wapi_key(pre_shared_key, wifi_psk_type);
918         if (ret != ERRCODE_SUCC) {
919             service_error_log1(SERVICE_ERROR, "Srv:%d:wapi key check failed", __LINE__);
920             return ret;
921         }
922     }
923     return ERRCODE_SUCC;
924 }
925 
service_set_ipv4(const wifi_sta_config_stru * config)926 static wifi_return_code service_set_ipv4(const wifi_sta_config_stru *config)
927 {
928     ip4_addr_t ipaddr = {0};
929     ip4_addr_t netmask = {0};
930     ip4_addr_t gw = {0};
931     struct netif *netif = netifapi_netif_find_by_name(g_sta_ifname);
932     if (netif == NULL) {
933         service_error_log1(SERVICE_ERROR, "Srv:%d:find_by_name failed", __LINE__);
934         return ERROR_WIFI_UNKNOWN;
935     }
936 
937     if (config->static_ip.ip_address == 0 || config->static_ip.netmask == 0) {
938         service_error_log1(SERVICE_ERROR, "Srv:%d:ipv4 is invalid", __LINE__);
939         return ERROR_WIFI_INVALID_ARGS;
940     }
941 
942     if ((memcpy_s(&ipaddr, sizeof(ip4_addr_t), &config->static_ip.ip_address, sizeof(unsigned int))) != EOK) {
943         service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
944         return ERROR_WIFI_UNKNOWN;
945     }
946 
947     if ((memcpy_s(&netmask, sizeof(ip4_addr_t), &config->static_ip.netmask, sizeof(unsigned int))) != EOK) {
948         service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
949         return ERROR_WIFI_UNKNOWN;
950     }
951 
952     if ((memcpy_s(&gw, sizeof(ip4_addr_t), &config->static_ip.gateway, sizeof(unsigned int))) != EOK) {
953         service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
954         return ERROR_WIFI_UNKNOWN;
955     }
956 
957     if (netifapi_netif_set_addr(netif, &ipaddr, &netmask, &gw) != 0) {
958         service_error_log1(SERVICE_ERROR, "Srv:%d:set_addr failed", __LINE__);
959         return ERROR_WIFI_UNKNOWN;
960     }
961     return ERRCODE_SUCC;
962 }
963 
service_set_ipv6(const wifi_sta_config_stru * config)964 static wifi_return_code service_set_ipv6(const wifi_sta_config_stru *config)
965 {
966 #if defined(LWIP_IPV4) && defined(LWIP_IPV6) && (LWIP_IPV4) && (LWIP_IPV6)
967     ip_addr_t ipaddr = {0};
968 
969     if (strlen(config->static_ipv6.ipv6_address) == 0 ||
970         strlen(config->static_ipv6.ipv6_address) > WIFI_IPV6_ADDR_LEN) {
971         service_error_log1(SERVICE_ERROR, "Srv:%d:ipv6 is invalid", __LINE__);
972         return ERROR_WIFI_INVALID_ARGS;
973     }
974 
975     struct netif *netif = netifapi_netif_find_by_name(g_sta_ifname);
976     if (netif == NULL) {
977         service_error_log1(SERVICE_ERROR, "Srv:%d:find_by_name failed", __LINE__);
978         return ERROR_WIFI_UNKNOWN;
979     }
980 
981     if ((memcpy_s(ipaddr.u_addr.ip6.addr, sizeof(ipaddr.u_addr.ip6.addr),
982         config->static_ipv6.ipv6_address, WIFI_IPV6_ADDR_LEN)) != EOK) {
983         service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
984         return ERROR_WIFI_UNKNOWN;
985     }
986 
987     ipaddr.type = IPADDR_TYPE_V6;
988     if (netifapi_netif_add_ip6_address(netif, &ipaddr) != 0) {
989         service_error_log1(SERVICE_ERROR, "Srv:%d:set_addr failed", __LINE__);
990         return ERROR_WIFI_UNKNOWN;
991     }
992 #endif
993     return ERRCODE_SUCC;
994 }
995 
service_set_ipv4_dns(const wifi_sta_config_stru * config)996 static wifi_return_code service_set_ipv4_dns(const wifi_sta_config_stru *config)
997 {
998 #if defined(LWIP_IPV4) && defined(LWIP_IPV6) && (LWIP_IPV4) && (LWIP_IPV6)
999     unsigned char   nums;
1000     if (config->static_ip.dns_servers[0] != 0 && config->static_ip.dns_servers[1] != 0) {
1001         nums = WIFI_MAX_DNS_NUM;
1002     } else if (config->static_ip.dns_servers[0] != 0) {
1003         nums = 1;
1004     } else {
1005         return ERRCODE_SUCC;
1006     }
1007 
1008     if (nums == 1) {
1009         ip_addr_t dnsserver;
1010         dnsserver.type = IPADDR_TYPE_V4;
1011 
1012         if ((memcpy_s(&dnsserver.u_addr.ip4, sizeof(ip4_addr_t),
1013             config->static_ip.dns_servers, sizeof(unsigned int))) != EOK) {
1014             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
1015             return ERROR_WIFI_UNKNOWN;
1016         }
1017 
1018         if (lwip_dns_setserver(nums, &dnsserver) != 0) {
1019             service_error_log1(SERVICE_ERROR, "Srv:%d:dns_setserver failed", __LINE__);
1020             return ERROR_WIFI_UNKNOWN;
1021         }
1022     } else {
1023         ip_addr_t dnsserver[WIFI_MAX_DNS_NUM];
1024         dnsserver[0].type = IPADDR_TYPE_V4;
1025         dnsserver[1].type = IPADDR_TYPE_V4;
1026 
1027         if ((memcpy_s(&dnsserver[0].u_addr.ip4, sizeof(ip4_addr_t),
1028             config->static_ip.dns_servers, sizeof(unsigned int))) != EOK) {
1029             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
1030             return ERROR_WIFI_UNKNOWN;
1031         }
1032 
1033         if ((memcpy_s(&dnsserver[1].u_addr.ip4, sizeof(ip4_addr_t),
1034             &config->static_ip.dns_servers[1], sizeof(unsigned int))) != EOK) {
1035             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
1036             return ERROR_WIFI_UNKNOWN;
1037         }
1038 
1039         if (lwip_dns_setserver(nums, dnsserver) != 0) {
1040             service_error_log1(SERVICE_ERROR, "Srv:%d:dns_setserver_v4 failed", __LINE__);
1041             return ERROR_WIFI_UNKNOWN;
1042         }
1043     }
1044 #endif
1045     return ERRCODE_SUCC;
1046 }
1047 
service_set_ipv6_dns(const wifi_sta_config_stru * config)1048 static wifi_return_code service_set_ipv6_dns(const wifi_sta_config_stru *config)
1049 {
1050 #if defined(LWIP_IPV4) && defined(LWIP_IPV6) && (LWIP_IPV4) && (LWIP_IPV6)
1051     unsigned char   nums;
1052     if (strlen(config->static_ipv6.ipv6_dns_servers[0]) != 0 && strlen(config->static_ipv6.ipv6_dns_servers[1]) != 0) {
1053         nums = WIFI_MAX_DNS_NUM;
1054     } else if (strlen(config->static_ipv6.ipv6_dns_servers[0]) != 0) {
1055         nums = 1;
1056     } else {
1057         return ERRCODE_SUCC;
1058     }
1059 
1060     if (nums == 1) {
1061         ip_addr_t dnsserver;
1062         dnsserver.type = IPADDR_TYPE_V6;
1063 
1064         if ((memcpy_s(&dnsserver.u_addr.ip6, sizeof(ip6_addr_t),
1065             config->static_ipv6.ipv6_dns_servers[0], WIFI_IPV6_DNS_LEN)) != EOK) {
1066             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
1067             return ERROR_WIFI_UNKNOWN;
1068         }
1069 
1070         if (lwip_dns_setserver(nums, &dnsserver) != 0) {
1071             service_error_log1(SERVICE_ERROR, "Srv:%d:dns_setserver failed", __LINE__);
1072             return ERROR_WIFI_UNKNOWN;
1073         }
1074     } else {
1075         ip_addr_t dnsserver[WIFI_MAX_DNS_NUM];
1076         dnsserver[0].type = IPADDR_TYPE_V6;
1077         dnsserver[1].type = IPADDR_TYPE_V6;
1078 
1079         if ((memcpy_s(&dnsserver[0].u_addr.ip6, sizeof(ip6_addr_t),
1080             config->static_ipv6.ipv6_dns_servers[0], WIFI_IPV6_DNS_LEN)) != EOK) {
1081             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
1082             return ERROR_WIFI_UNKNOWN;
1083         }
1084 
1085         if ((memcpy_s(&dnsserver[1].u_addr.ip6, sizeof(ip6_addr_t),
1086             config->static_ipv6.ipv6_dns_servers[1], WIFI_IPV6_DNS_LEN)) != EOK) {
1087             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
1088             return ERROR_WIFI_UNKNOWN;
1089         }
1090 
1091         if (lwip_dns_setserver(nums, dnsserver) != 0) {
1092             service_error_log1(SERVICE_ERROR, "Srv:%d:dns_setserver_v6 failed", __LINE__);
1093             return ERROR_WIFI_UNKNOWN;
1094         }
1095     }
1096 #endif
1097     return ERRCODE_SUCC;
1098 }
1099 
service_set_ip(const wifi_sta_config_stru * config)1100 static wifi_return_code service_set_ip(const wifi_sta_config_stru *config)
1101 {
1102     int ret1, ret2;
1103     if (config->ip_type == DHCP) {
1104         return ERRCODE_SUCC;
1105     }
1106 
1107     ret1 = service_set_ipv4(config);
1108     ret2 = service_set_ipv6(config);
1109     if (ret1 != ERRCODE_SUCC && ret2 != ERRCODE_SUCC) {
1110         service_error_log1(SERVICE_ERROR, "Srv:%d:IPv4 and IPv6 settings failed", __LINE__);
1111         return ERROR_WIFI_UNKNOWN;
1112     }
1113 
1114     ret1 = service_set_ipv4_dns(config);
1115     if (ret1 != ERRCODE_SUCC) {
1116         service_error_log1(SERVICE_ERROR, "Srv:%d:service_set_ipv4_dns failed", __LINE__);
1117         return ret1;
1118     }
1119 
1120     ret2 = service_set_ipv6_dns(config);
1121     if (ret2 != ERRCODE_SUCC) {
1122         service_error_log1(SERVICE_ERROR, "Srv:%d:service_set_ipv6_dns failed", __LINE__);
1123         return ret2;
1124     }
1125 
1126     return ERRCODE_SUCC;
1127 }
1128 
service_set_assoc_config(const wifi_sta_config_stru * config,ext_wifi_assoc_request * req,const char * wep_key)1129 static wifi_return_code service_set_assoc_config(const wifi_sta_config_stru *config, ext_wifi_assoc_request *req,
1130     const char *wep_key)
1131 {
1132     uint32_t len;
1133     if (strlen(config->ssid) != 0 && strlen(config->ssid) <= EXT_WIFI_MAX_SSID_LEN) {
1134         if ((memcpy_s(req->ssid, EXT_WIFI_MAX_SSID_LEN + 1, config->ssid, strlen(config->ssid) + 1)) != EOK) {
1135             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
1136             return ERROR_WIFI_UNKNOWN;
1137         }
1138     }
1139 
1140     if (service_addr_precheck(config->bssid) == ERRCODE_SUCC) {
1141         if ((memcpy_s(req->bssid, WIFI_MAC_LEN, config->bssid, WIFI_MAC_LEN)) != EOK) {
1142             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
1143             return ERROR_WIFI_UNKNOWN;
1144         }
1145     }
1146 
1147     req->auth = config->security_type;
1148     if (config->security_type == WIFI_SEC_TYPE_WEP && config->wifi_psk_type == 1) {
1149         req->key[0] = '\"';
1150         len = strlen(wep_key);
1151         if ((memcpy_s(req->key + 1, EXT_WIFI_MAX_KEY_LEN, wep_key, len + 1)) != EOK) {
1152             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
1153             return ERROR_WIFI_UNKNOWN;
1154         }
1155         req->key[len + 1] = '\"';
1156         req->key[len + 2] = '\0';  // 在len + 2置字符串结束符
1157     } else if (config->security_type == WIFI_SEC_TYPE_WEP && config->wifi_psk_type == 0) {
1158         req->key[0] = '\"';
1159         len = strlen(config->pre_shared_key);
1160         if ((memcpy_s(req->key + 1, EXT_WIFI_MAX_KEY_LEN, config->pre_shared_key, len + 1)) != EOK) {
1161             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
1162             return ERROR_WIFI_UNKNOWN;
1163         }
1164         req->key[len + 1] = '\"';
1165         req->key[len + 2] = '\0'; // 在len + 2置字符串结束符
1166     } else if (wifi_is_need_psk(config->security_type) == 1) {
1167         if ((memcpy_s(req->key, EXT_WIFI_MAX_KEY_LEN + 1,
1168             config->pre_shared_key, strlen(config->pre_shared_key) + 1)) != EOK) {
1169             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
1170             return ERROR_WIFI_UNKNOWN;
1171         }
1172     }
1173 
1174     if (config->wifi_psk_type == 1) {
1175         req->hex_flag = 1;
1176     } else {
1177         req->hex_flag = 0;
1178     }
1179     if (config->security_type == WIFI_SEC_TYPE_OWE) {
1180         req->pairwise = EXT_WIFI_PAIRWISE_AES;
1181     }
1182 
1183     return ERRCODE_SUCC;
1184 }
1185 
service_check_hotspot_config_advance(const softap_config_advance_stru * config)1186 static wifi_return_code service_check_hotspot_config_advance(const softap_config_advance_stru *config)
1187 {
1188     // beacon_interval的范围为25到1000
1189     if (config->beacon_interval < 25 || config->beacon_interval > 1000) {
1190         service_error_log1(SERVICE_ERROR, "Srv:%d:config->beacon is invalid", __LINE__);
1191         return ERROR_WIFI_INVALID_ARGS;
1192     }
1193 
1194     // dtim的范围为1到30
1195     if (config->dtim_period < 1 || config->dtim_period > 30) {
1196         service_error_log1(SERVICE_ERROR, "Srv:%d:config->dtim_period is invalid", __LINE__);
1197         return ERROR_WIFI_INVALID_ARGS;
1198     }
1199 
1200     // group_rekey的范围为30到86400
1201     if (config->group_rekey < 30 || config->group_rekey > 86400) {
1202         service_error_log1(SERVICE_ERROR, "Srv:%d:config->group_rekey is invalid", __LINE__);
1203         return ERROR_WIFI_INVALID_ARGS;
1204     }
1205 
1206     // 1不隐藏,2隐藏
1207     if (config->hidden_ssid_flag < 1 || config->hidden_ssid_flag > 2) {
1208         service_error_log1(SERVICE_ERROR, "Srv:%d:config->hidden_ssid_flag is invalid", __LINE__);
1209         return ERROR_WIFI_INVALID_ARGS;
1210     }
1211 
1212     if (config->protocol_mode != WIFI_MODE_11B && config->protocol_mode != WIFI_MODE_11B_G &&
1213         config->protocol_mode != WIFI_MODE_11B_G_N && config->protocol_mode != WIFI_MODE_11B_G_N_AX) {
1214         service_error_log1(SERVICE_ERROR, "Srv:%d:config->protocol_mode is invalid", __LINE__);
1215         return ERROR_WIFI_INVALID_ARGS;
1216     }
1217 
1218     g_softap_advance_config.beacon_interval = config->beacon_interval;
1219     g_softap_advance_config.dtim_period = config->dtim_period;
1220     g_softap_advance_config.gi = config->gi;
1221     g_softap_advance_config.group_rekey = config->group_rekey;
1222     g_softap_advance_config.hidden_ssid_flag = config->hidden_ssid_flag;
1223     g_softap_advance_config.protocol_mode = config->protocol_mode;
1224     return ERRCODE_SUCC;
1225 }
1226 
service_ie_frame_check(wifi_if_type_enum iftype,const unsigned char frame_type_bitmap,ext_wifi_iftype * iftype_tmp)1227 static errcode_t service_ie_frame_check(wifi_if_type_enum iftype, const unsigned char frame_type_bitmap,
1228                                         ext_wifi_iftype *iftype_tmp)
1229 {
1230     if (iftype == IFTYPE_STA) {
1231         if (frame_type_bitmap != 2) {       /* sta只会在probe request中插入IE,即frame_type_bitmap 为2 */
1232             service_error_log1(SERVICE_ERROR, "Srv:%d:frame_type_bitmap is invalid", __LINE__);
1233             return ERROR_WIFI_INVALID_ARGS;
1234         }
1235         *iftype_tmp = EXT_WIFI_IFTYPE_STATION;
1236         return ERRCODE_SUCC;
1237     }
1238 
1239     // softap只会在beacon和probe response中插入IE
1240     if (iftype == IFTYPE_AP) {
1241         if ((frame_type_bitmap & 0xFA) != 0) {
1242             service_error_log1(SERVICE_ERROR, "Srv:%d:frame_type_bitmap is invalid", __LINE__);
1243             return ERROR_WIFI_INVALID_ARGS;
1244         }
1245         *iftype_tmp = EXT_WIFI_IFTYPE_AP;
1246         return ERRCODE_SUCC;
1247     }
1248 
1249     // p2p go只会在beacon和probe response中插入IE
1250     if (iftype == IFTYPE_P2P_GO) {
1251         if ((frame_type_bitmap & 0xFA) != 0) {
1252             service_error_log1(SERVICE_ERROR, "Srv:%d:frame_type_bitmap is invalid", __LINE__);
1253             return ERROR_WIFI_INVALID_ARGS;
1254         }
1255         *iftype_tmp = EXT_WIFI_IFTYPE_P2P_GO;
1256         return ERRCODE_SUCC;
1257     }
1258 
1259     // p2p device可以在probe request和probe response中均插入IE
1260     if (iftype == IFTYPE_P2P_DEVICE) {
1261         if ((frame_type_bitmap & 0xF9) != 0) {
1262             service_error_log1(SERVICE_ERROR, "Srv:%d:frame_type_bitmap is invalid", __LINE__);
1263             return ERROR_WIFI_INVALID_ARGS;
1264         }
1265         *iftype_tmp = EXT_WIFI_IFTYPE_P2P_DEVICE;
1266         return ERRCODE_SUCC;
1267     }
1268     return ERRCODE_FAIL;
1269 }
1270 
service_set_softap_protocol(protocol_mode_enum protocol_mode)1271 static wifi_return_code service_set_softap_protocol(protocol_mode_enum protocol_mode)
1272 {
1273     if (uapi_wifi_softap_set_protocol_mode(protocol_mode) != EXT_WIFI_OK) {
1274         service_error_log1(SERVICE_ERROR, "Srv:%d:set_protocol_mod failed", __LINE__);
1275         return ERROR_WIFI_UNKNOWN;
1276     }
1277     return ERRCODE_SUCC;
1278 }
1279 
1280 
1281 #ifdef CONFIG_WPS_SUPPORT
service_pbc_pin_connect(wifi_wps_config * wps_config)1282 static wifi_return_code service_pbc_pin_connect(wifi_wps_config  *wps_config)
1283 {
1284     unsigned int   spin_len;
1285     int ret = 0;
1286     if (wps_config->wps_method == WIFI_WPS_PBC) {
1287         if (strlen(wps_config->bssid) == 0) {
1288             ret =  uapi_wifi_sta_wps_pbc(NULL, 0);
1289         } else {
1290             ret =  uapi_wifi_sta_wps_pbc(wps_config->bssid, WIFI_MAC_LEN);
1291         }
1292     } else if (wps_config->wps_method == WIFI_WPS_PIN) {
1293         spin_len = strlen(wps_config->wps_pin);
1294         if (spin_len != (WIFI_WPS_PIN_MAX_LEN_NUM - 1)) {
1295             service_error_log1(SERVICE_ERROR, "Srv:%d: wps_config pin_len is null", __LINE__);
1296             return ERROR_WIFI_UNKNOWN;
1297         }
1298 
1299         for (int i = 0; i < spin_len; i++) {
1300             if ((wps_config->wps_pin[i] < '0') || (wps_config->wps_pin[i] > '9')) {
1301                 service_error_log1(SERVICE_ERROR, "Srv:%d: wps_config.wpsPin is invalid", __LINE__);
1302                 return ERROR_WIFI_UNKNOWN;
1303             }
1304         }
1305 
1306         if (strlen(wps_config->bssid) == 0) {
1307             ret = uapi_wifi_sta_wps_pin(wps_config->wps_pin, spin_len, NULL, 0);
1308         } else {
1309             ret = uapi_wifi_sta_wps_pin(wps_config->wps_pin, spin_len, wps_config->bssid, WIFI_MAC_LEN);
1310         }
1311     }
1312 
1313     if (ret == EXT_WIFI_OK) {
1314         return ERRCODE_SUCC;
1315     } else {
1316         service_error_log1(SERVICE_ERROR, "Srv:%d:failed", __LINE__);
1317         return ERROR_WIFI_UNKNOWN;
1318     }
1319 }
1320 
get_wps_mode(unsigned short config_methods)1321 static unsigned char get_wps_mode(unsigned short config_methods)
1322 {
1323     unsigned char wps_mode = 0;
1324     if ((config_methods & WPS_PUSHBUTTON) != 0) {
1325         wps_mode |= 0x1;
1326     }
1327     if ((config_methods & WPS_LABEL) != 0) {
1328         wps_mode |= 0x2;
1329     }
1330     if ((config_methods & WPS_DISPLAY) != 0) {
1331         wps_mode |= 0x4;
1332     }
1333     if ((config_methods & WPS_KEYPAD) != 0) {
1334         wps_mode |= 0x8;
1335     }
1336     if (wps_mode == 0) {
1337         wps_mode = 0x10;
1338     }
1339     return wps_mode;
1340 }
1341 #endif /* CONFIG_WPS_SUPPORT */
1342 
wifi_init(void)1343 errcode_t wifi_init(void)
1344 {
1345     uint8_t  vap_res_num = WIFI_ACTIVE_VAP_MAX_NUM;
1346     uint8_t user_res_num = WIFI_DEFAULT_MAX_NUM_STA;
1347 
1348     if (uapi_wifi_get_init_status() == 1) {
1349         service_error_log1(SERVICE_ERROR, "Srv:%d:WiFi has been initialized", __LINE__);
1350         return ERRCODE_FAIL;
1351     }
1352 
1353     if (uapi_wifi_init(vap_res_num, user_res_num) != 0) {
1354         service_error_log1(SERVICE_ERROR, "Srv:%d:wifi_init failed", __LINE__);
1355         return ERRCODE_FAIL;
1356     }
1357 
1358     if (wifi_sta_set_pmf_mode(WIFI_MGMT_FRAME_PROTECTION_OPTIONAL) != ERRCODE_SUCC) {
1359         service_error_log1(SERVICE_ERROR, "Srv:%d:SetWiFiPmf failed", __LINE__);
1360         return ERRCODE_FAIL;
1361     }
1362 
1363     return ERRCODE_SUCC;
1364 }
1365 
wifi_deinit(void)1366 errcode_t wifi_deinit(void)
1367 {
1368     if (uapi_wifi_get_init_status() == 0) {
1369         service_error_log1(SERVICE_ERROR, "Srv:%d:WiFi has been uninstalled", __LINE__);
1370         return ERRCODE_FAIL;
1371     }
1372 
1373     if (uapi_wifi_deinit() != 0) {
1374         service_error_log1(SERVICE_ERROR, "Srv:%d:wifi_deinit failed", __LINE__);
1375         return ERRCODE_FAIL;
1376     } else {
1377         memset_s(g_sta_ifname, WIFI_IFNAME_MAX_SIZE + 1, 0, WIFI_IFNAME_MAX_SIZE + 1);
1378         memset_s(g_hotspot_ifname, WIFI_IFNAME_MAX_SIZE + 1, 0, WIFI_IFNAME_MAX_SIZE + 1);
1379         memset_s(g_p2p_ifname, WIFI_IFNAME_MAX_SIZE + 1, 0, WIFI_IFNAME_MAX_SIZE + 1);
1380         g_sta_enble_flag = 0;
1381         g_p2p_enable_flag = 0;
1382         g_softap_enble_flag = 0;
1383         g_softap_hidden = 1;
1384         return ERRCODE_SUCC;
1385     }
1386 }
1387 
wifi_is_wifi_inited(void)1388 int32_t wifi_is_wifi_inited(void)
1389 {
1390     int32_t result = uapi_wifi_get_init_status();
1391     return result;
1392 }
1393 
get_ifname(wifi_device_type_enum type,unsigned int * name_bufsize,char * ifname)1394 wifi_return_code get_ifname(wifi_device_type_enum type, unsigned int *name_bufsize, char *ifname)
1395 {
1396     if (ifname == NULL || type >= WIFI_TYPE_BUTT || name_bufsize == NULL || *name_bufsize > WIFI_IFNAME_MAX_SIZE + 1) {
1397         service_error_log1(SERVICE_ERROR, "Srv:%d:param is NULL", __LINE__);
1398         return ERROR_WIFI_INVALID_ARGS;
1399     }
1400 
1401     if (uapi_wifi_get_init_status() != 1) {
1402         service_error_log1(SERVICE_ERROR, "Srv:%d:WiFi is not initialized", __LINE__);
1403         return ERROR_WIFI_NOT_STARTED;
1404     }
1405 
1406     if (type == WIFI_TYPE_STA) {
1407         if (g_sta_enble_flag != 1) {
1408             service_error_log1(SERVICE_ERROR, "Srv:%d:sta is not enabled", __LINE__);
1409             return ERROR_WIFI_IFACE_INVALID;
1410         }
1411 
1412         if ((memcpy_s(ifname, *name_bufsize, g_sta_ifname, strlen(g_sta_ifname) + 1)) != EOK) {
1413             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
1414             return ERROR_WIFI_UNKNOWN;
1415         }
1416     } else if (type == WIFI_TYPE_HOTSPOT) {
1417         if (g_softap_enble_flag != 1) {
1418             service_error_log1(SERVICE_ERROR, "Srv:%d:softap is not enabled", __LINE__);
1419             return ERROR_WIFI_IFACE_INVALID;
1420         }
1421 
1422         if ((memcpy_s(ifname, *name_bufsize, g_hotspot_ifname, strlen(g_hotspot_ifname) + 1)) != EOK) {
1423             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
1424             return ERROR_WIFI_UNKNOWN;
1425         }
1426     } else if (type == WIFI_TYPE_P2P) {
1427         if (g_p2p_enable_flag != 1) {
1428             service_error_log1(SERVICE_ERROR, "Srv:%d:p2p is not enabled", __LINE__);
1429             return ERROR_WIFI_IFACE_INVALID;
1430         }
1431 
1432         if ((memcpy_s(ifname, *name_bufsize, g_p2p_ifname, strlen(g_p2p_ifname) + 1)) != EOK) {
1433             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
1434             return ERROR_WIFI_UNKNOWN;
1435         }
1436     }
1437 
1438     *name_bufsize = (unsigned int)strlen(ifname) + 1;
1439 
1440     return ERRCODE_SUCC;
1441 }
1442 
wifi_sta_enable(void)1443 errcode_t wifi_sta_enable(void)
1444 {
1445     if (wifi_is_sta_enabled() == 1) {
1446         service_error_log1(SERVICE_ERROR, "Srv:%d:sta has been enabled", __LINE__);
1447         return ERRCODE_FAIL;
1448     }
1449 
1450     int len = WIFI_IFNAME_MAX_SIZE + 1;
1451     if (register_callback() != 0) {
1452         service_error_log1(SERVICE_ERROR, "Srv:%d: register callback fail", __LINE__);
1453         return ERRCODE_FAIL;
1454     }
1455     if (uapi_wifi_sta_start(g_sta_ifname, &len) != EXT_WIFI_OK) {
1456         service_error_log1(SERVICE_ERROR, "Srv:%d:sta_start failed", __LINE__);
1457         return ERRCODE_FAIL;
1458     } else {
1459         g_sta_enble_flag = 1;
1460         return ERRCODE_SUCC;
1461     }
1462 }
1463 
wifi_sta_disable(void)1464 errcode_t wifi_sta_disable(void)
1465 {
1466     if (wifi_is_sta_enabled() == 0) {
1467         service_error_log1(SERVICE_ERROR, "Srv:%d:sta has been disabled", __LINE__);
1468         return ERRCODE_FAIL;
1469     }
1470 
1471     /* 关闭STA时,自动关闭DHCP */
1472     struct netif *netif = netifapi_netif_find_by_name(g_sta_ifname);
1473     if (netifapi_dhcp_stop(netif) != EXT_WIFI_OK) {
1474         service_error_log1(SERVICE_ERROR, "Srv:%d:DHCP stop failed", __LINE__);
1475         return ERRCODE_FAIL;
1476     }
1477 
1478     if (uapi_wifi_sta_stop() != EXT_WIFI_OK) {
1479         service_error_log1(SERVICE_ERROR, "Srv:%d:sta_stop failed", __LINE__);
1480         return ERRCODE_FAIL;
1481     } else {
1482         memset_s(g_sta_ifname, WIFI_IFNAME_MAX_SIZE + 1, 0, WIFI_IFNAME_MAX_SIZE + 1);
1483         g_sta_enble_flag = 0;
1484         g_sta_last_scan_time_stamp_ms = 0;
1485         return ERRCODE_SUCC;
1486     }
1487 }
1488 
wifi_is_sta_enabled(void)1489 int32_t wifi_is_sta_enabled(void)
1490 {
1491     if (uapi_wifi_get_init_status() != 1) {
1492         return 0;
1493     }
1494 
1495     if (g_sta_enble_flag == 0) {
1496         return 0;
1497     } else {
1498         return 1;
1499     }
1500 }
1501 
wifi_get_dev(wifi_iftype_t iftype)1502 wifi_dev_t *wifi_get_dev(wifi_iftype_t iftype)
1503 {
1504     return (wifi_dev_t *)wifi_dev_get(iftype);
1505 }
1506 
wifi_sta_set_protocol_mode(protocol_mode_enum mode)1507 errcode_t wifi_sta_set_protocol_mode(protocol_mode_enum mode)
1508 {
1509     if (uapi_wifi_sta_set_protocol_mode(mode) != EXT_WIFI_OK) {
1510         service_error_log1(SERVICE_ERROR, "Srv:%d:set_protocol_mod failed", __LINE__);
1511         return ERRCODE_FAIL;
1512     }
1513     return ERRCODE_SUCC;
1514 }
1515 
wifi_sta_get_protocol_mode(void)1516 protocol_mode_enum wifi_sta_get_protocol_mode(void)
1517 {
1518     return uapi_wifi_sta_get_protocol_mode();
1519 }
1520 
wifi_sta_set_scan_policy(wifi_if_type_enum iftype,wifi_scan_strategy_stru * scan_strategy)1521 errcode_t wifi_sta_set_scan_policy(wifi_if_type_enum iftype, wifi_scan_strategy_stru *scan_strategy)
1522 {
1523     int ret;
1524     ext_wifi_scan_param_stru scan_param = { 0 };
1525 
1526     if (iftype >= IFTYPES_BUTT || scan_strategy == NULL) {
1527         service_error_log1(SERVICE_ERROR, "Srv:%d:para is invalid", __LINE__);
1528         return ERRCODE_NOT_SUPPORT;
1529     }
1530 
1531     scan_param.scan_cnt = scan_strategy->scan_cnt;   /* 扫描次数,取值范围0,1~10。默认值为2 */
1532     scan_param.scan_time = scan_strategy->scan_time; /* 扫描驻留时间,取值范围0,20~120。默认值为20ms。 */
1533     scan_param.single_probe_send_times = scan_strategy->single_probe_send_times; /* 单个probe发送次数, 即host重发次数 */
1534 
1535     if (iftype == IFTYPE_STA && g_sta_enble_flag == 1) {
1536         ret = uapi_wifi_set_scan_param(g_sta_ifname, &scan_param);
1537     } else if (iftype == IFTYPE_AP && g_softap_enble_flag == 1) {
1538         ret = uapi_wifi_set_scan_param(g_hotspot_ifname, &scan_param);
1539     } else if (g_p2p_enable_flag == 1) {
1540         ret = uapi_wifi_set_scan_param(g_p2p_ifname, &scan_param);
1541     } else {
1542         service_error_log1(SERVICE_ERROR, "Srv:%d:vap is not enabled or iftype is invalid", __LINE__);
1543         return ERRCODE_FAIL;
1544     }
1545 
1546     if (ret == EXT_WIFI_OK) {
1547         return ERRCODE_SUCC;
1548     } else {
1549         service_error_log1(SERVICE_ERROR, "Srv:%dSetWifiScanStrategy failed", __LINE__);
1550         return ERRCODE_FAIL;
1551     }
1552 }
1553 
wifi_sta_scan_stop(void)1554 errcode_t wifi_sta_scan_stop(void)
1555 {
1556     return uapi_wifi_force_scan_complete();
1557 }
1558 
wifi_ap_scan(void)1559 errcode_t wifi_ap_scan(void)
1560 {
1561     uint32_t ret = service_wifi_and_ap_check();
1562     if (ret != ERRCODE_SUCC) {
1563         service_error_log1(SERVICE_ERROR, "Srv:%d:service_wifi_and_ap_check failed", __LINE__);
1564         return ret;
1565     }
1566 
1567     int32_t res = uapi_wifi_ap_scan();
1568     if (res == EXT_WIFI_OK) {
1569         return ERRCODE_SUCC;
1570     } else {
1571         service_error_log1(SERVICE_ERROR, "Srv:%d:failed", __LINE__);
1572         return ERRCODE_FAIL;
1573     }
1574 }
1575 
wifi_raw_scan(wifi_scan_params_stru * scan_param,wifi_scan_no_save_cb cb)1576 errcode_t wifi_raw_scan(wifi_scan_params_stru *scan_param, wifi_scan_no_save_cb cb)
1577 {
1578     ext_wifi_scan_params sp = {0};
1579 
1580     uint32_t ret = service_wifi_and_sta_check();
1581     if (ret != ERRCODE_SUCC) {
1582         service_error_log1(SERVICE_ERROR, "Srv:%d:service_wifi_and_sta_check failed", __LINE__);
1583         return ret;
1584     }
1585 
1586     if (scan_param == NULL) {
1587         service_error_log1(SERVICE_ERROR, "Srv:%d: raw scan_param is null", __LINE__);
1588         return ERRCODE_FAIL;
1589     }
1590 
1591     if (scan_param->scan_type == WIFI_BASIC_SCAN) {
1592         sp.scan_type = EXT_WIFI_BASIC_SCAN;
1593     } else if (scan_param->scan_type == WIFI_CHANNEL_SCAN) {
1594         sp.chan_list[0] = (uint8_t)scan_param->channel_num;
1595         if (sp.chan_list[0] == 0 || sp.chan_list[0] > 14) {  // 2.4g信道最大14
1596             service_error_log1(SERVICE_ERROR, "Srv:%d: raw scan_param->freqs is invalid", __LINE__);
1597             return ERRCODE_FAIL;
1598         }
1599         sp.scan_type = EXT_WIFI_CHANNEL_SCAN;
1600         sp.chan_num = 1;
1601     } else if (scan_param->scan_type == WIFI_SSID_SCAN || scan_param->scan_type == WIFI_SSID_PREFIX_SCAN) {
1602         uint32_t len = strlen(scan_param->ssid);
1603         if (scan_param->ssid_len == 0 || scan_param->ssid_len > EXT_WIFI_MAX_SSID_LEN || scan_param->ssid_len != len) {
1604             service_error_log1(SERVICE_ERROR, "Srv:%d: scan ssid is invalid", __LINE__);
1605             return ERRCODE_FAIL;
1606         }
1607 
1608         sp.scan_type = (scan_param->scan_type == WIFI_SSID_SCAN) ? EXT_WIFI_SSID_SCAN : EXT_WIFI_SSID_PREFIX_SCAN;
1609         if ((memcpy_s(sp.ssid, EXT_WIFI_MAX_SSID_LEN + 1, (const void *)scan_param->ssid,
1610             (size_t)scan_param->ssid_len + 1)) != EOK) {
1611             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
1612             return ERRCODE_FAIL;
1613         }
1614 
1615         sp.ssid_len = (uint8_t)scan_param->ssid_len;
1616     } else if (scan_param->scan_type == WIFI_BSSID_SCAN) {
1617         if (service_addr_precheck(scan_param->bssid) != ERRCODE_SUCC) {
1618             service_error_log1(SERVICE_ERROR, "Srv:%d: raw bssid is invalid", __LINE__);
1619             return ERRCODE_FAIL;
1620         }
1621 
1622         sp.scan_type = EXT_WIFI_BSSID_SCAN;
1623         memcpy_s(sp.bssid, EXT_WIFI_MAC_LEN, scan_param->bssid, EXT_WIFI_MAC_LEN);
1624     } else {
1625         service_error_log1(SERVICE_ERROR, "Srv:%d: scan_param->scan_type is not supported", __LINE__);
1626         return ERRCODE_FAIL;
1627     }
1628 
1629     return uapi_wifi_sta_raw_scan(&sp, (ext_wifi_scan_no_save_cb)cb) == EXT_WIFI_OK ? ERRCODE_SUCC : ERRCODE_FAIL;
1630 }
1631 
wifi_sta_scan(void)1632 errcode_t wifi_sta_scan(void)
1633 {
1634     uint32_t ret = service_wifi_and_sta_check();
1635     if (ret != ERRCODE_SUCC) {
1636         service_error_log1(SERVICE_ERROR, "Srv:%d:service_wifi_and_sta_check failed", __LINE__);
1637         return ret;
1638     }
1639 
1640     int32_t res = uapi_wifi_sta_scan();
1641     if (res == EXT_WIFI_OK) {
1642         return ERRCODE_SUCC;
1643     } else {
1644         service_error_log1(SERVICE_ERROR, "Srv:%d:failed", __LINE__);
1645         return ERRCODE_FAIL;
1646     }
1647 }
1648 
wifi_sta_scan_advance(const wifi_scan_params_stru * scan_param)1649 errcode_t wifi_sta_scan_advance(const wifi_scan_params_stru *scan_param)
1650 {
1651     ext_wifi_scan_params sp = {0};
1652 
1653     uint32_t ret = service_wifi_and_sta_check();
1654     if (ret != ERRCODE_SUCC) {
1655         service_error_log1(SERVICE_ERROR, "Srv:%d:service_wifi_and_sta_check failed", __LINE__);
1656         return ret;
1657     }
1658 
1659     if (scan_param == NULL) {
1660         service_error_log1(SERVICE_ERROR, "Srv:%d:adv scan_param is null", __LINE__);
1661         return ERRCODE_FAIL;
1662     }
1663 
1664     if (scan_param->scan_type == WIFI_CHANNEL_SCAN) {
1665         sp.chan_list[0] = (uint8_t)scan_param->channel_num;
1666         if (sp.chan_list[0] == 0 || sp.chan_list[0] > 14) {  // 2.4g信道最大14
1667             service_error_log1(SERVICE_ERROR, "Srv:%d: scan_param->freqs is invalid", __LINE__);
1668             return ERRCODE_FAIL;
1669         }
1670         sp.scan_type = EXT_WIFI_CHANNEL_SCAN;
1671         sp.chan_num = 1;
1672     } else if (scan_param->scan_type == WIFI_SSID_SCAN || scan_param->scan_type == WIFI_SSID_PREFIX_SCAN) {
1673         uint32_t len = strlen(scan_param->ssid);
1674         if (scan_param->ssid_len == 0 || scan_param->ssid_len > EXT_WIFI_MAX_SSID_LEN || scan_param->ssid_len != len) {
1675             service_error_log1(SERVICE_ERROR, "Srv:%d: ssid is invalid", __LINE__);
1676             return ERRCODE_FAIL;
1677         }
1678 
1679         sp.scan_type = (scan_param->scan_type == WIFI_SSID_SCAN) ? EXT_WIFI_SSID_SCAN : EXT_WIFI_SSID_PREFIX_SCAN;
1680         if ((memcpy_s(sp.ssid, EXT_WIFI_MAX_SSID_LEN + 1, (const void *)scan_param->ssid,
1681             (size_t)scan_param->ssid_len + 1)) != EOK) {
1682             service_error_log1(SERVICE_ERROR, "Srv:%d:ssid memcpy_s failed", __LINE__);
1683             return ERRCODE_FAIL;
1684         }
1685 
1686         sp.ssid_len = (uint8_t)scan_param->ssid_len;
1687     } else if (scan_param->scan_type == WIFI_BSSID_SCAN) {
1688         if (service_addr_precheck(scan_param->bssid) != ERRCODE_SUCC) {
1689             service_error_log1(SERVICE_ERROR, "Srv:%d: bssid is invalid", __LINE__);
1690             return ERRCODE_FAIL;
1691         }
1692 
1693         sp.scan_type = EXT_WIFI_BSSID_SCAN;
1694         memcpy_s(sp.bssid, EXT_WIFI_MAC_LEN, scan_param->bssid, EXT_WIFI_MAC_LEN);
1695     } else {
1696         service_error_log1(SERVICE_ERROR, "Srv:%d: scan_param->scan_type is not supported", __LINE__);
1697         return ERRCODE_FAIL;
1698     }
1699 
1700     return uapi_wifi_sta_advance_scan(&sp) == EXT_WIFI_OK ? ERRCODE_SUCC : ERRCODE_FAIL;
1701 }
1702 
wifi_sta_get_scan_info(wifi_scan_info_stru * result,uint32_t * size)1703 errcode_t wifi_sta_get_scan_info(wifi_scan_info_stru *result, uint32_t *size)
1704 {
1705     ext_wifi_ap_info *ap_list = NULL;
1706 
1707     uint32_t ret = service_wifi_and_sta_check();
1708     if (ret != ERRCODE_SUCC) {
1709         service_error_log1(SERVICE_ERROR, "Srv:%d:service_wifi_and_sta_check failed", __LINE__);
1710         return ret;
1711     }
1712 
1713     // size 最大为64,超过64底层接口截取按64计算
1714     if (result == NULL || size == NULL || *size == 0 || *size > WIFI_SCAN_AP_LIMIT) {
1715         service_error_log1(SERVICE_ERROR, "Srv:%d: size is invalid", __LINE__);
1716         return ERRCODE_FAIL;
1717     }
1718 
1719     memset_s(result, sizeof(wifi_scan_info_stru) * (*size), 0, sizeof(wifi_scan_info_stru) * (*size));
1720 
1721     ap_list = (ext_wifi_ap_info *)malloc(sizeof(ext_wifi_ap_info) * (*size));
1722     if (ap_list == NULL) {
1723         service_error_log1(SERVICE_ERROR, "Srv:%dGetScanInfoList malloc err.", __LINE__);
1724         return ERRCODE_FAIL;
1725     }
1726 
1727     int32_t res = uapi_wifi_sta_scan_results(ap_list, size);
1728     if (res != EXT_WIFI_OK) {
1729         service_error_log1(SERVICE_ERROR, "Srv:%d:sta_scan_results failed", __LINE__);
1730         free(ap_list);
1731         return ERRCODE_FAIL;
1732     }
1733 
1734     service_error_log2(SERVICE_ERROR, "Srv:%d:sta_scan_results cnt %d", __LINE__, *size);
1735     for (uint32_t i = 0; i < *size; i++) {
1736         uint32_t ssid_len = strlen(ap_list[i].ssid);
1737         if ((memcpy_s(result[i].ssid, WIFI_MAX_SSID_LEN, ap_list[i].ssid, ssid_len + 1)) != EOK) {
1738             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
1739             free(ap_list);
1740             return ERRCODE_FAIL;
1741         }
1742 
1743         if ((memcpy_s(result[i].bssid, WIFI_MAC_LEN, ap_list[i].bssid, WIFI_MAC_LEN)) != EOK) {
1744             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
1745             free(ap_list);
1746             return ERRCODE_FAIL;
1747         }
1748 
1749         result[i].security_type = ap_list[i].auth;
1750         // 扫描结果中的rssi需要除以100才能获得实际的rssi
1751         result[i].rssi = ap_list[i].rssi / 100;
1752         result[i].band = 1;                    // 92只支持2.4g
1753 
1754         result[i].channel_num = (int32_t)ap_list[i].channel;
1755     }
1756     free(ap_list);
1757     return ERRCODE_SUCC;
1758 }
1759 
wifi_sta_scan_result_clear(void)1760 errcode_t wifi_sta_scan_result_clear(void)
1761 {
1762     uint32_t ret = service_wifi_and_sta_check();
1763     if (ret != ERRCODE_SUCC) {
1764         service_error_log1(SERVICE_ERROR, "Srv:%d:service_wifi_and_sta_check failed", __LINE__);
1765         return ret;
1766     }
1767     return uapi_wifi_sta_scan_results_clear() != EXT_WIFI_OK ? ERRCODE_FAIL : ERRCODE_SUCC;
1768 }
1769 
wifi_set_channel(wifi_if_type_enum iftype,int32_t channel)1770 errcode_t wifi_set_channel(wifi_if_type_enum iftype, int32_t channel)
1771 {
1772     const char *ifname = NULL;
1773 
1774     if (iftype >= IFTYPES_BUTT) {
1775         service_error_log0(SERVICE_ERROR, "wifi_set_channel::para is invalid");
1776         return ERRCODE_FAIL;
1777     }
1778     if (iftype == IFTYPE_STA && g_sta_enble_flag == 1) {
1779         ifname = g_sta_ifname;
1780     } else if (iftype == IFTYPE_AP && g_softap_enble_flag == 1) {
1781         ifname = g_hotspot_ifname;
1782     } else if (iftype >= IFTYPE_P2P_CLIENT && iftype <= IFTYPE_P2P_DEVICE && g_p2p_enable_flag == 1) {
1783         ifname = g_p2p_ifname;
1784     } else {
1785         service_error_log1(SERVICE_ERROR, "wifi_set_channel::vap is not enabled or iftype[%d] is invalid", iftype);
1786         return ERRCODE_FAIL;
1787     }
1788 
1789     if (uapi_wifi_set_channel(ifname, (uint8_t)strlen(ifname), channel) != 0) {
1790         service_error_log1(SERVICE_ERROR, "wifi_set_channel::uapi_wifi_set_channel[%d] failed", channel);
1791         return ERRCODE_FAIL;
1792     }
1793 
1794     return ERRCODE_SUCC;
1795 }
1796 
wifi_get_channel(wifi_if_type_enum iftype,int32_t * channel)1797 errcode_t wifi_get_channel(wifi_if_type_enum iftype, int32_t *channel)
1798 {
1799     const char *ifname = NULL;
1800 
1801     if (iftype >= IFTYPES_BUTT) {
1802         service_error_log0(SERVICE_ERROR, "wifi_get_channel::para is invalid");
1803         return ERRCODE_FAIL;
1804     }
1805     if (iftype == IFTYPE_STA && g_sta_enble_flag == 1) {
1806         ifname = g_sta_ifname;
1807     } else if (iftype == IFTYPE_AP && g_softap_enble_flag == 1) {
1808         ifname = g_hotspot_ifname;
1809     } else if (iftype >= IFTYPE_P2P_CLIENT && iftype <= IFTYPE_P2P_DEVICE && g_p2p_enable_flag == 1) {
1810         ifname = g_p2p_ifname;
1811     } else {
1812         service_error_log1(SERVICE_ERROR, "wifi_get_channel::vap is not enabled or iftype[%d] is invalid", iftype);
1813         return ERRCODE_FAIL;
1814     }
1815 
1816     *channel = uapi_wifi_get_channel(ifname, (uint8_t)strlen(ifname));
1817     return ERRCODE_SUCC;
1818 }
1819 
1820 #ifdef CONFIG_WNM
wifi_sta_wnm_bss_query(int32_t reason_code,int32_t candidate_list)1821 errcode_t wifi_sta_wnm_bss_query(int32_t reason_code, int32_t candidate_list)
1822 {
1823     uint32_t ret = service_wifi_and_sta_check();
1824     if (ret != ERRCODE_SUCC) {
1825         service_error_log1(SERVICE_ERROR, "Srv:%d:wifi_sta_wnm_bss_query failed", __LINE__);
1826         return ret;
1827     }
1828     if (uapi_wifi_wnm_bss_query(reason_code, candidate_list) != EXT_WIFI_OK) {
1829         return ERRCODE_FAIL;
1830     }
1831     return ERRCODE_SUCC;
1832 }
1833 #endif
1834 
1835 /* 根据认证方式, 设置pkt_type ;WEP 通过长度判断:5/13 为ascii 10/26为hex */
wifi_conn_filter_sta_key(wifi_sta_config_stru * assoc_req)1836 OSAL_STATIC int32_t wifi_conn_filter_sta_key(wifi_sta_config_stru *assoc_req)
1837 {
1838     uint32_t key_len = strlen(assoc_req->pre_shared_key);
1839 
1840     if (assoc_req->security_type == WIFI_SEC_TYPE_WPA2PSK ||
1841         assoc_req->security_type == WIFI_SEC_TYPE_WPA2_WPA_PSK_MIX ||
1842         assoc_req->security_type == WIFI_SEC_TYPE_WPAPSK) {
1843         if (assoc_req->wifi_psk_type == 0 && key_len == 64) {   /* 64位密钥长度不能设置psk_type为0 */
1844             service_error_log1(SERVICE_ERROR, "Srv:%d:WPA/WPA2/WPA-WPA2 psk_type invalid.", __LINE__);
1845             return ERRCODE_FAIL;
1846         }
1847         /* 8-63位密钥长度不能设置psk_type为1,64位为HEX类型密钥 */
1848         if (assoc_req->wifi_psk_type == 1 && key_len >= 8 && key_len < 64) {
1849             service_error_log1(SERVICE_ERROR, "Srv:%d:WPA/WPA2/WPA-WPA2 psk_type invalid.", __LINE__);
1850             return ERRCODE_FAIL;
1851         }
1852     }
1853 
1854     if (assoc_req->security_type == WIFI_SEC_TYPE_WEP) {
1855         if (assoc_req->wifi_psk_type == 0 && (key_len == 10 || key_len == 26)) {    /* WEP密钥长度为10/26表示HEX类型 */
1856             service_error_log1(SERVICE_ERROR, "Srv:%d:WEP psk_type invalid.", __LINE__);
1857             return ERRCODE_FAIL;
1858         }
1859         if (assoc_req->wifi_psk_type == 1 && (key_len == 5 || key_len == 13)) {     /* WEP密钥长度为5/13表示ASCII类型 */
1860             service_error_log1(SERVICE_ERROR, "Srv:%d:WEP psk_type invalid.", __LINE__);
1861             return ERRCODE_FAIL;
1862         }
1863     }
1864 
1865     return ERRCODE_SUCC;
1866 }
1867 
1868 /* 检查关联请求信息与扫描结果是否匹配不匹配返回0, 匹配返回1 */
wifi_conn_scan_result_cmp(wifi_sta_config_stru * conn_config,ext_wifi_ap_info * ap_list)1869 OSAL_STATIC int32_t wifi_conn_scan_result_cmp(wifi_sta_config_stru *conn_config, ext_wifi_ap_info *ap_list)
1870 {
1871     /* 如果SSID/BSSID都没设置,直接err */
1872     if (strlen(conn_config->ssid) == 0 && service_addr_precheck(conn_config->bssid) != ERRCODE_SUCC) {
1873         return 0;
1874     }
1875 
1876     /* 如果同时设置了SSID与BSSID,两者必须都匹配 */
1877     if (strlen(conn_config->ssid) > 0 && service_addr_precheck(conn_config->bssid) == ERRCODE_SUCC) {
1878         if (strcmp(conn_config->ssid, ap_list->ssid) == 0 &&
1879             memcmp(conn_config->bssid, ap_list->bssid, WIFI_MAC_LEN) == 0) {
1880             return 1;
1881         }
1882         return 0;
1883     }
1884     /* 只设置了SSID OR BSSID, 单项匹配即可 */
1885     if (strlen(conn_config->ssid) == 0) {
1886         return (memcmp(conn_config->bssid, ap_list->bssid, WIFI_MAC_LEN) == 0);
1887     }
1888     return (strcmp(conn_config->ssid, ap_list->ssid) == 0);
1889 }
1890 
wifi_sta_connect_fill_security_type(ext_wifi_assoc_request * req)1891 OSAL_STATIC void wifi_sta_connect_fill_security_type(ext_wifi_assoc_request *req)
1892 {
1893     uint32_t num = WIFI_SCAN_AP_LIMIT;
1894     int32_t rssi = -128; /* rssi最小值 */
1895     uint8_t ft_flag = 0;
1896     wifi_security_enum security_type = WIFI_SEC_TYPE_INVALID;
1897     ext_wifi_ap_info *ap_list = NULL;
1898     ext_wifi_pairwise pairwise = EXT_WIFI_PARIWISE_UNKNOWN;
1899     int32_t ret;
1900 
1901     ap_list = (ext_wifi_ap_info *)malloc(sizeof(ext_wifi_ap_info) * num);
1902     if (ap_list == NULL) {
1903         service_error_log1(SERVICE_ERROR, "Srv:%dwifi_sta_connect_fill_security_type malloc err.", __LINE__);
1904         return;
1905     }
1906 
1907     ret = uapi_wifi_sta_scan_results(ap_list, &num);
1908     if (ret != EXT_WIFI_OK) {
1909         service_error_log1(SERVICE_ERROR, "Srv:%d:sta_scan_results failed", __LINE__);
1910         free(ap_list);
1911         return;
1912     }
1913 
1914     /* 根据SSID匹配认证方式 */
1915     for (int32_t i = 0; (i < num) && (i < WIFI_SCAN_AP_LIMIT); i++) {
1916         if ((wifi_conn_scan_result_cmp(&g_sta_conn_config, &ap_list[i]) == 0) ||
1917             (g_sta_conn_config.channel != 0 && ap_list[i].channel != g_sta_conn_config.channel)) {
1918             continue;
1919         }
1920         /* 多个匹配情况,取RSSI大者, 扫描结果中的rssi需要除以100才能获得实际的rssi */
1921         if (ap_list[i].rssi / 100 > rssi) {
1922             security_type = ap_list[i].auth;
1923             pairwise = ap_list[i].pairwise;
1924             rssi = ap_list[i].rssi / 100; /* 除以100获得实际的rssi */
1925             ft_flag = ap_list[i].ft_flag;
1926             if (g_sta_conn_config.channel != 0 && memcpy_s(g_sta_conn_config.bssid, sizeof(g_sta_conn_config.bssid),
1927                 ap_list[i].bssid, sizeof(ap_list[i].bssid)) != EOK) {
1928                 free(ap_list);
1929                 return;
1930             }
1931             service_error_log4(SERVICE_ERROR, "Srv:find ssid[%s] auth type[%d] pairwise[%d] ft_flag[%d]",
1932                 g_sta_conn_config.ssid, security_type, pairwise, ft_flag);
1933         }
1934     }
1935 
1936     free(ap_list);
1937 
1938     g_sta_conn_config.security_type = security_type;
1939     req->pairwise = pairwise;
1940     req->ft_flag = ft_flag;
1941 }
1942 
1943 /* 校验关联参数中的密码与加密类型是否匹配 */
wifi_sta_pre_check_key(wifi_sta_config_stru * sta_conn_config)1944 OAL_STATIC osal_u8 wifi_sta_pre_check_key(wifi_sta_config_stru *sta_conn_config)
1945 {
1946     if (sta_conn_config == OSAL_NULL) {
1947         return OSAL_FALSE;
1948     }
1949     /* 仅在strengthen_verify为1时才进行校验 */
1950     if (sta_conn_config->strengthen_verify == 0) {
1951         return OSAL_TRUE;
1952     }
1953     /* 带密码关联OPEN场景 */
1954     if (sta_conn_config->security_type == WIFI_SEC_TYPE_OPEN && strlen(sta_conn_config->pre_shared_key) != 0) {
1955         return OSAL_FALSE;
1956     }
1957     /* 无密码关联加密场景 */
1958     if (sta_conn_config->security_type > WIFI_SEC_TYPE_OPEN && strlen(sta_conn_config->pre_shared_key) == 0) {
1959         return OSAL_FALSE;
1960     }
1961     return OSAL_TRUE;
1962 }
1963 
1964 /* 关联下半段 */
wifi_sta_connect_part2(void)1965 OAL_STATIC errcode_t wifi_sta_connect_part2(void)
1966 {
1967     ext_wifi_assoc_request req = {0};
1968     event_wifi_disconnected wifi_disconnected = {0};
1969     char wep_key[WIFI_MAX_KEY_LEN];
1970     (void)memset_s(wep_key, WIFI_MAX_KEY_LEN, 0, WIFI_MAX_KEY_LEN);
1971 
1972     g_sta_conn_req_flag = 0;
1973     /* 根据扫描结果查询认证方式 */
1974     wifi_sta_connect_fill_security_type(&req);
1975 
1976     if (g_sta_conn_config.security_type == WIFI_SEC_TYPE_INVALID || wifi_sta_pre_check_key(&g_sta_conn_config) == OSAL_FALSE) {
1977         /* 未扫到对应AP的情况下,上报错误码 */
1978         wifi_disconnected.reason_code = WIFI_NETWORK_NOT_FOUND_ERROR;
1979         wifi_disconnected.locally_generated = 1;
1980         if (memcpy_s(wifi_disconnected.ifname, sizeof(wifi_disconnected.ifname),
1981             g_sta_ifname, sizeof(g_sta_ifname)) != EOK) {
1982             return ERRCODE_FAIL;
1983         }
1984         send_broadcast_disconnected(&wifi_disconnected);
1985         service_error_log1(SERVICE_ERROR, "Srv:%d: not found suitable SSID and auth type", __LINE__);
1986         return ERRCODE_FAIL;
1987     }
1988     /* 如果找到了, 走完关联下半段 */
1989     if (wifi_is_need_psk(g_sta_conn_config.security_type) == 1) {
1990         wifi_conn_filter_sta_key(&g_sta_conn_config);
1991     }
1992 
1993     uint32_t ret = service_check_pre_shared_key(g_sta_conn_config.pre_shared_key, g_sta_conn_config.security_type,
1994         g_sta_conn_config.wifi_psk_type, wep_key);
1995     if (ret != ERRCODE_SUCC) {
1996         /* 密码校验错误,上报错误码 */
1997         wifi_disconnected.reason_code = WLAN_REASON_MIC_FAILURE;
1998         wifi_disconnected.locally_generated = 1;
1999         if (memcpy_s(wifi_disconnected.ifname, sizeof(wifi_disconnected.ifname),
2000             g_sta_ifname, sizeof(g_sta_ifname)) != EOK) {
2001             return ERRCODE_FAIL;
2002         }
2003         send_broadcast_disconnected(&wifi_disconnected);
2004         service_error_log1(SERVICE_ERROR, "Srv:%d: config->pre_shared_key is invalid", __LINE__);
2005         return ret;
2006     }
2007 
2008     ret = service_set_assoc_config(&g_sta_conn_config, &req, wep_key);
2009     if (ret != ERRCODE_SUCC) {
2010         service_error_log1(SERVICE_ERROR, "Srv:%d: service_set_assoc_config failed", __LINE__);
2011         return ret;
2012     }
2013 
2014     if (uapi_wifi_sta_connect(&req) != EXT_WIFI_OK) {
2015         service_error_log1(SERVICE_ERROR, "Srv:%d:failed", __LINE__);
2016         return ERRCODE_FAIL;
2017     } else {
2018         return ERRCODE_SUCC;
2019     }
2020 }
2021 
wifi_sta_connect_fill_scan_param(const wifi_sta_config_stru * config,wifi_scan_params_stru * scan_param)2022 static void wifi_sta_connect_fill_scan_param(const wifi_sta_config_stru *config, wifi_scan_params_stru *scan_param)
2023 {
2024     (void)memset_s(scan_param, sizeof(wifi_scan_params_stru), 0, sizeof(wifi_scan_params_stru));
2025 
2026     /* 设置SSID */
2027     if (strlen(config->ssid) > 0) {
2028         service_error_log0(SERVICE_ERROR, "wifi_sta_connect_fill_scan_param:: Specifying an SSID for scanning");
2029         scan_param->scan_type = WIFI_SSID_SCAN;
2030         (void)memcpy_s(scan_param->ssid, WIFI_MAX_SSID_LEN, config->ssid, WIFI_MAX_SSID_LEN);
2031         scan_param->ssid_len = (int8_t)strlen(config->ssid);
2032     }
2033 }
2034 
2035 /* 关联上半段, 如果5s内没扫描过,触发扫描,在扫描结果上报时走完下半段 */
wifi_sta_connect_part1(const wifi_sta_config_stru * config)2036 OSAL_STATIC errcode_t wifi_sta_connect_part1(const wifi_sta_config_stru *config)
2037 {
2038     uint64_t current_time_stamp_ms = wifi_get_timestamp_ms();
2039     wifi_scan_params_stru scan_param = {0};
2040 
2041     /* 保存请求关联的信息, 认证方式置为无效,由扫描结果获取 */
2042     if (memcpy_s(&g_sta_conn_config, sizeof(wifi_sta_config_stru), config, sizeof(wifi_sta_config_stru)) != EOK) {
2043         service_error_log1(SERVICE_ERROR, "Srv:%d: memcpy_s fail", __LINE__);
2044         return ERRCODE_FAIL;
2045     }
2046     g_sta_conn_config.security_type = WIFI_SEC_TYPE_INVALID;
2047 
2048     /* 如果当前正在扫描,在扫描完成回调执行关联的下半段 */
2049     if (uapi_wifi_get_scan_flag() != 0) {
2050         service_error_log1(SERVICE_ERROR, "Srv:%d: Trigger association after the scanning is complete", __LINE__);
2051         g_sta_conn_req_flag = 1;
2052         return ERRCODE_SUCC;
2053     }
2054 
2055     /* 5s内没扫描, 且当前没有扫描, 则主动触发扫描,在扫描结束后执行关联的下半段 */
2056     if ((current_time_stamp_ms - g_sta_last_scan_time_stamp_ms >= WIFI_CONN_SCAN_INTERVAL) ||
2057         (g_sta_last_scan_time_stamp_ms == 0)) {
2058         service_error_log1(SERVICE_ERROR, "Srv:%d: last scantime > 5s, trigger scan", __LINE__);
2059         g_sta_conn_req_flag = 1;
2060         wifi_sta_connect_fill_scan_param(config, &scan_param);
2061         return wifi_sta_scan_advance(&scan_param);
2062     } else {
2063         /* 如果5s内扫描过,直接走关联下半段 */
2064         return wifi_sta_connect_part2();
2065     }
2066 
2067     return ERRCODE_SUCC;
2068 }
2069 
wifi_sta_connect(const wifi_sta_config_stru * config)2070 errcode_t wifi_sta_connect(const wifi_sta_config_stru *config)
2071 {
2072     uint32_t ret = service_wifi_and_sta_check();
2073     if (ret != ERRCODE_SUCC) {
2074         service_error_log1(SERVICE_ERROR, "Srv:%d:service_wifi_and_sta_check failed", __LINE__);
2075         return ret;
2076     }
2077 
2078     ret = service_check_wifi_device_config(config);
2079     if (ret != ERRCODE_SUCC) {
2080         service_error_log1(SERVICE_ERROR, "Srv:%d:service_check_wifi_device_config failed", __LINE__);
2081         return ret;
2082     }
2083 
2084     ret = service_set_ip(config);
2085     if (ret != ERRCODE_SUCC) {
2086         service_error_log1(SERVICE_ERROR, "Srv:%d: ip is invalid", __LINE__);
2087         return ret;
2088     }
2089 
2090     return wifi_sta_connect_part1(config);
2091 }
2092 
wifi_sta_disconnect(void)2093 errcode_t wifi_sta_disconnect(void)
2094 {
2095     uint32_t ret = service_wifi_and_sta_check();
2096     if (ret != ERRCODE_SUCC) {
2097         service_error_log1(SERVICE_ERROR, "Srv:%d:service_wifi_and_sta_check failed", __LINE__);
2098         return ret;
2099     }
2100     /* 下发DISCONN时, 将关联请求置为false, 终止关联动作 */
2101     g_sta_conn_req_flag = 0;
2102     int32_t res = uapi_wifi_sta_disconnect();
2103     if (res == EXT_WIFI_OK) {
2104         return ERRCODE_SUCC;
2105     } else {
2106         service_error_log1(SERVICE_ERROR, "Srv:%d:failed", __LINE__);
2107         return ERRCODE_FAIL;
2108     }
2109 }
2110 
wifi_sta_get_ap_info(wifi_linked_info_stru * result)2111 errcode_t wifi_sta_get_ap_info(wifi_linked_info_stru *result)
2112 {
2113     uint32_t ret = service_wifi_and_sta_check();
2114     if (ret != ERRCODE_SUCC) {
2115         service_error_log1(SERVICE_ERROR, "Srv:%d:service_wifi_and_sta_check failed", __LINE__);
2116         return ret;
2117     }
2118 
2119     if (result == NULL) {
2120         service_error_log1(SERVICE_ERROR, "Srv:%d: result is NULL", __LINE__);
2121         return ERRCODE_FAIL;
2122     }
2123 
2124     memset_s(result, sizeof(wifi_linked_info_stru), 0, sizeof(wifi_linked_info_stru));
2125 
2126     ext_wifi_status connect_status = {0};
2127     int32_t res = uapi_wifi_sta_get_connect_info(&connect_status);
2128     if (res != EXT_WIFI_OK) {
2129         service_error_log1(SERVICE_ERROR, "Srv:%d:get_connect_info failed", __LINE__);
2130         return ERRCODE_FAIL;
2131     }
2132 
2133     result->conn_state = connect_status.status;
2134 
2135     if ((memcpy_s(result->ssid, WIFI_MAX_SSID_LEN, connect_status.ssid, strlen(connect_status.ssid) + 1)) != EOK) {
2136         service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
2137         return ERRCODE_FAIL;
2138     }
2139 
2140     if ((memcpy_s(result->bssid, WIFI_MAC_LEN, connect_status.bssid, WIFI_MAC_LEN)) != EOK) {
2141         service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
2142         return ERRCODE_FAIL;
2143     }
2144 
2145     result->channel_num = (int32_t)connect_status.channel;
2146 
2147     result->rssi = uapi_wifi_sta_get_ap_rssi();
2148     result->snr = uapi_wifi_sta_get_ap_snr();
2149 
2150     return ERRCODE_SUCC;
2151 }
2152 
wifi_sta_set_reconnect_policy(int32_t enable,uint32_t seconds,uint32_t period,uint32_t max_try_count)2153 errcode_t wifi_sta_set_reconnect_policy(int32_t enable, uint32_t seconds,
2154     uint32_t period, uint32_t max_try_count)
2155 {
2156     uint32_t ret = service_wifi_and_sta_check();
2157     if (ret != ERRCODE_SUCC) {
2158         service_error_log1(SERVICE_ERROR, "Srv:%d:service_wifi_and_sta_check failed", __LINE__);
2159         return ret;
2160     }
2161     int32_t res = uapi_wifi_sta_set_reconnect_policy(enable, seconds, period, max_try_count);
2162     if (res == EXT_WIFI_OK) {
2163         return ERRCODE_SUCC;
2164     } else {
2165         service_error_log1(SERVICE_ERROR, "Srv:%d:failed", __LINE__);
2166         return ERRCODE_FAIL;
2167     }
2168 }
2169 
wifi_sta_set_pmf_mode(wifi_pmf_option_enum pmf)2170 errcode_t wifi_sta_set_pmf_mode(wifi_pmf_option_enum pmf)
2171 {
2172     int32_t ret = uapi_wifi_set_pmf(pmf);
2173     if (ret != EXT_WIFI_OK) {
2174         service_error_log1(SERVICE_ERROR, "Srv:%d:set pmf failed", __LINE__);
2175         return ERRCODE_FAIL;
2176     }
2177     return ERRCODE_SUCC;
2178 }
2179 
wifi_sta_get_pmf_mode(void)2180 wifi_pmf_option_enum wifi_sta_get_pmf_mode(void)
2181 {
2182     return uapi_wifi_get_pmf();
2183 }
2184 
wifi_sta_get_connect_status_code(void)2185 int16_t wifi_sta_get_connect_status_code(void)
2186 {
2187     return uapi_wifi_get_mac_status_code();
2188 }
2189 
wifi_set_mgmt_frame_rx_cb(wifi_rx_mgmt_cb data_cb,uint8_t mode)2190 errcode_t wifi_set_mgmt_frame_rx_cb(wifi_rx_mgmt_cb data_cb, uint8_t mode)
2191 {
2192     if (uapi_wifi_set_mgmt_report(data_cb, mode) != 0) {
2193         return ERRCODE_FAIL;
2194     }
2195     return ERRCODE_SUCC;
2196 }
2197 
wifi_set_promis_mode(wifi_if_type_enum iftype,int32_t enable,const wifi_ptype_filter_stru * filter)2198 errcode_t wifi_set_promis_mode(wifi_if_type_enum iftype,
2199     int32_t enable, const wifi_ptype_filter_stru *filter)
2200 {
2201     const char *ifname = NULL;
2202 
2203     if (iftype >= IFTYPES_BUTT) {
2204         service_error_log1(SERVICE_ERROR, "Srv:%d:para is invalid", __LINE__);
2205         return ERRCODE_FAIL;
2206     }
2207     if (iftype == IFTYPE_STA && g_sta_enble_flag == 1) {
2208         ifname = g_sta_ifname;
2209     } else if (iftype == IFTYPE_AP && g_softap_enble_flag == 1) {
2210         ifname = g_hotspot_ifname;
2211     } else if (iftype >= IFTYPE_P2P_CLIENT && iftype <= IFTYPE_P2P_DEVICE && g_p2p_enable_flag == 1) {
2212         ifname = g_p2p_ifname;
2213     } else {
2214         service_error_log1(SERVICE_ERROR, "Srv:%d:vap is not enabled or iftype is invalid", __LINE__);
2215         return ERRCODE_FAIL;
2216     }
2217     if (uapi_wifi_promis_enable(ifname, enable, (const ext_wifi_ptype_filter_stru *)filter) != 0) {
2218         service_error_log1(SERVICE_ERROR, "Srv:%d:uapi_wifi_promis_enable failed", __LINE__);
2219         return ERRCODE_FAIL;
2220     }
2221     return ERRCODE_SUCC;
2222 }
2223 #ifdef _PRE_WLAN_FEATURE_SDP
wifi_set_sdp_mode_config(wifi_if_type_enum iftype,char ** ifname,uint32_t line_num)2224 static errcode_t wifi_set_sdp_mode_config(wifi_if_type_enum iftype, char **ifname, uint32_t line_num)
2225 {
2226     if (iftype >= IFTYPES_BUTT) {
2227         service_error_log1(SERVICE_ERROR, "Srv:%d:iftype is invalid", line_num);
2228         return ERRCODE_FAIL;
2229     }
2230     if (iftype == IFTYPE_STA && g_sta_enble_flag == 1) {
2231         *ifname = g_sta_ifname;
2232     } else if (iftype == IFTYPE_AP && g_softap_enble_flag == 1) {
2233         *ifname = g_hotspot_ifname;
2234     } else if (iftype >= IFTYPE_P2P_CLIENT && iftype <= IFTYPE_P2P_DEVICE && g_p2p_enable_flag == 1) {
2235         *ifname = g_p2p_ifname;
2236     } else {
2237         service_error_log1(SERVICE_ERROR, "Srv:%d:vap is not enabled or iftype is invalid", line_num);
2238         return ERRCODE_FAIL;
2239     }
2240     return ERRCODE_SUCC;
2241 }
2242 
wifi_set_sdp_mode(wifi_if_type_enum iftype,int32_t enable,int32_t ratio)2243 errcode_t wifi_set_sdp_mode(wifi_if_type_enum iftype, int32_t enable, int32_t ratio)
2244 {
2245     char *ifname = NULL;
2246 
2247     if (wifi_set_sdp_mode_config(iftype, &ifname, __LINE__) != ERRCODE_SUCC) {
2248         return ERRCODE_FAIL;
2249     }
2250 
2251     if (uapi_wifi_sdp_enable(ifname, enable, ratio) != 0) {
2252         service_error_log1(SERVICE_ERROR, "Srv:%d:uapi_wifi_promis_enable failed", __LINE__);
2253         return ERRCODE_FAIL;
2254     }
2255     return ERRCODE_SUCC;
2256 }
2257 
wifi_set_sdp_subscribe(wifi_if_type_enum iftype,char * sdp_subscribe,int32_t local_handle)2258 errcode_t wifi_set_sdp_subscribe(wifi_if_type_enum iftype, char *sdp_subscribe, int32_t local_handle)
2259 {
2260     char *ifname = NULL;
2261 
2262     if (wifi_set_sdp_mode_config(iftype, &ifname, __LINE__) != ERRCODE_SUCC) {
2263         return ERRCODE_FAIL;
2264     }
2265 
2266     if (uapi_wifi_sdp_subscribe(ifname, sdp_subscribe, local_handle) != 0) {
2267         service_error_log1(SERVICE_ERROR, "Srv:%d:uapi_wifi_promis_enable failed", __LINE__);
2268         return ERRCODE_FAIL;
2269     }
2270     return ERRCODE_SUCC;
2271 }
2272 #endif
wifi_set_promis_rx_pkt_cb(wifi_promis_cb data_cb)2273 errcode_t wifi_set_promis_rx_pkt_cb(wifi_promis_cb data_cb)
2274 {
2275     if (uapi_wifi_promis_set_rx_callback(data_cb) != 0) {
2276         return ERRCODE_FAIL;
2277     }
2278     return ERRCODE_SUCC;
2279 }
2280 
2281 #ifdef CONFIG_WPS_SUPPORT
wps_connect(const wifi_wps_config * wps_config)2282 wifi_return_code wps_connect(const wifi_wps_config *wps_config)
2283 {
2284     wifi_wps_config  *wps_config_tmp = NULL;
2285 
2286     int ret = service_wifi_and_sta_check();
2287     if (ret != ERRCODE_SUCC) {
2288         service_error_log1(SERVICE_ERROR, "Srv:%d:service_wifi_and_sta_check failed", __LINE__);
2289         return ret;
2290     }
2291     if (wps_config == NULL || (wps_config->wps_method >= WIFI_WPS_BUTT)) {
2292         service_error_log1(SERVICE_ERROR, "Srv:%d:wps_config.wps_method is invalid", __LINE__);
2293         return ERROR_WIFI_INVALID_ARGS;
2294     }
2295 
2296     wps_config_tmp = (wifi_wps_config *)malloc(sizeof(wifi_wps_config));
2297     if (wps_config_tmp == NULL) {
2298         service_error_log1(SERVICE_ERROR, "Srv:%d:wifi_wps_config malloc err.", __LINE__);
2299         return ERROR_WIFI_UNKNOWN;
2300     }
2301 
2302     if (memcpy_s(wps_config_tmp, sizeof(wifi_wps_config), wps_config, sizeof(wifi_wps_config)) != EOK) {
2303         service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
2304         free(wps_config_tmp);
2305         return ERROR_WIFI_INVALID_ARGS;
2306     }
2307     ret = service_pbc_pin_connect(wps_config_tmp);
2308 
2309     free(wps_config_tmp);
2310     if (ret == ERRCODE_SUCC) {
2311         return ERRCODE_SUCC;
2312     } else {
2313         service_error_log1(SERVICE_ERROR, "Srv:%d:failed", __LINE__);
2314         return ERROR_WIFI_UNKNOWN;
2315     }
2316 }
2317 #endif /* CONFIG_WPS_SUPPORT */
2318 
wifi_sta_fast_connect(const wifi_fast_connect_stru * fast_request)2319 errcode_t wifi_sta_fast_connect(const wifi_fast_connect_stru *fast_request)
2320 {
2321     ext_wifi_fast_assoc_request  fast_request_temp = {0};
2322     int8_t wep_key[WIFI_MAX_KEY_LEN];
2323 
2324     if (fast_request->psk_flag != WIFI_WPA_PSK_NOT_USE) {
2325         service_error_log1(SERVICE_ERROR, "Srv:%d:psk_flag is invalid", __LINE__);
2326         return ERRCODE_FAIL;
2327     }
2328 
2329     uint32_t ret = service_wifi_and_sta_check();
2330     if (ret != ERRCODE_SUCC) {
2331         service_error_log1(SERVICE_ERROR, "Srv:%d:service_wifi_and_sta_check failed", __LINE__);
2332         return ret;
2333     }
2334 
2335     ret = service_check_wifi_device_config(&fast_request->config);
2336     if (ret != ERRCODE_SUCC) {
2337         service_error_log1(SERVICE_ERROR, "Srv:%d:service_check_wifi_device_config failed", __LINE__);
2338         return ret;
2339     }
2340 
2341     ret = service_set_ip(&fast_request->config);
2342     if (ret != ERRCODE_SUCC) {
2343         service_error_log1(SERVICE_ERROR, "Srv:%d: ip is invalid", __LINE__);
2344         return ret;
2345     }
2346 
2347     ret = service_check_pre_shared_key(fast_request->config.pre_shared_key, fast_request->config.security_type,
2348                                        fast_request->config.wifi_psk_type, wep_key);
2349     if (ret != ERRCODE_SUCC) {
2350         service_error_log1(SERVICE_ERROR, "Srv:%d: config->pre_shared_key is invalid", __LINE__);
2351         return ret;
2352     }
2353 
2354     ret = service_set_assoc_config(&fast_request->config, &fast_request_temp.req, wep_key);
2355     if (ret != ERRCODE_SUCC) {
2356         service_error_log1(SERVICE_ERROR, "Srv:%d: service_set_assoc_config failed", __LINE__);
2357         return ret;
2358     }
2359 
2360     // 2.4g信道最大14
2361     if (fast_request->channel_num > 14 || fast_request->channel_num == 0) {
2362         service_error_log1(SERVICE_ERROR, "Srv:%d: 5g is not supported", __LINE__);
2363         return ERRCODE_FAIL;
2364     } else {
2365         fast_request_temp.channel = fast_request->channel_num;
2366     }
2367 
2368     fast_request_temp.psk_flag = 0;
2369 
2370     if (uapi_wifi_sta_fast_connect(&fast_request_temp) == EXT_WIFI_OK) {
2371         return ERRCODE_SUCC;
2372     } else {
2373         service_error_log1(SERVICE_ERROR, "Srv:%d:sta_fast_connect failed", __LINE__);
2374         return ERRCODE_FAIL;
2375     }
2376 }
2377 
2378 #if defined(_PRE_WLAN_FEATURE_WOW_OFFLOAD) || defined(CONFIG_WOW_OFFLOAD)
wifi_set_wow_pattern(int32_t type,uint8_t index,int8_t * pattern)2379 errcode_t wifi_set_wow_pattern(int32_t type, uint8_t index, int8_t *pattern)
2380 {
2381     int32_t ret, len;
2382     ret = service_wifi_and_sta_check();
2383     if (ret != WIFI_SUCCESS) {
2384         service_error_log2(SERVICE_ERROR, "Srv:%d:service_wifi_and_sta_check fail ret[%d]", __LINE__, ret);
2385         return ERRCODE_FAIL;
2386     }
2387 
2388     len = (int)strlen(pattern);
2389     // pattern的大小为1至64, index范围为0至3, 2奇偶校验
2390     if ((len > 64) || (index > 3) || (len % 2 != 0) || (type < WOW_PATTERN_ADD || type >= WOW_PATTERN_BUTT)) {
2391         service_error_log1(SERVICE_ERROR, "Srv:%d:parameter is invalid.", __LINE__);
2392         return ERRCODE_INVALID_PARAM;
2393     }
2394 
2395     ret = uapi_wifi_set_wow_pattern((td_u8)type, index, pattern);
2396     if (ret == 0) {
2397         return ERRCODE_SUCC;
2398     } else {
2399         service_error_log1(SERVICE_ERROR, "Srv:%d:set_wow_pattern failed.", __LINE__);
2400         return ERRCODE_FAIL;
2401     }
2402 }
2403 
wifi_set_wow_sleep_mode(uint8_t en)2404 errcode_t wifi_set_wow_sleep_mode(uint8_t en)
2405 {
2406     int32_t ret;
2407 
2408     ret = service_wifi_and_sta_check();
2409     if (ret != WIFI_SUCCESS) {
2410         service_error_log2(SERVICE_ERROR, "Srv:%d:sta_check failed ret[%d]", __LINE__, ret);
2411         return ERRCODE_FAIL;
2412     }
2413 
2414     if (en != 0 && en != 1) {
2415         service_error_log1(SERVICE_ERROR, "Srv:%d:en is invalid.", __LINE__);
2416         return ERRCODE_INVALID_PARAM;
2417     }
2418 
2419     ret = uapi_wifi_set_wow_switch(en);
2420     if (ret == 0) {
2421         return ERRCODE_SUCC;
2422     } else {
2423         service_error_log1(SERVICE_ERROR, "Srv:%d:set_wow_switch failed.", __LINE__);
2424         return ERRCODE_FAIL;
2425     }
2426 }
2427 #endif
2428 
wifi_send_custom_pkt(const wifi_if_type_enum iftype,const uint8_t * data,uint32_t len)2429 errcode_t wifi_send_custom_pkt(const wifi_if_type_enum iftype, const uint8_t *data, uint32_t len)
2430 {
2431     char *ifname = NULL;
2432 
2433     if (uapi_wifi_get_init_status() != 1) {
2434         service_error_log1(SERVICE_ERROR, "Srv:%d:WiFi is not initialized", __LINE__);
2435         return ERRCODE_NOT_SUPPORT;
2436     }
2437 
2438     /* 待发送帧的内容长度为24到1400 */
2439     if (data == NULL || len < WIFI_SENDPKT_MIN_LEN || len > WIFI_SENDPKT_MAX_LEN) {
2440         service_error_log1(SERVICE_ERROR, "Srv:%d:parameter is invalid.", __LINE__);
2441         return ERRCODE_INVALID_PARAM;
2442     }
2443 
2444     if (iftype >= IFTYPES_BUTT) {
2445         service_error_log1(SERVICE_ERROR, "Srv:%d:para is invalid", __LINE__);
2446         return ERRCODE_INVALID_PARAM;
2447     }
2448     if (iftype == IFTYPE_STA && g_sta_enble_flag == 1) {
2449         ifname = g_sta_ifname;
2450     } else if (iftype == IFTYPE_AP && g_softap_enble_flag == 1) {
2451         ifname = g_hotspot_ifname;
2452     } else if (((iftype == IFTYPE_P2P_CLIENT) || (iftype == IFTYPE_P2P_GO) || (iftype == IFTYPE_P2P_DEVICE)) &&
2453         g_p2p_enable_flag == 1) {
2454         ifname = g_p2p_ifname;
2455     } else {
2456         service_error_log1(SERVICE_ERROR, "Srv:%d:vap is not enabled or iftype is invalid", __LINE__);
2457         return ERRCODE_INVALID_PARAM;
2458     }
2459 
2460     if (uapi_wifi_send_custom_pkt(ifname, data, len) == 0) {
2461         return ERRCODE_SUCC;
2462     }
2463     service_error_log1(SERVICE_ERROR, "Srv:%d:uapi_wifi_send_custom_pkt failed.", __LINE__);
2464     return ERRCODE_FAIL;
2465 }
2466 
2467 #ifdef _PRE_WLAN_FEATURE_CSI
wifi_set_csi_config(const int8_t * ifname,const csi_config_stru * config)2468 errcode_t wifi_set_csi_config(const int8_t *ifname, const csi_config_stru *config)
2469 {
2470     if (config == NULL || (strcmp(ifname, g_sta_ifname) != 0 &&
2471             strcmp(ifname, g_hotspot_ifname) != 0 && strcmp(ifname, g_p2p_ifname) != 0)) {
2472         service_error_log1(SERVICE_ERROR, "Srv:%d:param is invalid", __LINE__);
2473         return ERRCODE_FAIL;
2474     }
2475 
2476     if (wifi_is_wifi_inited() == 0) {
2477         return ERROR_WIFI_NOT_STARTED;
2478     }
2479     uapi_wifi_csi_set_config(ifname, (const ext_csi_config *)config);
2480     return ERRCODE_SUCC;
2481 }
2482 
wifi_register_csi_report_cb(wifi_csi_data_cb data_cb)2483 errcode_t wifi_register_csi_report_cb(wifi_csi_data_cb data_cb)
2484 {
2485     uapi_csi_register_data_report_cb(data_cb);
2486     return ERRCODE_SUCC;
2487 }
wifi_csi_start(void)2488 errcode_t wifi_csi_start(void)
2489 {
2490     int32_t ret;
2491 
2492     if (wifi_is_wifi_inited() == 0) {
2493         return ERRCODE_FAIL;
2494     }
2495 
2496     if (g_csi_enable_flag == 0) {
2497         ret = uapi_wifi_csi_start();
2498     } else {
2499         return ERRCODE_FAIL;
2500     }
2501     if (ret == EXT_WIFI_OK) {
2502         g_csi_enable_flag = 1;
2503         return ERRCODE_SUCC;
2504     } else {
2505         service_error_log1(SERVICE_ERROR, "Srv:%d: start csi failed", __LINE__);
2506         return ERRCODE_FAIL;
2507     }
2508 }
2509 
wifi_csi_stop(void)2510 errcode_t wifi_csi_stop(void)
2511 {
2512     int32_t ret;
2513 
2514     if (wifi_is_wifi_inited() == 0) {
2515         return ERRCODE_FAIL;
2516     }
2517     ret = uapi_wifi_csi_stop();
2518     if (ret == EXT_WIFI_OK) {
2519         g_csi_enable_flag = 0;
2520         return ERRCODE_SUCC;
2521     } else {
2522         service_error_log1(SERVICE_ERROR, "Srv:%d: stop csi failed", __LINE__);
2523         return ERRCODE_FAIL;
2524     }
2525 }
2526 #endif
2527 
wifi_set_softap_config_advance(const softap_config_advance_stru * config)2528 errcode_t wifi_set_softap_config_advance(const softap_config_advance_stru *config)
2529 {
2530     if (config == NULL || (config->gi != 0 && config->gi != 1)) {
2531         service_error_log1(SERVICE_ERROR, "Srv:%d:config is invalid", __LINE__);
2532         return ERRCODE_FAIL;
2533     }
2534 
2535     if (g_softap_enble_flag == 1 || uapi_wifi_get_init_status() != 1) {
2536         service_error_log1(SERVICE_ERROR, "Srv:%d:Softap is enabled or Wi-Fi is not initialized.", __LINE__);
2537         return ERRCODE_FAIL;
2538     }
2539 
2540     uint32_t ret = service_check_hotspot_config_advance(config);
2541     if (ret != ERRCODE_SUCC) {
2542         service_error_log1(SERVICE_ERROR, "Srv:%d:config is invalid", __LINE__);
2543         return ret;
2544     }
2545 
2546     if (uapi_wifi_softap_set_beacon_period((int32_t)config->beacon_interval) != EXT_WIFI_OK) {
2547         service_error_log1(SERVICE_ERROR, "Srv:%d:set_beacon_period failed", __LINE__);
2548         return ERRCODE_FAIL;
2549     }
2550 
2551     if (uapi_wifi_softap_set_dtim_period((int32_t)config->dtim_period) != EXT_WIFI_OK) {
2552         service_error_log1(SERVICE_ERROR, "Srv:%d:set_dtim_period failed", __LINE__);
2553         return ERRCODE_FAIL;
2554     }
2555 
2556     if (uapi_wifi_softap_set_group_rekey((int32_t)config->group_rekey) != EXT_WIFI_OK) {
2557         service_error_log1(SERVICE_ERROR, "Srv:%d:set_group_rekey failed", __LINE__);
2558         return ERRCODE_FAIL;
2559     }
2560 
2561     /* 1 不隐藏 2 隐藏 */
2562     if (config->hidden_ssid_flag < 1 || config->hidden_ssid_flag > 2) {
2563         service_error_log1(SERVICE_ERROR, "Srv:%d:set_hidden_ssid_flag failed", __LINE__);
2564         return ERRCODE_FAIL;
2565     }
2566     g_softap_hidden = (int32_t)config->hidden_ssid_flag;
2567 
2568     if (uapi_wifi_softap_set_shortgi((int32_t)config->gi) != EXT_WIFI_OK) {
2569         service_error_log1(SERVICE_ERROR, "Srv:%d:set_shortgi failed", __LINE__);
2570         return ERRCODE_FAIL;
2571     }
2572 
2573     if (config->protocol_mode != 0) {
2574         ret = service_set_softap_protocol(config->protocol_mode);
2575         if (ret != ERRCODE_SUCC) {
2576             service_error_log1(SERVICE_ERROR, "Srv:%d:set_protocol_mod failed", __LINE__);
2577             return ret;
2578         }
2579     }
2580 
2581     return ERRCODE_SUCC;
2582 }
wifi_check_softap_config(const softap_config_stru * config)2583 static errcode_t wifi_check_softap_config(const softap_config_stru *config)
2584 {
2585     if (config == NULL || (config->wifi_psk_type != 0 && config->wifi_psk_type != 1)) {
2586         service_error_log1(SERVICE_ERROR, "Srv:%d:config is invalid", __LINE__);
2587         return ERRCODE_FAIL;
2588     }
2589 
2590     if (uapi_wifi_get_init_status() != 1) {
2591         service_error_log1(SERVICE_ERROR, "Srv:%d:WiFi is not initialized", __LINE__);
2592         return ERRCODE_FAIL;
2593     }
2594 
2595     if (strlen(config->ssid) == 0 || strlen(config->ssid) > EXT_WIFI_MAX_SSID_LEN) {
2596         service_error_log1(SERVICE_ERROR, "Srv:%d:ssid is invalid", __LINE__);
2597         return ERRCODE_FAIL;
2598     }
2599     return ERRCODE_SUCC;
2600 }
2601 
wifi_set_softap_config(const softap_config_stru * config)2602 static errcode_t wifi_set_softap_config(const softap_config_stru *config)
2603 {
2604     ext_wifi_softap_config  conf = {0};
2605     char wep_key[WIFI_MAX_KEY_LEN];
2606 
2607     if ((memcpy_s(g_softap_config.ssid, WIFI_MAX_SSID_LEN, config->ssid, strlen(config->ssid) + 1)) != EOK) {
2608         service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
2609         return ERRCODE_FAIL;
2610     }
2611 
2612     if (wifi_is_need_psk(config->security_type) == 1 && config->security_type != WIFI_SEC_TYPE_WPA2PSK &&
2613         config->security_type != WIFI_SEC_TYPE_WPA2_WPA_PSK_MIX && config->security_type != WIFI_SEC_TYPE_SAE &&
2614         config->security_type != WIFI_SEC_TYPE_WEP && config->security_type != WIFI_SEC_TYPE_WEP_OPEN &&
2615 #ifdef CONFIG_OWE
2616         config->security_type != WIFI_SEC_TYPE_OWE &&
2617 #endif /* CONFIG_OWE */
2618         config->security_type != WIFI_SEC_TYPE_WPA3_WPA2_PSK_MIX) {
2619         service_error_log1(SERVICE_ERROR, "Srv:%d:security_type does not support", __LINE__);
2620         return ERRCODE_FAIL;
2621     }
2622 
2623     uint32_t ret = service_check_pre_shared_key(config->pre_shared_key, config->security_type,
2624         (int8_t)config->wifi_psk_type, wep_key);
2625     if (ret != ERRCODE_SUCC) {
2626         service_error_log1(SERVICE_ERROR, "Srv:%d: config->pre_shared_key is invalid", __LINE__);
2627         return ret;
2628     }
2629 
2630     /* WEP: 密钥赋值 */
2631     if ((config->security_type == WIFI_SEC_TYPE_WEP || config->security_type == WIFI_SEC_TYPE_WEP_OPEN) &&
2632         config->wifi_psk_type == 1) {
2633         if ((memcpy_s(g_softap_config.pre_shared_key, WIFI_MAX_KEY_LEN, wep_key, strlen(wep_key) + 1)) != EOK) {
2634             service_error_log1(SERVICE_ERROR, "Srv:%d:hex key memcpy_s failed", __LINE__);
2635             return ERRCODE_FAIL;
2636         }
2637     } else {
2638         if ((memcpy_s(g_softap_config.pre_shared_key, WIFI_MAX_KEY_LEN,
2639             config->pre_shared_key, strlen(config->pre_shared_key) + 1)) != EOK) {
2640             service_error_log1(SERVICE_ERROR, "Srv:%d:ascii key memcpy_s failed", __LINE__);
2641             return ERRCODE_FAIL;
2642         }
2643     }
2644 
2645     g_softap_config.security_type = config->security_type;
2646     g_softap_config.wifi_psk_type = config->wifi_psk_type;
2647 
2648     // 2.4g信道号最大14
2649     if (config->channel_num < 0 || config->channel_num > 14) {
2650         service_error_log1(SERVICE_ERROR, "Srv:%d:channel_num is invalid", __LINE__);
2651         return ERRCODE_FAIL;
2652     }
2653     g_softap_config.channel_num = config->channel_num;
2654 
2655     return ERRCODE_SUCC;
2656 }
2657 
wifi_get_enable_status(void)2658 static errcode_t wifi_get_enable_status(void)
2659 {
2660     if (uapi_wifi_get_init_status() != 1) {
2661         service_error_log1(SERVICE_ERROR, "Srv:%d:WiFi is not init", __LINE__);
2662         return ERRCODE_FAIL;
2663     }
2664 
2665     if (g_softap_enble_flag == 1) {
2666         service_error_log1(SERVICE_ERROR, "Srv:%d:SoftAP has been enabled", __LINE__);
2667         return ERRCODE_FAIL;
2668     }
2669 
2670     if (g_p2p_enable_flag == 1) {
2671         service_error_log1(SERVICE_ERROR, "Srv:%d:P2P has been enabled", __LINE__);
2672         return ERRCODE_FAIL;
2673     }
2674     return ERRCODE_SUCC;
2675 }
2676 
wifi_softap_enable(const softap_config_stru * config)2677 errcode_t wifi_softap_enable(const softap_config_stru *config)
2678 {
2679     ext_wifi_softap_config conf = {0};
2680 
2681     if (wifi_check_softap_config(config) != ERRCODE_SUCC || wifi_set_softap_config(config) != ERRCODE_SUCC) {
2682         service_error_log1(SERVICE_ERROR, "Srv:%d:config set failed", __LINE__);
2683         return ERRCODE_FAIL;
2684     }
2685 
2686     if (wifi_get_enable_status() != ERRCODE_SUCC) {
2687         return ERRCODE_FAIL;
2688     }
2689 
2690     if ((memcpy_s(conf.ssid, EXT_WIFI_MAX_SSID_LEN + 1, g_softap_config.ssid, strlen(g_softap_config.ssid) + 1)) !=
2691         EOK) {
2692         service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
2693         return ERRCODE_FAIL;
2694     }
2695 
2696     if ((memcpy_s(conf.key, EXT_WIFI_AP_KEY_LEN + 1,
2697         g_softap_config.pre_shared_key, strlen(g_softap_config.pre_shared_key) + 1)) != EOK) {
2698         service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
2699         return ERRCODE_FAIL;
2700     }
2701 
2702     conf.authmode = g_softap_config.security_type;
2703     conf.channel_num = (uint8_t)g_softap_config.channel_num;
2704 
2705     conf.ssid_hidden = (g_softap_hidden == 1) ? 0 : 1;
2706     conf.pairwise = EXT_WIFI_PAIRWISE_TKIP_AES_MIX;
2707     if (g_softap_config.security_type ==  WIFI_SEC_TYPE_SAE ||
2708 #ifdef CONFIG_OWE
2709         g_softap_config.security_type == WIFI_SEC_TYPE_OWE ||
2710 #endif /* CONFIG_OWE */
2711         g_softap_config.security_type == WIFI_SEC_TYPE_WPA3_WPA2_PSK_MIX) {
2712         conf.pairwise = EXT_WIFI_PAIRWISE_AES;
2713     }
2714 
2715     int32_t len = WIFI_IFNAME_MAX_SIZE + 1;
2716     if (register_callback() != 0) {
2717         service_error_log1(SERVICE_ERROR, "Srv:%d: register callback fail", __LINE__);
2718         return ERRCODE_FAIL;
2719     }
2720     if (uapi_wifi_softap_start(&conf, g_hotspot_ifname, &len) == EXT_WIFI_OK) {
2721         g_softap_enble_flag = 1;
2722         return ERRCODE_SUCC;
2723     }
2724     service_error_log1(SERVICE_ERROR, "Srv:%d:softap_start failed", __LINE__);
2725     return ERRCODE_FAIL;
2726 }
2727 
wifi_softap_disable(void)2728 errcode_t wifi_softap_disable(void)
2729 {
2730     if (uapi_wifi_get_init_status() != 1) {
2731         service_error_log1(SERVICE_ERROR, "Srv:%d:WiFi is not initialized", __LINE__);
2732         return ERRCODE_FAIL;
2733     }
2734 
2735     if (g_softap_enble_flag == 0) {
2736         service_error_log1(SERVICE_ERROR, "Srv:%d:SoftAP has been disabled", __LINE__);
2737         return ERRCODE_FAIL;
2738     }
2739 
2740 #if defined(LWIP_DHCPS) && LWIP_DHCPS
2741     /* 关闭SoftAP时,自动关闭DHCPS */
2742     struct netif *netif = netifapi_netif_find_by_name(g_hotspot_ifname);
2743     if (netifapi_dhcps_stop(netif) != EXT_WIFI_OK) {
2744         service_error_log1(SERVICE_ERROR, "Srv:%d:DHCPS stop failed", __LINE__);
2745         return ERRCODE_FAIL;
2746     }
2747 #endif
2748 
2749     if (uapi_wifi_softap_stop() == EXT_WIFI_OK) {
2750         g_softap_enble_flag = 0;
2751         memset_s(g_hotspot_ifname, WIFI_IFNAME_MAX_SIZE + 1, 0, WIFI_IFNAME_MAX_SIZE + 1);
2752         return ERRCODE_SUCC;
2753     } else {
2754         service_error_log1(SERVICE_ERROR, "Srv:%d:softap_stop failed", __LINE__);
2755         return ERRCODE_FAIL;
2756     }
2757 }
2758 
wifi_softap_get_sta_list(wifi_sta_info_stru * result,uint32_t * size)2759 errcode_t wifi_softap_get_sta_list(wifi_sta_info_stru *result, uint32_t *size)
2760 {
2761     ext_wifi_ap_sta_info *sta_list;
2762     uint32_t sta_num = WIFI_DEFAULT_MAX_NUM_STA;
2763 
2764     if (result == NULL || size == NULL || (*size) == 0) {
2765         service_error_log1(SERVICE_ERROR, "Srv:%d:para is invalid", __LINE__);
2766         return ERRCODE_FAIL;
2767     }
2768 
2769     sta_list = (ext_wifi_ap_sta_info *)malloc(sizeof(ext_wifi_ap_sta_info) * sta_num);
2770     if (sta_list == NULL) {
2771         service_error_log1(SERVICE_ERROR, "Srv:%d: alloc fail", __LINE__);
2772         return ERRCODE_FAIL;
2773     }
2774 
2775     memset_s(sta_list, sizeof(ext_wifi_ap_sta_info) * sta_num, 0, sizeof(ext_wifi_ap_sta_info) * sta_num);
2776 
2777     if (uapi_wifi_get_init_status() != 1) {
2778         service_error_log1(SERVICE_ERROR, "Srv:%d:WiFi is not initialized", __LINE__);
2779         free(sta_list);
2780         return ERRCODE_FAIL;
2781     }
2782 
2783     if (g_softap_enble_flag == 0) {
2784         service_error_log1(SERVICE_ERROR, "Srv:%d:SoftAP has been disabled", __LINE__);
2785         free(sta_list);
2786         return ERRCODE_FAIL;
2787     }
2788     /* 读出ap下挂sta数量及mac信息, 注意此接口入参的sta_num是sta数量的最大值 */
2789     int ret = uapi_wifi_softap_get_connected_sta(sta_list, &sta_num);
2790 
2791     if (sta_num < *size) {
2792         service_error_log2(SERVICE_ERROR, "Srv:%d:sta_num[%d]!!", __LINE__, sta_num);
2793         *size = sta_num;
2794     }
2795 
2796     if (ret != EXT_WIFI_OK) {
2797         service_error_log1(SERVICE_ERROR, "Srv:%d:get_connected_sta failed", __LINE__);
2798         free(sta_list);
2799         return ERRCODE_FAIL;
2800     }
2801 
2802     for (int i = 0; i < *size; i++) {
2803         if ((memcpy_s(result[i].mac_addr, WIFI_MAC_LEN, sta_list[i].mac, WIFI_MAC_LEN)) != EOK) {
2804             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
2805             free(sta_list);
2806             return ERRCODE_FAIL;
2807         }
2808         ret = uapi_wifi_ap_get_sta_info(result[i].mac_addr, &result[i].rssi, &result[i].best_rate);
2809         if (ret != EXT_WIFI_OK) {
2810             service_error_log1(SERVICE_ERROR, "Srv:%d:get_connected_sta info failed", __LINE__);
2811             free(sta_list);
2812             return ERRCODE_FAIL;
2813         }
2814     }
2815     free(sta_list);
2816     return ERRCODE_SUCC;
2817 }
2818 
wifi_is_softap_enabled(void)2819 int32_t wifi_is_softap_enabled(void)
2820 {
2821     if (uapi_wifi_get_init_status() != 1) {
2822         return 0;
2823     }
2824 
2825     if (g_softap_enble_flag == 0) {
2826         return 0;
2827     } else {
2828         return 1;
2829     }
2830 }
2831 
wifi_get_softap_config(softap_config_stru * result)2832 errcode_t wifi_get_softap_config(softap_config_stru *result)
2833 {
2834     if ((memcpy_s(result, sizeof(softap_config_stru), &g_softap_config, sizeof(softap_config_stru))) != EOK) {
2835         service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
2836         return ERRCODE_FAIL;
2837     }
2838     return ERRCODE_SUCC;
2839 }
2840 
wifi_get_softap_config_advance(softap_config_advance_stru * result)2841 errcode_t wifi_get_softap_config_advance(softap_config_advance_stru *result)
2842 {
2843     if ((memcpy_s(result, sizeof(softap_config_advance_stru),
2844         &g_softap_advance_config, sizeof(softap_config_advance_stru))) != EOK) {
2845         service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
2846         return ERRCODE_FAIL;
2847     }
2848     return ERRCODE_SUCC;
2849 }
2850 
wifi_softap_deauth_sta(const uint8_t * mac,int32_t mac_len)2851 errcode_t wifi_softap_deauth_sta(const uint8_t *mac, int32_t mac_len)
2852 {
2853     if (uapi_wifi_get_init_status() != 1) {
2854         service_error_log1(SERVICE_ERROR, "Srv:%d:WiFi is not initialized", __LINE__);
2855         return ERRCODE_FAIL;
2856     }
2857 
2858     if (g_softap_enble_flag != 1) {
2859         service_error_log1(SERVICE_ERROR, "Srv:%d:SoftAP does not enable", __LINE__);
2860         return ERRCODE_FAIL;
2861     }
2862 
2863     if (mac == NULL || mac_len != WIFI_MAC_LEN) {
2864         service_error_log1(SERVICE_ERROR, "Srv:%d:para is invalid", __LINE__);
2865         return ERRCODE_FAIL;
2866     }
2867 
2868     int ret = uapi_wifi_softap_deauth_sta(mac, WIFI_MAC_LEN);
2869     if (ret == EXT_WIFI_OK) {
2870         return ERRCODE_SUCC;
2871     } else {
2872         service_error_log1(SERVICE_ERROR, "Srv:%d:failed", __LINE__);
2873         return ERRCODE_FAIL;
2874     }
2875 }
2876 
wifi_set_app_ie(wifi_if_type_enum iftype,ie_index_enmu ie_index,uint8_t frame_type_bitmap,const uint8_t * ie,uint16_t ie_len)2877 errcode_t wifi_set_app_ie(wifi_if_type_enum iftype, ie_index_enmu ie_index, uint8_t frame_type_bitmap,
2878                           const uint8_t *ie, uint16_t ie_len)
2879 {
2880     uint32_t ret;
2881     ext_wifi_iftype iftype_tmp;
2882     if (iftype >= IFTYPES_BUTT || ie_index >= IE_BUTT || ie_len > EXT_WIFI_USR_IE_MAX_SIZE || frame_type_bitmap == 0 ||
2883         iftype == IFTYPE_P2P_CLIENT || ie == NULL) {
2884         service_error_log1(SERVICE_ERROR, "Srv:%d:para is invalid", __LINE__);
2885         return ERRCODE_FAIL;
2886     }
2887 
2888     if (uapi_wifi_get_init_status() != 1) {
2889         service_error_log1(SERVICE_ERROR, "Srv:%d:WiFi is not initialized", __LINE__);
2890         return ERRCODE_FAIL;
2891     }
2892 
2893     // bit0表示在beacon帧中插入;bit1表示在probe request帧中插入;bit2表示在probe response帧中插入;bit3~7 保留
2894     if ((frame_type_bitmap & 0xF8) > 0) {
2895         service_error_log1(SERVICE_ERROR, "Srv:%d:frame_type_bitmap is invalid", __LINE__);
2896         return ERRCODE_FAIL;
2897     }
2898 
2899     ret = service_ie_frame_check(iftype, frame_type_bitmap, &iftype_tmp);
2900     if (ret != ERRCODE_SUCC) {
2901         service_error_log1(SERVICE_ERROR, "Srv:%d:service_ie_frame_check failed", __LINE__);
2902         return ERRCODE_FAIL;
2903     }
2904 
2905     // ie的索引1,2;数据段的长度为总长度减2
2906     if ((ie[0] != 0xdd) || (ie[1] != (ie_len - 2))) {
2907         service_error_log1(SERVICE_ERROR, "Srv:%d:Ie setting error", __LINE__);
2908         return ERRCODE_FAIL;
2909     }
2910 
2911     int32_t res = uapi_wifi_add_usr_app_ie(iftype_tmp, ie_index, frame_type_bitmap, ie, ie_len);
2912     if (res == EXT_WIFI_OK) {
2913         return ERRCODE_SUCC;
2914     } else {
2915         service_error_log1(SERVICE_ERROR, "Srv:%d:failed", __LINE__);
2916         return ERRCODE_FAIL;
2917     }
2918 }
2919 
wifi_del_app_ie(wifi_if_type_enum iftype,ie_index_enmu ie_index,uint8_t frame_type_bitmap)2920 errcode_t wifi_del_app_ie(wifi_if_type_enum iftype, ie_index_enmu ie_index, uint8_t frame_type_bitmap)
2921 {
2922     uint32_t ret;
2923     ext_wifi_iftype iftype_tmp;
2924     if (iftype >= IFTYPES_BUTT || ie_index >= IE_BUTT ||  frame_type_bitmap == 0 || iftype == IFTYPE_P2P_CLIENT) {
2925         service_error_log1(SERVICE_ERROR, "Srv:%d:para is invalid", __LINE__);
2926         return ERRCODE_FAIL;
2927     }
2928 
2929     if (uapi_wifi_get_init_status() != 1) {
2930         service_error_log1(SERVICE_ERROR, "Srv:%d:WiFi is not initialized", __LINE__);
2931         return ERRCODE_FAIL;
2932     }
2933 
2934     ret = service_ie_frame_check(iftype, frame_type_bitmap, &iftype_tmp);
2935     if (ret != ERRCODE_SUCC) {
2936         service_error_log1(SERVICE_ERROR, "Srv:%d:service_ie_frame_check failed", __LINE__);
2937         return ERRCODE_FAIL;
2938     }
2939 
2940     return uapi_wifi_delete_usr_app_ie(iftype_tmp, ie_index, frame_type_bitmap) != EXT_WIFI_OK ?
2941         ERRCODE_FAIL : ERRCODE_SUCC;
2942 }
2943 
wifi_set_country_code(const int8_t * country_code,uint8_t len)2944 errcode_t wifi_set_country_code(const int8_t* country_code, uint8_t len)
2945 {
2946     int ret;
2947 
2948     if (uapi_wifi_get_init_status() != 1) {
2949         service_error_log1(SERVICE_ERROR, "Srv:%d:WiFi is not initialized", __LINE__);
2950         return ERRCODE_FAIL;
2951     }
2952 
2953     if (country_code == NULL || len != WIFI_MAC_CONTRY_CODE_LEN) {
2954         service_error_log1(SERVICE_ERROR, "Srv:%d:para is invalid", __LINE__);
2955         return ERRCODE_FAIL;
2956     }
2957 
2958     ret = uapi_wifi_set_country(country_code, len);
2959     if (ret == 0) {
2960         return ERRCODE_SUCC;
2961     } else {
2962         service_error_log1(SERVICE_ERROR, "Srv:%d:set_country failed", __LINE__);
2963         return ERRCODE_FAIL;
2964     }
2965 }
2966 
wifi_get_country_code(int8_t * country_code,uint8_t * len)2967 errcode_t wifi_get_country_code(int8_t *country_code, uint8_t *len)
2968 {
2969     int ret;
2970 
2971     if (uapi_wifi_get_init_status() != 1) {
2972         service_error_log1(SERVICE_ERROR, "Srv:%d:WiFi is not initialized", __LINE__);
2973         return ERRCODE_FAIL;
2974     }
2975 
2976     if (country_code == NULL) {
2977         service_error_log1(SERVICE_ERROR, "Srv:%d:countryCode is NULL", __LINE__);
2978         return ERRCODE_FAIL;
2979     }
2980 
2981     ret = uapi_wifi_get_country(country_code, len);
2982     if (ret != 0) {
2983         service_error_log1(SERVICE_ERROR, "Srv:%d:get_country failed", __LINE__);
2984         return ERRCODE_FAIL;
2985     }
2986     service_error_log2(SERVICE_ERROR, "Srv:GetCountryCode[%s][%d]", country_code, *len);
2987     return ERRCODE_SUCC;
2988 }
2989 
service_freqs_to_chan(int32_t freqs)2990 OSAL_STATIC int32_t service_freqs_to_chan(int32_t freqs)
2991 {
2992     int32_t chan;
2993     int32_t is_freqs = service_is_freqs(freqs);
2994     if (is_freqs == 0) {
2995         return 0;
2996     } else if (is_freqs == 2484) {                                 // 14信道中心频率2484
2997         chan = 14;                                                 // 14信道
2998     } else {
2999         chan = ((freqs - SERVICE_BASE_FREQS) / SERVICE_FREQS_INTERVAL) + 1;
3000     }
3001     return chan;
3002 }
3003 
3004 #ifdef CONFIG_P2P_SUPPORT
p2p_find_peer_by_bssid(ext_wifi_p2p_peer_info * peer,const unsigned char * bssid,unsigned long bssid_len,int peer_num,int * peer_id)3005 static void p2p_find_peer_by_bssid(ext_wifi_p2p_peer_info *peer,
3006     const unsigned char *bssid, unsigned long bssid_len, int peer_num, int *peer_id)
3007 {
3008     int peer_idx;
3009     for (peer_idx = 0; peer_idx < peer_num; peer_idx++) {
3010         if (memcmp(bssid, peer[peer_idx].device_addr, bssid_len) == 0) {
3011             *peer_id = peer_idx;
3012             service_error_log2(SERVICE_ERROR, "Srv:%d:group_cap %d", __LINE__, peer[peer_idx].group_capab);
3013             break;
3014         }
3015     }
3016     if (peer_idx == *peer_id) {
3017         service_error_log1(SERVICE_ERROR, "Srv:%d:can not find same peer", __LINE__);
3018     }
3019     *peer_id = peer_idx;
3020 }
3021 
3022 
wifi_p2p_enable(void)3023 errcode_t wifi_p2p_enable(void)
3024 {
3025     int ret;
3026     int len = WIFI_IFNAME_MAX_SIZE + 1;
3027 
3028     if (wifi_is_wifi_inited() == 0) {
3029         service_error_log1(SERVICE_ERROR, "Srv:%d: wifi not init", __LINE__);
3030         return ERRCODE_FAIL;
3031     }
3032 
3033     if (g_p2p_enable_flag == 0) {
3034         if (g_softap_enble_flag == 1) {
3035             service_error_log1(SERVICE_ERROR, "Srv:%d: SoftAP has been enabled", __LINE__);
3036             return ERRCODE_FAIL;
3037         }
3038         if (register_callback() != 0) {
3039             service_error_log1(SERVICE_ERROR, "Srv:%d: register callback fail", __LINE__);
3040             return ERRCODE_FAIL;
3041         }
3042         ret = uapi_wifi_p2p_start(g_p2p_ifname, &len);
3043     } else {
3044         return ERRCODE_FAIL;
3045     }
3046 
3047     if (ret == EXT_WIFI_OK) {
3048         ret = uapi_wifi_set_peer_connect_choose(EXT_WIFI_P2P_PEER_CONNECT_MANUAL);
3049         if (ret != EXT_WIFI_OK) {
3050             service_error_log1(SERVICE_ERROR, "Srv:%d: set manual connect fail", __LINE__);
3051             uapi_wifi_p2p_stop();
3052             return ERRCODE_FAIL;
3053         }
3054         g_p2p_enable_flag = 1;
3055         return ERRCODE_SUCC;
3056     } else {
3057         service_error_log1(SERVICE_ERROR, "Srv:%d: wpa enablep2p fail", __LINE__);
3058         return ERRCODE_FAIL;
3059     }
3060 }
3061 
wifi_p2p_disable(void)3062 errcode_t wifi_p2p_disable(void)
3063 {
3064     int ret;
3065 
3066     if (wifi_is_wifi_inited() == 0) {
3067         return ERRCODE_FAIL;
3068     }
3069 
3070     /* 关闭P2P时,根据GC模式下自动关闭DHCP、GO模式下自动关闭DHCPS、不管p2p_device情况 */
3071     struct netif *netif = netifapi_netif_find_by_name(g_p2p_ifname);
3072     if (g_p2p_isgo && netifapi_dhcps_stop(netif) != EXT_WIFI_OK) {
3073         service_error_log1(SERVICE_ERROR, "Srv:%d:DHCPS stop failed", __LINE__);
3074         return ERRCODE_FAIL;
3075     } else if (!g_p2p_isgo && netifapi_dhcp_stop(netif) != EXT_WIFI_OK) {
3076         service_error_log1(SERVICE_ERROR, "Srv:%d:DHCP stop failed", __LINE__);
3077         return ERRCODE_FAIL;
3078     }
3079 
3080     if (g_p2p_enable_flag == 1) {
3081         ret = uapi_wifi_p2p_stop();
3082     } else {
3083         return ERRCODE_FAIL;
3084     }
3085     if (ret == EXT_WIFI_OK) {
3086         if (memset_s(g_p2p_ifname, sizeof(g_p2p_ifname), 0, sizeof(g_p2p_ifname)) != EOK) {
3087             service_error_log1(SERVICE_ERROR, "Srv:%d: memset_s failed", __LINE__);
3088             return ERRCODE_FAIL;
3089         }
3090         g_p2p_enable_flag = 0;
3091         return ERRCODE_SUCC;
3092     } else {
3093         service_error_log1(SERVICE_ERROR, "Srv:%d: wpa disablep2p fail", __LINE__);
3094         return ERRCODE_FAIL;
3095     }
3096 }
3097 
wifi_p2p_is_enabled(void)3098 int32_t wifi_p2p_is_enabled(void)
3099 {
3100     return g_p2p_enable_flag;
3101 }
3102 
wifi_p2p_find(int32_t sec)3103 errcode_t wifi_p2p_find(int32_t sec)
3104 {
3105     int ret;
3106 
3107     if (wifi_is_wifi_inited() == 0) {
3108         return ERRCODE_FAIL;
3109     }
3110     if (g_p2p_enable_flag == 0) {
3111         return ERRCODE_FAIL;
3112     }
3113     if (sec == 0) {
3114         ret = uapi_wifi_p2p_find(SERVICE_P2P_MAX_FIND_TIME);
3115     } else if (sec >= SERVICE_P2P_MIN_FIND_TIME && sec <= SERVICE_P2P_MAX_FIND_TIME) {
3116         ret = uapi_wifi_p2p_find(sec);
3117     } else {
3118         service_error_log1(SERVICE_ERROR, "Srv:%d: input arg is inlegal", __LINE__);
3119         return ERRCODE_FAIL;
3120     }
3121 
3122     if (ret == EXT_WIFI_OK) {
3123         return ERRCODE_SUCC;
3124     } else {
3125         service_error_log1(SERVICE_ERROR, "Srv:%d: wpa findp2p fail", __LINE__);
3126         return ERRCODE_FAIL;
3127     }
3128 }
3129 
wifi_p2p_stop_find(void)3130 errcode_t wifi_p2p_stop_find(void)
3131 {
3132     int ret;
3133 
3134     if (wifi_is_wifi_inited() == 0) {
3135         return ERRCODE_FAIL;
3136     }
3137     if (g_p2p_enable_flag == 0) {
3138         return ERRCODE_FAIL;
3139     }
3140     ret = uapi_wifi_p2p_stop_find();
3141     if (ret == EXT_WIFI_OK) {
3142         return ERRCODE_SUCC;
3143     } else {
3144         service_error_log1(SERVICE_ERROR, "Srv:%d: wpa stop p2p find fail", __LINE__);
3145         return ERRCODE_FAIL;
3146     }
3147 }
3148 
wifi_p2p_connect_cancel(void)3149 errcode_t wifi_p2p_connect_cancel(void)
3150 {
3151     int ret;
3152 
3153     if (wifi_is_wifi_inited() == 0) {
3154         return ERRCODE_FAIL;
3155     }
3156     if (g_p2p_enable_flag == 0) {
3157         return ERRCODE_FAIL;
3158     }
3159     ret = uapi_wifi_p2p_cancel();
3160     if (ret == EXT_WIFI_OK) {
3161         return ERRCODE_SUCC;
3162     } else {
3163         service_error_log1(SERVICE_ERROR, "Srv:%d: wpa cancel p2p connect fail", __LINE__);
3164         return ERRCODE_FAIL;
3165     }
3166 }
3167 
wifi_p2p_listen(uint32_t period,uint32_t interval)3168 errcode_t wifi_p2p_listen(uint32_t period, uint32_t interval)
3169 {
3170     int ret;
3171     if (wifi_is_wifi_inited() == 0) {
3172         return ERROR_WIFI_NOT_STARTED;
3173     }
3174     if (g_p2p_enable_flag == 0) {
3175         return ERROR_WIFI_IFACE_INVALID;
3176     }
3177     if (period > SERVICE_MAX_PERIOD_INTERVAL || interval > SERVICE_MAX_PERIOD_INTERVAL || period > interval ||
3178         (period == 0 && interval > 0) || (period > 0 && interval == 0) || period < 0 || interval < 0) {
3179         service_error_log1(SERVICE_ERROR, "Srv:%d:period or interval is error", __LINE__);
3180         return ERROR_WIFI_INVALID_ARGS;
3181     }
3182 
3183     ret = uapi_wifi_p2p_listen(period, interval);
3184     if (ret == EXT_WIFI_OK) {
3185         return WIFI_SUCCESS;
3186     } else {
3187         service_error_log1(SERVICE_ERROR, "Srv:%d: wpa p2p listen fail", __LINE__);
3188         return ERROR_WIFI_UNKNOWN;
3189     }
3190 }
3191 
wifi_p2p_get_peers_info(p2p_device_stru * dev_list,uint32_t * dev_num)3192 errcode_t wifi_p2p_get_peers_info(p2p_device_stru *dev_list, uint32_t *dev_num)
3193 {
3194     int ret;
3195     int peer_num;
3196     unsigned int num = 0;
3197     ext_wifi_p2p_peer_info *peers_res = NULL;
3198 
3199     if (wifi_is_wifi_inited() == 0) {
3200         return ERRCODE_FAIL;
3201     }
3202     if (g_p2p_enable_flag == 0) {
3203         return ERRCODE_FAIL;
3204     }
3205     if (dev_list == NULL || dev_num == NULL || *dev_num > SERVICE_P2P_MAX_FIND_NUM) {
3206         service_error_log1(SERVICE_ERROR, "Srv:%d: input is invalid", __LINE__);
3207         return ERRCODE_FAIL;
3208     }
3209     peers_res = (ext_wifi_p2p_peer_info *)malloc(sizeof(ext_wifi_p2p_peer_info) * (*dev_num));
3210     if (peers_res == NULL) {
3211         service_error_log1(SERVICE_ERROR, "Srv:%d: alloc fail", __LINE__);
3212         return ERRCODE_FAIL;
3213     }
3214     ret = uapi_wifi_p2p_peers(peers_res, dev_num);
3215     if (ret != EXT_WIFI_OK) {
3216         service_error_log1(SERVICE_ERROR, "Srv:%d: wpa stop p2p find fail", __LINE__);
3217         free(peers_res);
3218         return ERRCODE_FAIL;
3219     }
3220     for (peer_num = 0; peer_num < *dev_num; peer_num++) {
3221         /* 将没有config_method的屏蔽,不进行显示 */
3222         if ((get_wps_mode(peers_res[peer_num].config_methods) & 0xF) != 0) {
3223             if ((memcpy_s(dev_list[num].name, sizeof(dev_list[num].name), peers_res[peer_num].device_name,
3224                 sizeof(peers_res[peer_num].device_name))) != EOK) {
3225                 service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
3226                 free(peers_res);
3227                 return ERRCODE_FAIL;
3228             }
3229             if ((memcpy_s(dev_list[num].bssid, sizeof(dev_list[num].bssid), peers_res[peer_num].device_addr,
3230                 sizeof(peers_res[peer_num].device_addr))) != EOK) {
3231                 service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
3232                 free(peers_res);
3233                 return ERRCODE_FAIL;
3234             }
3235             dev_list[num].wps_method = get_wps_mode(peers_res[peer_num].config_methods);
3236             dev_list[num].is_go = peers_res[peer_num].group_capab & 0x1;
3237             dev_list[num].enable_add_group = !(peers_res[peer_num].group_capab & 0x4);
3238             num++;
3239         }
3240     }
3241     *dev_num = num;
3242     free(peers_res);
3243     return ERRCODE_SUCC;
3244 }
3245 
wifi_p2p_connect_etc(const char * bssid,int bssid_len,int join_group,int persistent,int go_intent)3246 static int wifi_p2p_connect_etc(const char *bssid, int bssid_len, int join_group, int persistent, int go_intent)
3247 {
3248     ext_wifi_p2p_connect connect;
3249     ext_wifi_p2p_user_conf user_config = {0};
3250 
3251     if ((memcpy_s(connect.peer_addr, sizeof(connect.peer_addr), bssid, bssid_len)) != EOK) {
3252         service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
3253         return ERRCODE_FAIL;
3254     }
3255     connect.join_group = join_group;
3256     connect.persistent = persistent;
3257     connect.go_intent = go_intent;
3258 
3259     if (uapi_wifi_p2p_get_user_config(&user_config) != EXT_WIFI_OK) {
3260         service_error_log1(SERVICE_ERROR, "Srv:%d: wpa p2p set user conf fail", __LINE__);
3261         return ERRCODE_FAIL;
3262     }
3263 
3264     connect.wps_method = user_config.wps_method;
3265     /* auto connect, put pin to 0 */
3266     if ((memset_s(connect.pin, sizeof(connect.pin), 0, sizeof(connect.pin))) != EOK) {
3267         service_error_log1(SERVICE_ERROR, "Srv:%d:memset_s pin failed", __LINE__);
3268         return ERRCODE_FAIL;
3269     }
3270     service_error_log4(SERVICE_ERROR, "Srv:%d wifi_p2p_connect_etc join = %d wps_method=%d go_intent = %d",
3271         __LINE__, connect.join_group, connect.wps_method, connect.go_intent);
3272     return uapi_wifi_p2p_connect(connect);
3273 }
3274 
wifi_p2p_conn_check_config(const p2p_config_stru * p2p_config,uint32_t line_num)3275 static uint32_t wifi_p2p_conn_check_config(const p2p_config_stru *p2p_config, uint32_t line_num)
3276 {
3277     if (wifi_is_wifi_inited() == 0) {
3278         return ERRCODE_FAIL;
3279     }
3280 
3281     if (g_p2p_enable_flag == 0) {
3282         return ERRCODE_FAIL;
3283     }
3284 
3285     if (p2p_config == NULL || p2p_config->go_intent > SERVICE_P2P_MAX_INTENT ||
3286         p2p_config->go_intent < 0 || p2p_config->persistent > 1 || p2p_config->persistent < 0) {
3287         service_error_log1(SERVICE_ERROR, "Srv:%d: input is invalid", line_num);
3288         return ERRCODE_FAIL;
3289     }
3290     return ERRCODE_SUCC;
3291 }
3292 
wifi_p2p_connect(const p2p_config_stru * p2p_config)3293 errcode_t wifi_p2p_connect(const p2p_config_stru *p2p_config)
3294 {
3295     int32_t ret;
3296     int32_t peer_idx = 0;
3297     ext_wifi_p2p_status_info status;
3298     int32_t peer_num = SERVICE_P2P_MAX_FIND_NUM;
3299     ext_wifi_p2p_peer_info peer[SERVICE_P2P_MAX_FIND_NUM];
3300 
3301     if (wifi_p2p_conn_check_config(p2p_config, __LINE__) != ERRCODE_SUCC) {
3302         return ERRCODE_FAIL;
3303     }
3304 
3305     ret = uapi_wifi_p2p_status(&status);
3306     if (ret != EXT_WIFI_OK) {
3307         service_error_log1(SERVICE_ERROR, "Srv:%d: wpa get p2p status fail", __LINE__);
3308         return ERRCODE_FAIL;
3309     }
3310     if (status.mode == EXT_WIFI_P2P_MODE_GC || status.mode == EXT_WIFI_P2P_MODE_GO) {
3311         service_error_log1(SERVICE_ERROR, "Srv:%d:p2p will invite", __LINE__);
3312         ret = uapi_wifi_p2p_invite(-1, p2p_config->bssid, sizeof(p2p_config->bssid));
3313     } else if (status.mode == EXT_WIFI_P2P_DEVICE_ONLY) {
3314         uapi_wifi_p2p_peers(peer, &peer_num);
3315         p2p_find_peer_by_bssid(peer, p2p_config->bssid, WIFI_MAC_LEN, peer_num, &peer_idx);
3316         if ((peer_idx >= peer_num) || ((peer[peer_idx].group_capab & 0x1) == 0)) {
3317             ret = wifi_p2p_connect_etc(p2p_config->bssid, WIFI_MAC_LEN, 0, p2p_config->persistent,
3318                 p2p_config->go_intent);
3319             if (ret != EXT_WIFI_OK) {
3320                 service_error_log1(SERVICE_ERROR, "Srv:%d: wpa p2p connect fail", __LINE__);
3321                 return ERRCODE_FAIL;
3322             } else {
3323                 return ERRCODE_SUCC;
3324             }
3325         }
3326         if (((peer[peer_idx].group_capab & 0x1) == 0x1) && ((peer[peer_idx].group_capab & 0x4) == 0x4)) {
3327             service_error_log1(SERVICE_ERROR, "Srv:%d: can not join this group group limit", __LINE__);
3328             return ERRCODE_FAIL;
3329         }
3330         ret = wifi_p2p_connect_etc(p2p_config->bssid, WIFI_MAC_LEN, 1, p2p_config->persistent, p2p_config->go_intent);
3331     }
3332     if (ret != EXT_WIFI_OK) {
3333         service_error_log1(SERVICE_ERROR, "Srv:%d: wpa p2p connect fail", __LINE__);
3334         return ERRCODE_FAIL;
3335     }
3336     return ERRCODE_SUCC;
3337 }
3338 
wifi_p2p_wps_method_check(const p2p_config_stru * p2p_config)3339 static int32_t wifi_p2p_wps_method_check(const p2p_config_stru *p2p_config)
3340 {
3341     ext_wifi_p2p_user_conf user_config = {0};
3342     if (uapi_wifi_p2p_get_user_config(&user_config) != EXT_WIFI_OK) {
3343         service_error_log1(SERVICE_ERROR, "Srv:%d: wpa p2p get user conf fail", __LINE__);
3344         return ERROR_WIFI_UNKNOWN;
3345     }
3346 
3347     /* 如果没有pincode,表示使用pbc的方式连接,如果当前设置过,并且设置的不是pbc的方式,直接返回失败 */
3348     if ((p2p_config->wps_method == WPS_PBC) && (user_config.wps_method == WPS_PIN_DISPLAY)) {
3349         service_error_log1(SERVICE_ERROR, "Srv:%d: wps method not match", __LINE__);
3350         return ERROR_WIFI_UNKNOWN;
3351     }
3352 
3353     /* 如果有pincode,表示使用pin的方式连接,如果当前设置过,并且设置的不是pbc的方式,直接返回失败 */
3354     if ((p2p_config->wps_method == WPS_PIN_DISPLAY) && (user_config.wps_method == WPS_PBC)) {
3355         service_error_log1(SERVICE_ERROR, "Srv:%d: wps method not match", __LINE__);
3356         return ERROR_WIFI_UNKNOWN;
3357     }
3358 
3359     return EXT_WIFI_OK;
3360 }
3361 
wifi_p2p_connect_accept(const p2p_config_stru * p2p_config,int32_t assoc)3362 errcode_t wifi_p2p_connect_accept(const p2p_config_stru *p2p_config, int32_t assoc)
3363 {
3364     ext_wifi_p2p_status_info status;
3365     ext_wifi_p2p_connect connect_param = {0};
3366     ext_wifi_p2p_peer_info peer[SERVICE_P2P_MAX_FIND_NUM];
3367     int32_t peer_num = SERVICE_P2P_MAX_FIND_NUM;
3368     int32_t peer_idx = 0;
3369 
3370     if (wifi_p2p_conn_check_config(p2p_config, __LINE__) != ERRCODE_SUCC) {
3371         return ERRCODE_FAIL;
3372     }
3373     /* 对wps_method进行校验 */
3374     if (wifi_p2p_wps_method_check(p2p_config) != EXT_WIFI_OK) {
3375         service_error_log1(SERVICE_ERROR, "Srv:%d: wifi_p2p_wps_method_check error", __LINE__);
3376         return ERRCODE_FAIL;
3377     }
3378 
3379     if (uapi_wifi_p2p_status(&status) != EXT_WIFI_OK || status.mode != EXT_WIFI_P2P_DEVICE_ONLY) {
3380         service_error_log1(SERVICE_ERROR, "Srv:%d: wpa p2p status error", __LINE__);
3381         return ERRCODE_FAIL;
3382     }
3383     if ((memcpy_s(connect_param.peer_addr, sizeof(connect_param.peer_addr), p2p_config->bssid,
3384         sizeof(p2p_config->bssid))) != EOK) {
3385         service_error_log1(SERVICE_ERROR, "Srv:%d:addr memcpy_s failed", __LINE__);
3386         return ERRCODE_FAIL;
3387     }
3388     if ((memcpy_s(connect_param.pin, sizeof(connect_param.pin), p2p_config->pin, sizeof(p2p_config->pin))) != EOK) {
3389         service_error_log1(SERVICE_ERROR, "Srv:%d:pin memcpy_s failed", __LINE__);
3390         return ERRCODE_FAIL;
3391     }
3392     connect_param.persistent = p2p_config->persistent;
3393     connect_param.wps_method = p2p_config->wps_method;
3394     connect_param.go_intent = p2p_config->go_intent;
3395     uapi_wifi_p2p_peers(peer, &peer_num);
3396     p2p_find_peer_by_bssid(peer, p2p_config->bssid, WIFI_MAC_LEN, peer_num, &peer_idx);
3397     if ((peer_idx >= peer_num) || ((peer[peer_idx].group_capab & 0x1) == 0)) {
3398         connect_param.join_group = 0;
3399         return uapi_wifi_p2p_user_accept(connect_param, assoc) != EXT_WIFI_OK ? ERRCODE_FAIL : ERRCODE_SUCC;
3400     }
3401     connect_param.join_group = 1;
3402     return uapi_wifi_p2p_user_accept(connect_param, assoc) != EXT_WIFI_OK ? ERRCODE_FAIL : ERRCODE_SUCC;
3403 }
3404 
wifi_p2p_disconnect(void)3405 errcode_t wifi_p2p_disconnect(void)
3406 {
3407     int32_t ret;
3408 
3409     if (wifi_is_wifi_inited() == 0) {
3410         return ERRCODE_FAIL;
3411     }
3412     if (g_p2p_enable_flag == 0) {
3413         return ERRCODE_FAIL;
3414     }
3415     ret = uapi_wifi_p2p_cancel();
3416     if (ret != EXT_WIFI_OK) {
3417         service_error_log1(SERVICE_ERROR, "Srv:%d: wpa p2p cancel connect fail", __LINE__);
3418         return ERRCODE_FAIL;
3419     }
3420     ret = uapi_wifi_p2p_remove_group();
3421     if (ret != EXT_WIFI_OK) {
3422         service_error_log1(SERVICE_ERROR, "Srv:%d: wpa p2p remove group fail", __LINE__);
3423         return ERRCODE_FAIL;
3424     }
3425     return ERRCODE_SUCC;
3426 }
3427 
wifi_p2p_get_connect_info(p2p_status_info_stru * status)3428 errcode_t wifi_p2p_get_connect_info(p2p_status_info_stru *status)
3429 {
3430     int32_t ret;
3431     ext_wifi_p2p_status_info p2p_status;
3432 
3433     if (wifi_is_wifi_inited() == 0) {
3434         return ERRCODE_FAIL;
3435     }
3436     if (g_p2p_enable_flag == 0) {
3437         return ERRCODE_FAIL;
3438     }
3439     if (status == NULL) {
3440         service_error_log1(SERVICE_ERROR, "Srv:%d: input is invalid", __LINE__);
3441         return ERRCODE_FAIL;
3442     }
3443     if (memset_s(status, sizeof(p2p_status_info_stru), 0, sizeof(p2p_status_info_stru)) != EOK) {
3444         service_error_log1(SERVICE_ERROR, "Srv:%d: memset_s failed", __LINE__);
3445         return ERRCODE_FAIL;
3446     }
3447     ret = uapi_wifi_p2p_status(&p2p_status);
3448     if (ret != EXT_WIFI_OK) {
3449         service_error_log1(SERVICE_ERROR, "Srv:%d: wpa p2p get status fail", __LINE__);
3450         return ERRCODE_FAIL;
3451     }
3452     status->mode = p2p_status.mode;
3453     if (status->mode == P2P_MODE_DEVICE_ONLY) {
3454         return ERRCODE_SUCC;
3455     } else {
3456         status->wpa_state = p2p_status.p2p_state;
3457     }
3458     status->operation_channel = service_freqs_to_chan(p2p_status.op_freq);
3459 
3460     if ((memcpy_s(status->group_ssid, sizeof(status->group_ssid), p2p_status.ssid, sizeof(p2p_status.ssid))) != EOK) {
3461         service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
3462         return ERRCODE_FAIL;
3463     }
3464     if ((memcpy_s(status->group_bssid, sizeof(status->group_bssid),
3465         p2p_status.bssid, sizeof(p2p_status.bssid))) != EOK) {
3466         service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
3467         return ERRCODE_FAIL;
3468     }
3469     return ERRCODE_SUCC;
3470 }
3471 
wifi_p2p_go_get_gc_info(p2p_client_info_stru * client_list,uint32_t * client_num)3472 errcode_t wifi_p2p_go_get_gc_info(p2p_client_info_stru *client_list, uint32_t *client_num)
3473 {
3474     int32_t ret;
3475     int32_t gc_idx;
3476     ext_wifi_p2p_client_info *connected_client = NULL;
3477 
3478     if (wifi_is_wifi_inited() == 0) {
3479         return ERRCODE_FAIL;
3480     }
3481     if (g_p2p_enable_flag == 0) {
3482         return ERRCODE_FAIL;
3483     }
3484     if (client_list == NULL || client_num == NULL || *client_num > SERVICE_P2P_MAX_GC_NUM) {
3485         service_error_log1(SERVICE_ERROR, "Srv:%d: input is invalid", __LINE__);
3486         return ERRCODE_FAIL;
3487     }
3488     connected_client = (ext_wifi_p2p_client_info *)malloc(sizeof(ext_wifi_p2p_client_info) * (*client_num));
3489     if (connected_client == NULL) {
3490         service_error_log1(SERVICE_ERROR, "Srv:%d: alloc fail", __LINE__);
3491         return ERRCODE_FAIL;
3492     }
3493     ret = uapi_wifi_p2p_get_connected_client(connected_client, client_num);
3494     if (ret != EXT_WIFI_OK) {
3495         service_error_log1(SERVICE_ERROR, "Srv:%d: p2p get gc fail", __LINE__);
3496         goto SERVICE_P2P_GET_GC_FAIL;
3497     }
3498     for (gc_idx = 0; gc_idx < *client_num; gc_idx++) {
3499         if ((memcpy_s(client_list[gc_idx].gc_bssid, sizeof(client_list[gc_idx].gc_bssid), connected_client[gc_idx].mac,
3500             sizeof(connected_client[gc_idx].mac))) != EOK) {
3501             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
3502             goto SERVICE_P2P_GET_GC_FAIL;
3503         }
3504         if ((memcpy_s(client_list[gc_idx].gc_device_bssid, sizeof(client_list[gc_idx].gc_device_bssid),
3505             connected_client[gc_idx].dev_addr, sizeof(connected_client[gc_idx].dev_addr))) != EOK) {
3506             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
3507             goto SERVICE_P2P_GET_GC_FAIL;
3508         }
3509         if ((memcpy_s(client_list[gc_idx].gc_device_name, sizeof(client_list[gc_idx].gc_device_name),
3510             connected_client[gc_idx].device_name, sizeof(connected_client[gc_idx].device_name))) != EOK) {
3511             service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
3512             goto SERVICE_P2P_GET_GC_FAIL;
3513         }
3514     }
3515     free(connected_client);
3516     return ERRCODE_SUCC;
3517 SERVICE_P2P_GET_GC_FAIL:
3518     if (connected_client != NULL) {
3519         free(connected_client);
3520     }
3521     return ERRCODE_FAIL;
3522 }
3523 
wifi_p2p_set_device_config(const p2p_device_config_stru * p2p_dev_set_info)3524 errcode_t wifi_p2p_set_device_config(const p2p_device_config_stru *p2p_dev_set_info)
3525 {
3526     int32_t ret;
3527     ext_wifi_p2p_user_conf user_config = {0};
3528 
3529     if (wifi_is_wifi_inited() == 0) {
3530         return ERRCODE_FAIL;
3531     }
3532     if (g_p2p_enable_flag == 0) {
3533         return ERRCODE_FAIL;
3534     }
3535     if (p2p_dev_set_info == NULL ||
3536         (p2p_dev_set_info->wps_method != WPS_PBC && p2p_dev_set_info->wps_method != WPS_PIN_DISPLAY)) {
3537         service_error_log1(SERVICE_ERROR, "Srv:%d: input is invalid", __LINE__);
3538         return ERRCODE_FAIL;
3539     }
3540     user_config.go_intent = SERVICE_P2P_DEFAULT_INTENT;
3541     user_config.oper_channel = p2p_dev_set_info->oper_channel;
3542     user_config.listen_channel = p2p_dev_set_info->listen_channel;
3543     user_config.wps_method = p2p_dev_set_info->wps_method;
3544     if ((memcpy_s(user_config.device_name, sizeof(user_config.device_name),
3545         p2p_dev_set_info->dev_name, sizeof(p2p_dev_set_info->dev_name))) != EOK) {
3546         service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
3547         return ERRCODE_FAIL;
3548     }
3549     ret = uapi_wifi_p2p_user_config(user_config);
3550     if (ret != EXT_WIFI_OK) {
3551         service_error_log1(SERVICE_ERROR, "Srv:%d: wpa p2p set user conf fail", __LINE__);
3552         return ERRCODE_FAIL;
3553     }
3554     return ERRCODE_SUCC;
3555 }
3556 
wifi_p2p_get_device_config(p2p_device_config_stru * p2p_dev_set_info)3557 errcode_t wifi_p2p_get_device_config(p2p_device_config_stru *p2p_dev_set_info)
3558 {
3559     ext_wifi_p2p_user_conf user_config = {0};
3560 
3561     if (wifi_is_wifi_inited() == 0) {
3562         return ERRCODE_FAIL;
3563     }
3564     if (g_p2p_enable_flag == 0) {
3565         return ERRCODE_FAIL;
3566     }
3567 
3568     if (p2p_dev_set_info == NULL) {
3569         service_error_log1(SERVICE_ERROR, "Srv:%d: input is invalid", __LINE__);
3570         return ERRCODE_FAIL;
3571     }
3572 
3573     if (uapi_wifi_p2p_get_user_config(&user_config) != EXT_WIFI_OK) {
3574         service_error_log1(SERVICE_ERROR, "Srv:%d: wpa p2p get user conf fail", __LINE__);
3575         return ERRCODE_FAIL;
3576     }
3577 
3578     if ((memcpy_s(p2p_dev_set_info->dev_name, WPS_DEV_NAME_MAX_LEN + 1,
3579         &user_config.device_name, WPS_DEV_NAME_MAX_LEN + 1)) != EOK) {
3580         service_error_log1(SERVICE_ERROR, "Srv:%d:memcpy_s failed", __LINE__);
3581         return ERRCODE_FAIL;
3582     }
3583     p2p_dev_set_info->listen_channel = user_config.listen_channel;
3584     p2p_dev_set_info->oper_channel = user_config.oper_channel;
3585     p2p_dev_set_info->wps_method = user_config.wps_method;
3586 
3587     return ERRCODE_SUCC;
3588 }
3589 #endif /* end of CONFIG_P2P_SUPPORT */
3590 
wifi_set_pkt_retry_policy(uint8_t type,uint8_t limit)3591 errcode_t wifi_set_pkt_retry_policy(uint8_t type, uint8_t limit)
3592 {
3593     int32_t ret;
3594 
3595     if (type >= EXT_WIFI_RETRY_FRAME_BUTT) {
3596         service_error_log1(SERVICE_ERROR, "Srv:%d:parameter is invalid.", __LINE__);
3597         return ERRCODE_FAIL;
3598     }
3599     ret = uapi_wifi_set_pkt_retry_policy(type, limit);
3600     if (ret != EXT_WIFI_OK) {
3601         service_error_log1(SERVICE_ERROR, "Srv:%d: set retry params fail", __LINE__);
3602         return ERRCODE_FAIL;
3603     }
3604     return ERRCODE_SUCC;
3605 }
3606 
wifi_reset_mac_phy(void)3607 errcode_t wifi_reset_mac_phy(void)
3608 {
3609     int32_t ret;
3610 
3611     ret = uapi_wifi_mac_phy_reset();
3612     if (ret == EXT_WIFI_OK) {
3613         return ERRCODE_SUCC;
3614     } else {
3615         service_error_log1(SERVICE_ERROR, "Srv:%d:reset_mac_phy failed.", __LINE__);
3616         return ERRCODE_FAIL;
3617     }
3618 }
3619 
wifi_sta_set_pm(uint8_t ps_switch)3620 errcode_t wifi_sta_set_pm(uint8_t ps_switch)
3621 {
3622     int32_t ret;
3623 
3624     ret = service_wifi_and_sta_check();
3625     if (ret != WIFI_SUCCESS) {
3626         service_error_log2(SERVICE_ERROR, "Srv:%d:service_wifi_and_sta_check failed ret[%d]", __LINE__, ret);
3627         return ERRCODE_FAIL;
3628     }
3629 
3630     ret = uapi_wifi_set_pm_switch(ps_switch, 0);
3631     if (ret == EXT_WIFI_OK) {
3632         return ERRCODE_SUCC;
3633     } else {
3634         service_error_log1(SERVICE_ERROR, "Srv:%d:set_ps_switch failed.", __LINE__);
3635         return ERRCODE_FAIL;
3636     }
3637 }
3638 
wifi_sta_set_pm_param(uint8_t pm_timeout,uint8_t pm_timer_cnt,uint8_t bcn_timeout,uint8_t mcast_timeout,uint16_t sleep_time)3639 errcode_t wifi_sta_set_pm_param(uint8_t pm_timeout, uint8_t pm_timer_cnt, uint8_t bcn_timeout,
3640     uint8_t mcast_timeout, uint16_t sleep_time)
3641 {
3642     int32_t ret;
3643 
3644     ret = service_wifi_and_sta_check();
3645     if (ret != WIFI_SUCCESS) {
3646         service_error_log2(SERVICE_ERROR, "Srv:%d:sta check failed ret[%d]", __LINE__, ret);
3647         return ERRCODE_FAIL;
3648     }
3649 
3650     ret = uapi_wifi_sta_set_pm_param(pm_timeout, pm_timer_cnt, bcn_timeout, mcast_timeout, sleep_time);
3651     if (ret == EXT_WIFI_OK) {
3652         return ERRCODE_SUCC;
3653     } else {
3654         service_error_log1(SERVICE_ERROR, "Srv:%d:set_ps_param failed.", __LINE__);
3655         return ERRCODE_FAIL;
3656     }
3657 }
3658 
wifi_set_psd_mode(ext_psd_option_param * psd_option)3659 errcode_t wifi_set_psd_mode(ext_psd_option_param *psd_option)
3660 {
3661     int32_t ret;
3662     /* cycle 取值 100 ~ 1000 单位ms dur 取值 1 ~ 65535(u16不用重复校验) 单位min */
3663     if ((psd_option->cycle < 100 || psd_option->cycle > 1000 || psd_option->duration < 1) &&
3664         (psd_option->enable != 0)) {
3665         service_error_log1(SERVICE_ERROR, "Srv:%d: param invalid", __LINE__);
3666         return ERROR_WIFI_UNKNOWN;
3667     }
3668 
3669     ret = uapi_wifi_set_psd_enable((psd_option_param *)psd_option);
3670     if (ret != WIFI_SUCCESS) {
3671         service_error_log2(SERVICE_ERROR, "Srv:%d: fail ret [%d]", __LINE__, ret);
3672         return ERROR_WIFI_UNKNOWN;
3673     }
3674     return ERRCODE_SUCC;
3675 }
3676 
wifi_set_psd_cb(wifi_psd_cb data_cb)3677 errcode_t wifi_set_psd_cb(wifi_psd_cb data_cb)
3678 {
3679     int32_t ret;
3680 
3681     ret = uapi_wifi_set_psd_cb(data_cb);
3682     if (ret != WIFI_SUCCESS) {
3683         service_error_log2(SERVICE_ERROR, "Srv:%d: fail ret [%d]", __LINE__, ret);
3684         return ERROR_WIFI_UNKNOWN;
3685     }
3686     return ERRCODE_SUCC;
3687 }
3688 
wifi_set_rts_mode(uint8_t mode,uint16_t pkt_length)3689 errcode_t wifi_set_rts_mode(uint8_t mode, uint16_t pkt_length)
3690 {
3691     uint32_t ret;
3692 
3693     if (uapi_wifi_get_init_status() != 1) {
3694         service_error_log1(SERVICE_ERROR, "Srv:%d:WiFi is not initialized", __LINE__);
3695         return ERROR_WIFI_NOT_STARTED;
3696     }
3697 
3698     ret = uapi_wifi_set_rts_threshold(mode, pkt_length);
3699     if (ret == 0) {
3700         return ERRCODE_SUCC;
3701     } else {
3702         service_error_log1(SERVICE_ERROR, "Srv:%d:set_rts_thres failed", __LINE__);
3703         return ERROR_WIFI_UNKNOWN;
3704     }
3705 }
3706 
wifi_set_cca_threshold(uint8_t mode,int8_t threshold)3707 errcode_t wifi_set_cca_threshold(uint8_t mode, int8_t threshold)
3708 {
3709     uint32_t ret;
3710 
3711     if (uapi_wifi_get_init_status() != 1) {
3712         service_error_log1(SERVICE_ERROR, "Srv:%d:WiFi is not initialized", __LINE__);
3713         return ERROR_WIFI_NOT_STARTED;
3714     }
3715 
3716     ret = uapi_wifi_set_cca_threshold(mode, threshold);
3717     if (ret == 0) {
3718         return ERRCODE_SUCC;
3719     } else {
3720         service_error_log1(SERVICE_ERROR, "Srv:%d:set_cca failed", __LINE__);
3721         return ERROR_WIFI_UNKNOWN;
3722     }
3723 }
wifi_set_tpc_mode(uint32_t tpc_value)3724 errcode_t wifi_set_tpc_mode(uint32_t tpc_value)
3725 {
3726     uint32_t ret;
3727 
3728     if (uapi_wifi_get_init_status() != 1) {
3729         service_error_log1(SERVICE_ERROR, "Srv:%d:WiFi is not initialized", __LINE__);
3730         return ERROR_WIFI_NOT_STARTED;
3731     }
3732 
3733     ret = uapi_wifi_set_tpc(tpc_value);
3734     if (ret == 0) {
3735         return ERRCODE_SUCC;
3736     } else {
3737         service_error_log1(SERVICE_ERROR, "Srv:%d:set_tpc failed", __LINE__);
3738         return ERROR_WIFI_UNKNOWN;
3739     }
3740 }
wifi_set_fixed_tx_rate(unsigned char auto_rate,alg_param_stru * alg_param)3741 errcode_t wifi_set_fixed_tx_rate(unsigned char auto_rate, alg_param_stru *alg_param)
3742 {
3743     uint32_t ret;
3744 
3745     if (uapi_wifi_get_init_status() != 1) {
3746         service_error_log1(SERVICE_ERROR, "Srv:%d:WiFi is not initialized", __LINE__);
3747         return ERROR_WIFI_NOT_STARTED;
3748     }
3749 
3750     if (alg_param == NULL) {
3751         service_error_log1(SERVICE_ERROR, "Srv:%d:para is invalid", __LINE__);
3752         return ERROR_WIFI_INVALID_ARGS;
3753     }
3754 
3755     ret = uapi_wifi_set_phy_txrate(auto_rate, (ext_alg_param_stru *)alg_param);
3756     if (ret == 0) {
3757         return ERRCODE_SUCC;
3758     } else {
3759         service_error_log1(SERVICE_ERROR, "Srv:%d:set_txrate failed", __LINE__);
3760         return ERROR_WIFI_UNKNOWN;
3761     }
3762 }
3763 
3764 #ifdef _PRE_WLAN_FEATURE_INTRF_MODE
wifi_set_intrf_mode(const char * ifname,uint8_t enable,uint16_t flag)3765 errcode_t wifi_set_intrf_mode(const char *ifname, uint8_t enable, uint16_t flag)
3766 {
3767     uint32_t ret;
3768 
3769     if (uapi_wifi_get_init_status() != 1) {
3770         service_error_log1(SERVICE_ERROR, "Srv:%d:WiFi is not initialized", __LINE__);
3771         return ERROR_WIFI_NOT_STARTED;
3772     }
3773 
3774     if (ifname == NULL) {
3775         service_error_log1(SERVICE_ERROR, "Srv:%d:para is invalid", __LINE__);
3776         return ERROR_WIFI_INVALID_ARGS;
3777     }
3778 
3779     ret = uapi_wifi_enable_intrf_mode(ifname, enable, flag);
3780     if (ret == 0) {
3781         return ERRCODE_SUCC;
3782     } else {
3783         service_error_log1(SERVICE_ERROR, "Srv:%d:set_intrfmode failed", __LINE__);
3784         return ERROR_WIFI_UNKNOWN;
3785     }
3786 }
3787 #endif
wifi_get_negotiated_rate(const uint8_t * mac,int32_t mac_len,uint32_t * tx_best_rate)3788 errcode_t wifi_get_negotiated_rate(const uint8_t *mac, int32_t mac_len, uint32_t *tx_best_rate)
3789 {
3790     int32_t ret;
3791     int8_t addr_txt[EXT_WIFI_TXT_ADDR_LEN + 2] = { 0 };
3792 
3793     if (uapi_wifi_get_init_status() != 1) {
3794         service_error_log1(SERVICE_ERROR, "Srv:%d:WiFi is not initialized", __LINE__);
3795         return ERROR_WIFI_NOT_STARTED;
3796     }
3797 
3798     if (mac == NULL) {
3799         service_error_log1(SERVICE_ERROR, "Srv:%d:para is invalid", __LINE__);
3800         return ERROR_WIFI_INVALID_ARGS;
3801     }
3802 
3803     /* 从第2个开始 */
3804     ret = snprintf_s(addr_txt + 1, sizeof(addr_txt) - 1, sizeof(addr_txt) - 2, MACSTR, mac2str(mac));
3805     if (ret < 0) {
3806         service_error_log1(SERVICE_ERROR, "Srv:%d snprintf_s faild", __LINE__);
3807         return EXT_WIFI_FAIL;
3808     }
3809     ret = uapi_wifi_get_tx_params(addr_txt, sizeof(addr_txt), tx_best_rate);
3810     if (ret == 0) {
3811         return ERRCODE_SUCC;
3812     } else {
3813         service_error_log1(SERVICE_ERROR, "Srv:%d:GetWifiNegotiateSpeed failed", __LINE__);
3814         return ERROR_WIFI_UNKNOWN;
3815     }
3816 }
3817 
wifi_set_linkloss_config(linkloss_paras_stru * linkloss_paras)3818 errcode_t wifi_set_linkloss_config(linkloss_paras_stru *linkloss_paras)
3819 {
3820     int32_t ret;
3821     uint8_t proberequest_ratio = 0;
3822     uint16_t linkloss_threshold = 0;
3823     if (uapi_wifi_get_init_status() != 1) {
3824         service_error_log1(SERVICE_ERROR, "Srv:%d:WiFi is not initialized", __LINE__);
3825         return ERRCODE_FAIL;
3826     }
3827     proberequest_ratio = linkloss_paras->send_probe_request_ratio;
3828     linkloss_threshold = linkloss_paras->linkloss_threshold;
3829     if (proberequest_ratio < SERVICE_LINKLOSS_PROBEREQ_RATIO_MIN ||
3830         proberequest_ratio > SERVICE_LINKLOSS_PROBEREQ_RATIO_MAX ||
3831         linkloss_threshold < SERVICE_LINKLOSS_THRESHOLD_MIN || linkloss_threshold > SERVICE_LINKLOSS_THRESHOLD_MAX) {
3832         service_error_log3(SERVICE_ERROR,
3833             "Srv:%d:paraments out of rangle Ratio[%d] threshold[%d]", __LINE__,
3834             proberequest_ratio, linkloss_threshold);
3835     }
3836     ret = uapi_wifi_set_linkloss_paras(proberequest_ratio, linkloss_threshold);
3837     if (ret == EXT_WIFI_OK) {
3838         return ERRCODE_SUCC;
3839     } else {
3840         service_error_log1(SERVICE_ERROR, "Srv:%d:set_ps_switch failed.", __LINE__);
3841         return ERRCODE_FAIL;
3842     }
3843 }
3844 
3845 #ifdef CONFIG_RST_SUPPORT
plat_set_pm_mode(int32_t pm_switch)3846 errcode_t plat_set_pm_mode(int32_t pm_switch)
3847 {
3848     int ret;
3849     ret = plat_pm_func_enable_switch(pm_switch);
3850     if (ret == 0) {
3851         return ERRCODE_SUCC;
3852     } else {
3853         service_error_log2(SERVICE_ERROR, "Srv:%d:set pm mode fail, ret=[%d].", __LINE__, ret);
3854         return ERRCODE_FAIL;
3855     }
3856 }
3857 
plat_reset_board(void)3858 errcode_t plat_reset_board(void)
3859 {
3860     uint32_t ret;
3861     ret = wifi_deinit(); // 去初始化wifi
3862     if (ret != EXT_WIFI_OK) {
3863         service_error_log2(SERVICE_ERROR, "Srv:%d:WifiUninit fail, ret=[%d].", __LINE__, ret);
3864         return ERRCODE_FAIL;
3865     }
3866     ret = pm_board_power_reset(); // device复位
3867     if (ret != EXT_WIFI_OK) {
3868         service_error_log2(SERVICE_ERROR, "Srv:%d:pm_board_power_reset fail, ret=[%d].", __LINE__, ret);
3869         return ERRCODE_FAIL;
3870     }
3871     ret = plat_reset_reinit_etc(); // 重新初始化平台
3872     if (ret != EXT_WIFI_OK) {
3873         service_error_log2(SERVICE_ERROR, "Srv:%d:plat_reset_reinit_etc fail, ret=[%d].", __LINE__, ret);
3874         return ERRCODE_FAIL;
3875     }
3876 #ifdef _PRE_WLAN_FEATURE_CENTRALIZE
3877 #ifdef BOARD_FPGA_WIFI
3878     // 共核编译的直接在host初始化abb 其他情况在device初始化中完成
3879     hh503_abb5_init_pre();
3880     mpw0_poweron();
3881 #endif
3882 #endif
3883     ret = wifi_init(); // 初始化wifi
3884     if (ret != EXT_WIFI_OK) {
3885         service_error_log2(SERVICE_ERROR, "Srv:%d:WifiInit fail, ret=[%d].", __LINE__, ret);
3886         return ERRCODE_FAIL;
3887     }
3888     return ERRCODE_SUCC;
3889 }
3890 #endif
3891 
plat_get_temperature(int8_t * temperature)3892 static errcode_t plat_get_temperature(int8_t *temperature)
3893 {
3894     if (temperature == NULL) {
3895         return ERROR_WIFI_INVALID_ARGS;
3896     }
3897     if (uapi_tsensor_get_current_temp(temperature) != WIFI_SUCCESS) {
3898         return ERROR_WIFI_UNKNOWN;
3899     }
3900     return WIFI_SUCCESS;
3901 }
3902 
wifi_set_base_mac_addr(const int8_t * mac_addr,uint8_t mac_len)3903 errcode_t wifi_set_base_mac_addr(const int8_t *mac_addr, uint8_t mac_len)
3904 {
3905     uint32_t ret;
3906     if (mac_addr == NULL || mac_len != WIFI_MAC_LEN) {
3907         service_error_log1(SERVICE_ERROR, "Srv:%d:parameter is invalid.", __LINE__);
3908         return ERRCODE_INVALID_PARAM;
3909     }
3910     if (uapi_wifi_get_init_status() != 1) {
3911         service_error_log1(SERVICE_ERROR, "Srv:%d:WiFi is not initialized", __LINE__);
3912         return ERRCODE_FAIL;
3913     }
3914 
3915     if (g_softap_enble_flag == 1 || g_sta_enble_flag == 1 || g_p2p_enable_flag == 1) {
3916         service_error_log1(SERVICE_ERROR, "Srv:%d:sta、p2p or softap are enabled", __LINE__);
3917         return ERRCODE_FAIL;
3918     }
3919     ret = set_dev_addr(mac_addr, mac_len, 0);
3920     if (ret != EXT_WIFI_OK) {
3921         service_error_log1(SERVICE_ERROR, "Srv:%d: fail", __LINE__);
3922         return ERRCODE_FAIL;
3923     }
3924     return ERRCODE_SUCC;
3925 }
3926 
wifi_get_base_mac_addr(int8_t * mac_addr,uint8_t mac_len)3927 errcode_t wifi_get_base_mac_addr(int8_t *mac_addr, uint8_t mac_len)
3928 {
3929     uint32_t ret;
3930     if (mac_addr == NULL || mac_len != WIFI_MAC_LEN) {
3931         service_error_log1(SERVICE_ERROR, "Srv:%d:parameter is invalid.", __LINE__);
3932         return ERRCODE_INVALID_PARAM;
3933     }
3934     ret = get_dev_addr(mac_addr, mac_len, 2); /* 2表示NL80211_IFTYPE_STATION */
3935     if (ret != EXT_WIFI_OK) {
3936         service_error_log1(SERVICE_ERROR, "Srv:%d: fail", __LINE__);
3937         return ERRCODE_FAIL;
3938     }
3939     return ERRCODE_SUCC;
3940 }
3941 
3942 #define NL80211_IFTYPE_AP 3
wifi_softap_set_mac_addr(const int8_t * mac_addr,uint8_t mac_len)3943 errcode_t wifi_softap_set_mac_addr(const int8_t *mac_addr, uint8_t mac_len)
3944 {
3945     uint32_t ret;
3946     if (mac_addr == NULL || mac_len != WIFI_MAC_LEN) {
3947         service_error_log1(SERVICE_ERROR, "Srv:%d:parameter is invalid.", __LINE__);
3948         return ERRCODE_INVALID_PARAM;
3949     }
3950     if (g_softap_enble_flag == 1 || g_sta_enble_flag == 1 || g_p2p_enable_flag == 1) {
3951         service_error_log1(SERVICE_ERROR, "Srv:%d:sta、p2p or softap are enabled", __LINE__);
3952         return ERRCODE_FAIL;
3953     }
3954     ret = set_dev_addr(mac_addr, mac_len, NL80211_IFTYPE_AP);
3955     if (ret != EXT_WIFI_OK) {
3956         service_error_log1(SERVICE_ERROR, "Srv:%d: fail", __LINE__);
3957         return ERRCODE_FAIL;
3958     }
3959     return ERRCODE_SUCC;
3960 }
3961 
wifi_softap_get_mac_addr(int8_t * mac_addr,uint8_t mac_len)3962 errcode_t wifi_softap_get_mac_addr(int8_t *mac_addr, uint8_t mac_len)
3963 {
3964     uint32_t ret;
3965     if (mac_addr == NULL || mac_len != WIFI_MAC_LEN) {
3966         service_error_log1(SERVICE_ERROR, "Srv:%d:parameter is invalid.", __LINE__);
3967         return ERRCODE_INVALID_PARAM;
3968     }
3969     ret = get_dev_addr(mac_addr, mac_len, NL80211_IFTYPE_AP);
3970         if (ret != EXT_WIFI_OK) {
3971         service_error_log1(SERVICE_ERROR, "Srv:%d: fail", __LINE__);
3972         return ERRCODE_FAIL;
3973     }
3974     return ERRCODE_SUCC;
3975 }
3976 
wifi_set_mac_derivation_ptr(wifi_mac_derivation_ptr ptr)3977 errcode_t wifi_set_mac_derivation_ptr(wifi_mac_derivation_ptr ptr)
3978 {
3979     set_mac_derivation_ptr((mac_derivation_ptr)ptr);
3980     return WIFI_SUCCESS;
3981 }
3982 
3983 #ifdef CONFIG_PLAT_DFR_SUPPORT
plat_register_driver_panic_cb(wifi_driver_event_cb event_cb)3984 errcode_t plat_register_driver_panic_cb(wifi_driver_event_cb event_cb)
3985 {
3986     int ret;
3987     ret = wifi_driver_event_callback_register(event_cb);
3988     if (ret == 0) {
3989         return ERRCODE_SUCC;
3990     } else {
3991         service_error_log2(SERVICE_ERROR, "Srv:%d: fail, ret=[%d].", ret, __LINE__);
3992         return ERRCODE_FAIL;
3993     }
3994 }
3995 
plat_set_gpio_mode(int32_t id,int32_t sw)3996 errcode_t plat_set_gpio_mode(int32_t id, int32_t sw)
3997 {
3998     int ret;
3999     gpio_param_t gpio;
4000 
4001     gpio.id = id;
4002     gpio.val = sw;
4003 
4004     ret = uapi_set_gpio_val((int8_t *)&gpio);
4005     if (ret == 0) {
4006         return ERRCODE_SUCC;
4007     } else {
4008         service_error_log2(SERVICE_ERROR, "Srv:%d: fail, ret=[%d].", __LINE__, ret);
4009         return ERRCODE_FAIL;
4010     }
4011 }
4012 #endif
4013 
wifi_set_low_current_boot_mode(uint8_t flag)4014 errcode_t wifi_set_low_current_boot_mode(uint8_t flag)
4015 {
4016     int ret;
4017 
4018     if (uapi_wifi_get_init_status() != 1) {
4019         service_error_log1(SERVICE_ERROR, "Srv:%d:WiFi is not initialized", __LINE__);
4020         return ERROR_WIFI_NOT_STARTED;
4021     }
4022 
4023     ret = uapi_set_low_current_boot_mode(flag);
4024     if (ret != 0) {
4025         service_error_log1(SERVICE_ERROR, "Srv:%d:config failed", __LINE__);
4026         return ERROR_WIFI_UNKNOWN;
4027     }
4028     return ERRCODE_SUCC;
4029 }
4030 
wifi_brctl_setbr(const char * oper)4031 errcode_t wifi_brctl_setbr(const char *oper)
4032 {
4033     if (oper == NULL) {
4034         return ERRCODE_INVALID_PARAM;
4035     }
4036     if (uapi_wifi_set_brctl(oper, NULL) != 0) {
4037         return ERRCODE_FAIL;
4038     }
4039     return ERRCODE_SUCC;
4040 }
4041 
wifi_brctl_setif(const char * oper,const char * if_name)4042 errcode_t wifi_brctl_setif(const char *oper, const char *if_name)
4043 {
4044     if (oper == NULL || if_name == NULL) {
4045         return ERRCODE_INVALID_PARAM;
4046     }
4047     if (uapi_wifi_set_brctl(oper, if_name) != 0) {
4048         return ERRCODE_FAIL;
4049     }
4050     return ERRCODE_SUCC;
4051 }
4052 
wifi_brctl_show(const char * oper)4053 errcode_t wifi_brctl_show(const char *oper)
4054 {
4055     if (oper == NULL) {
4056         return ERRCODE_INVALID_PARAM;
4057     }
4058     if (uapi_wifi_set_brctl(oper, NULL) != 0) {
4059         return ERRCODE_FAIL;
4060     }
4061     return ERRCODE_SUCC;
4062 }
4063 
wifi_set_tx_pwr_offset(const int8_t * ifname,int16_t offset)4064 errcode_t wifi_set_tx_pwr_offset(const int8_t *ifname, int16_t offset)
4065 {
4066     uint32_t ret;
4067 
4068     if (uapi_wifi_get_init_status() != 1) {
4069         service_error_log0(SERVICE_ERROR, "Service::SetTxPwrOffset:WiFi is not initialized");
4070         return ERROR_WIFI_NOT_STARTED;
4071     }
4072 
4073     if (ifname == NULL) {
4074         service_error_log0(SERVICE_ERROR, "Service::SetTxPwrOffset:parameter is invalid.");
4075         return ERROR_WIFI_INVALID_ARGS;
4076     }
4077 
4078     ret = uapi_wifi_set_tx_pwr_offset(ifname, offset);
4079     if (ret != 0) {
4080         service_error_log0(SERVICE_ERROR, "Service::SetTxPwrOffset:config failed");
4081         return ERROR_WIFI_UNKNOWN;
4082     }
4083     return ERRCODE_SUCC;
4084 }
4085 
4086 #ifdef __cplusplus
4087 #if __cplusplus
4088     }
4089 #endif
4090 #endif
4091