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 <memory> 21 #include <unordered_map> 22 23 #include "hci/acl_manager/classic_acl_connection.h" 24 #include "l2cap/classic/dynamic_channel_configuration_option.h" 25 #include "l2cap/classic/internal/dynamic_channel_service_manager_impl.h" 26 #include "l2cap/classic/internal/fixed_channel_impl.h" 27 #include "l2cap/classic/internal/fixed_channel_service_manager_impl.h" 28 #include "l2cap/classic/security_enforcement_interface.h" 29 #include "l2cap/internal/data_pipeline_manager.h" 30 #include "l2cap/internal/dynamic_channel_allocator.h" 31 #include "l2cap/internal/dynamic_channel_impl.h" 32 #include "l2cap/internal/fixed_channel_allocator.h" 33 #include "l2cap/internal/ilink.h" 34 #include "l2cap/internal/parameter_provider.h" 35 #include "os/alarm.h" 36 #include "os/handler.h" 37 #include "signalling_manager.h" 38 39 namespace bluetooth { 40 namespace l2cap { 41 namespace classic { 42 namespace internal { 43 44 class LinkManager; 45 class DumpsysHelper; 46 47 class Link : public l2cap::internal::ILink, public hci::acl_manager::ConnectionManagementCallbacks { 48 public: 49 Link(os::Handler* l2cap_handler, std::unique_ptr<hci::acl_manager::ClassicAclConnection> acl_connection, 50 l2cap::internal::ParameterProvider* parameter_provider, 51 DynamicChannelServiceManagerImpl* dynamic_service_manager, FixedChannelServiceManagerImpl* fixed_service_manager, 52 LinkManager* link_manager); 53 GetDevice()54 hci::AddressWithType GetDevice() const override { 55 return {acl_connection_->GetAddress(), hci::AddressType::PUBLIC_DEVICE_ADDRESS}; 56 } 57 58 struct PendingDynamicChannelConnection { 59 os::Handler* handler_; 60 DynamicChannelManager::OnConnectionOpenCallback on_open_callback_; 61 DynamicChannelManager::OnConnectionFailureCallback on_fail_callback_; 62 classic::DynamicChannelConfigurationOption configuration_; 63 }; 64 65 struct PendingAuthenticateDynamicChannelConnection { 66 Psm psm_; 67 Cid cid_; 68 PendingDynamicChannelConnection pending_dynamic_channel_connection_; 69 }; 70 71 // ACL methods 72 73 virtual void OnAclDisconnected(hci::ErrorCode status); 74 75 virtual void Disconnect(); 76 77 virtual void Encrypt(); 78 79 virtual void Authenticate(); 80 81 virtual bool IsAuthenticated() const; 82 83 virtual void ReadRemoteVersionInformation(); 84 85 virtual void ReadRemoteSupportedFeatures(); 86 87 virtual void ReadRemoteExtendedFeatures(uint8_t page_number); 88 89 virtual void ReadClockOffset(); 90 91 // Increase the link usage refcount to ensure the link won't be disconnected when SecurityModule needs it 92 virtual void AcquireSecurityHold(); 93 94 // Decrease the link usage refcount when SecurityModule no longer needs it 95 virtual void ReleaseSecurityHold(); 96 97 // FixedChannel methods 98 99 std::shared_ptr<FixedChannelImpl> AllocateFixedChannel(Cid cid); 100 101 virtual bool IsFixedChannelAllocated(Cid cid); 102 103 // DynamicChannel methods 104 105 virtual Cid ReserveDynamicChannel(); 106 107 virtual void SendConnectionRequest(Psm psm, Cid local_cid); 108 virtual void SendConnectionRequest(Psm psm, Cid local_cid, 109 PendingDynamicChannelConnection pending_dynamic_channel_connection); 110 void SetChannelTxPriority(Cid local_cid, bool high_priority) override; 111 112 // When a Link is established, LinkManager notifies pending dynamic channels to connect 113 virtual void SetPendingDynamicChannels(std::list<Psm> psm_list, 114 std::list<Link::PendingDynamicChannelConnection> callback_list); 115 116 // Invoked by signalling manager to indicate an outgoing connection request failed and link shall free resources 117 virtual void OnOutgoingConnectionRequestFail(Cid local_cid, DynamicChannelManager::ConnectionResult result); 118 119 virtual void SendInitialConfigRequestOrQueue(Cid local_cid); 120 121 virtual void SendInformationRequest(InformationRequestInfoType type); 122 123 virtual void SendDisconnectionRequest(Cid local_cid, Cid remote_cid) override; 124 125 virtual std::shared_ptr<l2cap::internal::DynamicChannelImpl> AllocateDynamicChannel(Psm psm, Cid remote_cid); 126 127 virtual std::shared_ptr<l2cap::internal::DynamicChannelImpl> AllocateReservedDynamicChannel(Cid reserved_cid, Psm psm, 128 Cid remote_cid); 129 130 virtual classic::DynamicChannelConfigurationOption GetConfigurationForInitialConfiguration(Cid cid); 131 132 virtual void FreeDynamicChannel(Cid cid); 133 134 // Check how many channels are acquired or in use, if zero, start tear down timer, if non-zero, cancel tear down timer 135 virtual void RefreshRefCount(); 136 137 virtual void NotifyChannelCreation(Cid cid, std::unique_ptr<DynamicChannel> channel); 138 virtual void NotifyChannelFail(Cid cid, DynamicChannelManager::ConnectionResult result); 139 140 // Information received from signaling channel 141 virtual void SetRemoteConnectionlessMtu(Mtu mtu); 142 virtual Mtu GetRemoteConnectionlessMtu() const; 143 virtual bool GetRemoteSupportsErtm() const; 144 virtual bool GetRemoteSupportsFcs() const; 145 virtual void OnRemoteExtendedFeatureReceived(bool ertm_supported, bool fcs_supported); 146 ToString()147 virtual std::string ToString() const { 148 return GetDevice().ToString(); 149 } 150 SendLeCredit(Cid local_cid,uint16_t credit)151 void SendLeCredit(Cid local_cid, uint16_t credit) override {} 152 153 // ConnectionManagementCallbacks 154 void OnConnectionPacketTypeChanged(uint16_t packet_type) override; 155 void OnAuthenticationComplete(hci::ErrorCode hci_status) override; 156 void OnEncryptionChange(hci::EncryptionEnabled enabled) override; 157 void OnChangeConnectionLinkKeyComplete() override; 158 void OnReadClockOffsetComplete(uint16_t clock_offset) override; 159 void OnModeChange(hci::ErrorCode status, hci::Mode current_mode, uint16_t interval) override; 160 void OnSniffSubrating( 161 hci::ErrorCode hci_status, 162 uint16_t maximum_transmit_latency, 163 uint16_t maximum_receive_latency, 164 uint16_t minimum_remote_timeout, 165 uint16_t minimum_local_timeout) override; 166 void OnQosSetupComplete(hci::ServiceType service_type, uint32_t token_rate, uint32_t peak_bandwidth, uint32_t latency, 167 uint32_t delay_variation) override; 168 void OnFlowSpecificationComplete(hci::FlowDirection flow_direction, hci::ServiceType service_type, 169 uint32_t token_rate, uint32_t token_bucket_size, uint32_t peak_bandwidth, 170 uint32_t access_latency) override; 171 void OnFlushOccurred() override; 172 void OnRoleDiscoveryComplete(hci::Role current_role) override; 173 void OnReadLinkPolicySettingsComplete(uint16_t link_policy_settings) override; 174 void OnReadAutomaticFlushTimeoutComplete(uint16_t flush_timeout) override; 175 void OnReadTransmitPowerLevelComplete(uint8_t transmit_power_level) override; 176 void OnReadLinkSupervisionTimeoutComplete(uint16_t link_supervision_timeout) override; 177 void OnReadFailedContactCounterComplete(uint16_t failed_contact_counter) override; 178 void OnReadLinkQualityComplete(uint8_t link_quality) override; 179 void OnReadAfhChannelMapComplete(hci::AfhMode afh_mode, std::array<uint8_t, 10> afh_channel_map) override; 180 void OnReadRssiComplete(uint8_t rssi) override; 181 void OnReadClockComplete(uint32_t clock, uint16_t accuracy) override; 182 void OnCentralLinkKeyComplete(hci::KeyFlag key_flag) override; 183 void OnRoleChange(hci::ErrorCode hci_status, hci::Role new_role) override; 184 void OnDisconnection(hci::ErrorCode reason) override; 185 void OnReadRemoteVersionInformationComplete( 186 hci::ErrorCode hci_status, uint8_t lmp_version, uint16_t manufacturer_name, uint16_t sub_version); 187 void OnReadRemoteSupportedFeaturesComplete(uint64_t features); 188 void OnReadRemoteExtendedFeaturesComplete(uint8_t page_number, uint8_t max_page_number, uint64_t features); 189 190 struct EncryptionChangeListener { 191 Cid cid; 192 Psm psm; 193 }; 194 void AddEncryptionChangeListener(EncryptionChangeListener); 195 GetAclHandle()196 uint16_t GetAclHandle() const { 197 return acl_handle_; 198 } 199 GetRole()200 hci::Role GetRole() const { 201 return role_; 202 } 203 204 void OnPendingPacketChange(Cid local_cid, bool has_packet) override; 205 206 private: 207 friend class DumpsysHelper; 208 void connect_to_pending_dynamic_channels(); 209 void send_pending_configuration_requests(); 210 211 os::Handler* l2cap_handler_; 212 l2cap::internal::FixedChannelAllocator<FixedChannelImpl, Link> fixed_channel_allocator_{this, l2cap_handler_}; 213 l2cap::internal::DynamicChannelAllocator dynamic_channel_allocator_{this, l2cap_handler_}; 214 std::unique_ptr<hci::acl_manager::ClassicAclConnection> acl_connection_; 215 l2cap::internal::DataPipelineManager data_pipeline_manager_; 216 l2cap::internal::ParameterProvider* parameter_provider_; 217 DynamicChannelServiceManagerImpl* dynamic_service_manager_; 218 FixedChannelServiceManagerImpl* fixed_service_manager_; 219 LinkManager* link_manager_; 220 std::unordered_map<Cid, PendingDynamicChannelConnection> local_cid_to_pending_dynamic_channel_connection_map_; 221 os::Alarm link_idle_disconnect_alarm_{l2cap_handler_}; 222 ClassicSignallingManager signalling_manager_; 223 uint16_t acl_handle_; 224 Mtu remote_connectionless_mtu_ = kMinimumClassicMtu; 225 hci::Role role_ = hci::Role::CENTRAL; 226 bool remote_extended_feature_received_ = false; 227 bool remote_supports_ertm_ = false; 228 bool remote_supports_fcs_ = false; 229 hci::EncryptionEnabled encryption_enabled_ = hci::EncryptionEnabled::OFF; 230 std::list<Psm> pending_dynamic_psm_list_; 231 std::list<Link::PendingDynamicChannelConnection> pending_dynamic_channel_callback_list_; 232 std::list<uint16_t> pending_outgoing_configuration_request_list_; 233 bool used_by_security_module_ = false; 234 bool has_requested_authentication_ = false; 235 std::list<EncryptionChangeListener> encryption_change_listener_; 236 std::atomic_int remaining_packets_to_be_sent_ = 0; 237 DISALLOW_COPY_AND_ASSIGN(Link); 238 }; 239 240 } // namespace internal 241 } // namespace classic 242 } // namespace l2cap 243 } // namespace bluetooth 244