1 /*
2 * Copyright (C) 2021-2022 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 "wifi_hal_sta_interface.h"
17 #include "securec.h"
18 #include "wifi_common_def.h"
19 #include "wifi_hal_adapter.h"
20 #include "wifi_hal_module_manage.h"
21 #include "wifi_log.h"
22 #include "wifi_supplicant_hal.h"
23 #include "wifi_wpa_hal.h"
24
25 #undef LOG_TAG
26 #define LOG_TAG "WifiHalStaInterface"
27
28 #ifdef NON_SEPERATE_P2P
29 #ifdef WPA_CTRL_IFACE_UNIX
30 #define START_CMD "wpa_supplicant -c"CONFIG_ROOR_DIR"/wpa_supplicant/wpa_supplicant.conf"\
31 " -g@abstract:"CONFIG_ROOR_DIR"/sockets/wpa/wlan0"
32 #else
33 #define START_CMD "wpa_supplicant -iglan0 -g"CONFIG_ROOR_DIR"/sockets/wpa"\
34 " -m"CONFIG_ROOR_DIR"/wpa_supplicant/p2p_supplicant.conf"
35 #endif // WPA_CTRL_IFACE_UNIX
36
37 #else // NON_SEPERATE_P2P
38 #ifdef WPA_CTRL_IFACE_UNIX
39 #define START_CMD "wpa_supplicant -c"CONFIG_ROOR_DIR"/wpa_supplicant/wpa_supplicant.conf"\
40 " -g@abstract:"CONFIG_ROOR_DIR"/sockets/wpa/wlan0"
41 #else
42 #define START_CMD "wpa_supplicant -iglan0 -g"CONFIG_ROOR_DIR"/sockets/wpa"
43 #endif // WPA_CTRL_IFACE_UNIX
44 #endif // NON_SEPERATE_P2P
45
AddWpaIface(int staNo)46 static WifiErrorNo AddWpaIface(int staNo)
47 {
48 WifiWpaInterface *pWpaInterface = GetWifiWapGlobalInterface();
49 if (pWpaInterface == NULL) {
50 LOGE("Get wpa interface failed!");
51 return WIFI_HAL_FAILED;
52 }
53 if (pWpaInterface->wpaCliConnect(pWpaInterface) < 0) {
54 LOGE("Failed to connect to wpa!");
55 return WIFI_HAL_FAILED;
56 }
57 AddInterfaceArgv argv;
58 if (staNo == 0) {
59 if (strcpy_s(argv.name, sizeof(argv.name), "wlan0") != EOK || strcpy_s(argv.confName, sizeof(argv.confName),
60 CONFIG_ROOR_DIR"/wpa_supplicant/wpa_supplicant.conf") != EOK) {
61 return WIFI_HAL_FAILED;
62 }
63 } else {
64 if (strcpy_s(argv.name, sizeof(argv.name), "wlan2") != EOK || strcpy_s(argv.confName, sizeof(argv.confName),
65 CONFIG_ROOR_DIR"/wpa_supplicant/wpa_supplicant.conf") != EOK) {
66 return WIFI_HAL_FAILED;
67 }
68 }
69 if (pWpaInterface->wpaCliAddIface(pWpaInterface, &argv, true) < 0) {
70 LOGE("Failed to add wpa iface!");
71 return WIFI_HAL_FAILED;
72 }
73 return WIFI_HAL_SUCCESS;
74 }
75
RemoveWpaIface(int staNo)76 static WifiErrorNo RemoveWpaIface(int staNo)
77 {
78 int ret = -1;
79 WifiWpaInterface *pWpaInterface = GetWifiWapGlobalInterface();
80 if (pWpaInterface == NULL) {
81 LOGE("Get wpa interface failed!");
82 return WIFI_HAL_FAILED;
83 }
84 if (staNo == 0) {
85 ret = pWpaInterface->wpaCliRemoveIface(pWpaInterface, "wlan0");
86 } else {
87 ret = pWpaInterface->wpaCliRemoveIface(pWpaInterface, "wlan2");
88 }
89 return (ret == 0 ? WIFI_HAL_SUCCESS : WIFI_HAL_FAILED);
90 }
91
StopWpaAndWpaHal(int staNo)92 static WifiErrorNo StopWpaAndWpaHal(int staNo)
93 {
94 if (DisconnectSupplicant() != WIFI_HAL_SUCCESS) {
95 LOGE("wpa_s hal already stop!");
96 }
97
98 if (RemoveWpaIface(staNo) != WIFI_HAL_SUCCESS) {
99 LOGE("RemoveWpaIface return fail!");
100 }
101
102 if (StopSupplicant() != WIFI_HAL_SUCCESS) {
103 LOGE("wpa_supplicant stop failed!");
104 return WIFI_HAL_FAILED;
105 }
106 ReleaseWifiStaInterface(staNo);
107 LOGI("wpa_supplicant stop successfully");
108 return WIFI_HAL_SUCCESS;
109 }
110
Start(void)111 WifiErrorNo Start(void)
112 {
113 LOGI("Ready to start wifi");
114 if (StartSupplicant() != WIFI_HAL_SUCCESS) {
115 LOGE("wpa_supplicant start failed!");
116 return WIFI_HAL_OPEN_SUPPLICANT_FAILED;
117 }
118 LOGI("wpa_supplicant start successfully!");
119
120 if (AddWpaIface(0) != WIFI_HAL_SUCCESS) {
121 LOGE("Failed to add wpa interface!");
122 StopWpaAndWpaHal(0);
123 return WIFI_HAL_CONN_SUPPLICANT_FAILED;
124 }
125
126 if (ConnectSupplicant() != WIFI_HAL_SUCCESS) {
127 LOGE("SupplicantHal connect wpa_supplicant failed!");
128 StopWpaAndWpaHal(0);
129 return WIFI_HAL_CONN_SUPPLICANT_FAILED;
130 }
131 LOGI("Start wifi successfully");
132 return WIFI_HAL_SUCCESS;
133 }
134
Stop(void)135 WifiErrorNo Stop(void)
136 {
137 LOGI("Ready to Stop wifi");
138 WifiErrorNo err = StopWpaAndWpaHal(0);
139 if (err == WIFI_HAL_FAILED) {
140 LOGE("Wifi stop failed!");
141 return WIFI_HAL_FAILED;
142 }
143 LOGI("Wifi stop successfully!");
144 return WIFI_HAL_SUCCESS;
145 }
146
ForceStop(void)147 WifiErrorNo ForceStop(void)
148 {
149 LOGI("Ready force Stop wifi");
150 WifiWpaStaInterface *p = TraversalWifiStaInterface();
151 while (p != NULL) {
152 StopWpaAndWpaHal(p->staNo);
153 p = TraversalWifiStaInterface();
154 }
155 return WIFI_HAL_SUCCESS;
156 }
157
StartSupplicant(void)158 WifiErrorNo StartSupplicant(void)
159 {
160 LOGI("Start supplicant");
161 if (CopyConfigFile("wpa_supplicant.conf") != 0) {
162 return WIFI_HAL_FAILED;
163 }
164 #ifdef NON_SEPERATE_P2P
165 if (CopyConfigFile("p2p_supplicant.conf") != 0) {
166 return WIFI_HAL_FAILED;
167 }
168 #endif
169 ModuleManageRetCode ret = StartModule(WPA_SUPPLICANT_NAME, START_CMD);
170 if (ret != MM_SUCCESS) {
171 LOGE("start wpa_supplicant failed!");
172 return WIFI_HAL_FAILED;
173 }
174 return WIFI_HAL_SUCCESS;
175 }
176
StopSupplicant(void)177 WifiErrorNo StopSupplicant(void)
178 {
179 LOGI("Stop supplicant");
180 ModuleManageRetCode ret = StopModule(WPA_SUPPLICANT_NAME, false);
181 if (ret == MM_FAILED) {
182 LOGE("stop wpa_supplicant failed!");
183 return WIFI_HAL_FAILED;
184 }
185
186 if (ret == MM_SUCCESS) {
187 ReleaseWpaGlobalInterface();
188 }
189 return WIFI_HAL_SUCCESS;
190 }
191
ConnectSupplicant(void)192 WifiErrorNo ConnectSupplicant(void)
193 {
194 LOGD("Ready to connect wpa_supplicant.");
195 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
196 if (pStaIfc == NULL) {
197 return WIFI_HAL_SUPPLICANT_NOT_INIT;
198 }
199 return WIFI_HAL_SUCCESS;
200 }
201
DisconnectSupplicant(void)202 WifiErrorNo DisconnectSupplicant(void)
203 {
204 LOGD("Ready to disconnect wpa_supplicant.");
205 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
206 if (pStaIfc == NULL) {
207 return WIFI_HAL_SUPPLICANT_NOT_INIT;
208 }
209 LOGD("Disconnect wpa_supplicant finish!");
210 return WIFI_HAL_SUCCESS;
211 }
212
RequestToSupplicant(const unsigned char * buf,int32_t bufSize)213 WifiErrorNo RequestToSupplicant(const unsigned char *buf, int32_t bufSize)
214 {
215 if (buf == NULL) {
216 LOGE("RequestToSupplicant buf id NULL");
217 return WIFI_HAL_FAILED;
218 }
219 LOGD("RequestToSupplicant:buf:%{private}s, buf_size:%{public}d", buf, bufSize);
220 return WIFI_HAL_SUCCESS;
221 }
222
StartScan(const ScanSettings * settings)223 WifiErrorNo StartScan(const ScanSettings *settings)
224 {
225 LOGI("Ready to start scan with param.");
226 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
227 if (pStaIfc == NULL) {
228 return WIFI_HAL_SUPPLICANT_NOT_INIT;
229 }
230 int ret = pStaIfc->wpaCliCmdScan(pStaIfc, settings);
231 if (ret < 0) {
232 LOGE("StartScan failed! ret=%{public}d", ret);
233 return WIFI_HAL_FAILED;
234 }
235 if (ret == WIFI_HAL_SCAN_BUSY) {
236 LOGD("StartScan return scan busy");
237 return WIFI_HAL_SCAN_BUSY;
238 }
239 LOGI("StartScan successfully!");
240 return WIFI_HAL_SUCCESS;
241 }
242
GetScanInfos(ScanInfo * results,int * size)243 WifiErrorNo GetScanInfos(ScanInfo *results, int *size)
244 {
245 LOGI("Ready to get scan result.");
246 if (results == NULL || size == NULL || *size == 0) {
247 return WIFI_HAL_SUCCESS;
248 }
249 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
250 if (pStaIfc == NULL) {
251 return WIFI_HAL_SUPPLICANT_NOT_INIT;
252 }
253 int ret = pStaIfc->wpaCliCmdScanInfo(pStaIfc, results, size);
254 if (ret < 0) {
255 LOGE("GetScanInfos failed! ret=%{public}d", ret);
256 return WIFI_HAL_FAILED;
257 }
258 LOGI("Get scan result successfully!");
259 return WIFI_HAL_SUCCESS;
260 }
261
GetNetworkList(WifiNetworkInfo * infos,int * size)262 WifiErrorNo GetNetworkList(WifiNetworkInfo *infos, int *size)
263 {
264 LOGI("GetNetworkList()");
265 if (infos == NULL || size == NULL || *size == 0) {
266 return WIFI_HAL_FAILED;
267 }
268 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
269 if (pStaIfc == NULL) {
270 return WIFI_HAL_SUPPLICANT_NOT_INIT;
271 }
272 int ret = pStaIfc->wpaCliCmdListNetworks(pStaIfc, infos, size);
273 if (ret < 0) {
274 LOGE("WpaCliCmdSelectNetwork failed! ret=%{public}d", ret);
275 return WIFI_HAL_FAILED;
276 }
277 return WIFI_HAL_SUCCESS;
278 }
279
StartPnoScan(const PnoScanSettings * settings)280 WifiErrorNo StartPnoScan(const PnoScanSettings *settings)
281 {
282 LOGI("Ready to start pnoscan with param.");
283 ScanSettings scanSettings;
284 scanSettings.freqs = settings->freqs;
285 scanSettings.freqSize = settings->freqSize;
286 scanSettings.hiddenSsidSize = settings->hiddenSsidSize;
287 scanSettings.hiddenSsid = settings->hiddenSsid;
288 scanSettings.savedPnoSsidSize = settings->savedSsidSize;
289 scanSettings.savedPnoSsid = settings->savedSsid;
290 scanSettings.minPnoRssi2Dot4Ghz = settings->minRssi2Dot4Ghz;
291 scanSettings.minPnoRssi5Ghz = settings->minRssi5Ghz;
292 scanSettings.pnoScanInterval = settings->scanInterval;
293 scanSettings.scanStyle = SCAN_TYPE_PNO;
294 scanSettings.isStartPnoScan = 1;
295 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
296 if (pStaIfc == NULL) {
297 return WIFI_HAL_SUPPLICANT_NOT_INIT;
298 }
299 int ret = pStaIfc->wpaCliCmdScan(pStaIfc, &scanSettings);
300 if (ret < 0) {
301 LOGE("StartPnoScan failed! ret=%{public}d", ret);
302 return WIFI_HAL_FAILED;
303 }
304 LOGI("StartPnoScan successfully!");
305 return WIFI_HAL_SUCCESS;
306 }
307
StopPnoScan(void)308 WifiErrorNo StopPnoScan(void)
309 {
310 LOGI("Ready to stop pnoscan.");
311 ScanSettings scanSettings;
312 scanSettings.scanStyle = SCAN_TYPE_PNO;
313 scanSettings.isStartPnoScan = 0;
314 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
315 if (pStaIfc == NULL) {
316 return WIFI_HAL_SUPPLICANT_NOT_INIT;
317 }
318 int ret = pStaIfc->wpaCliCmdScan(pStaIfc, &scanSettings);
319 if (ret < 0) {
320 LOGE("StopPnoScan failed! ret=%{public}d", ret);
321 return WIFI_HAL_FAILED;
322 }
323 LOGI("StopPnoScan successfully!");
324 return WIFI_HAL_SUCCESS;
325 }
326
Connect(int networkId)327 WifiErrorNo Connect(int networkId)
328 {
329 LOGI("Connect() networkid %{public}d", networkId);
330 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
331 if (pStaIfc == NULL) {
332 return WIFI_HAL_SUPPLICANT_NOT_INIT;
333 }
334 int ret = pStaIfc->wpaCliCmdSelectNetwork(pStaIfc, networkId);
335 if (ret < 0) {
336 LOGE("WpaCliCmdSelectNetwork failed! ret=%{public}d", ret);
337 return WIFI_HAL_FAILED;
338 }
339 return WIFI_HAL_SUCCESS;
340 }
341
WpaAutoConnect(int enable)342 WifiErrorNo WpaAutoConnect(int enable)
343 {
344 LOGD("WpaAutoConnect() enable= %{public}d", enable);
345 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
346 if (pStaIfc == NULL) {
347 return WIFI_HAL_SUPPLICANT_NOT_INIT;
348 }
349 int ret = pStaIfc->wpaCliCmdSetAutoConnect(pStaIfc, enable);
350 if (ret < 0) {
351 LOGE("WpaCliCmdSetAutoConnect failed! ret=%{public}d", ret);
352 return WIFI_HAL_FAILED;
353 }
354 LOGD("WpaAutoConnect set success");
355 return WIFI_HAL_SUCCESS;
356 }
357
Reconnect(void)358 WifiErrorNo Reconnect(void)
359 {
360 LOGD("Reconnect()");
361 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
362 if (pStaIfc == NULL) {
363 return WIFI_HAL_SUPPLICANT_NOT_INIT;
364 }
365 int ret = pStaIfc->wpaCliCmdReconnect(pStaIfc);
366 if (ret < 0) {
367 LOGE("WpaCliCmdReconnect failed!");
368 return WIFI_HAL_FAILED;
369 }
370 return WIFI_HAL_SUCCESS;
371 }
372
Reassociate(void)373 WifiErrorNo Reassociate(void)
374 {
375 LOGD("Reassociate()");
376 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
377 if (pStaIfc == NULL) {
378 return WIFI_HAL_SUPPLICANT_NOT_INIT;
379 }
380 int ret = pStaIfc->wpaCliCmdReassociate(pStaIfc);
381 if (ret < 0) {
382 LOGE("WpaCliCmdReassociate failed!");
383 return WIFI_HAL_FAILED;
384 }
385 return WIFI_HAL_SUCCESS;
386 }
387
Disconnect(void)388 WifiErrorNo Disconnect(void)
389 {
390 LOGD("Disconnect()");
391 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
392 if (pStaIfc == NULL) {
393 return WIFI_HAL_SUPPLICANT_NOT_INIT;
394 }
395 int ret = pStaIfc->wpaCliCmdDisconnect(pStaIfc);
396 if (ret < 0) {
397 LOGE("WpaCliCmdDisconnect failed!");
398 return WIFI_HAL_FAILED;
399 }
400 return WIFI_HAL_SUCCESS;
401 }
402
SetPowerSave(int enable)403 WifiErrorNo SetPowerSave(int enable)
404 {
405 LOGD("SetPowerSave() %{public}d", enable);
406 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
407 if (pStaIfc == NULL) {
408 return WIFI_HAL_SUPPLICANT_NOT_INIT;
409 }
410 int ret = pStaIfc->wpaCliCmdPowerSave(pStaIfc, enable);
411 if (ret < 0) {
412 LOGE("WpaCliCmdPowerSave failed!");
413 return WIFI_HAL_FAILED;
414 }
415 return WIFI_HAL_SUCCESS;
416 }
417
GetStaCapabilities(int32_t * capabilities)418 WifiErrorNo GetStaCapabilities(int32_t *capabilities)
419 {
420 if (capabilities == NULL) {
421 LOGE("input param is NULL");
422 return WIFI_HAL_FAILED;
423 }
424 WifiHalVendorInterface *pInterface = GetWifiHalVendorInterface();
425 if (pInterface == NULL) {
426 return WIFI_HAL_GET_VENDOR_HAL_FAILED;
427 }
428 long feature = 0;
429 HalVendorError err = pInterface->func.wifiGetSupportedFeature(&feature);
430 if (ConvertErrorCode(err) != WIFI_HAL_SUCCESS) {
431 return ConvertErrorCode(err);
432 }
433 /* convert supported feature to capabilities */
434 *capabilities = (int32_t)feature;
435 return WIFI_HAL_SUCCESS;
436 }
437
GetDeviceMacAddress(unsigned char * mac,int * lenMac)438 WifiErrorNo GetDeviceMacAddress(unsigned char *mac, int *lenMac)
439 {
440 /* wificond need iface name, temporary use wpa_supplicant get mac address */
441 if (mac == NULL || lenMac == NULL) {
442 LOGE("GetDeviceMacAddress mac or lenMac is NULL");
443 return WIFI_HAL_FAILED;
444 }
445 LOGD("GetDeviceMacAddress lenMac %{public}d", *lenMac);
446 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
447 if (pStaIfc == NULL) {
448 return WIFI_HAL_SUPPLICANT_NOT_INIT;
449 }
450 struct WpaHalCmdStatus status;
451 if (memset_s(&status, sizeof(status), 0, sizeof(status)) != EOK) {
452 return WIFI_HAL_FAILED;
453 }
454 int ret = pStaIfc->wpaCliCmdStatus(pStaIfc, &status);
455 if (ret < 0) {
456 LOGE("WpaCliCmdStatus failed!");
457 return WIFI_HAL_FAILED;
458 }
459 int length = strlen(status.address);
460 if (*lenMac <= length) {
461 LOGE("Input mac length %{public}d is little than mac address length %{public}d", *lenMac, length);
462 return WIFI_HAL_BUFFER_TOO_LITTLE;
463 }
464 if (strncpy_s((char *)mac, *lenMac, status.address, length) != EOK) {
465 return WIFI_HAL_FAILED;
466 }
467 *lenMac = length;
468 return WIFI_HAL_SUCCESS;
469 }
470
GetFrequencies(int32_t band,int * frequencies,int32_t * size)471 WifiErrorNo GetFrequencies(int32_t band, int *frequencies, int32_t *size)
472 {
473 if (frequencies == NULL || size == NULL) {
474 LOGE("GetFrequencies frequencies or size is NULL");
475 return WIFI_HAL_FAILED;
476 }
477 LOGD("GetFrequencies");
478 return WIFI_HAL_NOT_SUPPORT;
479 }
480
SetAssocMacAddr(const unsigned char * mac,int lenMac,const int portType)481 WifiErrorNo SetAssocMacAddr(const unsigned char *mac, int lenMac, const int portType)
482 {
483 if (mac == NULL) {
484 LOGE("SetAssocMacAddr() mac is NULL");
485 return WIFI_HAL_FAILED;
486 }
487 LOGD("SetAssocMacAddr() lenMac:%{public}d, portType:%{public}d", lenMac, portType);
488 return WIFI_HAL_NOT_SUPPORT;
489 }
490
SetScanningMacAddress(const unsigned char * mac,int lenMac)491 WifiErrorNo SetScanningMacAddress(const unsigned char *mac, int lenMac)
492 {
493 if (mac == NULL) {
494 LOGE("SetScanningMacAddress() mac is NULL");
495 return WIFI_HAL_FAILED;
496 }
497 LOGD("SetScanningMacAddress mac length: %{public}d", lenMac);
498 WifiHalVendorInterface *pInterface = GetWifiHalVendorInterface();
499 if (pInterface == NULL) {
500 return WIFI_HAL_GET_VENDOR_HAL_FAILED;
501 }
502 HalVendorError err = pInterface->func.wifiSetScanningMacAddress((const char *)mac, lenMac);
503 return ConvertErrorCode(err);
504 }
505
DeauthLastRoamingBssid(const unsigned char * mac,int lenMac)506 WifiErrorNo DeauthLastRoamingBssid(const unsigned char *mac, int lenMac)
507 {
508 if (mac == NULL) {
509 LOGE("DeauthLastRoamingBssid() mac is NULL");
510 return WIFI_HAL_FAILED;
511 }
512 LOGD("DeauthLastRoamingBssid() mac length: %{public}d", lenMac);
513 WifiHalVendorInterface *pInterface = GetWifiHalVendorInterface();
514 if (pInterface == NULL) {
515 return WIFI_HAL_GET_VENDOR_HAL_FAILED;
516 }
517 HalVendorError err = pInterface->func.wifiDeauthLastRoamingBssid((const char *)mac, lenMac);
518 return ConvertErrorCode(err);
519 }
520
GetSupportFeature(long * feature)521 WifiErrorNo GetSupportFeature(long *feature)
522 {
523 if (feature == NULL) {
524 LOGE("GetSupportFeature() feature is NULL");
525 return WIFI_HAL_FAILED;
526 }
527 LOGD("GetFeatureSupport()");
528 WifiHalVendorInterface *pInterface = GetWifiHalVendorInterface();
529 if (pInterface == NULL) {
530 return WIFI_HAL_GET_VENDOR_HAL_FAILED;
531 }
532 HalVendorError err = pInterface->func.wifiGetSupportedFeature(feature);
533 return ConvertErrorCode(err);
534 }
535
RunCmd(const char * ifname,int32_t cmdid,const unsigned char * buf,int32_t bufSize)536 WifiErrorNo RunCmd(const char *ifname, int32_t cmdid, const unsigned char *buf, int32_t bufSize)
537 {
538 if (ifname == NULL || buf == NULL) {
539 LOGE("RunCmd() ifname or buf is NULL");
540 return WIFI_HAL_FAILED;
541 }
542 LOGD("ifname: %{public}s, cmdid: %{public}d, buf: %{public}s", ifname, cmdid, buf);
543 return WIFI_HAL_SUCCESS;
544 }
545
SetWifiTxPower(int32_t power)546 WifiErrorNo SetWifiTxPower(int32_t power)
547 {
548 LOGD("SetWifiTxPower() power: %{public}d", power);
549 WifiHalVendorInterface *pInterface = GetWifiHalVendorInterface();
550 if (pInterface == NULL) {
551 return WIFI_HAL_GET_VENDOR_HAL_FAILED;
552 }
553 HalVendorError err = pInterface->func.wifiSetWifiTxPower(power);
554 return ConvertErrorCode(err);
555 }
556
RemoveNetwork(int networkId)557 WifiErrorNo RemoveNetwork(int networkId)
558 {
559 LOGD("RemoveNetwork() networkid: %{public}d", networkId);
560 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
561 if (pStaIfc == NULL) {
562 return WIFI_HAL_SUPPLICANT_NOT_INIT;
563 }
564 int ret = pStaIfc->wpaCliCmdRemoveNetwork(pStaIfc, networkId);
565 if (ret != WIFI_HAL_SUCCESS) {
566 LOGE("WpaCliCmdRemoveNetwork remove network failed! ret = %{public}d", ret);
567 return WIFI_HAL_FAILED;
568 }
569 return WIFI_HAL_SUCCESS;
570 }
571
AddNetwork(int * networkId)572 WifiErrorNo AddNetwork(int *networkId)
573 {
574 if (networkId == NULL) {
575 LOGE("AddNetwork() networkId is NULL");
576 return WIFI_HAL_FAILED;
577 }
578 LOGD("AddNetwork()");
579 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
580 if (pStaIfc == NULL) {
581 return WIFI_HAL_SUPPLICANT_NOT_INIT;
582 }
583 int ret = pStaIfc->wpaCliCmdAddNetworks(pStaIfc);
584 if (ret < 0) {
585 LOGE("WpaCliCmdAddNetworks failed! ret=%{public}d", ret);
586 return WIFI_HAL_FAILED;
587 }
588 *networkId = ret;
589 return WIFI_HAL_SUCCESS;
590 }
591
EnableNetwork(int networkId)592 WifiErrorNo EnableNetwork(int networkId)
593 {
594 LOGI("EnableNetwork() networkid [%{public}d]", networkId);
595 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
596 if (pStaIfc == NULL) {
597 return WIFI_HAL_SUPPLICANT_NOT_INIT;
598 }
599 int ret = pStaIfc->wpaCliCmdEnableNetwork(pStaIfc, networkId);
600 if (ret < 0) {
601 LOGE("WpaCliCmdEnableNetwork failed! ret=%{public}d", ret);
602 return WIFI_HAL_FAILED;
603 }
604 return WIFI_HAL_SUCCESS;
605 }
606
DisableNetwork(int networkId)607 WifiErrorNo DisableNetwork(int networkId)
608 {
609 LOGD("DisableNetwork() networkid [%{public}d]", networkId);
610 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
611 if (pStaIfc == NULL) {
612 return WIFI_HAL_SUPPLICANT_NOT_INIT;
613 }
614 int ret = pStaIfc->wpaCliCmdDisableNetwork(pStaIfc, networkId);
615 if (ret < 0) {
616 LOGE("WpaCliCmdDisableNetwork failed! ret=%{public}d", ret);
617 return WIFI_HAL_FAILED;
618 }
619 return WIFI_HAL_SUCCESS;
620 }
621
SetNetwork(int networkId,const SetNetworkConfig * confs,int size)622 WifiErrorNo SetNetwork(int networkId, const SetNetworkConfig *confs, int size)
623 {
624 if (confs == NULL) {
625 LOGE("SetNetwork() confs is NULL");
626 return WIFI_HAL_FAILED;
627 }
628 LOGD("SetNetwork()");
629 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
630 if (pStaIfc == NULL) {
631 return WIFI_HAL_SUPPLICANT_NOT_INIT;
632 }
633 struct WpaSetNetworkArgv conf;
634 for (int i = 0; i < size; ++i) {
635 if (memset_s(&conf, sizeof(conf), 0, sizeof(conf)) != EOK) {
636 return WIFI_HAL_FAILED;
637 }
638 conf.id = networkId;
639 conf.param = confs[i].cfgParam;
640 if (strncpy_s(conf.value, sizeof(conf.value), confs[i].cfgValue, strlen(confs[i].cfgValue)) != EOK) {
641 return WIFI_HAL_FAILED;
642 }
643 int ret = pStaIfc->wpaCliCmdSetNetwork(pStaIfc, &conf);
644 if (ret < 0) {
645 return WIFI_HAL_FAILED;
646 }
647 }
648 return WIFI_HAL_SUCCESS;
649 }
650
SaveNetworkConfig(void)651 WifiErrorNo SaveNetworkConfig(void)
652 {
653 LOGD("SaveNetworkConfig()");
654 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
655 if (pStaIfc == NULL) {
656 return WIFI_HAL_SUPPLICANT_NOT_INIT;
657 }
658 int ret = pStaIfc->wpaCliCmdSaveConfig(pStaIfc);
659 if (ret < 0) {
660 LOGE("WpaCliCmdSaveConfig failed! ret=%{public}d", ret);
661 return WIFI_HAL_FAILED;
662 }
663 return WIFI_HAL_SUCCESS;
664 }
665
StartWpsPbcMode(const WifiWpsParam * param)666 WifiErrorNo StartWpsPbcMode(const WifiWpsParam *param)
667 {
668 LOGD("StartWpsPbcMode()");
669 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
670 if (pStaIfc == NULL) {
671 return WIFI_HAL_SUPPLICANT_NOT_INIT;
672 }
673 int ret;
674 if (param == NULL || (param->anyFlag < 0 && param->multiAp <= 0 && strlen(param->bssid) == 0)) {
675 ret = pStaIfc->wpaCliCmdWpsPbc(pStaIfc, NULL);
676 } else {
677 struct WpaWpsPbcArgv config = {0};
678 config.anyFlag = param->anyFlag;
679 config.multiAp = param->multiAp;
680 if (strncpy_s(config.bssid, sizeof(config.bssid), param->bssid, strlen(param->bssid)) != EOK) {
681 return WIFI_HAL_FAILED;
682 }
683 ret = pStaIfc->wpaCliCmdWpsPbc(pStaIfc, &config);
684 }
685 if (ret < 0) {
686 LOGE("StartWpsPbcMode failed! ret=%{public}d", ret);
687 return WIFI_HAL_FAILED;
688 } else if (ret == WIFI_HAL_PBC_OVERLAP) {
689 LOGD("StartWpsPbcMode: failed-PBC-OVERLAP");
690 return WIFI_HAL_PBC_OVERLAP;
691 }
692 return WIFI_HAL_SUCCESS;
693 }
694
StartWpsPinMode(const WifiWpsParam * param,int * pinCode)695 WifiErrorNo StartWpsPinMode(const WifiWpsParam *param, int *pinCode)
696 {
697 LOGD("StartWpsPinMode()");
698 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
699 if (pStaIfc == NULL) {
700 return WIFI_HAL_SUPPLICANT_NOT_INIT;
701 }
702 if (param == NULL || pinCode == NULL) {
703 return WIFI_HAL_FAILED;
704 }
705 struct WpaWpsPinArgv config = {{0}, {0}};
706 if (strncpy_s(config.bssid, sizeof(config.bssid), param->bssid, strlen(param->bssid)) != EOK) {
707 return WIFI_HAL_FAILED;
708 }
709 int ret = pStaIfc->wpaCliCmdWpsPin(pStaIfc, &config, pinCode);
710 if (ret < 0) {
711 LOGE("StartWpsPinMode failed! ret=%{public}d", ret);
712 return WIFI_HAL_FAILED;
713 }
714 return WIFI_HAL_SUCCESS;
715 }
716
StopWps(void)717 WifiErrorNo StopWps(void)
718 {
719 LOGD("StopWps()");
720 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
721 if (pStaIfc == NULL) {
722 return WIFI_HAL_SUPPLICANT_NOT_INIT;
723 }
724 int ret = pStaIfc->wpaCliCmdWpsCancel(pStaIfc);
725 if (ret < 0) {
726 LOGE("StopWps failed! ret=%{public}d", ret);
727 return WIFI_HAL_FAILED;
728 }
729 return WIFI_HAL_SUCCESS;
730 }
731
GetRoamingCapabilities(WifiRoamCapability * capability)732 WifiErrorNo GetRoamingCapabilities(WifiRoamCapability *capability)
733 {
734 LOGI("GetRoamingCapabilities");
735 if (capability == NULL) {
736 return WIFI_HAL_FAILED;
737 }
738 return WIFI_HAL_SUCCESS;
739 }
740
SetRoamConfig(char ** blocklist,int blocksize,char ** trustlist,int trustsize)741 WifiErrorNo SetRoamConfig(char **blocklist, int blocksize, char **trustlist, int trustsize)
742 {
743 LOGI("SetRoamConfig, blocksize:%{public}d, trustsize:%{public}d", blocksize, trustsize);
744 if (trustlist != NULL && trustsize > 0) {
745 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
746 if (pStaIfc == NULL) {
747 return WIFI_HAL_SUPPLICANT_NOT_INIT;
748 }
749 for (int i = 0; i < trustsize; i++) {
750 int ret = pStaIfc->wpaCliCmdSetRoamConfig(pStaIfc, trustlist[i]);
751 if (ret < 0) {
752 LOGE("wpaCliCmdSetRoamConfig failed! ret=%{public}d", ret);
753 return WIFI_HAL_FAILED;
754 }
755 }
756 }
757
758 if (blocklist == NULL || blocksize <= 0) {
759 return WIFI_HAL_SUCCESS;
760 }
761 return WIFI_HAL_SUCCESS;
762 }
763
WpaSetCountryCode(const char * countryCode)764 WifiErrorNo WpaSetCountryCode(const char *countryCode)
765 {
766 if (countryCode == NULL) {
767 LOGE("WpaSetCountryCode countryCode is NULL");
768 return WIFI_HAL_FAILED;
769 }
770 LOGD("WpaSetCountryCode ");
771 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
772 if (pStaIfc == NULL) {
773 return WIFI_HAL_SUPPLICANT_NOT_INIT;
774 }
775 int ret = pStaIfc->wpaCliCmdSetCountryCode(pStaIfc, countryCode);
776 if (ret < 0) {
777 LOGE("WpaSetCountryCode failed! ret=%{public}d", ret);
778 return WIFI_HAL_FAILED;
779 }
780 return WIFI_HAL_SUCCESS;
781 }
WpaGetCountryCode(char * countryCode,int codeSize)782 WifiErrorNo WpaGetCountryCode(char *countryCode, int codeSize)
783 {
784 if (countryCode == NULL) {
785 LOGE("WpaGetCountryCode countryCode is NULL");
786 return WIFI_HAL_FAILED;
787 }
788 LOGD("WpaGetCountryCode ");
789 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
790 if (pStaIfc == NULL) {
791 return WIFI_HAL_SUPPLICANT_NOT_INIT;
792 }
793 int ret = pStaIfc->wpaCliCmdGetCountryCode(pStaIfc, countryCode, codeSize);
794 if (ret < 0) {
795 LOGE("WpaSetCountryCode failed! ret=%{public}d", ret);
796 return WIFI_HAL_FAILED;
797 }
798 return WIFI_HAL_SUCCESS;
799 }
WpaGetNetWork(GetNetworkConfig * conf)800 WifiErrorNo WpaGetNetWork(GetNetworkConfig *conf)
801 {
802 if (conf == NULL) {
803 LOGE("WpaGetNetWork conf is NULL");
804 return WIFI_HAL_FAILED;
805 }
806 LOGD("WpaGetNetWork()");
807 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
808 if (pStaIfc == NULL) {
809 return WIFI_HAL_SUPPLICANT_NOT_INIT;
810 }
811 struct WpaGetNetworkArgv argv = {0};
812 argv.id = conf->networkId;
813 if (strncpy_s(argv.param, sizeof(argv.param), conf->param, strlen(conf->param)) != EOK) {
814 return WIFI_HAL_FAILED;
815 }
816 int ret = pStaIfc->wpaCliCmdGetNetwork(pStaIfc, &argv, conf->value, sizeof(conf->value));
817 if (ret < 0) {
818 LOGE("WpaGetNetWork failed! ret=%{public}d", ret);
819 return WIFI_HAL_FAILED;
820 }
821 return WIFI_HAL_SUCCESS;
822 }
823
WpaBlocklistClear(void)824 WifiErrorNo WpaBlocklistClear(void)
825 {
826 LOGD("WpaBlocklistClear()");
827 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
828 if (pStaIfc == NULL) {
829 return WIFI_HAL_SUPPLICANT_NOT_INIT;
830 }
831 int ret = pStaIfc->wpaCliCmdWpaBlockListClear(pStaIfc);
832 if (ret < 0) {
833 LOGE("WpaBlocklistClear failed! ret=%{public}d", ret);
834 return WIFI_HAL_FAILED;
835 }
836 return WIFI_HAL_SUCCESS;
837 }
838
GetConnectSignalInfo(const char * endBssid,WpaSignalInfo * info)839 WifiErrorNo GetConnectSignalInfo(const char *endBssid, WpaSignalInfo *info)
840 {
841 if (endBssid == NULL || info == NULL) {
842 LOGE("GetConnectSignalInfo endBssid or info is NULL");
843 return WIFI_HAL_FAILED;
844 }
845 LOGD("GetConnectSignalInfo()");
846 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
847 if (pStaIfc == NULL) {
848 return WIFI_HAL_SUPPLICANT_NOT_INIT;
849 }
850 int ret = pStaIfc->wpaCliCmdGetSignalInfo(pStaIfc, info);
851 if (ret < 0) {
852 LOGE("WpaCliCmdGetSignalInfo failed! ret=%{public}d", ret);
853 return WIFI_HAL_FAILED;
854 }
855 return WIFI_HAL_SUCCESS;
856 }
857
SetSuspendMode(bool mode)858 WifiErrorNo SetSuspendMode(bool mode)
859 {
860 LOGI("SetSuspendMode() mode: %{public}d", mode);
861 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
862 if (pStaIfc == NULL) {
863 LOGE("GetWifiStaInterface return failed");
864 return WIFI_HAL_SUPPLICANT_NOT_INIT;
865 }
866 int ret = pStaIfc->wpaCliCmdWpaSetSuspendMode(pStaIfc, mode);
867 if (ret != WIFI_HAL_SUCCESS) {
868 LOGE("wpaCliCmdWpaSetSuspendMode return failed! ret = %{public}d", ret);
869 return WIFI_HAL_FAILED;
870 }
871 return WIFI_HAL_SUCCESS;
872 }
873
SetPowerMode(bool mode)874 WifiErrorNo SetPowerMode(bool mode)
875 {
876 LOGI("SetPowerMode() mode: %{public}d", mode);
877 WifiWpaStaInterface *pStaIfc = GetWifiStaInterface(0);
878 if (pStaIfc == NULL) {
879 LOGE("GetWifiStaInterface return failed");
880 return WIFI_HAL_SUPPLICANT_NOT_INIT;
881 }
882 int ret = pStaIfc->wpaCliCmdWpaSetPowerMode(pStaIfc, mode);
883 if (ret != WIFI_HAL_SUCCESS) {
884 LOGE("wpaCliCmdWpaSetPowerMode return failed! ret = %{public}d", ret);
885 return WIFI_HAL_FAILED;
886 }
887 return WIFI_HAL_SUCCESS;
888 }
889
890