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