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 <cstdint>
20 #include <set>
21 #include <unordered_map>
22 
23 #include "acl_connection.h"
24 #include "hci/address.h"
25 #include "hci/address_with_type.h"
26 #include "isochronous_connection_handler.h"
27 #include "phy.h"
28 
29 namespace test_vendor_lib {
30 static constexpr uint16_t kReservedHandle = 0xF00;
31 
32 class AclConnectionHandler {
33  public:
34   AclConnectionHandler() = default;
35 
36   virtual ~AclConnectionHandler() = default;
37 
38   bool CreatePendingConnection(bluetooth::hci::Address addr,
39                                bool authenticate_on_connect);
40   bool HasPendingConnection(bluetooth::hci::Address addr) const;
41   bool CancelPendingConnection(bluetooth::hci::Address addr);
42   bool AuthenticatePendingConnection() const;
43 
44   bool CreatePendingLeConnection(bluetooth::hci::AddressWithType addr);
45   bool HasPendingLeConnection(bluetooth::hci::AddressWithType addr) const;
46   bool CancelPendingLeConnection(bluetooth::hci::AddressWithType addr);
47 
48   uint16_t CreateConnection(bluetooth::hci::Address addr,
49                             bluetooth::hci::Address own_addr);
50   uint16_t CreateLeConnection(bluetooth::hci::AddressWithType addr,
51                               bluetooth::hci::AddressWithType own_addr);
52   bool Disconnect(uint16_t handle);
53   bool HasHandle(uint16_t handle) const;
54 
55   uint16_t GetHandle(bluetooth::hci::AddressWithType addr) const;
56   uint16_t GetHandleOnlyAddress(bluetooth::hci::Address addr) const;
57   bluetooth::hci::AddressWithType GetAddress(uint16_t handle) const;
58   bluetooth::hci::AddressWithType GetOwnAddress(uint16_t handle) const;
59 
60   void Encrypt(uint16_t handle);
61   bool IsEncrypted(uint16_t handle) const;
62 
63   void SetAddress(uint16_t handle, bluetooth::hci::AddressWithType address);
64 
65   Phy::Type GetPhyType(uint16_t handle) const;
66 
67   std::unique_ptr<bluetooth::hci::LeSetCigParametersCompleteBuilder>
68   SetCigParameters(uint8_t id, uint32_t sdu_interval_m_to_s,
69                    uint32_t sdu_interval_s_to_m,
70                    bluetooth::hci::ClockAccuracy accuracy,
71                    bluetooth::hci::Packing packing,
72                    bluetooth::hci::Enable framing,
73                    uint16_t max_transport_latency_m_to_s_,
74                    uint16_t max_transport_latency_s_to_m_,
75                    std::vector<bluetooth::hci::CisParametersConfig>& streams);
76 
77   void CreatePendingCis(bluetooth::hci::CreateCisConfig config);
78 
79   bool ConnectCis(uint16_t handle);
80 
81   void SetRemoteCisHandle(uint16_t handle, uint16_t remote_handle);
82 
83   uint16_t GetPendingAclHandle(uint16_t cis_handle) const;
84 
85   bool RejectCis(uint16_t handle);
86 
87   bool DisconnectCis(uint16_t handle);
88 
89   bluetooth::hci::ErrorCode RemoveCig(uint8_t cig_id);
90 
91   bool HasPendingCis() const;
92 
93   bool HasPendingCisConnection(uint16_t handle) const;
94 
95   bool HasCisHandle(uint16_t handle) const;
96 
97   bool HasConnectedCis(uint16_t handle) const;
98 
99   uint16_t GetAclHandleForCisHandle(uint16_t cis_handle) const;
100   uint16_t GetRemoteCisHandleForCisHandle(uint16_t cis_handle) const;
101 
102   StreamParameters GetStreamParameters(uint16_t handle) const;
103   GroupParameters GetGroupParameters(uint8_t id) const;
104 
105  private:
106   std::unordered_map<uint16_t, AclConnection> acl_connections_;
107   bool classic_connection_pending_{false};
108   bluetooth::hci::Address pending_connection_address_{
109       bluetooth::hci::Address::kEmpty};
110   bool authenticate_pending_classic_connection_{false};
111   bool le_connection_pending_{false};
112   bluetooth::hci::AddressWithType pending_le_connection_address_{
113       bluetooth::hci::Address::kEmpty,
114       bluetooth::hci::AddressType::PUBLIC_DEVICE_ADDRESS};
115 
116   uint16_t GetUnusedHandle();
117   uint16_t last_handle_{kReservedHandle - 2};
118   IsochronousConnectionHandler isochronous_connection_handler_;
119   struct CisHandles {
120     uint16_t acl_handle_ = kReservedHandle;
121     uint16_t cis_handle_ = kReservedHandle;
122     uint16_t remote_cis_handle_ = kReservedHandle;
123   };
124   std::vector<CisHandles> connected_streams_;
125   std::vector<CisHandles> pending_streams_;
126 };
127 
128 }  // namespace test_vendor_lib
129