From 3e4337a2a954215abf9b6e5e03f84c73889c3aa6 Mon Sep 17 00:00:00 2001 From: zhangfeng Date: Sat, 23 Oct 2021 09:11:10 +0000 Subject: [PATCH] add new function and napi optimize Signed-off-by: zhangfeng --- .../native_cpp/napi/wifi_napi_device.cpp | 602 ++++++++++-------- .../native_cpp/napi/wifi_napi_device.h | 69 +- .../native_cpp/napi/wifi_napi_entry.cpp | 20 +- .../native_cpp/napi/wifi_napi_event.cpp | 104 +-- .../native_cpp/napi/wifi_napi_hotspot.cpp | 172 +++++ .../native_cpp/napi/wifi_napi_hotspot.h | 7 +- .../native_cpp/napi/wifi_napi_utils.cpp | 202 +++++- .../native_cpp/napi/wifi_napi_utils.h | 70 +- 8 files changed, 905 insertions(+), 341 deletions(-) diff --git a/interfaces/innerkits/native_cpp/napi/wifi_napi_device.cpp b/interfaces/innerkits/native_cpp/napi/wifi_napi_device.cpp index 3afaf93c3..211a4ce64 100755 --- a/interfaces/innerkits/native_cpp/napi/wifi_napi_device.cpp +++ b/interfaces/innerkits/native_cpp/napi/wifi_napi_device.cpp @@ -15,9 +15,8 @@ #include "wifi_napi_device.h" #include "wifi_logger.h" -#include "wifi_device.h" -#include "wifi_scan.h" #include +#include namespace OHOS { namespace Wifi { @@ -25,15 +24,10 @@ DEFINE_WIFILOG_LABEL("WifiNAPIDevice"); std::unique_ptr wifiDevicePtr = WifiDevice::GetInstance(WIFI_DEVICE_ABILITY_ID); std::unique_ptr wifiScanPtr = WifiScan::GetInstance(WIFI_SCAN_ABILITY_ID); - napi_value EnableWifi(napi_env env, napi_callback_info info) { - size_t argc = 1; - napi_value argv[1]; - napi_value thisVar; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - - NAPI_ASSERT(env, wifiDevicePtr != nullptr, "[NAPI] Wifi device instance is null."); + TRACE_FUNC_CALL; + NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); ErrCode ret = wifiDevicePtr->EnableWifi(); napi_value result; napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result); @@ -42,12 +36,8 @@ napi_value EnableWifi(napi_env env, napi_callback_info info) napi_value DisableWifi(napi_env env, napi_callback_info info) { - size_t argc = 1; - napi_value argv[1]; - napi_value thisVar; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - - NAPI_ASSERT(env, wifiDevicePtr != nullptr, "[NAPI] Wifi device instance is null."); + TRACE_FUNC_CALL; + NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); ErrCode ret = wifiDevicePtr->DisableWifi(); napi_value result; napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result); @@ -56,16 +46,11 @@ napi_value DisableWifi(napi_env env, napi_callback_info info) napi_value IsWifiActive(napi_env env, napi_callback_info info) { - size_t argc = 1; - napi_value argv[1]; - napi_value thisVar; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - - NAPI_ASSERT(env, wifiDevicePtr != nullptr, "[NAPI] Wifi device instance is null."); - bool activeStatus = true; + NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); + bool activeStatus = false; ErrCode ret = wifiDevicePtr->IsWifiActive(activeStatus); if (ret != WIFI_OPT_SUCCESS) { - WIFI_LOGE("[Napi Device] Get wifi active status fail: %{public}d", ret); + WIFI_LOGE("Get wifi active status fail: %{public}d", ret); } napi_value result; @@ -75,12 +60,8 @@ napi_value IsWifiActive(napi_env env, napi_callback_info info) napi_value Scan(napi_env env, napi_callback_info info) { - size_t argc = 1; - napi_value argv[1]; - napi_value thisVar; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - - NAPI_ASSERT(env, wifiScanPtr != nullptr, "[NAPI] Wifi scan instance is null."); + TRACE_FUNC_CALL; + NAPI_ASSERT(env, wifiScanPtr != nullptr, "Wifi scan instance is null."); ErrCode ret = wifiScanPtr->Scan(); napi_value result; @@ -115,8 +96,8 @@ static SecTypeJs SecurityTypeNativeToJs(const WifiSecurity& cppSecurityType) return jsSecurityType; } -static bool NativeScanInfosToJsObj(const napi_env& env, napi_value& arrayResult, - const std::vector& vecScnIanfos) +static bool NativeScanInfosToJsObj(const napi_env& env, + const std::vector& vecScnIanfos, napi_value& arrayResult) { uint32_t idx = 0; for (auto& each : vecScnIanfos) { @@ -133,126 +114,43 @@ static bool NativeScanInfosToJsObj(const napi_env& env, napi_value& arrayResult, napi_status status = napi_set_element(env, arrayResult, idx++, eachObj); if (status != napi_ok) { - WIFI_LOGE("[Napi Device] wifi napi set element error: %{public}d, idx: %{public}d", status, idx - 1); + WIFI_LOGE("Wifi napi set element error: %{public}d, idx: %{public}d", status, idx - 1); return false; } } return true; } -static bool GetWifiScanInfoList(const napi_env& env, napi_value& arrayResult) -{ - std::vector vecCppScanInfos; - if (wifiScanPtr->GetScanInfoList(vecCppScanInfos) != WIFI_OPT_SUCCESS) { - WIFI_LOGE("[Napi Device] Get Scaninf list error"); - return false; - } - - WIFI_LOGI("[Napi Device] GetScanInfoList, size: %{public}zu", vecCppScanInfos.size()); - napi_create_array_with_length(env, vecCppScanInfos.size(), &arrayResult); - return NativeScanInfosToJsObj(env, arrayResult, vecCppScanInfos); -} - -static napi_value ScanInfoToCallBack(const napi_env& env, AsyncCallbackInfo *asCallbackInfo, - const size_t argc, const napi_value *argv) -{ - napi_value resourceName; - napi_create_string_latin1(env, "getScanInfos", NAPI_AUTO_LENGTH, &resourceName); - - for (size_t i = 0; i != argc; ++i) { - napi_valuetype valuetype; - NAPI_CALL(env, napi_typeof(env, argv[i], &valuetype)); - NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); - napi_create_reference(env, argv[i], 1, &asCallbackInfo->callback[i]); - } - - napi_create_async_work( - env, nullptr, resourceName, - [](napi_env env, void* data) { - }, - [](napi_env env, napi_status status, void* data) { - napi_value undefine; - napi_get_undefined(env, &undefine); - napi_value callback; - AsyncCallbackInfo* asCallbackInfo = (AsyncCallbackInfo *)data; - asCallbackInfo->isSuccess = GetWifiScanInfoList(env, asCallbackInfo->result); - if (asCallbackInfo->isSuccess) { - napi_get_reference_value(env, asCallbackInfo->callback[0], &callback); - napi_call_function(env, nullptr, callback, 1, &asCallbackInfo->result, &undefine); - } else { - if (asCallbackInfo->callback[1]) { - napi_get_reference_value(env, asCallbackInfo->callback[1], &callback); - napi_call_function(env, nullptr, callback, 1, &asCallbackInfo->result, &undefine); - } else { - WIFI_LOGE("[Napi Device] get scan info callback func is null"); - napi_throw_error(env, "error", "get scan info callback func is null"); - } - } - if (asCallbackInfo->callback[0] != nullptr) { - napi_delete_reference(env, asCallbackInfo->callback[0]); - } - if (asCallbackInfo->callback[1] != nullptr) { - napi_delete_reference(env, asCallbackInfo->callback[1]); - } - napi_delete_async_work(env, asCallbackInfo->asyncWork); - delete asCallbackInfo; - }, - (void *)asCallbackInfo, - &asCallbackInfo->asyncWork); - NAPI_CALL(env, napi_queue_async_work(env, asCallbackInfo->asyncWork)); - return UndefinedNapiValue(env); -} - -static napi_value ScanInfoToPromise(const napi_env& env, AsyncCallbackInfo *asCallbackInfo, napi_value& promise) -{ - napi_value resourceName; - napi_create_string_latin1(env, "getScanInfos", NAPI_AUTO_LENGTH, &resourceName); - - napi_deferred deferred; - NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - asCallbackInfo->deferred = deferred; - - napi_create_async_work( - env, - nullptr, - resourceName, - [](napi_env env, void *data) { - }, - [](napi_env env, napi_status status, void *data) { - AsyncCallbackInfo *asCallbackInfo = (AsyncCallbackInfo *)data; - asCallbackInfo->isSuccess = GetWifiScanInfoList(env, asCallbackInfo->result); - if (asCallbackInfo->isSuccess) { - napi_resolve_deferred(asCallbackInfo->env, asCallbackInfo->deferred, asCallbackInfo->result); - } else { - napi_reject_deferred(asCallbackInfo->env, asCallbackInfo->deferred, asCallbackInfo->result); - } - napi_delete_async_work(env, asCallbackInfo->asyncWork); - delete asCallbackInfo; - }, - (void *)asCallbackInfo, - &asCallbackInfo->asyncWork); - napi_queue_async_work(env, asCallbackInfo->asyncWork); - return UndefinedNapiValue(env); -} - napi_value GetScanInfos(napi_env env, napi_callback_info info) { + TRACE_FUNC_CALL; size_t argc = 2; napi_value argv[argc]; napi_value thisVar = nullptr; void *data = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); - - AsyncCallbackInfo *asCallbackInfo = - new AsyncCallbackInfo{.env = env, .asyncWork = nullptr, .deferred = nullptr}; - - if (argc >= 1) { - return ScanInfoToCallBack(env, asCallbackInfo, argc, argv); - } else { - napi_value promise; - ScanInfoToPromise(env, asCallbackInfo, promise); - return promise; - } + NAPI_ASSERT(env, wifiScanPtr != nullptr, "Wifi device instance is null."); + + ScanInfoAsyncContext *asyncContext = new ScanInfoAsyncContext(env); + NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null."); + napi_create_string_latin1(env, "getScanInfos", NAPI_AUTO_LENGTH, &asyncContext->resourceName); + + asyncContext->executeFunc = [&](void* data) -> void { + ScanInfoAsyncContext *context = static_cast(data); + TRACE_FUNC_CALL_NAME("wifiScanPtr->GetScanInfoList"); + context->isSuccess = (wifiScanPtr->GetScanInfoList(context->vecScanInfos) == WIFI_OPT_SUCCESS); + WIFI_LOGI("GetScanInfoList, size: %{public}zu", context->vecScanInfos.size()); + }; + + asyncContext->completeFunc = [&](void* data) -> void { + ScanInfoAsyncContext *context = static_cast(data); + napi_create_array_with_length(context->env, context->vecScanInfos.size(), &context->result); + context->isSuccess = NativeScanInfosToJsObj(context->env, context->vecScanInfos, context->result); + WIFI_LOGI("Push scan info list to client"); + }; + + size_t nonCallbackArgNum = 0; + return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum); } static void ConvertEncryptionMode(const SecTypeJs& securityType, std::string& keyMgmt) @@ -275,7 +173,7 @@ static void ConvertEncryptionMode(const SecTypeJs& securityType, std::string& ke break; default: - keyMgmt = "WPA-PSK"; + keyMgmt = "NONE"; break; } } @@ -291,143 +189,60 @@ static void JsObjToDeviceConfig(const napi_env& env, const napi_value& object, W ConvertEncryptionMode(SecTypeJs(type), cppConfig.keyMgmt); } -static napi_value AddDeviceConfigImpl(const napi_env& env, AsyncCallbackInfo *asCallbackInfo) -{ - NAPI_ASSERT(env, wifiDevicePtr != nullptr, "[NAPI] Wifi device instance is null."); - int addResult = -1; - int retValue = 0; - ErrCode ret = wifiDevicePtr->AddDeviceConfig(*(WifiDeviceConfig *)asCallbackInfo->obj, addResult); - if (addResult < 0 || ret != WIFI_OPT_SUCCESS) { - retValue = -1; - } else { - retValue = addResult; - } - - napi_value result; - napi_create_int32(env, retValue, &result); - asCallbackInfo->isSuccess = (ret == WIFI_OPT_SUCCESS); - asCallbackInfo->result = result; - return UndefinedNapiValue(env); -} - -static napi_value AddDeviceConfigCallBack(const napi_env& env, AsyncCallbackInfo *asCallbackInfo, - size_t argc, napi_value *argv) -{ - napi_value resourceName; - napi_create_string_latin1(env, "addDeviceConfig", NAPI_AUTO_LENGTH, &resourceName); - - for (size_t i = 1; i != argc; ++i) { - napi_valuetype valuetype; - NAPI_CALL(env, napi_typeof(env, argv[i], &valuetype)); - NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); - napi_create_reference(env, argv[i], 1, &asCallbackInfo->callback[i - 1]); - } - - napi_create_async_work( - env, nullptr, resourceName, - [](napi_env env, void* data) { - }, - [](napi_env env, napi_status status, void* data) { - AsyncCallbackInfo* asCallbackInfo = (AsyncCallbackInfo *)data; - AddDeviceConfigImpl(env, asCallbackInfo); - napi_value callback; - napi_value undefine; - napi_get_undefined(env, &undefine); - if (asCallbackInfo->isSuccess) { - napi_get_reference_value(env, asCallbackInfo->callback[0], &callback); - napi_call_function(env, nullptr, callback, 1, &asCallbackInfo->result, &undefine); - } else { - if (asCallbackInfo->callback[1]) { - napi_get_reference_value(env, asCallbackInfo->callback[1], &callback); - napi_call_function(env, nullptr, callback, 1, &asCallbackInfo->result, &undefine); - } else { - WIFI_LOGE("[Napi Device] get scan info callback func is null"); - napi_throw_error(env, "error", "add wifi config callback func is null"); - } - } - if (asCallbackInfo->callback[0] != nullptr) { - napi_delete_reference(env, asCallbackInfo->callback[0]); - } - if (asCallbackInfo->callback[1] != nullptr) { - napi_delete_reference(env, asCallbackInfo->callback[1]); - } - napi_delete_async_work(env, asCallbackInfo->asyncWork); - delete (WifiDeviceConfig *)asCallbackInfo->obj; - delete asCallbackInfo; - }, - (void *)asCallbackInfo, - &asCallbackInfo->asyncWork); - NAPI_CALL(env, napi_queue_async_work(env, asCallbackInfo->asyncWork)); - return UndefinedNapiValue(env); -} - -static napi_value AddDeviceConfigPromise(const napi_env& env, AsyncCallbackInfo *asCallbackInfo, napi_value& promise) -{ - napi_value resourceName; - napi_create_string_latin1(env, "addDeviceConfig", NAPI_AUTO_LENGTH, &resourceName); - - napi_deferred deferred; - NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - asCallbackInfo->deferred = deferred; - - napi_create_async_work( - env, - nullptr, - resourceName, - [](napi_env env, void *data) { - }, - [](napi_env env, napi_status status, void *data) { - AsyncCallbackInfo *asCallbackInfo = (AsyncCallbackInfo *)data; - AddDeviceConfigImpl(env, asCallbackInfo); - if (asCallbackInfo->isSuccess) { - napi_resolve_deferred(asCallbackInfo->env, asCallbackInfo->deferred, asCallbackInfo->result); - } else { - napi_reject_deferred(asCallbackInfo->env, asCallbackInfo->deferred, asCallbackInfo->result); - } - napi_delete_async_work(env, asCallbackInfo->asyncWork); - delete (WifiDeviceConfig *)asCallbackInfo->obj; - delete asCallbackInfo; - }, - (void *)asCallbackInfo, - &asCallbackInfo->asyncWork); - napi_queue_async_work(env, asCallbackInfo->asyncWork); - return UndefinedNapiValue(env); -} - napi_value AddDeviceConfig(napi_env env, napi_callback_info info) { + TRACE_FUNC_CALL; size_t argc = 3; napi_value argv[argc]; napi_value thisVar = nullptr; void *data = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments"); - + NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); + napi_valuetype valueType; napi_typeof(env, argv[0], &valueType); - NAPI_ASSERT(env, valueType == napi_object, "Wrong argument type. Object expected for parameter 1."); + NAPI_ASSERT(env, valueType == napi_object, "Wrong argument type, object is expected for parameter 1."); - AsyncCallbackInfo *asCallbackInfo = - new AsyncCallbackInfo{.env = env, .asyncWork = nullptr, .deferred = nullptr}; + AddDeviceConfigContext *asyncContext = new AddDeviceConfigContext(env); + NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null."); + napi_create_string_latin1(env, "addDeviceConfig", NAPI_AUTO_LENGTH, &asyncContext->resourceName); WifiDeviceConfig *config = new WifiDeviceConfig(); - if (config == NULL) { - delete asCallbackInfo; + if (config == nullptr) { + delete asyncContext; return UndefinedNapiValue(env); } JsObjToDeviceConfig(env, argv[0], *config); - asCallbackInfo->obj = config; - if (argc > 1) { - return AddDeviceConfigCallBack(env, asCallbackInfo, argc, argv); - } else { - napi_value promise; - AddDeviceConfigPromise(env, asCallbackInfo, promise); - return promise; - } + asyncContext->config = config; + + asyncContext->executeFunc = [&](void* data) -> void { + AddDeviceConfigContext *context = static_cast(data); + TRACE_FUNC_CALL_NAME("wifiDevicePtr->AddDeviceConfig"); + ErrCode ret = wifiDevicePtr->AddDeviceConfig(*context->config, context->addResult); + if (context->addResult < 0 || ret != WIFI_OPT_SUCCESS) { + context->addResult = -1; + } + context->isSuccess = (ret == WIFI_OPT_SUCCESS); + }; + + asyncContext->completeFunc = [&](void* data) -> void { + AddDeviceConfigContext *context = static_cast(data); + napi_create_int32(context->env, context->addResult, &context->result); + if (context->config != nullptr) { + delete context->config; + context->config = nullptr; + } + WIFI_LOGI("Push add device config result to client"); + }; + + size_t nonCallbackArgNum = 1; + return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum); } napi_value ConnectToNetwork(napi_env env, napi_callback_info info) { + TRACE_FUNC_CALL; size_t argc = 1; napi_value argv[1]; napi_value thisVar; @@ -441,7 +256,7 @@ napi_value ConnectToNetwork(napi_env env, napi_callback_info info) int networkId = -1; napi_get_value_int32(env, argv[0], &networkId); - NAPI_ASSERT(env, wifiDevicePtr != nullptr, "[NAPI] Wifi device instance is null."); + NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); ErrCode ret = wifiDevicePtr->ConnectToNetwork(networkId); napi_value result; napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result); @@ -450,6 +265,7 @@ napi_value ConnectToNetwork(napi_env env, napi_callback_info info) napi_value ConnectToDevice(napi_env env, napi_callback_info info) { + TRACE_FUNC_CALL; size_t argc = 1; napi_value argv[1]; napi_value thisVar; @@ -459,10 +275,13 @@ napi_value ConnectToDevice(napi_env env, napi_callback_info info) napi_typeof(env, argv[0], &valueType); NAPI_ASSERT(env, valueType == napi_object, "Wrong argument type. Object expected."); - NAPI_ASSERT(env, wifiDevicePtr != nullptr, "[NAPI] Wifi device instance is null."); + NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); WifiDeviceConfig config; JsObjToDeviceConfig(env, argv[0], config); ErrCode ret = wifiDevicePtr->ConnectToDevice(config); + if (ret != WIFI_OPT_SUCCESS) { + WIFI_LOGE("Connect to device fail: %{public}d", ret); + } napi_value result; napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result); @@ -471,12 +290,8 @@ napi_value ConnectToDevice(napi_env env, napi_callback_info info) napi_value Disconnect(napi_env env, napi_callback_info info) { - size_t argc = 1; - napi_value argv[1]; - napi_value thisVar; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - - NAPI_ASSERT(env, wifiDevicePtr != nullptr, "[NAPI] Wifi device instance is null."); + TRACE_FUNC_CALL; + NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); ErrCode ret = wifiDevicePtr->Disconnect(); napi_value result; napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result); @@ -498,21 +313,270 @@ napi_value GetSignalLevel(napi_env env, napi_callback_info info) napi_typeof(env, argv[1], &type2); NAPI_ASSERT(env, type1 == napi_number, "Wrong argument type. napi_number expected."); NAPI_ASSERT(env, type2 == napi_number, "Wrong argument type. napi_number expected."); + NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); - int rssi, band; + int level = -1; + int rssi = 0; + int band = 0; napi_get_value_int32(env, argv[0], &rssi); napi_get_value_int32(env, argv[1], &band); - - NAPI_ASSERT(env, wifiDevicePtr != nullptr, "[NAPI] Wifi device instance is null."); - int level = -1; ErrCode ret = wifiDevicePtr->GetSignalLevel(rssi, band, level); if (ret != WIFI_OPT_SUCCESS) { - WIFI_LOGW("[Napi Device] Get wifi signal level fail: %{public}d", ret); + WIFI_LOGE("Get wifi signal level fail: %{public}d", ret); } napi_value result; napi_create_uint32(env, level, &result); return result; } + +/* This interface has not been implemented */ +napi_value ReConnect(napi_env env, napi_callback_info info) +{ + TRACE_FUNC_CALL; + NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); + napi_value result = nullptr; + return result; +} + +/* This interface has not been implemented */ +napi_value ReAssociate(napi_env env, napi_callback_info info) +{ + TRACE_FUNC_CALL; + NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); + napi_value result = nullptr; + return result; +} + +/* This interface has not been implemented */ +napi_value GetIpInfo(napi_env env, napi_callback_info info) +{ + TRACE_FUNC_CALL; + NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); + napi_value result = nullptr; + return result; +} + +static void LinkedInfoToJs(const napi_env& env, WifiLinkedInfo& linkedInfo, napi_value& result) +{ + SetValueUtf8String(env, "ssid", linkedInfo.ssid.c_str(), result); + SetValueUtf8String(env, "bssid", linkedInfo.bssid.c_str(), result); + SetValueInt32(env, "networkId", linkedInfo.networkId, result); + SetValueInt32(env, "rssi", linkedInfo.rssi, result); + SetValueInt32(env, "band", linkedInfo.band, result); + SetValueInt32(env, "linkSpeed", linkedInfo.linkSpeed, result); + SetValueInt32(env, "frequency", linkedInfo.frequency, result); + SetValueBool(env, "isHidden", linkedInfo.ifHiddenSSID, result); + /* isRestricted not support now, set as default value */ + SetValueBool(env, "isRestricted", false, result); + SetValueInt32(env, "chload", linkedInfo.chload, result); + SetValueInt32(env, "snr", linkedInfo.snr, result); + SetValueUtf8String(env, "macAddress", linkedInfo.macAddress.c_str(), result); + SetValueInt32(env, "ipAddress", linkedInfo.ipAddress, result); + /* Check suppState is consistent with HOS */ + SetValueInt32(env, "suppState", static_cast(linkedInfo.supplicantState), result); + /* Check connState is consistent with HOS */ + SetValueInt32(env, "connState", static_cast(linkedInfo.connState), result); +} + +/* This interface has not been fully implemented */ +napi_value GetLinkedInfo(napi_env env, napi_callback_info info) +{ + TRACE_FUNC_CALL; + size_t argc = 2; + napi_value argv[argc]; + napi_value thisVar = nullptr; + void *data = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); + NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); + + LinkedInfoAsyncContext *asyncContext = new LinkedInfoAsyncContext(env); + NAPI_ASSERT(env, asyncContext != nullptr, "asyncContext is null."); + napi_create_string_latin1(env, "getLinkedInfo", NAPI_AUTO_LENGTH, &asyncContext->resourceName); + + asyncContext->executeFunc = [&](void* data) -> void { + LinkedInfoAsyncContext *context = static_cast(data); + TRACE_FUNC_CALL_NAME("wifiDevicePtr->GetLinkedInfo"); + context->isSuccess = (wifiDevicePtr->GetLinkedInfo(context->linkedInfo) == WIFI_OPT_SUCCESS); + }; + + asyncContext->completeFunc = [&](void* data) -> void { + LinkedInfoAsyncContext *context = static_cast(data); + napi_create_object(context->env, &context->result); + LinkedInfoToJs(context->env, context->linkedInfo, context->result); + WIFI_LOGI("Push get linkedInfo result to client"); + }; + + size_t nonCallbackArgNum = 0; + return DoAsyncWork(env, asyncContext, argc, argv, nonCallbackArgNum); +} + +napi_value RemoveDevice(napi_env env, napi_callback_info info) +{ + TRACE_FUNC_CALL; + size_t argc = 1; + napi_value argv[1]; + napi_value thisVar; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + NAPI_ASSERT(env, argc == 1, "Wrong number of arguments"); + + napi_valuetype valueType; + napi_typeof(env, argv[0], &valueType); + NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. napi_number expected."); + + int networkId = -1; + napi_get_value_int32(env, argv[0], &networkId); + NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); + + napi_value result; + napi_get_boolean(env, wifiDevicePtr->RemoveDevice(networkId) == WIFI_OPT_SUCCESS, &result); + return result; +} + +napi_value RemoveAllNetwork(napi_env env, napi_callback_info info) +{ + TRACE_FUNC_CALL; + NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); + napi_value result; + napi_get_boolean(env, wifiDevicePtr->RemoveAllDevice() == WIFI_OPT_SUCCESS, &result); + return result; +} + +/* This interface has not been implemented */ +napi_value DisableNetwork(napi_env env, napi_callback_info info) +{ + TRACE_FUNC_CALL; + size_t argc = 1; + napi_value argv[1]; + napi_value thisVar; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + NAPI_ASSERT(env, argc == 1, "Wrong number of arguments"); + + napi_valuetype valueType; + napi_typeof(env, argv[0], &valueType); + NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. napi_number expected."); + NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); + + int networkId = -1; + napi_get_value_int32(env, argv[0], &networkId); + napi_value result = nullptr; + return result; +} + +napi_value GetCountryCode(napi_env env, napi_callback_info info) +{ + TRACE_FUNC_CALL; + NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); + std::string countryCode; + ErrCode ret = wifiDevicePtr->GetCountryCode(countryCode); + if (ret != WIFI_OPT_SUCCESS) { + WIFI_LOGE("Get countryCode fail: %{public}d", ret); + } + napi_value cc; + napi_create_string_utf8(env, countryCode.c_str(), NAPI_AUTO_LENGTH, &cc); + return cc; +} + +static SecTypeJs ConvertKeyMgmtToSecType(const std::string& keyMgmt) +{ + std::map mapKeyMgmtToSecType = { + {"NONE", SecTypeJs::SEC_TYPE_OPEN}, + {"WEP", SecTypeJs::SEC_TYPE_WEP}, + {"WPA-PSK", SecTypeJs::SEC_TYPE_PSK}, + {"SAE", SecTypeJs::SEC_TYPE_SAE}, + }; + + std::map::iterator iter = mapKeyMgmtToSecType.find(keyMgmt); + return iter == mapKeyMgmtToSecType.end() ? SecTypeJs::SEC_TYPE_OPEN : iter->second; +} + +static void DeviceConfigToJsArray(const napi_env& env, const std::vector& vecDeviceConfigs, + const int idx, napi_value& arrayResult) +{ + napi_value result; + napi_create_object(env, &result); + SetValueUtf8String(env, "ssid", vecDeviceConfigs[idx].ssid.c_str(), result); + SetValueUtf8String(env, "bssid", vecDeviceConfigs[idx].bssid.c_str(), result); + SetValueUtf8String(env, "preSharedKey", vecDeviceConfigs[idx].preSharedKey.c_str(), result); + SetValueBool(env, "isHiddenSsid", vecDeviceConfigs[idx].hiddenSSID, result); + SetValueInt32(env, "securityType", + static_cast(ConvertKeyMgmtToSecType(vecDeviceConfigs[idx].keyMgmt)), result); + + napi_status status = napi_set_element(env, arrayResult, idx, result); + if (status != napi_ok) { + WIFI_LOGE("Wifi napi set element error: %{public}d", status); + } +} + +napi_value GetDeviceConfigs(napi_env env, napi_callback_info info) +{ + TRACE_FUNC_CALL; + NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); + std::vector vecDeviceConfigs; + ErrCode ret = wifiDevicePtr->GetDeviceConfigs(vecDeviceConfigs); + if (ret != WIFI_OPT_SUCCESS) { + WIFI_LOGE("Get device configs fail: %{public}d", ret); + } + + WIFI_LOGI("Get device configs size: %{public}zu", vecDeviceConfigs.size()); + napi_value arrayResult; + napi_create_array_with_length(env, vecDeviceConfigs.size(), &arrayResult); + for (size_t i = 0; i != vecDeviceConfigs.size(); ++i) { + DeviceConfigToJsArray(env, vecDeviceConfigs, i, arrayResult); + } + return arrayResult; +} + +napi_value GetSupportedFeatures(napi_env env, napi_callback_info info) +{ + TRACE_FUNC_CALL; + NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); + long features = -1; + ErrCode ret = wifiDevicePtr->GetSupportedFeatures(features); + if (ret != WIFI_OPT_SUCCESS) { + WIFI_LOGE("Get supported features fail: %{public}d", ret); + } + + napi_value result; + napi_create_int64(env, features, &result); + return result; +} + +napi_value IsFeatureSupported(napi_env env, napi_callback_info info) +{ + TRACE_FUNC_CALL; + size_t argc = 1; + napi_value argv[1]; + napi_value thisVar; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + NAPI_ASSERT(env, argc == 1, "Wrong number of arguments"); + + napi_valuetype valueType; + napi_typeof(env, argv[0], &valueType); + NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. napi_number expected."); + + long feature = -1; + napi_get_value_int64(env, argv[0], (int64_t*)&feature); + NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); + + napi_value result; + napi_get_boolean(env, wifiDevicePtr->IsFeatureSupported(feature), &result); + return result; +} + +napi_value GetDeviceMacAddress(napi_env env, napi_callback_info info) +{ + TRACE_FUNC_CALL; + NAPI_ASSERT(env, wifiDevicePtr != nullptr, "Wifi device instance is null."); + std::string macAddr; + ErrCode ret = wifiDevicePtr->GetDeviceMacAddress(macAddr); + if (ret != WIFI_OPT_SUCCESS) { + WIFI_LOGE("Get mac address fail: %{public}d", ret); + } + + napi_value addr; + napi_create_string_utf8(env, macAddr.c_str(), NAPI_AUTO_LENGTH, &addr); + return addr; +} } // namespace Wifi } // namespace OHOS diff --git a/interfaces/innerkits/native_cpp/napi/wifi_napi_device.h b/interfaces/innerkits/native_cpp/napi/wifi_napi_device.h index a562b7bc3..ce19d2a7b 100755 --- a/interfaces/innerkits/native_cpp/napi/wifi_napi_device.h +++ b/interfaces/innerkits/native_cpp/napi/wifi_napi_device.h @@ -17,6 +17,8 @@ #define WIFI_NAPI_DEVICE_H_ #include "wifi_napi_utils.h" +#include "wifi_device.h" +#include "wifi_scan.h" namespace OHOS { namespace Wifi { @@ -30,13 +32,68 @@ napi_value ConnectToNetwork(napi_env env, napi_callback_info info); napi_value ConnectToDevice(napi_env env, napi_callback_info info); napi_value Disconnect(napi_env env, napi_callback_info info); napi_value GetSignalLevel(napi_env env, napi_callback_info info); +napi_value ReConnect(napi_env env, napi_callback_info info); +napi_value ReAssociate(napi_env env, napi_callback_info info); +napi_value GetIpInfo(napi_env env, napi_callback_info info); +napi_value GetLinkedInfo(napi_env env, napi_callback_info info); +napi_value RemoveDevice(napi_env env, napi_callback_info info); +napi_value RemoveAllNetwork(napi_env env, napi_callback_info info); +napi_value DisableNetwork(napi_env env, napi_callback_info info); +napi_value GetCountryCode(napi_env env, napi_callback_info info); +napi_value GetDeviceConfigs(napi_env env, napi_callback_info info); +napi_value GetSupportedFeatures(napi_env env, napi_callback_info info); +napi_value IsFeatureSupported(napi_env env, napi_callback_info info); +napi_value GetDeviceMacAddress(napi_env env, napi_callback_info info); -enum class SecTypeJs { - SEC_TYPE_INVALID = 0, /* Invalid security type */ - SEC_TYPE_OPEN = 1, /* Open */ - SEC_TYPE_WEP = 2, /* Wired Equivalent Privacy (WEP) */ - SEC_TYPE_PSK = 3, /* Pre-shared key (PSK) */ - SEC_TYPE_SAE = 4, /* Simultaneous Authentication of Equals (SAE) */ +enum class ConnStateJs { + SCANNING, /* The device is searching for an available AP */ + CONNECTING, /* The Wi-Fi connection is being set up */ + AUTHENTICATING, /* The Wi-Fi connection is being authenticated */ + OBTAINING_IPADDR, /* The IP address of the Wi-Fi connection is being obtained */ + CONNECTED, /* The Wi-Fi connection has been set up */ + DISCONNECTING, /* The Wi-Fi connection is being torn down */ + DISCONNECTED, /* The Wi-Fi connection has been torn down */ + UNKNOWN /* Failed to set up the Wi-Fi connection */ +}; + +class ScanInfoAsyncContext : public AsyncContext { +public: + std::vector vecScanInfos; + + ScanInfoAsyncContext(napi_env env, napi_async_work work = nullptr, napi_deferred deferred = nullptr) : + AsyncContext(env, work, deferred){} + + ScanInfoAsyncContext() = delete; + + virtual ~ScanInfoAsyncContext(){} +}; + +class AddDeviceConfigContext : public AsyncContext { +public: + WifiDeviceConfig *config; + int addResult; + + AddDeviceConfigContext(napi_env env, napi_async_work work = nullptr, napi_deferred deferred = nullptr) : + AsyncContext(env, work, deferred){ + config = nullptr; + addResult = -1; + } + + AddDeviceConfigContext() = delete; + + virtual ~AddDeviceConfigContext(){} +}; + +class LinkedInfoAsyncContext : public AsyncContext { +public: + WifiLinkedInfo linkedInfo; + + LinkedInfoAsyncContext(napi_env env, napi_async_work work = nullptr, napi_deferred deferred = nullptr) : + AsyncContext(env, work, deferred){} + + LinkedInfoAsyncContext() = delete; + + virtual ~LinkedInfoAsyncContext(){} }; } // namespace Wifi } // namespace OHOS diff --git a/interfaces/innerkits/native_cpp/napi/wifi_napi_entry.cpp b/interfaces/innerkits/native_cpp/napi/wifi_napi_entry.cpp index b30880e02..059554cb1 100755 --- a/interfaces/innerkits/native_cpp/napi/wifi_napi_entry.cpp +++ b/interfaces/innerkits/native_cpp/napi/wifi_napi_entry.cpp @@ -38,7 +38,7 @@ static void InitEventClass(napi_env& env, napi_value& exports) { sizeof(properties) / sizeof(napi_property_descriptor), properties, &eventListenerClass); napi_status status = napi_set_named_property(env, exports, "EventListener", eventListenerClass); if (status != napi_ok) { - WIFI_LOGE("[Napi Entry] Init event class set property error."); + WIFI_LOGE("Init event class set property error."); } } @@ -57,6 +57,24 @@ static napi_value Init(napi_env env, napi_value exports) { DECLARE_NAPI_FUNCTION("connectToDevice", ConnectToDevice), DECLARE_NAPI_FUNCTION("disconnect", Disconnect), DECLARE_NAPI_FUNCTION("getSignalLevel", GetSignalLevel), + DECLARE_NAPI_FUNCTION("reconnect", ReConnect), + DECLARE_NAPI_FUNCTION("reassociate", ReAssociate), + DECLARE_NAPI_FUNCTION("getIpInfo", GetIpInfo), + DECLARE_NAPI_FUNCTION("getLinkedInfo", GetLinkedInfo), + DECLARE_NAPI_FUNCTION("removeDevice", RemoveDevice), + DECLARE_NAPI_FUNCTION("removeAllNetwork", RemoveAllNetwork), + DECLARE_NAPI_FUNCTION("disableNetwork", DisableNetwork), + DECLARE_NAPI_FUNCTION("getCountryCode", GetCountryCode), + DECLARE_NAPI_FUNCTION("getDeviceConfigs", GetDeviceConfigs), + DECLARE_NAPI_FUNCTION("getSupportedFeatures", GetSupportedFeatures), + DECLARE_NAPI_FUNCTION("isFeatureSupported", IsFeatureSupported), + DECLARE_NAPI_FUNCTION("getDeviceMacAddress", GetDeviceMacAddress), + DECLARE_NAPI_FUNCTION("isHotspotActive", IsHotspotActive), + DECLARE_NAPI_FUNCTION("enableHotspot", EnableHotspot), + DECLARE_NAPI_FUNCTION("disableHotspot", DisableHotspot), + DECLARE_NAPI_FUNCTION("setHotspotConfig", SetHotspotConfig), + DECLARE_NAPI_FUNCTION("getHotspotConfig", GetHotspotConfig), + DECLARE_NAPI_FUNCTION("getStations", GetStations), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc)); diff --git a/interfaces/innerkits/native_cpp/napi/wifi_napi_event.cpp b/interfaces/innerkits/native_cpp/napi/wifi_napi_event.cpp index 8b45dc550..11c79a4fb 100755 --- a/interfaces/innerkits/native_cpp/napi/wifi_napi_event.cpp +++ b/interfaces/innerkits/native_cpp/napi/wifi_napi_event.cpp @@ -26,9 +26,19 @@ DEFINE_WIFILOG_LABEL("WifiNAPIEvent"); const std::string WIFI_EVENT_TYPE_POWER_STATE = "wifiStateChange"; const std::string WIFI_EVENT_TYPE_CONN_STATE = "wifiConnectionChange"; +const std::string WIFI_EVENT_TYPE_SCAN_STATE = "wifiScanStateChange"; +const std::string WIFI_EVENT_TYPE_RSSI_STATE = "wifiRssiChange"; +const std::string WIFI_EVENT_TYPE_HOTSPOT_STATE = "hotspotStateChange"; +const std::string WIFI_EVENT_TYPE_AP_STA_JOIN = "hotspotStaJoin"; +const std::string WIFI_EVENT_TYPE_AP_STA_LEAVE = "hotspotStaLeave"; const std::string WIFI_USUAL_EVENT_POWER_STATE = "usual.event.wifi.POWER_STATE"; const std::string WIFI_USUAL_EVENT_CONN_STATE = "usual.event.wifi.CONN_STATE"; +const std::string WIFI_USUAL_EVENT_SCAN_STATE = "usual.event.wifi.SCAN_STATE"; +const std::string WIFI_USUAL_EVENT_RSSI_STATE = "usual.event.wifi.RSSI_VALUE"; +const std::string WIFI_USUAL_EVENT_HOTSPOT_STATE = "usual.event.wifi.HOTSPOT_STATE"; +const std::string WIFI_USUAL_EVENT_AP_STA_JOIN = "usual.event.wifi.WIFI_HS_STA_JOIN"; +const std::string WIFI_USUAL_EVENT_AP_STA_LEAVE = "usual.event.wifi.WIFI_HS_STA_LEAVE"; std::shared_mutex g_regInfoMutex; static std::map g_eventRegisterInfo; @@ -36,6 +46,9 @@ static std::map g_eventRegisterInfo; static std::map g_mapEventTypeToUsualEvent = { { WIFI_EVENT_TYPE_POWER_STATE, WIFI_USUAL_EVENT_POWER_STATE }, { WIFI_EVENT_TYPE_CONN_STATE, WIFI_USUAL_EVENT_CONN_STATE }, + { WIFI_EVENT_TYPE_SCAN_STATE, WIFI_USUAL_EVENT_SCAN_STATE }, + { WIFI_EVENT_TYPE_RSSI_STATE, WIFI_USUAL_EVENT_RSSI_STATE }, + { WIFI_EVENT_TYPE_HOTSPOT_STATE, WIFI_USUAL_EVENT_HOTSPOT_STATE }, }; static std::map g_mapUserDefinedEventProcessFunc = {}; @@ -74,7 +87,7 @@ public: private: std::set m_handlersCb; std::shared_ptr m_subscriber; - EventManager* m_context; + EventManager *m_context; }; void Event::SetName(std::string& name) { @@ -121,38 +134,38 @@ static bool IsEventTypeExist(const std::string& type) { void WifiEventSubscriber::OnReceiveEvent(const CommonEventData& data) { std::string event = data.GetWant().GetAction(); int code = data.GetCode(); - WIFI_LOGI("[Napi Event] Received event: %{public}s, value: %{public}d", event.c_str(), code); + WIFI_LOGI("Received event: %{public}s, value: %{public}d", event.c_str(), code); std::string type; if (!GetEventTypeByUsualEvent(event, type)) { - WIFI_LOGI("[Napi Event] Received event: %{public}s is ignored", event.c_str()); + WIFI_LOGI("Received event: %{public}s is ignored", event.c_str()); return; } - EventManager* manager = nullptr; + EventManager *manager = nullptr; { std::shared_lock guard(g_regInfoMutex); std::map::iterator it = g_eventRegisterInfo.find(type); if (it == g_eventRegisterInfo.end()) { - WIFI_LOGE("[Napi Event] no register info for event: %{public}s", type.c_str()); + WIFI_LOGE("No register info for event: %{public}s", type.c_str()); return; } manager = it->second.GetContext(); if (manager == nullptr) { - WIFI_LOGE("[Napi Event] Context is null"); + WIFI_LOGE("Context is null"); return; } } std::map::iterator iter = g_mapUserDefinedEventProcessFunc.find(type); if (iter != g_mapUserDefinedEventProcessFunc.end()) { - WIFI_LOGI("[Napi Event] Has user-defined func for event: %{public}s", type.c_str()); + WIFI_LOGI("Has user-defined func for event: %{public}s", type.c_str()); iter->second(manager->GetEnv(), type, data); } else { - WIFI_LOGI("[Napi Event] Use default policy to process event: %{public}s", type.c_str()); + WIFI_LOGI("Use default policy to process event: %{public}s", type.c_str()); WifiCommonEvent commonEvent(manager->GetEnv(), type, code); if (!manager->Send(commonEvent)) { - WIFI_LOGE("[Napi Event] Send event error"); + WIFI_LOGE("Send event error"); } } } @@ -165,7 +178,7 @@ EventManager::EventManager(napi_env env, napi_value thisVar) : m_env(env) { EventManager::~EventManager() {} bool EventManager::Send(Event& event) { - WIFI_LOGI("[Napi Event] Report event: %{public}s", event.GetName().c_str()); + WIFI_LOGI("Report event: %{public}s", event.GetName().c_str()); napi_handle_scope scope = nullptr; napi_open_handle_scope(m_env, &scope); @@ -173,7 +186,7 @@ bool EventManager::Send(Event& event) { std::shared_lock guard(g_regInfoMutex); std::map::iterator it = g_eventRegisterInfo.find(event.GetName()); if (it == g_eventRegisterInfo.end()) { - WIFI_LOGE("[Napi Event] Event receive owner not exits: %{public}s", event.GetName().c_str()); + WIFI_LOGE("Event receive owner not exits: %{public}s", event.GetName().c_str()); return false; } @@ -187,7 +200,7 @@ bool EventManager::Send(Event& event) { napi_get_reference_value(m_env, each, &handler); napi_value jsEvent = event.PackResult(); if (napi_call_function(m_env, thisVar, handler, 1, &jsEvent, &undefine) != napi_ok) { - WIFI_LOGE("[Napi Event] Report event failed"); + WIFI_LOGE("Report event failed"); result = false; } } @@ -200,29 +213,33 @@ bool EventManager::SubscribeServiceEvent(const std::string& event) { matchingSkills.AddEvent(event); CommonEventSubscribeInfo subscriberInfo(matchingSkills); std::shared_ptr subscriber = std::make_shared(subscriberInfo); - WIFI_LOGI("[Napi Event] Subscribe event -> %{public}s", event.c_str()); - bool subscribeResult = CommonEventManager::SubscribeCommonEvent(subscriber); - if (subscribeResult) { + if (subscriber == nullptr) { + WIFI_LOGE("subscriber is null."); + return false; + } + WIFI_LOGI("Subscribe event -> %{public}s", event.c_str()); + bool result = CommonEventManager::SubscribeCommonEvent(subscriber); + if (result) { g_eventRegisterInfo[m_eventType].SetSubscriber(subscriber); } else { - WIFI_LOGE("[Napi Event] Subscribe service event error: %{public}s", event.c_str()); + WIFI_LOGE("Subscribe service event error: %{public}s", event.c_str()); } - return subscribeResult; + return result; } bool EventManager::UnsubscribeServiceEvent(const std::string& event) { - bool unsubscribeResult = CommonEventManager::SubscribeCommonEvent(g_eventRegisterInfo[m_eventType].GetSubscriber()); - if (!unsubscribeResult) { - WIFI_LOGE("[Napi Event] Unsubscribe service event error: %{public}s", event.c_str()); + bool result = CommonEventManager::UnSubscribeCommonEvent(g_eventRegisterInfo[m_eventType].GetSubscriber()); + if (!result) { + WIFI_LOGE("Unsubscribe service event error: %{public}s", event.c_str()); } - return unsubscribeResult; + return result; } bool EventManager::SubscribeEvent(const std::string& name, napi_value handler) { - WIFI_LOGI("[Napi Event] Subscribe event: %{public}s", name.c_str()); + WIFI_LOGI("Subscribe event: %{public}s", name.c_str()); if (!IsEventTypeExist(name)) { - WIFI_LOGE("[Napi Event] Subscribe event is not a valid event: %{public}s", name.c_str()); + WIFI_LOGE("Subscribe event is not a valid event: %{public}s", name.c_str()); return false; } SetEventType(name); @@ -233,7 +250,7 @@ bool EventManager::SubscribeEvent(const std::string& name, napi_value handler) { GetUsualEventByEventType(name, usualEvent); bool result = SubscribeServiceEvent(usualEvent); if (!result) { - WIFI_LOGE("[Napi Event] Service register event failed: %{public}s", name.c_str()); + WIFI_LOGE("Service register event failed: %{public}s", name.c_str()); return false; } @@ -242,7 +259,7 @@ bool EventManager::SubscribeEvent(const std::string& name, napi_value handler) { } if (g_eventRegisterInfo[name].GetContext() != this) { - WIFI_LOGW("[Napi Event] Subscribe event context changed!"); + WIFI_LOGW("Subscribe event context changed!"); g_eventRegisterInfo[name].SetContext(this); } @@ -274,10 +291,10 @@ void EventManager::DeleteAllHanderRef(std::set& setRefs) { } bool EventManager::UnsubscribeEvent(const std::string& name, napi_value handler) { - WIFI_LOGI("[Napi Event] Unsubscribe event: %{public}s", name.c_str()); + WIFI_LOGI("Unsubscribe event: %{public}s", name.c_str()); if (!IsEventTypeExist(name)) { - WIFI_LOGE("[Napi Event] Unsubscribe event is not a valid event: %{public}s", name.c_str()); + WIFI_LOGE("Unsubscribe event is not a valid event: %{public}s", name.c_str()); return false; } @@ -285,13 +302,13 @@ bool EventManager::UnsubscribeEvent(const std::string& name, napi_value handler) std::unique_lock guard(g_regInfoMutex); std::map::iterator it = g_eventRegisterInfo.find(name); if (it == g_eventRegisterInfo.end()) { - WIFI_LOGE("[Napi Event] Unsubscribe event is not subscribe: %{public}s", name.c_str()); + WIFI_LOGE("Unsubscribe event is not subscribe: %{public}s", name.c_str()); return false; } if (handler != nullptr) { DeleteHanderRef(it->second.GetHandlersCb(), handler); } else { - WIFI_LOGW("[Napi Event] All callback is unsubscribe for event: %{public}s", name.c_str()); + WIFI_LOGW("All callback is unsubscribe for event: %{public}s", name.c_str()); DeleteAllHanderRef(it->second.GetHandlersCb()); } /* No one subscribes event now */ @@ -306,7 +323,7 @@ bool EventManager::UnsubscribeEvent(const std::string& name, napi_value handler) bool result = UnsubscribeServiceEvent(usualEvent); g_eventRegisterInfo.erase(name); if (!result) { - WIFI_LOGE("[Napi Event] Service unregister event failed: %{public}s", name.c_str()); + WIFI_LOGE("Service unregister event failed: %{public}s", name.c_str()); return false; } } @@ -322,6 +339,7 @@ napi_env EventManager::GetEnv() { } napi_value On(napi_env env, napi_callback_info cbinfo) { + TRACE_FUNC_CALL; size_t requireArgc = 2; size_t argc = 2; napi_value argv[2] = {0}; @@ -337,15 +355,15 @@ napi_value On(napi_env env, napi_callback_info cbinfo) { napi_typeof(env, argv[1], &handler); NAPI_ASSERT(env, handler == napi_function, "type mismatch for parameter 2"); - EventManager* manager = nullptr; + EventManager *manager = nullptr; napi_status status = napi_unwrap(env, thisVar, (void**)&manager); - if (status == napi_ok) { + if (status == napi_ok && manager != nullptr) { char type[64] = {0}; size_t typeLen = 0; napi_get_value_string_utf8(env, argv[0], type, sizeof(type), &typeLen); manager->SubscribeEvent(type, argv[1]); } else { - WIFI_LOGE("[Napi Event] On unwrap class failed"); + WIFI_LOGE("On unwrap class failed"); } napi_value result = nullptr; napi_get_undefined(env, &result); @@ -353,6 +371,7 @@ napi_value On(napi_env env, napi_callback_info cbinfo) { } napi_value Off(napi_env env, napi_callback_info cbinfo) { + TRACE_FUNC_CALL; size_t requireArgc = 1; size_t requireArgcWithCb = 2; size_t argc = 2; @@ -371,15 +390,15 @@ napi_value Off(napi_env env, napi_callback_info cbinfo) { NAPI_ASSERT(env, handler == napi_function, "type mismatch for parameter 2"); } - EventManager* manager = nullptr; + EventManager *manager = nullptr; napi_status status = napi_unwrap(env, thisVar, (void**)&manager); - if (status == napi_ok) { + if (status == napi_ok && manager != nullptr) { char type[64] = {0}; size_t typeLen = 0; napi_get_value_string_utf8(env, argv[0], type, sizeof(type), &typeLen); manager->UnsubscribeEvent(type, argc >= requireArgcWithCb ? argv[1] : nullptr); } else { - WIFI_LOGE("[Napi Event] Off unwrap class failed"); + WIFI_LOGE("Off unwrap class failed"); } napi_value result = nullptr; napi_get_undefined(env, &result); @@ -387,18 +406,23 @@ napi_value Off(napi_env env, napi_callback_info cbinfo) { } napi_value EventListenerConstructor(napi_env env, napi_callback_info cbinfo) { + WIFI_LOGI("Event listener constructor"); napi_value thisVar = nullptr; void* data = nullptr; napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, &data); - EventManager* eventManager = new EventManager(env, thisVar); - WIFI_LOGI("[Napi Event] Event listener constructor"); + EventManager *eventManager = new EventManager(env, thisVar); + if (eventManager == nullptr) { + WIFI_LOGE("Init listener constructor failed"); + return nullptr; + } napi_wrap( env, thisVar, eventManager, [](napi_env env, void* data, void* hint) { - WIFI_LOGI("[Napi Event] Event listener destructor"); - EventManager* eventManager = (EventManager *)data; + WIFI_LOGI("Event listener destructor"); + EventManager *eventManager = (EventManager *)data; delete eventManager; + eventManager = nullptr; }, nullptr, nullptr); return thisVar; diff --git a/interfaces/innerkits/native_cpp/napi/wifi_napi_hotspot.cpp b/interfaces/innerkits/native_cpp/napi/wifi_napi_hotspot.cpp index 905df40e1..0d0006346 100755 --- a/interfaces/innerkits/native_cpp/napi/wifi_napi_hotspot.cpp +++ b/interfaces/innerkits/native_cpp/napi/wifi_napi_hotspot.cpp @@ -14,10 +14,182 @@ */ #include "wifi_napi_hotspot.h" +#include "wifi_hotspot.h" #include "wifi_logger.h" +#include namespace OHOS { namespace Wifi { +DEFINE_WIFILOG_LABEL("WifiNAPIHotspot"); +std::unique_ptr wifiHotspotPtr = WifiHotspot::GetInstance(WIFI_HOTSPOT_ABILITY_ID); + +std::map g_mapSecTypeToKeyMgmt = { + {SecTypeJs::SEC_TYPE_OPEN, KeyMgmt::NONE}, + {SecTypeJs::SEC_TYPE_PSK, KeyMgmt::WPA_PSK}, +}; + +napi_value EnableHotspot(napi_env env, napi_callback_info info) +{ + TRACE_FUNC_CALL; + NAPI_ASSERT(env, wifiHotspotPtr != nullptr, "Wifi hotspot instance is null."); + ErrCode ret = wifiHotspotPtr->EnableHotspot(); + if (ret != WIFI_OPT_SUCCESS) { + WIFI_LOGE("Enable hotspot error: %{public}d", ret); + } + napi_value result; + napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result); + return result; +} + +napi_value DisableHotspot(napi_env env, napi_callback_info info) +{ + TRACE_FUNC_CALL; + NAPI_ASSERT(env, wifiHotspotPtr != nullptr, "Wifi hotspot instance is null."); + ErrCode ret = wifiHotspotPtr->DisableHotspot(); + if (ret != WIFI_OPT_SUCCESS) { + WIFI_LOGE("Disable hotspot error: %{public}d", ret); + } + napi_value result; + napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result); + return result; +} + +napi_value IsHotspotActive(napi_env env, napi_callback_info info) +{ + NAPI_ASSERT(env, wifiHotspotPtr != nullptr, "Wifi hotspot instance is null."); + napi_value result; + napi_get_boolean(env, wifiHotspotPtr->IsHotspotActive(), &result); + return result; +} + +static KeyMgmt GetKeyMgmtFromJsSecurityType(int secType) +{ + std::map::iterator iter = g_mapSecTypeToKeyMgmt.find(SecTypeJs(secType)); + return iter == g_mapSecTypeToKeyMgmt.end() ? KeyMgmt::NONE : iter->second; +} + +static int GetJsSecurityTypeFromKeyMgmt(KeyMgmt keyMgmt) +{ + for (auto& each : g_mapSecTypeToKeyMgmt) { + if (each.second == keyMgmt) { + return static_cast(each.first); + } + } + return static_cast(SecTypeJs::SEC_TYPE_OPEN); +} + +static bool IsSecTypeSupported(int secType) +{ + return g_mapSecTypeToKeyMgmt.find(SecTypeJs(secType)) != g_mapSecTypeToKeyMgmt.end(); +} + +static bool GetHotspotconfigFromJs(const napi_env& env, const napi_value& object, HotspotConfig& config) +{ + std::string str = ""; + int value = 0; + JsObjectToString(env, object, "ssid", 33, str); // 33: ssid max length is 32 + '\0' + config.SetSsid(str); + str = ""; + JsObjectToInt(env, object, "securityType", value); + if (!IsSecTypeSupported(value)) { + WIFI_LOGE("securityType is not supported: %{public}d", value); + return false; + } + config.SetSecurityType(GetKeyMgmtFromJsSecurityType(value)); + value = 0; + JsObjectToInt(env, object, "band", value); + config.SetBand(BandType(value)); // 1: 2.4G, 2: 5G + value = 0; + JsObjectToString(env, object, "preSharedKey", 64, str); // 64: max length + config.SetPreSharedKey(str); + JsObjectToInt(env, object, "maxConn", value); + config.SetMaxConn(value); + return true; +} + +napi_value SetHotspotConfig(napi_env env, napi_callback_info info) +{ + TRACE_FUNC_CALL; + size_t argc = 1; + napi_value argv[1]; + napi_value thisVar; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + + napi_valuetype valueType; + napi_typeof(env, argv[0], &valueType); + NAPI_ASSERT(env, valueType == napi_object, "Wrong argument type. Object expected."); + NAPI_ASSERT(env, wifiHotspotPtr != nullptr, "Wifi hotspot instance is null."); + + ErrCode ret = WIFI_OPT_FAILED; + HotspotConfig config; + if (GetHotspotconfigFromJs(env, argv[0], config)) { + ret = wifiHotspotPtr->SetHotspotConfig(config); + if (ret != WIFI_OPT_SUCCESS) { + WIFI_LOGE("Set hotspot config error: %{public}d", ret); + } + } + napi_value result; + napi_get_boolean(env, ret == WIFI_OPT_SUCCESS, &result); + return result; +} + +static void HotspotconfigToJs(const napi_env& env, HotspotConfig& cppConfig, napi_value& result) +{ + SetValueUtf8String(env, "ssid", cppConfig.GetSsid().c_str(), result); + SetValueInt32(env, "securityType", GetJsSecurityTypeFromKeyMgmt(cppConfig.GetSecurityType()), result); + SetValueInt32(env, "band", static_cast(cppConfig.GetBand()), result); + SetValueUtf8String(env, "preSharedKey", cppConfig.GetPreSharedKey().c_str(), result); + SetValueInt32(env, "maxConn", cppConfig.GetMaxConn(), result); +} + +napi_value GetHotspotConfig(napi_env env, napi_callback_info info) +{ + TRACE_FUNC_CALL; + NAPI_ASSERT(env, wifiHotspotPtr != nullptr, "Wifi hotspot instance is null."); + HotspotConfig config; + ErrCode ret = wifiHotspotPtr->GetHotspotConfig(config); + if (ret != WIFI_OPT_SUCCESS) { + WIFI_LOGE("Get hotspot config error: %{public}d", ret); + } + napi_value result; + napi_create_object(env, &result); + HotspotconfigToJs(env, config, result); + return result; +} + +static void StationInfoToJsArray(const napi_env& env, const std::vector& StationInfo, + const int idx, napi_value& arrayResult) +{ + napi_value result; + napi_create_object(env, &result); + + SetValueUtf8String(env, "name", StationInfo[idx].deviceName.c_str(), result); + SetValueUtf8String(env, "macAddress", StationInfo[idx].bssid.c_str(), result); + SetValueUtf8String(env, "ipAddress", StationInfo[idx].ipAddr.c_str(), result); + napi_status status = napi_set_element(env, arrayResult, idx, result); + if (status != napi_ok) { + WIFI_LOGE("Set station element error: %{public}d", status); + } +} + +napi_value GetStations(napi_env env, napi_callback_info info) +{ + TRACE_FUNC_CALL; + NAPI_ASSERT(env, wifiHotspotPtr != nullptr, "Wifi hotspot instance is null."); + std::vector vecStationInfo; + ErrCode ret = wifiHotspotPtr->GetStationList(vecStationInfo); + if (ret != WIFI_OPT_SUCCESS) { + WIFI_LOGE("Get station list error: %{public}d", ret); + } + WIFI_LOGI("Get station list size: %{public}zu", vecStationInfo.size()); + + napi_value arrayResult; + napi_create_array_with_length(env, vecStationInfo.size(), &arrayResult); + for (size_t i = 0; i != vecStationInfo.size(); ++i) { + StationInfoToJsArray(env, vecStationInfo, i, arrayResult); + } + return arrayResult; +} } // namespace Wifi } // namespace OHOS diff --git a/interfaces/innerkits/native_cpp/napi/wifi_napi_hotspot.h b/interfaces/innerkits/native_cpp/napi/wifi_napi_hotspot.h index 74c47d78a..095b50a98 100755 --- a/interfaces/innerkits/native_cpp/napi/wifi_napi_hotspot.h +++ b/interfaces/innerkits/native_cpp/napi/wifi_napi_hotspot.h @@ -20,7 +20,12 @@ namespace OHOS { namespace Wifi { - +napi_value EnableHotspot(napi_env env, napi_callback_info info); +napi_value DisableHotspot(napi_env env, napi_callback_info info); +napi_value IsHotspotActive(napi_env env, napi_callback_info info); +napi_value SetHotspotConfig(napi_env env, napi_callback_info info); +napi_value GetHotspotConfig(napi_env env, napi_callback_info info); +napi_value GetStations(napi_env env, napi_callback_info info); } // namespace Wifi } // namespace OHOS diff --git a/interfaces/innerkits/native_cpp/napi/wifi_napi_utils.cpp b/interfaces/innerkits/native_cpp/napi/wifi_napi_utils.cpp index be68a0100..ece60b7f5 100755 --- a/interfaces/innerkits/native_cpp/napi/wifi_napi_utils.cpp +++ b/interfaces/innerkits/native_cpp/napi/wifi_napi_utils.cpp @@ -21,6 +21,25 @@ namespace OHOS { namespace Wifi { DEFINE_WIFILOG_LABEL("WifiNAPIUtils"); +TraceFuncCall::TraceFuncCall(std::string funcName): m_funcName(funcName) +{ + if (m_isTrace) { + m_startTime = std::chrono::steady_clock::now(); + WIFI_LOGD("Call func: %{public}s (start)", m_funcName.c_str()); + } +} + +TraceFuncCall::~TraceFuncCall() +{ + if (m_isTrace) { + auto us = std::chrono::duration_cast + (std::chrono::steady_clock::now() - m_startTime).count(); + constexpr int usForPerMs = 1000; + WIFI_LOGD("Call func: %{public}s (end), time cost:%{public}lldus, %{public}lldms", + m_funcName.c_str(), us, us / usForPerMs); + } +} + napi_value UndefinedNapiValue(const napi_env& env) { napi_value result; @@ -45,7 +64,7 @@ napi_value JsObjectToString(const napi_env& env, const napi_value& object, } char *buf = (char *)malloc(bufLen); if (buf == nullptr) { - WIFI_LOGE("[Wifi Js] js object to str malloc failed"); + WIFI_LOGE("Js object to str malloc failed"); goto error; } (void)memset_s(buf, bufLen, 0, bufLen); @@ -55,7 +74,7 @@ napi_value JsObjectToString(const napi_env& env, const napi_value& object, free(buf); buf = nullptr; } else { - WIFI_LOGW("[Wifi Js] wifi napi js to str no property: %{public}s", fieldStr); + WIFI_LOGW("Js obj to str no property: %{public}s", fieldStr); } error: @@ -75,7 +94,7 @@ napi_value JsObjectToInt(const napi_env& env, const napi_value& object, const ch NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Number expected."); napi_get_value_int32(env, field, &fieldRef); } else { - WIFI_LOGW("[Wifi Js] wifi napi js to int no property: %{public}s", fieldStr); + WIFI_LOGW("Js to int no property: %{public}s", fieldStr); } return UndefinedNapiValue(env); } @@ -93,31 +112,188 @@ napi_value JsObjectToBool(const napi_env& env, const napi_value& object, const c NAPI_ASSERT(env, valueType == napi_boolean, "Wrong argument type. Bool expected."); napi_get_value_bool(env, field, &fieldRef); } else { - WIFI_LOGW("[Wifi Js] wifi napi js to bool no property: %{public}s", fieldStr); + WIFI_LOGW("Js to bool no property: %{public}s", fieldStr); } return UndefinedNapiValue(env); } -void SetValueUtf8String(const napi_env& env, const char* fieldStr, const char* str, napi_value& result) +napi_status SetValueUtf8String(const napi_env& env, const char* fieldStr, const char* str, napi_value& result) +{ + napi_value value; + napi_status status = napi_create_string_utf8(env, str, NAPI_AUTO_LENGTH, &value); + if (status != napi_ok) { + WIFI_LOGE("Set value create utf8 string error! field: %{public}s", fieldStr); + return status; + } + status = napi_set_named_property(env, result, fieldStr, value); + if (status != napi_ok) { + WIFI_LOGE("Set utf8 string named property error! field: %{public}s", fieldStr); + } + return status; +} + +napi_status SetValueInt32(const napi_env& env, const char* fieldStr, const int intValue, napi_value& result) { napi_value value; - napi_create_string_utf8(env, str, NAPI_AUTO_LENGTH, &value); - napi_set_named_property(env, result, fieldStr, value); + napi_status status = napi_create_int32(env, intValue, &value); + if (status != napi_ok) { + WIFI_LOGE("Set value create int32 error! field: %{public}s", fieldStr); + return status; + } + status = napi_set_named_property(env, result, fieldStr, value); + if (status != napi_ok) { + WIFI_LOGE("Set int32 named property error! field: %{public}s", fieldStr); + } + return status; } -void SetValueInt32(const napi_env& env, const char* fieldStr, const int intValue, napi_value& result) +napi_status SetValueInt64(const napi_env& env, const char* fieldStr, const int64_t intValue, napi_value& result) { napi_value value; - napi_create_int32(env, intValue, &value); - napi_set_named_property(env, result, fieldStr, value); + napi_status status = napi_create_int64(env, intValue, &value); + if (status != napi_ok) { + WIFI_LOGE("Set value create int64 error! field: %{public}s", fieldStr); + return status; + } + status = napi_set_named_property(env, result, fieldStr, value); + if (status != napi_ok) { + WIFI_LOGE("Set int64 named property error! field: %{public}s", fieldStr); + } + return status; } -void SetValueInt64(const napi_env& env, const char* fieldStr, const int64_t intValue, napi_value& result) +napi_status SetValueBool(const napi_env& env, const char* fieldStr, const bool boolvalue, napi_value& result) { napi_value value; - napi_create_int64(env, intValue, &value); - napi_set_named_property(env, result, fieldStr, value); + napi_status status = napi_get_boolean(env, boolvalue, &value); + if (status != napi_ok) { + WIFI_LOGE("Set value create boolean error! field: %{public}s", fieldStr); + return status; + } + status = napi_set_named_property(env, result, fieldStr, value); + if (status != napi_ok) { + WIFI_LOGE("Set boolean named property error! field: %{public}s", fieldStr); + } + return status; } +static napi_value InitAsyncCallBackEnv(const napi_env& env, AsyncContext *asyncContext, + const size_t argc, const napi_value *argv, const size_t nonCallbackArgNum) +{ + for (size_t i = nonCallbackArgNum; i != argc; ++i) { + napi_valuetype valuetype; + NAPI_CALL(env, napi_typeof(env, argv[i], &valuetype)); + NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); + napi_create_reference(env, argv[i], 1, &asyncContext->callback[i - nonCallbackArgNum]); + } + return nullptr; +} + +static napi_value InitAsyncPromiseEnv(const napi_env& env, AsyncContext *asyncContext, napi_value& promise) +{ + napi_deferred deferred; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + asyncContext->deferred = deferred; + return nullptr; +} + +static napi_value DoCallBackAsyncWork(const napi_env& env, AsyncContext *asyncContext) +{ + napi_create_async_work( + env, + nullptr, + asyncContext->resourceName, + [](napi_env env, void* data) { + if (data == nullptr) { + WIFI_LOGE("Async data parameter is null"); + return; + } + AsyncContext *context = (AsyncContext *)data; + context->executeFunc(context); + }, + [](napi_env env, napi_status status, void* data) { + if (data == nullptr) { + WIFI_LOGE("Async data parameter is null"); + return; + } + AsyncContext *context = (AsyncContext *)data; + napi_value undefine; + napi_get_undefined(env, &undefine); + napi_value callback; + context->completeFunc(data); + if (context->isSuccess) { + napi_get_reference_value(env, context->callback[0], &callback); + napi_call_function(env, nullptr, callback, 1, &context->result, &undefine); + } else { + if (context->callback[1]) { + napi_get_reference_value(env, context->callback[1], &callback); + napi_call_function(env, nullptr, callback, 1, &context->result, &undefine); + } else { + WIFI_LOGE("Get scan info callback func is null"); + } + } + if (context->callback[0] != nullptr) { + napi_delete_reference(env, context->callback[0]); + } + if (context->callback[1] != nullptr) { + napi_delete_reference(env, context->callback[1]); + } + napi_delete_async_work(env, context->work); + delete context; + }, + (void *)asyncContext, + &asyncContext->work); + NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); + return UndefinedNapiValue(env); +} + +static napi_value DoPromiseAsyncWork(const napi_env& env, AsyncContext *asyncContext) +{ + napi_create_async_work( + env, + nullptr, + asyncContext->resourceName, + [](napi_env env, void *data) { + if (data == nullptr) { + WIFI_LOGE("Async data parameter is null"); + return; + } + AsyncContext *context = (AsyncContext *)data; + context->executeFunc(context); + }, + [](napi_env env, napi_status status, void *data) { + if (data == nullptr) { + WIFI_LOGE("Async data parameter is null"); + return; + } + AsyncContext *context = (AsyncContext *)data; + context->completeFunc(data); + if (context->isSuccess) { + napi_resolve_deferred(context->env, context->deferred, context->result); + } else { + napi_reject_deferred(context->env, context->deferred, context->result); + } + napi_delete_async_work(env, context->work); + delete context; + }, + (void *)asyncContext, + &asyncContext->work); + napi_queue_async_work(env, asyncContext->work); + return UndefinedNapiValue(env); +} + +napi_value DoAsyncWork(const napi_env& env, AsyncContext *asyncContext, + const size_t argc, const napi_value *argv, const size_t nonCallbackArgNum) +{ + if (argc > nonCallbackArgNum) { + InitAsyncCallBackEnv(env, asyncContext, argc, argv, nonCallbackArgNum); + return DoCallBackAsyncWork(env, asyncContext); + } else { + napi_value promise; + InitAsyncPromiseEnv(env, asyncContext, promise); + DoPromiseAsyncWork(env, asyncContext); + return promise; + } +} } // namespace Wifi } // namespace OHOS diff --git a/interfaces/innerkits/native_cpp/napi/wifi_napi_utils.h b/interfaces/innerkits/native_cpp/napi/wifi_napi_utils.h index b9eb6395d..9e16e0f57 100755 --- a/interfaces/innerkits/native_cpp/napi/wifi_napi_utils.h +++ b/interfaces/innerkits/native_cpp/napi/wifi_napi_utils.h @@ -17,30 +17,78 @@ #define WIFI_NAPI_UTILS_H_ #include +#include #include "napi/native_api.h" #include "napi/native_node_api.h" namespace OHOS { namespace Wifi { -napi_value UndefinedNapiValue(const napi_env& env); -napi_value JsObjectToString(const napi_env& env, const napi_value& object, - const char* fieldStr, const int bufLen, std::string& fieldRef); -napi_value JsObjectToInt(const napi_env& env, const napi_value& object, const char* fieldStr, int& fieldRef); -napi_value JsObjectToBool(const napi_env& env, const napi_value& object, const char* fieldStr, bool& fieldRef); -void SetValueUtf8String(const napi_env& env, const char* fieldStr, const char* str, napi_value& result); -void SetValueInt32(const napi_env& env, const char* fieldStr, const int intValue, napi_value& result); -void SetValueInt64(const napi_env& env, const char* fieldStr, const int64_t intValue, napi_value& result); +class TraceFuncCall final { +public: + TraceFuncCall(std::string funcName); + + TraceFuncCall() = delete; + + ~TraceFuncCall(); + +private: + std::string m_funcName; + std::chrono::steady_clock::time_point m_startTime; + bool m_isTrace = true; +}; + +#define TRACE_FUNC_CALL TraceFuncCall func(__func__) +#define TRACE_FUNC_CALL_NAME(name) TraceFuncCall funcName(name) -struct AsyncCallbackInfo { +class AsyncContext { +public: napi_env env; - napi_async_work asyncWork; + napi_async_work work; napi_deferred deferred; napi_ref callback[2] = { 0 }; - void *obj; + std::function executeFunc; + std::function completeFunc; + napi_value resourceName; napi_value result; bool isSuccess; + + AsyncContext(napi_env e, napi_async_work w = nullptr, napi_deferred d = nullptr) + { + env = e; + work = w; + deferred = d; + executeFunc = nullptr; + completeFunc = nullptr; + result = nullptr; + isSuccess = false; + } + + AsyncContext() = delete; + + virtual ~AsyncContext() + { + } }; +napi_value UndefinedNapiValue(const napi_env& env); +napi_value JsObjectToString(const napi_env& env, const napi_value& object, + const char* fieldStr, const int bufLen, std::string& fieldRef); +napi_value JsObjectToInt(const napi_env& env, const napi_value& object, const char* fieldStr, int& fieldRef); +napi_value JsObjectToBool(const napi_env& env, const napi_value& object, const char* fieldStr, bool& fieldRef); +napi_status SetValueUtf8String(const napi_env& env, const char* fieldStr, const char* str, napi_value& result); +napi_status SetValueInt32(const napi_env& env, const char* fieldStr, const int intValue, napi_value& result); +napi_status SetValueInt64(const napi_env& env, const char* fieldStr, const int64_t intValue, napi_value& result); +napi_status SetValueBool(const napi_env& env, const char* fieldStr, const bool boolValue, napi_value& result); +napi_value DoAsyncWork(const napi_env& env, AsyncContext *asyncContext, + const size_t argc, const napi_value *argv, const size_t nonCallbackArgNum); + +enum class SecTypeJs { + SEC_TYPE_INVALID = 0, /* Invalid security type */ + SEC_TYPE_OPEN = 1, /* Open */ + SEC_TYPE_WEP = 2, /* Wired Equivalent Privacy (WEP) */ + SEC_TYPE_PSK = 3, /* Pre-shared key (PSK) */ + SEC_TYPE_SAE = 4, /* Simultaneous Authentication of Equals (SAE) */ +}; } // namespace Wifi } // namespace OHOS -- Gitee