Skip to content

Commit 34fa014

Browse files
Thalleyfabiobaltieri
authored andcommitted
tests: Bluetooth: Add test for incorrect bcode as assistant
Add a bsim test that verifies the behavior when the assistant provides an incorrect code to the broadcast sink. Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
1 parent c49a058 commit 34fa014

File tree

3 files changed

+141
-17
lines changed

3 files changed

+141
-17
lines changed

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

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <zephyr/bluetooth/gap.h>
1717
#include <zephyr/bluetooth/gatt.h>
1818
#include <zephyr/bluetooth/hci.h>
19+
#include <zephyr/bluetooth/iso.h>
1920
#include <zephyr/bluetooth/uuid.h>
2021
#include <zephyr/net/buf.h>
2122
#include <zephyr/sys/printk.h>
@@ -47,6 +48,8 @@ CREATE_FLAG(flag_recv_state_read);
4748
CREATE_FLAG(flag_recv_state_updated);
4849
CREATE_FLAG(flag_recv_state_updated_with_bis_sync);
4950
CREATE_FLAG(flag_recv_state_removed);
51+
CREATE_FLAG(flag_broadcast_code_requested);
52+
CREATE_FLAG(flag_incorrect_broadcast_code);
5053

5154
/* Broadcaster variables */
5255
static bt_addr_le_t g_broadcaster_addr;
@@ -98,7 +101,7 @@ static void bap_broadcast_assistant_scan_cb(const struct bt_le_scan_recv_info *i
98101

99102
static bool metadata_entry(struct bt_data *data, void *user_data)
100103
{
101-
char metadata[CONFIG_BT_AUDIO_CODEC_CFG_MAX_METADATA_SIZE];
104+
char metadata[CONFIG_BT_AUDIO_CODEC_CFG_MAX_METADATA_SIZE] = {0};
102105

103106
(void)bin2hex(data->data, data->data_len, metadata, sizeof(metadata));
104107

@@ -130,14 +133,15 @@ static void bap_broadcast_assistant_recv_state_cb(
130133
bt_addr_le_to_str(&state->addr, le_addr, sizeof(le_addr));
131134
(void)bin2hex(state->bad_code, BT_AUDIO_BROADCAST_CODE_SIZE, bad_code,
132135
sizeof(bad_code));
133-
printk("BASS recv state: src_id %u, addr %s, sid %u, sync_state %u, "
134-
"encrypt_state %u%s%s\n", state->src_id, le_addr, state->adv_sid,
135-
state->pa_sync_state, state->encrypt_state,
136-
state->encrypt_state == BT_BAP_BIG_ENC_STATE_BAD_CODE ? ", bad code" : "",
136+
printk("BASS recv state: src_id %u, addr %s, sid %u, sync_state %u, encrypt_state %u%s%s\n",
137+
state->src_id, le_addr, state->adv_sid, state->pa_sync_state, state->encrypt_state,
138+
state->encrypt_state == BT_BAP_BIG_ENC_STATE_BAD_CODE ? ", bad code: " : "",
137139
bad_code);
138140

139-
if (state->encrypt_state == BT_BAP_BIG_ENC_STATE_BAD_CODE) {
140-
FAIL("Encryption state is BT_BAP_BIG_ENC_STATE_BAD_CODE");
141+
if (state->encrypt_state == BT_BAP_BIG_ENC_STATE_BCODE_REQ) {
142+
SET_FLAG(flag_broadcast_code_requested);
143+
} else if (state->encrypt_state == BT_BAP_BIG_ENC_STATE_BAD_CODE) {
144+
SET_FLAG(flag_incorrect_broadcast_code);
141145
return;
142146
}
143147

@@ -499,15 +503,10 @@ static void test_bass_mod_source_long_meta(void)
499503
printk("Server PA synced\n");
500504
}
501505

502-
static void test_bass_broadcast_code(void)
506+
static void test_bass_broadcast_code(const uint8_t broadcast_code[BT_AUDIO_BROADCAST_CODE_SIZE])
503507
{
504-
uint8_t broadcast_code[BT_AUDIO_BROADCAST_CODE_SIZE];
505508
int err;
506509

507-
for (int i = 0; i < ARRAY_SIZE(broadcast_code); i++) {
508-
broadcast_code[i] = i;
509-
}
510-
511510
printk("Adding broadcast code\n");
512511
UNSET_FLAG(flag_write_complete);
513512
err = bt_bap_broadcast_assistant_set_broadcast_code(default_conn, g_src_id, broadcast_code);
@@ -588,7 +587,7 @@ static void test_main_client_sync(void)
588587
test_bass_add_source();
589588
test_bass_mod_source();
590589
test_bass_mod_source_long_meta();
591-
test_bass_broadcast_code();
590+
test_bass_broadcast_code(BROADCAST_CODE);
592591

593592
printk("Waiting for receive state with BIS sync\n");
594593
WAIT_FOR_FLAG(flag_recv_state_updated_with_bis_sync);
@@ -598,6 +597,30 @@ static void test_main_client_sync(void)
598597
PASS("BAP Broadcast Assistant Client Sync Passed\n");
599598
}
600599

600+
static void test_main_client_sync_incorrect_code(void)
601+
{
602+
int err;
603+
604+
err = common_init();
605+
if (err != 0) {
606+
FAIL("Bluetooth enable failed (err %d)\n", err);
607+
return;
608+
}
609+
610+
test_bass_scan_start();
611+
test_bass_scan_stop();
612+
test_bass_create_pa_sync();
613+
test_bass_add_source();
614+
test_bass_mod_source();
615+
WAIT_FOR_FLAG(flag_broadcast_code_requested);
616+
test_bass_broadcast_code(INCORRECT_BROADCAST_CODE);
617+
WAIT_FOR_FLAG(flag_incorrect_broadcast_code);
618+
619+
test_bass_remove_source();
620+
621+
PASS("BAP Broadcast Assistant Client Sync Passed\n");
622+
}
623+
601624
static void test_main_server_sync_client_rem(void)
602625
{
603626
int err;
@@ -610,7 +633,7 @@ static void test_main_server_sync_client_rem(void)
610633

611634
WAIT_FOR_FLAG(flag_recv_state_updated);
612635

613-
test_bass_broadcast_code();
636+
test_bass_broadcast_code(BROADCAST_CODE);
614637

615638
printk("Waiting for receive state with BIS sync\n");
616639
WAIT_FOR_FLAG(flag_recv_state_updated_with_bis_sync);
@@ -632,7 +655,7 @@ static void test_main_server_sync_server_rem(void)
632655

633656
WAIT_FOR_FLAG(flag_recv_state_updated);
634657

635-
test_bass_broadcast_code();
658+
test_bass_broadcast_code(BROADCAST_CODE);
636659

637660
printk("Waiting for receive state with BIS sync\n");
638661
WAIT_FOR_FLAG(flag_recv_state_updated_with_bis_sync);
@@ -649,6 +672,12 @@ static const struct bst_test_instance test_bass[] = {
649672
.test_tick_f = test_tick,
650673
.test_main_f = test_main_client_sync,
651674
},
675+
{
676+
.test_id = "bap_broadcast_assistant_client_sync_incorrect_code",
677+
.test_pre_init_f = test_init,
678+
.test_tick_f = test_tick,
679+
.test_main_f = test_main_client_sync_incorrect_code,
680+
},
652681
{
653682
.test_id = "bap_broadcast_assistant_server_sync_client_rem",
654683
.test_pre_init_f = test_init,
@@ -661,7 +690,7 @@ static const struct bst_test_instance test_bass[] = {
661690
.test_tick_f = test_tick,
662691
.test_main_f = test_main_server_sync_server_rem,
663692
},
664-
BSTEST_END_MARKER
693+
BSTEST_END_MARKER,
665694
};
666695

667696
struct bst_test_list *test_bap_broadcast_assistant_install(struct bst_test_list *tests)

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ static struct bt_bap_stream *streams[ARRAY_SIZE(broadcast_sink_streams)];
5959
static uint32_t requested_bis_sync;
6060
static struct bt_le_ext_adv *ext_adv;
6161
static const struct bt_bap_scan_delegator_recv_state *req_recv_state;
62+
static uint8_t recv_state_broadcast_code[BT_AUDIO_BROADCAST_CODE_SIZE];
6263

6364
#define SUPPORTED_CHAN_COUNTS BT_AUDIO_CODEC_CAP_CHAN_COUNT_SUPPORT(1, 2)
6465
#define SUPPORTED_MIN_OCTETS_PER_FRAME 30
@@ -385,6 +386,8 @@ static int bis_sync_req_cb(struct bt_conn *conn,
385386
const struct bt_bap_scan_delegator_recv_state *recv_state,
386387
const uint32_t bis_sync_req[CONFIG_BT_BAP_BASS_MAX_SUBGROUPS])
387388
{
389+
req_recv_state = recv_state;
390+
388391
printk("BIS sync request received for %p: 0x%08x\n", recv_state, bis_sync_req[0]);
389392
/* We only care about a single subgroup in this test */
390393
requested_bis_sync = bis_sync_req[0];
@@ -398,10 +401,20 @@ static int bis_sync_req_cb(struct bt_conn *conn,
398401
return 0;
399402
}
400403

404+
static void broadcast_code_cb(struct bt_conn *conn,
405+
const struct bt_bap_scan_delegator_recv_state *recv_state,
406+
const uint8_t broadcast_code[BT_AUDIO_BROADCAST_CODE_SIZE])
407+
{
408+
req_recv_state = recv_state;
409+
410+
memcpy(recv_state_broadcast_code, broadcast_code, BT_AUDIO_BROADCAST_CODE_SIZE);
411+
}
412+
401413
static struct bt_bap_scan_delegator_cb scan_delegator_cbs = {
402414
.pa_sync_req = pa_sync_req_cb,
403415
.pa_sync_term_req = pa_sync_term_req_cb,
404416
.bis_sync_req = bis_sync_req_cb,
417+
.broadcast_code = broadcast_code_cb,
405418
};
406419

407420
static void validate_stream_codec_cfg(const struct bt_bap_stream *stream)
@@ -1138,6 +1151,50 @@ static void broadcast_sink_with_assistant(void)
11381151
PASS("Broadcast sink with assistant passed\n");
11391152
}
11401153

1154+
static void broadcast_sink_with_assistant_incorrect_code(void)
1155+
{
1156+
int err;
1157+
1158+
err = init();
1159+
if (err) {
1160+
FAIL("Init failed (err %d)\n", err);
1161+
return;
1162+
}
1163+
1164+
test_start_adv();
1165+
WAIT_FOR_FLAG(flag_connected);
1166+
1167+
printk("Waiting for PA sync request\n");
1168+
WAIT_FOR_FLAG(flag_pa_request);
1169+
1170+
test_scan_and_pa_sync();
1171+
test_broadcast_sink_create();
1172+
1173+
printk("Broadcast source PA synced, waiting for BASE\n");
1174+
WAIT_FOR_FLAG(flag_base_received);
1175+
printk("BASE received\n");
1176+
1177+
printk("Waiting for BIG syncable\n");
1178+
WAIT_FOR_FLAG(flag_syncable);
1179+
1180+
printk("Waiting for BIG sync request\n");
1181+
WAIT_FOR_FLAG(flag_bis_sync_requested);
1182+
test_broadcast_sync(recv_state_broadcast_code);
1183+
/* Wait for MIC failure */
1184+
WAIT_FOR_FLAG(flag_big_sync_mic_failure);
1185+
1186+
backchannel_sync_send_all(); /* let other devices know we have received data */
1187+
1188+
printk("Waiting for PA sync terminate request\n");
1189+
WAIT_FOR_UNSET_FLAG(flag_pa_request);
1190+
test_pa_sync_delete();
1191+
test_broadcast_delete();
1192+
1193+
backchannel_sync_send_all(); /* let the broadcast source know it can stop */
1194+
1195+
PASS("Broadcast sink with assistant and incorrect code passed\n");
1196+
}
1197+
11411198
static const struct bst_test_instance test_broadcast_sink[] = {
11421199
{
11431200
.test_id = "broadcast_sink",
@@ -1169,6 +1226,12 @@ static const struct bst_test_instance test_broadcast_sink[] = {
11691226
.test_tick_f = test_tick,
11701227
.test_main_f = broadcast_sink_with_assistant,
11711228
},
1229+
{
1230+
.test_id = "broadcast_sink_with_assistant_incorrect_code",
1231+
.test_pre_init_f = test_init,
1232+
.test_tick_f = test_tick,
1233+
.test_main_f = broadcast_sink_with_assistant_incorrect_code,
1234+
},
11721235
BSTEST_END_MARKER,
11731236
};
11741237

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Copyright (c) 2020-2022 Nordic Semiconductor ASA
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
SIMULATION_ID="bap_broadcast_audio_assistant_incorrect_code"
8+
VERBOSITY_LEVEL=2
9+
10+
source ${ZEPHYR_BASE}/tests/bsim/sh_common.source
11+
12+
cd ${BSIM_OUT_PATH}/bin
13+
14+
printf "\n\n======== Running BAP Broadcast audio assistant incorrect code =========\n\n"
15+
16+
Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_audio_prj_conf \
17+
-v=${VERBOSITY_LEVEL} -s=${SIMULATION_ID} -d=0 -RealEncryption=1 \
18+
-testid=broadcast_sink_with_assistant_incorrect_code -rs=24 -D=3
19+
20+
Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_audio_prj_conf \
21+
-v=${VERBOSITY_LEVEL} -s=${SIMULATION_ID} -d=1 -RealEncryption=1 \
22+
-testid=bap_broadcast_assistant_client_sync_incorrect_code -rs=46 -D=3
23+
24+
Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_audio_prj_conf \
25+
-v=${VERBOSITY_LEVEL} -s=${SIMULATION_ID} -d=2 -RealEncryption=1 \
26+
-testid=broadcast_source_encrypted -rs=69 -D=3
27+
28+
# Simulation time should be larger than the WAIT_TIME in common.h
29+
Execute ./bs_2G4_phy_v1 -v=${VERBOSITY_LEVEL} -s=${SIMULATION_ID} -D=3 \
30+
-sim_length=60e6 $@
31+
32+
wait_for_background_jobs

0 commit comments

Comments
 (0)