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 #ifndef NAPI_BLUETOOTH_UTILS_H
16 #define NAPI_BLUETOOTH_UTILS_H
17
18 #include "bluetooth_gatt_characteristic.h"
19 #include "bluetooth_gatt_client.h"
20 #include "bluetooth_gatt_descriptor.h"
21 #include "bluetooth_gatt_server.h"
22 #include "bluetooth_gatt_service.h"
23 #include "bluetooth_log.h"
24 #include "bluetooth_opp.h"
25 #include "bluetooth_remote_device.h"
26 #include "napi/native_api.h"
27 #include "napi/native_node_api.h"
28
29 #include <atomic>
30 #include <condition_variable>
31 #include <mutex>
32 #include <cstdint>
33 #include <string>
34 #include <vector>
35
36 #include "uv.h"
37
38 #include "bluetooth_socket.h"
39
40 namespace OHOS {
41 namespace Bluetooth {
42 constexpr size_t CALLBACK_SIZE = 1;
43 constexpr size_t ARGS_SIZE_ZERO = 0;
44 constexpr size_t ARGS_SIZE_ONE = 1;
45 constexpr size_t ARGS_SIZE_TWO = 2;
46 constexpr size_t ARGS_SIZE_THREE = 3;
47 constexpr size_t ARGS_SIZE_FOUR = 4;
48 constexpr int32_t DEFAULT_INT32 = 0;
49 constexpr int32_t PARAM0 = 0;
50 constexpr int32_t PARAM1 = 1;
51 constexpr int32_t PARAM2 = 2;
52 constexpr int32_t CODE_SUCCESS = 0;
53 constexpr int32_t CODE_FAILED = -1;
54 constexpr int ASYNC_IDLE = 0;
55 constexpr int ASYNC_START = 1;
56 constexpr int ASYNC_DONE = 2;
57 constexpr int32_t THREAD_WAIT_TIMEOUT = 5;
58 constexpr int32_t BLUETOOTH_DEVICE_FIND_TYPE = 1;
59 constexpr int32_t STATE_CHANGE_TYPE = 2;
60 constexpr int32_t PIN_REQUEST_TYPE = 3;
61 constexpr int32_t BOND_STATE_CHANGE_TYPE = 4;
62 constexpr int32_t BLE_DEVICE_FIND_TYPE = 5;
63
64 constexpr uint32_t INVALID_REF_COUNT = 0xFF;
65
66 struct ServerResponse {
67 std::string deviceId = "";
68 int transId = 0;
69 int status = 0;
70 int offset = 0;
71 uint8_t *value = nullptr;
72 int length = 0;
SetValueServerResponse73 void SetValue(uint8_t *values, size_t len)
74 {
75 HILOGI("GattCharacteristic::SetValue starts");
76 if (value != nullptr) {
77 value = nullptr;
78 }
79
80 length = len;
81 value = values;
82 }
83 };
84
85 struct SppOption {
86 std::string uuid_ = "";
87 bool secure_ = false;
88 SppSocketType type_;
89 };
90
91 const std::string REGISTER_DEVICE_FIND_TYPE = "bluetoothDeviceFind";
92 const std::string REGISTER_STATE_CHANGE_TYPE = "stateChange";
93 const std::string REGISTER_PIN_REQUEST_TYPE = "pinRequired";
94 const std::string REGISTER_BOND_STATE_TYPE = "bondStateChange";
95 const std::string REGISTER_BLE_FIND_DEVICE_TYPE = "BLEDeviceFind";
96 const std::string REGISTER_SYS_BLE_SCAN_TYPE = "sysBLEScan";
97 const std::string REGISTER_SYS_BLE_FIND_DEVICE_TYPE = "sysBLEDeviceFonud";
98
99 const std::string INVALID_DEVICE_ID = "00:00:00:00:00:00";
100 const std::string INVALID_PIN_CODE = "000000";
101
102 bool ParseString(napi_env env, std::string ¶m, napi_value args);
103 bool ParseInt32(napi_env env, int32_t ¶m, napi_value args);
104 bool ParseBool(napi_env env, bool ¶m, napi_value args);
105 bool ParseArrayBuffer(napi_env env, uint8_t **data, size_t &size, napi_value args);
106 napi_value GetCallbackErrorValue(napi_env env, int errCode);
107
108 napi_status ConvertStringVectorToJS(napi_env env, napi_value result, std::vector<std::string> &stringVector);
109
110 void ConvertGattServiceToJS(napi_env env, napi_value result, GattService &service);
111 void ConvertGattServiceVectorToJS(napi_env env, napi_value result, std::vector<GattService> &services);
112
113 void ConvertBLECharacteristicToJS(napi_env env, napi_value result, GattCharacteristic &characteristic);
114 void ConvertBLECharacteristicVectorToJS(
115 napi_env env, napi_value result, std::vector<GattCharacteristic> &characteristics);
116
117 void ConvertBLEDescriptorToJS(napi_env env, napi_value result, GattDescriptor &descriptor);
118 void ConvertBLEDescriptorVectorToJS(napi_env env, napi_value result, std::vector<GattDescriptor> &descriptors);
119
120 void ConvertCharacteristicReadReqToJS(napi_env env, napi_value result, const std::string &device,
121 GattCharacteristic &characteristic, int requestId);
122
123 void ConvertCharacteristicWriteReqToJS(napi_env env, napi_value result, const std::string &device,
124 GattCharacteristic &characteristic, int requestId);
125
126 void ConvertDescriptorReadReqToJS(
127 napi_env env, napi_value result, const std::string &device, GattDescriptor &descriptor, int requestId);
128
129 void ConvertDescriptorWriteReqToJS(
130 napi_env env, napi_value result, const std::string &device, GattDescriptor &descriptor, int requestId);
131 void ConvertStateChangeParamToJS(napi_env env, napi_value result, const std::string &device, int state);
132 void ConvertScoStateChangeParamToJS(napi_env env, napi_value result, const std::string &device, int state);
133
134 void ConvertOppTransferInformationToJS(napi_env env,
135 napi_value result, BluetoothOppTransferInformation& transferInformation);
136
137 bool GetServiceVectorFromJS(napi_env env, napi_value object, std::vector<GattService> &services,
138 std::shared_ptr<GattServer> server, std::shared_ptr<GattClient> client);
139 GattService *GetServiceFromJS(
140 napi_env env, napi_value object, std::shared_ptr<GattServer> server, std::shared_ptr<GattClient> client);
141
142 bool GetCharacteristicVectorFromJS(napi_env env, napi_value object, std::vector<GattCharacteristic> &characteristics,
143 std::shared_ptr<GattServer> server, std::shared_ptr<GattClient> client);
144 GattCharacteristic *GetCharacteristicFromJS(
145 napi_env env, napi_value object, std::shared_ptr<GattServer> server, std::shared_ptr<GattClient> client);
146
147 bool GetDescriptorVectorFromJS(napi_env env, napi_value object, std::vector<GattDescriptor> &descriptors);
148 GattDescriptor *GetDescriptorFromJS(
149 napi_env env, napi_value object, std::shared_ptr<GattServer> server, std::shared_ptr<GattClient> client);
150
151 ServerResponse GetServerResponseFromJS(napi_env env, napi_value object);
152 std::shared_ptr<SppOption> GetSppOptionFromJS(napi_env env, napi_value object);
153
154 void SetNamedPropertyByInteger(napi_env env, napi_value dstObj, int32_t objName, const char *propName);
155 napi_value NapiGetNull(napi_env env);
156 napi_value NapiGetBooleanFalse(napi_env env);
157 napi_value NapiGetBooleanTrue(napi_env env);
158 napi_value NapiGetBooleanRet(napi_env env, bool ret);
159 napi_value NapiGetUndefinedRet(napi_env env);
160 napi_value NapiGetInt32Ret(napi_env env, int32_t res);
161 napi_value RegisterObserver(napi_env env, napi_callback_info info);
162 napi_value DeregisterObserver(napi_env env, napi_callback_info info);
163
164 int GetProfileConnectionState(int state);
165 int GetScoConnectionState(int state);
166 uint32_t GetProfileId(int profile);
167
168 struct AsyncCallbackInfo {
169 napi_env env_;
170 napi_async_work asyncWork_;
171 napi_deferred deferred_;
172 napi_ref callback_ = 0;
173 int errorCode_ = 0;
174 };
175
176 struct ReadCharacteristicValueCallbackInfo : public AsyncCallbackInfo {
177 GattCharacteristic *inputCharacteristic_ = nullptr;
178 const GattCharacteristic *outputCharacteristic_ = nullptr;
179 int ret = -1;
180 std::shared_ptr<GattClient> client_ = nullptr;
181 int asyncState_ = ASYNC_IDLE;
182 };
183
184 struct ReadDescriptorValueCallbackInfo : public AsyncCallbackInfo {
185 GattDescriptor *inputDescriptor_ = nullptr;
186 const GattDescriptor *outputDescriptor_ = nullptr;
187 int ret = -1;
188 std::shared_ptr<GattClient> client_ = nullptr;
189 int asyncState_ = ASYNC_IDLE;
190 };
191
192 struct GetServiceCallbackInfo : public AsyncCallbackInfo {
193 std::vector<GattService> services_;
194 std::shared_ptr<GattClient> client_ = nullptr;
195 };
196
197 struct SppListenCallbackInfo : public AsyncCallbackInfo {
198 std::shared_ptr<SppServerSocket> server_ = nullptr;
199 std::string name_ = "";
200 std::shared_ptr<SppOption> sppOption_;
201 };
202
203 struct SppAcceptCallbackInfo : public AsyncCallbackInfo {
204 std::shared_ptr<SppServerSocket> server_ = nullptr;
205 std::shared_ptr<SppClientSocket> client_ = nullptr;
206 };
207
208 struct SppConnectCallbackInfo : public AsyncCallbackInfo {
209 std::shared_ptr<SppClientSocket> client_ = nullptr;
210 std::string deviceId_ = "";
211 std::shared_ptr<BluetoothRemoteDevice> device_ = nullptr;
212 std::shared_ptr<SppOption> sppOption_ = nullptr;
213 };
214
215 struct CallbackPromiseInfo {
216 napi_ref callback = nullptr;
217 napi_deferred deferred = nullptr;
218 bool isCallback = false;
219 int errorCode = CODE_FAILED;
220 };
221
222 struct GattGetDeviceNameCallbackInfo {
223 napi_env env = nullptr;
224 napi_async_work asyncWork = nullptr;
225 std::string deviceId = "";
226 CallbackPromiseInfo promise;
227 };
228
229 struct GattGetRssiValueCallbackInfo {
230 napi_env env = nullptr;
231 napi_async_work asyncWork = nullptr;
232 int32_t rssi = 0;
233 std::mutex mutexRssi {};
234 std::condition_variable cvfull {};
235 CallbackPromiseInfo promise;
236 };
237
238 struct BluetoothCallbackInfo {
239 napi_env env_;
240 napi_ref callback_ = 0;
241 int state_;
242 std::string deviceId_;
243 int info_;
244 };
245
246 struct GattCharacteristicCallbackInfo : public BluetoothCallbackInfo {
247 GattCharacteristic characteristic_ = {UUID::FromString("0"), 0, 0};
248 };
249
250 struct GattDescriptorCallbackInfo : public BluetoothCallbackInfo {
251 GattDescriptor descriptor_ = {UUID::FromString("0"), 0};
252 };
253
254 struct BufferCallbackInfo : public BluetoothCallbackInfo {
255 char buffer_[1024];
256 };
257
258 struct TransforInformationCallbackInfo : public BluetoothCallbackInfo {
259 BluetoothOppTransferInformation information_;
260 };
261
262 namespace {
263 using sysBLEMap = std::map<std::string, std::array<std::shared_ptr<BluetoothCallbackInfo>, ARGS_SIZE_THREE>>;
264 sysBLEMap g_sysBLEObserver;
265 std::mutex g_sysBLEObserverMutex;
266 std::map<std::string, std::shared_ptr<BluetoothCallbackInfo>> g_Observer;
267 std::mutex g_observerMutex;
268 std::shared_ptr<GattGetRssiValueCallbackInfo> callbackInfo = nullptr;
269 std::string deviceAddr;
270 std::atomic<bool> isCurrentAppOperate(false);
271 } // namespace
272 std::map<std::string, std::shared_ptr<BluetoothCallbackInfo>> GetObserver();
273 const sysBLEMap &GetSysBLEObserver();
274 void SetGattClientDeviceId(const std::string &deviceId);
275 std::string GetGattClientDeviceId();
276
277 void SetRssiValueCallbackInfo(std::shared_ptr<GattGetRssiValueCallbackInfo> &callback);
278 std::shared_ptr<GattGetRssiValueCallbackInfo> GetRssiValueCallbackInfo();
279
280 void SetCurrentAppOperate(const bool &isCurrentApp);
281 bool GetCurrentAppOperate();
282 void RegisterSysBLEObserver(const std::shared_ptr<BluetoothCallbackInfo> &, int32_t, const std::string &);
283 void UnregisterSysBLEObserver(const std::string &);
284 std::shared_ptr<BluetoothCallbackInfo> GetCallbackInfoByType(const std::string type);
285
286 struct ScanFilter {
287 std::string deviceId; // The name of a BLE peripheral device
288 std::string name; // The name of a BLE peripheral device
289 UUID serviceUuid; // The service UUID of a BLE peripheral device
290 UUID serviceUuidMask;
291 UUID serviceSolicitationUuid;
292 UUID serviceSolicitationUuidMask;
293
294 std::vector<uint8_t> serviceData;
295 std::vector<uint8_t> serviceDataMask;
296
297 uint16_t manufacturerId = 0;
298 std::vector<uint8_t> manufactureData;
299 std::vector<uint8_t> manufactureDataMask;
300 };
301
302 enum MatchMode {
303 MATCH_MODE_AGGRESSIVE = 1, // aggressive mode
304 MATCH_MODE_STICKY = 2 // sticky mode
305 };
306
307 enum class ScanDuty {
308 SCAN_MODE_LOW_POWER = 0, // low power mode */
309 SCAN_MODE_BALANCED = 1, // balanced power mode
310 SCAN_MODE_LOW_LATENCY = 2 // Scan using highest duty cycle
311 };
312
313 struct ScanOptions {
314 int32_t interval; // Time of delay for reporting the scan result
315 ScanDuty dutyMode = ScanDuty::SCAN_MODE_LOW_POWER; // Bluetooth LE scan mode
316 MatchMode MatchMode = MatchMode::MATCH_MODE_AGGRESSIVE; // Match mode for Bluetooth LE scan filters hardware match
317 };
318
319 struct ScanResult {
320 std::string deviceId; // Address of the scanned device
321 int32_t rssi; // RSSI of the remote device
322 std::vector<uint8_t> data; // The raw data of broadcast packet
323 };
324
325 struct NapiAdvManufactureData {
326 uint16_t id = 0;
327 std::string value {};
328 };
329
330 struct NapiAdvServiceData {
331 std::string uuid {};
332 std::vector<uint8_t> value {};
333 };
334
335 struct NapiNotifyCharacteristic {
336 UUID serviceUuid;
337 UUID characterUuid;
338 std::vector<uint8_t> characterValue {};
339 bool confirm;
340 };
341
342 struct NapiGattsServerResponse {
343 std::string deviceId {};
344 int transId;
345 int status;
346 int offset;
347 std::vector<uint8_t> value;
348 };
349
350 enum ProfileConnectionState {
351 STATE_DISCONNECTED = 0, // the current profile is disconnected
352 STATE_CONNECTING = 1, // the current profile is being connected
353 STATE_CONNECTED = 2, // the current profile is connected
354 STATE_DISCONNECTING = 3 // the current profile is being disconnected
355 };
356
357 enum ScoState {
358 SCO_DISCONNECTED,
359 SCO_CONNECTING,
360 SCO_DISCONNECTING,
361 SCO_CONNECTED
362 };
363
364 enum class BluetoothState {
365 /** Indicates the local Bluetooth is off */
366 STATE_OFF = 0,
367 /** Indicates the local Bluetooth is turning on */
368 STATE_TURNING_ON = 1,
369 /** Indicates the local Bluetooth is on, and ready for use */
370 STATE_ON = 2,
371 /** Indicates the local Bluetooth is turning off */
372 STATE_TURNING_OFF = 3,
373 /** Indicates the local Bluetooth is turning LE mode on */
374 STATE_BLE_TURNING_ON = 4,
375 /** Indicates the local Bluetooth is in LE only mode */
376 STATE_BLE_ON = 5,
377 /** Indicates the local Bluetooth is turning off LE only mode */
378 STATE_BLE_TURNING_OFF = 6
379 };
380
381 enum BondState {
382 BOND_STATE_INVALID = 0,
383 BOND_STATE_BONDING = 1,
384 BOND_STATE_BONDED = 2
385 };
386
387 enum class ScanMode {
388 SCAN_MODE_NONE = 0,
389 SCAN_MODE_CONNECTABLE = 1,
390 SCAN_MODE_GENERAL_DISCOVERABLE = 2,
391 SCAN_MODE_LIMITED_DISCOVERABLE = 3,
392 SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE = 4,
393 SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE = 5
394 };
395
396 enum MajorClass {
397 MAJOR_MISC = 0x0000,
398 MAJOR_COMPUTER = 0x0100,
399 MAJOR_PHONE = 0x0200,
400 MAJOR_NETWORKING = 0x0300,
401 MAJOR_AUDIO_VIDEO = 0x0400,
402 MAJOR_PERIPHERAL = 0x0500,
403 MAJOR_IMAGING = 0x0600,
404 MAJOR_WEARABLE = 0x0700,
405 MAJOR_TOY = 0x0800,
406 MAJOR_HEALTH = 0x0900,
407 MAJOR_UNCATEGORIZED = 0x1F00
408 };
409
410 enum MajorMinorClass {
411 // The Minor Device Class field
412 // Computer Major Class
413 COMPUTER_UNCATEGORIZED = 0x0100,
414 COMPUTER_DESKTOP = 0x0104,
415 COMPUTER_SERVER = 0x0108,
416 COMPUTER_LAPTOP = 0x010C,
417 COMPUTER_HANDHELD_PC_PDA = 0x0110,
418 COMPUTER_PALM_SIZE_PC_PDA = 0x0114,
419 COMPUTER_WEARABLE = 0x0118,
420 COMPUTER_TABLET = 0x011C,
421
422 // Phone Major Class
423 PHONE_UNCATEGORIZED = 0x0200,
424 PHONE_CELLULAR = 0x0204,
425 PHONE_CORDLESS = 0x0208,
426 PHONE_SMART = 0x020C,
427 PHONE_MODEM_OR_GATEWAY = 0x0210,
428 PHONE_ISDN = 0x0214,
429
430 // LAN/Network Access Point Major Class
431 NETWORK_FULLY_AVAILABLE = 0x0300,
432 NETWORK_1_TO_17_UTILIZED = 0x0320,
433 NETWORK_17_TO_33_UTILIZED = 0x0340,
434 NETWORK_33_TO_50_UTILIZED = 0x0360,
435 NETWORK_60_TO_67_UTILIZED = 0x0380,
436 NETWORK_67_TO_83_UTILIZED = 0x03A0,
437 NETWORK_83_TO_99_UTILIZED = 0x03C0,
438 NETWORK_NO_SERVICE = 0x03E0,
439
440 // Audio/Video Major Class
441 AUDIO_VIDEO_UNCATEGORIZED = 0x0400,
442 AUDIO_VIDEO_WEARABLE_HEADSET = 0x0404,
443 AUDIO_VIDEO_HANDSFREE = 0x0408,
444 AUDIO_VIDEO_MICROPHONE = 0x0410,
445 AUDIO_VIDEO_LOUDSPEAKER = 0x0414,
446 AUDIO_VIDEO_HEADPHONES = 0x0418,
447 AUDIO_VIDEO_PORTABLE_AUDIO = 0x041C,
448 AUDIO_VIDEO_CAR_AUDIO = 0x0420,
449 AUDIO_VIDEO_SET_TOP_BOX = 0x0424,
450 AUDIO_VIDEO_HIFI_AUDIO = 0x0428,
451 AUDIO_VIDEO_VCR = 0x042C,
452 AUDIO_VIDEO_VIDEO_CAMERA = 0x0430,
453 AUDIO_VIDEO_CAMCORDER = 0x0434,
454 AUDIO_VIDEO_VIDEO_MONITOR = 0x0438,
455 AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER = 0x043C,
456 AUDIO_VIDEO_VIDEO_CONFERENCING = 0x0440,
457 AUDIO_VIDEO_VIDEO_GAMING_TOY = 0x0448,
458
459 // Peripheral Major Class
460 PERIPHERAL_NON_KEYBOARD_NON_POINTING = 0x0500,
461 PERIPHERAL_KEYBOARD = 0x0540,
462 PERIPHERAL_POINTING_DEVICE = 0x0580,
463 PERIPHERAL_KEYBOARD_POINTING = 0x05C0,
464 PERIPHERAL_UNCATEGORIZED = 0x0500,
465 PERIPHERAL_JOYSTICK = 0x0504,
466 PERIPHERAL_GAMEPAD = 0x0508,
467 PERIPHERAL_REMOTE_CONTROL = 0x05C0,
468 PERIPHERAL_SENSING_DEVICE = 0x0510,
469 PERIPHERAL_DIGITIZER_TABLET = 0x0514,
470 PERIPHERAL_CARD_READER = 0x0518,
471 PERIPHERAL_DIGITAL_PEN = 0x051C,
472 PERIPHERAL_SCANNER_RFID = 0x0520,
473 PERIPHERAL_GESTURAL_INPUT = 0x0522,
474
475 // Imaging Major Class
476 IMAGING_UNCATEGORIZED = 0x0600,
477 IMAGING_DISPLAY = 0x0610,
478 IMAGING_CAMERA = 0x0620,
479 IMAGING_SCANNER = 0x0640,
480 IMAGING_PRINTER = 0x0680,
481
482 // Wearable Major Class
483 WEARABLE_UNCATEGORIZED = 0x0700,
484 WEARABLE_WRIST_WATCH = 0x0704,
485 WEARABLE_PAGER = 0x0708,
486 WEARABLE_JACKET = 0x070C,
487 WEARABLE_HELMET = 0x0710,
488 WEARABLE_GLASSES = 0x0714,
489
490 // Minor Device Class field - Toy Major Class
491 TOY_UNCATEGORIZED = 0x0800,
492 TOY_ROBOT = 0x0804,
493 TOY_VEHICLE = 0x0808,
494 TOY_DOLL_ACTION_FIGURE = 0x080C,
495 TOY_CONTROLLER = 0x0810,
496 TOY_GAME = 0x0814,
497
498 // Minor Device Class field - Health
499 HEALTH_UNCATEGORIZED = 0x0900,
500 HEALTH_BLOOD_PRESSURE = 0x0904,
501 HEALTH_THERMOMETER = 0x0908,
502 HEALTH_WEIGHING = 0x090C,
503 HEALTH_GLUCOSE = 0x0910,
504 HEALTH_PULSE_OXIMETER = 0x0914,
505 HEALTH_PULSE_RATE = 0x0918,
506 HEALTH_DATA_DISPLAY = 0x091C,
507 HEALTH_STEP_COUNTER = 0x0920,
508 HEALTH_BODY_COMPOSITION_ANALYZER = 0x0924,
509 HEALTH_PEAK_FLOW_MOITOR = 0x0928,
510 HEALTH_MEDICATION_MONITOR = 0x092C,
511 HEALTH_KNEE_PROSTHESIS = 0x0930,
512 HEALTH_ANKLE_PROSTHESIS = 0x0934,
513 HEALTH_GENERIC_HEALTH_MANAGER = 0x0938,
514 HEALTH_PERSONAL_MOBILITY_DEVICE = 0x093C,
515 };
516
517 enum SppType {
518 /** RFCOMM */
519 SPP_RFCOMM = 0
520 };
521
522 enum PlayingState {
523 STATE_NOT_PLAYING = 0,
524 STATE_PLAYING = 1
525 };
526
527 enum ProfileId {
528 PROFILE_A2DP_SINK = 0,
529 PROFILE_A2DP_SOURCE = 1,
530 PROFILE_AVRCP_CT = 2,
531 PROFILE_AVRCP_TG = 3,
532 PROFILE_HANDS_FREE_AUDIO_GATEWAY = 4,
533 PROFILE_HANDS_FREE_UNIT = 5,
534 PROFILE_HID_HOST = 6,
535 PROFILE_PAN_NETWORK = 7,
536 PROFILE_PBAP_CLIENT = 8,
537 PROFILE_PBAP_SERVER = 9,
538 PROFILE_OPP = 10
539 };
540
541 template<typename T1, typename T2, typename T3>
542 struct AfterWorkCallbackData {
543 T1* object;
544 T2 function;
545 napi_env env;
546 napi_ref callback;
547 T3 data;
548 };
549
550 template<typename T>
AfterWorkCallback(uv_work_t * work,int status)551 void AfterWorkCallback(uv_work_t *work, int status)
552 {
553 if (work == nullptr) {
554 return;
555 }
556 T data = static_cast<T>(work->data);
557 (data->object->*(data->function))(work, data->data);
558 if (work->data != nullptr) {
559 delete data;
560 work->data = nullptr;
561 }
562 delete work;
563 work = nullptr;
564 }
565
566 #define NAPI_BT_CALL_RETURN(func) \
567 do { \
568 napi_status ret = (func); \
569 if (ret != napi_ok) { \
570 HILOGE("napi call function failed. ret:%{public}d", ret); \
571 return ret; \
572 } \
573 } while (0)
574
575 #define NAPI_BT_RETURN_IF(condition, msg, ret) \
576 do { \
577 if ((condition)) { \
578 HILOGE(msg); \
579 return (ret); \
580 } \
581 } while (0)
582
583 bool IsValidAddress(std::string bdaddr);
584 bool IsValidUuid(std::string uuid);
585 napi_status NapiIsBoolean(napi_env env, napi_value value);
586 napi_status NapiIsNumber(napi_env env, napi_value value);
587 napi_status NapiIsString(napi_env env, napi_value value);
588 napi_status NapiIsFunction(napi_env env, napi_value value);
589 napi_status NapiIsArray(napi_env env, napi_value value);
590 napi_status NapiIsArrayBuffer(napi_env env, napi_value value);
591 napi_status NapiIsObject(napi_env env, napi_value value);
592 napi_status ParseNumberParams(napi_env env, napi_value object, const char *name, bool &outExist, napi_value &outParam);
593 napi_status ParseInt32Params(napi_env env, napi_value object, const char *name, bool &outExist, int32_t &outParam);
594 napi_status ParseUint32Params(napi_env env, napi_value object, const char *name, bool &outExist, uint32_t &outParam);
595 napi_status ParseBooleanParams(napi_env env, napi_value object, const char *name, bool &outExist, bool &outParam);
596 napi_status ParseStringParams(napi_env env, napi_value object, const char *name, bool &outExist,
597 std::string &outParam);
598 napi_status ParseArrayBufferParams(napi_env env, napi_value object, const char *name, bool &outExist,
599 std::vector<uint8_t> &outParam);
600 napi_status ParseUuidParams(napi_env env, napi_value object, const char *name, bool &outExist, UUID &outUuid);
601
602 bool CheckDeivceIdParam(napi_env env, napi_callback_info info, std::string &addr);
603 bool CheckProfileIdParam(napi_env env, napi_callback_info info, int &profileId);
604 bool CheckSetDevicePairingConfirmationParam(napi_env env, napi_callback_info info, std::string &addr, bool &accept);
605 bool CheckLocalNameParam(napi_env env, napi_callback_info info, std::string &name);
606 bool CheckSetBluetoothScanModeParam(napi_env env, napi_callback_info info, int32_t &mode, int32_t &duration);
607 napi_status CheckDeregisterObserver(napi_env env, napi_callback_info info);
608 napi_status CheckEmptyParam(napi_env env, napi_callback_info info);
609 napi_status NapiCheckObjectPropertiesName(napi_env env, napi_value object, const std::vector<std::string> &names);
610
611 } // namespace Bluetooth
612 } // namespace OHOS
613 #endif // NAPI_BLUETOOTH_UTILS_H
614