From b30d2d1bf5efa381e65708cd229ffb8739e6b12e Mon Sep 17 00:00:00 2001 From: Rubin Gerritsen Date: Mon, 3 Jun 2024 13:32:52 +0200 Subject: [PATCH] Bluetooth: Host: Add a test for connection creation timeout It seemed like there was lacking test coverage for this functionality. Signed-off-by: Rubin Gerritsen --- .../bluetooth/host/central/CMakeLists.txt | 23 ++++ tests/bsim/bluetooth/host/central/prj.conf | 3 + tests/bsim/bluetooth/host/central/src/main.c | 109 ++++++++++++++++++ .../host/central/tests_scripts/_compile.sh | 13 +++ .../run_central_connect_timeout.sh | 18 +++ tests/bsim/bluetooth/host/compile.sh | 1 + 6 files changed, 167 insertions(+) create mode 100644 tests/bsim/bluetooth/host/central/CMakeLists.txt create mode 100644 tests/bsim/bluetooth/host/central/prj.conf create mode 100644 tests/bsim/bluetooth/host/central/src/main.c create mode 100755 tests/bsim/bluetooth/host/central/tests_scripts/_compile.sh create mode 100755 tests/bsim/bluetooth/host/central/tests_scripts/run_central_connect_timeout.sh diff --git a/tests/bsim/bluetooth/host/central/CMakeLists.txt b/tests/bsim/bluetooth/host/central/CMakeLists.txt new file mode 100644 index 00000000000..ba3c9179e29 --- /dev/null +++ b/tests/bsim/bluetooth/host/central/CMakeLists.txt @@ -0,0 +1,23 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(central) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources} ) + +# This contains babblesim-specific helpers, e.g. device synchronization. +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + +zephyr_include_directories( + ${BSIM_COMPONENTS_PATH}/libUtilv1/src/ + ${BSIM_COMPONENTS_PATH}/libPhyComv1/src/ + ) + +add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/common/testlib testlib) +target_link_libraries(app PRIVATE + testlib +) diff --git a/tests/bsim/bluetooth/host/central/prj.conf b/tests/bsim/bluetooth/host/central/prj.conf new file mode 100644 index 00000000000..1f446a5bb27 --- /dev/null +++ b/tests/bsim/bluetooth/host/central/prj.conf @@ -0,0 +1,3 @@ +CONFIG_ASSERT=y +CONFIG_BT=y +CONFIG_BT_CENTRAL=y diff --git a/tests/bsim/bluetooth/host/central/src/main.c b/tests/bsim/bluetooth/host/central/src/main.c new file mode 100644 index 00000000000..2e66f417982 --- /dev/null +++ b/tests/bsim/bluetooth/host/central/src/main.c @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2024 Nordic Semiconductor + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "bstests.h" +#include "babblekit/testcase.h" +#include + +static K_SEM_DEFINE(sem_connected, 0, 1); + +static void connected_cb(struct bt_conn *conn, uint8_t err) +{ + TEST_ASSERT(conn); + TEST_ASSERT(err == BT_HCI_ERR_UNKNOWN_CONN_ID, "Expected connection timeout"); + + k_sem_give(&sem_connected); + bt_conn_unref(conn); +} + +static struct bt_conn_cb conn_cb = { + .connected = connected_cb, +}; + +static void test_central_connect_timeout_with_timeout(uint32_t timeout_ms) +{ + int err; + struct bt_conn *conn; + + /* A zero value for `bt_conn_le_create_param.timeout` shall be + * interpreted as `CONFIG_BT_CREATE_CONN_TIMEOUT`. + */ + uint32_t expected_conn_timeout_ms = + timeout_ms ? timeout_ms : CONFIG_BT_CREATE_CONN_TIMEOUT * MSEC_PER_SEC; + + bt_addr_le_t peer = {.a.val = {0x01}}; + const struct bt_conn_le_create_param create_param = { + .options = BT_CONN_LE_OPT_NONE, + .interval = BT_GAP_SCAN_FAST_INTERVAL, + .window = BT_GAP_SCAN_FAST_WINDOW, + .interval_coded = 0, + .window_coded = 0, + .timeout = timeout_ms / 10, + }; + + k_sem_reset(&sem_connected); + + const uint64_t conn_create_start = k_uptime_get(); + + err = bt_conn_le_create(&peer, &create_param, BT_LE_CONN_PARAM_DEFAULT, &conn); + TEST_ASSERT(err == 0, "Failed starting initiator (err %d)", err); + + err = k_sem_take(&sem_connected, K_MSEC(2 * expected_conn_timeout_ms)); + TEST_ASSERT(err == 0, "Failed getting connected timeout within %d s (err %d)", + 2 * expected_conn_timeout_ms, err); + + const uint64_t conn_create_end = k_uptime_get(); + + const int64_t time_diff_ms = conn_create_end - conn_create_start; + const int64_t diff_to_expected_ms = abs(time_diff_ms - expected_conn_timeout_ms); + + TEST_PRINT("Connection timeout after %d ms", time_diff_ms); + TEST_ASSERT(diff_to_expected_ms < 0.1 * expected_conn_timeout_ms, + "Connection timeout not within 10%% of expected timeout. " + "Actual timeout: %d", time_diff_ms); +} + +static void test_central_connect_timeout(void) +{ + int err; + + bt_conn_cb_register(&conn_cb); + + /* Initialize Bluetooth */ + err = bt_enable(NULL); + TEST_ASSERT(err == 0, "Can't enable Bluetooth (err %d)", err); + + test_central_connect_timeout_with_timeout(0); + test_central_connect_timeout_with_timeout(1000); + + TEST_PASS("Correct timeout"); +} + +static const struct bst_test_instance test_def[] = { + { + .test_id = "central_connect_timeout", + .test_descr = "Verifies that the default connection timeout is used correctly", + .test_tick_f = bst_tick, + .test_main_f = test_central_connect_timeout + }, + BSTEST_END_MARKER +}; + +static struct bst_test_list *test_central_install(struct bst_test_list *tests) +{ + return bst_add_tests(tests, test_def); +} + +bst_test_install_t test_installers[] = { + test_central_install, + NULL +}; + +int main(void) +{ + bst_main(); + return 0; +} diff --git a/tests/bsim/bluetooth/host/central/tests_scripts/_compile.sh b/tests/bsim/bluetooth/host/central/tests_scripts/_compile.sh new file mode 100755 index 00000000000..f46c09eab66 --- /dev/null +++ b/tests/bsim/bluetooth/host/central/tests_scripts/_compile.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +# Copyright 2024 Nordic Semiconductor ASA +# SPDX-License-Identifier: Apache-2.0 +set -eu +: "${ZEPHYR_BASE:?ZEPHYR_BASE must be defined}" + +INCR_BUILD=1 + +source ${ZEPHYR_BASE}/tests/bsim/compile.source + +app="$(guess_test_relpath)" compile + +wait_for_background_jobs diff --git a/tests/bsim/bluetooth/host/central/tests_scripts/run_central_connect_timeout.sh b/tests/bsim/bluetooth/host/central/tests_scripts/run_central_connect_timeout.sh new file mode 100755 index 00000000000..3b0cdcab24e --- /dev/null +++ b/tests/bsim/bluetooth/host/central/tests_scripts/run_central_connect_timeout.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +# Copyright (c) 2024 Nordic Semiconductor +# SPDX-License-Identifier: Apache-2.0 + +source ${ZEPHYR_BASE}/tests/bsim/sh_common.source + +simulation_id="central_connect_timeout" +verbosity_level=2 + +cd ${BSIM_OUT_PATH}/bin + +Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_host_central_prj_conf \ + -v=${verbosity_level} -s=${simulation_id} -d=0 -testid=central_connect_timeout + +Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \ + -D=1 -sim_length=60e6 $@ + +wait_for_background_jobs diff --git a/tests/bsim/bluetooth/host/compile.sh b/tests/bsim/bluetooth/host/compile.sh index 265bb6fae39..1b7960d47b7 100755 --- a/tests/bsim/bluetooth/host/compile.sh +++ b/tests/bsim/bluetooth/host/compile.sh @@ -12,6 +12,7 @@ set -ue source ${ZEPHYR_BASE}/tests/bsim/compile.source ${ZEPHYR_BASE}/tests/bsim/bluetooth/host/adv/compile.sh +app=tests/bsim/bluetooth/host/central compile ${ZEPHYR_BASE}/tests/bsim/bluetooth/host/att/compile.sh ${ZEPHYR_BASE}/tests/bsim/bluetooth/host/gatt/compile.sh ${ZEPHYR_BASE}/tests/bsim/bluetooth/host/l2cap/compile.sh