• 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 ble advertiser, peripheral deviceand central manager functions,
21  *  including scan settings and filters, advertise settings and data etc.
22  *
23  * @since 6
24  */
25 
26 /**
27  * @file ble_data.h
28  *
29  * @brief Ble data class.
30  *
31  * @since 6
32  */
33 
34 #ifndef BLE_PARCEL_DATA_H
35 #define BLE_PARCEL_DATA_H
36 
37 #include <map>
38 #include <vector>
39 
40 #include "ble_service_data.h"
41 #include "bt_uuid.h"
42 #include "raw_address.h"
43 
44 namespace OHOS {
45 namespace bluetooth {
46 class AdvertiserData {
47 public:
48     /**
49      * @brief A constructor used to create a <b>BleAdvertiserData</b> instance.
50      *
51      * @since 6
52      */
AdvertiserData()53     AdvertiserData(){};
54 
55     /**
56      * @brief A destructor used to delete the <b>BleAdvertiserData</b> instance.
57      *
58      * @since 6
59      */
~AdvertiserData()60     virtual ~AdvertiserData(){};
61 
GetManufacturerData()62     std::map<uint16_t, std::string> GetManufacturerData() const
63     {
64         return manufacturerSpecificData_;
65     }
66 
67     /**
68      * @brief Get service data.
69      *
70      * @return Returns service data.
71      * @since 6
72      */
GetServiceData()73     std::map<Uuid, std::string> GetServiceData() const
74     {
75         return serviceData_;
76     }
77 
78     /**
79      * @brief Get service uuids.
80      *
81      * @return Returns service uuids.
82      * @since 6
83      */
GetServiceUuids()84     std::vector<Uuid> GetServiceUuids() const
85     {
86         return serviceUuids_;
87     }
88 
89     /**
90      * @brief Get advertiser flag.
91      *
92      * @return Returns advertiser flag.
93      * @since 6
94      */
GetAdvFlag()95     uint8_t GetAdvFlag() const
96     {
97         return advFlag_;
98     }
99 
100     /**
101      * @brief Get payload.
102      *
103      * @return Returns payload.
104      * @since 6
105      */
GetPayload()106     std::string GetPayload() const
107     {
108         return payload_;
109     }
110 
111     /**
112      * @brief Set advertiser flag.
113      *
114      * @param flag Advertiser flag.
115      * @since 6
116      */
SetAdvFlag(uint8_t flag)117     void SetAdvFlag(uint8_t flag)
118     {
119         advFlag_ = flag;
120     }
121 
122     /**
123      * @brief Set payload data.
124      *
125      * @param Payload payload.
126      * @since 6
127      */
SetPayload(const std::string & payload)128     void SetPayload(const std::string &payload)
129     {
130         payload_ = payload;
131     }
132 
133     /**
134      * @brief Add manufacture data.
135      *
136      * @param manufacturerId Manufacture Id which addad data.
137      * @since 6
138      */
AddManufacturerData(uint16_t manufacturerId,std::string data)139     void AddManufacturerData(uint16_t manufacturerId, std::string data)
140     {
141         manufacturerSpecificData_.insert(std::make_pair(manufacturerId, data));
142     }
143 
144     /**
145      * @brief Add service data.
146      *
147      * @param uuid Uuid of service data.
148      * @param serviceData Service data.
149      * @since 6
150      */
AddServiceData(bluetooth::Uuid uuid,std::string serviceData)151     void AddServiceData(bluetooth::Uuid uuid, std::string serviceData)
152     {
153         serviceData_.insert(std::make_pair(uuid, serviceData));
154     }
155 
156     /**
157      * @brief Add service uuid.
158      *
159      * @param serviceUuid Service uuid.
160      * @since 6
161      */
AddServiceUuid(const bluetooth::Uuid & serviceUuid)162     void AddServiceUuid(const bluetooth::Uuid &serviceUuid)
163     {
164         serviceUuids_.push_back(serviceUuid);
165     }
166 
167     /**
168      * @brief Get whether the device name will be included in the advertisement packet.
169      *
170      * @return Returns includeDeviceName flag.
171      * @since 6
172      */
GetIncludeDeviceName()173     bool GetIncludeDeviceName() const
174     {
175         return includeDeviceName_;
176     }
177 
178     /**
179      * @brief Set whether the device name will be included in the advertisement packet.
180      *
181      * @param flag includeDeviceName flag.
182      * @since 6
183      */
SetIncludeDeviceName(bool flag)184     void SetIncludeDeviceName(bool flag)
185     {
186         includeDeviceName_ = flag;
187     }
188 
189 public:
190     std::vector<Uuid> serviceUuids_ {};
191     std::map<uint16_t, std::string> manufacturerSpecificData_ {};
192     std::map<Uuid, std::string> serviceData_ {};
193     uint8_t advFlag_ {};
194     std::string payload_ = "";
195     bool includeDeviceName_ = false;
196 };
197 
198 /**
199  * @brief Represents advertise settings.
200  *
201  * @since 6
202  */
203 class AdvertiserSettings {
204 public:
205     /**
206      * @brief A constructor used to create a <b>BleAdvertiseSettings</b> instance.
207      *
208      * @since 6
209      */
AdvertiserSettings()210     AdvertiserSettings(){};
211 
212     /**
213      * @brief A destructor used to delete the <b>BleAdvertiseSettings</b> instance.
214      *
215      * @since 6
216      */
~AdvertiserSettings()217     virtual ~AdvertiserSettings(){};
218 
219     /**
220      * @brief Check if device service is connectable.
221      *
222      * @return Returns <b>true</b> if device service is connectable;
223      *         returns <b>false</b> if device service is not connectable.
224      * @since 6
225      */
IsConnectable()226     bool IsConnectable() const
227     {
228         return connectable_;
229     }
230 
231     /**
232      * @brief Check if advertiser is legacy mode.
233      *
234      * @return Returns <b>true</b> if advertiser is legacy mode;
235      *         returns <b>false</b> if advertiser is not legacy mode.
236      * @since 6
237      */
IsLegacyMode()238     bool IsLegacyMode() const
239     {
240         return legacyMode_;
241     }
242 
243     /**
244      * @brief Get advertise interval.
245      *
246      * @return Returns advertise interval.
247      * @since 6
248      */
GetInterval()249     int GetInterval() const
250     {
251         return interval_;
252     }
253 
254     /**
255      * @brief Get Tx power.
256      *
257      * @return Returns Tx power.
258      * @since 6
259      */
GetTxPower()260     int GetTxPower() const
261     {
262         return txPower_;
263     }
264 
265     /**
266      * @brief Set connectable.
267      *
268      * @param connectable Whether it is connectable.
269      * @since 6
270      */
SetConnectable(bool connectable)271     void SetConnectable(bool connectable)
272     {
273         connectable_ = connectable;
274     }
275 
276     /**
277      * @brief Set legacyMode.
278      *
279      * @param connectable Whether it is legacyMode.
280      * @since 6
281      */
SetLegacyMode(bool legacyMode)282     void SetLegacyMode(bool legacyMode)
283     {
284         legacyMode_ = legacyMode;
285     }
286 
287     /**
288      * @brief Set advertise interval.
289      *
290      * @param interval Advertise interval.
291      * @since 6
292      */
SetInterval(uint16_t interval)293     void SetInterval(uint16_t interval)
294     {
295         interval_ = interval;
296     }
297 
298     /**
299      * @brief Set Tx power.
300      *
301      * @param txPower Tx power.
302      * @since 6
303      */
SetTxPower(uint8_t txPower)304     void SetTxPower(uint8_t txPower)
305     {
306         txPower_ = txPower;
307     }
308 
309     /**
310      * @brief Get primary phy.
311      *
312      * @return Returns primary phy.
313      * @since 6
314      */
GetPrimaryPhy()315     int GetPrimaryPhy() const
316     {
317         return primaryPhy_;
318     }
319 
320     /**
321      * @brief Set primary phy.
322      *
323      * @param primaryPhy Primary phy.
324      * @since 6
325      */
SetPrimaryPhy(int primaryPhy)326     void SetPrimaryPhy(int primaryPhy)
327     {
328         primaryPhy_ = primaryPhy;
329     }
330 
331     /**
332      * @brief Get second phy.
333      *
334      * @return Returns primary phy.
335      * @since 6
336      */
GetSecondaryPhy()337     int GetSecondaryPhy() const
338     {
339         return secondaryPhy_;
340     }
341 
342     /**
343      * @brief Set second phy.
344      *
345      * @param secondaryPhy Second phy.
346      * @since 6
347      */
SetSecondaryPhy(int secondaryPhy)348     void SetSecondaryPhy(int secondaryPhy)
349     {
350         secondaryPhy_ = secondaryPhy;
351     }
352 
353 public:
354     bool connectable_ {};
355     bool legacyMode_ {};
356     uint16_t interval_ {};
357     uint8_t txPower_ {};
358     int primaryPhy_ {};
359     int secondaryPhy_ {};
360 };
361 
362 class ScanResult {
363 public:
364     /**
365      * @brief A constructor used to create a <b>BleScanResult</b> instance.
366      *
367      * @since 6
368      */
ScanResult()369     ScanResult(){};
370 
371     explicit ScanResult(const BleScanResultImpl &other);
372 
373     /**
374      * @brief A destructor used to delete the <b>BleScanResult</b> instance.
375      *
376      * @since 6
377      */
~ScanResult()378     virtual ~ScanResult(){};
379 
380     /**
381      * @brief Get service uuids.
382      *
383      * @return Returns service uuids.
384      * @since 6
385      */
GetServiceUuids()386     std::vector<Uuid> GetServiceUuids() const
387     {
388         return serviceUuids_;
389     }
390 
391     /**
392      * @brief Get manufacture data.
393      *
394      * @return Returns manufacture data.
395      * @since 6
396      */
GetManufacturerData()397     std::map<uint16_t, std::string> GetManufacturerData() const
398     {
399         return manufacturerSpecificData_;
400     }
401 
402     /**
403      * @brief Get service data.
404      *
405      * @return Returns service data.
406      * @since 6
407      */
GetServiceData()408     std::map<Uuid, std::string> GetServiceData() const
409     {
410         return serviceData_;
411     }
412 
413     /**
414      * @brief Get peripheral device.
415      *
416      * @return Returns peripheral device pointer.
417      * @since 6
418      */
GetPeripheralDevice()419     const RawAddress &GetPeripheralDevice() const
420     {
421         return addr_;
422     }
423 
424     /**
425      * @brief Get peer device rssi.
426      *
427      * @return Returns peer device rssi.
428      * @since 6
429      */
GetRssi()430     int8_t GetRssi() const
431     {
432         return rssi_;
433     }
434 
435     /**
436      * @brief Check if device is connectable.
437      *
438      * @return Returns <b>true</b> if device is connectable;
439      *         returns <b>false</b> if device is not connectable.
440      * @since 6
441      */
IsConnectable()442     bool IsConnectable() const
443     {
444         return connectable_;
445     }
446 
447     /**
448      * @brief Get advertiser flag.
449      *
450      * @return Returns advertiser flag.
451      * @since 6
452      */
GetAdvertiseFlag()453     uint8_t GetAdvertiseFlag() const
454     {
455         return advertiseFlag_;
456     }
457 
458     /**
459      * @brief Add manufacture data.
460      *
461      * @param manufacturerId Manufacture Id which addad data.
462      * @since 6
463      */
AddManufacturerData(uint16_t manufacturerId,std::string data)464     void AddManufacturerData(uint16_t manufacturerId, std::string data)
465     {
466         manufacturerSpecificData_.insert(std::make_pair(manufacturerId, data));
467     }
468 
469     /**
470      * @brief Add service data.
471      *
472      * @param uuid Uuid of service data.
473      * @param serviceData Service data.
474      * @since 6
475      */
AddServiceData(Uuid uuid,std::string serviceData)476     void AddServiceData(Uuid uuid, std::string serviceData)
477     {
478         serviceData_.insert(std::make_pair(uuid, serviceData));
479     }
480 
481     /**
482      * @brief Add service uuid.
483      *
484      * @param serviceUuid Service uuid.
485      * @since 6
486      */
AddServiceUuid(const Uuid & serviceUuid)487     void AddServiceUuid(const Uuid &serviceUuid)
488     {
489         serviceUuids_.push_back(serviceUuid);
490     }
491 
492     /**
493      * @brief Set peripheral device.
494      *
495      * @param device Remote device.
496      * @since 6
497      */
SetPeripheralDevice(const RawAddress & device)498     void SetPeripheralDevice(const RawAddress &device)
499     {
500         addr_ = device;
501     }
502 
503     /**
504      * @brief Set peer device rssi.
505      *
506      * @param rssi Peer device rssi.
507      * @since 6
508      */
SetRssi(int8_t rssi)509     void SetRssi(int8_t rssi)
510     {
511         rssi_ = rssi;
512     }
513 
514     /**
515      * @brief Set connectable.
516      *
517      * @param connectable Whether it is connectable.
518      * @since 6
519      */
SetConnectable(bool connectable)520     void SetConnectable(bool connectable)
521     {
522         connectable_ = connectable;
523     }
524 
525     /**
526      * @brief Set advertiser flag.
527      *
528      * @param flag Advertiser flag.
529      * @since 6
530      */
SetAdvertiseFlag(uint8_t flag)531     void SetAdvertiseFlag(uint8_t flag)
532     {
533         advertiseFlag_ = flag;
534     }
535 
SetPayload(const std::string & payload)536     void SetPayload(const std::string &payload)
537     {
538         payload_ = payload;
539     }
540 
GetPayload()541     std::string GetPayload() const
542     {
543         return payload_;
544     }
545 
SetName(const std::string & name)546     void SetName(const std::string &name)
547     {
548         name_ = name;
549     }
550 
GetName(void)551     std::string GetName(void) const
552     {
553         return name_;
554     }
555 
556 public:
557     std::vector<Uuid> serviceUuids_ {};
558     std::map<uint16_t, std::string> manufacturerSpecificData_ {};
559     std::map<Uuid, std::string> serviceData_ {};
560     RawAddress addr_ {};
561     int8_t rssi_ {};
562     bool connectable_ {};
563     uint8_t advertiseFlag_ {};
564     std::string payload_ {};
565     std::string name_ {};
566 };
567 
568 /**
569  * @brief Represents Scan settings.
570  *
571  * @since 6
572  */
573 class ScanSettings {
574 public:
575     /**
576      * @brief A constructor used to create a <b>BleScanSettings</b> instance.
577      *
578      * @since 6
579      */
ScanSettings()580     ScanSettings(){};
581 
582     /**
583      * @brief A destructor used to delete the <b>BleScanSettings</b> instance.
584      *
585      * @since 6
586      */
~ScanSettings()587     virtual ~ScanSettings(){};
588 
589     /**
590      * @brief Set report delay time.
591      *
592      * @param reportDelayMillis Report delay time.
593      * @since 6
594      */
SetReportDelay(long reportDelayMillis)595     void SetReportDelay(long reportDelayMillis)
596     {
597         reportDelayMillis_ = reportDelayMillis;
598     }
599 
600     /**
601      * @brief Get report delay time.
602      *
603      * @return Returns Report delay time.
604      * @since 6
605      */
GetReportDelayMillisValue()606     long GetReportDelayMillisValue() const
607     {
608         return reportDelayMillis_;
609     }
610 
611     /**
612      * @brief Set scan mode.
613      *
614      * @param scanMode Scan mode.
615      * @return If the scanMode is invalid.
616      * @since 6
617      */
SetScanMode(int scanMode)618     void SetScanMode(int scanMode)
619     {
620         scanMode_ = scanMode;
621     }
622 
623     /**
624      * @brief Get scan mode.
625      *
626      * @return Scan mode.
627      * @since 6
628      */
GetScanMode()629     int GetScanMode() const
630     {
631         return scanMode_;
632     }
633 
634     /**
635      * @brief Set legacy flag.
636      *
637      * @param legacy Legacy value.
638      * @since 6
639      */
SetLegacy(bool legacy)640     void SetLegacy(bool legacy)
641     {
642         legacy_ = legacy;
643     }
644 
645     /**
646      * @brief Get legacy flag.
647      *
648      * @return Legacy flag.
649      * @since 6
650      */
GetLegacy()651     bool GetLegacy() const
652     {
653         return legacy_;
654     }
655 
656     /**
657      * @brief Set phy value.
658      *
659      * @param phy Phy value.
660      * @since 6
661      */
SetPhy(int phy)662     void SetPhy(int phy)
663     {
664         phy_ = phy;
665     }
666 
667     /**
668      * @brief Get phy value.
669      *
670      * @return Phy value.
671      * @since 6
672      */
GetPhy()673     int GetPhy() const
674     {
675         return phy_;
676     }
677 
678 public:
679     long reportDelayMillis_ = 0;
680     int scanMode_ = 0;
681     bool legacy_ = true;
682     int phy_ = 255;
683 };
684 
685 /**
686  * @brief Represents Scan filter.
687  *
688  */
689 class ScanFilter {
690 public:
691     /**
692      * @brief A constructor used to create a <b>BleScanFilter</b> instance.
693      *
694      */
ScanFilter()695     ScanFilter() {}
696 
697     /**
698      * @brief A destructor used to delete the <b>BleScanFilter</b> instance.
699      *
700      */
~ScanFilter()701     virtual ~ScanFilter() {}
702 
703     /**
704      * @brief Set device id.
705      *
706      * @param deviceId device id.
707      */
SetDeviceId(const std::string & deviceId)708     void SetDeviceId(const std::string &deviceId)
709     {
710         deviceId_ = deviceId;
711     }
712 
713     /**
714      * @brief Get device id.
715      *
716      * @return Returns device id.
717      */
GetDeviceId()718     std::string GetDeviceId() const
719     {
720         return deviceId_;
721     }
722 
SetName(const std::string & name)723     void SetName(const std::string &name)
724     {
725         name_ = name;
726     }
727 
GetName()728     std::string GetName() const
729     {
730         return name_;
731     }
732 
SetServiceUuid(const Uuid & uuid)733     void SetServiceUuid(const Uuid &uuid)
734     {
735         serviceUuid_ = uuid;
736         hasServiceUuid_ = true;
737     }
738 
HasServiceUuid()739     bool HasServiceUuid() const
740     {
741         return hasServiceUuid_;
742     }
743 
GetServiceUuid()744     Uuid GetServiceUuid() const
745     {
746         return serviceUuid_;
747     }
748 
SetServiceUuidMask(const Uuid & serviceUuidMask)749     void SetServiceUuidMask(const Uuid &serviceUuidMask)
750     {
751         serviceUuidMask_ = serviceUuidMask;
752         hasServiceUuidMask_ = true;
753     }
754 
HasServiceUuidMask()755     bool HasServiceUuidMask() const
756     {
757         return hasServiceUuidMask_;
758     }
759 
GetServiceUuidMask()760     Uuid GetServiceUuidMask() const
761     {
762         return serviceUuidMask_;
763     }
764 
SetServiceSolicitationUuid(const Uuid & serviceSolicitationUuid)765     void SetServiceSolicitationUuid(const Uuid &serviceSolicitationUuid)
766     {
767         serviceSolicitationUuid_ = serviceSolicitationUuid;
768         hasSolicitationUuid_ = true;
769     }
770 
HasSolicitationUuid()771     bool HasSolicitationUuid() const
772     {
773         return hasSolicitationUuid_;
774     }
775 
GetServiceSolicitationUuid()776     Uuid GetServiceSolicitationUuid() const
777     {
778         return serviceSolicitationUuid_;
779     }
780 
SetServiceSolicitationUuidMask(const Uuid & serviceSolicitationUuidMask)781     void SetServiceSolicitationUuidMask(const Uuid &serviceSolicitationUuidMask)
782     {
783         serviceSolicitationUuidMask_ = serviceSolicitationUuidMask;
784         hasSolicitationUuidMask_ = true;
785     }
786 
HasSolicitationUuidMask()787     bool HasSolicitationUuidMask() const
788     {
789         return hasSolicitationUuidMask_;
790     }
791 
GetServiceSolicitationUuidMask()792     Uuid GetServiceSolicitationUuidMask() const
793     {
794         return serviceSolicitationUuidMask_;
795     }
796 
SetServiceData(const std::vector<uint8_t> & serviceData)797     void SetServiceData(const std::vector<uint8_t> &serviceData)
798     {
799         serviceData_ = serviceData;
800     }
801 
GetServiceData()802     std::vector<uint8_t> GetServiceData() const
803     {
804         return serviceData_;
805     }
806 
SetServiceDataMask(const std::vector<uint8_t> & serviceDataMask)807     void SetServiceDataMask(const std::vector<uint8_t> &serviceDataMask)
808     {
809         serviceDataMask_ = serviceDataMask;
810     }
811 
GetServiceDataMask()812     std::vector<uint8_t> GetServiceDataMask() const
813     {
814         return serviceDataMask_;
815     }
816 
SetManufacturerId(uint16_t manufacturerId)817     void SetManufacturerId(uint16_t manufacturerId)
818     {
819         manufacturerId_ = manufacturerId;
820     }
821 
GetManufacturerId()822     uint16_t GetManufacturerId() const
823     {
824         return manufacturerId_;
825     }
826 
SetManufactureData(const std::vector<uint8_t> & manufactureData)827     void SetManufactureData(const std::vector<uint8_t> &manufactureData)
828     {
829         manufactureData_ = manufactureData;
830     }
831 
GetManufactureData()832     std::vector<uint8_t> GetManufactureData() const
833     {
834         return manufactureData_;
835     }
836 
SetManufactureDataMask(const std::vector<uint8_t> & manufactureDataMask)837     void SetManufactureDataMask(const std::vector<uint8_t> &manufactureDataMask)
838     {
839         manufactureDataMask_ = manufactureDataMask;
840     }
841 
GetManufactureDataMask()842     std::vector<uint8_t> GetManufactureDataMask() const
843     {
844         return manufactureDataMask_;
845     }
846 
847 public:
848     std::string deviceId_;
849     std::string name_;
850 
851     Uuid serviceUuid_;
852     Uuid serviceUuidMask_;
853     Uuid serviceSolicitationUuid_;
854     Uuid serviceSolicitationUuidMask_;
855     bool hasServiceUuid_ = false;
856     bool hasServiceUuidMask_ = false;
857     bool hasSolicitationUuid_ = false;
858     bool hasSolicitationUuidMask_ = false;
859 
860     std::vector<uint8_t> serviceData_;
861     std::vector<uint8_t> serviceDataMask_;
862 
863     uint16_t manufacturerId_ = 0;
864     std::vector<uint8_t> manufactureData_;
865     std::vector<uint8_t> manufactureDataMask_;
866 };
867 }  // namespace bluetooth
868 }  // namespace OHOS
869 
870 #endif  /// BLE_PARCEL_DATA_H
871