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

Commit cfde5f3a authored by Myles Watson's avatar Myles Watson Committed by Gerrit Code Review
Browse files

Merge changes I8980eeb6,I62cefd36

* changes:
  HCI: Sync reactor in hci_layer_unittest
  HCI: Add fake HAL for testing
parents 1088ff5f 4916e4fa
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -54,6 +54,13 @@ filegroup {
    ],
}

filegroup {
    name: "BluetoothHalFake",
    srcs: [
        "hci_hal_fake.cc",
    ],
}

filegroup {
    name: "BluetoothFacade_hci_hal",
    srcs: [
+94 −0
Original line number Diff line number Diff line
/*
 * Copyright 2023 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 "hal/hci_hal_fake.h"

namespace bluetooth {
namespace hal {

void TestHciHal::sendHciCommand(hal::HciPacket command) {
  outgoing_commands_.push(std::move(command));
}

void TestHciHal::sendAclData(hal::HciPacket data) {
  outgoing_acl_.push(std::move(data));
}

void TestHciHal::sendScoData(hal::HciPacket data) {
  outgoing_sco_.push(std::move(data));
}

void TestHciHal::sendIsoData(hal::HciPacket data) {
  outgoing_iso_.push(std::move(data));
}

packet::PacketView<packet::kLittleEndian> TestHciHal::GetPacketView(hal::HciPacket data) {
  auto shared = std::make_shared<std::vector<uint8_t>>(data);
  return packet::PacketView<packet::kLittleEndian>(shared);
}

std::optional<hci::CommandView> TestHciHal::GetSentCommand(std::chrono::milliseconds timeout) {
  if (!outgoing_commands_.wait_to_take(timeout)) {
    // Timed out
    return {};
  }
  auto command = hci::CommandView::Create(GetPacketView(std::move(outgoing_commands_.take())));
  ASSERT(command.IsValid());
  return command;
}

std::optional<hci::AclView> TestHciHal::GetSentAcl(std::chrono::milliseconds timeout) {
  if (!outgoing_acl_.wait_to_take(timeout)) {
    // Timed out
    return {};
  }
  auto acl = hci::AclView::Create(GetPacketView(std::move(outgoing_acl_.take())));
  ASSERT(acl.IsValid());
  return acl;
}

std::optional<hci::ScoView> TestHciHal::GetSentSco(std::chrono::milliseconds timeout) {
  if (!outgoing_commands_.wait_to_take(timeout)) {
    // Timed out
    return {};
  }
  auto sco = hci::ScoView::Create(GetPacketView(std::move(outgoing_sco_.take())));
  ASSERT(sco.IsValid());
  return sco;
}

std::optional<hci::IsoView> TestHciHal::GetSentIso(std::chrono::milliseconds timeout) {
  if (!outgoing_commands_.wait_to_take(timeout)) {
    // Timed out
    return {};
  }
  ASSERT(outgoing_iso_.wait_to_take(timeout));
  auto iso = hci::IsoView::Create(GetPacketView(std::move(outgoing_iso_.take())));
  ASSERT(iso.IsValid());
  return iso;
}

void TestHciHal::InjectEvent(std::unique_ptr<packet::BasePacketBuilder> event) {
  ASSERT(callbacks != nullptr);
  auto view = std::vector<uint8_t>();
  packet::BitInserter bi{view};
  event->Serialize(bi);
  callbacks->hciEventReceived(view);
}

const ModuleFactory TestHciHal::Factory = ModuleFactory([]() { return new TestHciHal(); });
}  // namespace hal
}  // namespace bluetooth
+95 −0
Original line number Diff line number Diff line
/*
 * Copyright 2023 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 <future>
#include <list>
#include <optional>

#include "common/blocking_queue.h"
#include "hal/hci_hal.h"
#include "hci/hci_packets.h"
#include "packet/packet_view.h"

namespace bluetooth {
namespace hal {

class TestHciHal : public hal::HciHal {
 public:
  TestHciHal() : hal::HciHal() {}

  ~TestHciHal() {
    if (callbacks != nullptr) {
      LOG_ALWAYS_FATAL("unregisterIncomingPacketCallback() must be called");
    }
  }

  void registerIncomingPacketCallback(hal::HciHalCallbacks* callback) override {
    callbacks = callback;
  }

  void unregisterIncomingPacketCallback() override {
    callbacks = nullptr;
  }

  void sendHciCommand(hal::HciPacket command) override;

  void sendAclData(hal::HciPacket data) override;

  void sendScoData(hal::HciPacket data) override;

  void sendIsoData(hal::HciPacket data) override;

  hal::HciHalCallbacks* callbacks = nullptr;

  packet::PacketView<packet::kLittleEndian> GetPacketView(hal::HciPacket data);

  std::optional<hci::CommandView> GetSentCommand(
      std::chrono::milliseconds timeout = std::chrono::seconds(1));

  std::optional<hci::AclView> GetSentAcl(
      std::chrono::milliseconds timeout = std::chrono::seconds(1));

  std::optional<hci::ScoView> GetSentSco(
      std::chrono::milliseconds timeout = std::chrono::seconds(1));

  std::optional<hci::IsoView> GetSentIso(
      std::chrono::milliseconds timeout = std::chrono::seconds(1));

  void InjectEvent(std::unique_ptr<packet::BasePacketBuilder> event);

  void Start() {}

  void Stop() {}

  void ListDependencies(ModuleList*) const {}

  std::string ToString() const override {
    return std::string("TestHciHal");
  }

  static const ModuleFactory Factory;

 private:
  common::BlockingQueue<hal::HciPacket> outgoing_commands_;
  common::BlockingQueue<hal::HciPacket> outgoing_acl_;
  common::BlockingQueue<hal::HciPacket> outgoing_sco_;
  common::BlockingQueue<hal::HciPacket> outgoing_iso_;
};

}  // namespace hal
}  // namespace bluetooth
+1 −0
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ filegroup {
filegroup {
    name: "BluetoothHciUnitTestSources",
    srcs: [
        ":BluetoothHalFake",
        "acl_builder_test.cc",
        "acl_manager_test.cc",
        "acl_manager_unittest.cc",
+132 −430

File changed.

Preview size limit exceeded, changes collapsed.

Loading