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
22 namespace OHOS {
23 namespace Wifi {
24 DEFINE_WIFILOG_LABEL("WifiNAPIDevice");
25 static constexpr int DEFAULT_INVALID_VALUE = -1;
26 static constexpr int ERROR_CODE_API_IS_NOT_SUPPORTED = 801;
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);
EnableWifi(napi_env env,napi_callback_info info)30 napi_value EnableWifi(napi_env env, napi_callback_info info)
31 {
32 TRACE_FUNC_CALL;
33 NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null.");
34 ErrCode ret = wifiDevicePtr->EnableWifi();
35 WriteWifiStateHiSysEvent(HISYS_SERVICE_TYPE_STA, WifiOperType::ENABLE);
36 napi_value result;
37 napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result);
38 return result;
39 }
40
DisableWifi(napi_env env,napi_callback_info info)41 napi_value DisableWifi(napi_env env, napi_callback_info info)
42 {
43 TRACE_FUNC_CALL;
44 NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null.");
45 ErrCode ret = wifiDevicePtr->DisableWifi();
46 WriteWifiStateHiSysEvent(HISYS_SERVICE_TYPE_STA, WifiOperType::DISABLE);
47 napi_value result;
48 napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result);
49 return result;
50 }
51
IsWifiActive(napi_env env,napi_callback_info info)52 napi_value IsWifiActive(napi_env env, napi_callback_info info)
53 {
54 NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null.");
55 bool activeStatus = false;
56 ErrCode ret = wifiDevicePtr->IsWifiActive(activeStatus);
57 if (ret != WIFI_OPT_SUCCESS) {
58 WIFI_LOGE("Get wifi active status fail: %{public}d", ret);
59 }
60
61 napi_value result;
62 napi_get_boolean(env, activeStatus, &result);
63 return result;
64 }
65
Scan(napi_env env,napi_callback_info info)66 napi_value Scan(napi_env env, napi_callback_info info)
67 {
68 TRACE_FUNC_CALL;
69 NAPI_ASSERT(env, wifiScanPtr != nullptr, "Wifi scan instance is null.");
70 ErrCode ret = wifiScanPtr->Scan();
71 WriteWifiScanHiSysEvent(static_cast<int>(ret), GetBundleName());
72 napi_value result;
73 napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result);
74 return result;
75 }
76
SecurityTypeNativeToJs(const WifiSecurity & cppSecurityType)77 static SecTypeJs SecurityTypeNativeToJs(const WifiSecurity& cppSecurityType)
78 {
79 SecTypeJs jsSecurityType = SecTypeJs::SEC_TYPE_INVALID;
80 switch (cppSecurityType) {
81 case WifiSecurity::OPEN:
82 jsSecurityType = SecTypeJs::SEC_TYPE_OPEN;
83 break;
84
85 case WifiSecurity::WEP:
86 jsSecurityType = SecTypeJs::SEC_TYPE_WEP;
87 break;
88
89 case WifiSecurity::PSK:
90 jsSecurityType = SecTypeJs::SEC_TYPE_PSK;
91 break;
92
93 case WifiSecurity::SAE:
94 jsSecurityType = SecTypeJs::SEC_TYPE_SAE;
95 break;
96
97 default:
98 jsSecurityType = SecTypeJs::SEC_TYPE_INVALID;
99 break;
100 }
101 return jsSecurityType;
102 }
103
NativeScanInfosToJsObj(const napi_env & env,const std::vector<WifiScanInfo> & vecScnIanfos,napi_value & arrayResult)104 static ErrCode NativeScanInfosToJsObj(const napi_env& env,
105 const std::vector<WifiScanInfo>& vecScnIanfos, napi_value& arrayResult)
106 {
107 uint32_t idx = 0;
108 for (auto& each : vecScnIanfos) {
109 napi_value eachObj;
110 napi_create_object(env, &eachObj);
111
112 SetValueUtf8String(env, "ssid", each.ssid.c_str(), eachObj);
113 SetValueUtf8String(env, "bssid", each.bssid.c_str(), eachObj);
114 SetValueUtf8String(env, "capabilities", each.capabilities.c_str(), eachObj);
115 SetValueInt32(env, "securityType", static_cast<int>(SecurityTypeNativeToJs(each.securityType)), eachObj);
116 SetValueInt32(env, "rssi", each.rssi, eachObj);
117 SetValueInt32(env, "band", each.band, eachObj);
118 SetValueInt32(env, "frequency", each.frequency, eachObj);
119 SetValueInt32(env, "channelWidth", static_cast<int>(each.channelWidth), eachObj);
120 SetValueInt64(env, "timestamp", each.timestamp, eachObj);
121
122 napi_status status = napi_set_element(env, arrayResult, idx++, eachObj);
123 if (status != napi_ok) {
124 WIFI_LOGE("Wifi napi set element error: %{public}d, idx: %{public}d", status, idx - 1);
125 return WIFI_OPT_FAILED;
126 }
127 }
128 return WIFI_OPT_SUCCESS;
129 }
130
GetScanInfos(napi_env env,napi_callback_info info)131 napi_value GetScanInfos(napi_env env, napi_callback_info info)
132 {
133 TRACE_FUNC_CALL;
134 size_t argc = 2;
135 napi_value argv[argc];
136 napi_value thisVar = nullptr;
137 void *data = nullptr;
138 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
139 NAPI_ASSERT(env, wifiScanPtr != nullptr, "Wifi device instance is null.");
140
141 ScanInfoAsyncContext *asyncContext = new ScanInfoAsyncContext(env);
142 NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null.");
143 napi_create_string_latin1(env, "getScanInfos", NAPI_AUTO_LENGTH, &asyncContext->resourceName);
144
145 asyncContext->executeFunc = [&](void* data) -> void {
146 ScanInfoAsyncContext *context = static_cast<ScanInfoAsyncContext *>(data);
147 TRACE_FUNC_CALL_NAME("wifiScanPtr->GetScanInfoList");
148 context->errorCode = wifiScanPtr->GetScanInfoList(context->vecScanInfos);
149 WIFI_LOGI("GetScanInfoList, size: %{public}zu", context->vecScanInfos.size());
150 };
151
152 asyncContext->completeFunc = [&](void* data) -> void {
153 ScanInfoAsyncContext *context = static_cast<ScanInfoAsyncContext *>(data);
154 napi_create_array_with_length(context->env, context->vecScanInfos.size(), &context->result);
155 context->errorCode = NativeScanInfosToJsObj(context->env, context->vecScanInfos, context->result);
156 WIFI_LOGI("Push scan info list to client");
157 };
158
159 size_t nonCallbackArgNum = 0;
160 return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum);
161 }
162
ConvertEncryptionMode(const SecTypeJs & securityType,std::string & keyMgmt)163 static void ConvertEncryptionMode(const SecTypeJs& securityType, std::string& keyMgmt)
164 {
165 switch (securityType) {
166 case SecTypeJs::SEC_TYPE_OPEN:
167 case SecTypeJs::SEC_TYPE_WEP:
168 keyMgmt = KEY_MGMT_NONE;
169 break;
170
171 case SecTypeJs::SEC_TYPE_PSK:
172 keyMgmt = KEY_MGMT_WPA_PSK;
173 break;
174
175 case SecTypeJs::SEC_TYPE_SAE:
176 keyMgmt = KEY_MGMT_SAE;
177 break;
178
179 default:
180 keyMgmt = KEY_MGMT_NONE;
181 break;
182 }
183 }
184
ProcessPassphrase(const SecTypeJs & securityType,WifiDeviceConfig & cppConfig)185 static void ProcessPassphrase(const SecTypeJs& securityType, WifiDeviceConfig& cppConfig)
186 {
187 if (securityType == SecTypeJs::SEC_TYPE_WEP) {
188 cppConfig.wepKeys[0] = cppConfig.preSharedKey;
189 cppConfig.wepTxKeyIndex = 0;
190 cppConfig.preSharedKey = "";
191 }
192 }
193
JsObjToDeviceConfig(const napi_env & env,const napi_value & object,WifiDeviceConfig & cppConfig)194 static void JsObjToDeviceConfig(const napi_env& env, const napi_value& object, WifiDeviceConfig& cppConfig)
195 {
196 JsObjectToString(env, object, "ssid", 33, cppConfig.ssid); /* 33: ssid max length is 32 + '\0' */
197 JsObjectToString(env, object, "bssid", 18, cppConfig.bssid); /* 18: max bssid length for string type */
198 JsObjectToString(env, object, "preSharedKey", 256, cppConfig.preSharedKey); /* 256: max length */
199 JsObjectToBool(env, object, "isHiddenSsid", cppConfig.hiddenSSID);
200 int type = static_cast<int>(SecTypeJs::SEC_TYPE_INVALID);
201 JsObjectToInt(env, object, "securityType", type);
202 ConvertEncryptionMode(SecTypeJs(type), cppConfig.keyMgmt);
203 ProcessPassphrase(SecTypeJs(type), cppConfig);
204 /* "creatorUid" is not supported currently */
205 /* "disableReason" is not supported currently */
206 JsObjectToInt(env, object, "netId", cppConfig.networkId);
207 /* "randomMacType" is not supported currently */
208 /* "randomMacAddr" is not supported currently */
209 int ipType = static_cast<int>(AssignIpMethod::UNASSIGNED);
210 JsObjectToInt(env, object, "ipType", ipType);
211 /* "staticIp" is not supported currently */
212 }
213
AddDeviceConfig(napi_env env,napi_callback_info info)214 napi_value AddDeviceConfig(napi_env env, napi_callback_info info)
215 {
216 TRACE_FUNC_CALL;
217 size_t argc = 3;
218 napi_value argv[argc];
219 napi_value thisVar = nullptr;
220 void *data = nullptr;
221 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
222 NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
223 NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null.");
224
225 napi_valuetype valueType;
226 napi_typeof(env, argv[0], &valueType);
227 NAPI_ASSERT(env, valueType == napi_object, "Wrong argument type, object is expected for parameter 1.");
228
229 AddDeviceConfigContext *asyncContext = new AddDeviceConfigContext(env);
230 NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null.");
231 napi_create_string_latin1(env, "addDeviceConfig", NAPI_AUTO_LENGTH, &asyncContext->resourceName);
232
233 WifiDeviceConfig *config = new WifiDeviceConfig();
234 if (config == nullptr) {
235 delete asyncContext;
236 return UndefinedNapiValue(env);
237 }
238 JsObjToDeviceConfig(env, argv[0], *config);
239 asyncContext->config = config;
240
241 asyncContext->executeFunc = [&](void* data) -> void {
242 AddDeviceConfigContext *context = static_cast<AddDeviceConfigContext *>(data);
243 TRACE_FUNC_CALL_NAME("wifiDevicePtr->AddDeviceConfig");
244 ErrCode ret = wifiDevicePtr->AddDeviceConfig(*context->config, context->addResult);
245 if (context->addResult < 0 || ret != WIFI_OPT_SUCCESS) {
246 context->addResult = -1;
247 }
248 context->errorCode = ret;
249 };
250
251 asyncContext->completeFunc = [&](void* data) -> void {
252 AddDeviceConfigContext *context = static_cast<AddDeviceConfigContext *>(data);
253 napi_create_int32(context->env, context->addResult, &context->result);
254 if (context->config != nullptr) {
255 delete context->config;
256 context->config = nullptr;
257 }
258 WIFI_LOGI("Push add device config result to client");
259 };
260
261 size_t nonCallbackArgNum = 1;
262 return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum);
263 }
264
AddUntrustedConfig(napi_env env,napi_callback_info info)265 napi_value AddUntrustedConfig(napi_env env, napi_callback_info info)
266 {
267 TRACE_FUNC_CALL;
268 size_t argc = 2;
269 napi_value argv[argc];
270 napi_value thisVar = nullptr;
271 void *data = nullptr;
272 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
273 NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
274 NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null.");
275
276 napi_valuetype valueType;
277 napi_typeof(env, argv[0], &valueType);
278 NAPI_ASSERT(env, valueType == napi_object, "Wrong argument type, object is expected for parameter 1.");
279
280 AddDeviceConfigContext *asyncContext = new AddDeviceConfigContext(env);
281 NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null.");
282 napi_create_string_latin1(env, "AddUntrustedConfig", NAPI_AUTO_LENGTH, &asyncContext->resourceName);
283
284 WifiDeviceConfig *config = new WifiDeviceConfig();
285 if (config == nullptr) {
286 delete asyncContext;
287 return UndefinedNapiValue(env);
288 }
289 JsObjToDeviceConfig(env, argv[0], *config);
290 asyncContext->config = config;
291
292 asyncContext->executeFunc = [&](void* data) -> void {
293 AddDeviceConfigContext *context = static_cast<AddDeviceConfigContext *>(data);
294 TRACE_FUNC_CALL_NAME("wifiDevicePtr->AddUntrustedConfig");
295 /* This interface is not supported currently */
296 context->addResult = -1;
297 context->errorCode = ERROR_CODE_API_IS_NOT_SUPPORTED;
298 };
299
300 asyncContext->completeFunc = [&](void* data) -> void {
301 AddDeviceConfigContext *context = static_cast<AddDeviceConfigContext *>(data);
302 /* This interface is not supported currently */
303 napi_get_boolean(context->env, false, &context->result);
304 if (context->config != nullptr) {
305 delete context->config;
306 context->config = nullptr;
307 }
308 WIFI_LOGI("Push add untrusted device config result to client");
309 };
310
311 size_t nonCallbackArgNum = 1;
312 return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum);
313 }
314
RemoveUntrustedConfig(napi_env env,napi_callback_info info)315 napi_value RemoveUntrustedConfig(napi_env env, napi_callback_info info)
316 {
317 TRACE_FUNC_CALL;
318 size_t argc = 3;
319 napi_value argv[argc];
320 napi_value thisVar = nullptr;
321 void *data = nullptr;
322 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
323 NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
324 NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null.");
325
326 napi_valuetype valueType;
327 napi_typeof(env, argv[0], &valueType);
328 NAPI_ASSERT(env, valueType == napi_object, "Wrong argument type, object is expected for parameter 1.");
329
330 AddDeviceConfigContext *asyncContext = new AddDeviceConfigContext(env);
331 NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null.");
332 napi_create_string_latin1(env, "RemoveUntrustedConfig", NAPI_AUTO_LENGTH, &asyncContext->resourceName);
333
334 WifiDeviceConfig *config = new WifiDeviceConfig();
335 if (config == nullptr) {
336 delete asyncContext;
337 return UndefinedNapiValue(env);
338 }
339 JsObjToDeviceConfig(env, argv[0], *config);
340 asyncContext->config = config;
341
342 asyncContext->executeFunc = [&](void* data) -> void {
343 AddDeviceConfigContext *context = static_cast<AddDeviceConfigContext *>(data);
344 TRACE_FUNC_CALL_NAME("wifiDevicePtr->RemoveUntrustedConfig");
345 /* This interface is not supported currently */
346 context->addResult = -1;
347 context->errorCode = ERROR_CODE_API_IS_NOT_SUPPORTED;
348 };
349
350 asyncContext->completeFunc = [&](void* data) -> void {
351 AddDeviceConfigContext *context = static_cast<AddDeviceConfigContext *>(data);
352 /* This interface is not supported currently */
353 napi_get_boolean(context->env, false, &context->result);
354 if (context->config != nullptr) {
355 delete context->config;
356 context->config = nullptr;
357 }
358 WIFI_LOGI("Push remove untrusted device config result to client");
359 };
360
361 size_t nonCallbackArgNum = 1;
362 return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum);
363 }
364
ConnectToNetwork(napi_env env,napi_callback_info info)365 napi_value ConnectToNetwork(napi_env env, napi_callback_info info)
366 {
367 TRACE_FUNC_CALL;
368 size_t argc = 1;
369 napi_value argv[1];
370 napi_value thisVar;
371 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
372 NAPI_ASSERT(env, argc == 1, "Wrong number of arguments");
373
374 napi_valuetype valueType;
375 napi_typeof(env, argv[0], &valueType);
376 NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. napi_number expected.");
377
378 int networkId = -1;
379 napi_get_value_int32(env, argv[0], &networkId);
380
381 NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null.");
382 ErrCode ret = wifiDevicePtr->ConnectToNetwork(networkId);
383 WriteWifiConnectionHiSysEvent(WifiConnectionType::CONNECT, GetBundleName());
384 napi_value result;
385 napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result);
386 return result;
387 }
388
ConnectToDevice(napi_env env,napi_callback_info info)389 napi_value ConnectToDevice(napi_env env, napi_callback_info info)
390 {
391 TRACE_FUNC_CALL;
392 size_t argc = 1;
393 napi_value argv[1];
394 napi_value thisVar;
395 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
396
397 napi_valuetype valueType;
398 napi_typeof(env, argv[0], &valueType);
399 NAPI_ASSERT(env, valueType == napi_object, "Wrong argument type. Object expected.");
400
401 NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null.");
402 WifiDeviceConfig config;
403 JsObjToDeviceConfig(env, argv[0], config);
404 ErrCode ret = wifiDevicePtr->ConnectToDevice(config);
405 if (ret != WIFI_OPT_SUCCESS) {
406 WIFI_LOGE("Connect to device fail: %{public}d", ret);
407 }
408 WriteWifiConnectionHiSysEvent(WifiConnectionType::CONNECT, GetBundleName());
409 napi_value result;
410 napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result);
411 return result;
412 }
413
IsConnected(napi_env env,napi_callback_info info)414 napi_value IsConnected(napi_env env, napi_callback_info info)
415 {
416 TRACE_FUNC_CALL;
417 NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null.");
418 napi_value result;
419 napi_get_boolean(env, wifiDevicePtr->IsConnected(), &result);
420 return result;
421 }
422
Disconnect(napi_env env,napi_callback_info info)423 napi_value Disconnect(napi_env env, napi_callback_info info)
424 {
425 TRACE_FUNC_CALL;
426 NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null.");
427 ErrCode ret = wifiDevicePtr->Disconnect();
428 WriteWifiConnectionHiSysEvent(WifiConnectionType::DISCONNECT, GetBundleName());
429 napi_value result;
430 napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result);
431 return result;
432 }
433
GetSignalLevel(napi_env env,napi_callback_info info)434 napi_value GetSignalLevel(napi_env env, napi_callback_info info)
435 {
436 size_t argc = 2;
437 napi_value argv[2];
438 napi_value thisVar;
439 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
440 /* the input have 2 parameters */
441 NAPI_ASSERT(env, argc == 2, "Wrong number of arguments");
442
443 napi_valuetype type1;
444 napi_valuetype type2;
445 napi_typeof(env, argv[0], &type1);
446 napi_typeof(env, argv[1], &type2);
447 NAPI_ASSERT(env, type1 == napi_number, "Wrong argument type. napi_number expected.");
448 NAPI_ASSERT(env, type2 == napi_number, "Wrong argument type. napi_number expected.");
449 NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null.");
450
451 int level = -1;
452 int rssi = 0;
453 int band = 0;
454 napi_get_value_int32(env, argv[0], &rssi);
455 napi_get_value_int32(env, argv[1], &band);
456 ErrCode ret = wifiDevicePtr->GetSignalLevel(rssi, band, level);
457 if (ret != WIFI_OPT_SUCCESS) {
458 WIFI_LOGE("Get wifi signal level fail: %{public}d", ret);
459 }
460
461 napi_value result;
462 napi_create_uint32(env, level, &result);
463 return result;
464 }
465
ReConnect(napi_env env,napi_callback_info info)466 napi_value ReConnect(napi_env env, napi_callback_info info)
467 {
468 TRACE_FUNC_CALL;
469 NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null.");
470
471 napi_value result;
472 napi_get_boolean(env, wifiDevicePtr->ReConnect() == WIFI_OPT_SUCCESS, &result);
473 WriteWifiConnectionHiSysEvent(WifiConnectionType::CONNECT, GetBundleName());
474 return result;
475 }
476
ReAssociate(napi_env env,napi_callback_info info)477 napi_value ReAssociate(napi_env env, napi_callback_info info)
478 {
479 TRACE_FUNC_CALL;
480 NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null.");
481 napi_value result;
482 napi_get_boolean(env, wifiDevicePtr->ReAssociate() == WIFI_OPT_SUCCESS, &result);
483 WriteWifiConnectionHiSysEvent(WifiConnectionType::CONNECT, GetBundleName());
484 return result;
485 }
486
IpInfoToJsObj(const napi_env & env,IpInfo & ipInfo,napi_value & result)487 static void IpInfoToJsObj(const napi_env& env, IpInfo& ipInfo, napi_value& result)
488 {
489 napi_create_object(env, &result);
490 SetValueUnsignedInt32(env, "ipAddress", ipInfo.ipAddress, result);
491 SetValueUnsignedInt32(env, "gateway", ipInfo.gateway, result);
492 SetValueUnsignedInt32(env, "netmask", ipInfo.netmask, result);
493 SetValueUnsignedInt32(env, "primaryDns", ipInfo.primaryDns, result);
494 SetValueUnsignedInt32(env, "secondDns", ipInfo.secondDns, result);
495 SetValueUnsignedInt32(env, "serverIp", ipInfo.serverIp, result);
496 SetValueUnsignedInt32(env, "leaseDuration", ipInfo.leaseDuration, result);
497 }
498
GetIpInfo(napi_env env,napi_callback_info info)499 napi_value GetIpInfo(napi_env env, napi_callback_info info)
500 {
501 TRACE_FUNC_CALL;
502 NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null.");
503
504 IpInfo ipInfo;
505 napi_value result;
506 ErrCode ret = wifiDevicePtr->GetIpInfo(ipInfo);
507 if (ret != WIFI_OPT_SUCCESS) {
508 WIFI_LOGE("Get ip info fail: %{public}d", ret);
509 }
510 IpInfoToJsObj(env, ipInfo, result);
511 return result;
512 }
513
LinkedInfoToJs(const napi_env & env,WifiLinkedInfo & linkedInfo,napi_value & result)514 static void LinkedInfoToJs(const napi_env& env, WifiLinkedInfo& linkedInfo, napi_value& result)
515 {
516 SetValueUtf8String(env, "ssid", linkedInfo.ssid.c_str(), result);
517 SetValueUtf8String(env, "bssid", linkedInfo.bssid.c_str(), result);
518 SetValueInt32(env, "networkId", linkedInfo.networkId, result);
519 SetValueInt32(env, "rssi", linkedInfo.rssi, result);
520 SetValueInt32(env, "band", linkedInfo.band, result);
521 SetValueInt32(env, "linkSpeed", linkedInfo.linkSpeed, result);
522 SetValueInt32(env, "frequency", linkedInfo.frequency, result);
523 SetValueBool(env, "isHidden", linkedInfo.ifHiddenSSID, result);
524 /* isRestricted not support now, set as default value */
525 SetValueBool(env, "isRestricted", false, result);
526 SetValueInt32(env, "chload", linkedInfo.chload, result);
527 SetValueInt32(env, "snr", linkedInfo.snr, result);
528 SetValueUtf8String(env, "macAddress", linkedInfo.macAddress.c_str(), result);
529 SetValueUnsignedInt32(env, "ipAddress", linkedInfo.ipAddress, result);
530 SetValueInt32(env, "suppState", static_cast<int>(linkedInfo.supplicantState), result);
531 SetValueInt32(env, "connState", static_cast<int>(linkedInfo.connState), result);
532 }
533
534 /* This interface has not been fully implemented */
GetLinkedInfo(napi_env env,napi_callback_info info)535 napi_value GetLinkedInfo(napi_env env, napi_callback_info info)
536 {
537 TRACE_FUNC_CALL;
538 size_t argc = 2;
539 napi_value argv[argc];
540 napi_value thisVar = nullptr;
541 void *data = nullptr;
542 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data));
543 NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null.");
544
545 LinkedInfoAsyncContext *asyncContext = new LinkedInfoAsyncContext(env);
546 NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null.");
547 napi_create_string_latin1(env, "getLinkedInfo", NAPI_AUTO_LENGTH, &asyncContext->resourceName);
548
549 asyncContext->executeFunc = [&](void* data) -> void {
550 LinkedInfoAsyncContext *context = static_cast<LinkedInfoAsyncContext *>(data);
551 TRACE_FUNC_CALL_NAME("wifiDevicePtr->GetLinkedInfo");
552 context->errorCode = wifiDevicePtr->GetLinkedInfo(context->linkedInfo);
553 };
554
555 asyncContext->completeFunc = [&](void* data) -> void {
556 LinkedInfoAsyncContext *context = static_cast<LinkedInfoAsyncContext *>(data);
557 napi_create_object(context->env, &context->result);
558 LinkedInfoToJs(context->env, context->linkedInfo, context->result);
559 WIFI_LOGI("Push get linkedInfo result to client");
560 };
561
562 size_t nonCallbackArgNum = 0;
563 return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum);
564 }
565
RemoveDevice(napi_env env,napi_callback_info info)566 napi_value RemoveDevice(napi_env env, napi_callback_info info)
567 {
568 TRACE_FUNC_CALL;
569 size_t argc = 1;
570 napi_value argv[1];
571 napi_value thisVar;
572 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
573 NAPI_ASSERT(env, argc == 1, "Wrong number of arguments");
574
575 napi_valuetype valueType;
576 napi_typeof(env, argv[0], &valueType);
577 NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. napi_number expected.");
578
579 int networkId = -1;
580 napi_get_value_int32(env, argv[0], &networkId);
581 NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null.");
582
583 napi_value result;
584 napi_get_boolean(env, wifiDevicePtr->RemoveDevice(networkId) == WIFI_OPT_SUCCESS, &result);
585 return result;
586 }
587
RemoveAllNetwork(napi_env env,napi_callback_info info)588 napi_value RemoveAllNetwork(napi_env env, napi_callback_info info)
589 {
590 TRACE_FUNC_CALL;
591 NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null.");
592 napi_value result;
593 napi_get_boolean(env, wifiDevicePtr->RemoveAllDevice() == WIFI_OPT_SUCCESS, &result);
594 return result;
595 }
596
DisableNetwork(napi_env env,napi_callback_info info)597 napi_value DisableNetwork(napi_env env, napi_callback_info info)
598 {
599 TRACE_FUNC_CALL;
600 size_t argc = 1;
601 napi_value argv[1];
602 napi_value thisVar;
603 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
604 NAPI_ASSERT(env, argc == 1, "Wrong number of arguments");
605
606 napi_valuetype valueType;
607 napi_typeof(env, argv[0], &valueType);
608 NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. napi_number expected.");
609 NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null.");
610
611 int networkId = -1;
612 napi_get_value_int32(env, argv[0], &networkId);
613 napi_value result;
614 napi_get_boolean(env, wifiDevicePtr->DisableDeviceConfig(networkId) == WIFI_OPT_SUCCESS, &result);
615 return result;
616 }
617
GetCountryCode(napi_env env,napi_callback_info info)618 napi_value GetCountryCode(napi_env env, napi_callback_info info)
619 {
620 TRACE_FUNC_CALL;
621 NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null.");
622 std::string countryCode;
623 ErrCode ret = wifiDevicePtr->GetCountryCode(countryCode);
624 if (ret != WIFI_OPT_SUCCESS) {
625 WIFI_LOGE("Get countryCode fail: %{public}d", ret);
626 }
627 napi_value cc;
628 napi_create_string_utf8(env, countryCode.c_str(), NAPI_AUTO_LENGTH, &cc);
629 return cc;
630 }
631
ConvertKeyMgmtToSecType(const std::string & keyMgmt)632 static SecTypeJs ConvertKeyMgmtToSecType(const std::string& keyMgmt)
633 {
634 std::map<std::string, SecTypeJs> mapKeyMgmtToSecType = {
635 {KEY_MGMT_NONE, SecTypeJs::SEC_TYPE_OPEN},
636 {KEY_MGMT_WEP, SecTypeJs::SEC_TYPE_WEP},
637 {KEY_MGMT_WPA_PSK, SecTypeJs::SEC_TYPE_PSK},
638 {KEY_MGMT_SAE, SecTypeJs::SEC_TYPE_SAE},
639 };
640
641 std::map<std::string, SecTypeJs>::iterator iter = mapKeyMgmtToSecType.find(keyMgmt);
642 return iter == mapKeyMgmtToSecType.end() ? SecTypeJs::SEC_TYPE_OPEN : iter->second;
643 }
644
IpConfigToJs(const napi_env & env,const WifiIpConfig & wifiIpConfig,napi_value & ipCfgObj)645 static void IpConfigToJs(const napi_env& env, const WifiIpConfig& wifiIpConfig, napi_value& ipCfgObj)
646 {
647 SetValueInt32(env, "ipAddress", wifiIpConfig.staticIpAddress.ipAddress.address.addressIpv4, ipCfgObj);
648 SetValueInt32(env, "gateway", wifiIpConfig.staticIpAddress.gateway.addressIpv4, ipCfgObj);
649
650 const int DNS_NUM = 2;
651 napi_value dnsArray;
652 napi_create_array_with_length(env, DNS_NUM, &dnsArray);
653 std::vector<unsigned int> vecDns = {wifiIpConfig.staticIpAddress.dnsServer1.addressIpv4,
654 wifiIpConfig.staticIpAddress.dnsServer2.addressIpv4};
655 for (int i = 0; i != DNS_NUM; ++i) {
656 napi_value value;
657 napi_status status = napi_create_int32(env, vecDns[i], &value);
658 if (status != napi_ok) {
659 WIFI_LOGE("Ip config to js create int32 error!");
660 return;
661 }
662 status = napi_set_element(env, dnsArray, i, value);
663 if (status != napi_ok) {
664 WIFI_LOGE("Ip config to js set element error: %{public}d", status);
665 return;
666 }
667 }
668 if (napi_set_named_property(env, ipCfgObj, "dnsServers", dnsArray) != napi_ok) {
669 WIFI_LOGE("Set dnsServers named property error!");
670 }
671
672 const int DOMAINS_NUM = 1;
673 napi_value domainsArray;
674 napi_create_array_with_length(env, DOMAINS_NUM, &domainsArray);
675 std::vector<std::string> vecDomains = {wifiIpConfig.staticIpAddress.domains};
676 for (int i = 0; i != DOMAINS_NUM; ++i) {
677 napi_value value;
678 napi_status status = napi_create_string_utf8(env, vecDomains[i].c_str(), NAPI_AUTO_LENGTH, &value);
679 if (status != napi_ok) {
680 WIFI_LOGE("Ip config to js create utf8 string error!");
681 return;
682 }
683 status = napi_set_element(env, domainsArray, i, value);
684 if (status != napi_ok) {
685 WIFI_LOGE("Ip config to js set element error: %{public}d", status);
686 }
687 }
688 if (napi_set_named_property(env, ipCfgObj, "domains", domainsArray) != napi_ok) {
689 WIFI_LOGE("Set domains named property error!");
690 }
691 }
692
UpdateSecurityTypeAndPreSharedKey(WifiDeviceConfig & cppConfig)693 static void UpdateSecurityTypeAndPreSharedKey(WifiDeviceConfig& cppConfig)
694 {
695 if (cppConfig.keyMgmt != KEY_MGMT_NONE) {
696 return;
697 }
698 for (int i = 0; i != WEPKEYS_SIZE; ++i) {
699 if (!cppConfig.wepKeys[i].empty() && cppConfig.wepTxKeyIndex == i) {
700 cppConfig.keyMgmt = KEY_MGMT_WEP;
701 cppConfig.preSharedKey = cppConfig.wepKeys[i];
702 }
703 }
704 }
705
DeviceConfigToJsArray(const napi_env & env,std::vector<WifiDeviceConfig> & vecDeviceConfigs,const int idx,napi_value & arrayResult)706 static void DeviceConfigToJsArray(const napi_env& env, std::vector<WifiDeviceConfig>& vecDeviceConfigs,
707 const int idx, napi_value& arrayResult)
708 {
709 UpdateSecurityTypeAndPreSharedKey(vecDeviceConfigs[idx]);
710 napi_value result;
711 napi_create_object(env, &result);
712 SetValueUtf8String(env, "ssid", vecDeviceConfigs[idx].ssid.c_str(), result);
713 SetValueUtf8String(env, "bssid", vecDeviceConfigs[idx].bssid.c_str(), result);
714 SetValueUtf8String(env, "preSharedKey", vecDeviceConfigs[idx].preSharedKey.c_str(), result);
715 SetValueBool(env, "isHiddenSsid", vecDeviceConfigs[idx].hiddenSSID, result);
716 SetValueInt32(env, "securityType",
717 static_cast<int>(ConvertKeyMgmtToSecType(vecDeviceConfigs[idx].keyMgmt)), result);
718 /* not supported currently */
719 SetValueInt32(env, "creatorUid", DEFAULT_INVALID_VALUE, result);
720 /* not supported currently */
721 SetValueInt32(env, "disableReason", DEFAULT_INVALID_VALUE, result);
722 SetValueInt32(env, "netId", vecDeviceConfigs[idx].networkId, result);
723 /* not supported currently */
724 SetValueInt32(env, "randomMacType", DEFAULT_INVALID_VALUE, result);
725 /* not supported currently */
726 SetValueUtf8String(env, "randomMacAddr", std::string("").c_str(), result);
727 /* not fully supported, set as dhcp now */
728 SetValueInt32(env, "ipType", static_cast<int>(AssignIpMethod::DHCP), result);
729
730 napi_value ipCfgObj;
731 napi_create_object(env, &ipCfgObj);
732 IpConfigToJs(env, vecDeviceConfigs[idx].wifiIpConfig, ipCfgObj);
733 napi_status status = napi_set_named_property(env, result, "staticIp", ipCfgObj);
734 if (status != napi_ok) {
735 WIFI_LOGE("Set staticIp field!");
736 }
737 status = napi_set_element(env, arrayResult, idx, result);
738 if (status != napi_ok) {
739 WIFI_LOGE("Wifi napi set element error: %{public}d", status);
740 }
741 }
742
GetDeviceConfigs(napi_env env,napi_callback_info info)743 napi_value GetDeviceConfigs(napi_env env, napi_callback_info info)
744 {
745 TRACE_FUNC_CALL;
746 NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null.");
747 std::vector<WifiDeviceConfig> vecDeviceConfigs;
748 ErrCode ret = wifiDevicePtr->GetDeviceConfigs(vecDeviceConfigs);
749 if (ret != WIFI_OPT_SUCCESS) {
750 WIFI_LOGE("Get device configs fail: %{public}d", ret);
751 }
752
753 WIFI_LOGI("Get device configs size: %{public}zu", vecDeviceConfigs.size());
754 napi_value arrayResult;
755 napi_create_array_with_length(env, vecDeviceConfigs.size(), &arrayResult);
756 for (size_t i = 0; i != vecDeviceConfigs.size(); ++i) {
757 DeviceConfigToJsArray(env, vecDeviceConfigs, i, arrayResult);
758 }
759 return arrayResult;
760 }
761
UpdateNetwork(napi_env env,napi_callback_info info)762 napi_value UpdateNetwork(napi_env env, napi_callback_info info)
763 {
764 TRACE_FUNC_CALL;
765 size_t argc = 1;
766 napi_value argv[1];
767 napi_value thisVar;
768 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
769
770 napi_valuetype valueType;
771 napi_typeof(env, argv[0], &valueType);
772 NAPI_ASSERT(env, valueType == napi_object, "Wrong argument type. Object expected.");
773
774 NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null.");
775 int updateResult;
776 WifiDeviceConfig config;
777 JsObjToDeviceConfig(env, argv[0], config);
778 ErrCode ret = wifiDevicePtr->UpdateDeviceConfig(config, updateResult);
779 if (ret != WIFI_OPT_SUCCESS) {
780 WIFI_LOGE("Update device config fail: %{public}d", ret);
781 }
782
783 napi_value result;
784 napi_create_uint32(env, updateResult, &result);
785 return result;
786 }
787
GetSupportedFeatures(napi_env env,napi_callback_info info)788 napi_value GetSupportedFeatures(napi_env env, napi_callback_info info)
789 {
790 TRACE_FUNC_CALL;
791 NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null.");
792 long features = -1;
793 ErrCode ret = wifiDevicePtr->GetSupportedFeatures(features);
794 if (ret != WIFI_OPT_SUCCESS) {
795 WIFI_LOGE("Get supported features fail: %{public}d", ret);
796 }
797
798 napi_value result;
799 napi_create_int64(env, features, &result);
800 return result;
801 }
802
IsFeatureSupported(napi_env env,napi_callback_info info)803 napi_value IsFeatureSupported(napi_env env, napi_callback_info info)
804 {
805 TRACE_FUNC_CALL;
806 size_t argc = 1;
807 napi_value argv[1];
808 napi_value thisVar;
809 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
810 NAPI_ASSERT(env, argc == 1, "Wrong number of arguments");
811
812 napi_valuetype valueType;
813 napi_typeof(env, argv[0], &valueType);
814 NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. napi_number expected.");
815
816 long feature = -1;
817 napi_get_value_int64(env, argv[0], (int64_t*)&feature);
818 NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null.");
819
820 napi_value result;
821 napi_get_boolean(env, wifiDevicePtr->IsFeatureSupported(feature), &result);
822 return result;
823 }
824
GetDeviceMacAddress(napi_env env,napi_callback_info info)825 napi_value GetDeviceMacAddress(napi_env env, napi_callback_info info)
826 {
827 TRACE_FUNC_CALL;
828 NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null.");
829 std::string macAddr;
830 ErrCode ret = wifiDevicePtr->GetDeviceMacAddress(macAddr);
831 if (ret != WIFI_OPT_SUCCESS) {
832 WIFI_LOGE("Get mac address fail: %{public}d", ret);
833 }
834
835 napi_value addr;
836 napi_create_string_utf8(env, macAddr.c_str(), NAPI_AUTO_LENGTH, &addr);
837 return addr;
838 }
839 } // namespace Wifi
840 } // namespace OHOS
841