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 #ifndef CLASSIC_ADAPTER_H 17 #define CLASSIC_ADAPTER_H 18 19 #include <map> 20 #include <vector> 21 22 #include "base_def.h" 23 #include "bt_uuid.h" 24 #include "btm.h" 25 #include "classic_adapter_properties.h" 26 #include "classic_battery_observer.h" 27 #include "classic_bluetooth_data.h" 28 #include "classic_remote_device.h" 29 #include "context.h" 30 #include "gap_if.h" 31 #include "interface_adapter_classic.h" 32 #include "log.h" 33 #include "log_util.h" 34 #include "raw_address.h" 35 #include "sdp.h" 36 37 /** 38 * @addtogroup Bluetooth 39 * @{ 40 * 41 * @brief Defines adapter classic common functions. 42 * 43 */ 44 45 /** 46 * @file classic_adapter.h 47 * 48 * @brief Adapter classic. 49 * 50 */ 51 52 namespace OHOS { 53 namespace bluetooth { 54 /** 55 * @brief Represents classic adapter. 56 * 57 */ 58 class ClassicAdapter : public IAdapterClassic, public utility::Context { 59 public: 60 /** 61 * @brief A constructor used to create a <b>ClassicAdapter</b> instance. 62 * 63 */ 64 ClassicAdapter(); 65 66 /** 67 * @brief A destructor used to delete the <b>ClassicAdapter</b> instance. 68 * 69 */ 70 ~ClassicAdapter(); 71 72 /** 73 * @brief Get local device address. 74 * 75 * @return Returns local device address. 76 */ 77 std::string GetLocalAddress() const override; 78 79 /** 80 * @brief Get local device name. 81 * 82 * @return Returns local device name. 83 */ 84 std::string GetLocalName() const override; 85 86 /** 87 * @brief Set local device name. 88 * 89 * @param name Device name. 90 * @return Returns <b>true</b> if the operation is successful; 91 * returns <b>false</b> if the operation fails. 92 */ 93 bool SetLocalName(const std::string &name) const override; 94 95 /** 96 * @brief Set bondable mode. 97 * 98 * @param mode Bondable mode. 99 * @return Returns <b>true</b> if the operation is successful; 100 * returns <b>false</b> if the operation fails. 101 */ 102 bool SetBondableMode(int mode) const override; 103 104 /** 105 * @brief Get bondable mode. 106 * 107 * @return Returns bondable mode. 108 */ 109 int GetBondableMode() const override; 110 111 /** 112 * @brief Get remote device name. 113 * 114 * @param device Remote device. 115 * @return Returns device name. 116 */ 117 std::string GetDeviceName(const RawAddress &device) const override; 118 119 /** 120 * @brief Get remote device uuids. 121 * 122 * @param device Remote device. 123 * @return Returns device uuids. 124 */ 125 std::vector<Uuid> GetDeviceUuids(const RawAddress &device) const override; 126 127 /** 128 * @brief Get paired devices. 129 * 130 * @return Returns device list. 131 */ 132 std::vector<RawAddress> GetPairedDevices() const override; 133 134 /** 135 * @brief Local device start pair. 136 * 137 * @param device Remote device. 138 * @return Returns <b>true</b> if the operation is successful; 139 * returns <b>false</b> if the operation fails. 140 */ 141 bool StartPair(const RawAddress &device) override; 142 143 /** 144 * @brief Check if remote device was bonded from local. 145 * 146 * @param device Remote device. 147 * @return Returns <b>true</b> if device was bonded from local; 148 * returns <b>false</b> if device was not bonded from local. 149 */ 150 bool IsBondedFromLocal(const RawAddress &device) const override; 151 152 /** 153 * @brief Cancel pair operation. 154 * 155 * @param device Remote device. 156 * @return Returns <b>true</b> if the operation is successful; 157 * returns <b>false</b> if the operation fails. 158 */ 159 bool CancelPairing(const RawAddress &device) override; 160 161 /** 162 * @brief Remove pair. 163 * 164 * @param device Remote device. 165 * @return Returns <b>true</b> if the operation is successful; 166 * returns <b>false</b> if the operation fails. 167 */ 168 bool RemovePair(const RawAddress &device) override; 169 170 /** 171 * @brief Get device pair state. 172 * 173 * @param device Remote device. 174 * @return Returns device pair state. 175 */ 176 int GetPairState(const RawAddress &device) const override; 177 178 /** 179 * @brief Set device pairing confirmation. 180 * 181 * @param device Remote device. 182 * @param accept Set gap accept flag. 183 * @return Returns <b>true</b> if the operation is successful; 184 * returns <b>false</b> if the operation fails. 185 */ 186 bool SetDevicePairingConfirmation(const RawAddress &device, bool accept) const override; 187 188 /** 189 * @brief Set device pair passkey. 190 * 191 * @param device Remote device. 192 * @param passkey Device passkey. 193 * @param accept Set gap accept flag. 194 * @return Returns <b>true</b> if the operation is successful; 195 * returns <b>false</b> if the operation fails. 196 */ 197 bool SetDevicePasskey(const RawAddress &device, int passkey, bool accept) const override; 198 199 /** 200 * @brief Check remote device pair request reply. 201 * 202 * @param device Remote device. 203 * @param accept Set gap accept flag. 204 * @return Returns <b>true</b> if the operation is successful; 205 * returns <b>false</b> if the operation fails. 206 */ 207 bool PairRequestReply(const RawAddress &device, bool accept) const override; 208 209 /** 210 * @brief Check if acl connected with remote device. 211 * 212 * @param device Remote device. 213 * @return Returns <b>true</b> if device acl connected; 214 * returns <b>false</b> if device does not acl connect. 215 */ 216 bool IsAclConnected(const RawAddress &device) const override; 217 218 /** 219 * @brief Check if remote device acl Encrypted. 220 * 221 * @return Returns <b>true</b> if device acl Encrypted; 222 * returns <b>false</b> if device does not acl Encrypt. 223 */ 224 bool IsAclEncrypted(const RawAddress &device) const override; 225 226 /** 227 * @brief Get the instance of the ClassicAdapter. 228 * 229 * @return Returns the instance of the ClassicAdapter. 230 */ 231 utility::Context *GetContext() override; 232 233 /** 234 * @brief Enable classic service. 235 * 236 */ 237 void Enable() override; 238 239 /** 240 * @brief Disable classic service. 241 * 242 */ 243 void Disable() override; 244 245 /** 246 * @brief PostEnable classic service. 247 * 248 */ 249 void PostEnable() override; 250 251 /** 252 * @brief Get remote device type. 253 * 254 * @param device Remote device. 255 * @return Returns remote device type. 256 */ 257 int GetDeviceType(const RawAddress &device) const override; 258 259 /** 260 * @brief Get remote device class. 261 * 262 * @param device Remote device. 263 * @return Returns remote device class. 264 */ 265 int GetDeviceClass(const RawAddress &device) const override; 266 267 /** 268 * @brief Get remote device alias name. 269 * 270 * @param device Remote device 271 * @return Returns remote device alias name. 272 */ 273 std::string GetAliasName(const RawAddress &device) const override; 274 275 /** 276 * @brief Set remote device alias name. 277 * 278 * @param device Remote device which setted alias name. 279 * @param name Alias name. 280 * @return Returns <b>true</b> if the operation is successful; 281 * returns <b>false</b> if the operation fails. 282 */ 283 bool SetAliasName(const RawAddress &device, const std::string &name) const override; 284 285 /** 286 * @brief Register remote device observer. 287 * 288 * @param observer Class IClassicRemoteDeviceObserver pointer to register observer. 289 * @return Returns <b>true</b> if the operation is successful; 290 * returns <b>false</b> if the operation fails. 291 */ 292 bool RegisterRemoteDeviceObserver(IClassicRemoteDeviceObserver &observer) const override; 293 294 /** 295 * @brief Deregister remote device observer. 296 * 297 * @param observer Class IClassicRemoteDeviceObserver pointer to register observer. 298 * @return Returns <b>true</b> if the operation is successful; 299 * returns <b>false</b> if the operation fails. 300 */ 301 bool DeregisterRemoteDeviceObserver(IClassicRemoteDeviceObserver &observer) const override; 302 303 /** 304 * @brief Register classic adapter observer. 305 * 306 * @param observer Class IAdapterClassicObserver pointer to register observer. 307 * @return Returns <b>true</b> if the operation is successful; 308 * returns <b>false</b> if the operation fails. 309 */ 310 bool RegisterClassicAdapterObserver(IAdapterClassicObserver &observer) const override; 311 312 /** 313 * @brief Deregister classic adapter observer. 314 * 315 * @param observer Class IAdapterClassicObserver pointer to register observer. 316 * @return Returns <b>true</b> if the operation is successful; 317 * returns <b>false</b> if the operation fails. 318 */ 319 bool DeregisterClassicAdapterObserver(IAdapterClassicObserver &observer) const override; 320 321 /** 322 * @brief Remove all pairs. 323 * 324 * @return Returns <b>true</b> if the operation is successful; 325 * returns <b>false</b> if the operation fails. 326 */ 327 bool RemoveAllPairs() override; 328 329 /** 330 * @brief Set device pair pin. 331 * 332 * @param device Remote device address. 333 * @param pin Pin code. 334 * @param length Pin code length. 335 * @return Returns <b>true</b> if the operation is successful; 336 * returns <b>false</b> if the operation fails. 337 */ 338 bool SetDevicePin(const RawAddress &device, const std::string &pinCode) const override; 339 340 /** 341 * @brief Set device scan mode. 342 * 343 * @param mode Scan mode. 344 * @param duration Scan time. 345 * @return Returns <b>true</b> if the operation is successful; 346 * returns <b>false</b> if the operation fails. 347 */ 348 bool SetBtScanMode(int mode, int duration) override; 349 350 /** 351 * @brief Get device scan mode. 352 * 353 * @return Returns bluetooth scan mode. 354 */ 355 int GetBtScanMode() const override; 356 357 /** 358 * @brief Get local device class. 359 * 360 * @return Returns local device class. 361 */ 362 int GetLocalDeviceClass() const override; 363 364 /** 365 * @brief Set local device class. 366 * 367 * @param deviceClass Device class. 368 * @return Returns <b>true</b> if the operation is successful; 369 * returns <b>false</b> if the operation fails. 370 */ 371 bool SetLocalDeviceClass(int deviceClass) const override; 372 373 /** 374 * @brief Get device address. 375 * 376 * @return Returns <b>true</b> if the operation is successful; 377 * returns <b>false</b> if the operation fails. 378 */ 379 bool StartBtDiscovery() override; 380 381 /** 382 * @brief Cancel device discovery. 383 * 384 * @return Returns <b>true</b> if the operation is successful; 385 * returns <b>false</b> if the operation fails. 386 */ 387 bool CancelBtDiscovery() override; 388 389 /** 390 * @brief Check if device is discovering. 391 * 392 * @return Returns <b>true</b> if device is discovering; 393 * returns <b>false</b> if device is not discovering. 394 */ 395 bool IsBtDiscovering() const override; 396 397 /** 398 * @brief Get device discovery end time. 399 * 400 * @return Returns device discovery end time. 401 */ 402 long GetBtDiscoveryEndMillis() const override; 403 void SetDeviceBatteryLevel(const RawAddress &device, int batteryLevel) const override; 404 bool IsHfpCodSupported(const RawAddress &device) override; 405 406 bool SetHidPnpInfo(const std::string &remoteAddr, int vendorId, int productId, int version) override; 407 bool SetHidDescInfo( 408 const std::string &remoteAddr, int ctryCode, const std::vector<uint8_t> &descData, int descLength) override; 409 void GetHidPnpInfo(const std::string &remoteAddr, int &vendorId, int &productId, int &version) override; 410 void GetHidDescInfo( 411 const std::string &remoteAddr, int &ctryCode, std::vector<uint8_t> &descData, int &descLength) override; 412 413 private: 414 /** 415 * @brief Inquiry result callback. 416 * 417 * @param addr Device address. 418 * @param classOfDevice Device class. 419 * @param context Classic adapter object. 420 */ 421 static void InquiryResultCallback(const BtAddr *addr, uint32_t classOfDevice, void *context); 422 423 /** 424 * @brief Inquiry result rssi callback. 425 * 426 * @param addr Device address. 427 * @param classOfDevice Device class. 428 * @param rssi Device rssi. 429 * @param context Classic adapter object. 430 */ 431 static void InquiryResultRssiCallback(const BtAddr *addr, uint32_t classOfDevice, int8_t rssi, void *context); 432 433 /** 434 * @brief Extended inquiry result callback. 435 * 436 * @param addr Device address. 437 * @param classOfDevice Device class. 438 * @param rssi Device rssi. 439 * @param eir Eir data. 440 * @param context Classic adapter object. 441 */ 442 static void ExtendedInquiryResultCallback(const BtAddr *addr, uint32_t classOfDevice, int8_t rssi, 443 const uint8_t eir[MAX_EXTEND_INQUIRY_RESPONSE_LEN], void *context); 444 445 /** 446 * @brief Inquiry complete callback. 447 * 448 * @param status Inquire status. 449 * @param context Classic adapter object. 450 */ 451 static void InquiryCompleteCallback(uint8_t status, void *context); 452 453 /** 454 * @brief Remote device name callback. 455 * 456 * @param status Inquire status. 457 * @param addr Device address. 458 * @param name Device name. 459 * @param context Classic adapter object. 460 */ 461 static void RemoteNameCallback( 462 uint8_t status, const BtAddr *addr, const uint8_t name[MAX_LOC_BT_NAME_LEN], void *context); 463 /** 464 * @brief User confirm request callback. 465 * 466 * @param addr Device address. 467 * @param number Pass key. 468 * @param context Classic adapter object. 469 */ 470 static void UserConfirmReqCallback(const BtAddr *addr, uint32_t number, int localMitmRequired, int remoteMitmRequired, void *context); 471 472 /** 473 * @brief User pass key request callback. 474 * 475 * @param addr Device address. 476 * @param context Classic adapter object. 477 */ 478 static void UserPasskeyReqCallback(const BtAddr *addr, void *context); 479 480 /** 481 * @brief User pass key notification callback. 482 * 483 * @param addr Device address. 484 * @param number Pass key. 485 * @param context Classic adapter object. 486 */ 487 static void UserPasskeyNotificationCallback(const BtAddr *addr, uint32_t number, void *context); 488 489 /** 490 * @brief Remote oob key request callback. 491 * 492 * @param addr Device address. 493 * @param context Classic adapter object. 494 */ 495 static void RemoteOobReqCallback(const BtAddr *addr, void *context); 496 497 /** 498 * @brief Pin code request callback. 499 * 500 * @param addr Device address. 501 * @param context Classic adapter object. 502 */ 503 static void PinCodeReqCallback(const BtAddr *addr, void *context); 504 505 /** 506 * @brief Link key request callback. 507 * 508 * @param addr Device address. 509 * @param context Classic adapter object. 510 */ 511 static void LinkKeyReqCallback(const BtAddr *addr, void *context); 512 513 /** 514 * @brief Io capability request callback. 515 * 516 * @param addr Device address. 517 * @param context Classic adapter object. 518 */ 519 static void IoCapabilityReqCallback(const BtAddr *addr, void *context); 520 521 /** 522 * @brief Io capability response callback. 523 * 524 * @param addr Device address. 525 * @param ioCapability Device iocapability. 526 * @param context Classic adapter object. 527 */ 528 static void IoCapabilityRspCallback(const BtAddr *addr, uint8_t ioCapability, void *context); 529 530 /** 531 * @brief Link key notification. 532 * 533 * @param addr Device address. 534 * @param linkkey Link key. 535 * @param keyType Link key type. 536 * @param context Classic adapter object. 537 */ 538 static void LinkKeyNotification( 539 const BtAddr *addr, const uint8_t linkKey[GAP_LINKKEY_SIZE], uint8_t keyType, void *context); 540 541 /** 542 * @brief Simple pair complete callback. 543 * 544 * @param addr Device address. 545 * @param status Pair status. 546 * @param context Classic adapter object. 547 */ 548 static void SimplePairCompleteCallback(const BtAddr *addr, uint8_t status, void *context); 549 550 /** 551 * @brief Authentication complete callback. 552 * 553 * @param addr Device address. 554 * @param status Authentication status. 555 * @param context Classic adapter object. 556 */ 557 static void AuthenticationCompleteCallback(const BtAddr *addr, uint8_t status, void *context); 558 559 /** 560 * @brief Encryption change complete callback. 561 * 562 * @param addr Device address. 563 * @param status Encryption status. 564 * @param context Classic adapter object. 565 */ 566 static void EncryptionChangeCallback(const BtAddr *addr, uint8_t status, void *context); 567 568 /** 569 * @brief Authorize ind callback. 570 * 571 * @param addr Device address. 572 * @param service Gap service. 573 * @param context Classic adapter object. 574 */ 575 static void AuthorizeIndCallback(const BtAddr *addr, GAP_Service service, void *context); 576 577 /// BTM ACL Change status callback. 578 /** 579 * @brief Acl connection complete callback. 580 * 581 * @param status Connection status. 582 * @param connectionHandle Connection handle. 583 * @param addr Device address. 584 * @param encyptionEnabled Encyption enable status. 585 * @param context Classic adapter object. 586 */ 587 static void ConnectionComplete(const BtmAclConnectCompleteParam *param, void *context); 588 589 /** 590 * @brief Acl disconnection complete callback. 591 * 592 * @param status Connection status. 593 * @param connectionHandle Connection handle. 594 * @param reason Disconnection reason. 595 * @param context Classic adapter object. 596 */ 597 static void DisconnectionComplete(uint8_t status, uint16_t connectionHandle, uint8_t reason, void *context); 598 599 /// SDP search remote device supported uuid callback. 600 /** 601 * @brief Service search attribute callback. 602 * 603 * @param addr Device address. 604 * @param serviceArray Service array. 605 * @param serviceNum Service number. 606 * @param context Classic adapter object. 607 */ 608 static void ServiceSearchAttributeCb( 609 const BtAddr *addr, const SdpService *serviceArray, uint16_t serviceNum, void *context); 610 611 /** 612 * @brief Handle inquiry event. 613 * 614 * @param event inquiry callback event. 615 * @param param Gap callback paramer. 616 */ 617 void HandleInquiryEvent(GAP_CB_EVENT event, const GapCallbackParam ¶m); 618 619 /** 620 * @brief Handle security event. 621 * 622 * @param event security callback event. 623 * @param param Gap callback paramer. 624 */ 625 void HandleSecurityEvent(GAP_CB_EVENT event, const GapCallbackParam ¶m); 626 627 /** 628 * @brief ClassicAdater start up. Load config file and enable stack. 629 * 630 */ 631 void StartUp(); 632 633 /** 634 * @brief ClassicAdater shut down. Save config file and disable stack. 635 * 636 */ 637 void ShutDown(); 638 639 /** 640 * @brief Process post enable. 641 * 642 */ 643 void ProcessPostEnable(); 644 645 /** 646 * @brief Set scan mode. 647 * 648 * @param mode Scan mode. 649 * @return Returns <b>true</b> if the operation is successful; 650 * returns <b>false</b> if the operation fails. 651 */ 652 bool SetScanMode(int mode); 653 654 /** 655 * @brief Set scan mode result callback. 656 * 657 * @param status Scan status. 658 * @param context ClassicAdapterProperties object. 659 */ 660 static void SetScanModeResultCallback(uint8_t status, void *context); 661 662 /** 663 * @brief Receive SetScanMode callback. 664 * 665 * @param status SetScanMode result. 666 */ 667 void ReceiveSetScanModeCallback(uint8_t status); 668 669 /** 670 * @brief ScanMode timeout. 671 * 672 */ 673 void ScanModeTimeout(); 674 675 /** 676 * @brief Hw(Controller) process timeout. 677 * 678 */ 679 void HwProcessTimeout(); 680 void HwTimeout(); 681 682 /** 683 * @brief Register callback. 684 * 685 */ 686 bool RegisterCallback(); 687 688 /** 689 * @brief Deregister callback. 690 * 691 */ 692 bool DeregisterCallback() const; 693 694 /** 695 * @brief Get remote device name. 696 * 697 * @param addr Device address. 698 * @return Returns <b>true</b> if the operation is successful; 699 * returns <b>false</b> if the operation fails. 700 */ 701 bool GetRemoteName(const BtAddr &addr) const; 702 703 /** 704 * @brief Set link key. 705 * 706 * @param addr Device address. 707 */ 708 void SetLinkKey(const BtAddr &addr); 709 710 /** 711 * @brief Set Io capability. 712 * 713 * @param addr Device address. 714 */ 715 void SetIoCapability(const BtAddr &addr); 716 717 /** 718 * @brief Parser eir data. 719 * 720 * @param remote Remote device. 721 * @param data Eir data. 722 */ 723 void ParserEirData(std::shared_ptr<ClassicRemoteDevice> remote, const std::vector<uint8_t> &data) const; 724 725 /** 726 * @brief Load paired device info from config file. 727 * 728 */ 729 void LoadPairedDeviceInfo(); 730 731 /** 732 * @brief Save paired devices info. 733 * 734 */ 735 void SavePairedDevices() const; 736 737 /** 738 * @brief Get service uuid from device uuid. 739 * 740 * @param uuid Device uuid. 741 * @return Returns service uuid. 742 */ 743 Uuid GetUuidFromBtUuid(const BtUuid &inUuid) const; 744 745 /** 746 * @brief Get local support uuids then update the uuids to EIR data. 747 * 748 */ 749 void UpdateSupportedUuids() const; 750 751 /** 752 * @brief Save remote device Io capability. 753 * 754 * @param addr Remote device address. 755 * @param ioCapability Device Io capability 756 */ 757 void SaveRemoteIoCapability(const BtAddr &addr, uint8_t ioCapability); 758 759 /** 760 * @brief Send scan mode changed. 761 * 762 * @param mode ScamMode. 763 */ 764 void SendScanModeChanged(int mode) const; 765 766 /** 767 * @brief Send discovery state changed. 768 * 769 */ 770 void SendDiscoveryStateChanged(int discoveryState) const; 771 772 /** 773 * @brief Send discovery result. 774 * 775 * @param device Remote device. 776 * @param rssi Rssi of device. 777 * @param deviceName Name of device. 778 * @param deviceClass Class of device. 779 */ 780 void SendDiscoveryResult( 781 const RawAddress &device, int rssi, const std::string deviceName, int deviceClass) const; 782 783 /** 784 * @brief Send remote device cod changed. 785 * 786 * @param device Remote device. 787 * @param cod Remote device cod. 788 */ 789 void SendRemoteCodChanged(const RawAddress &device, int cod) const; 790 791 /** 792 * @brief Send remote device battery level changed. 793 * 794 * @param device Remote device. 795 * @param batteryLevel Remote device battery level. 796 */ 797 void SendRemoteBatteryLevelChanged(const RawAddress &device, int batteryLevel) const; 798 799 /** 800 * @brief Send remote device name changed. 801 * 802 * @param device Remote device. 803 * @param deviceName Remote device name. 804 */ 805 void SendRemoteNameChanged(const RawAddress &device, const std::string &deviceName) const; 806 807 /** 808 * @brief Send pair status changed. 809 * 810 * @param transport Remote device transport. 811 * @param device Remote device. 812 * @param status Pair status. 813 */ 814 void SendPairStatusChanged(const BTTransport transport, const RawAddress &device, int status) const; 815 816 /** 817 * @brief Send pair request. 818 * 819 * @param transport Remote device transport. 820 * @param device Remote device. 821 */ 822 void SendPairRequested(const BTTransport transport, const RawAddress &device) const; 823 824 /** 825 * @brief Send pair confirmed. 826 * 827 * @param device Remote device. 828 * @param reqType confirmed type (SSP/Pin Code). 829 * @param number confirmed number. 830 */ 831 void SendPairConfirmed(const RawAddress &device, int reqType, int number) const; 832 833 /** 834 * @brief Send remote device uuid changed. 835 * 836 * @param device Remote device. 837 * @param uuids Device uuid. 838 */ 839 void SendRemoteUuidChanged(const RawAddress &device, const std::vector<Uuid> &uuids) const; 840 841 /** 842 * @brief Send remote alias name changed. 843 * 844 * @param device Remote device. 845 * @param aliasName alias name. 846 */ 847 void SendRemoteAliasChanged(const RawAddress &device, const std::string &aliasName) const; 848 849 /** 850 * @brief Handle inquire result. 851 * 852 * @param addr Device address. 853 * @param classOfDevice Device class. 854 * @param rssi Device rssi. 855 * @param eir Eir data. 856 */ 857 void HandleInquiryResult(const BtAddr &addr, uint32_t classOfDevice, std::vector<uint8_t> eir, int8_t rssi = 0); 858 859 /** 860 * @brief Handle inquire complete. 861 * 862 * @param status Inquire status. 863 */ 864 void HandleInquiryComplete(uint8_t status); 865 866 /** 867 * @brief Receive remote device name. 868 * 869 * @param status Receive status. 870 * @param addr Remote device address. 871 * @param name Remote device name. 872 */ 873 void ReceiveRemoteName(uint8_t status, const BtAddr &addr, const uint8_t name[MAX_LOC_BT_NAME_LEN]); 874 875 /** 876 * @brief SSP confirm request. 877 * 878 * @param addr Remote device address. 879 * @param reqType Request type. 880 * @param number Pass key. 881 */ 882 void SSPConfirmReq(const BtAddr &addr, int reqType, int number, int localMitmRequired, int remoteMitmRequired); 883 884 /** 885 * @brief Set authorize response. 886 * 887 * @param addr Remote device address. 888 * @param service Gap service. 889 */ 890 void SetAuthorizeRes(const BtAddr &addr, GAP_Service service) const; 891 892 /** 893 * @brief Receive link key notification. 894 * 895 * @param addr Remote device address. 896 * @param linkKey Link key. 897 * @param keyType Link key type. 898 */ 899 void ReceiveLinkKeyNotification(const BtAddr &addr, const uint8_t linkKey[GAP_LINKKEY_SIZE], uint8_t keyType); 900 901 /** 902 * @brief Receive simple pair complete. 903 * 904 * @param addr Remote device address. 905 * @param status Pair status. 906 */ 907 void ReceiveSimplePairComplete(const BtAddr &addr, uint8_t status); 908 909 /** 910 * @brief Receive authentication complete. 911 * 912 * @param addr Remote device address. 913 * @param status Authentication status. 914 */ 915 void ReceiveAuthenticationComplete(const BtAddr &addr, uint8_t status); 916 917 /** 918 * @brief Receive encryption change. 919 * 920 * @param addr Remote device address. 921 * @param status Encryption status. 922 */ 923 void ReceiveEncryptionChange(const BtAddr &addr, uint8_t status); 924 925 /** 926 * @brief Receive acl connection complete. 927 * 928 * @param status Connection status. 929 * @param connectionHandle Connection handle. 930 * @param remoteAddr Device address. 931 * @param encyptionEnabled Encyption enable status. 932 */ 933 void ReceiveConnectionComplete(uint8_t status, uint16_t connectionHandle, const RawAddress &device, 934 uint32_t classOfDevice, bool encyptionEnabled); 935 936 /** 937 * @brief Receive acl disconnection complete. 938 * 939 * @param status Connection status. 940 * @param connectionHandle Connection handle. 941 * @param reason Disconnection reason. 942 */ 943 void ReceiveDisconnectionComplete(uint8_t status, uint16_t connectionHandle, uint8_t reason); 944 945 /** 946 * @brief User confirm auto reply. 947 * 948 * @param device Remote device. 949 * @param reqType Request type. 950 * @param accept Request accept or not. 951 */ 952 void UserConfirmAutoReply(const RawAddress &device, int reqType, bool accept) const; 953 954 /** 955 * @brief Set pin code. 956 * 957 * @param device Remote device. 958 * @param pin Pin code. 959 * @param accept Request accept or not. 960 * @return Returns <b>true</b> if the operation is successful; 961 * returns <b>false</b> if the operation fails. 962 */ 963 bool SetPinCode(const RawAddress &device, const std::vector<uint8_t> &pin, bool accept) const; 964 965 /** 966 * @brief Check if auto reply. 967 * 968 * @param remoteIo Remote device Io capability. 969 * @return Returns <b>true</b> if the operation is successful; 970 * returns <b>false</b> if the operation fails. 971 */ 972 bool CheckAutoReply(int remoteIo, int localMitmRequired, int remoteMitmRequired) const; 973 974 /** 975 * @brief Check ssp confirm type. 976 * 977 * @param remoteIo Remote device Io capability. 978 * @param type Confirm type. 979 * @return Returns confirm type. 980 */ 981 int CheckSspConfirmType(int remoteIo, int type) const; 982 983 /** 984 * @brief Find remote device. 985 * 986 * @param remoteAddr Remote device address. 987 * @return Returns remote device. 988 */ 989 std::shared_ptr<ClassicRemoteDevice> FindRemoteDevice(const RawAddress &device); 990 991 /** 992 * @brief Parser remote name from eir data. 993 * 994 * @param remote Remote device. 995 * @param type type of Eir data structure. 996 * @param value Eir data of Eir data structure. 997 * @return Returns <b>true</b> if the operation is successful; 998 * returns <b>false</b> if the operation fails. 999 */ 1000 bool ParserRemoteNameFromEir( 1001 std::shared_ptr<ClassicRemoteDevice> remote, int type, const std::vector<uint8_t> &value) const; 1002 1003 /** 1004 * @brief Parser service uuid from eir data. 1005 * 1006 * @param type type of Eir data structure. 1007 * @param value Eir data of Eir data structure. 1008 * @return Returns uuids parser from eir data. 1009 */ 1010 std::vector<Uuid> ParserUuidFromEir(int type, const std::vector<uint8_t> &value) const; 1011 1012 /** 1013 * @brief Parser 16 bit service uuid from eir data. 1014 * 1015 * @param value Eir data of Eir data structure. 1016 * @return Returns 16 Bit uuids parser from eir data. 1017 */ 1018 std::vector<Uuid> Parser16BitUuidFromEir(const std::vector<uint8_t> &value) const; 1019 1020 /** 1021 * @brief Parser 32 bit service uuid from eir data. 1022 * 1023 * @param value Eir data of Eir data structure. 1024 * @return Returns 32 Bit uuids parses from eir data. 1025 */ 1026 std::vector<Uuid> Parser32BitUuidFromEir(const std::vector<uint8_t> &value) const; 1027 1028 /** 1029 * @brief Parser 128 bit service uuid from eir data. 1030 * 1031 * @param value Eir data of Eir data structure. 1032 * @return Returns 128 Bit uuids parser from eir data. 1033 */ 1034 std::vector<Uuid> Parser128BitUuidFromEir(const std::vector<uint8_t> &value) const; 1035 1036 /** 1037 * @brief Save remote device uuids. 1038 * If uuids is changed, send OnRemoteUuidChanged to client. 1039 * 1040 * @param remote Remote device. 1041 * @param uuids Remote device uuids. 1042 */ 1043 void SaveRemoteDeviceUuids(std::shared_ptr<ClassicRemoteDevice> remote, const std::vector<Uuid> &uuids) const; 1044 1045 /** 1046 * @brief Check uuids1 is equal with uuids2. 1047 * 1048 * @param uuids1 uuids. 1049 * @param uuids2 uuids. 1050 * @return Returns <b>true</b> if the operation is successful; 1051 * returns <b>false</b> if the operation fails. 1052 */ 1053 bool IsUuidsEqual(const std::vector<Uuid> &uuids1, const std::vector<Uuid> &uuids2) const; 1054 1055 void InitMode(); 1056 void FreeMemory(); 1057 void DisableBTM(); 1058 bool DiscoverRemoteName(); 1059 bool CancelGetRemoteName() const; 1060 void SearchRemoteUuids(const RawAddress &device, uint16_t uuid); 1061 void ResetScanMode(); 1062 void DeleteLinkKey(std::shared_ptr<ClassicRemoteDevice> remoteDevice) const; 1063 BtAddr ConvertToBtAddr(const RawAddress &device) const; 1064 void DisablePairProcess(); 1065 void SearchAttributeEnd(const RawAddress &device, const std::vector<Uuid> &uuids); 1066 void PinCodeReq(const BtAddr &addr); 1067 ClassicAdapterProperties &adapterProperties_; 1068 std::unique_ptr<utility::Timer> timer_ {}; 1069 std::unique_ptr<utility::Timer> hwTimer_ {}; 1070 int discoveryState_ {}; 1071 int scanMode_ {}; 1072 long discoveryEndMs_ {}; 1073 bool pinMode_ {}; 1074 bool btmEnableSuccess_ {}; 1075 bool isDisable_ {}; 1076 bool receiveInquiryComplete_ {}; 1077 bool cancelDiscovery_ {}; 1078 bool waitPairResult_ {}; 1079 uint16_t searchUuid_ {}; 1080 std::vector<Uuid> uuids_ {}; 1081 std::string remoteNameAddr_ {INVALID_MAC_ADDRESS}; 1082 std::map<std::string, std::shared_ptr<ClassicRemoteDevice>> devices_ {}; 1083 BtmAclCallbacks btmAclCbs_ {}; 1084 ClassicBluetoothData eirData_ {}; 1085 std::unique_ptr<ClassicBatteryObserverHf> batteryObserverHf_ {}; 1086 IProfileHfpHf *hfService_ {nullptr}; 1087 std::unique_ptr<ClassicBatteryObserverAg> batteryObserverAg_ {}; 1088 IProfileHfpAg *agService_ {nullptr}; 1089 1090 DECLARE_IMPL(); 1091 BT_DISALLOW_COPY_AND_ASSIGN(ClassicAdapter); 1092 }; 1093 } // namespace bluetooth 1094 } // namespace OHOS 1095 #endif // CLASSIC_ADAPTER_H