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 #include "acl_connection_handler.h"
18 
19 #include "os/log.h"
20 
21 #include "hci/address.h"
22 
23 using std::shared_ptr;
24 
25 namespace test_vendor_lib {
26 
27 using ::bluetooth::hci::Address;
28 using ::bluetooth::hci::AddressType;
29 using ::bluetooth::hci::AddressWithType;
30 
HasHandle(uint16_t handle) const31 bool AclConnectionHandler::HasHandle(uint16_t handle) const {
32   if (acl_connections_.count(handle) == 0) {
33     return false;
34   }
35   return true;
36 }
37 
GetUnusedHandle()38 uint16_t AclConnectionHandler::GetUnusedHandle() {
39   while (acl_connections_.count(last_handle_) == 1) {
40     last_handle_ = (last_handle_ + 1) % acl::kReservedHandle;
41   }
42   uint16_t unused_handle = last_handle_;
43   last_handle_ = (last_handle_ + 1) % acl::kReservedHandle;
44   return unused_handle;
45 }
46 
CreatePendingConnection(Address addr,bool authenticate_on_connect)47 bool AclConnectionHandler::CreatePendingConnection(
48     Address addr, bool authenticate_on_connect) {
49   if (classic_connection_pending_) {
50     return false;
51   }
52   classic_connection_pending_ = true;
53   pending_connection_address_ = addr;
54   authenticate_pending_classic_connection_ = authenticate_on_connect;
55   return true;
56 }
57 
HasPendingConnection(Address addr) const58 bool AclConnectionHandler::HasPendingConnection(Address addr) const {
59   return classic_connection_pending_ && pending_connection_address_ == addr;
60 }
61 
AuthenticatePendingConnection() const62 bool AclConnectionHandler::AuthenticatePendingConnection() const {
63   return authenticate_pending_classic_connection_;
64 }
65 
CancelPendingConnection(Address addr)66 bool AclConnectionHandler::CancelPendingConnection(Address addr) {
67   if (!classic_connection_pending_ || pending_connection_address_ != addr) {
68     return false;
69   }
70   classic_connection_pending_ = false;
71   pending_connection_address_ = Address::kEmpty;
72   return true;
73 }
74 
CreatePendingLeConnection(AddressWithType addr)75 bool AclConnectionHandler::CreatePendingLeConnection(AddressWithType addr) {
76   bool device_connected = false;
77   for (auto pair : acl_connections_) {
78     auto connection = std::get<AclConnection>(pair);
79     if (connection.GetAddress() == addr) {
80       device_connected = true;
81     }
82   }
83   if (device_connected) {
84     LOG_INFO("%s: %s is already connected", __func__, addr.ToString().c_str());
85     return false;
86   }
87   if (le_connection_pending_) {
88     LOG_INFO("%s: connection already pending", __func__);
89     return false;
90   }
91   le_connection_pending_ = true;
92   pending_le_connection_address_ = addr;
93   return true;
94 }
95 
HasPendingLeConnection(AddressWithType addr) const96 bool AclConnectionHandler::HasPendingLeConnection(AddressWithType addr) const {
97   return le_connection_pending_ && pending_le_connection_address_ == addr;
98 }
99 
CancelPendingLeConnection(AddressWithType addr)100 bool AclConnectionHandler::CancelPendingLeConnection(AddressWithType addr) {
101   if (!le_connection_pending_ || pending_le_connection_address_ != addr) {
102     return false;
103   }
104   le_connection_pending_ = false;
105   pending_le_connection_address_ =
106       AddressWithType{Address::kEmpty, AddressType::PUBLIC_DEVICE_ADDRESS};
107   return true;
108 }
109 
CreateConnection(Address addr,Address own_addr)110 uint16_t AclConnectionHandler::CreateConnection(Address addr,
111                                                 Address own_addr) {
112   if (CancelPendingConnection(addr)) {
113     uint16_t handle = GetUnusedHandle();
114     acl_connections_.emplace(
115         handle,
116         AclConnection{
117             AddressWithType{addr, AddressType::PUBLIC_DEVICE_ADDRESS},
118             AddressWithType{own_addr, AddressType::PUBLIC_DEVICE_ADDRESS},
119             Phy::Type::BR_EDR});
120     return handle;
121   }
122   return acl::kReservedHandle;
123 }
124 
CreateLeConnection(AddressWithType addr,AddressWithType own_addr)125 uint16_t AclConnectionHandler::CreateLeConnection(AddressWithType addr,
126                                                   AddressWithType own_addr) {
127   if (CancelPendingLeConnection(addr)) {
128     uint16_t handle = GetUnusedHandle();
129     acl_connections_.emplace(
130         handle, AclConnection{addr, own_addr, Phy::Type::LOW_ENERGY});
131     return handle;
132   }
133   return acl::kReservedHandle;
134 }
135 
Disconnect(uint16_t handle)136 bool AclConnectionHandler::Disconnect(uint16_t handle) {
137   return acl_connections_.erase(handle) > 0;
138 }
139 
GetHandle(AddressWithType addr) const140 uint16_t AclConnectionHandler::GetHandle(AddressWithType addr) const {
141   for (auto pair : acl_connections_) {
142     if (std::get<AclConnection>(pair).GetAddress() == addr) {
143       return std::get<0>(pair);
144     }
145   }
146   return acl::kReservedHandle;
147 }
148 
GetHandleOnlyAddress(bluetooth::hci::Address addr) const149 uint16_t AclConnectionHandler::GetHandleOnlyAddress(
150     bluetooth::hci::Address addr) const {
151   for (auto pair : acl_connections_) {
152     if (std::get<AclConnection>(pair).GetAddress().GetAddress() == addr) {
153       return std::get<0>(pair);
154     }
155   }
156   return acl::kReservedHandle;
157 }
158 
GetAddress(uint16_t handle) const159 AddressWithType AclConnectionHandler::GetAddress(uint16_t handle) const {
160   ASSERT_LOG(HasHandle(handle), "Handle unknown %hd", handle);
161   return acl_connections_.at(handle).GetAddress();
162 }
163 
GetOwnAddress(uint16_t handle) const164 AddressWithType AclConnectionHandler::GetOwnAddress(uint16_t handle) const {
165   ASSERT_LOG(HasHandle(handle), "Handle unknown %hd", handle);
166   return acl_connections_.at(handle).GetOwnAddress();
167 }
168 
Encrypt(uint16_t handle)169 void AclConnectionHandler::Encrypt(uint16_t handle) {
170   if (!HasHandle(handle)) {
171     return;
172   }
173   acl_connections_.at(handle).Encrypt();
174 }
175 
IsEncrypted(uint16_t handle) const176 bool AclConnectionHandler::IsEncrypted(uint16_t handle) const {
177   if (!HasHandle(handle)) {
178     return false;
179   }
180   return acl_connections_.at(handle).IsEncrypted();
181 }
182 
SetAddress(uint16_t handle,AddressWithType address)183 void AclConnectionHandler::SetAddress(uint16_t handle,
184                                       AddressWithType address) {
185   if (!HasHandle(handle)) {
186     return;
187   }
188   auto connection = acl_connections_.at(handle);
189   connection.SetAddress(address);
190 }
191 
GetPhyType(uint16_t handle) const192 Phy::Type AclConnectionHandler::GetPhyType(uint16_t handle) const {
193   if (!HasHandle(handle)) {
194     return Phy::Type::BR_EDR;
195   }
196   return acl_connections_.at(handle).GetPhyType();
197 }
198 
199 }  // namespace test_vendor_lib
200