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 "model/controller/acl_connection.h"
18 
19 #include <chrono>
20 #include <cstdint>
21 
22 #include "packets/hci_packets.h"
23 #include "phy.h"
24 
25 namespace rootcanal {
AclConnection(AddressWithType address,AddressWithType own_address,AddressWithType resolved_address,Phy::Type phy_type,bluetooth::hci::Role role)26 AclConnection::AclConnection(AddressWithType address,
27                              AddressWithType own_address,
28                              AddressWithType resolved_address,
29                              Phy::Type phy_type, bluetooth::hci::Role role)
30     : address_(address),
31       own_address_(own_address),
32       resolved_address_(resolved_address),
33       type_(phy_type),
34       role_(role),
35       last_packet_timestamp_(std::chrono::steady_clock::now()),
36       timeout_(std::chrono::seconds(3)) {}
37 
Encrypt()38 void AclConnection::Encrypt() { encrypted_ = true; }
39 
IsEncrypted() const40 bool AclConnection::IsEncrypted() const { return encrypted_; }
41 
SetLinkPolicySettings(uint16_t settings)42 void AclConnection::SetLinkPolicySettings(uint16_t settings) {
43   link_policy_settings_ = settings;
44 }
45 
GetRole() const46 bluetooth::hci::Role AclConnection::GetRole() const { return role_; }
47 
SetRole(bluetooth::hci::Role role)48 void AclConnection::SetRole(bluetooth::hci::Role role) { role_ = role; }
49 
GetRssi() const50 int8_t AclConnection::GetRssi() const { return rssi_; }
51 
SetRssi(int8_t rssi)52 void AclConnection::SetRssi(int8_t rssi) { rssi_ = rssi; }
53 
ResetLinkTimer()54 void AclConnection::ResetLinkTimer() {
55   last_packet_timestamp_ = std::chrono::steady_clock::now();
56 }
57 
TimeUntilNearExpiring() const58 std::chrono::steady_clock::duration AclConnection::TimeUntilNearExpiring()
59     const {
60   return (last_packet_timestamp_ + timeout_ / 2) -
61          std::chrono::steady_clock::now();
62 }
63 
IsNearExpiring() const64 bool AclConnection::IsNearExpiring() const {
65   return TimeUntilNearExpiring() < std::chrono::steady_clock::duration::zero();
66 }
67 
TimeUntilExpired() const68 std::chrono::steady_clock::duration AclConnection::TimeUntilExpired() const {
69   return (last_packet_timestamp_ + timeout_) - std::chrono::steady_clock::now();
70 }
71 
HasExpired() const72 bool AclConnection::HasExpired() const {
73   return TimeUntilExpired() < std::chrono::steady_clock::duration::zero();
74 }
75 
76 }  // namespace rootcanal
77