• 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 #define LOG_TAG "acl_connection_handler"
18 
19 #include "acl_connection_handler.h"
20 
21 #include "base/logging.h"
22 
23 #include "osi/include/log.h"
24 #include "types/address.h"
25 
26 using std::shared_ptr;
27 
28 namespace test_vendor_lib {
29 
HasHandle(uint16_t handle) const30 bool AclConnectionHandler::HasHandle(uint16_t handle) const {
31   if (acl_connections_.count(handle) == 0) {
32     return false;
33   }
34   return true;
35 }
36 
GetUnusedHandle()37 uint16_t AclConnectionHandler::GetUnusedHandle() {
38   static uint16_t sNextHandle = acl::kReservedHandle - 2;
39   while (acl_connections_.count(sNextHandle) == 1) {
40     sNextHandle = (sNextHandle + 1) % acl::kReservedHandle;
41   }
42   uint16_t unused_handle = sNextHandle;
43   sNextHandle = (sNextHandle + 1) % acl::kReservedHandle;
44   return unused_handle;
45 }
46 
CreatePendingConnection(const Address & addr)47 bool AclConnectionHandler::CreatePendingConnection(const Address& addr) {
48   if ((pending_connections_.size() + 1 > max_pending_connections_) || HasPendingConnection(addr)) {
49     return false;
50   }
51   pending_connections_.insert(addr);
52   return true;
53 }
54 
HasPendingConnection(const Address & addr)55 bool AclConnectionHandler::HasPendingConnection(const Address& addr) {
56   return pending_connections_.count(addr) == 1;
57 }
58 
CancelPendingConnection(const Address & addr)59 bool AclConnectionHandler::CancelPendingConnection(const Address& addr) {
60   if (!HasPendingConnection(addr)) {
61     return false;
62   }
63   pending_connections_.erase(addr);
64   return true;
65 }
66 
CreateConnection(const Address & addr)67 uint16_t AclConnectionHandler::CreateConnection(const Address& addr) {
68   if (CancelPendingConnection(addr)) {
69     uint16_t handle = GetUnusedHandle();
70     acl_connections_.emplace(handle, addr);
71     SetConnected(handle, true);
72     return handle;
73   }
74   return acl::kReservedHandle;
75 }
76 
Disconnect(uint16_t handle)77 bool AclConnectionHandler::Disconnect(uint16_t handle) {
78   return acl_connections_.erase(handle) > 0;
79 }
80 
GetHandle(const Address & addr) const81 uint16_t AclConnectionHandler::GetHandle(const Address& addr) const {
82   for (auto pair : acl_connections_) {
83     if (std::get<AclConnection>(pair).GetAddress() == addr) {
84       return std::get<0>(pair);
85     }
86   }
87   return acl::kReservedHandle;
88 }
89 
GetAddress(uint16_t handle) const90 const Address& AclConnectionHandler::GetAddress(uint16_t handle) const {
91   CHECK(HasHandle(handle)) << "Handle unknown " << handle;
92   return acl_connections_.at(handle).GetAddress();
93 }
94 
SetConnected(uint16_t handle,bool connected)95 void AclConnectionHandler::SetConnected(uint16_t handle, bool connected) {
96   if (!HasHandle(handle)) {
97     return;
98   }
99   acl_connections_.at(handle).SetConnected(connected);
100 }
101 
IsConnected(uint16_t handle) const102 bool AclConnectionHandler::IsConnected(uint16_t handle) const {
103   if (!HasHandle(handle)) {
104     return false;
105   }
106   return acl_connections_.at(handle).IsConnected();
107 }
108 
Encrypt(uint16_t handle)109 void AclConnectionHandler::Encrypt(uint16_t handle) {
110   if (!HasHandle(handle)) {
111     return;
112   }
113   acl_connections_.at(handle).Encrypt();
114 }
115 
IsEncrypted(uint16_t handle) const116 bool AclConnectionHandler::IsEncrypted(uint16_t handle) const {
117   if (!HasHandle(handle)) {
118     return false;
119   }
120   return acl_connections_.at(handle).IsEncrypted();
121 }
122 
SetAddress(uint16_t handle,const Address & address)123 void AclConnectionHandler::SetAddress(uint16_t handle, const Address& address) {
124   if (!HasHandle(handle)) {
125     return;
126   }
127   acl_connections_.at(handle).SetAddress(address);
128 }
129 
130 }  // namespace test_vendor_lib
131