• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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 <cstdint>
21 #include <set>
22 #include <unordered_map>
23 
24 #include "acl_connection.h"
25 #include "hci/address.h"
26 #include "hci/address_with_type.h"
27 #include "isochronous_connection_handler.h"
28 #include "model/setup/async_manager.h"
29 #include "phy.h"
30 #include "sco_connection.h"
31 
32 namespace rootcanal {
33 static constexpr uint16_t kReservedHandle = 0xF00;
34 
35 class AclConnectionHandler {
36  public:
37   AclConnectionHandler() = default;
38 
39   virtual ~AclConnectionHandler() = default;
40 
41   void RegisterTaskScheduler(
42       std::function<AsyncTaskId(std::chrono::milliseconds, const TaskCallback&)>
43           event_scheduler);
44 
45   bool CreatePendingConnection(bluetooth::hci::Address addr,
46                                bool authenticate_on_connect);
47   bool HasPendingConnection(bluetooth::hci::Address addr) const;
48   bool CancelPendingConnection(bluetooth::hci::Address addr);
49   bool AuthenticatePendingConnection() const;
50 
51   bool HasPendingScoConnection(bluetooth::hci::Address addr) const;
52   ScoState GetScoConnectionState(bluetooth::hci::Address addr) const;
53   bool IsLegacyScoConnection(bluetooth::hci::Address addr) const;
54   void CreateScoConnection(bluetooth::hci::Address addr,
55                            ScoConnectionParameters const& parameters,
56                            ScoState state, ScoDatapath datapath,
57                            bool legacy = false);
58   void CancelPendingScoConnection(bluetooth::hci::Address addr);
59   bool AcceptPendingScoConnection(bluetooth::hci::Address addr,
60                                   ScoLinkParameters const& parameters,
61                                   std::function<AsyncTaskId()> startStream);
62   bool AcceptPendingScoConnection(bluetooth::hci::Address addr,
63                                   ScoConnectionParameters const& parameters,
64                                   std::function<AsyncTaskId()> startStream);
65   uint16_t GetScoHandle(bluetooth::hci::Address addr) const;
66   ScoConnectionParameters GetScoConnectionParameters(
67       bluetooth::hci::Address addr) const;
68   ScoLinkParameters GetScoLinkParameters(bluetooth::hci::Address addr) const;
69 
70   bool CreatePendingLeConnection(bluetooth::hci::AddressWithType peer,
71                                  bluetooth::hci::AddressWithType resolved_peer,
72                                  bluetooth::hci::AddressWithType local_address);
73   bool HasPendingLeConnection(bluetooth::hci::AddressWithType addr) const;
74   bool CancelPendingLeConnection(bluetooth::hci::AddressWithType addr);
75 
76   uint16_t CreateConnection(bluetooth::hci::Address addr,
77                             bluetooth::hci::Address own_addr);
78   uint16_t CreateLeConnection(bluetooth::hci::AddressWithType addr,
79                               bluetooth::hci::AddressWithType own_addr,
80                               bluetooth::hci::Role role);
81   bool Disconnect(uint16_t handle, std::function<void(AsyncTaskId)> stopStream);
82   bool HasHandle(uint16_t handle) const;
83   bool HasScoHandle(uint16_t handle) const;
84 
85   uint16_t GetHandle(bluetooth::hci::AddressWithType addr) const;
86   uint16_t GetHandleOnlyAddress(bluetooth::hci::Address addr) const;
87   bluetooth::hci::AddressWithType GetAddress(uint16_t handle) const;
88   bluetooth::hci::Address GetScoAddress(uint16_t handle) const;
89   bluetooth::hci::AddressWithType GetOwnAddress(uint16_t handle) const;
90   bluetooth::hci::AddressWithType GetResolvedAddress(uint16_t handle) const;
91 
92   void Encrypt(uint16_t handle);
93   bool IsEncrypted(uint16_t handle) const;
94 
95   Phy::Type GetPhyType(uint16_t handle) const;
96 
97   uint16_t GetAclLinkPolicySettings(uint16_t handle) const;
98   void SetAclLinkPolicySettings(uint16_t handle, uint16_t settings);
99 
100   bluetooth::hci::Role GetAclRole(uint16_t handle) const;
101   void SetAclRole(uint16_t handle, bluetooth::hci::Role role);
102 
103   std::unique_ptr<bluetooth::hci::LeSetCigParametersCompleteBuilder>
104   SetCigParameters(uint8_t id, uint32_t sdu_interval_m_to_s,
105                    uint32_t sdu_interval_s_to_m,
106                    bluetooth::hci::ClockAccuracy accuracy,
107                    bluetooth::hci::Packing packing,
108                    bluetooth::hci::Enable framing,
109                    uint16_t max_transport_latency_m_to_s_,
110                    uint16_t max_transport_latency_s_to_m_,
111                    std::vector<bluetooth::hci::CisParametersConfig>& streams);
112 
113   void CreatePendingCis(bluetooth::hci::CreateCisConfig config);
114 
115   bool ConnectCis(uint16_t handle);
116 
117   void SetRemoteCisHandle(uint16_t handle, uint16_t remote_handle);
118 
119   uint16_t GetPendingAclHandle(uint16_t cis_handle) const;
120 
121   bool RejectCis(uint16_t handle);
122 
123   bool DisconnectCis(uint16_t handle);
124 
125   bluetooth::hci::ErrorCode RemoveCig(uint8_t cig_id);
126 
127   bool HasPendingCis() const;
128 
129   bool HasPendingCisConnection(uint16_t handle) const;
130 
131   bool HasCisHandle(uint16_t handle) const;
132 
133   bool HasConnectedCis(uint16_t handle) const;
134 
135   uint16_t GetAclHandleForCisHandle(uint16_t cis_handle) const;
136   uint16_t GetRemoteCisHandleForCisHandle(uint16_t cis_handle) const;
137 
138   StreamParameters GetStreamParameters(uint16_t handle) const;
139   GroupParameters GetGroupParameters(uint8_t id) const;
140 
141   std::vector<uint16_t> GetAclHandles() const;
142 
143   void ResetLinkTimer(uint16_t handle);
144   std::chrono::steady_clock::duration TimeUntilLinkNearExpiring(
145       uint16_t handle) const;
146   bool IsLinkNearExpiring(uint16_t handle) const;
147   std::chrono::steady_clock::duration TimeUntilLinkExpired(
148       uint16_t handle) const;
149   bool HasLinkExpired(uint16_t handle) const;
150 
151  private:
152   std::unordered_map<uint16_t, AclConnection> acl_connections_;
153   std::unordered_map<uint16_t, ScoConnection> sco_connections_;
154 
155   std::function<AsyncTaskId(std::chrono::milliseconds, const TaskCallback&)>
156       schedule_task_;
157 
158   bool classic_connection_pending_{false};
159   bluetooth::hci::Address pending_connection_address_{
160       bluetooth::hci::Address::kEmpty};
161   bool authenticate_pending_classic_connection_{false};
162   bool le_connection_pending_{false};
163   bluetooth::hci::AddressWithType pending_le_connection_address_{
164       bluetooth::hci::Address::kEmpty,
165       bluetooth::hci::AddressType::PUBLIC_DEVICE_ADDRESS};
166   bluetooth::hci::AddressWithType pending_le_connection_own_address_{
167       bluetooth::hci::Address::kEmpty,
168       bluetooth::hci::AddressType::PUBLIC_DEVICE_ADDRESS};
169   bluetooth::hci::AddressWithType pending_le_connection_resolved_address_{
170       bluetooth::hci::Address::kEmpty,
171       bluetooth::hci::AddressType::PUBLIC_DEVICE_ADDRESS};
172 
173   uint16_t GetUnusedHandle();
174   uint16_t last_handle_{kReservedHandle - 2};
175   IsochronousConnectionHandler isochronous_connection_handler_;
176   struct CisHandles {
177     uint16_t acl_handle_ = kReservedHandle;
178     uint16_t cis_handle_ = kReservedHandle;
179     uint16_t remote_cis_handle_ = kReservedHandle;
180   };
181   std::vector<CisHandles> connected_streams_;
182   std::vector<CisHandles> pending_streams_;
183 };
184 
185 }  // namespace rootcanal
186