Skip to content

Commit 93997da

Browse files
mjchen0danieldegrasse
authored andcommitted
tests: bsim: add early disconnect tests for iso and l2cap
Add early_disconnect test cases for iso/cis and l2cap/stress to test the bluetooth stack handling of disconnects while transmit is still in progress. Signed-off-by: Mike J. Chen <mjchen@google.com>
1 parent a392c33 commit 93997da

File tree

8 files changed

+327
-12
lines changed

8 files changed

+327
-12
lines changed

tests/bsim/bluetooth/host/iso/cis/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ CONFIG_BT_ISO_TX_MTU=200
1616
CONFIG_BT_ISO_RX_MTU=200
1717

1818
CONFIG_BT_ISO_LOG_LEVEL_DBG=y
19+
CONFIG_NET_BUF_POOL_USAGE=y
1920

2021
# Controller Connected ISO configs
2122
CONFIG_BT_CTLR_CENTRAL_ISO=y

tests/bsim/bluetooth/host/iso/cis/src/cis_central.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ static void iso_disconnected(struct bt_iso_chan *chan, uint8_t reason)
154154

155155
err = bt_iso_remove_data_path(chan, BT_HCI_DATAPATH_DIR_HOST_TO_CTLR);
156156
TEST_ASSERT(err == 0, "Failed to remove ISO data path: %d", err);
157+
158+
if (seq_num < 100) {
159+
printk("Channel disconnected early, bumping seq_num to 1000 to end test\n");
160+
seq_num = 1000;
161+
}
157162
}
158163

159164
static void sdu_sent_cb(struct bt_iso_chan *chan)
@@ -462,9 +467,16 @@ static void test_main(void)
462467
k_sleep(K_USEC(interval_us));
463468
}
464469

465-
disconnect_cis();
466-
disconnect_acl();
467-
terminate_cig();
470+
if (seq_num == 100) {
471+
disconnect_cis();
472+
disconnect_acl();
473+
terminate_cig();
474+
}
475+
476+
/* check that all buffers returned to pool */
477+
TEST_ASSERT(atomic_get(&tx_pool.avail_count) == ENQUEUE_COUNT,
478+
"tx_pool has non returned buffers, should be %u but is %u",
479+
ENQUEUE_COUNT, atomic_get(&tx_pool.avail_count));
468480

469481
TEST_PASS("Test passed");
470482
}

tests/bsim/bluetooth/host/iso/cis/src/cis_peripheral.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ static const struct bt_data ad[] = {
3535
};
3636
static struct bt_iso_chan iso_chan;
3737

38+
static size_t disconnect_after_recv_cnt;
39+
static size_t iso_recv_cnt;
40+
3841
/** Print data as d_0 d_1 d_2 ... d_(n-2) d_(n-1) d_(n) to show the 3 first and 3 last octets
3942
*
4043
* Examples:
@@ -72,14 +75,26 @@ static void iso_print_data(uint8_t *data, size_t data_len)
7275
printk("\t %s\n", data_str);
7376
}
7477

78+
static void disconnect_device(struct bt_conn *conn, void *data)
79+
{
80+
int err = bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
81+
82+
TEST_ASSERT(!err, "Failed to initate disconnect (err %d)", err);
83+
}
84+
7585
static void iso_recv(struct bt_iso_chan *chan, const struct bt_iso_recv_info *info,
7686
struct net_buf *buf)
7787
{
88+
iso_recv_cnt++;
7889
if (info->flags & BT_ISO_FLAGS_VALID) {
7990
printk("Incoming data channel %p len %u\n", chan, buf->len);
8091
iso_print_data(buf->data, buf->len);
8192
SET_FLAG(flag_data_received);
8293
}
94+
if (disconnect_after_recv_cnt && (iso_recv_cnt >= disconnect_after_recv_cnt)) {
95+
printk("Disconnecting\n");
96+
bt_conn_foreach(BT_CONN_TYPE_LE, disconnect_device, NULL);
97+
}
8398
}
8499

85100
static void iso_connected(struct bt_iso_chan *chan)
@@ -189,12 +204,33 @@ static void test_main(void)
189204
}
190205
}
191206

207+
static void test_main_early_disconnect(void)
208+
{
209+
init();
210+
211+
disconnect_after_recv_cnt = 10;
212+
213+
while (true) {
214+
adv_connect();
215+
bt_testlib_conn_wait_free();
216+
217+
if (IS_FLAG_SET(flag_data_received)) {
218+
TEST_PASS("Test passed");
219+
}
220+
}
221+
}
222+
192223
static const struct bst_test_instance test_def[] = {
193224
{
194225
.test_id = "peripheral",
195226
.test_descr = "Peripheral",
196227
.test_main_f = test_main,
197228
},
229+
{
230+
.test_id = "peripheral_early_disconnect",
231+
.test_descr = "Peripheral that tests early disconnect",
232+
.test_main_f = test_main_early_disconnect,
233+
},
198234
BSTEST_END_MARKER,
199235
};
200236

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env bash
2+
# Copyright (c) 2025 Google LLC
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
source ${ZEPHYR_BASE}/tests/bsim/sh_common.source
6+
7+
# Tests cleanup within the ble stack for ISO connections when
8+
# peripheral disconnects early while the central still has
9+
# buffers queued for sending. Using the base code, which
10+
# has the central sending 100 ISO packets, the peripheral
11+
# triggers a disconnect after receiving 10 packets.
12+
# The central checks that all tx_pool buffers have been
13+
# returned to the pool after the disconnect.
14+
simulation_id="iso_cis_early_disconnect"
15+
verbosity_level=2
16+
EXECUTE_TIMEOUT=120
17+
18+
cd ${BSIM_OUT_PATH}/bin
19+
20+
Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_host_iso_cis_prj_conf \
21+
-v=${verbosity_level} -s=${simulation_id} -d=0 -testid=central
22+
23+
Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_host_iso_cis_prj_conf \
24+
-v=${verbosity_level} -s=${simulation_id} -d=1 -testid=peripheral_early_disconnect
25+
26+
Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \
27+
-D=2 -sim_length=30e6 $@
28+
29+
wait_for_background_jobs
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_BT_L2CAP_SEG_RECV=y

0 commit comments

Comments
 (0)