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