@@ -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