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 <unordered_set> 22 #include <utility> 23 24 #include "os/handler.h" 25 26 #include "hci/acl_manager/le_acl_connection.h" 27 #include "hci/address.h" 28 #include "hci/address_with_type.h" 29 #include "hci/le_advertising_manager.h" 30 #include "l2cap/internal/parameter_provider.h" 31 #include "l2cap/internal/scheduler.h" 32 #include "l2cap/le/fixed_channel_manager.h" 33 #include "l2cap/le/internal/dynamic_channel_service_manager_impl.h" 34 #include "l2cap/le/internal/fixed_channel_service_manager_impl.h" 35 #include "l2cap/le/internal/link.h" 36 #include "l2cap/le/link_property_listener.h" 37 38 namespace bluetooth { 39 namespace l2cap { 40 namespace le { 41 namespace internal { 42 43 class LinkManager : public hci::acl_manager::LeConnectionCallbacks { 44 public: LinkManager(os::Handler * l2cap_handler,hci::AclManager * acl_manager,FixedChannelServiceManagerImpl * service_manager,DynamicChannelServiceManagerImpl * dynamic_service_manager,l2cap::internal::ParameterProvider * parameter_provider)45 LinkManager( 46 os::Handler* l2cap_handler, 47 hci::AclManager* acl_manager, 48 FixedChannelServiceManagerImpl* service_manager, 49 DynamicChannelServiceManagerImpl* dynamic_service_manager, 50 l2cap::internal::ParameterProvider* parameter_provider) 51 : l2cap_handler_(l2cap_handler), 52 acl_manager_(acl_manager), 53 fixed_channel_service_manager_(service_manager), 54 dynamic_channel_service_manager_(dynamic_service_manager), 55 parameter_provider_(parameter_provider) { 56 acl_manager_->RegisterLeCallbacks(this, l2cap_handler_); 57 } 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::AddressWithType address_with_type); 71 void OnLeConnectSuccess(hci::AddressWithType connecting_address_with_type, 72 std::unique_ptr<hci::acl_manager::LeAclConnection> acl_connection) override; 73 void OnLeConnectFail(hci::AddressWithType address_with_type, hci::ErrorCode reason) override; 74 75 // FixedChannelManager methods 76 77 void ConnectFixedChannelServices(hci::AddressWithType address_with_type, 78 PendingFixedChannelConnection pending_fixed_channel_connection); 79 80 // DynamicChannelManager methods 81 82 void ConnectDynamicChannelServices(hci::AddressWithType device, 83 Link::PendingDynamicChannelConnection pending_dynamic_channel_connection, Psm psm); 84 85 void OnDisconnect(hci::AddressWithType address_with_type); 86 87 // Link methods 88 89 void RegisterLinkPropertyListener(os::Handler* handler, LinkPropertyListener* listener); 90 91 void OnReadRemoteVersionInformationComplete( 92 hci::ErrorCode hci_status, 93 hci::AddressWithType address_with_type, 94 uint8_t lmp_version, 95 uint16_t manufacturer_name, 96 uint16_t sub_version); 97 98 // Reported by link to indicate how many pending packets are remaining to be set. 99 // If there is anything outstanding, don't delete link 100 void OnPendingPacketChange(hci::AddressWithType remote, int num_packets); 101 102 private: 103 // Dependencies 104 os::Handler* l2cap_handler_; 105 hci::AclManager* acl_manager_; 106 FixedChannelServiceManagerImpl* fixed_channel_service_manager_; 107 DynamicChannelServiceManagerImpl* dynamic_channel_service_manager_; 108 l2cap::internal::ParameterProvider* parameter_provider_; 109 110 // Internal states 111 std::unordered_map<hci::AddressWithType, PendingLink> pending_links_; 112 std::unordered_map<hci::AddressWithType, Link> links_; 113 std::unordered_map<hci::AddressWithType, std::list<std::pair<Psm, Link::PendingDynamicChannelConnection>>> 114 pending_dynamic_channels_; 115 os::Handler* link_property_callback_handler_ = nullptr; 116 LinkPropertyListener* link_property_listener_ = nullptr; 117 std::unordered_set<hci::AddressWithType> disconnected_links_; 118 std::unordered_set<hci::AddressWithType> links_with_pending_packets_; 119 120 DISALLOW_COPY_AND_ASSIGN(LinkManager); 121 }; 122 123 } // namespace internal 124 } // namespace le 125 } // namespace l2cap 126 } // namespace bluetooth 127