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

Commit b9f8f996 authored by David Duarte's avatar David Duarte
Browse files

Remove dependency to libbluetoothtbd_hal

Bug: 263885771
Test: atest net_test_bluetooth
Change-Id: I5466e8d7fc9f870bb4160afd3129a4f5ff62e389
parent e4c993bb
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -69,7 +69,6 @@ cc_defaults {
        "libaudio-a2dp-hw-utils",
        "libbluetooth-dumpsys",
        "libbluetooth-types",
        "libbluetoothtbd_hal",
        "libbt-audio-hal-interface",
        "libbt-bta",
        "libbt-bta-core",
+3 −5
Original line number Diff line number Diff line
@@ -176,13 +176,11 @@ TEST_F(BluetoothTest, AdapterCleanupDuringDiscovery) {
  EXPECT_EQ(GetState(), BT_STATE_OFF)
      << "Test should be run with Adapter disabled";

  bt_callbacks_t* bt_callbacks =
      bluetooth::hal::BluetoothInterface::Get()->GetHALCallbacks();
  ASSERT_TRUE(bt_callbacks != nullptr);
  bt_callbacks_t* callbacks = bt_callbacks();
  ASSERT_TRUE(callbacks != nullptr);

  for (int i = 0; i < kTestRepeatCount; ++i) {
    bt_interface()->init(bt_callbacks, false, false, 0, nullptr, false,
                         nullptr);
    bt_interface()->init(callbacks, false, false, 0, nullptr, false, nullptr);
    EXPECT_EQ(bt_interface()->enable(), BT_STATUS_SUCCESS);
    semaphore_wait(adapter_state_changed_callback_sem_);
    EXPECT_EQ(GetState(), BT_STATE_ON) << "Adapter did not turn on.";
+56 −59
Original line number Diff line number Diff line
@@ -26,13 +26,7 @@
#include "btcore/include/property.h"
#include "types/raw_address.h"

namespace {

// Mutex lock used by callbacks to protect |callback_semaphores_| from
// racey behaviour caused when Wait and Notify are called at the same time
std::mutex callback_lock;

}  // namespace
extern bt_interface_t bluetoothInterface;

void semaphore_wait(btsemaphore &s) {
  s.wait();
@@ -46,9 +40,51 @@ void semaphore_try_wait(btsemaphore &s) {

namespace bttest {

static BluetoothTest* instance = nullptr;

void AdapterStateChangedCallback(bt_state_t new_state) {
  instance->state_ = new_state;
  semaphore_post(instance->adapter_state_changed_callback_sem_);
}

void AdapterPropertiesCallback(bt_status_t status, int num_properties,
                               bt_property_t* new_properties) {
  property_free_array(instance->last_changed_properties_,
                      instance->properties_changed_count_);
  instance->last_changed_properties_ =
      property_copy_array(new_properties, num_properties);
  instance->properties_changed_count_ = num_properties;
  semaphore_post(instance->adapter_properties_callback_sem_);
}

void RemoteDevicePropertiesCallback(bt_status_t status,
                                    RawAddress* remote_bd_addr,
                                    int num_properties,
                                    bt_property_t* properties) {
  instance->curr_remote_device_ = *remote_bd_addr;
  property_free_array(instance->remote_device_last_changed_properties_,
                      instance->remote_device_properties_changed_count_);
  instance->remote_device_last_changed_properties_ =
      property_copy_array(properties, num_properties);
  instance->remote_device_properties_changed_count_ = num_properties;
  semaphore_post(instance->remote_device_properties_callback_sem_);
}

void DiscoveryStateChangedCallback(bt_discovery_state_t state) {
  instance->discovery_state_ = state;
  semaphore_post(instance->discovery_state_changed_callback_sem_);
}

static bt_callbacks_t callbacks = {
    .size = sizeof(bt_callbacks_t),
    .adapter_state_changed_cb = AdapterStateChangedCallback,
    .adapter_properties_cb = AdapterPropertiesCallback,
    .remote_device_properties_cb = RemoteDevicePropertiesCallback,
    .discovery_state_changed_cb = DiscoveryStateChangedCallback,
};

void BluetoothTest::SetUp() {
  android::ProcessState::self()->startThreadPool();
  bt_interface_ = nullptr;
  state_ = BT_STATE_OFF;
  properties_changed_count_ = 0;
  last_changed_properties_ = nullptr;
@@ -61,21 +97,15 @@ void BluetoothTest::SetUp() {
  remove("/data/misc/bluedroid/bt_config.conf.encrypted-checksum");
  remove("/data/misc/bluedroid/bt_config.bak.encrypted-checksum");

  bluetooth::hal::BluetoothInterface::Initialize();
  ASSERT_TRUE(bluetooth::hal::BluetoothInterface::IsInitialized());

  auto bt_hal_interface = bluetooth::hal::BluetoothInterface::Get();
  bt_hal_interface->AddObserver(this);
  bt_interface_ = bt_hal_interface->GetHALInterface();
  ASSERT_NE(nullptr, bt_interface_) << "bt_interface is null.";
  instance = this;
  int status = bluetoothInterface.init(&callbacks, false, false, 0, nullptr,
                                       false, nullptr);
  ASSERT_EQ(status, BT_STATUS_SUCCESS);
}

void BluetoothTest::TearDown() {

  auto bt_hal_interface = bluetooth::hal::BluetoothInterface::Get();
  bt_hal_interface->RemoveObserver(this);
  bt_hal_interface->CleanUp();
  ASSERT_FALSE(bt_hal_interface->IsInitialized());
  bluetoothInterface.cleanup();
  instance = nullptr;
}

void BluetoothTest::ClearSemaphore(btsemaphore& sem) {
@@ -83,7 +113,11 @@ void BluetoothTest::ClearSemaphore(btsemaphore& sem) {
    ;
}

const bt_interface_t* BluetoothTest::bt_interface() { return bt_interface_; }
const bt_interface_t* BluetoothTest::bt_interface() {
  return &bluetoothInterface;
}

bt_callbacks_t* BluetoothTest::bt_callbacks() { return &callbacks; }

bt_state_t BluetoothTest::GetState() { return state_; }

@@ -121,41 +155,4 @@ bt_acl_state_t BluetoothTest::GetAclState() { return acl_state_; }
// Returns the device bond state.
bt_bond_state_t BluetoothTest::GetBondState() { return bond_state_; }

// callback
void BluetoothTest::AdapterStateChangedCallback(bt_state_t new_state) {
  state_ = new_state;
  semaphore_post(adapter_state_changed_callback_sem_);
}

// callback
void BluetoothTest::AdapterPropertiesCallback(bt_status_t status,
                                              int num_properties,
                                              bt_property_t* new_properties) {
  property_free_array(last_changed_properties_, properties_changed_count_);
  last_changed_properties_ =
      property_copy_array(new_properties, num_properties);
  properties_changed_count_ = num_properties;
  semaphore_post(adapter_properties_callback_sem_);
}

// callback
void BluetoothTest::RemoteDevicePropertiesCallback(bt_status_t status,
                                                   RawAddress* remote_bd_addr,
                                                   int num_properties,
                                                   bt_property_t* properties) {
  curr_remote_device_ = *remote_bd_addr;
  property_free_array(remote_device_last_changed_properties_,
                      remote_device_properties_changed_count_);
  remote_device_last_changed_properties_ =
      property_copy_array(properties, num_properties);
  remote_device_properties_changed_count_ = num_properties;
  semaphore_post(remote_device_properties_callback_sem_);
}

// callback
void BluetoothTest::DiscoveryStateChangedCallback(bt_discovery_state_t state) {
  discovery_state_ = state;
  semaphore_post(discovery_state_changed_callback_sem_);
}

}  // bttest
}  // namespace bttest
+14 −17
Original line number Diff line number Diff line
@@ -26,15 +26,13 @@
#include <signal.h>
#include <time.h>

#include <condition_variable>
#include <map>
#include <mutex>
#include <string>

#include "service/hal/bluetooth_interface.h"
#include "types/raw_address.h"

#include <mutex>
#include <condition_variable>

class btsemaphore {
 public:
  void post() {
@@ -73,8 +71,7 @@ namespace bttest {

// This class represents the Bluetooth testing framework and provides
// helpers and callbacks for GUnit to use for testing.
class BluetoothTest : public ::testing::Test,
                      public bluetooth::hal::BluetoothInterface::Observer {
class BluetoothTest : public ::testing::Test {
 protected:
  BluetoothTest() = default;
  BluetoothTest(const BluetoothTest&) = delete;
@@ -85,6 +82,9 @@ class BluetoothTest : public ::testing::Test,
  // Getter for the bt_interface
  const bt_interface_t* bt_interface();

  // Getter for the bt_callbacks
  bt_callbacks_t* bt_callbacks();

  // Gets the current state of the Bluetooth Interface
  bt_state_t GetState();

@@ -119,20 +119,20 @@ class BluetoothTest : public ::testing::Test,
  void TearDown() override;

  // A callback that is called when a property changes
  void AdapterPropertiesCallback(bt_status_t status, int num_properties,
                                 bt_property_t* properties) override;
  friend void AdapterPropertiesCallback(bt_status_t status, int num_properties,
                                        bt_property_t* properties);

  // A callback that is called when the remote device's property changes
  void RemoteDevicePropertiesCallback(bt_status_t status,
  friend void RemoteDevicePropertiesCallback(bt_status_t status,
                                             RawAddress* remote_bd_addr,
                                             int num_properties,
                                      bt_property_t* properties) override;
                                             bt_property_t* properties);

  // A callback that is called when the adapter state changes
  void AdapterStateChangedCallback(bt_state_t state) override;
  friend void AdapterStateChangedCallback(bt_state_t state);

  // A callback that is called when the Discovery state changes
  void DiscoveryStateChangedCallback(bt_discovery_state_t state) override;
  friend void DiscoveryStateChangedCallback(bt_discovery_state_t state);

  // Semaphores used to wait for specific callback execution. Each callback
  // has its own semaphore associated with it.
@@ -142,9 +142,6 @@ class BluetoothTest : public ::testing::Test,
  btsemaphore discovery_state_changed_callback_sem_;

 private:
  // The bluetooth interface that all the tests use to interact with the HAL
  const bt_interface_t* bt_interface_;

  bt_state_t state_;
  int properties_changed_count_;
  bt_property_t* last_changed_properties_;
+84 −70
Original line number Diff line number Diff line
@@ -24,9 +24,80 @@

namespace bttest {

static GattTest* instance = nullptr;

void RegisterClientCallback(int status, int clientIf,
                            const bluetooth::Uuid& app_uuid) {
  instance->status_ = status;
  instance->client_interface_id_ = clientIf;
  semaphore_post(instance->register_client_callback_sem_);
}

void ScanResultCallback(uint16_t ble_evt_type, uint8_t addr_type,
                        RawAddress* bda, uint8_t ble_primary_phy,
                        uint8_t ble_secondary_phy, uint8_t ble_advertising_sid,
                        int8_t ble_tx_power, int8_t rssi,
                        uint16_t ble_periodic_adv_int,
                        std::vector<uint8_t> adv_data,
                        RawAddress* original_bda) {
  semaphore_post(instance->scan_result_callback_sem_);
}

// GATT server callbacks
void RegisterServerCallback(int status, int server_if,
                            const bluetooth::Uuid& uuid) {
  instance->status_ = status;
  instance->server_interface_id_ = server_if;
  semaphore_post(instance->register_server_callback_sem_);
}

void ServiceAddedCallback(int status, int server_if,
                          const btgatt_db_element_t* service,
                          size_t service_count) {
  instance->status_ = status;
  instance->server_interface_id_ = server_if;
  instance->service_handle_ = service[0].attribute_handle;
  semaphore_post(instance->service_added_callback_sem_);
}

void ServiceStoppedCallback(int status, int server_if, int srvc_handle) {
  instance->status_ = status;
  instance->server_interface_id_ = server_if;
  instance->service_handle_ = srvc_handle;
  semaphore_post(instance->service_stopped_callback_sem_);
}

void ServiceDeletedCallback(int status, int server_if, int srvc_handle) {
  instance->status_ = status;
  instance->server_interface_id_ = server_if;
  instance->service_handle_ = srvc_handle;
  semaphore_post(instance->service_deleted_callback_sem_);
}

static const btgatt_scanner_callbacks_t scanner_callbacks = {
    .scan_result_cb = ScanResultCallback,
};

static const btgatt_client_callbacks_t client_callbacks = {
    .register_client_cb = RegisterClientCallback,
};

static const btgatt_server_callbacks_t server_callbacks = {
    .register_server_cb = RegisterServerCallback,
    .service_added_cb = ServiceAddedCallback,
    .service_stopped_cb = ServiceStoppedCallback,
    .service_deleted_cb = ServiceDeletedCallback,
};

static const btgatt_callbacks_t callbacks = {
    sizeof(btgatt_callbacks_t),
    &client_callbacks,
    &server_callbacks,
    &scanner_callbacks,
};

void GattTest::SetUp() {
  gatt_client_interface_ = nullptr;
  gatt_server_interface_ = nullptr;
  gatt_interface_ = nullptr;

  client_interface_id_ = 0;
  server_interface_id_ = 0;
@@ -40,24 +111,17 @@ void GattTest::SetUp() {
  semaphore_wait(adapter_state_changed_callback_sem_);
  EXPECT_TRUE(GetState() == BT_STATE_ON);

  bluetooth::hal::BluetoothGattInterface::Initialize();
  ASSERT_TRUE(bluetooth::hal::BluetoothGattInterface::IsInitialized());
  auto gatt_interface = bluetooth::hal::BluetoothGattInterface::Get();
  gatt_interface->AddClientObserver(this);
  gatt_interface->AddServerObserver(this);

  gatt_client_interface_ = gatt_interface->GetClientHALInterface();
  gatt_server_interface_ = gatt_interface->GetServerHALInterface();

  ASSERT_NE(nullptr, gatt_client_interface_);
  ASSERT_NE(nullptr, gatt_server_interface_);
  gatt_interface_ = reinterpret_cast<const btgatt_interface_t*>(
      bt_interface()->get_profile_interface(BT_PROFILE_GATT_ID));
  ASSERT_NE(nullptr, gatt_interface_);
  instance = this;
  auto status = gatt_interface_->init(&callbacks);
  ASSERT_EQ(status, BT_STATUS_SUCCESS);
}

void GattTest::TearDown() {
  gatt_client_interface_ = nullptr;
  gatt_server_interface_ = nullptr;

  bluetooth::hal::BluetoothGattInterface::CleanUp();
  instance = nullptr;
  gatt_interface_ = nullptr;

  ASSERT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
  semaphore_wait(adapter_state_changed_callback_sem_);
@@ -65,65 +129,15 @@ void GattTest::TearDown() {
}

const BleScannerInterface* GattTest::gatt_scanner_interface() {
  return gatt_scanner_interface_;
  return gatt_interface_->scanner;
}

const btgatt_client_interface_t* GattTest::gatt_client_interface() {
  return gatt_client_interface_;
  return gatt_interface_->client;
}

const btgatt_server_interface_t* GattTest::gatt_server_interface() {
  return gatt_server_interface_;
}

void GattTest::RegisterClientCallback(
    bluetooth::hal::BluetoothGattInterface* /* unused */, int status,
    int clientIf, const bluetooth::Uuid& app_uuid) {
  status_ = status;
  client_interface_id_ = clientIf;
  semaphore_post(register_client_callback_sem_);
}

void GattTest::ScanResultCallback(
    bluetooth::hal::BluetoothGattInterface* /* unused */, const RawAddress& bda,
    int rssi, std::vector<uint8_t> adv_data) {
  semaphore_post(scan_result_callback_sem_);
}

// GATT server callbacks
void GattTest::RegisterServerCallback(
    bluetooth::hal::BluetoothGattInterface* /* unused */, int status,
    int server_if, const bluetooth::Uuid& uuid) {
  status_ = status;
  server_interface_id_ = server_if;
  semaphore_post(register_server_callback_sem_);
}

void GattTest::ServiceAddedCallback(
    bluetooth::hal::BluetoothGattInterface* /* unused */, int status,
    int server_if, std::vector<btgatt_db_element_t> service) {
  status_ = status;
  server_interface_id_ = server_if;
  service_handle_ = service[0].attribute_handle;
  semaphore_post(service_added_callback_sem_);
}

void GattTest::ServiceStoppedCallback(
    bluetooth::hal::BluetoothGattInterface* /* unused */, int status,
    int server_if, int srvc_handle) {
  status_ = status;
  server_interface_id_ = server_if;
  service_handle_ = srvc_handle;
  semaphore_post(service_stopped_callback_sem_);
}

void GattTest::ServiceDeletedCallback(
    bluetooth::hal::BluetoothGattInterface* /* unused */, int status,
    int server_if, int srvc_handle) {
  status_ = status;
  server_interface_id_ = server_if;
  service_handle_ = srvc_handle;
  semaphore_post(service_deleted_callback_sem_);
  return gatt_interface_->server;
}

}  // bttest
Loading