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