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