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