From 4844dabf3abf531855f1ea2ec948686dc4c3b39f Mon Sep 17 00:00:00 2001 From: Emil Gydesen Date: Mon, 25 Mar 2024 18:58:12 +0100 Subject: [PATCH] Samples: Bluetooth: Add encrypted broadcast audio source Add support for supplying a broadcast code to the broadcast audio sample, which will, if non-empty, encrypt the broadcast. Signed-off-by: Emil Gydesen --- samples/bluetooth/broadcast_audio_source/Kconfig | 7 +++++++ samples/bluetooth/broadcast_audio_source/src/main.c | 12 ++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/samples/bluetooth/broadcast_audio_source/Kconfig b/samples/bluetooth/broadcast_audio_source/Kconfig index 683eeaf9dbf..0189da81b69 100644 --- a/samples/bluetooth/broadcast_audio_source/Kconfig +++ b/samples/bluetooth/broadcast_audio_source/Kconfig @@ -38,4 +38,11 @@ config USE_USB_AUDIO_INPUT select USB_DEVICE_AUDIO select RING_BUFFER +config BROADCAST_CODE + string "The broadcast code (if any) to use for encrypted broadcast" + default "" + help + Setting a non-empty string for this option will encrypt the broadcast using this + string as the broadcast code. The length of the string shall be between 1 and 16 octets. + source "Kconfig.zephyr" diff --git a/samples/bluetooth/broadcast_audio_source/src/main.c b/samples/bluetooth/broadcast_audio_source/src/main.c index 80def0ee960..c63a1337992 100644 --- a/samples/bluetooth/broadcast_audio_source/src/main.c +++ b/samples/bluetooth/broadcast_audio_source/src/main.c @@ -9,6 +9,9 @@ #include #include +BUILD_ASSERT(strlen(CONFIG_BROADCAST_CODE) <= BT_AUDIO_BROADCAST_CODE_SIZE, + "Invalid broadcast code"); + /* Zephyr Controller works best while Extended Advertising interval to be a multiple * of the ISO Interval minus 10 ms (max. advertising random delay). This is * required to place the AUX_ADV_IND PDUs in a non-overlapping interval with the @@ -384,7 +387,7 @@ static int setup_broadcast_source(struct bt_bap_broadcast_source **source) stream_params[CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT]; struct bt_bap_broadcast_source_subgroup_param subgroup_param[CONFIG_BT_BAP_BROADCAST_SRC_SUBGROUP_COUNT]; - struct bt_bap_broadcast_source_param create_param; + struct bt_bap_broadcast_source_param create_param = {0}; const size_t streams_per_subgroup = ARRAY_SIZE(stream_params) / ARRAY_SIZE(subgroup_param); uint8_t left[] = {BT_AUDIO_CODEC_DATA(BT_AUDIO_CODEC_CFG_CHAN_ALLOC, BT_BYTES_LIST_LE32(BT_AUDIO_LOCATION_FRONT_LEFT))}; @@ -408,9 +411,14 @@ static int setup_broadcast_source(struct bt_bap_broadcast_source **source) create_param.params_count = ARRAY_SIZE(subgroup_param); create_param.params = subgroup_param; create_param.qos = &preset_active.qos; - create_param.encryption = false; + create_param.encryption = strlen(CONFIG_BROADCAST_CODE) > 0; create_param.packing = BT_ISO_PACKING_SEQUENTIAL; + if (create_param.encryption) { + memcpy(create_param.broadcast_code, CONFIG_BROADCAST_CODE, + strlen(CONFIG_BROADCAST_CODE)); + } + printk("Creating broadcast source with %zu subgroups with %zu streams\n", ARRAY_SIZE(subgroup_param), ARRAY_SIZE(subgroup_param) * streams_per_subgroup);