• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 "bluetooth_errorcode.h"
17 #include "bluetooth_log.h"
18 #include "bluetooth_pan.h"
19 #include "bluetooth_utils.h"
20 #include "napi_bluetooth_error.h"
21 #include "napi_bluetooth_pan.h"
22 #include "napi_bluetooth_pan_observer.h"
23 #include "napi_bluetooth_profile.h"
24 #include "napi_bluetooth_utils.h"
25 
26 namespace OHOS {
27 namespace Bluetooth {
28 using namespace std;
29 NapiBluetoothPanObserver NapiBluetoothPan::observer_;
30 thread_local napi_ref NapiBluetoothPan::consRef_ = nullptr;
31 
CreatePanProfile(napi_env env,napi_callback_info info)32 napi_value NapiBluetoothPan::CreatePanProfile(napi_env env, napi_callback_info info)
33 {
34     HILOGI("enter");
35     napi_value napiProfile;
36     napi_value constructor = nullptr;
37     napi_get_reference_value(env, consRef_, &constructor);
38     napi_new_instance(env, constructor, 0, nullptr, &napiProfile);
39     NapiProfile::SetProfile(env, ProfileId::PROFILE_PAN_NETWORK, napiProfile);
40     Pan *profile = Pan::GetProfile();
41     profile->RegisterObserver(&NapiBluetoothPan::observer_);
42     HILOGI("finished");
43 
44     return napiProfile;
45 }
46 
DefinePanJSClass(napi_env env,napi_value exports)47 void NapiBluetoothPan::DefinePanJSClass(napi_env env, napi_value exports)
48 {
49     napi_value constructor;
50     napi_property_descriptor properties [] = {
51         DECLARE_NAPI_FUNCTION("on", NapiBluetoothPan::On),
52         DECLARE_NAPI_FUNCTION("off", NapiBluetoothPan::Off),
53         DECLARE_NAPI_FUNCTION("disconnect", NapiBluetoothPan::Disconnect),
54         DECLARE_NAPI_FUNCTION("setTethering", NapiBluetoothPan::SetTethering),
55         DECLARE_NAPI_FUNCTION("isTetheringOn", NapiBluetoothPan::IsTetheringOn),
56 #ifdef BLUETOOTH_API_SINCE_10
57         DECLARE_NAPI_FUNCTION("getConnectedDevices", NapiBluetoothPan::GetConnectedDevices),
58         DECLARE_NAPI_FUNCTION("getConnectionState", NapiBluetoothPan::GetConnectionState),
59         DECLARE_NAPI_FUNCTION("setConnectionStrategy", NapiBluetoothPan::SetConnectionStrategy),
60         DECLARE_NAPI_FUNCTION("getConnectionStrategy", NapiBluetoothPan::GetConnectionStrategy),
61 #else
62         DECLARE_NAPI_FUNCTION("getConnectionDevices", NapiBluetoothPan::GetConnectionDevices),
63         DECLARE_NAPI_FUNCTION("getDeviceState", NapiBluetoothPan::GetDeviceState),
64 #endif
65     };
66 
67     napi_define_class(env, "NapiBluetoothPan", NAPI_AUTO_LENGTH, NapiBluetoothPan::PanConstructor, nullptr,
68         sizeof(properties) / sizeof(properties[0]), properties, &constructor);
69 
70 #ifdef BLUETOOTH_API_SINCE_10
71     DefineCreateProfile(env, exports);
72     napi_create_reference(env, constructor, 1, &consRef_);
73 #else
74     napi_define_class(env, "NapiBluetoothPan", NAPI_AUTO_LENGTH, PanConstructor, nullptr,
75         sizeof(properties) / sizeof(properties[0]), properties, &constructor);
76     napi_value napiProfile;
77     napi_new_instance(env, constructor, 0, nullptr, &napiProfile);
78     NapiProfile::SetProfile(env, ProfileId::PROFILE_PAN_NETWORK, napiProfile);
79     Pan *profile = Pan::GetProfile();
80     profile->RegisterObserver(&NapiBluetoothPan::observer_);
81 #endif
82 }
83 
DefineCreateProfile(napi_env env,napi_value exports)84 napi_value NapiBluetoothPan::DefineCreateProfile(napi_env env, napi_value exports)
85 {
86     napi_property_descriptor properties [] = {
87         DECLARE_NAPI_FUNCTION("createPanProfile", NapiBluetoothPan::CreatePanProfile),
88     };
89     napi_define_properties(env, exports, sizeof(properties) / sizeof(properties[0]), properties);
90     return exports;
91 }
92 
SetConnectionStrategy(napi_env env,napi_callback_info info)93 napi_value NapiBluetoothPan::SetConnectionStrategy(napi_env env, napi_callback_info info)
94 {
95     HILOGI("enter");
96     NAPI_BT_ASSERT_RETURN_UNDEF(env, false, BT_ERR_API_NOT_SUPPORT);
97     return NapiGetUndefinedRet(env);
98 }
99 
GetConnectionStrategy(napi_env env,napi_callback_info info)100 napi_value NapiBluetoothPan::GetConnectionStrategy(napi_env env, napi_callback_info info)
101 {
102     HILOGI("enter");
103     NAPI_BT_ASSERT_RETURN_UNDEF(env, false, BT_ERR_API_NOT_SUPPORT);
104     return NapiGetUndefinedRet(env);
105 }
106 
PanConstructor(napi_env env,napi_callback_info info)107 napi_value NapiBluetoothPan::PanConstructor(napi_env env, napi_callback_info info)
108 {
109     napi_value thisVar = nullptr;
110     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
111     return thisVar;
112 }
113 
On(napi_env env,napi_callback_info info)114 napi_value NapiBluetoothPan::On(napi_env env, napi_callback_info info)
115 {
116     HILOGI("enter");
117     size_t expectedArgsCount = ARGS_SIZE_TWO;
118     size_t argc = expectedArgsCount;
119     napi_value argv[ARGS_SIZE_TWO] = {0};
120     napi_value thisVar = nullptr;
121 
122     napi_value ret = nullptr;
123     napi_get_undefined(env, &ret);
124 
125     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
126     if (argc != expectedArgsCount) {
127         HILOGE("Requires 2 argument.");
128         return ret;
129     }
130     string type;
131     if (!ParseString(env, type, argv[PARAM0])) {
132         HILOGE("string expected.");
133         return ret;
134     }
135     std::shared_ptr<BluetoothCallbackInfo> callbackInfo = std::make_shared<BluetoothCallbackInfo>();
136     callbackInfo->env_ = env;
137 
138     napi_valuetype valueType = napi_undefined;
139     napi_typeof(env, argv[PARAM1], &valueType);
140     if (valueType != napi_function) {
141         HILOGE("Wrong argument type. Function expected.");
142         return ret;
143     }
144     napi_create_reference(env, argv[PARAM1], 1, &callbackInfo->callback_);
145     observer_.callbackInfos_[type] = callbackInfo;
146 
147     HILOGI("%{public}s is registered", type.c_str());
148     return ret;
149 }
150 
Off(napi_env env,napi_callback_info info)151 napi_value NapiBluetoothPan::Off(napi_env env, napi_callback_info info)
152 {
153     HILOGI("enter");
154     size_t expectedArgsCount = ARGS_SIZE_ONE;
155     size_t argc = expectedArgsCount;
156     napi_value argv[ARGS_SIZE_ONE] = {0};
157     napi_value thisVar = nullptr;
158 
159     napi_value ret = nullptr;
160     napi_get_undefined(env, &ret);
161 
162     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
163     if (argc != expectedArgsCount) {
164         HILOGE("Requires 1 argument.");
165         return ret;
166     }
167     string type;
168     if (!ParseString(env, type, argv[PARAM0])) {
169         HILOGE("string expected.");
170         return ret;
171     }
172     if (observer_.callbackInfos_[type] != nullptr) {
173     std::shared_ptr<BluetoothCallbackInfo> callbackInfo = observer_.callbackInfos_[type];
174     napi_delete_reference(env, callbackInfo->callback_);
175     }
176     observer_.callbackInfos_[type] = nullptr;
177 
178     HILOGI("%{public}s is unregistered", type.c_str());
179 
180     return ret;
181 }
182 
GetConnectionDevices(napi_env env,napi_callback_info info)183 napi_value NapiBluetoothPan::GetConnectionDevices(napi_env env, napi_callback_info info)
184 {
185     HILOGI("enter");
186     napi_value ret = nullptr;
187     if (napi_create_array(env, &ret) != napi_ok) {
188         HILOGE("napi_create_array failed.");
189     }
190 
191     napi_status checkRet = CheckEmptyParam(env, info);
192     NAPI_BT_ASSERT_RETURN(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM, ret);
193 
194     Pan *profile = Pan::GetProfile();
195     vector<int> states = { static_cast<int>(BTConnectState::CONNECTED) };
196     vector<BluetoothRemoteDevice> devices;
197     int errorCode = profile->GetDevicesByStates(states, devices);
198     HILOGI("errorCode:%{public}s, devices size:%{public}zu", GetErrorCode(errorCode).c_str(), devices.size());
199     NAPI_BT_ASSERT_RETURN(env, errorCode == BT_NO_ERROR, errorCode, ret);
200 
201     vector<string> deviceVector;
202     for (auto &device : devices) {
203         deviceVector.push_back(device.GetDeviceAddr());
204     }
205     ConvertStringVectorToJS(env, ret, deviceVector);
206     return ret;
207 }
208 
GetDeviceState(napi_env env,napi_callback_info info)209 napi_value NapiBluetoothPan::GetDeviceState(napi_env env, napi_callback_info info)
210 {
211     HILOGI("enter");
212     napi_value result = nullptr;
213     int32_t profileState = ProfileConnectionState::STATE_DISCONNECTED;
214     if (napi_create_int32(env, profileState, &result) != napi_ok) {
215         HILOGE("napi_create_int32 failed.");
216     }
217 
218     std::string remoteAddr {};
219     bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
220     NAPI_BT_ASSERT_RETURN(env, checkRet, BT_ERR_INVALID_PARAM, result);
221 
222     Pan *profile = Pan::GetProfile();
223     BluetoothRemoteDevice device(remoteAddr, BT_TRANSPORT_BREDR);
224     int32_t state = static_cast<int32_t>(BTConnectState::DISCONNECTED);
225     int32_t errorCode = profile->GetDeviceState(device, state);
226     HILOGI("errorCode:%{public}s", GetErrorCode(errorCode).c_str());
227     NAPI_BT_ASSERT_RETURN(env, errorCode == BT_NO_ERROR, errorCode, result);
228 
229     profileState = GetProfileConnectionState(state);
230     if (napi_create_int32(env, profileState, &result) != napi_ok) {
231         HILOGE("napi_create_int32 failed.");
232     }
233     HILOGI("profileState: %{public}d", profileState);
234     return result;
235 }
236 
GetConnectedDevices(napi_env env,napi_callback_info info)237 napi_value NapiBluetoothPan::GetConnectedDevices(napi_env env, napi_callback_info info)
238 {
239     HILOGI("enter");
240     napi_value ret = nullptr;
241     if (napi_create_array(env, &ret) != napi_ok) {
242         HILOGE("napi_create_array failed.");
243     }
244 
245     napi_status checkRet = CheckEmptyParam(env, info);
246     NAPI_BT_ASSERT_RETURN(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM, ret);
247 
248     Pan *profile = Pan::GetProfile();
249     vector<int> states = { static_cast<int>(BTConnectState::CONNECTED) };
250     vector<BluetoothRemoteDevice> devices;
251     int errorCode = profile->GetDevicesByStates(states, devices);
252     HILOGI("errorCode:%{public}s, devices size:%{public}zu", GetErrorCode(errorCode).c_str(), devices.size());
253     NAPI_BT_ASSERT_RETURN(env, errorCode == BT_NO_ERROR, errorCode, ret);
254 
255     vector<string> deviceVector;
256     for (auto &device : devices) {
257         deviceVector.push_back(device.GetDeviceAddr());
258     }
259     ConvertStringVectorToJS(env, ret, deviceVector);
260     return ret;
261 }
262 
GetConnectionState(napi_env env,napi_callback_info info)263 napi_value NapiBluetoothPan::GetConnectionState(napi_env env, napi_callback_info info)
264 {
265     HILOGI("enter");
266     napi_value result = nullptr;
267     int32_t profileState = ProfileConnectionState::STATE_DISCONNECTED;
268     if (napi_create_int32(env, profileState, &result) != napi_ok) {
269         HILOGE("napi_create_int32 failed.");
270     }
271 
272     std::string remoteAddr {};
273     bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
274     NAPI_BT_ASSERT_RETURN(env, checkRet, BT_ERR_INVALID_PARAM, result);
275 
276     Pan *profile = Pan::GetProfile();
277     BluetoothRemoteDevice device(remoteAddr, BT_TRANSPORT_BREDR);
278     int32_t state = static_cast<int32_t>(BTConnectState::DISCONNECTED);
279     int32_t errorCode = profile->GetDeviceState(device, state);
280     HILOGI("errorCode:%{public}s", GetErrorCode(errorCode).c_str());
281     NAPI_BT_ASSERT_RETURN(env, errorCode == BT_NO_ERROR, errorCode, result);
282 
283     profileState = GetProfileConnectionState(state);
284     if (napi_create_int32(env, profileState, &result) != napi_ok) {
285         HILOGE("napi_create_int32 failed.");
286     }
287     HILOGI("profileState: %{public}d", profileState);
288     return result;
289 }
290 
Disconnect(napi_env env,napi_callback_info info)291 napi_value NapiBluetoothPan::Disconnect(napi_env env, napi_callback_info info)
292 {
293     HILOGI("enter");
294     std::string remoteAddr {};
295     bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
296     NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
297 
298     Pan *profile = Pan::GetProfile();
299     BluetoothRemoteDevice device(remoteAddr, BT_TRANSPORT_BREDR);
300     int32_t errorCode = profile->Disconnect(device);
301     HILOGI("errorCode:%{public}s", GetErrorCode(errorCode).c_str());
302     NAPI_BT_ASSERT_RETURN_FALSE(env, errorCode == BT_NO_ERROR, errorCode);
303 
304     return NapiGetBooleanTrue(env);
305 }
306 
CheckSetTetheringParam(napi_env env,napi_callback_info info,bool & out)307 static bool CheckSetTetheringParam(napi_env env, napi_callback_info info, bool &out)
308 {
309     size_t argc = ARGS_SIZE_ONE;
310     napi_value argv[ARGS_SIZE_ONE] = {nullptr};
311     NAPI_BT_RETURN_IF(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr) != napi_ok, "call failed.", false);
312     NAPI_BT_RETURN_IF(argc != ARGS_SIZE_ONE, "Requires 1 argument.", false);
313     NAPI_BT_RETURN_IF(!ParseBool(env, out, argv[PARAM0]), "Bool expected.", false);
314     return true;
315 }
316 
SetTethering(napi_env env,napi_callback_info info)317 napi_value NapiBluetoothPan::SetTethering(napi_env env, napi_callback_info info)
318 {
319     HILOGI("enter");
320     bool value = false;
321     bool checkRet = CheckSetTetheringParam(env, info, value);
322     NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
323 
324     Pan *profile = Pan::GetProfile();
325     int32_t errorCode = profile->SetTethering(value);
326     HILOGI("errorCode:%{public}s", GetErrorCode(errorCode).c_str());
327     NAPI_BT_ASSERT_RETURN_UNDEF(env, errorCode == BT_NO_ERROR, errorCode);
328 
329     return NapiGetUndefinedRet(env);
330 }
331 
IsTetheringOn(napi_env env,napi_callback_info info)332 napi_value NapiBluetoothPan::IsTetheringOn(napi_env env, napi_callback_info info)
333 {
334     HILOGI("enter");
335     napi_status checkRet = CheckEmptyParam(env, info);
336     NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM);
337 
338     Pan *profile = Pan::GetProfile();
339     bool result = false;
340     int32_t errorCode = profile->IsTetheringOn(result);
341     HILOGI("errorCode:%{public}s", GetErrorCode(errorCode).c_str());
342     NAPI_BT_ASSERT_RETURN_FALSE(env, errorCode == BT_NO_ERROR, errorCode);
343 
344     HILOGI("IsTetheringOn: %{public}d", result);
345     return NapiGetBooleanRet(env, result);
346 }
347 }  // namespace Bluetooth
348 }  // namespace OHOS