Skip to content

Commit c7516a6

Browse files
committed
samples: Bluetooth: BAP Unicast Server: Improve reporting and TX
Improve reporting by introducing a configurable reporting interval and make the report text more useful. Change TX to always send the same size, as that is what is expected/required by LE Audio. Signed-off-by: Emil Gydesen <[email protected]>
1 parent bc25ddb commit c7516a6

File tree

2 files changed

+56
-28
lines changed

2 files changed

+56
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (c) 2025 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
mainmenu "Bluetooth: BAP Unicast Client"
5+
6+
config INFO_REPORTING_INTERVAL
7+
int "Number of SDUs received between each information report"
8+
default 1000
9+
help
10+
Determines how often information about received data is logged.
11+
Set to 0 to disable reporting.
12+
13+
source "Kconfig.zephyr"

samples/bluetooth/bap_unicast_server/src/main.c

+43-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021-2024 Nordic Semiconductor ASA
2+
* Copyright (c) 2021-2025 Nordic Semiconductor ASA
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -54,12 +54,14 @@ static const struct bt_audio_codec_cap lc3_codec_cap = BT_AUDIO_CODEC_CAP_LC3(
5454

5555
static struct bt_conn *default_conn;
5656
static struct k_work_delayable audio_send_work;
57-
static struct bt_bap_stream sink_streams[CONFIG_BT_ASCS_MAX_ASE_SNK_COUNT];
57+
static struct audio_sink {
58+
struct bt_bap_stream stream;
59+
size_t recv_cnt;
60+
} sink_streams[CONFIG_BT_ASCS_MAX_ASE_SNK_COUNT];
5861
static struct audio_source {
5962
struct bt_bap_stream stream;
6063
uint16_t seq_num;
61-
uint16_t max_sdu;
62-
size_t len_to_send;
64+
size_t send_cnt;
6365
} source_streams[CONFIG_BT_ASCS_MAX_ASE_SRC_COUNT];
6466
static size_t configured_source_stream_count;
6567

@@ -237,20 +239,21 @@ static void audio_timer_timeout(struct k_work *work)
237239
}
238240
net_buf_reserve(buf, BT_ISO_CHAN_SEND_RESERVE);
239241

240-
net_buf_add_mem(buf, buf_data, ++source_streams[i].len_to_send);
242+
net_buf_add_mem(buf, buf_data, stream->qos->sdu);
241243

242244
ret = bt_bap_stream_send(stream, buf, get_and_incr_seq_num(stream));
243245
if (ret < 0) {
244246
printk("Failed to send audio data on streams[%zu] (%p): (%d)\n",
245247
i, stream, ret);
246248
net_buf_unref(buf);
247249
} else {
248-
printk("Sending mock data with len %zu on streams[%zu] (%p)\n",
249-
source_streams[i].len_to_send, i, stream);
250-
}
250+
source_streams[i].send_cnt++;
251251

252-
if (source_streams[i].len_to_send >= source_streams[i].max_sdu) {
253-
source_streams[i].len_to_send = 0;
252+
if (CONFIG_INFO_REPORTING_INTERVAL > 0 &&
253+
(source_streams[i].send_cnt % CONFIG_INFO_REPORTING_INTERVAL) == 0U) {
254+
printk("Stream %p: Sent %u total SDUs of size %u\n", stream,
255+
source_streams[i].send_cnt, stream->qos->sdu);
256+
}
254257
}
255258
}
256259

@@ -270,7 +273,7 @@ static enum bt_audio_dir stream_dir(const struct bt_bap_stream *stream)
270273
}
271274

272275
for (size_t i = 0U; i < ARRAY_SIZE(sink_streams); i++) {
273-
if (stream == &sink_streams[i]) {
276+
if (stream == &sink_streams[i].stream) {
274277
return BT_AUDIO_DIR_SINK;
275278
}
276279
}
@@ -291,7 +294,7 @@ static struct bt_bap_stream *stream_alloc(enum bt_audio_dir dir)
291294
}
292295
} else {
293296
for (size_t i = 0; i < ARRAY_SIZE(sink_streams); i++) {
294-
struct bt_bap_stream *stream = &sink_streams[i];
297+
struct bt_bap_stream *stream = &sink_streams[i].stream;
295298

296299
if (!stream->conn) {
297300
return stream;
@@ -360,13 +363,6 @@ static int lc3_qos(struct bt_bap_stream *stream, const struct bt_bap_qos_cfg *qo
360363

361364
print_qos(qos);
362365

363-
for (size_t i = 0U; i < configured_source_stream_count; i++) {
364-
if (stream == &source_streams[i].stream) {
365-
source_streams[i].max_sdu = qos->sdu;
366-
break;
367-
}
368-
}
369-
370366
return 0;
371367
}
372368

@@ -428,7 +424,6 @@ static int lc3_start(struct bt_bap_stream *stream, struct bt_bap_ascs_rsp *rsp)
428424
for (size_t i = 0U; i < configured_source_stream_count; i++) {
429425
if (stream == &source_streams[i].stream) {
430426
source_streams[i].seq_num = 0U;
431-
source_streams[i].len_to_send = 0U;
432427
break;
433428
}
434429
}
@@ -509,18 +504,27 @@ static void stream_recv_lc3_codec(struct bt_bap_stream *stream,
509504
const struct bt_iso_recv_info *info,
510505
struct net_buf *buf)
511506
{
507+
struct audio_sink *sink_stream = CONTAINER_OF(stream, struct audio_sink, stream);
512508
const bool valid_data = (info->flags & BT_ISO_FLAGS_VALID) != 0;
513509
const int octets_per_frame = buf->len / frames_per_sdu;
514510

511+
if (valid_data) {
512+
sink_stream->recv_cnt++;
513+
514+
if (CONFIG_INFO_REPORTING_INTERVAL > 0 &&
515+
(sink_stream->recv_cnt % CONFIG_INFO_REPORTING_INTERVAL) == 0U) {
516+
printk("Incoming audio on stream %p len %u (%zu)\n", stream, buf->len,
517+
sink_stream->recv_cnt);
518+
}
519+
} else {
520+
printk("Bad packet: 0x%02X\n", info->flags);
521+
}
522+
515523
if (lc3_decoder == NULL) {
516524
printk("LC3 decoder not setup, cannot decode data.\n");
517525
return;
518526
}
519527

520-
if (!valid_data) {
521-
printk("Bad packet: 0x%02X\n", info->flags);
522-
}
523-
524528
for (int i = 0; i < frames_per_sdu; i++) {
525529
/* Passing NULL performs PLC */
526530
const int err = lc3_decode(
@@ -533,8 +537,6 @@ static void stream_recv_lc3_codec(struct bt_bap_stream *stream,
533537
printk("[%d]: Decoder failed - wrong parameters?: %d\n", i, err);
534538
}
535539
}
536-
537-
printk("RX stream %p len %u\n", stream, buf->len);
538540
}
539541

540542
#else
@@ -544,7 +546,14 @@ static void stream_recv(struct bt_bap_stream *stream,
544546
struct net_buf *buf)
545547
{
546548
if (info->flags & BT_ISO_FLAGS_VALID) {
547-
printk("Incoming audio on stream %p len %u\n", stream, buf->len);
549+
struct audio_sink *sink_stream = CONTAINER_OF(stream, struct audio_sink, stream);
550+
551+
sink_stream->recv_cnt++;
552+
553+
if (CONFIG_INFO_REPORTING_INTERVAL > 0 &&
554+
(sink_stream->recv_cnt % CONFIG_INFO_REPORTING_INTERVAL) == 0U) {
555+
printk("Incoming audio on stream %p len %u\n", stream, buf->len);
556+
}
548557
}
549558
}
550559

@@ -561,6 +570,12 @@ static void stream_stopped(struct bt_bap_stream *stream, uint8_t reason)
561570
static void stream_started(struct bt_bap_stream *stream)
562571
{
563572
printk("Audio Stream %p started\n", stream);
573+
574+
if (stream_dir(stream) == BT_AUDIO_DIR_SINK) {
575+
struct audio_sink *sink_stream = CONTAINER_OF(stream, struct audio_sink, stream);
576+
577+
sink_stream->recv_cnt = 0U;
578+
}
564579
}
565580

566581
static void stream_enabled_cb(struct bt_bap_stream *stream)
@@ -754,7 +769,7 @@ int main(void)
754769
bt_pacs_cap_register(BT_AUDIO_DIR_SOURCE, &cap_source);
755770

756771
for (size_t i = 0; i < ARRAY_SIZE(sink_streams); i++) {
757-
bt_bap_stream_cb_register(&sink_streams[i], &stream_ops);
772+
bt_bap_stream_cb_register(&sink_streams[i].stream, &stream_ops);
758773
}
759774

760775
for (size_t i = 0; i < ARRAY_SIZE(source_streams); i++) {

0 commit comments

Comments
 (0)