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