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 <rubin.gerritsen@nordicsemi.no>
This commit is contained in:
Rubin Gerritsen 2024-06-03 13:32:52 +02:00 committed by Alberto Escolar
commit b30d2d1bf5
6 changed files with 167 additions and 0 deletions

View file

@ -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
)

View file

@ -0,0 +1,3 @@
CONFIG_ASSERT=y
CONFIG_BT=y
CONFIG_BT_CENTRAL=y

View file

@ -0,0 +1,109 @@
/*
* Copyright (c) 2024 Nordic Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "bstests.h"
#include "babblekit/testcase.h"
#include <zephyr/bluetooth/conn.h>
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;
}

View file

@ -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

View file

@ -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

View file

@ -12,6 +12,7 @@ set -ue
source ${ZEPHYR_BASE}/tests/bsim/compile.source source ${ZEPHYR_BASE}/tests/bsim/compile.source
${ZEPHYR_BASE}/tests/bsim/bluetooth/host/adv/compile.sh ${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/att/compile.sh
${ZEPHYR_BASE}/tests/bsim/bluetooth/host/gatt/compile.sh ${ZEPHYR_BASE}/tests/bsim/bluetooth/host/gatt/compile.sh
${ZEPHYR_BASE}/tests/bsim/bluetooth/host/l2cap/compile.sh ${ZEPHYR_BASE}/tests/bsim/bluetooth/host/l2cap/compile.sh