diff --git a/tests/bsim/bluetooth/audio/src/bap_broadcast_sink_test.c b/tests/bsim/bluetooth/audio/src/bap_broadcast_sink_test.c index 32522d79cba5c..d7cb7867bf697 100644 --- a/tests/bsim/bluetooth/audio/src/bap_broadcast_sink_test.c +++ b/tests/bsim/bluetooth/audio/src/bap_broadcast_sink_test.c @@ -570,6 +570,7 @@ static void stream_started_cb(struct bt_bap_stream *stream) memset(&test_stream->last_info, 0, sizeof(test_stream->last_info)); test_stream->rx_cnt = 0U; test_stream->valid_rx_cnt = 0U; + UNSET_FLAG(test_stream->flag_audio_received); err = bt_bap_ep_get_info(stream->ep, &info); if (err != 0) { @@ -920,6 +921,17 @@ static void test_broadcast_delete_inval(void) } } +static void wait_for_data(void) +{ + printk("Waiting for data\n"); + ARRAY_FOR_EACH_PTR(broadcast_sink_streams, test_stream) { + if (audio_test_stream_is_streaming(test_stream)) { + WAIT_FOR_FLAG(test_stream->flag_audio_received); + } + } + printk("Data received\n"); +} + static void test_common(void) { int err; @@ -953,8 +965,7 @@ static void test_common(void) k_sem_take(&sem_stream_started, K_FOREVER); } - printk("Waiting for data\n"); - WAIT_FOR_FLAG(flag_audio_received); + wait_for_data(); backchannel_sync_send_all(); /* let other devices know we have received what we wanted */ } @@ -1067,8 +1078,7 @@ static void test_sink_encrypted(void) k_sem_take(&sem_stream_started, K_FOREVER); } - printk("Waiting for data\n"); - WAIT_FOR_FLAG(flag_audio_received); + wait_for_data(); backchannel_sync_send_all(); /* let other devices know we have received data */ @@ -1122,9 +1132,7 @@ static void test_sink_encrypted_incorrect_code(void) k_sem_take(&sem_stream_started, K_FOREVER); } - printk("Waiting for data\n"); - WAIT_FOR_FLAG(flag_audio_received); - printk("Data received\n"); + wait_for_data(); backchannel_sync_send_all(); /* let other devices know we have received data */ backchannel_sync_send_all(); /* let the broadcast source know it can stop */ @@ -1171,8 +1179,7 @@ static void broadcast_sink_with_assistant(void) k_sem_take(&sem_stream_started, K_FOREVER); } - printk("Waiting for data\n"); - WAIT_FOR_FLAG(flag_audio_received); + wait_for_data(); backchannel_sync_send_all(); /* let other devices know we have received what we wanted */ printk("Waiting for BIG sync terminate request\n"); diff --git a/tests/bsim/bluetooth/audio/src/bap_common.c b/tests/bsim/bluetooth/audio/src/bap_common.c index b21e0427e5312..518d167322c01 100644 --- a/tests/bsim/bluetooth/audio/src/bap_common.c +++ b/tests/bsim/bluetooth/audio/src/bap_common.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -19,6 +20,7 @@ #include #include "bap_common.h" +#include "common.h" #define VS_CODEC_CID 0x05F1 /* Linux foundation*/ #define VS_CODEC_VID 0x1234 /* any value*/ @@ -138,3 +140,39 @@ void copy_unicast_stream_preset(struct unicast_stream *stream, memcpy(&stream->qos, &named_preset->preset.qos, sizeof(stream->qos)); memcpy(&stream->codec_cfg, &named_preset->preset.codec_cfg, sizeof(stream->codec_cfg)); } + +bool bap_stream_is_streaming(const struct bt_bap_stream *bap_stream) +{ + struct bt_bap_ep_info ep_info; + int err; + + if (bap_stream == NULL) { + return false; + } + + /* No-op if stream is not configured */ + if (bap_stream->ep == NULL) { + return false; + } + + err = bt_bap_ep_get_info(bap_stream->ep, &ep_info); + if (err != 0) { + return false; + } + + if (ep_info.iso_chan == NULL || ep_info.iso_chan->state != BT_ISO_STATE_CONNECTED) { + return false; + } + + return ep_info.state == BT_BAP_EP_STATE_STREAMING; +} + +bool cap_stream_is_streaming(const struct bt_cap_stream *cap_stream) +{ + return bap_stream_is_streaming(&cap_stream->bap_stream); +} + +bool audio_test_stream_is_streaming(const struct audio_test_stream *test_stream) +{ + return cap_stream_is_streaming(&test_stream->stream); +} diff --git a/tests/bsim/bluetooth/audio/src/bap_common.h b/tests/bsim/bluetooth/audio/src/bap_common.h index fcfcabaad583e..dc4c16ab106a0 100644 --- a/tests/bsim/bluetooth/audio/src/bap_common.h +++ b/tests/bsim/bluetooth/audio/src/bap_common.h @@ -117,5 +117,7 @@ static inline bool valid_metadata_type(uint8_t type, uint8_t len) return false; } } - +bool bap_stream_is_streaming(const struct bt_bap_stream *bap_stream); +bool cap_stream_is_streaming(const struct bt_cap_stream *cap_stream); +bool audio_test_stream_is_streaming(const struct audio_test_stream *test_stream); #endif /* ZEPHYR_TEST_BSIM_BT_AUDIO_TEST_COMMON_ */ diff --git a/tests/bsim/bluetooth/audio/src/bap_stream_rx.c b/tests/bsim/bluetooth/audio/src/bap_stream_rx.c index 094fbf55cbdbf..b910e82dfea22 100644 --- a/tests/bsim/bluetooth/audio/src/bap_stream_rx.c +++ b/tests/bsim/bluetooth/audio/src/bap_stream_rx.c @@ -38,8 +38,7 @@ void bap_stream_rx_recv_cb(struct bt_bap_stream *stream, const struct bt_iso_rec test_stream->valid_rx_cnt++; if (test_stream->valid_rx_cnt >= MIN_SEND_COUNT) { - /* We set the flag is just one stream has received the expected */ - SET_FLAG(flag_audio_received); + SET_FLAG(test_stream->flag_audio_received); } } else { log_stream_rx(stream, info, buf); @@ -68,7 +67,8 @@ void bap_stream_rx_recv_cb(struct bt_bap_stream *stream, const struct bt_iso_rec if (info->flags & BT_ISO_FLAGS_ERROR) { /* Fail the test if we have not received what we expected */ log_stream_rx(stream, info, buf); - if (test_stream->valid_rx_cnt > 1U && !TEST_FLAG(flag_audio_received)) { + if (test_stream->valid_rx_cnt > 1U && + !TEST_FLAG(test_stream->flag_audio_received)) { FAIL("ISO receive error\n"); } } diff --git a/tests/bsim/bluetooth/audio/src/bap_stream_tx.c b/tests/bsim/bluetooth/audio/src/bap_stream_tx.c index 1f51f0be6cf98..6c70eb4c934e3 100644 --- a/tests/bsim/bluetooth/audio/src/bap_stream_tx.c +++ b/tests/bsim/bluetooth/audio/src/bap_stream_tx.c @@ -27,6 +27,7 @@ #include #include +#include "bap_common.h" #include "bap_stream_tx.h" #include "common.h" @@ -43,32 +44,6 @@ struct tx_stream { static struct tx_stream tx_streams[CONFIG_BT_ISO_MAX_CHAN]; -static bool stream_is_streaming(const struct bt_bap_stream *bap_stream) -{ - struct bt_bap_ep_info ep_info; - int err; - - if (bap_stream == NULL) { - return false; - } - - /* No-op if stream is not configured */ - if (bap_stream->ep == NULL) { - return false; - } - - err = bt_bap_ep_get_info(bap_stream->ep, &ep_info); - if (err != 0) { - return false; - } - - if (ep_info.iso_chan == NULL || ep_info.iso_chan->state != BT_ISO_STATE_CONNECTED) { - return false; - } - - return ep_info.state == BT_BAP_EP_STATE_STREAMING; -} - static void tx_thread_func(void *arg1, void *arg2, void *arg3) { NET_BUF_POOL_FIXED_DEFINE(tx_pool, CONFIG_BT_ISO_TX_BUF_COUNT, @@ -88,7 +63,7 @@ static void tx_thread_func(void *arg1, void *arg2, void *arg3) for (size_t i = 0U; i < ARRAY_SIZE(tx_streams); i++) { struct bt_bap_stream *bap_stream = tx_streams[i].bap_stream; - if (stream_is_streaming(bap_stream) && + if (bap_stream_is_streaming(bap_stream) && atomic_get(&tx_streams[i].enqueued) < ENQUEUE_CNT) { struct net_buf *buf; @@ -102,7 +77,7 @@ static void tx_thread_func(void *arg1, void *arg2, void *arg3) tx_streams[i].seq_num++; atomic_inc(&tx_streams[i].enqueued); } else { - if (!stream_is_streaming(bap_stream)) { + if (!bap_stream_is_streaming(bap_stream)) { /* Can happen if we disconnected while waiting for a * buffer - Ignore */ diff --git a/tests/bsim/bluetooth/audio/src/bap_unicast_client_test.c b/tests/bsim/bluetooth/audio/src/bap_unicast_client_test.c index 69da47a4e2e5b..796184bfef87e 100644 --- a/tests/bsim/bluetooth/audio/src/bap_unicast_client_test.c +++ b/tests/bsim/bluetooth/audio/src/bap_unicast_client_test.c @@ -850,8 +850,11 @@ static void transceive_streams(void) } if (source_stream != NULL) { + struct audio_test_stream *test_stream = + audio_test_stream_from_bap_stream(source_stream); + printk("Waiting for data\n"); - WAIT_FOR_FLAG(flag_audio_received); + WAIT_FOR_FLAG(test_stream->flag_audio_received); } } diff --git a/tests/bsim/bluetooth/audio/src/bap_unicast_server_test.c b/tests/bsim/bluetooth/audio/src/bap_unicast_server_test.c index 980915d21b8c3..515b4714d842b 100644 --- a/tests/bsim/bluetooth/audio/src/bap_unicast_server_test.c +++ b/tests/bsim/bluetooth/audio/src/bap_unicast_server_test.c @@ -336,8 +336,11 @@ static void transceive_test_streams(void) } if (sink_stream != NULL) { + struct audio_test_stream *test_stream = + audio_test_stream_from_bap_stream(sink_stream); + printk("Waiting for data\n"); - WAIT_FOR_FLAG(flag_audio_received); + WAIT_FOR_FLAG(test_stream->flag_audio_received); } } diff --git a/tests/bsim/bluetooth/audio/src/cap_acceptor_test.c b/tests/bsim/bluetooth/audio/src/cap_acceptor_test.c index af3bb51429d68..07c140c3909fc 100644 --- a/tests/bsim/bluetooth/audio/src/cap_acceptor_test.c +++ b/tests/bsim/bluetooth/audio/src/cap_acceptor_test.c @@ -282,6 +282,7 @@ static void started_cb(struct bt_bap_stream *stream) test_stream->valid_rx_cnt = 0U; test_stream->seq_num = 0U; test_stream->tx_cnt = 0U; + UNSET_FLAG(test_stream->flag_audio_received); printk("Stream %p started\n", stream); k_sem_give(&sem_broadcast_started); @@ -333,6 +334,7 @@ static void unicast_stream_started(struct bt_bap_stream *stream) test_stream->valid_rx_cnt = 0U; test_stream->seq_num = 0U; test_stream->tx_cnt = 0U; + UNSET_FLAG(test_stream->flag_audio_received); printk("Started stream %p\n", stream); @@ -796,7 +798,6 @@ static void init(void) UNSET_FLAG(flag_base_received); UNSET_FLAG(flag_pa_synced); UNSET_FLAG(flag_pa_request); - UNSET_FLAG(flag_audio_received); UNSET_FLAG(flag_base_metadata_updated); UNSET_FLAG(flag_bis_sync_requested); @@ -882,7 +883,11 @@ static void init(void) static void wait_for_data(void) { printk("Waiting for data\n"); - WAIT_FOR_FLAG(flag_audio_received); + ARRAY_FOR_EACH_PTR(broadcast_sink_streams, test_stream) { + if (audio_test_stream_is_streaming(test_stream)) { + WAIT_FOR_FLAG(test_stream->flag_audio_received); + } + } printk("Data received\n"); } diff --git a/tests/bsim/bluetooth/audio/src/cap_handover_peripheral_test.c b/tests/bsim/bluetooth/audio/src/cap_handover_peripheral_test.c index 98166d3bab35c..27320d256621d 100644 --- a/tests/bsim/bluetooth/audio/src/cap_handover_peripheral_test.c +++ b/tests/bsim/bluetooth/audio/src/cap_handover_peripheral_test.c @@ -769,10 +769,15 @@ static void create_and_sync_sink(void) static void wait_for_data(void) { - UNSET_FLAG(flag_audio_received); - LOG_DBG("Waiting for data"); - WAIT_FOR_FLAG(flag_audio_received); + + ARRAY_FOR_EACH_PTR(streams, test_stream) { + if (audio_test_stream_is_streaming(test_stream) && + bap_stream_rx_can_recv(&test_stream->stream.bap_stream)) { + WAIT_FOR_FLAG(test_stream->flag_audio_received); + } + } + LOG_DBG("Data received"); } diff --git a/tests/bsim/bluetooth/audio/src/cap_initiator_unicast_test.c b/tests/bsim/bluetooth/audio/src/cap_initiator_unicast_test.c index a1b7868a4a478..8ce7353bc3851 100644 --- a/tests/bsim/bluetooth/audio/src/cap_initiator_unicast_test.c +++ b/tests/bsim/bluetooth/audio/src/cap_initiator_unicast_test.c @@ -173,6 +173,7 @@ static void unicast_stream_started(struct bt_bap_stream *stream) test_stream->valid_rx_cnt = 0U; test_stream->seq_num = 0U; test_stream->tx_cnt = 0U; + UNSET_FLAG(test_stream->flag_audio_received); printk("Started stream %p\n", stream); @@ -905,6 +906,17 @@ static void unicast_group_delete(struct bt_cap_unicast_group *unicast_group) } } +static void wait_for_data(void) +{ + printk("Waiting for data\n"); + ARRAY_FOR_EACH_PTR(unicast_client_source_streams, test_stream) { + if (audio_test_stream_is_streaming(test_stream)) { + WAIT_FOR_FLAG(test_stream->flag_audio_received); + } + } + printk("Data received\n"); +} + static void test_main_cap_initiator_unicast(void) { struct bt_cap_unicast_group *unicast_group; @@ -930,14 +942,15 @@ static void test_main_cap_initiator_unicast(void) for (size_t j = 0U; j < iterations; j++) { printk("\nRunning iteration j=%zu\n\n", i); - UNSET_FLAG(flag_audio_received); + ARRAY_FOR_EACH_PTR(unicast_client_sink_streams, test_stream) { + UNSET_FLAG(test_stream->flag_audio_received); + } unicast_audio_start(unicast_group, true); unicast_audio_update(); - printk("Waiting for data\n"); - WAIT_FOR_FLAG(flag_audio_received); + wait_for_data(); /* Due to how the backchannel sync is implemented for LE Audio we cannot * easily tell the remote (CAP acceptor) how many times to wait for data, @@ -981,9 +994,7 @@ static void test_main_cap_initiator_unicast_inval(void) unicast_audio_update_inval(); unicast_audio_update(); - printk("Waiting for data\n"); - WAIT_FOR_FLAG(flag_audio_received); - printk("Data received\n"); + wait_for_data(); /* Wait until acceptors have received expected data */ backchannel_sync_wait_all(); @@ -1091,9 +1102,7 @@ static void test_cap_initiator_unicast_ase_error(void) /* Without invalid metadata type, start should pass */ unicast_audio_start(unicast_group, true); - printk("Waiting for data\n"); - WAIT_FOR_FLAG(flag_audio_received); - printk("Data received\n"); + wait_for_data(); /* Wait until acceptors have received expected data */ backchannel_sync_wait_all(); @@ -1487,7 +1496,7 @@ static void test_cap_initiator_ac(const struct cap_initiator_ac_param *param) if (expect_rx) { printk("Waiting for data\n"); - WAIT_FOR_FLAG(flag_audio_received); + wait_for_data(); } cap_initiator_unicast_audio_stop(unicast_group); diff --git a/tests/bsim/bluetooth/audio/src/common.c b/tests/bsim/bluetooth/audio/src/common.c index 29e8c5cc23797..2018b05aac673 100644 --- a/tests/bsim/bluetooth/audio/src/common.c +++ b/tests/bsim/bluetooth/audio/src/common.c @@ -42,7 +42,6 @@ struct bt_conn *default_conn; atomic_t flag_connected; atomic_t flag_disconnected; atomic_t flag_conn_updated; -atomic_t flag_audio_received; volatile bt_security_t security_level; #if defined(CONFIG_BT_CSIP_SET_MEMBER) uint8_t csip_rsi[BT_CSIP_RSI_SIZE]; diff --git a/tests/bsim/bluetooth/audio/src/common.h b/tests/bsim/bluetooth/audio/src/common.h index 916ed29994d6f..4d8739ffbc55f 100644 --- a/tests/bsim/bluetooth/audio/src/common.h +++ b/tests/bsim/bluetooth/audio/src/common.h @@ -140,7 +140,6 @@ extern struct bt_conn *default_conn; extern atomic_t flag_connected; extern atomic_t flag_disconnected; extern atomic_t flag_conn_updated; -extern atomic_t flag_audio_received; extern volatile bt_security_t security_level; extern uint8_t csip_rsi[BT_CSIP_RSI_SIZE]; @@ -170,6 +169,7 @@ struct audio_test_stream { struct bt_iso_recv_info last_info; size_t rx_cnt; size_t valid_rx_cnt; + atomic_t flag_audio_received; }; static inline struct bt_cap_stream *cap_stream_from_bap_stream(struct bt_bap_stream *bap_stream) diff --git a/tests/bsim/bluetooth/audio/src/gmap_ugg_test.c b/tests/bsim/bluetooth/audio/src/gmap_ugg_test.c index a4adecd183efe..185422d58cb6c 100644 --- a/tests/bsim/bluetooth/audio/src/gmap_ugg_test.c +++ b/tests/bsim/bluetooth/audio/src/gmap_ugg_test.c @@ -187,6 +187,7 @@ static void stream_started_cb(struct bt_bap_stream *stream) test_stream->valid_rx_cnt = 0U; test_stream->seq_num = 0U; test_stream->tx_cnt = 0U; + UNSET_FLAG(test_stream->flag_audio_received); printk("Started stream %p\n", stream); @@ -935,6 +936,20 @@ static void unicast_group_delete(struct bt_cap_unicast_group *unicast_group) } } +static void wait_for_data(void) +{ + printk("Waiting for data\n"); + + ARRAY_FOR_EACH_PTR(unicast_streams, unicast_stream) { + if (bap_stream_rx_can_recv(&unicast_stream->stream.stream.bap_stream) && + audio_test_stream_is_streaming(&unicast_stream->stream)) { + WAIT_FOR_FLAG(unicast_stream->stream.flag_audio_received); + } + } + + printk("Data received\n"); +} + static void test_gmap_ugg_unicast_ac(const struct gmap_unicast_ac_param *param) { struct bt_cap_unicast_group *unicast_group; @@ -994,8 +1009,7 @@ static void test_gmap_ugg_unicast_ac(const struct gmap_unicast_ac_param *param) } if (expect_rx) { - printk("Waiting for data\n"); - WAIT_FOR_FLAG(flag_audio_received); + wait_for_data(); } cap_initiator_unicast_audio_stop(unicast_group); diff --git a/tests/bsim/bluetooth/audio/src/gmap_ugt_test.c b/tests/bsim/bluetooth/audio/src/gmap_ugt_test.c index f2a606a2227c9..e41a6259dea23 100644 --- a/tests/bsim/bluetooth/audio/src/gmap_ugt_test.c +++ b/tests/bsim/bluetooth/audio/src/gmap_ugt_test.c @@ -90,6 +90,7 @@ static void unicast_stream_started_cb(struct bt_bap_stream *stream) test_stream->valid_rx_cnt = 0U; test_stream->seq_num = 0U; test_stream->tx_cnt = 0U; + UNSET_FLAG(test_stream->flag_audio_received); printk("Started stream %p\n", stream); @@ -395,11 +396,16 @@ static void discover_gmas(struct bt_conn *conn) static void wait_for_data(void) { - if (expect_rx) { - printk("Waiting for data\n"); - WAIT_FOR_FLAG(flag_audio_received); - printk("Data received\n"); + printk("Waiting for data\n"); + + ARRAY_FOR_EACH_PTR(unicast_streams, test_stream) { + if (bap_stream_rx_can_recv(&test_stream->stream.bap_stream) && + audio_test_stream_is_streaming(test_stream)) { + WAIT_FOR_FLAG(test_stream->flag_audio_received); + } } + + printk("Data received\n"); /* let initiator know we have received what we wanted */ backchannel_sync_send(GMAP_UGG_DEV_ID); } diff --git a/tests/bsim/bluetooth/audio/src/pbp_public_broadcast_sink_test.c b/tests/bsim/bluetooth/audio/src/pbp_public_broadcast_sink_test.c index c96e3fa8604b2..ccc341d05bb08 100644 --- a/tests/bsim/bluetooth/audio/src/pbp_public_broadcast_sink_test.c +++ b/tests/bsim/bluetooth/audio/src/pbp_public_broadcast_sink_test.c @@ -29,6 +29,7 @@ #include #include +#include "bap_common.h" #include "bap_stream_rx.h" #include "bstests.h" #include "common.h" @@ -100,6 +101,7 @@ static void started_cb(struct bt_bap_stream *stream) memset(&test_stream->last_info, 0, sizeof(test_stream->last_info)); test_stream->rx_cnt = 0U; test_stream->valid_rx_cnt = 0U; + UNSET_FLAG(test_stream->flag_audio_received); printk("Stream %p started\n", stream); } @@ -176,7 +178,6 @@ static int reset(void) k_sem_reset(&sem_base_received); k_sem_reset(&sem_syncable); k_sem_reset(&sem_pa_sync_lost); - UNSET_FLAG(flag_audio_received); broadcast_id = BT_BAP_INVALID_BROADCAST_ID; bis_index_bitfield = 0U; @@ -325,6 +326,17 @@ static void broadcast_scan_recv(const struct bt_le_scan_recv_info *info, } } +static void wait_for_data(void) +{ + printk("Waiting for data\n"); + ARRAY_FOR_EACH_PTR(test_streams, test_stream) { + if (audio_test_stream_is_streaming(test_stream)) { + WAIT_FOR_FLAG(test_stream->flag_audio_received); + } + } + printk("Data received\n"); +} + static void test_main(void) { int count = 0; @@ -391,9 +403,7 @@ static void test_main(void) break; } - /* Wait for data */ - printk("Waiting for data\n"); - WAIT_FOR_FLAG(flag_audio_received); + wait_for_data(); printk("Sending signal to broadcaster to stop\n"); backchannel_sync_send_all(); /* let the broadcast source know it can stop */