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

Commit c55889c1 authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "[Connection Manager] Add LeAcceptlistCallbacks" am: b634177f

parents 6670de5f b634177f
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
#include "hci/acl_manager/acl_scheduler.h"
#include "hci/acl_manager/classic_impl.h"
#include "hci/acl_manager/connection_management_callbacks.h"
#include "hci/acl_manager/le_acceptlist_callbacks.h"
#include "hci/acl_manager/le_acl_connection.h"
#include "hci/acl_manager/le_impl.h"
#include "hci/acl_manager/round_robin_scheduler.h"
@@ -49,6 +50,7 @@ using acl_manager::ClassicAclConnection;
using acl_manager::ConnectionCallbacks;

using acl_manager::le_impl;
using acl_manager::LeAcceptlistCallbacks;
using acl_manager::LeAclConnection;
using acl_manager::LeConnectionCallbacks;

@@ -228,6 +230,14 @@ void AclManager::RegisterLeCallbacks(LeConnectionCallbacks* callbacks, os::Handl
      common::Unretained(handler));
}

void AclManager::RegisterLeAcceptlistCallbacks(LeAcceptlistCallbacks* callbacks) {
  ASSERT(callbacks != nullptr);
  CallOn(
      pimpl_->le_impl_,
      &le_impl::handle_register_le_acceptlist_callbacks,
      common::Unretained(callbacks));
}

void AclManager::UnregisterLeCallbacks(LeConnectionCallbacks* callbacks, std::promise<void> promise) {
  ASSERT(callbacks != nullptr);
  CallOn(pimpl_->le_impl_, &le_impl::handle_unregister_le_callbacks, common::Unretained(callbacks), std::move(promise));
+2 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@
#include "common/bidi_queue.h"
#include "common/callback.h"
#include "hci/acl_manager/connection_callbacks.h"
#include "hci/acl_manager/le_acceptlist_callbacks.h"
#include "hci/acl_manager/le_connection_callbacks.h"
#include "hci/address.h"
#include "hci/address_with_type.h"
@@ -78,6 +79,7 @@ public:
 // Should register only once when user module starts.
 virtual void RegisterLeCallbacks(acl_manager::LeConnectionCallbacks* callbacks, os::Handler* handler);
 virtual void UnregisterLeCallbacks(acl_manager::LeConnectionCallbacks* callbacks, std::promise<void> promise);
 void RegisterLeAcceptlistCallbacks(acl_manager::LeAcceptlistCallbacks* callbacks);

 // Generates OnConnectSuccess if connected, or OnConnectFail otherwise
 virtual void CreateConnection(Address address);
+44 −0
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.
 */

#pragma once

#include <memory>

#include "hci/acl_manager/le_acl_connection.h"
#include "hci/address_with_type.h"
#include "hci/hci_packets.h"
#include "os/handler.h"

namespace bluetooth {
namespace hci {
namespace acl_manager {

/// @brief These are callbacks needed to track the state of the acceptlist, used by the
/// Rust connection manager.
class LeAcceptlistCallbacks {
 public:
  virtual ~LeAcceptlistCallbacks() = default;
  // Invoked when controller sends Connection Complete event with Success error code
  // AddressWithType is the address returned by the controller.
  virtual void OnLeConnectSuccess(AddressWithType) = 0;
  // Invoked when the resolving list has changed, so we need to re-resolve our addresses.
  virtual void OnResolvingListChange() = 0;
};

}  // namespace acl_manager
}  // namespace hci
}  // namespace bluetooth
+22 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@
#include "common/init_flags.h"
#include "crypto_toolbox/crypto_toolbox.h"
#include "hci/acl_manager/assembler.h"
#include "hci/acl_manager/le_acceptlist_callbacks.h"
#include "hci/acl_manager/le_connection_management_callbacks.h"
#include "hci/acl_manager/round_robin_scheduler.h"
#include "hci/controller.h"
@@ -469,6 +470,9 @@ struct le_impl : public bluetooth::hci::LeAddressManagerCallback {
          common::Unretained(le_client_callbacks_),
          remote_address,
          std::move(connection)));
      if (le_acceptlist_callbacks_ != nullptr) {
        le_acceptlist_callbacks_->OnLeConnectSuccess(remote_address);
      }
    }
  }

@@ -626,6 +630,9 @@ struct le_impl : public bluetooth::hci::LeAddressManagerCallback {
          common::Unretained(le_client_callbacks_),
          remote_address,
          std::move(connection)));
      if (le_acceptlist_callbacks_ != nullptr) {
        le_acceptlist_callbacks_->OnLeConnectSuccess(remote_address);
      }
    }
  }

@@ -775,6 +782,9 @@ struct le_impl : public bluetooth::hci::LeAddressManagerCallback {
        conn_handle, DataAsPeripheral{adv_set_address, adv_set_id, is_discoverable});

    if (connection != nullptr) {
      if (le_acceptlist_callbacks_ != nullptr) {
        le_acceptlist_callbacks_->OnLeConnectSuccess(connection->GetRemoteAddress());
      }
      le_client_handler_->Post(common::BindOnce(
          &LeConnectionCallbacks::OnLeConnectSuccess,
          common::Unretained(le_client_callbacks_),
@@ -833,12 +843,18 @@ struct le_impl : public bluetooth::hci::LeAddressManagerCallback {
    register_with_address_manager();
    le_address_manager_->AddDeviceToResolvingList(
        address_with_type.ToPeerAddressType(), address_with_type.GetAddress(), peer_irk, local_irk);
    if (le_acceptlist_callbacks_ != nullptr) {
      le_acceptlist_callbacks_->OnResolvingListChange();
    }
  }

  void remove_device_from_resolving_list(AddressWithType address_with_type) {
    register_with_address_manager();
    le_address_manager_->RemoveDeviceFromResolvingList(
        address_with_type.ToPeerAddressType(), address_with_type.GetAddress());
    if (le_acceptlist_callbacks_ != nullptr) {
      le_acceptlist_callbacks_->OnResolvingListChange();
    }
  }

  void update_connectability_state_after_armed(const ErrorCode& status) {
@@ -1194,6 +1210,11 @@ struct le_impl : public bluetooth::hci::LeAddressManagerCallback {
    le_client_handler_ = handler;
  }

  void handle_register_le_acceptlist_callbacks(LeAcceptlistCallbacks* callbacks) {
    ASSERT(le_acceptlist_callbacks_ == nullptr);
    le_acceptlist_callbacks_ = callbacks;
  }

  void handle_unregister_le_callbacks(LeConnectionCallbacks* callbacks, std::promise<void> promise) {
    ASSERT_LOG(le_client_callbacks_ == callbacks, "Registered le callback entity is different then unregister request");
    le_client_callbacks_ = nullptr;
@@ -1312,6 +1333,7 @@ struct le_impl : public bluetooth::hci::LeAddressManagerCallback {
  LeAclConnectionInterface* le_acl_connection_interface_ = nullptr;
  LeConnectionCallbacks* le_client_callbacks_ = nullptr;
  os::Handler* le_client_handler_ = nullptr;
  LeAcceptlistCallbacks* le_acceptlist_callbacks_ = nullptr;
  std::unordered_set<AddressWithType> connecting_le_{};
  bool arm_on_resume_{};
  std::unordered_set<AddressWithType> direct_connections_{};
+58 −1
Original line number Diff line number Diff line
@@ -405,6 +405,12 @@ class MockLeConnectionCallbacks : public LeConnectionCallbacks {
      void, OnLeConnectFail, (AddressWithType address_with_type, ErrorCode reason), (override));
};

class MockLeAcceptlistCallbacks : public LeAcceptlistCallbacks {
 public:
  MOCK_METHOD(void, OnLeConnectSuccess, (AddressWithType address_with_type), (override));
  MOCK_METHOD(void, OnResolvingListChange, (), (override));
};

class MockLeConnectionManagementCallbacks : public LeConnectionManagementCallbacks {
 public:
  MOCK_METHOD(
@@ -542,7 +548,6 @@ class LeImplTest : public ::testing::Test {

  Address remote_address_;
  AddressWithType fixed_address_;
  AddressWithType remote_public_address_;
  Address local_rpa_;
  Address remote_rpa_;
  AddressWithType remote_public_address_with_type_;
@@ -1669,6 +1674,58 @@ INSTANTIATE_TEST_SUITE_P(
    LeImplTestParameterizedByDiscoverability,
    ::testing::Values(false, true));

TEST_F(LeImplTest, ConnectionCompleteAcceptlistCallback) {
  // arrange
  MockLeAcceptlistCallbacks callbacks;
  le_impl_->handle_register_le_acceptlist_callbacks(&callbacks);
  set_random_device_address_policy();

  // expect
  AddressWithType remote_address;
  EXPECT_CALL(callbacks, OnLeConnectSuccess(_)).WillOnce([&](AddressWithType addr) {
    remote_address = addr;
  });

  // act
  auto command = LeEnhancedConnectionCompleteBuilder::Create(
      ErrorCode::SUCCESS,
      kHciHandle,
      Role::PERIPHERAL,
      AddressType::PUBLIC_DEVICE_ADDRESS,
      remote_address_,
      local_rpa_,
      remote_rpa_,
      0x0024,
      0x0000,
      0x0011,
      ClockAccuracy::PPM_30);
  auto bytes = Serialize<LeEnhancedConnectionCompleteBuilder>(std::move(command));
  auto view = CreateLeEventView<hci::LeEnhancedConnectionCompleteView>(bytes);
  ASSERT_TRUE(view.IsValid());
  le_impl_->on_le_event(view);
  sync_handler();

  // assert
  ASSERT_EQ(remote_public_address_with_type_, remote_address);
}

TEST_F(LeImplTest, ResolvingListCallback) {
  // arrange
  MockLeAcceptlistCallbacks callbacks;
  le_impl_->handle_register_le_acceptlist_callbacks(&callbacks);

  // expect
  AddressWithType remote_address;
  EXPECT_CALL(callbacks, OnResolvingListChange()).Times(1);

  // act
  le_impl_->add_device_to_resolving_list(
      remote_public_address_with_type_, kPeerIdentityResolvingKey, kLocalIdentityResolvingKey);

  // assert
  Mock::VerifyAndClearExpectations(&callbacks);
}

}  // namespace acl_manager
}  // namespace hci
}  // namespace bluetooth