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_napi_device.h"
17 #include <vector>
18 #include <functional>
19 #include "wifi_common_util.h"
20 #include "wifi_logger.h"
21 #include "wifi_napi_errcode.h"
22
23 namespace OHOS {
24 namespace Wifi {
25 DEFINE_WIFILOG_LABEL("WifiNAPIDevice");
26 static const std::string EAP_METHOD[] = { "NONE", "PEAP", "TLS", "TTLS", "PWD", "SIM", "AKA", "AKA'" };
27
28 std::shared_ptr<WifiDevice> wifiDevicePtr = WifiDevice::GetInstance(WIFI_DEVICE_ABILITY_ID);
29 std::shared_ptr<WifiScan> wifiScanPtr = WifiScan::GetInstance(WIFI_SCAN_ABILITY_ID);
30
EnableWifi(napi_env env,napi_callback_info info)31 NO_SANITIZE("cfi") napi_value EnableWifi(napi_env env, napi_callback_info info)
32 {
33 TRACE_FUNC_CALL;
34 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
35 ErrCode ret = wifiDevicePtr->EnableWifi();
36 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
37 }
38
DisableWifi(napi_env env,napi_callback_info info)39 NO_SANITIZE("cfi") napi_value DisableWifi(napi_env env, napi_callback_info info)
40 {
41 TRACE_FUNC_CALL;
42 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
43 ErrCode ret = wifiDevicePtr->DisableWifi();
44 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
45 }
46
IsWifiActive(napi_env env,napi_callback_info info)47 NO_SANITIZE("cfi") napi_value IsWifiActive(napi_env env, napi_callback_info info)
48 {
49 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
50 bool activeStatus = false;
51 ErrCode ret = wifiDevicePtr->IsWifiActive(activeStatus);
52 if (ret != WIFI_OPT_SUCCESS) {
53 WIFI_LOGE("Get wifi active status fail: %{public}d", ret);
54 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
55 }
56 napi_value result;
57 napi_get_boolean(env, activeStatus, &result);
58 return result;
59 }
60
Scan(napi_env env,napi_callback_info info)61 NO_SANITIZE("cfi") napi_value Scan(napi_env env, napi_callback_info info)
62 {
63 TRACE_FUNC_CALL;
64 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
65 bool compatible = true;
66 ErrCode ret = wifiScanPtr->Scan(compatible);
67 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
68 }
69
StartScan(napi_env env,napi_callback_info info)70 NO_SANITIZE("cfi") napi_value StartScan(napi_env env, napi_callback_info info)
71 {
72 TRACE_FUNC_CALL;
73 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
74 bool compatible = false;
75 ErrCode ret = wifiScanPtr->Scan(compatible);
76 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
77 }
78
SecurityTypeNativeToJs(const WifiSecurity & cppSecurityType)79 static SecTypeJs SecurityTypeNativeToJs(const WifiSecurity& cppSecurityType)
80 {
81 SecTypeJs jsSecurityType = SecTypeJs::SEC_TYPE_INVALID;
82 switch (cppSecurityType) {
83 case WifiSecurity::OPEN:
84 jsSecurityType = SecTypeJs::SEC_TYPE_OPEN;
85 break;
86 case WifiSecurity::WEP:
87 jsSecurityType = SecTypeJs::SEC_TYPE_WEP;
88 break;
89 case WifiSecurity::PSK:
90 jsSecurityType = SecTypeJs::SEC_TYPE_PSK;
91 break;
92 case WifiSecurity::SAE:
93 case WifiSecurity::PSK_SAE:
94 jsSecurityType = SecTypeJs::SEC_TYPE_SAE;
95 break;
96 case WifiSecurity::EAP:
97 jsSecurityType = SecTypeJs::SEC_TYPE_EAP;
98 break;
99 case WifiSecurity::EAP_SUITE_B:
100 jsSecurityType = SecTypeJs::SEC_TYPE_EAP_SUITE_B;
101 break;
102 case WifiSecurity::WAPI_CERT:
103 jsSecurityType = SecTypeJs::SEC_TYPE_WAPI_CERT;
104 break;
105 case WifiSecurity::WAPI_PSK:
106 jsSecurityType = SecTypeJs::SEC_TYPE_WAPI_PSK;
107 break;
108 default:
109 jsSecurityType = SecTypeJs::SEC_TYPE_INVALID;
110 break;
111 }
112 return jsSecurityType;
113 }
114
NativeInfoElemsToJsObj(const napi_env & env,const std::vector<WifiInfoElem> & infoElems,napi_value & eachObj)115 static ErrCode NativeInfoElemsToJsObj(const napi_env& env,
116 const std::vector<WifiInfoElem>& infoElems, napi_value& eachObj)
117 {
118 napi_value arr;
119 napi_create_array(env, &arr);
120 uint8_t idx_ie = 0;
121 napi_status status;
122 int valueStep = 2;
123 for (size_t i = 0; i < infoElems.size(); i++) {
124 napi_value ieObj;
125 napi_create_object(env, &ieObj);
126 SetValueInt32(env, "eid", infoElems[i].id, ieObj);
127 const char *uStr = &infoElems[i].content[0];
128 size_t len = infoElems[i].content.size();
129 size_t inLen = static_cast<size_t>(infoElems[i].content.size() * valueStep + 1);
130 char *buf = (char *)calloc(inLen + 1, sizeof(char));
131 if (buf == NULL) {
132 return WIFI_OPT_FAILED;
133 }
134 int pos = 0;
135 for (size_t k = 0; k < len; ++k) {
136 pos = (k << 1);
137 if (snprintf_s(buf + pos, inLen - pos, inLen - pos - 1, "%02x", uStr[k]) < 0) {
138 free(buf);
139 buf = NULL;
140 return WIFI_OPT_FAILED;
141 }
142 }
143 SetValueUtf8String(env, "content", (const char *)buf, ieObj, inLen - 1);
144 status = napi_set_element(env, arr, idx_ie++, ieObj);
145 if (status != napi_ok) {
146 WIFI_LOGE("set content error");
147 free(buf);
148 buf = NULL;
149 return WIFI_OPT_FAILED;
150 }
151 free(buf);
152 buf = NULL;
153 }
154 status = napi_set_named_property(env, eachObj, "infoElems", arr);
155 if (status != napi_ok) {
156 WIFI_LOGE("set infoElems error");
157 return WIFI_OPT_FAILED;
158 }
159 return WIFI_OPT_SUCCESS;
160 }
161
NativeScanInfosToJsObj(const napi_env & env,const std::vector<WifiScanInfo> & vecScnIanfos,napi_value & arrayResult)162 static ErrCode NativeScanInfosToJsObj(const napi_env& env,
163 const std::vector<WifiScanInfo>& vecScnIanfos, napi_value& arrayResult)
164 {
165 uint32_t idx = 0;
166 for (auto& each : vecScnIanfos) {
167 napi_value eachObj;
168 napi_create_object(env, &eachObj);
169 SetValueUtf8String(env, "ssid", each.ssid.c_str(), eachObj);
170 SetValueUtf8String(env, "bssid", each.bssid.c_str(), eachObj);
171 SetValueInt32(env, "bssidType", each.bssidType, eachObj);
172 SetValueUtf8String(env, "capabilities", each.capabilities.c_str(), eachObj);
173 SetValueInt32(env, "securityType", static_cast<int>(SecurityTypeNativeToJs(each.securityType)), eachObj);
174 SetValueInt32(env, "rssi", each.rssi, eachObj);
175 SetValueInt32(env, "band", each.band, eachObj);
176 SetValueInt32(env, "frequency", each.frequency, eachObj);
177 SetValueInt32(env, "channelWidth", static_cast<int>(each.channelWidth), eachObj);
178 SetValueInt32(env, "centerFrequency0", each.centerFrequency0, eachObj);
179 SetValueInt32(env, "centerFrequency1", each.centerFrequency1, eachObj);
180 NativeInfoElemsToJsObj(env, each.infoElems, eachObj);
181 SetValueInt64(env, "timestamp", each.timestamp, eachObj);
182 SetValueInt32(env, "supportedWifiCategory", static_cast<int>(each.supportedWifiCategory), eachObj);
183 SetValueBool(env, "isHiLinkNetwork", each.isHiLinkNetwork, eachObj);
184 napi_status status = napi_set_element(env, arrayResult, idx++, eachObj);
185 if (status != napi_ok) {
186 WIFI_LOGE("Wifi napi set element error: %{public}d, idx: %{public}d", status, idx - 1);
187 return WIFI_OPT_FAILED;
188 }
189 }
190 return WIFI_OPT_SUCCESS;
191 }
192
GetScanInfos(napi_env env,napi_callback_info info)193 NO_SANITIZE("cfi") napi_value GetScanInfos(napi_env env, napi_callback_info info)
194 {
195 TRACE_FUNC_CALL;
196 size_t argc = 2;
197 napi_value argv[argc];
198 napi_value thisVar = nullptr;
199 void *data = nullptr;
200 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
201 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
202
203 ScanInfoAsyncContext *asyncContext = new ScanInfoAsyncContext(env);
204 WIFI_NAPI_ASSERT(env, asyncContext != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
205 napi_create_string_latin1(env, "getScanInfos", NAPI_AUTO_LENGTH, &asyncContext->resourceName);
206
207 asyncContext->executeFunc = [&](void* data) -> void {
208 ScanInfoAsyncContext *context = static_cast<ScanInfoAsyncContext *>(data);
209 context->vecScanInfos.clear();
210 TRACE_FUNC_CALL_NAME("wifiScanPtr->GetScanInfoList");
211 bool compatible = true;
212 context->errorCode = wifiScanPtr->GetScanInfoList(context->vecScanInfos, compatible);
213 WIFI_LOGI("GetScanInfoList, size: %{public}zu", context->vecScanInfos.size());
214 };
215
216 asyncContext->completeFunc = [&](void* data) -> void {
217 ScanInfoAsyncContext *context = static_cast<ScanInfoAsyncContext *>(data);
218 napi_create_array_with_length(context->env, context->vecScanInfos.size(), &context->result);
219 if (context->errorCode == WIFI_OPT_SUCCESS) {
220 context->errorCode = NativeScanInfosToJsObj(context->env, context->vecScanInfos, context->result);
221 }
222 WIFI_LOGI("Push scan info list to client");
223 };
224
225 size_t nonCallbackArgNum = 0;
226 asyncContext->sysCap = SYSCAP_WIFI_STA;
227 return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum);
228 }
229
GetScanInfoResults(napi_env env,napi_callback_info info)230 NO_SANITIZE("cfi") napi_value GetScanInfoResults(napi_env env, napi_callback_info info)
231 {
232 TRACE_FUNC_CALL;
233 size_t argc = 2;
234 napi_value argv[argc];
235 napi_value thisVar = nullptr;
236 void *data = nullptr;
237 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
238 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
239
240 ScanInfoAsyncContext *asyncContext = new ScanInfoAsyncContext(env);
241 WIFI_NAPI_ASSERT(env, asyncContext != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
242 napi_create_string_latin1(env, "getScanInfos", NAPI_AUTO_LENGTH, &asyncContext->resourceName);
243
244 asyncContext->executeFunc = [&](void* data) -> void {
245 ScanInfoAsyncContext *context = static_cast<ScanInfoAsyncContext *>(data);
246 context->vecScanInfos.clear();
247 TRACE_FUNC_CALL_NAME("wifiScanPtr->GetScanInfoList");
248 bool compatible = false;
249 context->errorCode = wifiScanPtr->GetScanInfoList(context->vecScanInfos, compatible);
250 WIFI_LOGI("GetScanInfoList, size: %{public}zu", context->vecScanInfos.size());
251 };
252
253 asyncContext->completeFunc = [&](void* data) -> void {
254 ScanInfoAsyncContext *context = static_cast<ScanInfoAsyncContext *>(data);
255 napi_create_array_with_length(context->env, context->vecScanInfos.size(), &context->result);
256 if (context->errorCode == WIFI_OPT_SUCCESS) {
257 context->errorCode = NativeScanInfosToJsObj(context->env, context->vecScanInfos, context->result);
258 }
259 WIFI_LOGI("Push scan info list to client");
260 };
261
262 size_t nonCallbackArgNum = 0;
263 asyncContext->sysCap = SYSCAP_WIFI_STA;
264 return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum);
265 }
266
GetScanResults(napi_env env,napi_callback_info info)267 NO_SANITIZE("cfi") napi_value GetScanResults(napi_env env, napi_callback_info info)
268 {
269 TRACE_FUNC_CALL;
270 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
271 bool compatible = true;
272 std::vector<WifiScanInfo> scanInfos;
273 ErrCode ret = wifiScanPtr->GetScanInfoList(scanInfos, compatible);
274 if (ret != WIFI_OPT_SUCCESS) {
275 WIFI_LOGE("GetScanInfoList return fail: %{public}d", ret);
276 }
277
278 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
279 WIFI_LOGI("GetScanInfoList, size: %{public}zu", scanInfos.size());
280 napi_value arrayResult;
281 napi_create_array_with_length(env, scanInfos.size(), &arrayResult);
282 ret = NativeScanInfosToJsObj(env, scanInfos, arrayResult);
283 if (ret != WIFI_OPT_SUCCESS) {
284 WIFI_LOGE("NativeScanInfosToJsObj return fail: %{public}d", ret);
285 }
286 return arrayResult;
287 }
288
GetScanInfoList(napi_env env,napi_callback_info info)289 NO_SANITIZE("cfi") napi_value GetScanInfoList(napi_env env, napi_callback_info info)
290 {
291 TRACE_FUNC_CALL;
292 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
293 bool compatible = false;
294 std::vector<WifiScanInfo> scanInfos;
295 ErrCode ret = wifiScanPtr->GetScanInfoList(scanInfos, compatible);
296 if (ret != WIFI_OPT_SUCCESS) {
297 WIFI_LOGE("GetScanInfoList return fail: %{public}d", ret);
298 }
299
300 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
301 WIFI_LOGI("GetScanInfoList, size: %{public}zu", scanInfos.size());
302 napi_value arrayResult;
303 napi_create_array_with_length(env, scanInfos.size(), &arrayResult);
304 ret = NativeScanInfosToJsObj(env, scanInfos, arrayResult);
305 if (ret != WIFI_OPT_SUCCESS) {
306 WIFI_LOGE("NativeScanInfosToJsObj return fail: %{public}d", ret);
307 }
308 return arrayResult;
309 }
310
ConvertEncryptionMode(const SecTypeJs & securityType,std::string & keyMgmt)311 static void ConvertEncryptionMode(const SecTypeJs& securityType, std::string& keyMgmt)
312 {
313 switch (securityType) {
314 case SecTypeJs::SEC_TYPE_OPEN:
315 keyMgmt = KEY_MGMT_NONE;
316 break;
317 case SecTypeJs::SEC_TYPE_WEP:
318 keyMgmt = KEY_MGMT_WEP;
319 break;
320 case SecTypeJs::SEC_TYPE_PSK:
321 keyMgmt = KEY_MGMT_WPA_PSK;
322 break;
323 case SecTypeJs::SEC_TYPE_SAE:
324 keyMgmt = KEY_MGMT_SAE;
325 break;
326 case SecTypeJs::SEC_TYPE_EAP:
327 keyMgmt = KEY_MGMT_EAP;
328 break;
329 case SecTypeJs::SEC_TYPE_EAP_SUITE_B:
330 keyMgmt = KEY_MGMT_SUITE_B_192;
331 break;
332 case SecTypeJs::SEC_TYPE_WAPI_CERT:
333 keyMgmt = KEY_MGMT_WAPI_CERT;
334 break;
335 case SecTypeJs::SEC_TYPE_WAPI_PSK:
336 keyMgmt = KEY_MGMT_WAPI_PSK;
337 break;
338 default:
339 keyMgmt = KEY_MGMT_NONE;
340 break;
341 }
342 }
343
ProcessPassphrase(const SecTypeJs & securityType,WifiDeviceConfig & cppConfig)344 static void ProcessPassphrase(const SecTypeJs& securityType, WifiDeviceConfig& cppConfig)
345 {
346 if (securityType == SecTypeJs::SEC_TYPE_WEP) {
347 cppConfig.wepKeys[0] = cppConfig.preSharedKey;
348 cppConfig.wepTxKeyIndex = 0;
349 cppConfig.preSharedKey = "";
350 std::string().swap(cppConfig.preSharedKey);
351 }
352 }
353
EapMethod2Str(const int & method)354 static std::string EapMethod2Str(const int& method)
355 {
356 if (method < 0 || method >= static_cast<int>(sizeof(EAP_METHOD) / sizeof(EAP_METHOD[0]))) {
357 return "NONE";
358 }
359 return EAP_METHOD[method];
360 }
361
ProcessEapConfig(const napi_env & env,const napi_value & object,WifiDeviceConfig & devConfig)362 napi_value ProcessEapConfig(const napi_env& env, const napi_value& object, WifiDeviceConfig& devConfig)
363 {
364 bool hasProperty = false;
365 NAPI_CALL(env, napi_has_named_property(env, object, "eapConfig", &hasProperty));
366 if (!hasProperty) {
367 WIFI_LOGI("Js has no property: eapConfig.");
368 return UndefinedNapiValue(env);
369 }
370
371 napi_value napiEap;
372 napi_get_named_property(env, object, "eapConfig", &napiEap);
373 int eapMethod = static_cast<int>(EapMethodJs::EAP_NONE);
374
375 // EAP authentication mode
376 JsObjectToInt(env, napiEap, "eapMethod", eapMethod);
377 devConfig.wifiEapConfig.eap = EapMethod2Str(eapMethod);
378 if (devConfig.wifiEapConfig.eap == EAP_METHOD_NONE) {
379 return UndefinedNapiValue(env);
380 }
381 WIFI_LOGI("%{public}s eapMethod: %{public}d[%{public}s]",
382 __func__, eapMethod, devConfig.wifiEapConfig.eap.c_str());
383
384 int phase2 = static_cast<int>(Phase2Method::NONE);
385 JsObjectToInt(env, napiEap, "phase2Method", phase2);
386 devConfig.wifiEapConfig.phase2Method = Phase2Method(phase2);
387 JsObjectToString(env, napiEap, "identity", NAPI_MAX_STR_LENT, devConfig.wifiEapConfig.identity);
388 JsObjectToString(env, napiEap, "anonymousIdentity", NAPI_MAX_STR_LENT, devConfig.wifiEapConfig.anonymousIdentity);
389 JsObjectToString(env, napiEap, "password", NAPI_MAX_STR_LENT, devConfig.wifiEapConfig.password);
390 JsObjectToString(env, napiEap, "caCertAlias", NAPI_MAX_STR_LENT, devConfig.wifiEapConfig.caCertAlias);
391 JsObjectToString(env, napiEap, "caPath", NAPI_MAX_STR_LENT, devConfig.wifiEapConfig.caCertPath);
392 JsObjectToString(env, napiEap, "clientCertAlias", NAPI_MAX_STR_LENT, devConfig.wifiEapConfig.clientCert);
393 JsObjectToString(env, napiEap, "clientCertAlias", NAPI_MAX_STR_LENT, devConfig.wifiEapConfig.privateKey);
394 devConfig.wifiEapConfig.certEntry = JsObjectToU8Vector(env, napiEap, "certEntry");
395
396 std::string certPwd;
397 JsObjectToString(env, napiEap, "certPassword", NAPI_MAX_STR_LENT, certPwd);
398 if (strncpy_s(devConfig.wifiEapConfig.certPassword, sizeof(devConfig.wifiEapConfig.certPassword),
399 certPwd.c_str(), certPwd.length()) != EOK) {
400 WIFI_LOGE("%{public}s: failed to copy", __func__);
401 }
402 JsObjectToString(env, napiEap, "altSubjectMatch", NAPI_MAX_STR_LENT, devConfig.wifiEapConfig.altSubjectMatch);
403 JsObjectToString(env, napiEap, "domainSuffixMatch", NAPI_MAX_STR_LENT, devConfig.wifiEapConfig.domainSuffixMatch);
404 JsObjectToString(env, napiEap, "realm", NAPI_MAX_STR_LENT, devConfig.wifiEapConfig.realm);
405 JsObjectToString(env, napiEap, "plmn", NAPI_MAX_STR_LENT, devConfig.wifiEapConfig.plmn);
406 JsObjectToInt(env, napiEap, "eapSubId", devConfig.wifiEapConfig.eapSubId);
407 return CreateInt32(env);
408 }
409
ConfigStaticIp(const napi_env & env,const napi_value & object,WifiDeviceConfig & cppConfig)410 napi_value ConfigStaticIp(const napi_env& env, const napi_value& object, WifiDeviceConfig& cppConfig)
411 {
412 bool hasProperty = false;
413 NAPI_CALL(env, napi_has_named_property(env, object, "staticIp", &hasProperty));
414 if (!hasProperty) {
415 WIFI_LOGE("ConfigStaticIp, Js has no property: staticIp.");
416 return UndefinedNapiValue(env);
417 }
418 napi_value staticIp;
419 napi_value dnsServers;
420 napi_value primaryDns;
421 napi_value secondDns;
422 napi_get_named_property(env, object, "staticIp", &staticIp);
423 JsObjectToUint(env, staticIp, "ipAddress",
424 cppConfig.wifiIpConfig.staticIpAddress.ipAddress.address.addressIpv4);
425 cppConfig.wifiIpConfig.staticIpAddress.ipAddress.address.family = 0;
426 JsObjectToUint(env, staticIp, "gateway", cppConfig.wifiIpConfig.staticIpAddress.gateway.addressIpv4);
427 JsObjectToInt(env, staticIp, "prefixLength", cppConfig.wifiIpConfig.staticIpAddress.ipAddress.prefixLength);
428
429 NAPI_CALL(env, napi_has_named_property(env, staticIp, "dnsServers", &hasProperty));
430 if (!hasProperty) {
431 WIFI_LOGE("ConfigStaticIp, Js has no property: dnsServers.");
432 return UndefinedNapiValue(env);
433 }
434 uint32_t arrayLength = 0;
435 const int DNS_NUM = 2;
436 napi_get_named_property(env, staticIp, "dnsServers", &dnsServers);
437 napi_get_array_length(env, dnsServers, &arrayLength);
438 if (arrayLength == 0 || arrayLength > DNS_NUM) {
439 WIFI_LOGE("ConfigStaticIp, It needs dns servers or dns too much.");
440 return UndefinedNapiValue(env);
441 }
442 napi_get_element(env, dnsServers, 0, &primaryDns);
443 napi_get_element(env, dnsServers, 1, &secondDns);
444 napi_get_value_uint32(env, primaryDns, &cppConfig.wifiIpConfig.staticIpAddress.dnsServer1.addressIpv4);
445 napi_get_value_uint32(env, secondDns, &cppConfig.wifiIpConfig.staticIpAddress.dnsServer2.addressIpv4);
446
447 return CreateInt32(env);
448 }
449
ProcessProxyConfig(const napi_env & env,const napi_value & object,WifiDeviceConfig & cppConfig)450 ErrCode ProcessProxyConfig(const napi_env& env, const napi_value& object, WifiDeviceConfig& cppConfig)
451 {
452 bool hasProperty = false;
453 NAPI_CALL_BASE(env, napi_has_named_property(env, object, "proxyConfig", &hasProperty), {});
454 ErrCode ret = WIFI_OPT_SUCCESS;
455 if (hasProperty) {
456 napi_value proxyConfig;
457 napi_get_named_property(env, object, "proxyConfig", &proxyConfig);
458 napi_valuetype valueType;
459 napi_typeof(env, proxyConfig, &valueType);
460 if (valueType == napi_null || valueType == napi_undefined) {
461 WIFI_LOGE("ProcessProxyConfig, proxyConfig is null.");
462 return ret;
463 }
464
465 int proxyConfigMethod = static_cast<int>(ConfigureProxyMethod::CLOSED);
466 JsObjectToInt(env, proxyConfig, "proxyMethod", proxyConfigMethod);
467 cppConfig.wifiProxyconfig.configureMethod = ConfigureProxyMethod::CLOSED;
468 switch (ConfigureProxyMethod(proxyConfigMethod)) {
469 case ConfigureProxyMethod::AUTOCONFIGUE:
470 cppConfig.wifiProxyconfig.configureMethod = ConfigureProxyMethod::AUTOCONFIGUE;
471 JsObjectToString(env, proxyConfig, "pacWebAddress", NAPI_MAX_STR_LENT,
472 cppConfig.wifiProxyconfig.autoProxyConfig.pacWebAddress);
473 ret = WIFI_OPT_NOT_SUPPORTED;
474 break;
475 case ConfigureProxyMethod::MANUALCONFIGUE:
476 cppConfig.wifiProxyconfig.configureMethod = ConfigureProxyMethod::MANUALCONFIGUE;
477 JsObjectToString(env, proxyConfig, "serverHostName", NAPI_MAX_STR_LENT,
478 cppConfig.wifiProxyconfig.manualProxyConfig.serverHostName);
479 JsObjectToString(env, proxyConfig, "exclusionObjects", NAPI_MAX_STR_LENT,
480 cppConfig.wifiProxyconfig.manualProxyConfig.exclusionObjectList);
481 JsObjectToInt(env, proxyConfig, "serverPort", cppConfig.wifiProxyconfig.manualProxyConfig.serverPort);
482 if (cppConfig.wifiProxyconfig.manualProxyConfig.serverPort < 0) {
483 ret = WIFI_OPT_INVALID_PARAM;
484 }
485 break;
486 case ConfigureProxyMethod::CLOSED:
487 WIFI_LOGI("ProcessProxyConfig, configureMethod is closed.");
488 break;
489 default:
490 WIFI_LOGE("ProcessProxyConfig, configureMethod %{public}d is not supported.", proxyConfigMethod);
491 ret = WIFI_OPT_INVALID_PARAM;
492 }
493 }
494
495 return ret;
496 }
497
JsObjToWapiConfig(const napi_env & env,const napi_value & object,WifiDeviceConfig & devConfig)498 napi_value JsObjToWapiConfig(const napi_env& env, const napi_value& object, WifiDeviceConfig& devConfig)
499 {
500 bool hasProperty = false;
501 NAPI_CALL(env, napi_has_named_property(env, object, "wapiConfig", &hasProperty));
502 if (!hasProperty) {
503 WIFI_LOGI("Js has no property: wapiConfig.");
504 return UndefinedNapiValue(env);
505 }
506
507 napi_value napiEap;
508 napi_get_named_property(env, object, "wapiConfig", &napiEap);
509
510 JsObjectToInt(env, napiEap, "wapiPskType", devConfig.wifiWapiConfig.wapiPskType);
511 JsObjectToString(env, napiEap, "wapiAsCert", NAPI_WAPI_MAX_STR_LENT, devConfig.wifiWapiConfig.wapiAsCertData);
512 JsObjectToString(env, napiEap, "wapiUserCert", NAPI_WAPI_MAX_STR_LENT, devConfig.wifiWapiConfig.wapiUserCertData);
513 return CreateInt32(env);
514 }
515
JsObjToDeviceConfig(const napi_env & env,const napi_value & object,WifiDeviceConfig & cppConfig)516 static napi_value JsObjToDeviceConfig(const napi_env& env, const napi_value& object, WifiDeviceConfig& cppConfig)
517 {
518 JsObjectToString(env, object, "ssid", NAPI_MAX_STR_LENT, cppConfig.ssid); /* ssid max length is 32 + '\0' */
519 JsObjectToString(env, object, "bssid", NAPI_MAX_STR_LENT, cppConfig.bssid); /* max bssid length: 18 */
520 cppConfig.bssidType = RANDOM_DEVICE_ADDRESS;
521 JsObjectToInt(env, object, "bssidType", cppConfig.bssidType);
522 WIFI_LOGE("JsObjToDeviceConfig, bssid length: %{public}d, bssidType: %{public}d",
523 static_cast<int>(cppConfig.bssid.length()), cppConfig.bssidType);
524 JsObjectToString(env, object, "preSharedKey", NAPI_MAX_STR_LENT, cppConfig.preSharedKey);
525 JsObjectToBool(env, object, "isHiddenSsid", cppConfig.hiddenSSID);
526 int type = static_cast<int>(SecTypeJs::SEC_TYPE_INVALID);
527 JsObjectToInt(env, object, "securityType", type);
528 ConvertEncryptionMode(SecTypeJs(type), cppConfig.keyMgmt);
529 ProcessPassphrase(SecTypeJs(type), cppConfig);
530 /* "creatorUid" is not supported currently */
531 /* "disableReason" is not supported currently */
532 JsObjectToInt(env, object, "netId", cppConfig.networkId);
533 int randomMacType = static_cast<int>(WifiPrivacyConfig::RANDOMMAC);
534 JsObjectToInt(env, object, "randomMacType", randomMacType);
535 cppConfig.wifiPrivacySetting = WifiPrivacyConfig(randomMacType);
536 /* "randomMacAddr" is not supported currently */
537 int ipType = static_cast<int>(AssignIpMethod::UNASSIGNED);
538 JsObjectToInt(env, object, "ipType", ipType);
539 WIFI_LOGI("JsObjToDeviceConfig, ipType: %{public}d, type: %{public}d.", ipType, type);
540 if (IpTypeJs(ipType) == IpTypeJs::IP_TYPE_DHCP) {
541 cppConfig.wifiIpConfig.assignMethod = AssignIpMethod::DHCP;
542 } else if (IpTypeJs(ipType) == IpTypeJs::IP_TYPE_STATIC) {
543 cppConfig.wifiIpConfig.assignMethod = AssignIpMethod::STATIC;
544 napi_valuetype valueType;
545 napi_value ret = ConfigStaticIp(env, object, cppConfig);
546 napi_typeof(env, ret, &valueType);
547 if (valueType == napi_undefined) {
548 WIFI_LOGI("JsObjToDeviceConfig, ConfigStaticIp return napi_undefined.");
549 return UndefinedNapiValue(env);
550 }
551 }
552 ErrCode ret = ProcessProxyConfig(env, object, cppConfig);
553 if (ret != WIFI_OPT_SUCCESS) {
554 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
555 }
556 if (SecTypeJs(type) == SecTypeJs::SEC_TYPE_EAP || SecTypeJs(type) == SecTypeJs::SEC_TYPE_EAP_SUITE_B) {
557 return ProcessEapConfig(env, object, cppConfig);
558 }
559 if (SecTypeJs(type) == SecTypeJs::SEC_TYPE_WAPI_CERT || SecTypeJs(type) == SecTypeJs::SEC_TYPE_WAPI_PSK) {
560 return JsObjToWapiConfig(env, object, cppConfig);
561 }
562 return CreateInt32(env);
563 }
564
AddDeviceConfig(napi_env env,napi_callback_info info)565 NO_SANITIZE("cfi") napi_value AddDeviceConfig(napi_env env, napi_callback_info info)
566 {
567 TRACE_FUNC_CALL;
568 size_t argc = 3;
569 napi_value argv[argc];
570 napi_value thisVar = nullptr;
571 void *data = nullptr;
572 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
573 WIFI_NAPI_ASSERT(env, argc >= 1, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
574 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
575
576 napi_valuetype valueType;
577 napi_typeof(env, argv[0], &valueType);
578 WIFI_NAPI_ASSERT(env, valueType == napi_object, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
579
580 DeviceConfigContext *asyncContext = new DeviceConfigContext(env);
581 WIFI_NAPI_ASSERT(env, asyncContext != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
582 napi_create_string_latin1(env, "addDeviceConfig", NAPI_AUTO_LENGTH, &asyncContext->resourceName);
583
584 WifiDeviceConfig *config = new WifiDeviceConfig();
585 if (config == nullptr) {
586 delete asyncContext;
587 return UndefinedNapiValue(env);
588 }
589 napi_value ret = JsObjToDeviceConfig(env, argv[0], *config);
590 napi_typeof(env, ret, &valueType);
591 WIFI_NAPI_ASSERT(env, valueType != napi_undefined, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
592
593 asyncContext->config = config;
594 asyncContext->isCandidate = false;
595 asyncContext->executeFunc = [&](void* data) -> void {
596 DeviceConfigContext *context = static_cast<DeviceConfigContext *>(data);
597 TRACE_FUNC_CALL_NAME("wifiDevicePtr->AddDeviceConfig");
598 ErrCode ret = wifiDevicePtr->AddDeviceConfig(*context->config, context->networkId, context->isCandidate);
599 if (context->networkId < 0 || ret != WIFI_OPT_SUCCESS) {
600 context->networkId = -1;
601 }
602 context->errorCode = ret;
603 };
604
605 asyncContext->completeFunc = [&](void* data) -> void {
606 DeviceConfigContext *context = static_cast<DeviceConfigContext *>(data);
607 napi_create_int32(context->env, context->networkId, &context->result);
608 if (context->config != nullptr) {
609 delete context->config;
610 context->config = nullptr;
611 }
612 WIFI_LOGI("Push add device config result to client");
613 };
614
615 size_t nonCallbackArgNum = 1;
616 asyncContext->sysCap = SYSCAP_WIFI_STA;
617 return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum);
618 }
619
AddUntrustedConfig(napi_env env,napi_callback_info info)620 NO_SANITIZE("cfi") napi_value AddUntrustedConfig(napi_env env, napi_callback_info info)
621 {
622 TRACE_FUNC_CALL;
623 size_t argc = 2;
624 napi_value argv[argc];
625 napi_value thisVar = nullptr;
626 void *data = nullptr;
627 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
628 WIFI_NAPI_ASSERT(env, argc >= 1, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
629 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
630
631 napi_valuetype valueType;
632 napi_typeof(env, argv[0], &valueType);
633 WIFI_NAPI_ASSERT(env, valueType == napi_object, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
634
635 DeviceConfigContext *asyncContext = new DeviceConfigContext(env);
636 WIFI_NAPI_ASSERT(env, asyncContext != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
637 napi_create_string_latin1(env, "AddUntrustedConfig", NAPI_AUTO_LENGTH, &asyncContext->resourceName);
638
639 WifiDeviceConfig *config = new WifiDeviceConfig();
640 if (config == nullptr) {
641 delete asyncContext;
642 return UndefinedNapiValue(env);
643 }
644 napi_value ret = JsObjToDeviceConfig(env, argv[0], *config);
645 napi_typeof(env, ret, &valueType);
646 WIFI_NAPI_ASSERT(env, valueType != napi_undefined, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
647 asyncContext->config = config;
648 asyncContext->isCandidate = true;
649
650 asyncContext->executeFunc = [&](void* data) -> void {
651 DeviceConfigContext *context = static_cast<DeviceConfigContext *>(data);
652 TRACE_FUNC_CALL_NAME("wifiDevicePtr->AddUntrustedConfig");
653 ErrCode ret = wifiDevicePtr->AddDeviceConfig(*context->config, context->networkId, context->isCandidate);
654 if (context->networkId < 0 || ret != WIFI_OPT_SUCCESS) {
655 context->networkId = -1;
656 }
657 context->errorCode = ret;
658 };
659
660 asyncContext->completeFunc = [&](void* data) -> void {
661 DeviceConfigContext *context = static_cast<DeviceConfigContext *>(data);
662 napi_get_boolean(context->env, (context->networkId >= 0), &context->result);
663 if (context->config != nullptr) {
664 delete context->config;
665 context->config = nullptr;
666 }
667 WIFI_LOGI("Push add untrusted device config result to client");
668 };
669
670 size_t nonCallbackArgNum = 1;
671 asyncContext->sysCap = SYSCAP_WIFI_STA;
672 return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum);
673 }
674
RemoveUntrustedConfig(napi_env env,napi_callback_info info)675 NO_SANITIZE("cfi") napi_value RemoveUntrustedConfig(napi_env env, napi_callback_info info)
676 {
677 TRACE_FUNC_CALL;
678 size_t argc = 3;
679 napi_value argv[argc];
680 napi_value thisVar = nullptr;
681 void *data = nullptr;
682 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
683 WIFI_NAPI_ASSERT(env, argc >= 1, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
684 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
685
686 napi_valuetype valueType;
687 napi_typeof(env, argv[0], &valueType);
688 WIFI_NAPI_ASSERT(env, valueType == napi_object, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
689
690 DeviceConfigContext *asyncContext = new DeviceConfigContext(env);
691 WIFI_NAPI_ASSERT(env, asyncContext != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
692 napi_create_string_latin1(env, "RemoveUntrustedConfig", NAPI_AUTO_LENGTH, &asyncContext->resourceName);
693
694 WifiDeviceConfig *config = new WifiDeviceConfig();
695 if (config == nullptr) {
696 delete asyncContext;
697 return UndefinedNapiValue(env);
698 }
699 napi_value ret = JsObjToDeviceConfig(env, argv[0], *config);
700 napi_typeof(env, ret, &valueType);
701 WIFI_NAPI_ASSERT(env, valueType != napi_undefined, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
702 asyncContext->config = config;
703 asyncContext->executeFunc = [&](void* data) -> void {
704 DeviceConfigContext *context = static_cast<DeviceConfigContext *>(data);
705 TRACE_FUNC_CALL_NAME("wifiDevicePtr->RemoveCandidateConfig");
706 context->errorCode = wifiDevicePtr->RemoveCandidateConfig(*context->config);
707 };
708
709 asyncContext->completeFunc = [&](void* data) -> void {
710 DeviceConfigContext *context = static_cast<DeviceConfigContext *>(data);
711 napi_get_boolean(context->env, context->errorCode == WIFI_OPT_SUCCESS, &context->result);
712 if (context->config != nullptr) {
713 delete context->config;
714 context->config = nullptr;
715 }
716 WIFI_LOGI("Push remove untrusted device config result to client");
717 };
718
719 size_t nonCallbackArgNum = 1;
720 asyncContext->sysCap = SYSCAP_WIFI_STA;
721 return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum);
722 }
723
AddCandidateConfig(napi_env env,napi_callback_info info)724 NO_SANITIZE("cfi") napi_value AddCandidateConfig(napi_env env, napi_callback_info info)
725 {
726 TRACE_FUNC_CALL;
727 size_t argc = 2;
728 napi_value argv[argc];
729 napi_value thisVar = nullptr;
730 void *data = nullptr;
731 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
732 WIFI_NAPI_ASSERT(env, argc >= 1, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
733 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
734
735 napi_valuetype valueType;
736 napi_typeof(env, argv[0], &valueType);
737 WIFI_NAPI_ASSERT(env, valueType == napi_object, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
738
739 DeviceConfigContext *asyncContext = new DeviceConfigContext(env);
740 WIFI_NAPI_ASSERT(env, asyncContext != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
741 napi_create_string_latin1(env, "AddCandidateConfig", NAPI_AUTO_LENGTH, &asyncContext->resourceName);
742
743 WifiDeviceConfig *config = new WifiDeviceConfig();
744 if (config == nullptr) {
745 delete asyncContext;
746 return UndefinedNapiValue(env);
747 }
748
749 napi_value ret = JsObjToDeviceConfig(env, argv[0], *config);
750 napi_typeof(env, ret, &valueType);
751 WIFI_NAPI_ASSERT(env, valueType != napi_undefined, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
752 asyncContext->config = config;
753 asyncContext->isCandidate = true;
754 asyncContext->executeFunc = [&](void* data) -> void {
755 DeviceConfigContext *context = static_cast<DeviceConfigContext *>(data);
756 TRACE_FUNC_CALL_NAME("wifiDevicePtr->AddCandidateConfig");
757 ErrCode ret = wifiDevicePtr->AddDeviceConfig(*context->config, context->networkId, context->isCandidate);
758 if (context->networkId < 0 || ret != WIFI_OPT_SUCCESS) {
759 WIFI_LOGE("Add candidate device config failed: %{public}d", static_cast<int>(ret));
760 context->networkId = -1;
761 }
762 context->errorCode = ret;
763 };
764
765 asyncContext->completeFunc = [&](void* data) -> void {
766 DeviceConfigContext *context = static_cast<DeviceConfigContext *>(data);
767 napi_create_int32(context->env, context->networkId, &context->result);
768 if (context->config != nullptr) {
769 delete context->config;
770 context->config = nullptr;
771 }
772 WIFI_LOGI("Push add candidate device config result to client");
773 };
774
775 size_t nonCallbackArgNum = 1;
776 asyncContext->sysCap = SYSCAP_WIFI_STA;
777 return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum);
778 }
779
RemoveCandidateConfig(napi_env env,napi_callback_info info)780 NO_SANITIZE("cfi") napi_value RemoveCandidateConfig(napi_env env, napi_callback_info info)
781 {
782 TRACE_FUNC_CALL;
783 size_t argc = 3;
784 napi_value argv[argc];
785 napi_value thisVar = nullptr;
786 void *data = nullptr;
787 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
788 WIFI_NAPI_ASSERT(env, argc >= 1, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
789 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
790
791 napi_valuetype valueType;
792 napi_typeof(env, argv[0], &valueType);
793 WIFI_NAPI_ASSERT(env, valueType == napi_number, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
794
795 DeviceConfigContext *asyncContext = new DeviceConfigContext(env);
796 WIFI_NAPI_ASSERT(env, asyncContext != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
797 napi_create_string_latin1(env, "RemoveCandidateConfig", NAPI_AUTO_LENGTH, &asyncContext->resourceName);
798
799 napi_get_value_int32(env, argv[0], &asyncContext->networkId);
800 asyncContext->executeFunc = [&](void* data) -> void {
801 DeviceConfigContext *context = static_cast<DeviceConfigContext *>(data);
802 context->errorCode = wifiDevicePtr->RemoveCandidateConfig(context->networkId);
803 };
804
805 asyncContext->completeFunc = [&](void* data) -> void {
806 DeviceConfigContext *context = static_cast<DeviceConfigContext *>(data);
807 napi_get_boolean(context->env, (context->errorCode == WIFI_OPT_SUCCESS), &context->result);
808 if (context->config != nullptr) {
809 delete context->config;
810 context->config = nullptr;
811 }
812 WIFI_LOGI("Push remove candidate device config result to client");
813 };
814
815 size_t nonCallbackArgNum = 1;
816 asyncContext->sysCap = SYSCAP_WIFI_STA;
817 return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum);
818 }
819
ConnectToCandidateConfig(napi_env env,napi_callback_info info)820 NO_SANITIZE("cfi") napi_value ConnectToCandidateConfig(napi_env env, napi_callback_info info)
821 {
822 TRACE_FUNC_CALL;
823 size_t argc = 1;
824 napi_value argv[1];
825 napi_value thisVar;
826 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
827 WIFI_NAPI_ASSERT(env, argc == 1, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
828
829 napi_valuetype valueType;
830 napi_typeof(env, argv[0], &valueType);
831 WIFI_NAPI_ASSERT(env, valueType == napi_number, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
832
833 int networkId = -1;
834 napi_get_value_int32(env, argv[0], &networkId);
835 bool isCandidate = true;
836
837 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
838 ErrCode ret = wifiDevicePtr->ConnectToNetwork(networkId, isCandidate);
839 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
840 }
841
ConnectToNetwork(napi_env env,napi_callback_info info)842 NO_SANITIZE("cfi") napi_value ConnectToNetwork(napi_env env, napi_callback_info info)
843 {
844 TRACE_FUNC_CALL;
845 size_t argc = 1;
846 napi_value argv[1];
847 napi_value thisVar;
848 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
849 WIFI_NAPI_ASSERT(env, argc == 1, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
850
851 napi_valuetype valueType;
852 napi_typeof(env, argv[0], &valueType);
853 WIFI_NAPI_ASSERT(env, valueType == napi_number, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
854
855 int networkId = -1;
856 napi_get_value_int32(env, argv[0], &networkId);
857 bool isCandidate = false;
858
859 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
860 ErrCode ret = wifiDevicePtr->ConnectToNetwork(networkId, isCandidate);
861 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
862 }
863
ConnectToDevice(napi_env env,napi_callback_info info)864 NO_SANITIZE("cfi") napi_value ConnectToDevice(napi_env env, napi_callback_info info)
865 {
866 TRACE_FUNC_CALL;
867 size_t argc = 1;
868 napi_value argv[1];
869 napi_value thisVar;
870 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
871
872 napi_valuetype valueType;
873 napi_typeof(env, argv[0], &valueType);
874 WIFI_NAPI_ASSERT(env, valueType == napi_object, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
875
876 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
877 WifiDeviceConfig config;
878 napi_value napiRet = JsObjToDeviceConfig(env, argv[0], config);
879 napi_typeof(env, napiRet, &valueType);
880 WIFI_NAPI_ASSERT(env, valueType != napi_undefined, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
881 ErrCode ret = wifiDevicePtr->ConnectToDevice(config);
882 if (ret != WIFI_OPT_SUCCESS) {
883 WIFI_LOGE("Connect to device fail: %{public}d", ret);
884 }
885 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
886 }
887
IsConnected(napi_env env,napi_callback_info info)888 NO_SANITIZE("cfi") napi_value IsConnected(napi_env env, napi_callback_info info)
889 {
890 TRACE_FUNC_CALL;
891 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
892 bool isConnected = false;
893 ErrCode ret = wifiDevicePtr->IsConnected(isConnected);
894 if (ret != WIFI_OPT_SUCCESS) {
895 WIFI_LOGE("IsConnected return error: %{public}d", ret);
896 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
897 }
898 napi_value result;
899 napi_get_boolean(env, isConnected, &result);
900 return result;
901 }
902
Disconnect(napi_env env,napi_callback_info info)903 NO_SANITIZE("cfi") napi_value Disconnect(napi_env env, napi_callback_info info)
904 {
905 TRACE_FUNC_CALL;
906 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
907 ErrCode ret = wifiDevicePtr->Disconnect();
908 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
909 }
910
GetSignalLevel(napi_env env,napi_callback_info info)911 NO_SANITIZE("cfi") napi_value GetSignalLevel(napi_env env, napi_callback_info info)
912 {
913 size_t argc = 2;
914 const int PARAMS_NUM = 2;
915 napi_value argv[2];
916 napi_value thisVar;
917 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
918 /* the input have 2 parameters */
919 WIFI_NAPI_ASSERT(env, argc == PARAMS_NUM, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
920
921 napi_valuetype type1;
922 napi_valuetype type2;
923 napi_typeof(env, argv[0], &type1);
924 napi_typeof(env, argv[1], &type2);
925 WIFI_NAPI_ASSERT(env, type1 == napi_number, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
926 WIFI_NAPI_ASSERT(env, type2 == napi_number, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
927 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
928
929 int level = -1;
930 int rssi = 0;
931 int band = 0;
932 napi_get_value_int32(env, argv[0], &rssi);
933 napi_get_value_int32(env, argv[1], &band);
934 ErrCode ret = wifiDevicePtr->GetSignalLevel(rssi, band, level);
935 if (ret != WIFI_OPT_SUCCESS) {
936 WIFI_LOGE("Get wifi signal level fail: %{public}d", ret);
937 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
938 }
939 napi_value result;
940 napi_create_uint32(env, level, &result);
941 return result;
942 }
943
ReConnect(napi_env env,napi_callback_info info)944 NO_SANITIZE("cfi") napi_value ReConnect(napi_env env, napi_callback_info info)
945 {
946 TRACE_FUNC_CALL;
947 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
948 ErrCode ret = wifiDevicePtr->ReConnect();
949 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
950 }
951
ReAssociate(napi_env env,napi_callback_info info)952 NO_SANITIZE("cfi") napi_value ReAssociate(napi_env env, napi_callback_info info)
953 {
954 TRACE_FUNC_CALL;
955 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
956 ErrCode ret = wifiDevicePtr->ReAssociate();
957 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
958 }
959
IpInfoToJsObj(const napi_env & env,IpInfo & ipInfo,napi_value & result)960 static void IpInfoToJsObj(const napi_env& env, IpInfo& ipInfo, napi_value& result)
961 {
962 napi_create_object(env, &result);
963 SetValueUnsignedInt32(env, "ipAddress", ipInfo.ipAddress, result);
964 SetValueUnsignedInt32(env, "gateway", ipInfo.gateway, result);
965 SetValueUnsignedInt32(env, "netmask", ipInfo.netmask, result);
966 SetValueUnsignedInt32(env, "primaryDns", ipInfo.primaryDns, result);
967 SetValueUnsignedInt32(env, "secondDns", ipInfo.secondDns, result);
968 SetValueUnsignedInt32(env, "serverIp", ipInfo.serverIp, result);
969 SetValueUnsignedInt32(env, "leaseDuration", ipInfo.leaseDuration, result);
970 }
971
IpV6InfoToJsObj(const napi_env & env,IpV6Info & ipInfo,napi_value & result)972 static void IpV6InfoToJsObj(const napi_env& env, IpV6Info& ipInfo, napi_value& result)
973 {
974 napi_create_object(env, &result);
975 SetValueUtf8String(env, "linkIpv6Address", ipInfo.linkIpV6Address, result);
976 SetValueUtf8String(env, "globalIpv6Address", ipInfo.globalIpV6Address, result);
977 SetValueUtf8String(env, "randomGlobalIpv6Address", ipInfo.randGlobalIpV6Address, result);
978 SetValueUtf8String(env, "uniqueIpv6Address", ipInfo.uniqueLocalAddress1, result);
979 SetValueUtf8String(env, "randomUniqueIpv6Address", ipInfo.uniqueLocalAddress2, result);
980 SetValueUtf8String(env, "gateway", ipInfo.gateway, result);
981 SetValueUtf8String(env, "netmask", ipInfo.netmask, result);
982 SetValueUtf8String(env, "primaryDNS", ipInfo.primaryDns, result);
983 SetValueUtf8String(env, "secondDNS", ipInfo.secondDns, result);
984 }
985
GetIpInfo(napi_env env,napi_callback_info info)986 NO_SANITIZE("cfi") napi_value GetIpInfo(napi_env env, napi_callback_info info)
987 {
988 TRACE_FUNC_CALL;
989 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
990
991 IpInfo ipInfo;
992 napi_value result;
993 ErrCode ret = wifiDevicePtr->GetIpInfo(ipInfo);
994 if (ret != WIFI_OPT_SUCCESS) {
995 WIFI_LOGE("Get ip info fail: %{public}d", ret);
996 }
997 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
998 IpInfoToJsObj(env, ipInfo, result);
999 return result;
1000 }
1001
GetIpv6Info(napi_env env,napi_callback_info info)1002 NO_SANITIZE("cfi") napi_value GetIpv6Info(napi_env env, napi_callback_info info)
1003 {
1004 TRACE_FUNC_CALL;
1005 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1006
1007 IpV6Info ipInfo;
1008 napi_value result;
1009 ErrCode ret = wifiDevicePtr->GetIpv6Info(ipInfo);
1010 if (ret != WIFI_OPT_SUCCESS) {
1011 WIFI_LOGE("Get ip info fail: %{public}d", ret);
1012 }
1013 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1014 IpV6InfoToJsObj(env, ipInfo, result);
1015 return result;
1016 }
1017
LinkedInfoToJs(const napi_env & env,WifiLinkedInfo & linkedInfo,napi_value & result)1018 static void LinkedInfoToJs(const napi_env& env, WifiLinkedInfo& linkedInfo, napi_value& result)
1019 {
1020 SetValueUtf8String(env, "ssid", linkedInfo.ssid.c_str(), result);
1021 SetValueUtf8String(env, "bssid", linkedInfo.bssid.c_str(), result);
1022 SetValueInt32(env, "networkId", linkedInfo.networkId, result);
1023 SetValueInt32(env, "rssi", linkedInfo.rssi, result);
1024 SetValueInt32(env, "band", linkedInfo.band, result);
1025 SetValueInt32(env, "linkSpeed", linkedInfo.linkSpeed, result);
1026 SetValueInt32(env, "frequency", linkedInfo.frequency, result);
1027 SetValueBool(env, "isHidden", linkedInfo.ifHiddenSSID, result);
1028 SetValueBool(env, "isRestricted", linkedInfo.isDataRestricted, result);
1029 SetValueInt32(env, "chload", linkedInfo.chload, result);
1030 SetValueInt32(env, "snr", linkedInfo.snr, result);
1031 SetValueUtf8String(env, "macAddress", linkedInfo.macAddress.c_str(), result);
1032 SetValueInt32(env, "macType", linkedInfo.macType, result);
1033 SetValueUnsignedInt32(env, "ipAddress", linkedInfo.ipAddress, result);
1034 SetValueInt32(env, "suppState", static_cast<int>(linkedInfo.supplicantState), result);
1035 SetValueInt32(env, "connState", static_cast<int>(linkedInfo.connState), result);
1036 SetValueInt32(env, "wifiStandard", static_cast<int>(linkedInfo.wifiStandard), result);
1037 SetValueInt32(env, "maxSupportedRxLinkSpeed", static_cast<int>(linkedInfo.maxSupportedRxLinkSpeed), result);
1038 SetValueInt32(env, "maxSupportedTxLinkSpeed", static_cast<int>(linkedInfo.maxSupportedTxLinkSpeed), result);
1039 SetValueInt32(env, "rxLinkSpeed", static_cast<int>(linkedInfo.rxLinkSpeed), result);
1040 SetValueInt32(env, "linkSpeed", static_cast<int>(linkedInfo.txLinkSpeed), result);
1041 SetValueInt32(env, "channelWidth", static_cast<int>(linkedInfo.channelWidth), result);
1042 SetValueInt32(env, "supportedWifiCategory", static_cast<int>(linkedInfo.supportedWifiCategory), result);
1043 SetValueBool(env, "isHiLinkNetwork", linkedInfo.isHiLinkNetwork, result);
1044 }
1045
1046 /* This interface has not been fully implemented */
GetLinkedInfo(napi_env env,napi_callback_info info)1047 NO_SANITIZE("cfi") napi_value GetLinkedInfo(napi_env env, napi_callback_info info)
1048 {
1049 TRACE_FUNC_CALL;
1050 size_t argc = 2;
1051 napi_value argv[argc];
1052 napi_value thisVar = nullptr;
1053 void *data = nullptr;
1054 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
1055 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1056
1057 LinkedInfoAsyncContext *asyncContext = new LinkedInfoAsyncContext(env);
1058 WIFI_NAPI_ASSERT(env, asyncContext != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1059 napi_create_string_latin1(env, "getLinkedInfo", NAPI_AUTO_LENGTH, &asyncContext->resourceName);
1060
1061 asyncContext->executeFunc = [&](void* data) -> void {
1062 LinkedInfoAsyncContext *context = static_cast<LinkedInfoAsyncContext *>(data);
1063 TRACE_FUNC_CALL_NAME("wifiDevicePtr->GetLinkedInfo");
1064 context->errorCode = wifiDevicePtr->GetLinkedInfo(context->linkedInfo);
1065 };
1066
1067 asyncContext->completeFunc = [&](void* data) -> void {
1068 LinkedInfoAsyncContext *context = static_cast<LinkedInfoAsyncContext *>(data);
1069 napi_create_object(context->env, &context->result);
1070 LinkedInfoToJs(context->env, context->linkedInfo, context->result);
1071 WIFI_LOGD("Push get linkedInfo result to client");
1072 };
1073
1074 size_t nonCallbackArgNum = 0;
1075 asyncContext->sysCap = SYSCAP_WIFI_STA;
1076 return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum);
1077 }
1078
GetDisconnectedReason(napi_env env,napi_callback_info info)1079 NO_SANITIZE("cfi") napi_value GetDisconnectedReason(napi_env env, napi_callback_info info)
1080 {
1081 TRACE_FUNC_CALL;
1082 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_CORE);
1083 DisconnectedReason reason = DisconnectedReason::DISC_REASON_DEFAULT;
1084 ErrCode ret = wifiDevicePtr->GetDisconnectedReason(reason);
1085 if (ret != WIFI_OPT_SUCCESS) {
1086 WIFI_LOGE("GetDisconnectedReason failed:%{public}d", ret);
1087 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1088 }
1089 napi_value value;
1090 napi_create_int32(env, static_cast<int>(reason), &value);
1091 return value;
1092 }
1093
IsMeteredHotspot(napi_env env,napi_callback_info info)1094 NO_SANITIZE("cfi") napi_value IsMeteredHotspot(napi_env env, napi_callback_info info)
1095 {
1096 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1097 bool isMeteredHotspot = false;
1098 ErrCode ret = wifiDevicePtr->IsMeteredHotspot(isMeteredHotspot);
1099 if (ret != WIFI_OPT_SUCCESS) {
1100 WIFI_LOGE("Get isMeteredHotspot value fail: %{public}d", ret);
1101 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1102 }
1103 napi_value result;
1104 napi_get_boolean(env, isMeteredHotspot, &result);
1105 return result;
1106 }
1107
RemoveDevice(napi_env env,napi_callback_info info)1108 NO_SANITIZE("cfi") napi_value RemoveDevice(napi_env env, napi_callback_info info)
1109 {
1110 TRACE_FUNC_CALL;
1111 size_t argc = 1;
1112 napi_value argv[1];
1113 napi_value thisVar;
1114 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
1115 WIFI_NAPI_ASSERT(env, argc == 1, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
1116
1117 napi_valuetype valueType;
1118 napi_typeof(env, argv[0], &valueType);
1119 WIFI_NAPI_ASSERT(env, valueType == napi_number, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
1120
1121 int networkId = -1;
1122 napi_get_value_int32(env, argv[0], &networkId);
1123 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1124
1125 ErrCode ret = wifiDevicePtr->RemoveDevice(networkId);
1126 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1127 }
1128
RemoveAllNetwork(napi_env env,napi_callback_info info)1129 NO_SANITIZE("cfi") napi_value RemoveAllNetwork(napi_env env, napi_callback_info info)
1130 {
1131 TRACE_FUNC_CALL;
1132 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1133 ErrCode ret = wifiDevicePtr->RemoveAllDevice();
1134 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1135 }
1136
DisableNetwork(napi_env env,napi_callback_info info)1137 NO_SANITIZE("cfi") napi_value DisableNetwork(napi_env env, napi_callback_info info)
1138 {
1139 TRACE_FUNC_CALL;
1140 size_t argc = 1;
1141 napi_value argv[1];
1142 napi_value thisVar;
1143 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
1144 WIFI_NAPI_ASSERT(env, argc == 1, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
1145
1146 napi_valuetype valueType;
1147 napi_typeof(env, argv[0], &valueType);
1148 WIFI_NAPI_ASSERT(env, valueType == napi_number, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
1149 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1150
1151 int networkId = -1;
1152 napi_get_value_int32(env, argv[0], &networkId);
1153 ErrCode ret = wifiDevicePtr->DisableDeviceConfig(networkId);
1154 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1155 }
1156
GetCountryCode(napi_env env,napi_callback_info info)1157 NO_SANITIZE("cfi") napi_value GetCountryCode(napi_env env, napi_callback_info info)
1158 {
1159 TRACE_FUNC_CALL;
1160 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_CORE);
1161 std::string countryCode;
1162 ErrCode ret = wifiDevicePtr->GetCountryCode(countryCode);
1163 if (ret != WIFI_OPT_SUCCESS) {
1164 WIFI_LOGE("Get countryCode fail: %{public}d", ret);
1165 }
1166 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_CORE);
1167 napi_value cc;
1168 napi_create_string_utf8(env, countryCode.c_str(), NAPI_AUTO_LENGTH, &cc);
1169 return cc;
1170 }
1171
ConvertKeyMgmtToSecType(const std::string & keyMgmt)1172 static SecTypeJs ConvertKeyMgmtToSecType(const std::string& keyMgmt)
1173 {
1174 std::map<std::string, SecTypeJs> mapKeyMgmtToSecType = {
1175 {KEY_MGMT_NONE, SecTypeJs::SEC_TYPE_OPEN},
1176 {KEY_MGMT_WEP, SecTypeJs::SEC_TYPE_WEP},
1177 {KEY_MGMT_WPA_PSK, SecTypeJs::SEC_TYPE_PSK},
1178 {KEY_MGMT_SAE, SecTypeJs::SEC_TYPE_SAE},
1179 {KEY_MGMT_EAP, SecTypeJs::SEC_TYPE_EAP},
1180 {KEY_MGMT_SUITE_B_192, SecTypeJs::SEC_TYPE_EAP_SUITE_B},
1181 {KEY_MGMT_WAPI_CERT, SecTypeJs::SEC_TYPE_WAPI_CERT},
1182 {KEY_MGMT_WAPI_PSK, SecTypeJs::SEC_TYPE_WAPI_PSK},
1183 };
1184
1185 std::map<std::string, SecTypeJs>::iterator iter = mapKeyMgmtToSecType.find(keyMgmt);
1186 return iter == mapKeyMgmtToSecType.end() ? SecTypeJs::SEC_TYPE_OPEN : iter->second;
1187 }
1188
IpConfigToJs(const napi_env & env,const WifiIpConfig & wifiIpConfig,napi_value & ipCfgObj)1189 static void IpConfigToJs(const napi_env& env, const WifiIpConfig& wifiIpConfig, napi_value& ipCfgObj)
1190 {
1191 SetValueInt32(env, "ipAddress", wifiIpConfig.staticIpAddress.ipAddress.address.addressIpv4, ipCfgObj);
1192 SetValueInt32(env, "gateway", wifiIpConfig.staticIpAddress.gateway.addressIpv4, ipCfgObj);
1193
1194 const int DNS_NUM = 2;
1195 napi_value dnsArray;
1196 napi_create_array_with_length(env, DNS_NUM, &dnsArray);
1197 std::vector<unsigned int> vecDns = {wifiIpConfig.staticIpAddress.dnsServer1.addressIpv4,
1198 wifiIpConfig.staticIpAddress.dnsServer2.addressIpv4};
1199 for (int i = 0; i != DNS_NUM; ++i) {
1200 napi_value value;
1201 napi_status status = napi_create_int32(env, vecDns[i], &value);
1202 if (status != napi_ok) {
1203 WIFI_LOGE("Ip config to js create int32 error!");
1204 return;
1205 }
1206 status = napi_set_element(env, dnsArray, i, value);
1207 if (status != napi_ok) {
1208 WIFI_LOGE("Ip config to js set element error: %{public}d", status);
1209 return;
1210 }
1211 }
1212 if (napi_set_named_property(env, ipCfgObj, "dnsServers", dnsArray) != napi_ok) {
1213 WIFI_LOGE("Set dnsServers named property error!");
1214 }
1215
1216 const int DOMAINS_NUM = 1;
1217 napi_value domainsArray;
1218 napi_create_array_with_length(env, DOMAINS_NUM, &domainsArray);
1219 std::vector<std::string> vecDomains = {wifiIpConfig.staticIpAddress.domains};
1220 for (int i = 0; i != DOMAINS_NUM; ++i) {
1221 napi_value value;
1222 napi_status status = napi_create_string_utf8(env, vecDomains[i].c_str(), NAPI_AUTO_LENGTH, &value);
1223 if (status != napi_ok) {
1224 WIFI_LOGE("Ip config to js create utf8 string error!");
1225 return;
1226 }
1227 status = napi_set_element(env, domainsArray, i, value);
1228 if (status != napi_ok) {
1229 WIFI_LOGE("Ip config to js set element error: %{public}d", status);
1230 }
1231 }
1232 if (napi_set_named_property(env, ipCfgObj, "domains", domainsArray) != napi_ok) {
1233 WIFI_LOGE("Set domains named property error!");
1234 }
1235 }
1236
ProxyConfigToJs(const napi_env & env,const WifiDeviceConfig & wifiDeviceConfig,napi_value & result)1237 static void ProxyConfigToJs(const napi_env& env, const WifiDeviceConfig& wifiDeviceConfig, napi_value& result)
1238 {
1239 napi_value proxyCfgObj;
1240 napi_create_object(env, &proxyCfgObj);
1241 SetValueInt32(env, "proxyMethod", static_cast<int>(wifiDeviceConfig.wifiProxyconfig.configureMethod), proxyCfgObj);
1242 switch (wifiDeviceConfig.wifiProxyconfig.configureMethod) {
1243 case ConfigureProxyMethod::CLOSED:
1244 WIFI_LOGI("%{public}s get config method closed", __FUNCTION__);
1245 break;
1246 case ConfigureProxyMethod::AUTOCONFIGUE:
1247 SetValueUtf8String(env, "preSharedKey",
1248 wifiDeviceConfig.wifiProxyconfig.autoProxyConfig.pacWebAddress.c_str(), proxyCfgObj);
1249 break;
1250 case ConfigureProxyMethod::MANUALCONFIGUE:
1251 SetValueUtf8String(env, "serverHostName",
1252 wifiDeviceConfig.wifiProxyconfig.manualProxyConfig.serverHostName.c_str(), proxyCfgObj);
1253 SetValueInt32(env, "serverPort",
1254 wifiDeviceConfig.wifiProxyconfig.manualProxyConfig.serverPort, proxyCfgObj);
1255 SetValueUtf8String(env, "exclusionObjects",
1256 wifiDeviceConfig.wifiProxyconfig.manualProxyConfig.exclusionObjectList.c_str(), proxyCfgObj);
1257 break;
1258 default:
1259 break;
1260 }
1261 napi_status status = napi_set_named_property(env, result, "proxyConfig", proxyCfgObj);
1262 if (status != napi_ok) {
1263 WIFI_LOGE("%{public}s set proxy config failed!", __FUNCTION__);
1264 }
1265 }
1266
UpdateSecurityTypeAndPreSharedKey(WifiDeviceConfig & cppConfig)1267 static void UpdateSecurityTypeAndPreSharedKey(WifiDeviceConfig& cppConfig)
1268 {
1269 if (cppConfig.keyMgmt == KEY_MGMT_NONE || cppConfig.keyMgmt == KEY_MGMT_WEP) {
1270 WIFI_LOGD("%{public}s get psk!", __FUNCTION__);
1271 for (int i = 0; i != WEPKEYS_SIZE; ++i) {
1272 if (!cppConfig.wepKeys[i].empty() && cppConfig.wepTxKeyIndex == i) {
1273 cppConfig.keyMgmt = KEY_MGMT_WEP;
1274 cppConfig.preSharedKey = cppConfig.wepKeys[i];
1275 }
1276 }
1277 }
1278 }
1279
Str2EapMethod(const std::string & str)1280 static int Str2EapMethod(const std::string& str)
1281 {
1282 WIFI_LOGD("%{public}s: eapMethod is %{public}s", __func__, str.c_str());
1283 int len = sizeof(EAP_METHOD) / sizeof(EAP_METHOD[0]);
1284 for (int i = 0; i < len; i++) {
1285 if (EAP_METHOD[i] == str) {
1286 WIFI_LOGD("%{public}s: index is %{public}d", __func__, i);
1287 return i;
1288 }
1289 }
1290 return 0;
1291 }
1292
EapConfigToJs(const napi_env & env,const WifiEapConfig & wifiEapConfig,napi_value & cfgObj)1293 static void EapConfigToJs(const napi_env& env, const WifiEapConfig& wifiEapConfig, napi_value& cfgObj)
1294 {
1295 SetValueInt32(env, "eapMethod", Str2EapMethod(wifiEapConfig.eap), cfgObj);
1296 SetValueInt32(env, "phase2Method", static_cast<int>(wifiEapConfig.phase2Method), cfgObj);
1297 SetValueUtf8String(env, "identity", wifiEapConfig.identity.c_str(), cfgObj);
1298 SetValueUtf8String(env, "anonymousIdentity", wifiEapConfig.anonymousIdentity.c_str(), cfgObj);
1299 SetValueUtf8String(env, "password", wifiEapConfig.password.c_str(), cfgObj);
1300 SetValueUtf8String(env, "caCertAlias", wifiEapConfig.caCertAlias.c_str(), cfgObj);
1301 SetValueUtf8String(env, "caPath", wifiEapConfig.caCertPath.c_str(), cfgObj);
1302 SetValueUtf8String(env, "clientCertAlias", wifiEapConfig.clientCert.c_str(), cfgObj);
1303 SetValueU8Vector(env, "certEntry", wifiEapConfig.certEntry, cfgObj);
1304 SetValueUtf8String(env, "certPassword", wifiEapConfig.certPassword, cfgObj);
1305 SetValueUtf8String(env, "altSubjectMatch", wifiEapConfig.altSubjectMatch.c_str(), cfgObj);
1306 SetValueUtf8String(env, "domainSuffixMatch", wifiEapConfig.domainSuffixMatch.c_str(), cfgObj);
1307 SetValueUtf8String(env, "realm", wifiEapConfig.realm.c_str(), cfgObj);
1308 SetValueUtf8String(env, "plmn", wifiEapConfig.plmn.c_str(), cfgObj);
1309 SetValueInt32(env, "eapSubId", wifiEapConfig.eapSubId, cfgObj);
1310 }
1311
WapiConfigToJs(const napi_env & env,const WifiDeviceConfig & wifiDeviceConfig,napi_value & result)1312 static void WapiConfigToJs(const napi_env& env, const WifiDeviceConfig& wifiDeviceConfig, napi_value& result)
1313 {
1314 napi_value wapiCfgObj;
1315 napi_create_object(env, &wapiCfgObj);
1316 SetValueInt32(env, "wapiPskType", wifiDeviceConfig.wifiWapiConfig.wapiPskType, wapiCfgObj);
1317
1318 napi_status status = napi_set_named_property(env, result, "wapiConfig", wapiCfgObj);
1319 if (status != napi_ok) {
1320 WIFI_LOGE("%{public}s set wapi config failed!", __FUNCTION__);
1321 }
1322 }
1323
DeviceConfigToJsArray(const napi_env & env,std::vector<WifiDeviceConfig> & vecDeviceConfigs,const int idx,napi_value & arrayResult)1324 static void DeviceConfigToJsArray(const napi_env& env, std::vector<WifiDeviceConfig>& vecDeviceConfigs,
1325 const int idx, napi_value& arrayResult)
1326 {
1327 UpdateSecurityTypeAndPreSharedKey(vecDeviceConfigs[idx]);
1328 napi_value result;
1329 napi_create_object(env, &result);
1330 SetValueUtf8String(env, "ssid", vecDeviceConfigs[idx].ssid.c_str(), result);
1331 SetValueUtf8String(env, "bssid", vecDeviceConfigs[idx].bssid.c_str(), result);
1332 SetValueInt32(env, "bssidType", static_cast<int>(vecDeviceConfigs[idx].bssidType), result);
1333 SetValueUtf8String(env, "preSharedKey", vecDeviceConfigs[idx].preSharedKey.c_str(), result);
1334 SetValueBool(env, "isHiddenSsid", vecDeviceConfigs[idx].hiddenSSID, result);
1335 SetValueInt32(env, "securityType",
1336 static_cast<int>(ConvertKeyMgmtToSecType(vecDeviceConfigs[idx].keyMgmt)), result);
1337 SetValueInt32(env, "creatorUid", vecDeviceConfigs[idx].uid, result);
1338 SetValueInt32(env, "configStatus",
1339 static_cast<int>(vecDeviceConfigs[idx].networkSelectionStatus.status), result);
1340 SetValueInt32(env, "disableReason",
1341 static_cast<int>(vecDeviceConfigs[idx].networkSelectionStatus.networkSelectionDisableReason), result);
1342 SetValueInt32(env, "netId", vecDeviceConfigs[idx].networkId, result);
1343 SetValueInt32(env, "randomMacType", static_cast<int>(vecDeviceConfigs[idx].wifiPrivacySetting), result);
1344 /* not supported currently */
1345 SetValueUtf8String(env, "randomMacAddr", std::string("").c_str(), result);
1346 if (vecDeviceConfigs[idx].wifiIpConfig.assignMethod == AssignIpMethod::STATIC) {
1347 SetValueInt32(env, "ipType", static_cast<int>(IpTypeJs::IP_TYPE_STATIC), result);
1348 } else {
1349 SetValueInt32(env, "ipType", static_cast<int>(IpTypeJs::IP_TYPE_DHCP), result);
1350 }
1351 napi_value ipCfgObj;
1352 napi_create_object(env, &ipCfgObj);
1353 IpConfigToJs(env, vecDeviceConfigs[idx].wifiIpConfig, ipCfgObj);
1354 napi_status status = napi_set_named_property(env, result, "staticIp", ipCfgObj);
1355 if (status != napi_ok) {
1356 WIFI_LOGE("Set staticIp field!");
1357 }
1358 ProxyConfigToJs(env, vecDeviceConfigs[idx], result);
1359
1360 napi_value eapCfgObj;
1361 napi_create_object(env, &eapCfgObj);
1362 EapConfigToJs(env, vecDeviceConfigs[idx].wifiEapConfig, eapCfgObj);
1363 status = napi_set_named_property(env, result, "eapConfig", eapCfgObj);
1364 if (status != napi_ok) {
1365 WIFI_LOGE("failed to set eapConfig!");
1366 }
1367
1368 WapiConfigToJs(env, vecDeviceConfigs[idx], result);
1369
1370 status = napi_set_element(env, arrayResult, idx, result);
1371 if (status != napi_ok) {
1372 WIFI_LOGE("Wifi napi set element error: %{public}d", status);
1373 }
1374 }
1375
GetDeviceConfigs(napi_env env,napi_callback_info info)1376 NO_SANITIZE("cfi") napi_value GetDeviceConfigs(napi_env env, napi_callback_info info)
1377 {
1378 TRACE_FUNC_CALL;
1379 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1380 std::vector<WifiDeviceConfig> vecDeviceConfigs;
1381 bool isCandidate = false;
1382 ErrCode ret = wifiDevicePtr->GetDeviceConfigs(vecDeviceConfigs, isCandidate);
1383 if (ret != WIFI_OPT_SUCCESS) {
1384 WIFI_LOGE("Get device configs fail: %{public}d", ret);
1385 }
1386
1387 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1388 WIFI_LOGI("Get device configs size: %{public}zu", vecDeviceConfigs.size());
1389 napi_value arrayResult;
1390 napi_create_array_with_length(env, vecDeviceConfigs.size(), &arrayResult);
1391 for (size_t i = 0; i != vecDeviceConfigs.size(); ++i) {
1392 DeviceConfigToJsArray(env, vecDeviceConfigs, i, arrayResult);
1393 }
1394 return arrayResult;
1395 }
1396
GetCandidateConfigs(napi_env env,napi_callback_info info)1397 NO_SANITIZE("cfi") napi_value GetCandidateConfigs(napi_env env, napi_callback_info info)
1398 {
1399 TRACE_FUNC_CALL;
1400 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1401 std::vector<WifiDeviceConfig> vecDeviceConfigs;
1402 bool isCandidate = true;
1403 ErrCode ret = wifiDevicePtr->GetDeviceConfigs(vecDeviceConfigs, isCandidate);
1404 if (ret != WIFI_OPT_SUCCESS) {
1405 WIFI_LOGE("Get candidate device configs fail: %{public}d", ret);
1406 }
1407
1408 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1409 WIFI_LOGI("Get candidate device configs size: %{public}zu", vecDeviceConfigs.size());
1410 napi_value arrayResult;
1411 napi_create_array_with_length(env, vecDeviceConfigs.size(), &arrayResult);
1412 for (size_t i = 0; i != vecDeviceConfigs.size(); ++i) {
1413 DeviceConfigToJsArray(env, vecDeviceConfigs, i, arrayResult);
1414 }
1415 return arrayResult;
1416 }
1417
UpdateNetwork(napi_env env,napi_callback_info info)1418 NO_SANITIZE("cfi") napi_value UpdateNetwork(napi_env env, napi_callback_info info)
1419 {
1420 TRACE_FUNC_CALL;
1421 size_t argc = 1;
1422 napi_value argv[1];
1423 napi_value thisVar;
1424 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
1425
1426 napi_valuetype valueType;
1427 napi_typeof(env, argv[0], &valueType);
1428 WIFI_NAPI_ASSERT(env, valueType == napi_object, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
1429
1430 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1431 int updateResult;
1432 WifiDeviceConfig config;
1433 napi_value res = JsObjToDeviceConfig(env, argv[0], config);
1434 napi_typeof(env, res, &valueType);
1435 WIFI_NAPI_ASSERT(env, valueType != napi_undefined, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
1436 ErrCode ret = wifiDevicePtr->UpdateDeviceConfig(config, updateResult);
1437 if (ret != WIFI_OPT_SUCCESS) {
1438 WIFI_LOGE("Update device config fail: %{public}d", ret);
1439 }
1440
1441 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1442 napi_value result;
1443 napi_create_uint32(env, updateResult, &result);
1444 return result;
1445 }
1446
GetSupportedFeatures(napi_env env,napi_callback_info info)1447 NO_SANITIZE("cfi") napi_value GetSupportedFeatures(napi_env env, napi_callback_info info)
1448 {
1449 TRACE_FUNC_CALL;
1450 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_CORE);
1451 long features = -1;
1452 ErrCode ret = wifiDevicePtr->GetSupportedFeatures(features);
1453 if (ret != WIFI_OPT_SUCCESS) {
1454 WIFI_LOGE("Get supported features fail: %{public}d", ret);
1455 }
1456
1457 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_CORE);
1458 napi_value result;
1459 napi_create_int64(env, features, &result);
1460 return result;
1461 }
1462
IsFeatureSupported(napi_env env,napi_callback_info info)1463 NO_SANITIZE("cfi") napi_value IsFeatureSupported(napi_env env, napi_callback_info info)
1464 {
1465 TRACE_FUNC_CALL;
1466 size_t argc = 1;
1467 napi_value argv[1];
1468 napi_value thisVar;
1469 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
1470 WIFI_NAPI_ASSERT(env, argc == 1, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_CORE);
1471
1472 napi_valuetype valueType;
1473 napi_typeof(env, argv[0], &valueType);
1474 WIFI_NAPI_ASSERT(env, valueType == napi_number, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_CORE);
1475 long feature = -1;
1476 napi_get_value_int64(env, argv[0], (int64_t*)&feature);
1477 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_CORE);
1478 bool isSupported = false;
1479 ErrCode ret = wifiDevicePtr->IsFeatureSupported(feature, isSupported);
1480 if (ret != WIFI_OPT_SUCCESS) {
1481 WIFI_LOGE("Get supported features fail: %{public}d", ret);
1482 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_CORE);
1483 }
1484
1485 napi_value result;
1486 napi_get_boolean(env, isSupported, &result);
1487 return result;
1488 }
1489
GetDeviceMacAddress(napi_env env,napi_callback_info info)1490 NO_SANITIZE("cfi") napi_value GetDeviceMacAddress(napi_env env, napi_callback_info info)
1491 {
1492 TRACE_FUNC_CALL;
1493 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1494 std::string macAddr;
1495 ErrCode ret = wifiDevicePtr->GetDeviceMacAddress(macAddr);
1496 if (ret != WIFI_OPT_SUCCESS) {
1497 WIFI_LOGE("Get mac address fail: %{public}d", ret);
1498 }
1499
1500 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1501 napi_value addr;
1502 napi_create_string_utf8(env, macAddr.c_str(), NAPI_AUTO_LENGTH, &addr);
1503 return addr;
1504 }
1505
IsBandTypeSupported(napi_env env,napi_callback_info info)1506 NO_SANITIZE("cfi") napi_value IsBandTypeSupported(napi_env env, napi_callback_info info)
1507 {
1508 TRACE_FUNC_CALL;
1509 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1510
1511 size_t argc = 1;
1512 napi_value argv[1];
1513 napi_value thisVar;
1514 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
1515 WIFI_NAPI_ASSERT(env, argc == 1, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
1516
1517 napi_valuetype valueType;
1518 napi_typeof(env, argv[0], &valueType);
1519 WIFI_NAPI_ASSERT(env, valueType == napi_number, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
1520 int bandType = 1;
1521 napi_get_value_int32(env, argv[0], &bandType);
1522 WIFI_NAPI_ASSERT(env, bandType > (int)WifiBandTypeJS::BAND_NONE && bandType <= (int)WifiBandTypeJS::BAND_60GHZ,
1523 WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
1524 bool supported = false;
1525 ErrCode ret = wifiDevicePtr->IsBandTypeSupported(bandType, supported);
1526 if (ret != WIFI_OPT_SUCCESS) {
1527 WIFI_LOGE("Get band type supported fail: %{public}d", ret);
1528 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1529 }
1530 napi_value result;
1531 napi_get_boolean(env, supported, &result);
1532 return result;
1533 }
1534
Get5GHzChannelList(napi_env env,napi_callback_info info)1535 NO_SANITIZE("cfi") napi_value Get5GHzChannelList(napi_env env, napi_callback_info info)
1536 {
1537 TRACE_FUNC_CALL;
1538 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1539 std::vector<int> vec5GChannels;
1540 ErrCode ret = wifiDevicePtr->Get5GHzChannelList(vec5GChannels);
1541 if (ret != WIFI_OPT_SUCCESS) {
1542 WIFI_LOGE("Get 5g channellist fail: %{public}d", ret);
1543 }
1544
1545 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1546 WIFI_LOGI("Get 5g channellist size: %{public}zu", vec5GChannels.size());
1547 napi_value arrayResult;
1548 napi_create_array_with_length(env, vec5GChannels.size(), &arrayResult);
1549 for (size_t i = 0; i != vec5GChannels.size(); ++i) {
1550 napi_value result;
1551 napi_create_uint32(env, vec5GChannels[i], &result);
1552 napi_status status = napi_set_element(env, arrayResult, i, result);
1553 if (status != napi_ok) {
1554 WIFI_LOGE("wifi napi set 56 list element error: %{public}d", status);
1555 }
1556 }
1557 return arrayResult;
1558 }
1559
StartPortalCertification(napi_env env,napi_callback_info info)1560 NO_SANITIZE("cfi") napi_value StartPortalCertification(napi_env env, napi_callback_info info)
1561 {
1562 TRACE_FUNC_CALL;
1563 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1564
1565 ErrCode ret = wifiDevicePtr->StartPortalCertification();
1566 if (ret != WIFI_OPT_SUCCESS) {
1567 WIFI_LOGE("StartPortalCertification fail: %{public}d", ret);
1568 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1569 }
1570 napi_value result;
1571 napi_create_uint32(env, ret, &result);
1572 return result;
1573 }
1574
SetScanOnlyAvailable(napi_env env,napi_callback_info info)1575 NO_SANITIZE("cfi") napi_value SetScanOnlyAvailable(napi_env env, napi_callback_info info)
1576 {
1577 WIFI_LOGI("wifi napi In SetScanOnlyAvailable");
1578 TRACE_FUNC_CALL;
1579 size_t argc = 1;
1580 napi_value argv[1];
1581 napi_value thisVar;
1582 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
1583
1584 napi_valuetype valueType;
1585 napi_typeof(env, argv[0], &valueType);
1586
1587 bool bScanOnlyAvailableStatus = false;
1588 napi_get_value_bool(env, argv[0], &bScanOnlyAvailableStatus);
1589
1590 ErrCode ret = wifiScanPtr->SetScanOnlyAvailable(bScanOnlyAvailableStatus);
1591 if (ret != WIFI_OPT_SUCCESS) {
1592 WIFI_LOGE("Set wifi scanOnlyAvailable fail: %{public}d", ret);
1593 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_CORE);
1594 }
1595
1596 napi_value result;
1597 napi_create_int32(env, (int32_t)ret, &result);
1598 return result;
1599 }
1600
GetScanOnlyAvailable(napi_env env,napi_callback_info info)1601 NO_SANITIZE("cfi") napi_value GetScanOnlyAvailable(napi_env env, napi_callback_info info)
1602 {
1603 bool bScanOnlyAvailableStatus = false;
1604
1605 ErrCode ret = wifiScanPtr->GetScanOnlyAvailable(bScanOnlyAvailableStatus);
1606 if (ret != WIFI_OPT_SUCCESS) {
1607 WIFI_LOGE("Get wifi scanOnlyAvailable fail: %{public}d", ret);
1608 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_CORE);
1609 }
1610
1611 napi_value result;
1612 napi_get_boolean(env, bScanOnlyAvailableStatus, &result);
1613 return result;
1614 }
1615
GetWifiProtect(napi_env env,napi_callback_info info)1616 NO_SANITIZE("cfi") napi_value GetWifiProtect(napi_env env, napi_callback_info info)
1617 {
1618 TRACE_FUNC_CALL;
1619 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1620
1621 size_t argc = 1;
1622 napi_value argv[1];
1623 napi_value thisVar;
1624 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
1625 WIFI_NAPI_ASSERT(env, argc == 1, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
1626
1627 napi_valuetype valueType;
1628 napi_typeof(env, argv[0], &valueType);
1629 WIFI_NAPI_ASSERT(env, valueType == napi_number, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
1630
1631 int protectMode = 0;
1632 napi_get_value_int32(env, argv[0], &protectMode);
1633 WIFI_NAPI_ASSERT(env, protectMode >= (int)WifiProtectMode::WIFI_PROTECT_FULL &&
1634 protectMode <= (int)WifiProtectMode::WIFI_PROTECT_NO_HELD, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
1635
1636 ErrCode ret = wifiDevicePtr->GetWifiProtect(static_cast<WifiProtectMode>(protectMode));
1637 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1638 }
1639
PutWifiProtect(napi_env env,napi_callback_info info)1640 NO_SANITIZE("cfi") napi_value PutWifiProtect(napi_env env, napi_callback_info info)
1641 {
1642 TRACE_FUNC_CALL;
1643 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1644 ErrCode ret = wifiDevicePtr->PutWifiProtect();
1645 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1646 }
1647
FactoryReset(napi_env env,napi_callback_info info)1648 NO_SANITIZE("cfi") napi_value FactoryReset(napi_env env, napi_callback_info info)
1649 {
1650 TRACE_FUNC_CALL;
1651 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1652 ErrCode ret = wifiDevicePtr->FactoryReset();
1653 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1654 }
1655
EnableHiLinkHandshake(napi_env env,napi_callback_info info)1656 NO_SANITIZE("cfi") napi_value EnableHiLinkHandshake(napi_env env, napi_callback_info info)
1657 {
1658 TRACE_FUNC_CALL;
1659 size_t argc = 3;
1660 napi_value argv[argc];
1661 napi_value thisVar;
1662 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
1663 napi_valuetype valueType;
1664 napi_typeof(env, argv[2], &valueType);
1665 WIFI_NAPI_ASSERT(env, valueType == napi_object, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
1666 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1667
1668 bool uiFlag = false;
1669 napi_get_value_bool(env, argv[0], &uiFlag);
1670 std::string bssid;
1671 char tmp[NAPI_MAX_STR_LENT] = {0};
1672 size_t result = 0;
1673 napi_get_value_string_utf8(env, argv[1], tmp, NAPI_MAX_STR_LENT, &result);
1674 bssid = tmp;
1675 WifiDeviceConfig deviceConfig;
1676 napi_value napiRet = JsObjToDeviceConfig(env, argv[2], deviceConfig);
1677 napi_typeof(env, napiRet, &valueType);
1678 WIFI_NAPI_ASSERT(env, valueType != napi_undefined, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
1679
1680 ErrCode ret = wifiDevicePtr->EnableHiLinkHandshake(uiFlag, bssid, deviceConfig);
1681 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1682 }
1683
EnableSemiWifi(napi_env env,napi_callback_info info)1684 NO_SANITIZE("cfi") napi_value EnableSemiWifi(napi_env env, napi_callback_info info)
1685 {
1686 TRACE_FUNC_CALL;
1687 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1688 ErrCode ret = wifiDevicePtr->EnableSemiWifi();
1689 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1690 }
1691
GetWifiDetailState(napi_env env,napi_callback_info info)1692 NO_SANITIZE("cfi") napi_value GetWifiDetailState(napi_env env, napi_callback_info info)
1693 {
1694 TRACE_FUNC_CALL;
1695 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_CORE);
1696 WifiDetailState state = WifiDetailState::STATE_UNKNOWN;
1697 ErrCode ret = wifiDevicePtr->GetWifiDetailState(state);
1698 if (ret != WIFI_OPT_SUCCESS) {
1699 WIFI_LOGE("GetWifiDetailState failed:%{public}d", ret);
1700 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1701 }
1702 napi_value value;
1703 napi_create_int32(env, static_cast<int>(state), &value);
1704 return value;
1705 }
1706
1707 } // namespace Wifi
1708 } // namespace OHOS
1709