Skip to content

Commit b4f3763

Browse files
Thalleyhenrikbrixandersen
authored andcommitted
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 <[email protected]>
1 parent bc4f026 commit b4f3763

13 files changed

+185
-240
lines changed

subsys/bluetooth/audio/bap_stream.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,13 @@ static int bap_stream_send(struct bt_bap_stream *stream, struct net_buf *buf, ui
362362
struct bt_bap_ep *ep;
363363
int ret;
364364

365-
if (stream == NULL || stream->ep == NULL) {
365+
if (stream == NULL) {
366+
LOG_DBG("stream is NULL");
367+
return -EINVAL;
368+
}
369+
370+
if (stream->ep == NULL) {
371+
LOG_DBG("stream->ep %p is NULL", stream);
366372
return -EINVAL;
367373
}
368374

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

Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <zephyr/toolchain.h>
3333

3434
#include "bap_common.h"
35+
#include "bap_stream_rx.h"
3536
#include "bstests.h"
3637
#include "common.h"
3738

@@ -44,7 +45,6 @@ CREATE_FLAG(flag_base_metadata_updated);
4445
CREATE_FLAG(flag_pa_synced);
4546
CREATE_FLAG(flag_syncable);
4647
CREATE_FLAG(flag_pa_sync_lost);
47-
CREATE_FLAG(flag_received);
4848
CREATE_FLAG(flag_pa_request);
4949
CREATE_FLAG(flag_bis_sync_requested);
5050
CREATE_FLAG(flag_big_sync_mic_failure);
@@ -528,9 +528,13 @@ static void validate_stream_codec_cfg(const struct bt_bap_stream *stream)
528528

529529
static void started_cb(struct bt_bap_stream *stream)
530530
{
531+
struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream);
531532
struct bt_bap_ep_info info;
532533
int err;
533534

535+
memset(&test_stream->last_info, 0, sizeof(test_stream->last_info));
536+
test_stream->rx_cnt = 0U;
537+
534538
err = bt_bap_ep_get_info(stream->ep, &info);
535539
if (err != 0) {
536540
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)
578582
}
579583
}
580584

581-
static void recv_cb(struct bt_bap_stream *stream,
582-
const struct bt_iso_recv_info *info,
583-
struct net_buf *buf)
584-
{
585-
struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream);
586-
587-
if ((test_stream->rx_cnt % 100U) == 0U) {
588-
printk("[%zu]: Incoming audio on stream %p len %u and ts %u\n", test_stream->rx_cnt,
589-
stream, buf->len, info->ts);
590-
}
591-
592-
if (test_stream->rx_cnt > 0U && info->ts == test_stream->last_info.ts) {
593-
FAIL("Duplicated timestamp received: %u\n", test_stream->last_info.ts);
594-
return;
595-
}
596-
597-
if (test_stream->rx_cnt > 0U && info->seq_num == test_stream->last_info.seq_num) {
598-
FAIL("Duplicated PSN received: %u\n", test_stream->last_info.seq_num);
599-
return;
600-
}
601-
602-
if (info->flags & BT_ISO_FLAGS_ERROR) {
603-
/* Fail the test if we have not received what we expected */
604-
if (!TEST_FLAG(flag_received)) {
605-
FAIL("ISO receive error\n");
606-
}
607-
608-
return;
609-
}
610-
611-
if (info->flags & BT_ISO_FLAGS_LOST) {
612-
FAIL("ISO receive lost\n");
613-
return;
614-
}
615-
616-
if (memcmp(buf->data, mock_iso_data, buf->len) == 0) {
617-
test_stream->rx_cnt++;
618-
619-
if (test_stream->rx_cnt >= MIN_SEND_COUNT) {
620-
/* We set the flag is just one stream has received the expected */
621-
SET_FLAG(flag_received);
622-
}
623-
} else {
624-
FAIL("Unexpected data received\n");
625-
}
626-
}
627-
628585
static struct bt_bap_stream_ops stream_ops = {
629586
.started = started_cb,
630587
.stopped = stopped_cb,
631-
.recv = recv_cb
588+
.recv = bap_stream_rx_recv_cb,
632589
};
633590

634591
static int init(void)
@@ -964,7 +921,7 @@ static void test_common(void)
964921
}
965922

966923
printk("Waiting for data\n");
967-
WAIT_FOR_FLAG(flag_received);
924+
WAIT_FOR_FLAG(flag_audio_received);
968925
backchannel_sync_send_all(); /* let other devices know we have received what we wanted */
969926

970927
/* Ensure that we also see the metadata update */
@@ -1051,7 +1008,7 @@ static void test_sink_encrypted(void)
10511008
}
10521009

10531010
printk("Waiting for data\n");
1054-
WAIT_FOR_FLAG(flag_received);
1011+
WAIT_FOR_FLAG(flag_audio_received);
10551012

10561013
backchannel_sync_send_all(); /* let other devices know we have received data */
10571014

@@ -1140,7 +1097,7 @@ static void broadcast_sink_with_assistant(void)
11401097
}
11411098

11421099
printk("Waiting for data\n");
1143-
WAIT_FOR_FLAG(flag_received);
1100+
WAIT_FOR_FLAG(flag_audio_received);
11441101
backchannel_sync_send_all(); /* let other devices know we have received what we wanted */
11451102

11461103
/* Ensure that we also see the metadata update */

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,13 @@ static void validate_stream_codec_cfg(const struct bt_bap_stream *stream)
176176

177177
static void started_cb(struct bt_bap_stream *stream)
178178
{
179+
struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream);
179180
struct bt_bap_ep_info info;
180181
int err;
181182

183+
test_stream->seq_num = 0U;
184+
test_stream->tx_cnt = 0U;
185+
182186
err = bt_bap_ep_get_info(stream->ep, &info);
183187
if (err != 0) {
184188
FAIL("Failed to get EP info: %d\n", err);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/bluetooth/audio/bap.h>
8+
#include <zephyr/bluetooth/iso.h>
9+
#include <zephyr/logging/log.h>
10+
#include <zephyr/logging/log_core.h>
11+
#include <zephyr/net_buf.h>
12+
13+
#include "common.h"
14+
#include <string.h>
15+
16+
LOG_MODULE_REGISTER(stream_tx, LOG_LEVEL_INF);
17+
18+
static void log_stream_rx(struct bt_bap_stream *stream, const struct bt_iso_recv_info *info,
19+
struct net_buf *buf)
20+
{
21+
struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream);
22+
23+
LOG_INF("[%zu]: Incoming audio on stream %p len %u, flags 0x%02X, seq_num %u and ts %u\n",
24+
test_stream->rx_cnt, stream, buf->len, info->flags, info->seq_num, info->ts);
25+
}
26+
27+
void bap_stream_rx_recv_cb(struct bt_bap_stream *stream, const struct bt_iso_recv_info *info,
28+
struct net_buf *buf)
29+
{
30+
struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream);
31+
32+
if ((test_stream->rx_cnt % 50U) == 0U) {
33+
log_stream_rx(stream, info, buf);
34+
}
35+
36+
if (test_stream->rx_cnt > 0U && info->ts == test_stream->last_info.ts) {
37+
log_stream_rx(stream, info, buf);
38+
FAIL("Duplicated timestamp received: %u\n", test_stream->last_info.ts);
39+
return;
40+
}
41+
42+
if (test_stream->rx_cnt > 0U && info->seq_num == test_stream->last_info.seq_num) {
43+
log_stream_rx(stream, info, buf);
44+
FAIL("Duplicated PSN received: %u\n", test_stream->last_info.seq_num);
45+
return;
46+
}
47+
48+
if (info->flags & BT_ISO_FLAGS_ERROR) {
49+
/* Fail the test if we have not received what we expected */
50+
if (!TEST_FLAG(flag_audio_received)) {
51+
log_stream_rx(stream, info, buf);
52+
FAIL("ISO receive error\n");
53+
}
54+
55+
return;
56+
}
57+
58+
if (info->flags & BT_ISO_FLAGS_LOST) {
59+
log_stream_rx(stream, info, buf);
60+
FAIL("ISO receive lost\n");
61+
return;
62+
}
63+
64+
if (memcmp(buf->data, mock_iso_data, buf->len) == 0) {
65+
test_stream->rx_cnt++;
66+
67+
if (test_stream->rx_cnt >= MIN_SEND_COUNT) {
68+
/* We set the flag is just one stream has received the expected */
69+
SET_FLAG(flag_audio_received);
70+
}
71+
} else {
72+
log_stream_rx(stream, info, buf);
73+
FAIL("Unexpected data received\n");
74+
}
75+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/bluetooth/audio/bap.h>
8+
#include <zephyr/bluetooth/iso.h>
9+
#include <zephyr/net_buf.h>
10+
11+
void bap_stream_rx_recv_cb(struct bt_bap_stream *stream, const struct bt_iso_recv_info *info,
12+
struct net_buf *buf);

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

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <zephyr/sys/printk.h>
3030
#include <zephyr/sys/util.h>
3131

32+
#include "bap_stream_rx.h"
3233
#include "bstests.h"
3334
#include "common.h"
3435
#include "bap_common.h"
@@ -154,43 +155,6 @@ static void stream_released(struct bt_bap_stream *stream)
154155
SET_FLAG(flag_stream_released);
155156
}
156157

157-
static void stream_recv_cb(struct bt_bap_stream *stream, const struct bt_iso_recv_info *info,
158-
struct net_buf *buf)
159-
{
160-
struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream);
161-
162-
if ((test_stream->rx_cnt % 100U) == 0U) {
163-
printk("[%zu]: Incoming audio on stream %p len %u and ts %u\n", test_stream->rx_cnt,
164-
stream, buf->len, info->ts);
165-
}
166-
167-
if (test_stream->rx_cnt > 0U && info->ts == test_stream->last_info.ts) {
168-
FAIL("Duplicated timestamp received: %u\n", test_stream->last_info.ts);
169-
return;
170-
}
171-
172-
if (test_stream->rx_cnt > 0U && info->seq_num == test_stream->last_info.seq_num) {
173-
FAIL("Duplicated PSN received: %u\n", test_stream->last_info.seq_num);
174-
return;
175-
}
176-
177-
if (info->flags & BT_ISO_FLAGS_ERROR) {
178-
FAIL("ISO receive error\n");
179-
return;
180-
}
181-
182-
if (info->flags & BT_ISO_FLAGS_LOST) {
183-
FAIL("ISO receive lost\n");
184-
return;
185-
}
186-
187-
if (memcmp(buf->data, mock_iso_data, buf->len) == 0) {
188-
test_stream->rx_cnt++;
189-
} else {
190-
FAIL("Unexpected data received\n");
191-
}
192-
}
193-
194158
static void stream_sent_cb(struct bt_bap_stream *stream)
195159
{
196160
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 = {
234198
.disabled = stream_disabled,
235199
.stopped = stream_stopped,
236200
.released = stream_released,
237-
.recv = stream_recv_cb,
201+
.recv = bap_stream_rx_recv_cb,
238202
.sent = stream_sent_cb,
239203
.connected = stream_connected,
240204
.disconnected = stream_disconnected,
@@ -894,13 +858,8 @@ static void transceive_streams(void)
894858
}
895859

896860
if (source_stream != NULL) {
897-
const struct audio_test_stream *test_stream =
898-
audio_test_stream_from_bap_stream(source_stream);
899-
900-
/* Keep receiving until we reach the minimum expected */
901-
while (test_stream->rx_cnt < MIN_SEND_COUNT) {
902-
k_sleep(K_MSEC(100));
903-
}
861+
printk("Waiting for data\n");
862+
WAIT_FOR_FLAG(flag_audio_received);
904863
}
905864
}
906865

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

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <zephyr/sys/util_macro.h>
2828

2929
#include "bap_common.h"
30+
#include "bap_stream_rx.h"
3031
#include "bstests.h"
3132
#include "common.h"
3233

@@ -273,43 +274,6 @@ static void stream_started_cb(struct bt_bap_stream *stream)
273274
SET_FLAG(flag_stream_started);
274275
}
275276

276-
static void stream_recv_cb(struct bt_bap_stream *stream, const struct bt_iso_recv_info *info,
277-
struct net_buf *buf)
278-
{
279-
struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream);
280-
281-
if ((test_stream->rx_cnt % 100U) == 0U) {
282-
printk("[%zu]: Incoming audio on stream %p len %u and ts %u\n", test_stream->rx_cnt,
283-
stream, buf->len, info->ts);
284-
}
285-
286-
if (test_stream->rx_cnt > 0U && info->ts == test_stream->last_info.ts) {
287-
FAIL("Duplicated timestamp received: %u\n", test_stream->last_info.ts);
288-
return;
289-
}
290-
291-
if (test_stream->rx_cnt > 0U && info->seq_num == test_stream->last_info.seq_num) {
292-
FAIL("Duplicated PSN received: %u\n", test_stream->last_info.seq_num);
293-
return;
294-
}
295-
296-
if (info->flags & BT_ISO_FLAGS_ERROR) {
297-
FAIL("ISO receive error\n");
298-
return;
299-
}
300-
301-
if (info->flags & BT_ISO_FLAGS_LOST) {
302-
FAIL("ISO receive lost\n");
303-
return;
304-
}
305-
306-
if (memcmp(buf->data, mock_iso_data, buf->len) == 0) {
307-
test_stream->rx_cnt++;
308-
} else {
309-
FAIL("Unexpected data received\n");
310-
}
311-
}
312-
313277
static void stream_sent_cb(struct bt_bap_stream *stream)
314278
{
315279
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)
347311
static struct bt_bap_stream_ops stream_ops = {
348312
.enabled = stream_enabled_cb,
349313
.started = stream_started_cb,
350-
.recv = stream_recv_cb,
314+
.recv = bap_stream_rx_recv_cb,
351315
.sent = stream_sent_cb,
352316
};
353317

@@ -406,13 +370,8 @@ static void transceive_test_streams(void)
406370
}
407371

408372
if (sink_stream != NULL) {
409-
const struct audio_test_stream *test_stream =
410-
audio_test_stream_from_bap_stream(sink_stream);
411-
412-
/* Keep receiving until we reach the minimum expected */
413-
while (test_stream->rx_cnt < MIN_SEND_COUNT) {
414-
k_sleep(K_MSEC(100));
415-
}
373+
printk("Waiting for data\n");
374+
WAIT_FOR_FLAG(flag_audio_received);
416375
}
417376
}
418377

0 commit comments

Comments
 (0)