tests: Bluetooth: Add encrypted BAP broadcast test

Added test to test encrypted broadcast

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
This commit is contained in:
Emil Gydesen 2024-07-04 11:39:14 +02:00 committed by Anas Nashif
commit 72d98692dc
4 changed files with 167 additions and 11 deletions

View file

@ -30,6 +30,7 @@
#include <zephyr/sys/util_macro.h>
#include <zephyr/toolchain.h>
#include "bap_common.h"
#include "bstests.h"
#include "common.h"
@ -766,12 +767,13 @@ static void test_broadcast_sink_create_inval(void)
}
}
static void test_broadcast_sync(void)
static void test_broadcast_sync(bool encryption)
{
int err;
printk("Syncing the sink\n");
err = bt_bap_broadcast_sink_sync(g_sink, bis_index_bitfield, streams, NULL);
err = bt_bap_broadcast_sink_sync(g_sink, bis_index_bitfield, streams,
encryption ? BROADCAST_CODE : NULL);
if (err != 0) {
FAIL("Unable to sync the sink: %d\n", err);
return;
@ -952,7 +954,7 @@ static void test_common(void)
WAIT_FOR_FLAG(flag_syncable);
test_broadcast_sync_inval();
test_broadcast_sync();
test_broadcast_sync(false);
printk("Waiting for data\n");
WAIT_FOR_FLAG(flag_received);
@ -994,7 +996,7 @@ static void test_sink_disconnect(void)
test_broadcast_stop();
/* Retry sync*/
test_broadcast_sync();
test_broadcast_sync(false);
test_broadcast_stop();
test_broadcast_delete_inval();
@ -1005,6 +1007,51 @@ static void test_sink_disconnect(void)
PASS("Broadcast sink disconnect passed\n");
}
static void test_sink_encrypted(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(true);
printk("Waiting for data\n");
WAIT_FOR_FLAG(flag_received);
backchannel_sync_send_all(); /* let other devices know we have received data */
backchannel_sync_send_all(); /* let the broadcast source know it can stop */
/* The order of PA sync lost and BIG Sync lost is irrelevant
* and depend on timeout parameters. We just wait for PA first, but
* either way will work.
*/
printk("Waiting for PA disconnected\n");
WAIT_FOR_FLAG(flag_pa_sync_lost);
printk("Waiting for streams to be stopped\n");
for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) {
k_sem_take(&sem_stopped, K_FOREVER);
}
PASS("Broadcast sink passed\n");
}
static void broadcast_sink_with_assistant(void)
{
int err;
@ -1033,7 +1080,7 @@ static void broadcast_sink_with_assistant(void)
printk("Waiting for BIG sync request\n");
WAIT_FOR_FLAG(flag_bis_sync_requested);
test_broadcast_sync();
test_broadcast_sync(false);
printk("Waiting for data\n");
WAIT_FOR_FLAG(flag_received);
@ -1071,6 +1118,12 @@ static const struct bst_test_instance test_broadcast_sink[] = {
.test_tick_f = test_tick,
.test_main_f = test_sink_disconnect,
},
{
.test_id = "broadcast_sink_encrypted",
.test_pre_init_f = test_init,
.test_tick_f = test_tick,
.test_main_f = test_sink_encrypted,
},
{
.test_id = "broadcast_sink_with_assistant",
.test_pre_init_f = test_init,

View file

@ -24,6 +24,7 @@
#include <zephyr/sys/util.h>
#include <zephyr/toolchain.h>
#include "bap_common.h"
#include "bstests.h"
#include "common.h"
@ -264,7 +265,7 @@ static struct bt_bap_stream_ops stream_ops = {
.sent = stream_sent_cb,
};
static int setup_broadcast_source(struct bt_bap_broadcast_source **source)
static int setup_broadcast_source(struct bt_bap_broadcast_source **source, bool encryption)
{
struct bt_bap_broadcast_source_stream_param
stream_params[ARRAY_SIZE(broadcast_source_streams)];
@ -297,7 +298,10 @@ static int setup_broadcast_source(struct bt_bap_broadcast_source **source)
create_param.params = subgroup_params;
create_param.qos = &preset_16_2_1.qos;
create_param.packing = BT_ISO_PACKING_SEQUENTIAL;
create_param.encryption = false;
create_param.encryption = encryption;
if (encryption) {
memcpy(create_param.broadcast_code, BROADCAST_CODE, sizeof(BROADCAST_CODE));
}
printk("Creating broadcast source with %zu subgroups and %zu streams\n",
ARRAY_SIZE(subgroup_params), ARRAY_SIZE(stream_params));
@ -577,7 +581,7 @@ static void test_main(void)
printk("Bluetooth initialized\n");
err = setup_broadcast_source(&source);
err = setup_broadcast_source(&source, false);
if (err != 0) {
FAIL("Unable to setup broadcast source: %d\n", err);
return;
@ -630,7 +634,7 @@ static void test_main(void)
/* Recreate broadcast source to verify that it's possible */
printk("Recreating broadcast source\n");
err = setup_broadcast_source(&source);
err = setup_broadcast_source(&source, false);
if (err != 0) {
FAIL("Unable to setup broadcast source: %d\n", err);
return;
@ -643,14 +647,80 @@ static void test_main(void)
PASS("Broadcast source passed\n");
}
static void test_main_encrypted(void)
{
struct bt_bap_broadcast_source *source;
struct bt_le_ext_adv *adv;
int err;
err = bt_enable(NULL);
if (err) {
FAIL("Bluetooth init failed (err %d)\n", err);
return;
}
printk("Bluetooth initialized\n");
err = setup_broadcast_source(&source, true);
if (err != 0) {
FAIL("Unable to setup broadcast source: %d\n", err);
return;
}
err = setup_extended_adv(source, &adv);
if (err != 0) {
FAIL("Failed to setup extended advertising: %d\n", err);
return;
}
test_broadcast_source_start(source, adv);
/* Initialize sending */
printk("Sending data\n");
for (size_t i = 0U; i < ARRAY_SIZE(broadcast_source_streams); i++) {
for (unsigned int j = 0U; j < BROADCAST_ENQUEUE_COUNT; j++) {
struct audio_test_stream *test_stream = &broadcast_source_streams[i];
test_stream->tx_active = true;
stream_sent_cb(&test_stream->stream.bap_stream);
}
}
/* Wait for other devices to have received data */
backchannel_sync_wait_any();
/* Wait for other devices to let us know when we can stop the source */
backchannel_sync_wait_any();
test_broadcast_source_stop(source);
test_broadcast_source_delete(source);
source = NULL;
err = stop_extended_adv(adv);
if (err != 0) {
FAIL("Unable to stop extended advertising: %d\n", err);
return;
}
adv = NULL;
PASS("Broadcast source encrypted passed\n");
}
static const struct bst_test_instance test_broadcast_source[] = {
{
.test_id = "broadcast_source",
.test_pre_init_f = test_init,
.test_tick_f = test_tick,
.test_main_f = test_main
.test_main_f = test_main,
},
BSTEST_END_MARKER
{
.test_id = "broadcast_source_encrypted",
.test_pre_init_f = test_init,
.test_tick_f = test_tick,
.test_main_f = test_main_encrypted,
},
BSTEST_END_MARKER,
};
struct bst_test_list *test_broadcast_source_install(struct bst_test_list *tests)

View file

@ -37,6 +37,10 @@
#define LONG_META_LEN (sizeof((uint8_t []){LONG_META}) + 1U) /* Size of data + type */
#define BROADCAST_CODE \
((uint8_t[]){0x00, 0x11, 0x22, 0x33, 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;

View file

@ -0,0 +1,29 @@
#!/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 test =========\n\n"
SIMULATION_ID="broadcaster_encrypted"
Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_audio_prj_conf \
-v=${VERBOSITY_LEVEL} -s=${SIMULATION_ID} -d=0 -testid=broadcast_source_encrypted -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 -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