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