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 /*
26 * Module initialization function
27 */
Init(napi_env env,napi_value exports)28 static napi_value Init(napi_env env, napi_value exports) {
29 napi_property_descriptor desc[] = {
30 DECLARE_NAPI_FUNCTION("enableWifi", EnableWifi),
31 DECLARE_NAPI_FUNCTION("disableWifi", DisableWifi),
32 DECLARE_NAPI_FUNCTION("isWifiActive", IsWifiActive),
33 DECLARE_NAPI_FUNCTION("scan", Scan),
34 DECLARE_NAPI_FUNCTION("getScanInfos", GetScanInfos),
35 DECLARE_NAPI_FUNCTION("addDeviceConfig", AddDeviceConfig),
36 DECLARE_NAPI_FUNCTION("addUntrustedConfig", AddUntrustedConfig),
37 DECLARE_NAPI_FUNCTION("removeUntrustedConfig", RemoveUntrustedConfig),
38 DECLARE_NAPI_FUNCTION("connectToNetwork", ConnectToNetwork),
39 DECLARE_NAPI_FUNCTION("connectToDevice", ConnectToDevice),
40 DECLARE_NAPI_FUNCTION("isConnected", IsConnected),
41 DECLARE_NAPI_FUNCTION("disconnect", Disconnect),
42 DECLARE_NAPI_FUNCTION("getSignalLevel", GetSignalLevel),
43 DECLARE_NAPI_FUNCTION("reconnect", ReConnect),
44 DECLARE_NAPI_FUNCTION("reassociate", ReAssociate),
45 DECLARE_NAPI_FUNCTION("getIpInfo", GetIpInfo),
46 DECLARE_NAPI_FUNCTION("getLinkedInfo", GetLinkedInfo),
47 DECLARE_NAPI_FUNCTION("removeDevice", RemoveDevice),
48 DECLARE_NAPI_FUNCTION("removeAllNetwork", RemoveAllNetwork),
49 DECLARE_NAPI_FUNCTION("disableNetwork", DisableNetwork),
50 DECLARE_NAPI_FUNCTION("getCountryCode", GetCountryCode),
51 DECLARE_NAPI_FUNCTION("getDeviceConfigs", GetDeviceConfigs),
52 DECLARE_NAPI_FUNCTION("updateNetwork", UpdateNetwork),
53 DECLARE_NAPI_FUNCTION("getSupportedFeatures", GetSupportedFeatures),
54 DECLARE_NAPI_FUNCTION("isFeatureSupported", IsFeatureSupported),
55 DECLARE_NAPI_FUNCTION("getDeviceMacAddress", GetDeviceMacAddress),
56 DECLARE_NAPI_FUNCTION("isHotspotActive", IsHotspotActive),
57 DECLARE_NAPI_FUNCTION("isHotspotDualBandSupported", IsHotspotDualBandSupported),
58 DECLARE_NAPI_FUNCTION("enableHotspot", EnableHotspot),
59 DECLARE_NAPI_FUNCTION("disableHotspot", DisableHotspot),
60 DECLARE_NAPI_FUNCTION("setHotspotConfig", SetHotspotConfig),
61 DECLARE_NAPI_FUNCTION("getHotspotConfig", GetHotspotConfig),
62 DECLARE_NAPI_FUNCTION("getStations", GetStations),
63 DECLARE_NAPI_FUNCTION("getP2pLinkedInfo", GetP2pLinkedInfo),
64 DECLARE_NAPI_FUNCTION("getCurrentGroup", GetCurrentGroup),
65 DECLARE_NAPI_FUNCTION("getP2pPeerDevices", GetP2pDevices),
66 DECLARE_NAPI_FUNCTION("createGroup", CreateGroup),
67 DECLARE_NAPI_FUNCTION("removeGroup", RemoveGroup),
68 DECLARE_NAPI_FUNCTION("p2pConnect", P2pConnect),
69 DECLARE_NAPI_FUNCTION("p2pCancelConnect", P2pCancelConnect),
70 DECLARE_NAPI_FUNCTION("startDiscoverDevices", StartDiscoverDevices),
71 DECLARE_NAPI_FUNCTION("stopDiscoverDevices", StopDiscoverDevices),
72 DECLARE_NAPI_FUNCTION("deletePersistentGroup", DeletePersistentGroup),
73 DECLARE_NAPI_FUNCTION("setDeviceName", SetDeviceName),
74 DECLARE_NAPI_FUNCTION("on", On),
75 DECLARE_NAPI_FUNCTION("off", Off),
76 };
77
78 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc));
79 return exports;
80 }
81
82 static napi_module wifiJsModule = {
83 .nm_version = 1,
84 .nm_flags = 0,
85 .nm_filename = NULL,
86 .nm_register_func = Init,
87 .nm_modname = "wifi",
88 .nm_priv = ((void *)0),
89 .reserved = { 0 }
90 };
91
92 #else
93
94 /*
95 * Module initialization function
96 */
97 static napi_value InitForCompatible(napi_env env, napi_value exports) {
98 napi_property_descriptor desc[] = {
99 DECLARE_NAPI_FUNCTION("enableWifi", EnableWifi),
100 DECLARE_NAPI_FUNCTION("disableWifi", DisableWifi),
101 DECLARE_NAPI_FUNCTION("isWifiActive", IsWifiActive),
102 DECLARE_NAPI_FUNCTION("scan", Scan),
103 DECLARE_NAPI_FUNCTION("getScanInfos", GetScanInfos),
104 DECLARE_NAPI_FUNCTION("addDeviceConfig", AddDeviceConfig),
105 DECLARE_NAPI_FUNCTION("connectToNetwork", ConnectToNetwork),
106 DECLARE_NAPI_FUNCTION("connectToDevice", ConnectToDevice),
107 DECLARE_NAPI_FUNCTION("disconnect", Disconnect),
108 DECLARE_NAPI_FUNCTION("getSignalLevel", GetSignalLevel),
109 };
110
111 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc));
112 return exports;
113 }
114
115 /* @Deprecated - Changeme module name from "wifi_native_js" to "wifi",
116 * "wifi_native_js" will be discarded. Modify @11/2021
117 */
118 static napi_module wifiJsModuleForCompatible = {
119 .nm_version = 1,
120 .nm_flags = 0,
121 .nm_filename = NULL,
122 .nm_register_func = InitForCompatible,
123 .nm_modname = "wifi_native_js",
124 .nm_priv = ((void *)0),
125 .reserved = { 0 }
126 };
127 #endif
128
RegisterModule(void)129 extern "C" __attribute__((constructor)) void RegisterModule(void) {
130 #ifndef ENABLE_NAPI_COMPATIBLE
131 napi_module_register(&wifiJsModule);
132 #else
133 napi_module_register(&wifiJsModuleForCompatible);
134 #endif
135 }
136 } // namespace Wifi
137 } // namespace OHOS
138