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 #include "wifi_napi_device.h"
17 #include "wifi_napi_hotspot.h"
18 #include "wifi_napi_p2p.h"
19 #include "wifi_napi_event.h"
20 #include "wifi_logger.h"
21
22 namespace OHOS {
23 namespace Wifi {
24 #ifndef ENABLE_NAPI_COMPATIBLE
25
SuppStateInit(napi_env env)26 static napi_value SuppStateInit(napi_env env)
27 {
28 napi_value suppState = nullptr;
29 napi_create_object(env, &suppState);
30 SetNamedPropertyByInteger(env, suppState, static_cast<int>(SuppStateJs::DISCONNECTED), "DISCONNECTED");
31 SetNamedPropertyByInteger(env, suppState, static_cast<int>(SuppStateJs::INTERFACE_DISABLED), "INTERFACE_DISABLED");
32 SetNamedPropertyByInteger(env, suppState, static_cast<int>(SuppStateJs::INACTIVE), "INACTIVE");
33 SetNamedPropertyByInteger(env, suppState, static_cast<int>(SuppStateJs::SCANNING), "SCANNING");
34 SetNamedPropertyByInteger(env, suppState, static_cast<int>(SuppStateJs::AUTHENTICATING), "AUTHENTICATING");
35 SetNamedPropertyByInteger(env, suppState, static_cast<int>(SuppStateJs::ASSOCIATING), "ASSOCIATING");
36 SetNamedPropertyByInteger(env, suppState, static_cast<int>(SuppStateJs::ASSOCIATED), "ASSOCIATED");
37 SetNamedPropertyByInteger(env, suppState, static_cast<int>(SuppStateJs::FOUR_WAY_HANDSHAKE), "FOUR_WAY_HANDSHAKE");
38 SetNamedPropertyByInteger(env, suppState, static_cast<int>(SuppStateJs::GROUP_HANDSHAKE), "GROUP_HANDSHAKE");
39 SetNamedPropertyByInteger(env, suppState, static_cast<int>(SuppStateJs::COMPLETED), "COMPLETED");
40 SetNamedPropertyByInteger(env, suppState, static_cast<int>(SuppStateJs::UNINITIALIZED), "UNINITIALIZED");
41 SetNamedPropertyByInteger(env, suppState, static_cast<int>(SuppStateJs::INVALID), "INVALID");
42 return suppState;
43 }
44
SecurityTypeInit(napi_env env)45 static napi_value SecurityTypeInit(napi_env env)
46 {
47 napi_value securityType = nullptr;
48 napi_create_object(env, &securityType);
49 SetNamedPropertyByInteger(env, securityType,
50 static_cast<int>(SecTypeJs::SEC_TYPE_INVALID), "WIFI_SEC_TYPE_INVALID");
51 SetNamedPropertyByInteger(env, securityType, static_cast<int>(SecTypeJs::SEC_TYPE_OPEN), "WIFI_SEC_TYPE_OPEN");
52 SetNamedPropertyByInteger(env, securityType, static_cast<int>(SecTypeJs::SEC_TYPE_WEP), "WIFI_SEC_TYPE_WEP");
53 SetNamedPropertyByInteger(env, securityType, static_cast<int>(SecTypeJs::SEC_TYPE_PSK), "WIFI_SEC_TYPE_PSK");
54 SetNamedPropertyByInteger(env, securityType, static_cast<int>(SecTypeJs::SEC_TYPE_SAE), "WIFI_SEC_TYPE_SAE");
55 #ifdef ENABLE_NAPI_WIFI_MANAGER
56 SetNamedPropertyByInteger(env, securityType, static_cast<int>(SecTypeJs::SEC_TYPE_EAP), "WIFI_SEC_TYPE_EAP");
57 SetNamedPropertyByInteger(env, securityType,
58 static_cast<int>(SecTypeJs::SEC_TYPE_EAP_SUITE_B), "WIFI_SEC_TYPE_EAP_SUITE_B");
59 SetNamedPropertyByInteger(env, securityType, static_cast<int>(SecTypeJs::SEC_TYPE_OWE), "WIFI_SEC_TYPE_OWE");
60 SetNamedPropertyByInteger(env, securityType,
61 static_cast<int>(SecTypeJs::SEC_TYPE_WAPI_CERT), "WIFI_SEC_TYPE_WAPI_CERT");
62 SetNamedPropertyByInteger(env, securityType,
63 static_cast<int>(SecTypeJs::SEC_TYPE_WAPI_PSK), "WIFI_SEC_TYPE_WAPI_PSK");
64 #endif
65 return securityType;
66 }
67
IpTypeInit(napi_env env)68 static napi_value IpTypeInit(napi_env env)
69 {
70 napi_value IpType = nullptr;
71 napi_create_object(env, &IpType);
72 SetNamedPropertyByInteger(env, IpType, static_cast<int>(IpTypeJs::IP_TYPE_STATIC), "STATIC");
73 SetNamedPropertyByInteger(env, IpType, static_cast<int>(IpTypeJs::IP_TYPE_DHCP), "DHCP");
74 SetNamedPropertyByInteger(env, IpType, static_cast<int>(IpTypeJs::IP_TYPE_UNKNOWN), "UNKNOWN");
75 return IpType;
76 }
77
ConnStateInit(napi_env env)78 static napi_value ConnStateInit(napi_env env)
79 {
80 napi_value connState = nullptr;
81 napi_create_object(env, &connState);
82 SetNamedPropertyByInteger(env, connState, static_cast<int>(ConnStateJs::SCANNING), "SCANNING");
83 SetNamedPropertyByInteger(env, connState, static_cast<int>(ConnStateJs::CONNECTING), "CONNECTING");
84 SetNamedPropertyByInteger(env, connState, static_cast<int>(ConnStateJs::AUTHENTICATING), "AUTHENTICATING");
85 SetNamedPropertyByInteger(env, connState, static_cast<int>(ConnStateJs::OBTAINING_IPADDR), "OBTAINING_IPADDR");
86 SetNamedPropertyByInteger(env, connState, static_cast<int>(ConnStateJs::CONNECTED), "CONNECTED");
87 SetNamedPropertyByInteger(env, connState, static_cast<int>(ConnStateJs::DISCONNECTING), "DISCONNECTING");
88 SetNamedPropertyByInteger(env, connState, static_cast<int>(ConnStateJs::DISCONNECTED), "DISCONNECTED");
89 SetNamedPropertyByInteger(env, connState, static_cast<int>(ConnStateJs::UNKNOWN), "UNKNOWN");
90 return connState;
91 }
92
93
P2pConnStateInit(napi_env env)94 static napi_value P2pConnStateInit(napi_env env)
95 {
96 napi_value p2pConnState = nullptr;
97 napi_create_object(env, &p2pConnState);
98 SetNamedPropertyByInteger(env, p2pConnState, static_cast<int>(P2pConnectStateJs::DISCONNECTED), "DISCONNECTED");
99 SetNamedPropertyByInteger(env, p2pConnState, static_cast<int>(P2pConnectStateJs::CONNECTED), "CONNECTED");
100 return p2pConnState;
101 }
102
P2pDeviceStatusInit(napi_env env)103 static napi_value P2pDeviceStatusInit(napi_env env)
104 {
105 napi_value p2pDeviceStatus = nullptr;
106 napi_create_object(env, &p2pDeviceStatus);
107 SetNamedPropertyByInteger(env, p2pDeviceStatus, static_cast<int>(P2pDeviceStatusJs::CONNECTED), "CONNECTED");
108 SetNamedPropertyByInteger(env, p2pDeviceStatus, static_cast<int>(P2pDeviceStatusJs::INVITED), "INVITED");
109 SetNamedPropertyByInteger(env, p2pDeviceStatus, static_cast<int>(P2pDeviceStatusJs::FAILED), "FAILED");
110 SetNamedPropertyByInteger(env, p2pDeviceStatus, static_cast<int>(P2pDeviceStatusJs::AVAILABLE), "AVAILABLE");
111 SetNamedPropertyByInteger(env, p2pDeviceStatus, static_cast<int>(P2pDeviceStatusJs::UNAVAILABLE), "UNAVAILABLE");
112 return p2pDeviceStatus;
113 }
114
GroupOwnerBandInit(napi_env env)115 static napi_value GroupOwnerBandInit(napi_env env)
116 {
117 napi_value groupOwnerBand = nullptr;
118 napi_create_object(env, &groupOwnerBand);
119 SetNamedPropertyByInteger(env, groupOwnerBand, static_cast<int>(GroupOwnerBandJs::GO_BAND_AUTO), "GO_BAND_AUTO");
120 SetNamedPropertyByInteger(env, groupOwnerBand, static_cast<int>(GroupOwnerBandJs::GO_BAND_2GHZ), "GO_BAND_2GHZ");
121 SetNamedPropertyByInteger(env, groupOwnerBand, static_cast<int>(GroupOwnerBandJs::GO_BAND_5GHZ), "GO_BAND_5GHZ");
122 return groupOwnerBand;
123 }
124
125 #ifdef ENABLE_NAPI_WIFI_MANAGER
Phase2MethodInit(napi_env env)126 static napi_value Phase2MethodInit(napi_env env)
127 {
128 napi_value phase2Method = nullptr;
129 napi_create_object(env, &phase2Method);
130 SetNamedPropertyByInteger(env, phase2Method, static_cast<int>(Phase2MethodJs::PHASE2_NONE), "PHASE2_NONE");
131 SetNamedPropertyByInteger(env, phase2Method, static_cast<int>(Phase2MethodJs::PHASE2_PAP), "PHASE2_PAP");
132 SetNamedPropertyByInteger(env, phase2Method, static_cast<int>(Phase2MethodJs::PHASE2_MSCHAP), "PHASE2_MSCHAP");
133 SetNamedPropertyByInteger(env, phase2Method, static_cast<int>(Phase2MethodJs::PHASE2_MSCHAPV2), "PHASE2_MSCHAPV2");
134 SetNamedPropertyByInteger(env, phase2Method, static_cast<int>(Phase2MethodJs::PHASE2_GTC), "PHASE2_GTC");
135 SetNamedPropertyByInteger(env, phase2Method, static_cast<int>(Phase2MethodJs::PHASE2_SIM), "PHASE2_SIM");
136 SetNamedPropertyByInteger(env, phase2Method, static_cast<int>(Phase2MethodJs::PHASE2_AKA), "PHASE2_AKA");
137 SetNamedPropertyByInteger(env, phase2Method,
138 static_cast<int>(Phase2MethodJs::PHASE2_AKA_PRIME), "PHASE2_AKA_PRIME");
139 return phase2Method;
140 }
WifiChannelWidthInit(napi_env env)141 static napi_value WifiChannelWidthInit(napi_env env)
142 {
143 napi_value wifiChannelWidth = nullptr;
144 napi_create_object(env, &wifiChannelWidth);
145 SetNamedPropertyByInteger(env, wifiChannelWidth, static_cast<int>(WifiChannelWidthJs::WIDTH_20MHZ), "WIDTH_20MHZ");
146 SetNamedPropertyByInteger(env, wifiChannelWidth, static_cast<int>(WifiChannelWidthJs::WIDTH_40MHZ), "WIDTH_40MHZ");
147 SetNamedPropertyByInteger(env, wifiChannelWidth, static_cast<int>(WifiChannelWidthJs::WIDTH_80MHZ), "WIDTH_80MHZ");
148 SetNamedPropertyByInteger(env, wifiChannelWidth,
149 static_cast<int>(WifiChannelWidthJs::WIDTH_160MHZ), "WIDTH_160MHZ");
150 SetNamedPropertyByInteger(env, wifiChannelWidth,
151 static_cast<int>(WifiChannelWidthJs::WIDTH_80MHZ_PLUS), "WIDTH_80MHZ_PLUS");
152 SetNamedPropertyByInteger(env, wifiChannelWidth,
153 static_cast<int>(WifiChannelWidthJs::WIDTH_INVALID), "WIDTH_INVALID");
154 return wifiChannelWidth;
155 }
156
EapMethodInit(napi_env env)157 static napi_value EapMethodInit(napi_env env)
158 {
159 napi_value eapMethod = nullptr;
160 napi_create_object(env, &eapMethod);
161 SetNamedPropertyByInteger(env, eapMethod, static_cast<int>(EapMethodJs::EAP_NONE), "EAP_NONE");
162 SetNamedPropertyByInteger(env, eapMethod, static_cast<int>(EapMethodJs::EAP_PEAP), "EAP_PEAP");
163 SetNamedPropertyByInteger(env, eapMethod, static_cast<int>(EapMethodJs::EAP_TLS), "EAP_TLS");
164 SetNamedPropertyByInteger(env, eapMethod, static_cast<int>(EapMethodJs::EAP_TTLS), "EAP_TTLS");
165 SetNamedPropertyByInteger(env, eapMethod, static_cast<int>(EapMethodJs::EAP_PWD), "EAP_PWD");
166 SetNamedPropertyByInteger(env, eapMethod, static_cast<int>(EapMethodJs::EAP_SIM), "EAP_SIM");
167 SetNamedPropertyByInteger(env, eapMethod, static_cast<int>(EapMethodJs::EAP_AKA), "EAP_AKA");
168 SetNamedPropertyByInteger(env, eapMethod, static_cast<int>(EapMethodJs::EAP_AKA_PRIME), "EAP_AKA_PRIME");
169 SetNamedPropertyByInteger(env, eapMethod, static_cast<int>(EapMethodJs::EAP_UNAUTH_TLS), "EAP_UNAUTH_TLS");
170 return eapMethod;
171 }
172 #endif
173
PropertyValueInit(napi_env env,napi_value exports)174 static napi_value PropertyValueInit(napi_env env, napi_value exports)
175 {
176 napi_value suppStateObj = SuppStateInit(env);
177 napi_value securityTypeObj = SecurityTypeInit(env);
178 napi_value ipTypeObj = IpTypeInit(env);
179 napi_value connStateObj = ConnStateInit(env);
180 napi_value p2pConnStateObj = P2pConnStateInit(env);
181 napi_value P2pDeviceStatusObj = P2pDeviceStatusInit(env);
182 napi_value groupOwnerBandObj = GroupOwnerBandInit(env);
183 #ifdef ENABLE_NAPI_WIFI_MANAGER
184 napi_value phase2MethodObj = Phase2MethodInit(env);
185 napi_value WifiChannelWidthObj = WifiChannelWidthInit(env);
186 napi_value EapMethodObj = EapMethodInit(env);
187 #endif
188 napi_property_descriptor exportFuncs[] = {
189 #ifdef ENABLE_NAPI_WIFI_MANAGER
190 DECLARE_NAPI_PROPERTY("Phase2Method", phase2MethodObj),
191 DECLARE_NAPI_PROPERTY("WifiChannelWidth", WifiChannelWidthObj),
192 DECLARE_NAPI_PROPERTY("EapMethod", EapMethodObj),
193 #endif
194 DECLARE_NAPI_PROPERTY("SuppState", suppStateObj),
195 DECLARE_NAPI_PROPERTY("WifiSecurityType", securityTypeObj),
196 DECLARE_NAPI_PROPERTY("IpType", ipTypeObj),
197 DECLARE_NAPI_PROPERTY("ConnState", connStateObj),
198 DECLARE_NAPI_PROPERTY("P2pConnectState", p2pConnStateObj),
199 DECLARE_NAPI_PROPERTY("P2pDeviceStatus", P2pDeviceStatusObj),
200 DECLARE_NAPI_PROPERTY("GroupOwnerBand", groupOwnerBandObj),
201 };
202 napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(napi_property_descriptor), exportFuncs);
203 return exports;
204 }
205 /*
206 * Module initialization function
207 */
Init(napi_env env,napi_value exports)208 static napi_value Init(napi_env env, napi_value exports) {
209 PropertyValueInit(env, exports);
210 napi_property_descriptor desc[] = {
211 DECLARE_NAPI_FUNCTION("enableWifi", EnableWifi),
212 DECLARE_NAPI_FUNCTION("disableWifi", DisableWifi),
213 DECLARE_NAPI_FUNCTION("isWifiActive", IsWifiActive),
214 DECLARE_NAPI_FUNCTION("scan", Scan),
215 DECLARE_NAPI_FUNCTION("getScanInfos", GetScanInfos),
216 DECLARE_NAPI_FUNCTION("getScanInfosSync", GetScanResults),
217 DECLARE_NAPI_FUNCTION("getScanResults", GetScanInfos),
218 DECLARE_NAPI_FUNCTION("getScanResultsSync", GetScanResults),
219 DECLARE_NAPI_FUNCTION("addDeviceConfig", AddDeviceConfig),
220 DECLARE_NAPI_FUNCTION("addUntrustedConfig", AddUntrustedConfig),
221 DECLARE_NAPI_FUNCTION("removeUntrustedConfig", RemoveUntrustedConfig),
222 DECLARE_NAPI_FUNCTION("addCandidateConfig", AddCandidateConfig),
223 DECLARE_NAPI_FUNCTION("removeCandidateConfig", RemoveCandidateConfig),
224 DECLARE_NAPI_FUNCTION("connectToCandidateConfig", ConnectToCandidateConfig),
225 DECLARE_NAPI_FUNCTION("getCandidateConfigs", GetCandidateConfigs),
226 DECLARE_NAPI_FUNCTION("connectToNetwork", ConnectToNetwork),
227 DECLARE_NAPI_FUNCTION("connectToDevice", ConnectToDevice),
228 DECLARE_NAPI_FUNCTION("isConnected", IsConnected),
229 DECLARE_NAPI_FUNCTION("disconnect", Disconnect),
230 DECLARE_NAPI_FUNCTION("getSignalLevel", GetSignalLevel),
231 DECLARE_NAPI_FUNCTION("reconnect", ReConnect),
232 DECLARE_NAPI_FUNCTION("reassociate", ReAssociate),
233 DECLARE_NAPI_FUNCTION("getIpInfo", GetIpInfo),
234 DECLARE_NAPI_FUNCTION("getLinkedInfo", GetLinkedInfo),
235 DECLARE_NAPI_FUNCTION("removeDevice", RemoveDevice),
236 DECLARE_NAPI_FUNCTION("removeAllNetwork", RemoveAllNetwork),
237 DECLARE_NAPI_FUNCTION("disableNetwork", DisableNetwork),
238 DECLARE_NAPI_FUNCTION("getCountryCode", GetCountryCode),
239 DECLARE_NAPI_FUNCTION("getDeviceConfigs", GetDeviceConfigs),
240 DECLARE_NAPI_FUNCTION("updateNetwork", UpdateNetwork),
241 DECLARE_NAPI_FUNCTION("getSupportedFeatures", GetSupportedFeatures),
242 DECLARE_NAPI_FUNCTION("isFeatureSupported", IsFeatureSupported),
243 DECLARE_NAPI_FUNCTION("getDeviceMacAddress", GetDeviceMacAddress),
244 DECLARE_NAPI_FUNCTION("isHotspotActive", IsHotspotActive),
245 DECLARE_NAPI_FUNCTION("isHotspotDualBandSupported", IsHotspotDualBandSupported),
246 DECLARE_NAPI_FUNCTION("enableHotspot", EnableHotspot),
247 DECLARE_NAPI_FUNCTION("disableHotspot", DisableHotspot),
248 DECLARE_NAPI_FUNCTION("setHotspotConfig", SetHotspotConfig),
249 DECLARE_NAPI_FUNCTION("getHotspotConfig", GetHotspotConfig),
250 DECLARE_NAPI_FUNCTION("getStations", GetStations),
251 DECLARE_NAPI_FUNCTION("addBlockList", AddBlockList),
252 DECLARE_NAPI_FUNCTION("delBlockList", DelBlockList),
253 DECLARE_NAPI_FUNCTION("getP2pLinkedInfo", GetP2pLinkedInfo),
254 DECLARE_NAPI_FUNCTION("getCurrentGroup", GetCurrentGroup),
255 DECLARE_NAPI_FUNCTION("getP2pPeerDevices", GetP2pDevices),
256 DECLARE_NAPI_FUNCTION("getP2pLocalDevice", GetP2pLocalDevice),
257 DECLARE_NAPI_FUNCTION("createGroup", CreateGroup),
258 DECLARE_NAPI_FUNCTION("removeGroup", RemoveGroup),
259 DECLARE_NAPI_FUNCTION("p2pConnect", P2pConnect),
260 DECLARE_NAPI_FUNCTION("p2pCancelConnect", P2pCancelConnect),
261 DECLARE_NAPI_FUNCTION("p2pDisconnect", P2pCancelConnect),
262 DECLARE_NAPI_FUNCTION("startDiscoverDevices", StartDiscoverDevices),
263 DECLARE_NAPI_FUNCTION("stopDiscoverDevices", StopDiscoverDevices),
264 DECLARE_NAPI_FUNCTION("deletePersistentGroup", DeletePersistentGroup),
265 DECLARE_NAPI_FUNCTION("getP2pGroups", GetP2pGroups),
266 DECLARE_NAPI_FUNCTION("setDeviceName", SetDeviceName),
267 DECLARE_NAPI_FUNCTION("on", On),
268 DECLARE_NAPI_FUNCTION("off", Off),
269 };
270
271 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc));
272 return exports;
273 }
274
275 static napi_module wifiJsModule = {
276 .nm_version = 1,
277 .nm_flags = 0,
278 .nm_filename = NULL,
279 .nm_register_func = Init,
280 #ifdef ENABLE_NAPI_WIFI_MANAGER
281 .nm_modname = "wifiManager",
282 #else
283 .nm_modname = "wifi",
284 #endif
285 .nm_priv = ((void *)0),
286 .reserved = { 0 }
287 };
288
289 #else
290
291 /*
292 * Module initialization function
293 */
294 static napi_value InitForCompatible(napi_env env, napi_value exports) {
295 napi_property_descriptor desc[] = {
296 DECLARE_NAPI_FUNCTION("enableWifi", EnableWifi),
297 DECLARE_NAPI_FUNCTION("disableWifi", DisableWifi),
298 DECLARE_NAPI_FUNCTION("isWifiActive", IsWifiActive),
299 DECLARE_NAPI_FUNCTION("scan", Scan),
300 DECLARE_NAPI_FUNCTION("getScanInfos", GetScanInfos),
301 DECLARE_NAPI_FUNCTION("addDeviceConfig", AddDeviceConfig),
302 DECLARE_NAPI_FUNCTION("connectToNetwork", ConnectToNetwork),
303 DECLARE_NAPI_FUNCTION("connectToDevice", ConnectToDevice),
304 DECLARE_NAPI_FUNCTION("disconnect", Disconnect),
305 DECLARE_NAPI_FUNCTION("getSignalLevel", GetSignalLevel),
306 };
307
308 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc));
309 return exports;
310 }
311
312 /* @Deprecated - Changeme module name from "wifi_native_js" to "wifi",
313 * "wifi_native_js" will be discarded. Modify @11/2021
314 */
315 static napi_module wifiJsModuleForCompatible = {
316 .nm_version = 1,
317 .nm_flags = 0,
318 .nm_filename = NULL,
319 .nm_register_func = InitForCompatible,
320 .nm_modname = "wifi_native_js",
321 .nm_priv = ((void *)0),
322 .reserved = { 0 }
323 };
324 #endif
325
RegisterModule(void)326 extern "C" __attribute__((constructor)) void RegisterModule(void) {
327 #ifndef ENABLE_NAPI_COMPATIBLE
328 napi_module_register(&wifiJsModule);
329 #else
330 napi_module_register(&wifiJsModuleForCompatible);
331 #endif
332 }
333 } // namespace Wifi
334 } // namespace OHOS
335