1 /*
2 * Copyright (c) 2023 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 #ifdef HDI_WPA_INTERFACE_SUPPORT
17 #include "wifi_hdi_wpa_client.h"
18
19 #include <codecvt>
20 #include <fstream>
21 #include <iostream>
22 #include <locale>
23 #include <securec.h>
24 #include <unistd.h>
25 #include <sys/stat.h>
26
27 #include "wifi_hdi_wpa_sta_impl.h"
28 #include "wifi_hdi_wpa_callback.h"
29 #include "wifi_hdi_wpa_ap_impl.h"
30 #include "wifi_hdi_wpa_p2p_impl.h"
31 #include "wifi_hdi_util.h"
32 #include "wifi_common_util.h"
33 #include "wifi_common_def.h"
34 #include "hdi_struct_toolkit.h"
35 #include "wifi_hdi_wpa_proxy.h"
36 #include "define.h"
37
38 #ifndef UT_TEST
39 #include "wifi_log.h"
40 #else
41 #define static
42 #define LOGI(...)
43 #define LOGE(...)
44 #endif
45
46 #undef LOG_TAG
47 #define LOG_TAG "WifiHdiWpaClient"
48
49 #define HOSTAPD_CFG_VALUE_ON 1
50 #define DEFAULT_HOSTAPD_CONF_PATH CONFIG_ROOR_DIR"/wpa_supplicant/hostapd.conf"
51
52 namespace OHOS {
53 namespace Wifi {
54 #define MAX_IFACENAME_LEN 6
55 #define MAX_CMD_BUFFER_SIZE 1024
56 #define MAX_PASSWORD_LEN 64
57 constexpr int PMF_OPTIONAL = 1;
58 constexpr int PMF_REQUIRED = 2;
59 const int BUFFER_SIZE = 4096;
60 constexpr int WIFI_HDI_STR_MAC_LENGTH = 17;
61 constexpr int WIFI_HDI_MAX_STR_LENGTH = 512;
62 constexpr int WIFI_MAX_SCAN_COUNT = 256;
63 constexpr int P2P_SUPPLICANT_DISCONNECTED = 0;
64 constexpr int P2P_SUPPLICANT_CONNECTED = 1;
65 constexpr int BAND_WIDTH_OFFSET = 16;
66
StartWifi(const std::string & ifaceName,int instId)67 WifiErrorNo WifiHdiWpaClient::StartWifi(const std::string &ifaceName, int instId)
68 {
69 WifiEventCallback callback;
70 callback.onConnectChanged = [](int param1, int param2, const std::string ¶m3, int param4) {};
71 ReqRegisterStaEventCallback(callback, ifaceName.c_str(), instId);
72 LOGI("WifiHdiWpaClient StartWifi ifaceName:%{public}s instId:%{public}d", ifaceName.c_str(), instId);
73 return HdiWpaStaStart(ifaceName.c_str(), instId);
74 }
75
StopWifi(int instId)76 WifiErrorNo WifiHdiWpaClient::StopWifi(int instId)
77 {
78 return HdiWpaStaStop(instId);
79 }
80
ReqConnect(int networkId,const char * ifaceName)81 WifiErrorNo WifiHdiWpaClient::ReqConnect(int networkId, const char *ifaceName)
82 {
83 return HdiWpaStaConnect(networkId, ifaceName);
84 }
85
ReqReconnect(const char * ifaceName)86 WifiErrorNo WifiHdiWpaClient::ReqReconnect(const char *ifaceName)
87 {
88 return HdiWpaStaReconnect(ifaceName);
89 }
90
ReqReassociate(const char * ifaceName)91 WifiErrorNo WifiHdiWpaClient::ReqReassociate(const char *ifaceName)
92 {
93 return HdiWpaStaReassociate(ifaceName);
94 }
95
ReqDisconnect(const char * ifaceName)96 WifiErrorNo WifiHdiWpaClient::ReqDisconnect(const char *ifaceName)
97 {
98 return HdiWpaStaDisconnect(ifaceName);
99 }
100
GetStaCapabilities(unsigned int & capabilities)101 WifiErrorNo WifiHdiWpaClient::GetStaCapabilities(unsigned int &capabilities)
102 {
103 capabilities = 0;
104 return WIFI_HAL_OPT_OK;
105 }
106
GetStaDeviceMacAddress(std::string & mac,const char * ifaceName)107 WifiErrorNo WifiHdiWpaClient::GetStaDeviceMacAddress(std::string &mac, const char *ifaceName)
108 {
109 char macAddr[HAL_BSSID_LENGTH + 1] = {0};
110 int macAddrLen = HAL_BSSID_LENGTH + 1;
111 WifiErrorNo err = HdiWpaStaGetDeviceMacAddress(macAddr, macAddrLen, ifaceName);
112 if (err == WIFI_HAL_OPT_OK) {
113 mac = std::string(macAddr);
114 }
115 return err;
116 }
117
GetSupportFrequencies(int band,std::vector<int> & frequencies)118 WifiErrorNo WifiHdiWpaClient::GetSupportFrequencies(int band, std::vector<int> &frequencies)
119 {
120 return WIFI_HAL_OPT_NOT_SUPPORT;
121 }
122
SetConnectMacAddr(const std::string & mac)123 WifiErrorNo WifiHdiWpaClient::SetConnectMacAddr(const std::string &mac)
124 {
125 return WIFI_HAL_OPT_NOT_SUPPORT;
126 }
127
Scan(const WifiHalScanParam & scanParam)128 WifiErrorNo WifiHdiWpaClient::Scan(const WifiHalScanParam &scanParam)
129 {
130 return HdiWpaStaScan();
131 }
132
QueryScanInfos(std::vector<InterScanInfo> & scanInfos)133 WifiErrorNo WifiHdiWpaClient::QueryScanInfos(std::vector<InterScanInfo> &scanInfos)
134 {
135 LOGI("WifiHdiWpaClient::%{public}s enter", __func__);
136 int size = HAL_GET_MAX_SCAN_INFO;
137 ScanInfo *results = HdiWpaStaGetScanInfos(&size, GetHdiStaIfaceName(INSTID_WLAN0));
138 if (results == NULL) {
139 return size == 0 ? WIFI_HAL_OPT_OK : WIFI_HAL_OPT_FAILED;
140 }
141 for (int i = 0; i < size; ++i) {
142 InterScanInfo tmp;
143 tmp.ssid = results[i].ssid;
144 tmp.bssid = results[i].bssid;
145 tmp.frequency = results[i].freq;
146 tmp.rssi = results[i].siglv;
147 tmp.timestamp = results[i].timestamp;
148 tmp.capabilities = results[i].flags;
149 tmp.channelWidth = (WifiChannelWidth)results[i].channelWidth;
150 tmp.centerFrequency0 = results[i].centerFrequency0;
151 tmp.centerFrequency1 = results[i].centerFrequency1;
152 tmp.isVhtInfoExist = results[i].isVhtInfoExist;
153 tmp.isHtInfoExist = results[i].isHtInfoExist;
154 tmp.isHeInfoExist = results[i].isHeInfoExist;
155 tmp.isErpExist = results[i].isErpExist;
156 tmp.maxRates = results[i].maxRates > results[i].extMaxRates ? results[i].maxRates : results[i].extMaxRates;
157 LOGI("WifiHdiWpaClient::QueryScanInfos ssid = %{public}s, ssid = %{public}s",
158 SsidAnonymize(results[i].ssid).c_str(), MacAnonymize(results[i].bssid).c_str());
159 for (int j = 0; j < results[i].ieSize; ++j) {
160 WifiInfoElem infoElemTmp;
161 int infoElemSize = results[i].infoElems[j].size;
162 infoElemTmp.id = results[i].infoElems[j].id;
163 for (int k = 0; k < infoElemSize; ++k) {
164 infoElemTmp.content.emplace_back(results[i].infoElems[j].content[k]);
165 }
166 if (results[i].infoElems[j].content) {
167 free(results[i].infoElems[j].content);
168 }
169 tmp.infoElems.emplace_back(infoElemTmp);
170 }
171 if (results[i].infoElems) {
172 free(results[i].infoElems);
173 }
174 tmp.isHiLinkNetwork = results[i].isHiLinkNetwork;
175 scanInfos.emplace_back(tmp);
176 }
177 free(results);
178 results = nullptr;
179 return WIFI_HAL_OPT_OK;
180 }
181
ReqStartPnoScan(const WifiHalPnoScanParam & scanParam)182 WifiErrorNo WifiHdiWpaClient::ReqStartPnoScan(const WifiHalPnoScanParam &scanParam)
183 {
184 return WIFI_HAL_OPT_NOT_SUPPORT;
185 }
186
ReqStopPnoScan(void)187 WifiErrorNo WifiHdiWpaClient::ReqStopPnoScan(void)
188 {
189 return WIFI_HAL_OPT_NOT_SUPPORT;
190 }
191
RemoveDevice(int networkId,const char * ifaceName)192 WifiErrorNo WifiHdiWpaClient::RemoveDevice(int networkId, const char *ifaceName)
193 {
194 if (networkId < 0) {
195 return WIFI_HAL_OPT_INVALID_PARAM;
196 }
197
198 return HdiWpaStaRemoveNetwork(networkId, ifaceName);
199 }
200
ClearDeviceConfig(const char * ifaceName) const201 WifiErrorNo WifiHdiWpaClient::ClearDeviceConfig(const char *ifaceName) const
202 {
203 return HdiWpaStaRemoveNetwork(-1, ifaceName);
204 }
205
GetNextNetworkId(int & networkId,const char * ifaceName)206 WifiErrorNo WifiHdiWpaClient::GetNextNetworkId(int &networkId, const char *ifaceName)
207 {
208 return HdiWpaStaAddNetwork(&networkId, ifaceName);
209 }
210
ReqEnableNetwork(int networkId,const char * ifaceName)211 WifiErrorNo WifiHdiWpaClient::ReqEnableNetwork(int networkId, const char *ifaceName)
212 {
213 return HdiWpaStaEnableNetwork(networkId, ifaceName);
214 }
215
ReqDisableNetwork(int networkId,const char * ifaceName)216 WifiErrorNo WifiHdiWpaClient::ReqDisableNetwork(int networkId, const char *ifaceName)
217 {
218 return HdiWpaStaDisableNetwork(networkId, ifaceName);
219 }
220
SetWapiConfig(const WifiHalDeviceConfig & config,SetNetworkConfig * conf,int & num)221 void WifiHdiWpaClient::SetWapiConfig(const WifiHalDeviceConfig &config, SetNetworkConfig *conf, int &num)
222 {
223 LOGI("Enter SetWapiConfig, keyMgmt is %{public}s, pskType is %{public}d", config.keyMgmt.c_str(),
224 config.wapiPskType);
225 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_KEYMGMT, config.keyMgmt);
226 if (config.keyMgmt == KEY_MGMT_WAPI_PSK) {
227 num += PushDeviceConfigInt(conf + num, DEVICE_CONFIG_WAPI_PSK_TYPE, config.wapiPskType);
228 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_WAPI_PSK, config.psk);
229 } else if (config.keyMgmt == KEY_MGMT_WAPI_CERT) {
230 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_WAPI_USER_CERT, config.wapiUserCertData);
231 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_WAPI_CA_CERT, config.wapiAsCertData);
232 }
233 }
234
SetDeviceConfig(int networkId,const WifiHalDeviceConfig & config,const char * ifaceName)235 WifiErrorNo WifiHdiWpaClient::SetDeviceConfig(int networkId, const WifiHalDeviceConfig &config, const char *ifaceName)
236 {
237 if (CheckValidDeviceConfig(config) != WIFI_HAL_OPT_OK) {
238 LOGE("SetDeviceConfig, CheckValidDeviceConfig return error!");
239 return WIFI_HAL_OPT_FAILED;
240 }
241 SetNetworkConfig conf[DEVICE_CONFIG_END_POS];
242 if (memset_s(conf, sizeof(conf), 0, sizeof(conf)) != EOK) {
243 LOGE("SetDeviceConfig, memset_s return error!");
244 return WIFI_HAL_OPT_FAILED;
245 }
246 int num = 0;
247 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_SSID, config.ssid);
248 do {
249 if (config.keyMgmt.find(KEY_MGMT_WAPI) != std::string::npos) {
250 SetWapiConfig(config, conf, num);
251 break;
252 }
253 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_PSK, config.psk);
254 if (config.keyMgmt.find(KEY_MGMT_SAE) != std::string::npos) {
255 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_SAE_PASSWD, config.psk);
256 }
257 if (config.keyMgmt == KEY_MGMT_NONE || config.keyMgmt == KEY_MGMT_WEP) {
258 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_KEYMGMT, KEY_MGMT_NONE);
259 } else {
260 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_KEYMGMT, config.keyMgmt);
261 }
262 } while (0);
263 EapMethod eapMethod = WifiEapConfig::Str2EapMethod(config.eapConfig.eap);
264 LOGI("%{public}s, eap:%{public}s, eapMethod:%{public}d, identity:%{private}s, num:%{public}d",
265 __func__, config.eapConfig.eap.c_str(), eapMethod, config.eapConfig.identity.c_str(), num);
266 switch (eapMethod) {
267 case EapMethod::EAP_PEAP:
268 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_EAP, config.eapConfig.eap);
269 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_IDENTITY, config.eapConfig.identity);
270 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_PASSWORD, config.eapConfig.password);
271 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_EAP_CA_CERT, config.eapConfig.caCertPath);
272 if (config.eapConfig.phase2Method != static_cast<int>(Phase2Method::NONE)) {
273 std::string strPhase2Method = WifiEapConfig::Phase2MethodToStr(config.eapConfig.eap,
274 config.eapConfig.phase2Method);
275 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_EAP_PHASE2METHOD, strPhase2Method);
276 }
277 break;
278 case EapMethod::EAP_TLS:
279 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_EAP, config.eapConfig.eap);
280 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_IDENTITY, config.eapConfig.identity);
281 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_EAP_CA_CERT, config.eapConfig.caCertPath);
282 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_EAP_CLIENT_CERT, config.eapConfig.clientCert);
283 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_EAP_PRIVATE_KEY, config.eapConfig.privateKey);
284 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_PASSWORD, config.eapConfig.password);
285 break;
286 case EapMethod::EAP_TTLS:
287 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_EAP, config.eapConfig.eap);
288 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_IDENTITY, config.eapConfig.identity);
289 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_EAP_CA_CERT, config.eapConfig.caCertPath);
290 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_PASSWORD, config.eapConfig.password);
291 if (config.eapConfig.phase2Method != static_cast<int>(Phase2Method::NONE)) {
292 std::string strPhase2Method = WifiEapConfig::Phase2MethodToStr(config.eapConfig.eap,
293 config.eapConfig.phase2Method);
294 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_EAP_PHASE2METHOD, strPhase2Method);
295 }
296 break;
297 case EapMethod::EAP_PWD:
298 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_EAP, config.eapConfig.eap);
299 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_IDENTITY, config.eapConfig.identity);
300 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_PASSWORD, config.eapConfig.password);
301 break;
302 case EapMethod::EAP_SIM:
303 case EapMethod::EAP_AKA:
304 case EapMethod::EAP_AKA_PRIME:
305 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_EAP, config.eapConfig.eap);
306 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_IDENTITY, config.eapConfig.identity);
307 break;
308 default:
309 LOGE("%{public}s, invalid eapMethod:%{public}d", __func__, eapMethod);
310 break;
311 }
312
313 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_BSSID, config.bssid);
314 int i = 0;
315 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_WEP_KEY_0, config.wepKeys[i++]);
316 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_WEP_KEY_1, config.wepKeys[i++]);
317 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_WEP_KEY_2, config.wepKeys[i++]);
318 num += PushDeviceConfigString(conf + num, DEVICE_CONFIG_WEP_KEY_3, config.wepKeys[i++]);
319 if (config.priority >= 0) {
320 num += PushDeviceConfigInt(conf + num, DEVICE_CONFIG_PRIORITY, config.priority);
321 }
322 if (config.scanSsid == 1) {
323 num += PushDeviceConfigInt(conf + num, DEVICE_CONFIG_SCAN_SSID, config.scanSsid);
324 }
325 if (config.wepKeyIdx >= 0) {
326 num += PushDeviceConfigInt(conf + num, DEVICE_CONFIG_WEP_KEY_IDX, config.wepKeyIdx);
327 }
328 if (config.authAlgorithms > 0) {
329 num += PushDeviceConfigAuthAlgorithm(conf + num, DEVICE_CONFIG_AUTH_ALGORITHMS, config.authAlgorithms);
330 }
331 if (config.isRequirePmf) {
332 num += PushDeviceConfigInt(conf + num, DEVICE_CONFIG_IEEE80211W, PMF_REQUIRED);
333 } else {
334 num += PushDeviceConfigInt(conf + num, DEVICE_CONFIG_IEEE80211W, PMF_OPTIONAL);
335 }
336 if (config.allowedProtocols > 0) {
337 std::string protocolsStr[] = {"WPA ", "RSN ", "WPA2 ", "OSEN ", "WAPI "};
338 num += PushDeviceConfigParseMask(conf + num, DEVICE_CONFIG_ALLOW_PROTOCOLS, config.allowedProtocols,
339 protocolsStr, sizeof(protocolsStr)/sizeof(protocolsStr[0]));
340 }
341 if (config.allowedPairwiseCiphers > 0) {
342 std::string pairwiseCipherStr[] = {"NONE ", "TKIP ", "CCMP ", "GCMP ", "CCMP-256 ", "GCMP-256 ", "SMS4 "};
343 num += PushDeviceConfigParseMask(conf + num, DEVICE_CONFIG_PAIRWISE_CIPHERS, config.allowedPairwiseCiphers,
344 pairwiseCipherStr, sizeof(pairwiseCipherStr)/sizeof(pairwiseCipherStr[0]));
345 }
346 if (config.allowedGroupCiphers > 0) {
347 std::string groupCipherStr[] = {"GTK_NOT_USED ", "TKIP ", "CCMP ", "GCMP ", "CCMP-256 ", "GCMP-256 ", "SMS4 "};
348 num += PushDeviceConfigParseMask(conf + num, DEVICE_CONFIG_GROUP_CIPHERS, config.allowedGroupCiphers,
349 groupCipherStr, sizeof(groupCipherStr)/sizeof(groupCipherStr[0]));
350 }
351 if (config.allowedGroupMgmtCiphers > 0) {
352 std::string groupMgmtCipherStr[] = {"AES-128-CMAC ", "BIP-GMAC-128 ", "BIP-GMAC-256 ", "BIP-CMAC-256 "};
353 num += PushDeviceConfigParseMask(conf + num, DEVICE_CONFIG_GROUP_MGMT_CIPHERS, config.allowedGroupMgmtCiphers,
354 groupMgmtCipherStr, sizeof(groupMgmtCipherStr)/sizeof(groupMgmtCipherStr[0]));
355 }
356 if (num == 0) {
357 return WIFI_HAL_OPT_OK;
358 }
359 return HdiWpaStaSetNetwork(networkId, conf, num, ifaceName);
360 }
361
SetBssid(int networkId,const std::string & bssid,const char * ifaceName)362 WifiErrorNo WifiHdiWpaClient::SetBssid(int networkId, const std::string &bssid, const char *ifaceName)
363 {
364 SetNetworkConfig conf;
365 int num = PushDeviceConfigString(&conf, DEVICE_CONFIG_BSSID, bssid, false);
366 if (num == 0) {
367 LOGE("SetBssid, PushDeviceConfigString return error!");
368 return WIFI_HAL_OPT_OK;
369 }
370
371 return HdiWpaStaSetNetwork(networkId, &conf, num, ifaceName);
372 }
373
SaveDeviceConfig(const char * ifaceName)374 WifiErrorNo WifiHdiWpaClient::SaveDeviceConfig(const char *ifaceName)
375 {
376 return HdiWpaStaSaveConfig(ifaceName);
377 }
378
ReqRegisterStaEventCallback(const WifiEventCallback & callback,const char * ifaceName,int instId)379 WifiErrorNo WifiHdiWpaClient::ReqRegisterStaEventCallback(
380 const WifiEventCallback &callback, const char *ifaceName, int instId)
381 {
382 struct IWpaCallback cWifiHdiWpaCallback;
383 if (memset_s(&cWifiHdiWpaCallback, sizeof(cWifiHdiWpaCallback), 0, sizeof(cWifiHdiWpaCallback)) != EOK) {
384 return WIFI_HAL_OPT_FAILED;
385 }
386
387 if (callback.onConnectChanged != nullptr) {
388 cWifiHdiWpaCallback.OnEventDisconnected = OnEventDisconnected;
389 cWifiHdiWpaCallback.OnEventConnected = OnEventConnected;
390 cWifiHdiWpaCallback.OnEventBssidChanged = OnEventBssidChanged;
391 cWifiHdiWpaCallback.OnEventStateChanged = OnEventStateChanged;
392 cWifiHdiWpaCallback.OnEventTempDisabled = OnEventTempDisabled;
393 cWifiHdiWpaCallback.OnEventAssociateReject = OnEventAssociateReject;
394 cWifiHdiWpaCallback.OnEventWpsOverlap = OnEventWpsOverlap;
395 cWifiHdiWpaCallback.OnEventWpsTimeout = OnEventWpsTimeout;
396 cWifiHdiWpaCallback.OnEventAuthTimeout = OnEventAuthTimeout;
397 cWifiHdiWpaCallback.OnEventScanResult = OnEventScanResult;
398 cWifiHdiWpaCallback.OnEventStaNotify = OnEventStaNotify;
399 cWifiHdiWpaCallback.OnEventAuthReject = OnEventAuthReject;
400 }
401
402 return RegisterHdiWpaStaEventCallback(&cWifiHdiWpaCallback, ifaceName, instId);
403 }
404
ReqStartWpsPbcMode(const WifiHalWpsConfig & config,const char * ifaceName)405 WifiErrorNo WifiHdiWpaClient::ReqStartWpsPbcMode(const WifiHalWpsConfig &config, const char *ifaceName)
406 {
407 WifiWpsParam param;
408 if (memset_s(¶m, sizeof(param), 0, sizeof(param)) != EOK) {
409 return WIFI_HAL_OPT_FAILED;
410 }
411 param.anyFlag = config.anyFlag;
412 param.multiAp = config.multiAp;
413 if (strncpy_s(param.bssid, sizeof(param.bssid), config.bssid.c_str(), config.bssid.length()) != EOK) {
414 return WIFI_HAL_OPT_FAILED;
415 }
416 return HdiWpaStaStartWpsPbcMode(¶m, ifaceName);
417 }
418
ReqStartWpsPinMode(const WifiHalWpsConfig & config,int & pinCode,const char * ifaceName)419 WifiErrorNo WifiHdiWpaClient::ReqStartWpsPinMode(const WifiHalWpsConfig &config, int &pinCode, const char *ifaceName)
420 {
421 WifiWpsParam param;
422 if (memset_s(¶m, sizeof(param), 0, sizeof(param)) != EOK) {
423 return WIFI_HAL_OPT_FAILED;
424 }
425 param.anyFlag = config.anyFlag;
426 param.multiAp = config.multiAp;
427 if (strncpy_s(param.bssid, sizeof(param.bssid), config.bssid.c_str(), config.bssid.length()) != EOK) {
428 return WIFI_HAL_OPT_FAILED;
429 }
430 if (!config.pinCode.empty()) {
431 if (strncpy_s(param.pinCode, sizeof(param.pinCode), config.pinCode.c_str(), config.pinCode.length()) != EOK) {
432 return WIFI_HAL_OPT_FAILED;
433 }
434 }
435 return HdiWpaStaStartWpsPinMode(¶m, &pinCode, ifaceName);
436 }
437
ReqStopWps(const char * ifaceName)438 WifiErrorNo WifiHdiWpaClient::ReqStopWps(const char *ifaceName)
439 {
440 return HdiStopWpsSta(ifaceName);
441 }
442
ReqGetRoamingCapabilities(WifiHalRoamCapability & capability)443 WifiErrorNo WifiHdiWpaClient::ReqGetRoamingCapabilities(WifiHalRoamCapability &capability)
444 {
445 return WIFI_HAL_OPT_NOT_SUPPORT;
446 }
447
ReqSetRoamConfig(const WifiHalRoamConfig & config)448 WifiErrorNo WifiHdiWpaClient::ReqSetRoamConfig(const WifiHalRoamConfig &config)
449 {
450 return WIFI_HAL_OPT_NOT_SUPPORT;
451 }
452
ReqGetConnectSignalInfo(const std::string & endBssid,WifiSignalPollInfo & info) const453 WifiErrorNo WifiHdiWpaClient::ReqGetConnectSignalInfo(const std::string &endBssid, WifiSignalPollInfo &info) const
454 {
455 return WIFI_HAL_OPT_NOT_SUPPORT;
456 }
457
ReqWpaAutoConnect(int enable,const char * ifaceName)458 WifiErrorNo WifiHdiWpaClient::ReqWpaAutoConnect(int enable, const char *ifaceName)
459 {
460 return HdiWpaStaAutoConnect(enable, ifaceName);
461 }
462
ReqWpaBlocklistClear(const char * ifaceName)463 WifiErrorNo WifiHdiWpaClient::ReqWpaBlocklistClear(const char *ifaceName)
464 {
465 return HdiWpaStaBlocklistClear(ifaceName);
466 }
467
ReqSetPowerSave(bool enable,const char * ifaceName)468 WifiErrorNo WifiHdiWpaClient::ReqSetPowerSave(bool enable, const char *ifaceName)
469 {
470 return HdiWpaStaSetPowerSave(enable, ifaceName);
471 }
472
ReqWpaSetCountryCode(const std::string & countryCode,const char * ifaceName)473 WifiErrorNo WifiHdiWpaClient::ReqWpaSetCountryCode(const std::string &countryCode, const char *ifaceName)
474 {
475 return HdiWpaStaSetCountryCode(countryCode.c_str(), ifaceName);
476 }
477
ReqWpaGetCountryCode(std::string & countryCode,const char * ifaceName)478 WifiErrorNo WifiHdiWpaClient::ReqWpaGetCountryCode(std::string &countryCode, const char *ifaceName)
479 {
480 char szCountryCode[HAL_COUNTRY_CODE_LENGTH + 1] = "";
481 if (WIFI_HAL_OPT_OK != HdiWpaStaGetCountryCode(szCountryCode, HAL_COUNTRY_CODE_LENGTH, ifaceName)) {
482 return WIFI_HAL_OPT_FAILED;
483 }
484 countryCode = szCountryCode;
485 return WIFI_HAL_OPT_OK;
486 }
487
ReqWpaSetSuspendMode(bool mode,const char * ifaceName) const488 WifiErrorNo WifiHdiWpaClient::ReqWpaSetSuspendMode(bool mode, const char *ifaceName) const
489 {
490 return HdiWpaStaSetSuspendMode(mode, ifaceName);
491 }
492
ReqWpaShellCmd(const std::string & ifName,const std::string & cmd)493 WifiErrorNo WifiHdiWpaClient::ReqWpaShellCmd(const std::string &ifName, const std::string &cmd)
494 {
495 char ifNameBuf[MAX_IFACENAME_LEN];
496 if (strncpy_s(ifNameBuf, sizeof(ifNameBuf), ifName.c_str(), ifName.length()) != EOK) {
497 LOGE("%{public}s: failed to copy", __func__);
498 return WIFI_HAL_OPT_FAILED;
499 }
500
501 const int MAX_EAP_CMD_BUFFER_SIZE = 4096;
502 char cmdBuf[MAX_EAP_CMD_BUFFER_SIZE];
503 if (strncpy_s(cmdBuf, sizeof(cmdBuf), cmd.c_str(), cmd.length()) != EOK) {
504 LOGE("%{public}s: failed to copy", __func__);
505 return WIFI_HAL_OPT_FAILED;
506 }
507 return HdiWpaStaSetShellCmd(ifNameBuf, cmdBuf);
508 }
509
ReqWpaGetPskPassphrase(const std::string & ifName,std::string & psk)510 WifiErrorNo WifiHdiWpaClient::ReqWpaGetPskPassphrase(const std::string &ifName, std::string &psk)
511 {
512 char ifNameBuf[MAX_IFACENAME_LEN];
513 char tmpPsk[MAX_CMD_BUFFER_SIZE] = {0};
514 uint32_t pskLen = MAX_PASSWORD_LEN;
515 if (strncpy_s(ifNameBuf, sizeof(ifNameBuf), ifName.c_str(), ifName.length()) != EOK) {
516 LOGE("%{public}s: failed to copy", __func__);
517 return WIFI_HAL_OPT_FAILED;
518 }
519
520 if (HdiWpaStaGetPskPassphrase(ifNameBuf, tmpPsk, pskLen) != WIFI_HAL_OPT_OK) {
521 LOGE("%{public}s: GetPskPassphrase failed", __func__);
522 return WIFI_HAL_OPT_FAILED;
523 }
524 psk = tmpPsk;
525 return WIFI_HAL_OPT_OK;
526 }
527
PushDeviceConfigString(SetNetworkConfig * pConfig,DeviceConfigType type,const std::string & msg,bool checkEmpty) const528 int WifiHdiWpaClient::PushDeviceConfigString(
529 SetNetworkConfig *pConfig, DeviceConfigType type, const std::string &msg, bool checkEmpty) const
530 {
531 if (!checkEmpty || msg.length() > 0) {
532 pConfig->cfgParam = type;
533 if (strncpy_s(pConfig->cfgValue, sizeof(pConfig->cfgValue), msg.c_str(), msg.length()) != EOK) {
534 return 0;
535 }
536 return 1;
537 } else {
538 return 0;
539 }
540 }
541
PushDeviceConfigInt(SetNetworkConfig * pConfig,DeviceConfigType type,int i) const542 int WifiHdiWpaClient::PushDeviceConfigInt(SetNetworkConfig *pConfig, DeviceConfigType type, int i) const
543 {
544 pConfig->cfgParam = type;
545 if (snprintf_s(pConfig->cfgValue, sizeof(pConfig->cfgValue), sizeof(pConfig->cfgValue) - 1, "%d", i) < 0) {
546 return 0;
547 }
548 return 1;
549 }
550
PushDeviceConfigAuthAlgorithm(SetNetworkConfig * pConfig,DeviceConfigType type,unsigned int alg) const551 int WifiHdiWpaClient::PushDeviceConfigAuthAlgorithm(
552 SetNetworkConfig *pConfig, DeviceConfigType type, unsigned int alg) const
553 {
554 pConfig->cfgParam = type;
555 if (alg & 0x1) {
556 if (strcat_s(pConfig->cfgValue, sizeof(pConfig->cfgValue), "OPEN ") != EOK) {
557 return 0;
558 }
559 }
560 if (alg & 0x2) {
561 if (strcat_s(pConfig->cfgValue, sizeof(pConfig->cfgValue), "OPEN SHARED ") != EOK) {
562 return 0;
563 }
564 }
565 if (alg & 0x4) {
566 if (strcat_s(pConfig->cfgValue, sizeof(pConfig->cfgValue), "LEAP ") != EOK) {
567 return 0;
568 }
569 }
570 return 1;
571 }
572
PushDeviceConfigParseMask(SetNetworkConfig * pConfig,DeviceConfigType type,unsigned int mask,const std::string parseStr[],int size) const573 int WifiHdiWpaClient::PushDeviceConfigParseMask(
574 SetNetworkConfig *pConfig, DeviceConfigType type,
575 unsigned int mask, const std::string parseStr[], int size) const
576 {
577 pConfig->cfgParam = type;
578 for (int i = 0; i < size; i++) {
579 if (mask & (0x1 << i)) {
580 if (strcat_s(pConfig->cfgValue, sizeof(pConfig->cfgValue), parseStr[i].c_str()) != EOK) {
581 return 0;
582 }
583 }
584 }
585 return 1;
586 }
587
CheckValidDeviceConfig(const WifiHalDeviceConfig & config) const588 WifiErrorNo WifiHdiWpaClient::CheckValidDeviceConfig(const WifiHalDeviceConfig &config) const
589 {
590 uint32_t minLen = config.keyMgmt == KEY_MGMT_SAE ? HAL_SAE_PSK_MIN_LENGTH : HAL_PSK_MIN_LENGTH;
591 uint32_t maxLen = HAL_PSK_MAX_LENGTH;
592 if (config.psk.length() > 0) {
593 if (config.psk.length() < minLen || config.psk.length() > maxLen) {
594 return WIFI_HAL_OPT_FAILED;
595 }
596 }
597 if (config.authAlgorithms >= HAL_AUTH_ALGORITHM_MAX) { /* max is 0111 */
598 return WIFI_HAL_OPT_FAILED;
599 }
600 return WIFI_HAL_OPT_OK;
601 }
602
GetNetworkList(std::vector<WifiHalWpaNetworkInfo> & networkList,const char * ifaceName)603 WifiErrorNo WifiHdiWpaClient::GetNetworkList(std::vector<WifiHalWpaNetworkInfo> &networkList, const char *ifaceName)
604 {
605 HdiWifiWpaNetworkInfo *listNetwork = new HdiWifiWpaNetworkInfo[WIFI_MAX_SCAN_COUNT];
606 if (listNetwork == nullptr) {
607 LOGE("WifiHdiWpaClient::%{public}s alloc mem failed", __func__);
608 return WIFI_HAL_OPT_FAILED;
609 }
610 uint32_t size = WIFI_MAX_SCAN_COUNT;
611 if (WIFI_HAL_OPT_OK != HdiWpaListNetworks(listNetwork, &size, ifaceName)) {
612 if (listNetwork != nullptr) {
613 delete[] listNetwork;
614 listNetwork = nullptr;
615 }
616 LOGE("WifiHdiWpaClient::%{public}s failed", __func__);
617 return WIFI_HAL_OPT_FAILED;
618 }
619 for (uint32_t i = 0; i < size; i++) {
620 WifiHalWpaNetworkInfo networkInfo;
621 networkInfo.id = listNetwork[i].id;
622 char szssid[WIFI_HDI_MAX_STR_LENGTH +1] = {0};
623 for (uint32_t j = 0; j < listNetwork[i].ssidLen; j++) {
624 szssid[j] = listNetwork[i].ssid[j];
625 }
626 networkInfo.ssid = szssid;
627 char szBssid[WIFI_HDI_STR_MAC_LENGTH +1] = {0};
628 ConvertMacArr2String(listNetwork[i].bssid, listNetwork[i].bssidLen, szBssid, sizeof(szBssid));
629 networkInfo.bssid = szBssid;
630 char flags[WIFI_HDI_MAX_STR_LENGTH +1] = {0};
631 for (uint32_t j = 0; j < listNetwork[i].flagsLen;j++) {
632 flags[j] = listNetwork[i].flags[j];
633 }
634 networkInfo.flag = flags;
635 networkList.push_back(networkInfo);
636 FreeHdiWifiWpaNetworkInfo(&listNetwork[i]);
637 }
638 if (listNetwork != nullptr) {
639 delete[] listNetwork;
640 listNetwork = nullptr;
641 }
642 return WIFI_HAL_OPT_OK;
643 }
644
GetDeviceConfig(WifiHalGetDeviceConfig & config,const char * ifaceName)645 WifiErrorNo WifiHdiWpaClient::GetDeviceConfig(WifiHalGetDeviceConfig &config, const char *ifaceName)
646 {
647 int32_t networkId = config.networkId;
648 char param[WIFI_HDI_MAX_STR_LENGTH +1] = {0};
649 if (memcpy_s(param, WIFI_HDI_MAX_STR_LENGTH, config.param.c_str(), config.param.length()) != EOK) {
650 LOGE("WifiHdiWpaClient::%{public}s memcpy_s failed", __func__);
651 return WIFI_HAL_OPT_FAILED;
652 }
653 char value[WIFI_HDI_MAX_STR_LENGTH +1] = {0};
654 uint32_t valueLen = WIFI_HDI_MAX_STR_LENGTH;
655 if (WIFI_HAL_OPT_OK != HdiWpaGetNetwork(networkId, param, value, valueLen, ifaceName)) {
656 LOGE("WifiHdiWpaClient::%{public}s failed", __func__);
657 return WIFI_HAL_OPT_FAILED;
658 }
659 config.value = value;
660 return WIFI_HAL_OPT_OK;
661 }
662
StartAp(int id,const std::string & ifaceName)663 WifiErrorNo WifiHdiWpaClient::StartAp(int id, const std::string &ifaceName)
664 {
665 return HdiStartAp(ifaceName.c_str(), id);
666 }
667
StopAp(int id)668 WifiErrorNo WifiHdiWpaClient::StopAp(int id)
669 {
670 return HdiStopAp(id);
671 }
672
RegisterApEvent(IWifiApMonitorEventCallback callback,int id) const673 WifiErrorNo WifiHdiWpaClient::RegisterApEvent(IWifiApMonitorEventCallback callback, int id) const
674 {
675 IHostapdCallback cEventCallback;
676 if (memset_s(&cEventCallback, sizeof(cEventCallback), 0, sizeof(cEventCallback)) != EOK) {
677 return WIFI_HAL_OPT_FAILED;
678 }
679 if (callback.onStaJoinOrLeave != nullptr) {
680 cEventCallback.OnEventStaJoin = onEventStaJoin;
681 cEventCallback.OnEventApState = onEventApState;
682 }
683 return HdiRegisterApEventCallback(&cEventCallback);
684 }
685
AppendStr(std::string & dst,const char * format,va_list args)686 void WifiHdiWpaClient::AppendStr(std::string &dst, const char* format, va_list args)
687 {
688 char space[MAX_CMD_BUFFER_SIZE] __attribute__((__uninitialized__));
689 va_list argsTmp;
690
691 va_copy(argsTmp, args);
692 int result = vsnprintf_s(space, sizeof(space), sizeof(space) - 1, format, argsTmp);
693 va_end(argsTmp);
694 if (result >= 0) {
695 dst.append(space, result);
696 return;
697 }
698 if (result < 0) {
699 LOGE("AppendStr failed");
700 return;
701 }
702 }
703
StringCombination(const char * fmt,...)704 std::string WifiHdiWpaClient::StringCombination(const char* fmt, ...)
705 {
706 va_list args;
707 va_start(args, fmt);
708 std::string result;
709 AppendStr(result, fmt, args);
710 va_end(args);
711 return result;
712 }
713
GetEncryptionString(const HotspotConfig & config,std::string & encryptionString)714 bool WifiHdiWpaClient::GetEncryptionString(const HotspotConfig &config, std::string &encryptionString)
715 {
716 switch (config.GetSecurityType()) {
717 case KeyMgmt::WPA_PSK:
718 encryptionString = StringCombination(
719 "wpa=3\n"
720 "wpa_pairwise=TKIP CCMP\n");
721 break;
722 case KeyMgmt::WPA2_PSK:
723 encryptionString = StringCombination(
724 "wpa=2\n"
725 "rsn_pairwise=CCMP\n");
726 break;
727 default:
728 LOGE("unsupport security type");
729 encryptionString = "";
730 return false;
731 }
732 return true;
733 }
734
GetChannelString(const HotspotConfig & config,std::string & channelString)735 void WifiHdiWpaClient::GetChannelString(const HotspotConfig &config, std::string &channelString)
736 {
737 uint32_t channel = (static_cast<uint32_t>(config.GetChannel()) |
738 (static_cast<uint32_t>(config.GetBandWidth()) << BAND_WIDTH_OFFSET));
739 LOGI("set ap channel mix info is %{public}u", channel);
740 channelString = StringCombination(
741 "channel=%u\n"
742 "op_class=0",
743 channel);
744 }
745
GetModeString(const HotspotConfig & config,std::string & modeString)746 void WifiHdiWpaClient::GetModeString(const HotspotConfig &config, std::string &modeString)
747 {
748 /* configure hardware mode */
749 if (config.GetBand() == BandType::BAND_2GHZ) {
750 modeString = "hw_mode=g";
751 } else if (config.GetBand() == BandType::BAND_5GHZ) {
752 modeString = "hw_mode=a";
753 } else {
754 LOGE("unsupport band");
755 modeString = "";
756 }
757 }
758
WriteConfigToFile(const std::string & fileContext)759 bool WifiHdiWpaClient::WriteConfigToFile(const std::string &fileContext)
760 {
761 std::string destPath = DEFAULT_HOSTAPD_CONF_PATH;
762 std::ofstream file;
763 file.open(destPath, std::ios::out);
764 if (!file.is_open()) {
765 LOGE("hostapd file open failed");
766 return false;
767 }
768 file << fileContext << std::endl;
769 file.close();
770 if (chmod(destPath.c_str(), S_IRUSR | S_IWUSR) != 0) {
771 LOGE("Set file permissions failed: %s", strerror(errno));
772 return false;
773 }
774 return true;
775 }
776
SetSoftApConfig(const std::string & ifName,const HotspotConfig & config,int id)777 WifiErrorNo WifiHdiWpaClient::SetSoftApConfig(const std::string &ifName, const HotspotConfig &config, int id)
778 {
779 std::string ssid2String;
780 std::string encryptionString;
781 std::string channelString;
782 std::vector<uint8_t> ssidUtf8;
783 std::string modeString;
784 std::string fileContext;
785 ssid2String = StringToHex(config.GetSsid());
786 if (!GetEncryptionString(config, encryptionString)) {
787 LOGE("set psk failed");
788 return WIFI_HAL_OPT_FAILED;
789 }
790 GetChannelString(config, channelString);
791 GetModeString(config, modeString);
792 fileContext = StringCombination(
793 "interface=%s\n"
794 "driver=nl80211\n"
795 "ctrl_interface=/data/service/el1/public/wifi/sockets/wpa\n"
796 "ssid2=%s\n"
797 "%s\n"
798 "ieee80211n=1\n"
799 "%s\n"
800 "ignore_broadcast_ssid=0\n"
801 "wowlan_triggers=any\n"
802 "%s\n",
803 ifName.c_str(), ssid2String.c_str(),
804 channelString.c_str(),
805 modeString.c_str(),
806 encryptionString.c_str());
807 if (!WriteConfigToFile(fileContext)) {
808 LOGE("write config failed");
809 return WIFI_HAL_OPT_FAILED;
810 }
811 return WIFI_HAL_OPT_OK;
812 }
813
EnableAp(int id)814 WifiErrorNo WifiHdiWpaClient::EnableAp(int id)
815 {
816 if (HdiEnableAp(id) != WIFI_HAL_OPT_OK) {
817 return WIFI_HAL_OPT_FAILED;
818 }
819 return WIFI_HAL_OPT_OK;
820 }
821
SetApPasswd(const char * pass,int id)822 WifiErrorNo WifiHdiWpaClient::SetApPasswd(const char *pass, int id)
823 {
824 if (HdiSetApPasswd(pass, id) != WIFI_HAL_OPT_OK) {
825 return WIFI_HAL_OPT_FAILED;
826 }
827 return WIFI_HAL_OPT_OK;
828 }
829
GetStationList(std::vector<std::string> & result,int id)830 WifiErrorNo WifiHdiWpaClient::GetStationList(std::vector<std::string> &result, int id)
831 {
832 char *staInfos = new (std::nothrow) char[BUFFER_SIZE]();
833 if (staInfos == nullptr) {
834 return WIFI_HAL_OPT_FAILED;
835 }
836 WifiErrorNo err = HdiGetStaInfos(staInfos, BUFFER_SIZE, id);
837 if (err != WIFI_HAL_OPT_OK) {
838 delete[] staInfos;
839 staInfos = nullptr;
840 return WIFI_HAL_OPT_FAILED;
841 }
842 std::string strStaInfo = std::string(staInfos);
843 SplitString(strStaInfo, ",", result);
844 delete[] staInfos;
845 staInfos = nullptr;
846 return WIFI_HAL_OPT_OK;
847 }
848
AddBlockByMac(const std::string & mac,int id)849 WifiErrorNo WifiHdiWpaClient::AddBlockByMac(const std::string &mac, int id)
850 {
851 if (CheckMacIsValid(mac) != 0) {
852 return WIFI_HAL_OPT_INPUT_MAC_INVALID;
853 }
854 return HdiSetMacFilter(mac.c_str(), id);
855 }
856
DelBlockByMac(const std::string & mac,int id)857 WifiErrorNo WifiHdiWpaClient::DelBlockByMac(const std::string &mac, int id)
858 {
859 if (CheckMacIsValid(mac) != 0) {
860 return WIFI_HAL_OPT_INPUT_MAC_INVALID;
861 }
862 return HdiDelMacFilter(mac.c_str(), id);
863 }
864
RemoveStation(const std::string & mac,int id)865 WifiErrorNo WifiHdiWpaClient::RemoveStation(const std::string &mac, int id)
866 {
867 if (CheckMacIsValid(mac) != 0) {
868 return WIFI_HAL_OPT_INPUT_MAC_INVALID;
869 }
870 return HdiDisassociateSta(mac.c_str(), id);
871 }
872
ReqDisconnectStaByMac(const std::string & mac,int id)873 WifiErrorNo WifiHdiWpaClient::ReqDisconnectStaByMac(const std::string &mac, int id)
874 {
875 if (CheckMacIsValid(mac) != 0) {
876 return WIFI_HAL_OPT_INPUT_MAC_INVALID;
877 }
878 return HdiDisassociateSta(mac.c_str(), id);
879 }
880
ReqP2pStart(const std::string & ifaceName,const bool hasPersisentGroup)881 WifiErrorNo WifiHdiWpaClient::ReqP2pStart(const std::string &ifaceName, const bool hasPersisentGroup)
882 {
883 WifiErrorNo ret = HdiWpaP2pStart(ifaceName.c_str(), hasPersisentGroup);
884 if (ret == WIFI_HAL_OPT_OK) {
885 OnEventP2pStateChanged(P2P_SUPPLICANT_CONNECTED);
886 }
887 return ret;
888 }
889
ReqP2pStop()890 WifiErrorNo WifiHdiWpaClient::ReqP2pStop()
891 {
892 WifiErrorNo ret = HdiWpaP2pStop();
893 if (ret == WIFI_HAL_OPT_OK) {
894 OnEventP2pStateChanged(P2P_SUPPLICANT_DISCONNECTED);
895 }
896 return ret;
897 }
898
ReqP2pSetDeviceName(const std::string & name) const899 WifiErrorNo WifiHdiWpaClient::ReqP2pSetDeviceName(const std::string &name) const
900 {
901 return HdiP2pSetDeviceName(name.c_str());
902 }
903
ReqP2pSetSsidPostfixName(const std::string & postfixName) const904 WifiErrorNo WifiHdiWpaClient::ReqP2pSetSsidPostfixName(const std::string &postfixName) const
905 {
906 return HdiP2pSetSsidPostfixName(postfixName.c_str());
907 }
908
ReqP2pSetWpsDeviceType(const std::string & type) const909 WifiErrorNo WifiHdiWpaClient::ReqP2pSetWpsDeviceType(const std::string &type) const
910 {
911 return HdiP2pSetWpsDeviceType(type.c_str());
912 }
913
ReqP2pSetWpsSecondaryDeviceType(const std::string & type) const914 WifiErrorNo WifiHdiWpaClient::ReqP2pSetWpsSecondaryDeviceType(const std::string &type) const
915 {
916 return HdiP2pSetWpsSecondaryDeviceType(type.c_str());
917 }
918
ReqP2pSetWpsConfigMethods(const std::string & config) const919 WifiErrorNo WifiHdiWpaClient::ReqP2pSetWpsConfigMethods(const std::string &config) const
920 {
921 return HdiP2pSetWpsConfigMethods(config.c_str());
922 }
923
ReqP2pGetDeviceAddress(std::string & deviceAddress) const924 WifiErrorNo WifiHdiWpaClient::ReqP2pGetDeviceAddress(std::string &deviceAddress) const
925 {
926 char address[HAL_P2P_DEV_ADDRESS_LEN] = {0};
927 WifiErrorNo ret = HdiP2pGetDeviceAddress(address, HAL_P2P_DEV_ADDRESS_LEN);
928 if (ret == WIFI_HAL_OPT_OK) {
929 deviceAddress = address;
930 }
931 return ret;
932 }
933
ReqP2pFlush() const934 WifiErrorNo WifiHdiWpaClient::ReqP2pFlush() const
935 {
936 return HdiP2pFlush();
937 }
938
ReqP2pFlushService() const939 WifiErrorNo WifiHdiWpaClient::ReqP2pFlushService() const
940 {
941 return HdiP2pFlushService();
942 }
943
ReqP2pSaveConfig() const944 WifiErrorNo WifiHdiWpaClient::ReqP2pSaveConfig() const
945 {
946 return HdiP2pSaveConfig();
947 }
948
ReqP2pRegisterCallback(const P2pHalCallback & callbacks) const949 WifiErrorNo WifiHdiWpaClient::ReqP2pRegisterCallback(const P2pHalCallback &callbacks) const
950 {
951 struct IWpaCallback cWifiHdiWpaCallback;
952 if (memset_s(&cWifiHdiWpaCallback, sizeof(cWifiHdiWpaCallback), 0, sizeof(cWifiHdiWpaCallback)) != EOK) {
953 return WIFI_HAL_OPT_FAILED;
954 }
955
956 if (callbacks.onConnectSupplicant != nullptr) {
957 cWifiHdiWpaCallback.OnEventDeviceFound = OnEventDeviceFound;
958 cWifiHdiWpaCallback.OnEventDeviceLost = OnEventDeviceLost;
959 cWifiHdiWpaCallback.OnEventGoNegotiationRequest = OnEventGoNegotiationRequest;
960 cWifiHdiWpaCallback.OnEventGoNegotiationCompleted = OnEventGoNegotiationCompleted;
961 cWifiHdiWpaCallback.OnEventInvitationReceived = OnEventInvitationReceived;
962 cWifiHdiWpaCallback.OnEventInvitationResult = OnEventInvitationResult;
963 cWifiHdiWpaCallback.OnEventGroupFormationSuccess = OnEventGroupFormationSuccess;
964 cWifiHdiWpaCallback.OnEventGroupFormationFailure = OnEventGroupFormationFailure;
965 cWifiHdiWpaCallback.OnEventGroupStarted = OnEventGroupStarted;
966 cWifiHdiWpaCallback.OnEventGroupInfoStarted = OnEventGroupInfoStarted;
967 cWifiHdiWpaCallback.OnEventGroupRemoved = OnEventGroupRemoved;
968 cWifiHdiWpaCallback.OnEventProvisionDiscoveryCompleted = OnEventProvisionDiscoveryCompleted;
969 cWifiHdiWpaCallback.OnEventFindStopped = OnEventFindStopped;
970 cWifiHdiWpaCallback.OnEventServDiscReq = OnEventServDiscReq;
971 cWifiHdiWpaCallback.OnEventServDiscResp = OnEventServDiscResp;
972 cWifiHdiWpaCallback.OnEventStaConnectState = OnEventStaConnectState;
973 cWifiHdiWpaCallback.OnEventIfaceCreated = OnEventIfaceCreated;
974 cWifiHdiWpaCallback.OnEventStaNotify = OnEventStaNotify;
975 }
976
977 return RegisterHdiWpaP2pEventCallback(&cWifiHdiWpaCallback);
978 }
979
ReqP2pSetupWpsPbc(const std::string & groupInterface,const std::string & bssid) const980 WifiErrorNo WifiHdiWpaClient::ReqP2pSetupWpsPbc(const std::string &groupInterface, const std::string &bssid) const
981 {
982 return HdiP2pSetupWpsPbc(groupInterface.c_str(), bssid.c_str());
983 }
984
ReqP2pSetupWpsPin(const std::string & groupInterface,const std::string & address,const std::string & pin,std::string & result) const985 WifiErrorNo WifiHdiWpaClient::ReqP2pSetupWpsPin(
986 const std::string &groupInterface, const std::string &address, const std::string &pin, std::string &result) const
987 {
988 if (!pin.empty() && pin.size() != HAL_PIN_CODE_LENGTH) {
989 return WIFI_HAL_OPT_INVALID_PARAM;
990 }
991 char szPinCode[1024] = {0};
992 WifiErrorNo ret = HdiP2pSetupWpsPin(groupInterface.c_str(), address.c_str(), pin.c_str(), szPinCode);
993 if (ret == WIFI_HAL_OPT_OK) {
994 result = szPinCode;
995 }
996 return ret;
997 }
998
ReqP2pRemoveNetwork(int networkId) const999 WifiErrorNo WifiHdiWpaClient::ReqP2pRemoveNetwork(int networkId) const
1000 {
1001 return HdiP2pRemoveNetwork(networkId);
1002 }
1003
ReqP2pListNetworks(std::map<int,WifiP2pGroupInfo> & mapGroups) const1004 WifiErrorNo WifiHdiWpaClient::ReqP2pListNetworks(std::map<int, WifiP2pGroupInfo> &mapGroups) const
1005 {
1006 HdiP2pNetworkList infoList = {0};
1007 WifiErrorNo ret = HdiP2pListNetworks(&infoList);
1008 if (ret != WIFI_HAL_OPT_OK) {
1009 return ret;
1010 }
1011 if (infoList.infos == nullptr) {
1012 return ret;
1013 }
1014 LOGI("ReqP2pListNetworks size=%{public}d", infoList.infoNum);
1015 for (int i = 0; i < infoList.infoNum; ++i) {
1016 WifiP2pGroupInfo groupInfo;
1017 groupInfo.SetNetworkId(infoList.infos[i].id);
1018 if (infoList.infos[i].ssid != nullptr) {
1019 groupInfo.SetGroupName((char *)infoList.infos[i].ssid);
1020 }
1021 char address[18] = {0};
1022 if (infoList.infos[i].bssid != nullptr) {
1023 ConvertMacArr2String(infoList.infos[i].bssid, ETH_ALEN, address, sizeof(address));
1024 }
1025 WifiP2pDevice device;
1026 device.SetDeviceAddress(address);
1027 groupInfo.SetOwner(device);
1028 if (infoList.infos[i].flags != nullptr &&
1029 strstr((char *)infoList.infos[i].flags, "P2P-PERSISTENT") != nullptr) {
1030 groupInfo.SetIsPersistent(true);
1031 }
1032 if (infoList.infos[i].clientList != nullptr) {
1033 const std::string str(reinterpret_cast<const char*>(infoList.infos[i].clientList));
1034 std::vector<std::string> result = StrSplit(str, "-");
1035 for (size_t i = 0; i < result.size(); i++) {
1036 WifiP2pDevice device;
1037 device.SetDeviceAddress(result[i]);
1038 groupInfo.AddPersistentDevice(device);
1039 }
1040 }
1041 mapGroups.insert(std::pair<int, WifiP2pGroupInfo>(infoList.infos[i].id, groupInfo));
1042 LOGI("ReqP2pListNetworks id=%{public}d, address=%{private}s", infoList.infos[i].id, address);
1043 }
1044 FreeHdiP2pNetworkList(&infoList);
1045 return ret;
1046 }
1047
ReqP2pSetGroupMaxIdle(const std::string & groupInterface,size_t time) const1048 WifiErrorNo WifiHdiWpaClient::ReqP2pSetGroupMaxIdle(const std::string &groupInterface, size_t time) const
1049 {
1050 return HdiP2pSetGroupMaxIdle(groupInterface.c_str(), time);
1051 }
1052
ReqP2pSetPowerSave(const std::string & groupInterface,bool enable) const1053 WifiErrorNo WifiHdiWpaClient::ReqP2pSetPowerSave(const std::string &groupInterface, bool enable) const
1054 {
1055 int flag = enable;
1056 return HdiP2pSetPowerSave(groupInterface.c_str(), flag);
1057 }
1058
ReqP2pSetWfdEnable(bool enable) const1059 WifiErrorNo WifiHdiWpaClient::ReqP2pSetWfdEnable(bool enable) const
1060 {
1061 int flag = enable;
1062 return HdiP2pSetWfdEnable(flag);
1063 }
1064
ReqP2pSetWfdDeviceConfig(const std::string & config) const1065 WifiErrorNo WifiHdiWpaClient::ReqP2pSetWfdDeviceConfig(const std::string &config) const
1066 {
1067 return HdiP2pSetWfdDeviceConfig(config.c_str());
1068 }
1069
ReqP2pStartFind(size_t timeout) const1070 WifiErrorNo WifiHdiWpaClient::ReqP2pStartFind(size_t timeout) const
1071 {
1072 return HdiP2pStartFind(timeout);
1073 }
1074
ReqP2pStopFind() const1075 WifiErrorNo WifiHdiWpaClient::ReqP2pStopFind() const
1076 {
1077 return HdiP2pStopFind();
1078 }
1079
ReqP2pSetExtListen(bool enable,size_t period,size_t interval) const1080 WifiErrorNo WifiHdiWpaClient::ReqP2pSetExtListen(bool enable, size_t period, size_t interval) const
1081 {
1082 if (enable) {
1083 if (period < HAL_P2P_LISTEN_MIN_TIME || period > HAL_P2P_LISTEN_MAX_TIME ||
1084 interval < HAL_P2P_LISTEN_MIN_TIME || interval > HAL_P2P_LISTEN_MAX_TIME || period > interval) {
1085 return WIFI_HAL_OPT_INVALID_PARAM;
1086 }
1087 }
1088 int flag = enable;
1089 return HdiP2pSetExtListen(flag, period, interval);
1090 }
1091
ReqP2pSetListenChannel(size_t channel,unsigned char regClass) const1092 WifiErrorNo WifiHdiWpaClient::ReqP2pSetListenChannel(size_t channel, unsigned char regClass) const
1093 {
1094 return HdiP2pSetListenChannel(channel, regClass);
1095 }
1096
ReqP2pConnect(const WifiP2pConfigInternal & config,bool isJoinExistingGroup,std::string & pin) const1097 WifiErrorNo WifiHdiWpaClient::ReqP2pConnect(const WifiP2pConfigInternal &config, bool isJoinExistingGroup,
1098 std::string &pin) const
1099 {
1100 LOGI("ReqP2pConnect");
1101 P2pConnectInfo info = {0};
1102 info.mode = isJoinExistingGroup;
1103 info.persistent = config.GetNetId();
1104 if (isJoinExistingGroup) {
1105 info.goIntent = 0;
1106 } else {
1107 info.goIntent = config.GetGroupOwnerIntent();
1108 }
1109 if (info.goIntent < HAL_P2P_GO_INTENT_MIN_LENGTH || info.goIntent > HAL_P2P_GO_INTENT_MAX_LENGTH) {
1110 info.goIntent = HAL_P2P_GO_INTENT_DEFAULT_LENGTH;
1111 }
1112 std::string address = config.GetDeviceAddress();
1113 if (address.size() < HAL_BSSID_LENGTH) {
1114 LOGI("ReqP2pConnect Device Address is too short");
1115 return WIFI_HAL_OPT_INVALID_PARAM;
1116 }
1117 WpsMethod mode = config.GetWpsInfo().GetWpsMethod();
1118 if (mode == WpsMethod::WPS_METHOD_LABEL) {
1119 mode = WpsMethod::WPS_METHOD_KEYPAD;
1120 }
1121 info.provdisc = (int)mode;
1122 std::string pinCode = config.GetWpsInfo().GetPin();
1123 if (mode == WpsMethod::WPS_METHOD_PBC && !pinCode.empty()) {
1124 LOGI("ReqP2pConnect Expected empty pin for PBC.");
1125 return WIFI_HAL_OPT_INVALID_PARAM;
1126 }
1127 if (strncpy_s(info.peerDevAddr, sizeof(info.peerDevAddr), address.c_str(), address.length()) != EOK ||
1128 strncpy_s(info.pin, sizeof(info.pin), pinCode.c_str(), pinCode.length()) != EOK) {
1129 LOGI("ReqP2pConnect failed");
1130 return WIFI_HAL_OPT_FAILED;
1131 }
1132 char resPin[HAL_PIN_CODE_LENGTH + 1] = {0};
1133 WifiErrorNo ret = HdiP2pConnect(&info, resPin, HAL_PIN_CODE_LENGTH + 1);
1134 if (ret == WIFI_HAL_OPT_OK) {
1135 pin = resPin;
1136 }
1137 return ret;
1138 }
1139
ReqP2pCancelConnect() const1140 WifiErrorNo WifiHdiWpaClient::ReqP2pCancelConnect() const
1141 {
1142 return HdiP2pCancelConnect();
1143 }
1144
ReqP2pProvisionDiscovery(const WifiP2pConfigInternal & config) const1145 WifiErrorNo WifiHdiWpaClient::ReqP2pProvisionDiscovery(const WifiP2pConfigInternal &config) const
1146 {
1147 WpsMethod mode = config.GetWpsInfo().GetWpsMethod();
1148 if (mode == WpsMethod::WPS_METHOD_LABEL) {
1149 mode = WpsMethod::WPS_METHOD_DISPLAY;
1150 } else if (mode == WpsMethod::WPS_METHOD_DISPLAY) {
1151 mode = WpsMethod::WPS_METHOD_KEYPAD;
1152 } else if (mode == WpsMethod::WPS_METHOD_KEYPAD) {
1153 mode = WpsMethod::WPS_METHOD_DISPLAY;
1154 } else if (mode != WpsMethod::WPS_METHOD_PBC) {
1155 return WIFI_HAL_OPT_FAILED;
1156 }
1157 return HdiP2pProvisionDiscovery(config.GetDeviceAddress().c_str(), static_cast<int>(mode));
1158 }
1159
ReqP2pAddGroup(bool isPersistent,int networkId,int freq) const1160 WifiErrorNo WifiHdiWpaClient::ReqP2pAddGroup(bool isPersistent, int networkId, int freq) const
1161 {
1162 int flag = isPersistent;
1163 return HdiP2pAddGroup(flag, networkId, freq);
1164 }
1165
ReqP2pRemoveGroup(const std::string & groupInterface) const1166 WifiErrorNo WifiHdiWpaClient::ReqP2pRemoveGroup(const std::string &groupInterface) const
1167 {
1168 return HdiP2pRemoveGroup(groupInterface.c_str());
1169 }
1170
ReqP2pRemoveGroupClient(const std::string & deviceMac,const std::string & ifName) const1171 WifiErrorNo WifiHdiWpaClient::ReqP2pRemoveGroupClient(const std::string &deviceMac, const std::string &ifName) const
1172 {
1173 return HdiP2pRemoveGroupClient(deviceMac.c_str(), ifName.c_str());
1174 }
1175
ReqP2pInvite(const WifiP2pGroupInfo & group,const std::string & deviceAddr) const1176 WifiErrorNo WifiHdiWpaClient::ReqP2pInvite(const WifiP2pGroupInfo &group, const std::string &deviceAddr) const
1177 {
1178 return HdiP2pInvite(deviceAddr.c_str(), group.GetOwner().GetDeviceAddress().c_str(),
1179 group.GetInterface().c_str());
1180 }
1181
ReqP2pReinvoke(int networkId,const std::string & deviceAddr) const1182 WifiErrorNo WifiHdiWpaClient::ReqP2pReinvoke(int networkId, const std::string &deviceAddr) const
1183 {
1184 LOGI("HdiP2pReinvoke networkId=%{public}d, bssid=%{public}s", networkId, MacAnonymize(deviceAddr).c_str());
1185 return HdiP2pReinvoke(networkId, deviceAddr.c_str());
1186 }
1187
ReqP2pGetGroupCapability(const std::string & deviceAddress,uint32_t & cap) const1188 WifiErrorNo WifiHdiWpaClient::ReqP2pGetGroupCapability(const std::string &deviceAddress, uint32_t &cap) const
1189 {
1190 int capacity = 0;
1191 WifiErrorNo ret = HdiP2pGetGroupCapability(deviceAddress.c_str(), capacity);
1192 if (ret == WIFI_HAL_OPT_OK) {
1193 cap = capacity;
1194 }
1195 return ret;
1196 }
1197
ReqP2pAddService(const WifiP2pServiceInfo & info) const1198 WifiErrorNo WifiHdiWpaClient::ReqP2pAddService(const WifiP2pServiceInfo &info) const
1199 {
1200 WifiErrorNo ret = WIFI_HAL_OPT_OK;
1201 HdiP2pServiceInfo servInfo = {0};
1202 std::vector<std::string> queryList = info.GetQueryList();
1203 for (auto iter = queryList.begin(); iter != queryList.end(); iter++) {
1204 std::vector<std::string> vec;
1205 SplitString(*iter, " ", vec);
1206 if (vec.size() < HAL_P2P_SERVICE_TYPE_MIN_SIZE) {
1207 return WIFI_HAL_OPT_FAILED;
1208 }
1209 if (memset_s(&servInfo, sizeof(servInfo), 0, sizeof(servInfo)) != EOK) {
1210 return WIFI_HAL_OPT_FAILED;
1211 }
1212 const std::string &tmp = vec[HAL_P2P_SERVICE_TYPE_2_POS];
1213 if (vec[0] == "upnp") {
1214 servInfo.mode = 0;
1215 servInfo.version = atoi(vec[1].c_str());
1216 if (strncpy_s((char *)servInfo.name, sizeof(servInfo.name), tmp.c_str(), tmp.length()) != EOK) {
1217 return WIFI_HAL_OPT_FAILED;
1218 }
1219 ret = HdiP2pAddService(&servInfo);
1220 } else if (vec[0] == "bonjour") {
1221 servInfo.mode = 1;
1222 if (strncpy_s((char *)servInfo.query, sizeof(servInfo.query), vec[1].c_str(), vec[1].length()) != EOK ||
1223 strncpy_s((char *)servInfo.resp, sizeof(servInfo.resp), tmp.c_str(), tmp.length()) != EOK) {
1224 return WIFI_HAL_OPT_FAILED;
1225 }
1226 ret = HdiP2pAddService(&servInfo);
1227 } else {
1228 ret = WIFI_HAL_OPT_FAILED;
1229 }
1230 if (ret != WIFI_HAL_OPT_OK) {
1231 break;
1232 }
1233 }
1234 return ret;
1235 }
1236
ReqP2pRemoveService(const WifiP2pServiceInfo & info) const1237 WifiErrorNo WifiHdiWpaClient::ReqP2pRemoveService(const WifiP2pServiceInfo &info) const
1238 {
1239 WifiErrorNo ret = WIFI_HAL_OPT_OK;
1240 HdiP2pServiceInfo servInfo = {0};
1241 std::vector<std::string> queryList = info.GetQueryList();
1242 for (auto iter = queryList.begin(); iter != queryList.end(); iter++) {
1243 std::vector<std::string> vec;
1244 SplitString(*iter, " ", vec);
1245 if (vec.size() < HAL_P2P_SERVICE_TYPE_MIN_SIZE) {
1246 return WIFI_HAL_OPT_FAILED;
1247 }
1248 if (memset_s(&servInfo, sizeof(servInfo), 0, sizeof(servInfo)) != EOK) {
1249 return WIFI_HAL_OPT_FAILED;
1250 }
1251 const std::string &tmp = vec[HAL_P2P_SERVICE_TYPE_2_POS];
1252 if (vec[0] == "upnp") {
1253 servInfo.mode = 0;
1254 servInfo.version = atoi(vec[1].c_str());
1255 if (strncpy_s((char *)servInfo.name, sizeof(servInfo.name), tmp.c_str(), tmp.length()) != EOK) {
1256 return WIFI_HAL_OPT_FAILED;
1257 }
1258 ret = HdiP2pRemoveService(&servInfo);
1259 } else if (vec[0] == "bonjour") {
1260 servInfo.mode = 1;
1261 if (strncpy_s((char *)servInfo.query, sizeof(servInfo.query), vec[1].c_str(), vec[1].length()) != EOK) {
1262 return WIFI_HAL_OPT_FAILED;
1263 }
1264 ret = HdiP2pRemoveService(&servInfo);
1265 } else {
1266 ret = WIFI_HAL_OPT_FAILED;
1267 }
1268 if (ret != WIFI_HAL_OPT_OK) {
1269 break;
1270 }
1271 }
1272 return ret;
1273 }
1274
ReqP2pReqServiceDiscovery(const std::string & deviceAddress,const std::vector<unsigned char> & tlvs,std::string & reqID) const1275 WifiErrorNo WifiHdiWpaClient::ReqP2pReqServiceDiscovery(
1276 const std::string &deviceAddress, const std::vector<unsigned char> &tlvs, std::string &reqID) const
1277 {
1278 if (deviceAddress.size() != HAL_BSSID_LENGTH || tlvs.empty()) {
1279 return WIFI_HAL_OPT_INVALID_PARAM;
1280 }
1281 unsigned size = (tlvs.size() << 1) + 1;
1282 char *pTlvs = (char *)calloc(size, sizeof(char));
1283 if (pTlvs == nullptr || Val2HexChar(tlvs, pTlvs, size) < 0) {
1284 free(pTlvs);
1285 pTlvs = nullptr;
1286 return WIFI_HAL_OPT_FAILED;
1287 }
1288 struct HdiP2pReqService wpsParam = {0};
1289 wpsParam.bssid = (unsigned char *)deviceAddress.c_str();
1290 wpsParam.msg = (unsigned char *)pTlvs;
1291 char retBuf[HAL_P2P_TMP_BUFFER_SIZE_128] = {0};
1292 WifiErrorNo ret = HdiP2pReqServiceDiscovery(&wpsParam, retBuf, HAL_P2P_TMP_BUFFER_SIZE_128);
1293 if (ret == WIFI_HAL_OPT_OK) {
1294 reqID = retBuf;
1295 }
1296 free(pTlvs);
1297 pTlvs = nullptr;
1298 return ret;
1299 }
1300
ReqP2pCancelServiceDiscovery(const std::string & id) const1301 WifiErrorNo WifiHdiWpaClient::ReqP2pCancelServiceDiscovery(const std::string &id) const
1302 {
1303 return HdiP2pCancelServiceDiscovery(id.c_str());
1304 }
1305
ReqP2pSetRandomMac(bool enable) const1306 WifiErrorNo WifiHdiWpaClient::ReqP2pSetRandomMac(bool enable) const
1307 {
1308 return HdiP2pSetRandomMac(enable ? 1 : 0);
1309 }
1310
ReqP2pSetMiracastType(int type) const1311 WifiErrorNo WifiHdiWpaClient::ReqP2pSetMiracastType(int type) const
1312 {
1313 return WIFI_HAL_OPT_FAILED;
1314 }
1315
ReqSetPersistentReconnect(int mode) const1316 WifiErrorNo WifiHdiWpaClient::ReqSetPersistentReconnect(int mode) const
1317 {
1318 return HdiP2pSetPersistentReconnect(mode);
1319 }
1320
ReqRespServiceDiscovery(const WifiP2pDevice & device,int frequency,int dialogToken,const std::vector<unsigned char> & tlvs) const1321 WifiErrorNo WifiHdiWpaClient::ReqRespServiceDiscovery(
1322 const WifiP2pDevice &device, int frequency, int dialogToken, const std::vector<unsigned char> &tlvs) const
1323 {
1324 if (tlvs.empty()) {
1325 return WIFI_HAL_OPT_INVALID_PARAM;
1326 }
1327 unsigned size = (tlvs.size() << 1) + 1;
1328 char *pTlvs = (char *)calloc(size, sizeof(char));
1329 if (pTlvs == nullptr || Val2HexChar(tlvs, pTlvs, size) < 0) {
1330 if (pTlvs != nullptr) {
1331 free(pTlvs);
1332 pTlvs = nullptr;
1333 }
1334 return WIFI_HAL_OPT_FAILED;
1335 }
1336 struct HdiP2pServDiscReqInfo wpsParam = {0};
1337 wpsParam.freq = frequency;
1338 wpsParam.dialogToken = dialogToken;
1339 wpsParam.mac = (unsigned char *)device.GetDeviceAddress().c_str();
1340 wpsParam.tlvs = (unsigned char *)pTlvs;
1341 WifiErrorNo ret = HdiP2pRespServerDiscovery(&wpsParam);
1342 free(pTlvs);
1343 pTlvs = nullptr;
1344 return ret;
1345 }
1346
ReqSetServiceDiscoveryExternal(bool isExternalProcess) const1347 WifiErrorNo WifiHdiWpaClient::ReqSetServiceDiscoveryExternal(bool isExternalProcess) const
1348 {
1349 return HdiP2pSetServDiscExternal(isExternalProcess);
1350 }
1351
ReqGetP2pPeer(const std::string & deviceAddress,WifiP2pDevice & device) const1352 WifiErrorNo WifiHdiWpaClient::ReqGetP2pPeer(const std::string &deviceAddress, WifiP2pDevice &device) const
1353 {
1354 HdiP2pDeviceInfo peerInfo;
1355 if (memset_s(&peerInfo, sizeof(peerInfo), 0, sizeof(peerInfo)) != EOK) {
1356 return WIFI_HAL_OPT_FAILED;
1357 }
1358 WifiErrorNo ret = HdiP2pGetPeer(deviceAddress.c_str(), &peerInfo);
1359 if (ret == WIFI_HAL_OPT_OK) {
1360 device.SetDeviceAddress((char *)peerInfo.p2pDeviceAddress);
1361 device.SetDeviceName((char *)peerInfo.deviceName);
1362 device.SetPrimaryDeviceType((char *)peerInfo.primaryDeviceType);
1363 device.SetWpsConfigMethod(peerInfo.configMethods);
1364 device.SetDeviceCapabilitys(peerInfo.deviceCapabilities);
1365 device.SetGroupCapabilitys(peerInfo.groupCapabilities);
1366 device.SetNetworkName((char *)peerInfo.operSsid);
1367 }
1368 FreeHdiP2pDeviceInfo(&peerInfo);
1369 return ret;
1370 }
1371
ReqP2pGetSupportFrequencies(int band,std::vector<int> & frequencies) const1372 WifiErrorNo WifiHdiWpaClient::ReqP2pGetSupportFrequencies(int band, std::vector<int> &frequencies) const
1373 {
1374 return WIFI_HAL_OPT_FAILED;
1375 }
1376
ReqP2pSetSingleConfig(int networkId,const std::string & key,const std::string & value) const1377 WifiErrorNo WifiHdiWpaClient::ReqP2pSetSingleConfig(int networkId,
1378 const std::string &key, const std::string &value) const
1379 {
1380 LOGI("WifiHdiWpaClient::%{public}s enter, key=%{public}s", __func__, key.c_str());
1381 return HdiP2pSetSingleConfig(networkId, key.c_str(), value.c_str());
1382 }
1383
ReqP2pSetGroupConfig(int networkId,const HalP2pGroupConfig & config) const1384 WifiErrorNo WifiHdiWpaClient::ReqP2pSetGroupConfig(int networkId, const HalP2pGroupConfig &config) const
1385 {
1386 P2pGroupConfig conf[GROUP_CONFIG_END_POS];
1387 if (memset_s(conf, sizeof(conf), 0, sizeof(conf)) != EOK) {
1388 return WIFI_HAL_OPT_FAILED;
1389 }
1390 int num = 0;
1391 num += PushP2pGroupConfigString(conf + num, GROUP_CONFIG_SSID, "\"" + config.ssid + "\"");
1392 num += PushP2pGroupConfigString(conf + num, GROUP_CONFIG_BSSID, config.bssid);
1393 // If the PSK length is less than 8 or greater than 63, Do not set this psk field.
1394 if (config.psk.length() >= HAL_PSK_MIN_LENGTH && config.psk.length() < HAL_PSK_MAX_LENGTH) {
1395 std::string tmp = "\"" + config.psk + "\"";
1396 num += PushP2pGroupConfigString(conf + num, GROUP_CONFIG_PSK, tmp);
1397 } else if (config.psk.length() == HAL_PSK_MAX_LENGTH) {
1398 num += PushP2pGroupConfigString(conf + num, GROUP_CONFIG_PSK, config.psk);
1399 }
1400 num += PushP2pGroupConfigString(conf + num, GROUP_CONFIG_PROTO, config.proto);
1401 num += PushP2pGroupConfigString(conf + num, GROUP_CONFIG_KEY_MGMT, config.keyMgmt);
1402 num += PushP2pGroupConfigString(conf + num, GROUP_CONFIG_PAIRWISE, config.pairwise);
1403 num += PushP2pGroupConfigString(conf + num, GROUP_CONFIG_AUTH_ALG, config.authAlg);
1404 num += PushP2pGroupConfigString(conf + num, GROUP_CONFIG_CLIENTLIST, config.clientList);
1405
1406 num += PushP2pGroupConfigInt(conf + num, GROUP_CONFIG_MODE, config.mode);
1407 num += PushP2pGroupConfigInt(conf + num, GROUP_CONFIG_DISABLED, config.disabled);
1408 if (num == 0) {
1409 return WIFI_HAL_OPT_OK;
1410 }
1411 LOGI("WifiHdiWpaClient::%{public}s enter, mode=%{public}d", __func__, config.mode);
1412 return HdiP2pSetGroupConfig(networkId, conf, num);
1413 }
1414
PushP2pGroupConfigString(P2pGroupConfig * pConfig,P2pGroupConfigType type,const std::string & str) const1415 int WifiHdiWpaClient::PushP2pGroupConfigString(
1416 P2pGroupConfig *pConfig, P2pGroupConfigType type, const std::string &str) const
1417 {
1418 if (str.length() > 0) {
1419 pConfig->cfgParam = type;
1420 if (strncpy_s(pConfig->cfgValue, sizeof(pConfig->cfgValue), str.c_str(), str.length()) != EOK) {
1421 return 0;
1422 }
1423 return 1;
1424 } else {
1425 return 0;
1426 }
1427 }
1428
PushP2pGroupConfigInt(P2pGroupConfig * pConfig,P2pGroupConfigType type,int i) const1429 int WifiHdiWpaClient::PushP2pGroupConfigInt(P2pGroupConfig *pConfig, P2pGroupConfigType type, int i) const
1430 {
1431 pConfig->cfgParam = type;
1432 if (snprintf_s(pConfig->cfgValue, sizeof(pConfig->cfgValue), sizeof(pConfig->cfgValue) - 1, "%d", i) < 0) {
1433 return 0;
1434 }
1435 return 1;
1436 }
1437
ReqP2pGetGroupConfig(int networkId,HalP2pGroupConfig & config) const1438 WifiErrorNo WifiHdiWpaClient::ReqP2pGetGroupConfig(int networkId, HalP2pGroupConfig &config) const
1439 {
1440 char ssid[] = "ssid";
1441 char cfgValue[WIFI_P2P_GROUP_CONFIG_VALUE_LENGTH];
1442 if (HdiP2pGetGroupConfig(networkId, ssid, cfgValue) != 0) {
1443 return WIFI_HAL_OPT_FAILED;
1444 }
1445 config.ssid = cfgValue;
1446 return WIFI_HAL_OPT_OK;
1447 }
1448
ReqP2pAddNetwork(int & networkId) const1449 WifiErrorNo WifiHdiWpaClient::ReqP2pAddNetwork(int &networkId) const
1450 {
1451 return HdiP2pAddNetwork(&networkId);
1452 }
1453
ReqP2pHid2dConnect(const Hid2dConnectConfig & config) const1454 WifiErrorNo WifiHdiWpaClient::ReqP2pHid2dConnect(const Hid2dConnectConfig &config) const
1455 {
1456 Hid2dConnectInfo info;
1457 if (memset_s(&info, sizeof(info), 0, sizeof(info)) != EOK) {
1458 return WIFI_HAL_OPT_FAILED;
1459 }
1460 if (strncpy_s(info.ssid, sizeof(info.ssid), config.GetSsid().c_str(), config.GetSsid().length()) != EOK) {
1461 return WIFI_HAL_OPT_FAILED;
1462 }
1463 if (strncpy_s(info.bssid, sizeof(info.bssid), config.GetBssid().c_str(),
1464 config.GetBssid().length()) != EOK) {
1465 return WIFI_HAL_OPT_FAILED;
1466 }
1467 if (strncpy_s(info.passphrase, sizeof(info.passphrase),
1468 config.GetPreSharedKey().c_str(), config.GetPreSharedKey().length()) != EOK) {
1469 return WIFI_HAL_OPT_FAILED;
1470 }
1471 info.frequency = config.GetFrequency();
1472 if (config.GetDhcpMode() == DhcpMode::CONNECT_AP_DHCP ||
1473 config.GetDhcpMode() == DhcpMode::CONNECT_AP_NODHCP) {
1474 info.isLegacyGo = 1;
1475 } else {
1476 info.isLegacyGo = 0;
1477 }
1478 WifiErrorNo ret = HdiP2pHid2dConnect(&info);
1479 return ret;
1480 }
1481
DeliverP2pData(int32_t cmdType,int32_t dataType,const std::string & carryData) const1482 WifiErrorNo WifiHdiWpaClient::DeliverP2pData(int32_t cmdType, int32_t dataType, const std::string& carryData) const
1483 {
1484 return HdiDeliverP2pData(cmdType, dataType, carryData.c_str());
1485 }
1486
ReqRegisterNativeProcessCallback(const std::function<void (int)> & callback) const1487 WifiErrorNo WifiHdiWpaClient::ReqRegisterNativeProcessCallback(const std::function<void(int)> &callback) const
1488 {
1489 if (callback) {
1490 return HdiSetNativeProcessCallback(OnNativeProcessDeath);
1491 }
1492 return WIFI_HAL_OPT_FAILED;
1493 }
1494
HandleMloLinkData(char * staData,uint32_t staDataLen,std::vector<WifiLinkedInfo> & mloLinkInfo)1495 WifiErrorNo WifiHdiWpaClient::HandleMloLinkData(char *staData, uint32_t staDataLen,
1496 std::vector<WifiLinkedInfo> &mloLinkInfo)
1497 {
1498 if (staData == NULL || staDataLen == 0) {
1499 LOGE("%{public}s: hdiInfo null or length err copy", __func__);
1500 return WIFI_HAL_OPT_FAILED;
1501 }
1502 char *savedPtr = NULL;
1503 std::string value = "";
1504 char *token = strtok_r(staData, "=", &savedPtr);
1505 WifiLinkedInfo linkInfo;
1506 while (token != NULL) {
1507 char* tmpValue = strtok_r(NULL, "\n", &savedPtr);
1508 if (!tmpValue) {
1509 LOGE("%{public}s: tmpValue is null", __func__);
1510 return WIFI_HAL_OPT_FAILED;
1511 }
1512 value = tmpValue;
1513 if (strcmp(token, "freq") == 0) {
1514 linkInfo.frequency = CheckDataLegal(value);
1515 } else if (strcmp(token, "link_id") == 0) {
1516 linkInfo.linkId = CheckDataLegal(value);
1517 } else if (strcmp(token, "ap_link_addr") == 0) {
1518 linkInfo.bssid = value;
1519 } else if (strcmp(token, "sta_link_addr") == 0) {
1520 linkInfo.macAddress = value;
1521 mloLinkInfo.push_back(linkInfo);
1522 }
1523 token = strtok_r(NULL, "=", &savedPtr);
1524 }
1525 if (mloLinkInfo.size() != WIFI_MAX_MLO_LINK_NUM) {
1526 LOGE("%{public}s: err link num", __func__);
1527 mloLinkInfo.clear();
1528 return WIFI_HAL_OPT_FAILED;
1529 }
1530 return WIFI_HAL_OPT_OK;
1531 }
1532
GetMloLinkedInfo(const std::string & ifName,std::vector<WifiLinkedInfo> & mloLinkInfo)1533 WifiErrorNo WifiHdiWpaClient::GetMloLinkedInfo(const std::string &ifName,
1534 std::vector<WifiLinkedInfo> &mloLinkInfo)
1535 {
1536 char ifNameBuf[MAX_IFACENAME_LEN];
1537 char staParam[] = "MLO_STATUS";
1538 char staData[WIFI_MAX_WPA_STA_BUF_SIZE] = {0};
1539 uint32_t staDataLen = WIFI_MAX_WPA_STA_BUF_SIZE;
1540 if (strncpy_s(ifNameBuf, sizeof(ifNameBuf), ifName.c_str(), ifName.length()) != EOK) {
1541 LOGE("%{public}s: failed to copy", __func__);
1542 return WIFI_HAL_OPT_FAILED;
1543 }
1544
1545 if (HdiWpaGetMloLinkedInfo(ifNameBuf, staParam, staData, staDataLen) != WIFI_HAL_OPT_OK) {
1546 LOGE("%{public}s: failed", __func__);
1547 return WIFI_HAL_OPT_FAILED;
1548 }
1549
1550 if (HandleMloLinkData(staData, staDataLen, mloLinkInfo) != WIFI_HAL_OPT_OK) {
1551 LOGE("%{public}s: HandleMloLinkData failed", __func__);
1552 return WIFI_HAL_OPT_FAILED;
1553 }
1554 return WIFI_HAL_OPT_OK;
1555 }
1556
GetMloSignalPollInfo(const std::string & ifName,std::vector<WifiMloSignalInfo> & mloSignalInfo)1557 WifiErrorNo WifiHdiWpaClient::GetMloSignalPollInfo(const std::string &ifName,
1558 std::vector<WifiMloSignalInfo> &mloSignalInfo)
1559 {
1560 char ifNameBuf[MAX_IFACENAME_LEN];
1561 char staParam[] = "MLO_SIGNAL_POLL";
1562 char staData[WIFI_MAX_WPA_STA_BUF_SIZE] = {0};
1563 uint32_t staDataLen = WIFI_MAX_WPA_STA_BUF_SIZE;
1564 if (strncpy_s(ifNameBuf, sizeof(ifNameBuf), ifName.c_str(), ifName.length()) != EOK) {
1565 LOGE("%{public}s: failed to copy", __func__);
1566 return WIFI_HAL_OPT_FAILED;
1567 }
1568 if (HdiWpaGetMloLinkedInfo(ifNameBuf, staParam, staData, staDataLen) != WIFI_HAL_OPT_OK) {
1569 LOGE("%{public}s: failed", __func__);
1570 return WIFI_HAL_OPT_FAILED;
1571 }
1572
1573 if (HandleMloSignalPollData(staData, staDataLen, mloSignalInfo) != WIFI_HAL_OPT_OK) {
1574 LOGE("%{public}s: HandleMloLinkData failed", __func__);
1575 return WIFI_HAL_OPT_FAILED;
1576 }
1577 return WIFI_HAL_OPT_OK;
1578 }
1579
HandleMloSignalPollData(char * staData,uint32_t staDataLen,std::vector<WifiMloSignalInfo> & mloSignalInfo)1580 WifiErrorNo WifiHdiWpaClient::HandleMloSignalPollData(char *staData, uint32_t staDataLen,
1581 std::vector<WifiMloSignalInfo> &mloSignalInfo)
1582 {
1583 if (staData == NULL || staDataLen == 0) {
1584 LOGE("%{public}s: hdiInfo null or length err copy", __func__);
1585 return WIFI_HAL_OPT_FAILED;
1586 }
1587 char *savedPtr = NULL;
1588 std::string value = "";
1589 char *token = strtok_r(staData, "=", &savedPtr);
1590 WifiMloSignalInfo signalInfo;
1591 while (token != NULL) {
1592 char* tmpValue = strtok_r(NULL, "\n", &savedPtr);
1593 if (!tmpValue) {
1594 LOGE("%{public}s: tmpValue is null", __func__);
1595 return WIFI_HAL_OPT_FAILED;
1596 }
1597 value = tmpValue;
1598 if (strcmp(token, "LINK_ID") == 0) {
1599 signalInfo.linkId = CheckDataLegal(value);
1600 } else if (strcmp(token, "RSSI") == 0) {
1601 signalInfo.rssi = CheckDataLegal(value);
1602 } else if (strcmp(token, "FREQUENCY") == 0) {
1603 signalInfo.frequency = CheckDataLegal(value);
1604 } else if (strcmp(token, "TXLINKSPEED") == 0) {
1605 signalInfo.txLinkSpeed = CheckDataLegal(value);
1606 } else if (strcmp(token, "RXLINKSPEED") == 0) {
1607 signalInfo.rxLinkSpeed = CheckDataLegal(value);
1608 } else if (strcmp(token, "TXPACKETS") == 0) {
1609 signalInfo.txPackets = CheckDataLegal(value);
1610 } else if (strcmp(token, "RXPACKETS") == 0) {
1611 signalInfo.rxPackets = CheckDataLegal(value);
1612 mloSignalInfo.push_back(signalInfo);
1613 }
1614 token = strtok_r(NULL, "=", &savedPtr);
1615 }
1616 if (mloSignalInfo.size() != WIFI_MAX_MLO_LINK_NUM) {
1617 LOGE("%{public}s: invalid link num: %{public}zu", __func__, mloSignalInfo.size());
1618 mloSignalInfo.clear();
1619 return WIFI_HAL_OPT_FAILED;
1620 }
1621 return WIFI_HAL_OPT_OK;
1622 }
1623
P2pReject(const std::string & mac)1624 WifiErrorNo WifiHdiWpaClient::P2pReject(const std::string &mac)
1625 {
1626 return HdiP2pReject(mac.c_str());
1627 }
1628
SetMiracastSinkConfig(const std::string & config)1629 WifiErrorNo WifiHdiWpaClient::SetMiracastSinkConfig(const std::string& config)
1630 {
1631 char configBuf[MAX_CMD_BUFFER_SIZE];
1632 if (strncpy_s(configBuf, sizeof(configBuf), config.c_str(), config.length()) != EOK) {
1633 LOGE("%{public}s: failed to copy", __func__);
1634 return WIFI_HAL_OPT_FAILED;
1635 }
1636 return HdiSetMiracastSinkConfig(configBuf);
1637 }
1638
P2pSetTempConfig(int networkId,const HalP2pGroupConfig & config) const1639 WifiErrorNo WifiHdiWpaClient::P2pSetTempConfig(int networkId, const HalP2pGroupConfig &config) const
1640 {
1641 P2pGroupConfig conf[GROUP_CONFIG_END_POS];
1642 if (memset_s(conf, sizeof(conf), 0, sizeof(conf)) != EOK) {
1643 return WIFI_HAL_OPT_FAILED;
1644 }
1645 int num = 0;
1646 num += PushP2pGroupConfigString(conf + num, GROUP_CONFIG_SSID, config.ssid);
1647 // If the PSK length is less than 8 or greater than 63, Do not set this psk field.
1648 if (config.psk.length() >= HAL_PSK_MIN_LENGTH && config.psk.length() < HAL_PSK_MAX_LENGTH) {
1649 std::string tmp = config.psk;
1650 num += PushP2pGroupConfigString(conf + num, GROUP_CONFIG_PSK, tmp);
1651 }
1652 if (num == 0) {
1653 return WIFI_HAL_OPT_OK;
1654 }
1655 return HdiP2pSetGroupConfig(networkId, conf, num);
1656 }
1657
P2pTempGroupAdd(int freq)1658 WifiErrorNo WifiHdiWpaClient::P2pTempGroupAdd(int freq)
1659 {
1660 return HdiP2pTempGroupAdd(freq);
1661 }
1662 } // namespace Wifi
1663 } // namespace OHOS
1664 #endif