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