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 /**
17  * @addtogroup Bluetooth
18  * @{
19  *
20  * @brief Defines adapter ble, including observer, callbacks and common functions.
21  *
22  * @since 6
23  */
24 
25 /**
26  * @file interface_adapter_ble.h
27  *
28  * @brief Adapter ble interface.
29  *
30  * @since 6
31  */
32 
33 #ifndef INTERFACE_ADAPTER_BLE_H
34 #define INTERFACE_ADAPTER_BLE_H
35 
36 #include "interface_adapter.h"
37 #include "ble_service_data.h"
38 #include <memory>
39 
40 #ifndef NO_SANITIZE
41 #ifdef __has_attribute
42 #if __has_attribute(no_sanitize)
43 #define NO_SANITIZE(type) __attribute__((no_sanitize(type)))
44 #endif
45 #endif
46 #endif
47 
48 #ifndef NO_SANITIZE
49 #define NO_SANITIZE(type)
50 #endif
51 
52 namespace OHOS {
53 namespace bluetooth {
54 /**
55  * @brief Represents central manager callbacks.
56  *
57  * @since 6
58  */
59 class IBleCentralManagerCallback {
60 public:
61     /**
62      * @brief A destructor used to delete the <b>IBleCentralManagerCallback</b> instance.
63      *
64      * @since 6
65      */
66     virtual ~IBleCentralManagerCallback() = default;
67 
68     /**
69      * @brief Scan callback.
70      *
71      * @param result Scan result.
72      * @since 6
73      */
74     virtual void OnScanCallback(const BleScanResultImpl &result) = 0;
75 
76     /**
77      * @brief Scan results event callback.
78      *
79      * @param results Scan results.
80      * @since 6
81      */
82     virtual void OnBleBatchScanResultsEvent(std::vector<BleScanResultImpl> &results) = 0;
83 
84     /**
85      * @brief Start or Stop scan event callback.
86      *
87      * @param resultCode Start scan result code.
88      * @since 6
89      */
90     virtual void OnStartOrStopScanEvent(int resultCode, bool isStartScan) = 0;
91 
92     virtual void OnNotifyMsgReportFromLpDevice(FilterIdxInfo &info, int msgType,
93         const std::vector<uint8_t> &notifyValue) = 0;
94 };
95 
96 /**
97  * @brief Represents advertise callbacks.
98  *
99  * @since 6
100  */
101 class IBleAdvertiserCallback {
102 public:
103     virtual ~IBleAdvertiserCallback() = default;
104     virtual void OnStartResultEvent(int result, uint8_t advHandle, int opcode = BLE_ADV_DEFAULT_OP_CODE) = 0;
105     virtual void OnEnableResultEvent(int result, uint8_t advHandle) = 0;
106     virtual void OnDisableResultEvent(int result, uint8_t advHandle) = 0;
107     virtual void OnStopResultEvent(int result, uint8_t advHandle) = 0;
108     virtual void OnAutoStopAdvEvent(uint8_t advHandle) = 0;
109     virtual void OnSetAdvDataEvent(int32_t result, int32_t advHandle) = 0;
110 };
111 
112 /**
113  * @brief Represents ble adapter observer.
114  *
115  * @since 6
116  */
117 class IAdapterBleObserver {
118 public:
119     /**
120      * @brief A destructor used to delete the <b>IBleAdapterObserver</b> instance.
121      *
122      * @since 6
123      */
124     virtual ~IAdapterBleObserver() = default;
125 
126     /**
127      * @brief Discovery state changed observer.
128      *
129      * @param status Device discovery status.
130      * @since 6
131      */
132     virtual void OnDiscoveryStateChanged(const int status) = 0;
133 
134     /**
135      * @brief Discovery result observer.
136      *
137      * @param device Remote device.
138      * @param rssi Rssi of device.
139      * @param deviceName Name of device.
140      * @param deviceClass Class of device.
141      * @since 6
142      */
143     virtual void OnDiscoveryResult(
144         const RawAddress &device, int rssi, const std::string deviceName, int deviceClass) = 0;
145 
146     /**
147      * @brief Pair request observer.
148      *
149      * @param device Remote device.
150      * @since 6
151      */
152     virtual void OnPairRequested(const BTTransport transport, const RawAddress &device) = 0;
153 
154     /**
155      * @brief Pair confirmed observer.
156      *
157      * @param device Remote device.
158      * @param reqType Pair type.
159      * @param number Paired passkey.
160      * @since 6
161      */
162     virtual void OnPairConfirmed(
163         const BTTransport transport, const RawAddress &device, const int reqType, const int number) = 0;
164 
165     /**
166      * @brief Scan mode changed observer.
167      *
168      * @param mode Device scan mode.
169      * @since 6
170      */
171     virtual void OnScanModeChanged(const int mode) = 0;
172 
173     /**
174      * @brief Device name changed observer.
175      *
176      * @param deviceName Device name.
177      * @since 6
178      */
179     virtual void OnDeviceNameChanged(const std::string deviceName) = 0;
180 
181     /**
182      * @brief Device address changed observer.
183      *
184      * @param address Device address.
185      * @since 6
186      */
187     virtual void OnDeviceAddrChanged(const std::string address) = 0;
188 
189     /**
190      * @brief Advertising state changed observer.
191      *
192      * @param state Advertising state.
193      * @since 6
194      */
195     virtual void OnAdvertisingStateChanged(const int state) = 0;
196 };
197 
198 /**
199  * @brief Represents peripheral callback.
200  *
201  * @since 6
202  */
203 class IBlePeripheralCallback {
204 public:
205     /**
206      * @brief A destructor used to delete the <b>IBlePeripheralCallback</b> instance.
207      *
208      * @since 6
209      */
210     virtual ~IBlePeripheralCallback() = default;
211 
212     /**
213      * @brief Read remote rssi event callback.
214      *
215      * @param device Remote device.
216      * @param rssi Remote device rssi.
217      * @param status Read status.
218      * @since 6
219      */
220     virtual void OnReadRemoteRssiEvent(const RawAddress &device, int rssi, int status) = 0;
221     /**
222      * @brief Read remote rssi event callback.
223      *
224      * @param device Remote device.
225      * @param rssi Remote device rssi.
226      * @param status Read status.
227      * @since 6
228      */
229     virtual void OnPairStatusChanged(const BTTransport transport, const RawAddress &device, int status) = 0;
230 
231     virtual void OnAclStateChanged(const RawAddress &device, int state, unsigned int reason) = 0;
232 };
233 
234 /**
235  * @brief Represents ble adapter interface.
236  *
237  * @since 6
238  */
239 class IAdapterBle : public IAdapter {
240 public:
241     /**
242      * @brief Register central manager callback.
243      *
244      * @param callback Class IBleCentralManagerCallback pointer to register callback.
245      * @since 6
246      */
247     virtual void RegisterBleCentralManagerCallback(IBleCentralManagerCallback &callback) = 0;
248 
249     /**
250      * @brief Deregister central manager callback.
251      *
252      * @since 6
253      */
254     virtual void DeregisterBleCentralManagerCallback() const = 0;
255 
256     /**
257      * @brief Register advertiser callback.
258      *
259      * @param callback Class IBleAdvertiseCallback pointer to register callback.
260      * @since 6
261      */
262     virtual void RegisterBleAdvertiserCallback(IBleAdvertiserCallback &callback) = 0;
263 
264     /**
265      * @brief Deregister advertiser callback.
266      *
267      * @since 6
268      */
269     virtual void DeregisterBleAdvertiserCallback() const = 0;
270 
271     /**
272      * @brief Read remote device rssi value.
273      *
274      * @param device Remote device
275      * @return Returns <b>true</b> if the operation is successful;
276      *         returns <b>false</b> if the operation fails.
277      * @since 6
278      */
279     virtual bool ReadRemoteRssiValue(const RawAddress &device) const = 0;
280 
281     /**
282      * @brief Register ble adapter observer.
283      *
284      * @param observer Class IBleAdapterObserver pointer to register observer.
285      * @return Returns <b>true</b> if the operation is successful;
286      *         returns <b>false</b> if the operation fails.
287      * @since 6
288      */
289     virtual bool RegisterBleAdapterObserver(IAdapterBleObserver &observer) const = 0;
290 
291     /**
292      * @brief Deregister ble adapter observer.
293      *
294      * @return Returns <b>true</b> if the operation is successful;
295      *         returns <b>false</b> if the operation fails.
296      * @since 6
297      */
298     virtual bool DeregisterBleAdapterObserver(IAdapterBleObserver &observer) const = 0;
299 
300     /**
301      * @brief Register peripheral callback.
302      *
303      * @param callback Class IBlePeripheralCallback pointer to register callback.
304      * @since 6
305      */
306     virtual void RegisterBlePeripheralCallback(IBlePeripheralCallback &callback) const = 0;
307 
308     /**
309      * @brief Deregister peripheral callback.
310      *
311      * @since 6
312      */
313     virtual void DeregisterBlePeripheralCallback(IBlePeripheralCallback &callback) const = 0;
314 
315     /**
316      * @brief Get device IO capability.
317      *
318      * @return Returns device IO capability.
319      * @since 6
320      */
321     virtual int GetIoCapability() const = 0;
322 
323     /**
324      * @brief Set device IO capability.
325      *
326      * @param ioCapability IO capability.
327      * @return Returns <b>true</b> if the operation is successful;
328      *         returns <b>false</b> if the operation fails.
329      * @since 6
330      */
331     virtual bool SetIoCapability(int ioCapability) const = 0;
332 
333     /**
334      * @brief Get max advertising data length.
335      *
336      * @return Returns max advertising data length.
337      * @since 6
338      */
339     virtual int GetBleMaxAdvertisingDataLength() const = 0;
340 
341     /**
342      * @brief Get peer device address type.
343      *
344      * @param device Remote device.
345      * @return Returns peer device address type.
346      * @since 6
347      */
348     virtual int GetPeerDeviceAddrType(const RawAddress &device) const = 0;
349 
350     /**
351      * @brief Check if device is discovering.
352      *
353      * @return Returns <b>true</b> if device is discovering;
354      *         returns <b>false</b> if device is not discovering.
355      * @since 6
356      */
357     virtual bool IsBtDiscovering() const = 0;
358 
359     /**
360      * @brief Get advertiser id.
361      *
362      * @return Returns advertiser handle.
363      * @since 6
364      */
365     virtual uint8_t GetAdvertiserHandle() const = 0;
366 
367     /**
368      * @brief Get advertiser status.
369      *
370      * @return Returns advertiser status.
371      * @since 6
372      */
373     virtual int GetAdvertisingStatus() const = 0;
374 
375     /**
376      * @brief Get Link Layer Privacy Supported.
377      *
378      * @return True:supported; False:not supported.
379      * @since 6
380      */
381     virtual bool IsLlPrivacySupported() const = 0;
382 
383     /**
384      * @brief Add characteristic value.
385      *
386      * @param adtype Type of the field.
387      * @param data Field data.
388      * @since 6
389      */
390     virtual void AddCharacteristicValue(uint8_t adtype, const std::string &data) const = 0;
391 
392     /**
393      * @brief Start advertising.
394      *
395      * @param settings Advertise settings.
396      * @param advData Advertise data.
397      * @param scanResponse Scan response data
398      * @param advHandle Advertise handle
399      * @since 6
400      */
401     virtual void StartAdvertising(const BleAdvertiserSettingsImpl &settings, const BleAdvertiserDataImpl &advData,
402         const BleAdvertiserDataImpl &scanResponse, uint8_t advHandle) const = 0;
403 
404     /**
405      * @brief Stop advertising.
406      *
407      * @param advHandle Advertise handle
408      * @since 6
409      */
410     virtual void StopAdvertising(uint8_t advHandle) const = 0;
411 
412     /**
413      * @brief Cleans up advertisers.
414      *
415      * @since 6
416      */
417     virtual void Close(uint8_t advHandle) const = 0;
418 
419     /**
420      * @brief Start scan
421      *
422      * @param setting Scan setting.
423      * @since 6
424      */
425     virtual void StartScan(const BleScanSettingsImpl &setting) const = 0;
426 
427     /**
428      * @brief Stop scan.
429      *
430      * @since 6
431      */
432     virtual void StopScan() const = 0;
433 
434     /**
435      * @brief Config scan filter
436      *
437      * @param scannerId indicate one scan.
438      * @param filter Scan filter.
439      * @return ret
440      */
441     virtual int ConfigScanFilter(int32_t scannerId, const std::vector<BleScanFilterImpl> &filters) = 0;
442 
443     /**
444      * @brief Remove scan filter
445      *
446      * @param scannerId scanner id.
447      */
448     virtual void RemoveScanFilter(int32_t scannerId) = 0;
449 
450     /**
451      * @brief Alloc scan object id.
452      *
453      * @return scanner id.
454      */
455     virtual int32_t AllocScannerId() = 0;
456 
457     /**
458      * @brief Remove scan object id.
459      *
460      * @param scannerId scanner id.
461      */
462     virtual void RemoveScannerId(int32_t scannerId) = 0;
463 };
464 }  // namespace bluetooth
465 }  // namespace OHOS
466 
467 #endif  // INTERFACE_ADAPTER_BLE_H
468