• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #ifndef BLE_SEND_MANAGER_H
17 #define BLE_SEND_MANAGER_H
18 
19 #include <string>
20 #include <map>
21 #include <mutex>
22 #include <queue>
23 #include <thread>
24 #include <condition_variable>
25 #include <cstdint>
26 
27 #include "event_handler.h"
28 
29 #include "bluetooth_ble_central_manager.h"
30 #include "bluetooth_gatt_client.h"
31 #include "bluetooth_ble_central_manager.h"
32 #include "bluetooth_gatt_client.h"
33 #include "bluetooth_remote_device.h"
34 #include "bluetooth_hid_host.h"
35 #include "bluetooth_def.h"
36 #include "bluetooth_host.h"
37 #include "bluetooth_errorcode.h"
38 #include "mechbody_controller_log.h"
39 #include "mechbody_controller_types.h"
40 
41 namespace OHOS {
42 namespace MechBodyController {
43 
44 typedef enum {
45     MECH_GATT_PRIORITY_BALANCED = 0x0,
46     MECH_GATT_PRIORITY_HIGH,
47     MECH_GATT_PRIORITY_LOW_POWER,
48 } MechBleGattPriority;
49 
50 struct BleScanParam {
51     std::vector<OHOS::Bluetooth::BleScanFilter> filters;
52     OHOS::Bluetooth::BleScanSettings settings;
53 };
54 
55 class BleReceviceListener {
56 public:
57     virtual int32_t OnReceive(const uint8_t *data, uint32_t dataLen) = 0;
58 };
59 
60 class BleGattClientCallback : public OHOS::Bluetooth::GattClientCallback {
61 public:
62     BleGattClientCallback();
63     ~BleGattClientCallback();
64     void OnConnectionStateChanged(int connectionState, int ret);
65     void OnCharacteristicWriteResult(const OHOS::Bluetooth::GattCharacteristic &characteristic, int ret);
66     void OnServicesDiscovered(int status);
67     void OnSetNotifyCharacteristic(const OHOS::Bluetooth::GattCharacteristic &characteristic, int status);
68     void OnCharacteristicChanged(const OHOS::Bluetooth::GattCharacteristic &characteristic);
69     const std::string &getMac() const;
70     void setMac(const std::string &mac);
71 
72 private:
73     std::string mac;
74 };
75 
76 class HostObserver : public OHOS::Bluetooth::BluetoothRemoteDeviceObserver {
77     const std::string TAG = "HostObserver";
78 public:
79 
80     ~HostObserver() = default;
81 
82     void OnAclStateChanged(const OHOS::Bluetooth::BluetoothRemoteDevice &device,
83         int state, unsigned int reason) override;
84 
85     void OnPairStatusChanged(const OHOS::Bluetooth::BluetoothRemoteDevice &device,
86         int status, int cause) override;
87 
88     void OnRemoteUuidChanged(const OHOS::Bluetooth::BluetoothRemoteDevice &device,
89         const std::vector<Bluetooth::ParcelUuid> &uuids)override;
90 
91     void OnRemoteNameChanged(const OHOS::Bluetooth::BluetoothRemoteDevice &device,
92         const std::string &deviceName)override;
93 
94     void OnRemoteAliasChanged(const OHOS::Bluetooth::BluetoothRemoteDevice &device,
95         const std::string &alias)override;
96 
97     void OnRemoteCodChanged(const OHOS::Bluetooth::BluetoothRemoteDevice &device,
98         const OHOS::Bluetooth::BluetoothDeviceClass &cod) override;
99 
100     void OnRemoteBatteryLevelChanged(const OHOS::Bluetooth::BluetoothRemoteDevice &device,
101         int batteryLevel) override;
102 
103     void OnReadRemoteRssiEvent(const OHOS::Bluetooth::BluetoothRemoteDevice &device, int rssi,
104         int status) override;
105 
106     void OnRemoteBatteryChanged(const OHOS::Bluetooth::BluetoothRemoteDevice &device,
107         const OHOS::Bluetooth::DeviceBatteryInfo &batteryInfo) override;
108 
109     void OnRemoteDeviceCommonInfoReport(const OHOS::Bluetooth::BluetoothRemoteDevice &device,
110         const std::vector<uint8_t> &value) override;
111 };
112 
113 class HidObserver : public OHOS::Bluetooth::HidHostObserver {
114     const std::string TAG = "HidObserver";
115 public:
116     void OnConnectionStateChanged(const OHOS::Bluetooth::BluetoothRemoteDevice &device, int state, int cause) override;
117 };
118 
119 class BleSendManager {
120 public:
121     static BleSendManager& GetInstance();
122 private:
123     BleSendManager(const BleSendManager&) = delete;
124     BleSendManager& operator= (const BleSendManager&) = delete;
125     BleSendManager(BleSendManager&&) = delete;
126     BleSendManager& operator= (BleSendManager&&) = delete;
127 
128 public:
129     BleSendManager();
130     ~BleSendManager();
131 
132     void Init();
133     void StartEvent();
134     void UnInit();
135 
136     void OnScanCallback(const Bluetooth::BleScanResult &result);
137 
138     int32_t SendData(uint8_t *data, uint32_t dataLen);
139     int32_t OnReceive(uint8_t *data, size_t size);
140     void OnConnectionStateChanged(int connectionState, int ret, MechInfo &mechInfo);
141     void OnPairStateChanged(int pairState, int cause, MechInfo &mechInfo);
142     void OnHidStateChanged(int hidState, int cause, MechInfo &mechInfo);
143     bool CheckGattcIsReady();
144     int32_t RegisterTransportSendAdapter(const std::shared_ptr<BleReceviceListener> listener);
145     int32_t UnRegisterTransportSendAdapter(const std::shared_ptr<BleReceviceListener> listener);
146     void OnGattReady(MechInfo &mechInfo);
147     void OnGattDisconnect(MechInfo &mechInfo);
148     int32_t MechbodyConnect(std::string mac, std::string deviceName);
149     int32_t MechbodyGattcConnect(std::string mac, std::string deviceName);
150     int32_t MechbodyPair(std::string &mac, std::string &deviceName);
151     int32_t MechbodyDisConnect(MechInfo &mechInfo);
152     int32_t MechbodyDisConnectSync(MechInfo &mechInfo);
153     int32_t MechbodyHidConnect(std::string &mac, std::string &deviceName);
154     int32_t MechbodyGattcDisconnect(MechInfo mechInfo);
155     int32_t MechbodyHidDisconnect(MechInfo &mechInfo);
156 
157 public:
158     bool isGattReady_ = false;
159     std::mutex isGattReadyMtx_;
160 
161     std::shared_ptr<OHOS::Bluetooth::GattClient> gattClient_ {nullptr};
162     std::shared_ptr<BleGattClientCallback> bleGattClientCallBack_ {nullptr};
163     /* write Character record */
164     uint16_t handle_ = 0;
165     int32_t permissions_ = 0;
166     int32_t properties_ = 0;
167 
168 private:
169     void InitScanPara();
170     void StartScan();
171     void StopScan();
172 
173     // send
174     int32_t MechbodyGattcWriteCharacteristic(uint8_t *data, uint32_t dataLen);
175 
176 private:
177     std::shared_ptr<Bluetooth::BleCentralManager> bleCentralManager_ = nullptr;
178     BleScanParam scanParam_;
179     std::string address_;
180     std::string deviceName_;
181     std::mutex bleReceviceMutex_;
182     std::vector<std::shared_ptr<BleReceviceListener>> bleReceviceListeners_;
183     std::atomic<bool> isBleConnected_ = false;
184 
185     std::mutex observerRegisterMutex_;
186     std::shared_ptr<HidObserver> observer_ = nullptr;
187     std::shared_ptr<HostObserver> hostObserver_ = nullptr;
188 
189     std::shared_ptr<OHOS::AppExecFwk::EventHandler> eventHandler_ = nullptr;
190 
191     std::mutex gattMutex_;
192     std::condition_variable gattCv_;
193     std::mutex pairMutex_;
194     std::condition_variable pairCv_;
195     std::mutex hidMutex_;
196     std::condition_variable hidCv_;
197     std::mutex gattDisconnMutex_;
198     std::condition_variable gattDisconnCv_;
199     std::mutex hidDisconnMutex_;
200     std::condition_variable hidDisconnCv_;
201 };
202 
203 class BleCentralManagerCallbackImpl : public Bluetooth::BleCentralManagerCallback {
204 public:
205     BleCentralManagerCallbackImpl() = default;
206     ~BleCentralManagerCallbackImpl() = default;
207 
208     void OnScanCallback(const Bluetooth::BleScanResult &result) override;
OnFoundOrLostCallback(const Bluetooth::BleScanResult & result,uint8_t callbackType)209     void OnFoundOrLostCallback(const Bluetooth::BleScanResult &result, uint8_t callbackType) override
210     {}
OnBleBatchScanResultsEvent(const std::vector<Bluetooth::BleScanResult> & results)211     void OnBleBatchScanResultsEvent(const std::vector<Bluetooth::BleScanResult> &results) override
212     {}
OnStartOrStopScanEvent(int32_t resultCode,bool isStartScan)213     void OnStartOrStopScanEvent(int32_t resultCode, bool isStartScan) override
214     {}
OnNotifyMsgReportFromLpDevice(const Bluetooth::UUID & btUuid,int msgType,const std::vector<uint8_t> & value)215     void OnNotifyMsgReportFromLpDevice(const Bluetooth::UUID &btUuid, int msgType,
216      const std::vector<uint8_t> &value) override
217     {}
218 };
219 } // namespace MechBodyController
220 } // namespace OHOS
221 #endif // BLE_SEND_MANAGER_H
222