From b4f3763c3105e07a0e24a956e5725ba4d3c0c7cf Mon Sep 17 00:00:00 2001 From: Emil Gydesen Date: Mon, 18 Nov 2024 11:43:42 +0100 Subject: [PATCH] tests: Bluetooth: Audio: Use same recv_cb for all tests This commit changes the BSIM tests to use the same recv callback for all tests. The purpose of this is to reduce code duplication and make it easier to maintain the tests. This also changes the recv_cb so that in case of any error we log the most recently received SDU, which should provide more information about why a test failed in case of RX error. PBP had to be updated a bit to support the audio_stream struct. Also modifies a check and log in bap_stream that was less than helpful to determine if it was the stream or the endpoint that was NULL. Signed-off-by: Emil Gydesen --- subsys/bluetooth/audio/bap_stream.c | 8 +- .../audio/src/bap_broadcast_sink_test.c | 61 +++------------ .../audio/src/bap_broadcast_source_test.c | 4 + .../bsim/bluetooth/audio/src/bap_stream_rx.c | 75 +++++++++++++++++++ .../bsim/bluetooth/audio/src/bap_stream_rx.h | 12 +++ .../audio/src/bap_unicast_client_test.c | 49 +----------- .../audio/src/bap_unicast_server_test.c | 49 +----------- .../bluetooth/audio/src/cap_acceptor_test.c | 64 ++++------------ .../audio/src/cap_initiator_broadcast_test.c | 5 ++ tests/bsim/bluetooth/audio/src/common.c | 1 + tests/bsim/bluetooth/audio/src/common.h | 4 +- .../src/pbp_public_broadcast_sink_test.c | 43 +++++------ .../src/pbp_public_broadcast_source_test.c | 50 ++++++++----- 13 files changed, 185 insertions(+), 240 deletions(-) create mode 100644 tests/bsim/bluetooth/audio/src/bap_stream_rx.c create mode 100644 tests/bsim/bluetooth/audio/src/bap_stream_rx.h diff --git a/subsys/bluetooth/audio/bap_stream.c b/subsys/bluetooth/audio/bap_stream.c index 9f3ae7984d1..761ef9d8a8d 100644 --- a/subsys/bluetooth/audio/bap_stream.c +++ b/subsys/bluetooth/audio/bap_stream.c @@ -362,7 +362,13 @@ static int bap_stream_send(struct bt_bap_stream *stream, struct net_buf *buf, ui struct bt_bap_ep *ep; int ret; - if (stream == NULL || stream->ep == NULL) { + if (stream == NULL) { + LOG_DBG("stream is NULL"); + return -EINVAL; + } + + if (stream->ep == NULL) { + LOG_DBG("stream->ep %p is NULL", stream); return -EINVAL; } diff --git a/tests/bsim/bluetooth/audio/src/bap_broadcast_sink_test.c b/tests/bsim/bluetooth/audio/src/bap_broadcast_sink_test.c index 633844a8203..52df6b43373 100644 --- a/tests/bsim/bluetooth/audio/src/bap_broadcast_sink_test.c +++ b/tests/bsim/bluetooth/audio/src/bap_broadcast_sink_test.c @@ -32,6 +32,7 @@ #include #include "bap_common.h" +#include "bap_stream_rx.h" #include "bstests.h" #include "common.h" @@ -44,7 +45,6 @@ CREATE_FLAG(flag_base_metadata_updated); CREATE_FLAG(flag_pa_synced); CREATE_FLAG(flag_syncable); CREATE_FLAG(flag_pa_sync_lost); -CREATE_FLAG(flag_received); CREATE_FLAG(flag_pa_request); CREATE_FLAG(flag_bis_sync_requested); CREATE_FLAG(flag_big_sync_mic_failure); @@ -528,9 +528,13 @@ static void validate_stream_codec_cfg(const struct bt_bap_stream *stream) static void started_cb(struct bt_bap_stream *stream) { + struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream); struct bt_bap_ep_info info; int err; + memset(&test_stream->last_info, 0, sizeof(test_stream->last_info)); + test_stream->rx_cnt = 0U; + err = bt_bap_ep_get_info(stream->ep, &info); if (err != 0) { FAIL("Failed to get EP info: %d\n", err); @@ -578,57 +582,10 @@ static void stopped_cb(struct bt_bap_stream *stream, uint8_t reason) } } -static void recv_cb(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); - - if ((test_stream->rx_cnt % 100U) == 0U) { - printk("[%zu]: Incoming audio on stream %p len %u and ts %u\n", test_stream->rx_cnt, - stream, buf->len, info->ts); - } - - if (test_stream->rx_cnt > 0U && info->ts == test_stream->last_info.ts) { - FAIL("Duplicated timestamp received: %u\n", test_stream->last_info.ts); - return; - } - - if (test_stream->rx_cnt > 0U && info->seq_num == test_stream->last_info.seq_num) { - FAIL("Duplicated PSN received: %u\n", 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_received)) { - FAIL("ISO receive error\n"); - } - - return; - } - - if (info->flags & BT_ISO_FLAGS_LOST) { - FAIL("ISO receive lost\n"); - return; - } - - if (memcmp(buf->data, mock_iso_data, buf->len) == 0) { - test_stream->rx_cnt++; - - if (test_stream->rx_cnt >= MIN_SEND_COUNT) { - /* We set the flag is just one stream has received the expected */ - SET_FLAG(flag_received); - } - } else { - FAIL("Unexpected data received\n"); - } -} - static struct bt_bap_stream_ops stream_ops = { .started = started_cb, .stopped = stopped_cb, - .recv = recv_cb + .recv = bap_stream_rx_recv_cb, }; static int init(void) @@ -964,7 +921,7 @@ static void test_common(void) } printk("Waiting for data\n"); - WAIT_FOR_FLAG(flag_received); + WAIT_FOR_FLAG(flag_audio_received); backchannel_sync_send_all(); /* let other devices know we have received what we wanted */ /* Ensure that we also see the metadata update */ @@ -1051,7 +1008,7 @@ static void test_sink_encrypted(void) } printk("Waiting for data\n"); - WAIT_FOR_FLAG(flag_received); + WAIT_FOR_FLAG(flag_audio_received); backchannel_sync_send_all(); /* let other devices know we have received data */ @@ -1140,7 +1097,7 @@ static void broadcast_sink_with_assistant(void) } printk("Waiting for data\n"); - WAIT_FOR_FLAG(flag_received); + WAIT_FOR_FLAG(flag_audio_received); backchannel_sync_send_all(); /* let other devices know we have received what we wanted */ /* Ensure that we also see the metadata update */ diff --git a/tests/bsim/bluetooth/audio/src/bap_broadcast_source_test.c b/tests/bsim/bluetooth/audio/src/bap_broadcast_source_test.c index 330687088f0..de0d7764779 100644 --- a/tests/bsim/bluetooth/audio/src/bap_broadcast_source_test.c +++ b/tests/bsim/bluetooth/audio/src/bap_broadcast_source_test.c @@ -176,9 +176,13 @@ static void validate_stream_codec_cfg(const struct bt_bap_stream *stream) static void started_cb(struct bt_bap_stream *stream) { + struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream); struct bt_bap_ep_info info; int err; + test_stream->seq_num = 0U; + test_stream->tx_cnt = 0U; + err = bt_bap_ep_get_info(stream->ep, &info); if (err != 0) { FAIL("Failed to get EP info: %d\n", err); diff --git a/tests/bsim/bluetooth/audio/src/bap_stream_rx.c b/tests/bsim/bluetooth/audio/src/bap_stream_rx.c new file mode 100644 index 00000000000..e2133a44a76 --- /dev/null +++ b/tests/bsim/bluetooth/audio/src/bap_stream_rx.c @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include + +#include "common.h" +#include + +LOG_MODULE_REGISTER(stream_tx, 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", + test_stream->rx_cnt, stream, buf->len, info->flags, info->seq_num, info->ts); +} + +void bap_stream_rx_recv_cb(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); + + if ((test_stream->rx_cnt % 50U) == 0U) { + log_stream_rx(stream, info, buf); + } + + 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); + 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); + 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"); + } + + return; + } + + if (info->flags & BT_ISO_FLAGS_LOST) { + log_stream_rx(stream, info, buf); + FAIL("ISO receive lost\n"); + return; + } + + if (memcmp(buf->data, mock_iso_data, buf->len) == 0) { + test_stream->rx_cnt++; + + if (test_stream->rx_cnt >= MIN_SEND_COUNT) { + /* We set the flag is just one stream has received the expected */ + SET_FLAG(flag_audio_received); + } + } else { + log_stream_rx(stream, info, buf); + FAIL("Unexpected data received\n"); + } +} diff --git a/tests/bsim/bluetooth/audio/src/bap_stream_rx.h b/tests/bsim/bluetooth/audio/src/bap_stream_rx.h new file mode 100644 index 00000000000..d24b679a448 --- /dev/null +++ b/tests/bsim/bluetooth/audio/src/bap_stream_rx.h @@ -0,0 +1,12 @@ +/* + * Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +void bap_stream_rx_recv_cb(struct bt_bap_stream *stream, const struct bt_iso_recv_info *info, + struct net_buf *buf); diff --git a/tests/bsim/bluetooth/audio/src/bap_unicast_client_test.c b/tests/bsim/bluetooth/audio/src/bap_unicast_client_test.c index 913c21b6ecc..970983e2cbe 100644 --- a/tests/bsim/bluetooth/audio/src/bap_unicast_client_test.c +++ b/tests/bsim/bluetooth/audio/src/bap_unicast_client_test.c @@ -29,6 +29,7 @@ #include #include +#include "bap_stream_rx.h" #include "bstests.h" #include "common.h" #include "bap_common.h" @@ -154,43 +155,6 @@ static void stream_released(struct bt_bap_stream *stream) SET_FLAG(flag_stream_released); } -static void stream_recv_cb(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); - - if ((test_stream->rx_cnt % 100U) == 0U) { - printk("[%zu]: Incoming audio on stream %p len %u and ts %u\n", test_stream->rx_cnt, - stream, buf->len, info->ts); - } - - if (test_stream->rx_cnt > 0U && info->ts == test_stream->last_info.ts) { - FAIL("Duplicated timestamp received: %u\n", test_stream->last_info.ts); - return; - } - - if (test_stream->rx_cnt > 0U && info->seq_num == test_stream->last_info.seq_num) { - FAIL("Duplicated PSN received: %u\n", test_stream->last_info.seq_num); - return; - } - - if (info->flags & BT_ISO_FLAGS_ERROR) { - FAIL("ISO receive error\n"); - return; - } - - if (info->flags & BT_ISO_FLAGS_LOST) { - FAIL("ISO receive lost\n"); - return; - } - - if (memcmp(buf->data, mock_iso_data, buf->len) == 0) { - test_stream->rx_cnt++; - } else { - FAIL("Unexpected data received\n"); - } -} - static void stream_sent_cb(struct bt_bap_stream *stream) { struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream); @@ -234,7 +198,7 @@ static struct bt_bap_stream_ops stream_ops = { .disabled = stream_disabled, .stopped = stream_stopped, .released = stream_released, - .recv = stream_recv_cb, + .recv = bap_stream_rx_recv_cb, .sent = stream_sent_cb, .connected = stream_connected, .disconnected = stream_disconnected, @@ -894,13 +858,8 @@ static void transceive_streams(void) } if (source_stream != NULL) { - const struct audio_test_stream *test_stream = - audio_test_stream_from_bap_stream(source_stream); - - /* Keep receiving until we reach the minimum expected */ - while (test_stream->rx_cnt < MIN_SEND_COUNT) { - k_sleep(K_MSEC(100)); - } + printk("Waiting for data\n"); + WAIT_FOR_FLAG(flag_audio_received); } } diff --git a/tests/bsim/bluetooth/audio/src/bap_unicast_server_test.c b/tests/bsim/bluetooth/audio/src/bap_unicast_server_test.c index 7dff0e1a97f..5db704fad34 100644 --- a/tests/bsim/bluetooth/audio/src/bap_unicast_server_test.c +++ b/tests/bsim/bluetooth/audio/src/bap_unicast_server_test.c @@ -27,6 +27,7 @@ #include #include "bap_common.h" +#include "bap_stream_rx.h" #include "bstests.h" #include "common.h" @@ -273,43 +274,6 @@ static void stream_started_cb(struct bt_bap_stream *stream) SET_FLAG(flag_stream_started); } -static void stream_recv_cb(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); - - if ((test_stream->rx_cnt % 100U) == 0U) { - printk("[%zu]: Incoming audio on stream %p len %u and ts %u\n", test_stream->rx_cnt, - stream, buf->len, info->ts); - } - - if (test_stream->rx_cnt > 0U && info->ts == test_stream->last_info.ts) { - FAIL("Duplicated timestamp received: %u\n", test_stream->last_info.ts); - return; - } - - if (test_stream->rx_cnt > 0U && info->seq_num == test_stream->last_info.seq_num) { - FAIL("Duplicated PSN received: %u\n", test_stream->last_info.seq_num); - return; - } - - if (info->flags & BT_ISO_FLAGS_ERROR) { - FAIL("ISO receive error\n"); - return; - } - - if (info->flags & BT_ISO_FLAGS_LOST) { - FAIL("ISO receive lost\n"); - return; - } - - if (memcmp(buf->data, mock_iso_data, buf->len) == 0) { - test_stream->rx_cnt++; - } else { - FAIL("Unexpected data received\n"); - } -} - static void stream_sent_cb(struct bt_bap_stream *stream) { struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream); @@ -347,7 +311,7 @@ static void stream_sent_cb(struct bt_bap_stream *stream) static struct bt_bap_stream_ops stream_ops = { .enabled = stream_enabled_cb, .started = stream_started_cb, - .recv = stream_recv_cb, + .recv = bap_stream_rx_recv_cb, .sent = stream_sent_cb, }; @@ -406,13 +370,8 @@ static void transceive_test_streams(void) } if (sink_stream != NULL) { - const struct audio_test_stream *test_stream = - audio_test_stream_from_bap_stream(sink_stream); - - /* Keep receiving until we reach the minimum expected */ - while (test_stream->rx_cnt < MIN_SEND_COUNT) { - k_sleep(K_MSEC(100)); - } + printk("Waiting for data\n"); + WAIT_FOR_FLAG(flag_audio_received); } } diff --git a/tests/bsim/bluetooth/audio/src/cap_acceptor_test.c b/tests/bsim/bluetooth/audio/src/cap_acceptor_test.c index dab3708657a..a90e090789c 100644 --- a/tests/bsim/bluetooth/audio/src/cap_acceptor_test.c +++ b/tests/bsim/bluetooth/audio/src/cap_acceptor_test.c @@ -33,6 +33,7 @@ #include #include +#include "bap_stream_rx.h" #include "bstests.h" #include "common.h" #include "bap_common.h" @@ -50,7 +51,6 @@ CREATE_FLAG(flag_broadcast_code); CREATE_FLAG(flag_base_received); CREATE_FLAG(flag_pa_synced); CREATE_FLAG(flag_syncable); -CREATE_FLAG(flag_received); CREATE_FLAG(flag_pa_sync_lost); CREATE_FLAG(flag_pa_request); CREATE_FLAG(flag_bis_sync_requested); @@ -275,6 +275,13 @@ static struct bt_le_per_adv_sync_cb bap_pa_sync_cb = { static void started_cb(struct bt_bap_stream *stream) { + struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream); + + memset(&test_stream->last_info, 0, sizeof(test_stream->last_info)); + test_stream->rx_cnt = 0U; + test_stream->seq_num = 0U; + test_stream->tx_cnt = 0U; + printk("Stream %p started\n", stream); k_sem_give(&sem_broadcast_started); } @@ -285,54 +292,11 @@ static void stopped_cb(struct bt_bap_stream *stream, uint8_t reason) k_sem_give(&sem_broadcast_stopped); } -static void recv_cb(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); - - if ((test_stream->rx_cnt % 50U) == 0U) { - printk("[%zu]: Incoming audio on stream %p len %u and ts %u\n", test_stream->rx_cnt, - stream, buf->len, info->ts); - } - - if (test_stream->rx_cnt > 0U && info->ts == test_stream->last_info.ts) { - FAIL("Duplicated timestamp received: %u\n", test_stream->last_info.ts); - return; - } - - if (test_stream->rx_cnt > 0U && info->seq_num == test_stream->last_info.seq_num) { - FAIL("Duplicated PSN received: %u\n", 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_received)) { - FAIL("ISO receive error\n"); - } - - return; - } - - if (info->flags & BT_ISO_FLAGS_LOST) { - FAIL("ISO receive lost\n"); - return; - } - - if (memcmp(buf->data, mock_iso_data, buf->len) == 0) { - test_stream->rx_cnt++; - - if (test_stream->rx_cnt >= MIN_SEND_COUNT) { - /* We set the flag is just one stream has received the expected */ - SET_FLAG(flag_received); - } - } else { - FAIL("Unexpected data received\n"); - } -} - static struct bt_bap_stream_ops broadcast_stream_ops = { - .started = started_cb, .stopped = stopped_cb, .recv = recv_cb}; + .started = started_cb, + .stopped = stopped_cb, + .recv = bap_stream_rx_recv_cb, +}; static void unicast_stream_enabled_cb(struct bt_bap_stream *stream) { @@ -796,7 +760,7 @@ static void init(void) UNSET_FLAG(flag_base_received); UNSET_FLAG(flag_pa_synced); UNSET_FLAG(flag_pa_request); - UNSET_FLAG(flag_received); + UNSET_FLAG(flag_audio_received); UNSET_FLAG(flag_base_metadata_updated); UNSET_FLAG(flag_bis_sync_requested); @@ -998,7 +962,7 @@ static void create_and_sync_sink(struct bt_bap_stream *bap_streams[], size_t *st static void sink_wait_for_data(void) { printk("Waiting for data\n"); - WAIT_FOR_FLAG(flag_received); + WAIT_FOR_FLAG(flag_audio_received); backchannel_sync_send_all(); /* let other devices know we have received what we wanted */ } diff --git a/tests/bsim/bluetooth/audio/src/cap_initiator_broadcast_test.c b/tests/bsim/bluetooth/audio/src/cap_initiator_broadcast_test.c index b81b1cfe18d..98896e40766 100644 --- a/tests/bsim/bluetooth/audio/src/cap_initiator_broadcast_test.c +++ b/tests/bsim/bluetooth/audio/src/cap_initiator_broadcast_test.c @@ -116,6 +116,11 @@ static const struct named_lc3_preset lc3_broadcast_presets[] = { static void broadcast_started_cb(struct bt_bap_stream *stream) { + struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream); + + test_stream->seq_num = 0U; + test_stream->tx_cnt = 0U; + printk("Stream %p started\n", stream); k_sem_give(&sem_broadcast_started); } diff --git a/tests/bsim/bluetooth/audio/src/common.c b/tests/bsim/bluetooth/audio/src/common.c index 7931ed1adb8..7fcac688a13 100644 --- a/tests/bsim/bluetooth/audio/src/common.c +++ b/tests/bsim/bluetooth/audio/src/common.c @@ -35,6 +35,7 @@ struct bt_conn *default_conn; atomic_t flag_connected; atomic_t flag_disconnected; atomic_t flag_conn_updated; +atomic_t flag_audio_received; volatile bt_security_t security_level; const struct bt_data ad[AD_SIZE] = { diff --git a/tests/bsim/bluetooth/audio/src/common.h b/tests/bsim/bluetooth/audio/src/common.h index 76020b8529d..5487e455936 100644 --- a/tests/bsim/bluetooth/audio/src/common.h +++ b/tests/bsim/bluetooth/audio/src/common.h @@ -29,6 +29,7 @@ #include #include +#include "bstests.h" #include "bs_types.h" #include "bs_tracing.h" @@ -88,7 +89,7 @@ static const uint8_t mock_iso_data[] = { (void)k_sleep(K_MSEC(1)); \ } - +extern enum bst_result_t bst_result; #define FAIL(...) \ do { \ bst_result = Failed; \ @@ -114,6 +115,7 @@ extern struct bt_conn *default_conn; extern atomic_t flag_connected; extern atomic_t flag_disconnected; extern atomic_t flag_conn_updated; +extern atomic_t flag_audio_received; extern volatile bt_security_t security_level; void disconnected(struct bt_conn *conn, uint8_t reason); diff --git a/tests/bsim/bluetooth/audio/src/pbp_public_broadcast_sink_test.c b/tests/bsim/bluetooth/audio/src/pbp_public_broadcast_sink_test.c index 1f3eb5315c2..0df8ae7c609 100644 --- a/tests/bsim/bluetooth/audio/src/pbp_public_broadcast_sink_test.c +++ b/tests/bsim/bluetooth/audio/src/pbp_public_broadcast_sink_test.c @@ -1,11 +1,13 @@ /* * Copyright 2023 NXP + * Copyright (c) 2024 Nordic Semiconductor ASA * * SPDX-License-Identifier: Apache-2.0 */ #include #include #include +#include #include #include @@ -27,6 +29,7 @@ #include #include +#include "bap_stream_rx.h" #include "bstests.h" #include "common.h" @@ -41,13 +44,12 @@ static K_SEM_DEFINE(sem_pa_synced, 0U, 1U); static K_SEM_DEFINE(sem_base_received, 0U, 1U); static K_SEM_DEFINE(sem_syncable, 0U, 1U); static K_SEM_DEFINE(sem_pa_sync_lost, 0U, 1U); -static K_SEM_DEFINE(sem_data_received, 0U, 1U); static struct bt_bap_broadcast_sink *broadcast_sink; static struct bt_le_per_adv_sync *bcast_pa_sync; -static struct bt_bap_stream streams[CONFIG_BT_BAP_BROADCAST_SNK_STREAM_COUNT]; -static struct bt_bap_stream *streams_p[ARRAY_SIZE(streams)]; +static struct audio_test_stream test_streams[CONFIG_BT_BAP_BROADCAST_SNK_STREAM_COUNT]; +static struct bt_bap_stream *streams_p[ARRAY_SIZE(test_streams)]; static const struct bt_audio_codec_cap codec = BT_AUDIO_CODEC_CAP_LC3( BT_AUDIO_CODEC_CAP_FREQ_16KHZ | BT_AUDIO_CODEC_CAP_FREQ_24KHZ | @@ -59,7 +61,7 @@ static const struct bt_audio_codec_cap codec = BT_AUDIO_CODEC_CAP_LC3( * we have. We add an additional 1 since the bis indexes start from 1 and not * 0. */ -static const uint32_t bis_index_mask = BIT_MASK(ARRAY_SIZE(streams) + 1U); +static const uint32_t bis_index_mask = BIT_MASK(ARRAY_SIZE(test_streams) + 1U); static uint32_t bis_index_bitfield; static uint32_t broadcast_id; @@ -93,6 +95,11 @@ static struct bt_bap_broadcast_sink_cb broadcast_sink_cbs = { static void started_cb(struct bt_bap_stream *stream) { + struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream); + + memset(&test_stream->last_info, 0, sizeof(test_stream->last_info)); + test_stream->rx_cnt = 0U; + printk("Stream %p started\n", stream); } @@ -101,19 +108,6 @@ static void stopped_cb(struct bt_bap_stream *stream, uint8_t reason) printk("Stream %p stopped with reason 0x%02X\n", stream, reason); } -static void recv_cb(struct bt_bap_stream *stream, - const struct bt_iso_recv_info *info, - struct net_buf *buf) -{ - static uint32_t recv_cnt; - - recv_cnt++; - if (recv_cnt >= MIN_SEND_COUNT) { - k_sem_give(&sem_data_received); - } - printk("Receiving ISO packets\n"); -} - static bool pa_decode_base(struct bt_data *data, void *user_data) { const struct bt_bap_base *base = bt_bap_base_get_base_from_ad(data); @@ -164,7 +158,7 @@ static void broadcast_pa_terminated(struct bt_le_per_adv_sync *sync, static struct bt_bap_stream_ops stream_ops = { .started = started_cb, .stopped = stopped_cb, - .recv = recv_cb + .recv = bap_stream_rx_recv_cb, }; static struct bt_le_per_adv_sync_cb broadcast_sync_cb = { @@ -181,7 +175,7 @@ static int reset(void) k_sem_reset(&sem_base_received); k_sem_reset(&sem_syncable); k_sem_reset(&sem_pa_sync_lost); - k_sem_reset(&sem_data_received); + UNSET_FLAG(flag_audio_received); broadcast_id = BT_BAP_INVALID_BROADCAST_ID; bis_index_bitfield = 0U; @@ -224,12 +218,9 @@ static int init(void) return err; } - for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) { - streams[i].ops = &stream_ops; - } - - for (size_t i = 0U; i < ARRAY_SIZE(streams_p); i++) { - streams_p[i] = &streams[i]; + for (size_t i = 0U; i < ARRAY_SIZE(test_streams); i++) { + streams_p[i] = bap_stream_from_audio_test_stream(&test_streams[i]); + bt_bap_stream_cb_register(streams_p[i], &stream_ops); } return 0; @@ -389,7 +380,7 @@ static void test_main(void) /* Wait for data */ printk("Waiting for data\n"); - k_sem_take(&sem_data_received, SEM_TIMEOUT); + WAIT_FOR_FLAG(flag_audio_received); printk("Sending signal to broadcaster to stop\n"); backchannel_sync_send_all(); /* let the broadcast source know it can stop */ diff --git a/tests/bsim/bluetooth/audio/src/pbp_public_broadcast_source_test.c b/tests/bsim/bluetooth/audio/src/pbp_public_broadcast_source_test.c index 86df90cc386..8947e386efe 100644 --- a/tests/bsim/bluetooth/audio/src/pbp_public_broadcast_source_test.c +++ b/tests/bsim/bluetooth/audio/src/pbp_public_broadcast_source_test.c @@ -1,11 +1,13 @@ /* * Copyright 2023 NXP + * Copyright (c) 2024 Nordic Semiconductor ASA * * SPDX-License-Identifier: Apache-2.0 */ #include #include #include +#include #include #include @@ -55,8 +57,7 @@ static uint8_t bis_codec_data[] = { BT_AUDIO_CODEC_DATA(BT_AUDIO_CODEC_CFG_FREQ, BT_BYTES_LIST_LE16(BT_AUDIO_CODEC_CFG_FREQ_48KHZ))}; -static struct bt_cap_stream broadcast_source_stream; -static struct bt_cap_stream *broadcast_stream; +static struct audio_test_stream broadcast_source_stream; static struct bt_cap_initiator_broadcast_stream_param stream_params; static struct bt_cap_initiator_broadcast_subgroup_param subgroup_param; @@ -74,6 +75,11 @@ static struct bt_le_ext_adv *adv; static void started_cb(struct bt_bap_stream *stream) { + struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream); + + test_stream->seq_num = 0U; + test_stream->tx_cnt = 0U; + printk("Stream %p started\n", stream); k_sem_give(&sem_started); } @@ -86,26 +92,20 @@ static void stopped_cb(struct bt_bap_stream *stream, uint8_t reason) static void sent_cb(struct bt_bap_stream *stream) { - static uint8_t mock_data[CONFIG_BT_ISO_TX_MTU]; - static bool mock_data_initialized; - static uint32_t seq_num; + 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 (broadcast_preset_48_2_1.qos.sdu > CONFIG_BT_ISO_TX_MTU) { printk("Invalid SDU %u for the MTU: %d", broadcast_preset_48_2_1.qos.sdu, CONFIG_BT_ISO_TX_MTU); return; } - if (!mock_data_initialized) { - for (size_t i = 0U; i < ARRAY_SIZE(mock_data); i++) { - /* Initialize mock data */ - mock_data[i] = (uint8_t)i; - } - mock_data_initialized = true; - } - buf = net_buf_alloc(&tx_pool, K_FOREVER); if (buf == NULL) { printk("Could not allocate buffer when sending on %p\n", stream); @@ -113,11 +113,16 @@ static void sent_cb(struct bt_bap_stream *stream) } net_buf_reserve(buf, BT_ISO_CHAN_SEND_RESERVE); - net_buf_add_mem(buf, mock_data, broadcast_preset_48_2_1.qos.sdu); - ret = bt_bap_stream_send(stream, buf, seq_num++); + net_buf_add_mem(buf, mock_iso_data, broadcast_preset_48_2_1.qos.sdu); + 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); + + /* 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); + } return; } } @@ -293,10 +298,10 @@ static void test_main(void) return; } - broadcast_stream = &broadcast_source_stream; - bt_bap_stream_cb_register(&broadcast_stream->bap_stream, &broadcast_stream_ops); + bt_bap_stream_cb_register(&broadcast_source_stream.stream.bap_stream, + &broadcast_stream_ops); - stream_params.stream = &broadcast_source_stream; + stream_params.stream = &broadcast_source_stream.stream; stream_params.data_len = ARRAY_SIZE(bis_codec_data); stream_params.data = bis_codec_data; @@ -347,12 +352,17 @@ static void test_main(void) k_sem_take(&sem_started, SEM_TIMEOUT); /* Initialize sending */ + broadcast_source_stream.tx_active = true; for (unsigned int j = 0U; j < BROADCAST_ENQUEUE_COUNT; j++) { - sent_cb(&broadcast_stream->bap_stream); + sent_cb(&broadcast_source_stream.stream.bap_stream); } /* Wait for other devices to let us know when we can stop the source */ - printk("Waiting for signal from receiver to stop\n"); backchannel_sync_wait_any(); + printk("Waiting for signal from receiver to stop\n"); + backchannel_sync_wait_any(); + + printk("Stopping broadcast source\n"); + broadcast_source_stream.tx_active = false; err = bt_cap_initiator_broadcast_audio_stop(broadcast_source); if (err != 0) {