Skip to content

Commit

Permalink
tests: Bluetooth: Audio: Remove uses of K_FOREVER in syswg for TX
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
Thalley committed Nov 22, 2024
1 parent 054d60f commit 33f39e6
Show file tree
Hide file tree
Showing 12 changed files with 433 additions and 392 deletions.
6 changes: 3 additions & 3 deletions samples/bluetooth/bap_unicast_client/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ static void stream_enabled(struct bt_bap_stream *stream)
k_sem_give(&sem_stream_enabled);
}

static bool stream_is_tx(const struct bt_bap_stream *stream)
static bool stream_tx_can_send(const struct bt_bap_stream *stream)
{
struct bt_bap_ep_info info;
int err;
Expand Down Expand Up @@ -272,7 +272,7 @@ static void stream_started(struct bt_bap_stream *stream)
{
printk("Audio Stream %p started\n", stream);
/* Register the stream for TX if it can send */
if (IS_ENABLED(CONFIG_BT_AUDIO_TX) && stream_is_tx(stream)) {
if (IS_ENABLED(CONFIG_BT_AUDIO_TX) && stream_tx_can_send(stream)) {
const int err = stream_tx_register(stream);

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

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

if (err != 0) {
Expand Down
2 changes: 1 addition & 1 deletion tests/bsim/bluetooth/audio/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ CONFIG_BT_BAP_BROADCAST_SNK_STREAM_COUNT=2
CONFIG_BT_ISO_PERIPHERAL=y
CONFIG_BT_ISO_MAX_CHAN=4
CONFIG_BT_ISO_TX_MTU=310
CONFIG_BT_ISO_TX_BUF_COUNT=4
CONFIG_BT_ISO_TX_BUF_COUNT=8
CONFIG_BT_ISO_RX_MTU=310
CONFIG_BT_ISO_RX_BUF_COUNT=4

Expand Down
94 changes: 16 additions & 78 deletions tests/bsim/bluetooth/audio/src/bap_broadcast_source_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <zephyr/toolchain.h>

#include "bap_common.h"
#include "bap_stream_tx.h"
#include "bstests.h"
#include "common.h"

Expand All @@ -35,22 +36,7 @@
#define SUPPORTED_MAX_FRAMES_PER_SDU 1

#if defined(CONFIG_BT_BAP_BROADCAST_SOURCE)
/* When BROADCAST_ENQUEUE_COUNT > 1 we can enqueue enough buffers to ensure that
* the controller is never idle
*/
#define BROADCAST_ENQUEUE_COUNT 2U
#define TOTAL_BUF_NEEDED (BROADCAST_ENQUEUE_COUNT * CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT)

BUILD_ASSERT(CONFIG_BT_ISO_TX_BUF_COUNT >= TOTAL_BUF_NEEDED,
"CONFIG_BT_ISO_TX_BUF_COUNT should be at least "
"BROADCAST_ENQUEUE_COUNT * CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT");

NET_BUF_POOL_FIXED_DEFINE(tx_pool,
TOTAL_BUF_NEEDED,
BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU),
CONFIG_BT_CONN_TX_USER_DATA_SIZE, NULL);

extern enum bst_result_t bst_result;
static struct audio_test_stream broadcast_source_streams[CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT];
static struct bt_bap_lc3_preset preset_16_2_1 = BT_BAP_LC3_BROADCAST_PRESET_16_2_1(
BT_AUDIO_LOCATION_FRONT_LEFT, BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
Expand Down Expand Up @@ -214,60 +200,36 @@ static void started_cb(struct bt_bap_stream *stream)
return;
}

err = stream_tx_register(stream);
if (err != 0) {
FAIL("Failed to register stream %p for TX: %d\n", stream, err);
return;
}

printk("Stream %p started\n", stream);
validate_stream_codec_cfg(stream);
k_sem_give(&sem_started);
}

static void stopped_cb(struct bt_bap_stream *stream, uint8_t reason)
{
printk("Stream %p stopped with reason 0x%02X\n", stream, reason);
k_sem_give(&sem_stopped);
}

static void stream_sent_cb(struct bt_bap_stream *stream)
{
struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream);
struct net_buf *buf;
int ret;

if (!test_stream->tx_active) {
return;
}

if ((test_stream->tx_cnt % 100U) == 0U) {
printk("Sent with seq_num %u\n", test_stream->seq_num);
}

buf = net_buf_alloc(&tx_pool, K_FOREVER);
if (buf == NULL) {
printk("Could not allocate buffer when sending on %p\n",
stream);
return;
}

net_buf_reserve(buf, BT_ISO_CHAN_SEND_RESERVE);
net_buf_add_mem(buf, mock_iso_data, test_stream->tx_sdu_size);
ret = bt_bap_stream_send(stream, buf, test_stream->seq_num++);
if (ret < 0) {
/* This will end broadcasting on this stream. */
net_buf_unref(buf);
int err;

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

err = stream_tx_unregister(stream);
if (err != 0) {
FAIL("Failed to unregister stream %p for TX: %d\n", stream, err);
return;
}

test_stream->tx_cnt++;
k_sem_give(&sem_stopped);
}

static struct bt_bap_stream_ops stream_ops = {
.started = started_cb,
.stopped = stopped_cb,
.sent = stream_sent_cb,
.sent = stream_tx_sent_cb,
};

static int setup_broadcast_source(struct bt_bap_broadcast_source **source, bool encryption)
Expand Down Expand Up @@ -509,10 +471,6 @@ static void test_broadcast_source_stop(struct bt_bap_broadcast_source *source)

printk("Stopping broadcast source\n");

for (size_t i = 0U; i < ARRAY_SIZE(broadcast_source_streams); i++) {
broadcast_source_streams[i].tx_active = false;
}

err = bt_bap_broadcast_source_stop(source);
if (err != 0) {
FAIL("Unable to stop broadcast source: %d\n", err);
Expand Down Expand Up @@ -577,6 +535,7 @@ static void test_main(void)
}

printk("Bluetooth initialized\n");
stream_tx_init();

err = setup_broadcast_source(&source, false);
if (err != 0) {
Expand All @@ -594,17 +553,6 @@ static void test_main(void)

test_broadcast_source_start(source, adv);

/* Initialize sending */
printk("Sending data\n");
for (size_t i = 0U; i < ARRAY_SIZE(broadcast_source_streams); i++) {
for (unsigned int j = 0U; j < BROADCAST_ENQUEUE_COUNT; j++) {
struct audio_test_stream *test_stream = &broadcast_source_streams[i];

test_stream->tx_active = true;
stream_sent_cb(&test_stream->stream.bap_stream);
}
}

/* Wait for other devices to have received what they wanted */
backchannel_sync_wait_any();

Expand Down Expand Up @@ -657,6 +605,7 @@ static void test_main_encrypted(void)
}

printk("Bluetooth initialized\n");
stream_tx_init();

err = setup_broadcast_source(&source, true);
if (err != 0) {
Expand All @@ -672,17 +621,6 @@ static void test_main_encrypted(void)

test_broadcast_source_start(source, adv);

/* Initialize sending */
printk("Sending data\n");
for (size_t i = 0U; i < ARRAY_SIZE(broadcast_source_streams); i++) {
for (unsigned int j = 0U; j < BROADCAST_ENQUEUE_COUNT; j++) {
struct audio_test_stream *test_stream = &broadcast_source_streams[i];

test_stream->tx_active = true;
stream_sent_cb(&test_stream->stream.bap_stream);
}
}

/* Wait for other devices to have received data */
backchannel_sync_wait_any();

Expand Down
14 changes: 7 additions & 7 deletions tests/bsim/bluetooth/audio/src/bap_stream_rx.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
#include "common.h"
#include <string.h>

LOG_MODULE_REGISTER(stream_tx, LOG_LEVEL_INF);
LOG_MODULE_REGISTER(stream_rx, LOG_LEVEL_INF);

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

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

Expand All @@ -35,29 +35,29 @@ void bap_stream_rx_recv_cb(struct bt_bap_stream *stream, const struct bt_iso_rec

if (test_stream->rx_cnt > 0U && info->ts == test_stream->last_info.ts) {
log_stream_rx(stream, info, buf);
FAIL("Duplicated timestamp received: %u\n", test_stream->last_info.ts);
FAIL("Duplicated timestamp received: %u", test_stream->last_info.ts);
return;
}

if (test_stream->rx_cnt > 0U && info->seq_num == test_stream->last_info.seq_num) {
log_stream_rx(stream, info, buf);
FAIL("Duplicated PSN received: %u\n", test_stream->last_info.seq_num);
FAIL("Duplicated PSN received: %u", test_stream->last_info.seq_num);
return;
}

if (info->flags & BT_ISO_FLAGS_ERROR) {
/* Fail the test if we have not received what we expected */
if (!TEST_FLAG(flag_audio_received)) {
log_stream_rx(stream, info, buf);
FAIL("ISO receive error\n");
FAIL("ISO receive error");
}

return;
}

if (info->flags & BT_ISO_FLAGS_LOST) {
log_stream_rx(stream, info, buf);
FAIL("ISO receive lost\n");
FAIL("ISO receive lost");
return;
}

Expand All @@ -70,6 +70,6 @@ void bap_stream_rx_recv_cb(struct bt_bap_stream *stream, const struct bt_iso_rec
}
} else {
log_stream_rx(stream, info, buf);
FAIL("Unexpected data received\n");
FAIL("Unexpected data received");
}
}
Loading

0 comments on commit 33f39e6

Please sign in to comment.