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