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

Commit e89bcbf4 authored by Chris Manton's avatar Chris Manton
Browse files

gd_acl_shim: Start schedule address rotation on first client registration

Set random address when privacy policy is registered.
Do not rotate address on every registration.

Bug: 171568335
Test: CtsVerifier
Test: gd/cert/run --host
Test: atest --host bluetooth_test_gd
Tag: #refactor
Change-Id: Ie8b8e8a111763dc62a0be9a38eac4b211fe50e17
parent 08d726db
Loading
Loading
Loading
Loading
+8 −4
Original line number Diff line number Diff line
@@ -318,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);
@@ -337,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));
  }

@@ -1234,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(
@@ -1251,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));
  }
};

+43 −6
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"

@@ -66,6 +83,7 @@ void LeAddressManager::SetPrivacyPolicyForInitiatorAddress(
      minimum_rotation_time_ = minimum_rotation_time;
      maximum_rotation_time_ = maximum_rotation_time;
      address_rotation_alarm_ = std::make_unique<os::Alarm>(handler_);
      set_random_address();
      break;
    case AddressPolicy::POLICY_NOT_SET:
      LOG_ALWAYS_FATAL("invalid parameters");
@@ -129,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();
@@ -215,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();
@@ -237,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();
+0 −2
Original line number Diff line number Diff line
@@ -444,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();
}