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
GetLinkPolicySettings() const36 uint16_t AclConnection::GetLinkPolicySettings() const {
37 return link_policy_settings_;
38 };
39
SetLinkPolicySettings(uint16_t settings)40 void AclConnection::SetLinkPolicySettings(uint16_t settings) {
41 link_policy_settings_ = settings;
42 }
43
GetRole() const44 bluetooth::hci::Role AclConnection::GetRole() const { return role_; };
45
SetRole(bluetooth::hci::Role role)46 void AclConnection::SetRole(bluetooth::hci::Role role) { role_ = role; }
47
GetRssi() const48 int8_t AclConnection::GetRssi() const { return rssi_; }
49
SetRssi(int8_t rssi)50 void AclConnection::SetRssi(int8_t rssi) { rssi_ = rssi; }
51
ResetLinkTimer()52 void AclConnection::ResetLinkTimer() {
53 last_packet_timestamp_ = std::chrono::steady_clock::now();
54 }
55
TimeUntilNearExpiring() const56 std::chrono::steady_clock::duration AclConnection::TimeUntilNearExpiring()
57 const {
58 return (last_packet_timestamp_ + timeout_ / 2) -
59 std::chrono::steady_clock::now();
60 }
61
IsNearExpiring() const62 bool AclConnection::IsNearExpiring() const {
63 return TimeUntilNearExpiring() < std::chrono::steady_clock::duration::zero();
64 }
65
TimeUntilExpired() const66 std::chrono::steady_clock::duration AclConnection::TimeUntilExpired() const {
67 return (last_packet_timestamp_ + timeout_) - std::chrono::steady_clock::now();
68 }
69
HasExpired() const70 bool AclConnection::HasExpired() const {
71 return TimeUntilExpired() < std::chrono::steady_clock::duration::zero();
72 }
73
74 } // namespace rootcanal
75