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, AddressWithType own_address,
27                              AddressWithType resolved_address, Phy::Type phy_type,
28                              bluetooth::hci::Role role)
29     : address_(address),
30       own_address_(own_address),
31       resolved_address_(resolved_address),
32       type_(phy_type),
33       role_(role),
34       last_packet_timestamp_(std::chrono::steady_clock::now()),
35       timeout_(std::chrono::seconds(3)) {}
36 
Encrypt()37 void AclConnection::Encrypt() { encrypted_ = true; }
38 
IsEncrypted() const39 bool AclConnection::IsEncrypted() const { return encrypted_; }
40 
SetLinkPolicySettings(uint16_t settings)41 void AclConnection::SetLinkPolicySettings(uint16_t settings) { link_policy_settings_ = settings; }
42 
GetRole() const43 bluetooth::hci::Role AclConnection::GetRole() const { return role_; }
44 
SetRole(bluetooth::hci::Role role)45 void AclConnection::SetRole(bluetooth::hci::Role role) { role_ = role; }
46 
GetRssi() const47 int8_t AclConnection::GetRssi() const { return rssi_; }
48 
SetRssi(int8_t rssi)49 void AclConnection::SetRssi(int8_t rssi) { rssi_ = rssi; }
50 
ResetLinkTimer()51 void AclConnection::ResetLinkTimer() { last_packet_timestamp_ = std::chrono::steady_clock::now(); }
52 
TimeUntilNearExpiring() const53 std::chrono::steady_clock::duration AclConnection::TimeUntilNearExpiring() const {
54   return (last_packet_timestamp_ + timeout_ / 2) - std::chrono::steady_clock::now();
55 }
56 
IsNearExpiring() const57 bool AclConnection::IsNearExpiring() const {
58   return TimeUntilNearExpiring() < std::chrono::steady_clock::duration::zero();
59 }
60 
TimeUntilExpired() const61 std::chrono::steady_clock::duration AclConnection::TimeUntilExpired() const {
62   return (last_packet_timestamp_ + timeout_) - std::chrono::steady_clock::now();
63 }
64 
HasExpired() const65 bool AclConnection::HasExpired() const {
66   return TimeUntilExpired() < std::chrono::steady_clock::duration::zero();
67 }
68 
69 }  // namespace rootcanal
70