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/le_acl_connection.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 "l2cap/le/link_options.h"
34 #include "l2cap/le/security_enforcement_interface.h"
35 #include "os/alarm.h"
36 
37 namespace bluetooth {
38 namespace l2cap {
39 namespace le {
40 namespace internal {
41 
42 class LinkManager;
43 
44 class Link : public l2cap::internal::ILink, public hci::acl_manager::LeConnectionManagementCallbacks {
45  public:
46   Link(os::Handler* l2cap_handler, std::unique_ptr<hci::acl_manager::LeAclConnection> acl_connection,
47        l2cap::internal::ParameterProvider* parameter_provider,
48        DynamicChannelServiceManagerImpl* dynamic_service_manager, FixedChannelServiceManagerImpl* fixed_service_manager,
49        LinkManager* link_manager);
50 
51   ~Link() override = default;
52 
GetDevice()53   inline hci::AddressWithType GetDevice() const override {
54     return acl_connection_->GetRemoteAddress();
55   }
56 
57   struct PendingDynamicChannelConnection {
58     os::Handler* handler_;
59     DynamicChannelManager::OnConnectionOpenCallback on_open_callback_;
60     DynamicChannelManager::OnConnectionFailureCallback on_fail_callback_;
61     le::DynamicChannelConfigurationOption configuration_;
62   };
63 
GetRole()64   inline virtual hci::Role GetRole() {
65     return acl_connection_->GetRole();
66   }
67 
GetAclConnection()68   inline virtual hci::acl_manager::LeAclConnection* GetAclConnection() {
69     return acl_connection_.get();
70   }
71 
72   // ACL methods
73 
74   virtual void OnAclDisconnected(hci::ErrorCode reason);
75 
76   void OnDisconnection(hci::ErrorCode reason) override;
77 
78   void OnConnectionUpdate(uint16_t connection_interval, uint16_t connection_latency,
79                           uint16_t supervision_timeout) override;
80 
81   virtual void Disconnect();
82 
83   // Handles connection parameter update request from remote
84   virtual void UpdateConnectionParameterFromRemote(SignalId signal_id, uint16_t conn_interval_min,
85                                                    uint16_t conn_interval_max, uint16_t conn_latency,
86                                                    uint16_t supervision_timeout);
87   virtual bool CheckConnectionParameters(
88       uint16_t conn_interval_min, uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout);
89 
90   virtual void SendConnectionParameterUpdate(uint16_t conn_interval_min, uint16_t conn_interval_max,
91                                              uint16_t conn_latency, uint16_t supervision_timeout,
92                                              uint16_t min_ce_length, uint16_t max_ce_length);
93 
94   // FixedChannel methods
95 
96   virtual std::shared_ptr<FixedChannelImpl> AllocateFixedChannel(Cid cid, SecurityPolicy security_policy);
97 
98   virtual bool IsFixedChannelAllocated(Cid cid);
99 
100   // DynamicChannel methods
101 
102   virtual Cid ReserveDynamicChannel();
103 
104   virtual void SendConnectionRequest(Psm psm, PendingDynamicChannelConnection pending_dynamic_channel_connection);
105 
106   void SendDisconnectionRequest(Cid local_cid, Cid remote_cid) override;
107 
108   // Invoked by signalling manager to indicate an outgoing connection request failed and link shall free resources
109   virtual void OnOutgoingConnectionRequestFail(Cid local_cid, LeCreditBasedConnectionResponseResult result);
110 
111   virtual std::shared_ptr<l2cap::internal::DynamicChannelImpl> AllocateDynamicChannel(Psm psm, Cid remote_cid);
112 
113   virtual std::shared_ptr<l2cap::internal::DynamicChannelImpl> AllocateReservedDynamicChannel(Cid reserved_cid, Psm psm,
114                                                                                               Cid remote_cid);
115 
116   virtual void FreeDynamicChannel(Cid cid);
117 
118   // Check how many channels are acquired or in use, if zero, start tear down timer, if non-zero, cancel tear down timer
119   virtual void RefreshRefCount();
120 
121   void NotifyChannelCreation(Cid cid, std::unique_ptr<DynamicChannel> user_channel);
122   void NotifyChannelFail(Cid cid, DynamicChannelManager::ConnectionResult result);
123 
ToString()124   virtual std::string ToString() {
125     return GetDevice().ToString();
126   }
127 
128   virtual uint16_t GetMps() const;
129 
130   virtual uint16_t GetInitialCredit() const;
131 
132   void SendLeCredit(Cid local_cid, uint16_t credit) override;
133 
GetLinkOptions()134   LinkOptions* GetLinkOptions() {
135     return &link_options_;
136   }
137 
138  private:
139   os::Handler* l2cap_handler_;
140   l2cap::internal::FixedChannelAllocator<FixedChannelImpl, Link> fixed_channel_allocator_{this, l2cap_handler_};
141   l2cap::internal::DynamicChannelAllocator dynamic_channel_allocator_{this, l2cap_handler_};
142   std::unique_ptr<hci::acl_manager::LeAclConnection> acl_connection_;
143   l2cap::internal::DataPipelineManager data_pipeline_manager_;
144   l2cap::internal::ParameterProvider* parameter_provider_;
145   DynamicChannelServiceManagerImpl* dynamic_service_manager_;
146   LeSignallingManager signalling_manager_;
147   std::unordered_map<Cid, PendingDynamicChannelConnection> local_cid_to_pending_dynamic_channel_connection_map_;
148   os::Alarm link_idle_disconnect_alarm_{l2cap_handler_};
149   LinkOptions link_options_{acl_connection_.get(), this, l2cap_handler_};
150   LinkManager* link_manager_;
151   SignalId update_request_signal_id_ = kInvalidSignalId;
152   uint16_t update_request_interval_min_;
153   uint16_t update_request_interval_max_;
154   uint16_t update_request_latency_;
155   uint16_t update_request_supervision_timeout_;
156   DISALLOW_COPY_AND_ASSIGN(Link);
157 
158   // Received connection update complete from ACL manager. SignalId is bound to a valid number when we need to send a
159   // response to remote. If SignalId is bound to an invalid number, we don't send a response to remote, because the
160   // connection update request is not from remote LL slave.
161   void on_connection_update_complete(SignalId signal_id, hci::ErrorCode error_code);
162 };
163 
164 }  // namespace internal
165 }  // namespace le
166 }  // namespace l2cap
167 }  // namespace bluetooth
168