Skip to content

Commit

Permalink
tests: Bluetooth: Add BT Tester GAP smoke test
Browse files Browse the repository at this point in the history
Add a babblesim test of the BT Tester doing a GAP smoke test
connecting 2 BT testers using BTP.

The purpose of this is to further increase the test coverage
of the BT Tester in CI, as it is only being built, and runtime
errors are typically not caught.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
  • Loading branch information
Thalley committed Feb 24, 2025
1 parent ad845fd commit 78d85c5
Show file tree
Hide file tree
Showing 16 changed files with 345 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tests/bluetooth/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ LIST(APPEND QEMU_EXTRA_FLAGS -serial unix:/tmp/bt-stack-tester)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(tester)

if(CONFIG_SOC_SERIES_BSIM_NRFXX)
zephyr_include_directories(
${BSIM_COMPONENTS_PATH}/libUtilv1/src/
${BSIM_COMPONENTS_PATH}/libPhyComv1/src/
)
endif() # CONFIG_SOC_SERIES_BSIM_NRFXX

zephyr_library_include_directories(${ZEPHYR_BASE}/samples/bluetooth)
zephyr_library_include_directories(${ZEPHYR_BASE}/subsys/bluetooth/mesh)
zephyr_library_include_directories(${ZEPHYR_BASE}/subsys/bluetooth/host)
Expand Down
15 changes: 15 additions & 0 deletions tests/bluetooth/tester/boards/nrf52_bsim.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# CONFIG_TEST enforces minimal logging, which we don't want
CONFIG_TEST=n

CONFIG_ASSERT=y
# Enable the option below to measure stack usage
#CONFIG_INIT_STACKS=y
CONFIG_THREAD_NAME=y

CONFIG_LOG=y
CONFIG_LOG_BACKEND_SHOW_COLOR=y

CONFIG_LOG_DEFAULT_LEVEL=3
CONFIG_BTTESTER_LOG_LEVEL_DBG=y

CONFIG_UART_PIPE=n
24 changes: 24 additions & 0 deletions tests/bluetooth/tester/boards/nrf5340bsim_nrf5340_cpuapp.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# CONFIG_TEST enforces minimal logging, which we don't want
CONFIG_TEST=n

CONFIG_ASSERT=y
# Enable the option below to measure stack usage
#CONFIG_INIT_STACKS=y
CONFIG_THREAD_NAME=y
CONFIG_HW_STACK_PROTECTION=y

CONFIG_LOG=y
CONFIG_LOG_BUFFER_SIZE=4096
CONFIG_RTT_CONSOLE=y
CONFIG_LOG_BACKEND_RTT=y
CONFIG_LOG_BACKEND_RTT_MODE_DROP=y
CONFIG_LOG_BACKEND_RTT_MESSAGE_SIZE=256
CONFIG_USE_SEGGER_RTT=y
CONFIG_SEGGER_RTT_BUFFER_SIZE_UP=4096
CONFIG_LOG_BACKEND_SHOW_COLOR=n
CONFIG_LOG_PROCESS_THREAD_STACK_SIZE=1024

CONFIG_LOG_DEFAULT_LEVEL=3
CONFIG_BTTESTER_LOG_LEVEL_DBG=y

CONFIG_UART_INTERRUPT_DRIVEN=y
13 changes: 13 additions & 0 deletions tests/bluetooth/tester/src/btp.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,19 @@ static void uart_send(const uint8_t *data, size_t len)
{
uart_pipe_send(data, len);
}
#elif defined(CONFIG_SOC_SERIES_BSIM_NRFXX)
extern void btp_register_tester(uint8_t *buffer, size_t len, uart_pipe_recv_cb cb);
extern void btp_send_to_bsim(const uint8_t *data, size_t len);

static void uart_init(uint8_t *data)
{
btp_register_tester(data, BTP_MTU, recv_cb);
}

static void uart_send(const uint8_t *data, size_t len)
{
btp_send_to_bsim(data, len);
}
#else /* !CONFIG_UART_PIPE */
static uint8_t *recv_buf;
static size_t recv_off;
Expand Down
13 changes: 13 additions & 0 deletions tests/bluetooth/tester/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

/*
* Copyright (c) 2015-2016 Intel Corporation
* Copyright (c) 2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/autoconf.h>

#include <zephyr/kernel.h>
#include <zephyr/sys/util_macro.h>
#include <zephyr/types.h>
#include <zephyr/toolchain.h>

#if defined(CONFIG_SOC_SERIES_BSIM_NRFXX)
#include "bstests.h"
#endif /* CONFIG_SOC_SERIES_BSIM_NRFXX */

#include <zephyr/logging/log.h>
#define LOG_MODULE_NAME bttester_main
LOG_MODULE_REGISTER(LOG_MODULE_NAME, CONFIG_BTTESTER_LOG_LEVEL);
Expand All @@ -19,5 +27,10 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME, CONFIG_BTTESTER_LOG_LEVEL);
int main(void)
{
tester_init();

#if defined(CONFIG_SOC_SERIES_BSIM_NRFXX)
bst_main();
#endif /* CONFIG_SOC_SERIES_BSIM_NRFXX */

return 0;
}
29 changes: 29 additions & 0 deletions tests/bsim/bluetooth/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(tester_bsim)

# Add the BSIM tester
add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/tester ${CMAKE_BINARY_DIR}/tester_build)

# This contains babblesim-specific helpers, e.g. device synchronization.
add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit)
target_link_libraries(app PRIVATE babblekit)

# Add the testlib
add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/common/testlib testlib)
target_link_libraries(app PRIVATE testlib)

zephyr_include_directories(
${BSIM_COMPONENTS_PATH}/libUtilv1/src/
${BSIM_COMPONENTS_PATH}/libPhyComv1/src/
)

target_sources(app PRIVATE
src/btp.c
src/gap_central.c
src/gap_peripheral.c
src/test_main.c
)
4 changes: 4 additions & 0 deletions tests/bsim/bluetooth/tester/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) 2024-2025 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

source "${ZEPHYR_BASE}/tests/bluetooth/tester/Kconfig"
10 changes: 10 additions & 0 deletions tests/bsim/bluetooth/tester/Kconfig.sysbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2023-2025 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

source "${ZEPHYR_BASE}/tests/bluetooth/tester/Kconfig.sysbuild"

config NATIVE_SIMULATOR_PRIMARY_MCU_INDEX
int
# Let's pass the test arguments to the application MCU test
# otherwise by default they would have gone to the net core.
default 0 if $(BOARD_TARGET_STRING) = "NRF5340BSIM_NRF5340_CPUAPP"
26 changes: 26 additions & 0 deletions tests/bsim/bluetooth/tester/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
# Copyright 2023-2025 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

#set -x #uncomment this line for debugging
set -ue

: "${ZEPHYR_BASE:?ZEPHYR_BASE must be set to point to the zephyr root directory}"

source ${ZEPHYR_BASE}/tests/bsim/compile.source

if [ "${BOARD_TS}" == "nrf5340bsim_nrf5340_cpuapp" ]; then
app=tests/bsim/bluetooth/tester \
tester=${ZEPHYR_BASE}/tests/bluetooth/tester \
conf_file=${tester}/prj.conf \
extra_conf_file="${tester}/boards/nrf5340bsim_nrf5340_cpuapp.conf" \
exe_name=bs_${BOARD_TS}_${app}_prj_conf sysbuild=1 compile
else
app=tests/bsim/bluetooth/tester \
tester=${ZEPHYR_BASE}/tests/bluetooth/tester \
conf_file=${tester}/prj.conf \
extra_conf_file="${tester}/boards/nrf52_bsim.conf" \
exe_name=bs_${BOARD_TS}_${app}_prj_conf sysbuild=1 compile
fi

wait_for_background_jobs
2 changes: 2 additions & 0 deletions tests/bsim/bluetooth/tester/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Please build using the sample configuration file:
# ${ZEPHYR_BASE}/tests/bluetooth/tester/prj.conf
89 changes: 89 additions & 0 deletions tests/bsim/bluetooth/tester/src/btp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdint.h>
#include <stddef.h>
#include <string.h>

#include <zephyr/drivers/uart_pipe.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/util.h>
#include <zephyr/sys_clock.h>

#include "babblekit/testcase.h"

#include "btp.h"

LOG_MODULE_REGISTER(bsim_btp, CONFIG_BTTESTER_LOG_LEVEL);

static uint8_t *btp_buffer;
static size_t btp_buffer_len;
static uart_pipe_recv_cb btp_cb;

static uint8_t rsp_buffer[1024];
static size_t rsp_buffer_len;
static void wait_for_response(void)
{
const uint64_t timeout_us = 10 * USEC_PER_SEC;
struct btp_hdr *rsp_hdr;

memset(rsp_buffer, 0, sizeof(rsp_buffer)); /* reset response buffer */
rsp_buffer_len = 0U;

WAIT_FOR(rsp_buffer_len > sizeof(*rsp_hdr), timeout_us, k_msleep(1));
rsp_hdr = (struct btp_hdr *)rsp_buffer;
if (rsp_hdr->len > 0) {
WAIT_FOR(rsp_buffer_len > sizeof(*rsp_hdr) + rsp_hdr->len, timeout_us, k_msleep(1));
}
rsp_buffer_len = 0U;
}

/* BTP communication is inspired from `uart_pipe_rx` to achieve a similar API */
void btp_send_to_tester(const uint8_t *data, size_t len)
{
size_t offset = 0U;

TEST_ASSERT(len > 0U);
TEST_ASSERT(data != NULL);
TEST_ASSERT(btp_buffer != NULL);
TEST_ASSERT(len <= btp_buffer_len);

memcpy(btp_buffer, data, len);

btp_buffer = btp_cb(btp_buffer, &offset);
TEST_ASSERT(offset == 0U);

wait_for_response();
}

void btp_register_tester(uint8_t *buffer, size_t len, uart_pipe_recv_cb cb)
{
TEST_ASSERT(cb > 0U);
TEST_ASSERT(len > 0U);
TEST_ASSERT(buffer != NULL);

btp_buffer = buffer;
btp_buffer_len = len;
btp_cb = cb;

LOG_INF("%p %zu", buffer, len);
}

void btp_send_to_bsim(const uint8_t *data, size_t len)
{

TEST_ASSERT(len > 0U);
TEST_ASSERT(data != NULL);
TEST_ASSERT(len <= ARRAY_SIZE(rsp_buffer));

/* TODO: Parse incoming data and call relevant handlers */
/* We will always get a `struct btp_hdr msg` first and then if len > 0 then some additional
* data
*/
LOG_ERR("%zu", len);
memcpy(rsp_buffer + rsp_buffer_len, data, len);
rsp_buffer_len += len;
}
33 changes: 33 additions & 0 deletions tests/bsim/bluetooth/tester/src/gap_central.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <inttypes.h>
#include <stdint.h>

#include <zephyr/sys/util_macro.h>

#include "babblekit/testcase.h"
#include "bstests.h"

#include "bttester.h"

static void test_gap_central(void)
{
TEST_PASS("PASSED\n");
}

static const struct bst_test_instance test_sample[] = {
{
.test_id = "gap_central",
.test_descr = "Smoketest for the GAP central BT Tester behavior",
.test_main_f = test_gap_central,
},
BSTEST_END_MARKER,
};

struct bst_test_list *test_gap_central_install(struct bst_test_list *tests)
{
return bst_add_tests(tests, test_sample);
}
31 changes: 31 additions & 0 deletions tests/bsim/bluetooth/tester/src/gap_peripheral.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <inttypes.h>
#include <stdint.h>

#include <zephyr/sys/util_macro.h>

#include "babblekit/testcase.h"
#include "bstests.h"

static void test_gap_peripheral(void)
{
TEST_PASS("PASSED\n");
}

static const struct bst_test_instance test_sample[] = {
{
.test_id = "gap_peripheral",
.test_descr = "Smoketest for the GAP central BT Tester behavior",
.test_main_f = test_gap_peripheral,
},
BSTEST_END_MARKER,
};

struct bst_test_list *test_gap_peripheral_install(struct bst_test_list *tests)
{
return bst_add_tests(tests, test_sample);
}
19 changes: 19 additions & 0 deletions tests/bsim/bluetooth/tester/src/test_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stddef.h>

#include "bstests.h"

extern struct bst_test_list *test_gap_central_install(struct bst_test_list *tests);
extern struct bst_test_list *test_gap_peripheral_install(struct bst_test_list *tests);

bst_test_install_t test_installers[] = {
test_gap_central_install,
test_gap_peripheral_install,
NULL,
};

/* bst_main will be called by the BT tester's `main` function */
6 changes: 6 additions & 0 deletions tests/bsim/bluetooth/tester/sysbuild.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright (c) 2023-2025 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

include(${ZEPHYR_BASE}/tests/bluetooth/tester/sysbuild.cmake)

native_simulator_set_primary_mcu_index(${DEFAULT_IMAGE} ${NET_APP})
24 changes: 24 additions & 0 deletions tests/bsim/bluetooth/tester/tests_scripts/gap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
# Copyright 2023-2025 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

# Smoketest for GAP BTP commands with the BT tester

simulation_id="tester_gap"
verbosity_level=2
EXECUTE_TIMEOUT=20

source ${ZEPHYR_BASE}/tests/bsim/sh_common.source

cd ${BSIM_OUT_PATH}/bin

Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_tester_prj_conf \
-v=${verbosity_level} -s=${simulation_id} -d=0 -RealEncryption=1 -testid=gap_central

Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_tester_prj_conf \
-v=${verbosity_level} -s=${simulation_id} -d=1 -RealEncryption=1 -testid=gap_peripheral

Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \
-D=2 -sim_length=20e6 $@ -argschannel -at=40

wait_for_background_jobs #Wait for all programs in background and return != 0 if any fails

0 comments on commit 78d85c5

Please sign in to comment.