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