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