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