33 *
44 * SPDX-License-Identifier: Apache-2.0
55 */
6+ #include <errno.h>
67#include <stdbool.h>
78#include <stddef.h>
89#include <stdint.h>
10+ #include <stdlib.h>
911#include <string.h>
1012
1113#include <zephyr/autoconf.h>
@@ -48,6 +50,8 @@ static uint8_t bis_codec_data[] = {
4850 BT_AUDIO_CODEC_DATA (BT_AUDIO_CODEC_CFG_CHAN_ALLOC ,
4951 BT_BYTES_LIST_LE32 (BT_AUDIO_LOCATION_FRONT_CENTER )),
5052};
53+ static unsigned long subgroup_cnt_arg = 1 ;
54+ static unsigned long streams_per_subgroup_cnt_arg = 1 ;
5155
5256static K_SEM_DEFINE (sem_stream_started , 0U , ARRAY_SIZE (broadcast_source_streams )) ;
5357static K_SEM_DEFINE (sem_stream_stopped , 0U , ARRAY_SIZE (broadcast_source_streams )) ;
@@ -251,13 +255,21 @@ static int setup_broadcast_source(struct bt_bap_broadcast_source **source, bool
251255 stream_params [ARRAY_SIZE (broadcast_source_streams )];
252256 struct bt_bap_broadcast_source_subgroup_param
253257 subgroup_params [CONFIG_BT_BAP_BROADCAST_SRC_SUBGROUP_COUNT ];
258+ const unsigned long stream_cnt = subgroup_cnt_arg * streams_per_subgroup_cnt_arg ;
254259 struct bt_bap_broadcast_source_param create_param ;
255260 int err ;
256261
262+ if (stream_cnt > ARRAY_SIZE (stream_params )) {
263+ printk ("Unable to create broadcast source with %lu subgroups with %lu streams each "
264+ "(%lu total)\n" ,
265+ subgroup_cnt_arg , streams_per_subgroup_cnt_arg , stream_cnt );
266+ return - ENOMEM ;
267+ }
268+
257269 (void )memset (broadcast_source_streams , 0 ,
258270 sizeof (broadcast_source_streams ));
259271
260- for (size_t i = 0 ; i < ARRAY_SIZE ( stream_params ) ; i ++ ) {
272+ for (size_t i = 0 ; i < stream_cnt ; i ++ ) {
261273 stream_params [i ].stream =
262274 bap_stream_from_audio_test_stream (& broadcast_source_streams [i ]);
263275 bt_bap_stream_cb_register (stream_params [i ].stream ,
@@ -268,13 +280,13 @@ static int setup_broadcast_source(struct bt_bap_broadcast_source **source, bool
268280#endif /* CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE > 0 */
269281 }
270282
271- for (size_t i = 0U ; i < ARRAY_SIZE ( subgroup_params ) ; i ++ ) {
272- subgroup_params [i ].params_count = ARRAY_SIZE ( stream_params ) ;
273- subgroup_params [i ].params = & stream_params [i ];
283+ for (size_t i = 0U ; i < subgroup_cnt_arg ; i ++ ) {
284+ subgroup_params [i ].params_count = streams_per_subgroup_cnt_arg ;
285+ subgroup_params [i ].params = & stream_params [i * streams_per_subgroup_cnt_arg ];
274286 subgroup_params [i ].codec_cfg = & preset_16_1_1 .codec_cfg ;
275287 }
276288
277- create_param .params_count = ARRAY_SIZE ( subgroup_params ) ;
289+ create_param .params_count = subgroup_cnt_arg ;
278290 create_param .params = subgroup_params ;
279291 create_param .qos = & preset_16_2_1 .qos ;
280292 create_param .packing = BT_ISO_PACKING_SEQUENTIAL ;
@@ -283,15 +295,15 @@ static int setup_broadcast_source(struct bt_bap_broadcast_source **source, bool
283295 memcpy (create_param .broadcast_code , BROADCAST_CODE , sizeof (BROADCAST_CODE ));
284296 }
285297
286- printk ("Creating broadcast source with %zu subgroups and %zu streams\n" ,
287- ARRAY_SIZE ( subgroup_params ), ARRAY_SIZE ( stream_params ) );
298+ printk ("Creating broadcast source with %lu subgroups and %lu streams\n" , subgroup_cnt_arg ,
299+ stream_cnt );
288300 err = bt_bap_broadcast_source_create (& create_param , source );
289301 if (err != 0 ) {
290302 printk ("Unable to create broadcast source: %d\n" , err );
291303 return err ;
292304 }
293305
294- for (size_t i = 0U ; i < ARRAY_SIZE ( broadcast_source_streams ) ; i ++ ) {
306+ for (size_t i = 0U ; i < stream_cnt ; i ++ ) {
295307 struct audio_test_stream * test_stream = & broadcast_source_streams [i ];
296308
297309 test_stream -> tx_sdu_size = preset_16_1_1 .qos .sdu ;
@@ -393,23 +405,24 @@ static void test_broadcast_source_reconfig(struct bt_bap_broadcast_source *sourc
393405 stream_params [ARRAY_SIZE (broadcast_source_streams )];
394406 struct bt_bap_broadcast_source_subgroup_param
395407 subgroup_params [CONFIG_BT_BAP_BROADCAST_SRC_SUBGROUP_COUNT ];
408+ const unsigned long stream_cnt = subgroup_cnt_arg * streams_per_subgroup_cnt_arg ;
396409 struct bt_bap_broadcast_source_param reconfig_param ;
397410 int err ;
398411
399- for (size_t i = 0 ; i < ARRAY_SIZE ( stream_params ) ; i ++ ) {
412+ for (size_t i = 0 ; i < stream_cnt ; i ++ ) {
400413 stream_params [i ].stream =
401414 bap_stream_from_audio_test_stream (& broadcast_source_streams [i ]);
402415 stream_params [i ].data_len = ARRAY_SIZE (bis_codec_data );
403416 stream_params [i ].data = bis_codec_data ;
404417 }
405418
406- for (size_t i = 0U ; i < ARRAY_SIZE ( subgroup_params ) ; i ++ ) {
407- subgroup_params [i ].params_count = 1U ;
408- subgroup_params [i ].params = & stream_params [i ];
419+ for (size_t i = 0U ; i < subgroup_cnt_arg ; i ++ ) {
420+ subgroup_params [i ].params_count = streams_per_subgroup_cnt_arg ;
421+ subgroup_params [i ].params = & stream_params [i * streams_per_subgroup_cnt_arg ];
409422 subgroup_params [i ].codec_cfg = & preset_16_1_1 .codec_cfg ;
410423 }
411424
412- reconfig_param .params_count = ARRAY_SIZE ( subgroup_params ) ;
425+ reconfig_param .params_count = subgroup_cnt_arg ;
413426 reconfig_param .params = subgroup_params ;
414427 reconfig_param .qos = & preset_16_1_1 .qos ;
415428 reconfig_param .packing = BT_ISO_PACKING_SEQUENTIAL ;
@@ -422,7 +435,7 @@ static void test_broadcast_source_reconfig(struct bt_bap_broadcast_source *sourc
422435 return ;
423436 }
424437
425- for (size_t i = 0U ; i < ARRAY_SIZE ( broadcast_source_streams ) ; i ++ ) {
438+ for (size_t i = 0U ; i < stream_cnt ; i ++ ) {
426439 struct audio_test_stream * test_stream = & broadcast_source_streams [i ];
427440
428441 test_stream -> tx_sdu_size = preset_16_1_1 .qos .sdu ;
@@ -432,6 +445,7 @@ static void test_broadcast_source_reconfig(struct bt_bap_broadcast_source *sourc
432445static void test_broadcast_source_start (struct bt_bap_broadcast_source * source ,
433446 struct bt_le_ext_adv * adv )
434447{
448+ const unsigned long stream_cnt = subgroup_cnt_arg * streams_per_subgroup_cnt_arg ;
435449 int err ;
436450
437451 printk ("Starting broadcast source\n" );
@@ -442,8 +456,8 @@ static void test_broadcast_source_start(struct bt_bap_broadcast_source *source,
442456 }
443457
444458 /* Wait for all to be started */
445- printk ("Waiting for %zu streams to be started\n" , ARRAY_SIZE ( broadcast_source_streams ) );
446- for (size_t i = 0U ; i < ARRAY_SIZE ( broadcast_source_streams ) ; i ++ ) {
459+ printk ("Waiting for %lu streams to be started\n" , stream_cnt );
460+ for (size_t i = 0U ; i < stream_cnt ; i ++ ) {
447461 k_sem_take (& sem_stream_started , K_FOREVER );
448462 }
449463
@@ -482,6 +496,7 @@ static void test_broadcast_source_update_metadata(struct bt_bap_broadcast_source
482496
483497static void test_broadcast_source_stop (struct bt_bap_broadcast_source * source )
484498{
499+ const unsigned long stream_cnt = subgroup_cnt_arg * streams_per_subgroup_cnt_arg ;
485500 int err ;
486501
487502 printk ("Stopping broadcast source\n" );
@@ -493,8 +508,8 @@ static void test_broadcast_source_stop(struct bt_bap_broadcast_source *source)
493508 }
494509
495510 /* Wait for all to be stopped */
496- printk ("Waiting for %zu streams to be stopped\n" , ARRAY_SIZE ( broadcast_source_streams ) );
497- for (size_t i = 0U ; i < ARRAY_SIZE ( broadcast_source_streams ) ; i ++ ) {
511+ printk ("Waiting for %lu streams to be stopped\n" , stream_cnt );
512+ for (size_t i = 0U ; i < stream_cnt ; i ++ ) {
498513 k_sem_take (& sem_stream_stopped , K_FOREVER );
499514 }
500515
@@ -669,18 +684,48 @@ static void test_main_encrypted(void)
669684 PASS ("Broadcast source encrypted passed\n" );
670685}
671686
687+ static void test_args (int argc , char * argv [])
688+ {
689+ for (size_t argn = 0 ; argn < argc ; argn ++ ) {
690+ const char * arg = argv [argn ];
691+
692+ if (strcmp (arg , "subgroup_cnt" ) == 0 ) {
693+ arg = argv [++ argn ];
694+ subgroup_cnt_arg = strtoul (arg , NULL , 10 );
695+
696+ if (!IN_RANGE (subgroup_cnt_arg , 1 ,
697+ CONFIG_BT_BAP_BROADCAST_SRC_SUBGROUP_COUNT )) {
698+ FAIL ("Invalid number of subgroups: %lu\n" , subgroup_cnt_arg );
699+ }
700+ } else if (strcmp (arg , "streams_per_subgroup_cnt" ) == 0 ) {
701+ arg = argv [++ argn ];
702+ streams_per_subgroup_cnt_arg = strtoul (arg , NULL , 10 );
703+
704+ if (!IN_RANGE (streams_per_subgroup_cnt_arg , 1 ,
705+ CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT )) {
706+ FAIL ("Invalid number of streams per subgroup: %lu\n" ,
707+ streams_per_subgroup_cnt_arg );
708+ }
709+ } else {
710+ FAIL ("Invalid arg: %s\n" , arg );
711+ }
712+ }
713+ }
714+
672715static const struct bst_test_instance test_broadcast_source [] = {
673716 {
674717 .test_id = "broadcast_source" ,
675718 .test_pre_init_f = test_init ,
676719 .test_tick_f = test_tick ,
677720 .test_main_f = test_main ,
721+ .test_args_f = test_args ,
678722 },
679723 {
680724 .test_id = "broadcast_source_encrypted" ,
681725 .test_pre_init_f = test_init ,
682726 .test_tick_f = test_tick ,
683727 .test_main_f = test_main_encrypted ,
728+ .test_args_f = test_args ,
684729 },
685730 BSTEST_END_MARKER ,
686731};
0 commit comments