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