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 #pragma once
18 
19 #include <chrono>
20 #include <cstdint>
21 
22 #include "hci/address_with_type.h"
23 #include "packets/hci_packets.h"
24 #include "phy.h"
25 
26 namespace rootcanal {
27 
28 using ::bluetooth::hci::AddressWithType;
29 
30 enum AclConnectionState {
31   kActiveMode,
32   kHoldMode,
33   kSniffMode,
34 };
35 
36 // Model the connection of a device to the controller.
37 class AclConnection {
38 public:
39   AclConnection(AddressWithType address, AddressWithType own_address,
40                 AddressWithType resolved_address, Phy::Type phy_type, bluetooth::hci::Role role);
41 
42   virtual ~AclConnection() = default;
43 
GetPhyType()44   Phy::Type GetPhyType() const { return type_; }
45 
GetAddress()46   AddressWithType GetAddress() const { return address_; }
GetOwnAddress()47   AddressWithType GetOwnAddress() const { return own_address_; }
GetResolvedAddress()48   AddressWithType GetResolvedAddress() const { return resolved_address_; }
49 
50   void Encrypt();
51   bool IsEncrypted() const;
52 
53   void SetLinkPolicySettings(uint16_t settings);
GetLinkPolicySettings()54   uint16_t GetLinkPolicySettings() const { return link_policy_settings_; }
IsRoleSwitchEnabled()55   bool IsRoleSwitchEnabled() const { return (link_policy_settings_ & 0x1) != 0; }
IsHoldModeEnabled()56   bool IsHoldModeEnabled() const { return (link_policy_settings_ & 0x2) != 0; }
IsSniffModeEnabled()57   bool IsSniffModeEnabled() const { return (link_policy_settings_ & 0x4) != 0; }
58 
GetMode()59   AclConnectionState GetMode() const { return state_; }
60 
61   bluetooth::hci::Role GetRole() const;
62   void SetRole(bluetooth::hci::Role role);
63 
64   int8_t GetRssi() const;
65   void SetRssi(int8_t rssi);
66 
67   std::chrono::steady_clock::duration TimeUntilNearExpiring() const;
68   std::chrono::steady_clock::duration TimeUntilExpired() const;
69   void ResetLinkTimer();
70   bool IsNearExpiring() const;
71   bool HasExpired() const;
72 
73   // LE-ACL state.
InitiatePhyUpdate()74   void InitiatePhyUpdate() { initiated_phy_update_ = true; }
PhyUpdateComplete()75   void PhyUpdateComplete() { initiated_phy_update_ = false; }
InitiatedPhyUpdate()76   bool InitiatedPhyUpdate() const { return initiated_phy_update_; }
GetTxPhy()77   bluetooth::hci::PhyType GetTxPhy() const { return tx_phy_; }
GetRxPhy()78   bluetooth::hci::PhyType GetRxPhy() const { return rx_phy_; }
SetTxPhy(bluetooth::hci::PhyType phy)79   void SetTxPhy(bluetooth::hci::PhyType phy) { tx_phy_ = phy; }
SetRxPhy(bluetooth::hci::PhyType phy)80   void SetRxPhy(bluetooth::hci::PhyType phy) { rx_phy_ = phy; }
81 
82 private:
83   AddressWithType address_;
84   AddressWithType own_address_;
85   AddressWithType resolved_address_;
86   Phy::Type type_{Phy::Type::BR_EDR};
87 
88   // Reports the RSSI measured for the last packet received on
89   // this connection.
90   int8_t rssi_{0};
91 
92   // State variables
93   bool encrypted_{false};
94   uint16_t link_policy_settings_{0};
95   AclConnectionState state_{kActiveMode};
96   bluetooth::hci::Role role_{bluetooth::hci::Role::CENTRAL};
97   std::chrono::steady_clock::time_point last_packet_timestamp_;
98   std::chrono::steady_clock::duration timeout_;
99 
100   // LE-ACL state.
101   bluetooth::hci::PhyType tx_phy_{bluetooth::hci::PhyType::LE_1M};
102   bluetooth::hci::PhyType rx_phy_{bluetooth::hci::PhyType::LE_1M};
103   bool initiated_phy_update_{false};
104 };
105 
106 }  // namespace rootcanal
107