1 /* 2 * Copyright 2019 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 <atomic> 20 #include <chrono> 21 #include <memory> 22 23 #include "common/interfaces/ILoggable.h" 24 #include "hci/acl_manager/le_acl_connection.h" 25 #include "l2cap/internal/data_pipeline_manager.h" 26 #include "l2cap/internal/dynamic_channel_allocator.h" 27 #include "l2cap/internal/fixed_channel_allocator.h" 28 #include "l2cap/internal/ilink.h" 29 #include "l2cap/internal/parameter_provider.h" 30 #include "l2cap/le/dynamic_channel_manager.h" 31 #include "l2cap/le/internal/dynamic_channel_service_manager_impl.h" 32 #include "l2cap/le/internal/fixed_channel_impl.h" 33 #include "l2cap/le/internal/fixed_channel_service_manager_impl.h" 34 #include "l2cap/le/internal/signalling_manager.h" 35 #include "l2cap/le/link_options.h" 36 #include "l2cap/le/security_enforcement_interface.h" 37 #include "os/alarm.h" 38 39 namespace bluetooth { 40 namespace l2cap { 41 namespace le { 42 namespace internal { 43 44 class LinkManager; 45 46 class Link : public l2cap::internal::ILink, 47 public hci::acl_manager::LeConnectionManagementCallbacks, 48 public bluetooth::common::IRedactableLoggable { 49 public: 50 Link(os::Handler* l2cap_handler, std::unique_ptr<hci::acl_manager::LeAclConnection> acl_connection, 51 l2cap::internal::ParameterProvider* parameter_provider, 52 DynamicChannelServiceManagerImpl* dynamic_service_manager, FixedChannelServiceManagerImpl* fixed_service_manager, 53 LinkManager* link_manager); 54 55 Link(const Link&) = delete; 56 Link& operator=(const Link&) = delete; 57 58 ~Link() = default; 59 GetDevice()60 inline hci::AddressWithType GetDevice() const override { 61 return acl_connection_->GetRemoteAddress(); 62 } 63 64 struct PendingDynamicChannelConnection { 65 os::Handler* handler_; 66 DynamicChannelManager::OnConnectionOpenCallback on_open_callback_; 67 DynamicChannelManager::OnConnectionFailureCallback on_fail_callback_; 68 le::DynamicChannelConfigurationOption configuration_; 69 }; 70 GetRole()71 inline virtual hci::Role GetRole() { 72 return acl_connection_->GetRole(); 73 } 74 GetAclConnection()75 inline virtual hci::acl_manager::LeAclConnection* GetAclConnection() { 76 return acl_connection_.get(); 77 } 78 79 // ACL methods 80 81 virtual void OnAclDisconnected(hci::ErrorCode reason); 82 83 void OnDisconnection(hci::ErrorCode reason) override; 84 85 void OnConnectionUpdate( 86 hci::ErrorCode hci_status, 87 uint16_t connection_interval, 88 uint16_t connection_latency, 89 uint16_t supervision_timeout) override; 90 91 void OnDataLengthChange(uint16_t tx_octets, uint16_t tx_time, uint16_t rx_octets, uint16_t rx_time) override; 92 93 void OnReadRemoteVersionInformationComplete( 94 hci::ErrorCode hci_status, uint8_t lmp_version, uint16_t manufacturer_name, uint16_t sub_version) override; 95 96 void OnLeReadRemoteFeaturesComplete(hci::ErrorCode hci_status, uint64_t features) override; 97 98 void OnPhyUpdate(hci::ErrorCode hci_status, uint8_t tx_phy, uint8_t rx_phy) override; 99 100 void OnLeSubrateChange( 101 hci::ErrorCode hci_status, 102 uint16_t subrate_factor, 103 uint16_t peripheral_latency, 104 uint16_t continuation_number, 105 uint16_t supervision_timeout) override; 106 107 virtual void Disconnect(); 108 109 // Handles connection parameter update request from remote 110 virtual void UpdateConnectionParameterFromRemote(SignalId signal_id, uint16_t conn_interval_min, 111 uint16_t conn_interval_max, uint16_t conn_latency, 112 uint16_t supervision_timeout); 113 virtual bool CheckConnectionParameters( 114 uint16_t conn_interval_min, uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout); 115 116 virtual void SendConnectionParameterUpdate(uint16_t conn_interval_min, uint16_t conn_interval_max, 117 uint16_t conn_latency, uint16_t supervision_timeout, 118 uint16_t min_ce_length, uint16_t max_ce_length); 119 120 // FixedChannel methods 121 122 virtual std::shared_ptr<FixedChannelImpl> AllocateFixedChannel(Cid cid, SecurityPolicy security_policy); 123 124 virtual bool IsFixedChannelAllocated(Cid cid); 125 126 // DynamicChannel methods 127 128 virtual Cid ReserveDynamicChannel(); 129 130 virtual void SendConnectionRequest(Psm psm, PendingDynamicChannelConnection pending_dynamic_channel_connection); 131 132 void SendDisconnectionRequest(Cid local_cid, Cid remote_cid) override; 133 134 // Invoked by signalling manager to indicate an outgoing connection request failed and link shall free resources 135 virtual void OnOutgoingConnectionRequestFail(Cid local_cid, LeCreditBasedConnectionResponseResult result); 136 137 virtual std::shared_ptr<l2cap::internal::DynamicChannelImpl> AllocateDynamicChannel(Psm psm, Cid remote_cid); 138 139 virtual std::shared_ptr<l2cap::internal::DynamicChannelImpl> AllocateReservedDynamicChannel(Cid reserved_cid, Psm psm, 140 Cid remote_cid); 141 142 virtual void FreeDynamicChannel(Cid cid); 143 144 // Check how many channels are acquired or in use, if zero, start tear down timer, if non-zero, cancel tear down timer 145 virtual void RefreshRefCount(); 146 147 void NotifyChannelCreation(Cid cid, std::unique_ptr<DynamicChannel> user_channel); 148 void NotifyChannelFail(Cid cid, DynamicChannelManager::ConnectionResult result); 149 ToString()150 virtual std::string ToString() { 151 return GetDevice().ToString(); 152 } 153 ToStringForLogging()154 std::string ToStringForLogging() const override { 155 return GetDevice().ToStringForLogging(); 156 } 157 ToRedactedStringForLogging()158 std::string ToRedactedStringForLogging() const override { 159 return GetDevice().ToRedactedStringForLogging(); 160 } 161 162 virtual uint16_t GetMps() const; 163 164 virtual uint16_t GetInitialCredit() const; 165 166 void SendLeCredit(Cid local_cid, uint16_t credit) override; 167 GetLinkOptions()168 LinkOptions* GetLinkOptions() { 169 return &link_options_; 170 } 171 172 void ReadRemoteVersionInformation(); 173 174 void OnPendingPacketChange(Cid local_cid, bool has_packet) override; 175 176 private: 177 os::Handler* l2cap_handler_; 178 l2cap::internal::FixedChannelAllocator<FixedChannelImpl, Link> fixed_channel_allocator_{this, l2cap_handler_}; 179 l2cap::internal::DynamicChannelAllocator dynamic_channel_allocator_{this, l2cap_handler_}; 180 std::unique_ptr<hci::acl_manager::LeAclConnection> acl_connection_; 181 l2cap::internal::DataPipelineManager data_pipeline_manager_; 182 l2cap::internal::ParameterProvider* parameter_provider_; 183 DynamicChannelServiceManagerImpl* dynamic_service_manager_; 184 LeSignallingManager signalling_manager_; 185 std::unordered_map<Cid, PendingDynamicChannelConnection> local_cid_to_pending_dynamic_channel_connection_map_; 186 os::Alarm link_idle_disconnect_alarm_{l2cap_handler_}; 187 LinkOptions link_options_{acl_connection_.get(), this, l2cap_handler_}; 188 LinkManager* link_manager_; 189 SignalId update_request_signal_id_ = kInvalidSignalId; 190 uint16_t update_request_interval_min_; 191 uint16_t update_request_interval_max_; 192 uint16_t update_request_latency_; 193 uint16_t update_request_supervision_timeout_; 194 std::atomic_int remaining_packets_to_be_sent_ = 0; 195 196 // Received connection update complete from ACL manager. SignalId is bound to a valid number when we need to send a 197 // response to remote. If SignalId is bound to an invalid number, we don't send a response to remote, because the 198 // connection update request is not from remote LL peripheral. 199 void on_connection_update_complete(SignalId signal_id, hci::ErrorCode error_code); 200 }; 201 202 } // namespace internal 203 } // namespace le 204 } // namespace l2cap 205 } // namespace bluetooth 206