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