Skip to content

Commit b7f5e7e

Browse files
Andries Kruithofnashif
authored andcommitted
Bluetooth: Audio: CAP: refactoring babblesim test for acceptor
The babblesim tests for the CAP acceptor test have been refactored for improved readability Signed-off-by: Andries Kruithof <[email protected]>
1 parent f775ab4 commit b7f5e7e

File tree

1 file changed

+56
-83
lines changed

1 file changed

+56
-83
lines changed

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

Lines changed: 56 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -913,27 +913,31 @@ static uint16_t interval_to_sync_timeout(uint16_t pa_interval)
913913
return pa_timeout;
914914
}
915915

916-
static int pa_sync_create(void)
916+
static void pa_sync_create(void)
917917
{
918918
struct bt_le_per_adv_sync_param create_params = {0};
919+
int err;
919920

920921
bt_addr_le_copy(&create_params.addr, &broadcaster_addr);
921922
create_params.options = BT_LE_PER_ADV_SYNC_OPT_FILTER_DUPLICATE;
922923
create_params.sid = broadcaster_info.sid;
923924
create_params.skip = PA_SYNC_SKIP;
924925
create_params.timeout = interval_to_sync_timeout(broadcaster_info.interval);
925926

926-
return bt_le_per_adv_sync_create(&create_params, &pa_sync);
927+
err = bt_le_per_adv_sync_create(&create_params, &pa_sync);
928+
if (err != 0) {
929+
FAIL("Could not create Broadcast PA sync: %d\n", err);
930+
return;
931+
}
932+
933+
printk("Broadcast source found, waiting for PA sync\n");
934+
WAIT_FOR_FLAG(flag_pa_synced);
927935
}
928936

929-
static void test_cap_acceptor_broadcast(void)
937+
static void pa_sync_to_broadcaster(void)
930938
{
931-
static struct bt_bap_stream *bap_streams[ARRAY_SIZE(broadcast_sink_streams)];
932-
size_t stream_count;
933939
int err;
934940

935-
init();
936-
937941
printk("Scanning for broadcast sources\n");
938942
err = bt_le_scan_start(BT_LE_SCAN_ACTIVE, NULL);
939943
if (err != 0) {
@@ -952,14 +956,13 @@ static void test_cap_acceptor_broadcast(void)
952956

953957
printk("Scan stopped, attempting to PA sync to the broadcaster with id 0x%06X\n",
954958
broadcaster_broadcast_id);
955-
err = pa_sync_create();
956-
if (err != 0) {
957-
FAIL("Could not create Broadcast PA sync: %d\n", err);
958-
return;
959-
}
960959

961-
printk("Broadcast source found, waiting for PA sync\n");
962-
WAIT_FOR_FLAG(flag_pa_synced);
960+
pa_sync_create();
961+
}
962+
963+
static void create_and_sync_sink(struct bt_bap_stream *bap_streams[], size_t *stream_count)
964+
{
965+
int err;
963966

964967
printk("Creating the broadcast sink\n");
965968
err = bt_bap_broadcast_sink_create(pa_sync, broadcaster_broadcast_id, &g_broadcast_sink);
@@ -980,10 +983,10 @@ static void test_cap_acceptor_broadcast(void)
980983
}
981984

982985
printk("Syncing the sink\n");
983-
stream_count = 0;
986+
*stream_count = 0;
984987
for (int i = 1; i < BT_ISO_MAX_GROUP_ISO_COUNT; i++) {
985988
if ((bis_index_bitfield & BIT(i)) != 0) {
986-
stream_count++;
989+
*stream_count += 1;
987990
}
988991
}
989992

@@ -994,15 +997,28 @@ static void test_cap_acceptor_broadcast(void)
994997
}
995998

996999
/* Wait for all to be started */
997-
printk("Waiting for %zu streams to be started\n", stream_count);
998-
for (size_t i = 0U; i < stream_count; i++) {
1000+
printk("Waiting for %zu streams to be started\n", *stream_count);
1001+
for (size_t i = 0U; i < *stream_count; i++) {
9991002
k_sem_take(&sem_broadcast_started, K_FOREVER);
10001003
}
1004+
}
10011005

1006+
static void sink_wait_for_data(void)
1007+
{
10021008
printk("Waiting for data\n");
10031009
WAIT_FOR_FLAG(flag_received);
10041010
backchannel_sync_send_all(); /* let other devices know we have received what we wanted */
1011+
}
1012+
1013+
static void base_wait_for_metadata_update(void)
1014+
{
1015+
printk("Waiting for meta update\n");
1016+
WAIT_FOR_FLAG(flag_base_metadata_updated);
1017+
backchannel_sync_send_all(); /* let others know we have received a metadata update */
1018+
}
10051019

1020+
static void wait_for_streams_stop(int stream_count)
1021+
{
10061022
/* The order of PA sync lost and BIG Sync lost is irrelevant
10071023
* and depend on timeout parameters. We just wait for PA first, but
10081024
* either way will work.
@@ -1014,93 +1030,50 @@ static void test_cap_acceptor_broadcast(void)
10141030
for (size_t i = 0U; i < stream_count; i++) {
10151031
k_sem_take(&sem_broadcast_stopped, K_FOREVER);
10161032
}
1017-
1018-
PASS("CAP acceptor broadcast passed\n");
10191033
}
10201034

1021-
static void test_cap_acceptor_broadcast_reception(void)
1035+
static void test_cap_acceptor_broadcast(void)
10221036
{
10231037
static struct bt_bap_stream *bap_streams[ARRAY_SIZE(broadcast_sink_streams)];
10241038
size_t stream_count;
1025-
int err;
10261039

10271040
init();
10281041

1029-
WAIT_FOR_FLAG(flag_pa_request);
1042+
pa_sync_to_broadcaster();
10301043

1031-
err = pa_sync_create();
1032-
if (err != 0) {
1033-
FAIL("Could not create Broadcast PA sync: %d\n", err);
1034-
return;
1035-
}
1044+
create_and_sync_sink(bap_streams, &stream_count);
10361045

1037-
printk("Waiting for PA sync\n");
1038-
WAIT_FOR_FLAG(flag_pa_synced);
1046+
sink_wait_for_data();
10391047

1040-
err = bt_bap_broadcast_sink_create(pa_sync, broadcaster_broadcast_id, &g_broadcast_sink);
1041-
if (err != 0) {
1042-
FAIL("Unable to create the sink: %d\n", err);
1043-
return;
1044-
}
1048+
wait_for_streams_stop(stream_count);
10451049

1046-
if (req_recv_state->num_subgroups == 0) {
1047-
FAIL("Number of subgroups is 0");
1048-
return;
1049-
}
1050-
1051-
printk("Broadcast source PA synced, waiting for BASE\n");
1052-
WAIT_FOR_FLAG(flag_base_received);
1053-
printk("BASE received\n");
1054-
1055-
WAIT_FOR_FLAG(flag_syncable);
1056-
1057-
for (size_t i = 0U; i < ARRAY_SIZE(broadcast_sink_streams); i++) {
1058-
bap_streams[i] = bap_stream_from_audio_test_stream(&broadcast_sink_streams[i]);
1059-
}
1050+
PASS("CAP acceptor broadcast passed\n");
1051+
}
10601052

1061-
printk("Syncing the sink\n");
1062-
stream_count = 0;
1063-
for (int i = 1; i < BT_ISO_MAX_GROUP_ISO_COUNT; i++) {
1064-
if ((bis_index_bitfield & BIT(i)) != 0) {
1065-
stream_count++;
1066-
}
1067-
}
1053+
static void test_cap_acceptor_broadcast_reception(void)
1054+
{
1055+
static struct bt_bap_stream *bap_streams[ARRAY_SIZE(broadcast_sink_streams)];
1056+
size_t stream_count;
10681057

1069-
err = bt_bap_broadcast_sink_sync(g_broadcast_sink, bis_index_bitfield, bap_streams, NULL);
1070-
if (err != 0) {
1071-
FAIL("Unable to sync the sink: %d\n", err);
1072-
return;
1073-
}
1058+
init();
10741059

1075-
/* Wait for all to be started */
1076-
printk("Waiting for %zu streams to be started\n", stream_count);
1077-
for (size_t i = 0U; i < stream_count; i++) {
1078-
k_sem_take(&sem_broadcast_started, K_FOREVER);
1079-
}
1060+
WAIT_FOR_FLAG(flag_pa_request);
10801061

1081-
printk("Waiting for data\n");
1082-
WAIT_FOR_FLAG(flag_received);
1062+
pa_sync_create();
10831063

1084-
backchannel_sync_send_all(); /* let others know we have received some data */
1064+
create_and_sync_sink(bap_streams, &stream_count);
10851065

1086-
printk("Waiting for meta update\n");
1087-
WAIT_FOR_FLAG(flag_base_metadata_updated);
1066+
sink_wait_for_data();
10881067

1089-
backchannel_sync_send_all(); /* let others know we have received a metadata update */
1068+
/* Since we are re-using the BAP broadcast source test
1069+
* we get a metadata udate, and we need to send an extra
1070+
* backchannel sync
1071+
*/
1072+
base_wait_for_metadata_update();
10901073

10911074
backchannel_sync_send_all(); /* let broadcaster know we can stop the source */
10921075

1093-
/* The order of PA sync lost and BIG Sync lost is irrelevant
1094-
* and depend on timeout parameters. We just wait for PA first, but
1095-
* either way will work.
1096-
*/
1097-
printk("Waiting for PA disconnected\n");
1098-
WAIT_FOR_FLAG(flag_pa_sync_lost);
1099-
1100-
printk("Waiting for %zu streams to be stopped\n", stream_count);
1101-
for (size_t i = 0U; i < stream_count; i++) {
1102-
k_sem_take(&sem_broadcast_stopped, K_FOREVER);
1103-
}
1076+
wait_for_streams_stop(stream_count);
11041077

11051078
PASS("CAP acceptor broadcast reception passed\n");
11061079
}

0 commit comments

Comments
 (0)