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.h"
18
19 namespace rootcanal {
AclConnection(AddressWithType address,AddressWithType own_address,AddressWithType resolved_address,Phy::Type phy_type,bluetooth::hci::Role role)20 AclConnection::AclConnection(AddressWithType address,
21 AddressWithType own_address,
22 AddressWithType resolved_address,
23 Phy::Type phy_type, bluetooth::hci::Role role)
24 : address_(address),
25 own_address_(own_address),
26 resolved_address_(resolved_address),
27 type_(phy_type),
28 role_(role),
29 last_packet_timestamp_(std::chrono::steady_clock::now()),
30 timeout_(std::chrono::seconds(1)) {}
31
Encrypt()32 void AclConnection::Encrypt() { encrypted_ = true; };
33
IsEncrypted() const34 bool AclConnection::IsEncrypted() const { return encrypted_; };
35
GetAddress() const36 AddressWithType AclConnection::GetAddress() const { return address_; }
37
SetAddress(AddressWithType address)38 void AclConnection::SetAddress(AddressWithType address) { address_ = address; }
39
GetOwnAddress() const40 AddressWithType AclConnection::GetOwnAddress() const { return own_address_; }
41
GetResolvedAddress() const42 AddressWithType AclConnection::GetResolvedAddress() const {
43 return resolved_address_;
44 }
45
SetOwnAddress(AddressWithType address)46 void AclConnection::SetOwnAddress(AddressWithType address) {
47 own_address_ = address;
48 }
49
GetPhyType() const50 Phy::Type AclConnection::GetPhyType() const { return type_; }
51
GetLinkPolicySettings() const52 uint16_t AclConnection::GetLinkPolicySettings() const {
53 return link_policy_settings_;
54 };
55
SetLinkPolicySettings(uint16_t settings)56 void AclConnection::SetLinkPolicySettings(uint16_t settings) {
57 link_policy_settings_ = settings;
58 }
59
GetRole() const60 bluetooth::hci::Role AclConnection::GetRole() const { return role_; };
61
SetRole(bluetooth::hci::Role role)62 void AclConnection::SetRole(bluetooth::hci::Role role) { role_ = role; }
63
ResetLinkTimer()64 void AclConnection::ResetLinkTimer() {
65 last_packet_timestamp_ = std::chrono::steady_clock::now();
66 }
67
TimeUntilNearExpiring() const68 std::chrono::steady_clock::duration AclConnection::TimeUntilNearExpiring()
69 const {
70 return (last_packet_timestamp_ + timeout_ / 2) -
71 std::chrono::steady_clock::now();
72 }
73
IsNearExpiring() const74 bool AclConnection::IsNearExpiring() const {
75 return TimeUntilNearExpiring() < std::chrono::steady_clock::duration::zero();
76 }
77
TimeUntilExpired() const78 std::chrono::steady_clock::duration AclConnection::TimeUntilExpired() const {
79 return (last_packet_timestamp_ + timeout_) - std::chrono::steady_clock::now();
80 }
81
HasExpired() const82 bool AclConnection::HasExpired() const {
83 return TimeUntilExpired() < std::chrono::steady_clock::duration::zero();
84 }
85
86 } // namespace rootcanal
87