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