Skip to content

Commit 163a750

Browse files
committed
tests: Bluetooth: BSIM: ISO: Refactor TX
Refactor the TX for the ISO host BSIM tests to use a separate thread, instead of a timer. This is the recommended way to handle TX, and is what the LE Audio BSIM tests and samples use. The TX will send 1->sdu number of octets, increasing, to verify that any size can be sent (and to keep the check for this in the bis_receiver). Signed-off-by: Emil Gydesen <[email protected]>
1 parent b75f069 commit 163a750

File tree

10 files changed

+424
-207
lines changed

10 files changed

+424
-207
lines changed

tests/bsim/bluetooth/host/compile.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ set -ue
1212
#Set a default value to BOARD if it does not have one yet
1313
BOARD="${BOARD:-nrf52_bsim/native}"
1414

15-
west twister -T ${ZEPHYR_BASE}/tests/bsim/bluetooth/host/ -p ${BOARD}
15+
west twister -T ${ZEPHYR_BASE}/tests/bsim/bluetooth/host/iso -p ${BOARD}

tests/bsim/bluetooth/host/iso/bis/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ target_sources(app PRIVATE
1313
src/bis_broadcaster.c
1414
src/bis_receiver.c
1515
src/main.c
16+
17+
${ZEPHYR_BASE}/tests/bsim/bluetooth/host/iso/common/iso_tx.c
1618
)
1719

1820
zephyr_include_directories(
1921
${BSIM_COMPONENTS_PATH}/libUtilv1/src/
2022
${BSIM_COMPONENTS_PATH}/libPhyComv1/src/
23+
24+
${ZEPHYR_BASE}/tests/bsim/bluetooth/host/iso/common
2125
)

tests/bsim/bluetooth/host/iso/bis/src/bis_broadcaster.c

Lines changed: 14 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "babblekit/testcase.h"
2727
#include "bstests.h"
2828
#include "common.h"
29+
#include "iso_tx.h"
2930

3031
LOG_MODULE_REGISTER(bis_broadcaster, LOG_LEVEL_INF);
3132

@@ -34,10 +35,6 @@ LOG_MODULE_REGISTER(bis_broadcaster, LOG_LEVEL_INF);
3435
extern enum bst_result_t bst_result;
3536
static struct bt_iso_chan iso_chans[CONFIG_BT_ISO_MAX_CHAN];
3637
static struct bt_iso_chan *default_chan = &iso_chans[0];
37-
static uint16_t seq_num;
38-
NET_BUF_POOL_FIXED_DEFINE(tx_pool, CONFIG_BT_ISO_TX_BUF_COUNT,
39-
BT_ISO_SDU_BUF_SIZE(ARRAY_SIZE(mock_iso_data)),
40-
CONFIG_BT_CONN_TX_USER_DATA_SIZE, NULL);
4138

4239
static struct bt_iso_chan_io_qos iso_tx = {
4340
.sdu = 0U,
@@ -52,54 +49,6 @@ static struct bt_iso_chan_qos iso_qos = {
5249

5350
DEFINE_FLAG_STATIC(flag_iso_connected);
5451

55-
static void send_data_cb(struct k_work *work);
56-
K_WORK_DELAYABLE_DEFINE(iso_send_work, send_data_cb);
57-
58-
static void send_data(struct bt_iso_chan *chan)
59-
{
60-
static size_t len_to_send = 1U;
61-
struct net_buf *buf;
62-
int ret;
63-
64-
if (!IS_FLAG_SET(flag_iso_connected)) {
65-
/* TX has been aborted */
66-
return;
67-
}
68-
69-
buf = net_buf_alloc(&tx_pool, K_NO_WAIT);
70-
TEST_ASSERT(buf != NULL, "Failed to allocate buffer");
71-
72-
net_buf_reserve(buf, BT_ISO_CHAN_SEND_RESERVE);
73-
74-
net_buf_add_mem(buf, mock_iso_data, len_to_send);
75-
76-
ret = bt_iso_chan_send(default_chan, buf, seq_num++);
77-
if (ret < 0) {
78-
LOG_DBG("Failed to send ISO data: %d", ret);
79-
net_buf_unref(buf);
80-
81-
/* Reschedule for next interval */
82-
k_work_reschedule(&iso_send_work, K_USEC(SDU_INTERVAL_US));
83-
84-
return;
85-
}
86-
87-
len_to_send++;
88-
if (len_to_send > chan->qos->tx->sdu) {
89-
len_to_send = 1;
90-
}
91-
}
92-
93-
static void send_data_cb(struct k_work *work)
94-
{
95-
const uint16_t tx_pool_cnt = tx_pool.uninit_count;
96-
97-
/* Send/enqueue as many as we can */
98-
for (uint16_t i = 0U; i < tx_pool_cnt; i++) {
99-
send_data(default_chan);
100-
}
101-
}
102-
10352
static void iso_connected_cb(struct bt_iso_chan *chan)
10453
{
10554
const struct bt_iso_chan_path hci_path = {
@@ -151,43 +100,38 @@ static void iso_connected_cb(struct bt_iso_chan *chan)
151100
IN_RANGE(info.broadcaster.bis_number, BT_ISO_BIS_INDEX_MIN, BT_ISO_BIS_INDEX_MAX),
152101
"Invalid BIS number 0x%02x", info.broadcaster.bis_number);
153102

103+
err = bt_iso_setup_data_path(chan, BT_HCI_DATAPATH_DIR_HOST_TO_CTLR, &hci_path);
104+
TEST_ASSERT(err == 0, "Failed to set ISO data path: %d", err);
105+
154106
if (chan == default_chan) {
155-
seq_num = 0U;
107+
/* Register for TX to start sending */
108+
err = iso_tx_register(chan);
109+
TEST_ASSERT(err == 0, "Failed to register chan for TX: %d", err);
156110

157111
SET_FLAG(flag_iso_connected);
158112
}
159-
160-
err = bt_iso_setup_data_path(chan, BT_HCI_DATAPATH_DIR_HOST_TO_CTLR, &hci_path);
161-
TEST_ASSERT(err == 0, "Failed to set ISO data path: %d", err);
162113
}
163114

164115
static void iso_disconnected_cb(struct bt_iso_chan *chan, uint8_t reason)
165116
{
166117
LOG_INF("ISO Channel %p disconnected (reason 0x%02x)", chan, reason);
167118

168119
if (chan == default_chan) {
169-
k_work_cancel_delayable(&iso_send_work);
120+
int err;
170121

171-
UNSET_FLAG(flag_iso_connected);
172-
}
173-
}
122+
err = iso_tx_unregister(chan);
123+
TEST_ASSERT(err == 0, "Failed to unregister chan for TX: %d", err);
174124

175-
static void sdu_sent_cb(struct bt_iso_chan *chan)
176-
{
177-
if (!IS_FLAG_SET(flag_iso_connected)) {
178-
/* TX has been aborted */
179-
return;
125+
UNSET_FLAG(flag_iso_connected);
180126
}
181-
182-
send_data(chan);
183127
}
184128

185129
static void init(void)
186130
{
187131
static struct bt_iso_chan_ops iso_ops = {
188132
.disconnected = iso_disconnected_cb,
189133
.connected = iso_connected_cb,
190-
.sent = sdu_sent_cb,
134+
.sent = iso_tx_sent_cb,
191135
};
192136
struct bt_le_local_features local_features;
193137
int err;
@@ -212,6 +156,8 @@ static void init(void)
212156
}
213157

214158
bk_sync_init();
159+
160+
iso_tx_init();
215161
}
216162

217163
static void create_ext_adv(struct bt_le_ext_adv **adv)
@@ -277,18 +223,6 @@ static void create_big(struct bt_le_ext_adv *adv, size_t cnt, struct bt_iso_big
277223
WAIT_FOR_FLAG(flag_iso_connected);
278224
}
279225

280-
static void start_tx(void)
281-
{
282-
const uint16_t tx_pool_cnt = tx_pool.uninit_count;
283-
284-
LOG_INF("Starting TX");
285-
286-
/* Send/enqueue as many as we can */
287-
for (uint16_t i = 0U; i < tx_pool_cnt; i++) {
288-
send_data(default_chan);
289-
}
290-
}
291-
292226
static void terminate_big(struct bt_iso_big *big)
293227
{
294228
int err;
@@ -325,7 +259,6 @@ static void test_main(void)
325259
create_ext_adv(&adv);
326260
create_big(adv, 1U, &big);
327261
start_ext_adv(adv);
328-
start_tx();
329262

330263
/* Wait for receiver to tell us to terminate */
331264
bk_sync_wait();
@@ -358,7 +291,6 @@ static void test_main_disable(void)
358291
create_ext_adv(&adv);
359292
create_big(adv, ARRAY_SIZE(iso_chans), &big);
360293
start_ext_adv(adv);
361-
start_tx();
362294

363295
/* Wait for receiver to tell us to terminate */
364296
bk_sync_wait();
@@ -400,7 +332,6 @@ static void test_main_fragment(void)
400332
create_ext_adv(&adv);
401333
create_big(adv, 1U, &big);
402334
start_ext_adv(adv);
403-
start_tx();
404335

405336
/* Wait for receiver to tell us to terminate */
406337
bk_sync_wait();

tests/bsim/bluetooth/host/iso/bis/src/bis_receiver.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "babblekit/testcase.h"
2121

2222
#include "common.h"
23+
#include "iso_tx.h"
2324

2425
LOG_MODULE_REGISTER(bis_receiver, LOG_LEVEL_INF);
2526

@@ -89,20 +90,22 @@ static void iso_recv(struct bt_iso_chan *chan, const struct bt_iso_recv_info *in
8990
if (info->flags & BT_ISO_FLAGS_VALID) {
9091
static uint16_t last_buf_len;
9192
static uint32_t last_ts;
93+
static size_t pass_cnt;
9294
static size_t rx_cnt;
9395

94-
LOG_DBG("Incoming data channel %p len %u", chan, buf->len);
96+
rx_cnt++;
97+
LOG_DBG("[%zu]: Incoming data channel %p len %u", rx_cnt, chan, buf->len);
9598
iso_log_data(buf->data, buf->len);
9699

97100
if (memcmp(buf->data, mock_iso_data, buf->len) != 0) {
98101
TEST_FAIL("Unexpected data received");
99102
} else if (last_buf_len != 0U && buf->len != 1U && buf->len != last_buf_len + 1) {
100103
TEST_FAIL("Unexpected data length (%u) received (expected 1 or %u)",
101-
buf->len, last_buf_len);
104+
buf->len, last_buf_len + 1);
102105
} else if (last_ts != 0U && info->ts > last_ts + 2 * SDU_INTERVAL_US) {
103106
TEST_FAIL("Unexpected timestamp (%u) received (expected %u)", info->ts,
104107
last_ts + SDU_INTERVAL_US);
105-
} else if (rx_cnt++ > RX_CNT_TO_PASS) {
108+
} else if (pass_cnt++ > RX_CNT_TO_PASS) {
106109
LOG_INF("Data received");
107110
SET_FLAG(flag_data_received);
108111
}

tests/bsim/bluetooth/host/iso/bis/src/common.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include <stdint.h>
1010

11-
#include <zephyr/sys/util_macro.h>
11+
#include <zephyr/sys/clock.h>
1212

1313
#include "bs_types.h"
1414

@@ -18,9 +18,3 @@ void test_init(void);
1818
void test_tick(bs_time_t HW_device_time);
1919

2020
#define SDU_INTERVAL_US 10U * USEC_PER_MSEC /* 10 ms */
21-
22-
/* Generate 1 KiB of mock data going 0x00, 0x01, ..., 0xff, 0x00, 0x01, ..., 0xff, etc */
23-
#define ISO_DATA_GEN(_i, _) (uint8_t)_i
24-
static const uint8_t mock_iso_data[] = {
25-
LISTIFY(1024, ISO_DATA_GEN, (,)),
26-
};

tests/bsim/bluetooth/host/iso/cis/CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,24 @@ cmake_minimum_required(VERSION 3.20.0)
55
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
66
project(bsim_test_iso_cis)
77

8-
add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit)
9-
target_link_libraries(app PRIVATE babblekit)
10-
118
target_sources(app PRIVATE
129
src/common.c
1310
src/cis_central.c
1411
src/cis_peripheral.c
1512
src/main.c
13+
14+
${ZEPHYR_BASE}/tests/bsim/bluetooth/host/iso/common/iso_tx.c
1615
)
1716

1817
zephyr_include_directories(
1918
${BSIM_COMPONENTS_PATH}/libUtilv1/src/
2019
${BSIM_COMPONENTS_PATH}/libPhyComv1/src/
21-
)
20+
21+
${ZEPHYR_BASE}/tests/bsim/bluetooth/host/iso/common
22+
)
2223

2324
add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/common/testlib testlib)
25+
add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit)
2426

2527
target_link_libraries(app PRIVATE
2628
testlib

0 commit comments

Comments
 (0)