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

Commit 42967d08 authored by Myles Watson's avatar Myles Watson
Browse files

Add a pure virtual HCI layer for tests

Bug: 322230000
Test: atest net_test_btm_iso
Flag: EXEMPT, test-only change
Change-Id: Ic5976c8604a7203fc36068456ab122f4371779ab
parent 82e31399
Loading
Loading
Loading
Loading
+132 −0
Original line number Diff line number Diff line
/*
 * Copyright 2024 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 <utility>

#include "common/bidi_queue.h"
#include "common/contextual_callback.h"
#include "hci/acl_connection_interface.h"
#include "hci/address.h"
#include "hci/class_of_device.h"
#include "hci/distance_measurement_interface.h"
#include "hci/hci_packets.h"
#include "hci/le_acl_connection_interface.h"
#include "hci/le_advertising_interface.h"
#include "hci/le_iso_interface.h"
#include "hci/le_scanning_interface.h"
#include "hci/le_security_interface.h"
#include "hci/security_interface.h"

namespace bluetooth {
namespace hci {

class HciInterface : public CommandInterface<CommandBuilder> {
 public:
  HciInterface() = default;
  virtual ~HciInterface() = default;

  virtual void EnqueueCommand(
      std::unique_ptr<CommandBuilder> command,
      common::ContextualOnceCallback<void(CommandCompleteView)> on_complete) override = 0;

  virtual void EnqueueCommand(
      std::unique_ptr<CommandBuilder> command,
      common::ContextualOnceCallback<void(CommandStatusView)> on_status) override = 0;

  virtual common::BidiQueueEnd<AclBuilder, AclView>* GetAclQueueEnd() = 0;

  virtual common::BidiQueueEnd<ScoBuilder, ScoView>* GetScoQueueEnd() = 0;

  virtual common::BidiQueueEnd<IsoBuilder, IsoView>* GetIsoQueueEnd() = 0;

  virtual void RegisterEventHandler(
      EventCode event_code, common::ContextualCallback<void(EventView)> event_handler) = 0;

  virtual void UnregisterEventHandler(EventCode event_code) = 0;

  virtual void RegisterLeEventHandler(
      SubeventCode subevent_code,
      common::ContextualCallback<void(LeMetaEventView)> event_handler) = 0;

  virtual void UnregisterLeEventHandler(SubeventCode subevent_code) = 0;

  virtual void RegisterForDisconnects(
      common::ContextualCallback<void(uint16_t, hci::ErrorCode)> on_disconnect) = 0;

  virtual SecurityInterface* GetSecurityInterface(
      common::ContextualCallback<void(EventView)> event_handler) = 0;

  virtual LeSecurityInterface* GetLeSecurityInterface(
      common::ContextualCallback<void(LeMetaEventView)> event_handler) = 0;

  virtual AclConnectionInterface* GetAclConnectionInterface(
      common::ContextualCallback<void(EventView)> event_handler,
      common::ContextualCallback<void(uint16_t, hci::ErrorCode)> on_disconnect,
      common::ContextualCallback<void(Address, ClassOfDevice)> on_connection_request,
      common::ContextualCallback<void(hci::ErrorCode, uint16_t, uint8_t, uint16_t, uint16_t)>
          on_read_remote_version_complete) = 0;
  virtual void PutAclConnectionInterface() = 0;

  virtual LeAclConnectionInterface* GetLeAclConnectionInterface(
      common::ContextualCallback<void(LeMetaEventView)> event_handler,
      common::ContextualCallback<void(uint16_t, hci::ErrorCode)> on_disconnect,
      common::ContextualCallback<void(hci::ErrorCode, uint16_t, uint8_t, uint16_t, uint16_t)>
          on_read_remote_version_complete) = 0;
  virtual void PutLeAclConnectionInterface() = 0;

  virtual LeAdvertisingInterface* GetLeAdvertisingInterface(
      common::ContextualCallback<void(LeMetaEventView)> event_handler) = 0;

  virtual LeScanningInterface* GetLeScanningInterface(
      common::ContextualCallback<void(LeMetaEventView)> event_handler) = 0;

  virtual void RegisterForScoConnectionRequests(
      common::ContextualCallback<void(Address, ClassOfDevice, ConnectionRequestLinkType)>
          on_sco_connection_request) = 0;

  virtual LeIsoInterface* GetLeIsoInterface(
      common::ContextualCallback<void(LeMetaEventView)> event_handler) = 0;

  virtual DistanceMeasurementInterface* GetDistanceMeasurementInterface(
      common::ContextualCallback<void(LeMetaEventView)> event_handler) = 0;

 protected:
  template <typename T>
  class CommandInterfaceImpl : public CommandInterface<T> {
   public:
    explicit CommandInterfaceImpl(HciInterface& hci) : hci_(hci) {}
    virtual ~CommandInterfaceImpl() = default;

    void EnqueueCommand(
        std::unique_ptr<T> command,
        common::ContextualOnceCallback<void(CommandCompleteView)> on_complete) override {
      hci_.EnqueueCommand(std::move(command), std::move(on_complete));
    }

    void EnqueueCommand(
        std::unique_ptr<T> command,
        common::ContextualOnceCallback<void(CommandStatusView)> on_status) override {
      hci_.EnqueueCommand(std::move(command), std::move(on_status));
    }
    HciInterface& hci_;
  };
};

}  // namespace hci
}  // namespace bluetooth
+3 −1
Original line number Diff line number Diff line
@@ -19,17 +19,19 @@
#ifdef TARGET_FLOSS
#include <signal.h>
#endif
#include <map>
#include <utility>

#include "common/bind.h"
#include "common/init_flags.h"
#include "common/stop_watch.h"
#include "hal/hci_hal.h"
#include "hci/class_of_device.h"
#include "hci/hci_metrics_logging.h"
#include "os/alarm.h"
#include "os/metrics.h"
#include "os/queue.h"
#include "osi/include/stack_power_telemetry.h"
#include "packet/packet_builder.h"
#include "storage/storage_module.h"

namespace bluetooth {
+2 −24
Original line number Diff line number Diff line
@@ -18,19 +18,16 @@

#include <chrono>
#include <list>
#include <map>
#include <memory>
#include <string>
#include <utility>

#include "address.h"
#include "class_of_device.h"
#include "common/bidi_queue.h"
#include "common/callback.h"
#include "common/contextual_callback.h"
#include "hal/hci_hal.h"
#include "hci/acl_connection_interface.h"
#include "hci/distance_measurement_interface.h"
#include "hci/hci_interface.h"
#include "hci/hci_packets.h"
#include "hci/le_acl_connection_interface.h"
#include "hci/le_advertising_interface.h"
@@ -40,12 +37,11 @@
#include "hci/security_interface.h"
#include "module.h"
#include "os/handler.h"
#include "os/utils.h"

namespace bluetooth {
namespace hci {

class HciLayer : public Module, public CommandInterface<CommandBuilder> {
class HciLayer : public Module, public HciInterface {
  // LINT.IfChange
 public:
  HciLayer();
@@ -150,24 +146,6 @@ class HciLayer : public Module, public CommandInterface<CommandBuilder> {
  impl* impl_;
  hal_callbacks* hal_callbacks_;

  template <typename T>
  class CommandInterfaceImpl : public CommandInterface<T> {
   public:
    explicit CommandInterfaceImpl(HciLayer& hci) : hci_(hci) {}
    ~CommandInterfaceImpl() = default;

    void EnqueueCommand(std::unique_ptr<T> command,
                        common::ContextualOnceCallback<void(CommandCompleteView)> on_complete) override {
      hci_.EnqueueCommand(std::move(command), std::move(on_complete));
    }

    void EnqueueCommand(std::unique_ptr<T> command,
                        common::ContextualOnceCallback<void(CommandStatusView)> on_status) override {
      hci_.EnqueueCommand(std::move(command), std::move(on_status));
    }
    HciLayer& hci_;
  };

  std::mutex callback_handlers_guard_;
  void on_connection_request(EventView event_view);
  void on_disconnection_complete(EventView event_view);
+13 −4
Original line number Diff line number Diff line
@@ -21,17 +21,15 @@

#include "common/contextual_callback.h"
#include "hci/address.h"
#include "hci/hci_layer.h"
#include "hci/hci_interface.h"
#include "hci/hci_packets.h"
#include "module.h"
#include "os/handler.h"

// Unit test interfaces
namespace bluetooth {
namespace hci {
namespace testing {

class MockHciLayer : public HciLayer {
class MockHciLayer : public HciInterface {
 public:
  MOCK_METHOD(
      void,
@@ -64,6 +62,11 @@ class MockHciLayer : public HciLayer {
      (common::ContextualCallback<void(EventView)> event_handler),
      (override));

  MOCK_METHOD(
      (void),
      RegisterForDisconnects,
      (common::ContextualCallback<void(uint16_t, hci::ErrorCode)> on_disconnect));

  MOCK_METHOD(
      (LeSecurityInterface*),
      GetLeSecurityInterface,
@@ -114,6 +117,12 @@ class MockHciLayer : public HciLayer {
      GetDistanceMeasurementInterface,
      (common::ContextualCallback<void(LeMetaEventView)> event_handler),
      (override));

  MOCK_METHOD(
      void,
      RegisterForScoConnectionRequests,
      (common::ContextualCallback<void(Address, ClassOfDevice, ConnectionRequestLinkType)>
           on_sco_connection_request));
};

}  // namespace testing
+1 −1
Original line number Diff line number Diff line
@@ -76,7 +76,7 @@ neighbor::InquiryModule* GetInquiry() {
      ->GetInstance<neighbor::InquiryModule>();
}

hci::HciLayer* GetHciLayer() {
hci::HciInterface* GetHciLayer() {
  return Stack::GetInstance()->GetStackManager()->GetInstance<hci::HciLayer>();
}

Loading