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