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 87de852ff7d..5152d4f48fa 100644 --- a/tests/bsim/bluetooth/audio/src/bap_broadcast_sink_test.c +++ b/tests/bsim/bluetooth/audio/src/bap_broadcast_sink_test.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Nordic Semiconductor ASA + * Copyright (c) 2021-2024 Nordic Semiconductor ASA * * SPDX-License-Identifier: Apache-2.0 */ @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -46,6 +47,7 @@ CREATE_FLAG(flag_pa_sync_lost); CREATE_FLAG(flag_received); CREATE_FLAG(flag_pa_request); CREATE_FLAG(flag_bis_sync_requested); +CREATE_FLAG(flag_big_sync_mic_failure); static struct bt_bap_broadcast_sink *g_sink; static struct bt_le_scan_recv_info broadcaster_info; @@ -551,6 +553,10 @@ static void stopped_cb(struct bt_bap_stream *stream, uint8_t reason) { printk("Stream %p stopped with reason 0x%02X\n", stream, reason); k_sem_give(&sem_stopped); + + if (reason == BT_HCI_ERR_TERM_DUE_TO_MIC_FAIL) { + SET_FLAG(flag_big_sync_mic_failure); + } } static void recv_cb(struct bt_bap_stream *stream, @@ -745,23 +751,16 @@ static void test_broadcast_sink_create_inval(void) } } -static void test_broadcast_sync(bool encryption) +static void test_broadcast_sync(const uint8_t broadcast_code[BT_AUDIO_BROADCAST_CODE_SIZE]) { int err; printk("Syncing the sink\n"); - err = bt_bap_broadcast_sink_sync(g_sink, bis_index_bitfield, streams, - encryption ? BROADCAST_CODE : NULL); + err = bt_bap_broadcast_sink_sync(g_sink, bis_index_bitfield, streams, broadcast_code); if (err != 0) { FAIL("Unable to sync the sink: %d\n", err); return; } - - /* Wait for all to be started */ - printk("Waiting for streams to be started\n"); - for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) { - k_sem_take(&sem_started, K_FOREVER); - } } static void test_broadcast_sync_inval(void) @@ -932,7 +931,13 @@ static void test_common(void) WAIT_FOR_FLAG(flag_syncable); test_broadcast_sync_inval(); - test_broadcast_sync(false); + test_broadcast_sync(NULL); + + /* Wait for all to be started */ + printk("Waiting for streams to be started\n"); + for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) { + k_sem_take(&sem_started, K_FOREVER); + } printk("Waiting for data\n"); WAIT_FOR_FLAG(flag_received); @@ -974,7 +979,14 @@ static void test_sink_disconnect(void) test_broadcast_stop(); /* Retry sync*/ - test_broadcast_sync(false); + test_broadcast_sync(NULL); + + /* Wait for all to be started */ + printk("Waiting for streams to be started\n"); + for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) { + k_sem_take(&sem_started, K_FOREVER); + } + test_broadcast_stop(); test_broadcast_delete_inval(); @@ -1006,7 +1018,13 @@ static void test_sink_encrypted(void) printk("Waiting for BIG syncable\n"); WAIT_FOR_FLAG(flag_syncable); - test_broadcast_sync(true); + test_broadcast_sync(BROADCAST_CODE); + + /* Wait for all to be started */ + printk("Waiting for streams to be started\n"); + for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) { + k_sem_take(&sem_started, K_FOREVER); + } printk("Waiting for data\n"); WAIT_FOR_FLAG(flag_received); @@ -1027,7 +1045,38 @@ static void test_sink_encrypted(void) k_sem_take(&sem_stopped, K_FOREVER); } - PASS("Broadcast sink passed\n"); + PASS("Broadcast sink encrypted passed\n"); +} + +static void test_sink_encrypted_incorrect_code(void) +{ + int err; + + err = init(); + if (err) { + FAIL("Init failed (err %d)\n", err); + return; + } + + test_scan_and_pa_sync(); + + test_broadcast_sink_create(); + + printk("Broadcast source PA synced, waiting for BASE\n"); + WAIT_FOR_FLAG(flag_base_received); + printk("BASE received\n"); + + printk("Waiting for BIG syncable\n"); + WAIT_FOR_FLAG(flag_syncable); + + test_broadcast_sync(INCORRECT_BROADCAST_CODE); + /* Wait for MIC failure */ + WAIT_FOR_FLAG(flag_big_sync_mic_failure); + + backchannel_sync_send_all(); /* let other devices know we have received data */ + backchannel_sync_send_all(); /* let the broadcast source know it can stop */ + + PASS("Broadcast sink incorrect code passed\n"); } static void broadcast_sink_with_assistant(void) @@ -1058,7 +1107,13 @@ static void broadcast_sink_with_assistant(void) printk("Waiting for BIG sync request\n"); WAIT_FOR_FLAG(flag_bis_sync_requested); - test_broadcast_sync(false); + test_broadcast_sync(NULL); + + /* Wait for all to be started */ + printk("Waiting for streams to be started\n"); + for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) { + k_sem_take(&sem_started, K_FOREVER); + } printk("Waiting for data\n"); WAIT_FOR_FLAG(flag_received); @@ -1102,6 +1157,12 @@ static const struct bst_test_instance test_broadcast_sink[] = { .test_tick_f = test_tick, .test_main_f = test_sink_encrypted, }, + { + .test_id = "broadcast_sink_encrypted_incorrect_code", + .test_pre_init_f = test_init, + .test_tick_f = test_tick, + .test_main_f = test_sink_encrypted_incorrect_code, + }, { .test_id = "broadcast_sink_with_assistant", .test_pre_init_f = test_init, diff --git a/tests/bsim/bluetooth/audio/src/bap_common.h b/tests/bsim/bluetooth/audio/src/bap_common.h index b87525b652a..ccf11689c9f 100644 --- a/tests/bsim/bluetooth/audio/src/bap_common.h +++ b/tests/bsim/bluetooth/audio/src/bap_common.h @@ -40,7 +40,9 @@ #define BROADCAST_CODE \ ((uint8_t[]){0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, \ 0xdd, 0xee, 0xff}) - +#define INCORRECT_BROADCAST_CODE \ + ((uint8_t[]){0xDE, 0xAD, 0xBE, 0xEF, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, \ + 0xdd, 0xee, 0xff}) struct unicast_stream { struct bt_cap_stream stream; struct bt_audio_codec_cfg codec_cfg; diff --git a/tests/bsim/bluetooth/audio/test_scripts/bap_broadcast_audio_encrypted_incorrect_code.sh b/tests/bsim/bluetooth/audio/test_scripts/bap_broadcast_audio_encrypted_incorrect_code.sh new file mode 100755 index 00000000000..4c80b2be952 --- /dev/null +++ b/tests/bsim/bluetooth/audio/test_scripts/bap_broadcast_audio_encrypted_incorrect_code.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +# +# Copyright (c) 2021-2024 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: Apache-2.0 + +VERBOSITY_LEVEL=2 +EXECUTE_TIMEOUT=100 + +source ${ZEPHYR_BASE}/tests/bsim/sh_common.source + +cd ${BSIM_OUT_PATH}/bin + +printf "\n\n======== Broadcaster encrypted incorrect code test =========\n\n" + +SIMULATION_ID="broadcaster_encrypted_incorrect_code" + +Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_audio_prj_conf \ + -v=${VERBOSITY_LEVEL} -s=${SIMULATION_ID} -d=0 -testid=broadcast_source_encrypted \ + -RealEncryption=1 -rs=23 -D=2 + + +Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_audio_prj_conf \ + -v=${VERBOSITY_LEVEL} -s=${SIMULATION_ID} -d=1 -testid=broadcast_sink_encrypted_incorrect_code \ + -RealEncryption=1 -rs=27 -D=2 + +# Simulation time should be larger than the WAIT_TIME in common.h +Execute ./bs_2G4_phy_v1 -v=${VERBOSITY_LEVEL} -s=${SIMULATION_ID} \ + -D=2 -sim_length=60e6 $@ + +wait_for_background_jobs