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