• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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 #include "wifi_manager_addon.h"
16 
17 #include "edm_constants.h"
18 #include "edm_log.h"
19 #include "message_parcel_utils.h"
20 #include "securec.h"
21 
22 #include "napi_edm_adapter.h"
23 using namespace OHOS::EDM;
24 
CreateWifiSecurityTypeObject(napi_env env,napi_value value)25 void WifiManagerAddon::CreateWifiSecurityTypeObject(napi_env env, napi_value value)
26 {
27     napi_value nInvalid;
28     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(SecurityType::SEC_TYPE_INVALID), &nInvalid));
29     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "WIFI_SEC_TYPE_INVALID", nInvalid));
30     napi_value nOpen;
31     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(SecurityType::SEC_TYPE_OPEN), &nOpen));
32     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "WIFI_SEC_TYPE_OPEN", nOpen));
33     napi_value nWep;
34     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(SecurityType::SEC_TYPE_WEP), &nWep));
35     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "WIFI_SEC_TYPE_WEP", nWep));
36     napi_value nPsk;
37     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(SecurityType::SEC_TYPE_PSK), &nPsk));
38     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "WIFI_SEC_TYPE_PSK", nPsk));
39     napi_value nSae;
40     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(SecurityType::SEC_TYPE_SAE), &nSae));
41     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "WIFI_SEC_TYPE_SAE", nSae));
42     napi_value nEap;
43     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(SecurityType::SEC_TYPE_EAP), &nEap));
44     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "WIFI_SEC_TYPE_EAP", nEap));
45     napi_value nEapSuiteB;
46     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env,
47         static_cast<int32_t>(SecurityType::SEC_TYPE_EAP_SUITE_B), &nEapSuiteB));
48     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "WIFI_SEC_TYPE_EAP_SUITE_B", nEapSuiteB));
49     napi_value nOwe;
50     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(SecurityType::SEC_TYPE_OWE), &nOwe));
51     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "WIFI_SEC_TYPE_OWE", nOwe));
52     napi_value nWapiCert;
53     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env,
54         static_cast<int32_t>(SecurityType::SEC_TYPE_WAPI_CERT), &nWapiCert));
55     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "WIFI_SEC_TYPE_WAPI_CERT", nWapiCert));
56     napi_value nWapiPsk;
57     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env,
58         static_cast<int32_t>(SecurityType::SEC_TYPE_WAPI_PSK), &nWapiPsk));
59     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "WIFI_SEC_TYPE_WAPI_PSK", nWapiPsk));
60 }
61 
CreateIpTypeObject(napi_env env,napi_value value)62 void WifiManagerAddon::CreateIpTypeObject(napi_env env, napi_value value)
63 {
64     napi_value nStatic;
65     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(IpType::STATIC), &nStatic));
66     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "STATIC", nStatic));
67     napi_value nDhcp;
68     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(IpType::DHCP), &nDhcp));
69     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "DHCP", nDhcp));
70     napi_value nUnknown;
71     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env,
72         static_cast<int32_t>(IpType::UNKNOWN), &nUnknown));
73     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "UNKNOWN", nUnknown));
74 }
75 
CreateEapMethodObject(napi_env env,napi_value value)76 void WifiManagerAddon::CreateEapMethodObject(napi_env env, napi_value value)
77 {
78     napi_value nNone;
79     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(EapMethod::EAP_NONE), &nNone));
80     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "EAP_NONE", nNone));
81     napi_value nPeap;
82     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(EapMethod::EAP_PEAP), &nPeap));
83     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "EAP_PEAP", nPeap));
84     napi_value nTls;
85     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(EapMethod::EAP_TLS), &nTls));
86     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "EAP_TLS", nTls));
87     napi_value nTtls;
88     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(EapMethod::EAP_TTLS), &nTtls));
89     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "EAP_TTLS", nTtls));
90     napi_value nPwd;
91     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(EapMethod::EAP_PWD), &nPwd));
92     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "EAP_PWD", nPwd));
93     napi_value nSim;
94     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(EapMethod::EAP_SIM), &nSim));
95     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "EAP_SIM", nSim));
96     napi_value nAka;
97     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(EapMethod::EAP_AKA), &nAka));
98     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "EAP_AKA", nAka));
99     napi_value nAkaPrime;
100     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(EapMethod::EAP_AKA_PRIME), &nAkaPrime));
101     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "EAP_AKA_PRIME", nAkaPrime));
102     napi_value nUnauthTls;
103     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(EapMethod::EAP_UNAUTH_TLS), &nUnauthTls));
104     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "EAP_UNAUTH_TLS", nUnauthTls));
105 }
106 
CreatePhase2MethodObject(napi_env env,napi_value value)107 void WifiManagerAddon::CreatePhase2MethodObject(napi_env env, napi_value value)
108 {
109 #ifdef WIFI_EDM_ENABLE
110     napi_value nNone;
111     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(Wifi::Phase2Method::NONE), &nNone));
112     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "PHASE2_NONE", nNone));
113     napi_value nPap;
114     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(Wifi::Phase2Method::PAP), &nPap));
115     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "PHASE2_PAP", nPap));
116     napi_value nMSChap;
117     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(Wifi::Phase2Method::MSCHAP), &nMSChap));
118     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "PHASE2_MSCHAP", nMSChap));
119     napi_value nMSChapV2;
120     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(Wifi::Phase2Method::MSCHAPV2), &nMSChapV2));
121     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "PHASE2_MSCHAPV2", nMSChapV2));
122     napi_value nGtc;
123     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(Wifi::Phase2Method::GTC), &nGtc));
124     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "PHASE2_GTC", nGtc));
125     napi_value nSim;
126     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(Wifi::Phase2Method::SIM), &nSim));
127     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "PHASE2_SIM", nSim));
128     napi_value nAka;
129     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(Wifi::Phase2Method::AKA), &nAka));
130     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "PHASE2_AKA", nAka));
131     napi_value nAkaPrime;
132     NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast<int32_t>(Wifi::Phase2Method::AKA_PRIME), &nAkaPrime));
133     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "PHASE2_AKA_PRIME", nAkaPrime));
134 #endif
135 }
136 
Init(napi_env env,napi_value exports)137 napi_value WifiManagerAddon::Init(napi_env env, napi_value exports)
138 {
139     napi_value nWifiSecurityType = nullptr;
140     NAPI_CALL(env, napi_create_object(env, &nWifiSecurityType));
141     CreateWifiSecurityTypeObject(env, nWifiSecurityType);
142 
143     napi_value nIpType = nullptr;
144     NAPI_CALL(env, napi_create_object(env, &nIpType));
145     CreateIpTypeObject(env, nIpType);
146 
147     napi_value nEapMethod = nullptr;
148     NAPI_CALL(env, napi_create_object(env, &nEapMethod));
149     CreateEapMethodObject(env, nEapMethod);
150 
151     napi_value nPhase2Method = nullptr;
152     NAPI_CALL(env, napi_create_object(env, &nPhase2Method));
153     CreatePhase2MethodObject(env, nPhase2Method);
154 
155     napi_property_descriptor property[] = {
156         DECLARE_NAPI_FUNCTION("isWifiActive", IsWifiActive),
157         DECLARE_NAPI_FUNCTION("setWifiProfile", SetWifiProfile),
158         DECLARE_NAPI_FUNCTION("setWifiDisabled", SetWifiDisabled),
159         DECLARE_NAPI_FUNCTION("isWifiDisabled", IsWifiDisabled),
160         DECLARE_NAPI_FUNCTION("isWifiActiveSync", IsWifiActiveSync),
161         DECLARE_NAPI_FUNCTION("setWifiProfileSync", SetWifiProfileSync),
162 
163         DECLARE_NAPI_PROPERTY("WifiSecurityType", nWifiSecurityType),
164         DECLARE_NAPI_PROPERTY("IpType", nIpType),
165         DECLARE_NAPI_PROPERTY("EapMethod", nEapMethod),
166         DECLARE_NAPI_PROPERTY("Phase2Method", nPhase2Method)
167     };
168     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(property) / sizeof(property[0]), property));
169     return exports;
170 }
171 
IsWifiActive(napi_env env,napi_callback_info info)172 napi_value WifiManagerAddon::IsWifiActive(napi_env env, napi_callback_info info)
173 {
174     return IsWifiActiveHandler(env, info, NativeIsWifiActive);
175 }
176 
SetWifiDisabled(napi_env env,napi_callback_info info)177 napi_value WifiManagerAddon::SetWifiDisabled(napi_env env, napi_callback_info info)
178 {
179     AddonMethodSign addonMethodSign;
180     addonMethodSign.name = "setWifiDisabled";
181     addonMethodSign.argsType = {EdmAddonCommonType::ELEMENT, EdmAddonCommonType::BOOLEAN};
182     addonMethodSign.methodAttribute = MethodAttribute::HANDLE;
183     addonMethodSign.apiVersionTag = EdmConstants::PERMISSION_TAG_VERSION_11;
184     AdapterAddonData adapterAddonData{};
185     napi_value result = JsObjectToData(env, info, addonMethodSign, &adapterAddonData);
186     if (result == nullptr) {
187         return nullptr;
188     }
189     int32_t ret = WifiManagerProxy::GetWifiManagerProxy()->SetWifiDisabled(adapterAddonData.data);
190     if (FAILED(ret)) {
191         napi_throw(env, CreateError(env, ret));
192     }
193     return nullptr;
194 }
195 
IsWifiDisabled(napi_env env,napi_callback_info info)196 napi_value WifiManagerAddon::IsWifiDisabled(napi_env env, napi_callback_info info)
197 {
198     AddonMethodSign addonMethodSign;
199     addonMethodSign.name = "isWifiDisabled";
200     addonMethodSign.methodAttribute = MethodAttribute::GET;
201     addonMethodSign.argsType = {EdmAddonCommonType::ELEMENT_NULL};
202     addonMethodSign.apiVersionTag = EdmConstants::PERMISSION_TAG_VERSION_11;
203     AdapterAddonData adapterAddonData{};
204     napi_value result = JsObjectToData(env, info, addonMethodSign, &adapterAddonData);
205     if (result == nullptr) {
206         return nullptr;
207     }
208     int32_t ret = ERR_OK;
209     bool isDisabled = false;
210     ret = WifiManagerProxy::GetWifiManagerProxy()->IsWifiDisabled(adapterAddonData.data, isDisabled);
211     if (FAILED(ret)) {
212         napi_throw(env, CreateError(env, ret));
213         return nullptr;
214     }
215     result = nullptr;
216     NAPI_CALL(env, napi_get_boolean(env, isDisabled, &result));
217     return result;
218 }
219 
SetWifiProfile(napi_env env,napi_callback_info info)220 napi_value WifiManagerAddon::SetWifiProfile(napi_env env, napi_callback_info info)
221 {
222     return SetWifiProfileHandler(env, info, NativeSetWifiProfile);
223 }
224 
225 #ifdef WIFI_EDM_ENABLE
NativeSetWifiProfile(napi_env env,void * data)226 void WifiManagerAddon::NativeSetWifiProfile(napi_env env, void *data)
227 {
228     EDMLOGI("NAPI_NativeSetWifiProfile called");
229     if (data == nullptr) {
230         EDMLOGE("data is nullptr");
231         return;
232     }
233     AdapterAddonData *asyncCallbackInfo = static_cast<AdapterAddonData *>(data);
234     auto wifiManagerProxy = WifiManagerProxy::GetWifiManagerProxy();
235     if (wifiManagerProxy == nullptr) {
236         EDMLOGE("can not get WifiManagerProxy");
237         return;
238     }
239     asyncCallbackInfo->ret = wifiManagerProxy->SetWifiProfile(asyncCallbackInfo->data);
240 }
241 #endif
242 
NativeIsWifiActive(napi_env env,void * data)243 void WifiManagerAddon::NativeIsWifiActive(napi_env env, void *data)
244 {
245     EDMLOGI("NAPI_NativeIsWifiActive called");
246     if (data == nullptr) {
247         EDMLOGE("data is nullptr");
248         return;
249     }
250     AdapterAddonData *asyncCallbackInfo = static_cast<AdapterAddonData *>(data);
251     auto wifiManagerProxy = WifiManagerProxy::GetWifiManagerProxy();
252     if (wifiManagerProxy == nullptr) {
253         EDMLOGE("can not get WifiManagerProxy");
254         return;
255     }
256     asyncCallbackInfo->ret = wifiManagerProxy->IsWifiActive(asyncCallbackInfo->data,
257         asyncCallbackInfo->boolRet);
258 }
259 #ifdef WIFI_EDM_ENABLE
JsObjToDeviceConfig(napi_env env,napi_value object,Wifi::WifiDeviceConfig & config,WifiPassword & pwd)260 bool WifiManagerAddon::JsObjToDeviceConfig(napi_env env, napi_value object, Wifi::WifiDeviceConfig &config,
261     WifiPassword &pwd)
262 {
263     int32_t type = static_cast<int32_t>(SecurityType::SEC_TYPE_INVALID);
264     int32_t ipType = static_cast<int32_t>(IpType::UNKNOWN);
265     /* "creatorUid" "disableReason" "randomMacType" "randomMacAddr" is not supported currently */
266     std::tuple<int, bool> charArrayProp = {WIFI_PASSWORD_LEN, true};
267     std::vector<char> ret;
268     if (!JsObjectToString(env, object, "ssid", true, config.ssid) ||
269         !JsObjectToString(env, object, "bssid", false, config.bssid) ||
270         !JsObjectToCharArray(env, object, "preSharedKey", charArrayProp, ret) ||
271         !JsObjectToBool(env, object, "isHiddenSsid", false, config.hiddenSSID) ||
272         !JsObjectToInt(env, object, "securityType", true, type) ||
273         !JsObjectToInt(env, object, "netId", false, config.networkId) ||
274         !JsObjectToInt(env, object, "ipType", false, ipType) ||
275         !ProcessIpType(ipType, env, object, config.wifiIpConfig)) {
276         return false;
277     }
278     if (ret.size() != 0) {
279         pwd.preSharedKey = (char*) malloc(ret.size());
280         if (pwd.preSharedKey == nullptr) {
281             memset_s(ret.data(), ret.size(), '\0', ret.size());
282             return false;
283         }
284         if (strncpy_s(pwd.preSharedKey, ret.size(), ret.data(), ret.size()) != ERR_OK) {
285             memset_s(ret.data(), ret.size(), '\0', ret.size());
286             return false;
287         }
288         memset_s(ret.data(), ret.size(), '\0', ret.size());
289         pwd.preSharedKeySize = ret.size();
290     }
291     if (!ConvertEncryptionMode(type, config, pwd)) {
292         return false;
293     }
294     if (type == static_cast<int32_t>(SecurityType::SEC_TYPE_EAP)) {
295         return ProcessEapConfig(env, object, config.wifiEapConfig, pwd);
296     }
297     return true;
298 }
299 
ConvertEncryptionMode(int32_t securityType,Wifi::WifiDeviceConfig & config,WifiPassword & pwd)300 bool WifiManagerAddon::ConvertEncryptionMode(int32_t securityType, Wifi::WifiDeviceConfig &config, WifiPassword &pwd)
301 {
302     switch (securityType) {
303         case static_cast<int32_t>(SecurityType::SEC_TYPE_OPEN):
304             config.keyMgmt = Wifi::KEY_MGMT_NONE;
305             break;
306         case static_cast<int32_t>(SecurityType::SEC_TYPE_WEP):
307             config.keyMgmt = Wifi::KEY_MGMT_WEP;
308             pwd.wepKey = (char*) malloc((pwd.preSharedKeySize + NAPI_RETURN_ONE) * sizeof(char));
309             if (pwd.wepKey == nullptr) {
310                 return false;
311             }
312             if (strncpy_s(pwd.wepKey, pwd.preSharedKeySize + NAPI_RETURN_ONE,
313                 pwd.preSharedKey, pwd.preSharedKeySize) != ERR_OK) {
314                 return false;
315             }
316             pwd.wepKeySize = pwd.preSharedKeySize;
317             EdmUtils::ClearCharArray(pwd.preSharedKey, pwd.preSharedKeySize);
318             pwd.preSharedKeySize = 0;
319             config.wepTxKeyIndex = 0;
320             break;
321         case static_cast<int32_t>(SecurityType::SEC_TYPE_PSK):
322             config.keyMgmt = Wifi::KEY_MGMT_WPA_PSK;
323             break;
324         case static_cast<int32_t>(SecurityType::SEC_TYPE_SAE):
325             config.keyMgmt = Wifi::KEY_MGMT_SAE;
326             break;
327         case static_cast<int32_t>(SecurityType::SEC_TYPE_EAP):
328             config.keyMgmt = Wifi::KEY_MGMT_EAP;
329             break;
330         default:
331             config.keyMgmt = Wifi::KEY_MGMT_NONE;
332             break;
333     }
334     return true;
335 }
336 
ProcessIpType(int32_t ipType,napi_env env,napi_value object,Wifi::WifiIpConfig & ipConfig)337 bool WifiManagerAddon::ProcessIpType(int32_t ipType, napi_env env, napi_value object, Wifi::WifiIpConfig &ipConfig)
338 {
339     switch (ipType) {
340         case static_cast<int32_t>(IpType::DHCP):
341             ipConfig.assignMethod = Wifi::AssignIpMethod::DHCP;
342             break;
343         case static_cast<int32_t>(IpType::STATIC):
344             ipConfig.assignMethod = Wifi::AssignIpMethod::STATIC;
345             break;
346         default:
347             ipConfig.assignMethod = Wifi::AssignIpMethod::UNASSIGNED;
348             break;
349     }
350     if (ipType == static_cast<int32_t>(IpType::STATIC) && !ConfigStaticIp(env, object, ipConfig)) {
351         return false;
352     }
353     return true;
354 }
355 
ConfigStaticIp(napi_env env,napi_value object,Wifi::WifiIpConfig & ipConfig)356 bool WifiManagerAddon::ConfigStaticIp(napi_env env, napi_value object, Wifi::WifiIpConfig &ipConfig)
357 {
358     napi_value staticIp;
359     napi_value dnsServers;
360     napi_value primaryDns;
361     napi_value secondDns;
362     if (!GetJsProperty(env, object, "staticIp", staticIp) ||
363         !JsObjectToUint(env, staticIp, "ipAddress", true,
364         ipConfig.staticIpAddress.ipAddress.address.addressIpv4) ||
365         !JsObjectToUint(env, staticIp, "gateway", true,
366         ipConfig.staticIpAddress.gateway.addressIpv4) ||
367         !JsObjectToInt(env, staticIp, "prefixLength", true,
368         ipConfig.staticIpAddress.ipAddress.prefixLength) ||
369         !GetJsProperty(env, staticIp, "dnsServers", dnsServers)) {
370         return false;
371     }
372     ipConfig.staticIpAddress.ipAddress.address.family = 0;
373     uint32_t arrayLength = 0;
374     const uint32_t DNS_NUM = 2;
375     napi_get_array_length(env, dnsServers, &arrayLength);
376     if (arrayLength != DNS_NUM) {
377         EDMLOGI("ConfigStaticIp, It needs two dns servers.");
378         return false;
379     }
380     napi_get_element(env, dnsServers, 0, &primaryDns);
381     napi_get_element(env, dnsServers, 1, &secondDns);
382     napi_get_value_uint32(env, primaryDns, &ipConfig.staticIpAddress.dnsServer1.addressIpv4);
383     napi_get_value_uint32(env, secondDns, &ipConfig.staticIpAddress.dnsServer2.addressIpv4);
384     return true;
385 }
386 
ProcessEapConfig(napi_env env,napi_value object,Wifi::WifiEapConfig & eapConfig,WifiPassword & pwd)387 bool WifiManagerAddon::ProcessEapConfig(napi_env env, napi_value object, Wifi::WifiEapConfig &eapConfig,
388     WifiPassword &pwd)
389 {
390     napi_value napiEap;
391     int32_t eapMethod = static_cast<int32_t>(EapMethod::EAP_NONE);
392     if (!GetJsProperty(env, object, "eapProfile", napiEap) ||
393         !JsObjectToInt(env, napiEap, "eapMethod", true, eapMethod)) {
394         return false;
395     }
396     switch (eapMethod) {
397         case static_cast<int32_t>(EapMethod::EAP_PEAP):
398             ProcessEapPeapConfig(env, napiEap, eapConfig, pwd);
399             break;
400         case static_cast<int32_t>(EapMethod::EAP_TLS):
401             ProcessEapTlsConfig(env, napiEap, eapConfig);
402             break;
403         default:
404             EDMLOGI("EapMethod: %{public}d unsupported", eapMethod);
405             return false;
406     }
407     return true;
408 }
409 
ProcessEapPeapConfig(napi_env env,napi_value object,Wifi::WifiEapConfig & eapConfig,WifiPassword & pwd)410 bool WifiManagerAddon::ProcessEapPeapConfig(napi_env env, napi_value object, Wifi::WifiEapConfig &eapConfig,
411     WifiPassword &pwd)
412 {
413     eapConfig.eap = Wifi::EAP_METHOD_PEAP;
414     int32_t phase2 = static_cast<int32_t>(Wifi::Phase2Method::NONE);
415     std::tuple<int, bool> charArrayProp = {WIFI_PASSWORD_LEN, true};
416     std::vector<char> ret;
417     if (!JsObjectToString(env, object, "identity", true, eapConfig.identity) ||
418         !JsObjectToCharArray(env, object, "password", charArrayProp, ret) ||
419         !JsObjectToInt(env, object, "phase2Method", true, phase2)) {
420         return false;
421     }
422     if (ret.size() != 0) {
423         pwd.password = (char*) malloc(ret.size());
424         if (pwd.password == nullptr) {
425             memset_s(ret.data(), ret.size(), '\0', ret.size());
426             return false;
427         }
428         if (strncpy_s(pwd.password, ret.size(), ret.data(), ret.size()) != ERR_OK) {
429             memset_s(ret.data(), ret.size(), '\0', ret.size());
430             return false;
431         }
432         memset_s(ret.data(), ret.size(), '\0', ret.size());
433         pwd.preSharedKeySize = ret.size();
434     }
435     MessageParcelUtils::ProcessPhase2Method(phase2, eapConfig);
436     return true;
437 }
438 
ProcessEapTlsConfig(napi_env env,napi_value object,Wifi::WifiEapConfig & eapConfig)439 bool WifiManagerAddon::ProcessEapTlsConfig(napi_env env, napi_value object, Wifi::WifiEapConfig &eapConfig)
440 {
441     eapConfig.eap = Wifi::EAP_METHOD_TLS;
442     std::tuple<int, bool> charArrayProp = {WIFI_PASSWORD_LEN, true};
443     std::vector<char> ret;
444     if (!JsObjectToString(env, object, "identity", true, eapConfig.identity) ||
445         !JsObjectToCharArray(env, object, "certPassword", charArrayProp, ret) ||
446         !JsObjectToU8Vector(env, object, "certEntry", eapConfig.certEntry)) {
447         return false;
448     }
449     if (ret.size() != 0) {
450         if (strncpy_s(eapConfig.certPassword, ret.size(), ret.data(), ret.size()) != ERR_OK) {
451             memset_s(ret.data(), ret.size(), '\0', ret.size());
452             return false;
453         }
454         memset_s(ret.data(), ret.size(), '\0', ret.size());
455     }
456     return true;
457 }
458 #endif
459 
IsWifiActiveSync(napi_env env,napi_callback_info info)460 napi_value WifiManagerAddon::IsWifiActiveSync(napi_env env, napi_callback_info info)
461 {
462     return IsWifiActiveHandler(env, info, nullptr);
463 }
464 
IsWifiActiveHandler(napi_env env,napi_callback_info info,napi_async_execute_callback execute)465 napi_value WifiManagerAddon::IsWifiActiveHandler(napi_env env,
466     napi_callback_info info, napi_async_execute_callback execute)
467 {
468     AddonMethodSign addonMethodSign;
469     addonMethodSign.name = "isWifiActive";
470     addonMethodSign.argsType = {EdmAddonCommonType::ELEMENT};
471     addonMethodSign.methodAttribute = MethodAttribute::GET;
472     addonMethodSign.apiVersionTag = EdmConstants::PERMISSION_TAG_VERSION_12;
473     if (execute != nullptr) {
474         return AddonMethodAdapter(env, info, addonMethodSign, execute, NativeBoolCallbackComplete);
475     }
476     AdapterAddonData adapterAddonData{};
477     napi_value result = JsObjectToData(env, info, addonMethodSign, &adapterAddonData);
478     if (result == nullptr) {
479         return nullptr;
480     }
481     auto wifiManagerProxy = WifiManagerProxy::GetWifiManagerProxy();
482     if (wifiManagerProxy == nullptr) {
483         EDMLOGE("can not get WifiManagerProxy");
484         return nullptr;
485     }
486     bool isActive = false;
487     int32_t ret = wifiManagerProxy->IsWifiActive(adapterAddonData.data, isActive);
488     if (FAILED(ret)) {
489         napi_throw(env, CreateError(env, ret));
490         return nullptr;
491     }
492     result = nullptr;
493     NAPI_CALL(env, napi_get_boolean(env, isActive, &result));
494     return result;
495 }
496 
SetWifiProfileSync(napi_env env,napi_callback_info info)497 napi_value WifiManagerAddon::SetWifiProfileSync(napi_env env, napi_callback_info info)
498 {
499     return SetWifiProfileHandler(env, info, nullptr);
500 }
501 
SetWifiProfileHandler(napi_env env,napi_callback_info info,napi_async_execute_callback execute)502 napi_value WifiManagerAddon::SetWifiProfileHandler(napi_env env,
503     napi_callback_info info, napi_async_execute_callback execute)
504 {
505 #ifdef WIFI_EDM_ENABLE
506     auto convertWifiDeviceConfigAndPwd2Data = [](napi_env env, napi_value argv, MessageParcel &data,
507         const AddonMethodSign &methodSign) {
508         Wifi::WifiDeviceConfig config;
509         WifiPassword pwd;
510         bool parseRet = JsObjToDeviceConfig(env, argv, config, pwd);
511         if (!parseRet) {
512             napi_throw(env, CreateError(env, EdmReturnErrCode::PARAM_ERROR, "parameter profile parse error"));
513             return false;
514         }
515         MessageParcelUtils::WriteWifiDeviceConfig(config, data, pwd);
516         return true;
517     };
518     AddonMethodSign addonMethodSign;
519     addonMethodSign.name = "setWifiProfile";
520     addonMethodSign.argsType = {EdmAddonCommonType::ELEMENT, EdmAddonCommonType::CUSTOM};
521     addonMethodSign.argsConvert = {nullptr, convertWifiDeviceConfigAndPwd2Data};
522     addonMethodSign.methodAttribute = MethodAttribute::HANDLE;
523     addonMethodSign.apiVersionTag = EdmConstants::PERMISSION_TAG_VERSION_11;
524     if (execute != nullptr) {
525         return AddonMethodAdapter(env, info, addonMethodSign, execute, NativeVoidCallbackComplete);
526     }
527     AdapterAddonData adapterAddonData{};
528     napi_value result = JsObjectToData(env, info, addonMethodSign, &adapterAddonData);
529     if (result == nullptr) {
530         return nullptr;
531     }
532     auto wifiManagerProxy = WifiManagerProxy::GetWifiManagerProxy();
533     if (wifiManagerProxy == nullptr) {
534         EDMLOGE("can not get WifiManagerProxy");
535         return nullptr;
536     }
537     int32_t ret = wifiManagerProxy->SetWifiProfile(adapterAddonData.data);
538     if (FAILED(ret)) {
539         napi_throw(env, CreateError(env, ret));
540     }
541     return nullptr;
542 #else
543     EDMLOGW("WifiManagerAddon::SetWifiProfileSync Unsupported Capabilities.");
544     napi_throw(env, CreateError(env, EdmReturnErrCode::INTERFACE_UNSUPPORTED));
545     return nullptr;
546 #endif
547 }
548 
549 static napi_module g_wifiManagerModule = {
550     .nm_version = 1,
551     .nm_flags = 0,
552     .nm_filename = nullptr,
553     .nm_register_func = WifiManagerAddon::Init,
554     .nm_modname = "enterprise.wifiManager",
555     .nm_priv = ((void *)0),
556     .reserved = { 0 },
557 };
558 
WifiManagerRegister()559 extern "C" __attribute__((constructor)) void WifiManagerRegister()
560 {
561     napi_module_register(&g_wifiManagerModule);
562 }