Skip to content

Commit 1e3943f

Browse files
committed
tests: Bluetooth: Tester: Fix miss check and alloc in BAP broadcast
Some of the BAP broadcast functions was missing a check for allocated. Adding the checks exposed a bug in the CAP commands where the broadcasts were never actually allocated. Since there is not a command for that in CAP, a workaround has been implemented to use the first command (setup_stream) to allocate a BAP broadcast with a dummy broadcast ID. Signed-off-by: Emil Gydesen <[email protected]>
1 parent 0e0ff29 commit 1e3943f

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

tests/bluetooth/tester/src/audio/btp_bap_broadcast.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ struct btp_bap_broadcast_local_source *
7676
btp_bap_broadcast_local_source_allocate(uint32_t broadcast_id)
7777
{
7878
for (size_t i = 0; i < ARRAY_SIZE(local_sources); i++) {
79-
if (local_sources[i].broadcast_id == broadcast_id) {
79+
if (local_sources[i].allocated && local_sources[i].broadcast_id == broadcast_id) {
8080
LOG_ERR("Local source already allocated for broadcast id %d", broadcast_id);
8181

8282
return NULL;
@@ -111,7 +111,7 @@ struct btp_bap_broadcast_local_source *
111111
btp_bap_broadcast_local_source_from_src_id_get(uint32_t source_id)
112112
{
113113
for (size_t i = 0; i < ARRAY_SIZE(local_sources); i++) {
114-
if (local_sources[i].source_id == source_id) {
114+
if (local_sources[i].allocated && local_sources[i].source_id == source_id) {
115115
return &local_sources[i];
116116
}
117117
}
@@ -125,7 +125,7 @@ static struct btp_bap_broadcast_local_source *
125125
btp_bap_broadcast_local_source_from_brcst_id_get(uint32_t broadcast_id)
126126
{
127127
for (size_t i = 0; i < ARRAY_SIZE(local_sources); i++) {
128-
if (local_sources[i].broadcast_id == broadcast_id) {
128+
if (local_sources[i].allocated && local_sources[i].broadcast_id == broadcast_id) {
129129
return &local_sources[i];
130130
}
131131
}

tests/bluetooth/tester/src/audio/btp_bap_broadcast.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ uint8_t btp_bap_broadcast_local_source_idx_get(struct btp_bap_broadcast_local_so
7676
struct btp_bap_broadcast_stream *btp_bap_broadcast_stream_alloc(
7777
struct btp_bap_broadcast_local_source *source);
7878

79+
struct btp_bap_broadcast_local_source *
80+
btp_bap_broadcast_local_source_allocate(uint32_t broadcast_id);
81+
7982
uint8_t btp_bap_broadcast_source_setup(const void *cmd, uint16_t cmd_len,
8083
void *rsp, uint16_t *rsp_len);
8184
uint8_t btp_bap_broadcast_source_setup_v2(const void *cmd, uint16_t cmd_len,

tests/bluetooth/tester/src/audio/btp_cap.c

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -499,13 +499,30 @@ static uint8_t btp_cap_broadcast_source_setup_stream(const void *cmd, uint16_t c
499499
btp_bap_broadcast_local_source_from_src_id_get(cp->source_id);
500500

501501
if (source == NULL) {
502-
return BTP_STATUS_FAILED;
502+
LOG_DBG("Could not get source by id %u, allocating", cp->source_id);
503+
/* The CAP BTP commands are fundametally broken as they expect a broadcast source
504+
* to already have been allocated. In the case that the source isn't allocated, we
505+
* attempt to allocate it with a dummy broadcast ID that will be updated later by
506+
* `btp_cap_broadcast_source_setup`.
507+
* The source_id returned from btp_bap_broadcast_local_source_allocate is also
508+
* overridden as it may not be the same as requested by this command.
509+
*/
510+
source = btp_bap_broadcast_local_source_allocate(0);
511+
512+
if (source == NULL) {
513+
LOG_DBG("Could not allocate source");
514+
515+
return BTP_STATUS_FAILED;
516+
} else {
517+
source->source_id = cp->source_id;
518+
}
503519
}
504520

505521
struct bt_audio_codec_cfg *codec_cfg;
506522

507523
stream = btp_bap_broadcast_stream_alloc(source);
508524
if (stream == NULL) {
525+
LOG_DBG("Could not allocate stream");
509526
return BTP_STATUS_FAILED;
510527
}
511528

@@ -541,21 +558,18 @@ static uint8_t btp_cap_broadcast_source_setup_subgroup(const void *cmd, uint16_t
541558
btp_bap_broadcast_local_source_from_src_id_get(cp->source_id);
542559

543560
if (source == NULL) {
561+
LOG_DBG("Could not get source from src_id %u", cp->source_id);
544562
return BTP_STATUS_FAILED;
545563
}
546564

547565
if (cp->subgroup_id >= ARRAY_SIZE(cap_broadcast_params[0].cap_subgroup_params)) {
548-
return BTP_STATUS_FAILED;
549-
}
550-
551-
uint8_t idx = btp_bap_broadcast_local_source_idx_get(source);
552-
553-
if (idx >= ARRAY_SIZE(cap_broadcast_params)) {
566+
LOG_DBG("Invalid subgroup_id: %u (>= %zu)", cp->subgroup_id,
567+
ARRAY_SIZE(cap_broadcast_params[0].cap_subgroup_params));
554568
return BTP_STATUS_FAILED;
555569
}
556570

557571
struct bt_cap_initiator_broadcast_subgroup_param *subgroup_param =
558-
&cap_broadcast_params[idx].cap_subgroup_params[cp->subgroup_id];
572+
&cap_broadcast_params[cp->source_id].cap_subgroup_params[cp->subgroup_id];
559573

560574
subgroup_param->codec_cfg = &source->subgroup_codec_cfg[cp->subgroup_id];
561575

@@ -657,7 +671,10 @@ static uint8_t btp_cap_broadcast_source_setup(const void *cmd, uint16_t cmd_len,
657671
struct btp_bap_broadcast_local_source *source =
658672
btp_bap_broadcast_local_source_from_src_id_get(cp->source_id);
659673

674+
LOG_DBG("");
675+
660676
if (source == NULL) {
677+
LOG_DBG("Could not get source from src_id %u", cp->source_id);
661678
return BTP_STATUS_FAILED;
662679
}
663680

@@ -666,10 +683,13 @@ static uint8_t btp_cap_broadcast_source_setup(const void *cmd, uint16_t cmd_len,
666683

667684
struct bt_bap_qos_cfg *qos = &source->qos;
668685

669-
LOG_DBG("");
670-
671686
memset(&create_param, 0, sizeof(create_param));
672687

688+
/* The source is allocated by btp_cap_broadcast_source_setup_stream but at that time we do
689+
* not have the broadcast_id, so update it here
690+
*/
691+
source->broadcast_id = sys_get_le24(cp->broadcast_id);
692+
673693
for (size_t i = 0; i < ARRAY_SIZE(source->streams); i++) {
674694
struct btp_bap_broadcast_stream *stream = &source->streams[i];
675695
struct bt_cap_initiator_broadcast_stream_param *stream_param;
@@ -705,6 +725,7 @@ static uint8_t btp_cap_broadcast_source_setup(const void *cmd, uint16_t cmd_len,
705725
}
706726

707727
if (create_param.subgroup_count == 0) {
728+
LOG_DBG("create_param.subgroup_count is 0");
708729
return BTP_STATUS_FAILED;
709730
}
710731

@@ -734,9 +755,11 @@ static uint8_t btp_cap_broadcast_source_setup(const void *cmd, uint16_t cmd_len,
734755

735756
err = cap_broadcast_source_adv_setup(source, &gap_settings);
736757
if (err != 0) {
758+
LOG_DBG("Failed to setup adv for source: %d", err);
737759
return BTP_STATUS_FAILED;
738760
}
739761

762+
rp->source_id = cp->source_id;
740763
rp->gap_settings = gap_settings;
741764
sys_put_le24(source->broadcast_id, rp->broadcast_id);
742765
*rsp_len = sizeof(*rp);

0 commit comments

Comments
 (0)