• 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 napi_value GattDisconnectReasonInit(napi_env env);
76 
77 void SetGattClientDeviceId(const std::string &deviceId);
78 std::string GetGattClientDeviceId();
79 napi_status CheckBleScanParams(napi_env env, napi_callback_info info, std::vector<BleScanFilter> &outScanfilters,
80     BleScanSettings &outSettinngs);
81 
82 struct GattCharacteristicCallbackInfo : public BluetoothCallbackInfo {
83     GattCharacteristic characteristic_ = {UUID::FromString("0"), 0, 0};
84 };
85 
86 struct GattDescriptorCallbackInfo : public BluetoothCallbackInfo {
87     GattDescriptor descriptor_ = {UUID::FromString("0"), 0};
88 };
89 
90 class NapiNativeBleCharacteristic : public NapiNativeObject {
91 public:
NapiNativeBleCharacteristic(const GattCharacteristic & character)92     NapiNativeBleCharacteristic(const GattCharacteristic &character) : character_(character) {}
93     ~NapiNativeBleCharacteristic() override = default;
94 
95     napi_value ToNapiValue(napi_env env) const override;
96 private:
97     GattCharacteristic character_;
98 };
99 
100 class NapiNativeBleDescriptor : public NapiNativeObject {
101 public:
NapiNativeBleDescriptor(const GattDescriptor & descriptor)102     NapiNativeBleDescriptor(const GattDescriptor &descriptor) : descriptor_(descriptor) {}
103     ~NapiNativeBleDescriptor() override = default;
104 
105     napi_value ToNapiValue(napi_env env) const override;
106 private:
107     GattDescriptor descriptor_;
108 };
109 
110 class NapiNativeBleScanResult : public NapiNativeObject {
111 public:
NapiNativeBleScanResult(const BleScanResult & scanResult)112     NapiNativeBleScanResult(const BleScanResult &scanResult) : scanResult_(scanResult) {}
113     ~NapiNativeBleScanResult() override = default;
114 
115     napi_value ToNapiValue(napi_env env) const override;
116 private:
117     BleScanResult scanResult_;
118 };
119 
120 class NapiNativeBleScanReport : public NapiNativeObject {
121 public:
NapiNativeBleScanReport(const BleScanResult & scanResult,ScanReportType scanReportType)122     NapiNativeBleScanReport(const BleScanResult &scanResult, ScanReportType scanReportType)
123         : scanResults_(std::vector<BleScanResult>{scanResult}), scanReportType_(scanReportType) {}
NapiNativeBleScanReport(const std::vector<BleScanResult> & scanResults,ScanReportType scanReportType)124     NapiNativeBleScanReport(const std::vector<BleScanResult> &scanResults, ScanReportType scanReportType)
125         : scanResults_(scanResults), scanReportType_(scanReportType) {}
126     ~NapiNativeBleScanReport() override = default;
127 
128     napi_value ToNapiValue(napi_env env) const override;
129 private:
130     std::vector<BleScanResult> scanResults_;
131     ScanReportType scanReportType_;
132 };
133 
134 class NapiNativeGattServiceArray : public NapiNativeObject {
135 public:
NapiNativeGattServiceArray(const std::vector<GattService> & gattServices)136     NapiNativeGattServiceArray(const std::vector<GattService> &gattServices) : gattServices_(gattServices) {}
137     ~NapiNativeGattServiceArray() override = default;
138 
139     napi_value ToNapiValue(napi_env env) const override;
140 private:
141     std::vector<GattService> gattServices_ {};
142 };
143 
144 class NapiNativeAdvertisingStateInfo : public NapiNativeObject {
145 public:
NapiNativeAdvertisingStateInfo(int advHandle,int advState)146     NapiNativeAdvertisingStateInfo(int advHandle, int advState) : advHandle_(advHandle), advState_(advState) {}
147     ~NapiNativeAdvertisingStateInfo() override = default;
148 
149     napi_value ToNapiValue(napi_env env) const override;
150 private:
151     int advHandle_;
152     int advState_;
153 };
154 
155 }  // namespace Bluetooth
156 }  // namespace OHOS
157 #endif  // NAPI_BLUETOOTH_BLE_UTILS_H