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