diff --git a/samples/bluetooth/central_hr/src/main.c b/samples/bluetooth/central_hr/src/main.c index e1f07e1eb02..95f45f7c140 100644 --- a/samples/bluetooth/central_hr/src/main.c +++ b/samples/bluetooth/central_hr/src/main.c @@ -27,6 +27,8 @@ static struct bt_uuid_16 discover_uuid = BT_UUID_INIT_16(0); static struct bt_gatt_discover_params discover_params; static struct bt_gatt_subscribe_params subscribe_params; +uint64_t total_rx_count; /* This value is exposed to test code */ + static uint8_t notify_func(struct bt_conn *conn, struct bt_gatt_subscribe_params *params, const void *data, uint16_t length) @@ -39,6 +41,8 @@ static uint8_t notify_func(struct bt_conn *conn, printk("[NOTIFICATION] data %p length %u\n", data, length); + total_rx_count++; + return BT_GATT_ITER_CONTINUE; } @@ -224,6 +228,8 @@ static void connected(struct bt_conn *conn, uint8_t conn_err) printk("Connected: %s\n", addr); + total_rx_count = 0U; + if (conn == default_conn) { memcpy(&discover_uuid, BT_UUID_HRS, sizeof(discover_uuid)); discover_params.uuid = &discover_uuid.uuid; diff --git a/tests/bsim/bluetooth/compile.nrf5340bsim_nrf5340_cpuapp.sh b/tests/bsim/bluetooth/compile.nrf5340bsim_nrf5340_cpuapp.sh index ea9afa930e5..e261adfd960 100755 --- a/tests/bsim/bluetooth/compile.nrf5340bsim_nrf5340_cpuapp.sh +++ b/tests/bsim/bluetooth/compile.nrf5340bsim_nrf5340_cpuapp.sh @@ -16,6 +16,7 @@ app=tests/bsim/bluetooth/ll/conn conf_file=prj_split_privacy.conf sysbuild=1 co app=tests/bsim/bluetooth/ll/bis sysbuild=1 compile app=tests/bsim/bluetooth/ll/cis conf_overlay=overlay-acl_group.conf sysbuild=1 compile +run_in_background ${ZEPHYR_BASE}/tests/bsim/bluetooth/samples/compile.sh run_in_background ${ZEPHYR_BASE}/tests/bsim/bluetooth/audio_samples/compile.sh wait_for_background_jobs diff --git a/tests/bsim/bluetooth/compile.sh b/tests/bsim/bluetooth/compile.sh index 84a4f8c981a..499626d1487 100755 --- a/tests/bsim/bluetooth/compile.sh +++ b/tests/bsim/bluetooth/compile.sh @@ -20,6 +20,7 @@ ${ZEPHYR_BASE}/tests/bsim/bluetooth/audio_samples/compile.sh ${ZEPHYR_BASE}/tests/bsim/bluetooth/host/compile.sh ${ZEPHYR_BASE}/tests/bsim/bluetooth/ll/compile.sh ${ZEPHYR_BASE}/tests/bsim/bluetooth/mesh/compile.sh +${ZEPHYR_BASE}/tests/bsim/bluetooth/samples/compile.sh if [ ${BOARD} == "nrf52_bsim" ]; then ${ZEPHYR_BASE}/tests/bsim/bluetooth/hci_uart/compile.sh fi diff --git a/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/CMakeLists.txt b/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/CMakeLists.txt new file mode 100644 index 00000000000..d716aed6fc1 --- /dev/null +++ b/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/CMakeLists.txt @@ -0,0 +1,24 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(central_hr_peripheral_hr_test) + +set(central_hr_path ${ZEPHYR_BASE}/samples/bluetooth/central_hr) + +target_sources(app PRIVATE + ${central_hr_path}/src/main.c +) + +target_sources(app PRIVATE + src/sample_test.c + src/test_main.c +) + +zephyr_library_include_directories(${ZEPHYR_BASE}/samples/bluetooth) + +zephyr_include_directories( + ${BSIM_COMPONENTS_PATH}/libUtilv1/src/ + ${BSIM_COMPONENTS_PATH}/libPhyComv1/src/ + ) diff --git a/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/Kconfig b/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/Kconfig new file mode 100644 index 00000000000..e2cbc2de917 --- /dev/null +++ b/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/Kconfig @@ -0,0 +1,10 @@ +# Copyright (c) 2023 Nordic Semiconductor ASA +# SPDX-License-Identifier: Apache-2.0 + +# source sample's Kconfig, if any +# source "${ZEPHYR_BASE}/samples/bluetooth/central_hr/Kconfig" +# OR +# source Zephyr Kernel's Kconfig, as below (not both) +menu "Zephyr Kernel" +source "Kconfig.zephyr" +endmenu diff --git a/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/Kconfig.sysbuild b/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/Kconfig.sysbuild new file mode 100644 index 00000000000..8a4e25038c4 --- /dev/null +++ b/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/Kconfig.sysbuild @@ -0,0 +1,11 @@ +# Copyright (c) 2023-2024 Nordic Semiconductor ASA +# SPDX-License-Identifier: Apache-2.0 + +# source the samples' Kconfig.sysbuild, if any +source "${ZEPHYR_BASE}/samples/bluetooth/central_hr/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" diff --git a/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/boards/nrf5340bsim_nrf5340_cpuapp.conf b/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/boards/nrf5340bsim_nrf5340_cpuapp.conf new file mode 100644 index 00000000000..4d2c3afd09e --- /dev/null +++ b/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/boards/nrf5340bsim_nrf5340_cpuapp.conf @@ -0,0 +1 @@ +CONFIG_BT_BUF_ACL_RX_SIZE=255 diff --git a/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/prj.conf b/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/prj.conf new file mode 100644 index 00000000000..ad774080560 --- /dev/null +++ b/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/prj.conf @@ -0,0 +1,2 @@ +# Please build using the sample configuration file: +# ${ZEPHYR_BASE}/samples/bluetooth/central_hr/prj.conf diff --git a/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/src/sample_test.c b/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/src/sample_test.c new file mode 100644 index 00000000000..107ae44b3c6 --- /dev/null +++ b/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/src/sample_test.c @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2023-2024 Nordic Semiconductor ASA + * Copyright (c) 2017-2019 Oticon A/S + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "bs_types.h" +#include "bs_tracing.h" +#include "bs_utils.h" +#include "time_machine.h" +#include "bstests.h" + +#define WAIT_TIME 10 /* Seconds */ + +#define PASS_THRESHOLD 5 /* packets */ + +extern enum bst_result_t bst_result; + +#define FAIL(...) \ + do { \ + bst_result = Failed; \ + bs_trace_error_time_line(__VA_ARGS__); \ + } while (0) + +#define PASS(...) \ + do { \ + bst_result = Passed; \ + bs_trace_info_time(1, __VA_ARGS__); \ + } while (0) + +static void test_sample_init(void) +{ + /* We set an absolute deadline in 30 seconds */ + bst_ticker_set_next_tick_absolute(WAIT_TIME*1e6); + bst_result = In_progress; +} + +static void test_sample_tick(bs_time_t HW_device_time) +{ + /* + * If in WAIT_TIME seconds we did not get enough packets through + * we consider the test failed + */ + + extern uint64_t total_rx_count; + + bs_trace_info_time(2, "%"PRIu64" packets received, expected >= %i\n", + total_rx_count, PASS_THRESHOLD); + + if (total_rx_count >= PASS_THRESHOLD) { + PASS("PASSED\n"); + bs_trace_exit("Done, disconnecting from simulation\n"); + } else { + FAIL("FAILED (Did not pass after %i seconds)\n", WAIT_TIME); + } +} + +static const struct bst_test_instance test_sample[] = { + { + .test_id = "central_hr_peripheral_hr", + .test_descr = "Test based on the peripheral and central HR samples. " + "It expects to be connected to a compatible sample, " + "waits for " STR(WAIT_TIME) " seconds, and checks how " + "many packets have been received correctly", + .test_post_init_f = test_sample_init, + .test_tick_f = test_sample_tick, + }, + BSTEST_END_MARKER +}; + +struct bst_test_list *test_sample_install(struct bst_test_list *tests) +{ + tests = bst_add_tests(tests, test_sample); + return tests; +} diff --git a/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/src/test_main.c b/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/src/test_main.c new file mode 100644 index 00000000000..601b9fcefa9 --- /dev/null +++ b/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/src/test_main.c @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2023-2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "bstests.h" + +extern struct bst_test_list *test_sample_install(struct bst_test_list *tests); + +bst_test_install_t test_installers[] = { + test_sample_install, + NULL +}; diff --git a/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/sysbuild.cmake b/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/sysbuild.cmake new file mode 100644 index 00000000000..98cdf5ccbcd --- /dev/null +++ b/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/sysbuild.cmake @@ -0,0 +1,6 @@ +# Copyright (c) 2023 Nordic Semiconductor ASA +# SPDX-License-Identifier: Apache-2.0 + +include(${ZEPHYR_BASE}/samples/bluetooth/central_hr/sysbuild.cmake) + +native_simulator_set_primary_mcu_index(${DEFAULT_IMAGE} ${NET_APP}) diff --git a/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/tests_scripts/central_hr_peripheral_hr.sh b/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/tests_scripts/central_hr_peripheral_hr.sh new file mode 100755 index 00000000000..29cec0cc114 --- /dev/null +++ b/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/tests_scripts/central_hr_peripheral_hr.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +# Copyright 2023-2024 Nordic Semiconductor ASA +# SPDX-License-Identifier: Apache-2.0 + +simulation_id="central_hr_peripheral_hr_test" +verbosity_level=2 + +source ${ZEPHYR_BASE}/tests/bsim/sh_common.source + +EXECUTE_TIMEOUT=10 + +cd ${BSIM_OUT_PATH}/bin + +Execute ./bs_${BOARD_TS}_samples_bluetooth_peripheral_hr_prj_conf \ + -v=${verbosity_level} -s=${simulation_id} -d=0 -RealEncryption=1 + +Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_samples_central_hr_peripheral_hr_prj_conf \ + -v=${verbosity_level} -s=${simulation_id} -d=1 -RealEncryption=1 \ + -testid=central_hr_peripheral_hr + +Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \ + -D=2 -sim_length=10e6 $@ + +wait_for_background_jobs #Wait for all programs in background and return != 0 if any fails diff --git a/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/tests_scripts/central_hr_peripheral_hr_extended.sh b/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/tests_scripts/central_hr_peripheral_hr_extended.sh new file mode 100755 index 00000000000..ad6d4cede4c --- /dev/null +++ b/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/tests_scripts/central_hr_peripheral_hr_extended.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +# Copyright 2023-2024 Nordic Semiconductor ASA +# SPDX-License-Identifier: Apache-2.0 + +source ${ZEPHYR_BASE}/tests/bsim/sh_common.source + +simulation_id="central_hr_peripheral_hr_extended_test" +test_long_name="$(guess_test_long_name)" +verbosity_level=2 + +EXECUTE_TIMEOUT=10 + +cd ${BSIM_OUT_PATH}/bin + +Execute ./bs_${BOARD_TS}_samples_bluetooth_peripheral_hr_prj_conf_overlay-extended_conf \ + -v=${verbosity_level} -s=${simulation_id} -d=0 -RealEncryption=1 + +Execute ./bs_${BOARD_TS}_${test_long_name}_prj_conf_overlay-extended_conf \ + -v=${verbosity_level} -s=${simulation_id} -d=1 -RealEncryption=1 \ + -testid=central_hr_peripheral_hr + +Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \ + -D=2 -sim_length=10e6 $@ + +wait_for_background_jobs #Wait for all programs in background and return != 0 if any fails diff --git a/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/tests_scripts/central_hr_peripheral_hr_phy_coded.sh b/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/tests_scripts/central_hr_peripheral_hr_phy_coded.sh new file mode 100755 index 00000000000..633ca41f538 --- /dev/null +++ b/tests/bsim/bluetooth/samples/central_hr_peripheral_hr/tests_scripts/central_hr_peripheral_hr_phy_coded.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +# Copyright 2023-2024 Nordic Semiconductor ASA +# SPDX-License-Identifier: Apache-2.0 + +source ${ZEPHYR_BASE}/tests/bsim/sh_common.source + +simulation_id="central_hr_peripheral_hr_phy_coded_test" +test_long_name="$(guess_test_long_name)" +verbosity_level=2 + +EXECUTE_TIMEOUT=10 + +cd ${BSIM_OUT_PATH}/bin + +Execute ./bs_${BOARD_TS}_samples_bluetooth_peripheral_hr_prj_conf_overlay-phy_coded_conf \ + -v=${verbosity_level} -s=${simulation_id} -d=0 -RealEncryption=1 + +Execute ./bs_${BOARD_TS}_${test_long_name}_prj_conf_overlay-phy_coded_conf \ + -v=${verbosity_level} -s=${simulation_id} -d=1 -RealEncryption=1 \ + -testid=central_hr_peripheral_hr + +Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \ + -D=2 -sim_length=10e6 $@ + +wait_for_background_jobs #Wait for all programs in background and return != 0 if any fails diff --git a/tests/bsim/bluetooth/samples/compile.sh b/tests/bsim/bluetooth/samples/compile.sh new file mode 100755 index 00000000000..3b61bdf1779 --- /dev/null +++ b/tests/bsim/bluetooth/samples/compile.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash +# Copyright 2023-2024 Nordic Semiconductor ASA +# SPDX-License-Identifier: Apache-2.0 + +# Compile all the applications needed by the bsim tests in these subfolders + +#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 + +app=samples/bluetooth/peripheral_hr \ + sysbuild=1 \ + compile +app=tests/bsim/bluetooth/samples/central_hr_peripheral_hr \ + sysbuild=1 \ + extra_conf_file=${ZEPHYR_BASE}/samples/bluetooth/central_hr/prj.conf \ + compile +app=samples/bluetooth/peripheral_hr \ + sysbuild=1 \ + conf_overlay=overlay-extended.conf \ + compile +app=tests/bsim/bluetooth/samples/central_hr_peripheral_hr \ + sysbuild=1 \ + extra_conf_file=${ZEPHYR_BASE}/samples/bluetooth/central_hr/prj.conf \ + conf_overlay=${ZEPHYR_BASE}/samples/bluetooth/central_hr/overlay-extended.conf \ + compile +app=samples/bluetooth/peripheral_hr \ + sysbuild=1 \ + conf_overlay=overlay-phy_coded.conf \ + compile +app=tests/bsim/bluetooth/samples/central_hr_peripheral_hr \ + sysbuild=1 \ + extra_conf_file=${ZEPHYR_BASE}/samples/bluetooth/central_hr/prj.conf \ + conf_overlay=${ZEPHYR_BASE}/samples/bluetooth/central_hr/overlay-phy_coded.conf \ + compile + +wait_for_background_jobs diff --git a/tests/bsim/bluetooth/tests.nrf5340bsim_nrf5340_cpuapp.txt b/tests/bsim/bluetooth/tests.nrf5340bsim_nrf5340_cpuapp.txt index 27a1fc4a76f..fec0c4a6002 100644 --- a/tests/bsim/bluetooth/tests.nrf5340bsim_nrf5340_cpuapp.txt +++ b/tests/bsim/bluetooth/tests.nrf5340bsim_nrf5340_cpuapp.txt @@ -2,4 +2,5 @@ # This file is used in CI to select which tests are run tests/bsim/bluetooth/ll/conn/tests_scripts/basic_conn_encrypted_split_privacy.sh tests/bsim/bluetooth/ll/bis/tests_scripts/broadcast_iso.sh +tests/bsim/bluetooth/samples/ tests/bsim/bluetooth/audio_samples/