1 /* 2 * Copyright 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <future> 20 #include <memory> 21 22 #include "gd/hci/acl_manager/connection_callbacks.h" 23 #include "gd/hci/acl_manager/le_connection_callbacks.h" 24 #include "gd/hci/address.h" 25 #include "gd/hci/address_with_type.h" 26 #include "gd/hci/class_of_device.h" 27 #include "gd/os/handler.h" 28 #include "gd/packet/raw_builder.h" 29 #include "main/shim/acl_legacy_interface.h" 30 #include "main/shim/link_connection_interface.h" 31 #include "main/shim/link_policy_interface.h" 32 #include "stack/include/bt_types.h" 33 #include "types/raw_address.h" 34 35 namespace bluetooth { 36 namespace shim { 37 namespace legacy { 38 39 class Acl : public hci::acl_manager::ConnectionCallbacks, 40 public hci::acl_manager::LeConnectionCallbacks, 41 public LinkConnectionInterface, 42 public LinkPolicyInterface { 43 public: 44 Acl(os::Handler* handler, const acl_interface_t& acl_interface, 45 uint8_t max_acceptlist_size, uint8_t max_address_resolution_size); 46 47 Acl(const Acl&) = delete; 48 Acl& operator=(const Acl&) = delete; 49 50 ~Acl(); 51 52 // hci::acl_manager::ConnectionCallbacks 53 void OnConnectSuccess( 54 std::unique_ptr<hci::acl_manager::ClassicAclConnection>) override; 55 void OnConnectFail(hci::Address, hci::ErrorCode reason) override; 56 57 void HACK_OnEscoConnectRequest(hci::Address, hci::ClassOfDevice) override; 58 void HACK_OnScoConnectRequest(hci::Address, hci::ClassOfDevice) override; 59 60 void OnClassicLinkDisconnected(uint16_t handle, hci::ErrorCode reason); 61 62 // hci::acl_manager::LeConnectionCallbacks 63 void OnLeConnectSuccess( 64 hci::AddressWithType, 65 std::unique_ptr<hci::acl_manager::LeAclConnection>) override; 66 void OnLeConnectFail(hci::AddressWithType, hci::ErrorCode reason) override; 67 void OnLeLinkDisconnected(uint16_t handle, hci::ErrorCode reason); 68 bluetooth::hci::AddressWithType GetConnectionLocalAddress( 69 const RawAddress& remote_bda); 70 71 // LinkConnectionInterface 72 void CreateClassicConnection(const hci::Address& address) override; 73 void CancelClassicConnection(const hci::Address& address) override; 74 void AcceptLeConnectionFrom(const hci::AddressWithType& address_with_type, 75 bool is_direct, 76 std::promise<bool> promise) override; 77 void IgnoreLeConnectionFrom( 78 const hci::AddressWithType& address_with_type) override; 79 void DisconnectClassic(uint16_t handle, tHCI_REASON reason, 80 std::string comment) override; 81 void DisconnectLe(uint16_t handle, tHCI_REASON reason, 82 std::string comment) override; 83 84 // Address Resolution List 85 void AddToAddressResolution(const hci::AddressWithType& address_with_type, 86 const std::array<uint8_t, 16>& peer_irk, 87 const std::array<uint8_t, 16>& local_irk); 88 void RemoveFromAddressResolution( 89 const hci::AddressWithType& address_with_type); 90 void ClearAddressResolution(); 91 92 // LinkPolicyInterface 93 bool HoldMode(uint16_t hci_handle, uint16_t max_interval, 94 uint16_t min_interval) override; 95 bool SniffMode(uint16_t hci_handle, uint16_t max_interval, 96 uint16_t min_interval, uint16_t attempt, 97 uint16_t timeout) override; 98 bool ExitSniffMode(uint16_t hci_handle) override; 99 bool SniffSubrating(uint16_t hci_handle, uint16_t maximum_latency, 100 uint16_t minimum_remote_timeout, 101 uint16_t minimum_local_timeout) override; 102 103 void WriteData(uint16_t hci_handle, 104 std::unique_ptr<packet::RawBuilder> packet); 105 106 void Dump(int fd) const; 107 void DumpConnectionHistory(int fd) const; 108 109 void Shutdown(); 110 void FinalShutdown(); 111 112 void ClearAcceptList(); 113 114 protected: 115 void on_incoming_acl_credits(uint16_t handle, uint16_t credits); 116 void write_data_sync(uint16_t hci_handle, 117 std::unique_ptr<packet::RawBuilder> packet); 118 119 private: 120 os::Handler* handler_; 121 const acl_interface_t acl_interface_; 122 123 bool CheckForOrphanedAclConnections() const; 124 125 struct impl; 126 std::unique_ptr<impl> pimpl_; 127 }; 128 129 } // namespace legacy 130 } // namespace shim 131 } // namespace bluetooth 132