• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef NAPI_BLUETOOTH_BLE_UTILS_H
16 #define NAPI_BLUETOOTH_BLE_UTILS_H
17 
18 #include "bluetooth_ble_central_manager.h"
19 #include "bluetooth_gatt_characteristic.h"
20 #include "bluetooth_gatt_client.h"
21 #include "bluetooth_gatt_descriptor.h"
22 #include "bluetooth_gatt_server.h"
23 #include "bluetooth_gatt_service.h"
24 #include "bluetooth_log.h"
25 #include "napi_native_object.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 #include "uv.h"
36 
37 
38 namespace OHOS {
39 namespace Bluetooth {
40 namespace {
41 std::string deviceAddr;
42 } // namespace
43 
44 enum class NapiGattWriteType {
45     WRITE = 1,
46     WRITE_NO_RESPONSE = 2,
47 };
48 
49 const char * const REGISTER_BLE_FIND_DEVICE_TYPE = "BLEDeviceFind";
50 const char * const REGISTER_SYS_BLE_SCAN_TYPE = "sysBLEScan";
51 const char * const REGISTER_SYS_BLE_FIND_DEVICE_TYPE = "sysBLEDeviceFonud";
52 const char * const REGISTER_BLE_ADVERTISING_STATE_INFO_TYPE = "advertisingStateChange";
53 
54 void ConvertGattServiceToJS(napi_env env, napi_value result, GattService &service);
55 void ConvertGattServiceVectorToJS(napi_env env, napi_value result, std::vector<GattService> &services);
56 void ConvertBLECharacteristicToJS(napi_env env, napi_value result, GattCharacteristic &characteristic);
57 void ConvertBLECharacteristicVectorToJS(
58     napi_env env, napi_value result, std::vector<GattCharacteristic> &characteristics);
59 void ConvertBLEDescriptorToJS(napi_env env, napi_value result, GattDescriptor &descriptor);
60 void ConvertBLEDescriptorVectorToJS(napi_env env, napi_value result, std::vector<GattDescriptor> &descriptors);
61 void ConvertCharacteristicReadReqToJS(napi_env env, napi_value result, const std::string &device,
62     GattCharacteristic &characteristic, int requestId);
63 void ConvertCharacteristicWriteReqToJS(napi_env env, napi_value result, const std::string &device,
64     GattCharacteristic &characteristic, int requestId);
65 void ConvertDescriptorReadReqToJS(
66     napi_env env, napi_value result, const std::string &device, GattDescriptor &descriptor, int requestId);
67 void ConvertDescriptorWriteReqToJS(
68     napi_env env, napi_value result, const std::string &device, GattDescriptor &descriptor, int requestId);
69 
70 napi_value ScanReportTypeInit(napi_env env);
71 napi_value ScanDutyInit(napi_env env);
72 napi_value MatchModeInit(napi_env env);
73 napi_value PhyTypeInit(napi_env env);
74 napi_value ScanReportModeInit(napi_env env);
75 
76 void SetGattClientDeviceId(const std::string &deviceId);
77 std::string GetGattClientDeviceId();
78 napi_status CheckBleScanParams(napi_env env, napi_callback_info info, std::vector<BleScanFilter> &outScanfilters,
79     BleScanSettings &outSettinngs);
80 
81 struct GattCharacteristicCallbackInfo : public BluetoothCallbackInfo {
82     GattCharacteristic characteristic_ = {UUID::FromString("0"), 0, 0};
83 };
84 
85 struct GattDescriptorCallbackInfo : public BluetoothCallbackInfo {
86     GattDescriptor descriptor_ = {UUID::FromString("0"), 0};
87 };
88 
89 class NapiNativeBleCharacteristic : public NapiNativeObject {
90 public:
NapiNativeBleCharacteristic(const GattCharacteristic & character)91     NapiNativeBleCharacteristic(const GattCharacteristic &character) : character_(character) {}
92     ~NapiNativeBleCharacteristic() override = default;
93 
94     napi_value ToNapiValue(napi_env env) const override;
95 private:
96     GattCharacteristic character_;
97 };
98 
99 class NapiNativeBleDescriptor : public NapiNativeObject {
100 public:
NapiNativeBleDescriptor(const GattDescriptor & descriptor)101     NapiNativeBleDescriptor(const GattDescriptor &descriptor) : descriptor_(descriptor) {}
102     ~NapiNativeBleDescriptor() override = default;
103 
104     napi_value ToNapiValue(napi_env env) const override;
105 private:
106     GattDescriptor descriptor_;
107 };
108 
109 class NapiNativeBleScanResult : public NapiNativeObject {
110 public:
NapiNativeBleScanResult(const BleScanResult & scanResult)111     NapiNativeBleScanResult(const BleScanResult &scanResult) : scanResult_(scanResult) {}
112     ~NapiNativeBleScanResult() override = default;
113 
114     napi_value ToNapiValue(napi_env env) const override;
115 private:
116     BleScanResult scanResult_;
117 };
118 
119 class NapiNativeBleScanReport : public NapiNativeObject {
120 public:
NapiNativeBleScanReport(const BleScanResult & scanResult,ScanReportType scanReportType)121     NapiNativeBleScanReport(const BleScanResult &scanResult, ScanReportType scanReportType)
122         : scanResult_(scanResult), scanReportType_(scanReportType) {}
123     ~NapiNativeBleScanReport() override = default;
124 
125     napi_value ToNapiValue(napi_env env) const override;
126 private:
127     BleScanResult scanResult_;
128     ScanReportType scanReportType_;
129 };
130 
131 class NapiNativeGattServiceArray : public NapiNativeObject {
132 public:
NapiNativeGattServiceArray(const std::vector<GattService> & gattServices)133     NapiNativeGattServiceArray(const std::vector<GattService> &gattServices) : gattServices_(gattServices) {}
134     ~NapiNativeGattServiceArray() override = default;
135 
136     napi_value ToNapiValue(napi_env env) const override;
137 private:
138     std::vector<GattService> gattServices_ {};
139 };
140 
141 class NapiNativeAdvertisingStateInfo : public NapiNativeObject {
142 public:
NapiNativeAdvertisingStateInfo(int advHandle,int advState)143     NapiNativeAdvertisingStateInfo(int advHandle, int advState) : advHandle_(advHandle), advState_(advState) {}
144     ~NapiNativeAdvertisingStateInfo() override = default;
145 
146     napi_value ToNapiValue(napi_env env) const override;
147 private:
148     int advHandle_;
149     int advState_;
150 };
151 
152 }  // namespace Bluetooth
153 }  // namespace OHOS
154 #endif  // NAPI_BLUETOOTH_BLE_UTILS_H