• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 OnAutoStopAdvEvent(uint8_t advHandle) = 0;
106     virtual void OnSetAdvDataEvent(int32_t result, int32_t advHandle) = 0;
107 };
108 
109 /**
110  * @brief Represents ble adapter observer.
111  *
112  * @since 6
113  */
114 class IAdapterBleObserver {
115 public:
116     /**
117      * @brief A destructor used to delete the <b>IBleAdapterObserver</b> instance.
118      *
119      * @since 6
120      */
121     virtual ~IAdapterBleObserver() = default;
122 
123     /**
124      * @brief Discovery state changed observer.
125      *
126      * @param status Device discovery status.
127      * @since 6
128      */
129     virtual void OnDiscoveryStateChanged(const int status) = 0;
130 
131     /**
132      * @brief Discovery result observer.
133      *
134      * @param device Remote device.
135      * @since 6
136      */
137     virtual void OnDiscoveryResult(const RawAddress &device) = 0;
138 
139     /**
140      * @brief Pair request observer.
141      *
142      * @param device Remote device.
143      * @since 6
144      */
145     virtual void OnPairRequested(const BTTransport transport, const RawAddress &device) = 0;
146 
147     /**
148      * @brief Pair confirmed observer.
149      *
150      * @param device Remote device.
151      * @param reqType Pair type.
152      * @param number Paired passkey.
153      * @since 6
154      */
155     virtual void OnPairConfirmed(
156         const BTTransport transport, const RawAddress &device, const int reqType, const int number) = 0;
157 
158     /**
159      * @brief Scan mode changed observer.
160      *
161      * @param mode Device scan mode.
162      * @since 6
163      */
164     virtual void OnScanModeChanged(const int mode) = 0;
165 
166     /**
167      * @brief Device name changed observer.
168      *
169      * @param deviceName Device name.
170      * @since 6
171      */
172     virtual void OnDeviceNameChanged(const std::string deviceName) = 0;
173 
174     /**
175      * @brief Device address changed observer.
176      *
177      * @param address Device address.
178      * @since 6
179      */
180     virtual void OnDeviceAddrChanged(const std::string address) = 0;
181 
182     /**
183      * @brief Advertising state changed observer.
184      *
185      * @param state Advertising state.
186      * @since 6
187      */
188     virtual void OnAdvertisingStateChanged(const int state) = 0;
189 };
190 
191 /**
192  * @brief Represents peripheral callback.
193  *
194  * @since 6
195  */
196 class IBlePeripheralCallback {
197 public:
198     /**
199      * @brief A destructor used to delete the <b>IBlePeripheralCallback</b> instance.
200      *
201      * @since 6
202      */
203     virtual ~IBlePeripheralCallback() = default;
204 
205     /**
206      * @brief Read remote rssi event callback.
207      *
208      * @param device Remote device.
209      * @param rssi Remote device rssi.
210      * @param status Read status.
211      * @since 6
212      */
213     virtual void OnReadRemoteRssiEvent(const RawAddress &device, int rssi, int status) = 0;
214     /**
215      * @brief Read remote rssi event callback.
216      *
217      * @param device Remote device.
218      * @param rssi Remote device rssi.
219      * @param status Read status.
220      * @since 6
221      */
222     virtual void OnPairStatusChanged(const BTTransport transport, const RawAddress &device, int status) = 0;
223 
224     virtual void OnAclStateChanged(const RawAddress &device, int state, unsigned int reason) = 0;
225 };
226 
227 /**
228  * @brief Represents ble adapter interface.
229  *
230  * @since 6
231  */
232 class IAdapterBle : public IAdapter {
233 public:
234     /**
235      * @brief Register central manager callback.
236      *
237      * @param callback Class IBleCentralManagerCallback pointer to register callback.
238      * @since 6
239      */
240     virtual void RegisterBleCentralManagerCallback(IBleCentralManagerCallback &callback) = 0;
241 
242     /**
243      * @brief Deregister central manager callback.
244      *
245      * @since 6
246      */
247     virtual void DeregisterBleCentralManagerCallback() const = 0;
248 
249     /**
250      * @brief Register advertiser callback.
251      *
252      * @param callback Class IBleAdvertiseCallback pointer to register callback.
253      * @since 6
254      */
255     virtual void RegisterBleAdvertiserCallback(IBleAdvertiserCallback &callback) = 0;
256 
257     /**
258      * @brief Deregister advertiser callback.
259      *
260      * @since 6
261      */
262     virtual void DeregisterBleAdvertiserCallback() const = 0;
263 
264     /**
265      * @brief Read remote device rssi value.
266      *
267      * @param device Remote device
268      * @return Returns <b>true</b> if the operation is successful;
269      *         returns <b>false</b> if the operation fails.
270      * @since 6
271      */
272     virtual bool ReadRemoteRssiValue(const RawAddress &device) const = 0;
273 
274     /**
275      * @brief Register ble adapter observer.
276      *
277      * @param observer Class IBleAdapterObserver pointer to register observer.
278      * @return Returns <b>true</b> if the operation is successful;
279      *         returns <b>false</b> if the operation fails.
280      * @since 6
281      */
282     virtual bool RegisterBleAdapterObserver(IAdapterBleObserver &observer) const = 0;
283 
284     /**
285      * @brief Deregister ble adapter observer.
286      *
287      * @return Returns <b>true</b> if the operation is successful;
288      *         returns <b>false</b> if the operation fails.
289      * @since 6
290      */
291     virtual bool DeregisterBleAdapterObserver(IAdapterBleObserver &observer) const = 0;
292 
293     /**
294      * @brief Register peripheral callback.
295      *
296      * @param callback Class IBlePeripheralCallback pointer to register callback.
297      * @since 6
298      */
299     virtual void RegisterBlePeripheralCallback(IBlePeripheralCallback &callback) const = 0;
300 
301     /**
302      * @brief Deregister peripheral callback.
303      *
304      * @since 6
305      */
306     virtual void DeregisterBlePeripheralCallback(IBlePeripheralCallback &callback) const = 0;
307 
308     /**
309      * @brief Get device IO capability.
310      *
311      * @return Returns device IO capability.
312      * @since 6
313      */
314     virtual int GetIoCapability() const = 0;
315 
316     /**
317      * @brief Set device IO capability.
318      *
319      * @param ioCapability IO capability.
320      * @return Returns <b>true</b> if the operation is successful;
321      *         returns <b>false</b> if the operation fails.
322      * @since 6
323      */
324     virtual bool SetIoCapability(int ioCapability) const = 0;
325 
326     /**
327      * @brief Get max advertising data length.
328      *
329      * @return Returns max advertising data length.
330      * @since 6
331      */
332     virtual int GetBleMaxAdvertisingDataLength() const = 0;
333 
334     /**
335      * @brief Get peer device address type.
336      *
337      * @param device Remote device.
338      * @return Returns peer device address type.
339      * @since 6
340      */
341     virtual int GetPeerDeviceAddrType(const RawAddress &device) const = 0;
342 
343     /**
344      * @brief Check if device is discovering.
345      *
346      * @return Returns <b>true</b> if device is discovering;
347      *         returns <b>false</b> if device is not discovering.
348      * @since 6
349      */
350     virtual bool IsBtDiscovering() const = 0;
351 
352     /**
353      * @brief Get advertiser id.
354      *
355      * @return Returns advertiser handle.
356      * @since 6
357      */
358     virtual uint8_t GetAdvertiserHandle() const = 0;
359 
360     /**
361      * @brief Get advertiser status.
362      *
363      * @return Returns advertiser status.
364      * @since 6
365      */
366     virtual int GetAdvertisingStatus() const = 0;
367 
368     /**
369      * @brief Get Link Layer Privacy Supported.
370      *
371      * @return True:supported; False:not supported.
372      * @since 6
373      */
374     virtual bool IsLlPrivacySupported() const = 0;
375 
376     /**
377      * @brief Add characteristic value.
378      *
379      * @param adtype Type of the field.
380      * @param data Field data.
381      * @since 6
382      */
383     virtual void AddCharacteristicValue(uint8_t adtype, const std::string &data) const = 0;
384 
385     /**
386      * @brief Start advertising.
387      *
388      * @param settings Advertise settings.
389      * @param advData Advertise data.
390      * @param scanResponse Scan response data
391      * @param advHandle Advertise handle
392      * @since 6
393      */
394     virtual void StartAdvertising(const BleAdvertiserSettingsImpl &settings, const BleAdvertiserDataImpl &advData,
395         const BleAdvertiserDataImpl &scanResponse, uint8_t advHandle) const = 0;
396 
397     /**
398      * @brief Stop advertising.
399      *
400      * @param advHandle Advertise handle
401      * @since 6
402      */
403     virtual void StopAdvertising(uint8_t advHandle) const = 0;
404 
405     /**
406      * @brief Cleans up advertisers.
407      *
408      * @since 6
409      */
410     virtual void Close(uint8_t advHandle) const = 0;
411 
412     /**
413      * @brief Start scan
414      *
415      * @since 6
416      */
417     virtual void StartScan() 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