1 /*
2 * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "napi_bluetooth_host.h"
17 #include "bluetooth_host.h"
18 #include "bluetooth_log.h"
19 #include "bluetooth_errorcode.h"
20 #include "napi_bluetooth_error.h"
21 #include "napi_bluetooth_host_observer.h"
22 #include "napi_bluetooth_remote_device_observer.h"
23 #include "napi_bluetooth_utils.h"
24
25 namespace OHOS {
26 namespace Bluetooth {
27 namespace {
28 NapiBluetoothHostObserver g_bluetoothHostObserver;
29 std::shared_ptr<BluetoothRemoteDeviceObserver> g_bluetoothRemoteDevice;
30 std::string g_RemoteDeviceAddr;
31 std::vector<std::shared_ptr<BluetoothRemoteDevice>> g_DiscoveryDevices;
32 std::mutex deviceMutex;
33 } // namespace
34
BluetoothHostInit(napi_env env,napi_value exports)35 napi_value BluetoothHostInit(napi_env env, napi_value exports)
36 {
37 HILOGI("start");
38 RegisterObserverToHost();
39 PropertyValueInit(env, exports);
40 napi_property_descriptor desc[] = {
41 DECLARE_NAPI_FUNCTION("getState", GetState),
42 DECLARE_NAPI_FUNCTION("getBtConnectionState", GetBtConnectionState),
43 DECLARE_NAPI_FUNCTION("pairDevice", PairDevice),
44 DECLARE_NAPI_FUNCTION("cancelPairedDevice", CancelPairedDevice),
45 DECLARE_NAPI_FUNCTION("getRemoteDeviceName", GetRemoteDeviceName),
46 DECLARE_NAPI_FUNCTION("getRemoteDeviceClass", GetRemoteDeviceClass),
47 DECLARE_NAPI_FUNCTION("enableBluetooth", EnableBluetooth),
48 DECLARE_NAPI_FUNCTION("disableBluetooth", DisableBluetooth),
49 DECLARE_NAPI_FUNCTION("getLocalName", GetLocalName),
50 DECLARE_NAPI_FUNCTION("getPairedDevices", GetPairedDevices),
51 DECLARE_NAPI_FUNCTION("getProfileConnState", GetProfileConnState),
52 DECLARE_NAPI_FUNCTION("getProfileConnectionState", GetProfileConnState),
53 DECLARE_NAPI_FUNCTION("setDevicePairingConfirmation", SetDevicePairingConfirmation),
54 DECLARE_NAPI_FUNCTION("setLocalName", SetLocalName),
55 DECLARE_NAPI_FUNCTION("setBluetoothScanMode", SetBluetoothScanMode),
56 DECLARE_NAPI_FUNCTION("getBluetoothScanMode", GetBluetoothScanMode),
57 DECLARE_NAPI_FUNCTION("startBluetoothDiscovery", StartBluetoothDiscovery),
58 DECLARE_NAPI_FUNCTION("stopBluetoothDiscovery", StopBluetoothDiscovery),
59 DECLARE_NAPI_FUNCTION("on", RegisterObserver),
60 DECLARE_NAPI_FUNCTION("off", DeregisterObserver),
61 };
62 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
63 HILOGI("end");
64 return exports;
65 }
66
RegisterObserverToHost()67 void RegisterObserverToHost()
68 {
69 HILOGI("start");
70 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
71 g_bluetoothRemoteDevice = std::make_shared<NapiBluetoothRemoteDeviceObserver>();
72 if (g_bluetoothRemoteDevice == nullptr) {
73 HILOGE("g_bluetoothRemoteDevice is null");
74 return;
75 }
76 host->RegisterObserver(g_bluetoothHostObserver);
77 host->RegisterRemoteDeviceObserver(g_bluetoothRemoteDevice);
78 }
79
EnableBluetooth(napi_env env,napi_callback_info info)80 napi_value EnableBluetooth(napi_env env, napi_callback_info info)
81 {
82 HILOGI("start");
83 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
84 int32_t ret = host->EnableBle();
85 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_SUCCESS, ret);
86 if (ret == BT_SUCCESS) {
87 SetCurrentAppOperate(true);
88 }
89 return NapiGetBooleanTrue(env);
90 }
91
DisableBluetooth(napi_env env,napi_callback_info info)92 napi_value DisableBluetooth(napi_env env, napi_callback_info info)
93 {
94 HILOGI("start");
95 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
96 // only ble
97 int state = BTStateID::STATE_TURN_OFF;
98 int ret = host->GetBtState(state);
99 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_SUCCESS, ret);
100 if (state == BTStateID::STATE_TURN_OFF) {
101 ret = host->DisableBle();
102 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_SUCCESS, ret);
103 } else {
104 ret = host->DisableBt();
105 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_SUCCESS, ret);
106 }
107
108 if (ret == BT_SUCCESS) {
109 SetCurrentAppOperate(true);
110 }
111 return NapiGetBooleanTrue(env);
112 }
113
SetLocalName(napi_env env,napi_callback_info info)114 napi_value SetLocalName(napi_env env, napi_callback_info info)
115 {
116 HILOGI("start");
117 std::string localName = INVALID_NAME;
118 bool checkRet = CheckLocalNameParam(env, info, localName);
119 NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
120
121 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
122 int32_t ret = host->SetLocalName(localName);
123 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_SUCCESS, ret);
124 return NapiGetBooleanTrue(env);
125 }
126
GetLocalName(napi_env env,napi_callback_info info)127 napi_value GetLocalName(napi_env env, napi_callback_info info)
128 {
129 napi_value result = nullptr;
130 HILOGI("start");
131 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
132 std::string localName = INVALID_NAME;
133 int32_t err = host->GetLocalName(localName);
134 napi_create_string_utf8(env, localName.c_str(), localName.size(), &result);
135 NAPI_BT_ASSERT_RETURN(env, err == BT_SUCCESS, err, result);
136 HILOGI("end");
137 return result;
138 }
139
GetRemoteDeviceName(napi_env env,napi_callback_info info)140 napi_value GetRemoteDeviceName(napi_env env, napi_callback_info info)
141 {
142 HILOGI("start");
143 std::string remoteAddr = INVALID_MAC_ADDRESS;
144 std::string name = INVALID_NAME;
145 napi_value result = nullptr;
146 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
147 napi_create_string_utf8(env, name.c_str(), name.size(), &result);
148 NAPI_BT_ASSERT_RETURN(env, checkRet == true, BT_ERR_INVALID_PARAM, result);
149
150 int transport = GetDeviceTransport(remoteAddr);
151 int32_t err = BluetoothHost::GetDefaultHost().GetRemoteDevice(remoteAddr, transport).GetDeviceName(name);
152 napi_create_string_utf8(env, name.c_str(), name.size(), &result);
153 NAPI_BT_ASSERT_RETURN(env, err == BT_SUCCESS, err, result);
154 HILOGI("end");
155 return result;
156 }
157
GetRemoteDeviceClass(napi_env env,napi_callback_info info)158 napi_value GetRemoteDeviceClass(napi_env env, napi_callback_info info)
159 {
160 HILOGI("start");
161 std::string remoteAddr = INVALID_MAC_ADDRESS;
162 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
163 NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
164
165 int transport = GetDeviceTransport(remoteAddr);
166 int deviceCod = 0;
167 int32_t err = BluetoothHost::GetDefaultHost().GetRemoteDevice(remoteAddr, transport).GetDeviceClass(deviceCod);
168 BluetoothDeviceClass deviceClass = BluetoothDeviceClass(deviceCod);
169 int tmpCod = deviceClass.GetClassOfDevice();
170 int tmpMajorClass = deviceClass.GetMajorClass();
171 int tmpMajorMinorClass = deviceClass.GetMajorMinorClass();
172 if (tmpCod == 0) {
173 HILOGI("cod = %{public}d", tmpCod);
174 tmpCod = MajorClass::MAJOR_UNCATEGORIZED;
175 tmpMajorClass = MajorClass::MAJOR_UNCATEGORIZED;
176 tmpMajorMinorClass = MajorClass::MAJOR_UNCATEGORIZED;
177 }
178 HILOGI("cod = %{public}d, majorClass = %{public}d, majorMinorClass = %{public}d",
179 tmpCod, tmpMajorClass, tmpMajorMinorClass);
180 napi_value result = nullptr;
181 napi_create_object(env, &result);
182 napi_value majorClass = 0;
183 napi_create_int32(env, tmpMajorClass, &majorClass);
184 napi_set_named_property(env, result, "majorClass", majorClass);
185 napi_value majorMinorClass = 0;
186 napi_create_int32(env, tmpMajorMinorClass, &majorMinorClass);
187 napi_set_named_property(env, result, "majorMinorClass", majorMinorClass);
188 napi_value cod = 0;
189 napi_create_int32(env, tmpCod, &cod);
190 napi_set_named_property(env, result, "classOfDevice", cod);
191 NAPI_BT_ASSERT_RETURN(env, err == BT_SUCCESS, err, result);
192 HILOGI("end");
193 return result;
194 }
195
SetBluetoothScanMode(napi_env env,napi_callback_info info)196 napi_value SetBluetoothScanMode(napi_env env, napi_callback_info info)
197 {
198 HILOGI("start");
199 int32_t mode = 0;
200 int32_t duration = 0;
201 bool checkRet = CheckSetBluetoothScanModeParam(env, info, mode, duration);
202 NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
203 HILOGI("mode = %{public}d,duration = %{public}d", mode, duration);
204
205 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
206 int32_t ret = host->SetBtScanMode(mode, duration);
207 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_SUCCESS, ret);
208 host->SetBondableMode(BT_TRANSPORT_BREDR, 1);
209 return NapiGetBooleanTrue(env);
210 }
211
GetBluetoothScanMode(napi_env env,napi_callback_info info)212 napi_value GetBluetoothScanMode(napi_env env, napi_callback_info info)
213 {
214 HILOGI("start");
215 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
216 int32_t scanMode = 0;
217 int32_t err = host->GetBtScanMode(scanMode);
218 napi_value result = nullptr;
219 napi_create_uint32(env, scanMode, &result);
220 NAPI_BT_ASSERT_RETURN(env, err == BT_SUCCESS, err, result);
221 HILOGI("end");
222 return result;
223 }
224
StartBluetoothDiscovery(napi_env env,napi_callback_info info)225 napi_value StartBluetoothDiscovery(napi_env env, napi_callback_info info)
226 {
227 HILOGI("start");
228 ClearDiscoveryDevice();
229 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
230 int ret = host->StartBtDiscovery();
231 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_SUCCESS, ret);
232 return NapiGetBooleanTrue(env);
233 }
234
StopBluetoothDiscovery(napi_env env,napi_callback_info info)235 napi_value StopBluetoothDiscovery(napi_env env, napi_callback_info info)
236 {
237 HILOGI("start");
238 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
239 int ret = host->CancelBtDiscovery();
240 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_SUCCESS, ret);
241 return NapiGetBooleanTrue(env);
242 }
243
GetState(napi_env env,napi_callback_info info)244 napi_value GetState(napi_env env, napi_callback_info info)
245 {
246 HILOGI("enter");
247 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
248 int32_t state = BTStateID::STATE_TURN_OFF;
249 int32_t err = host->GetBtState(state);
250 NAPI_BT_ASSERT_RETURN_FALSE(env, err == NO_ERROR, err);
251 int32_t status = static_cast<int32_t>(BluetoothState::STATE_OFF);
252 switch (state) {
253 case BTStateID::STATE_TURNING_ON:
254 HILOGI("STATE_TURNING_ON(1)");
255 status = static_cast<int32_t>(BluetoothState::STATE_TURNING_ON);
256 break;
257 case BTStateID::STATE_TURN_ON:
258 HILOGI("STATE_ON(2)");
259 status = static_cast<int32_t>(BluetoothState::STATE_ON);
260 break;
261 case BTStateID::STATE_TURNING_OFF:
262 HILOGI("STATE_TURNING_OFF(3)");
263 status = static_cast<int32_t>(BluetoothState::STATE_TURNING_OFF);
264 break;
265 case BTStateID::STATE_TURN_OFF:
266 HILOGI("STATE_OFF(0)");
267 status = static_cast<int32_t>(BluetoothState::STATE_OFF);
268 break;
269 default:
270 HILOGE("get state failed");
271 break;
272 }
273
274 bool enableBle = host->IsBleEnabled();
275 if (enableBle && (state == BTStateID::STATE_TURN_OFF)) {
276 HILOGI("BR off and BLE on, STATE_BLE_ON(5)");
277 status = static_cast<int32_t>(BluetoothState::STATE_BLE_ON);
278 } else if (!enableBle && (state == BTStateID::STATE_TURN_OFF)) {
279 status = static_cast<int32_t>(BluetoothState::STATE_OFF);
280 }
281
282 napi_value result = nullptr;
283 napi_create_int32(env, status, &result);
284 return result;
285 }
286
GetBtConnectionState(napi_env env,napi_callback_info info)287 napi_value GetBtConnectionState(napi_env env, napi_callback_info info)
288 {
289 HILOGI("start");
290 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
291 int state = static_cast<int>(BTConnectState::DISCONNECTED);
292 int32_t err = host->GetBtConnectionState(state);
293 HILOGI("start state %{public}d", state);
294 napi_value result = nullptr;
295 napi_create_int32(env, GetProfileConnectionState(state), &result);
296 NAPI_BT_ASSERT_RETURN(env, err == BT_SUCCESS, err, result);
297 HILOGI("end");
298 return result;
299 }
300
PairDevice(napi_env env,napi_callback_info info)301 napi_value PairDevice(napi_env env, napi_callback_info info)
302 {
303 HILOGI("start");
304 std::string remoteAddr = INVALID_MAC_ADDRESS;
305 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
306 NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
307
308 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr, BT_TRANSPORT_BREDR);
309 int deviceType = remoteDevice.GetDeviceType();
310 if (deviceType == INVALID_TYPE) {
311 HILOGE("device is not discovery or scan, just quick BLE pair");
312 remoteDevice = BluetoothRemoteDevice(remoteAddr, BT_TRANSPORT_BLE);
313 }
314 if (deviceType == DEVICE_TYPE_LE) {
315 remoteDevice = BluetoothRemoteDevice(remoteAddr, BT_TRANSPORT_BLE);
316 }
317 int32_t ret = remoteDevice.StartPair();
318 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_SUCCESS, ret);
319 return NapiGetBooleanTrue(env);
320 }
321
CancelPairedDevice(napi_env env,napi_callback_info info)322 napi_value CancelPairedDevice(napi_env env, napi_callback_info info)
323 {
324 HILOGI("start");
325 std::string remoteAddr {};
326 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
327 NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
328
329 int transport = GetDeviceTransport(remoteAddr);
330 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr, transport);
331 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
332 int32_t ret = host->RemovePair(remoteDevice);
333 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_SUCCESS, ret);
334
335 return NapiGetBooleanTrue(env);
336 }
337
GetPairedDevices(napi_env env,napi_callback_info info)338 napi_value GetPairedDevices(napi_env env, napi_callback_info info)
339 {
340 HILOGI("start");
341 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
342 std::vector<BluetoothRemoteDevice> remoteDeviceLists;
343 int32_t ret = host->GetPairedDevices(BT_TRANSPORT_BREDR, remoteDeviceLists);
344 napi_value result = nullptr;
345 int count = 0;
346 napi_create_array(env, &result);
347 for (auto vec : remoteDeviceLists) {
348 napi_value remoteDeviceResult;
349 napi_create_string_utf8(env, vec.GetDeviceAddr().c_str(), vec.GetDeviceAddr().size(), &remoteDeviceResult);
350 napi_set_element(env, result, count, remoteDeviceResult);
351 count++;
352 }
353 NAPI_BT_ASSERT_RETURN(env, ret == BT_SUCCESS, ret, result);
354 std::vector<BluetoothRemoteDevice> bleDeviceLists;
355 ret = host->GetPairedDevices(BT_TRANSPORT_BLE, bleDeviceLists);
356 for (auto vec : bleDeviceLists) {
357 napi_value remoteDeviceResult;
358 napi_create_string_utf8(env, vec.GetDeviceAddr().c_str(), vec.GetDeviceAddr().size(), &remoteDeviceResult);
359 napi_set_element(env, result, count, remoteDeviceResult);
360 count++;
361 }
362 NAPI_BT_ASSERT_RETURN(env, ret == BT_SUCCESS, ret, result);
363 HILOGI("end");
364 return result;
365 }
366
SetDevicePairingConfirmation(napi_env env,napi_callback_info info)367 napi_value SetDevicePairingConfirmation(napi_env env, napi_callback_info info)
368 {
369 HILOGI("start");
370 std::string remoteAddr {};
371 bool accept = false;
372 bool checkRet = CheckSetDevicePairingConfirmationParam(env, info, remoteAddr, accept);
373 NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
374
375 HILOGI("SetDevicePairingConfirmation::accept = %{public}d", accept);
376 int transport = GetDeviceTransport(remoteAddr);
377 int32_t ret = BluetoothHost::GetDefaultHost()
378 .GetRemoteDevice(remoteAddr, transport)
379 .SetDevicePairingConfirmation(accept);
380 NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_SUCCESS, ret);
381 return NapiGetBooleanTrue(env);
382 }
383
SetCallback(const napi_env & env,const napi_ref & callbackIn,const int & errorCode,const napi_value & result)384 static void SetCallback(const napi_env &env, const napi_ref &callbackIn, const int &errorCode, const napi_value &result)
385 {
386 HILOGI("enter");
387 napi_value undefined = nullptr;
388 napi_get_undefined(env, &undefined);
389
390 napi_value callback = nullptr;
391 napi_value resultout = nullptr;
392 napi_get_reference_value(env, callbackIn, &callback);
393 napi_value results[ARGS_SIZE_TWO] = {nullptr};
394 results[PARAM0] = GetCallbackErrorValue(env, errorCode);
395 results[PARAM1] = result;
396 NAPI_CALL_RETURN_VOID(
397 env, napi_call_function(env, undefined, callback, ARGS_SIZE_TWO, &results[PARAM0], &resultout));
398 HILOGI("end");
399 }
400
SetPromise(const napi_env & env,const napi_deferred & deferred,const napi_value & result)401 static void SetPromise(const napi_env &env, const napi_deferred &deferred, const napi_value &result)
402 {
403 HILOGI("enter");
404 napi_resolve_deferred(env, deferred, result);
405 HILOGI("end");
406 }
407
PaddingCallbackPromiseInfo(const napi_env & env,const napi_ref & callback,CallbackPromiseInfo & info,napi_value & promise)408 static void PaddingCallbackPromiseInfo(
409 const napi_env &env, const napi_ref &callback, CallbackPromiseInfo &info, napi_value &promise)
410 {
411 HILOGI("enter");
412 if (callback) {
413 info.callback = callback;
414 info.isCallback = true;
415 } else {
416 napi_deferred deferred = nullptr;
417 NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise));
418 info.deferred = deferred;
419 info.isCallback = false;
420 }
421 HILOGI("end");
422 }
423
ReturnCallbackPromise(const napi_env & env,const CallbackPromiseInfo & info,const napi_value & result)424 static void ReturnCallbackPromise(const napi_env &env, const CallbackPromiseInfo &info, const napi_value &result)
425 {
426 HILOGI("enter");
427 if (info.isCallback) {
428 SetCallback(env, info.callback, info.errorCode, result);
429 } else {
430 SetPromise(env, info.deferred, result);
431 }
432 HILOGI("end");
433 }
434
GetDeviceNameSyncWorkStart(GattGetDeviceNameCallbackInfo * asynccallbackinfo)435 static void GetDeviceNameSyncWorkStart(GattGetDeviceNameCallbackInfo *asynccallbackinfo)
436 {
437 HILOGI("enter");
438 std::string deviceId = GetGattClientDeviceId();
439 asynccallbackinfo->deviceId =
440 BluetoothHost::GetDefaultHost().GetRemoteDevice(deviceId, BT_TRANSPORT_BLE).GetDeviceName();
441 if (asynccallbackinfo->deviceId.empty()) {
442 asynccallbackinfo->promise.errorCode = CODE_FAILED;
443 } else {
444 asynccallbackinfo->promise.errorCode = CODE_SUCCESS;
445 }
446 }
447
AsyncCompleteCallbackGetDeviceName(napi_env env,napi_status status,void * data)448 void AsyncCompleteCallbackGetDeviceName(napi_env env, napi_status status, void *data)
449 {
450 HILOGI("napi_create_async_work complete start");
451 if (!data) {
452 HILOGE("Invalid async callback data");
453 return;
454 }
455 GattGetDeviceNameCallbackInfo *asynccallbackinfo = (GattGetDeviceNameCallbackInfo *)data;
456 napi_value result = nullptr;
457 if (asynccallbackinfo->promise.errorCode != CODE_SUCCESS) {
458 HILOGI("failed.");
459 result = NapiGetNull(env);
460 } else {
461 HILOGI("success.");
462 napi_create_string_utf8(env, asynccallbackinfo->deviceId.c_str(), asynccallbackinfo->deviceId.size(), &result);
463 }
464 ReturnCallbackPromise(env, asynccallbackinfo->promise, result);
465 if (asynccallbackinfo->promise.callback != nullptr) {
466 napi_delete_reference(env, asynccallbackinfo->promise.callback);
467 }
468 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
469 delete asynccallbackinfo;
470 asynccallbackinfo = nullptr;
471 HILOGI("napi_create_async_work complete end");
472 }
473
ParseAsyncCallbackParameters(napi_env env,napi_callback_info info,CallbackPromiseInfo & params)474 static napi_status ParseAsyncCallbackParameters(napi_env env, napi_callback_info info, CallbackPromiseInfo ¶ms)
475 {
476 HILOGI("enter");
477 size_t expectedArgsCount = ARGS_SIZE_ONE;
478 size_t argc = expectedArgsCount;
479 napi_value argv[ARGS_SIZE_ONE] = {nullptr};
480 NAPI_BT_CALL_RETURN(napi_get_cb_info(env, info, &argc, argv, nullptr, NULL));
481 NAPI_BT_RETURN_IF(argc != expectedArgsCount && argc != expectedArgsCount - CALLBACK_SIZE,
482 "Requires 0 or 1 arguments.", napi_invalid_arg);
483 if (argc == expectedArgsCount) {
484 NAPI_BT_CALL_RETURN(NapiIsFunction(env, argv[PARAM0]));
485 napi_create_reference(env, argv[PARAM0], 1, ¶ms.callback);
486 }
487 HILOGI("end");
488 return napi_ok;
489 }
490
GetDeviceName(napi_env env,napi_callback_info info)491 napi_value GetDeviceName(napi_env env, napi_callback_info info)
492 {
493 HILOGI("start");
494 GattGetDeviceNameCallbackInfo *asynccallbackinfo =
495 new GattGetDeviceNameCallbackInfo {.env = env, .asyncWork = nullptr};
496 auto status = ParseAsyncCallbackParameters(env, info, asynccallbackinfo->promise);
497 if (status != napi_ok) {
498 delete asynccallbackinfo;
499 asynccallbackinfo = nullptr;
500 }
501 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
502 napi_value promise = nullptr;
503 PaddingCallbackPromiseInfo(env, asynccallbackinfo->promise.callback, asynccallbackinfo->promise, promise);
504 napi_value resourceName = nullptr;
505 napi_create_string_latin1(env, "getDeviceName", NAPI_AUTO_LENGTH, &resourceName);
506 napi_create_async_work(
507 env,
508 nullptr,
509 resourceName,
510 [](napi_env env, void *data) {
511 HILOGI("napi_create_async_work start");
512 GattGetDeviceNameCallbackInfo *asynccallbackinfo = (GattGetDeviceNameCallbackInfo *)data;
513 GetDeviceNameSyncWorkStart(asynccallbackinfo);
514 },
515 AsyncCompleteCallbackGetDeviceName,
516 (void *)asynccallbackinfo,
517 &asynccallbackinfo->asyncWork);
518 NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork));
519 HILOGI("end");
520 if (asynccallbackinfo->promise.isCallback) {
521 return NapiGetUndefinedRet(env);
522 } else {
523 return promise;
524 }
525 }
526
GetRssiValueSyncWorkStart(GattGetRssiValueCallbackInfo * asynccallbackinfo)527 static void GetRssiValueSyncWorkStart(GattGetRssiValueCallbackInfo *asynccallbackinfo)
528 {
529 HILOGI("start");
530 std::string deviceId = GetGattClientDeviceId();
531 bool isResult = BluetoothHost::GetDefaultHost().GetRemoteDevice(deviceId, BT_TRANSPORT_BLE).ReadRemoteRssiValue();
532 if (!isResult) {
533 asynccallbackinfo->promise.errorCode = CODE_FAILED;
534 } else {
535 std::unique_lock<std::mutex> lock(asynccallbackinfo->mutexRssi);
536 std::shared_ptr<GattGetRssiValueCallbackInfo> callbackInfo(asynccallbackinfo,
537 [](GattGetRssiValueCallbackInfo *) {});
538 SetRssiValueCallbackInfo(callbackInfo);
539 if (asynccallbackinfo->cvfull.wait_for(lock, std::chrono::seconds(THREAD_WAIT_TIMEOUT)) ==
540 std::cv_status::timeout) {
541 HILOGI("ReadRemoteRssi timeout!");
542 }
543 }
544 }
545
AsyncCompleteCallbackGetRssiValue(napi_env env,napi_status status,void * data)546 void AsyncCompleteCallbackGetRssiValue(napi_env env, napi_status status, void *data)
547 {
548 HILOGI("napi_create_async_work complete start");
549 if (!data) {
550 HILOGE("Invalid async callback data");
551 return;
552 }
553 GattGetRssiValueCallbackInfo *asynccallbackinfo = (GattGetRssiValueCallbackInfo *)data;
554 napi_value result = nullptr;
555 if (asynccallbackinfo->promise.errorCode != CODE_SUCCESS) {
556 HILOGE("failed.");
557 result = NapiGetNull(env);
558 } else {
559 HILOGI("success.");
560 napi_create_int32(env, asynccallbackinfo->rssi, &result);
561 }
562
563 ReturnCallbackPromise(env, asynccallbackinfo->promise, result);
564 if (asynccallbackinfo->promise.callback != nullptr) {
565 napi_delete_reference(env, asynccallbackinfo->promise.callback);
566 }
567
568 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
569
570 delete asynccallbackinfo;
571 asynccallbackinfo = nullptr;
572 std::shared_ptr<GattGetRssiValueCallbackInfo> callbackInfo = nullptr;
573 SetRssiValueCallbackInfo(callbackInfo);
574 HILOGI("napi_create_async_work complete end");
575 }
576
GetRssiValue(napi_env env,napi_callback_info info)577 napi_value GetRssiValue(napi_env env, napi_callback_info info)
578 {
579 HILOGI("start");
580 GattGetRssiValueCallbackInfo *asynccallbackinfo =
581 new (std::nothrow) GattGetRssiValueCallbackInfo {.env = env, .asyncWork = nullptr};
582 auto status = ParseAsyncCallbackParameters(env, info, asynccallbackinfo->promise);
583 if (status != napi_ok) {
584 delete asynccallbackinfo;
585 asynccallbackinfo = nullptr;
586 }
587 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
588 napi_value promise = nullptr;
589 PaddingCallbackPromiseInfo(env, asynccallbackinfo->promise.callback, asynccallbackinfo->promise, promise);
590
591 napi_value resourceName = nullptr;
592 napi_create_string_latin1(env, "getRssiValue", NAPI_AUTO_LENGTH, &resourceName);
593 napi_create_async_work(
594 env,
595 nullptr,
596 resourceName,
597 [](napi_env env, void *data) {
598 HILOGI("napi_create_async_work start");
599 GattGetRssiValueCallbackInfo *asynccallbackinfo = (GattGetRssiValueCallbackInfo *)data;
600 GetRssiValueSyncWorkStart(asynccallbackinfo);
601 },
602 AsyncCompleteCallbackGetRssiValue,
603 (void *)asynccallbackinfo,
604 &asynccallbackinfo->asyncWork);
605 NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork));
606 HILOGI("end");
607 if (asynccallbackinfo->promise.isCallback) {
608 return NapiGetUndefinedRet(env);
609 } else {
610 return promise;
611 }
612 }
613
PropertyValueInit(napi_env env,napi_value exports)614 napi_value PropertyValueInit(napi_env env, napi_value exports)
615 {
616 HILOGI("start");
617 napi_value scanDutyObject = ScanDutyInit(env);
618 napi_value matchModeObject = MatchModeInit(env);
619 napi_value stateObj = StateChangeInit(env);
620 napi_value profileStateObj = ProfileStateInit(env);
621 napi_value scanModeObj = ScanModeInit(env);
622 napi_value bondStateObj = BondStateInit(env);
623 napi_value majorClassObj = MajorClassOfDeviceInit(env);
624 napi_value majorMinorClassObj = MajorMinorClassOfDeviceInit(env);
625 napi_property_descriptor exportFuncs[] = {
626 DECLARE_NAPI_PROPERTY("ScanDuty", scanDutyObject),
627 DECLARE_NAPI_PROPERTY("MatchMode", matchModeObject),
628 DECLARE_NAPI_PROPERTY("BluetoothState", stateObj),
629 DECLARE_NAPI_PROPERTY("ProfileConnectionState", profileStateObj),
630 DECLARE_NAPI_PROPERTY("ScanMode", scanModeObj),
631 DECLARE_NAPI_PROPERTY("BondState", bondStateObj),
632 DECLARE_NAPI_PROPERTY("MajorClass", majorClassObj),
633 DECLARE_NAPI_PROPERTY("MajorMinorClass", majorMinorClassObj),
634 };
635 napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs);
636 HILOGI("end");
637 return exports;
638 }
639
ScanDutyInit(napi_env env)640 napi_value ScanDutyInit(napi_env env)
641 {
642 HILOGI("enter");
643 napi_value scanDuty = nullptr;
644 napi_create_object(env, &scanDuty);
645 SetNamedPropertyByInteger(env, scanDuty, static_cast<int>(ScanDuty::SCAN_MODE_LOW_POWER), "SCAN_MODE_LOW_POWER");
646 SetNamedPropertyByInteger(env, scanDuty, static_cast<int>(ScanDuty::SCAN_MODE_BALANCED), "SCAN_MODE_BALANCED");
647 SetNamedPropertyByInteger(env, scanDuty, static_cast<int>(ScanDuty::SCAN_MODE_LOW_LATENCY),
648 "SCAN_MODE_LOW_LATENCY");
649 return scanDuty;
650 }
651
MatchModeInit(napi_env env)652 napi_value MatchModeInit(napi_env env)
653 {
654 HILOGI("enter");
655 napi_value matchMode = nullptr;
656 napi_create_object(env, &matchMode);
657 SetNamedPropertyByInteger(env, matchMode, static_cast<int>(MatchMode::MATCH_MODE_AGGRESSIVE),
658 "MATCH_MODE_AGGRESSIVE");
659 SetNamedPropertyByInteger(env, matchMode, static_cast<int>(MatchMode::MATCH_MODE_STICKY), "MATCH_MODE_STICKY");
660 return matchMode;
661 }
662
StateChangeInit(napi_env env)663 napi_value StateChangeInit(napi_env env)
664 {
665 HILOGI("enter");
666 napi_value state = nullptr;
667 napi_create_object(env, &state);
668 SetNamedPropertyByInteger(env, state, static_cast<int>(BluetoothState::STATE_OFF), "STATE_OFF");
669 SetNamedPropertyByInteger(env, state, static_cast<int>(BluetoothState::STATE_TURNING_ON), "STATE_TURNING_ON");
670 SetNamedPropertyByInteger(env, state, static_cast<int>(BluetoothState::STATE_ON), "STATE_ON");
671 SetNamedPropertyByInteger(env, state, static_cast<int>(BluetoothState::STATE_TURNING_OFF), "STATE_TURNING_OFF");
672 SetNamedPropertyByInteger(
673 env, state, static_cast<int>(BluetoothState::STATE_BLE_TURNING_ON), "STATE_BLE_TURNING_ON");
674 SetNamedPropertyByInteger(env, state, static_cast<int>(BluetoothState::STATE_BLE_ON), "STATE_BLE_ON");
675 SetNamedPropertyByInteger(
676 env, state, static_cast<int>(BluetoothState::STATE_BLE_TURNING_OFF), "STATE_BLE_TURNING_OFF");
677 return state;
678 }
679
ProfileStateInit(napi_env env)680 napi_value ProfileStateInit(napi_env env)
681 {
682 HILOGI("enter");
683 napi_value profileState = nullptr;
684 napi_create_object(env, &profileState);
685 SetNamedPropertyByInteger(env, profileState, ProfileConnectionState::STATE_DISCONNECTED, "STATE_DISCONNECTED");
686 SetNamedPropertyByInteger(env, profileState, ProfileConnectionState::STATE_CONNECTING, "STATE_CONNECTING");
687 SetNamedPropertyByInteger(env, profileState, ProfileConnectionState::STATE_CONNECTED, "STATE_CONNECTED");
688 SetNamedPropertyByInteger(env, profileState, ProfileConnectionState::STATE_DISCONNECTING, "STATE_DISCONNECTING");
689 return profileState;
690 }
691
ScanModeInit(napi_env env)692 napi_value ScanModeInit(napi_env env)
693 {
694 HILOGI("enter");
695 napi_value scanMode = nullptr;
696 napi_create_object(env, &scanMode);
697 SetNamedPropertyByInteger(env, scanMode, static_cast<int>(ScanMode::SCAN_MODE_NONE),
698 "SCAN_MODE_NONE");
699 SetNamedPropertyByInteger(env, scanMode, static_cast<int>(ScanMode::SCAN_MODE_CONNECTABLE),
700 "SCAN_MODE_CONNECTABLE");
701 SetNamedPropertyByInteger(env, scanMode, static_cast<int>(ScanMode::SCAN_MODE_GENERAL_DISCOVERABLE),
702 "SCAN_MODE_GENERAL_DISCOVERABLE");
703 SetNamedPropertyByInteger(env, scanMode, static_cast<int>(ScanMode::SCAN_MODE_LIMITED_DISCOVERABLE),
704 "SCAN_MODE_LIMITED_DISCOVERABLE");
705 SetNamedPropertyByInteger(env, scanMode, static_cast<int>(ScanMode::SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE),
706 "SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE");
707 SetNamedPropertyByInteger(env, scanMode, static_cast<int>(ScanMode::SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE),
708 "SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE");
709 return scanMode;
710 }
711
BondStateInit(napi_env env)712 napi_value BondStateInit(napi_env env)
713 {
714 HILOGI("enter");
715 napi_value bondState = nullptr;
716 napi_create_object(env, &bondState);
717 SetNamedPropertyByInteger(env, bondState, BondState::BOND_STATE_INVALID, "BOND_STATE_INVALID");
718 SetNamedPropertyByInteger(env, bondState, BondState::BOND_STATE_BONDING, "BOND_STATE_BONDING");
719 SetNamedPropertyByInteger(env, bondState, BondState::BOND_STATE_BONDED, "BOND_STATE_BONDED");
720 return bondState;
721 }
722
MajorClassOfDeviceInit(napi_env env)723 napi_value MajorClassOfDeviceInit(napi_env env)
724 {
725 HILOGI("enter");
726 napi_value majorClass = nullptr;
727 napi_create_object(env, &majorClass);
728 // MajorClass
729 SetNamedPropertyByInteger(env, majorClass, MajorClass::MAJOR_MISC, "MAJOR_MISC");
730 SetNamedPropertyByInteger(env, majorClass, MajorClass::MAJOR_COMPUTER, "MAJOR_COMPUTER");
731 SetNamedPropertyByInteger(env, majorClass, MajorClass::MAJOR_PHONE, "MAJOR_PHONE");
732 SetNamedPropertyByInteger(env, majorClass, MajorClass::MAJOR_NETWORKING, "MAJOR_NETWORKING");
733 SetNamedPropertyByInteger(env, majorClass, MajorClass::MAJOR_AUDIO_VIDEO, "MAJOR_AUDIO_VIDEO");
734 SetNamedPropertyByInteger(env, majorClass, MajorClass::MAJOR_PERIPHERAL, "MAJOR_PERIPHERAL");
735 SetNamedPropertyByInteger(env, majorClass, MajorClass::MAJOR_IMAGING, "MAJOR_IMAGING");
736 SetNamedPropertyByInteger(env, majorClass, MajorClass::MAJOR_WEARABLE, "MAJOR_WEARABLE");
737 SetNamedPropertyByInteger(env, majorClass, MajorClass::MAJOR_TOY, "MAJOR_TOY");
738 SetNamedPropertyByInteger(env, majorClass, MajorClass::MAJOR_HEALTH, "MAJOR_HEALTH");
739 SetNamedPropertyByInteger(env, majorClass, MajorClass::MAJOR_UNCATEGORIZED, "MAJOR_UNCATEGORIZED");
740 return majorClass;
741 }
742
MajorMinorClassOfDeviceInit(napi_env env)743 napi_value MajorMinorClassOfDeviceInit(napi_env env)
744 {
745 HILOGI("enter");
746 napi_value majorMinorClass = nullptr;
747 napi_create_object(env, &majorMinorClass);
748 // MajorMinorClass
749 // Computer Major Class
750 SetNamedPropertyByInteger(env, majorMinorClass,
751 static_cast<int>(MajorMinorClass::COMPUTER_UNCATEGORIZED), "COMPUTER_UNCATEGORIZED");
752 SetNamedPropertyByInteger(env, majorMinorClass,
753 static_cast<int>(MajorMinorClass::COMPUTER_DESKTOP), "COMPUTER_DESKTOP");
754 SetNamedPropertyByInteger(env, majorMinorClass,
755 static_cast<int>(MajorMinorClass::COMPUTER_SERVER), "COMPUTER_SERVER");
756 SetNamedPropertyByInteger(env, majorMinorClass,
757 static_cast<int>(MajorMinorClass::COMPUTER_LAPTOP), "COMPUTER_LAPTOP");
758 SetNamedPropertyByInteger(env, majorMinorClass,
759 static_cast<int>(MajorMinorClass::COMPUTER_HANDHELD_PC_PDA), "COMPUTER_HANDHELD_PC_PDA");
760 SetNamedPropertyByInteger(env, majorMinorClass,
761 static_cast<int>(MajorMinorClass::COMPUTER_PALM_SIZE_PC_PDA), "COMPUTER_PALM_SIZE_PC_PDA");
762 SetNamedPropertyByInteger(env, majorMinorClass,
763 static_cast<int>(MajorMinorClass::COMPUTER_WEARABLE), "COMPUTER_WEARABLE");
764 SetNamedPropertyByInteger(env, majorMinorClass,
765 static_cast<int>(MajorMinorClass::COMPUTER_TABLET), "COMPUTER_TABLET");
766 // Phone Major Class
767 SetNamedPropertyByInteger(env, majorMinorClass,
768 static_cast<int>(MajorMinorClass::PHONE_UNCATEGORIZED), "PHONE_UNCATEGORIZED");
769 SetNamedPropertyByInteger(env, majorMinorClass,
770 static_cast<int>(MajorMinorClass::PHONE_CELLULAR), "PHONE_CELLULAR");
771 SetNamedPropertyByInteger(env, majorMinorClass,
772 static_cast<int>(MajorMinorClass::PHONE_CORDLESS), "PHONE_CORDLESS");
773 SetNamedPropertyByInteger(env, majorMinorClass,
774 static_cast<int>(MajorMinorClass::PHONE_SMART), "PHONE_SMART");
775 SetNamedPropertyByInteger(env, majorMinorClass,
776 static_cast<int>(MajorMinorClass::PHONE_MODEM_OR_GATEWAY), "PHONE_MODEM_OR_GATEWAY");
777 SetNamedPropertyByInteger(env, majorMinorClass,
778 static_cast<int>(MajorMinorClass::PHONE_ISDN), "PHONE_ISDN");
779 // LAN/Network Access Point Major Class
780 SetNamedPropertyByInteger(env, majorMinorClass,
781 static_cast<int>(MajorMinorClass::NETWORK_FULLY_AVAILABLE), "NETWORK_FULLY_AVAILABLE");
782 SetNamedPropertyByInteger(env, majorMinorClass,
783 static_cast<int>(MajorMinorClass::NETWORK_1_TO_17_UTILIZED), "NETWORK_1_TO_17_UTILIZED");
784 SetNamedPropertyByInteger(env, majorMinorClass,
785 static_cast<int>(MajorMinorClass::NETWORK_17_TO_33_UTILIZED), "NETWORK_17_TO_33_UTILIZED");
786 SetNamedPropertyByInteger(env, majorMinorClass,
787 static_cast<int>(MajorMinorClass::NETWORK_33_TO_50_UTILIZED), "NETWORK_33_TO_50_UTILIZED");
788 SetNamedPropertyByInteger(env, majorMinorClass,
789 static_cast<int>(MajorMinorClass::NETWORK_60_TO_67_UTILIZED), "NETWORK_60_TO_67_UTILIZED");
790 SetNamedPropertyByInteger(env, majorMinorClass,
791 static_cast<int>(MajorMinorClass::NETWORK_67_TO_83_UTILIZED), "NETWORK_67_TO_83_UTILIZED");
792 SetNamedPropertyByInteger(env, majorMinorClass,
793 static_cast<int>(MajorMinorClass::NETWORK_83_TO_99_UTILIZED), "NETWORK_83_TO_99_UTILIZED");
794 SetNamedPropertyByInteger(env, majorMinorClass,
795 static_cast<int>(MajorMinorClass::NETWORK_NO_SERVICE), "NETWORK_NO_SERVICE");
796 // Audio/Video Major Class
797 SetNamedPropertyByInteger(env, majorMinorClass,
798 static_cast<int>(MajorMinorClass::AUDIO_VIDEO_UNCATEGORIZED), "AUDIO_VIDEO_UNCATEGORIZED");
799 SetNamedPropertyByInteger(env, majorMinorClass,
800 static_cast<int>(MajorMinorClass::AUDIO_VIDEO_WEARABLE_HEADSET), "AUDIO_VIDEO_WEARABLE_HEADSET");
801 SetNamedPropertyByInteger(env, majorMinorClass,
802 static_cast<int>(MajorMinorClass::AUDIO_VIDEO_HANDSFREE), "AUDIO_VIDEO_HANDSFREE");
803 SetNamedPropertyByInteger(env, majorMinorClass,
804 static_cast<int>(MajorMinorClass::AUDIO_VIDEO_MICROPHONE), "AUDIO_VIDEO_MICROPHONE");
805 SetNamedPropertyByInteger(env, majorMinorClass,
806 static_cast<int>(MajorMinorClass::AUDIO_VIDEO_LOUDSPEAKER), "AUDIO_VIDEO_LOUDSPEAKER");
807 SetNamedPropertyByInteger(env, majorMinorClass,
808 static_cast<int>(MajorMinorClass::AUDIO_VIDEO_HEADPHONES), "AUDIO_VIDEO_HEADPHONES");
809 SetNamedPropertyByInteger(env, majorMinorClass,
810 static_cast<int>(MajorMinorClass::AUDIO_VIDEO_PORTABLE_AUDIO), "AUDIO_VIDEO_PORTABLE_AUDIO");
811 SetNamedPropertyByInteger(env, majorMinorClass,
812 static_cast<int>(MajorMinorClass::AUDIO_VIDEO_CAR_AUDIO), "AUDIO_VIDEO_CAR_AUDIO");
813 SetNamedPropertyByInteger(env, majorMinorClass,
814 static_cast<int>(MajorMinorClass::AUDIO_VIDEO_SET_TOP_BOX), "AUDIO_VIDEO_SET_TOP_BOX");
815 SetNamedPropertyByInteger(env, majorMinorClass,
816 static_cast<int>(MajorMinorClass::AUDIO_VIDEO_HIFI_AUDIO), "AUDIO_VIDEO_HIFI_AUDIO");
817 SetNamedPropertyByInteger(env, majorMinorClass,
818 static_cast<int>(MajorMinorClass::AUDIO_VIDEO_VCR), "AUDIO_VIDEO_VCR");
819 SetNamedPropertyByInteger(env, majorMinorClass,
820 static_cast<int>(MajorMinorClass::AUDIO_VIDEO_VIDEO_CAMERA), "AUDIO_VIDEO_VIDEO_CAMERA");
821 SetNamedPropertyByInteger(env, majorMinorClass,
822 static_cast<int>(MajorMinorClass::AUDIO_VIDEO_CAMCORDER), "AUDIO_VIDEO_CAMCORDER");
823 SetNamedPropertyByInteger(env, majorMinorClass,
824 static_cast<int>(MajorMinorClass::AUDIO_VIDEO_VIDEO_MONITOR), "AUDIO_VIDEO_VIDEO_MONITOR");
825 SetNamedPropertyByInteger(env, majorMinorClass,
826 static_cast<int>(MajorMinorClass::AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER),
827 "AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER");
828 SetNamedPropertyByInteger(env, majorMinorClass,
829 static_cast<int>(MajorMinorClass::AUDIO_VIDEO_VIDEO_CONFERENCING), "AUDIO_VIDEO_VIDEO_CONFERENCING");
830 SetNamedPropertyByInteger(env, majorMinorClass,
831 static_cast<int>(MajorMinorClass::AUDIO_VIDEO_VIDEO_GAMING_TOY), "AUDIO_VIDEO_VIDEO_GAMING_TOY");
832 // Peripheral Major Class
833 SetNamedPropertyByInteger(env, majorMinorClass,
834 static_cast<int>(MajorMinorClass::PERIPHERAL_NON_KEYBOARD_NON_POINTING),
835 "PERIPHERAL_NON_KEYBOARD_NON_POINTING");
836 SetNamedPropertyByInteger(env, majorMinorClass,
837 static_cast<int>(MajorMinorClass::PERIPHERAL_KEYBOARD), "PERIPHERAL_KEYBOARD");
838 SetNamedPropertyByInteger(env, majorMinorClass,
839 static_cast<int>(MajorMinorClass::PERIPHERAL_POINTING_DEVICE), "PERIPHERAL_POINTING_DEVICE");
840 SetNamedPropertyByInteger(env, majorMinorClass,
841 static_cast<int>(MajorMinorClass::PERIPHERAL_KEYBOARD_POINTING), "PERIPHERAL_KEYBOARD_POINTING");
842 SetNamedPropertyByInteger(env, majorMinorClass,
843 static_cast<int>(MajorMinorClass::PERIPHERAL_UNCATEGORIZED), "PERIPHERAL_UNCATEGORIZED");
844 SetNamedPropertyByInteger(env, majorMinorClass,
845 static_cast<int>(MajorMinorClass::PERIPHERAL_JOYSTICK), "PERIPHERAL_JOYSTICK");
846 SetNamedPropertyByInteger(env, majorMinorClass,
847 static_cast<int>(MajorMinorClass::PERIPHERAL_GAMEPAD), "PERIPHERAL_GAMEPAD");
848 SetNamedPropertyByInteger(env, majorMinorClass,
849 static_cast<int>(MajorMinorClass::PERIPHERAL_REMOTE_CONTROL), "PERIPHERAL_REMOTE_CONTROL");
850 SetNamedPropertyByInteger(env, majorMinorClass,
851 static_cast<int>(MajorMinorClass::PERIPHERAL_SENSING_DEVICE), "PERIPHERAL_SENSING_DEVICE");
852 SetNamedPropertyByInteger(env, majorMinorClass,
853 static_cast<int>(MajorMinorClass::PERIPHERAL_DIGITIZER_TABLET), "PERIPHERAL_DIGITIZER_TABLET");
854 SetNamedPropertyByInteger(env, majorMinorClass,
855 static_cast<int>(MajorMinorClass::PERIPHERAL_CARD_READER), "PERIPHERAL_CARD_READER");
856 SetNamedPropertyByInteger(env, majorMinorClass,
857 static_cast<int>(MajorMinorClass::PERIPHERAL_DIGITAL_PEN), "PERIPHERAL_DIGITAL_PEN");
858 SetNamedPropertyByInteger(env, majorMinorClass,
859 static_cast<int>(MajorMinorClass::PERIPHERAL_SCANNER_RFID), "PERIPHERAL_SCANNER_RFID");
860 SetNamedPropertyByInteger(env, majorMinorClass,
861 static_cast<int>(MajorMinorClass::PERIPHERAL_GESTURAL_INPUT), "PERIPHERAL_GESTURAL_INPUT");
862 // Imaging Major Class
863 SetNamedPropertyByInteger(env, majorMinorClass,
864 static_cast<int>(MajorMinorClass::IMAGING_UNCATEGORIZED), "IMAGING_UNCATEGORIZED");
865 SetNamedPropertyByInteger(env, majorMinorClass,
866 static_cast<int>(MajorMinorClass::IMAGING_DISPLAY), "IMAGING_DISPLAY");
867 SetNamedPropertyByInteger(env, majorMinorClass,
868 static_cast<int>(MajorMinorClass::IMAGING_CAMERA), "IMAGING_CAMERA");
869 SetNamedPropertyByInteger(env, majorMinorClass,
870 static_cast<int>(MajorMinorClass::IMAGING_SCANNER), "IMAGING_SCANNER");
871 SetNamedPropertyByInteger(env, majorMinorClass,
872 static_cast<int>(MajorMinorClass::IMAGING_PRINTER), "IMAGING_PRINTER");
873 // Wearable Major Class
874 SetNamedPropertyByInteger(env, majorMinorClass,
875 static_cast<int>(MajorMinorClass::WEARABLE_UNCATEGORIZED), "WEARABLE_UNCATEGORIZED");
876 SetNamedPropertyByInteger(env, majorMinorClass,
877 static_cast<int>(MajorMinorClass::WEARABLE_WRIST_WATCH), "WEARABLE_WRIST_WATCH");
878 SetNamedPropertyByInteger(env, majorMinorClass,
879 static_cast<int>(MajorMinorClass::WEARABLE_PAGER), "WEARABLE_PAGER");
880 SetNamedPropertyByInteger(env, majorMinorClass,
881 static_cast<int>(MajorMinorClass::WEARABLE_JACKET), "WEARABLE_JACKET");
882 SetNamedPropertyByInteger(env, majorMinorClass,
883 static_cast<int>(MajorMinorClass::WEARABLE_HELMET), "WEARABLE_HELMET");
884 SetNamedPropertyByInteger(env, majorMinorClass,
885 static_cast<int>(MajorMinorClass::WEARABLE_GLASSES), "WEARABLE_GLASSES");
886 // Minor Device Class field - Toy Major Class
887 SetNamedPropertyByInteger(env, majorMinorClass,
888 static_cast<int>(MajorMinorClass::TOY_UNCATEGORIZED), "TOY_UNCATEGORIZED");
889 SetNamedPropertyByInteger(env, majorMinorClass,
890 static_cast<int>(MajorMinorClass::TOY_ROBOT), "TOY_ROBOT");
891 SetNamedPropertyByInteger(env, majorMinorClass,
892 static_cast<int>(MajorMinorClass::TOY_VEHICLE), "TOY_VEHICLE");
893 SetNamedPropertyByInteger(env, majorMinorClass,
894 static_cast<int>(MajorMinorClass::TOY_DOLL_ACTION_FIGURE), "TOY_DOLL_ACTION_FIGURE");
895 SetNamedPropertyByInteger(env, majorMinorClass,
896 static_cast<int>(MajorMinorClass::TOY_CONTROLLER), "TOY_CONTROLLER");
897 SetNamedPropertyByInteger(env, majorMinorClass,
898 static_cast<int>(MajorMinorClass::TOY_GAME), "TOY_GAME");
899 // Minor Device Class field - Health
900 SetNamedPropertyByInteger(env, majorMinorClass,
901 static_cast<int>(MajorMinorClass::HEALTH_UNCATEGORIZED), "HEALTH_UNCATEGORIZED");
902 SetNamedPropertyByInteger(env, majorMinorClass,
903 static_cast<int>(MajorMinorClass::HEALTH_BLOOD_PRESSURE), "HEALTH_BLOOD_PRESSURE");
904 SetNamedPropertyByInteger(env, majorMinorClass,
905 static_cast<int>(MajorMinorClass::HEALTH_THERMOMETER), "HEALTH_THERMOMETER");
906 SetNamedPropertyByInteger(env, majorMinorClass,
907 static_cast<int>(MajorMinorClass::HEALTH_WEIGHING), "HEALTH_WEIGHING");
908 SetNamedPropertyByInteger(env, majorMinorClass,
909 static_cast<int>(MajorMinorClass::HEALTH_GLUCOSE), "HEALTH_GLUCOSE");
910 SetNamedPropertyByInteger(env, majorMinorClass,
911 static_cast<int>(MajorMinorClass::HEALTH_PULSE_OXIMETER), "HEALTH_PULSE_OXIMETER");
912 SetNamedPropertyByInteger(env, majorMinorClass,
913 static_cast<int>(MajorMinorClass::HEALTH_PULSE_RATE), "HEALTH_PULSE_RATE");
914 SetNamedPropertyByInteger(env, majorMinorClass,
915 static_cast<int>(MajorMinorClass::HEALTH_DATA_DISPLAY), "HEALTH_DATA_DISPLAY");
916 SetNamedPropertyByInteger(env, majorMinorClass,
917 static_cast<int>(MajorMinorClass::HEALTH_STEP_COUNTER), "HEALTH_STEP_COUNTER");
918 SetNamedPropertyByInteger(env, majorMinorClass,
919 static_cast<int>(MajorMinorClass::HEALTH_BODY_COMPOSITION_ANALYZER), "HEALTH_BODY_COMPOSITION_ANALYZER");
920 SetNamedPropertyByInteger(env, majorMinorClass,
921 static_cast<int>(MajorMinorClass::HEALTH_PEAK_FLOW_MOITOR), "HEALTH_PEAK_FLOW_MOITOR");
922 SetNamedPropertyByInteger(env, majorMinorClass,
923 static_cast<int>(MajorMinorClass::HEALTH_MEDICATION_MONITOR), "HEALTH_MEDICATION_MONITOR");
924 SetNamedPropertyByInteger(env, majorMinorClass,
925 static_cast<int>(MajorMinorClass::HEALTH_KNEE_PROSTHESIS), "HEALTH_KNEE_PROSTHESIS");
926 SetNamedPropertyByInteger(env, majorMinorClass,
927 static_cast<int>(MajorMinorClass::HEALTH_ANKLE_PROSTHESIS), "HEALTH_ANKLE_PROSTHESIS");
928 SetNamedPropertyByInteger(env, majorMinorClass,
929 static_cast<int>(MajorMinorClass::HEALTH_GENERIC_HEALTH_MANAGER), "HEALTH_GENERIC_HEALTH_MANAGER");
930 SetNamedPropertyByInteger(env, majorMinorClass,
931 static_cast<int>(MajorMinorClass::HEALTH_PERSONAL_MOBILITY_DEVICE), "HEALTH_PERSONAL_MOBILITY_DEVICE");
932 return majorMinorClass;
933 }
934
GetProfileConnState(napi_env env,napi_callback_info info)935 napi_value GetProfileConnState(napi_env env, napi_callback_info info)
936 {
937 HILOGI("enter");
938 int profileId = 0;
939 bool checkRet = CheckProfileIdParam(env, info, profileId);
940 NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
941
942 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
943 int state = static_cast<int>(BTConnectState::DISCONNECTED);
944 int32_t err = host->GetBtProfileConnState(GetProfileId(profileId), state);
945 int status = GetProfileConnectionState(state);
946 napi_value ret = nullptr;
947 napi_create_int32(env, status, &ret);
948 NAPI_BT_ASSERT_RETURN(env, err == BT_SUCCESS, err, ret);
949 HILOGI("status: %{public}d", status);
950 return ret;
951 }
952
AddDiscoveryDevice(std::shared_ptr<BluetoothRemoteDevice> & device)953 void AddDiscoveryDevice(std::shared_ptr<BluetoothRemoteDevice> &device)
954 {
955 std::lock_guard<std::mutex> lock(deviceMutex);
956 for (auto dev : g_DiscoveryDevices) {
957 if (device->GetDeviceAddr().compare(dev->GetDeviceAddr()) == 0) {
958 return;
959 }
960 }
961 g_DiscoveryDevices.push_back(device);
962 }
963
ClearDiscoveryDevice()964 void ClearDiscoveryDevice()
965 {
966 std::lock_guard<std::mutex> lock(deviceMutex);
967 g_DiscoveryDevices.clear();
968 }
969
GetDeviceTransport(std::string & device)970 int GetDeviceTransport(std::string &device)
971 {
972 std::lock_guard<std::mutex> lock(deviceMutex);
973 for (auto dev : g_DiscoveryDevices) {
974 if (device.compare(dev->GetDeviceAddr()) == 0) {
975 return dev->GetTransportType();
976 }
977 }
978
979 return BT_TRANSPORT_BREDR;
980 }
981 } // namespace Bluetooth
982 } // namespace OHOS