• 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 #include "wifi_napi_hotspot.h"
17 #include "wifi_hotspot.h"
18 #include "wifi_logger.h"
19 #include <vector>
20 #include <map>
21 #include "wifi_napi_errcode.h"
22 
23 namespace OHOS {
24 namespace Wifi {
25 DEFINE_WIFILOG_LABEL("WifiNAPIHotspot");
26 
27 std::unique_ptr<WifiHotspot> wifiHotspotPtr = WifiHotspot::GetInstance(WIFI_HOTSPOT_ABILITY_ID);
28 
29 std::map<SecTypeJs, KeyMgmt> g_mapSecTypeToKeyMgmt = {
30     {SecTypeJs::SEC_TYPE_OPEN, KeyMgmt::NONE},
31     {SecTypeJs::SEC_TYPE_PSK, KeyMgmt::WPA_PSK},
32 };
33 
EnableHotspot(napi_env env,napi_callback_info info)34 napi_value EnableHotspot(napi_env env, napi_callback_info info)
35 {
36     TRACE_FUNC_CALL;
37     WIFI_NAPI_ASSERT(env, wifiHotspotPtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_AP_CORE);
38     ErrCode ret = wifiHotspotPtr->EnableHotspot();
39     if (ret != WIFI_OPT_SUCCESS) {
40         WIFI_LOGE("Enable hotspot error: %{public}d", ret);
41     }
42     WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_AP_CORE);
43 }
44 
DisableHotspot(napi_env env,napi_callback_info info)45 napi_value DisableHotspot(napi_env env, napi_callback_info info)
46 {
47     TRACE_FUNC_CALL;
48     WIFI_NAPI_ASSERT(env, wifiHotspotPtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_AP_CORE);
49     ErrCode ret = wifiHotspotPtr->DisableHotspot();
50     if (ret != WIFI_OPT_SUCCESS) {
51         WIFI_LOGE("Disable hotspot error: %{public}d", ret);
52     }
53     WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_AP_CORE);
54 }
55 
IsHotspotActive(napi_env env,napi_callback_info info)56 napi_value IsHotspotActive(napi_env env, napi_callback_info info)
57 {
58     WIFI_NAPI_ASSERT(env, wifiHotspotPtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_AP_CORE);
59     bool isActive = false;
60     ErrCode ret = wifiHotspotPtr->IsHotspotActive(isActive);
61     WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_AP_CORE);
62     napi_value result;
63     napi_get_boolean(env, isActive, &result);
64     return result;
65 }
66 
IsHotspotDualBandSupported(napi_env env,napi_callback_info info)67 napi_value IsHotspotDualBandSupported(napi_env env, napi_callback_info info)
68 {
69     WIFI_NAPI_ASSERT(env, wifiHotspotPtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_AP_CORE);
70     bool isSupported = false;
71     ErrCode ret = wifiHotspotPtr->IsHotspotDualBandSupported(isSupported);
72     WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_AP_CORE);
73     napi_value result;
74     napi_get_boolean(env, isSupported, &result);
75     return result;
76 }
77 
GetKeyMgmtFromJsSecurityType(int secType)78 static KeyMgmt GetKeyMgmtFromJsSecurityType(int secType)
79 {
80     std::map<SecTypeJs, KeyMgmt>::iterator iter = g_mapSecTypeToKeyMgmt.find(SecTypeJs(secType));
81     return iter == g_mapSecTypeToKeyMgmt.end() ? KeyMgmt::NONE : iter->second;
82 }
83 
GetJsSecurityTypeFromKeyMgmt(KeyMgmt keyMgmt)84 static int GetJsSecurityTypeFromKeyMgmt(KeyMgmt keyMgmt)
85 {
86     for (auto& each : g_mapSecTypeToKeyMgmt) {
87         if (each.second == keyMgmt) {
88             return static_cast<int>(each.first);
89         }
90     }
91     return static_cast<int>(SecTypeJs::SEC_TYPE_OPEN);
92 }
93 
IsSecTypeSupported(int secType)94 static bool IsSecTypeSupported(int secType)
95 {
96     return g_mapSecTypeToKeyMgmt.find(SecTypeJs(secType)) != g_mapSecTypeToKeyMgmt.end();
97 }
98 
GetHotspotconfigFromJs(const napi_env & env,const napi_value & object,HotspotConfig & config)99 static bool GetHotspotconfigFromJs(const napi_env& env, const napi_value& object, HotspotConfig& config)
100 {
101     std::string str = "";
102     int value = 0;
103     JsObjectToString(env, object, "ssid", NAPI_MAX_STR_LENT, str); // 33: ssid max length is 32 + '\0'
104     config.SetSsid(str);
105     str = "";
106     JsObjectToInt(env, object, "securityType", value);
107     if (!IsSecTypeSupported(value)) {
108         WIFI_LOGE("securityType is not supported: %{public}d", value);
109         return false;
110     }
111     config.SetSecurityType(GetKeyMgmtFromJsSecurityType(value));
112     value = 0;
113     JsObjectToInt(env, object, "band", value);
114     config.SetBand(BandType(value)); // 1: 2.4G, 2: 5G
115     if (config.GetBand() == BandType::BAND_5GHZ) {
116         config.SetChannel(AP_CHANNEL_5G_DEFAULT);
117     }
118     value = 0;
119     JsObjectToString(env, object, "preSharedKey", NAPI_MAX_STR_LENT, str); // 64: max length
120     config.SetPreSharedKey(str);
121     JsObjectToInt(env, object, "maxConn", value);
122     config.SetMaxConn(value);
123     return true;
124 }
125 
SetHotspotConfig(napi_env env,napi_callback_info info)126 napi_value SetHotspotConfig(napi_env env, napi_callback_info info)
127 {
128     TRACE_FUNC_CALL;
129     size_t argc = 1;
130     napi_value argv[1];
131     napi_value thisVar;
132     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
133 
134     napi_valuetype valueType;
135     napi_typeof(env, argv[0], &valueType);
136     WIFI_NAPI_ASSERT(env, valueType == napi_object, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_AP_CORE);
137     WIFI_NAPI_ASSERT(env, wifiHotspotPtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_AP_CORE);
138 
139     ErrCode ret = WIFI_OPT_FAILED;
140     HotspotConfig config;
141     if (GetHotspotconfigFromJs(env, argv[0], config)) {
142         ret = wifiHotspotPtr->SetHotspotConfig(config);
143         if (ret != WIFI_OPT_SUCCESS) {
144             WIFI_LOGE("Set hotspot config error: %{public}d", ret);
145         }
146     }
147     WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_AP_CORE);
148 }
149 
HotspotconfigToJs(const napi_env & env,HotspotConfig & cppConfig,napi_value & result)150 static void HotspotconfigToJs(const napi_env& env, HotspotConfig& cppConfig, napi_value& result)
151 {
152     SetValueUtf8String(env, "ssid", cppConfig.GetSsid().c_str(), result);
153     SetValueInt32(env, "securityType", GetJsSecurityTypeFromKeyMgmt(cppConfig.GetSecurityType()), result);
154     SetValueInt32(env, "band", static_cast<int>(cppConfig.GetBand()), result);
155     SetValueUtf8String(env, "preSharedKey", cppConfig.GetPreSharedKey().c_str(), result);
156     SetValueInt32(env, "maxConn", cppConfig.GetMaxConn(), result);
157 }
158 
GetHotspotConfig(napi_env env,napi_callback_info info)159 napi_value GetHotspotConfig(napi_env env, napi_callback_info info)
160 {
161     TRACE_FUNC_CALL;
162     WIFI_NAPI_ASSERT(env, wifiHotspotPtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_AP_CORE);
163     HotspotConfig config;
164     ErrCode ret = wifiHotspotPtr->GetHotspotConfig(config);
165     if (ret != WIFI_OPT_SUCCESS) {
166         WIFI_LOGE("Get hotspot config error: %{public}d", ret);
167     }
168     WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_AP_CORE);
169     napi_value result;
170     napi_create_object(env, &result);
171     HotspotconfigToJs(env, config, result);
172     return result;
173 }
174 
StationInfoToJsArray(const napi_env & env,const std::vector<StationInfo> & StationInfo,const int idx,napi_value & arrayResult)175 static void StationInfoToJsArray(const napi_env& env, const std::vector<StationInfo>& StationInfo,
176     const int idx, napi_value& arrayResult)
177 {
178     napi_value result;
179     napi_create_object(env, &result);
180 
181     SetValueUtf8String(env, "name", StationInfo[idx].deviceName.c_str(), result);
182     SetValueUtf8String(env, "macAddress", StationInfo[idx].bssid.c_str(), result);
183     SetValueUtf8String(env, "ipAddress", StationInfo[idx].ipAddr.c_str(), result);
184     napi_status status = napi_set_element(env, arrayResult, idx, result);
185     if (status != napi_ok) {
186         WIFI_LOGE("Set station element error: %{public}d", status);
187     }
188 }
189 
GetStations(napi_env env,napi_callback_info info)190 napi_value GetStations(napi_env env, napi_callback_info info)
191 {
192     TRACE_FUNC_CALL;
193     WIFI_NAPI_ASSERT(env, wifiHotspotPtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_AP_CORE);
194     std::vector<StationInfo> vecStationInfo;
195     ErrCode ret = wifiHotspotPtr->GetStationList(vecStationInfo);
196     if (ret != WIFI_OPT_SUCCESS) {
197         WIFI_LOGE("Get station list error: %{public}d", ret);
198     }
199     WIFI_LOGI("Get station list size: %{public}zu", vecStationInfo.size());
200     WIFI_NAPI_ASSERT(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_AP_CORE);
201 
202     napi_value arrayResult;
203     napi_create_array_with_length(env, vecStationInfo.size(), &arrayResult);
204     for (size_t i = 0; i != vecStationInfo.size(); ++i) {
205         StationInfoToJsArray(env, vecStationInfo, i, arrayResult);
206     }
207     return arrayResult;
208 }
209 
AddBlockList(napi_env env,napi_callback_info info)210 napi_value AddBlockList(napi_env env, napi_callback_info info)
211 {
212     TRACE_FUNC_CALL;
213     size_t argc = 1;
214     napi_value argv[argc];
215     napi_value thisVar;
216     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
217 
218     napi_valuetype valueType;
219     napi_typeof(env, argv[0], &valueType);
220     WIFI_NAPI_ASSERT(env, valueType == napi_string, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_AP_CORE);
221     WIFI_NAPI_ASSERT(env, wifiHotspotPtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_AP_CORE);
222 
223     StationInfo stationInfo;
224     char bssid[WIFI_BSSID_LENGTH] = {0};
225     size_t len = 0;
226     napi_get_value_string_utf8(env, argv[0], bssid, sizeof(bssid), &len);
227     stationInfo.bssid = bssid;
228     ErrCode ret = wifiHotspotPtr->AddBlockList(stationInfo);
229     if (ret != WIFI_OPT_SUCCESS) {
230         WIFI_LOGE("Add block list fail: %{public}d", ret);
231     }
232     WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_AP_CORE);
233 }
234 
DelBlockList(napi_env env,napi_callback_info info)235 napi_value DelBlockList(napi_env env, napi_callback_info info)
236 {
237     TRACE_FUNC_CALL;
238     size_t argc = 1;
239     napi_value argv[argc];
240     napi_value thisVar;
241     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
242     napi_valuetype valueType;
243     napi_typeof(env, argv[0], &valueType);
244     WIFI_NAPI_ASSERT(env, valueType == napi_string, WIFI_OPT_INVALID_PARAM, SYSCAP_WIFI_AP_CORE);
245     WIFI_NAPI_ASSERT(env, wifiHotspotPtr != nullptr, WIFI_OPT_FAILED, SYSCAP_WIFI_AP_CORE);
246 
247     StationInfo stationInfo;
248     char bssid[WIFI_BSSID_LENGTH] = {0};
249     size_t len = 0;
250     napi_get_value_string_utf8(env, argv[0], bssid, sizeof(bssid), &len);
251     stationInfo.bssid = bssid;
252     ErrCode ret = wifiHotspotPtr->DelBlockList(stationInfo);
253     if (ret != WIFI_OPT_SUCCESS) {
254         WIFI_LOGE("Del block list fail: %{public}d", ret);
255     }
256     WIFI_NAPI_RETURN(env, ret == WIFI_OPT_SUCCESS, ret, SYSCAP_WIFI_AP_CORE);
257 }
258 }  // namespace Wifi
259 }  // namespace OHOS
260