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 #include <vector>
24 
25 #include "hci/acl_manager/le_acl_connection.h"
26 #include "hci/address.h"
27 #include "hci/address_with_type.h"
28 #include "hci/le_advertising_manager.h"
29 #include "l2cap/internal/parameter_provider.h"
30 #include "l2cap/internal/scheduler.h"
31 #include "l2cap/le/fixed_channel_manager.h"
32 #include "l2cap/le/internal/dynamic_channel_service_manager_impl.h"
33 #include "l2cap/le/internal/fixed_channel_service_manager_impl.h"
34 #include "l2cap/le/internal/link.h"
35 #include "l2cap/le/link_property_listener.h"
36 #include "os/handler.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   LinkManager(const LinkManager&) = delete;
60   LinkManager& operator=(const LinkManager&) = delete;
61 
62   struct PendingFixedChannelConnection {
63     os::Handler* handler_;
64     FixedChannelManager::OnConnectionFailureCallback on_fail_callback_;
65   };
66 
67   struct PendingLink {
68     std::vector<PendingFixedChannelConnection> pending_fixed_channel_connections_;
69   };
70 
71   // ACL methods
72 
73   Link* GetLink(hci::AddressWithType address_with_type);
74   void OnLeConnectSuccess(hci::AddressWithType connecting_address_with_type,
75                           std::unique_ptr<hci::acl_manager::LeAclConnection> acl_connection) override;
76   void OnLeConnectFail(hci::AddressWithType address_with_type, hci::ErrorCode reason) override;
77 
78   // FixedChannelManager methods
79 
80   void ConnectFixedChannelServices(hci::AddressWithType address_with_type,
81                                    PendingFixedChannelConnection pending_fixed_channel_connection);
82 
83   // DynamicChannelManager methods
84 
85   void ConnectDynamicChannelServices(hci::AddressWithType device,
86                                      Link::PendingDynamicChannelConnection pending_dynamic_channel_connection, Psm psm);
87 
88   void OnDisconnect(hci::AddressWithType address_with_type);
89 
90   // Link methods
91 
92   void RegisterLinkPropertyListener(os::Handler* handler, LinkPropertyListener* listener);
93 
94   void OnReadRemoteVersionInformationComplete(
95       hci::ErrorCode hci_status,
96       hci::AddressWithType address_with_type,
97       uint8_t lmp_version,
98       uint16_t manufacturer_name,
99       uint16_t sub_version);
100 
101   // Reported by link to indicate how many pending packets are remaining to be set.
102   // If there is anything outstanding, don't delete link
103   void OnPendingPacketChange(hci::AddressWithType remote, int num_packets);
104 
105  private:
106   // Dependencies
107   os::Handler* l2cap_handler_;
108   hci::AclManager* acl_manager_;
109   FixedChannelServiceManagerImpl* fixed_channel_service_manager_;
110   DynamicChannelServiceManagerImpl* dynamic_channel_service_manager_;
111   l2cap::internal::ParameterProvider* parameter_provider_;
112 
113   // Internal states
114   std::unordered_map<hci::AddressWithType, PendingLink> pending_links_;
115   std::unordered_map<hci::AddressWithType, Link> links_;
116   std::unordered_map<hci::AddressWithType, std::list<std::pair<Psm, Link::PendingDynamicChannelConnection>>>
117       pending_dynamic_channels_;
118   os::Handler* link_property_callback_handler_ = nullptr;
119   LinkPropertyListener* link_property_listener_ = nullptr;
120   std::unordered_set<hci::AddressWithType> disconnected_links_;
121   std::unordered_set<hci::AddressWithType> links_with_pending_packets_;
122 };
123 
124 }  // namespace internal
125 }  // namespace le
126 }  // namespace l2cap
127 }  // namespace bluetooth
128