• 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 central manager, including scan filter and settings, and common functions.
21  *
22  * @since 6
23  */
24 
25 /**
26  * @file bluetooth_ble_central_manager.h
27  *
28  * @brief Central manager common functions.
29  *
30  * @since 6
31  */
32 
33 #ifndef BLUETOOTH_BLE_CENTRAL_MANAGER_H
34 #define BLUETOOTH_BLE_CENTRAL_MANAGER_H
35 
36 #include "bluetooth_def.h"
37 #include "bluetooth_types.h"
38 #include "bluetooth_remote_device.h"
39 #include "bluetooth_ble_advertiser.h"
40 
41 namespace OHOS {
42 namespace Bluetooth {
43 /**
44  * @brief Represents scan result.
45  *
46  * @since 6
47  */
48 class BLUETOOTH_API BleScanResult {
49 public:
50     /**
51      * @brief A constructor used to create a <b>BleScanResult</b> instance.
52      *
53      * @since 6
54      */
55     BleScanResult();
56 
57     /**
58      * @brief A destructor used to delete the <b>BleScanResult</b> instance.
59      *
60      * @since 6
61      */
62     ~BleScanResult();
63 
64     /**
65      * @brief Get service uuids.
66      *
67      * @return Returns service uuids.
68      * @since 6
69      */
70     std::vector<UUID> GetServiceUuids() const;
71 
72     /**
73      * @brief Get manufacture data.
74      *
75      * @return Returns manufacture data.
76      * @since 6
77      */
78     std::map<uint16_t, std::string> GetManufacturerData() const;
79 
80     /**
81      * @brief Get service data.
82      *
83      * @return Returns service data.
84      * @since 6
85      */
86     std::map<UUID, std::string> GetServiceData() const;
87 
88     /**
89      * @brief Get peripheral device.
90      *
91      * @return Returns peripheral device pointer.
92      * @since 6
93      */
94     BluetoothRemoteDevice GetPeripheralDevice() const;
95 
96     /**
97      * @brief Get peer device rssi.
98      *
99      * @return Returns peer device rssi.
100      * @since 6
101      */
102     int8_t GetRssi() const;
103 
104     /**
105      * @brief Check if device is connctable.
106      *
107      * @return Returns <b>true</b> if device is connctable;
108      *         returns <b>false</b> if device is not connctable.
109      * @since 6
110      */
111     bool IsConnectable() const;
112 
113     /**
114      * @brief Get advertiser flag.
115      *
116      * @return Returns advertiser flag.
117      * @since 6
118      */
119     uint8_t GetAdvertiseFlag() const;
120 
121     /**
122      * @brief Get payload.
123      *
124      * @return Returns payload.
125      * @since 6
126      */
127     std::vector<uint8_t> GetPayload() const;
128     /**
129      * @brief Add manufacture data.
130      *
131      * @param manufacturerId Manufacture Id which addad data.
132      * @param data Manufacture data
133      * @since 6
134      */
135 
136     void AddManufacturerData(uint16_t manufacturerId, const std::string &data);
137 
138     /**
139      * @brief Add service data.
140      *
141      * @param uuid Uuid of service data.
142      * @param serviceData Service data.
143      * @since 6
144      */
145     void AddServiceData(const UUID &uuid, const std::string &serviceData);
146 
147     /**
148      * @brief Add service uuid.
149      *
150      * @param serviceUuid Service uuid.
151      * @since 6
152      */
153     void AddServiceUuid(const UUID &serviceUuid);
154 
155     /**
156      * @brief Add data to payload.
157      *
158      * @param payload Added payload.
159      * @since 6
160      */
161     void SetPayload(std::string payload);
162 
163     /**
164      * @brief Set peripheral device.
165      *
166      * @param device Remote device.
167      * @since 6
168      */
169     void SetPeripheralDevice(const BluetoothRemoteDevice &device);
170 
171     /**
172      * @brief Set peer device rssi.
173      *
174      * @param rssi Peer device rssi.
175      * @since 6
176      */
177     void SetRssi(int8_t rssi);
178 
179     /**
180      * @brief Set connectable.
181      *
182      * @param connectable Whether it is connectable.
183      * @since 6
184      */
185     void SetConnectable(bool connectable);
186 
187     /**
188      * @brief Set advertiser flag.
189      *
190      * @param flag Advertiser flag.
191      * @since 6
192      */
193     void SetAdvertiseFlag(uint8_t flag);
194 
195     void SetName(const std::string &name);
196     std::string GetName(void);
197 
198 private:
199     std::vector<UUID> serviceUuids_ {};
200     std::map<uint16_t, std::string> manufacturerSpecificData_ {};
201     std::map<UUID, std::string> serviceData_ {};
202     BluetoothRemoteDevice peripheralDevice_ {};
203     int8_t rssi_ {};
204     bool connectable_ {};
205     uint8_t advertiseFlag_ {};
206     std::vector<uint8_t> payload_ {};
207     std::string name_ {};
208 };
209 /**
210  * @brief Represents central manager callback.
211  *
212  * @since 6
213  */
214 class BleCentralManagerCallback {
215 public:
216     /**
217      * @brief A destructor used to delete the <b>BleCentralManagerCallback</b> instance.
218      *
219      * @since 6
220      */
221     virtual ~BleCentralManagerCallback() = default;
222 
223     /**
224      * @brief Scan result callback.
225      *
226      * @param result Scan result.
227      * @since 6
228      */
229     virtual void OnScanCallback(const BleScanResult &result) = 0;
230 
231     /**
232      * @brief Batch scan results event callback.
233      *
234      * @param results Scan results.
235      * @since 6
236      */
237     virtual void OnBleBatchScanResultsEvent(const std::vector<BleScanResult> &results) = 0;
238 
239     /**
240      * @brief Start or Stop scan event callback.
241      *
242      * @param resultCode Scan result code.
243      * @param isStartScan true->start scan, false->stop scan.
244      * @since 6
245      */
246     virtual void OnStartOrStopScanEvent(int resultCode, bool isStartScan) = 0;
247 
248     /**
249      * @brief low power device msg report event callback.
250      *
251      * @param uuid Service uuid.
252      * @param msgType Report msg type.
253      * @param value Msg data.
254      * @since 6
255      */
OnNotifyMsgReportFromLpDevice(const UUID & uuid,int msgType,const std::vector<uint8_t> & value)256     virtual void OnNotifyMsgReportFromLpDevice(const UUID &uuid, int msgType, const std::vector<uint8_t> &value) {};
257 };
258 
259 /**
260  * @brief Represents Scan settings.
261  *
262  * @since 6
263  */
264 class BLUETOOTH_API BleScanSettings {
265 public:
266     /**
267      * @brief A constructor used to create a <b>BleScanSettings</b> instance.
268      *
269      * @since 6
270      */
271     BleScanSettings();
272 
273     /**
274      * @brief A destructor used to delete the <b>BleScanSettings</b> instance.
275      *
276      * @since 6
277      */
278     ~BleScanSettings();
279 
280     /**
281      * @brief Set repport delay time.
282      *
283      * @param reportDelayMillis Repport delay time.
284      * @since 6
285      */
286     void SetReportDelay(long reportDelayMillis = 0);
287 
288     /**
289      * @brief Get repport delay time.
290      *
291      * @return Returns Repport delay time.
292      * @since 6
293      */
294     long GetReportDelayMillisValue() const;
295 
296     /**
297      * @brief Set scan mode.
298      *
299      * @param scanMode Scan mode.
300      * @return Returns <b>true</b> if set scanMode successfully;
301      *         returns <b>false</b> if set scanMode failed.
302      * @since 6
303      */
304     int SetScanMode(int scanMode);
305 
306     /**
307      * @brief Get scan mode.
308      *
309      * @return Scan mode.
310      * @since 6
311      */
312     int GetScanMode() const;
313 
314     /**
315      * @brief Set legacy flag.
316      *
317      * @param legacy Legacy value.
318      * @since 6
319      */
320     void SetLegacy(bool legacy);
321 
322     /**
323      * @brief Get legacy flag.
324      *
325      * @return Legacy flag.
326      * @since 6
327      */
328     bool GetLegacy() const;
329 
330     /**
331      * @brief Set phy value.
332      *
333      * @param phy Phy value.
334      * @since 6
335      */
336     void SetPhy(int phy);
337 
338     /**
339      * @brief Get phy value.
340      *
341      * @return Phy value.
342      * @since 6
343      */
344     int GetPhy() const;
345 
346 private:
347     long reportDelayMillis_ = 0;
348     int scanMode_ = SCAN_MODE_LOW_POWER;
349     bool legacy_ = true;
350     int phy_ = PHY_LE_1M;
351 };
352 
353 /**
354  * @brief Represents Scan filter.
355  *
356  */
357 class BLUETOOTH_API BleScanFilter {
358 public:
359     /**
360      * @brief A constructor used to create a <b>BleScanFilter</b> instance.
361      *
362      */
363     BleScanFilter();
364 
365     /**
366      * @brief A destructor used to delete the <b>BleScanFilter</b> instance.
367      *
368      */
369     ~BleScanFilter();
370 
371     void SetDeviceId(std::string deviceId);
372 
373     std::string GetDeviceId() const;
374 
375     void SetName(std::string name);
376 
377     std::string GetName() const;
378 
379     void SetServiceUuid(const UUID &uuid);
380 
381     bool HasServiceUuid();
382 
383     UUID GetServiceUuid() const;
384 
385     void SetServiceUuidMask(const UUID &serviceUuidMask);
386 
387     bool HasServiceUuidMask();
388 
389     UUID GetServiceUuidMask() const;
390 
391     void SetServiceSolicitationUuid(const UUID &serviceSolicitationUuid);
392 
393     bool HasSolicitationUuid();
394 
395     UUID GetServiceSolicitationUuid() const;
396 
397     void SetServiceSolicitationUuidMask(const UUID &erviceSolicitationUuidMask);
398 
399     bool HasSolicitationUuidMask();
400 
401     UUID GetServiceSolicitationUuidMask() const;
402 
403     void SetServiceData(std::vector<uint8_t> serviceData);
404 
405     std::vector<uint8_t> GetServiceData() const;
406 
407     void SetServiceDataMask(std::vector<uint8_t> serviceDataMask);
408 
409     std::vector<uint8_t> GetServiceDataMask() const;
410 
411     void SetManufacturerId(uint16_t manufacturerId);
412 
413     uint16_t GetManufacturerId() const;
414 
415     void SetManufactureData(std::vector<uint8_t> manufactureData);
416 
417     std::vector<uint8_t> GetManufactureData() const;
418 
419     void SetManufactureDataMask(std::vector<uint8_t> manufactureDataMask);
420 
421     std::vector<uint8_t> GetManufactureDataMask() const;
422 
423     private:
424         std::string deviceId_;
425         std::string name_;
426 
427         UUID serviceUuid_;
428         UUID serviceUuidMask_;
429         UUID serviceSolicitationUuid_;
430         UUID serviceSolicitationUuidMask_;
431         bool hasServiceUuid_ = false;
432         bool hasServiceUuidMask_ = false;
433         bool hasSolicitationUuid_ = false;
434         bool hasSolicitationUuidMask_ = false;
435 
436         std::vector<uint8_t> serviceData_;
437         std::vector<uint8_t> serviceDataMask_;
438 
439         uint16_t manufacturerId_ = 0;
440         std::vector<uint8_t> manufactureData_;
441         std::vector<uint8_t> manufactureDataMask_;
442 };
443 
444 struct BleActiveDeviceInfo {
445     std::vector<int8_t> deviceId;
446     int32_t status;
447     int32_t timeOut;
448 };
449 
450 struct BleLpDeviceParamSet {
451     BleScanSettings scanSettings;
452     std::vector<BleScanFilter> scanFilters;
453     BleAdvertiserSettings advSettings;
454     std::vector<uint8_t> advData;
455     std::vector<uint8_t> respData;
456     UUID uuid;
457     std::vector<BleActiveDeviceInfo> activeDeviceInfos;
458     int32_t deliveryMode;
459     int32_t advHandle;
460     int32_t duration;
461     uint32_t fieldValidFlagBit;
462 };
463 
464 /**
465  * @brief Represents central manager.
466  *
467  * @since 6
468  */
469 class BLUETOOTH_API BleCentralManager {
470 public:
471     /**
472      * @brief A constructor used to create a <b>BleCentralManager</b> instance.
473      *
474      * @param cllback Central manager callback to create an <b>BleCentralManagerCallback</b> instance.
475      * @since 6
476      */
477     explicit BleCentralManager(BleCentralManagerCallback &callback);
478 
479     /**
480      * @brief A constructor used to create a <b>BleCentralManager</b> instance.
481      *
482      * @param cllback Central manager callback to create an <b>BleCentralManagerCallback</b> instance.
483      * @param enableRandomAddrMode Indicates whether to use random address for interface interaction.
484      * @since 6
485      */
486     explicit BleCentralManager(std::shared_ptr<BleCentralManagerCallback> callback, bool enableRandomAddrMode = true);
487 
488     /**
489      * @brief A destructor used to delete the <b>BleCentralManager</b> instance.
490      *
491      * @since 6
492      */
493     ~BleCentralManager();
494 
495     /**
496      * @brief Start scan.
497      *
498      * @since 6
499      */
500     int StartScan();
501 
502     /**
503      * @brief Start scan.
504      *
505      * @param settings Scan settings.
506      * @since 6
507      */
508     int StartScan(const BleScanSettings &settings);
509 
510     /**
511      * @brief Stop scan.
512      *
513      * @since 6
514      */
515     int StopScan();
516 
517     /**
518      * @brief Config scan filter.
519      *
520      */
521     int ConfigScanFilter(const std::vector<BleScanFilter> &filter);
522 
523     /**
524     * @brief set low power device adv param.
525     *
526     * @param duration advertise duration.
527     * @param maxExtAdvEvents maximum number of extended advertising events.
528     * @param window work window.
529     * @param interval work interval.
530     * @param advHandle Indicates the advertisement handle.
531     * @return Result.
532     */
533     int SetLpDeviceAdvParam(int duration, int maxExtAdvEvents, int window, int interval, int advHandle);
534 
535     /**
536     * @brief set scan result report channel.
537     *
538     * @param enable the switch of report.(true: report msg to low power device; false: not report).
539     * @return Result.
540     */
541     int SetScanReportChannelToLpDevice(bool enable);
542 
543     /**
544     * @brief Enable sync data to low power device.
545     *
546     * @return Result.
547     */
548     int EnableSyncDataToLpDevice();
549 
550     /**
551     * @brief Disable sync data to low power device.
552     *
553     * @return Result.
554     */
555     int DisableSyncDataToLpDevice();
556 
557     /**
558     * @brief Translate ParamData to low power device.
559     *
560     * @param data Indicates the pointer to the data.
561     * @param dataSize Indicates the data size.
562     * @param type Indicates the data type.
563     * @return Result.
564     */
565     int SendParamsToLpDevice(const std::vector<uint8_t> &dataValue, int32_t type);
566 
567     /**
568     * @brief Get whether support low power device.
569     *
570     * @return true: available; false: not available.
571     * @since 6
572     */
573     bool IsLpDeviceAvailable();
574 
575     /**
576     * @brief Set low power device param data.
577     *
578     * @param lpDeviceParamSet low power device param data.
579     * @return Result
580     * @since 6
581     */
582     int SetLpDeviceParam(const BleLpDeviceParamSet &lpDeviceParamSet);
583 
584     /**
585     * @brief Remove low power device param data.
586     *
587     * @param uuid Uuid.
588     * @return Result
589     * @since 6
590     */
591     int RemoveLpDeviceParam(const UUID &uuid);
592 
593 private:
594     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BleCentralManager);
595     BLUETOOTH_DECLARE_IMPL();
596 };
597 } // namespace Bluetooth
598 } // namespace OHOS
599 #endif