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