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, 41 bluetooth::hci::Role role); 42 43 virtual ~AclConnection() = default; 44 GetPhyType()45 Phy::Type GetPhyType() const { return type_; } 46 GetAddress()47 AddressWithType GetAddress() const { return address_; } GetOwnAddress()48 AddressWithType GetOwnAddress() const { return own_address_; } GetResolvedAddress()49 AddressWithType GetResolvedAddress() const { return resolved_address_; } 50 51 void Encrypt(); 52 bool IsEncrypted() const; 53 54 void SetLinkPolicySettings(uint16_t settings); GetLinkPolicySettings()55 uint16_t GetLinkPolicySettings() const { return link_policy_settings_; } IsRoleSwitchEnabled()56 bool IsRoleSwitchEnabled() const { 57 return (link_policy_settings_ & 0x1) != 0; 58 } IsHoldModeEnabled()59 bool IsHoldModeEnabled() const { return (link_policy_settings_ & 0x2) != 0; } IsSniffModeEnabled()60 bool IsSniffModeEnabled() const { return (link_policy_settings_ & 0x4) != 0; } 61 GetMode()62 AclConnectionState GetMode() const { return state_; } 63 64 bluetooth::hci::Role GetRole() const; 65 void SetRole(bluetooth::hci::Role role); 66 67 int8_t GetRssi() const; 68 void SetRssi(int8_t rssi); 69 70 std::chrono::steady_clock::duration TimeUntilNearExpiring() const; 71 std::chrono::steady_clock::duration TimeUntilExpired() const; 72 void ResetLinkTimer(); 73 bool IsNearExpiring() const; 74 bool HasExpired() const; 75 76 // LE-ACL state. InitiatePhyUpdate()77 void InitiatePhyUpdate() { initiated_phy_update_ = true; } PhyUpdateComplete()78 void PhyUpdateComplete() { initiated_phy_update_ = false; } InitiatedPhyUpdate()79 bool InitiatedPhyUpdate() const { return initiated_phy_update_; } GetTxPhy()80 bluetooth::hci::PhyType GetTxPhy() const { return tx_phy_; } GetRxPhy()81 bluetooth::hci::PhyType GetRxPhy() const { return rx_phy_; } SetTxPhy(bluetooth::hci::PhyType phy)82 void SetTxPhy(bluetooth::hci::PhyType phy) { tx_phy_ = phy; } SetRxPhy(bluetooth::hci::PhyType phy)83 void SetRxPhy(bluetooth::hci::PhyType phy) { rx_phy_ = phy; } 84 85 private: 86 AddressWithType address_; 87 AddressWithType own_address_; 88 AddressWithType resolved_address_; 89 Phy::Type type_{Phy::Type::BR_EDR}; 90 91 // Reports the RSSI measured for the last packet received on 92 // this connection. 93 int8_t rssi_{0}; 94 95 // State variables 96 bool encrypted_{false}; 97 uint16_t link_policy_settings_{0}; 98 AclConnectionState state_{kActiveMode}; 99 bluetooth::hci::Role role_{bluetooth::hci::Role::CENTRAL}; 100 std::chrono::steady_clock::time_point last_packet_timestamp_; 101 std::chrono::steady_clock::duration timeout_; 102 103 // LE-ACL state. 104 bluetooth::hci::PhyType tx_phy_{bluetooth::hci::PhyType::LE_1M}; 105 bluetooth::hci::PhyType rx_phy_{bluetooth::hci::PhyType::LE_1M}; 106 bool initiated_phy_update_{false}; 107 }; 108 109 } // namespace rootcanal 110