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

Commit 01b62d81 authored by Zach Johnson's avatar Zach Johnson
Browse files

Rework DynamicChannels to use ContextualCallbacks

Test: cert/run --host
Test: atest bluetooth_test_gd
Bug: 156859507
Tag: #gd-refactor
Change-Id: Ib3e33f38333c4a1a481df9b706858dd9f306bb79
parent 436d6ba6
Loading
Loading
Loading
Loading
+17 −20
Original line number Diff line number Diff line
@@ -24,38 +24,35 @@ namespace bluetooth {
namespace l2cap {
namespace classic {

bool DynamicChannelManager::ConnectChannel(hci::Address device, DynamicChannelConfigurationOption configuration_option,
                                           Psm psm, OnConnectionOpenCallback on_connection_open,
                                           OnConnectionFailureCallback on_fail_callback, os::Handler* handler) {
  internal::Link::PendingDynamicChannelConnection pending_dynamic_channel_connection{
      .handler_ = handler,
void DynamicChannelManager::ConnectChannel(
    hci::Address device,
    DynamicChannelConfigurationOption configuration_option,
    Psm psm,
    OnConnectionOpenCallback on_connection_open,
    OnConnectionFailureCallback on_fail_callback) {
  internal::Link::PendingDynamicChannelConnection pending_connection{
      .on_open_callback_ = std::move(on_connection_open),
      .on_fail_callback_ = std::move(on_fail_callback),
      .configuration_ = configuration_option,
  };
  l2cap_layer_handler_->Post(common::BindOnce(&internal::LinkManager::ConnectDynamicChannelServices,
                                              common::Unretained(link_manager_), device,
                                              std::move(pending_dynamic_channel_connection), psm));

  return true;
  l2cap_layer_handler_->CallOn(
      link_manager_, &internal::LinkManager::ConnectDynamicChannelServices, device, std::move(pending_connection), psm);
}

bool DynamicChannelManager::RegisterService(Psm psm, DynamicChannelConfigurationOption configuration_option,
void DynamicChannelManager::RegisterService(
    Psm psm,
    DynamicChannelConfigurationOption configuration_option,
    const classic::SecurityPolicy& security_policy,
    OnRegistrationCompleteCallback on_registration_complete,
                                            OnConnectionOpenCallback on_connection_open, os::Handler* handler) {
    OnConnectionOpenCallback on_connection_open) {
  internal::DynamicChannelServiceImpl::PendingRegistration pending_registration{
      .user_handler_ = handler,
      .security_policy_ = security_policy,
      .on_registration_complete_callback_ = std::move(on_registration_complete),
      .on_connection_open_callback_ = std::move(on_connection_open),
      .configuration_ = configuration_option,
  };
  l2cap_layer_handler_->Post(common::BindOnce(&internal::DynamicChannelServiceManagerImpl::Register,
                                              common::Unretained(service_manager_), psm,
                                              std::move(pending_registration)));

  return true;
  l2cap_layer_handler_->CallOn(
      service_manager_, &internal::DynamicChannelServiceManagerImpl::Register, psm, std::move(pending_registration));
}

}  // namespace classic
+17 −23
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@

#include <string>

#include "common/contextual_callback.h"
#include "hci/acl_manager.h"
#include "hci/address.h"
#include "l2cap/classic/dynamic_channel.h"
@@ -54,15 +55,10 @@ class DynamicChannelManager {
    hci::ErrorCode hci_error = hci::ErrorCode::SUCCESS;
    ConnectionResponseResult l2cap_connection_response_result = ConnectionResponseResult::SUCCESS;
  };
  /**
   * OnConnectionFailureCallback(std::string failure_reason);
   */
  using OnConnectionFailureCallback = common::OnceCallback<void(ConnectionResult result)>;

  /**
   * OnConnectionOpenCallback(DynamicChannel channel);
   */
  using OnConnectionOpenCallback = common::Callback<void(std::unique_ptr<DynamicChannel>)>;
  using OnConnectionFailureCallback = common::ContextualOnceCallback<void(ConnectionResult result)>;

  using OnConnectionOpenCallback = common::ContextualCallback<void(std::unique_ptr<DynamicChannel>)>;

  enum class RegistrationResult {
    SUCCESS = 0,
@@ -70,11 +66,8 @@ class DynamicChannelManager {
    FAIL_INVALID_SERVICE = 2,    // Invalid PSM
  };

  /**
   * OnRegistrationFailureCallback(RegistrationResult result, DynamicChannelService service);
   */
  using OnRegistrationCompleteCallback =
      common::OnceCallback<void(RegistrationResult, std::unique_ptr<DynamicChannelService>)>;
      common::ContextualOnceCallback<void(RegistrationResult, std::unique_ptr<DynamicChannelService>)>;

  /**
   * Connect to a Dynamic channel on a remote device
@@ -91,14 +84,14 @@ class DynamicChannelManager {
   * @param psm: Service PSM to connect. PSM is defined in Core spec Vol 3 Part A 4.2.
   * @param on_open_callback: A callback to indicate success of a connection initiated from a remote device.
   * @param on_fail_callback: A callback to indicate connection failure along with a status code.
   * @param handler: The handler context in which to execute the @callback parameters.
   * @param configuration_option: The configuration options for this channel
   *
   * Returns: true if connection was able to be initiated, false otherwise.
   */
  virtual bool ConnectChannel(hci::Address device, DynamicChannelConfigurationOption configuration_option, Psm psm,
                              OnConnectionOpenCallback on_connection_open, OnConnectionFailureCallback on_fail_callback,
                              os::Handler* handler);
  virtual void ConnectChannel(
      hci::Address device,
      DynamicChannelConfigurationOption configuration_option,
      Psm psm,
      OnConnectionOpenCallback on_connection_open,
      OnConnectionFailureCallback on_fail_callback);

  /**
   * Register a service to receive incoming connections bound to a specific channel.
@@ -118,13 +111,14 @@ class DynamicChannelManager {
   * @param on_registration_complete: A callback to indicate the service setup has completed. If the return status is
   *        not SUCCESS, it means service is not registered due to reasons like PSM already take
   * @param on_open_callback: A callback to indicate success of a connection initiated from a remote device.
   * @param handler: The handler context in which to execute the @callback parameter.
   * @param configuration_option: The configuration options for this channel
   */
  virtual bool RegisterService(Psm psm, DynamicChannelConfigurationOption configuration_option,
  virtual void RegisterService(
      Psm psm,
      DynamicChannelConfigurationOption configuration_option,
      const SecurityPolicy& security_policy,
      OnRegistrationCompleteCallback on_registration_complete,
                               OnConnectionOpenCallback on_connection_open, os::Handler* handler);
      OnConnectionOpenCallback on_connection_open);

  friend class L2capClassicModule;

+3 −4
Original line number Diff line number Diff line
@@ -22,11 +22,10 @@ namespace bluetooth {
namespace l2cap {
namespace classic {

void DynamicChannelService::Unregister(OnUnregisteredCallback on_unregistered, os::Handler* on_unregistered_handler) {
void DynamicChannelService::Unregister(OnUnregisteredCallback on_unregistered) {
  ASSERT_LOG(manager_ != nullptr, "this service is invalid");
  l2cap_layer_handler_->Post(common::BindOnce(&internal::DynamicChannelServiceManagerImpl::Unregister,
                                              common::Unretained(manager_), psm_, std::move(on_unregistered),
                                              on_unregistered_handler));
  l2cap_layer_handler_->CallOn(
      manager_, &internal::DynamicChannelServiceManagerImpl::Unregister, psm_, std::move(on_unregistered));
}

Psm DynamicChannelService::GetPsm() const {
+3 −3
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@

#pragma once

#include "common/callback.h"
#include "common/contextual_callback.h"
#include "l2cap/psm.h"
#include "os/handler.h"
#include "os/log.h"
@@ -33,7 +33,7 @@ class DynamicChannelService {
 public:
  DynamicChannelService() = default;

  using OnUnregisteredCallback = common::OnceCallback<void()>;
  using OnUnregisteredCallback = common::ContextualOnceCallback<void()>;

  /**
   * Unregister a service from L2CAP module. This operation cannot fail.
@@ -41,7 +41,7 @@ class DynamicChannelService {
   *
   * @param on_unregistered will be triggered when unregistration is complete
   */
  void Unregister(OnUnregisteredCallback on_unregistered, os::Handler* on_unregistered_handler);
  void Unregister(OnUnregisteredCallback on_unregistered);

  friend internal::DynamicChannelServiceManagerImpl;

+11 −9
Original line number Diff line number Diff line
@@ -138,10 +138,11 @@ class L2capClassicModuleFacadeService : public L2capClassicModuleFacade::Service
      DynamicChannelConfigurationOption configuration_option = {};
      configuration_option.channel_mode = (DynamicChannelConfigurationOption::RetransmissionAndFlowControlMode)mode;
      dynamic_channel_manager_->RegisterService(
          psm, configuration_option, SecurityPolicy::_SDP_ONLY_NO_SECURITY_WHATSOEVER_PLAINTEXT_TRANSPORT_OK,
          common::BindOnce(&L2capDynamicChannelHelper::on_l2cap_service_registration_complete,
                           common::Unretained(this)),
          common::Bind(&L2capDynamicChannelHelper::on_connection_open, common::Unretained(this)), handler_);
          psm,
          configuration_option,
          SecurityPolicy::_SDP_ONLY_NO_SECURITY_WHATSOEVER_PLAINTEXT_TRANSPORT_OK,
          handler_->BindOnceOn(this, &L2capDynamicChannelHelper::on_l2cap_service_registration_complete),
          handler_->BindOn(this, &L2capDynamicChannelHelper::on_connection_open));
    }

    ~L2capDynamicChannelHelper() {
@@ -156,9 +157,11 @@ class L2capClassicModuleFacadeService : public L2capClassicModuleFacade::Service
      configuration_option.channel_mode = (DynamicChannelConfigurationOption::RetransmissionAndFlowControlMode)mode_;

      dynamic_channel_manager_->ConnectChannel(
          address, configuration_option, psm_,
          common::Bind(&L2capDynamicChannelHelper::on_connection_open, common::Unretained(this)),
          common::Bind(&L2capDynamicChannelHelper::on_connect_fail, common::Unretained(this)), handler_);
          address,
          configuration_option,
          psm_,
          handler_->BindOn(this, &L2capDynamicChannelHelper::on_connection_open),
          handler_->BindOnceOn(this, &L2capDynamicChannelHelper::on_connect_fail));
      std::unique_lock<std::mutex> lock(channel_open_cv_mutex_);
      if (!channel_open_cv_.wait_for(lock, std::chrono::seconds(2), [this] { return channel_ != nullptr; })) {
        LOG_WARN("Channel is not open for psm %d", psm_);
@@ -190,8 +193,7 @@ class L2capClassicModuleFacadeService : public L2capClassicModuleFacade::Service
      }
      channel_open_cv_.notify_all();
      channel_->RegisterOnCloseCallback(
          facade_service_->facade_handler_,
          common::BindOnce(&L2capDynamicChannelHelper::on_close_callback, common::Unretained(this)));
          facade_service_->facade_handler_->BindOnceOn(this, &L2capDynamicChannelHelper::on_close_callback));
      dequeue_registered_ = true;
      channel_->GetQueueUpEnd()->RegisterDequeue(
          facade_service_->facade_handler_,
Loading