1 /* 2 * Copyright 2018 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 <chrono> 20 #include <cstdint> 21 #include <set> 22 #include <unordered_map> 23 24 #include "acl_connection.h" 25 #include "hci/address.h" 26 #include "hci/address_with_type.h" 27 #include "isochronous_connection_handler.h" 28 #include "phy.h" 29 #include "sco_connection.h" 30 31 namespace rootcanal { 32 static constexpr uint16_t kReservedHandle = 0xF00; 33 34 class AclConnectionHandler { 35 public: 36 AclConnectionHandler() = default; 37 virtual ~AclConnectionHandler() = default; 38 39 using TaskId = uint32_t; 40 41 // Reset the connection manager state, stopping any pending 42 // SCO connections. 43 void Reset(std::function<void(TaskId)> stopStream); 44 45 bool CreatePendingConnection(bluetooth::hci::Address addr, 46 bool authenticate_on_connect, 47 bool allow_role_switch); 48 bool HasPendingConnection(bluetooth::hci::Address addr) const; 49 bool CancelPendingConnection(bluetooth::hci::Address addr); 50 bool AuthenticatePendingConnection() const; 51 52 bool HasPendingScoConnection(bluetooth::hci::Address addr) const; 53 ScoState GetScoConnectionState(bluetooth::hci::Address addr) const; 54 bool IsLegacyScoConnection(bluetooth::hci::Address addr) const; 55 void CreateScoConnection(bluetooth::hci::Address addr, 56 ScoConnectionParameters const& parameters, 57 ScoState state, ScoDatapath datapath, 58 bool legacy = false); 59 void CancelPendingScoConnection(bluetooth::hci::Address addr); 60 bool AcceptPendingScoConnection(bluetooth::hci::Address addr, 61 ScoLinkParameters const& parameters, 62 std::function<TaskId()> startStream); 63 bool AcceptPendingScoConnection(bluetooth::hci::Address addr, 64 ScoConnectionParameters const& parameters, 65 std::function<TaskId()> startStream); 66 uint16_t GetScoHandle(bluetooth::hci::Address addr) const; 67 ScoConnectionParameters GetScoConnectionParameters( 68 bluetooth::hci::Address addr) const; 69 ScoLinkParameters GetScoLinkParameters(bluetooth::hci::Address addr) const; 70 71 bool CreatePendingLeConnection(bluetooth::hci::AddressWithType peer, 72 bluetooth::hci::AddressWithType resolved_peer, 73 bluetooth::hci::AddressWithType local_address); 74 bool HasPendingLeConnection(bluetooth::hci::AddressWithType addr) const; 75 bool CancelPendingLeConnection(bluetooth::hci::AddressWithType addr); 76 77 uint16_t CreateConnection(bluetooth::hci::Address addr, 78 bluetooth::hci::Address own_addr); 79 uint16_t CreateLeConnection(bluetooth::hci::AddressWithType addr, 80 bluetooth::hci::AddressWithType own_addr, 81 bluetooth::hci::Role role); 82 bool Disconnect(uint16_t handle, std::function<void(TaskId)> stopStream); 83 bool HasHandle(uint16_t handle) const; 84 bool HasScoHandle(uint16_t handle) const; 85 86 uint16_t GetHandle(bluetooth::hci::AddressWithType addr) const; 87 uint16_t GetHandleOnlyAddress(bluetooth::hci::Address addr) const; 88 bluetooth::hci::AddressWithType GetAddress(uint16_t handle) const; 89 std::optional<AddressWithType> GetAddressSafe(uint16_t handle) const; 90 bluetooth::hci::Address GetScoAddress(uint16_t handle) const; 91 bluetooth::hci::AddressWithType GetOwnAddress(uint16_t handle) const; 92 bluetooth::hci::AddressWithType GetResolvedAddress(uint16_t handle) const; 93 94 // Return the AclConnection for the selected connection handle, asserts 95 // if the handle is not currently used. 96 AclConnection& GetAclConnection(uint16_t handle); 97 98 void Encrypt(uint16_t handle); 99 bool IsEncrypted(uint16_t handle) const; 100 101 void SetRssi(uint16_t handle, int8_t rssi); 102 int8_t GetRssi(uint16_t handle) const; 103 104 Phy::Type GetPhyType(uint16_t handle) const; 105 106 uint16_t GetAclLinkPolicySettings(uint16_t handle) const; 107 void SetAclLinkPolicySettings(uint16_t handle, uint16_t settings); 108 109 bluetooth::hci::Role GetAclRole(uint16_t handle) const; 110 void SetAclRole(uint16_t handle, bluetooth::hci::Role role); 111 112 std::unique_ptr<bluetooth::hci::LeSetCigParametersCompleteBuilder> 113 SetCigParameters(uint8_t id, uint32_t sdu_interval_m_to_s, 114 uint32_t sdu_interval_s_to_m, 115 bluetooth::hci::ClockAccuracy accuracy, 116 bluetooth::hci::Packing packing, 117 bluetooth::hci::Enable framing, 118 uint16_t max_transport_latency_m_to_s, 119 uint16_t max_transport_latency_s_to_m, 120 std::vector<bluetooth::hci::CisParametersConfig>& streams); 121 122 void CreatePendingCis(bluetooth::hci::CreateCisConfig config); 123 124 bool ConnectCis(uint16_t handle); 125 126 void SetRemoteCisHandle(uint16_t handle, uint16_t remote_handle); 127 128 uint16_t GetPendingAclHandle(uint16_t cis_handle) const; 129 130 bool RejectCis(uint16_t handle); 131 132 bool DisconnectCis(uint16_t handle); 133 134 bluetooth::hci::ErrorCode RemoveCig(uint8_t cig_id); 135 136 bool HasPendingCis() const; 137 138 bool HasPendingCisConnection(uint16_t handle) const; 139 140 bool HasCisHandle(uint16_t handle) const; 141 142 bool HasConnectedCis(uint16_t handle) const; 143 144 uint16_t GetAclHandleForCisHandle(uint16_t cis_handle) const; 145 uint16_t GetRemoteCisHandleForCisHandle(uint16_t cis_handle) const; 146 147 StreamParameters GetStreamParameters(uint16_t handle) const; 148 GroupParameters GetGroupParameters(uint8_t id) const; 149 150 std::vector<uint16_t> GetAclHandles() const; 151 152 void ResetLinkTimer(uint16_t handle); 153 std::chrono::steady_clock::duration TimeUntilLinkNearExpiring( 154 uint16_t handle) const; 155 bool IsLinkNearExpiring(uint16_t handle) const; 156 std::chrono::steady_clock::duration TimeUntilLinkExpired( 157 uint16_t handle) const; 158 bool HasLinkExpired(uint16_t handle) const; 159 bool IsRoleSwitchAllowedForPendingConnection() const; 160 161 private: 162 std::unordered_map<uint16_t, AclConnection> acl_connections_; 163 std::unordered_map<uint16_t, ScoConnection> sco_connections_; 164 165 bool classic_connection_pending_{false}; 166 bluetooth::hci::Address pending_connection_address_{ 167 bluetooth::hci::Address::kEmpty}; 168 bool authenticate_pending_classic_connection_{false}; 169 bool pending_classic_connection_allow_role_switch_{false}; 170 bool le_connection_pending_{false}; 171 bluetooth::hci::AddressWithType pending_le_connection_address_{ 172 bluetooth::hci::Address::kEmpty, 173 bluetooth::hci::AddressType::PUBLIC_DEVICE_ADDRESS}; 174 bluetooth::hci::AddressWithType pending_le_connection_own_address_{ 175 bluetooth::hci::Address::kEmpty, 176 bluetooth::hci::AddressType::PUBLIC_DEVICE_ADDRESS}; 177 bluetooth::hci::AddressWithType pending_le_connection_resolved_address_{ 178 bluetooth::hci::Address::kEmpty, 179 bluetooth::hci::AddressType::PUBLIC_DEVICE_ADDRESS}; 180 181 uint16_t GetUnusedHandle(); 182 uint16_t last_handle_{kReservedHandle - 2}; 183 IsochronousConnectionHandler isochronous_connection_handler_; 184 struct CisHandles { 185 uint16_t acl_handle_ = kReservedHandle; 186 uint16_t cis_handle_ = kReservedHandle; 187 uint16_t remote_cis_handle_ = kReservedHandle; 188 }; 189 std::vector<CisHandles> connected_streams_; 190 std::vector<CisHandles> pending_streams_; 191 }; 192 193 } // namespace rootcanal 194