• 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 <chrono>
20 #include <memory>
21 
22 #include "hci/acl_manager.h"
23 #include "l2cap/internal/data_pipeline_manager.h"
24 #include "l2cap/internal/dynamic_channel_allocator.h"
25 #include "l2cap/internal/fixed_channel_allocator.h"
26 #include "l2cap/internal/ilink.h"
27 #include "l2cap/internal/parameter_provider.h"
28 #include "l2cap/le/dynamic_channel_manager.h"
29 #include "l2cap/le/internal/dynamic_channel_service_manager_impl.h"
30 #include "l2cap/le/internal/fixed_channel_impl.h"
31 #include "l2cap/le/internal/fixed_channel_service_manager_impl.h"
32 #include "l2cap/le/internal/signalling_manager.h"
33 #include "os/alarm.h"
34 
35 namespace bluetooth {
36 namespace l2cap {
37 namespace le {
38 namespace internal {
39 
40 class Link : public l2cap::internal::ILink {
41  public:
42   Link(os::Handler* l2cap_handler, std::unique_ptr<hci::AclConnection> acl_connection,
43        l2cap::internal::ParameterProvider* parameter_provider,
44        DynamicChannelServiceManagerImpl* dynamic_service_manager,
45        FixedChannelServiceManagerImpl* fixed_service_manager);
46 
47   ~Link() override = default;
48 
GetDevice()49   inline hci::AddressWithType GetDevice() override {
50     return {acl_connection_->GetAddress(), acl_connection_->GetAddressType()};
51   }
52 
53   struct PendingDynamicChannelConnection {
54     os::Handler* handler_;
55     DynamicChannelManager::OnConnectionOpenCallback on_open_callback_;
56     DynamicChannelManager::OnConnectionFailureCallback on_fail_callback_;
57     le::DynamicChannelConfigurationOption configuration_;
58   };
59 
GetRole()60   inline virtual hci::Role GetRole() {
61     return acl_connection_->GetRole();
62   }
63 
GetAclConnection()64   inline virtual hci::AclConnection* GetAclConnection() {
65     return acl_connection_.get();
66   }
67 
68   // ACL methods
69 
70   virtual void OnAclDisconnected(hci::ErrorCode status);
71 
72   virtual void Disconnect();
73 
74   // Handles connection parameter update request from remote
75   virtual void UpdateConnectionParameter(SignalId signal_id, uint16_t conn_interval_min, uint16_t conn_interval_max,
76                                          uint16_t conn_latency, uint16_t supervision_timeout);
77 
78   // FixedChannel methods
79 
80   virtual std::shared_ptr<FixedChannelImpl> AllocateFixedChannel(Cid cid, SecurityPolicy security_policy);
81 
82   virtual bool IsFixedChannelAllocated(Cid cid);
83 
84   // DynamicChannel methods
85 
86   virtual Cid ReserveDynamicChannel();
87 
88   virtual void SendConnectionRequest(Psm psm, PendingDynamicChannelConnection pending_dynamic_channel_connection);
89 
90   void SendDisconnectionRequest(Cid local_cid, Cid remote_cid) override;
91 
92   // Invoked by signalling manager to indicate an outgoing connection request failed and link shall free resources
93   virtual void OnOutgoingConnectionRequestFail(Cid local_cid);
94 
95   virtual std::shared_ptr<l2cap::internal::DynamicChannelImpl> AllocateDynamicChannel(Psm psm, Cid remote_cid,
96                                                                                       SecurityPolicy security_policy);
97 
98   virtual std::shared_ptr<l2cap::internal::DynamicChannelImpl> AllocateReservedDynamicChannel(
99       Cid reserved_cid, Psm psm, Cid remote_cid, SecurityPolicy security_policy);
100 
101   virtual DynamicChannelConfigurationOption GetConfigurationForInitialConfiguration(Cid cid);
102 
103   virtual void FreeDynamicChannel(Cid cid);
104 
105   // Check how many channels are acquired or in use, if zero, start tear down timer, if non-zero, cancel tear down timer
106   virtual void RefreshRefCount();
107 
108   void NotifyChannelCreation(Cid cid, std::unique_ptr<DynamicChannel> user_channel);
109   void NotifyChannelFail(Cid cid);
110 
ToString()111   virtual std::string ToString() {
112     return GetDevice().ToString();
113   }
114 
115   virtual uint16_t GetMps() const;
116 
117   virtual uint16_t GetInitialCredit() const;
118 
119   void SendLeCredit(Cid local_cid, uint16_t credit) override;
120 
121  private:
122   os::Handler* l2cap_handler_;
123   l2cap::internal::FixedChannelAllocator<FixedChannelImpl, Link> fixed_channel_allocator_{this, l2cap_handler_};
124   l2cap::internal::DynamicChannelAllocator dynamic_channel_allocator_{this, l2cap_handler_};
125   std::unique_ptr<hci::AclConnection> acl_connection_;
126   l2cap::internal::DataPipelineManager data_pipeline_manager_;
127   l2cap::internal::ParameterProvider* parameter_provider_;
128   DynamicChannelServiceManagerImpl* dynamic_service_manager_;
129   LeSignallingManager signalling_manager_;
130   std::unordered_map<Cid, PendingDynamicChannelConnection> local_cid_to_pending_dynamic_channel_connection_map_;
131   os::Alarm link_idle_disconnect_alarm_{l2cap_handler_};
132   DISALLOW_COPY_AND_ASSIGN(Link);
133 
134   void on_connection_update_complete(SignalId signal_id, hci::ErrorCode error_code);
135 };
136 
137 }  // namespace internal
138 }  // namespace le
139 }  // namespace l2cap
140 }  // namespace bluetooth
141