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 "hci/address.h" 24 #include "hci/class_of_device.h" 25 #include "l2cap/classic/dynamic_channel_manager.h" 26 #include "l2cap/classic/fixed_channel_manager.h" 27 #include "l2cap/classic/internal/dynamic_channel_service_manager_impl.h" 28 #include "l2cap/classic/internal/fixed_channel_service_manager_impl.h" 29 #include "l2cap/classic/internal/link.h" 30 #include "l2cap/classic/link_property_listener.h" 31 #include "l2cap/classic/link_security_interface.h" 32 #include "l2cap/internal/parameter_provider.h" 33 #include "l2cap/internal/scheduler.h" 34 #include "os/handler.h" 35 36 namespace bluetooth { 37 namespace l2cap { 38 namespace classic { 39 namespace internal { 40 41 class DumpsysHelper; 42 43 class LinkManager : public hci::acl_manager::ConnectionCallbacks { 44 public: LinkManager(os::Handler * l2cap_handler,hci::AclManager * acl_manager,FixedChannelServiceManagerImpl * fixed_channel_service_manager,DynamicChannelServiceManagerImpl * dynamic_channel_service_manager,l2cap::internal::ParameterProvider * parameter_provider)45 LinkManager(os::Handler* l2cap_handler, hci::AclManager* acl_manager, 46 FixedChannelServiceManagerImpl* fixed_channel_service_manager, 47 DynamicChannelServiceManagerImpl* dynamic_channel_service_manager, 48 l2cap::internal::ParameterProvider* parameter_provider) 49 : l2cap_handler_(l2cap_handler), acl_manager_(acl_manager), 50 fixed_channel_service_manager_(fixed_channel_service_manager), 51 dynamic_channel_service_manager_(dynamic_channel_service_manager), parameter_provider_(parameter_provider) { 52 acl_manager_->RegisterCallbacks(this, l2cap_handler_); 53 } 54 55 LinkManager(const LinkManager&) = delete; 56 LinkManager& operator=(const LinkManager&) = delete; 57 58 struct PendingFixedChannelConnection { 59 os::Handler* handler_; 60 FixedChannelManager::OnConnectionFailureCallback on_fail_callback_; 61 }; 62 63 struct PendingLink { 64 std::vector<PendingFixedChannelConnection> pending_fixed_channel_connections_; 65 }; 66 67 // ACL methods 68 69 Link* GetLink(hci::Address device); 70 void OnConnectSuccess(std::unique_ptr<hci::acl_manager::ClassicAclConnection> acl_connection) override; 71 void OnConnectRequest(hci::Address, hci::ClassOfDevice) override; 72 void OnConnectFail(hci::Address device, hci::ErrorCode reason, bool locally_initiated) override; 73 74 void HACK_OnEscoConnectRequest(hci::Address, hci::ClassOfDevice) override; 75 void HACK_OnScoConnectRequest(hci::Address, hci::ClassOfDevice) override; 76 77 virtual void OnDisconnect(hci::Address device, hci::ErrorCode status); 78 void OnAuthenticationComplete(hci::ErrorCode hci_status, hci::Address device); 79 void OnEncryptionChange(hci::Address device, hci::EncryptionEnabled enabled); 80 void OnReadRemoteVersionInformation( 81 hci::ErrorCode hci_status, 82 hci::Address device, 83 uint8_t lmp_version, 84 uint16_t manufacturer_name, 85 uint16_t sub_version); 86 void OnReadRemoteSupportedFeatures(hci::Address device, uint64_t features); 87 void OnReadRemoteExtendedFeatures( 88 hci::Address device, uint8_t page_number, uint8_t max_page_number, uint64_t features); 89 void OnRoleChange(hci::ErrorCode hci_status, hci::Address remote, hci::Role role); 90 void OnReadClockOffset(hci::Address remote, uint16_t clock_offset); 91 void OnModeChange(hci::ErrorCode hci_status, hci::Address remote, hci::Mode mode, uint16_t interval); 92 void OnSniffSubrating( 93 hci::ErrorCode hci_status, 94 hci::Address remote, 95 uint16_t max_tx_lat, 96 uint16_t max_rx_lat, 97 uint16_t min_remote_timeout, 98 uint16_t min_local_timeout); 99 100 // FixedChannelManager methods 101 102 void ConnectFixedChannelServices(hci::Address device, PendingFixedChannelConnection pending_fixed_channel_connection); 103 104 // DynamicChannelManager methods 105 106 void ConnectDynamicChannelServices( 107 hci::Address device, Link::PendingDynamicChannelConnection pending_connection, Psm psm); 108 109 // For SecurityModule to initiate an ACL link 110 void InitiateConnectionForSecurity(hci::Address remote); 111 112 // LinkManager will handle sending OnLinkConnected() callback and construct a LinkSecurityInterface proxy. 113 void RegisterLinkSecurityInterfaceListener(os::Handler* handler, LinkSecurityInterfaceListener* listener); 114 115 // For the link to get LinkSecurityInterfaceListener 116 LinkSecurityInterfaceListener* GetLinkSecurityInterfaceListener(); 117 118 // Registerlink callbacks 119 void RegisterLinkPropertyListener(os::Handler* handler, LinkPropertyListener* listener); 120 121 // Reported by link to indicate how many pending packets are remaining to be set. 122 // If there is anything outstanding, don't delete link 123 void OnPendingPacketChange(hci::Address remote, int num_packets); 124 125 private: 126 // Handles requests from LinkSecurityInterface 127 friend class LinkSecurityInterfaceImpl; 128 friend class DumpsysHelper; 129 void handle_link_security_hold(hci::Address remote); 130 void handle_link_security_release(hci::Address remote); 131 void handle_link_security_disconnect(hci::Address remote); 132 void handle_link_security_ensure_authenticated(hci::Address remote); 133 void handle_link_security_ensure_encrypted(hci::Address remote); 134 135 // Dependencies 136 os::Handler* l2cap_handler_; 137 hci::AclManager* acl_manager_; 138 FixedChannelServiceManagerImpl* fixed_channel_service_manager_; 139 DynamicChannelServiceManagerImpl* dynamic_channel_service_manager_; 140 l2cap::internal::ParameterProvider* parameter_provider_; 141 142 // Internal states 143 std::unordered_map<hci::Address, PendingLink> pending_links_; 144 std::unordered_map<hci::Address, Link> links_; 145 std::unordered_map<hci::Address, std::list<Psm>> pending_dynamic_channels_; 146 std::unordered_map<hci::Address, std::list<Link::PendingDynamicChannelConnection>> 147 pending_dynamic_channels_callbacks_; 148 os::Handler* link_security_interface_listener_handler_ = nullptr; 149 LinkSecurityInterfaceListener* link_security_interface_listener_ = nullptr; 150 LinkPropertyListener* link_property_listener_ = nullptr; 151 os::Handler* link_property_callback_handler_ = nullptr; 152 std::unordered_set<hci::Address> disconnected_links_; 153 std::unordered_set<hci::Address> links_with_pending_packets_; 154 }; 155 156 } // namespace internal 157 } // namespace classic 158 } // namespace l2cap 159 } // namespace bluetooth 160