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 24 namespace OHOS { 25 namespace Wifi { 26 static constexpr int NAPI_MAX_STR_LENT = 128; 27 static constexpr int NAPI_MAX_IPV4_LEN = 16; 28 static const std::int32_t SYSCAP_WIFI_CORE = 2400000; 29 static const std::int32_t SYSCAP_WIFI_STA = 2500000; 30 static const std::int32_t SYSCAP_WIFI_AP_CORE = 2600000; 31 static const std::int32_t SYSCAP_WIFI_AP_EXT = 2700000; 32 static const std::int32_t SYSCAP_WIFI_P2P = 2800000; 33 34 class TraceFuncCall final { 35 public: 36 TraceFuncCall(std::string funcName); 37 38 TraceFuncCall() = delete; 39 40 ~TraceFuncCall(); 41 42 private: 43 std::string m_funcName; 44 std::chrono::steady_clock::time_point m_startTime; 45 bool m_isTrace = true; 46 }; 47 48 #define TRACE_FUNC_CALL TraceFuncCall func(__func__) 49 #define TRACE_FUNC_CALL_NAME(name) TraceFuncCall funcName(name) 50 51 #ifndef NO_SANITIZE 52 #ifdef __has_attribute 53 #if __has_attribute(no_sanitize) 54 #define NO_SANITIZE(type) __attribute__((no_sanitize(type))) 55 #endif 56 #endif 57 #endif 58 59 #ifndef NO_SANITIZE 60 #define NO_SANITIZE(type) 61 #endif 62 63 constexpr int ERR_CODE_SUCCESS = 0; 64 65 class AsyncContext { 66 public: 67 napi_env env; 68 napi_async_work work; 69 napi_deferred deferred; 70 napi_ref callback[2] = { 0 }; 71 std::function<void(void*)> executeFunc; 72 std::function<void(void*)> completeFunc; 73 napi_value resourceName; 74 napi_value result; 75 int32_t sysCap; 76 int errorCode; 77 78 AsyncContext(napi_env e, napi_async_work w = nullptr, napi_deferred d = nullptr) 79 { 80 env = e; 81 work = w; 82 deferred = d; 83 executeFunc = nullptr; 84 completeFunc = nullptr; 85 result = nullptr; 86 sysCap = 0; 87 errorCode = ERR_CODE_SUCCESS; 88 } 89 90 AsyncContext() = delete; 91 ~AsyncContext()92 virtual ~AsyncContext() 93 { 94 } 95 }; 96 97 napi_value UndefinedNapiValue(const napi_env& env); 98 napi_value CreateInt32(const napi_env& env); 99 napi_value JsObjectToString(const napi_env& env, const napi_value& object, 100 const char* fieldStr, const int bufLen, std::string& fieldRef); 101 napi_value JsObjectToInt(const napi_env& env, const napi_value& object, const char* fieldStr, int& fieldRef); 102 napi_value JsObjectToUint(const napi_env& env, const napi_value& object, const char* fieldStr, uint32_t& fieldRef); 103 napi_value JsObjectToBool(const napi_env& env, const napi_value& object, const char* fieldStr, bool& fieldRef); 104 std::vector<uint8_t> JsObjectToU8Vector(const napi_env& env, const napi_value& object, const char* fieldStr); 105 napi_status SetValueUtf8String(const napi_env& env, const char* fieldStr, const char* str, 106 napi_value& result, size_t strLen = NAPI_AUTO_LENGTH); 107 napi_status SetValueUtf8String(const napi_env& env, const std::string &fieldStr, const std::string &valueStr, 108 napi_value& result); 109 napi_status SetValueInt32(const napi_env& env, const char* fieldStr, const int intValue, napi_value& result); 110 napi_status SetValueUnsignedInt32(const napi_env& env, const char* fieldStr, const int intValue, 111 napi_value& result); 112 napi_status SetValueInt64(const napi_env& env, const char* fieldStr, const int64_t intValue, napi_value& result); 113 napi_status SetValueBool(const napi_env& env, const char* fieldStr, const bool boolValue, napi_value& result); 114 napi_value DoAsyncWork(const napi_env& env, AsyncContext *asyncContext, 115 const size_t argc, const napi_value *argv, const size_t nonCallbackArgNum); 116 void SetNamedPropertyByInteger(napi_env, napi_value dstObj, int32_t objName, const char *propName); 117 118 enum class SecTypeJs { 119 /** Invalid security type */ 120 SEC_TYPE_INVALID = 0, 121 /** Open */ 122 SEC_TYPE_OPEN = 1, 123 /** Wired Equivalent Privacy (WEP) */ 124 SEC_TYPE_WEP = 2, 125 /** Pre-shared key (PSK) */ 126 SEC_TYPE_PSK = 3, 127 /** Simultaneous Authentication of Equals (SAE) */ 128 SEC_TYPE_SAE = 4, 129 /** EAP authentication. */ 130 SEC_TYPE_EAP = 5, 131 #ifdef ENABLE_NAPI_WIFI_MANAGER 132 /** SUITE_B_192 192 bit level. */ 133 SEC_TYPE_EAP_SUITE_B = 6, 134 /** Opportunistic Wireless Encryption. */ 135 SEC_TYPE_OWE = 7, 136 /** WAPI certificate to be specified. */ 137 SEC_TYPE_WAPI_CERT = 8, 138 /** WAPI pre-shared key to be specified. */ 139 SEC_TYPE_WAPI_PSK = 9, 140 #endif 141 }; 142 143 enum class AddressTypeJs { 144 /* random mac address */ 145 ADDR_TYPE_RANDOM_DEVICE_ADDRESS = 0, 146 /* real mac address */ 147 ADDR_TYPE_REAL_DEVICE_ADDRESS = 1 148 }; 149 150 enum class EapMethodJs { 151 EAP_NONE = 0, 152 EAP_PEAP = 1, 153 EAP_TLS = 2, 154 EAP_TTLS = 3, 155 EAP_PWD = 4, 156 EAP_SIM = 5, 157 EAP_AKA = 6, 158 EAP_AKA_PRIME = 7, 159 EAP_UNAUTH_TLS = 8, 160 }; 161 162 enum class ConnStateJs { 163 SCANNING, /* The device is searching for an available AP */ 164 CONNECTING, /* The Wi-Fi connection is being set up */ 165 AUTHENTICATING, /* The Wi-Fi connection is being authenticated */ 166 OBTAINING_IPADDR, /* The IP address of the Wi-Fi connection is being obtained */ 167 CONNECTED, /* The Wi-Fi connection has been set up */ 168 DISCONNECTING, /* The Wi-Fi connection is being torn down */ 169 DISCONNECTED, /* The Wi-Fi connection has been torn down */ 170 UNKNOWN /* Failed to set up the Wi-Fi connection */ 171 }; 172 173 enum class WifiBandTypeJS { 174 BAND_NONE = 0, /* unknown */ 175 BAND_2GHZ = 1, /* 2.4GHz */ 176 BAND_5GHZ = 2, /* 5GHz */ 177 BAND_6GHZ = 3, /* 6GHz */ 178 BAND_60GHZ = 4, /* 60GHz */ 179 }; 180 181 enum class SuppStateJs { 182 DISCONNECTED = 0, /* The network interface is disabled. */ 183 INTERFACE_DISABLED, /* The supplicant is disabled. */ 184 INACTIVE, /* The supplicant is scanning for a Wi-Fi connection. */ 185 SCANNING, /* The supplicant is authenticating with a specified AP. */ 186 AUTHENTICATING, /* The supplicant is associating with a specified AP. */ 187 ASSOCIATING, /* The supplicant is associated with a specified AP. */ 188 ASSOCIATED, /* The four-way handshake is ongoing. */ 189 FOUR_WAY_HANDSHAKE, /* The group handshake is ongoing. */ 190 GROUP_HANDSHAKE, /* All authentication is completed. */ 191 COMPLETED, /* Failed to establish a connection to the supplicant. */ 192 UNINITIALIZED, /* The supplicant is in an unknown or invalid state. */ 193 INVALID, 194 }; 195 196 enum class IpTypeJs { 197 /** Use statically configured IP settings */ 198 IP_TYPE_STATIC, 199 /** Use dynamically configured IP settings */ 200 IP_TYPE_DHCP, 201 /** No IP details are assigned */ 202 IP_TYPE_UNKNOWN, 203 }; 204 205 enum class P2pConnectStateJs { 206 DISCONNECTED = 0, 207 CONNECTED = 1, 208 }; 209 210 enum class P2pDeviceStatusJs { 211 CONNECTED = 0, 212 INVITED = 1, 213 FAILED = 2, 214 AVAILABLE = 3, 215 UNAVAILABLE = 4, 216 }; 217 218 enum class GroupOwnerBandJs { 219 GO_BAND_AUTO = 0, 220 GO_BAND_2GHZ = 1, 221 GO_BAND_5GHZ = 2, 222 }; 223 224 enum class Phase2MethodJs { 225 PHASE2_NONE, 226 PHASE2_PAP, 227 PHASE2_MSCHAP, 228 PHASE2_MSCHAPV2, 229 PHASE2_GTC, 230 PHASE2_SIM, 231 PHASE2_AKA, 232 PHASE2_AKA_PRIME, 233 }; 234 235 enum class WifiChannelWidthJs { 236 WIDTH_20MHZ = 0, 237 WIDTH_40MHZ = 1, 238 WIDTH_80MHZ = 2, 239 WIDTH_160MHZ = 3, 240 WIDTH_80MHZ_PLUS = 4, 241 WIDTH_INVALID, 242 }; 243 244 enum class WifiStandardJs { 245 WIFI_STANDARD_UNDEFINED = 0, 246 WIFI_STANDARD_11A = 1, 247 WIFI_STANDARD_11B = 2, 248 WIFI_STANDARD_11G = 3, 249 WIFI_STANDARD_11N = 4, 250 WIFI_STANDARD_11AC = 5, 251 WIFI_STANDARD_11AX = 6, 252 WIFI_STANDARD_11AD = 7, 253 }; 254 255 enum class PowerModelJs { 256 SLEEPING = 0, 257 GENERAL = 1, 258 THROUGH_WALL = 2, 259 }; 260 261 } // namespace Wifi 262 } // namespace OHOS 263 264 #endif 265