• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 Huawei Device Co., Ltd.
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  */
15 
16 #include "wpa_supplicant_hal.h"
17 #include <poll.h>
18 #include <pthread.h>
19 #include <stdbool.h>
20 #include <hdf_log.h>
21 #include "hdi_wpa_common.h"
22 #include "securec.h"
23 #include "utils/common.h"
24 #include "wpa_hdi_util.h"
25 
26 #undef LOG_TAG
27 #define LOG_TAG "HdiWpaStaHal"
28 
29 #define FAIL_BUSY 2
30 
31 #define COLUMN_INDEX_ZERO 0
32 #define COLUMN_INDEX_ONE 1
33 #define COLUMN_INDEX_TWO 2
34 #define COLUMN_INDEX_THREE 3
35 #define COLUMN_INDEX_FOUR 4
36 #define COLUMN_INDEX_FIVE 5
37 
38 #define FAIL_PBC_OVERLAP_RETUEN 3
39 #define CMD_BUFFER_SIZE 2148
40 #define MAX_NAME_LEN 12
41 #define REPLY_BUF_LENGTH (4096 * 10)
42 #define REPLY_BUF_SMALL_LENGTH 64
43 #define REPLY_BUF_STA_INFO_LENGTH 2048
44 #define CMD_FREQ_MAX_LEN 8
45 #define STA_NO_LEN 2
46 #define FREQ_MAX_SIZE 100
47 #define CMD_BUFFER_MIN_SIZE 15
48 
49 const int WPA_QUOTATION_MARKS_FLAG_YES = 0;
50 const int WPA_QUOTATION_MARKS_FLAG_NO = 1;
51 
52 const unsigned int HT_OPER_EID = 61;
53 const unsigned int VHT_OPER_EID = 192;
54 const unsigned int EXT_EXIST_EID = 255;
55 const unsigned int EXT_HE_OPER_EID = 36;
56 const unsigned int HE_OPER_BASIC_LEN = 6;
57 const unsigned int VHT_OPER_INFO_EXTST_MASK = 0x40;
58 const unsigned int GHZ_HE_INFO_EXIST_MASK_6 = 0x02;
59 const unsigned int GHZ_HE_WIDTH_MASK_6 = 0x03;
60 const unsigned int BSS_EXIST_MASK = 0x80;
61 const unsigned int VHT_OPER_INFO_BEGIN_INDEX = 6;
62 const unsigned int VHT_INFO_SIZE = 3;
63 const unsigned int HT_INFO_SIZE = 3;
64 const unsigned int UINT8_MASK = 0xFF;
65 const unsigned int UNSPECIFIED = -1;
66 const unsigned int MAX_INFO_ELEMS_SIZE = 256;
67 const unsigned int SUPP_RATES_SIZE = 8;
68 const unsigned int EXT_SUPP_RATES_SIZE = 4;
69 const unsigned int SUPPORTED_RATES_EID = 1;
70 const unsigned int ERP_EID = 42;
71 const unsigned int EXT_SUPPORTED_RATES_EID = 50;
72 
73 const unsigned int BAND_5_GHZ = 2;
74 const unsigned int BAND_6_GHZ = 8;
75 const unsigned int CHAN_WIDTH_20MHZ = 0;
76 const unsigned int CHAN_WIDTH_40MHZ = 1;
77 const unsigned int CHAN_WIDTH_80MHZ = 2;
78 const unsigned int CHAN_WIDTH_160MHZ = 3;
79 const unsigned int CHAN_WIDTH_80MHZ_MHZ = 4;
80 
81 WifiWpaStaInterface *g_wpaStaInterface = NULL;
82 
83 static WpaSsidField g_wpaHalSsidFields[] = {
84     {DEVICE_CONFIG_SSID, "ssid", WPA_QUOTATION_MARKS_FLAG_YES},
85     {DEVICE_CONFIG_PSK, "psk", WPA_QUOTATION_MARKS_FLAG_YES},
86     {DEVICE_CONFIG_KEYMGMT, "key_mgmt", WPA_QUOTATION_MARKS_FLAG_NO},
87     {DEVICE_CONFIG_PRIORITY, "priority", WPA_QUOTATION_MARKS_FLAG_NO},
88     {DEVICE_CONFIG_SCAN_SSID, "scan_ssid", WPA_QUOTATION_MARKS_FLAG_NO},
89     {DEVICE_CONFIG_EAP, "eap", WPA_QUOTATION_MARKS_FLAG_NO},
90     {DEVICE_CONFIG_IDENTITY, "identity", WPA_QUOTATION_MARKS_FLAG_YES},
91     {DEVICE_CONFIG_PASSWORD, "password", WPA_QUOTATION_MARKS_FLAG_YES},
92     {DEVICE_CONFIG_BSSID, "bssid", WPA_QUOTATION_MARKS_FLAG_NO},
93     {DEVICE_CONFIG_AUTH_ALGORITHMS, "auth_alg", WPA_QUOTATION_MARKS_FLAG_NO},
94     {DEVICE_CONFIG_WEP_KEY_IDX, "wep_tx_keyidx", WPA_QUOTATION_MARKS_FLAG_NO},
95     {DEVICE_CONFIG_WEP_KEY_0, "wep_key0", WPA_QUOTATION_MARKS_FLAG_NO},
96     {DEVICE_CONFIG_WEP_KEY_1, "wep_key1", WPA_QUOTATION_MARKS_FLAG_NO},
97     {DEVICE_CONFIG_WEP_KEY_2, "wep_key2", WPA_QUOTATION_MARKS_FLAG_NO},
98     {DEVICE_CONFIG_WEP_KEY_3, "wep_key3", WPA_QUOTATION_MARKS_FLAG_NO},
99     {DEVICE_CONFIG_EAP_CLIENT_CERT, "client_cert", WPA_QUOTATION_MARKS_FLAG_YES},
100     {DEVICE_CONFIG_EAP_PRIVATE_KEY, "private_key", WPA_QUOTATION_MARKS_FLAG_YES},
101     {DEVICE_CONFIG_EAP_PHASE2METHOD, "phase2", WPA_QUOTATION_MARKS_FLAG_YES},
102     {DEVICE_CONFIG_IEEE80211W, "ieee80211w", WPA_QUOTATION_MARKS_FLAG_NO},
103     {DEVICE_CONFIG_ALLOW_PROTOCOLS, "proto", WPA_QUOTATION_MARKS_FLAG_NO},
104     {DEVICE_CONFIG_GROUP_CIPHERS, "group", WPA_QUOTATION_MARKS_FLAG_NO},
105     {DEVICE_CONFIG_PAIRWISE_CIPHERS, "pairwise", WPA_QUOTATION_MARKS_FLAG_NO},
106     {DEVICE_CONFIG_SAE_PASSWD, "sae_password", WPA_QUOTATION_MARKS_FLAG_YES},
107     {DEVICE_CONFIG_EAP_CA_CERT, "ca_cert", WPA_QUOTATION_MARKS_FLAG_YES},
108     {DEVICE_CONFIG_EAP_CERT_PWD, "private_key_passwd", WPA_QUOTATION_MARKS_FLAG_YES},
109     {DEVICE_CONFIG_WAPI_CA_CERT, "wapi_ca_cert", WPA_QUOTATION_MARKS_FLAG_YES},
110     {DEVICE_CONFIG_WAPI_USER_CERT, "wapi_user_sel_cert", WPA_QUOTATION_MARKS_FLAG_YES},
111     {DEVICE_CONFIG_WAPI_PSK_KEY_TYPE, "psk_key_type", WPA_QUOTATION_MARKS_FLAG_NO},
112     {DEVICE_CONFIG_WAPI_PSK, "wapi_psk", WPA_QUOTATION_MARKS_FLAG_YES},
113 };
114 
WpaCliCmdStatus(WifiWpaStaInterface * this,const char * ifName,struct WpaHalCmdStatus * pcmd)115 static int WpaCliCmdStatus(WifiWpaStaInterface *this, const char*ifName, struct WpaHalCmdStatus *pcmd)
116 {
117     if (this == NULL || pcmd == NULL) {
118         return -1;
119     }
120     char cmd[CMD_BUFFER_SIZE] = {0};
121     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s STATUS", ifName == NULL ? this->ifname : ifName) < 0) {
122         HDF_LOGE("snprintf error");
123         return -1;
124     }
125     char buf[REPLY_BUF_LENGTH] = {0};
126     if (WpaCliCmd(cmd, buf, REPLY_BUF_LENGTH) != 0) {
127         return -1;
128     }
129     char *savedPtr = NULL;
130     char *key = strtok_r(buf, "=", &savedPtr);
131     while (key != NULL) {
132         char *value = strtok_r(NULL, "\n", &savedPtr);
133         if (strcmp(key, "bssid") == 0) {
134             if (strcpy_s(pcmd->bssid, sizeof(pcmd->bssid), value) != EOK) {
135                 HDF_LOGE("%{public}s strcpy failed", __func__);
136             }
137         } else if (strcmp(key, "freq") == 0) {
138             pcmd->freq = atoi(value);
139         } else if (strcmp(key, "ssid") == 0) {
140             if (strcpy_s(pcmd->ssid, sizeof(pcmd->ssid), value) != EOK) {
141                 HDF_LOGE("%{public}s strcpy failed", __func__);
142             }
143             printf_decode((u8 *)pcmd->ssid, sizeof(pcmd->ssid), pcmd->ssid);
144         } else if (strcmp(key, "id") == 0) {
145             pcmd->id = atoi(value);
146         } else if (strcmp(key, "key_mgmt") == 0) {
147             if (strcpy_s(pcmd->keyMgmt, sizeof(pcmd->keyMgmt), value) != EOK) {
148                 HDF_LOGE("%{public}s strcpy failed", __func__);
149             }
150         } else if (strcmp(key, "address") == 0) {
151             if (strcpy_s(pcmd->address, sizeof(pcmd->address), value) != EOK) {
152                 HDF_LOGE("%{public}s strcpy failed", __func__);
153             }
154         }
155 
156         key = strtok_r(NULL, "=", &savedPtr);
157     }
158     if (strcmp(pcmd->address, "") == 0) {
159         return -1;
160     }
161     if (strcmp(pcmd->bssid, "") == 0) {
162         return 1;
163     }
164     return 0;
165 }
166 
WpaCliCmdAddNetworks(WifiWpaStaInterface * this)167 static int WpaCliCmdAddNetworks(WifiWpaStaInterface *this)
168 {
169     if (this == NULL) {
170         return -1;
171     }
172     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
173     char cmd[CMD_BUFFER_SIZE] = {0};
174     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s ADD_NETWORK", this->ifname) < 0) {
175         HDF_LOGE("snprintf error");
176         return -1;
177     }
178     if (WpaCliCmd(cmd, buf, sizeof(buf)) != 0) {
179         return -1;
180     }
181     return atoi(buf);
182 }
183 
WpaCliCmdReconnect(WifiWpaStaInterface * this)184 static int WpaCliCmdReconnect(WifiWpaStaInterface *this)
185 {
186     if (this == NULL) {
187         return -1;
188     }
189     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
190     char cmd[CMD_BUFFER_SIZE] = {0};
191     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s RECONNECT", this->ifname) < 0) {
192         HDF_LOGE("snprintf error");
193         return -1;
194     }
195     return WpaCliCmd(cmd, buf, sizeof(buf));
196 }
197 
WpaCliCmdReassociate(WifiWpaStaInterface * this)198 static int WpaCliCmdReassociate(WifiWpaStaInterface *this)
199 {
200     if (this == NULL) {
201         return -1;
202     }
203     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
204     char cmd[CMD_BUFFER_SIZE] = {0};
205     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s REASSOCIATE", this->ifname) < 0) {
206         HDF_LOGE("snprintf error");
207         return -1;
208     }
209     return WpaCliCmd(cmd, buf, sizeof(buf));
210 }
211 
WpaCliCmdDisconnect(WifiWpaStaInterface * this)212 static int WpaCliCmdDisconnect(WifiWpaStaInterface *this)
213 {
214     if (this == NULL) {
215         return -1;
216     }
217     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
218     char cmd[CMD_BUFFER_SIZE] = {0};
219     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s DISCONNECT", this->ifname) < 0) {
220         HDF_LOGE("snprintf error");
221         return -1;
222     }
223     return WpaCliCmd(cmd, buf, sizeof(buf));
224 }
225 
WpaCliCmdSaveConfig(WifiWpaStaInterface * this)226 static int WpaCliCmdSaveConfig(WifiWpaStaInterface *this)
227 {
228     if (this == NULL) {
229         return -1;
230     }
231     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
232     char cmd[CMD_BUFFER_SIZE] = {0};
233     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s SAVE_CONFIG", this->ifname) < 0) {
234         HDF_LOGE("snprintf error");
235         return -1;
236     }
237     return WpaCliCmd(cmd, buf, sizeof(buf));
238 }
239 
WpaCliCmdSetNetwork(WifiWpaStaInterface * this,const struct WpaSetNetworkArgv * argv)240 static int WpaCliCmdSetNetwork(WifiWpaStaInterface *this, const struct WpaSetNetworkArgv *argv)
241 {
242     if (this == NULL || argv == NULL) {
243         return -1;
244     }
245     int pos = -1;
246     for (int i = 0; i < (int)(sizeof(g_wpaHalSsidFields) / sizeof(g_wpaHalSsidFields[0])); ++i) {
247         if (g_wpaHalSsidFields[i].field == argv->param) {
248             pos = i;
249             break;
250         }
251     }
252     if (pos < 0) {
253         HDF_LOGE("%{public}s unsupported param: %{public}d", __func__, argv->param);
254         return -1;
255     }
256     char cmd[CMD_BUFFER_SIZE] = {0};
257     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
258     int res;
259 
260     res = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s SET_NETWORK %d %s %s", this->ifname,
261             argv->id, g_wpaHalSsidFields[pos].fieldName, argv->value);
262     HDF_LOGI("%{public}s cmd= %{private}s", __func__, cmd);
263     if (res < 0) {
264         HDF_LOGE("%{public}s Internal error, set request message failed!", __func__);
265         return -1;
266     }
267     return WpaCliCmd(cmd, buf, sizeof(buf));
268 }
269 
WpaCliCmdEnableNetwork(WifiWpaStaInterface * this,int networkId)270 static int WpaCliCmdEnableNetwork(WifiWpaStaInterface *this, int networkId)
271 {
272     if (this == NULL) {
273         return -1;
274     }
275     char cmd[CMD_BUFFER_SIZE] = {0};
276     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
277     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s ENABLE_NETWORK %d", this->ifname, networkId) < 0) {
278         HDF_LOGE("snprintf err");
279         return -1;
280     }
281     return WpaCliCmd(cmd, buf, sizeof(buf));
282 }
283 
WpaCliCmdSelectNetwork(WifiWpaStaInterface * this,int networkId)284 static int WpaCliCmdSelectNetwork(WifiWpaStaInterface *this, int networkId)
285 {
286     if (this == NULL) {
287         return -1;
288     }
289     char cmd[CMD_BUFFER_SIZE] = {0};
290     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
291     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s SELECT_NETWORK %d", this->ifname, networkId) < 0) {
292         HDF_LOGE("snprintf err");
293         return -1;
294     }
295     return WpaCliCmd(cmd, buf, sizeof(buf));
296 }
297 
WpaCliCmdDisableNetwork(WifiWpaStaInterface * this,int networkId)298 static int WpaCliCmdDisableNetwork(WifiWpaStaInterface *this, int networkId)
299 {
300     if (this == NULL) {
301         return -1;
302     }
303     char cmd[CMD_BUFFER_SIZE] = {0};
304     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
305     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s DISABLE_NETWORK %d", this->ifname, networkId) < 0) {
306         HDF_LOGE("snprintf err");
307         return -1;
308     }
309     return WpaCliCmd(cmd, buf, sizeof(buf));
310 }
311 
WpaCliCmdRemoveNetwork(WifiWpaStaInterface * this,int networkId)312 static int WpaCliCmdRemoveNetwork(WifiWpaStaInterface *this, int networkId)
313 {
314     if (this == NULL) {
315         return -1;
316     }
317     char cmd[CMD_BUFFER_SIZE] = {0};
318     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
319     int res = 0;
320     if (networkId == -1) {
321         res = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s REMOVE_NETWORK all", this->ifname);
322     } else if (networkId >= 0) {
323         res = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s REMOVE_NETWORK %d", this->ifname, networkId);
324     } else {
325         return -1;
326     }
327     if (res < 0) {
328         HDF_LOGE("snprintf err");
329         return -1;
330     }
331     return WpaCliCmd(cmd, buf, sizeof(buf));
332 }
333 
WpaCliCmdGetNetwork(WifiWpaStaInterface * this,const struct WpaGetNetworkArgv * argv,char * pcmd,unsigned size)334 static int WpaCliCmdGetNetwork(
335     WifiWpaStaInterface *this, const struct WpaGetNetworkArgv *argv, char *pcmd, unsigned size)
336 {
337     if (this == NULL || argv == NULL || pcmd == NULL) {
338         return -1;
339     }
340     char cmd[CMD_BUFFER_SIZE] = {0};
341     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s GET_NETWORK %d %s", this->ifname, argv->id,
342         argv->param) < 0) {
343         HDF_LOGE("snprintf err");
344         return -1;
345     }
346     char *buf = (char *)calloc(REPLY_BUF_LENGTH, sizeof(char));
347     if (buf == NULL) {
348         return -1;
349     }
350     if (WpaCliCmd(cmd, buf, REPLY_BUF_LENGTH) != 0) {
351         free(buf);
352         return -1;
353     }
354     if (strncpy_s(pcmd, size, buf, strlen(buf)) != EOK) {
355         HDF_LOGE("copy set get_network result failed!");
356         free(buf);
357         return -1;
358     }
359     free(buf);
360     return 0;
361 }
362 
WpaCliCmdWpsPbc(WifiWpaStaInterface * this,const struct WpaWpsPbcArgv * wpspbc)363 static int WpaCliCmdWpsPbc(WifiWpaStaInterface *this, const struct WpaWpsPbcArgv *wpspbc)
364 {
365     if (this == NULL) {
366         return -1;
367     }
368     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
369     char cmd[CMD_BUFFER_SIZE] = {0};
370     int pos = 0;
371     int res = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s WPS_PBC", this->ifname);
372     if (res < 0) {
373         HDF_LOGE("snprintf err");
374         return -1;
375     }
376     pos += res;
377     if (wpspbc != NULL) {
378         res = 0; /* reset res value */
379         if (wpspbc->anyFlag == 1) {
380             res = snprintf_s(cmd + pos, sizeof(cmd) - pos, sizeof(cmd) - pos - 1, " %s", "any");
381         } else if (strlen(wpspbc->bssid) > 0) {
382             res = snprintf_s(cmd + pos, sizeof(cmd) - pos, sizeof(cmd) - pos - 1, " %s", wpspbc->bssid);
383         }
384         if (res < 0) {
385             HDF_LOGE("snprintf err");
386             return -1;
387         }
388         pos += res;
389         if (wpspbc->multiAp > 0) { /* The value of ap needs to be determined. The value is greater than 0. */
390             res = snprintf_s(
391                 cmd + pos, sizeof(cmd) - pos, sizeof(cmd) - pos - 1, " multi_ap=%d", wpspbc->multiAp);
392             if (res < 0) {
393                 HDF_LOGE("snprintf err");
394                 return -1;
395             }
396         }
397     }
398     if (WpaCliCmd(cmd, buf, sizeof(buf)) != 0) {
399         HDF_LOGE("wps_pbc return failed!");
400         return -1;
401     }
402     if (strncmp(buf, "FAIL-PBC-OVERLAP", strlen("FAIL-PBC-OVERLAP")) == 0) {
403         HDF_LOGE("wps_pbc success, but result err: buf =%{public}s", buf);
404         return FAIL_PBC_OVERLAP_RETUEN; /* Add a new enumerated value. */
405     }
406     return 0;
407 }
408 
WpaCliCmdWpsPin(WifiWpaStaInterface * this,const struct WpaWpsPinArgv * wpspin,int * pincode)409 static int WpaCliCmdWpsPin(WifiWpaStaInterface *this, const struct WpaWpsPinArgv *wpspin, int *pincode)
410 {
411     if (this == NULL || wpspin == NULL || pincode == NULL) {
412         return -1;
413     }
414     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
415     char cmd[CMD_BUFFER_SIZE] = {0};
416     int pos = 0;
417     int res = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s WPS_PIN", this->ifname);
418     if (res < 0) {
419         HDF_LOGE("snprintf err");
420         return -1;
421     }
422     pos += res;
423     if (strlen(wpspin->bssid) > 0) {
424         res = snprintf_s(cmd + pos, sizeof(cmd) - pos, sizeof(cmd) - pos - 1, " %s", wpspin->bssid);
425     } else {
426         res = snprintf_s(cmd + pos, sizeof(cmd) - pos, sizeof(cmd) - pos - 1, " any");
427     }
428     if (res < 0) {
429         HDF_LOGE("snprintf err");
430         return -1;
431     }
432     pos += res;
433     if (strlen(wpspin->pinCode) > 0) {
434         res = snprintf_s(cmd + pos, sizeof(cmd) - pos, sizeof(cmd) - pos - 1, " %s", wpspin->pinCode);
435         if (res < 0) {
436             HDF_LOGE("snprintf err");
437             return -1;
438         }
439     }
440     if (WpaCliCmd(cmd, buf, sizeof(buf)) != 0) {
441         HDF_LOGE("wps_pin return failed!");
442         return -1;
443     }
444     *pincode = StrtoInt(buf);
445     return 0;
446 }
447 
WpaCliCmdWpsCancel(WifiWpaStaInterface * this)448 static int WpaCliCmdWpsCancel(WifiWpaStaInterface *this)
449 {
450     if (this == NULL) {
451         return -1;
452     }
453     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
454     char cmd[CMD_BUFFER_SIZE] = {0};
455     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s WPS_CANCEL", this->ifname) < 0) {
456         HDF_LOGE("snprintf err");
457         return -1;
458     }
459     return WpaCliCmd(cmd, buf, sizeof(buf));
460 }
461 
WpaCliCmdPowerSave(WifiWpaStaInterface * this,int enable)462 static int WpaCliCmdPowerSave(WifiWpaStaInterface *this, int enable)
463 {
464     if (this == NULL) {
465         return -1;
466     }
467     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
468     char cmd[CMD_BUFFER_SIZE] = {0};
469     int ret;
470     if (enable) {
471         ret = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s SET PS 1", this->ifname);
472     } else {
473         ret = snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s SET PS 0", this->ifname);
474     }
475     if (ret < 0) {
476         HDF_LOGE("snprintf err");
477         return -1;
478     }
479     return WpaCliCmd(cmd, buf, sizeof(buf));
480 }
481 
WpaCliCmdSetRoamConfig(WifiWpaStaInterface * this,const char * bssid)482 static int WpaCliCmdSetRoamConfig(WifiWpaStaInterface *this, const char *bssid)
483 {
484     if (this == NULL || bssid == NULL) {
485         return -1;
486     }
487     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
488     char cmd[CMD_BUFFER_SIZE] = {0};
489     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s SET bssid %s", this->ifname, bssid) < 0) {
490         HDF_LOGE("snprintf err");
491         return -1;
492     }
493     return WpaCliCmd(cmd, buf, sizeof(buf));
494 }
495 
WpaCliCmdSetCountryCode(WifiWpaStaInterface * this,const char * countryCode)496 static int WpaCliCmdSetCountryCode(WifiWpaStaInterface *this, const char *countryCode)
497 {
498     if (this == NULL || countryCode == NULL) {
499         return -1;
500     }
501     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
502     char cmd[CMD_BUFFER_SIZE] = {0};
503     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s DRIVER COUNTRY %s", this->ifname, countryCode) < 0) {
504         HDF_LOGE("snprintf err");
505         return -1;
506     }
507     return WpaCliCmd(cmd, buf, sizeof(buf));
508 }
509 
WpaCliCmdGetCountryCode(WifiWpaStaInterface * this,char * countryCode,int codeSize)510 static int WpaCliCmdGetCountryCode(WifiWpaStaInterface *this, char *countryCode, int codeSize)
511 {
512     if (this == NULL || countryCode == NULL) {
513         return -1;
514     }
515     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
516     char cmd[CMD_BUFFER_SIZE] = {0};
517     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s GET country", this->ifname) < 0) {
518         HDF_LOGE("snprintf err");
519         return -1;
520     }
521     if (WpaCliCmd(cmd, buf, sizeof(buf)) != 0) {
522         HDF_LOGE("get countrycode failed");
523         return -1;
524     }
525     if (strncpy_s(countryCode, codeSize, buf, strlen(buf)) != EOK) {
526         HDF_LOGE("copy set country code failed!");
527         return -1;
528     }
529     return 0;
530 }
531 
WpaCliCmdGetConnectionCapabilities(WifiWpaStaInterface * this,struct ConnectionCapabilities * connectionCap)532 static int WpaCliCmdGetConnectionCapabilities(WifiWpaStaInterface *this, struct ConnectionCapabilities *connectionCap)
533 {
534     if (this == NULL || connectionCap == NULL) {
535         return -1;
536     }
537     char buf[REPLY_BUF_LENGTH] = {0};
538     char cmd[CMD_BUFFER_SIZE] = {0};
539     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s GET_CONNECTION_CAPABILITY", this->ifname) < 0) {
540         HDF_LOGE("snprintf err");
541         return -1;
542     }
543 
544     if (WpaCliCmd(cmd, buf, sizeof(buf)) != 0) {
545         HDF_LOGE("WpaCliCmd GET_CONNECTION_CAPABILITY failed");
546         return -1;
547     }
548     char *savedPtr = NULL;
549     char *key = strtok_r(buf, "=", &savedPtr);
550     while (key != NULL) {
551         char *value = strtok_r(NULL, "\n", &savedPtr);
552         if (strcmp(key, "technology") == 0) {
553             connectionCap->technology = atoi(value);
554         } else if (strcmp(key, "channelBandwidth") == 0) {
555             connectionCap->channelBandwidth = atoi(value);
556         }  else if (strcmp(key, "maxNumberTxSpatialStreams") == 0) {
557             connectionCap->maxNumberTxSpatialStreams = atoi(value);
558         }  else if (strcmp(key, "maxNumberRxSpatialStreams") == 0) {
559             connectionCap->maxNumberRxSpatialStreams = atoi(value);
560         }  else if (strcmp(key, "legacyMode") == 0) {
561             connectionCap->legacyMode = atoi(value);
562         }
563         key = strtok_r(NULL, "=", &savedPtr);
564     }
565     HDF_LOGI("WpaCliCmdGetConnectionCapabilities technology =%d channelBandwidth = %d", connectionCap->technology,
566         connectionCap->channelBandwidth);
567     return 0;
568 }
569 
WpaCliCmdGetRequirePmf(WifiWpaStaInterface * this,int * enable)570 static int WpaCliCmdGetRequirePmf(WifiWpaStaInterface *this, int *enable)
571 {
572     if (this == NULL || enable == NULL) {
573         return -1;
574     }
575     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
576     char cmd[CMD_BUFFER_SIZE] = {0};
577     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s GET_REQUIRE_PMF", this->ifname) < 0) {
578         HDF_LOGE("snprintf err");
579         return -1;
580     }
581 
582     if (WpaCliCmd(cmd, buf, sizeof(buf)) != 0) {
583         HDF_LOGE("WpaCliCmd GET_REQUIRE_PMF failed");
584         return -1;
585     }
586     char *savedPtr = NULL;
587     char *key = strtok_r(buf, "=", &savedPtr);
588     while (key != NULL) {
589         char *value = strtok_r(NULL, "\n", &savedPtr);
590         if (strcmp(key, "require_pmf") == 0) {
591             *enable = atoi(value);
592         }
593         key = strtok_r(NULL, "=", &savedPtr);
594     }
595     HDF_LOGI("WpaCliCmdGetRequirePmf enable =%d ", *enable);
596     return 0;
597 }
598 
WpaCliCmdWepKeyTxKeyIdx(WifiWpaStaInterface * this,int * keyIdx)599 static int WpaCliCmdWepKeyTxKeyIdx(WifiWpaStaInterface *this, int *keyIdx)
600 {
601     if (this == NULL || keyIdx == NULL) {
602         return -1;
603     }
604     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
605     char cmd[CMD_BUFFER_SIZE] = {0};
606     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s GET_WEP_KEY_IDX", this->ifname) < 0) {
607         HDF_LOGE("snprintf err");
608         return -1;
609     }
610 
611     if (WpaCliCmd(cmd, buf, sizeof(buf)) != 0) {
612         HDF_LOGE("WpaCliCmd GET_WEP_KEY_IDX failed");
613         return -1;
614     }
615     char *savedPtr = NULL;
616     char *key = strtok_r(buf, "=", &savedPtr);
617     while (key != NULL) {
618         char *value = strtok_r(NULL, "\n", &savedPtr);
619         if (strcmp(key, "wep_tx_keyidx") == 0) {
620             *keyIdx = StrtoInt(value);
621         }
622         key = strtok_r(NULL, "=", &savedPtr);
623     }
624     HDF_LOGI("WpaCliCmdWepKeyTxKeyIdx keyIdx =%d ", *keyIdx);
625     return 0;
626 }
627 
WpaCliCmdWepKey(WifiWpaStaInterface * this,int keyIdx,unsigned char * wepKey,unsigned int * wepKeyLen)628 static int WpaCliCmdWepKey(WifiWpaStaInterface *this, int keyIdx, unsigned char *wepKey, unsigned int *wepKeyLen)
629 {
630     if (this == NULL || wepKey == NULL || wepKeyLen == NULL) {
631         return -1;
632     }
633     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
634     char cmd[CMD_BUFFER_SIZE] = {0};
635     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s GET_WEP_KEY GET_WEP_KEY_IDX %d",
636 		this->ifname, keyIdx) < 0) {
637         HDF_LOGE("snprintf err");
638         return -1;
639     }
640 
641     if (WpaCliCmd(cmd, buf, sizeof(buf)) != 0) {
642         HDF_LOGE("WpaCliCmd WepKey failed");
643         return -1;
644     }
645     char *savedPtr = NULL;
646     char *key = strtok_r(buf, "=", &savedPtr);
647     while (key != NULL) {
648         char *value = strtok_r(NULL, "\n", &savedPtr);
649         if (strcmp(key, "wep_key") == 0) {
650             if (strncpy_s((char *)wepKey, strlen(value), value, strlen(value)) != 0) {
651                 HDF_LOGE("copy wep_key failed!");
652                 return -1;
653             }
654             *wepKeyLen = strlen(value);
655         }
656         key = strtok_r(NULL, "=", &savedPtr);
657     }
658     HDF_LOGI("WpaCliCmdWepKey wepKey =%{private}s", wepKey);
659     return 0;
660 }
661 
WpaCliCmdGetPsk(WifiWpaStaInterface * this,unsigned char * psk,unsigned int * pskLen)662 static int WpaCliCmdGetPsk(WifiWpaStaInterface *this, unsigned char *psk, unsigned int *pskLen)
663 {
664     if (this == NULL || psk == NULL || pskLen == NULL) {
665         return -1;
666     }
667     char buf[REPLY_BUF_LENGTH] = {0};
668     char cmd[CMD_BUFFER_SIZE] = {0};
669     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s GET_PSK", this->ifname) < 0) {
670         HDF_LOGE("snprintf err");
671         return -1;
672     }
673 
674     if (WpaCliCmd(cmd, buf, sizeof(buf)) != 0) {
675         HDF_LOGE("WpaCliCmd GET_PSK failed");
676         return -1;
677     }
678     char *savedPtr = NULL;
679     char *key = strtok_r(buf, "=", &savedPtr);
680     while (key != NULL) {
681         char *value = strtok_r(NULL, "\n", &savedPtr);
682         if (strcmp(key, "psk") == 0) {
683             if (strncpy_s((char *)psk, strlen(value), value, strlen(value)) != 0) {
684                 HDF_LOGE("copy psk failed!");
685                 return -1;
686             }
687             *pskLen = strlen(value);
688         }
689         key = strtok_r(NULL, "=", &savedPtr);
690     }
691     HDF_LOGI("WpaCliCmdGetPsk psk =%{private}s", psk);
692     return 0;
693 }
694 
WpaCliCmdGetPskPassphrase(WifiWpaStaInterface * this,char * psk,unsigned int pskLen)695 static int WpaCliCmdGetPskPassphrase(WifiWpaStaInterface *this, char *psk, unsigned int pskLen)
696 {
697     if (this == NULL || psk == NULL) {
698         return -1;
699     }
700     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
701     char cmd[CMD_BUFFER_SIZE] = {0};
702     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s GET_PSK_PASSPHRASE", this->ifname) < 0) {
703         HDF_LOGE("snprintf err");
704         return -1;
705     }
706 
707     if (WpaCliCmd(cmd, buf, sizeof(buf)) != 0) {
708         HDF_LOGE("WpaCliCmd GET_PSK_PASSPHRASE failed");
709         return -1;
710     }
711     char *savedPtr = NULL;
712     char *key = strtok_r(buf, "=", &savedPtr);
713     while (key != NULL) {
714         char *value = strtok_r(NULL, "\n", &savedPtr);
715         if (strcmp(key, "passphrase") == 0) {
716             if (strncpy_s((char *)psk, (int)pskLen, value, strlen(value)) != 0) {
717                 HDF_LOGE("copy passphrase failed!");
718                 return -1;
719             }
720         }
721         key = strtok_r(NULL, "=", &savedPtr);
722     }
723     HDF_LOGI("WpaCliCmdGetPskPassphrase psk = %{private}s", psk);
724     return 0;
725 }
726 
WpaCliCmdGetScanSsid(WifiWpaStaInterface * this,int * scanSsid)727 static int WpaCliCmdGetScanSsid(WifiWpaStaInterface *this, int *scanSsid)
728 {
729     if (this == NULL || scanSsid == NULL) {
730         return -1;
731     }
732     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
733     char cmd[CMD_BUFFER_SIZE] = {0};
734     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s GET_SCAN_SSID", this->ifname) < 0) {
735         HDF_LOGE("snprintf err");
736         return -1;
737     }
738 
739     if (WpaCliCmd(cmd, buf, sizeof(buf)) != 0) {
740         HDF_LOGE("WpaCliCmd GET_SCAN_SSID failed");
741         return -1;
742     }
743     char *savedPtr = NULL;
744     char *key = strtok_r(buf, "=", &savedPtr);
745     while (key != NULL) {
746         char *value = strtok_r(NULL, "\n", &savedPtr);
747         if (strcmp(key, "scan_ssid") == 0) {
748             *scanSsid = StrtoInt(value);
749         }
750         key = strtok_r(NULL, "=", &savedPtr);
751     }
752     HDF_LOGI("WpaCliCmdGetScanSsid scanSsid =%d ", *scanSsid);
753     return 0;
754 }
755 
WpaCliCmdSetAutoConnect(WifiWpaStaInterface * this,int enable)756 static int WpaCliCmdSetAutoConnect(WifiWpaStaInterface *this, int enable)
757 {
758     if (this == NULL) {
759         return -1;
760     }
761     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
762     char cmd[CMD_BUFFER_SIZE] = {0};
763     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s STA_AUTOCONNECT %d", this->ifname, enable) < 0) {
764         HDF_LOGE("snprintf err");
765         return -1;
766     }
767     return WpaCliCmd(cmd, buf, sizeof(buf));
768 }
769 
WpaCliCmdWpaBlockListClear(WifiWpaStaInterface * this)770 static int WpaCliCmdWpaBlockListClear(WifiWpaStaInterface *this)
771 {
772     if (this == NULL) {
773         return -1;
774     }
775     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
776     char cmd[CMD_BUFFER_SIZE] = {0};
777     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s BL%cCKLIST clear", this->ifname, 'A') < 0) {
778         HDF_LOGE("snprintf err");
779         return -1;
780     }
781     return WpaCliCmd(cmd, buf, sizeof(buf));
782 }
783 
ListNetworkProcess(WifiNetworkInfo * pcmd,char * tmpBuf,int bufLeng)784 static void ListNetworkProcess(WifiNetworkInfo *pcmd, char *tmpBuf, int bufLeng)
785 {
786     int start = 0; /* start pos */
787     int end = 0;   /* end pos */
788     int i = 0;
789     while (end < bufLeng) {
790         if (tmpBuf[end] != '\t') {
791             ++end;
792             continue;
793         }
794         tmpBuf[end] = '\0';
795         if (i == COLUMN_INDEX_ZERO) {
796             pcmd->id = atoi(tmpBuf);
797         } else if (i == COLUMN_INDEX_ONE) {
798             if (strcpy_s(pcmd->ssid, sizeof(pcmd->ssid), tmpBuf + start) != EOK) {
799                 break;
800             }
801             printf_decode((u8 *)pcmd->ssid, sizeof(pcmd->ssid), pcmd->ssid);
802         } else if (i == COLUMN_INDEX_TWO) {
803             if (strcpy_s(pcmd->bssid, sizeof(pcmd->bssid), tmpBuf + start) != EOK) {
804                 break;
805             }
806             start = end + 1;
807             if (strcpy_s(pcmd->flags, sizeof(pcmd->flags), tmpBuf + start) != EOK) {
808                 break;
809             }
810             break;
811         }
812         ++i;
813         end++;
814         start = end;
815     }
816     return;
817 }
818 
WpaCliCmdListNetworks(WifiWpaStaInterface * this,WifiNetworkInfo * pcmd,int * size)819 static int WpaCliCmdListNetworks(WifiWpaStaInterface *this, WifiNetworkInfo *pcmd, int *size)
820 {
821     if (this == NULL || pcmd == NULL || size == NULL || *size <= 0) {
822         return -1;
823     }
824     char cmd[CMD_BUFFER_SIZE] = {0};
825     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s LIST_NETWORKS", this->ifname) < 0) {
826         HDF_LOGE("snprintf err");
827         return -1;
828     }
829     char *buf = (char *)calloc(REPLY_BUF_LENGTH, sizeof(char));
830     if (buf == NULL) {
831         return -1;
832     }
833     if (WpaCliCmd(cmd, buf, REPLY_BUF_LENGTH) != 0) {
834         free(buf);
835         return -1;
836     }
837     char *savedPtr = NULL;
838     strtok_r(buf, "\n", &savedPtr); /* skip first line */
839     char *token = strtok_r(NULL, "\n", &savedPtr);
840     int j = 0;
841 
842     while (token != NULL) {
843         if (j >= *size) {
844             *size = j;
845             HDF_LOGW("list_networks full!");
846             free(buf);
847             return 0;
848         }
849         int length = strlen(token);
850         if (length <= 0) {
851             break;
852         }
853         ListNetworkProcess(pcmd + j, token, length);
854         token = strtok_r(NULL, "\n", &savedPtr);
855         j++;
856     }
857     *size = j;
858     if (*size <= 0) {
859         HDF_LOGW("list_networks empty!");
860     }
861     free(buf);
862     return 0;
863 }
864 
AssignCmdLen(WifiWpaStaInterface * this,const ScanSettings * settings)865 static unsigned AssignCmdLen(WifiWpaStaInterface *this, const ScanSettings *settings)
866 {
867     if (settings->scanStyle == SCAN_TYPE_PNO) {
868         unsigned exceptedLen = strlen("IFNAME=") + strlen(this->ifname) + 1 + strlen("set pno x");
869         if (settings->isStartPnoScan) {
870             HDF_LOGI("AssignCmdLen, startPnoScan, freqSize=%{public}d", settings->freqSize);
871             if (settings->freqSize > 0) {
872                 exceptedLen += strlen(" freq=") + (CMD_FREQ_MAX_LEN + 1) * settings->freqSize;
873             }
874         }
875         return exceptedLen;
876     }
877     unsigned exceptedLen = strlen("IFNAME=") + strlen(this->ifname) + 1 + strlen("SCAN");
878     HDF_LOGI("AssignCmdLen, startScan, freSize=%{public}d, hiddenSsidSize=%{public}d",
879         settings->freqSize, settings->hiddenSsidSize);
880     if (settings->freqSize > 0) {
881         exceptedLen += strlen(" freq=") + (CMD_FREQ_MAX_LEN + 1) * settings->freqSize;
882     }
883     for (int i = 0; i < settings->hiddenSsidSize; ++i) {
884         unsigned ssidLen = strlen(settings->hiddenSsid[i]);
885         exceptedLen += strlen(" ssid ") + (ssidLen << 1);
886     }
887     return exceptedLen;
888 }
889 
ConcatScanSetting(const ScanSettings * settings,char * buff,int len)890 static int ConcatScanSetting(const ScanSettings *settings, char *buff, int len)
891 {
892     if (settings == NULL || (settings->scanStyle == SCAN_TYPE_PNO && !settings->isStartPnoScan)) {
893         return 0;
894     }
895     int pos = 0;
896     int res;
897     int i;
898     if (settings->freqSize < 0 || settings->freqSize > FREQ_MAX_SIZE) {
899         HDF_LOGE("invalid parameter");
900         return 0;
901     }
902     for (i = 0; i < settings->freqSize; ++i) {
903         if (i == 0) {
904             res = snprintf_s(buff + pos, len - pos, len - pos - 1, "%s", " freq=");
905             if (res < 0) {
906                 HDF_LOGE("snprintf error");
907                 return -1;
908             }
909             pos += res;
910         }
911         if (i != (settings->freqSize - 1)) {
912             res = snprintf_s(buff + pos, len - pos, len - pos - 1, "%d,", settings->freqs[i]);
913         } else {
914             res = snprintf_s(buff + pos, len - pos, len - pos - 1, "%d;", settings->freqs[i]);
915         }
916         if (res < 0) {
917             HDF_LOGE("snprintf error");
918             return -1;
919         }
920         pos += res;
921     }
922     for (i = 0; (i < settings->hiddenSsidSize) && (settings->scanStyle != SCAN_TYPE_PNO); ++i) {
923         res = snprintf_s(buff + pos, len - pos, len - pos - 1, " ssid ");
924         if (res < 0) {
925             HDF_LOGE("snprintf error");
926             return -1;
927         }
928         pos += res;
929         char *p = settings->hiddenSsid[i];
930         while (*p) {
931             res = snprintf_s(buff + pos, len - pos, len - pos - 1, "%02x", *p);
932             if (res < 0) {
933                 HDF_LOGE("snprintf error");
934                 return -1;
935             }
936             pos += res;
937             p++;
938         }
939     }
940     return 0;
941 }
942 
WpaCliCmdBssFlush(WifiWpaStaInterface * this)943 static int WpaCliCmdBssFlush(WifiWpaStaInterface *this)
944 {
945     if (this == NULL) {
946         return -1;
947     }
948     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
949     char cmd[CMD_BUFFER_SIZE] = {0};
950     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s BSS_FLUSH 0", this->ifname) < 0) {
951         HDF_LOGE("snprintf err");
952         return -1;
953     }
954     return WpaCliCmd(cmd, buf, sizeof(buf));
955 }
956 
WpaCliCmdScan(WifiWpaStaInterface * this,const ScanSettings * settings)957 static int WpaCliCmdScan(WifiWpaStaInterface *this, const ScanSettings *settings)
958 {
959     if (this == NULL) {
960         HDF_LOGE("WpaCliCmdScan, this is NULL!");
961         return -1;
962     }
963 
964     /* Invalidate expired scan results */
965     WpaCliCmdBssFlush(this);
966     unsigned len = CMD_BUFFER_SIZE;
967     unsigned expectedLen = 0;
968     if (settings != NULL) {
969         expectedLen = AssignCmdLen(this, settings);
970     }
971     if (expectedLen < CMD_BUFFER_MIN_SIZE || expectedLen > CMD_BUFFER_SIZE) {
972         HDF_LOGE("invalid parameter");
973         return -1;
974     }
975     if (expectedLen >= len) {
976         len = expectedLen + 1;
977     }
978     char *pcmd = (char *)calloc(len, sizeof(char));
979     if (pcmd == NULL) {
980         HDF_LOGE("WpaCliCmdScan, pcmd is NULL!");
981         return -1;
982     }
983     int pos = 0;
984     int res = 0;
985     if (settings != NULL) {
986         if (settings->scanStyle == SCAN_TYPE_PNO && settings->isStartPnoScan) {
987             res = snprintf_s(pcmd, len, len - 1, "IFNAME=%s SET PNO 1", this->ifname);
988         } else if (settings->scanStyle == SCAN_TYPE_PNO && !settings->isStartPnoScan) {
989             res = snprintf_s(pcmd, len, len - 1, "IFNAME=%s SET PNO 0", this->ifname);
990         } else {
991             res = snprintf_s(pcmd, len, len - 1, "IFNAME=%s SCAN", this->ifname);
992         }
993     }
994     if (res < 0) {
995         HDF_LOGE("WpaCliCmdScan, snprintf_s error!");
996         free(pcmd);
997         return -1;
998     }
999     pos += res;
1000     if (settings != NULL && ConcatScanSetting(settings, pcmd + pos, len - pos) < 0) {
1001         HDF_LOGE("ConcatScanSetting return failed!");
1002         free(pcmd);
1003         return -1;
1004     }
1005     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
1006     if (WpaCliCmd(pcmd, buf, sizeof(buf)) != 0) {
1007         free(pcmd);
1008         return -1;
1009     }
1010     free(pcmd);
1011     if (strncmp(buf, "FAIL-BUSY", strlen("FAIL-BUSY")) == 0) {
1012         HDF_LOGE("WpaCliCmdScan, WpaCliCmd return FAIL-BUSY!");
1013         return FAIL_BUSY;
1014     }
1015     return 0;
1016 }
1017 
ConvertChanToFreqMhz(int channel,int band)1018 static int ConvertChanToFreqMhz(int channel, int band)
1019 {
1020     int band24Ghz = 1;
1021     int channelTimes = 5;
1022 
1023     if (band == band24Ghz) {
1024         int bandFirstChNum24 = 1;
1025         int bandLastChNum24 = 14;
1026         int bandStartFreqMhz24 = 2412;
1027         int bandSpecial = 2484;
1028         int channelSpecial = 14;
1029         if (channel == channelSpecial) {
1030             return bandSpecial;
1031         } else if (channel >= bandFirstChNum24 && channel <= bandLastChNum24) {
1032             return ((channel - bandFirstChNum24) * channelTimes) + bandStartFreqMhz24;
1033         } else {
1034             return UNSPECIFIED;
1035         }
1036     }
1037     if (band == BAND_5_GHZ) {
1038         int bandFirstChMum5 = 32;
1039         int bandLastChMum5 = 173;
1040         int bandStartFreqMhz5 = 5160;
1041         if (channel >= bandFirstChMum5 && channel <= bandLastChMum5) {
1042             return ((channel - bandFirstChMum5) * channelTimes) + bandStartFreqMhz5;
1043         } else {
1044             return UNSPECIFIED;
1045         }
1046     }
1047     if (band == BAND_6_GHZ) {
1048         int bandFirstChMum6 = 1;
1049         int bandLastChMum6 = 233;
1050         int bandStartFreqMhz6 = 5955;
1051         int bandCla2Freq136ChMhz6 = 5935;
1052         int channelType = 2;
1053         if (channel >= bandFirstChMum6 && channel <= bandLastChMum6) {
1054             if (channel == channelType) {
1055                 return bandCla2Freq136ChMhz6;
1056             }
1057             return ((channel - bandFirstChMum6) * channelTimes) + bandStartFreqMhz6;
1058         } else {
1059             return UNSPECIFIED;
1060         }
1061     }
1062     return UNSPECIFIED;
1063 }
1064 
GetHeChanWidth(int heChannelWidth,int centerSegFreq0,int centerSegFreq1)1065 static int GetHeChanWidth(int heChannelWidth, int centerSegFreq0, int centerSegFreq1)
1066 {
1067     int channelWidth = 2;
1068     int segFreqValue = 8;
1069     if (heChannelWidth == 0) {
1070         return CHAN_WIDTH_20MHZ;
1071     } else if (heChannelWidth == 1) {
1072         return CHAN_WIDTH_40MHZ;
1073     } else if (heChannelWidth == channelWidth) {
1074         return CHAN_WIDTH_80MHZ;
1075     } else if (abs(centerSegFreq1 - centerSegFreq0) == segFreqValue) {
1076         return CHAN_WIDTH_160MHZ;
1077     } else {
1078         return CHAN_WIDTH_80MHZ_MHZ;
1079     }
1080 }
1081 
GetHeCentFreq(int centerSegFreq)1082 static int GetHeCentFreq(int centerSegFreq)
1083 {
1084     if (centerSegFreq == 0) {
1085         return 0;
1086     }
1087     return ConvertChanToFreqMhz(centerSegFreq, BAND_6_GHZ);
1088 }
1089 
GetHtChanWidth(int secondOffsetChannel)1090 static int GetHtChanWidth(int secondOffsetChannel)
1091 {
1092     if (secondOffsetChannel != 0) {
1093         return CHAN_WIDTH_40MHZ;
1094     } else {
1095         return CHAN_WIDTH_20MHZ;
1096     }
1097 }
1098 
GetHtCentFreq0(int primaryFrequency,int secondOffsetChannel)1099 static int GetHtCentFreq0(int primaryFrequency, int secondOffsetChannel)
1100 {
1101     if (secondOffsetChannel != 0) {
1102         int freqValue = 10;
1103         int offsetChannle = 3;
1104         if (secondOffsetChannel == 1) {
1105             return primaryFrequency + freqValue;
1106         } else if (secondOffsetChannel == offsetChannle) {
1107             return primaryFrequency - freqValue;
1108         } else {
1109             HDF_LOGE("error on get centFreq0");
1110             return 0;
1111         }
1112     } else {
1113         return primaryFrequency;
1114     }
1115 }
1116 
GetVhtChanWidth(int channelType,int centerFrequencyIndex1,int centerFrequencyIndex2)1117 static int GetVhtChanWidth(int channelType, int centerFrequencyIndex1, int centerFrequencyIndex2)
1118 {
1119     int freqValue = 8;
1120     if (channelType == 0) {
1121         return UNSPECIFIED;
1122     } else if (centerFrequencyIndex2 == 0) {
1123         return CHAN_WIDTH_80MHZ;
1124     } else if (abs(centerFrequencyIndex1 - centerFrequencyIndex2) == freqValue) {
1125         return CHAN_WIDTH_160MHZ;
1126     } else {
1127         return CHAN_WIDTH_80MHZ_MHZ;
1128     }
1129 }
1130 
GetVhtCentFreq(int channelType,int centerFrequencyIndex)1131 static int GetVhtCentFreq(int channelType, int centerFrequencyIndex)
1132 {
1133     if (centerFrequencyIndex == 0 || channelType == 0) {
1134         return 0;
1135     } else {
1136         return ConvertChanToFreqMhz(centerFrequencyIndex, BAND_5_GHZ);
1137     }
1138 }
1139 
HexStringToString(const char * str,char * out)1140 static int HexStringToString(const char *str, char *out)
1141 {
1142     unsigned len = strlen(str);
1143     if ((len & 1) != 0) {
1144         return -1;
1145     }
1146     const int hexShiftNum = 4;
1147     for (unsigned i = 0, j = 0; i + 1 < len; ++i) {
1148         int8_t high = IsValidHexCharAndConvert(str[i]);
1149         int8_t low = IsValidHexCharAndConvert(str[++i]);
1150         if (high < 0 || low < 0) {
1151             return -1;
1152         }
1153         char tmp = ((high << hexShiftNum) | (low & 0x0F));
1154         out[j] = tmp;
1155         ++j;
1156     }
1157     return 0;
1158 }
1159 
GetChanWidthCenterFreqVht(ScanInfo * pcmd,ScanInfoElem * infoElem)1160 static bool GetChanWidthCenterFreqVht(ScanInfo *pcmd, ScanInfoElem* infoElem)
1161 {
1162     if ((pcmd == NULL) || (infoElem == NULL)) {
1163         HDF_LOGE("pcmd or infoElem is NULL.");
1164         return false;
1165     }
1166     if ((infoElem->content == NULL) || ((unsigned int)infoElem->size < VHT_INFO_SIZE)) {
1167         return false;
1168     }
1169     int channelType = infoElem->content[COLUMN_INDEX_ZERO] & UINT8_MASK;
1170     int centerFrequencyIndex1 = infoElem->content[COLUMN_INDEX_ONE] & UINT8_MASK;
1171     int centerFrequencyIndex2 = infoElem->content[COLUMN_INDEX_TWO] & UINT8_MASK;
1172     pcmd->isVhtInfoExist = 1;
1173     pcmd->channelWidth = GetVhtChanWidth(channelType, centerFrequencyIndex1, centerFrequencyIndex2);
1174     if ((unsigned int)pcmd->channelWidth == UNSPECIFIED) {
1175         return false;
1176     }
1177     pcmd->centerFrequency0 = GetVhtCentFreq(channelType, centerFrequencyIndex1);
1178     pcmd->centerFrequency1 = GetVhtCentFreq(channelType, centerFrequencyIndex2);
1179     return true;
1180 }
1181 
GetChanWidthCenterFreqHe(ScanInfo * pcmd,ScanInfoElem * infoElem)1182 static bool GetChanWidthCenterFreqHe(ScanInfo *pcmd, ScanInfoElem* infoElem)
1183 {
1184     if ((pcmd == NULL) || (infoElem == NULL)) {
1185         HDF_LOGE("pcmd or iesNeedParse is NULL.");
1186         return false;
1187     }
1188     if ((infoElem->content == NULL) || ((unsigned int)infoElem->size < (HE_OPER_BASIC_LEN + 1))) {
1189         return false;
1190     }
1191     if (infoElem->content[0] != EXT_HE_OPER_EID) {
1192         return false;
1193     }
1194     char* content = infoElem->content + 1;
1195     bool isVhtInfoExist = (content[COLUMN_INDEX_ONE] & VHT_OPER_INFO_EXTST_MASK) != 0;
1196     bool is6GhzInfoExist = (content[COLUMN_INDEX_TWO] & GHZ_HE_INFO_EXIST_MASK_6) != 0;
1197     bool coHostedBssPresent = (content[COLUMN_INDEX_ONE] & BSS_EXIST_MASK) != 0;
1198     int expectedLen = HE_OPER_BASIC_LEN + (isVhtInfoExist ? COLUMN_INDEX_THREE : 0) +
1199         (coHostedBssPresent ? 1 : 0) + (is6GhzInfoExist ? COLUMN_INDEX_FIVE : 0);
1200     pcmd->isHeInfoExist = 1;
1201     if (infoElem->size < expectedLen) {
1202         return false;
1203     }
1204     if (is6GhzInfoExist) {
1205         int startIndx = VHT_OPER_INFO_BEGIN_INDEX + (isVhtInfoExist ? COLUMN_INDEX_THREE : 0) +
1206             (coHostedBssPresent ? 1 : 0);
1207         int heChannelWidth = content[startIndx + 1] & GHZ_HE_WIDTH_MASK_6;
1208         int centerSegFreq0 = content[startIndx + COLUMN_INDEX_TWO] & UINT8_MASK;
1209         int centerSegFreq1 = content[startIndx + COLUMN_INDEX_THREE] & UINT8_MASK;
1210         pcmd->channelWidth = GetHeChanWidth(heChannelWidth, centerSegFreq0, centerSegFreq1);
1211         pcmd->centerFrequency0 = GetHeCentFreq(centerSegFreq0);
1212         pcmd->centerFrequency1 = GetHeCentFreq(centerSegFreq1);
1213         return true;
1214     }
1215     if (isVhtInfoExist) {
1216         struct ScanInfoElem vhtInformation = {0};
1217         vhtInformation.id = VHT_OPER_EID;
1218         vhtInformation.size = VHT_INFO_SIZE;
1219         vhtInformation.content = content + VHT_OPER_INFO_BEGIN_INDEX;
1220         return GetChanWidthCenterFreqVht(pcmd, &vhtInformation);
1221     }
1222     return false;
1223 }
1224 
GetChanWidthCenterFreqHt(ScanInfo * pcmd,ScanInfoElem * infoElem)1225 static bool GetChanWidthCenterFreqHt(ScanInfo *pcmd, ScanInfoElem* infoElem)
1226 {
1227     const int offsetBit = 0x3;
1228     if ((pcmd == NULL) || (infoElem == NULL)) {
1229         HDF_LOGE("pcmd or infoElem is NULL.");
1230         return false;
1231     }
1232     if ((infoElem->content == NULL) || ((unsigned int)infoElem->size < HT_INFO_SIZE)) {
1233         return false;
1234     }
1235     int secondOffsetChannel = infoElem->content[1] & offsetBit;
1236     pcmd->channelWidth = GetHtChanWidth(secondOffsetChannel);
1237     pcmd->centerFrequency0 = GetHtCentFreq0(pcmd->freq, secondOffsetChannel);
1238     pcmd->isHtInfoExist = 1;
1239     return true;
1240 }
1241 
GetChanMaxRates(ScanInfo * pcmd,ScanInfoElem * infoElem)1242 static bool GetChanMaxRates(ScanInfo *pcmd, ScanInfoElem* infoElem)
1243 {
1244     if ((pcmd == NULL) || (infoElem == NULL)) {
1245         HDF_LOGE("pcmd or infoElem is NULL.");
1246         return false;
1247     }
1248     if ((infoElem->content == NULL) || ((unsigned int)infoElem->size < SUPP_RATES_SIZE)) {
1249         return false;
1250     }
1251     int maxIndex = infoElem->size - 1;
1252     int maxRates = infoElem->content[maxIndex] & UINT8_MASK;
1253     pcmd->maxRates = maxRates;
1254     return true;
1255 }
1256 
GetChanExtMaxRates(ScanInfo * pcmd,ScanInfoElem * infoElem)1257 static bool GetChanExtMaxRates(ScanInfo *pcmd, ScanInfoElem* infoElem)
1258 {
1259     if ((pcmd == NULL) || (infoElem == NULL)) {
1260         HDF_LOGE("pcmd or infoElem is NULL.");
1261         return false;
1262     }
1263     if ((infoElem->content == NULL) || ((unsigned int)infoElem->size < EXT_SUPP_RATES_SIZE)) {
1264         return false;
1265     }
1266     int maxIndex = infoElem->size - 1;
1267     int maxRates = infoElem->content[maxIndex] & UINT8_MASK;
1268     pcmd->extMaxRates = maxRates;
1269     return true;
1270 }
1271 
GetChanWidthCenterFreq(ScanInfo * pcmd,struct NeedParseIe * iesNeedParse)1272 static void GetChanWidthCenterFreq(ScanInfo *pcmd, struct NeedParseIe* iesNeedParse)
1273 {
1274     if ((pcmd == NULL) || (iesNeedParse == NULL)) {
1275         HDF_LOGE("pcmd or iesNeedParse is NULL.");
1276         return;
1277     }
1278 
1279     if ((iesNeedParse->ieExtern != NULL) && GetChanWidthCenterFreqHe(pcmd, iesNeedParse->ieExtern)) {
1280         return;
1281     }
1282     if ((iesNeedParse->ieVhtOper != NULL) && GetChanWidthCenterFreqVht(pcmd, iesNeedParse->ieVhtOper)) {
1283         return;
1284     }
1285     if ((iesNeedParse->ieHtOper != NULL) && GetChanWidthCenterFreqHt(pcmd, iesNeedParse->ieHtOper)) {
1286         return;
1287     }
1288     if ((iesNeedParse->ieMaxRate != NULL) && GetChanMaxRates(pcmd, iesNeedParse->ieMaxRate)) {
1289         HDF_LOGE("pcmd maxRates is %{public}d.", pcmd->maxRates);
1290         return;
1291     }
1292     if ((iesNeedParse->ieExtMaxRate != NULL) && GetChanExtMaxRates(pcmd, iesNeedParse->ieExtMaxRate)) {
1293         HDF_LOGE("pcmd extMaxRates is %{public}d.", pcmd->extMaxRates);
1294         return;
1295     }
1296     if (iesNeedParse->ieErp != NULL) {
1297         HDF_LOGE("pcmd isErpExist is true.");
1298         pcmd->isErpExist = 1;
1299         return;
1300     }
1301     HDF_LOGE("GetChanWidthCenterFreq fail.");
1302     return;
1303 }
1304 
RecordIeNeedParse(unsigned int id,ScanInfoElem * ie,struct NeedParseIe * iesNeedParse)1305 static void RecordIeNeedParse(unsigned int id, ScanInfoElem* ie, struct NeedParseIe* iesNeedParse)
1306 {
1307     if (iesNeedParse == NULL) {
1308         return;
1309     }
1310     switch (id) {
1311         case EXT_EXIST_EID:
1312             iesNeedParse->ieExtern = ie;
1313             break;
1314         case VHT_OPER_EID:
1315             iesNeedParse->ieVhtOper = ie;
1316             break;
1317         case HT_OPER_EID:
1318             iesNeedParse->ieHtOper = ie;
1319             break;
1320         case SUPPORTED_RATES_EID:
1321             iesNeedParse->ieMaxRate = ie;
1322             break;
1323         case ERP_EID:
1324             iesNeedParse->ieErp = ie;
1325             break;
1326         case EXT_SUPPORTED_RATES_EID:
1327             iesNeedParse->ieExtMaxRate = ie;
1328             break;
1329         default:
1330             break;
1331     }
1332 }
1333 
GetInfoElems(int length,int end,char * srcBuf,ScanInfo * pcmd)1334 static void GetInfoElems(int length, int end, char *srcBuf, ScanInfo *pcmd)
1335 {
1336     int len;
1337     int start = end + 1;
1338     int last = end + 1;
1339     int lenValue = 2;
1340     int lastLength = 3;
1341     int remainingLength = length - start;
1342     int infoElemsSize = 0;
1343     struct NeedParseIe iesNeedParse = {NULL};
1344     ScanInfoElem* infoElemsTemp = (ScanInfoElem *)calloc(MAX_INFO_ELEMS_SIZE, sizeof(ScanInfoElem));
1345     if (infoElemsTemp == NULL) {
1346         return;
1347     }
1348     while (remainingLength > 1 && start < length) {
1349         if (srcBuf[start] == '[') {
1350             ++start;
1351             infoElemsTemp[infoElemsSize].id = atoi(srcBuf + start);
1352         }
1353         if (srcBuf[start] != ' ') {
1354             ++start;
1355         }
1356         if (srcBuf[last] != ']') {
1357             ++last;
1358             continue;
1359         }
1360         len = last - start - 1;
1361         infoElemsTemp[infoElemsSize].size = len / lenValue;
1362         infoElemsTemp[infoElemsSize].content = (char *)calloc(len / lenValue + 1, sizeof(char));
1363         if (infoElemsTemp[infoElemsSize].content == NULL) {
1364             break;
1365         }
1366         ++start;
1367         srcBuf[last] = '\0';
1368         HexStringToString(srcBuf + start, infoElemsTemp[infoElemsSize].content);
1369         if ((length - last) > lastLength) { // make sure there is no useless character
1370             last = last + 1;
1371         }
1372         start = last;
1373         remainingLength = length - last;
1374         RecordIeNeedParse(infoElemsTemp[infoElemsSize].id, &infoElemsTemp[infoElemsSize], &iesNeedParse);
1375         ++infoElemsSize;
1376     }
1377     GetChanWidthCenterFreq(pcmd, &iesNeedParse);
1378 
1379     // clear old infoElems first
1380     if (pcmd->infoElems != NULL) {
1381         for (int i = 0; i < pcmd->ieSize; i++) {
1382             if (pcmd->infoElems[i].content != NULL) {
1383                 free(pcmd->infoElems[i].content);
1384                 pcmd->infoElems[i].content = NULL;
1385             }
1386         }
1387         free(pcmd->infoElems);
1388         pcmd->infoElems = NULL;
1389     }
1390     pcmd->infoElems = infoElemsTemp;
1391     pcmd->ieSize = infoElemsSize;
1392     return;
1393 }
1394 
DelScanInfoLine(ScanInfo * pcmd,char * srcBuf,int length)1395 int DelScanInfoLine(ScanInfo *pcmd, char *srcBuf, int length)
1396 {
1397     int columnIndex = 0;
1398     int start = 0;
1399     int end = 0;
1400     int fail = 0;
1401     while (end < length) {
1402         if (srcBuf[end] != '\t') {
1403             ++end;
1404             continue;
1405         }
1406         srcBuf[end] = '\0';
1407         if (columnIndex == COLUMN_INDEX_ZERO) {
1408             if (strcpy_s(pcmd->bssid, sizeof(pcmd->bssid), srcBuf + start) != EOK) {
1409                 fail = 1;
1410                 break;
1411             }
1412         } else if (columnIndex == COLUMN_INDEX_ONE) {
1413             pcmd->freq = atoi(srcBuf + start);
1414         } else if (columnIndex == COLUMN_INDEX_TWO) {
1415             pcmd->siglv = atoi(srcBuf + start);
1416         } else if (columnIndex == COLUMN_INDEX_THREE) {
1417             if (strcpy_s(pcmd->flags, sizeof(pcmd->flags), srcBuf + start) != EOK) {
1418                 fail = 1;
1419                 break;
1420             }
1421         } else if (columnIndex == COLUMN_INDEX_FOUR) {
1422             if (strcpy_s(pcmd->ssid, sizeof(pcmd->ssid), srcBuf + start) != EOK) {
1423                 fail = 1;
1424                 break;
1425             }
1426             printf_decode((u8 *)pcmd->ssid, sizeof(pcmd->ssid), pcmd->ssid);
1427             GetInfoElems(length, end, srcBuf, pcmd);
1428             start = length;
1429             break;
1430         }
1431         ++columnIndex;
1432         ++end;
1433         start = end;
1434     }
1435     if (fail == 0 && start < length) {
1436         if (strcpy_s(pcmd->flags, sizeof(pcmd->flags), srcBuf + start) != EOK) {
1437             fail = 1;
1438         }
1439     }
1440     return fail;
1441 }
1442 
WpaCliCmdScanInfo(WifiWpaStaInterface * this,unsigned char * resultBuf,unsigned int * resultBufLen)1443 static int WpaCliCmdScanInfo(WifiWpaStaInterface *this, unsigned char *resultBuf,
1444     unsigned int *resultBufLen)
1445 {
1446     HDF_LOGI("enter WpaCliCmdScanInfo2");
1447     if (this == NULL || resultBuf == NULL || resultBufLen == NULL) {
1448         return -1;
1449     }
1450     char cmd[CMD_BUFFER_SIZE] = {0};
1451     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s SCAN_RESULTS", this->ifname) < 0) {
1452         HDF_LOGE("snprintf err");
1453         return -1;
1454     }
1455     if (WpaCliCmd(cmd, (char*)resultBuf, REPLY_BUF_LENGTH) != 0) {
1456         HDF_LOGE("WpaCliCmd SCAN_RESULTS fail");
1457         return -1;
1458     }
1459     *resultBufLen = strlen((char*)resultBuf);
1460     HDF_LOGI("WpaCliCmdScanInfo2, resultBufLen = %{public}d", *resultBufLen);
1461     return 0;
1462 }
1463 
WpaCliCmdGetSignalInfo(WifiWpaStaInterface * this,WpaSignalInfo * info)1464 static int WpaCliCmdGetSignalInfo(WifiWpaStaInterface *this, WpaSignalInfo *info)
1465 {
1466     if (this == NULL || info == NULL) {
1467         return -1;
1468     }
1469     char cmd[CMD_BUFFER_SIZE] = {0};
1470     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s SIGNAL_POLL", this->ifname) < 0) {
1471         HDF_LOGE("snprintf err");
1472         return -1;
1473     }
1474     char *buf = (char *)calloc(REPLY_BUF_LENGTH, sizeof(char));
1475     if (buf == NULL) {
1476         return -1;
1477     }
1478     if (WpaCliCmd(cmd, buf, REPLY_BUF_LENGTH) != 0) {
1479         free(buf);
1480         return -1;
1481     }
1482     char *savedPtr = NULL;
1483     char *token = strtok_r(buf, "=", &savedPtr);
1484     while (token != NULL) {
1485         if (strcmp(token, "RSSI") == 0) {
1486             token = strtok_r(NULL, "\n", &savedPtr);
1487             info->signal = atoi(token);
1488         } else if (strcmp(token, "LINKSPEED") == 0) {
1489             token = strtok_r(NULL, "\n", &savedPtr);
1490             info->txrate = atoi(token);
1491         } else if (strcmp(token, "NOISE") == 0) {
1492             token = strtok_r(NULL, "\n", &savedPtr);
1493             info->noise = atoi(token);
1494         } else if (strcmp(token, "FREQUENCY") == 0) {
1495             token = strtok_r(NULL, "\n", &savedPtr);
1496             info->frequency = atoi(token);
1497         } else {
1498             strtok_r(NULL, "\n", &savedPtr);
1499         }
1500         token = strtok_r(NULL, "=", &savedPtr);
1501     }
1502     free(buf);
1503     return 0;
1504 }
1505 
1506 /* mode: 0 - enabled, 1 - disabled. */
WpaCliCmdWpaSetPowerMode(WifiWpaStaInterface * this,bool mode)1507 static int WpaCliCmdWpaSetPowerMode(WifiWpaStaInterface *this, bool mode)
1508 {
1509     HDF_LOGI("Enter WpaCliCmdWpaSetPowerMode, mode:%{public}d.", mode);
1510     if (this == NULL) {
1511         HDF_LOGE("WpaCliCmdWpaSetPowerMode, this is NULL.");
1512         return -1;
1513     }
1514     char cmd[CMD_BUFFER_SIZE] = {0};
1515     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
1516     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s DRIVER POWERMODE %d",
1517         this->ifname, mode) < 0) {
1518         HDF_LOGE("WpaCliCmdWpaSetPowerMode, snprintf_s err");
1519         return -1;
1520     }
1521     return WpaCliCmd(cmd, buf, sizeof(buf));
1522 }
1523 
WpaCliCmdWpaSetSuspendMode(WifiWpaStaInterface * this,bool mode)1524 static int WpaCliCmdWpaSetSuspendMode(WifiWpaStaInterface *this, bool mode)
1525 {
1526     HDF_LOGI("Enter WpaCliCmdWpaSetSuspendMode, mode:%{public}d.", mode);
1527     if (this == NULL) {
1528         HDF_LOGE("WpaCliCmdWpaSetSuspendMode, this is NULL.");
1529         return -1;
1530     }
1531     char cmd[CMD_BUFFER_SIZE] = {0};
1532     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
1533     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s DRIVER SETSUSPENDMODE %d",
1534         this->ifname, mode) < 0) {
1535         HDF_LOGE("WpaCliCmdWpaSetSuspendMode, snprintf_s err");
1536         return -1;
1537     }
1538     return WpaCliCmd(cmd, buf, sizeof(buf));
1539 }
1540 
WpaCliCmdStaShellCmd(WifiWpaStaInterface * this,const char * params)1541 static int WpaCliCmdStaShellCmd(WifiWpaStaInterface *this, const char *params)
1542 {
1543     if (this == NULL || params == NULL) {
1544         return -1;
1545     }
1546     char cmd[CMD_BUFFER_SIZE] = {0};
1547     char buf[REPLY_BUF_SMALL_LENGTH] = {0};
1548     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s STA_SHELL %s",
1549         this->ifname, params) < 0) {
1550         HDF_LOGE("WpaCliCmdStaShellCmd, snprintf_s err");
1551         return -1;
1552     }
1553     return WpaCliCmd(cmd, buf, sizeof(buf));
1554 }
1555 
WpaCliCmdGetWpaStaData(WifiWpaStaInterface * this,const char * argv,char * staData,unsigned int size)1556 static int WpaCliCmdGetWpaStaData(WifiWpaStaInterface *this, const char *argv, char *staData, unsigned int size)
1557 {
1558     if (this == NULL || argv == NULL || staData == NULL) {
1559         HDF_LOGE("WpaCliCmdGetWpaStaData, interface null ir wpaMloInfo null");
1560         return -1;
1561     }
1562     char cmd[CMD_BUFFER_SIZE] = {0};
1563     char buf[REPLY_BUF_STA_INFO_LENGTH] = {0};
1564     if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, "IFNAME=%s %s", this->ifname, argv) < 0) {
1565         HDF_LOGE("WpaCliCmdGetWpaStaData, snprintf_s err");
1566         return -1;
1567     }
1568     if (WpaCliCmd(cmd, buf, sizeof(buf)) != 0) {
1569         HDF_LOGE("WpaCliCmdGetWpaStaData, err");
1570         return -1;
1571     }
1572     if (strncpy_s(staData, size, buf, sizeof(buf)) != EOK) {
1573         HDF_LOGE("WpaCliCmdGetWpaStaData, copy res err");
1574         return -1;
1575     }
1576     return 0;
1577 }
1578 
GetWifiStaInterface(const char * name)1579 WifiWpaStaInterface *GetWifiStaInterface(const char *name)
1580 {
1581     WifiWpaStaInterface *p = g_wpaStaInterface;
1582     char staNo[STA_NO_LEN + 1] = {0};
1583     while (p != NULL) {
1584         if (strcmp(p->ifname, name) == 0) {
1585             return p;
1586         }
1587         p = p->next;
1588     }
1589     p = (WifiWpaStaInterface *)calloc(1, sizeof(WifiWpaStaInterface));
1590     if (p == NULL) {
1591         return NULL;
1592     }
1593     if (strncpy_s(p->ifname, sizeof(p->ifname), name, sizeof(p->ifname)) != EOK) {
1594         HDF_LOGE("GetWifiStaInterface, strncpy ifname err");
1595         free(p);
1596         p = NULL;
1597         return NULL;
1598     }
1599     if (strncpy_s(staNo, sizeof(staNo), name + strlen("wlan"), STA_NO_LEN) != EOK) {
1600         HDF_LOGE("GetWifiStaInterface, strncpy stano err");
1601         free(p);
1602         p = NULL;
1603         return NULL;
1604     }
1605     p->staNo =  atoi(staNo);
1606     p->wpaCliCmdStatus = WpaCliCmdStatus;
1607     p->wpaCliCmdAddNetworks = WpaCliCmdAddNetworks;
1608     p->wpaCliCmdReconnect = WpaCliCmdReconnect;
1609     p->wpaCliCmdReassociate = WpaCliCmdReassociate;
1610     p->wpaCliCmdDisconnect = WpaCliCmdDisconnect;
1611     p->wpaCliCmdSaveConfig = WpaCliCmdSaveConfig;
1612     p->wpaCliCmdSetNetwork = WpaCliCmdSetNetwork;
1613     p->wpaCliCmdEnableNetwork = WpaCliCmdEnableNetwork;
1614     p->wpaCliCmdSelectNetwork = WpaCliCmdSelectNetwork;
1615     p->wpaCliCmdDisableNetwork = WpaCliCmdDisableNetwork;
1616     p->wpaCliCmdRemoveNetwork = WpaCliCmdRemoveNetwork;
1617     p->wpaCliCmdGetNetwork = WpaCliCmdGetNetwork;
1618     p->wpaCliCmdWpsPbc = WpaCliCmdWpsPbc;
1619     p->wpaCliCmdWpsPin = WpaCliCmdWpsPin;
1620     p->wpaCliCmdWpsCancel = WpaCliCmdWpsCancel;
1621     p->wpaCliCmdPowerSave = WpaCliCmdPowerSave;
1622     p->wpaCliCmdSetRoamConfig = WpaCliCmdSetRoamConfig;
1623     p->wpaCliCmdSetCountryCode = WpaCliCmdSetCountryCode;
1624     p->wpaCliCmdGetCountryCode = WpaCliCmdGetCountryCode;
1625     p->wpaCliCmdSetAutoConnect = WpaCliCmdSetAutoConnect;
1626     p->wpaCliCmdWpaBlockListClear = WpaCliCmdWpaBlockListClear;
1627     p->wpaCliCmdListNetworks = WpaCliCmdListNetworks;
1628     p->wpaCliCmdScan = WpaCliCmdScan;
1629     p->wpaCliCmdScanInfo = WpaCliCmdScanInfo;
1630     p->wpaCliCmdGetSignalInfo = WpaCliCmdGetSignalInfo;
1631     p->wpaCliCmdWpaSetSuspendMode = WpaCliCmdWpaSetSuspendMode;
1632     p->wpaCliCmdWpaSetPowerMode = WpaCliCmdWpaSetPowerMode;
1633     p->wpaCliCmdGetScanSsid = WpaCliCmdGetScanSsid;
1634     p->wpaCliCmdGetPskPassphrase = WpaCliCmdGetPskPassphrase;
1635     p->wpaCliCmdGetPsk = WpaCliCmdGetPsk;
1636     p->wpaCliCmdWepKey = WpaCliCmdWepKey;
1637     p->wpaCliCmdWepKeyTxKeyIdx = WpaCliCmdWepKeyTxKeyIdx;
1638     p->wpaCliCmdGetRequirePmf = WpaCliCmdGetRequirePmf;
1639     p->wpaCliCmdGetConnectionCapabilities = WpaCliCmdGetConnectionCapabilities;
1640     p->wpaCliCmdStaShellCmd = WpaCliCmdStaShellCmd;
1641     p->wpaCliCmdGetWpaStaData = WpaCliCmdGetWpaStaData;
1642     p->next = g_wpaStaInterface;
1643     g_wpaStaInterface = p;
1644 
1645     return p;
1646 }
1647 
ReleaseWifiStaInterface(int staNo)1648 void ReleaseWifiStaInterface(int staNo)
1649 {
1650     char name[MAX_NAME_LEN] = {0};
1651     if (snprintf_s(name, sizeof(name), sizeof(name) - 1, "wlan%d", staNo) < 0) {
1652         HDF_LOGE("snprintf error");
1653         return;
1654     }
1655     WifiWpaStaInterface *p = g_wpaStaInterface;
1656     WifiWpaStaInterface *prev = NULL;
1657     while (p != NULL) {
1658         if (strcmp(p->ifname, name) == 0) {
1659             break;
1660         }
1661         prev = p;
1662         p = p->next;
1663     }
1664     if (p == NULL) {
1665         return;
1666     }
1667     if (prev == NULL) {
1668         g_wpaStaInterface = p->next;
1669     } else {
1670         prev->next = p->next;
1671     }
1672     free(p);
1673     return;
1674 }
1675 
TraversalWifiStaInterface(void)1676 WifiWpaStaInterface *TraversalWifiStaInterface(void)
1677 {
1678     return g_wpaStaInterface;
1679 }
1680 
GetStaInterfaceNo(const char * ifName)1681 int GetStaInterfaceNo(const char *ifName)
1682 {
1683     WifiWpaStaInterface *p = g_wpaStaInterface;
1684     while (p != NULL) {
1685         if (strcmp(p->ifname, ifName) == 0) {
1686             return p->staNo;
1687         }
1688         p = p->next;
1689     }
1690     return -1;
1691 }
1692