Skip to content

Commit b30d2d1

Browse files
rugeGerritsenaescolar
authored andcommitted
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 <[email protected]>
1 parent e8b500f commit b30d2d1

File tree

6 files changed

+167
-0
lines changed

6 files changed

+167
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(central)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources} )
10+
11+
# This contains babblesim-specific helpers, e.g. device synchronization.
12+
add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit)
13+
target_link_libraries(app PRIVATE babblekit)
14+
15+
zephyr_include_directories(
16+
${BSIM_COMPONENTS_PATH}/libUtilv1/src/
17+
${BSIM_COMPONENTS_PATH}/libPhyComv1/src/
18+
)
19+
20+
add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/common/testlib testlib)
21+
target_link_libraries(app PRIVATE
22+
testlib
23+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_ASSERT=y
2+
CONFIG_BT=y
3+
CONFIG_BT_CENTRAL=y
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "bstests.h"
8+
#include "babblekit/testcase.h"
9+
#include <zephyr/bluetooth/conn.h>
10+
11+
static K_SEM_DEFINE(sem_connected, 0, 1);
12+
13+
static void connected_cb(struct bt_conn *conn, uint8_t err)
14+
{
15+
TEST_ASSERT(conn);
16+
TEST_ASSERT(err == BT_HCI_ERR_UNKNOWN_CONN_ID, "Expected connection timeout");
17+
18+
k_sem_give(&sem_connected);
19+
bt_conn_unref(conn);
20+
}
21+
22+
static struct bt_conn_cb conn_cb = {
23+
.connected = connected_cb,
24+
};
25+
26+
static void test_central_connect_timeout_with_timeout(uint32_t timeout_ms)
27+
{
28+
int err;
29+
struct bt_conn *conn;
30+
31+
/* A zero value for `bt_conn_le_create_param.timeout` shall be
32+
* interpreted as `CONFIG_BT_CREATE_CONN_TIMEOUT`.
33+
*/
34+
uint32_t expected_conn_timeout_ms =
35+
timeout_ms ? timeout_ms : CONFIG_BT_CREATE_CONN_TIMEOUT * MSEC_PER_SEC;
36+
37+
bt_addr_le_t peer = {.a.val = {0x01}};
38+
const struct bt_conn_le_create_param create_param = {
39+
.options = BT_CONN_LE_OPT_NONE,
40+
.interval = BT_GAP_SCAN_FAST_INTERVAL,
41+
.window = BT_GAP_SCAN_FAST_WINDOW,
42+
.interval_coded = 0,
43+
.window_coded = 0,
44+
.timeout = timeout_ms / 10,
45+
};
46+
47+
k_sem_reset(&sem_connected);
48+
49+
const uint64_t conn_create_start = k_uptime_get();
50+
51+
err = bt_conn_le_create(&peer, &create_param, BT_LE_CONN_PARAM_DEFAULT, &conn);
52+
TEST_ASSERT(err == 0, "Failed starting initiator (err %d)", err);
53+
54+
err = k_sem_take(&sem_connected, K_MSEC(2 * expected_conn_timeout_ms));
55+
TEST_ASSERT(err == 0, "Failed getting connected timeout within %d s (err %d)",
56+
2 * expected_conn_timeout_ms, err);
57+
58+
const uint64_t conn_create_end = k_uptime_get();
59+
60+
const int64_t time_diff_ms = conn_create_end - conn_create_start;
61+
const int64_t diff_to_expected_ms = abs(time_diff_ms - expected_conn_timeout_ms);
62+
63+
TEST_PRINT("Connection timeout after %d ms", time_diff_ms);
64+
TEST_ASSERT(diff_to_expected_ms < 0.1 * expected_conn_timeout_ms,
65+
"Connection timeout not within 10%% of expected timeout. "
66+
"Actual timeout: %d", time_diff_ms);
67+
}
68+
69+
static void test_central_connect_timeout(void)
70+
{
71+
int err;
72+
73+
bt_conn_cb_register(&conn_cb);
74+
75+
/* Initialize Bluetooth */
76+
err = bt_enable(NULL);
77+
TEST_ASSERT(err == 0, "Can't enable Bluetooth (err %d)", err);
78+
79+
test_central_connect_timeout_with_timeout(0);
80+
test_central_connect_timeout_with_timeout(1000);
81+
82+
TEST_PASS("Correct timeout");
83+
}
84+
85+
static const struct bst_test_instance test_def[] = {
86+
{
87+
.test_id = "central_connect_timeout",
88+
.test_descr = "Verifies that the default connection timeout is used correctly",
89+
.test_tick_f = bst_tick,
90+
.test_main_f = test_central_connect_timeout
91+
},
92+
BSTEST_END_MARKER
93+
};
94+
95+
static struct bst_test_list *test_central_install(struct bst_test_list *tests)
96+
{
97+
return bst_add_tests(tests, test_def);
98+
}
99+
100+
bst_test_install_t test_installers[] = {
101+
test_central_install,
102+
NULL
103+
};
104+
105+
int main(void)
106+
{
107+
bst_main();
108+
return 0;
109+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2024 Nordic Semiconductor ASA
3+
# SPDX-License-Identifier: Apache-2.0
4+
set -eu
5+
: "${ZEPHYR_BASE:?ZEPHYR_BASE must be defined}"
6+
7+
INCR_BUILD=1
8+
9+
source ${ZEPHYR_BASE}/tests/bsim/compile.source
10+
11+
app="$(guess_test_relpath)" compile
12+
13+
wait_for_background_jobs
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
# Copyright (c) 2024 Nordic Semiconductor
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
source ${ZEPHYR_BASE}/tests/bsim/sh_common.source
6+
7+
simulation_id="central_connect_timeout"
8+
verbosity_level=2
9+
10+
cd ${BSIM_OUT_PATH}/bin
11+
12+
Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_host_central_prj_conf \
13+
-v=${verbosity_level} -s=${simulation_id} -d=0 -testid=central_connect_timeout
14+
15+
Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \
16+
-D=1 -sim_length=60e6 $@
17+
18+
wait_for_background_jobs

tests/bsim/bluetooth/host/compile.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ set -ue
1212
source ${ZEPHYR_BASE}/tests/bsim/compile.source
1313

1414
${ZEPHYR_BASE}/tests/bsim/bluetooth/host/adv/compile.sh
15+
app=tests/bsim/bluetooth/host/central compile
1516
${ZEPHYR_BASE}/tests/bsim/bluetooth/host/att/compile.sh
1617
${ZEPHYR_BASE}/tests/bsim/bluetooth/host/gatt/compile.sh
1718
${ZEPHYR_BASE}/tests/bsim/bluetooth/host/l2cap/compile.sh

0 commit comments

Comments
 (0)