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 #include "ble_service_data.h"
17
18 #include <algorithm>
19
20 #include "array"
21 #include "map"
22 #include "vector"
23 #include "securec.h"
24 #include "bluetooth_log.h"
25
26 namespace OHOS {
27 namespace bluetooth {
28 /**
29 * @brief Represents scan settings.
30 *
31 * @since 6
32 */
33
SetReportDelay(long reportDelayMillis)34 void BleScanSettingsImpl::SetReportDelay(long reportDelayMillis)
35 {
36 reportDelayMillis_ = reportDelayMillis;
37 }
38
39 /**
40 * @brief Get the report delay time.
41 *
42 * @return Returns Report delay time.
43 * @since 6
44 */
GetReportDelayMillisValue() const45 long BleScanSettingsImpl::GetReportDelayMillisValue() const
46 {
47 return reportDelayMillis_;
48 }
49
SetScanMode(int scanMode)50 void BleScanSettingsImpl::SetScanMode(int scanMode)
51 {
52 scanMode_ = scanMode;
53 }
54
GetScanMode() const55 int BleScanSettingsImpl::GetScanMode() const
56 {
57 return scanMode_;
58 }
59
SetLegacy(bool legacy)60 void BleScanSettingsImpl::SetLegacy(bool legacy)
61 {
62 legacy_ = legacy;
63 }
64
GetLegacy() const65 bool BleScanSettingsImpl::GetLegacy() const
66 {
67 return legacy_;
68 }
69
SetPhy(int phy)70 void BleScanSettingsImpl::SetPhy(int phy)
71 {
72 phy_ = phy;
73 }
74
GetPhy() const75 int BleScanSettingsImpl::GetPhy() const
76 {
77 return phy_;
78 }
79
SetCallbackType(uint8_t callbackType)80 void BleScanSettingsImpl::SetCallbackType(uint8_t callbackType)
81 {
82 callbackType_ = callbackType;
83 }
84
GetCallbackType() const85 uint8_t BleScanSettingsImpl::GetCallbackType() const
86 {
87 return callbackType_;
88 }
89
SetMatchTrackAdvType(uint8_t matchTrackAdvType)90 void BleScanSettingsImpl::SetMatchTrackAdvType(uint8_t matchTrackAdvType)
91 {
92 matchTrackAdvType_ = matchTrackAdvType;
93 }
94
GetMatchTrackAdvType() const95 uint8_t BleScanSettingsImpl::GetMatchTrackAdvType() const
96 {
97 return matchTrackAdvType_;
98 }
99
SetMatchMode(uint8_t matchMode)100 void BleScanSettingsImpl::SetMatchMode(uint8_t matchMode)
101 {
102 matchMode_ = matchMode;
103 }
104
GetMatchMode() const105 uint8_t BleScanSettingsImpl::GetMatchMode() const
106 {
107 return matchMode_;
108 }
109
GetScanInterval() const110 uint16_t BleScanSettingsImpl::GetScanInterval() const
111 {
112 return scanInterval_;
113 }
114
GetScanWindow() const115 uint16_t BleScanSettingsImpl::GetScanWindow() const
116 {
117 return scanWindow_;
118 }
119
SetScanInterval(uint16_t scanInterval)120 void BleScanSettingsImpl::SetScanInterval(uint16_t scanInterval)
121 {
122 scanInterval_ = scanInterval;
123 }
124
SetScanWindow(uint16_t scanWindow)125 void BleScanSettingsImpl::SetScanWindow(uint16_t scanWindow)
126 {
127 scanWindow_ = scanWindow;
128 }
129
SetDeviceId(const std::string & deviceId)130 void BleScanFilterImpl::SetDeviceId(const std::string &deviceId)
131 {
132 deviceId_ = deviceId;
133 }
134
GetDeviceId() const135 std::string BleScanFilterImpl::GetDeviceId() const
136 {
137 return deviceId_;
138 }
139
SetName(const std::string & name)140 void BleScanFilterImpl::SetName(const std::string &name)
141 {
142 name_ = name;
143 }
144
GetName() const145 std::string BleScanFilterImpl::GetName() const
146 {
147 return name_;
148 }
149
SetServiceUuid(const Uuid & serviceUuid)150 void BleScanFilterImpl::SetServiceUuid(const Uuid &serviceUuid)
151 {
152 serviceUuid_ = serviceUuid;
153 hasServiceUuid_ = true;
154 }
155
HasServiceUuid() const156 bool BleScanFilterImpl::HasServiceUuid() const
157 {
158 return hasServiceUuid_;
159 }
160
GetServiceUuid() const161 Uuid BleScanFilterImpl::GetServiceUuid() const
162 {
163 return serviceUuid_;
164 }
165
SetServiceUuidMask(const Uuid & serviceUuidMask)166 void BleScanFilterImpl::SetServiceUuidMask(const Uuid &serviceUuidMask)
167 {
168 serviceUuidMask_ = serviceUuidMask;
169 hasServiceUuidMask_ = true;
170 }
171
HasServiceUuidMask() const172 bool BleScanFilterImpl::HasServiceUuidMask() const
173 {
174 return hasServiceUuidMask_;
175 }
176
GetServiceUuidMask() const177 Uuid BleScanFilterImpl::GetServiceUuidMask() const
178 {
179 return serviceUuidMask_;
180 }
181
SetServiceSolicitationUuid(const Uuid & serviceSolicitationUuid)182 void BleScanFilterImpl::SetServiceSolicitationUuid(const Uuid &serviceSolicitationUuid)
183 {
184 serviceSolicitationUuid_ = serviceSolicitationUuid;
185 hasSolicitationUuid_ = true;
186 }
187
HasSolicitationUuid() const188 bool BleScanFilterImpl::HasSolicitationUuid() const
189 {
190 return hasSolicitationUuid_;
191 }
192
GetServiceSolicitationUuid() const193 Uuid BleScanFilterImpl::GetServiceSolicitationUuid() const
194 {
195 return serviceSolicitationUuid_;
196 }
197
SetServiceSolicitationUuidMask(const Uuid & serviceSolicitationUuidMask)198 void BleScanFilterImpl::SetServiceSolicitationUuidMask(const Uuid &serviceSolicitationUuidMask)
199 {
200 serviceSolicitationUuidMask_ = serviceSolicitationUuidMask;
201 hasSolicitationUuidMask_ = true;
202 }
203
HasSolicitationUuidMask() const204 bool BleScanFilterImpl::HasSolicitationUuidMask() const
205 {
206 return hasSolicitationUuidMask_;
207 }
208
GetServiceSolicitationUuidMask() const209 Uuid BleScanFilterImpl::GetServiceSolicitationUuidMask() const
210 {
211 return serviceSolicitationUuidMask_;
212 }
213
SetServiceData(const std::vector<uint8_t> & serviceData)214 void BleScanFilterImpl::SetServiceData(const std::vector<uint8_t> &serviceData)
215 {
216 serviceData_ = serviceData;
217 }
218
GetServiceData() const219 std::vector<uint8_t> BleScanFilterImpl::GetServiceData() const
220 {
221 return serviceData_;
222 }
223
SetServiceDataMask(const std::vector<uint8_t> & serviceDataMask)224 void BleScanFilterImpl::SetServiceDataMask(const std::vector<uint8_t> &serviceDataMask)
225 {
226 serviceDataMask_ = serviceDataMask;
227 }
228
GetServiceDataMask() const229 std::vector<uint8_t> BleScanFilterImpl::GetServiceDataMask() const
230 {
231 return serviceDataMask_;
232 }
233
SetManufacturerId(uint16_t manufacturerId)234 void BleScanFilterImpl::SetManufacturerId(uint16_t manufacturerId)
235 {
236 manufacturerId_ = manufacturerId;
237 }
238
GetManufacturerId() const239 uint16_t BleScanFilterImpl::GetManufacturerId() const
240 {
241 return manufacturerId_;
242 }
243
SetManufactureData(const std::vector<uint8_t> & manufactureData)244 void BleScanFilterImpl::SetManufactureData(const std::vector<uint8_t> &manufactureData)
245 {
246 manufactureData_ = manufactureData;
247 }
248
GetManufactureData() const249 std::vector<uint8_t> BleScanFilterImpl::GetManufactureData() const
250 {
251 return manufactureData_;
252 }
253
SetManufactureDataMask(const std::vector<uint8_t> & manufactureDataMask)254 void BleScanFilterImpl::SetManufactureDataMask(const std::vector<uint8_t> &manufactureDataMask)
255 {
256 manufactureDataMask_ = manufactureDataMask;
257 }
258
GetManufactureDataMask() const259 std::vector<uint8_t> BleScanFilterImpl::GetManufactureDataMask() const
260 {
261 return manufactureDataMask_;
262 }
263
SetAdvIndReportFlag(bool advIndReport)264 void BleScanFilterImpl::SetAdvIndReportFlag(bool advIndReport)
265 {
266 advIndReport_ = advIndReport;
267 }
268
GetAdvIndReportFlag() const269 bool BleScanFilterImpl::GetAdvIndReportFlag() const
270 {
271 return advIndReport_;
272 }
273
SetClientId(int clientId)274 void BleScanFilterImpl::SetClientId(int clientId)
275 {
276 clientId_ = clientId;
277 }
278
GetClientId() const279 int BleScanFilterImpl::GetClientId() const
280 {
281 return clientId_;
282 }
283
SetFiltIndex(uint8_t filtIndex)284 void BleScanFilterImpl::SetFiltIndex(uint8_t filtIndex)
285 {
286 filtIndex_ = filtIndex;
287 }
288
GetFiltIndex() const289 uint8_t BleScanFilterImpl::GetFiltIndex() const
290 {
291 return filtIndex_;
292 }
293
SetFilterAction(uint8_t action)294 void BleScanFilterImpl::SetFilterAction(uint8_t action)
295 {
296 action_ = action;
297 }
298
GetFilterAction() const299 uint8_t BleScanFilterImpl::GetFilterAction() const
300 {
301 return action_;
302 }
303
304 /**
305 * @brief Check if the device service is connectable.
306 *
307 * @return Returns <b>true</b> if device service is connectable;
308 * Returns <b>false</b> otherwise.
309 * @since 6
310 */
IsConnectable() const311 bool BleAdvertiserSettingsImpl::IsConnectable() const
312 {
313 return connectable_;
314 }
315
316 /**
317 * @brief Set whether the device service is connectable.
318 *
319 * @param connectable Whether the device service is connectable.
320 * @since 6
321 */
SetConnectable(bool connectable)322 void BleAdvertiserSettingsImpl::SetConnectable(bool connectable)
323 {
324 connectable_ = connectable;
325 }
326
327 /**
328 * @brief Check if the advertiser is in legacy mode.
329 *
330 * @return Returns <b>true</b> if the advertiser is in legacy mode;
331 * Returns <b>false</b> otherwisee.
332 * @since 6
333 */
IsLegacyMode() const334 bool BleAdvertiserSettingsImpl::IsLegacyMode() const
335 {
336 return legacyMode_;
337 }
338
339 /**
340 * @brief Set whether to enable the legacy mode.
341 *
342 * @param connectable Whether to enable the legacy mode
343 * @since 6
344 */
SetLegacyMode(bool legacyMode)345 void BleAdvertiserSettingsImpl::SetLegacyMode(bool legacyMode)
346 {
347 legacyMode_ = legacyMode;
348 }
349
350 /**
351 * @brief Get advertise interval.
352 *
353 * @return Returns the advertising interval.
354 * @since 6
355 */
GetInterval() const356 int BleAdvertiserSettingsImpl::GetInterval() const
357 {
358 return interval_;
359 }
360
361 /**
362 * @brief Set advertise interval.
363 *
364 * @param interval Advertise interval.
365 * @since 6
366 */
SetInterval(int interval)367 void BleAdvertiserSettingsImpl::SetInterval(int interval)
368 {
369 interval_ = interval;
370 }
371
372 /**
373 * @brief Get the advertiser Tx power.
374 *
375 * @return Returns advertiser Tx power.
376 * @since 6
377 */
GetTxPower() const378 int8_t BleAdvertiserSettingsImpl::GetTxPower() const
379 {
380 return txPower_;
381 }
382
383 /**
384 * @brief Set the advertiser Tx power.
385 *
386 * @param txPower the advertiser Tx power.
387 * @since 6
388 */
SetTxPower(int8_t txPower)389 void BleAdvertiserSettingsImpl::SetTxPower(int8_t txPower)
390 {
391 txPower_ = txPower;
392 }
393
394 /**
395 * @brief Get the primary phy.
396 *
397 * @return Returns the primary phy.
398 * @since 6
399 */
GetPrimaryPhy() const400 int BleAdvertiserSettingsImpl::GetPrimaryPhy() const
401 {
402 return primaryPhy_;
403 }
404
405 /**
406 * @brief Set the primary phy.
407 *
408 * @param primaryPhy Primary phy.
409 * @since 6
410 */
SetPrimaryPhy(int primaryPhy)411 void BleAdvertiserSettingsImpl::SetPrimaryPhy(int primaryPhy)
412 {
413 primaryPhy_ = primaryPhy;
414 }
415
416 /**
417 * @brief Get the secondary Phy.
418 *
419 * @return Returns primary phy.
420 * @since 6
421 */
GetSecondaryPhy() const422 int BleAdvertiserSettingsImpl::GetSecondaryPhy() const
423 {
424 return secondaryPhy_;
425 }
426
427 /**
428 * @brief Set the secondary phy.
429 *
430 * @param secondaryPhy Secondary Phy.
431 * @since 6
432 */
SetSecondaryPhy(int secondaryPhy)433 void BleAdvertiserSettingsImpl::SetSecondaryPhy(int secondaryPhy)
434 {
435 secondaryPhy_ = secondaryPhy;
436 }
437
438 /**
439 * @brief Get own address.
440 *
441 * @param addr Own address.
442 * @since 6
443 */
GetOwnAddr() const444 std::array<uint8_t, RawAddress::BT_ADDRESS_BYTE_LEN> BleAdvertiserSettingsImpl::GetOwnAddr() const
445 {
446 return ownAddr_;
447 }
448
449 /**
450 * @brief Set own address.
451 *
452 * @param addr Own address.
453 * @since 6
454 */
SetOwnAddr(const std::array<uint8_t,RawAddress::BT_ADDRESS_BYTE_LEN> & addr)455 void BleAdvertiserSettingsImpl::SetOwnAddr(const std::array<uint8_t, RawAddress::BT_ADDRESS_BYTE_LEN>& addr)
456 {
457 ownAddr_ = addr;
458 }
459
460 /**
461 * @brief Get own address type.
462 *
463 * @return Returns own address type.
464 * @since 6
465 */
GetOwnAddrType() const466 int8_t BleAdvertiserSettingsImpl::GetOwnAddrType() const
467 {
468 return ownAddrType_;
469 }
470
471 /**
472 * @brief Set own address type.
473 *
474 * @param addrType Own address type.
475 * @since 6
476 */
SetOwnAddrType(int8_t addrType)477 void BleAdvertiserSettingsImpl::SetOwnAddrType(int8_t addrType)
478 {
479 ownAddrType_ = addrType;
480 }
481
482 /**
483 * @brief A constructor used to create a <b>BleAdvertiseDataInternal</b> instance.
484 *
485 * @since 6
486 */
BleAdvertiserDataImpl()487 BleAdvertiserDataImpl::BleAdvertiserDataImpl() : payload_()
488 {}
489
490 /**
491 * @brief Add manufacturer data.
492 *
493 * @param manufacturerId manufacturer Id which addad data.
494 * @param data manufacturer data
495 * @since 6
496 */
AddManufacturerData(uint16_t manufacturerId,const std::string & data)497 int BleAdvertiserDataImpl::AddManufacturerData(uint16_t manufacturerId, const std::string &data)
498 {
499 char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
500 cdata[0] = static_cast<char>(manufacturerId & 0xFF);
501 cdata[1] = static_cast<char>((manufacturerId >> BLE_ONE_BYTE_LEN) & 0xFF);
502 SetManufacturerData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) + data);
503
504 return RET_NO_ERROR;
505 }
506
507 /**
508 * @brief Add service data.
509 *
510 * @param uuid Uuid of service data.
511 * @param data Service data.
512 * @since 6
513 */
AddServiceData(const Uuid & uuid,const std::string & data)514 void BleAdvertiserDataImpl::AddServiceData(const Uuid &uuid, const std::string &data)
515 {
516 char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
517 switch (uuid.GetUuidType()) {
518 case Uuid::UUID16_BYTES_TYPE: {
519 /// [Len] [0x16] [UUID16] data
520 cdata[0] = data.length() + BLE_UUID_LEN_16 + 1;
521 cdata[1] = BLE_AD_TYPE_SERVICE_DATA; /// 0x16
522 uint16_t uuid16 = uuid.ConvertTo16Bits();
523 AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) +
524 std::string(reinterpret_cast<char *>(&uuid16), BLE_UUID_LEN_16) + data);
525 break;
526 }
527
528 case Uuid::UUID32_BYTES_TYPE: {
529 /// [Len] [0x20] [UUID32] data
530 cdata[0] = data.length() + BLE_UUID_LEN_32 + 1;
531 cdata[1] = BLE_AD_TYPE_32SERVICE_DATA; /// 0x20
532 uint32_t uuid32 = uuid.ConvertTo32Bits();
533 AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) +
534 std::string(reinterpret_cast<char *>(&uuid32), BLE_UUID_LEN_32) + data);
535 break;
536 }
537
538 case Uuid::UUID128_BYTES_TYPE: {
539 /// [Len] [0x21] [UUID128] data
540 cdata[0] = data.length() + BLE_UUID_LEN_128 + 1;
541 cdata[1] = BLE_AD_TYPE_128SERVICE_DATA; /// 0x21
542 uint8_t uuidData[BLE_UUID_LEN_128];
543 uuid.ConvertToBytesLE(uuidData);
544 AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) +
545 std::string(reinterpret_cast<char *>(uuidData), BLE_UUID_LEN_128) + data);
546 break;
547 }
548
549 default:
550 return;
551 }
552 }
553
554 /**
555 * @brief Add characteristic value.
556 *
557 * @param advertiser type of the field.
558 * @param advertiser data.
559 * @since 6
560 */
AddCharacteristicValue(uint8_t adtype,const std::string & data)561 void BleAdvertiserDataImpl::AddCharacteristicValue(uint8_t adtype, const std::string &data)
562 {
563 char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
564 cdata[0] = data.length() + 1;
565 cdata[1] = adtype;
566 AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) + data);
567 }
568
569 /**
570 * @brief Add service uuid.
571 *
572 * @param uuid Service uuid.
573 * @since 6
574 */
AddServiceUuid(const Uuid & uuid)575 void BleAdvertiserDataImpl::AddServiceUuid(const Uuid &uuid)
576 {
577 SetCompleteServices(uuid);
578 }
579
580 /**
581 * @brief Add service uuids.
582 *
583 * @param uuid Service uuids.
584 * @since 12
585 */
AddServiceUuids(const std::vector<Uuid> & uuidVec)586 void BleAdvertiserDataImpl::AddServiceUuids(const std::vector<Uuid> &uuidVec)
587 {
588 if (uuidVec.empty()) {
589 return;
590 }
591 std::string serviceUuid16 = "";
592 std::string serviceUuid32 = "";
593 std::string serviceUuid128 = "";
594
595 char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
596 for (auto &uuid : uuidVec) {
597 if (uuid.GetUuidType() == Uuid::UUID16_BYTES_TYPE) {
598 uint16_t uuid16 = uuid.ConvertTo16Bits();
599 serviceUuid16.append(std::string(reinterpret_cast<char *>(&uuid16), BLE_UUID_LEN_16));
600 } else if (uuid.GetUuidType() == Uuid::UUID32_BYTES_TYPE) {
601 uint32_t uuid32 = uuid.ConvertTo32Bits();
602 serviceUuid32.append(std::string(reinterpret_cast<char *>(&uuid32), BLE_UUID_LEN_32));
603 } else {
604 uint8_t uuidData[BLE_UUID_LEN_128];
605 uuid.ConvertToBytesLE(uuidData);
606 serviceUuid128.append(std::string(reinterpret_cast<char *>(uuidData), BLE_UUID_LEN_128));
607 }
608 }
609
610 if (!serviceUuid16.empty()) {
611 cdata[0] = serviceUuid16.size() + 1; // 1指BLE_AD_TYPE_16SRV_CMPL标识
612 cdata[1] = BLE_AD_TYPE_16SRV_CMPL; // 0x03
613 AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) + serviceUuid16);
614 }
615 if (!serviceUuid32.empty()) {
616 cdata[0] = serviceUuid32.size() + 1; // 1 指 BLE_AD_TYPE_32SRV_CMPL 标识
617 cdata[1] = BLE_AD_TYPE_32SRV_CMPL; // 0x05
618 AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) + serviceUuid32);
619 }
620 if (!serviceUuid128.empty()) {
621 cdata[0] = serviceUuid128.size() + 1; // 1 指 BLE_AD_TYPE_128SRV_CMPL 标识
622 cdata[1] = BLE_AD_TYPE_128SRV_CMPL; // 0x07
623 AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) + serviceUuid128);
624 }
625 }
626
627 /**
628 * @brief Set device appearance.
629 *
630 * @param appearance Device appearance.
631 * @since 6
632 */
SetAppearance(uint16_t appearance)633 void BleAdvertiserDataImpl::SetAppearance(uint16_t appearance)
634 {
635 char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
636 cdata[0] = BLE_ADV_DATA_BYTE_FIELD_LEN;
637 cdata[1] = BLE_AD_TYPE_APPEARANCE; /// 0x19
638 AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) +
639 std::string(reinterpret_cast<char *>(&appearance), BLE_ADV_DATA_FIELD_TYPE_AND_LEN));
640 }
641
642 /**
643 * @brief Set complete services.
644 *
645 * @param uuid Service uuid.
646 * @since 6
647 */
SetCompleteServices(const Uuid & uuid)648 void BleAdvertiserDataImpl::SetCompleteServices(const Uuid &uuid)
649 {
650 char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
651 switch (uuid.GetUuidType()) {
652 case Uuid::UUID16_BYTES_TYPE: {
653 /// [Len] [0x02] [LL] [HH]
654 cdata[0] = BLE_UUID_LEN_16 + 1;
655 cdata[1] = BLE_AD_TYPE_16SRV_CMPL; /// 0x03
656 uint16_t uuid16 = uuid.ConvertTo16Bits();
657 AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) +
658 std::string(reinterpret_cast<char *>(&uuid16), BLE_UUID_LEN_16));
659 break;
660 }
661
662 case Uuid::UUID32_BYTES_TYPE: {
663 /// [Len] [0x04] [LL] [LL] [HH] [HH]
664 cdata[0] = BLE_UUID_LEN_32 + 1;
665 cdata[1] = BLE_AD_TYPE_32SRV_CMPL; /// 0x05
666 uint32_t uuid32 = uuid.ConvertTo32Bits();
667 AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) +
668 std::string(reinterpret_cast<char *>(&uuid32), BLE_UUID_LEN_32));
669 break;
670 }
671
672 case Uuid::UUID128_BYTES_TYPE: {
673 /// [Len] [0x04] [0] [1] ... [15]
674 cdata[0] = BLE_UUID_LEN_128 + 1;
675 cdata[1] = BLE_AD_TYPE_128SRV_CMPL; /// 0x07
676 uint8_t uuidData[BLE_UUID_LEN_128];
677 uuid.ConvertToBytesLE(uuidData);
678 AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) +
679 std::string(reinterpret_cast<char *>(uuidData), BLE_UUID_LEN_128));
680 break;
681 }
682
683 default:
684 return;
685 }
686 }
687
688 /**
689 * @brief Set advertiser flag.
690 *
691 * @param flag Advertiser flag.
692 * @since 6
693 */
SetFlags(uint8_t flag)694 void BleAdvertiserDataImpl::SetFlags(uint8_t flag)
695 {
696 char cdata[BLE_ADV_DATA_BYTE_FIELD_LEN];
697 cdata[0] = BLE_ADV_DATA_FIELD_TYPE_AND_LEN;
698 cdata[1] = BLE_AD_TYPE_FLAG; /// 0x01
699 cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN] = flag;
700 advFlag_ = flag;
701 AddData(std::string(cdata, BLE_ADV_DATA_BYTE_FIELD_LEN));
702 }
703
GetFlags() const704 uint8_t BleAdvertiserDataImpl::GetFlags() const
705 {
706 return advFlag_;
707 }
708
709 /**
710 * @brief Set manufacturer data.
711 *
712 * @param data manufacturer data.
713 * @since 6
714 */
SetManufacturerData(const std::string & data)715 void BleAdvertiserDataImpl::SetManufacturerData(const std::string &data)
716 {
717 char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
718 cdata[0] = data.length() + 1;
719 cdata[1] = BLE_AD_MANUFACTURER_SPECIFIC_TYPE; /// 0xff
720 AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) + data);
721 }
722
723 /**
724 * @brief Set device name.
725 *
726 * @param name Device name.
727 * @since 6
728 */
SetDeviceName(const std::string & name)729 void BleAdvertiserDataImpl::SetDeviceName(const std::string &name)
730 {
731 if (name.length() > DEVICE_NAME_MAX_LEN) {
732 SetLongName(name);
733 } else {
734 SetShortName(name);
735 }
736 }
737
738 /**
739 * @brief Set Tx power level.
740 *
741 * @param txPowerLevel Tx power level.
742 * @since 6
743 */
SetTxPowerLevel(uint8_t txPowerLevel)744 void BleAdvertiserDataImpl::SetTxPowerLevel(uint8_t txPowerLevel)
745 {
746 char cdata[BLE_ADV_DATA_BYTE_FIELD_LEN];
747 cdata[0] = BLE_ADV_DATA_FIELD_TYPE_AND_LEN;
748 cdata[1] = BLE_AD_TYPE_TX_PWR; /// 0x09
749 cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN] = txPowerLevel;
750 AddData(std::string(cdata, BLE_ADV_DATA_BYTE_FIELD_LEN));
751 }
752
753 /**
754 * @brief Add service data.
755 *
756 * @param data Service data.
757 * @since 6
758 */
AddData(std::string data)759 void BleAdvertiserDataImpl::AddData(std::string data)
760 {
761 payload_.append(data);
762 }
763
764 /**
765 * @brief Get advertiser data packet.
766 *
767 * @return Returns advertiser data packet.
768 * @since 6
769 */
SetPayload(const std::string & payload)770 void BleAdvertiserDataImpl::SetPayload(const std::string &payload)
771 {
772 payload_ = payload;
773 }
774
775 /**
776 * @brief Get advertiser data packet.
777 *
778 * @return Returns advertiser data packet.
779 * @since 6
780 */
GetPayload() const781 std::string BleAdvertiserDataImpl::GetPayload() const
782 {
783 return payload_;
784 }
785
786 /**
787 * @brief Set advertiser data long name.
788 *
789 * @param name Bluetooth device name.
790 * @since 6
791 */
SetLongName(const std::string & name)792 void BleAdvertiserDataImpl::SetLongName(const std::string &name)
793 {
794 char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
795 // A shortened name only contain 26 contiguous characters from the beginning of the full name.
796 cdata[0] = DEVICE_NAME_MAX_LEN + 1;
797 cdata[1] = BLE_AD_TYPE_NAME_SHORT; /// 0x08
798 AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) + name.substr(0, DEVICE_NAME_MAX_LEN));
799 }
800
801 /**
802 * @brief Set advertiser data short name.
803 *
804 * @param name Bluetooth device name.
805 * @since 6
806 */
SetShortName(const std::string & name)807 void BleAdvertiserDataImpl::SetShortName(const std::string &name)
808 {
809 char cdata[BLE_ADV_DATA_FIELD_TYPE_AND_LEN];
810 cdata[0] = name.length() + 1;
811 cdata[1] = BLE_AD_TYPE_NAME_CMPL; /// 0x09
812 AddData(std::string(cdata, BLE_ADV_DATA_FIELD_TYPE_AND_LEN) + name);
813 }
814
BlePeripheralDevice()815 BlePeripheralDevice::BlePeripheralDevice()
816 : manufacturerData_(),
817 name_(),
818 serviceUUIDs_(),
819 txPower_(BLE_ADDR_TYPE_RANDOM),
820 serviceData_(),
821 serviceDataUUIDs_(),
822 pairState_(BLE_PAIR_NONE),
823 ioCapability_(BLE_IO_CAP_NONE),
824 aliasName_()
825 {
826 manufacturerData_.clear();
827 serviceDataUUIDs_.clear();
828 serviceData_.clear();
829 serviceUUIDs_.clear();
830 }
831
~BlePeripheralDevice()832 BlePeripheralDevice::~BlePeripheralDevice()
833 {
834 manufacturerData_.clear();
835 serviceUUIDs_.clear();
836 serviceData_.clear();
837 serviceDataUUIDs_.clear();
838 }
839
840 /**
841 * @brief Get device address.
842 *
843 * @return Returns device address.
844 * @since 6
845 */
GetRawAddress() const846 RawAddress BlePeripheralDevice::GetRawAddress() const
847 {
848 return address_;
849 }
850
851 /**
852 * @brief Get device appearance.
853 *
854 * @return Returns the device appearance.
855 * @since 6
856 */
GetAppearance() const857 uint16_t BlePeripheralDevice::GetAppearance() const
858 {
859 return appearance_;
860 }
861
862 /**
863 * @brief Get the manufacturer data.
864 *
865 * @return Returns the manufacturer data.
866 * @since 6
867 */
GetManufacturerData() const868 std::map<uint16_t, std::string> BlePeripheralDevice::GetManufacturerData() const
869 {
870 return manufacturerData_;
871 }
872
873 /**
874 * @brief the device name.
875 *
876 * @return Returns device Name.
877 * @since 6
878 */
GetName() const879 std::string BlePeripheralDevice::GetName() const
880 {
881 return name_;
882 }
883
884 /**
885 * @brief Get device RSSI.
886 *
887 * @return Returns device RSSI.
888 * @since 6
889 */
GetRSSI() const890 int8_t BlePeripheralDevice::GetRSSI() const
891 {
892 return rssi_;
893 }
894
895 /**
896 * @brief Get service data.
897 *
898 * @return Returns service data.
899 * @since 6
900 */
GetServiceData() const901 std::vector<std::string> BlePeripheralDevice::GetServiceData() const
902 {
903 return serviceData_;
904 }
905
906 /**
907 * @brief Get Service Data.
908 *
909 * @param index Service data index.
910 * @return Returns service data.
911 * @since 6
912 */
GetServiceData(int index) const913 std::string BlePeripheralDevice::GetServiceData(int index) const
914 {
915 return serviceData_.empty() ? "" :
916 (static_cast<size_t>(index) < serviceData_.size() ? serviceData_[index] : "");
917 }
918
919 /**
920 * @brief Get service data UUID.
921 *
922 * @return Returns service data UUID.
923 * @since 6
924 */
GetServiceDataUUID() const925 std::vector<Uuid> BlePeripheralDevice::GetServiceDataUUID() const
926 {
927 return serviceDataUUIDs_;
928 }
929
930 /**
931 * @brief Get service data UUID.
932 *
933 * @param index Service data index.
934 * @return Returns service data UUID.
935 * @since 6
936 */
GetServiceDataUUID(int index) const937 Uuid BlePeripheralDevice::GetServiceDataUUID(int index) const
938 {
939 Uuid uuid {};
940 return serviceDataUUIDs_.empty() ? uuid : serviceDataUUIDs_[index];
941 }
942
943 /**
944 * @brief Get the service UUID.
945 *
946 * @return Returns service UUID.
947 * @since 6
948 */
GetServiceUUID() const949 std::vector<Uuid> BlePeripheralDevice::GetServiceUUID() const
950 {
951 return serviceUUIDs_;
952 }
953
954 /**
955 * @brief Get service UUID.
956 *
957 * @param index Service UUID index.
958 * @return Return service UUID.
959 * @since 6
960 */
GetServiceUUID(int index) const961 Uuid BlePeripheralDevice::GetServiceUUID(int index) const
962 {
963 Uuid uuid {};
964 return serviceUUIDs_.empty() ? uuid : serviceUUIDs_[index];
965 }
966
967 /**
968 * @brief Get address type.
969 *
970 * @return Returns address type.
971 * @since 6
972 */
GetAddressType() const973 int BlePeripheralDevice::GetAddressType() const
974 {
975 return addressType_;
976 }
977
978 /**
979 * @brief Set address type.
980 *
981 * @param type Address type.
982 * @since 6
983 */
SetAddressType(int type)984 void BlePeripheralDevice::SetAddressType(int type)
985 {
986 addressType_ = type;
987 }
988
989 /**
990 * @brief Check if manufacturer data is included.
991 *
992 * @return Returns <b>true</b> if manufacturer data is included;
993 * Returns <b>false</b> otherwise.
994 * @since 6
995 */
IsManufacturerData() const996 bool BlePeripheralDevice::IsManufacturerData() const
997 {
998 return isManufacturerData_;
999 }
1000
1001 /**
1002 * @brief Check if the device RSSI is included.
1003 *
1004 * @return Returns <b>true</b> if include device rssi;
1005 * Returns <b>false</b> otherwise.
1006 * @since 6
1007 */
IsRSSI() const1008 bool BlePeripheralDevice::IsRSSI() const
1009 {
1010 return isRSSI_;
1011 }
1012
1013 /**
1014 * @brief Check if service data is included.
1015 *
1016 * @return Returns <b>true</b> if include service data;
1017 * Returns <b>false</b> otherwise.
1018 * @since 6
1019 */
IsServiceData() const1020 bool BlePeripheralDevice::IsServiceData() const
1021 {
1022 return isServiceData_;
1023 }
1024
1025 /**
1026 * @brief Check if the service UUID is included.
1027 *
1028 * @return Returns <b>true</b> if the service UUID is included;
1029 * Returns <b>false</b> otherwise.
1030 * @since 6
1031 */
IsServiceUUID() const1032 bool BlePeripheralDevice::IsServiceUUID() const
1033 {
1034 return isServiceUUID_;
1035 }
1036
IsName(void) const1037 bool BlePeripheralDevice::IsName(void) const
1038 {
1039 return isName_;
1040 }
1041
1042 /**
1043 * @brief Set device address.
1044 *
1045 * @param address device address.
1046 * @since 6
1047 */
SetAddress(const RawAddress & address)1048 void BlePeripheralDevice::SetAddress(const RawAddress &address)
1049 {
1050 address_ = address;
1051 }
1052
1053 /**
1054 * @brief Set RSSI value.
1055 *
1056 * @param RSSI value.
1057 * @since 6
1058 */
SetRSSI(int8_t rssi)1059 void BlePeripheralDevice::SetRSSI(int8_t rssi)
1060 {
1061 rssi_ = rssi;
1062 isRSSI_ = true;
1063 }
1064 /**
1065 * @brief Check whether device is connectable.
1066 *
1067 * @param [in] rssi value.
1068 * return Returns <b>true</b> if device is connectable.
1069 * Returns <b>false</b> otherwisee.
1070 */
IsConnectable() const1071 bool BlePeripheralDevice::IsConnectable() const
1072 {
1073 return connectable_;
1074 }
1075
1076 /**
1077 * @brief Sets whether the peer device is connectable.
1078 *
1079 * @param peer device's connectable.
1080 */
SetConnectable(bool connectable)1081 void BlePeripheralDevice::SetConnectable(bool connectable)
1082 {
1083 connectable_ = connectable;
1084 }
1085
1086 /**
1087 * @brief Sets adv event type.
1088 *
1089 * @param peer adv event type.
1090 */
SetEventType(uint16_t eventType)1091 void BlePeripheralDevice::SetEventType(uint16_t eventType)
1092 {
1093 eventType_ = eventType;
1094 isEventType_ = true;
1095 }
1096
1097 /**
1098 * @brief Check whether adv event type is included.
1099 *
1100 * return Returns <b>true</b> if event type is included.
1101 * Returns <b>false</b> otherwisee.
1102 */
IsEventType() const1103 bool BlePeripheralDevice::IsEventType() const
1104 {
1105 return isEventType_;
1106 }
1107
1108 /**
1109 * @brief Get adv event type.
1110 *
1111 * @return adv event type
1112 */
GetEventType() const1113 uint16_t BlePeripheralDevice::GetEventType() const
1114 {
1115 return eventType_;
1116 }
1117
1118 /**
1119 * @brief Parse advertisement packets.
1120 *
1121 * @param payload Advertisement packet.
1122 * @param total_len Advertisement packet length.
1123 * @since 6
1124 */
ParseAdvertiserment(BlePeripheralDeviceParseAdvData & parseAdvData)1125 void BlePeripheralDevice::ParseAdvertiserment(BlePeripheralDeviceParseAdvData &parseAdvData)
1126 {
1127 size_t sizeConsumed = 0;
1128 bool finished = false;
1129 size_t totalLength = parseAdvData.length;
1130 payload_ = parseAdvData.payload;
1131 payloadLen_ = parseAdvData.length;
1132
1133 while (!finished) {
1134 size_t length = *parseAdvData.payload;
1135 sizeConsumed += 1 + length;
1136 if (sizeConsumed > totalLength) {
1137 break;
1138 }
1139 parseAdvData.payload++;
1140
1141 if (length != 0) {
1142 uint8_t advType = *parseAdvData.payload;
1143 parseAdvData.payload++;
1144 length--;
1145 parseAdvData.length = length;
1146 BuildAdvertiserData(advType, parseAdvData);
1147 parseAdvData.payload += length;
1148 }
1149 if (sizeConsumed >= totalLength) {
1150 finished = true;
1151 }
1152 }
1153 }
1154
BuildAdvertiserData(uint8_t advType,BlePeripheralDeviceParseAdvData & parseAdvData)1155 void BlePeripheralDevice::BuildAdvertiserData(uint8_t advType, BlePeripheralDeviceParseAdvData &parseAdvData)
1156 {
1157 switch (advType) {
1158 case BLE_AD_TYPE_NAME_CMPL: /// Data Type: 0x09
1159 case BLE_AD_TYPE_NAME_SHORT: /// Data Type: 0x08
1160 SetName(std::string(reinterpret_cast<char *>(parseAdvData.payload), parseAdvData.length));
1161 break;
1162 case BLE_AD_TYPE_TX_PWR: /// Data Type: 0x0A
1163 SetTXPower(*parseAdvData.payload);
1164 break;
1165 case BLE_AD_TYPE_APPEARANCE: /// Data Type: 0x19
1166 SetAppearance(*reinterpret_cast<uint16_t *>(parseAdvData.payload));
1167 break;
1168 case BLE_AD_TYPE_FLAG: /// Data Type: 0x01
1169 SetAdFlag(*parseAdvData.payload);
1170 break;
1171 case BLE_AD_TYPE_16SRV_CMPL:
1172 case BLE_AD_TYPE_16SRV_PART: /// Data Type: 0x02
1173 SetServiceUUID16Bits(parseAdvData);
1174 break;
1175 case BLE_AD_TYPE_32SRV_CMPL:
1176 case BLE_AD_TYPE_32SRV_PART: /// Data Type: 0x04
1177 SetServiceUUID32Bits(parseAdvData);
1178 break;
1179 case BLE_AD_TYPE_128SRV_CMPL: /// Data Type: 0x07
1180 case BLE_AD_TYPE_128SRV_PART: /// Data Type: 0x06
1181 SetServiceUUID128Bits(parseAdvData);
1182 break;
1183 case BLE_AD_MANUFACTURER_SPECIFIC_TYPE:
1184 SetManufacturerData(std::string(reinterpret_cast<char *>(parseAdvData.payload), parseAdvData.length));
1185 break;
1186 case BLE_AD_TYPE_SERVICE_DATA: /// Data Type: 0x16
1187 SetServiceDataUUID16Bits(parseAdvData);
1188 break;
1189 case BLE_AD_TYPE_32SERVICE_DATA: /// Data Type: 0x20
1190 SetServiceDataUUID32Bits(parseAdvData);
1191 break;
1192 case BLE_AD_TYPE_128SERVICE_DATA: /// Data Type: 0x21
1193 SetServiceDataUUID128Bits(parseAdvData);
1194 break;
1195 default:
1196 break;
1197 }
1198 }
1199
SetServiceUUID16Bits(BlePeripheralDeviceParseAdvData & parseAdvData)1200 void BlePeripheralDevice::SetServiceUUID16Bits(BlePeripheralDeviceParseAdvData &parseAdvData)
1201 {
1202 for (size_t var = 0; var < parseAdvData.length / BLE_UUID_LEN_16; ++var) {
1203 SetServiceUUID(
1204 Uuid::ConvertFrom16Bits(*reinterpret_cast<uint16_t *>(parseAdvData.payload + var * BLE_UUID_LEN_16)));
1205 }
1206 }
1207
SetServiceUUID32Bits(BlePeripheralDeviceParseAdvData & parseAdvData)1208 void BlePeripheralDevice::SetServiceUUID32Bits(BlePeripheralDeviceParseAdvData &parseAdvData)
1209 {
1210 for (size_t var = 0; var < parseAdvData.length / BLE_UUID_LEN_32; ++var) {
1211 SetServiceUUID(
1212 Uuid::ConvertFrom32Bits(*reinterpret_cast<uint32_t *>(parseAdvData.payload + var * BLE_UUID_LEN_32)));
1213 }
1214 }
1215
SetServiceUUID128Bits(const BlePeripheralDeviceParseAdvData & parseAdvData)1216 void BlePeripheralDevice::SetServiceUUID128Bits(const BlePeripheralDeviceParseAdvData &parseAdvData)
1217 {
1218 for (size_t var = 0; var < parseAdvData.length / BLE_UUID_LEN_128; ++var) {
1219 std::array<uint8_t, BLE_UUID_LEN_128> data = {};
1220 for (int i = 0; i < BLE_UUID_LEN_128; i++) {
1221 data[i] = *(parseAdvData.payload + var * BLE_UUID_LEN_128 + i);
1222 }
1223 SetServiceUUID(Uuid::ConvertFromBytesLE(data.data()));
1224 }
1225 }
1226
SetServiceDataUUID16Bits(BlePeripheralDeviceParseAdvData & parseAdvData)1227 void BlePeripheralDevice::SetServiceDataUUID16Bits(BlePeripheralDeviceParseAdvData &parseAdvData)
1228 {
1229 if (parseAdvData.length < BLE_UUID_LEN_16) {
1230 return;
1231 }
1232 uint16_t uuid = *(reinterpret_cast<uint16_t *>(parseAdvData.payload));
1233 std::string data = "";
1234 if (parseAdvData.length > BLE_UUID_LEN_16) {
1235 data = std::string(
1236 reinterpret_cast<char *>(parseAdvData.payload + BLE_UUID_LEN_16), parseAdvData.length - BLE_UUID_LEN_16);
1237 }
1238 SetServiceDataUUID(Uuid::ConvertFrom16Bits(uuid), data);
1239 }
1240
SetServiceDataUUID32Bits(BlePeripheralDeviceParseAdvData & parseAdvData)1241 void BlePeripheralDevice::SetServiceDataUUID32Bits(BlePeripheralDeviceParseAdvData &parseAdvData)
1242 {
1243 if (parseAdvData.length < BLE_UUID_LEN_32) {
1244 return;
1245 }
1246 uint32_t uuid = *(reinterpret_cast<uint32_t *>(parseAdvData.payload));
1247 std::string data = "";
1248 if (parseAdvData.length > BLE_UUID_LEN_32) {
1249 data = std::string(
1250 reinterpret_cast<char *>(parseAdvData.payload + BLE_UUID_LEN_32), parseAdvData.length - BLE_UUID_LEN_32);
1251 }
1252 SetServiceDataUUID(Uuid::ConvertFrom32Bits(uuid), data);
1253 }
1254
SetServiceDataUUID128Bits(BlePeripheralDeviceParseAdvData & parseAdvData)1255 void BlePeripheralDevice::SetServiceDataUUID128Bits(BlePeripheralDeviceParseAdvData &parseAdvData)
1256 {
1257 if (parseAdvData.length < BLE_UUID_LEN_128) {
1258 return;
1259 }
1260 std::string data = "";
1261 if (parseAdvData.length > BLE_UUID_LEN_128) {
1262 data = std::string(
1263 reinterpret_cast<char *>(parseAdvData.payload + BLE_UUID_LEN_128), parseAdvData.length - BLE_UUID_LEN_128);
1264 }
1265 SetServiceDataUUID(Uuid::ConvertFromBytesLE(parseAdvData.payload), data);
1266 }
1267
1268 /**
1269 * @brief Set device name.
1270 *
1271 * @param name Device name.
1272 * @since 6
1273 */
SetName(const std::string & name)1274 void BlePeripheralDevice::SetName(const std::string &name)
1275 {
1276 name_ = name;
1277 isName_ = true;
1278 }
1279
1280 /**
1281 * @brief Set device roles.
1282 *
1283 * @param roles Device roles.
1284 * @since 6
1285 */
SetRoles(uint8_t roles)1286 void BlePeripheralDevice::SetRoles(uint8_t roles)
1287 {
1288 roles_ = roles;
1289 }
1290
1291 /**
1292 * @brief Set bonded from local.
1293 *
1294 * @param flag Advertiser flag.
1295 * @since 6
1296 */
SetBondedFromLocal(bool flag)1297 void BlePeripheralDevice::SetBondedFromLocal(bool flag)
1298 {
1299 bondFlag_ = flag;
1300 }
1301
1302 /**
1303 * @brief Set acl connection state.
1304 *
1305 * @param connectState Acl connection state.
1306 * @since 6
1307 */
SetAclConnectState(int connectState)1308 void BlePeripheralDevice::SetAclConnectState(int connectState)
1309 {
1310 aclConnected_ = connectState;
1311 }
1312
1313 /**
1314 * @brief Set acl connection handle.
1315 *
1316 * @param handle Acl connection handle.
1317 * @since 6
1318 */
SetConnectionHandle(int handle)1319 void BlePeripheralDevice::SetConnectionHandle(int handle)
1320 {
1321 connectionHandle_ = handle;
1322 }
1323
1324 /**
1325 * @brief Check if device acl is connected.
1326 *
1327 * @return Returns <b>true</b> if device acl is connected;
1328 * Returns <b>false</b> otherwise.
1329 * @since 6
1330 */
IsAclConnected() const1331 bool BlePeripheralDevice::IsAclConnected() const
1332 {
1333 if (aclConnected_ != BLE_CONNECTION_STATE_DISCONNECTED) {
1334 return true;
1335 } else {
1336 return false;
1337 }
1338 }
1339
1340 /**
1341 * @brief Check if device acl Encrypted.
1342 *
1343 * @return Returns <b>true</b> if device acl is Encrypted;
1344 * Returns <b>false</b> otherwise.
1345 * @since 6
1346 */
IsAclEncrypted() const1347 bool BlePeripheralDevice::IsAclEncrypted() const
1348 {
1349 if (aclConnected_ > BLE_CONNECTION_STATE_CONNECTED) {
1350 return true;
1351 } else {
1352 return false;
1353 }
1354 }
1355
1356 /**
1357 * @brief Check if device was bonded from local.
1358 *
1359 * @return Returns <b>true</b> if device was bonded from local;
1360 * Returns <b>false</b> otherwise.
1361 * @since 6
1362 */
IsBondedFromLocal() const1363 bool BlePeripheralDevice::IsBondedFromLocal() const
1364 {
1365 return bondFlag_;
1366 }
1367
1368 /**
1369 * @brief Get acl connection handle.
1370 *
1371 * @return Returns acl connection handle;
1372 * @since 6
1373 */
GetConnectionHandle() const1374 int BlePeripheralDevice::GetConnectionHandle() const
1375 {
1376 return connectionHandle_;
1377 }
1378
1379 /**
1380 * @brief Get device type.
1381 *
1382 * @return Returns device type.
1383 * @since 6
1384 */
GetDeviceType() const1385 uint8_t BlePeripheralDevice::GetDeviceType() const
1386 {
1387 if ((adFlag_ & BLE_ADV_FLAG_BREDR_NOT_SPT) > 0) {
1388 return BLE_BT_DEVICE_TYPE_DUMO;
1389 }
1390 return BLE_BT_DEVICE_TYPE_BLE;
1391 }
1392
1393 /**
1394 * @brief Get advertising flag.
1395 *
1396 * @return Returns advertising flag.
1397 * @since 6
1398 */
GetAdFlag() const1399 uint8_t BlePeripheralDevice::GetAdFlag() const
1400 {
1401 return adFlag_;
1402 }
1403
1404 /**
1405 * @brief Get paired status.
1406 *
1407 * @return Returns paired status.
1408 * @since 6
1409 */
GetPairedStatus() const1410 uint8_t BlePeripheralDevice::GetPairedStatus() const
1411 {
1412 return pairState_;
1413 }
1414
1415 /**
1416 * @brief Set paired status.
1417 *
1418 * @param status Paired status.
1419 * @return Returns <b>true</b> if the operation is successful;
1420 * Returns <b>false</b> otherwise.
1421 * @since 6
1422 */
SetPairedStatus(uint8_t status)1423 bool BlePeripheralDevice::SetPairedStatus(uint8_t status)
1424 {
1425 if (status < BLE_PAIR_NONE || status > BLE_PAIR_CANCELING) {
1426 return false;
1427 }
1428 if (pairState_ == status) {
1429 return true;
1430 }
1431 pairState_ = status;
1432 return true;
1433 }
1434
1435 /**
1436 * @brief Set alias name.
1437 *
1438 * @param name Device alias name.
1439 * @since 6
1440 */
SetAliasName(const std::string & name)1441 void BlePeripheralDevice::SetAliasName(const std::string &name)
1442 {
1443 aliasName_ = name;
1444 }
1445
1446 /**
1447 * @brief Get alias name.
1448 *
1449 * @return Returns alias name.
1450 * @since 6
1451 */
GetAliasName() const1452 std::string BlePeripheralDevice::GetAliasName() const
1453 {
1454 return aliasName_;
1455 }
1456
1457 /**
1458 * @brief Set IO capability.
1459 *
1460 * @param io IO capability
1461 * @since 6
1462 */
SetIoCapability(uint8_t io)1463 void BlePeripheralDevice::SetIoCapability(uint8_t io)
1464 {
1465 ioCapability_ = io;
1466 }
1467
1468 /**
1469 * @brief Get IO capability.
1470 *
1471 * @return Returns IO capability.
1472 * @since 6
1473 */
GetIoCapability() const1474 uint8_t BlePeripheralDevice::GetIoCapability() const
1475 {
1476 return ioCapability_;
1477 }
1478
1479 /**
1480 * @brief Set advertising flag.
1481 *
1482 * @param adFlag Advertising flag.
1483 * @since 6
1484 */
SetAdFlag(uint8_t adFlag)1485 void BlePeripheralDevice::SetAdFlag(uint8_t adFlag)
1486 {
1487 adFlag_ = adFlag;
1488 }
1489
1490 /**
1491 * @brief Set device appearance.
1492 *
1493 * @param device Appearance.
1494 * @since 6
1495 */
SetAppearance(uint16_t appearance)1496 void BlePeripheralDevice::SetAppearance(uint16_t appearance)
1497 {
1498 appearance_ = appearance;
1499 isAppearance_ = true;
1500 }
1501
1502 /**
1503 * @brief Set manufacturer data.
1504 *
1505 * @param manufacturerData Manufacturer data.
1506 * @since 6
1507 */
SetManufacturerData(std::string manufacturerData)1508 void BlePeripheralDevice::SetManufacturerData(std::string manufacturerData)
1509 {
1510 if (manufacturerData.size() > BLE_UUID_LEN_16) {
1511 uint16_t manufacturerId = uint8_t(manufacturerData[0]) | (uint16_t(manufacturerData[1]) << BLE_ONE_BYTE_LEN);
1512 std::map<uint16_t, std::string>::const_iterator iter = manufacturerData_.find(manufacturerId);
1513 if (iter == manufacturerData_.cend()) {
1514 manufacturerData_.insert(std::make_pair(manufacturerId, manufacturerData.substr(BLE_UUID_LEN_16)));
1515 }
1516 isManufacturerData_ = true;
1517 } else {
1518 manufacturerData_.clear();
1519 isManufacturerData_ = false;
1520 }
1521 }
1522
1523 /**
1524 * @brief Set service data UUID.
1525 *
1526 * @param uuid Service data UUID.
1527 * @since 6
1528 */
SetServiceDataUUID(Uuid uuid,std::string data)1529 void BlePeripheralDevice::SetServiceDataUUID(Uuid uuid, std::string data)
1530 {
1531 isServiceData_ = true;
1532 auto iter = std::find(serviceDataUUIDs_.begin(), serviceDataUUIDs_.end(), uuid);
1533 if (iter == serviceDataUUIDs_.end()) {
1534 serviceDataUUIDs_.push_back(uuid);
1535 serviceData_.push_back(data);
1536 }
1537 }
1538
1539 /**
1540 * @brief Set service UUID.
1541 *
1542 * @param serviceUUID Service UUID.
1543 * @since 6
1544 */
SetServiceUUID(Uuid serviceUUID)1545 void BlePeripheralDevice::SetServiceUUID(Uuid serviceUUID)
1546 {
1547 isServiceUUID_ = true;
1548 auto iter = std::find(serviceUUIDs_.begin(), serviceUUIDs_.end(), serviceUUID);
1549 if (iter == serviceUUIDs_.end()) {
1550 serviceUUIDs_.push_back(serviceUUID);
1551 }
1552 }
1553 /**
1554 * @brief Set TX power.
1555 *
1556 * @param txPower TX power.
1557 * @since 6
1558 */
SetTXPower(int8_t txPower)1559 void BlePeripheralDevice::SetTXPower(int8_t txPower)
1560 {
1561 isTXPower_ = true;
1562 txPower_ = txPower;
1563 }
1564 /**
1565 * @brief Get peripheral device.
1566 *
1567 * @return Returns peripheral device pointer.
1568 * @since 6
1569 */
GetPeripheralDevice() const1570 BlePeripheralDevice BleScanResultImpl::GetPeripheralDevice() const
1571 {
1572 return peripheralDevice_;
1573 }
1574
1575 /**
1576 * @brief Set peripheral device.
1577 *
1578 * @param dev Peripheral device.
1579 * @since 6
1580 */
SetPeripheralDevice(const BlePeripheralDevice & dev)1581 void BleScanResultImpl::SetPeripheralDevice(const BlePeripheralDevice &dev)
1582 {
1583 peripheralDevice_ = dev;
1584 }
1585
1586 /**
1587 * @brief Get advertiser data packet.
1588 *
1589 * @return Returns advertiser data packet.
1590 * @since 6
1591 */
GetPayload() const1592 uint8_t *BlePeripheralDevice::GetPayload() const
1593 {
1594 return payload_;
1595 }
1596
1597 /**
1598 * @brief Get advertising packet length.
1599 *
1600 * @return Returns advertising packet length.
1601 * @since 6
1602 */
GetPayloadLen() const1603 size_t BlePeripheralDevice::GetPayloadLen() const
1604 {
1605 return payloadLen_;
1606 }
1607 } // namespace bluetooth
1608 } // namespace OHOS
1609