1 /*
2 * Copyright (C) 2023 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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_napi_connection"
17 #endif
18
19 #include "napi_bluetooth_connection.h"
20
21 #include <set>
22
23 #include "napi_bluetooth_connection_observer.h"
24 #include "napi_bluetooth_remote_device_observer.h"
25 #include "bluetooth_log.h"
26 #include "bluetooth_errorcode.h"
27 #include "napi_bluetooth_error.h"
28 #include "napi_async_work.h"
29 #include "napi_bluetooth_utils.h"
30 #include "parser/napi_parser_utils.h"
31 #include "hitrace_meter.h"
32
33 namespace OHOS {
34 namespace Bluetooth {
35 std::shared_ptr<NapiBluetoothConnectionObserver> g_connectionObserver =
36 std::make_shared<NapiBluetoothConnectionObserver>();
37 std::shared_ptr<NapiBluetoothRemoteDeviceObserver> g_remoteDeviceObserver =
38 std::make_shared<NapiBluetoothRemoteDeviceObserver>();
39 std::mutex deviceMutex;
40
41 #ifdef BLUETOOTH_API_SINCE_10
DefineConnectionFunctions(napi_env env,napi_value exports)42 napi_value DefineConnectionFunctions(napi_env env, napi_value exports)
43 {
44 RegisterObserverToHost();
45 ConnectionPropertyValueInit(env, exports);
46 napi_property_descriptor desc[] = {
47 DECLARE_NAPI_FUNCTION("getBtConnectionState", GetBtConnectionState),
48 DECLARE_NAPI_FUNCTION("pairDevice", PairDeviceAsync),
49 DECLARE_NAPI_FUNCTION("cancelPairedDevice", CancelPairedDeviceAsync),
50 DECLARE_NAPI_FUNCTION("getProfileConnectionState", GetProfileConnectionStateEx),
51 DECLARE_NAPI_FUNCTION("getRemoteDeviceName", GetRemoteDeviceName),
52 DECLARE_NAPI_FUNCTION("getRemoteDeviceClass", GetRemoteDeviceClass),
53 DECLARE_NAPI_FUNCTION("getLocalName", GetLocalName),
54 DECLARE_NAPI_FUNCTION("getPairedDevices", GetPairedDevices),
55 DECLARE_NAPI_FUNCTION("getProfileConnState", GetProfileConnectionState),
56 DECLARE_NAPI_FUNCTION("setDevicePairingConfirmation", SetDevicePairingConfirmation),
57 DECLARE_NAPI_FUNCTION("setLocalName", SetLocalName),
58 DECLARE_NAPI_FUNCTION("setBluetoothScanMode", SetBluetoothScanMode),
59 DECLARE_NAPI_FUNCTION("getBluetoothScanMode", GetBluetoothScanMode),
60 DECLARE_NAPI_FUNCTION("startBluetoothDiscovery", StartBluetoothDiscovery),
61 DECLARE_NAPI_FUNCTION("stopBluetoothDiscovery", StopBluetoothDiscovery),
62 DECLARE_NAPI_FUNCTION("setDevicePinCode", SetDevicePinCode),
63 DECLARE_NAPI_FUNCTION("cancelPairingDevice", CancelPairingDevice),
64 DECLARE_NAPI_FUNCTION("pairCredibleDevice", PairCredibleDevice),
65 DECLARE_NAPI_FUNCTION("getLocalProfileUuids", GetLocalProfileUuids),
66 DECLARE_NAPI_FUNCTION("getRemoteProfileUuids", GetRemoteProfileUuids),
67 DECLARE_NAPI_FUNCTION("on", RegisterConnectionObserver),
68 DECLARE_NAPI_FUNCTION("off", DeRegisterConnectionObserver),
69 DECLARE_NAPI_FUNCTION("isBluetoothDiscovering", IsBluetoothDiscovering),
70 DECLARE_NAPI_FUNCTION("getPairState", GetPairState),
71 DECLARE_NAPI_FUNCTION("connectAllowedProfiles", ConnectAllowedProfiles),
72 DECLARE_NAPI_FUNCTION("disconnectAllowedProfiles", DisconnectAllowedProfiles),
73 DECLARE_NAPI_FUNCTION("getRemoteProductId", GetRemoteProductId),
74 DECLARE_NAPI_FUNCTION("setRemoteDeviceName", SetRemoteDeviceName),
75 DECLARE_NAPI_FUNCTION("setRemoteDeviceType", SetRemoteDeviceType),
76 DECLARE_NAPI_FUNCTION("getRemoteDeviceType", GetRemoteDeviceType),
77 DECLARE_NAPI_FUNCTION("getRemoteDeviceBatteryInfo", GetRemoteDeviceBatteryInfo),
78 DECLARE_NAPI_FUNCTION("getLastConnectionTime", GetRemoteDeviceConnectionTime),
79 DECLARE_NAPI_FUNCTION("updateCloudBluetoothDevice", UpdateCloudBluetoothDevice),
80 };
81
82 HITRACE_METER_NAME(HITRACE_TAG_OHOS, "connection:napi_define_properties");
83 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
84 return exports;
85 }
86 #else
DefineConnectionFunctions(napi_env env,napi_value exports)87 napi_value DefineConnectionFunctions(napi_env env, napi_value exports)
88 {
89 RegisterObserverToHost();
90 ConnectionPropertyValueInit(env, exports);
91 napi_property_descriptor desc[] = {
92 DECLARE_NAPI_FUNCTION("getBtConnectionState", GetBtConnectionState),
93 DECLARE_NAPI_FUNCTION("pairDevice", PairDevice),
94 DECLARE_NAPI_FUNCTION("cancelPairedDevice", CancelPairedDevice),
95 DECLARE_NAPI_FUNCTION("getProfileConnectionState", GetProfileConnectionState),
96 DECLARE_NAPI_FUNCTION("getRemoteDeviceName", GetRemoteDeviceName),
97 DECLARE_NAPI_FUNCTION("getRemoteDeviceClass", GetRemoteDeviceClass),
98 DECLARE_NAPI_FUNCTION("getLocalName", GetLocalName),
99 DECLARE_NAPI_FUNCTION("getPairedDevices", GetPairedDevices),
100 DECLARE_NAPI_FUNCTION("getProfileConnState", GetProfileConnectionState),
101 DECLARE_NAPI_FUNCTION("setDevicePairingConfirmation", SetDevicePairingConfirmation),
102 DECLARE_NAPI_FUNCTION("setLocalName", SetLocalName),
103 DECLARE_NAPI_FUNCTION("setBluetoothScanMode", SetBluetoothScanMode),
104 DECLARE_NAPI_FUNCTION("getBluetoothScanMode", GetBluetoothScanMode),
105 DECLARE_NAPI_FUNCTION("startBluetoothDiscovery", StartBluetoothDiscovery),
106 DECLARE_NAPI_FUNCTION("stopBluetoothDiscovery", StopBluetoothDiscovery),
107 DECLARE_NAPI_FUNCTION("setRemoteDeviceName", SetRemoteDeviceName),
108 DECLARE_NAPI_FUNCTION("setRemoteDeviceType", SetRemoteDeviceType),
109 DECLARE_NAPI_FUNCTION("getRemoteDeviceType", GetRemoteDeviceType),
110 DECLARE_NAPI_FUNCTION("getRemoteDeviceBatteryInfo", GetRemoteDeviceBatteryInfo),
111 DECLARE_NAPI_FUNCTION("getLastConnectionTime", GetRemoteDeviceConnectionTime),
112 DECLARE_NAPI_FUNCTION("updateCloudBluetoothDevice", UpdateCloudBluetoothDevice),
113 };
114
115 HITRACE_METER_NAME(HITRACE_TAG_OHOS, "connection:napi_define_properties");
116 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
117 return exports;
118 }
119 #endif
120
121 using NapiBluetoothOnOffFunc = std::function<napi_status(napi_env env, napi_callback_info info)>;
122
NapiConnectionOnOffExecute(napi_env env,napi_callback_info info,NapiBluetoothOnOffFunc connectionObserverFunc,NapiBluetoothOnOffFunc remoteDeviceObserverFunc)123 static napi_status NapiConnectionOnOffExecute(napi_env env, napi_callback_info info,
124 NapiBluetoothOnOffFunc connectionObserverFunc, NapiBluetoothOnOffFunc remoteDeviceObserverFunc)
125 {
126 std::string type = "";
127 NAPI_BT_CALL_RETURN(NapiGetOnOffCallbackName(env, info, type));
128
129 napi_status status = napi_ok;
130 if (type == REGISTER_DEVICE_FIND_TYPE ||
131 type == REGISTER_DISCOVERY_RESULT_TYPE ||
132 type == REGISTER_PIN_REQUEST_TYPE) {
133 status = connectionObserverFunc(env, info);
134 } else if (type == REGISTER_BOND_STATE_TYPE || type == REGISTER_BATTERY_CHANGE_TYPE) {
135 status = remoteDeviceObserverFunc(env, info);
136 } else {
137 HILOGE("Unsupported callback: %{public}s", type.c_str());
138 status = napi_invalid_arg;
139 }
140 return status;
141 }
142
RegisterConnectionObserver(napi_env env,napi_callback_info info)143 napi_value RegisterConnectionObserver(napi_env env, napi_callback_info info)
144 {
145 auto connectionObserverFunc = [](napi_env env, napi_callback_info info) {
146 return g_connectionObserver->eventSubscribe_.Register(env, info);
147 };
148 auto remoteDeviceObserverFunc = [](napi_env env, napi_callback_info info) {
149 return g_remoteDeviceObserver->eventSubscribe_.Register(env, info);
150 };
151
152 auto status = NapiConnectionOnOffExecute(env, info, connectionObserverFunc, remoteDeviceObserverFunc);
153 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
154 return NapiGetUndefinedRet(env);
155 }
156
DeRegisterConnectionObserver(napi_env env,napi_callback_info info)157 napi_value DeRegisterConnectionObserver(napi_env env, napi_callback_info info)
158 {
159 auto connectionObserverFunc = [](napi_env env, napi_callback_info info) {
160 return g_connectionObserver->eventSubscribe_.Deregister(env, info);
161 };
162 auto remoteDeviceObserverFunc = [](napi_env env, napi_callback_info info) {
163 return g_remoteDeviceObserver->eventSubscribe_.Deregister(env, info);
164 };
165
166 auto status = NapiConnectionOnOffExecute(env, info, connectionObserverFunc, remoteDeviceObserverFunc);
167 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
168 return NapiGetUndefinedRet(env);
169 }
170
GetBtConnectionState(napi_env env,napi_callback_info info)171 napi_value GetBtConnectionState(napi_env env, napi_callback_info info)
172 {
173 HILOGD("enter");
174 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
175 int state = static_cast<int>(BTConnectState::DISCONNECTED);
176 int32_t err = host->GetBtConnectionState(state);
177 HILOGD("start state %{publDc}d", state);
178 napi_value result = nullptr;
179 napi_create_int32(env, GetProfileConnectionState(state), &result);
180 NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, result);
181 return result;
182 }
183
PairDevice(napi_env env,napi_callback_info info)184 napi_value PairDevice(napi_env env, napi_callback_info info)
185 {
186 HILOGD("enter");
187 std::string remoteAddr = INVALID_MAC_ADDRESS;
188 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
189 NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
190
191 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
192 int32_t ret = remoteDevice.StartPair();
193 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_NO_ERROR, ret);
194 return NapiGetBooleanTrue(env);
195 }
196
CancelPairedDevice(napi_env env,napi_callback_info info)197 napi_value CancelPairedDevice(napi_env env, napi_callback_info info)
198 {
199 HILOGD("enter");
200 std::string remoteAddr{};
201 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
202 NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
203
204 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
205 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
206 int32_t ret = host->RemovePair(remoteDevice);
207 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_NO_ERROR, ret);
208
209 return NapiGetBooleanTrue(env);
210 }
211
CheckGetRemoteDeviceNameParam(napi_env env,napi_callback_info info,std::string & addr,bool & alias)212 bool CheckGetRemoteDeviceNameParam(napi_env env, napi_callback_info info, std::string &addr, bool &alias)
213 {
214 size_t argc = ARGS_SIZE_TWO;
215 napi_value argv[ARGS_SIZE_TWO] = {nullptr};
216 NAPI_BT_RETURN_IF(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr) != napi_ok, "call failed.", false);
217 NAPI_BT_RETURN_IF(NapiParseBdAddr(env, argv[PARAM0], addr) != napi_ok, "NapiParseBdAddr failed", false);
218 if (argc > ARGS_SIZE_ONE) {
219 NAPI_BT_RETURN_IF(!ParseBool(env, alias, argv[PARAM1]), "ParseBool failed", false);
220 }
221 return true;
222 }
223
GetRemoteDeviceName(napi_env env,napi_callback_info info)224 napi_value GetRemoteDeviceName(napi_env env, napi_callback_info info)
225 {
226 HILOGD("start");
227 std::string remoteAddr = INVALID_MAC_ADDRESS;
228 std::string name = INVALID_NAME;
229 napi_value result = nullptr;
230 bool alias = true;
231 bool checkRet = CheckGetRemoteDeviceNameParam(env, info, remoteAddr, alias);
232 napi_create_string_utf8(env, name.c_str(), name.size(), &result);
233 NAPI_BT_ASSERT_RETURN(env, checkRet == true, BT_ERR_INVALID_PARAM, result);
234
235 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
236 int32_t err = remoteDevice.GetDeviceName(name, alias);
237 napi_create_string_utf8(env, name.c_str(), name.size(), &result);
238 NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, result);
239 return result;
240 }
241
GetRemoteDeviceClass(napi_env env,napi_callback_info info)242 napi_value GetRemoteDeviceClass(napi_env env, napi_callback_info info)
243 {
244 HILOGD("start");
245 std::string remoteAddr = INVALID_MAC_ADDRESS;
246 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
247 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
248
249 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
250 int tmpCod = MajorClass::MAJOR_UNCATEGORIZED;
251 int tmpMajorClass = MajorClass::MAJOR_UNCATEGORIZED;
252 int tmpMajorMinorClass = MajorClass::MAJOR_UNCATEGORIZED;
253 int32_t err = remoteDevice.GetDeviceProductType(tmpCod, tmpMajorClass, tmpMajorMinorClass);
254 napi_value result = nullptr;
255 napi_create_object(env, &result);
256 napi_value majorClass = 0;
257 napi_create_int32(env, tmpMajorClass, &majorClass);
258 napi_set_named_property(env, result, "majorClass", majorClass);
259 napi_value majorMinorClass = 0;
260 napi_create_int32(env, tmpMajorMinorClass, &majorMinorClass);
261 napi_set_named_property(env, result, "majorMinorClass", majorMinorClass);
262 napi_value cod = 0;
263 napi_create_int32(env, tmpCod, &cod);
264 napi_set_named_property(env, result, "classOfDevice", cod);
265 NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, result);
266 return result;
267 }
268
GetLocalName(napi_env env,napi_callback_info info)269 napi_value GetLocalName(napi_env env, napi_callback_info info)
270 {
271 napi_value result = nullptr;
272 HILOGD("enter");
273 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
274 std::string localName = INVALID_NAME;
275 int32_t err = host->GetLocalName(localName);
276 napi_create_string_utf8(env, localName.c_str(), localName.size(), &result);
277 NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, result);
278 HILOGI("end");
279 return result;
280 }
281
GetPairedDevices(napi_env env,napi_callback_info info)282 napi_value GetPairedDevices(napi_env env, napi_callback_info info)
283 {
284 HILOGD("enter");
285 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
286 std::vector<BluetoothRemoteDevice> remoteDeviceLists;
287 int32_t ret = host->GetPairedDevices(BT_TRANSPORT_BREDR, remoteDeviceLists);
288 napi_value result = nullptr;
289 int count = 0;
290 napi_create_array(env, &result);
291 for (auto vec : remoteDeviceLists) {
292 napi_value remoteDeviceResult;
293 napi_create_string_utf8(env, vec.GetDeviceAddr().c_str(), vec.GetDeviceAddr().size(), &remoteDeviceResult);
294 napi_set_element(env, result, count, remoteDeviceResult);
295 count++;
296 }
297 NAPI_BT_ASSERT_RETURN(env, ret == BT_NO_ERROR, ret, result);
298 HILOGI("end");
299 return result;
300 }
301
GetProfileConnectionState(napi_env env,napi_callback_info info)302 napi_value GetProfileConnectionState(napi_env env, napi_callback_info info)
303 {
304 HILOGD("enter");
305 int profileId = 0;
306 bool checkRet = CheckProfileIdParam(env, info, profileId);
307 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
308
309 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
310 int state = static_cast<int>(BTConnectState::DISCONNECTED);
311 int32_t err = host->GetBtProfileConnState(GetProfileId(profileId), state);
312 int status = GetProfileConnectionState(state);
313 napi_value ret = nullptr;
314 napi_create_int32(env, status, &ret);
315 NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, ret);
316 HILOGD("status: %{public}d", status);
317 return ret;
318 }
319
GetProfileConnectionStateEx(napi_env env,napi_callback_info info)320 napi_value GetProfileConnectionStateEx(napi_env env, napi_callback_info info)
321 {
322 HILOGD("enter");
323 int profileId = 0;
324 size_t argSize = ARGS_SIZE_ONE;
325 bool checkRet = CheckProfileIdParamEx(env, info, profileId, argSize);
326 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
327
328 napi_value ret = nullptr;
329 if (argSize == 0) {
330 ret = GetBtConnectionState(env, info);
331 } else {
332 ret = GetProfileConnectionState(env, info);
333 }
334 return ret;
335 }
336
SetDevicePairingConfirmation(napi_env env,napi_callback_info info)337 napi_value SetDevicePairingConfirmation(napi_env env, napi_callback_info info)
338 {
339 HILOGD("enter");
340 std::string remoteAddr{};
341 bool accept = false;
342 bool checkRet = CheckSetDevicePairingConfirmationParam(env, info, remoteAddr, accept);
343 NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
344
345 HILOGI("SetDevicePairingConfirmation::accept = %{public}d", accept);
346 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
347 int32_t ret = BT_NO_ERROR;
348 if (accept) {
349 ret = remoteDevice.SetDevicePairingConfirmation(accept);
350 } else {
351 ret = remoteDevice.CancelPairing();
352 }
353 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_NO_ERROR, ret);
354 return NapiGetBooleanTrue(env);
355 }
356
SetLocalName(napi_env env,napi_callback_info info)357 napi_value SetLocalName(napi_env env, napi_callback_info info)
358 {
359 HILOGD("enter");
360 std::string localName = INVALID_NAME;
361 bool checkRet = CheckLocalNameParam(env, info, localName);
362 NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
363
364 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
365 int32_t ret = host->SetLocalName(localName);
366 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_NO_ERROR, ret);
367 return NapiGetBooleanTrue(env);
368 }
369
SetBluetoothScanMode(napi_env env,napi_callback_info info)370 napi_value SetBluetoothScanMode(napi_env env, napi_callback_info info)
371 {
372 HILOGD("enter");
373 int32_t mode = 0;
374 int32_t duration = 0;
375 bool checkRet = CheckSetBluetoothScanModeParam(env, info, mode, duration);
376 NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
377 HILOGI("mode = %{public}d,duration = %{public}d", mode, duration);
378
379 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
380 int32_t ret = host->SetBtScanMode(mode, duration);
381 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_NO_ERROR, ret);
382 host->SetBondableMode(BT_TRANSPORT_BREDR, 1);
383 return NapiGetBooleanTrue(env);
384 }
385
GetBluetoothScanMode(napi_env env,napi_callback_info info)386 napi_value GetBluetoothScanMode(napi_env env, napi_callback_info info)
387 {
388 HILOGD("enter");
389 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
390 int32_t scanMode = 0;
391 int32_t err = host->GetBtScanMode(scanMode);
392 napi_value result = nullptr;
393 napi_create_uint32(env, scanMode, &result);
394 NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, result);
395 HILOGI("end");
396 return result;
397 }
398
StartBluetoothDiscovery(napi_env env,napi_callback_info info)399 napi_value StartBluetoothDiscovery(napi_env env, napi_callback_info info)
400 {
401 HILOGD("enter");
402 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
403 int ret = host->StartBtDiscovery();
404 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_NO_ERROR, ret);
405 return NapiGetBooleanTrue(env);
406 }
407
StopBluetoothDiscovery(napi_env env,napi_callback_info info)408 napi_value StopBluetoothDiscovery(napi_env env, napi_callback_info info)
409 {
410 HILOGD("enter");
411 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
412 int ret = host->CancelBtDiscovery();
413 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_NO_ERROR, ret);
414 return NapiGetBooleanTrue(env);
415 }
416
417 #ifdef BLUETOOTH_API_SINCE_10
ParseSetDevicePinCodeParameters(napi_env env,napi_callback_info info,std::string & outRemoteAddr,std::string & outPinCode)418 napi_status ParseSetDevicePinCodeParameters(napi_env env, napi_callback_info info,
419 std::string &outRemoteAddr, std::string &outPinCode)
420 {
421 HILOGD("enter");
422 std::string remoteAddr{};
423 std::string pinCode{};
424 size_t argc = ARGS_SIZE_THREE;
425 napi_value argv[ARGS_SIZE_THREE] = {nullptr};
426 NAPI_BT_CALL_RETURN(napi_get_cb_info(env, info, &argc, argv, nullptr, NULL));
427 NAPI_BT_RETURN_IF(argc != ARGS_SIZE_TWO && argc != ARGS_SIZE_THREE,
428 "Requires 2 or 3 arguments.", napi_invalid_arg);
429 NAPI_BT_CALL_RETURN(NapiParseBdAddr(env, argv[PARAM0], remoteAddr));
430 NAPI_BT_RETURN_IF(!ParseString(env, pinCode, argv[PARAM1]), "pinCode ParseString failed", napi_invalid_arg);
431 outRemoteAddr = remoteAddr;
432 outPinCode = pinCode;
433 return napi_ok;
434 }
435
SetDevicePinCode(napi_env env,napi_callback_info info)436 napi_value SetDevicePinCode(napi_env env, napi_callback_info info)
437 {
438 HILOGD("enter");
439 std::string remoteAddr = "";
440 std::string pinCode = "";
441 auto status = ParseSetDevicePinCodeParameters(env, info, remoteAddr, pinCode);
442 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
443
444 auto func = [remoteAddr, pinCode]() {
445 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
446 int32_t err = remoteDevice.SetDevicePin(pinCode);
447 HILOGI("SetDevicePinCode err: %{public}d", err);
448 return NapiAsyncWorkRet(err);
449 };
450 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
451 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
452 asyncWork->Run();
453 return asyncWork->GetRet();
454 }
455
CheckDeviceAsyncParam(napi_env env,napi_callback_info info,std::string & addr)456 napi_status CheckDeviceAsyncParam(napi_env env, napi_callback_info info, std::string &addr)
457 {
458 size_t argc = ARGS_SIZE_TWO;
459 napi_value argv[ARGS_SIZE_TWO] = {nullptr};
460 NAPI_BT_CALL_RETURN(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
461 NAPI_BT_RETURN_IF(argc != ARGS_SIZE_ONE && argc != ARGS_SIZE_TWO, "Requires 1 or 2 arguments", napi_invalid_arg);
462 NAPI_BT_CALL_RETURN(NapiParseBdAddr(env, argv[PARAM0], addr));
463 return napi_ok;
464 }
465
PairDeviceAsync(napi_env env,napi_callback_info info)466 napi_value PairDeviceAsync(napi_env env, napi_callback_info info)
467 {
468 HILOGD("enter");
469 std::string remoteAddr = INVALID_MAC_ADDRESS;
470 auto checkRet = CheckDeviceAsyncParam(env, info, remoteAddr);
471 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM);
472
473 auto func = [remoteAddr]() {
474 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
475 int32_t err = remoteDevice.StartPair();
476 HILOGI("err: %{public}d", err);
477 return NapiAsyncWorkRet(err);
478 };
479 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
480 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
481 asyncWork->Run();
482 return asyncWork->GetRet();
483 }
484
CancelPairedDeviceAsync(napi_env env,napi_callback_info info)485 napi_value CancelPairedDeviceAsync(napi_env env, napi_callback_info info)
486 {
487 HILOGD("enter");
488 std::string remoteAddr {};
489 bool checkRet = CheckDeviceAsyncParam(env, info, remoteAddr);
490 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM);
491
492 auto func = [remoteAddr]() {
493 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
494 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
495 int32_t err = host->RemovePair(remoteDevice);
496 HILOGI("err: %{public}d", err);
497 return NapiAsyncWorkRet(err);
498 };
499 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
500 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
501 asyncWork->Run();
502 return asyncWork->GetRet();
503 }
504
CancelPairingDevice(napi_env env,napi_callback_info info)505 napi_value CancelPairingDevice(napi_env env, napi_callback_info info)
506 {
507 HILOGD("enter");
508 std::string remoteAddr{};
509 bool checkRet = CheckDeviceAsyncParam(env, info, remoteAddr);
510 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM);
511
512 auto func = [remoteAddr]() {
513 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
514 int32_t err = remoteDevice.CancelPairing();
515 HILOGI("err: %{public}d", err);
516 return NapiAsyncWorkRet(err);
517 };
518 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
519 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
520 asyncWork->Run();
521 return asyncWork->GetRet();
522 }
523
CheckPairCredibleDeviceParam(napi_env env,napi_callback_info info,std::string & addr,int & transport)524 napi_status CheckPairCredibleDeviceParam(napi_env env, napi_callback_info info, std::string &addr, int &transport)
525 {
526 size_t argc = ARGS_SIZE_THREE;
527 napi_value argv[ARGS_SIZE_THREE] = {nullptr};
528 NAPI_BT_CALL_RETURN(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
529 NAPI_BT_RETURN_IF(argc != ARGS_SIZE_TWO && argc != ARGS_SIZE_THREE, "Requires 2 or 3 arguments.", napi_invalid_arg);
530 NAPI_BT_CALL_RETURN(NapiParseBdAddr(env, argv[PARAM0], addr));
531 NAPI_BT_RETURN_IF(!ParseInt32(env, transport, argv[PARAM1]), "ParseInt32 failed", napi_invalid_arg);
532 NAPI_BT_RETURN_IF(!IsValidTransport(transport), "Invalid transport", napi_invalid_arg);
533 return napi_ok;
534 }
535
PairCredibleDevice(napi_env env,napi_callback_info info)536 napi_value PairCredibleDevice(napi_env env, napi_callback_info info)
537 {
538 HILOGD("enter");
539 std::string remoteAddr = INVALID_MAC_ADDRESS;
540 int transport = BT_TRANSPORT_NONE;
541 auto status = CheckPairCredibleDeviceParam(env, info, remoteAddr, transport);
542 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
543
544 auto func = [remoteAddr, transport]() {
545 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr, transport);
546 int32_t err = remoteDevice.StartCrediblePair();
547 HILOGI("err: %{public}d", err);
548 return NapiAsyncWorkRet(err);
549 };
550 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
551 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
552 asyncWork->Run();
553 return asyncWork->GetRet();
554 }
555
CheckGetProfileUuids(napi_env env,napi_callback_info info,std::string & address)556 napi_status CheckGetProfileUuids(napi_env env, napi_callback_info info, std::string &address)
557 {
558 size_t argc = ARGS_SIZE_TWO;
559 napi_value argv[ARGS_SIZE_TWO] = {0};
560 NAPI_BT_CALL_RETURN(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
561 NAPI_BT_RETURN_IF(argc != ARGS_SIZE_ONE && argc != ARGS_SIZE_TWO, "Requires 1 or 2 arguments.", napi_invalid_arg);
562 NAPI_BT_CALL_RETURN(NapiParseBdAddr(env, argv[PARAM0], address));
563 return napi_ok;
564 }
565
GetLocalProfileUuids(napi_env env,napi_callback_info info)566 napi_value GetLocalProfileUuids(napi_env env, napi_callback_info info)
567 {
568 HILOGD("enter");
569 auto func = []() {
570 std::vector<std::string> uuids{};
571 int32_t err = BluetoothHost::GetDefaultHost().GetLocalProfileUuids(uuids);
572 HILOGI("err: %{public}d", err);
573 auto object = std::make_shared<NapiNativeUuidsArray>(uuids);
574 return NapiAsyncWorkRet(err, object);
575 };
576 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
577 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
578 asyncWork->Run();
579 return asyncWork->GetRet();
580 }
581
GetRemoteProfileUuids(napi_env env,napi_callback_info info)582 napi_value GetRemoteProfileUuids(napi_env env, napi_callback_info info)
583 {
584 HILOGD("enter");
585 std::string address;
586 auto status = CheckGetProfileUuids(env, info, address);
587 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
588 auto func = [address]() {
589 std::vector<std::string> uuids{};
590 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(address);
591 int32_t err = remoteDevice.GetDeviceUuids(uuids);
592 HILOGI("err: %{public}d", err);
593 auto object = std::make_shared<NapiNativeUuidsArray>(uuids);
594 return NapiAsyncWorkRet(err, object);
595 };
596 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
597 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
598 asyncWork->Run();
599 return asyncWork->GetRet();
600 }
601
IsBluetoothDiscovering(napi_env env,napi_callback_info info)602 napi_value IsBluetoothDiscovering(napi_env env, napi_callback_info info)
603 {
604 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
605 bool isDiscovering = false;
606 int32_t err = host->IsBtDiscovering(isDiscovering);
607 napi_value result = nullptr;
608 NAPI_BT_ASSERT_RETURN(env, napi_get_boolean(env, isDiscovering, &result) == napi_ok, err, result);
609 NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, result);
610 HILOGE("isBluetoothDiscovering :%{public}d", isDiscovering);
611 return result;
612 }
613
GetPairState(napi_env env,napi_callback_info info)614 napi_value GetPairState(napi_env env, napi_callback_info info)
615 {
616 std::string remoteAddr = INVALID_MAC_ADDRESS;
617 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
618 NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
619 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
620 int state = PAIR_NONE;
621 int32_t err = remoteDevice.GetPairState(state);
622 int pairState = static_cast<int>(BondState::BOND_STATE_INVALID);
623 DealPairStatus(state, pairState);
624 napi_value result = nullptr;
625 NAPI_BT_ASSERT_RETURN(env, napi_create_int32(env, pairState, &result) == napi_ok, err, result);
626 NAPI_BT_ASSERT_RETURN(env, (err == BT_NO_ERROR || err == BT_ERR_INTERNAL_ERROR), err, result);
627 HILOGI("getPairState :%{public}d", pairState);
628 return result;
629 }
630
ConnectAllowedProfiles(napi_env env,napi_callback_info info)631 napi_value ConnectAllowedProfiles(napi_env env, napi_callback_info info)
632 {
633 HILOGI("enter");
634 std::string remoteAddr = INVALID_MAC_ADDRESS;
635 auto checkRet = CheckDeviceAsyncParam(env, info, remoteAddr);
636 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM);
637
638 auto func = [remoteAddr]() {
639 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
640 int32_t ret = host->ConnectAllowedProfiles(remoteAddr);
641 HILOGI("ret: %{public}d", ret);
642 return NapiAsyncWorkRet(ret);
643 };
644 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
645 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
646 asyncWork->Run();
647 return asyncWork->GetRet();
648 }
649
DisconnectAllowedProfiles(napi_env env,napi_callback_info info)650 napi_value DisconnectAllowedProfiles(napi_env env, napi_callback_info info)
651 {
652 HILOGI("enter");
653 std::string remoteAddr = INVALID_MAC_ADDRESS;
654 auto checkRet = CheckDeviceAsyncParam(env, info, remoteAddr);
655 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM);
656
657 auto func = [remoteAddr]() {
658 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
659 int32_t ret = host->DisconnectAllowedProfiles(remoteAddr);
660 HILOGI("ret: %{public}d", ret);
661 return NapiAsyncWorkRet(ret);
662 };
663 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
664 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
665 asyncWork->Run();
666 return asyncWork->GetRet();
667 }
668
GetRemoteProductId(napi_env env,napi_callback_info info)669 napi_value GetRemoteProductId(napi_env env, napi_callback_info info)
670 {
671 HILOGD("start");
672 std::string remoteAddr = INVALID_MAC_ADDRESS;
673 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
674 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
675
676 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
677 std::string productId;
678 int32_t err = remoteDevice.GetDeviceProductId(productId);
679
680 napi_value result = nullptr;
681 napi_create_string_utf8(env, productId.c_str(), productId.size(), &result);
682 NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, result);
683 HILOGI("GetRemoteProductId :%{public}s", productId.c_str());
684 return result;
685 }
686
687 #endif
688
ParseSetRemoteDeviceNameParameters(napi_env env,napi_callback_info info,std::string & outRemoteAddr,std::string & outDeviceName)689 napi_status ParseSetRemoteDeviceNameParameters(napi_env env, napi_callback_info info,
690 std::string &outRemoteAddr, std::string &outDeviceName)
691 {
692 HILOGD("enter");
693 std::string remoteAddr{};
694 std::string deviceName{};
695 size_t argc = ARGS_SIZE_TWO;
696 napi_value argv[ARGS_SIZE_TWO] = {nullptr};
697 NAPI_BT_CALL_RETURN(napi_get_cb_info(env, info, &argc, argv, nullptr, NULL));
698 NAPI_BT_RETURN_IF(argc != ARGS_SIZE_TWO, "Requires 2 arguments.", napi_invalid_arg);
699 NAPI_BT_CALL_RETURN(NapiParseBdAddr(env, argv[PARAM0], remoteAddr));
700 NAPI_BT_RETURN_IF(!ParseString(env, deviceName, argv[PARAM1]), "deviceName ParseString failed", napi_invalid_arg);
701 outRemoteAddr = remoteAddr;
702 outDeviceName = deviceName;
703 return napi_ok;
704 }
705
SetRemoteDeviceName(napi_env env,napi_callback_info info)706 napi_value SetRemoteDeviceName(napi_env env, napi_callback_info info)
707 {
708 HILOGD("enter");
709 std::string remoteAddr = "";
710 std::string deviceName = "";
711 auto status = ParseSetRemoteDeviceNameParameters(env, info, remoteAddr, deviceName);
712 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
713
714 auto func = [remoteAddr, deviceName]() {
715 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
716 int32_t err = remoteDevice.SetDeviceAlias(deviceName);
717 HILOGI("SetDeviceName err: %{public}d", err);
718 return NapiAsyncWorkRet(err);
719 };
720 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
721 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
722 asyncWork->Run();
723 return asyncWork->GetRet();
724 }
725
ParseSetRemoteDeviceTypeParameters(napi_env env,napi_callback_info info,std::string & outRemoteAddr,int32_t & outDeviceType)726 napi_status ParseSetRemoteDeviceTypeParameters(napi_env env, napi_callback_info info,
727 std::string &outRemoteAddr, int32_t &outDeviceType)
728 {
729 HILOGD("enter");
730 std::string remoteAddr{};
731 int32_t deviceType = DeviceType::DEVICE_TYPE_DEFAULT;
732 size_t argc = ARGS_SIZE_TWO;
733 napi_value argv[ARGS_SIZE_TWO] = {nullptr};
734 NAPI_BT_CALL_RETURN(napi_get_cb_info(env, info, &argc, argv, nullptr, NULL));
735 NAPI_BT_RETURN_IF(argc != ARGS_SIZE_TWO, "Requires 2 arguments.", napi_invalid_arg);
736 NAPI_BT_CALL_RETURN(NapiParseBdAddr(env, argv[PARAM0], remoteAddr));
737 NAPI_BT_RETURN_IF(!ParseInt32(env, deviceType, argv[PARAM1]), "deviceType ParseInt32 failed", napi_invalid_arg);
738 outRemoteAddr = remoteAddr;
739 outDeviceType = deviceType;
740 return napi_ok;
741 }
742
SetRemoteDeviceType(napi_env env,napi_callback_info info)743 napi_value SetRemoteDeviceType(napi_env env, napi_callback_info info)
744 {
745 HILOGD("enter");
746 std::string remoteAddr = INVALID_MAC_ADDRESS;
747 int32_t deviceType = DeviceType::DEVICE_TYPE_DEFAULT;
748 auto status = ParseSetRemoteDeviceTypeParameters(env, info, remoteAddr, deviceType);
749 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
750
751 auto func = [remoteAddr, deviceType]() {
752 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
753 int32_t err = remoteDevice.SetDeviceCustomType(deviceType);
754 HILOGI("SetRemoteDeviceType err: %{public}d", err);
755 return NapiAsyncWorkRet(err);
756 };
757 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
758 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
759 asyncWork->Run();
760 return asyncWork->GetRet();
761 }
762
GetRemoteDeviceType(napi_env env,napi_callback_info info)763 napi_value GetRemoteDeviceType(napi_env env, napi_callback_info info)
764 {
765 HILOGD("enter");
766 std::string remoteAddr;
767 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
768 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
769 auto func = [remoteAddr]() {
770 int32_t deviceType = DeviceType::DEVICE_TYPE_DEFAULT;
771 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
772 int32_t err = remoteDevice.GetDeviceCustomType(deviceType);
773 HILOGI("GetRemoteDeviceType err: %{public}d", err);
774 auto object = std::make_shared<NapiNativeInt>(deviceType);
775 return NapiAsyncWorkRet(err, object);
776 };
777 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
778 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
779 asyncWork->Run();
780 return asyncWork->GetRet();
781 }
782
GetRemoteDeviceBatteryInfo(napi_env env,napi_callback_info info)783 napi_value GetRemoteDeviceBatteryInfo(napi_env env, napi_callback_info info)
784 {
785 HILOGD("enter");
786 std::string remoteAddr = INVALID_MAC_ADDRESS;
787 auto checkRet = CheckDeivceIdParam(env, info, remoteAddr);
788 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
789 auto func = [remoteAddr]() {
790 DeviceBatteryInfo batteryInfo;
791 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
792 int32_t err = remoteDevice.GetRemoteDeviceBatteryInfo(batteryInfo);
793 HILOGI("err: %{public}d", err);
794 auto object = std::make_shared<NapiNativeBatteryInfo>(batteryInfo);
795 return NapiAsyncWorkRet(err, object);
796 };
797 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
798 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
799 asyncWork->Run();
800 return asyncWork->GetRet();
801 }
802
ConnectionPropertyValueInit(napi_env env,napi_value exports)803 napi_value ConnectionPropertyValueInit(napi_env env, napi_value exports)
804 {
805 HILOGD("enter");
806 napi_value scanModeObj = ScanModeInit(env);
807 napi_value bondStateObj = BondStateInit(env);
808 napi_value unbondCauseObj = UnbondCauseInit(env);
809 #ifdef BLUETOOTH_API_SINCE_10
810 napi_value bluetoothTransportObject = BluetoothTransportInit(env);
811 napi_value pinTypeObject = PinTypeInit(env);
812 #endif
813 napi_value deviceTypeObject = DeviceTypeInit(env);
814 napi_value deviceChargeStateObject = DeviceChargeStateInit(env);
815 napi_property_descriptor exportProperties[] = {
816 DECLARE_NAPI_PROPERTY("ScanMode", scanModeObj),
817 DECLARE_NAPI_PROPERTY("BondState", bondStateObj),
818 DECLARE_NAPI_PROPERTY("UnbondCause", unbondCauseObj),
819 #ifdef BLUETOOTH_API_SINCE_10
820 DECLARE_NAPI_PROPERTY("BluetoothTransport", bluetoothTransportObject),
821 DECLARE_NAPI_PROPERTY("PinType", pinTypeObject),
822 #endif
823 DECLARE_NAPI_PROPERTY("DeviceType", deviceTypeObject),
824 DECLARE_NAPI_PROPERTY("DeviceChargeState", deviceChargeStateObject),
825 };
826 HITRACE_METER_NAME(HITRACE_TAG_OHOS, "connection:napi_define_properties");
827 napi_define_properties(env, exports, sizeof(exportProperties) / sizeof(*exportProperties), exportProperties);
828 return exports;
829 }
830
ScanModeInit(napi_env env)831 napi_value ScanModeInit(napi_env env)
832 {
833 HILOGD("enter");
834 napi_value scanMode = nullptr;
835 napi_create_object(env, &scanMode);
836 SetNamedPropertyByInteger(env, scanMode, static_cast<int>(ScanMode::SCAN_MODE_NONE), "SCAN_MODE_NONE");
837 SetNamedPropertyByInteger(
838 env, scanMode, static_cast<int>(ScanMode::SCAN_MODE_CONNECTABLE), "SCAN_MODE_CONNECTABLE");
839 SetNamedPropertyByInteger(
840 env, scanMode, static_cast<int>(ScanMode::SCAN_MODE_GENERAL_DISCOVERABLE), "SCAN_MODE_GENERAL_DISCOVERABLE");
841 SetNamedPropertyByInteger(
842 env, scanMode, static_cast<int>(ScanMode::SCAN_MODE_LIMITED_DISCOVERABLE), "SCAN_MODE_LIMITED_DISCOVERABLE");
843 SetNamedPropertyByInteger(env,
844 scanMode,
845 static_cast<int>(ScanMode::SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE),
846 "SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE");
847 SetNamedPropertyByInteger(env,
848 scanMode,
849 static_cast<int>(ScanMode::SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE),
850 "SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE");
851 return scanMode;
852 }
853
BondStateInit(napi_env env)854 napi_value BondStateInit(napi_env env)
855 {
856 HILOGD("enter");
857 napi_value bondState = nullptr;
858 napi_create_object(env, &bondState);
859 SetNamedPropertyByInteger(env, bondState, static_cast<int>(BondState::BOND_STATE_INVALID), "BOND_STATE_INVALID");
860 SetNamedPropertyByInteger(env, bondState, static_cast<int>(BondState::BOND_STATE_BONDING), "BOND_STATE_BONDING");
861 SetNamedPropertyByInteger(env, bondState, static_cast<int>(BondState::BOND_STATE_BONDED), "BOND_STATE_BONDED");
862 return bondState;
863 }
864
UnbondCauseInit(napi_env env)865 napi_value UnbondCauseInit(napi_env env)
866 {
867 HILOGD("enter");
868 napi_value unbondCause = nullptr;
869 napi_create_object(env, &unbondCause);
870 SetNamedPropertyByInteger(env, unbondCause, UNBOND_CAUSE_USER_REMOVED, "USER_REMOVED");
871 SetNamedPropertyByInteger(env, unbondCause, UNBOND_CAUSE_REMOTE_DEVICE_DOWN, "REMOTE_DEVICE_DOWN");
872 SetNamedPropertyByInteger(env, unbondCause, UNBOND_CAUSE_AUTH_FAILURE, "AUTH_FAILURE");
873 SetNamedPropertyByInteger(env, unbondCause, UNBOND_CAUSE_AUTH_REJECTED, "AUTH_REJECTED");
874 SetNamedPropertyByInteger(env, unbondCause, UNBOND_CAUSE_INTERNAL_ERROR, "INTERNAL_ERROR");
875 return unbondCause;
876 }
877
878 #ifdef BLUETOOTH_API_SINCE_10
BluetoothTransportInit(napi_env env)879 napi_value BluetoothTransportInit(napi_env env)
880 {
881 HILOGD("enter");
882 napi_value bluetoothTransport = nullptr;
883 napi_create_object(env, &bluetoothTransport);
884 SetNamedPropertyByInteger(
885 env, bluetoothTransport, static_cast<int>(BluetoothTransport::TRANSPORT_BR_EDR), "TRANSPORT_BR_EDR");
886 SetNamedPropertyByInteger(
887 env, bluetoothTransport, static_cast<int>(BluetoothTransport::TRANSPORT_LE), "TRANSPORT_LE");
888 return bluetoothTransport;
889 }
890
PinTypeInit(napi_env env)891 napi_value PinTypeInit(napi_env env)
892 {
893 HILOGD("enter");
894 napi_value pinType = nullptr;
895 napi_create_object(env, &pinType);
896 SetNamedPropertyByInteger(
897 env, pinType, static_cast<int>(PinType::PIN_TYPE_ENTER_PIN_CODE), "PIN_TYPE_ENTER_PIN_CODE");
898 SetNamedPropertyByInteger(
899 env, pinType, static_cast<int>(PinType::PIN_TYPE_ENTER_PASSKEY), "PIN_TYPE_ENTER_PASSKEY");
900 SetNamedPropertyByInteger(
901 env, pinType, static_cast<int>(PinType::PIN_TYPE_CONFIRM_PASSKEY), "PIN_TYPE_CONFIRM_PASSKEY");
902 SetNamedPropertyByInteger(
903 env, pinType, static_cast<int>(PinType::PIN_TYPE_NO_PASSKEY_CONSENT), "PIN_TYPE_NO_PASSKEY_CONSENT");
904 SetNamedPropertyByInteger(
905 env, pinType, static_cast<int>(PinType::PIN_TYPE_NOTIFY_PASSKEY), "PIN_TYPE_NOTIFY_PASSKEY");
906 SetNamedPropertyByInteger(
907 env, pinType, static_cast<int>(PinType::PIN_TYPE_DISPLAY_PIN_CODE), "PIN_TYPE_DISPLAY_PIN_CODE");
908 SetNamedPropertyByInteger(env, pinType, static_cast<int>(PinType::PIN_TYPE_OOB_CONSENT), "PIN_TYPE_OOB_CONSENT");
909 SetNamedPropertyByInteger(
910 env, pinType, static_cast<int>(PinType::PIN_TYPE_PIN_16_DIGITS), "PIN_TYPE_PIN_16_DIGITS");
911 return pinType;
912 }
913 #endif
914
DeviceTypeInit(napi_env env)915 napi_value DeviceTypeInit(napi_env env)
916 {
917 HILOGD("enter");
918 napi_value deviceType = nullptr;
919 napi_create_object(env, &deviceType);
920 SetNamedPropertyByInteger(
921 env, deviceType, static_cast<int>(DeviceType::DEVICE_TYPE_DEFAULT), "DEVICE_TYPE_DEFAULT");
922 SetNamedPropertyByInteger(
923 env, deviceType, static_cast<int>(DeviceType::DEVICE_TYPE_CAR), "DEVICE_TYPE_CAR");
924 SetNamedPropertyByInteger(
925 env, deviceType, static_cast<int>(DeviceType::DEVICE_TYPE_HEADSET), "DEVICE_TYPE_HEADSET");
926 SetNamedPropertyByInteger(
927 env, deviceType, static_cast<int>(DeviceType::DEVICE_TYPE_HEARING), "DEVICE_TYPE_HEARING");
928 SetNamedPropertyByInteger(
929 env, deviceType, static_cast<int>(DeviceType::DEVICE_TYPE_GLASSES), "DEVICE_TYPE_GLASSES");
930 SetNamedPropertyByInteger(
931 env, deviceType, static_cast<int>(DeviceType::DEVICE_TYPE_WATCH), "DEVICE_TYPE_WATCH");
932 SetNamedPropertyByInteger(
933 env, deviceType, static_cast<int>(DeviceType::DEVICE_TYPE_SPEAKER), "DEVICE_TYPE_SPEAKER");
934 SetNamedPropertyByInteger(
935 env, deviceType, static_cast<int>(DeviceType::DEVICE_TYPE_OTHERS), "DEVICE_TYPE_OTHERS");
936 return deviceType;
937 }
938
DeviceChargeStateInit(napi_env env)939 napi_value DeviceChargeStateInit(napi_env env)
940 {
941 HILOGD("enter");
942 napi_value deviceChargeState = nullptr;
943 napi_create_object(env, &deviceChargeState);
944 SetNamedPropertyByInteger(
945 env, deviceChargeState, static_cast<int32_t>(DeviceChargeState::DEVICE_NORMAL_CHARGE_NOT_CHARGED),
946 "DEVICE_NORMAL_CHARGE_NOT_CHARGED");
947 SetNamedPropertyByInteger(
948 env, deviceChargeState, static_cast<int32_t>(DeviceChargeState::DEVICE_NORMAL_CHARGE_IN_CHARGING),
949 "DEVICE_NORMAL_CHARGE_IN_CHARGING");
950 SetNamedPropertyByInteger(
951 env, deviceChargeState, static_cast<int32_t>(DeviceChargeState::DEVICE_SUPER_CHARGE_NOT_CHARGED),
952 "DEVICE_SUPER_CHARGE_NOT_CHARGED");
953 SetNamedPropertyByInteger(
954 env, deviceChargeState, static_cast<int32_t>(DeviceChargeState::DEVICE_SUPER_CHARGE_IN_CHARGING),
955 "DEVICE_SUPER_CHARGE_IN_CHARGING");
956 return deviceChargeState;
957 }
958
RegisterObserverToHost()959 void RegisterObserverToHost()
960 {
961 HILOGD("enter");
962 BluetoothHost &host = BluetoothHost::GetDefaultHost();
963 host.RegisterObserver(g_connectionObserver);
964 host.RegisterRemoteDeviceObserver(g_remoteDeviceObserver);
965 }
966
DealPairStatus(const int & status,int & bondStatus)967 void DealPairStatus(const int &status, int &bondStatus)
968 {
969 HILOGD("status is %{public}d", status);
970 switch (status) {
971 case PAIR_NONE:
972 bondStatus = static_cast<int>(BondState::BOND_STATE_INVALID);
973 break;
974 case PAIR_PAIRING:
975 bondStatus = static_cast<int>(BondState::BOND_STATE_BONDING);
976 break;
977 case PAIR_PAIRED:
978 bondStatus = static_cast<int>(BondState::BOND_STATE_BONDED);
979 break;
980 default:
981 break;
982 }
983 }
984
GetRemoteDeviceConnectionTime(napi_env env,napi_callback_info info)985 napi_value GetRemoteDeviceConnectionTime(napi_env env, napi_callback_info info)
986 {
987 HILOGD("enter");
988 std::string remoteAddr = INVALID_MAC_ADDRESS;
989 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
990 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
991 auto func = [remoteAddr]() {
992 int64_t connectionTime = 0;
993 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
994 int32_t err = remoteDevice.GetLastConnectionTime(connectionTime);
995 HILOGI("GetRemoteDeviceConnectionTime GetLastConnectionTime err: %{public}d", err);
996 auto object = std::make_shared<NapiNativeInt64>(connectionTime);
997 return NapiAsyncWorkRet(err, object);
998 };
999 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
1000 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
1001 asyncWork->Run();
1002 return asyncWork->GetRet();
1003 }
1004
UpdateCloudBluetoothDevice(napi_env env,napi_callback_info info)1005 napi_value UpdateCloudBluetoothDevice(napi_env env, napi_callback_info info)
1006 {
1007 HILOGI("[CLOUD_DEV] UpdateCloudBluetoothDevice enter");
1008 size_t argc = ARGS_SIZE_ONE;
1009 napi_value argv[ARGS_SIZE_ONE] = {nullptr};
1010 napi_value thisVar = nullptr;
1011 auto checkRes = napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL);
1012 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRes == napi_ok, BT_ERR_INVALID_PARAM);
1013 std::vector<TrustPairDeviceParam> trustPairs {};
1014 auto status = NapiParseTrustPairDevice(env, argv[PARAM0], trustPairs);
1015 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
1016 auto func = [trustPairs]() {
1017 int32_t err = BluetoothHost::GetDefaultHost().UpdateCloudBluetoothDevice(trustPairs);
1018 HILOGI("[CLOUD_DEV] UpdateCloudBluetoothDevice err: %{public}d", err);
1019 return NapiAsyncWorkRet(err);
1020 };
1021 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
1022 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
1023 asyncWork->Run();
1024 return asyncWork->GetRet();
1025 }
1026 } // namespace Bluetooth
1027 } // namespace OHOS