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