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 #ifndef WIFI_NAPI_UTILS_H_ 17 #define WIFI_NAPI_UTILS_H_ 18 19 #include <string> 20 #include <chrono> 21 #include "napi/native_api.h" 22 #include "napi/native_node_api.h" 23 #include "wifi_hisysevent.h" 24 25 namespace OHOS { 26 namespace Wifi { 27 class TraceFuncCall final { 28 public: 29 TraceFuncCall(std::string funcName); 30 31 TraceFuncCall() = delete; 32 33 ~TraceFuncCall(); 34 35 private: 36 std::string m_funcName; 37 std::chrono::steady_clock::time_point m_startTime; 38 bool m_isTrace = true; 39 }; 40 41 #define TRACE_FUNC_CALL TraceFuncCall func(__func__) 42 #define TRACE_FUNC_CALL_NAME(name) TraceFuncCall funcName(name) 43 44 constexpr int ERR_CODE_SUCCESS = 0; 45 46 class AsyncContext { 47 public: 48 napi_env env; 49 napi_async_work work; 50 napi_deferred deferred; 51 napi_ref callback[2] = { 0 }; 52 std::function<void(void*)> executeFunc; 53 std::function<void(void*)> completeFunc; 54 napi_value resourceName; 55 napi_value result; 56 int errorCode; 57 58 AsyncContext(napi_env e, napi_async_work w = nullptr, napi_deferred d = nullptr) 59 { 60 env = e; 61 work = w; 62 deferred = d; 63 executeFunc = nullptr; 64 completeFunc = nullptr; 65 result = nullptr; 66 errorCode = ERR_CODE_SUCCESS; 67 } 68 69 AsyncContext() = delete; 70 ~AsyncContext()71 virtual ~AsyncContext() 72 { 73 } 74 }; 75 76 napi_value UndefinedNapiValue(const napi_env& env); 77 napi_value JsObjectToString(const napi_env& env, const napi_value& object, 78 const char* fieldStr, const int bufLen, std::string& fieldRef); 79 napi_value JsObjectToInt(const napi_env& env, const napi_value& object, const char* fieldStr, int& fieldRef); 80 napi_value JsObjectToBool(const napi_env& env, const napi_value& object, const char* fieldStr, bool& fieldRef); 81 napi_status SetValueUtf8String(const napi_env& env, const char* fieldStr, const char* str, napi_value& result); 82 napi_status SetValueInt32(const napi_env& env, const char* fieldStr, const int intValue, napi_value& result); 83 napi_status SetValueUnsignedInt32(const napi_env& env, const char* fieldStr, const int intValue, napi_value& result); 84 napi_status SetValueInt64(const napi_env& env, const char* fieldStr, const int64_t intValue, napi_value& result); 85 napi_status SetValueBool(const napi_env& env, const char* fieldStr, const bool boolValue, napi_value& result); 86 napi_value DoAsyncWork(const napi_env& env, AsyncContext *asyncContext, 87 const size_t argc, const napi_value *argv, const size_t nonCallbackArgNum); 88 89 enum class SecTypeJs { 90 SEC_TYPE_INVALID = 0, /* Invalid security type */ 91 SEC_TYPE_OPEN = 1, /* Open */ 92 SEC_TYPE_WEP = 2, /* Wired Equivalent Privacy (WEP) */ 93 SEC_TYPE_PSK = 3, /* Pre-shared key (PSK) */ 94 SEC_TYPE_SAE = 4, /* Simultaneous Authentication of Equals (SAE) */ 95 }; 96 97 std::string JsAbilityGetBundleName(); 98 } // namespace Wifi 99 } // namespace OHOS 100 101 #endif 102