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