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

Commit 13176475 authored by Ajay Panicker's avatar Ajay Panicker
Browse files

net_test_bluetooth: RFCOMM test refactor

Properly implement RFCOMM tests into net_test_rfcomm using GUnit.
Contains all the RFCOMM tests and the base class that the tests use.
This test currently requires the DUT to be paired with an HFP capable
device.

Bug: 25793348
Change-Id: I683db7c5f2779373b2e343c7ad21268e4969d014
parent 13dfc30c
Loading
Loading
Loading
Loading
+13 −1
Original line number Diff line number Diff line
@@ -4,12 +4,17 @@ known_tests=(
  bluetoothtbd_test
  net_test_bluetooth
  net_test_btcore
  net_test_btif
  net_test_device
  net_test_hci
  net_test_osi
  net_test_btif
)

known_remote_tests=(
  net_test_rfcomm
)


usage() {
  binary="$(basename "$0")"
  echo "Usage: ${binary} --help"
@@ -23,6 +28,13 @@ usage() {
  do
    echo "    ${name}"
  done

  echo
  echo "Known tests that need a remote device:"
  for name in "${known_remote_tests[@]}"
  do
    echo "    ${name}"
  done
}

iterations=1
+44 −10
Original line number Diff line number Diff line
@@ -16,13 +16,6 @@

LOCAL_PATH := $(call my-dir)

# Bluetooth test suite for target
# ========================================================
include $(CLEAR_VARS)
LOCAL_CPP_EXTENSION := .cc
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := net_test_bluetooth

# These tests use the bluetoothtbd HAL wrappers in order to easily interact
# with the interface using C++
# TODO: Make the bluetoothtbd HAL a static library
@@ -31,6 +24,13 @@ bluetoothHalSrc := \
    ../../service/hal/bluetooth_interface.cc \
    ../../service/logging_helpers.cc

# Bluetooth test suite for target
# ========================================================
include $(CLEAR_VARS)
LOCAL_CPP_EXTENSION := .cc
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := net_test_bluetooth

LOCAL_C_INCLUDES += \
    $(LOCAL_PATH)/../../

@@ -57,3 +57,37 @@ LOCAL_CONLYFLAGS += $(bluetooth_CONLYFLAGS)
LOCAL_CPPFLAGS += $(bluetooth_CPPFLAGS)

include $(BUILD_NATIVE_TEST)

# Bluetooth test suite for target
# ========================================================
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := net_test_rfcomm

LOCAL_C_INCLUDES += \
    $(LOCAL_PATH)/../../ \
    $(bluetooth_C_INCLUDES) \

LOCAL_SRC_FILES := \
    adapter/bluetooth_test.cc \
    rfcomm/rfcomm_test.cc \
    rfcomm/rfcomm_unittest.cc \
    $(bluetoothHalSrc)

LOCAL_SHARED_LIBRARIES += \
    liblog \
    libhardware \
    libhardware_legacy \
    libcutils \
    libchrome \

LOCAL_STATIC_LIBRARIES += \
    libbtcore \
    libosi \

LOCAL_CFLAGS += $(bluetooth_CFLAGS)
LOCAL_CONLYFLAGS += $(bluetooth_CONLYFLAGS)
LOCAL_CPPFLAGS += $(bluetooth_CPPFLAGS)

include $(BUILD_NATIVE_TEST)
+33 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@

extern "C" {
#include "btcore/include/property.h"
#include "btcore/include/bdaddr.h"
}

namespace {
@@ -39,11 +40,14 @@ void BluetoothTest::SetUp() {
  state_ = BT_STATE_OFF;
  properties_changed_count_ = 0;
  last_changed_properties_ = nullptr;
  remote_device_properties_changed_count_ = 0;
  remote_device_last_changed_properties_ = nullptr;
  discovery_state_ = BT_DISCOVERY_STOPPED;
  acl_state_ = BT_ACL_STATE_DISCONNECTED;
  bond_state_ = BT_BOND_STATE_NONE;

  adapter_properties_callback_sem_ = semaphore_new(0);
  remote_device_properties_callback_sem_ = semaphore_new(0);
  adapter_state_changed_callback_sem_ = semaphore_new(0);
  discovery_state_changed_callback_sem_ = semaphore_new(0);

@@ -57,6 +61,7 @@ void BluetoothTest::SetUp() {

void BluetoothTest::TearDown() {
  semaphore_free(adapter_properties_callback_sem_);
  semaphore_free(remote_device_properties_callback_sem_);
  semaphore_free(adapter_state_changed_callback_sem_);
  semaphore_free(discovery_state_changed_callback_sem_);

@@ -91,6 +96,19 @@ bt_property_t* BluetoothTest::GetProperty(bt_property_type_t type) {
  return nullptr;
}

bt_property_t* BluetoothTest::GetRemoteDeviceProperty(const bt_bdaddr_t* addr,
                                                      bt_property_type_t type) {
  if (!bdaddr_equals(&curr_remote_device_, addr))
    return nullptr;

  for (int i = 0; i < remote_device_properties_changed_count_; i++) {
    if (remote_device_last_changed_properties_[i].type == type) {
      return &remote_device_last_changed_properties_[i];
    }
  }
  return nullptr;
}

bt_discovery_state_t BluetoothTest::GetDiscoveryState() {
  return discovery_state_;
}
@@ -121,6 +139,21 @@ void BluetoothTest::AdapterPropertiesCallback(
  semaphore_post(adapter_properties_callback_sem_);
}

//callback
void BluetoothTest::RemoteDevicePropertiesCallback(
    bt_status_t status,
    bt_bdaddr_t *remote_bd_addr,
    int num_properties,
    bt_property_t *properties) {
  bdaddr_copy(&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;
+15 −0
Original line number Diff line number Diff line
@@ -59,6 +59,10 @@ class BluetoothTest : public ::testing::Test,
  // Get the value of a specific property
  bt_property_t* GetProperty(bt_property_type_t type);

  // Get the value of a specific remote device property
  bt_property_t* GetRemoteDeviceProperty(const bt_bdaddr_t* addr,
                                         bt_property_type_t type);

  // Get the current discovery state
  bt_discovery_state_t GetDiscoveryState();

@@ -84,6 +88,13 @@ class BluetoothTest : public ::testing::Test,
      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,
      bt_bdaddr_t *remote_bd_addr,
      int num_properties,
      bt_property_t *properties);

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

@@ -93,6 +104,7 @@ class BluetoothTest : public ::testing::Test,
  // Semaphores used to wait for specific callback execution. Each callback
  // has its own semaphore associated with it.
  semaphore_t* adapter_properties_callback_sem_;
  semaphore_t* remote_device_properties_callback_sem_;
  semaphore_t* adapter_state_changed_callback_sem_;
  semaphore_t* discovery_state_changed_callback_sem_;

@@ -103,6 +115,9 @@ class BluetoothTest : public ::testing::Test,
  bt_state_t state_;
  int properties_changed_count_;
  bt_property_t *last_changed_properties_;
  bt_bdaddr_t curr_remote_device_;
  int remote_device_properties_changed_count_;
  bt_property_t *remote_device_last_changed_properties_;
  bt_discovery_state_t discovery_state_;
  bt_acl_state_t acl_state_;
  bt_bond_state_t bond_state_;
+85 −0
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright (C) 2016 Google, Inc.
 *
 *  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 "adapter/bluetooth_test.h"
#include "rfcomm/rfcomm_test.h"

extern "C" {
#include "btcore/include/bdaddr.h"
#include "btcore/include/uuid.h"
}

namespace bttest {

const bt_uuid_t RFCommTest::HFP_UUID = {{ 0x00, 0x00, 0x11, 0x1E,
                                          0x00, 0x00, 0x10, 0x00,
                                          0x80, 0x00, 0x00, 0x80,
                                          0x5F, 0x9B, 0x34, 0xFB }};

void RFCommTest::SetUp(){
  BluetoothTest::SetUp();

  ASSERT_EQ(bt_interface()->enable(false), BT_STATUS_SUCCESS);
  semaphore_wait(adapter_state_changed_callback_sem_);
  ASSERT_TRUE(GetState() == BT_STATE_ON);
  socket_interface_ = (const btsock_interface_t *)bt_interface()->
                         get_profile_interface(BT_PROFILE_SOCKETS_ID);
  ASSERT_NE(socket_interface_, nullptr);

  // Find a bonded device that supports HFP
  string_to_bdaddr("00:00:00:00:00:00", &bt_remote_bdaddr_);
  char value[1280];

  bt_property_t* bonded_devices_prop = GetProperty(BT_PROPERTY_ADAPTER_BONDED_DEVICES);
  bt_bdaddr_t* devices = (bt_bdaddr_t*)bonded_devices_prop->val;
  int num_bonded_devices = bonded_devices_prop->len / sizeof(bt_bdaddr_t);

  for (int i = 0; i < num_bonded_devices && bdaddr_is_empty(&bt_remote_bdaddr_); i++) {
    ClearSemaphore(remote_device_properties_callback_sem_);
    bt_interface()->get_remote_device_property(&devices[i], BT_PROPERTY_UUIDS);
    semaphore_wait(remote_device_properties_callback_sem_);

    bt_property_t* uuid_prop = GetRemoteDeviceProperty(&devices[i], BT_PROPERTY_UUIDS);
    if (uuid_prop == nullptr)
      continue;
    bt_uuid_t* uuids = (bt_uuid_t*) uuid_prop->val;
    int num_uuids = uuid_prop->len / sizeof(bt_uuid_t);

    for (int j = 0; j < num_uuids; j++) {
      uuid_to_string(&uuids[j], (uuid_string_t*)value);
      if (!memcmp(uuids + j, &HFP_UUID, sizeof(bt_uuid_t))) {
        bdaddr_copy(&bt_remote_bdaddr_, devices+i);
        break;
      }
    }
  }

  ASSERT_FALSE(bdaddr_is_empty(&bt_remote_bdaddr_))
      << "Could not find paired device that supports HFP";
}

void RFCommTest::TearDown() {
  socket_interface_ = NULL;

  ASSERT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
  semaphore_wait(adapter_state_changed_callback_sem_);

  BluetoothTest::TearDown();
}

}  // bttest
Loading