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

Commit 06de850d authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes I3f609fe5,Ide5b514b,Ibe0a9880 into tm-dev

* changes:
  RootCanal: Use the resolved address type
  RootCanal: Implement RPA advertising
  RootCanal: Check connections when setting features
parents 19dd98e8 5d6d21a6
Loading
Loading
Loading
Loading
+13 −4
Original line number Diff line number Diff line
@@ -17,9 +17,14 @@
#include "acl_connection.h"

namespace rootcanal {
AclConnection::AclConnection(AddressWithType addr, AddressWithType own_addr,
AclConnection::AclConnection(AddressWithType address,
                             AddressWithType own_address,
                             AddressWithType resolved_address,
                             Phy::Type phy_type)
    : address_(addr), own_address_(own_addr), type_(phy_type) {}
    : address_(address),
      own_address_(own_address),
      resolved_address_(resolved_address),
      type_(phy_type) {}

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

@@ -31,8 +36,12 @@ void AclConnection::SetAddress(AddressWithType address) { address_ = address; }

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

void AclConnection::SetOwnAddress(AddressWithType own_addr) {
  own_address_ = own_addr;
AddressWithType AclConnection::GetResolvedAddress() const {
  return resolved_address_;
}

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

Phy::Type AclConnection::GetPhyType() const { return type_; }
+6 −3
Original line number Diff line number Diff line
@@ -28,8 +28,8 @@ using ::bluetooth::hci::AddressWithType;
// Model the connection of a device to the controller.
class AclConnection {
 public:
  AclConnection(AddressWithType addr, AddressWithType own_addr,
                Phy::Type phy_type);
  AclConnection(AddressWithType address, AddressWithType own_address,
                AddressWithType resolved_address, Phy::Type phy_type);

  virtual ~AclConnection() = default;

@@ -43,13 +43,16 @@ class AclConnection {

  AddressWithType GetOwnAddress() const;

  void SetOwnAddress(AddressWithType own_addr);
  void SetOwnAddress(AddressWithType address);

  AddressWithType GetResolvedAddress() const;

  Phy::Type GetPhyType() const;

 private:
  AddressWithType address_;
  AddressWithType own_address_;
  AddressWithType resolved_address_;
  Phy::Type type_{Phy::Type::BR_EDR};

  // State variables
+29 −20
Original line number Diff line number Diff line
@@ -70,27 +70,35 @@ bool AclConnectionHandler::CancelPendingConnection(Address addr) {
  }
  classic_connection_pending_ = false;
  pending_connection_address_ = Address::kEmpty;
  pending_le_connection_resolved_address_ = AddressWithType();
  return true;
}

bool AclConnectionHandler::CreatePendingLeConnection(AddressWithType addr) {
  bool device_connected = false;
bool AclConnectionHandler::CreatePendingLeConnection(
    AddressWithType peer, AddressWithType resolved_peer,
    AddressWithType local_address) {
  for (auto pair : acl_connections_) {
    auto connection = std::get<AclConnection>(pair);
    if (connection.GetAddress() == addr) {
      device_connected = true;
    if (connection.GetAddress() == peer ||
        connection.GetResolvedAddress() == resolved_peer) {
      LOG_INFO("%s: %s is already connected", __func__,
               peer.ToString().c_str());
      if (connection.GetResolvedAddress() == resolved_peer) {
        LOG_INFO("%s: allowing a second connection with %s", __func__,
                 resolved_peer.ToString().c_str());
      } else {
        return false;
      }
    }
  if (device_connected) {
    LOG_INFO("%s: %s is already connected", __func__, addr.ToString().c_str());
    return false;
  }
  if (le_connection_pending_) {
    LOG_INFO("%s: connection already pending", __func__);
    return false;
  }
  le_connection_pending_ = true;
  pending_le_connection_address_ = addr;
  pending_le_connection_address_ = peer;
  pending_le_connection_own_address_ = local_address;
  pending_le_connection_resolved_address_ = resolved_peer;
  return true;
}

@@ -105,6 +113,8 @@ bool AclConnectionHandler::CancelPendingLeConnection(AddressWithType addr) {
  le_connection_pending_ = false;
  pending_le_connection_address_ =
      AddressWithType{Address::kEmpty, AddressType::PUBLIC_DEVICE_ADDRESS};
  pending_le_connection_resolved_address_ =
      AddressWithType{Address::kEmpty, AddressType::PUBLIC_DEVICE_ADDRESS};
  return true;
}

@@ -117,7 +127,7 @@ uint16_t AclConnectionHandler::CreateConnection(Address addr,
        AclConnection{
            AddressWithType{addr, AddressType::PUBLIC_DEVICE_ADDRESS},
            AddressWithType{own_addr, AddressType::PUBLIC_DEVICE_ADDRESS},
            Phy::Type::BR_EDR});
            AddressWithType(), Phy::Type::BR_EDR});
    return handle;
  }
  return kReservedHandle;
@@ -125,10 +135,12 @@ uint16_t AclConnectionHandler::CreateConnection(Address addr,

uint16_t AclConnectionHandler::CreateLeConnection(AddressWithType addr,
                                                  AddressWithType own_addr) {
  AddressWithType resolved_peer = pending_le_connection_resolved_address_;
  if (CancelPendingLeConnection(addr)) {
    uint16_t handle = GetUnusedHandle();
    acl_connections_.emplace(
        handle, AclConnection{addr, own_addr, Phy::Type::LOW_ENERGY});
        handle,
        AclConnection{addr, own_addr, resolved_peer, Phy::Type::LOW_ENERGY});
    return handle;
  }
  return kReservedHandle;
@@ -184,6 +196,12 @@ AddressWithType AclConnectionHandler::GetOwnAddress(uint16_t handle) const {
  return acl_connections_.at(handle).GetOwnAddress();
}

AddressWithType AclConnectionHandler::GetResolvedAddress(
    uint16_t handle) const {
  ASSERT_LOG(HasHandle(handle), "Unknown handle %hd", handle);
  return acl_connections_.at(handle).GetResolvedAddress();
}

void AclConnectionHandler::Encrypt(uint16_t handle) {
  if (!HasHandle(handle)) {
    return;
@@ -198,15 +216,6 @@ bool AclConnectionHandler::IsEncrypted(uint16_t handle) const {
  return acl_connections_.at(handle).IsEncrypted();
}

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

Phy::Type AclConnectionHandler::GetPhyType(uint16_t handle) const {
  if (!HasHandle(handle)) {
    return Phy::Type::BR_EDR;
+10 −3
Original line number Diff line number Diff line
@@ -58,7 +58,9 @@ class AclConnectionHandler {
      bluetooth::hci::Address addr) const;
  ScoLinkParameters GetScoLinkParameters(bluetooth::hci::Address addr) const;

  bool CreatePendingLeConnection(bluetooth::hci::AddressWithType addr);
  bool CreatePendingLeConnection(bluetooth::hci::AddressWithType peer,
                                 bluetooth::hci::AddressWithType resolved_peer,
                                 bluetooth::hci::AddressWithType local_address);
  bool HasPendingLeConnection(bluetooth::hci::AddressWithType addr) const;
  bool CancelPendingLeConnection(bluetooth::hci::AddressWithType addr);

@@ -75,12 +77,11 @@ class AclConnectionHandler {
  bluetooth::hci::AddressWithType GetAddress(uint16_t handle) const;
  bluetooth::hci::Address GetScoAddress(uint16_t handle) const;
  bluetooth::hci::AddressWithType GetOwnAddress(uint16_t handle) const;
  bluetooth::hci::AddressWithType GetResolvedAddress(uint16_t handle) const;

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

  void SetAddress(uint16_t handle, bluetooth::hci::AddressWithType address);

  Phy::Type GetPhyType(uint16_t handle) const;

  std::unique_ptr<bluetooth::hci::LeSetCigParametersCompleteBuilder>
@@ -135,6 +136,12 @@ class AclConnectionHandler {
  bluetooth::hci::AddressWithType pending_le_connection_address_{
      bluetooth::hci::Address::kEmpty,
      bluetooth::hci::AddressType::PUBLIC_DEVICE_ADDRESS};
  bluetooth::hci::AddressWithType pending_le_connection_own_address_{
      bluetooth::hci::Address::kEmpty,
      bluetooth::hci::AddressType::PUBLIC_DEVICE_ADDRESS};
  bluetooth::hci::AddressWithType pending_le_connection_resolved_address_{
      bluetooth::hci::Address::kEmpty,
      bluetooth::hci::AddressType::PUBLIC_DEVICE_ADDRESS};

  uint16_t GetUnusedHandle();
  uint16_t last_handle_{kReservedHandle - 2};
+60 −28
Original line number Diff line number Diff line
@@ -1602,13 +1602,18 @@ void DualModeController::LeSetEventMask(CommandView command) {
void DualModeController::LeSetHostFeature(CommandView command) {
  auto command_view = gd_hci::LeSetHostFeatureView::Create(command);
  ASSERT(command_view.IsValid());
  // TODO: if the controller has active connections, return COMMAND_DISALLOED
  ErrorCode error_code =
      properties_.SetLeHostFeature(

  ErrorCode error_code = ErrorCode::SUCCESS;
  if (link_layer_controller_.HasAclConnection()) {
    error_code = ErrorCode::COMMAND_DISALLOWED;
  } else {
    bool bit_was_set = properties_.SetLeHostFeature(
        static_cast<uint8_t>(command_view.GetBitNumber()),
          static_cast<uint8_t>(command_view.GetBitValue()))
          ? ErrorCode::SUCCESS
          : ErrorCode::UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
        static_cast<uint8_t>(command_view.GetBitValue()));
    if (!bit_was_set) {
      error_code = ErrorCode::UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
    }
  }
  send_event_(bluetooth::hci::LeSetHostFeatureCompleteBuilder::Create(
      kNumCommandPackets, error_code));
}
@@ -1945,10 +1950,13 @@ void DualModeController::LeAddDeviceToFilterAcceptList(CommandView command) {
          gd_hci::AclCommandView::Create(command)));
  ASSERT(command_view.IsValid());

  uint8_t addr_type = static_cast<uint8_t>(command_view.GetAddressType());
  Address address = command_view.GetAddress();
  ErrorCode result =
      link_layer_controller_.LeFilterAcceptListAddDevice(address, addr_type);
  ErrorCode result = ErrorCode::INVALID_HCI_COMMAND_PARAMETERS;
  if (command_view.GetAddressType() !=
      bluetooth::hci::FilterAcceptListAddressType::ANONYMOUS_ADVERTISERS) {
    result = link_layer_controller_.LeFilterAcceptListAddDevice(
        command_view.GetAddress(), static_cast<bluetooth::hci::AddressType>(
                                       command_view.GetAddressType()));
  }
  send_event_(
      bluetooth::hci::LeAddDeviceToFilterAcceptListCompleteBuilder::Create(
          kNumCommandPackets, result));
@@ -1961,12 +1969,18 @@ void DualModeController::LeRemoveDeviceFromFilterAcceptList(
          gd_hci::AclCommandView::Create(command)));
  ASSERT(command_view.IsValid());

  uint8_t addr_type = static_cast<uint8_t>(command_view.GetAddressType());
  Address address = command_view.GetAddress();
  link_layer_controller_.LeFilterAcceptListRemoveDevice(address, addr_type);
  ErrorCode status = ErrorCode::SUCCESS;
  if (command_view.GetAddressType() !=
      bluetooth::hci::FilterAcceptListAddressType::ANONYMOUS_ADVERTISERS) {
    link_layer_controller_.LeFilterAcceptListAddDevice(
        command_view.GetAddress(), static_cast<bluetooth::hci::AddressType>(
                                       command_view.GetAddressType()));
  } else {
    status = ErrorCode::INVALID_HCI_COMMAND_PARAMETERS;
  }
  send_event_(
      bluetooth::hci::LeRemoveDeviceFromFilterAcceptListCompleteBuilder::Create(
          kNumCommandPackets, ErrorCode::SUCCESS));
          kNumCommandPackets, status));
}

void DualModeController::LeClearResolvingList(CommandView command) {
@@ -2037,14 +2051,18 @@ void DualModeController::LeAddDeviceToResolvingList(CommandView command) {
  auto command_view = gd_hci::LeAddDeviceToResolvingListView::Create(
      gd_hci::LeSecurityCommandView::Create(command));
  ASSERT(command_view.IsValid());

  auto addr_type =
      static_cast<uint8_t>(command_view.GetPeerIdentityAddressType());
  Address address = command_view.GetPeerIdentityAddress();

  AddressType peer_address_type;
  switch (command_view.GetPeerIdentityAddressType()) {
    case bluetooth::hci::PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS:
      peer_address_type = AddressType::PUBLIC_DEVICE_ADDRESS;
      break;
    case bluetooth::hci::PeerAddressType::RANDOM_DEVICE_OR_IDENTITY_ADDRESS:
      peer_address_type = AddressType::RANDOM_DEVICE_ADDRESS;
      break;
  }
  auto status = link_layer_controller_.LeResolvingListAddDevice(
      address, addr_type, command_view.GetPeerIrk(),
      command_view.GetLocalIrk());
      command_view.GetPeerIdentityAddress(), peer_address_type,
      command_view.GetPeerIrk(), command_view.GetLocalIrk());
  send_event_(bluetooth::hci::LeAddDeviceToResolvingListCompleteBuilder::Create(
      kNumCommandPackets, status));
}
@@ -2054,10 +2072,17 @@ void DualModeController::LeRemoveDeviceFromResolvingList(CommandView command) {
      gd_hci::LeSecurityCommandView::Create(command));
  ASSERT(command_view.IsValid());

  uint8_t addr_type =
      static_cast<uint8_t>(command_view.GetPeerIdentityAddressType());
  Address address = command_view.GetPeerIdentityAddress();
  link_layer_controller_.LeResolvingListRemoveDevice(address, addr_type);
  AddressType peer_address_type;
  switch (command_view.GetPeerIdentityAddressType()) {
    case bluetooth::hci::PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS:
      peer_address_type = AddressType::PUBLIC_DEVICE_ADDRESS;
      break;
    case bluetooth::hci::PeerAddressType::RANDOM_DEVICE_OR_IDENTITY_ADDRESS:
      peer_address_type = AddressType::RANDOM_DEVICE_ADDRESS;
      break;
  }
  link_layer_controller_.LeResolvingListRemoveDevice(
      command_view.GetPeerIdentityAddress(), peer_address_type);
  send_event_(
      bluetooth::hci::LeRemoveDeviceFromResolvingListCompleteBuilder::Create(
          kNumCommandPackets, ErrorCode::SUCCESS));
@@ -2146,11 +2171,18 @@ void DualModeController::LeSetPrivacyMode(CommandView command) {
      gd_hci::LeSecurityCommandView::Create(command));
  ASSERT(command_view.IsValid());

  uint8_t peer_identity_address_type =
      static_cast<uint8_t>(command_view.GetPeerIdentityAddressType());
  Address peer_identity_address = command_view.GetPeerIdentityAddress();
  uint8_t privacy_mode = static_cast<uint8_t>(command_view.GetPrivacyMode());

  AddressType peer_identity_address_type;
  switch (command_view.GetPeerIdentityAddressType()) {
    case bluetooth::hci::PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS:
      peer_identity_address_type = AddressType::PUBLIC_DEVICE_ADDRESS;
      break;
    case bluetooth::hci::PeerAddressType::RANDOM_DEVICE_OR_IDENTITY_ADDRESS:
      peer_identity_address_type = AddressType::RANDOM_DEVICE_ADDRESS;
      break;
  }
  if (link_layer_controller_.LeResolvingListContainsDevice(
          peer_identity_address, peer_identity_address_type)) {
    link_layer_controller_.LeSetPrivacyMode(
Loading