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

Commit e94e88c7 authored by Chris Manton's avatar Chris Manton Committed by Gerrit Code Review
Browse files

Merge changes Ie8b8e8a1,I5e1713f1,I0cabfa9c,I6235c954,I85dab865, ...

* changes:
  gd_acl_shim: Start schedule address rotation on first client registration
  Add stack/gatt/gatt_main::gatt_find_in_device_record
  gd_acl: Resolve address after connection
  gd: Present zeroed peer addr with type when using connect list
  gd: Store the address when using resolving privacy policy
  gd: gd/hci/le_address_manager_test::ASSERT_LOG when command queue is empty
  gd: Reset le address rotation timer
  gd: Remove unneeded client check when setting privacy
  gd: Unwind futures and promises in reverse time
parents e7248ae9 e89bcbf4
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -390,6 +390,10 @@ struct le_impl : public bluetooth::hci::LeAddressManagerCallback {

    connecting_le_.insert(address_with_type);

    if (initiator_filter_policy == InitiatorFilterPolicy::USE_CONNECT_LIST) {
      address_with_type = AddressWithType();
    }

    if (controller_->IsSupported(OpCode::LE_EXTENDED_CREATE_CONNECTION)) {
      LeCreateConnPhyScanParameters tmp;
      tmp.scan_interval_ = le_scan_interval;
+23 −8
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ using packet::PacketView;
using packet::RawBuilder;

constexpr std::chrono::seconds kTimeout = std::chrono::seconds(2);
const AddressWithType empty_address_with_type = hci::AddressWithType();

PacketView<kLittleEndian> GetPacketView(std::unique_ptr<packet::BasePacketBuilder> packet) {
  auto bytes = std::make_shared<std::vector<uint8_t>>();
@@ -168,8 +169,8 @@ class TestHciLayer : public HciLayer {

  ConnectionManagementCommandView GetLastCommandPacket(OpCode op_code) {
    if (!command_queue_.empty() && command_future_ != nullptr) {
      command_promise_.reset();
      command_future_.reset();
      command_promise_.reset();
    } else if (command_future_ != nullptr) {
      auto result = command_future_->wait_for(std::chrono::milliseconds(1000));
      EXPECT_NE(std::future_status::timeout, result);
@@ -317,7 +318,6 @@ class AclManagerNoCallbacksTest : public ::testing::Test {
    acl_manager_ = static_cast<AclManager*>(fake_registry_.GetModuleUnderTest(&AclManager::Factory));
    Address::FromString("A1:A2:A3:A4:A5:A6", remote);

    // Verify LE Set Random Address was sent during setup
    hci::Address address;
    Address::FromString("D0:05:04:03:02:01", address);
    hci::AddressWithType address_with_type(address, hci::AddressType::RANDOM_DEVICE_ADDRESS);
@@ -336,6 +336,8 @@ class AclManagerNoCallbacksTest : public ::testing::Test {
    ASSERT_TRUE(set_random_address_packet.IsValid());
    my_initiating_address =
        AddressWithType(set_random_address_packet.GetRandomAddress(), AddressType::RANDOM_DEVICE_ADDRESS);
    // Verify LE Set Random Address was sent during setup
    test_hci_layer_->GetLastCommandPacket(OpCode::LE_SET_RANDOM_ADDRESS);
    test_hci_layer_->IncomingEvent(LeSetRandomAddressCompleteBuilder::Create(0x01, ErrorCode::SUCCESS));
  }

@@ -352,6 +354,7 @@ class AclManagerNoCallbacksTest : public ::testing::Test {
  os::Handler* client_handler_ = nullptr;
  Address remote;
  AddressWithType my_initiating_address;
  const bool use_connect_list_ = true;  // gd currently only supports connect list

  std::future<void> GetConnectionFuture() {
    ASSERT_LOG(mock_connection_callback_.connection_promise_ == nullptr, "Promises promises ... Only one at a time");
@@ -582,8 +585,13 @@ class AclManagerWithLeConnectionTest : public AclManagerTest {
    auto le_connection_management_command_view = LeConnectionManagementCommandView::Create(packet);
    auto command_view = LeCreateConnectionView::Create(le_connection_management_command_view);
    ASSERT_TRUE(command_view.IsValid());
    EXPECT_EQ(command_view.GetPeerAddress(), remote);
    EXPECT_EQ(command_view.GetPeerAddressType(), AddressType::PUBLIC_DEVICE_ADDRESS);
    if (use_connect_list_) {
      ASSERT_EQ(command_view.GetPeerAddress(), empty_address_with_type.GetAddress());
      ASSERT_EQ(command_view.GetPeerAddressType(), empty_address_with_type.GetAddressType());
    } else {
      ASSERT_EQ(command_view.GetPeerAddress(), remote);
      ASSERT_EQ(command_view.GetPeerAddressType(), AddressType::PUBLIC_DEVICE_ADDRESS);
    }

    test_hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01));

@@ -659,7 +667,11 @@ TEST_F(AclManagerTest, invoke_registered_callback_le_connection_complete_fail) {
  auto le_connection_management_command_view = LeConnectionManagementCommandView::Create(packet);
  auto command_view = LeCreateConnectionView::Create(le_connection_management_command_view);
  ASSERT_TRUE(command_view.IsValid());
  EXPECT_EQ(command_view.GetPeerAddress(), remote);
  if (use_connect_list_) {
    ASSERT_EQ(command_view.GetPeerAddress(), hci::Address::kEmpty);
  } else {
    ASSERT_EQ(command_view.GetPeerAddress(), remote);
  }
  EXPECT_EQ(command_view.GetPeerAddressType(), AddressType::PUBLIC_DEVICE_ADDRESS);

  test_hci_layer_->IncomingEvent(LeCreateConnectionStatusBuilder::Create(ErrorCode::SUCCESS, 0x01));
@@ -1223,15 +1235,17 @@ class AclManagerWithResolvableAddressTest : public AclManagerNoCallbacksTest {
    fake_registry_.InjectTestModule(&Controller::Factory, test_controller_);
    client_handler_ = fake_registry_.GetTestModuleHandler(&HciLayer::Factory);
    ASSERT_NE(client_handler_, nullptr);
    test_hci_layer_->SetCommandFuture();
    fake_registry_.Start<AclManager>(&thread_);
    acl_manager_ = static_cast<AclManager*>(fake_registry_.GetModuleUnderTest(&AclManager::Factory));
    Address::FromString("A1:A2:A3:A4:A5:A6", remote);

    // Verify LE Set Random Address was sent during setup
    hci::Address address;
    Address::FromString("D0:05:04:03:02:01", address);
    hci::AddressWithType address_with_type(address, hci::AddressType::RANDOM_DEVICE_ADDRESS);
    crypto_toolbox::Octet16 irk = {};
    acl_manager_->RegisterCallbacks(&mock_connection_callback_, client_handler_);
    acl_manager_->RegisterLeCallbacks(&mock_le_connection_callbacks_, client_handler_);
    auto minimum_rotation_time = std::chrono::milliseconds(7 * 60 * 1000);
    auto maximum_rotation_time = std::chrono::milliseconds(15 * 60 * 1000);
    acl_manager_->SetPrivacyPolicyForInitiatorAddress(
@@ -1240,8 +1254,9 @@ class AclManagerWithResolvableAddressTest : public AclManagerNoCallbacksTest {
        irk,
        minimum_rotation_time,
        maximum_rotation_time);
    acl_manager_->RegisterCallbacks(&mock_connection_callback_, client_handler_);
    acl_manager_->RegisterLeCallbacks(&mock_le_connection_callbacks_, client_handler_);

    test_hci_layer_->GetLastCommandPacket(OpCode::LE_SET_RANDOM_ADDRESS);
    test_hci_layer_->IncomingEvent(LeSetRandomAddressCompleteBuilder::Create(0x01, ErrorCode::SUCCESS));
  }
};

+45 −11
Original line number Diff line number Diff line
/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "hci/le_address_manager.h"
#include "common/init_flags.h"
#include "os/log.h"
#include "os/rand.h"

@@ -61,15 +78,12 @@ void LeAddressManager::SetPrivacyPolicyForInitiatorAddress(
    } break;
    case AddressPolicy::USE_NON_RESOLVABLE_ADDRESS:
    case AddressPolicy::USE_RESOLVABLE_ADDRESS:
      le_address_ = fixed_address;
      rotation_irk_ = rotation_irk;
      minimum_rotation_time_ = minimum_rotation_time;
      maximum_rotation_time_ = maximum_rotation_time;
      address_rotation_alarm_ = std::make_unique<os::Alarm>(handler_);
      if (!registered_clients_.empty()) {
        // clients registered and paused before the policy set, rotate random address and resume
        // clients after set random address complete
        handler_->BindOnceOn(this, &LeAddressManager::rotate_random_address).Invoke();
      }
      set_random_address();
      break;
    case AddressPolicy::POLICY_NOT_SET:
      LOG_ALWAYS_FATAL("invalid parameters");
@@ -133,9 +147,15 @@ void LeAddressManager::register_client(LeAddressManagerCallback* callback) {
  } else if (
      address_policy_ == AddressPolicy::USE_RESOLVABLE_ADDRESS ||
      address_policy_ == AddressPolicy::USE_NON_RESOLVABLE_ADDRESS) {
    if (bluetooth::common::InitFlags::GdAclEnabled()) {
      if (registered_clients_.size() == 1) {
        schedule_rotate_random_address();
      }
    } else {
      prepare_to_rotate();
    }
  }
}

void LeAddressManager::Unregister(LeAddressManagerCallback* callback) {
  handler_->BindOnceOn(this, &LeAddressManager::unregister_client, callback).Invoke();
@@ -145,6 +165,7 @@ void LeAddressManager::unregister_client(LeAddressManagerCallback* callback) {
  registered_clients_.erase(callback);
  if (registered_clients_.empty() && address_rotation_alarm_ != nullptr) {
    address_rotation_alarm_->Cancel();
    address_rotation_alarm_.reset();
  }
}

@@ -218,17 +239,19 @@ void LeAddressManager::prepare_to_rotate() {
  pause_registered_clients();
}

void LeAddressManager::rotate_random_address() {
void LeAddressManager::schedule_rotate_random_address() {
  address_rotation_alarm_->Schedule(
      common::BindOnce(&LeAddressManager::prepare_to_rotate, common::Unretained(this)),
      get_next_private_address_interval_ms());
}

void LeAddressManager::set_random_address() {
  if (address_policy_ != AddressPolicy::USE_RESOLVABLE_ADDRESS &&
      address_policy_ != AddressPolicy::USE_NON_RESOLVABLE_ADDRESS) {
    LOG_ALWAYS_FATAL("Invalid address policy!");
    return;
  }

  address_rotation_alarm_->Schedule(
      common::BindOnce(&LeAddressManager::prepare_to_rotate, common::Unretained(this)),
      get_next_private_address_interval_ms());

  hci::Address address;
  if (address_policy_ == AddressPolicy::USE_RESOLVABLE_ADDRESS) {
    address = generate_rpa();
@@ -240,6 +263,17 @@ void LeAddressManager::rotate_random_address() {
  le_address_ = AddressWithType(address, AddressType::RANDOM_DEVICE_ADDRESS);
}

void LeAddressManager::rotate_random_address() {
  if (address_policy_ != AddressPolicy::USE_RESOLVABLE_ADDRESS &&
      address_policy_ != AddressPolicy::USE_NON_RESOLVABLE_ADDRESS) {
    LOG_ALWAYS_FATAL("Invalid address policy!");
    return;
  }

  schedule_rotate_random_address();
  set_random_address();
}

/* This function generates Resolvable Private Address (RPA) from Identity
 * Resolving Key |irk| and |prand|*/
hci::Address LeAddressManager::generate_rpa() {
+2 −0
Original line number Diff line number Diff line
@@ -96,6 +96,8 @@ class LeAddressManager {
  void unregister_client(LeAddressManagerCallback* callback);
  void prepare_to_rotate();
  void rotate_random_address();
  void schedule_rotate_random_address();
  void set_random_address();
  hci::Address generate_rpa();
  hci::Address generate_nrpa();
  std::chrono::milliseconds get_next_private_address_interval_ms();
+2 −6
Original line number Diff line number Diff line
@@ -76,10 +76,8 @@ class TestHciLayer : public HciLayer {
      auto result = command_future_->wait_for(std::chrono::milliseconds(1000));
      EXPECT_NE(std::future_status::timeout, result);
    }
    if (command_queue_.empty()) {
      return ConnectionManagementCommandView::Create(
          CommandPacketView::Create(PacketView<kLittleEndian>(std::make_shared<std::vector<uint8_t>>())));
    }
    ASSERT_LOG(
        !command_queue_.empty(), "Expecting command %s but command queue was empty", OpCodeText(op_code).c_str());
    CommandPacketView command_packet_view = GetLastCommand();
    EXPECT_TRUE(command_packet_view.IsValid());
    EXPECT_EQ(command_packet_view.GetOpCode(), op_code);
@@ -446,8 +444,6 @@ TEST_F(LeAddressManagerWithSingleClientTest, register_during_command_complete) {
  AllocateClients(1);
  test_hci_layer_->SetCommandFuture();
  le_address_manager_->Register(clients[1].get());
  test_hci_layer_->GetCommandPacket(OpCode::LE_SET_RANDOM_ADDRESS);
  test_hci_layer_->IncomingEvent(LeSetRandomAddressCompleteBuilder::Create(0x01, ErrorCode::SUCCESS));
  clients[0].get()->WaitForResume();
  clients[1].get()->WaitForResume();
}
Loading