Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4afc0833 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Rootcanal: Store address types in AclConnection"

parents 0173eb15 0bdfd495
Loading
Loading
Loading
Loading
+22 −1
Original line number Diff line number Diff line
@@ -18,4 +18,25 @@

using std::shared_ptr;

namespace test_vendor_lib {}  // namespace test_vendor_lib
namespace test_vendor_lib {
AclConnection::AclConnection(AddressWithType addr, AddressWithType own_addr,
                             Phy::Type phy_type)
    : address_(addr), own_address_(own_addr), type_(phy_type) {}

void AclConnection::Encrypt() { encrypted_ = true; };

bool AclConnection::IsEncrypted() const { return encrypted_; };

AddressWithType AclConnection::GetAddress() const { return address_; }

void AclConnection::SetAddress(AddressWithType address) { address_ = address; }

AddressWithType AclConnection::GetOwnAddress() const { return own_address_; }

void AclConnection::SetOwnAddress(AddressWithType own_addr) {
  own_address_ = own_addr;
}

Phy::Type AclConnection::GetPhyType() const { return type_; }

}  // namespace test_vendor_lib
+21 −32
Original line number Diff line number Diff line
@@ -18,50 +18,39 @@

#include <cstdint>

#include "hci/address.h"
#include "hci/address_with_type.h"
#include "phy.h"

namespace test_vendor_lib {

using ::bluetooth::hci::Address;
using ::bluetooth::hci::AddressWithType;

// Model the connection of a device to the controller.
class AclConnection {
 public:
  AclConnection(Address addr) : address_(addr), address_type_(0), own_address_type_(0) {}
  AclConnection(AddressWithType addr, AddressWithType own_addr,
                Phy::Type phy_type);

  virtual ~AclConnection() = default;

  void Encrypt() {
    encrypted_ = true;
  };
  bool IsEncrypted() const {
    return encrypted_;
  };
  void Encrypt();

  bool IsEncrypted() const;

  AddressWithType GetAddress() const;

  void SetAddress(AddressWithType address);

  AddressWithType GetOwnAddress() const;

  void SetOwnAddress(AddressWithType own_addr);

  Address GetAddress() const {
    return address_;
  }
  void SetAddress(Address address) {
    address_ = address;
  }

  uint8_t GetAddressType() const {
    return address_type_;
  }
  void SetAddressType(uint8_t address_type) {
    address_type_ = address_type;
  }
  uint8_t GetOwnAddressType() const {
    return own_address_type_;
  }
  void SetOwnAddressType(uint8_t address_type) {
    own_address_type_ = address_type;
  }
  Phy::Type GetPhyType() const;

 private:
  Address address_;
  uint8_t address_type_;
  uint8_t own_address_type_;
  AddressWithType address_;
  AddressWithType own_address_;
  Phy::Type type_{Phy::Type::BR_EDR};

  // State variables
  bool encrypted_{false};
+53 −52
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@ using std::shared_ptr;
namespace test_vendor_lib {

using ::bluetooth::hci::Address;
using ::bluetooth::hci::AddressType;
using ::bluetooth::hci::AddressWithType;

bool AclConnectionHandler::HasHandle(uint16_t handle) const {
  if (acl_connections_.count(handle) == 0) {
@@ -70,9 +72,16 @@ bool AclConnectionHandler::CancelPendingConnection(Address addr) {
  return true;
}

bool AclConnectionHandler::CreatePendingLeConnection(Address addr, uint8_t address_type) {
  if (IsDeviceConnected(addr, address_type)) {
    LOG_INFO("%s: %s (type %hhx) is already connected", __func__, addr.ToString().c_str(), address_type);
bool AclConnectionHandler::CreatePendingLeConnection(AddressWithType addr) {
  bool device_connected = false;
  for (auto pair : acl_connections_) {
    auto connection = std::get<AclConnection>(pair);
    if (connection.GetAddress() == addr) {
      device_connected = true;
    }
  }
  if (device_connected) {
    LOG_INFO("%s: %s is already connected", __func__, addr.ToString().c_str());
    return false;
  }
  if (le_connection_pending_) {
@@ -81,42 +90,44 @@ bool AclConnectionHandler::CreatePendingLeConnection(Address addr, uint8_t addre
  }
  le_connection_pending_ = true;
  pending_le_connection_address_ = addr;
  pending_le_connection_address_type_ = address_type;
  return true;
}

bool AclConnectionHandler::HasPendingLeConnection(Address addr,
                                                  uint8_t address_type) const {
  return le_connection_pending_ && pending_le_connection_address_ == addr &&
         pending_le_connection_address_type_ == address_type;
bool AclConnectionHandler::HasPendingLeConnection(AddressWithType addr) const {
  return le_connection_pending_ && pending_le_connection_address_ == addr;
}

bool AclConnectionHandler::CancelPendingLeConnection(Address addr, uint8_t address_type) {
  if (!le_connection_pending_ || pending_le_connection_address_ != addr ||
      pending_le_connection_address_type_ != address_type) {
bool AclConnectionHandler::CancelPendingLeConnection(AddressWithType addr) {
  if (!le_connection_pending_ || pending_le_connection_address_ != addr) {
    return false;
  }
  le_connection_pending_ = false;
  pending_le_connection_address_ = Address::kEmpty;
  pending_le_connection_address_type_ = 0xba;
  pending_le_connection_address_ =
      AddressWithType{Address::kEmpty, AddressType::PUBLIC_DEVICE_ADDRESS};
  return true;
}

uint16_t AclConnectionHandler::CreateConnection(Address addr) {
uint16_t AclConnectionHandler::CreateConnection(Address addr,
                                                Address own_addr) {
  if (CancelPendingConnection(addr)) {
    uint16_t handle = GetUnusedHandle();
    acl_connections_.emplace(handle, addr);
    acl_connections_.emplace(
        handle,
        AclConnection{
            AddressWithType{addr, AddressType::PUBLIC_DEVICE_ADDRESS},
            AddressWithType{own_addr, AddressType::PUBLIC_DEVICE_ADDRESS},
            Phy::Type::BR_EDR});
    return handle;
  }
  return acl::kReservedHandle;
}

uint16_t AclConnectionHandler::CreateLeConnection(Address addr, uint8_t address_type, uint8_t own_address_type) {
  if (CancelPendingLeConnection(addr, address_type)) {
uint16_t AclConnectionHandler::CreateLeConnection(AddressWithType addr,
                                                  AddressWithType own_addr) {
  if (CancelPendingLeConnection(addr)) {
    uint16_t handle = GetUnusedHandle();
    acl_connections_.emplace(handle, addr);
    set_own_address_type(handle, own_address_type);
    SetAddress(handle, addr, address_type);
    acl_connections_.emplace(
        handle, AclConnection{addr, own_addr, Phy::Type::LOW_ENERGY});
    return handle;
  }
  return acl::kReservedHandle;
@@ -126,7 +137,7 @@ bool AclConnectionHandler::Disconnect(uint16_t handle) {
  return acl_connections_.erase(handle) > 0;
}

uint16_t AclConnectionHandler::GetHandle(Address addr) const {
uint16_t AclConnectionHandler::GetHandle(AddressWithType addr) const {
  for (auto pair : acl_connections_) {
    if (std::get<AclConnection>(pair).GetAddress() == addr) {
      return std::get<0>(pair);
@@ -135,41 +146,24 @@ uint16_t AclConnectionHandler::GetHandle(Address addr) const {
  return acl::kReservedHandle;
}

Address AclConnectionHandler::GetAddress(uint16_t handle) const {
  ASSERT_LOG(HasHandle(handle), "Handle unknown %hd", handle);
  return acl_connections_.at(handle).GetAddress();
uint16_t AclConnectionHandler::GetHandleOnlyAddress(
    bluetooth::hci::Address addr) const {
  for (auto pair : acl_connections_) {
    if (std::get<AclConnection>(pair).GetAddress().GetAddress() == addr) {
      return std::get<0>(pair);
    }

uint8_t AclConnectionHandler::GetAddressType(uint16_t handle) const {
  ASSERT_LOG(HasHandle(handle), "Handle unknown %hd", handle);
  return acl_connections_.at(handle).GetAddressType();
  }

void AclConnectionHandler::set_own_address_type(uint16_t handle, uint8_t address_type) {
  ASSERT_LOG(HasHandle(handle), "Handle unknown %hd", handle);
  acl_connections_.at(handle).SetOwnAddressType(address_type);
  return acl::kReservedHandle;
}

uint8_t AclConnectionHandler::GetOwnAddressType(uint16_t handle) const {
AddressWithType AclConnectionHandler::GetAddress(uint16_t handle) const {
  ASSERT_LOG(HasHandle(handle), "Handle unknown %hd", handle);
  return acl_connections_.at(handle).GetOwnAddressType();
}

bool AclConnectionHandler::IsConnected(uint16_t handle) const {
  if (!HasHandle(handle)) {
    return false;
  }
  return true;
  return acl_connections_.at(handle).GetAddress();
}

bool AclConnectionHandler::IsDeviceConnected(Address addr, uint8_t address_type) const {
  for (auto pair : acl_connections_) {
    auto connection = std::get<AclConnection>(pair);
    if (connection.GetAddress() == addr && connection.GetAddressType() == address_type) {
      return true;
    }
  }
  return false;
AddressWithType AclConnectionHandler::GetOwnAddress(uint16_t handle) const {
  ASSERT_LOG(HasHandle(handle), "Handle unknown %hd", handle);
  return acl_connections_.at(handle).GetOwnAddress();
}

void AclConnectionHandler::Encrypt(uint16_t handle) {
@@ -186,13 +180,20 @@ bool AclConnectionHandler::IsEncrypted(uint16_t handle) const {
  return acl_connections_.at(handle).IsEncrypted();
}

void AclConnectionHandler::SetAddress(uint16_t handle, Address address, uint8_t address_type) {
void AclConnectionHandler::SetAddress(uint16_t handle,
                                      AddressWithType address) {
  if (!HasHandle(handle)) {
    return;
  }
  auto connection = acl_connections_.at(handle);
  connection.SetAddress(address);
  connection.SetAddressType(address_type);
}

Phy::Type AclConnectionHandler::GetPhyType(uint16_t handle) const {
  if (!HasHandle(handle)) {
    return Phy::Type::BR_EDR;
  }
  return acl_connections_.at(handle).GetPhyType();
}

}  // namespace test_vendor_lib
+25 −24
Original line number Diff line number Diff line
@@ -22,59 +22,60 @@

#include "acl_connection.h"
#include "hci/address.h"
#include "hci/address_with_type.h"
#include "include/acl.h"
#include "phy.h"

namespace test_vendor_lib {

using ::bluetooth::hci::Address;

class AclConnectionHandler {
 public:
  AclConnectionHandler() = default;

  virtual ~AclConnectionHandler() = default;

  bool CreatePendingConnection(Address addr, bool authenticate_on_connect);
  bool HasPendingConnection(Address addr) const;
  bool CancelPendingConnection(Address addr);
  bool CreatePendingConnection(bluetooth::hci::Address addr,
                               bool authenticate_on_connect);
  bool HasPendingConnection(bluetooth::hci::Address addr) const;
  bool CancelPendingConnection(bluetooth::hci::Address addr);
  bool AuthenticatePendingConnection() const;

  bool CreatePendingLeConnection(Address addr, uint8_t addr_type);
  bool HasPendingLeConnection(Address addr, uint8_t addr_type) const;
  bool CancelPendingLeConnection(Address addr, uint8_t addr_type);
  bool CreatePendingLeConnection(bluetooth::hci::AddressWithType addr);
  bool HasPendingLeConnection(bluetooth::hci::AddressWithType addr) const;
  bool CancelPendingLeConnection(bluetooth::hci::AddressWithType addr);

  uint16_t CreateConnection(Address addr);
  uint16_t CreateLeConnection(Address addr, uint8_t address_type, uint8_t own_address_type);
  uint16_t CreateConnection(bluetooth::hci::Address addr,
                            bluetooth::hci::Address own_addr);
  uint16_t CreateLeConnection(bluetooth::hci::AddressWithType addr,
                              bluetooth::hci::AddressWithType own_addr);
  bool Disconnect(uint16_t handle);
  bool HasHandle(uint16_t handle) const;

  uint16_t GetHandle(Address addr) const;
  Address GetAddress(uint16_t handle) const;
  uint8_t GetAddressType(uint16_t handle) const;
  uint8_t GetOwnAddressType(uint16_t handle) const;

  void SetConnected(uint16_t handle, bool connected);
  bool IsConnected(uint16_t handle) const;

  bool IsDeviceConnected(Address addr, uint8_t address_type = 0) const;
  uint16_t GetHandle(bluetooth::hci::AddressWithType addr) const;
  uint16_t GetHandleOnlyAddress(bluetooth::hci::Address addr) const;
  bluetooth::hci::AddressWithType GetAddress(uint16_t handle) const;
  bluetooth::hci::AddressWithType GetOwnAddress(uint16_t handle) const;

  void Encrypt(uint16_t handle);
  bool IsEncrypted(uint16_t handle) const;

  void SetAddress(uint16_t handle, Address address, uint8_t address_type = 0);  // default to public
  void SetAddress(uint16_t handle, bluetooth::hci::AddressWithType address);

  Phy::Type GetPhyType(uint16_t handle) const;

 private:
  std::unordered_map<uint16_t, AclConnection> acl_connections_;
  bool classic_connection_pending_{false};
  Address pending_connection_address_{Address::kEmpty};
  bluetooth::hci::Address pending_connection_address_{
      bluetooth::hci::Address::kEmpty};
  bool authenticate_pending_classic_connection_{false};
  bool le_connection_pending_{false};
  Address pending_le_connection_address_{Address::kEmpty};
  uint8_t pending_le_connection_address_type_{false};
  bluetooth::hci::AddressWithType pending_le_connection_address_{
      bluetooth::hci::Address::kEmpty,
      bluetooth::hci::AddressType::PUBLIC_DEVICE_ADDRESS};

  uint16_t GetUnusedHandle();
  uint16_t last_handle_{acl::kReservedHandle - 2};
  void set_own_address_type(uint16_t handle, uint8_t own_address_type);
};

}  // namespace test_vendor_lib
+116 −82

File changed.

Preview size limit exceeded, changes collapsed.

Loading