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 "l2cap/classic/dynamic_channel_manager.h"
25 #include "l2cap/classic/fixed_channel_manager.h"
26 #include "l2cap/classic/internal/dynamic_channel_service_manager_impl.h"
27 #include "l2cap/classic/internal/fixed_channel_service_manager_impl.h"
28 #include "l2cap/classic/internal/link.h"
29 #include "l2cap/classic/link_security_interface.h"
30 #include "l2cap/internal/parameter_provider.h"
31 #include "l2cap/internal/scheduler.h"
32 #include "os/handler.h"
33 
34 namespace bluetooth {
35 namespace l2cap {
36 namespace classic {
37 namespace internal {
38 
39 class LinkManager : public hci::acl_manager::ConnectionCallbacks {
40  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)41   LinkManager(os::Handler* l2cap_handler, hci::AclManager* acl_manager,
42               FixedChannelServiceManagerImpl* fixed_channel_service_manager,
43               DynamicChannelServiceManagerImpl* dynamic_channel_service_manager,
44               l2cap::internal::ParameterProvider* parameter_provider)
45       : l2cap_handler_(l2cap_handler), acl_manager_(acl_manager),
46         fixed_channel_service_manager_(fixed_channel_service_manager),
47         dynamic_channel_service_manager_(dynamic_channel_service_manager), parameter_provider_(parameter_provider) {
48     acl_manager_->RegisterCallbacks(this, l2cap_handler_);
49   }
50 
51   struct PendingFixedChannelConnection {
52     os::Handler* handler_;
53     FixedChannelManager::OnConnectionFailureCallback on_fail_callback_;
54   };
55 
56   struct PendingLink {
57     std::vector<PendingFixedChannelConnection> pending_fixed_channel_connections_;
58   };
59 
60   // ACL methods
61 
62   Link* GetLink(hci::Address device);
63   void OnConnectSuccess(std::unique_ptr<hci::acl_manager::ClassicAclConnection> acl_connection) override;
64   void OnConnectFail(hci::Address device, hci::ErrorCode reason) override;
65   virtual void OnDisconnect(hci::Address device, hci::ErrorCode status);
66 
67   // FixedChannelManager methods
68 
69   void ConnectFixedChannelServices(hci::Address device, PendingFixedChannelConnection pending_fixed_channel_connection);
70 
71   // DynamicChannelManager methods
72 
73   void ConnectDynamicChannelServices(
74       hci::Address device, Link::PendingDynamicChannelConnection pending_connection, Psm psm);
75 
76   // For SecurityModule to initiate an ACL link
77   void InitiateConnectionForSecurity(hci::Address remote);
78 
79   // LinkManager will handle sending OnLinkConnected() callback and construct a LinkSecurityInterface proxy.
80   void RegisterLinkSecurityInterfaceListener(os::Handler* handler, LinkSecurityInterfaceListener* listener);
81 
82  private:
83   // Handles requests from LinkSecurityInterface
84   friend class LinkSecurityInterfaceImpl;
85   void handle_link_security_hold(hci::Address remote);
86   void handle_link_security_release(hci::Address remote);
87   void handle_link_security_disconnect(hci::Address remote);
88   void handle_link_security_ensure_authenticated(hci::Address remote);
89 
90   // Dependencies
91   os::Handler* l2cap_handler_;
92   hci::AclManager* acl_manager_;
93   FixedChannelServiceManagerImpl* fixed_channel_service_manager_;
94   DynamicChannelServiceManagerImpl* dynamic_channel_service_manager_;
95   l2cap::internal::ParameterProvider* parameter_provider_;
96 
97   // Internal states
98   std::unordered_map<hci::Address, PendingLink> pending_links_;
99   std::unordered_map<hci::Address, Link> links_;
100   std::unordered_map<hci::Address, std::list<Psm>> pending_dynamic_channels_;
101   std::unordered_map<hci::Address, std::list<Link::PendingDynamicChannelConnection>>
102       pending_dynamic_channels_callbacks_;
103   os::Handler* link_security_interface_listener_handler_ = nullptr;
104   LinkSecurityInterfaceListener* link_security_interface_listener_ = nullptr;
105   DISALLOW_COPY_AND_ASSIGN(LinkManager);
106 };
107 
108 }  // namespace internal
109 }  // namespace classic
110 }  // namespace l2cap
111 }  // namespace bluetooth
112