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 constexpr int DEFAULT_INVALID_VALUE = -1;
27
28 std::unique_ptr<WifiDevice> wifiDevicePtr = WifiDevice::GetInstance(WIFI_DEVICE_ABILITY_ID);
29 std::unique_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 jsSecurityType = SecTypeJs::SEC_TYPE_SAE;
94 break;
95 case WifiSecurity::EAP:
96 jsSecurityType = SecTypeJs::SEC_TYPE_EAP;
97 break;
98 default:
99 jsSecurityType = SecTypeJs::SEC_TYPE_INVALID;
100 break;
101 }
102 return jsSecurityType;
103 }
104
NativeInfoElemsToJsObj(const napi_env & env,const std::vector<WifiInfoElem> & infoElems,napi_value & eachObj)105 static ErrCode NativeInfoElemsToJsObj(const napi_env& env,
106 const std::vector<WifiInfoElem>& infoElems, napi_value& eachObj)
107 {
108 napi_value arr;
109 napi_create_array(env, &arr);
110 uint8_t idx_ie = 0;
111 napi_status status;
112 int valueStep = 2;
113 for (size_t i = 0; i < infoElems.size(); i++) {
114 napi_value ieObj;
115 napi_create_object(env, &ieObj);
116 SetValueInt32(env, "eid", infoElems[i].id, ieObj);
117 const char *uStr = &infoElems[i].content[0];
118 size_t len = infoElems[i].content.size();
119 size_t inLen = (infoElems[i].content.size()) * valueStep + 1;
120 char *buf = (char *)calloc(inLen + 1, sizeof(char));
121 if (buf == NULL) {
122 return WIFI_OPT_FAILED;
123 }
124 int pos = 0;
125 for (size_t k = 0; k < len; ++k) {
126 pos = (k << 1);
127 if (snprintf_s(buf + pos, inLen - pos, inLen - pos - 1, "%02x", uStr[k]) < 0) {
128 free(buf);
129 buf = NULL;
130 return WIFI_OPT_FAILED;
131 }
132 }
133 SetValueUtf8String(env, "content", (const char *)buf, ieObj, inLen - 1);
134 status = napi_set_element(env, arr, idx_ie++, ieObj);
135 if (status != napi_ok) {
136 WIFI_LOGE("set content error");
137 free(buf);
138 buf = NULL;
139 return WIFI_OPT_FAILED;
140 }
141 free(buf);
142 buf = NULL;
143 }
144 status = napi_set_named_property(env, eachObj, "infoElems", arr);
145 if (status != napi_ok) {
146 WIFI_LOGE("set infoElems error");
147 return WIFI_OPT_FAILED;
148 }
149 return WIFI_OPT_SUCCESS;
150 }
151
NativeScanInfosToJsObj(const napi_env & env,const std::vector<WifiScanInfo> & vecScnIanfos,napi_value & arrayResult)152 static ErrCode NativeScanInfosToJsObj(const napi_env& env,
153 const std::vector<WifiScanInfo>& vecScnIanfos, napi_value& arrayResult)
154 {
155 uint32_t idx = 0;
156 for (auto& each : vecScnIanfos) {
157 napi_value eachObj;
158 napi_create_object(env, &eachObj);
159 SetValueUtf8String(env, "ssid", each.ssid.c_str(), eachObj);
160 SetValueUtf8String(env, "bssid", each.bssid.c_str(), eachObj);
161 SetValueInt32(env, "bssidType", each.bssidType, eachObj);
162 SetValueUtf8String(env, "capabilities", each.capabilities.c_str(), eachObj);
163 SetValueInt32(env, "securityType", static_cast<int>(SecurityTypeNativeToJs(each.securityType)), eachObj);
164 SetValueInt32(env, "rssi", each.rssi, eachObj);
165 SetValueInt32(env, "band", each.band, eachObj);
166 SetValueInt32(env, "frequency", each.frequency, eachObj);
167 SetValueInt32(env, "channelWidth", static_cast<int>(each.channelWidth), eachObj);
168 SetValueInt32(env, "centerFrequency0", each.centerFrequency0, eachObj);
169 SetValueInt32(env, "centerFrequency1", each.centerFrequency1, eachObj);
170 NativeInfoElemsToJsObj(env, each.infoElems, eachObj);
171 SetValueInt64(env, "timestamp", each.timestamp, eachObj);
172 napi_status status = napi_set_element(env, arrayResult, idx++, eachObj);
173 if (status != napi_ok) {
174 WIFI_LOGE("Wifi napi set element error: %{public}d, idx: %{public}d", status, idx - 1);
175 return WIFI_OPT_FAILED;
176 }
177 }
178 return WIFI_OPT_SUCCESS;
179 }
180
GetScanInfos(napi_env env,napi_callback_info info)181 NO_SANITIZE("cfi") napi_value GetScanInfos(napi_env env, napi_callback_info info)
182 {
183 TRACE_FUNC_CALL;
184 size_t argc = 2;
185 napi_value argv[argc];
186 napi_value thisVar = nullptr;
187 void *data = nullptr;
188 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
189 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
190
191 ScanInfoAsyncContext *asyncContext = new ScanInfoAsyncContext(env);
192 WIFI_NAPI_ASSERT(env, asyncContext != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
193 napi_create_string_latin1(env, "getScanInfos", NAPI_AUTO_LENGTH, &asyncContext->resourceName);
194
195 asyncContext->executeFunc = [&](void* data) -> void {
196 ScanInfoAsyncContext *context = static_cast<ScanInfoAsyncContext *>(data);
197 TRACE_FUNC_CALL_NAME("wifiScanPtr->GetScanInfoList");
198 bool compatible = true;
199 context->errorCode = wifiScanPtr->GetScanInfoList(context->vecScanInfos, compatible);
200 WIFI_LOGI("GetScanInfoList, size: %{public}zu", context->vecScanInfos.size());
201 };
202
203 asyncContext->completeFunc = [&](void* data) -> void {
204 ScanInfoAsyncContext *context = static_cast<ScanInfoAsyncContext *>(data);
205 napi_create_array_with_length(context->env, context->vecScanInfos.size(), &context->result);
206 if (context->errorCode == WIFI_OPT_SUCCESS) {
207 context->errorCode = NativeScanInfosToJsObj(context->env, context->vecScanInfos, context->result);
208 }
209 WIFI_LOGI("Push scan info list to client");
210 };
211
212 size_t nonCallbackArgNum = 0;
213 asyncContext->sysCap = SYSCAP_WIFI_STA;
214 return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum);
215 }
216
GetScanInfoResults(napi_env env,napi_callback_info info)217 NO_SANITIZE("cfi") napi_value GetScanInfoResults(napi_env env, napi_callback_info info)
218 {
219 TRACE_FUNC_CALL;
220 size_t argc = 2;
221 napi_value argv[argc];
222 napi_value thisVar = nullptr;
223 void *data = nullptr;
224 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
225 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
226
227 ScanInfoAsyncContext *asyncContext = new ScanInfoAsyncContext(env);
228 WIFI_NAPI_ASSERT(env, asyncContext != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
229 napi_create_string_latin1(env, "getScanInfos", NAPI_AUTO_LENGTH, &asyncContext->resourceName);
230
231 asyncContext->executeFunc = [&](void* data) -> void {
232 ScanInfoAsyncContext *context = static_cast<ScanInfoAsyncContext *>(data);
233 TRACE_FUNC_CALL_NAME("wifiScanPtr->GetScanInfoList");
234 bool compatible = false;
235 context->errorCode = wifiScanPtr->GetScanInfoList(context->vecScanInfos, compatible);
236 WIFI_LOGI("GetScanInfoList, size: %{public}zu", context->vecScanInfos.size());
237 };
238
239 asyncContext->completeFunc = [&](void* data) -> void {
240 ScanInfoAsyncContext *context = static_cast<ScanInfoAsyncContext *>(data);
241 napi_create_array_with_length(context->env, context->vecScanInfos.size(), &context->result);
242 if (context->errorCode == WIFI_OPT_SUCCESS) {
243 context->errorCode = NativeScanInfosToJsObj(context->env, context->vecScanInfos, context->result);
244 }
245 WIFI_LOGI("Push scan info list to client");
246 };
247
248 size_t nonCallbackArgNum = 0;
249 asyncContext->sysCap = SYSCAP_WIFI_STA;
250 return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum);
251 }
252
GetScanResults(napi_env env,napi_callback_info info)253 NO_SANITIZE("cfi") napi_value GetScanResults(napi_env env, napi_callback_info info)
254 {
255 TRACE_FUNC_CALL;
256 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
257 bool compatible = true;
258 std::vector<WifiScanInfo> scanInfos;
259 ErrCode ret = wifiScanPtr->GetScanInfoList(scanInfos, compatible);
260 if (ret != WIFI_OPT_SUCCESS) {
261 WIFI_LOGE("GetScanInfoList return fail: %{public}d", ret);
262 }
263
264 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
265 WIFI_LOGI("GetScanInfoList, size: %{public}zu", scanInfos.size());
266 napi_value arrayResult;
267 napi_create_array_with_length(env, scanInfos.size(), &arrayResult);
268 ret = NativeScanInfosToJsObj(env, scanInfos, arrayResult);
269 if (ret != WIFI_OPT_SUCCESS) {
270 WIFI_LOGE("NativeScanInfosToJsObj return fail: %{public}d", ret);
271 }
272 return arrayResult;
273 }
274
GetScanInfoList(napi_env env,napi_callback_info info)275 NO_SANITIZE("cfi") napi_value GetScanInfoList(napi_env env, napi_callback_info info)
276 {
277 TRACE_FUNC_CALL;
278 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
279 bool compatible = false;
280 std::vector<WifiScanInfo> scanInfos;
281 ErrCode ret = wifiScanPtr->GetScanInfoList(scanInfos, compatible);
282 if (ret != WIFI_OPT_SUCCESS) {
283 WIFI_LOGE("GetScanInfoList return fail: %{public}d", ret);
284 }
285
286 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
287 WIFI_LOGI("GetScanInfoList, size: %{public}zu", scanInfos.size());
288 napi_value arrayResult;
289 napi_create_array_with_length(env, scanInfos.size(), &arrayResult);
290 ret = NativeScanInfosToJsObj(env, scanInfos, arrayResult);
291 if (ret != WIFI_OPT_SUCCESS) {
292 WIFI_LOGE("NativeScanInfosToJsObj return fail: %{public}d", ret);
293 }
294 return arrayResult;
295 }
296
ConvertEncryptionMode(const SecTypeJs & securityType,std::string & keyMgmt)297 static void ConvertEncryptionMode(const SecTypeJs& securityType, std::string& keyMgmt)
298 {
299 switch (securityType) {
300 case SecTypeJs::SEC_TYPE_OPEN:
301 keyMgmt = KEY_MGMT_NONE;
302 break;
303 case SecTypeJs::SEC_TYPE_WEP:
304 keyMgmt = KEY_MGMT_WEP;
305 break;
306 case SecTypeJs::SEC_TYPE_PSK:
307 keyMgmt = KEY_MGMT_WPA_PSK;
308 break;
309 case SecTypeJs::SEC_TYPE_SAE:
310 keyMgmt = KEY_MGMT_SAE;
311 break;
312 case SecTypeJs::SEC_TYPE_EAP:
313 keyMgmt = KEY_MGMT_EAP;
314 break;
315 default:
316 keyMgmt = KEY_MGMT_NONE;
317 break;
318 }
319 }
320
ProcessPassphrase(const SecTypeJs & securityType,WifiDeviceConfig & cppConfig)321 static void ProcessPassphrase(const SecTypeJs& securityType, WifiDeviceConfig& cppConfig)
322 {
323 if (securityType == SecTypeJs::SEC_TYPE_WEP) {
324 cppConfig.wepKeys[0] = cppConfig.preSharedKey;
325 cppConfig.wepTxKeyIndex = 0;
326 cppConfig.preSharedKey = "";
327 }
328 }
329
ProcessEapPeapConfig(const napi_env & env,const napi_value & object,WifiEapConfig & eapConfig)330 static void ProcessEapPeapConfig(const napi_env& env, const napi_value& object, WifiEapConfig& eapConfig)
331 {
332 // identity, password, phase2Method filed is necessary
333 eapConfig.eap = EAP_METHOD_PEAP;
334 JsObjectToString(env, object, "identity", NAPI_MAX_STR_LENT, eapConfig.identity);
335 JsObjectToString(env, object, "password", NAPI_MAX_STR_LENT, eapConfig.password);
336
337 int phase2 = static_cast<int>(Phase2Method::NONE);
338 JsObjectToInt(env, object, "phase2Method", phase2);
339 eapConfig.phase2Method = Phase2Method(phase2);
340 }
341
ProcessEapTlsConfig(const napi_env & env,const napi_value & object,WifiEapConfig & eapConfig)342 static void ProcessEapTlsConfig(const napi_env& env, const napi_value& object, WifiEapConfig& eapConfig)
343 {
344 eapConfig.eap = EAP_METHOD_TLS;
345 std::string certPassword;
346 JsObjectToString(env, object, "identity", NAPI_MAX_STR_LENT, eapConfig.identity);
347 JsObjectToString(env, object, "certPassword", NAPI_MAX_STR_LENT, certPassword);
348 if (strncpy_s(eapConfig.certPassword, sizeof(eapConfig.certPassword), certPassword.c_str(),
349 sizeof(eapConfig.certPassword) - 1) != EOK) {
350 WIFI_LOGE("ProcessEapTlsConfig strcpy_s failed!");
351 }
352 std::string().swap(certPassword);
353 eapConfig.certEntry = JsObjectToU8Vector(env, object, "certEntry");
354 }
355
ProcessEapConfig(const napi_env & env,const napi_value & object,WifiDeviceConfig & devConfig)356 napi_value ProcessEapConfig(const napi_env& env, const napi_value& object, WifiDeviceConfig& devConfig)
357 {
358 bool hasProperty = false;
359 NAPI_CALL(env, napi_has_named_property(env, object, "eapConfig", &hasProperty));
360 if (!hasProperty) {
361 WIFI_LOGI("Js has no property: eapConfig.");
362 return UndefinedNapiValue(env);
363 }
364
365 napi_value napiEap;
366 napi_get_named_property(env, object, "eapConfig", &napiEap);
367 int eapMethod = static_cast<int>(EapMethodJs::EAP_NONE);
368 JsObjectToInt(env, napiEap, "eapMethod", eapMethod);
369 WIFI_LOGI("ProcessEapConfig, eapMethod: %{public}d.", eapMethod);
370 switch (EapMethodJs(eapMethod)) {
371 case EapMethodJs::EAP_PEAP:
372 ProcessEapPeapConfig(env, napiEap, devConfig.wifiEapConfig);
373 break;
374 case EapMethodJs::EAP_TLS:
375 ProcessEapTlsConfig(env, napiEap, devConfig.wifiEapConfig);
376 break;
377 default:
378 WIFI_LOGE("EapMethod: %{public}d unsupported", eapMethod);
379 return UndefinedNapiValue(env);
380 }
381 return CreateInt32(env);
382 }
383
ConfigStaticIp(const napi_env & env,const napi_value & object,WifiDeviceConfig & cppConfig)384 napi_value ConfigStaticIp(const napi_env& env, const napi_value& object, WifiDeviceConfig& cppConfig)
385 {
386 bool hasProperty = false;
387 NAPI_CALL(env, napi_has_named_property(env, object, "staticIp", &hasProperty));
388 if (!hasProperty) {
389 WIFI_LOGE("ConfigStaticIp, Js has no property: staticIp.");
390 return UndefinedNapiValue(env);
391 }
392 napi_value staticIp;
393 napi_value dnsServers;
394 napi_value primaryDns;
395 napi_value secondDns;
396 napi_get_named_property(env, object, "staticIp", &staticIp);
397 JsObjectToUint(env, staticIp, "ipAddress",
398 cppConfig.wifiIpConfig.staticIpAddress.ipAddress.address.addressIpv4);
399 cppConfig.wifiIpConfig.staticIpAddress.ipAddress.address.family = 0;
400 JsObjectToUint(env, staticIp, "gateway", cppConfig.wifiIpConfig.staticIpAddress.gateway.addressIpv4);
401 JsObjectToInt(env, staticIp, "prefixLength", cppConfig.wifiIpConfig.staticIpAddress.ipAddress.prefixLength);
402
403 NAPI_CALL(env, napi_has_named_property(env, staticIp, "dnsServers", &hasProperty));
404 if (!hasProperty) {
405 WIFI_LOGE("ConfigStaticIp, Js has no property: dnsServers.");
406 return UndefinedNapiValue(env);
407 }
408 uint32_t arrayLength = 0;
409 const int DNS_NUM = 2;
410 napi_get_named_property(env, staticIp, "dnsServers", &dnsServers);
411 napi_get_array_length(env, dnsServers, &arrayLength);
412 if (arrayLength != DNS_NUM) {
413 WIFI_LOGE("ConfigStaticIp, It needs two dns servers.");
414 return UndefinedNapiValue(env);
415 }
416 napi_get_element(env, dnsServers, 0, &primaryDns);
417 napi_get_element(env, dnsServers, 1, &secondDns);
418 napi_get_value_uint32(env, primaryDns, &cppConfig.wifiIpConfig.staticIpAddress.dnsServer1.addressIpv4);
419 napi_get_value_uint32(env, secondDns, &cppConfig.wifiIpConfig.staticIpAddress.dnsServer2.addressIpv4);
420
421 return CreateInt32(env);
422 }
423
ProcessProxyConfig(const napi_env & env,const napi_value & object,WifiDeviceConfig & cppConfig)424 ErrCode ProcessProxyConfig(const napi_env& env, const napi_value& object, WifiDeviceConfig& cppConfig)
425 {
426 bool hasProperty = false;
427 NAPI_CALL_BASE(env, napi_has_named_property(env, object, "proxyConfig", &hasProperty), {});
428 ErrCode ret = WIFI_OPT_SUCCESS;
429 if (hasProperty) {
430 napi_value proxyConfig;
431 napi_get_named_property(env, object, "proxyConfig", &proxyConfig);
432 int proxyConfigMethod = static_cast<int>(ConfigureProxyMethod::CLOSED);
433 JsObjectToInt(env, proxyConfig, "proxyMethod", proxyConfigMethod);
434 cppConfig.wifiProxyconfig.configureMethod = ConfigureProxyMethod::CLOSED;
435 switch (ConfigureProxyMethod(proxyConfigMethod)) {
436 case ConfigureProxyMethod::AUTOCONFIGUE:
437 cppConfig.wifiProxyconfig.configureMethod = ConfigureProxyMethod::AUTOCONFIGUE;
438 JsObjectToString(env, proxyConfig, "pacWebAddress", NAPI_MAX_STR_LENT,
439 cppConfig.wifiProxyconfig.autoProxyConfig.pacWebAddress);
440 ret = WIFI_OPT_NOT_SUPPORTED;
441 break;
442 case ConfigureProxyMethod::MANUALCONFIGUE:
443 cppConfig.wifiProxyconfig.configureMethod = ConfigureProxyMethod::MANUALCONFIGUE;
444 JsObjectToString(env, proxyConfig, "serverHostName", NAPI_MAX_STR_LENT,
445 cppConfig.wifiProxyconfig.manualProxyConfig.serverHostName);
446 JsObjectToString(env, proxyConfig, "exclusionObjects", NAPI_MAX_STR_LENT,
447 cppConfig.wifiProxyconfig.manualProxyConfig.exclusionObjectList);
448 JsObjectToInt(env, proxyConfig, "serverPort", cppConfig.wifiProxyconfig.manualProxyConfig.serverPort);
449 if (cppConfig.wifiProxyconfig.manualProxyConfig.serverPort < 0) {
450 ret = WIFI_OPT_INVALID_PARAM;
451 }
452 break;
453 case ConfigureProxyMethod::CLOSED:
454 WIFI_LOGI("ProcessProxyConfig, configureMethod is closed.");
455 break;
456 default:
457 WIFI_LOGE("ProcessProxyConfig, configureMethod %{public}d is not supported.", proxyConfigMethod);
458 ret = WIFI_OPT_INVALID_PARAM;
459 }
460 }
461
462 return ret;
463 }
464
JsObjToDeviceConfig(const napi_env & env,const napi_value & object,WifiDeviceConfig & cppConfig)465 static napi_value JsObjToDeviceConfig(const napi_env& env, const napi_value& object, WifiDeviceConfig& cppConfig)
466 {
467 JsObjectToString(env, object, "ssid", NAPI_MAX_STR_LENT, cppConfig.ssid); /* ssid max length is 32 + '\0' */
468 JsObjectToString(env, object, "bssid", NAPI_MAX_STR_LENT, cppConfig.bssid); /* max bssid length: 18 */
469 cppConfig.bssidType = RANDOM_DEVICE_ADDRESS;
470 JsObjectToInt(env, object, "bssidType", cppConfig.bssidType);
471 WIFI_LOGE("JsObjToDeviceConfig, bssid length: %{public}d, bssidType: %{public}d",
472 static_cast<int>(cppConfig.bssid.length()), cppConfig.bssidType);
473 JsObjectToString(env, object, "preSharedKey", NAPI_MAX_STR_LENT, cppConfig.preSharedKey);
474 JsObjectToBool(env, object, "isHiddenSsid", cppConfig.hiddenSSID);
475 int type = static_cast<int>(SecTypeJs::SEC_TYPE_INVALID);
476 JsObjectToInt(env, object, "securityType", type);
477 ConvertEncryptionMode(SecTypeJs(type), cppConfig.keyMgmt);
478 ProcessPassphrase(SecTypeJs(type), cppConfig);
479 /* "creatorUid" is not supported currently */
480 /* "disableReason" is not supported currently */
481 JsObjectToInt(env, object, "netId", cppConfig.networkId);
482 /* "randomMacType" is not supported currently */
483 /* "randomMacAddr" is not supported currently */
484 int ipType = static_cast<int>(AssignIpMethod::UNASSIGNED);
485 JsObjectToInt(env, object, "ipType", ipType);
486 WIFI_LOGI("JsObjToDeviceConfig, ipType: %{public}d, type: %{public}d.", ipType, type);
487 if (IpTypeJs(ipType) == IpTypeJs::IP_TYPE_DHCP) {
488 cppConfig.wifiIpConfig.assignMethod = AssignIpMethod::DHCP;
489 } else if (IpTypeJs(ipType) == IpTypeJs::IP_TYPE_STATIC) {
490 cppConfig.wifiIpConfig.assignMethod = AssignIpMethod::STATIC;
491 napi_valuetype valueType;
492 napi_value ret = ConfigStaticIp(env, object, cppConfig);
493 napi_typeof(env, ret, &valueType);
494 if (valueType == napi_undefined) {
495 WIFI_LOGI("JsObjToDeviceConfig, ConfigStaticIp return napi_undefined.");
496 return UndefinedNapiValue(env);
497 }
498 }
499 ErrCode ret = ProcessProxyConfig(env, object, cppConfig);
500 if (ret != WIFI_OPT_SUCCESS) {
501 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
502 }
503 if (SecTypeJs(type) == SecTypeJs::SEC_TYPE_EAP) {
504 return ProcessEapConfig(env, object, cppConfig);
505 }
506 return CreateInt32(env);
507 }
508
AddDeviceConfig(napi_env env,napi_callback_info info)509 NO_SANITIZE("cfi") napi_value AddDeviceConfig(napi_env env, napi_callback_info info)
510 {
511 TRACE_FUNC_CALL;
512 size_t argc = 3;
513 napi_value argv[argc];
514 napi_value thisVar = nullptr;
515 void *data = nullptr;
516 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
517 WIFI_NAPI_ASSERT(env, argc >= 1, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
518 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
519
520 napi_valuetype valueType;
521 napi_typeof(env, argv[0], &valueType);
522 WIFI_NAPI_ASSERT(env, valueType == napi_object, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
523
524 DeviceConfigContext *asyncContext = new DeviceConfigContext(env);
525 WIFI_NAPI_ASSERT(env, asyncContext != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
526 napi_create_string_latin1(env, "addDeviceConfig", NAPI_AUTO_LENGTH, &asyncContext->resourceName);
527
528 WifiDeviceConfig *config = new WifiDeviceConfig();
529 if (config == nullptr) {
530 delete asyncContext;
531 return UndefinedNapiValue(env);
532 }
533 napi_value ret = JsObjToDeviceConfig(env, argv[0], *config);
534 napi_typeof(env, ret, &valueType);
535 WIFI_NAPI_ASSERT(env, valueType != napi_undefined, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
536
537 asyncContext->config = config;
538 asyncContext->isCandidate = false;
539 asyncContext->executeFunc = [&](void* data) -> void {
540 DeviceConfigContext *context = static_cast<DeviceConfigContext *>(data);
541 TRACE_FUNC_CALL_NAME("wifiDevicePtr->AddDeviceConfig");
542 ErrCode ret = wifiDevicePtr->AddDeviceConfig(*context->config, context->networkId, context->isCandidate);
543 if (context->networkId < 0 || ret != WIFI_OPT_SUCCESS) {
544 context->networkId = -1;
545 }
546 context->errorCode = ret;
547 };
548
549 asyncContext->completeFunc = [&](void* data) -> void {
550 DeviceConfigContext *context = static_cast<DeviceConfigContext *>(data);
551 napi_create_int32(context->env, context->networkId, &context->result);
552 if (context->config != nullptr) {
553 delete context->config;
554 context->config = nullptr;
555 }
556 WIFI_LOGI("Push add device config result to client");
557 };
558
559 size_t nonCallbackArgNum = 1;
560 asyncContext->sysCap = SYSCAP_WIFI_STA;
561 return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum);
562 }
563
AddUntrustedConfig(napi_env env,napi_callback_info info)564 NO_SANITIZE("cfi") napi_value AddUntrustedConfig(napi_env env, napi_callback_info info)
565 {
566 TRACE_FUNC_CALL;
567 size_t argc = 2;
568 napi_value argv[argc];
569 napi_value thisVar = nullptr;
570 void *data = nullptr;
571 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
572 WIFI_NAPI_ASSERT(env, argc >= 1, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
573 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
574
575 napi_valuetype valueType;
576 napi_typeof(env, argv[0], &valueType);
577 WIFI_NAPI_ASSERT(env, valueType == napi_object, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
578
579 DeviceConfigContext *asyncContext = new DeviceConfigContext(env);
580 WIFI_NAPI_ASSERT(env, asyncContext != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
581 napi_create_string_latin1(env, "AddUntrustedConfig", NAPI_AUTO_LENGTH, &asyncContext->resourceName);
582
583 WifiDeviceConfig *config = new WifiDeviceConfig();
584 if (config == nullptr) {
585 delete asyncContext;
586 return UndefinedNapiValue(env);
587 }
588 napi_value ret = JsObjToDeviceConfig(env, argv[0], *config);
589 napi_typeof(env, ret, &valueType);
590 WIFI_NAPI_ASSERT(env, valueType != napi_undefined, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
591 asyncContext->config = config;
592 asyncContext->isCandidate = true;
593
594 asyncContext->executeFunc = [&](void* data) -> void {
595 DeviceConfigContext *context = static_cast<DeviceConfigContext *>(data);
596 TRACE_FUNC_CALL_NAME("wifiDevicePtr->AddUntrustedConfig");
597 ErrCode ret = wifiDevicePtr->AddDeviceConfig(*context->config, context->networkId, context->isCandidate);
598 if (context->networkId < 0 || ret != WIFI_OPT_SUCCESS) {
599 context->networkId = -1;
600 }
601 context->errorCode = ret;
602 };
603
604 asyncContext->completeFunc = [&](void* data) -> void {
605 DeviceConfigContext *context = static_cast<DeviceConfigContext *>(data);
606 napi_get_boolean(context->env, (context->networkId >= 0), &context->result);
607 if (context->config != nullptr) {
608 delete context->config;
609 context->config = nullptr;
610 }
611 WIFI_LOGI("Push add untrusted device config result to client");
612 };
613
614 size_t nonCallbackArgNum = 1;
615 asyncContext->sysCap = SYSCAP_WIFI_STA;
616 return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum);
617 }
618
RemoveUntrustedConfig(napi_env env,napi_callback_info info)619 NO_SANITIZE("cfi") napi_value RemoveUntrustedConfig(napi_env env, napi_callback_info info)
620 {
621 TRACE_FUNC_CALL;
622 size_t argc = 3;
623 napi_value argv[argc];
624 napi_value thisVar = nullptr;
625 void *data = nullptr;
626 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
627 WIFI_NAPI_ASSERT(env, argc >= 1, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
628 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
629
630 napi_valuetype valueType;
631 napi_typeof(env, argv[0], &valueType);
632 WIFI_NAPI_ASSERT(env, valueType == napi_object, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
633
634 DeviceConfigContext *asyncContext = new DeviceConfigContext(env);
635 WIFI_NAPI_ASSERT(env, asyncContext != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
636 napi_create_string_latin1(env, "RemoveUntrustedConfig", NAPI_AUTO_LENGTH, &asyncContext->resourceName);
637
638 WifiDeviceConfig *config = new WifiDeviceConfig();
639 if (config == nullptr) {
640 delete asyncContext;
641 return UndefinedNapiValue(env);
642 }
643 napi_value ret = JsObjToDeviceConfig(env, argv[0], *config);
644 napi_typeof(env, ret, &valueType);
645 WIFI_NAPI_ASSERT(env, valueType != napi_undefined, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
646 asyncContext->config = config;
647 asyncContext->executeFunc = [&](void* data) -> void {
648 DeviceConfigContext *context = static_cast<DeviceConfigContext *>(data);
649 TRACE_FUNC_CALL_NAME("wifiDevicePtr->RemoveCandidateConfig");
650 context->errorCode = wifiDevicePtr->RemoveCandidateConfig(*context->config);
651 };
652
653 asyncContext->completeFunc = [&](void* data) -> void {
654 DeviceConfigContext *context = static_cast<DeviceConfigContext *>(data);
655 napi_get_boolean(context->env, context->errorCode == WIFI_OPT_SUCCESS, &context->result);
656 if (context->config != nullptr) {
657 delete context->config;
658 context->config = nullptr;
659 }
660 WIFI_LOGI("Push remove untrusted device config result to client");
661 };
662
663 size_t nonCallbackArgNum = 1;
664 asyncContext->sysCap = SYSCAP_WIFI_STA;
665 return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum);
666 }
667
AddCandidateConfig(napi_env env,napi_callback_info info)668 NO_SANITIZE("cfi") napi_value AddCandidateConfig(napi_env env, napi_callback_info info)
669 {
670 TRACE_FUNC_CALL;
671 size_t argc = 2;
672 napi_value argv[argc];
673 napi_value thisVar = nullptr;
674 void *data = nullptr;
675 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
676 WIFI_NAPI_ASSERT(env, argc >= 1, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
677 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
678
679 napi_valuetype valueType;
680 napi_typeof(env, argv[0], &valueType);
681 WIFI_NAPI_ASSERT(env, valueType == napi_object, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
682
683 DeviceConfigContext *asyncContext = new DeviceConfigContext(env);
684 WIFI_NAPI_ASSERT(env, asyncContext != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
685 napi_create_string_latin1(env, "AddCandidateConfig", NAPI_AUTO_LENGTH, &asyncContext->resourceName);
686
687 WifiDeviceConfig *config = new WifiDeviceConfig();
688 if (config == nullptr) {
689 delete asyncContext;
690 return UndefinedNapiValue(env);
691 }
692
693 napi_value ret = JsObjToDeviceConfig(env, argv[0], *config);
694 napi_typeof(env, ret, &valueType);
695 WIFI_NAPI_ASSERT(env, valueType != napi_undefined, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
696 asyncContext->config = config;
697 asyncContext->isCandidate = true;
698 asyncContext->executeFunc = [&](void* data) -> void {
699 DeviceConfigContext *context = static_cast<DeviceConfigContext *>(data);
700 TRACE_FUNC_CALL_NAME("wifiDevicePtr->AddCandidateConfig");
701 ErrCode ret = wifiDevicePtr->AddDeviceConfig(*context->config, context->networkId, context->isCandidate);
702 if (context->networkId < 0 || ret != WIFI_OPT_SUCCESS) {
703 WIFI_LOGE("Add candidate device config failed: %{public}d", static_cast<int>(ret));
704 context->networkId = -1;
705 }
706 context->errorCode = ret;
707 };
708
709 asyncContext->completeFunc = [&](void* data) -> void {
710 DeviceConfigContext *context = static_cast<DeviceConfigContext *>(data);
711 napi_create_int32(context->env, context->networkId, &context->result);
712 if (context->config != nullptr) {
713 delete context->config;
714 context->config = nullptr;
715 }
716 WIFI_LOGI("Push add candidate 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
RemoveCandidateConfig(napi_env env,napi_callback_info info)724 NO_SANITIZE("cfi") napi_value RemoveCandidateConfig(napi_env env, napi_callback_info info)
725 {
726 TRACE_FUNC_CALL;
727 size_t argc = 3;
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_number, 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, "RemoveCandidateConfig", NAPI_AUTO_LENGTH, &asyncContext->resourceName);
742
743 napi_get_value_int32(env, argv[0], &asyncContext->networkId);
744 asyncContext->executeFunc = [&](void* data) -> void {
745 DeviceConfigContext *context = static_cast<DeviceConfigContext *>(data);
746 context->errorCode = wifiDevicePtr->RemoveCandidateConfig(context->networkId);
747 };
748
749 asyncContext->completeFunc = [&](void* data) -> void {
750 DeviceConfigContext *context = static_cast<DeviceConfigContext *>(data);
751 napi_get_boolean(context->env, (context->errorCode == WIFI_OPT_SUCCESS), &context->result);
752 if (context->config != nullptr) {
753 delete context->config;
754 context->config = nullptr;
755 }
756 WIFI_LOGI("Push remove candidate device config result to client");
757 };
758
759 size_t nonCallbackArgNum = 1;
760 asyncContext->sysCap = SYSCAP_WIFI_STA;
761 return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum);
762 }
763
ConnectToCandidateConfig(napi_env env,napi_callback_info info)764 NO_SANITIZE("cfi") napi_value ConnectToCandidateConfig(napi_env env, napi_callback_info info)
765 {
766 TRACE_FUNC_CALL;
767 size_t argc = 1;
768 napi_value argv[1];
769 napi_value thisVar;
770 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
771 WIFI_NAPI_ASSERT(env, argc == 1, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
772
773 napi_valuetype valueType;
774 napi_typeof(env, argv[0], &valueType);
775 WIFI_NAPI_ASSERT(env, valueType == napi_number, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
776
777 int networkId = -1;
778 napi_get_value_int32(env, argv[0], &networkId);
779 bool isCandidate = true;
780
781 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
782 ErrCode ret = wifiDevicePtr->ConnectToNetwork(networkId, isCandidate);
783 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
784 }
785
ConnectToNetwork(napi_env env,napi_callback_info info)786 NO_SANITIZE("cfi") napi_value ConnectToNetwork(napi_env env, napi_callback_info info)
787 {
788 TRACE_FUNC_CALL;
789 size_t argc = 1;
790 napi_value argv[1];
791 napi_value thisVar;
792 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
793 WIFI_NAPI_ASSERT(env, argc == 1, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
794
795 napi_valuetype valueType;
796 napi_typeof(env, argv[0], &valueType);
797 WIFI_NAPI_ASSERT(env, valueType == napi_number, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
798
799 int networkId = -1;
800 napi_get_value_int32(env, argv[0], &networkId);
801 bool isCandidate = false;
802
803 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
804 ErrCode ret = wifiDevicePtr->ConnectToNetwork(networkId, isCandidate);
805 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
806 }
807
ConnectToDevice(napi_env env,napi_callback_info info)808 NO_SANITIZE("cfi") napi_value ConnectToDevice(napi_env env, napi_callback_info info)
809 {
810 TRACE_FUNC_CALL;
811 size_t argc = 1;
812 napi_value argv[1];
813 napi_value thisVar;
814 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
815
816 napi_valuetype valueType;
817 napi_typeof(env, argv[0], &valueType);
818 WIFI_NAPI_ASSERT(env, valueType == napi_object, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
819
820 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
821 WifiDeviceConfig config;
822 napi_value napiRet = JsObjToDeviceConfig(env, argv[0], config);
823 napi_typeof(env, napiRet, &valueType);
824 WIFI_NAPI_ASSERT(env, valueType != napi_undefined, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
825 ErrCode ret = wifiDevicePtr->ConnectToDevice(config);
826 if (ret != WIFI_OPT_SUCCESS) {
827 WIFI_LOGE("Connect to device fail: %{public}d", ret);
828 }
829 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
830 }
831
IsConnected(napi_env env,napi_callback_info info)832 NO_SANITIZE("cfi") napi_value IsConnected(napi_env env, napi_callback_info info)
833 {
834 TRACE_FUNC_CALL;
835 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
836 bool isConnected = false;
837 ErrCode ret = wifiDevicePtr->IsConnected(isConnected);
838 if (ret != WIFI_OPT_SUCCESS) {
839 WIFI_LOGE("IsConnected return error: %{public}d", ret);
840 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
841 }
842 napi_value result;
843 napi_get_boolean(env, isConnected, &result);
844 return result;
845 }
846
Disconnect(napi_env env,napi_callback_info info)847 NO_SANITIZE("cfi") napi_value Disconnect(napi_env env, napi_callback_info info)
848 {
849 TRACE_FUNC_CALL;
850 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
851 ErrCode ret = wifiDevicePtr->Disconnect();
852 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
853 }
854
GetSignalLevel(napi_env env,napi_callback_info info)855 NO_SANITIZE("cfi") napi_value GetSignalLevel(napi_env env, napi_callback_info info)
856 {
857 size_t argc = 2;
858 const int PARAMS_NUM = 2;
859 napi_value argv[2];
860 napi_value thisVar;
861 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
862 /* the input have 2 parameters */
863 WIFI_NAPI_ASSERT(env, argc == PARAMS_NUM, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
864
865 napi_valuetype type1;
866 napi_valuetype type2;
867 napi_typeof(env, argv[0], &type1);
868 napi_typeof(env, argv[1], &type2);
869 WIFI_NAPI_ASSERT(env, type1 == napi_number, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
870 WIFI_NAPI_ASSERT(env, type2 == napi_number, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
871 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
872
873 int level = -1;
874 int rssi = 0;
875 int band = 0;
876 napi_get_value_int32(env, argv[0], &rssi);
877 napi_get_value_int32(env, argv[1], &band);
878 ErrCode ret = wifiDevicePtr->GetSignalLevel(rssi, band, level);
879 if (ret != WIFI_OPT_SUCCESS) {
880 WIFI_LOGE("Get wifi signal level fail: %{public}d", ret);
881 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
882 }
883 napi_value result;
884 napi_create_uint32(env, level, &result);
885 return result;
886 }
887
ReConnect(napi_env env,napi_callback_info info)888 NO_SANITIZE("cfi") napi_value ReConnect(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 ErrCode ret = wifiDevicePtr->ReConnect();
893 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
894 }
895
ReAssociate(napi_env env,napi_callback_info info)896 NO_SANITIZE("cfi") napi_value ReAssociate(napi_env env, napi_callback_info info)
897 {
898 TRACE_FUNC_CALL;
899 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
900 ErrCode ret = wifiDevicePtr->ReAssociate();
901 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
902 }
903
IpInfoToJsObj(const napi_env & env,IpInfo & ipInfo,napi_value & result)904 static void IpInfoToJsObj(const napi_env& env, IpInfo& ipInfo, napi_value& result)
905 {
906 napi_create_object(env, &result);
907 SetValueUnsignedInt32(env, "ipAddress", ipInfo.ipAddress, result);
908 SetValueUnsignedInt32(env, "gateway", ipInfo.gateway, result);
909 SetValueUnsignedInt32(env, "netmask", ipInfo.netmask, result);
910 SetValueUnsignedInt32(env, "primaryDns", ipInfo.primaryDns, result);
911 SetValueUnsignedInt32(env, "secondDns", ipInfo.secondDns, result);
912 SetValueUnsignedInt32(env, "serverIp", ipInfo.serverIp, result);
913 SetValueUnsignedInt32(env, "leaseDuration", ipInfo.leaseDuration, result);
914 }
915
IpV6InfoToJsObj(const napi_env & env,IpV6Info & ipInfo,napi_value & result)916 static void IpV6InfoToJsObj(const napi_env& env, IpV6Info& ipInfo, napi_value& result)
917 {
918 napi_create_object(env, &result);
919 SetValueUtf8String(env, "linkIpV6Address", ipInfo.linkIpV6Address, result);
920 SetValueUtf8String(env, "globalIpV6Address", ipInfo.globalIpV6Address, result);
921 SetValueUtf8String(env, "randGlobalIpV6Address", ipInfo.randGlobalIpV6Address, result);
922 SetValueUtf8String(env, "gateway", ipInfo.gateway, result);
923 SetValueUtf8String(env, "netmask", ipInfo.netmask, result);
924 SetValueUtf8String(env, "primaryDNS", ipInfo.primaryDns, result);
925 SetValueUtf8String(env, "secondDNS", ipInfo.secondDns, result);
926 }
927
GetIpInfo(napi_env env,napi_callback_info info)928 NO_SANITIZE("cfi") napi_value GetIpInfo(napi_env env, napi_callback_info info)
929 {
930 TRACE_FUNC_CALL;
931 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
932
933 IpInfo ipInfo;
934 napi_value result;
935 ErrCode ret = wifiDevicePtr->GetIpInfo(ipInfo);
936 if (ret != WIFI_OPT_SUCCESS) {
937 WIFI_LOGE("Get ip info fail: %{public}d", ret);
938 }
939 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
940 IpInfoToJsObj(env, ipInfo, result);
941 return result;
942 }
943
GetIpv6Info(napi_env env,napi_callback_info info)944 NO_SANITIZE("cfi") napi_value GetIpv6Info(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
949 IpV6Info ipInfo;
950 napi_value result;
951 ErrCode ret = wifiDevicePtr->GetIpv6Info(ipInfo);
952 if (ret != WIFI_OPT_SUCCESS) {
953 WIFI_LOGE("Get ip info fail: %{public}d", ret);
954 }
955 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
956 IpV6InfoToJsObj(env, ipInfo, result);
957 return result;
958 }
959
LinkedInfoToJs(const napi_env & env,WifiLinkedInfo & linkedInfo,napi_value & result)960 static void LinkedInfoToJs(const napi_env& env, WifiLinkedInfo& linkedInfo, napi_value& result)
961 {
962 SetValueUtf8String(env, "ssid", linkedInfo.ssid.c_str(), result);
963 SetValueUtf8String(env, "bssid", linkedInfo.bssid.c_str(), result);
964 SetValueInt32(env, "networkId", linkedInfo.networkId, result);
965 SetValueInt32(env, "rssi", linkedInfo.rssi, result);
966 SetValueInt32(env, "band", linkedInfo.band, result);
967 SetValueInt32(env, "linkSpeed", linkedInfo.linkSpeed, result);
968 SetValueInt32(env, "frequency", linkedInfo.frequency, result);
969 SetValueBool(env, "isHidden", linkedInfo.ifHiddenSSID, result);
970 /* isRestricted not support now, set as default value */
971 SetValueBool(env, "isRestricted", false, result);
972 SetValueInt32(env, "chload", linkedInfo.chload, result);
973 SetValueInt32(env, "snr", linkedInfo.snr, result);
974 SetValueUtf8String(env, "macAddress", linkedInfo.macAddress.c_str(), result);
975 SetValueInt32(env, "macType", linkedInfo.macType, result);
976 SetValueUnsignedInt32(env, "ipAddress", linkedInfo.ipAddress, result);
977 SetValueInt32(env, "suppState", static_cast<int>(linkedInfo.supplicantState), result);
978 SetValueInt32(env, "connState", static_cast<int>(linkedInfo.connState), result);
979 SetValueInt32(env, "WifiStandard", static_cast<int>(linkedInfo.wifiStandard), result);
980 SetValueInt32(env, "maxSupportedRxLinkSpeed", static_cast<int>(linkedInfo.maxSupportedRxLinkSpeed), result);
981 SetValueInt32(env, "maxSupportedTxLinkSpeed", static_cast<int>(linkedInfo.maxSupportedTxLinkSpeed), result);
982 SetValueInt32(env, "rxLinkSpeed", static_cast<int>(linkedInfo.rxLinkSpeed), result);
983 SetValueInt32(env, "linkSpeed", static_cast<int>(linkedInfo.txLinkSpeed), result);
984 SetValueInt32(env, "channelWidth", static_cast<int>(linkedInfo.channelWidth), result);
985 }
986
987 /* This interface has not been fully implemented */
GetLinkedInfo(napi_env env,napi_callback_info info)988 NO_SANITIZE("cfi") napi_value GetLinkedInfo(napi_env env, napi_callback_info info)
989 {
990 TRACE_FUNC_CALL;
991 size_t argc = 2;
992 napi_value argv[argc];
993 napi_value thisVar = nullptr;
994 void *data = nullptr;
995 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
996 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
997
998 LinkedInfoAsyncContext *asyncContext = new LinkedInfoAsyncContext(env);
999 WIFI_NAPI_ASSERT(env, asyncContext != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1000 napi_create_string_latin1(env, "getLinkedInfo", NAPI_AUTO_LENGTH, &asyncContext->resourceName);
1001
1002 asyncContext->executeFunc = [&](void* data) -> void {
1003 LinkedInfoAsyncContext *context = static_cast<LinkedInfoAsyncContext *>(data);
1004 TRACE_FUNC_CALL_NAME("wifiDevicePtr->GetLinkedInfo");
1005 context->errorCode = wifiDevicePtr->GetLinkedInfo(context->linkedInfo);
1006 };
1007
1008 asyncContext->completeFunc = [&](void* data) -> void {
1009 LinkedInfoAsyncContext *context = static_cast<LinkedInfoAsyncContext *>(data);
1010 napi_create_object(context->env, &context->result);
1011 LinkedInfoToJs(context->env, context->linkedInfo, context->result);
1012 WIFI_LOGI("Push get linkedInfo result to client");
1013 };
1014
1015 size_t nonCallbackArgNum = 0;
1016 asyncContext->sysCap = SYSCAP_WIFI_STA;
1017 return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum);
1018 }
1019
GetDisconnectedReason(napi_env env,napi_callback_info info)1020 NO_SANITIZE("cfi") napi_value GetDisconnectedReason(napi_env env, napi_callback_info info)
1021 {
1022 TRACE_FUNC_CALL;
1023 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_CORE);
1024 DisconnectedReason reason = DisconnectedReason::DISC_REASON_DEFAULT;
1025 ErrCode ret = wifiDevicePtr->GetDisconnectedReason(reason);
1026 if (ret != WIFI_OPT_SUCCESS) {
1027 WIFI_LOGE("GetDisconnectedReason failed:%{public}d", ret);
1028 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1029 }
1030 napi_value value;
1031 napi_create_int32(env, static_cast<int>(reason), &value);
1032 return value;
1033 }
1034
RemoveDevice(napi_env env,napi_callback_info info)1035 NO_SANITIZE("cfi") napi_value RemoveDevice(napi_env env, napi_callback_info info)
1036 {
1037 TRACE_FUNC_CALL;
1038 size_t argc = 1;
1039 napi_value argv[1];
1040 napi_value thisVar;
1041 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
1042 WIFI_NAPI_ASSERT(env, argc == 1, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
1043
1044 napi_valuetype valueType;
1045 napi_typeof(env, argv[0], &valueType);
1046 WIFI_NAPI_ASSERT(env, valueType == napi_number, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
1047
1048 int networkId = -1;
1049 napi_get_value_int32(env, argv[0], &networkId);
1050 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1051
1052 ErrCode ret = wifiDevicePtr->RemoveDevice(networkId);
1053 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1054 }
1055
RemoveAllNetwork(napi_env env,napi_callback_info info)1056 NO_SANITIZE("cfi") napi_value RemoveAllNetwork(napi_env env, napi_callback_info info)
1057 {
1058 TRACE_FUNC_CALL;
1059 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1060 ErrCode ret = wifiDevicePtr->RemoveAllDevice();
1061 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1062 }
1063
DisableNetwork(napi_env env,napi_callback_info info)1064 NO_SANITIZE("cfi") napi_value DisableNetwork(napi_env env, napi_callback_info info)
1065 {
1066 TRACE_FUNC_CALL;
1067 size_t argc = 1;
1068 napi_value argv[1];
1069 napi_value thisVar;
1070 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
1071 WIFI_NAPI_ASSERT(env, argc == 1, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
1072
1073 napi_valuetype valueType;
1074 napi_typeof(env, argv[0], &valueType);
1075 WIFI_NAPI_ASSERT(env, valueType == napi_number, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
1076 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1077
1078 int networkId = -1;
1079 napi_get_value_int32(env, argv[0], &networkId);
1080 ErrCode ret = wifiDevicePtr->DisableDeviceConfig(networkId);
1081 WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1082 }
1083
GetCountryCode(napi_env env,napi_callback_info info)1084 NO_SANITIZE("cfi") napi_value GetCountryCode(napi_env env, napi_callback_info info)
1085 {
1086 TRACE_FUNC_CALL;
1087 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_CORE);
1088 std::string countryCode;
1089 ErrCode ret = wifiDevicePtr->GetCountryCode(countryCode);
1090 if (ret != WIFI_OPT_SUCCESS) {
1091 WIFI_LOGE("Get countryCode fail: %{public}d", ret);
1092 }
1093 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_CORE);
1094 napi_value cc;
1095 napi_create_string_utf8(env, countryCode.c_str(), NAPI_AUTO_LENGTH, &cc);
1096 return cc;
1097 }
1098
ConvertKeyMgmtToSecType(const std::string & keyMgmt)1099 static SecTypeJs ConvertKeyMgmtToSecType(const std::string& keyMgmt)
1100 {
1101 std::map<std::string, SecTypeJs> mapKeyMgmtToSecType = {
1102 {KEY_MGMT_NONE, SecTypeJs::SEC_TYPE_OPEN},
1103 {KEY_MGMT_WEP, SecTypeJs::SEC_TYPE_WEP},
1104 {KEY_MGMT_WPA_PSK, SecTypeJs::SEC_TYPE_PSK},
1105 {KEY_MGMT_SAE, SecTypeJs::SEC_TYPE_SAE},
1106 {KEY_MGMT_EAP, SecTypeJs::SEC_TYPE_EAP},
1107 };
1108
1109 std::map<std::string, SecTypeJs>::iterator iter = mapKeyMgmtToSecType.find(keyMgmt);
1110 return iter == mapKeyMgmtToSecType.end() ? SecTypeJs::SEC_TYPE_OPEN : iter->second;
1111 }
1112
IpConfigToJs(const napi_env & env,const WifiIpConfig & wifiIpConfig,napi_value & ipCfgObj)1113 static void IpConfigToJs(const napi_env& env, const WifiIpConfig& wifiIpConfig, napi_value& ipCfgObj)
1114 {
1115 SetValueInt32(env, "ipAddress", wifiIpConfig.staticIpAddress.ipAddress.address.addressIpv4, ipCfgObj);
1116 SetValueInt32(env, "gateway", wifiIpConfig.staticIpAddress.gateway.addressIpv4, ipCfgObj);
1117
1118 const int DNS_NUM = 2;
1119 napi_value dnsArray;
1120 napi_create_array_with_length(env, DNS_NUM, &dnsArray);
1121 std::vector<unsigned int> vecDns = {wifiIpConfig.staticIpAddress.dnsServer1.addressIpv4,
1122 wifiIpConfig.staticIpAddress.dnsServer2.addressIpv4};
1123 for (int i = 0; i != DNS_NUM; ++i) {
1124 napi_value value;
1125 napi_status status = napi_create_int32(env, vecDns[i], &value);
1126 if (status != napi_ok) {
1127 WIFI_LOGE("Ip config to js create int32 error!");
1128 return;
1129 }
1130 status = napi_set_element(env, dnsArray, i, value);
1131 if (status != napi_ok) {
1132 WIFI_LOGE("Ip config to js set element error: %{public}d", status);
1133 return;
1134 }
1135 }
1136 if (napi_set_named_property(env, ipCfgObj, "dnsServers", dnsArray) != napi_ok) {
1137 WIFI_LOGE("Set dnsServers named property error!");
1138 }
1139
1140 const int DOMAINS_NUM = 1;
1141 napi_value domainsArray;
1142 napi_create_array_with_length(env, DOMAINS_NUM, &domainsArray);
1143 std::vector<std::string> vecDomains = {wifiIpConfig.staticIpAddress.domains};
1144 for (int i = 0; i != DOMAINS_NUM; ++i) {
1145 napi_value value;
1146 napi_status status = napi_create_string_utf8(env, vecDomains[i].c_str(), NAPI_AUTO_LENGTH, &value);
1147 if (status != napi_ok) {
1148 WIFI_LOGE("Ip config to js create utf8 string error!");
1149 return;
1150 }
1151 status = napi_set_element(env, domainsArray, i, value);
1152 if (status != napi_ok) {
1153 WIFI_LOGE("Ip config to js set element error: %{public}d", status);
1154 }
1155 }
1156 if (napi_set_named_property(env, ipCfgObj, "domains", domainsArray) != napi_ok) {
1157 WIFI_LOGE("Set domains named property error!");
1158 }
1159 }
1160
UpdateSecurityTypeAndPreSharedKey(WifiDeviceConfig & cppConfig)1161 static void UpdateSecurityTypeAndPreSharedKey(WifiDeviceConfig& cppConfig)
1162 {
1163 if (cppConfig.keyMgmt != KEY_MGMT_NONE) {
1164 return;
1165 }
1166 for (int i = 0; i != WEPKEYS_SIZE; ++i) {
1167 if (!cppConfig.wepKeys[i].empty() && cppConfig.wepTxKeyIndex == i) {
1168 cppConfig.keyMgmt = KEY_MGMT_WEP;
1169 cppConfig.preSharedKey = cppConfig.wepKeys[i];
1170 }
1171 }
1172 }
1173
DeviceConfigToJsArray(const napi_env & env,std::vector<WifiDeviceConfig> & vecDeviceConfigs,const int idx,napi_value & arrayResult)1174 static void DeviceConfigToJsArray(const napi_env& env, std::vector<WifiDeviceConfig>& vecDeviceConfigs,
1175 const int idx, napi_value& arrayResult)
1176 {
1177 UpdateSecurityTypeAndPreSharedKey(vecDeviceConfigs[idx]);
1178 napi_value result;
1179 napi_create_object(env, &result);
1180 SetValueUtf8String(env, "ssid", vecDeviceConfigs[idx].ssid.c_str(), result);
1181 SetValueUtf8String(env, "bssid", vecDeviceConfigs[idx].bssid.c_str(), result);
1182 SetValueInt32(env, "bssidType", static_cast<int>(vecDeviceConfigs[idx].bssidType), result);
1183 SetValueUtf8String(env, "preSharedKey", vecDeviceConfigs[idx].preSharedKey.c_str(), result);
1184 SetValueBool(env, "isHiddenSsid", vecDeviceConfigs[idx].hiddenSSID, result);
1185 SetValueInt32(env, "securityType",
1186 static_cast<int>(ConvertKeyMgmtToSecType(vecDeviceConfigs[idx].keyMgmt)), result);
1187 SetValueInt32(env, "creatorUid", vecDeviceConfigs[idx].uid, result);
1188 /* not supported currently */
1189 SetValueInt32(env, "disableReason", DEFAULT_INVALID_VALUE, result);
1190 SetValueInt32(env, "netId", vecDeviceConfigs[idx].networkId, result);
1191 /* not supported currently */
1192 SetValueInt32(env, "randomMacType", DEFAULT_INVALID_VALUE, result);
1193 /* not supported currently */
1194 SetValueUtf8String(env, "randomMacAddr", std::string("").c_str(), result);
1195 if (vecDeviceConfigs[idx].wifiIpConfig.assignMethod == AssignIpMethod::STATIC) {
1196 SetValueInt32(env, "ipType", static_cast<int>(IpTypeJs::IP_TYPE_STATIC), result);
1197 } else {
1198 SetValueInt32(env, "ipType", static_cast<int>(IpTypeJs::IP_TYPE_DHCP), result);
1199 }
1200 napi_value ipCfgObj;
1201 napi_create_object(env, &ipCfgObj);
1202 IpConfigToJs(env, vecDeviceConfigs[idx].wifiIpConfig, ipCfgObj);
1203 napi_status status = napi_set_named_property(env, result, "staticIp", ipCfgObj);
1204 if (status != napi_ok) {
1205 WIFI_LOGE("Set staticIp field!");
1206 }
1207 status = napi_set_element(env, arrayResult, idx, result);
1208 if (status != napi_ok) {
1209 WIFI_LOGE("Wifi napi set element error: %{public}d", status);
1210 }
1211 }
1212
GetDeviceConfigs(napi_env env,napi_callback_info info)1213 NO_SANITIZE("cfi") napi_value GetDeviceConfigs(napi_env env, napi_callback_info info)
1214 {
1215 TRACE_FUNC_CALL;
1216 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1217 std::vector<WifiDeviceConfig> vecDeviceConfigs;
1218 bool isCandidate = false;
1219 ErrCode ret = wifiDevicePtr->GetDeviceConfigs(vecDeviceConfigs, isCandidate);
1220 if (ret != WIFI_OPT_SUCCESS) {
1221 WIFI_LOGE("Get device configs fail: %{public}d", ret);
1222 }
1223
1224 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1225 WIFI_LOGI("Get device configs size: %{public}zu", vecDeviceConfigs.size());
1226 napi_value arrayResult;
1227 napi_create_array_with_length(env, vecDeviceConfigs.size(), &arrayResult);
1228 for (size_t i = 0; i != vecDeviceConfigs.size(); ++i) {
1229 DeviceConfigToJsArray(env, vecDeviceConfigs, i, arrayResult);
1230 }
1231 return arrayResult;
1232 }
1233
GetCandidateConfigs(napi_env env,napi_callback_info info)1234 NO_SANITIZE("cfi") napi_value GetCandidateConfigs(napi_env env, napi_callback_info info)
1235 {
1236 TRACE_FUNC_CALL;
1237 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1238 std::vector<WifiDeviceConfig> vecDeviceConfigs;
1239 bool isCandidate = true;
1240 ErrCode ret = wifiDevicePtr->GetDeviceConfigs(vecDeviceConfigs, isCandidate);
1241 if (ret != WIFI_OPT_SUCCESS) {
1242 WIFI_LOGE("Get candidate device configs fail: %{public}d", ret);
1243 }
1244
1245 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1246 WIFI_LOGI("Get candidate device configs size: %{public}zu", vecDeviceConfigs.size());
1247 napi_value arrayResult;
1248 napi_create_array_with_length(env, vecDeviceConfigs.size(), &arrayResult);
1249 for (size_t i = 0; i != vecDeviceConfigs.size(); ++i) {
1250 DeviceConfigToJsArray(env, vecDeviceConfigs, i, arrayResult);
1251 }
1252 return arrayResult;
1253 }
1254
UpdateNetwork(napi_env env,napi_callback_info info)1255 NO_SANITIZE("cfi") napi_value UpdateNetwork(napi_env env, napi_callback_info info)
1256 {
1257 TRACE_FUNC_CALL;
1258 size_t argc = 1;
1259 napi_value argv[1];
1260 napi_value thisVar;
1261 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
1262
1263 napi_valuetype valueType;
1264 napi_typeof(env, argv[0], &valueType);
1265 WIFI_NAPI_ASSERT(env, valueType == napi_object, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
1266
1267 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1268 int updateResult;
1269 WifiDeviceConfig config;
1270 napi_value res = JsObjToDeviceConfig(env, argv[0], config);
1271 napi_typeof(env, res, &valueType);
1272 WIFI_NAPI_ASSERT(env, valueType != napi_undefined, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
1273 ErrCode ret = wifiDevicePtr->UpdateDeviceConfig(config, updateResult);
1274 if (ret != WIFI_OPT_SUCCESS) {
1275 WIFI_LOGE("Update device config fail: %{public}d", ret);
1276 }
1277
1278 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1279 napi_value result;
1280 napi_create_uint32(env, updateResult, &result);
1281 return result;
1282 }
1283
GetSupportedFeatures(napi_env env,napi_callback_info info)1284 NO_SANITIZE("cfi") napi_value GetSupportedFeatures(napi_env env, napi_callback_info info)
1285 {
1286 TRACE_FUNC_CALL;
1287 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_CORE);
1288 long features = -1;
1289 ErrCode ret = wifiDevicePtr->GetSupportedFeatures(features);
1290 if (ret != WIFI_OPT_SUCCESS) {
1291 WIFI_LOGE("Get supported features fail: %{public}d", ret);
1292 }
1293
1294 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_CORE);
1295 napi_value result;
1296 napi_create_int64(env, features, &result);
1297 return result;
1298 }
1299
IsFeatureSupported(napi_env env,napi_callback_info info)1300 NO_SANITIZE("cfi") napi_value IsFeatureSupported(napi_env env, napi_callback_info info)
1301 {
1302 TRACE_FUNC_CALL;
1303 size_t argc = 1;
1304 napi_value argv[1];
1305 napi_value thisVar;
1306 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
1307 WIFI_NAPI_ASSERT(env, argc == 1, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_CORE);
1308
1309 napi_valuetype valueType;
1310 napi_typeof(env, argv[0], &valueType);
1311 WIFI_NAPI_ASSERT(env, valueType == napi_number, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_CORE);
1312 long feature = -1;
1313 napi_get_value_int64(env, argv[0], (int64_t*)&feature);
1314 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_CORE);
1315 bool isSupported = false;
1316 ErrCode ret = wifiDevicePtr->IsFeatureSupported(feature, isSupported);
1317 if (ret != WIFI_OPT_SUCCESS) {
1318 WIFI_LOGE("Get supported features fail: %{public}d", ret);
1319 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_CORE);
1320 }
1321
1322 napi_value result;
1323 napi_get_boolean(env, isSupported, &result);
1324 return result;
1325 }
1326
GetDeviceMacAddress(napi_env env,napi_callback_info info)1327 NO_SANITIZE("cfi") napi_value GetDeviceMacAddress(napi_env env, napi_callback_info info)
1328 {
1329 TRACE_FUNC_CALL;
1330 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1331 std::string macAddr;
1332 ErrCode ret = wifiDevicePtr->GetDeviceMacAddress(macAddr);
1333 if (ret != WIFI_OPT_SUCCESS) {
1334 WIFI_LOGE("Get mac address fail: %{public}d", ret);
1335 }
1336
1337 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1338 napi_value addr;
1339 napi_create_string_utf8(env, macAddr.c_str(), NAPI_AUTO_LENGTH, &addr);
1340 return addr;
1341 }
1342
IsBandTypeSupported(napi_env env,napi_callback_info info)1343 NO_SANITIZE("cfi") napi_value IsBandTypeSupported(napi_env env, napi_callback_info info)
1344 {
1345 TRACE_FUNC_CALL;
1346 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1347
1348 size_t argc = 1;
1349 napi_value argv[1];
1350 napi_value thisVar;
1351 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
1352 WIFI_NAPI_ASSERT(env, argc == 1, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
1353
1354 napi_valuetype valueType;
1355 napi_typeof(env, argv[0], &valueType);
1356 WIFI_NAPI_ASSERT(env, valueType == napi_number, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
1357 int bandType = 1;
1358 napi_get_value_int32(env, argv[0], &bandType);
1359 WIFI_NAPI_ASSERT(env, bandType > (int)WifiBandTypeJS::BAND_NONE && bandType <= (int)WifiBandTypeJS::BAND_60GHZ,
1360 WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_STA);
1361 bool supported = false;
1362 ErrCode ret = wifiDevicePtr->IsBandTypeSupported(bandType, supported);
1363 if (ret != WIFI_OPT_SUCCESS) {
1364 WIFI_LOGE("Get band type supported fail: %{public}d", ret);
1365 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1366 }
1367 napi_value result;
1368 napi_get_boolean(env, supported, &result);
1369 return result;
1370 }
1371
Get5GHzChannelList(napi_env env,napi_callback_info info)1372 NO_SANITIZE("cfi") napi_value Get5GHzChannelList(napi_env env, napi_callback_info info)
1373 {
1374 TRACE_FUNC_CALL;
1375 WIFI_NAPI_ASSERT(env, wifiDevicePtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_STA);
1376 std::vector<int> vec5GChannels;
1377 ErrCode ret = wifiDevicePtr->Get5GHzChannelList(vec5GChannels);
1378 if (ret != WIFI_OPT_SUCCESS) {
1379 WIFI_LOGE("Get 5g channellist fail: %{public}d", ret);
1380 }
1381
1382 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_STA);
1383 WIFI_LOGI("Get 5g channellist size: %{public}zu", vec5GChannels.size());
1384 napi_value arrayResult;
1385 napi_create_array_with_length(env, vec5GChannels.size(), &arrayResult);
1386 for (size_t i = 0; i != vec5GChannels.size(); ++i) {
1387 napi_value result;
1388 napi_create_uint32(env, vec5GChannels[i], &result);
1389 napi_status status = napi_set_element(env, arrayResult, i, result);
1390 if (status != napi_ok) {
1391 WIFI_LOGE("wifi napi set 56 list element error: %{public}d", status);
1392 }
1393 }
1394 return arrayResult;
1395 }
1396
SetScanOnlyAvailable(napi_env env,napi_callback_info info)1397 NO_SANITIZE("cfi") napi_value SetScanOnlyAvailable(napi_env env, napi_callback_info info)
1398 {
1399 WIFI_LOGI("wifi napi In SetScanOnlyAvailable");
1400 TRACE_FUNC_CALL;
1401 size_t argc = 1;
1402 napi_value argv[1];
1403 napi_value thisVar;
1404 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
1405
1406 napi_valuetype valueType;
1407 napi_typeof(env, argv[0], &valueType);
1408
1409 bool bScanOnlyAvailableStatus = false;
1410 napi_get_value_bool(env, argv[0], &bScanOnlyAvailableStatus);
1411
1412 ErrCode ret = wifiScanPtr->SetScanOnlyAvailable(bScanOnlyAvailableStatus);
1413 if (ret != WIFI_OPT_SUCCESS) {
1414 WIFI_LOGE("Set wifi scanOnlyAvailable fail: %{public}d", ret);
1415 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_CORE);
1416 }
1417
1418 napi_value result;
1419 napi_create_int32(env, (int32_t)ret, &result);
1420 return result;
1421 }
1422
GetScanOnlyAvailable(napi_env env,napi_callback_info info)1423 NO_SANITIZE("cfi") napi_value GetScanOnlyAvailable(napi_env env, napi_callback_info info)
1424 {
1425 bool bScanOnlyAvailableStatus = false;
1426
1427 ErrCode ret = wifiScanPtr->GetScanOnlyAvailable(bScanOnlyAvailableStatus);
1428 if (ret != WIFI_OPT_SUCCESS) {
1429 WIFI_LOGE("Get wifi scanOnlyAvailable fail: %{public}d", ret);
1430 WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_CORE);
1431 }
1432
1433 napi_value result;
1434 napi_get_boolean(env, bScanOnlyAvailableStatus, &result);
1435 return result;
1436 }
1437
1438 } // namespace Wifi
1439 } // namespace OHOS
1440