Skip to content

Commit 89bfb01

Browse files
committed
tests: Bluetooth: Audio: Remove uses of K_FOREVER in syswg for TX
Several tests were using K_FOREVER when allocating the buffer for TX in the system workqueue, which is illegal behavior. The solution chosen was to create a TX thread to handle TX, similar to the solution used in the audio shell and some sample applications. This way we can continue to use K_FOREVER when allocting buffers and it will always be done in a round-robin fashion while TXing as much as possible, by always enqueuing all the buffers with mock data. Since this works for all streams (both broadcast and unicast), it was obvious to use the same implementation for all tests, and thus cleaning up the tests a bit and more them more similar. Signed-off-by: Emil Gydesen <[email protected]>
1 parent e12096e commit 89bfb01

File tree

11 files changed

+425
-387
lines changed

11 files changed

+425
-387
lines changed

samples/bluetooth/bap_unicast_client/src/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ static void stream_enabled(struct bt_bap_stream *stream)
236236
k_sem_give(&sem_stream_enabled);
237237
}
238238

239-
static bool stream_is_tx(const struct bt_bap_stream *stream)
239+
static bool stream_tx_can_send(const struct bt_bap_stream *stream)
240240
{
241241
struct bt_bap_ep_info info;
242242
int err;
@@ -272,7 +272,7 @@ static void stream_started(struct bt_bap_stream *stream)
272272
{
273273
printk("Audio Stream %p started\n", stream);
274274
/* Register the stream for TX if it can send */
275-
if (IS_ENABLED(CONFIG_BT_AUDIO_TX) && stream_is_tx(stream)) {
275+
if (IS_ENABLED(CONFIG_BT_AUDIO_TX) && stream_tx_can_send(stream)) {
276276
const int err = stream_tx_register(stream);
277277

278278
if (err != 0) {
@@ -298,7 +298,7 @@ static void stream_stopped(struct bt_bap_stream *stream, uint8_t reason)
298298
printk("Audio Stream %p stopped with reason 0x%02X\n", stream, reason);
299299

300300
/* Unregister the stream for TX if it can send */
301-
if (IS_ENABLED(CONFIG_BT_AUDIO_TX) && stream_is_tx(stream)) {
301+
if (IS_ENABLED(CONFIG_BT_AUDIO_TX) && stream_tx_can_send(stream)) {
302302
const int err = stream_tx_unregister(stream);
303303

304304
if (err != 0) {

tests/bsim/bluetooth/audio/src/bap_broadcast_source_test.c

Lines changed: 15 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <zephyr/toolchain.h>
2727

2828
#include "bap_common.h"
29+
#include "bap_stream_tx.h"
2930
#include "bstests.h"
3031
#include "common.h"
3132

@@ -37,22 +38,6 @@
3738
#if defined(CONFIG_BT_BAP_BROADCAST_SOURCE)
3839
CREATE_FLAG(flag_source_started);
3940

40-
/* When BROADCAST_ENQUEUE_COUNT > 1 we can enqueue enough buffers to ensure that
41-
* the controller is never idle
42-
*/
43-
#define BROADCAST_ENQUEUE_COUNT 2U
44-
#define TOTAL_BUF_NEEDED (BROADCAST_ENQUEUE_COUNT * CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT)
45-
46-
BUILD_ASSERT(CONFIG_BT_ISO_TX_BUF_COUNT >= TOTAL_BUF_NEEDED,
47-
"CONFIG_BT_ISO_TX_BUF_COUNT should be at least "
48-
"BROADCAST_ENQUEUE_COUNT * CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT");
49-
50-
NET_BUF_POOL_FIXED_DEFINE(tx_pool,
51-
TOTAL_BUF_NEEDED,
52-
BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU),
53-
CONFIG_BT_CONN_TX_USER_DATA_SIZE, NULL);
54-
55-
extern enum bst_result_t bst_result;
5641
static struct audio_test_stream broadcast_source_streams[CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT];
5742
static struct bt_bap_lc3_preset preset_16_2_1 = BT_BAP_LC3_BROADCAST_PRESET_16_2_1(
5843
BT_AUDIO_LOCATION_FRONT_LEFT, BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
@@ -216,60 +201,36 @@ static void stream_started_cb(struct bt_bap_stream *stream)
216201
return;
217202
}
218203

204+
err = bap_stream_tx_register(stream);
205+
if (err != 0) {
206+
FAIL("Failed to register stream %p for TX: %d\n", stream, err);
207+
return;
208+
}
209+
219210
printk("Stream %p started\n", stream);
220211
validate_stream_codec_cfg(stream);
221212
k_sem_give(&sem_stream_started);
222213
}
223214

224215
static void steam_stopped_cb(struct bt_bap_stream *stream, uint8_t reason)
225216
{
226-
printk("Stream %p stopped with reason 0x%02X\n", stream, reason);
227-
k_sem_give(&sem_stream_stopped);
228-
}
229-
230-
static void stream_sent_cb(struct bt_bap_stream *stream)
231-
{
232-
struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream);
233-
struct net_buf *buf;
234-
int ret;
235-
236-
if (!test_stream->tx_active) {
237-
return;
238-
}
239-
240-
if ((test_stream->tx_cnt % 100U) == 0U) {
241-
printk("Sent with seq_num %u\n", test_stream->seq_num);
242-
}
243-
244-
buf = net_buf_alloc(&tx_pool, K_FOREVER);
245-
if (buf == NULL) {
246-
printk("Could not allocate buffer when sending on %p\n",
247-
stream);
248-
return;
249-
}
250-
251-
net_buf_reserve(buf, BT_ISO_CHAN_SEND_RESERVE);
252-
net_buf_add_mem(buf, mock_iso_data, test_stream->tx_sdu_size);
253-
ret = bt_bap_stream_send(stream, buf, test_stream->seq_num++);
254-
if (ret < 0) {
255-
/* This will end broadcasting on this stream. */
256-
net_buf_unref(buf);
217+
int err;
257218

258-
/* Only fail if tx is active (may fail if we are disabling the stream) */
259-
if (test_stream->tx_active) {
260-
FAIL("Unable to broadcast data on %p: %d\n", stream, ret);
261-
}
219+
printk("Stream %p stopped with reason 0x%02X\n", stream, reason);
262220

221+
err = bap_stream_tx_unregister(stream);
222+
if (err != 0) {
223+
FAIL("Failed to unregister stream %p for TX: %d\n", stream, err);
263224
return;
264225
}
265226

266-
test_stream->tx_cnt++;
227+
k_sem_give(&sem_stream_stopped);
267228
}
268229

269230
static struct bt_bap_stream_ops stream_ops = {
270231
.started = stream_started_cb,
271232
.stopped = steam_stopped_cb,
272-
.sent = stream_sent_cb,
233+
.sent = bap_stream_tx_sent_cb,
273234
};
274235

275236
static void source_started_cb(struct bt_bap_broadcast_source *source)
@@ -525,10 +486,6 @@ static void test_broadcast_source_stop(struct bt_bap_broadcast_source *source)
525486

526487
printk("Stopping broadcast source\n");
527488

528-
for (size_t i = 0U; i < ARRAY_SIZE(broadcast_source_streams); i++) {
529-
broadcast_source_streams[i].tx_active = false;
530-
}
531-
532489
err = bt_bap_broadcast_source_stop(source);
533490
if (err != 0) {
534491
FAIL("Unable to stop broadcast source: %d\n", err);
@@ -597,6 +554,7 @@ static void init(void)
597554
}
598555

599556
printk("Bluetooth initialized\n");
557+
bap_stream_tx_init();
600558

601559
err = bt_bap_broadcast_source_register_cb(&broadcast_source_cb);
602560
if (err != 0) {
@@ -629,17 +587,6 @@ static void test_main(void)
629587

630588
test_broadcast_source_start(source, adv);
631589

632-
/* Initialize sending */
633-
printk("Sending data\n");
634-
for (size_t i = 0U; i < ARRAY_SIZE(broadcast_source_streams); i++) {
635-
for (unsigned int j = 0U; j < BROADCAST_ENQUEUE_COUNT; j++) {
636-
struct audio_test_stream *test_stream = &broadcast_source_streams[i];
637-
638-
test_stream->tx_active = true;
639-
stream_sent_cb(&test_stream->stream.bap_stream);
640-
}
641-
}
642-
643590
/* Wait for other devices to have received what they wanted */
644591
backchannel_sync_wait_any();
645592

@@ -701,17 +648,6 @@ static void test_main_encrypted(void)
701648

702649
test_broadcast_source_start(source, adv);
703650

704-
/* Initialize sending */
705-
printk("Sending data\n");
706-
for (size_t i = 0U; i < ARRAY_SIZE(broadcast_source_streams); i++) {
707-
for (unsigned int j = 0U; j < BROADCAST_ENQUEUE_COUNT; j++) {
708-
struct audio_test_stream *test_stream = &broadcast_source_streams[i];
709-
710-
test_stream->tx_active = true;
711-
stream_sent_cb(&test_stream->stream.bap_stream);
712-
}
713-
}
714-
715651
/* Wait for other devices to have received data */
716652
backchannel_sync_wait_any();
717653

tests/bsim/bluetooth/audio/src/bap_stream_rx.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
#include "common.h"
1414
#include <string.h>
1515

16-
LOG_MODULE_REGISTER(stream_tx, LOG_LEVEL_INF);
16+
LOG_MODULE_REGISTER(bap_stream_rx, LOG_LEVEL_INF);
1717

1818
static void log_stream_rx(struct bt_bap_stream *stream, const struct bt_iso_recv_info *info,
1919
struct net_buf *buf)
2020
{
2121
struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream);
2222

23-
LOG_INF("[%zu]: Incoming audio on stream %p len %u, flags 0x%02X, seq_num %u and ts %u\n",
23+
LOG_INF("[%zu]: Incoming audio on stream %p len %u, flags 0x%02X, seq_num %u and ts %u",
2424
test_stream->rx_cnt, stream, buf->len, info->flags, info->seq_num, info->ts);
2525
}
2626

0 commit comments

Comments
 (0)