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, advertising settings and data etc.
22  *
23  * @since 6
24  */
25 
26 /**
27  * @file ble_data.cpp
28  *
29  * @brief Ble data class.
30  *
31  * @since 6
32  */
33 
34 #ifndef BLE_SERVICE_DATA_H
35 #define BLE_SERVICE_DATA_H
36 
37 #include <cstddef>
38 #include <cstdint>
39 #include <map>
40 #include <vector>
41 #include <functional>
42 #include "bt_def.h"
43 #include "bt_uuid.h"
44 #include "cstdint"
45 #include "iosfwd"
46 #include "raw_address.h"
47 #include "string"
48 #include "utility"
49 
50 namespace OHOS {
51 namespace bluetooth {
52 /**
53  * @brief Represents scan settings.
54  *
55  * @since 6
56  */
57 class BleScanSettingsImpl {
58 public:
59     /**
60      * @brief A constructor used to create a <b>BleScanSettingsInternal</b> instance.
61      *
62      * @since 6
63      */
BleScanSettingsImpl()64     BleScanSettingsImpl(){};
65 
66     /**
67      * @brief A destructor used to delete the <b>BleScanSettingsInternal</b> instance.
68      *
69      * @since 6
70      */
~BleScanSettingsImpl()71     ~BleScanSettingsImpl(){};
72 
73     /**
74      * @brief Set report delay time.
75      *
76      * @param reportDelayMillis Report delay time.
77      * @since 6
78      */
79     void SetReportDelay(long reportDelayMillis = 0);
80 
81     /**
82      * @brief Get report delay time.
83      *
84      * @return Report delay time.
85      * @since 6
86      */
87     long GetReportDelayMillisValue() const;
88 
89     /**
90      * @brief Set scan mode.
91      *
92      * @param scanMode Scan mode.
93      * @since 6
94      */
95     void SetScanMode(int scanMode);
96 
97     /**
98      * @brief Get scan mode.
99      *
100      * @return Scan mode.
101      * @since 6
102      */
103     int GetScanMode() const;
104 
105     /**
106      * @brief Set legacy flag.
107      *
108      * @param legacy Legacy flag.
109      * @since 6
110      */
111     void SetLegacy(bool legacy);
112 
113     /**
114      * @brief Get legacy flag.
115      *
116      * @return Legacy flag.
117      * @since 6
118      */
119     bool GetLegacy() const;
120 
121     /**
122      * @brief Set phy value.
123      *
124      * @param phy Phy value.
125      * @since 6
126      */
127     void SetPhy(int phy);
128 
129     /**
130      * @brief Get phy value.
131      *
132      * @return Phy value.
133      * @since 6
134      */
135     int GetPhy() const;
136 
137     /**
138      * @brief Set callback type.
139      *
140      * @param callbackType callback type.
141      * @since 12
142      */
143     void SetCallbackType(uint8_t callbackType);
144 
145     /**
146      * @brief Get callback type.
147      *
148      * @return callback type value.
149      * @since 12
150      */
151     uint8_t GetCallbackType() const;
152 
153     /**
154      * @brief Set match track adv type for total number of advertisers to track per filter.
155      *
156      * @param matchTrackAdvType match track adv type value.
157      * @since 12
158      */
159     void SetMatchTrackAdvType(uint8_t matchTrackAdvType);
160 
161     /**
162      * @brief Get match track adv type.
163      *
164      * @return match track adv type value.
165      * @since 12
166      */
167     uint8_t GetMatchTrackAdvType() const;
168 
169     /**
170      * @brief Set match mode for Bluetooth LE scan filters hardware match.
171      *
172      * @param matchMode match mode value.
173      * @since 12
174      */
175     void SetMatchMode(uint8_t matchMode);
176 
177     /**
178      * @brief Get match mode.
179      *
180      * @return match mode value.
181      * @since 12
182      */
183     uint8_t GetMatchMode() const;
184 
operator ==(const BleScanSettingsImpl &rhs) const185     bool operator == (const BleScanSettingsImpl &rhs) const
186     {
187         return (legacy_ == rhs.GetLegacy()) &&
188                (phy_ == rhs.GetPhy()) &&
189                (reportDelayMillis_ == rhs.GetReportDelayMillisValue()) &&
190                (scanMode_ == rhs.GetScanMode()) &&
191                (callbackType_ == rhs.GetCallbackType()) &&
192                (matchTrackAdvType_ == rhs.GetMatchTrackAdvType()) &&
193                (matchMode_ == rhs.GetMatchMode());
194     }
195 
196 private:
197     /// delay millistime
198     long reportDelayMillis_ = 0;
199     int scanMode_ = SCAN_MODE_LOW_POWER;
200     bool legacy_ = true;
201     int phy_ = PHY_LE_1M;
202     uint8_t callbackType_ = BLE_SCAN_CALLBACK_TYPE_ALL_MATCH;
203     uint8_t matchTrackAdvType_ = MAX_MATCH_TRACK_ADV;
204     uint8_t matchMode_ = MATCH_MODE_AGGRESSIVE;
205 };
206 
207 /**
208  * @brief Represents advertise settings.
209  *
210  * @since 6
211  */
212 class BleAdvertiserSettingsImpl {
213 public:
214     /**
215      * @brief A constructor used to create a <b>BleAdvertiseSettingsInternal</b> instance.
216      *
217      * @since 6
218      */
BleAdvertiserSettingsImpl()219     BleAdvertiserSettingsImpl(){};
220 
221     /**
222      * @brief A destructor used to delete the <b>BleAdvertiseSettingsInternal</b> instance.
223      *
224      * @since 6
225      */
~BleAdvertiserSettingsImpl()226     ~BleAdvertiserSettingsImpl(){};
227 
228     /**
229      * @brief Check if device service is connectable.
230      *
231      * @return Returns <b>true</b> if device service is connectable;
232      *         returns <b>false</b> if device service is not connectable.
233      * @since 6
234      */
235     bool IsConnectable() const;
236 
237     /**
238      * @brief Set connectable.
239      *
240      * @param connectable Whether it is connectable.
241      * @since 6
242      */
243     void SetConnectable(bool connectable);
244     /**
245      * @brief Check if advertiser is legacy mode.
246      *
247      * @return Returns <b>true</b> if advertiser is legacy mode;
248      *         returns <b>false</b> if advertiser is not legacy mode.
249      * @since 6
250      */
251     bool IsLegacyMode() const;
252 
253     /**
254      * @brief Set legacyMode.
255      *
256      * @param connectable Whether it is legacyMode.
257      * @since 6
258      */
259     void SetLegacyMode(bool legacyMode);
260 
261     /**
262      * @brief Get advertise interval.
263      *
264      * @return Returns advertise interval.
265      * @since 6
266      */
267     int GetInterval() const;
268 
269     /**
270      * @brief Set advertise interval.
271      *
272      * @param interval Advertise interval.
273      * @since 6
274      */
275     void SetInterval(int interval = BLE_ADV_DEFAULT_INTERVAL);
276 
277     /**
278      * @brief Get advertiser Tx power.
279      *
280      * @return Returns advertiser Tx power.
281      * @since 6
282      */
283     int8_t GetTxPower() const;
284 
285     /**
286      * @brief Set advertiser Tx power.
287      *
288      * @param txPower Advertiser Tx power.
289      * @since 6
290      */
291     void SetTxPower(int8_t txPower);
292 
293     /**
294      * @brief Get primary phy.
295      *
296      * @return Returns primary phy.
297      * @since 6
298      */
299     int GetPrimaryPhy() const;
300 
301     /**
302      * @brief Set primary phy.
303      *
304      * @param primaryPhy Primary phy.
305      * @since 6
306      */
307     void SetPrimaryPhy(int primaryPhy);
308 
309     /**
310      * @brief Get second phy.
311      *
312      * @return Returns primary phy.
313      * @since 6
314      */
315     int GetSecondaryPhy() const;
316 
317     /**
318      * @brief Set second phy.
319      *
320      * @param secondaryPhy Second phy.
321      * @since 6
322      */
323     void SetSecondaryPhy(int secondaryPhy);
324 
325     /**
326      * @brief Get own address.
327      *
328      * @return Returns own address.
329      * @since 6
330      */
331     std::array<uint8_t, RawAddress::BT_ADDRESS_BYTE_LEN> GetOwnAddr() const;
332 
333     /**
334      * @brief Set own address.
335      *
336      * @param addr Own address.
337      * @since 6
338      */
339     void SetOwnAddr(const std::array<uint8_t, RawAddress::BT_ADDRESS_BYTE_LEN>& addr);
340 
341     /**
342      * @brief Get own address type.
343      *
344      * @return Returns own address type.
345      * @since 6
346      */
347     int8_t GetOwnAddrType() const;
348 
349     /**
350      * @brief Set own address type.
351      *
352      * @param addrType Own address type.
353      * @since 6
354      */
355     void SetOwnAddrType(int8_t addrType);
356 
357 private:
358     /// Advertising interval.
359     int interval_ = BLE_ADV_DEFAULT_INTERVAL;
360     /// Advertising connectable.
361     bool connectable_ = true;
362     /// Advertising txPower.
363     int8_t txPower_ = BLE_ADV_TX_POWER_MEDIUM_VALUE;
364     /// Advertising legacyMode.
365     bool legacyMode_ = true;
366     /// Advertising primaryPhy.
367     int primaryPhy_ = BLE_ADVERTISEMENT_PHY_1M;
368     /// Advertising secondaryPhy.
369     int secondaryPhy_ = BLE_ADVERTISEMENT_PHY_1M;
370     /// Own address.
371     std::array<uint8_t, RawAddress::BT_ADDRESS_BYTE_LEN> ownAddr_ = {};
372     /// Own address type.
373     int8_t ownAddrType_ = -1;
374 };
375 
376 /**
377  * @brief Represents scan filter.
378  *
379  */
380 class BleScanFilterImpl {
381 public:
BleScanFilterImpl()382     BleScanFilterImpl() {}
~BleScanFilterImpl()383     ~BleScanFilterImpl() {}
384 
385     /**
386      * @brief Set device id.
387      *
388      * @param deviceId filter device id.
389      */
390     void SetDeviceId(const std::string &deviceId);
391 
392     /**
393      * @brief Get filter device id.
394      *
395      * @return Returns filter device id.
396      */
397     std::string GetDeviceId() const;
398 
399     void SetName(const std::string &name);
400 
401     std::string GetName() const;
402 
403     void SetServiceUuid(const Uuid &serviceUuid);
404 
405     bool HasServiceUuid() const;
406 
407     Uuid GetServiceUuid() const;
408 
409     void SetServiceUuidMask(const Uuid &serviceUuidMask);
410 
411     bool HasServiceUuidMask() const;
412 
413     Uuid GetServiceUuidMask() const;
414 
415     void SetServiceSolicitationUuid(const Uuid &serviceSolicitationUuid);
416 
417     bool HasSolicitationUuid() const;
418 
419     Uuid GetServiceSolicitationUuid() const;
420 
421     void SetServiceSolicitationUuidMask(const Uuid &serviceSolicitationUuidMask);
422 
423     bool HasSolicitationUuidMask() const;
424 
425     Uuid GetServiceSolicitationUuidMask() const;
426 
427     void SetServiceData(const std::vector<uint8_t> &serviceData);
428 
429     std::vector<uint8_t> GetServiceData() const;
430 
431     void SetServiceDataMask(const std::vector<uint8_t> &serviceDataMask);
432 
433     std::vector<uint8_t> GetServiceDataMask() const;
434 
435     void SetManufacturerId(uint16_t manufacturerId);
436 
437     uint16_t GetManufacturerId() const;
438 
439     void SetManufactureData(const std::vector<uint8_t> &manufactureData);
440 
441     std::vector<uint8_t> GetManufactureData() const;
442 
443     void SetManufactureDataMask(const std::vector<uint8_t> &manufactureDataMask);
444 
445     std::vector<uint8_t> GetManufactureDataMask() const;
446 
447     void SetClientId(int clientId);
448 
449     int GetClientId() const;
450 
451     void SetFiltIndex(uint8_t filtIndex);
452 
453     uint8_t GetFiltIndex() const;
454 
455     void SetFilterAction(uint8_t action);
456 
457     uint8_t GetFilterAction() const;
458 
459     void SetAdvIndReportFlag(bool advIndReport);
460 
461     bool GetAdvIndReportFlag() const;
462 
463 private:
464     std::string deviceId_;
465     std::string name_;
466 
467     Uuid serviceUuid_;
468     Uuid serviceUuidMask_;
469     Uuid serviceSolicitationUuid_;
470     Uuid serviceSolicitationUuidMask_;
471     bool hasServiceUuid_ = false;
472     bool hasServiceUuidMask_ = false;
473     bool hasSolicitationUuid_ = false;
474     bool hasSolicitationUuidMask_ = false;
475 
476     std::vector<uint8_t> serviceData_;
477     std::vector<uint8_t> serviceDataMask_;
478 
479     uint16_t manufacturerId_ = 0;
480     std::vector<uint8_t> manufactureData_;
481     std::vector<uint8_t> manufactureDataMask_;
482     bool advIndReport_ = false;
483 
484     int clientId_ = 0;
485     uint8_t filtIndex_ = 0;
486     uint8_t action_ = -1;
487 };
488 
489 /**
490  * @brief Represents advertise data.
491  *
492  * @since 6
493  */
494 class BleAdvertiserDataImpl {
495 public:
496     /**
497      * @brief A constructor used to create a <b>BleAdvertiseDataInternal</b> instance.
498      *
499      * @since 6
500      */
501     BleAdvertiserDataImpl();
502 
503     /**
504      * @brief A destructor used to delete the <b>BleAdvertiseDataInternal</b> instance.
505      *
506      * @since 6
507      */
~BleAdvertiserDataImpl()508     ~BleAdvertiserDataImpl()
509     {}
510 
511     /**
512      * @brief Add manufacture data.
513      *
514      * @param manufacturerId Manufacture Id which addad data.
515      * @param data Manufacture data
516      * @since 6
517      */
518     int AddManufacturerData(uint16_t manufacturerId, const std::string &data);
519 
520     /**
521      * @brief Add service data.
522      *
523      * @param uuid Uuid of service data.
524      * @param data Service data.
525      * @since 6
526      */
527     void AddServiceData(const Uuid &uuid, const std::string &data);
528 
529     /**
530      * @brief Add characteristic value.
531      *
532      * @param adtype Type of the field.
533      * @param data Field data.
534      * @since 6
535      */
536     void AddCharacteristicValue(uint8_t adtype, const std::string &data);
537 
538     /**
539      * @brief Add service uuid.
540      *
541      * @param uuid Service uuid.
542      * @since 6
543      */
544     void AddServiceUuid(const Uuid &uuid);
545 
546     /**
547      * @brief Add service uuids.
548      *
549      * @param uuid Service uuids.
550      * @since 12
551      */
552     void AddServiceUuids(const std::vector<Uuid> &uuidVec);
553 
554     /**
555      * @brief Set device appearance.
556      *
557      * @param appearance Device appearance.
558      * @since 6
559      */
560     void SetAppearance(uint16_t appearance);
561 
562     /**
563      * @brief Set complete services.
564      *
565      * @param uuid Service uuid.
566      * @since 6
567      */
568     void SetCompleteServices(const Uuid &uuid);
569 
570     /**
571      * @brief Set advertiser flag.
572      *
573      * @param flag Advertiser flag.
574      * @since 6
575      */
576     void SetFlags(uint8_t flag);
577 
578     /**
579      * @brief Get advertiser flag.
580      *
581      * @return flag Advertiser flag.
582      * @since 6
583      */
584     uint8_t GetFlags() const;
585 
586     /**
587      * @brief Set manufacture data.
588      *
589      * @param data Manufacture data.
590      * @since 6
591      */
592     void SetManufacturerData(const std::string &data);
593 
594     /**
595      * @brief Set device name.
596      *
597      * @param name Device name.
598      * @since 6
599      */
600     void SetDeviceName(const std::string &name);
601 
602     /**
603      * @brief Set Tx power level.
604      *
605      * @param txPowerLevel Tx power level.
606      * @since 6
607      */
608     void SetTxPowerLevel(uint8_t txPowerLevel);
609 
610     /**
611      * @brief Add service data.
612      *
613      * @param data Service data.
614      * @since 6
615      */
616     void AddData(std::string data);
617 
618     /**
619      * @brief Set advertiser data packet.
620      *
621      * @return Returns advertiser data packet.
622      * @since 1.0
623      * @version 1.0
624      */
625     void SetPayload(const std::string &payload);
626     /**
627      * @brief Get advertiser data packet.
628      *
629      * @return Returns advertiser data packet.
630      * @since 6
631      */
632     std::string GetPayload() const;
633 
634 private:
635     /// Advertiser data packet
636     std::string payload_ {};
637     uint8_t advFlag_ {};
638 
639     /**
640      * @brief Set advertiser data long name.
641      *
642      * @param name Bluetooth device name.
643      * @since 6
644      */
645     void SetLongName(const std::string &name);
646 
647     /**
648      * @brief Set advertiser data short name
649      *
650      * @param name Bluetooth device name.
651      * @since 6
652      */
653     void SetShortName(const std::string &name);
654 };
655 
656 /**
657  * @brief Parse advertisement parameter .
658  *
659  * @since 6
660  */
661 struct BlePeripheralDeviceParseAdvData {
662     uint8_t *payload = nullptr;
663     size_t length = 0;
664 };
665 /**
666  * @brief Represents peripheral device.
667  *
668  * @since 6
669  */
670 class BlePeripheralDevice {
671 public:
672     /**
673      * @brief A constructor used to create a <b>BlePeripheralDevice</b> instance.
674      *
675      * @since 6
676      */
677     BlePeripheralDevice();
678 
679     /**
680      * @brief A destructor used to delete the <b>BlePeripheralDevice</b> instance.
681      *
682      * @since 6
683      */
684     ~BlePeripheralDevice();
685 
686     /**
687      * @brief Get device address.
688      *
689      * @return Returns device address.
690      * @since 6
691      */
692     RawAddress GetRawAddress() const;
693 
694     /**
695      * @brief Get device Appearance.
696      *
697      * @return Returns device Appearance.
698      * @since 6
699      */
700     uint16_t GetAppearance() const;
701 
702     /**
703      * @brief Get Manufacturer Data.
704      *
705      * @return Returns Manufacturer Data.
706      * @since 6
707      */
708     std::map<uint16_t, std::string> GetManufacturerData() const;
709 
710     /**
711      * @brief Get device Name.
712      *
713      * @return Returns device Name.
714      * @since 6
715      */
716     std::string GetName() const;
717 
718     /**
719      * @brief Get device RSSI.
720      *
721      * @return Returns device RSSI.
722      * @since 6
723      */
724     int8_t GetRSSI() const;
725 
726     /**
727      * @brief Get service Data.
728      *
729      * @return Returns service data.
730      * @since 6
731      */
732     std::vector<std::string> GetServiceData() const;
733 
734     /**
735      * @brief Get Service Data.
736      *
737      * @param index Service data index.
738      * @return Returns service data.
739      * @since 6
740      */
741     std::string GetServiceData(int index) const;
742 
743     /**
744      * @brief Get service data UUID.
745      *
746      * @return Returns service data UUID.
747      * @since 6
748      */
749     std::vector<Uuid> GetServiceDataUUID() const;
750 
751     /**
752      * @brief Get service data UUID.
753      *
754      * @param index Service data index.
755      * @return Returns service data UUID.
756      * @since 6
757      */
758     Uuid GetServiceDataUUID(int index) const;
759 
760     /**
761      * @brief Get serviceU UUID.
762      *
763      * @return Returns service UUID.
764      * @since 6
765      */
766     std::vector<Uuid> GetServiceUUID() const;
767 
768     /**
769      * @brief Get service UUID.
770      *
771      * @param index Service UUID index.
772      * @return Return service UUID.
773      * @since 6
774      */
775     Uuid GetServiceUUID(int index) const;
776 
777     /**
778      * @brief Get advertiser data packet.
779      *
780      * @return Returns advertiser data packet.
781      * @since 6
782      */
783     uint8_t *GetPayload() const;
784 
785     /**
786      * @brief Get advertising packet length.
787      *
788      * @return Returns advertising packet length.
789      * @since 6
790      */
791     size_t GetPayloadLen() const;
792 
793     /**
794      * @brief Get address type.
795      *
796      * @return Returns address type.
797      * @since 6
798      */
799     int GetAddressType() const;
800 
801     /**
802      * @brief Set address type.
803      *
804      * @param type Address type.
805      * @since 6
806      */
807     void SetAddressType(int type);
808     /**
809      * @brief Check if include manufacture data.
810      *
811      * @return Returns <b>true</b> if include manufacture data;
812      *         returns <b>false</b> if do not include manufacture data.
813      * @since 6
814      */
815     bool IsManufacturerData() const;
816 
817     /**
818      * @brief Check if include device rssi.
819      *
820      * @return Returns <b>true</b> if include device rssi;
821      *         returns <b>false</b> if do not include device rssi.
822      * @since 6
823      */
824     bool IsRSSI() const;
825 
826     /**
827      * @brief Check if include service data.
828      *
829      * @return Returns <b>true</b> if include service data;
830      *         returns <b>false</b> if do not include service data.
831      * @since 6
832      */
833     bool IsServiceData() const;
834 
835     /**
836      * @brief Check if include service UUID.
837      *
838      * @return Returns <b>true</b> if include service UUID;
839      *         returns <b>false</b> if do not include service UUID.
840      * @since 6
841      */
842     bool IsServiceUUID() const;
843 
844     bool IsName(void) const;
845 
846     /**
847      * @brief set device address.
848      *
849      * @param address device address.
850      * @since 6
851      */
852     void SetAddress(const RawAddress &address);
853 
854     /**
855      * @brief set rssi value.
856      *
857      * @param rssi rssi value.
858      * @since 6
859      */
860     void SetRSSI(int8_t rssi);
861 
862     /**
863      * @brief set rssi value.
864      *
865      * @param [in] rssi value.
866      */
867     bool IsConnectable() const;
868 
869     /**
870      * @brief set rssi value.
871      *
872      * @param [in] rssi value.
873      */
874     void SetConnectable(bool connectable);
875 
876     /**
877      * @brief Parse advertisement data.
878      *
879      * @param payload Advertisement packet.
880      * @param totalLen Advertisement packet total len.
881      * @since 6
882      */
883     void ParseAdvertiserment(BlePeripheralDeviceParseAdvData &parseAdvData);
884 
885     /**
886      * @brief Build advertisement data.
887      *
888      * @param advType Advertisement packet type.
889      * @param payload Advertisement packet.
890      * @param length Advertisement packet len.
891      *
892      * @since 6
893      */
894     void BuildAdvertiserData(uint8_t advType, BlePeripheralDeviceParseAdvData &parseAdvData);
895 
896     /**
897      * @brief Set service uuid 16 bit data.
898      *
899      * @param payload Advertisement packet.
900      * @param total_len Advertisement packet len.
901      * @since 6
902      */
903     void SetServiceUUID16Bits(BlePeripheralDeviceParseAdvData &parseAdvData);
904 
905     /**
906      * @brief Set service uuid 32 bit data.
907      *
908      * @param payload Advertisement packet.
909      * @param total_len Advertisement packet len.
910      * @since 6
911      */
912     void SetServiceUUID32Bits(BlePeripheralDeviceParseAdvData &parseAdvData);
913 
914     /**
915      * @brief Set service uuid 128 bit data.
916      *
917      * @param payload Advertisement packet.
918      * @param total_len Advertisement packet len.
919      * @since 6
920      */
921     void SetServiceUUID128Bits(const BlePeripheralDeviceParseAdvData &parseAdvData);
922 
923     /**
924      * @brief Set service data uuid 16 bit data.
925      *
926      * @param payload Advertisement packet.
927      * @param total_len Advertisement packet len.
928      * @since 6
929      */
930     void SetServiceDataUUID16Bits(BlePeripheralDeviceParseAdvData &parseAdvData);
931 
932     /**
933      * @brief Set service data uuid 32 bit data.
934      *
935      * @param payload Advertisement packet.
936      * @param total_len Advertisement packet len.
937      * @since 6
938      */
939     void SetServiceDataUUID32Bits(BlePeripheralDeviceParseAdvData &parseAdvData);
940 
941     /**
942      * @brief Set service data uuid 128 bit data.
943      *
944      * @param payload Advertisement packet.
945      * @param total_len Advertisement packet len.
946      * @since 6
947      */
948     void SetServiceDataUUID128Bits(BlePeripheralDeviceParseAdvData &parseAdvData);
949 
950     /**
951      * @brief Set device name.
952      *
953      * @param name Device name.
954      * @since 6
955      */
956     void SetName(const std::string &name);
957 
958     /**
959      * @brief Set device roles.
960      *
961      * @param roles Device roles.
962      * @since 6
963      */
964     void SetRoles(uint8_t roles);
965 
966     /**
967      * @brief Set bonded from local.
968      *
969      * @param flag Advertiser flag.
970      * @since 6
971      */
972     void SetBondedFromLocal(bool flag);
973 
974     /**
975      * @brief Set acl connect state.
976      *
977      * @param connectState Acl connect state.
978      * @since 6
979      */
980     void SetAclConnectState(int connectState);
981 
982     /**
983      * @brief Set acl connection handle.
984      *
985      * @param handle Acl connection handle.
986      * @since 6
987      */
988     void SetConnectionHandle(int handle);
989 
990     /**
991      * @brief Check if device acl connected.
992      *
993      * @return Returns <b>true</b> if device acl connected;
994      *         returns <b>false</b> if device does not acl connect.
995      * @since 6
996      */
997     bool IsAclConnected() const;
998 
999     /**
1000      * @brief Check if device acl Encrypted.
1001      *
1002      * @return Returns <b>true</b> if device acl Encrypted;
1003      *         returns <b>false</b> if device does not acl Encrypt.
1004      * @since 6
1005      */
1006     bool IsAclEncrypted() const;
1007 
1008     /**
1009      * @brief Check if device was bonded from local.
1010      *
1011      * @return Returns <b>true</b> if device was bonded from local;
1012      *         returns <b>false</b> if device was not bonded from local.
1013      * @since 6
1014      */
1015     bool IsBondedFromLocal() const;
1016 
1017     /**
1018      * @brief Get acl connection handle.
1019      *
1020      * @return Returns acl connection handle;
1021      * @since 6
1022      */
1023     int GetConnectionHandle() const;
1024 
1025     /**
1026      * @brief Get device type.
1027      *
1028      * @return Returns device type.
1029      * @since 6
1030      */
1031     uint8_t GetDeviceType() const;
1032 
1033     /**
1034      * @brief Get advertising flag.
1035      *
1036      * @return Returns advertising flag.
1037      * @since 6
1038      */
1039     uint8_t GetAdFlag() const;
1040 
1041     /**
1042      * @brief Get paired status.
1043      *
1044      * @return Returns paired status.
1045      * @since 6
1046      */
1047     uint8_t GetPairedStatus() const;
1048 
1049     /**
1050      * @brief Set paired status.
1051      *
1052      * @param status Paired status.
1053      * @return Returns <b>true</b> if the operation is successful;
1054      *         returns <b>false</b> if the operation fails.
1055      * @since 6
1056      */
1057     bool SetPairedStatus(uint8_t status);
1058 
1059     /**
1060      * @brief Set alias name.
1061      *
1062      * @param name Device alias name.
1063      * @since 6
1064      */
1065     void SetAliasName(const std::string &name);
1066 
1067     /**
1068      * @brief Get alias name.
1069      *
1070      * @return Returns alias name.
1071      * @since 6
1072      */
1073     std::string GetAliasName() const;
1074 
1075     /**
1076      * @brief Set IO capability.
1077      *
1078      * @param io IO capability
1079      * @since 6
1080      */
1081     void SetIoCapability(uint8_t io);
1082 
1083     /**
1084      * @brief Get IO capability.
1085      *
1086      * @return Returns IO capability.
1087      * @since 6
1088      */
1089     uint8_t GetIoCapability() const;
1090 
1091     /**
1092      * @brief Set manufacturer data.
1093      *
1094      * @param manufacturerData Manufacturer data.
1095      * @since 6
1096      */
1097     void SetManufacturerData(std::string manufacturerData);
1098 
1099 /**
1100  * @brief Sets adv event type.
1101  *
1102  * @param peer adv event type.
1103  * @since 11
1104  */
1105     void SetEventType(uint16_t eventType);
1106 
1107 /**
1108  * @brief Check whether adv event type is included.
1109  *
1110  * return Returns <b>true</b> if event type is included.
1111  *        Returns <b>false</b> otherwisee.
1112  * @since 11
1113  */
1114 bool IsEventType() const;
1115 
1116 /**
1117  * @brief Get adv event type.
1118  *
1119  * @return adv event type
1120  * @since 11
1121  */
1122 uint16_t GetEventType() const;
1123 
1124 private:
1125     /**
1126      * @brief Set advertising flag.
1127      *
1128      * @param adFlag Advertising flag.
1129      * @since 6
1130      */
1131     void SetAdFlag(uint8_t adFlag);
1132 
1133     /**
1134      * @brief Set appearance.
1135      *
1136      * @param appearance Appearance.
1137      * @since 6
1138      */
1139     void SetAppearance(uint16_t appearance);
1140 
1141     /**
1142      * @brief Set service data UUID.
1143      *
1144      * @param uuid Service data UUID.
1145      * @since 6
1146      */
1147     void SetServiceDataUUID(Uuid uuid, std::string data);
1148 
1149     /**
1150      * @brief Set service  UUID.
1151      *
1152      * @param serviceUUID Service  UUID.
1153      * @since 6
1154      */
1155     void SetServiceUUID(Uuid serviceUUID);
1156     /**
1157      * @brief Set TX power.
1158      *
1159      * @param txPower TX power.
1160      * @since 6
1161      */
1162     void SetTXPower(int8_t txPower);
1163 
1164     /// include appearance?
1165     bool isAppearance_ = false;
1166     /// include Manufacturer Data?
1167     bool isManufacturerData_ = false;
1168     /// include device name?
1169     bool isName_ = false;
1170     /// include rssi value?
1171     bool isRSSI_ = false;
1172     /// include service data?
1173     bool isServiceData_ = false;
1174     /// include service uuid?
1175     bool isServiceUUID_ = false;
1176     /// include tx power?
1177     bool isTXPower_ = false;
1178     /// peer roles
1179     uint8_t roles_ = 0;
1180     /// device address
1181     RawAddress address_ = RawAddress("00:00:00:00:00:00");
1182     /// device address
1183     RawAddress currentAddr_ = RawAddress("00:00:00:00:00:00");
1184     /// advertising flag
1185     uint8_t adFlag_ = 0;
1186     /// appearance
1187     uint16_t appearance_ = 0;
1188     /// manufacturer Data
1189     std::map<uint16_t, std::string> manufacturerData_ {};
1190     /// device name
1191     std::string name_ {};
1192     /// rssi value
1193     int8_t rssi_ = 0;
1194     /// service uuid
1195     std::vector<Uuid> serviceUUIDs_ {};
1196     /// tx power
1197     int8_t txPower_ {};
1198     /// service data
1199     std::vector<std::string> serviceData_ {};
1200     /// service data uuid
1201     std::vector<Uuid> serviceDataUUIDs_ {};
1202     /// address type
1203     uint8_t addressType_ = BLE_ADDR_TYPE_RANDOM;
1204     int aclConnected_ = 0;
1205     int connectionHandle_ = 0;
1206     bool bondFlag_ = false;
1207     uint8_t pairState_ {};
1208     uint8_t ioCapability_ {};
1209     std::string aliasName_ {};
1210     bool connectable_ = true;
1211     uint8_t* payload_ {};
1212     size_t payloadLen_ = 0;
1213     // include eventType value
1214     bool isEventType_ = false;
1215     uint16_t eventType_ = BLE_LEGACY_ADV_NONCONN_IND_WITH_EX_ADV;
1216 };
1217 
1218 /**
1219  * @brief Represents scan result.
1220  *
1221  * @since 6
1222  */
1223 class BleScanResultImpl {
1224 public:
1225     /**
1226      * @brief A constructor used to create a <b>BleScanResultInternal</b> instance.
1227      *
1228      * @since 6
1229      */
BleScanResultImpl()1230     BleScanResultImpl() : peripheralDevice_()
1231     {}
1232 
1233     /**
1234      * @brief A destructor used to delete the <b>BleScanResultInternal</b> instance.
1235      *
1236      * @since 6
1237      */
~BleScanResultImpl()1238     ~BleScanResultImpl()
1239     {}
1240 
1241     /**
1242      * @brief Get peripheral device.
1243      *
1244      * @return Returns peripheral device pointer.
1245      * @since 6
1246      */
1247     BlePeripheralDevice GetPeripheralDevice() const;
1248 
1249     /**
1250      * @brief Set peripheral device.
1251      *
1252      * @param dev Peripheral device.
1253      * @since 6
1254      */
1255     void SetPeripheralDevice(const BlePeripheralDevice &dev);
1256 
1257     /**
1258      * @brief Get service uuids.
1259      *
1260      * @return Returns service uuids.
1261      * @since 6
1262      */
GetServiceUuids() const1263     std::vector<Uuid> GetServiceUuids() const
1264     {
1265         return serviceUuids_;
1266     }
1267 
1268     /**
1269      * @brief Get manufacture data.
1270      *
1271      * @return Returns manufacture data.
1272      * @since 6
1273      */
GetManufacturerData() const1274     std::map<uint16_t, std::string> GetManufacturerData() const
1275     {
1276         return manufacturerSpecificData_;
1277     }
1278 
1279     /**
1280      * @brief Get service data.
1281      *
1282      * @return Returns service data.
1283      * @since 6
1284      */
GetServiceData() const1285     std::map<Uuid, std::string> GetServiceData() const
1286     {
1287         return serviceData_;
1288     }
1289 
1290     /**
1291      * @brief Get peer device rssi.
1292      *
1293      * @return Returns peer device rssi.
1294      * @since 6
1295      */
GetRssi() const1296     int8_t GetRssi() const
1297     {
1298         return rssi_;
1299     }
1300 
1301     /**
1302      * @brief Check if device is connectable.
1303      *
1304      * @return Returns <b>true</b> if device is connectable;
1305      *         returns <b>false</b> if device is not connectable.
1306      * @since 6
1307      */
IsConnectable() const1308     bool IsConnectable() const
1309     {
1310         return connectable_;
1311     }
1312 
1313     /**
1314      * @brief Get advertiser flag.
1315      *
1316      * @return Returns advertiser flag.
1317      * @since 6
1318      */
GetAdvertiseFlag() const1319     uint8_t GetAdvertiseFlag() const
1320     {
1321         return advertiseFlag_;
1322     }
1323 
1324     /**
1325      * @brief Add manufacture data.
1326      *
1327      * @param manufacturerId Manufacture Id which addad data.
1328      * @since 6
1329      */
AddManufacturerData(uint16_t manufacturerId, std::string data)1330     void AddManufacturerData(uint16_t manufacturerId, std::string data)
1331     {
1332         manufacturerSpecificData_.insert(std::make_pair(manufacturerId, data));
1333     }
1334 
1335     /**
1336      * @brief Add service data.
1337      *
1338      * @param uuid Uuid of service data.
1339      * @param serviceData Service data.
1340      * @since 6
1341      */
AddServiceData(Uuid uuid, std::string serviceData)1342     void AddServiceData(Uuid uuid, std::string serviceData)
1343     {
1344         serviceData_.insert(std::make_pair(uuid, serviceData));
1345     }
1346 
1347     /**
1348      * @brief Add service uuid.
1349      *
1350      * @param serviceUuid Service uuid.
1351      * @since 6
1352      */
AddServiceUuid(const Uuid &serviceUuid)1353     void AddServiceUuid(const Uuid &serviceUuid)
1354     {
1355         serviceUuids_.push_back(serviceUuid);
1356     }
1357 
1358     /**
1359      * @brief Set peripheral device.
1360      *
1361      * @param device Remote device.
1362      * @since 6
1363      */
SetPeripheralDevice(const RawAddress &device)1364     void SetPeripheralDevice(const RawAddress &device)
1365     {
1366         addr_ = device;
1367     }
1368 
1369     /**
1370      * @brief Set peer device rssi.
1371      *
1372      * @param rssi Peer device rssi.
1373      * @since 6
1374      */
SetRssi(int8_t rssi)1375     void SetRssi(int8_t rssi)
1376     {
1377         rssi_ = rssi;
1378     }
1379 
1380     /**
1381      * @brief Set connectable.
1382      *
1383      * @param connectable Whether it is connectable.
1384      * @since 6
1385      */
SetConnectable(bool connectable)1386     void SetConnectable(bool connectable)
1387     {
1388         connectable_ = connectable;
1389     }
1390 
1391     /**
1392      * @brief Set advertiser flag.
1393      *
1394      * @param flag Advertiser flag.
1395      * @since 6
1396      */
SetAdvertiseFlag(uint8_t flag)1397     void SetAdvertiseFlag(uint8_t flag)
1398     {
1399         advertiseFlag_ = flag;
1400     }
1401 
1402 private:
1403     /// scan device results
1404     BlePeripheralDevice peripheralDevice_;
1405     std::vector<Uuid> serviceUuids_ {};
1406     std::map<uint16_t, std::string> manufacturerSpecificData_ {};
1407     std::map<Uuid, std::string> serviceData_ {};
1408     RawAddress addr_ {};
1409     int8_t rssi_ {};
1410     bool connectable_ {};
1411     uint8_t advertiseFlag_ {};
1412 };
1413 
1414 struct BleActiveDeviceInfoImpl {
1415     std::vector<int8_t> deviceId;
1416     int32_t status;
1417     int32_t timeOut;
1418 };
1419 
1420 struct BleLpDeviceParamSetImpl {
1421     BleScanSettingsImpl scanSettingImpl;
1422     std::vector<BleScanFilterImpl> scanFliterImpls;
1423     BleAdvertiserSettingsImpl advSettingsImpl;
1424     BleAdvertiserDataImpl advDataImpl;
1425     BleAdvertiserDataImpl respDataImpl;
1426     std::vector<BleActiveDeviceInfoImpl> activeDeviceInfoImpls;
1427     int32_t advHandle;
1428     int32_t duration;
1429     int32_t deliveryMode;
1430     uint32_t fieldValidFlagBit;
1431 };
1432 
1433 struct FilterIdxInfo {
1434     FilterIdxInfo() = default;
FilterIdxInfoOHOS::bluetooth::FilterIdxInfo1435     FilterIdxInfo(int pid, int uid, const Uuid &uuid) : pid(pid), uid(uid), uuid(uuid) {}
operator ==OHOS::bluetooth::FilterIdxInfo1436     bool operator == (const FilterIdxInfo &info) const
1437     {
1438         if (pid == info.pid && uid == info.uid && uuid == info.uuid) {
1439             return true;
1440         }
1441         return false;
1442     }
1443 
1444     int32_t pid = 0;
1445     int32_t uid = 0;
1446     Uuid uuid;
1447 };
1448 
1449 }  // namespace bluetooth
1450 }  // namespace OHOS
1451 #endif  /// BLE_SERVICE_DATA_H
1452