• 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 #include "ble_service_data.h"
17 
18 #include <algorithm>
19 
20 namespace bluetooth {
21 /**
22  * @brief Represents scan settings.
23  *
24  * @since 6
25  */
26 
SetReportDelay(long reportDelayMillis)27 void BleScanSettingsImpl::SetReportDelay(long reportDelayMillis)
28 {
29     reportDelayMillis_ = reportDelayMillis;
30 }
31 
32 /**
33  * @brief Get repport delay time.
34  *
35  * @return Returns Repport delay time.
36  * @since 6
37  */
GetReportDelayMillisValue() const38 long BleScanSettingsImpl::GetReportDelayMillisValue() const
39 {
40     return reportDelayMillis_;
41 }
42 
SetScanMode(int scanMode)43 void BleScanSettingsImpl::SetScanMode(int scanMode)
44 {
45     scanMode_ = scanMode;
46 }
47 
GetScanMode() const48 int BleScanSettingsImpl::GetScanMode() const
49 {
50     return scanMode_;
51 }
52 
SetLegacy(bool legacy)53 void BleScanSettingsImpl::SetLegacy(bool legacy)
54 {
55     legacy_ = legacy;
56 }
57 
GetLegacy() const58 bool BleScanSettingsImpl::GetLegacy() const
59 {
60     return legacy_;
61 }
62 
SetPhy(int phy)63 void BleScanSettingsImpl::SetPhy(int phy)
64 {
65     phy_ = phy;
66 }
67 
GetPhy() const68 int BleScanSettingsImpl::GetPhy() const
69 {
70     return phy_;
71 }
72 
73 /**
74  * @brief Check if device service is connectable.
75  *
76  * @return Returns <b>true</b> if device service is connectable;
77  *         returns <b>false</b> if device service is not connectable.
78  * @since 6
79  */
IsConnectable() const80 bool BleAdvertiserSettingsImpl::IsConnectable() const
81 {
82     return connectable_;
83 }
84 
85 /**
86  * @brief Set connectable.
87  *
88  * @param connectable Whether it is connectable.
89  * @since 6
90  */
SetConnectable(bool connectable)91 void BleAdvertiserSettingsImpl::SetConnectable(bool connectable)
92 {
93     connectable_ = connectable;
94 }
95 
96 /**
97  * @brief Check if advertiser is legacy mode.
98  *
99  * @return Returns <b>true</b> if advertiser is legacy mode;
100  *         returns <b>false</b> if advertiser is not legacy mode.
101  * @since 6
102  */
IsLegacyMode() const103 bool BleAdvertiserSettingsImpl::IsLegacyMode() const
104 {
105     return legacyMode_;
106 }
107 
108 /**
109  * @brief Set legacyMode.
110  *
111  * @param connectable Whether it is legacyMode.
112  * @since 6
113  */
SetLegacyMode(bool legacyMode)114 void BleAdvertiserSettingsImpl::SetLegacyMode(bool legacyMode)
115 {
116     legacyMode_ = legacyMode;
117 }
118 
119 /**
120  * @brief Get advertise interval.
121  *
122  * @return Returns advertise interval.
123  * @since 6
124  */
GetInterval() const125 int BleAdvertiserSettingsImpl::GetInterval() const
126 {
127     return interval_;
128 }
129 
130 /**
131  * @brief Set advertise interval.
132  *
133  * @param interval Advertise interval.
134  * @since 6
135  */
SetInterval(int interval)136 void BleAdvertiserSettingsImpl::SetInterval(int interval)
137 {
138     interval_ = interval;
139 }
140 
141 /**
142  * @brief Get advertiser Tx power.
143  *
144  * @return Returns advertiser Tx power.
145  * @since 6
146  */
GetTxPower() const147 int BleAdvertiserSettingsImpl::GetTxPower() const
148 {
149     return txPower_;
150 }
151 
152 /**
153  * @brief Set advertiser Tx power.
154  *
155  * @param txPower Advertiser Tx power.
156  * @since 6
157  */
SetTxPower(int txPower)158 int BleAdvertiserSettingsImpl::SetTxPower(int txPower)
159 {
160     if (txPower > BLE_ADV_TX_POWER_HIGH || txPower < BLE_ADV_TX_POWER_ULTRA_LOW) {
161         return RET_BAD_PARAM;
162     }
163     switch (txPower) {
164         case BLE_ADV_TX_POWER_ULTRA_LOW:
165             txPower_ = BLE_ADV_TX_POWER_ULTRA_LOW_VALUE;
166             break;
167         case BLE_ADV_TX_POWER_LOW:
168             txPower_ = BLE_ADV_TX_POWER_LOW_VALUE;
169             break;
170         case BLE_ADV_TX_POWER_MEDIUM:
171             txPower_ = BLE_ADV_TX_POWER_MEDIUM_VALUE;
172             break;
173         case BLE_ADV_TX_POWER_HIGH:
174             txPower_ = BLE_ADV_TX_POWER_HIGH_VALUE;
175             break;
176         default:
177             txPower_ = BLE_ADV_TX_POWER_LOW_VALUE;
178             break;
179     }
180 
181     return RET_NO_ERROR;
182 }
183 
184 /**
185  * @brief Get primary phy.
186  *
187  * @return Returns primary phy.
188  * @since 6
189  */
GetPrimaryPhy() const190 int BleAdvertiserSettingsImpl::GetPrimaryPhy() const
191 {
192     return primaryPhy_;
193 }
194 
195 /**
196  * @brief Set primary phy.
197  *
198  * @param primaryPhy Primary phy.
199  * @since 6
200  */
SetPrimaryPhy(int primaryPhy)201 void BleAdvertiserSettingsImpl::SetPrimaryPhy(int primaryPhy)
202 {
203     primaryPhy_ = primaryPhy;
204 }
205 
206 /**
207  * @brief Get second phy.
208  *
209  * @return Returns primary phy.
210  * @since 6
211  */
GetSecondaryPhy() const212 int BleAdvertiserSettingsImpl::GetSecondaryPhy() const
213 {
214     return secondaryPhy_;
215 }
216 
217 /**
218  * @brief Set second phy.
219  *
220  * @param secondaryPhy Second phy.
221  * @since 6
222  */
SetSecondaryPhy(int secondaryPhy)223 void BleAdvertiserSettingsImpl::SetSecondaryPhy(int secondaryPhy)
224 {
225     secondaryPhy_ = secondaryPhy;
226 }
227 
228 /**
229  * @brief A constructor used to create a <b>BleAdvertiseDataInternal</b> instance.
230  *
231  * @since 6
232  */
BleAdvertiserDataImpl()233 BleAdvertiserDataImpl::BleAdvertiserDataImpl() : payload_()
234 {}
235 
236 /**
237  * @brief Add manufacture data.
238  *
239  * @param manufacturerId Manufacture Id which addad data.
240  * @param data Manufacture data
241  * @since 6
242  */
AddManufacturerData(uint16_t manufacturerId,const std::string & data)243 int BleAdvertiserDataImpl::AddManufacturerData(uint16_t manufacturerId, const std::string &data)
244 {
245     char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
246     cdata[0] = (char)(manufacturerId & 0xFF);
247     cdata[1] = (char)((manufacturerId >> BLE_ONE_BYTE_LEN) & 0xFF);
248     SetManufacturerData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) + data);
249 
250     return RET_NO_ERROR;
251 }
252 
253 /**
254  * @brief Add service data.
255  *
256  * @param uuid Uuid of service data.
257  * @param data Service data.
258  * @since 6
259  */
AddServiceData(const Uuid & uuid,const std::string & data)260 void BleAdvertiserDataImpl::AddServiceData(const Uuid &uuid, const std::string &data)
261 {
262     char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
263     switch (uuid.GetUuidType()) {
264         case Uuid::UUID16_BYTES_TYPE: {
265             /// [Len] [0x16] [UUID16] data
266             cdata[0] = data.length() + BLE_UUID_LEN_16 + 1;
267             cdata[1] = BLE_AD_TYPE_SERVICE_DATA;  /// 0x16
268             uint16_t uuid16 = uuid.ConvertTo16Bits();
269             AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) +
270                     std::string(reinterpret_cast<char *>(&uuid16), BLE_UUID_LEN_16) + data);
271             break;
272         }
273 
274         case Uuid::UUID32_BYTES_TYPE: {
275             /// [Len] [0x20] [UUID32] data
276             cdata[0] = data.length() + BLE_UUID_LEN_32 + 1;
277             cdata[1] = BLE_AD_TYPE_32SERVICE_DATA;  /// 0x20
278             uint32_t uuid32 = uuid.ConvertTo32Bits();
279             AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) +
280                     std::string(reinterpret_cast<char *>(&uuid32), BLE_UUID_LEN_32) + data);
281             break;
282         }
283 
284         case Uuid::UUID128_BYTES_TYPE: {
285             /// [Len] [0x21] [UUID128] data
286             cdata[0] = data.length() + BLE_UUID_LEN_128 + 1;
287             cdata[1] = BLE_AD_TYPE_128SERVICE_DATA;  /// 0x21
288             uint8_t uuidData[BLE_UUID_LEN_128];
289             uuid.ConvertToBytesLE(uuidData);
290             AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) +
291                     std::string(reinterpret_cast<char *>(uuidData), BLE_UUID_LEN_128) + data);
292             break;
293         }
294 
295         default:
296             return;
297     }
298 }
299 
300 /**
301  * @brief Add characteristic value.
302  *
303  * @param adtype Type of the field.
304  * @param data Field data.
305  * @since 6
306  */
AddCharacteristicValue(uint8_t adtype,const std::string & data)307 void BleAdvertiserDataImpl::AddCharacteristicValue(uint8_t adtype, const std::string &data)
308 {
309     char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
310     cdata[0] = data.length() + 1;
311     cdata[1] = adtype;
312     AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) + data);
313 }
314 
315 /**
316  * @brief Add service uuid.
317  *
318  * @param uuid Service uuid.
319  * @since 6
320  */
AddServiceUuid(const Uuid & uuid)321 void BleAdvertiserDataImpl::AddServiceUuid(const Uuid &uuid)
322 {
323     SetCompleteServices(uuid);
324 }
325 
326 /**
327  * @brief Set device appearance.
328  *
329  * @param appearance Device appearance.
330  * @since 6
331  */
SetAppearance(uint16_t appearance)332 void BleAdvertiserDataImpl::SetAppearance(uint16_t appearance)
333 {
334     char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
335     cdata[0] = BLE_ADV_DATA_BYTE_FIELD_LEN;
336     cdata[1] = BLE_AD_TYPE_APPEARANCE;  /// 0x19
337     AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) +
338             std::string(reinterpret_cast<char *>(&appearance), BLE_ADV_DATA_FIELD_TYPE_AND_LEN));
339 }
340 
341 /**
342  * @brief Set complete services.
343  *
344  * @param uuid Service uuid.
345  * @since 6
346  */
SetCompleteServices(const Uuid & uuid)347 void BleAdvertiserDataImpl::SetCompleteServices(const Uuid &uuid)
348 {
349     char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
350     switch (uuid.GetUuidType()) {
351         case Uuid::UUID16_BYTES_TYPE: {
352             /// [Len] [0x02] [LL] [HH]
353             cdata[0] = BLE_UUID_LEN_16 + 1;
354             cdata[1] = BLE_AD_TYPE_16SRV_CMPL;  /// 0x03
355             uint16_t uuid16 = uuid.ConvertTo16Bits();
356             AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) +
357                     std::string(reinterpret_cast<char *>(&uuid16), BLE_UUID_LEN_16));
358             break;
359         }
360 
361         case Uuid::UUID32_BYTES_TYPE: {
362             /// [Len] [0x04] [LL] [LL] [HH] [HH]
363             cdata[0] = BLE_UUID_LEN_32 + 1;
364             cdata[1] = BLE_AD_TYPE_32SRV_CMPL;  /// 0x05
365             uint32_t uuid32 = uuid.ConvertTo32Bits();
366             AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) +
367                     std::string(reinterpret_cast<char *>(&uuid32), BLE_UUID_LEN_32));
368             break;
369         }
370 
371         case Uuid::UUID128_BYTES_TYPE: {
372             /// [Len] [0x04] [0] [1] ... [15]
373             cdata[0] = BLE_UUID_LEN_128 + 1;
374             cdata[1] = BLE_AD_TYPE_128SRV_CMPL;  /// 0x07
375             uint8_t uuidData[BLE_UUID_LEN_128];
376             uuid.ConvertToBytesLE(uuidData);
377             AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) +
378                     std::string(reinterpret_cast<char *>(uuidData), BLE_UUID_LEN_128));
379             break;
380         }
381 
382         default:
383             return;
384     }
385 }
386 
387 /**
388  * @brief Set advertiser flag.
389  *
390  * @param flag Advertiser flag.
391  * @since 6
392  */
SetFlags(uint8_t flag)393 void BleAdvertiserDataImpl::SetFlags(uint8_t flag)
394 {
395     char cdata[BLE_ADV_DATA_BYTE_FIELD_LEN];
396     cdata[0] = BLE_ADV_DATA_FIELD_TYPE_AND_LEN;
397     cdata[1] = BLE_AD_TYPE_FLAG;  /// 0x01
398     cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN] = flag;
399     advFlag_ = flag;
400     AddData(std::string(cdata, BLE_ADV_DATA_BYTE_FIELD_LEN));
401 }
402 
GetFlags() const403 uint8_t BleAdvertiserDataImpl::GetFlags() const
404 {
405     return advFlag_;
406 }
407 
408 /**
409  * @brief Set manufacture data.
410  *
411  * @param data Manufacture data.
412  * @since 6
413  */
SetManufacturerData(const std::string & data)414 void BleAdvertiserDataImpl::SetManufacturerData(const std::string &data)
415 {
416     char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
417     cdata[0] = data.length() + 1;
418     cdata[1] = BLE_AD_MANUFACTURER_SPECIFIC_TYPE;  /// 0xff
419     AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) + data);
420 }
421 
422 /**
423  * @brief Set device name.
424  *
425  * @param name Device name.
426  * @since 6
427  */
SetDeviceName(const std::string & name)428 void BleAdvertiserDataImpl::SetDeviceName(const std::string &name)
429 {
430     if (name.length() > DEVICE_NAME_MAX_LEN) {
431         SetLongName(name);
432     } else {
433         SetShortName(name);
434     }
435 }
436 
437 /**
438  * @brief Set Tx power level.
439  *
440  * @param txPowerLevel Tx power level.
441  * @since 6
442  */
SetTxPowerLevel(uint8_t txPowerLevel)443 void BleAdvertiserDataImpl::SetTxPowerLevel(uint8_t txPowerLevel)
444 {
445     char cdata[BLE_ADV_DATA_BYTE_FIELD_LEN];
446     cdata[0] = BLE_ADV_DATA_FIELD_TYPE_AND_LEN;
447     cdata[1] = BLE_AD_TYPE_TX_PWR;  /// 0x09
448     cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN] = txPowerLevel;
449     AddData(std::string(cdata, BLE_ADV_DATA_BYTE_FIELD_LEN));
450 }
451 
452 /**
453  * @brief Add service data.
454  *
455  * @param data Service data.
456  * @since 6
457  */
AddData(std::string data)458 void BleAdvertiserDataImpl::AddData(std::string data)
459 {
460     payload_.append(data);
461 }
462 
463 /**
464  * @brief Get advertiser data packet.
465  *
466  * @return Returns advertiser data packet.
467  * @since 6
468  */
SetPayload(const std::string & payload)469 void BleAdvertiserDataImpl::SetPayload(const std::string &payload)
470 {
471     payload_ = payload;
472 }
473 
474 /**
475  * @brief Get advertiser data packet.
476  *
477  * @return Returns advertiser data packet.
478  * @since 6
479  */
GetPayload() const480 std::string BleAdvertiserDataImpl::GetPayload() const
481 {
482     return payload_;
483 }
484 
485 /**
486  * @brief Set advertiser data long name.
487  *
488  * @param name Bluetooth device name.
489  * @since 6
490  */
SetLongName(const std::string & name)491 void BleAdvertiserDataImpl::SetLongName(const std::string &name)
492 {
493     char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
494     cdata[0] = name.length() + 1;
495     cdata[1] = BLE_AD_TYPE_NAME_SHORT;  /// 0x08
496     AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) + name);
497 }
498 
499 /**
500  * @brief Set advertiser data short name
501  *
502  * @param name Bluetooth device name.
503  * @since 6
504  */
SetShortName(const std::string & name)505 void BleAdvertiserDataImpl::SetShortName(const std::string &name)
506 {
507     char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
508     cdata[0] = name.length() + 1;
509     cdata[1] = BLE_AD_TYPE_NAME_CMPL;  /// 0x09
510     AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) + name);
511 }
512 
BlePeripheralDevice()513 BlePeripheralDevice::BlePeripheralDevice()
514     : manufacturerData_(),
515       name_(),
516       serviceUUIDs_(),
517       txPower_(BLE_ADDR_TYPE_RANDOM),
518       serviceData_(),
519       serviceDataUUIDs_(),
520       pairState_(BLE_PAIR_NONE),
521       ioCapability_(BLE_IO_CAP_NONE),
522       aliasName_()
523 {
524     manufacturerData_.clear();
525     serviceDataUUIDs_.clear();
526     serviceData_.clear();
527     serviceUUIDs_.clear();
528 }
529 
~BlePeripheralDevice()530 BlePeripheralDevice::~BlePeripheralDevice()
531 {
532     manufacturerData_.clear();
533     serviceUUIDs_.clear();
534     serviceData_.clear();
535     serviceDataUUIDs_.clear();
536 }
537 
538 /**
539  * @brief Get device address.
540  *
541  * @return Returns device address.
542  * @since 6
543  */
GetRawAddress() const544 RawAddress BlePeripheralDevice::GetRawAddress() const
545 {
546     return address_;
547 }
548 
549 /**
550  * @brief Get device Appearance.
551  *
552  * @return Returns device Appearance.
553  * @since 6
554  */
GetAppearance() const555 uint16_t BlePeripheralDevice::GetAppearance() const
556 {
557     return appearance_;
558 }
559 
560 /**
561  * @brief Get Manufacturer Data.
562  *
563  * @return Returns Manufacturer Data.
564  * @since 6
565  */
GetManufacturerData() const566 std::map<uint16_t, std::string> BlePeripheralDevice::GetManufacturerData() const
567 {
568     return manufacturerData_;
569 }
570 
571 /**
572  * @brief Get device Name.
573  *
574  * @return Returns device Name.
575  * @since 6
576  */
GetName() const577 std::string BlePeripheralDevice::GetName() const
578 {
579     return name_;
580 }
581 
582 /**
583  * @brief Get device RSSI.
584  *
585  * @return Returns device RSSI.
586  * @since 6
587  */
GetRSSI() const588 int8_t BlePeripheralDevice::GetRSSI() const
589 {
590     return rssi_;
591 }
592 
593 /**
594  * @brief Get service Data.
595  *
596  * @return Returns service data.
597  * @since 6
598  */
GetServiceData() const599 std::vector<std::string> BlePeripheralDevice::GetServiceData() const
600 {
601     return serviceData_;
602 }
603 
604 /**
605  * @brief Get Service Data.
606  *
607  * @param index Service data index.
608  * @return Returns service data.
609  * @since 6
610  */
GetServiceData(int index) const611 std::string BlePeripheralDevice::GetServiceData(int index) const
612 {
613     return serviceData_.empty() ? "" : ((size_t)index < serviceData_.size() ? serviceData_[index] : "");
614 }
615 
616 /**
617  * @brief Get service data UUID.
618  *
619  * @return Returns service data UUID.
620  * @since 6
621  */
GetServiceDataUUID() const622 std::vector<Uuid> BlePeripheralDevice::GetServiceDataUUID() const
623 {
624     return serviceDataUUIDs_;
625 }
626 
627 /**
628  * @brief Get service data UUID.
629  *
630  * @param index Service data index.
631  * @return Returns service data UUID.
632  * @since 6
633  */
GetServiceDataUUID(int index) const634 Uuid BlePeripheralDevice::GetServiceDataUUID(int index) const
635 {
636     Uuid uuid {};
637     return serviceDataUUIDs_.empty() ? uuid : serviceDataUUIDs_[index];
638 }
639 
640 /**
641  * @brief Get serviceU UUID.
642  *
643  * @return Returns service UUID.
644  * @since 6
645  */
GetServiceUUID() const646 std::vector<Uuid> BlePeripheralDevice::GetServiceUUID() const
647 {
648     return serviceUUIDs_;
649 }
650 
651 /**
652  * @brief Get service UUID.
653  *
654  * @param index Service UUID index.
655  * @return Return service UUID.
656  * @since 6
657  */
GetServiceUUID(int index) const658 Uuid BlePeripheralDevice::GetServiceUUID(int index) const
659 {
660     Uuid uuid {};
661     return serviceUUIDs_.empty() ? uuid : serviceUUIDs_[index];
662 }
663 
664 /**
665  * @brief Get address type.
666  *
667  * @return Returns address type.
668  * @since 6
669  */
GetAddressType() const670 int BlePeripheralDevice::GetAddressType() const
671 {
672     return addressType_;
673 }
674 
675 /**
676  * @brief Set address type.
677  *
678  * @param type Address type.
679  * @since 6
680  */
SetAddressType(int type)681 void BlePeripheralDevice::SetAddressType(int type)
682 {
683     addressType_ = type;
684 }
685 
686 /**
687  * @brief Check if include manufacture data.
688  *
689  * @return Returns <b>true</b> if include manufacture data;
690  *         returns <b>false</b> if do not include manufacture data.
691  * @since 6
692  */
IsManufacturerData() const693 bool BlePeripheralDevice::IsManufacturerData() const
694 {
695     return isManufacturerData_;
696 }
697 
698 /**
699  * @brief Check if include device rssi.
700  *
701  * @return Returns <b>true</b> if include device rssi;
702  *         returns <b>false</b> if do not include device rssi.
703  * @since 6
704  */
IsRSSI() const705 bool BlePeripheralDevice::IsRSSI() const
706 {
707     return isRSSI_;
708 }
709 
710 /**
711  * @brief Check if include service data.
712  *
713  * @return Returns <b>true</b> if include service data;
714  *         returns <b>false</b> if do not include service data.
715  * @since 6
716  */
IsServiceData() const717 bool BlePeripheralDevice::IsServiceData() const
718 {
719     return isServiceData_;
720 }
721 
722 /**
723  * @brief Check if include service UUID.
724  *
725  * @return Returns <b>true</b> if include service UUID;
726  *         returns <b>false</b> if do not include service UUID.
727  * @since 6
728  */
IsServiceUUID() const729 bool BlePeripheralDevice::IsServiceUUID() const
730 {
731     return isServiceUUID_;
732 }
733 
734 /**
735  * @brief set device address.
736  *
737  * @param address device address.
738  * @since 6
739  */
SetAddress(const RawAddress & address)740 void BlePeripheralDevice::SetAddress(const RawAddress &address)
741 {
742     address_ = address;
743 }
744 
745 /**
746  * @brief set rssi value.
747  *
748  * @param rssi rssi value.
749  * @since 6
750  */
SetRSSI(int8_t rssi)751 void BlePeripheralDevice::SetRSSI(int8_t rssi)
752 {
753     rssi_ = rssi;
754     isRSSI_ = true;
755 }
756 /**
757  * @brief set rssi value.
758  *
759  * @param [in] rssi value.
760  */
IsConnectable() const761 bool BlePeripheralDevice::IsConnectable() const
762 {
763     return connectable_;
764 }
765 /**
766  * @brief set rssi value.
767  *
768  * @param [in] rssi value.
769  */
SetConnectable(bool connectable)770 void BlePeripheralDevice::SetConnectable(bool connectable)
771 {
772     connectable_ = connectable;
773 }
774 /**
775  * @brief Parse advertisement data.
776  *
777  * @param payload Advertisement packet.
778  * @param total_len Advertisement packet len.
779  * @since 6
780  */
ParseAdvertiserment(BlePeripheralDeviceParseAdvData & parseAdvData)781 void BlePeripheralDevice::ParseAdvertiserment(BlePeripheralDeviceParseAdvData &parseAdvData)
782 {
783     size_t sizeConsumed = 0;
784     bool finished = false;
785     size_t totalLength = parseAdvData.length;
786     payload_ = parseAdvData.payload;
787     payloadLen_ = parseAdvData.length;
788 
789     while (!finished) {
790         size_t length = *parseAdvData.payload;
791         sizeConsumed += 1 + length;
792         if (sizeConsumed > totalLength) {
793             break;
794         }
795         parseAdvData.payload++;
796 
797         if (length != 0) {
798             uint8_t advType = *parseAdvData.payload;
799             parseAdvData.payload++;
800             length--;
801             parseAdvData.length = length;
802             BuildAdvertiserData(advType, parseAdvData);
803             parseAdvData.payload += length;
804         }
805         if (sizeConsumed >= totalLength) {
806             finished = true;
807         }
808     }
809 }
810 
BuildAdvertiserData(uint8_t advType,BlePeripheralDeviceParseAdvData & parseAdvData)811 void BlePeripheralDevice::BuildAdvertiserData(uint8_t advType, BlePeripheralDeviceParseAdvData &parseAdvData)
812 {
813     switch (advType) {
814         case BLE_AD_TYPE_NAME_CMPL:   /// Data Type: 0x09
815         case BLE_AD_TYPE_NAME_SHORT:  /// Data Type: 0x08
816             SetName(std::string(reinterpret_cast<char *>(parseAdvData.payload), parseAdvData.length));
817             break;
818         case BLE_AD_TYPE_TX_PWR:  /// Data Type: 0x0A
819             SetTXPower(*parseAdvData.payload);
820             break;
821         case BLE_AD_TYPE_APPEARANCE:  /// Data Type: 0x19
822             SetAppearance(*reinterpret_cast<uint16_t *>(parseAdvData.payload));
823             break;
824         case BLE_AD_TYPE_FLAG:  /// Data Type: 0x01
825             SetAdFlag(*parseAdvData.payload);
826             break;
827         case BLE_AD_TYPE_16SRV_CMPL:
828         case BLE_AD_TYPE_16SRV_PART:  /// Data Type: 0x02
829             SetServiceUUID16Bits(parseAdvData);
830             break;
831         case BLE_AD_TYPE_32SRV_CMPL:
832         case BLE_AD_TYPE_32SRV_PART:  /// Data Type: 0x04
833             SetServiceUUID32Bits(parseAdvData);
834             break;
835         case BLE_AD_TYPE_128SRV_CMPL:  /// Data Type: 0x07
836         case BLE_AD_TYPE_128SRV_PART:  /// Data Type: 0x06
837             SetServiceUUID128Bits(parseAdvData);
838             break;
839         case BLE_AD_MANUFACTURER_SPECIFIC_TYPE:
840             SetManufacturerData(std::string(reinterpret_cast<char *>(parseAdvData.payload), parseAdvData.length));
841             break;
842         case BLE_AD_TYPE_SERVICE_DATA:  /// Data Type: 0x16
843             SetServiceDataUUID16Bits(parseAdvData);
844             break;
845         case BLE_AD_TYPE_32SERVICE_DATA:  /// Data Type: 0x20
846             SetServiceDataUUID32Bits(parseAdvData);
847             break;
848         case BLE_AD_TYPE_128SERVICE_DATA:  /// Data Type: 0x21
849             SetServiceDataUUID128Bits(parseAdvData);
850             break;
851         default:
852             break;
853     }
854 }
855 
SetServiceUUID16Bits(BlePeripheralDeviceParseAdvData & parseAdvData)856 void BlePeripheralDevice::SetServiceUUID16Bits(BlePeripheralDeviceParseAdvData &parseAdvData)
857 {
858     for (size_t var = 0; var < parseAdvData.length / BLE_UUID_LEN_16; ++var) {
859         SetServiceUUID(
860             Uuid::ConvertFrom16Bits(*reinterpret_cast<uint16_t *>(parseAdvData.payload + var * BLE_UUID_LEN_16)));
861     }
862 }
863 
SetServiceUUID32Bits(BlePeripheralDeviceParseAdvData & parseAdvData)864 void BlePeripheralDevice::SetServiceUUID32Bits(BlePeripheralDeviceParseAdvData &parseAdvData)
865 {
866     for (size_t var = 0; var < parseAdvData.length / BLE_UUID_LEN_32; ++var) {
867         SetServiceUUID(
868             Uuid::ConvertFrom32Bits(*reinterpret_cast<uint32_t *>(parseAdvData.payload + var * BLE_UUID_LEN_32)));
869     }
870 }
871 
SetServiceUUID128Bits(const BlePeripheralDeviceParseAdvData & parseAdvData)872 void BlePeripheralDevice::SetServiceUUID128Bits(const BlePeripheralDeviceParseAdvData &parseAdvData)
873 {
874     for (size_t var = 0; var < parseAdvData.length / BLE_UUID_LEN_128; ++var) {
875         std::array<uint8_t, BLE_UUID_LEN_128> data = {};
876         for (int i = 0; i < BLE_UUID_LEN_128; i++) {
877             data[i] = *(parseAdvData.payload + var * BLE_UUID_LEN_128 + i);
878         }
879         SetServiceUUID(Uuid::ConvertFromBytesLE(data.data()));
880     }
881 }
882 
SetServiceDataUUID16Bits(BlePeripheralDeviceParseAdvData & parseAdvData)883 void BlePeripheralDevice::SetServiceDataUUID16Bits(BlePeripheralDeviceParseAdvData &parseAdvData)
884 {
885     if (parseAdvData.length < BLE_UUID_LEN_16) {
886         return;
887     }
888     uint16_t uuid = *(reinterpret_cast<uint16_t *>(parseAdvData.payload));
889     std::string data = "";
890     if (parseAdvData.length > BLE_UUID_LEN_16) {
891         data = std::string(
892             reinterpret_cast<char *>(parseAdvData.payload + BLE_UUID_LEN_16), parseAdvData.length - BLE_UUID_LEN_16);
893     }
894     SetServiceDataUUID(Uuid::ConvertFrom16Bits(uuid), data);
895 }
896 
SetServiceDataUUID32Bits(BlePeripheralDeviceParseAdvData & parseAdvData)897 void BlePeripheralDevice::SetServiceDataUUID32Bits(BlePeripheralDeviceParseAdvData &parseAdvData)
898 {
899     if (parseAdvData.length < BLE_UUID_LEN_32) {
900         return;
901     }
902     uint32_t uuid = *(reinterpret_cast<uint32_t *>(parseAdvData.payload));
903     std::string data = "";
904     if (parseAdvData.length > BLE_UUID_LEN_32) {
905         data = std::string(
906             reinterpret_cast<char *>(parseAdvData.payload + BLE_UUID_LEN_32), parseAdvData.length - BLE_UUID_LEN_32);
907     }
908     SetServiceDataUUID(Uuid::ConvertFrom32Bits(uuid), data);
909 }
910 
SetServiceDataUUID128Bits(BlePeripheralDeviceParseAdvData & parseAdvData)911 void BlePeripheralDevice::SetServiceDataUUID128Bits(BlePeripheralDeviceParseAdvData &parseAdvData)
912 {
913     if (parseAdvData.length < BLE_UUID_LEN_128) {
914         return;
915     }
916     std::string data = "";
917     if (parseAdvData.length > BLE_UUID_LEN_128) {
918         data = std::string(
919             reinterpret_cast<char *>(parseAdvData.payload + BLE_UUID_LEN_128), parseAdvData.length - BLE_UUID_LEN_128);
920     }
921     SetServiceDataUUID(Uuid::ConvertFromBytesLE(parseAdvData.payload), data);
922 }
923 
924 /**
925  * @brief Set device name.
926  *
927  * @param name Device name.
928  * @since 6
929  */
SetName(const std::string & name)930 void BlePeripheralDevice::SetName(const std::string &name)
931 {
932     name_ = name;
933     isName_ = true;
934 }
935 
936 /**
937  * @brief Set device roles.
938  *
939  * @param roles Device roles.
940  * @since 6
941  */
SetRoles(uint8_t roles)942 void BlePeripheralDevice::SetRoles(uint8_t roles)
943 {
944     roles_ = roles;
945 }
946 
947 /**
948  * @brief Set bonded from local.
949  *
950  * @param flag Advertiser flag.
951  * @since 6
952  */
SetBondedFromLocal(bool flag)953 void BlePeripheralDevice::SetBondedFromLocal(bool flag)
954 {
955     bondFlag_ = flag;
956 }
957 
958 /**
959  * @brief Set acl connect state.
960  *
961  * @param connectState Acl connect state.
962  * @since 6
963  */
SetAclConnectState(int connectState)964 void BlePeripheralDevice::SetAclConnectState(int connectState)
965 {
966     aclConnected_ = connectState;
967 }
968 
969 /**
970  * @brief Set acl connection handle.
971  *
972  * @param handle Acl connection handle.
973  * @since 6
974  */
SetConnectionHandle(const int handle)975 void BlePeripheralDevice::SetConnectionHandle(const int handle)
976 {
977     connectionHandle_ = handle;
978 }
979 
980 /**
981  * @brief Check if device acl connected.
982  *
983  * @return Returns <b>true</b> if device acl connected;
984  *         returns <b>false</b> if device does not acl connect.
985  * @since 6
986  */
IsAclConnected() const987 bool BlePeripheralDevice::IsAclConnected() const
988 {
989     if (aclConnected_ != BLE_CONNECTION_STATE_DISCONNECTED) {
990         return true;
991     } else {
992         return false;
993     }
994 }
995 
996 /**
997  * @brief Check if device acl Encrypted.
998  *
999  * @return Returns <b>true</b> if device acl Encrypted;
1000  *         returns <b>false</b> if device does not acl Encrypt.
1001  * @since 6
1002  */
IsAclEncrypted() const1003 bool BlePeripheralDevice::IsAclEncrypted() const
1004 {
1005     if (aclConnected_ > BLE_CONNECTION_STATE_CONNECTED) {
1006         return true;
1007     } else {
1008         return false;
1009     }
1010 }
1011 
1012 /**
1013  * @brief Check if device was bonded from local.
1014  *
1015  * @return Returns <b>true</b> if device was bonded from local;
1016  *         returns <b>false</b> if device was not bonded from local.
1017  * @since 6
1018  */
IsBondedFromLocal() const1019 bool BlePeripheralDevice::IsBondedFromLocal() const
1020 {
1021     return bondFlag_;
1022 }
1023 
1024 /**
1025  * @brief Get acl connection handle.
1026  *
1027  * @return Returns acl connection handle;
1028  * @since 6
1029  */
GetConnectionHandle() const1030 int BlePeripheralDevice::GetConnectionHandle() const
1031 {
1032     return connectionHandle_;
1033 }
1034 
1035 /**
1036  * @brief Get device type.
1037  *
1038  * @return Returns device type.
1039  * @since 6
1040  */
GetDeviceType() const1041 uint8_t BlePeripheralDevice::GetDeviceType() const
1042 {
1043     if (adFlag_ & BLE_ADV_FLAG_BREDR_NOT_SPT) {
1044         return BLE_BT_DEVICE_TYPE_DUMO;
1045     }
1046     return BLE_BT_DEVICE_TYPE_BLE;
1047 }
1048 
1049 /**
1050  * @brief Get advertising flag.
1051  *
1052  * @return Returns advertising flag.
1053  * @since 6
1054  */
GetAdFlag() const1055 uint8_t BlePeripheralDevice::GetAdFlag() const
1056 {
1057     return adFlag_;
1058 }
1059 
1060 /**
1061  * @brief Get paired status.
1062  *
1063  * @return Returns paired status.
1064  * @since 6
1065  */
GetPairedStatus() const1066 uint8_t BlePeripheralDevice::GetPairedStatus() const
1067 {
1068     return pairState_;
1069 }
1070 
1071 /**
1072  * @brief Set paired status.
1073  *
1074  * @param status Paired status.
1075  * @return Returns <b>true</b> if the operation is successful;
1076  *         returns <b>false</b> if the operation fails.
1077  * @since 6
1078  */
SetPairedStatus(uint8_t status)1079 bool BlePeripheralDevice::SetPairedStatus(uint8_t status)
1080 {
1081     if (BLE_PAIR_NONE > status || BLE_PAIR_CANCELING < status) {
1082         return false;
1083     }
1084     if (pairState_ == status) {
1085         return true;
1086     }
1087     pairState_ = status;
1088     return true;
1089 }
1090 
1091 /**
1092  * @brief Set alias name.
1093  *
1094  * @param name Device alias name.
1095  * @since 6
1096  */
SetAliasName(const std::string & name)1097 void BlePeripheralDevice::SetAliasName(const std::string &name)
1098 {
1099     aliasName_ = name;
1100 }
1101 
1102 /**
1103  * @brief Get alias name.
1104  *
1105  * @return Returns alias name.
1106  * @since 6
1107  */
GetAliasName() const1108 std::string BlePeripheralDevice::GetAliasName() const
1109 {
1110     return aliasName_;
1111 }
1112 
1113 /**
1114  * @brief Set IO capability.
1115  *
1116  * @param io IO capability
1117  * @since 6
1118  */
SetIoCapability(uint8_t io)1119 void BlePeripheralDevice::SetIoCapability(uint8_t io)
1120 {
1121     ioCapability_ = io;
1122 }
1123 
1124 /**
1125  * @brief Get IO capability.
1126  *
1127  * @return Returns IO capability.
1128  * @since 6
1129  */
GetIoCapability() const1130 uint8_t BlePeripheralDevice::GetIoCapability() const
1131 {
1132     return ioCapability_;
1133 }
1134 
1135 /**
1136  * @brief Set advertising flag.
1137  *
1138  * @param adFlag Advertising flag.
1139  * @since 6
1140  */
SetAdFlag(uint8_t adFlag)1141 void BlePeripheralDevice::SetAdFlag(uint8_t adFlag)
1142 {
1143     adFlag_ = adFlag;
1144 }
1145 
1146 /**
1147  * @brief Set appearance.
1148  *
1149  * @param appearance Appearance.
1150  * @since 6
1151  */
SetAppearance(uint16_t appearance)1152 void BlePeripheralDevice::SetAppearance(uint16_t appearance)
1153 {
1154     appearance_ = appearance;
1155     isAppearance_ = true;
1156 }
1157 
1158 /**
1159  * @brief Set manufacturer data.
1160  *
1161  * @param manufacturerData Manufacturer data.
1162  * @since 6
1163  */
SetManufacturerData(std::string manufacturerData)1164 void BlePeripheralDevice::SetManufacturerData(std::string manufacturerData)
1165 {
1166     if (manufacturerData.size() > BLE_UUID_LEN_16) {
1167         uint16_t manufacturerId = uint8_t(manufacturerData[0]) | (uint16_t(manufacturerData[1]) << BLE_ONE_BYTE_LEN);
1168         auto iter = manufacturerData_.find(manufacturerId);
1169         if (iter == manufacturerData_.end()) {
1170             manufacturerData_.insert(std::make_pair(manufacturerId, manufacturerData.substr(BLE_UUID_LEN_16)));
1171         }
1172         isManufacturerData_ = true;
1173     } else {
1174         manufacturerData_.clear();
1175         isManufacturerData_ = false;
1176     }
1177 }
1178 
1179 /**
1180  * @brief Set service data UUID.
1181  *
1182  * @param uuid Service data UUID.
1183  * @since 6
1184  */
SetServiceDataUUID(Uuid uuid,std::string data)1185 void BlePeripheralDevice::SetServiceDataUUID(Uuid uuid, std::string data)
1186 {
1187     isServiceData_ = true;
1188     auto iter = std::find(serviceDataUUIDs_.begin(), serviceDataUUIDs_.end(), uuid);
1189     if (iter == serviceDataUUIDs_.end()) {
1190         serviceDataUUIDs_.push_back(uuid);
1191         serviceData_.push_back(data);
1192     }
1193 }
1194 
1195 /**
1196  * @brief Set service  UUID.
1197  *
1198  * @param serviceUUID Service  UUID.
1199  * @since 6
1200  */
SetServiceUUID(Uuid serviceUUID)1201 void BlePeripheralDevice::SetServiceUUID(Uuid serviceUUID)
1202 {
1203     isServiceUUID_ = true;
1204     auto iter = std::find(serviceUUIDs_.begin(), serviceUUIDs_.end(), serviceUUID);
1205     if (iter == serviceUUIDs_.end()) {
1206         serviceUUIDs_.push_back(serviceUUID);
1207     }
1208 }
1209 /**
1210  * @brief Set TX power.
1211  *
1212  * @param txPower TX power.
1213  * @since 6
1214  */
SetTXPower(int8_t txPower)1215 void BlePeripheralDevice::SetTXPower(int8_t txPower)
1216 {
1217     isTXPower_ = true;
1218     txPower_ = txPower;
1219 }
1220 /**
1221  * @brief Get peripheral device.
1222  *
1223  * @return Returns peripheral device pointer.
1224  * @since 6
1225  */
GetPeripheralDevice() const1226 BlePeripheralDevice BleScanResultImpl::GetPeripheralDevice() const
1227 {
1228     return peripheralDevice_;
1229 }
1230 
1231 /**
1232  * @brief Set peripheral device.
1233  *
1234  * @param dev Peripheral device.
1235  * @since 6
1236  */
SetPeripheralDevice(const BlePeripheralDevice & dev)1237 void BleScanResultImpl::SetPeripheralDevice(const BlePeripheralDevice &dev)
1238 {
1239     peripheralDevice_ = dev;
1240 }
1241 
1242 /**
1243  * @brief Get advertiser data packet.
1244  *
1245  * @return Returns advertiser data packet.
1246  * @since 6
1247  */
GetPayload() const1248 uint8_t *BlePeripheralDevice::GetPayload() const
1249 {
1250     return payload_;
1251 }
1252 
1253 /**
1254  * @brief Get advertising packet length.
1255  *
1256  * @return Returns advertising packet length.
1257  * @since 6
1258  */
GetPayloadLen() const1259 size_t BlePeripheralDevice::GetPayloadLen() const
1260 {
1261     return payloadLen_;
1262 }
1263 }  // namespace bluetooth
1264