• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <utility>
22 
23 #include "os/handler.h"
24 
25 #include "hci/acl_manager.h"
26 #include "hci/address.h"
27 #include "hci/address_with_type.h"
28 #include "l2cap/internal/parameter_provider.h"
29 #include "l2cap/internal/scheduler.h"
30 #include "l2cap/le/fixed_channel_manager.h"
31 #include "l2cap/le/internal/fixed_channel_service_manager_impl.h"
32 #include "l2cap/le/internal/link.h"
33 
34 namespace bluetooth {
35 namespace l2cap {
36 namespace le {
37 namespace internal {
38 
39 class LinkManager : public hci::LeConnectionCallbacks {
40  public:
LinkManager(os::Handler * l2cap_handler,hci::AclManager * acl_manager,FixedChannelServiceManagerImpl * service_manager,l2cap::internal::ParameterProvider * parameter_provider)41   LinkManager(os::Handler* l2cap_handler, hci::AclManager* acl_manager, FixedChannelServiceManagerImpl* service_manager,
42               l2cap::internal::ParameterProvider* parameter_provider)
43       : l2cap_handler_(l2cap_handler), acl_manager_(acl_manager), fixed_channel_service_manager_(service_manager),
44         parameter_provider_(parameter_provider) {
45     acl_manager_->RegisterLeCallbacks(this, l2cap_handler_);
46   }
47 
48   struct PendingFixedChannelConnection {
49     os::Handler* handler_;
50     FixedChannelManager::OnConnectionFailureCallback on_fail_callback_;
51   };
52 
53   struct PendingLink {
54     std::vector<PendingFixedChannelConnection> pending_fixed_channel_connections_;
55   };
56 
57   // ACL methods
58 
59   Link* GetLink(hci::AddressWithType address_with_type);
60   void OnLeConnectSuccess(hci::AddressWithType connecting_address_with_type,
61                           std::unique_ptr<hci::AclConnection> acl_connection) override;
62   void OnLeConnectFail(hci::AddressWithType address_with_type, hci::ErrorCode reason) override;
63   void OnDisconnect(hci::AddressWithType address_with_type, hci::ErrorCode status);
64 
65   // FixedChannelManager methods
66 
67   void ConnectFixedChannelServices(hci::AddressWithType address_with_type,
68                                    PendingFixedChannelConnection pending_fixed_channel_connection);
69 
70   // DynamicChannelManager methods
71 
72   void ConnectDynamicChannelServices(hci::AddressWithType device,
73                                      Link::PendingDynamicChannelConnection pending_dynamic_channel_connection, Psm psm);
74 
75  private:
76   // Dependencies
77   os::Handler* l2cap_handler_;
78   hci::AclManager* acl_manager_;
79   FixedChannelServiceManagerImpl* fixed_channel_service_manager_;
80   DynamicChannelServiceManagerImpl* dynamic_channel_service_manager_;
81   l2cap::internal::ParameterProvider* parameter_provider_;
82 
83   // Internal states
84   std::unordered_map<hci::AddressWithType, PendingLink> pending_links_;
85   std::unordered_map<hci::AddressWithType, Link> links_;
86   std::unordered_map<hci::AddressWithType, std::list<std::pair<Psm, Link::PendingDynamicChannelConnection>>>
87       pending_dynamic_channels_;
88   DISALLOW_COPY_AND_ASSIGN(LinkManager);
89 };
90 
91 }  // namespace internal
92 }  // namespace le
93 }  // namespace l2cap
94 }  // namespace bluetooth
95