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