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