diff --git a/include/zephyr/bluetooth/audio/audio.h b/include/zephyr/bluetooth/audio/audio.h index a7c35e6a653..d5f81764c06 100644 --- a/include/zephyr/bluetooth/audio/audio.h +++ b/include/zephyr/bluetooth/audio/audio.h @@ -17,6 +17,7 @@ #include #include #include +#include #include /** @@ -2085,6 +2086,12 @@ struct bt_audio_broadcast_source_create_param { * controller may ignore. */ uint8_t packing; + + /** Whether or not to encrypt the streams. */ + bool encryption; + + /** @brief Broadcast code */ + uint8_t broadcast_code[BT_BAP_BROADCAST_CODE_SIZE]; }; /** @brief Create audio broadcast source. diff --git a/samples/bluetooth/broadcast_audio_source/src/main.c b/samples/bluetooth/broadcast_audio_source/src/main.c index 76448b495c4..784e3eca191 100644 --- a/samples/bluetooth/broadcast_audio_source/src/main.c +++ b/samples/bluetooth/broadcast_audio_source/src/main.c @@ -113,6 +113,7 @@ static int setup_broadcast_source(struct bt_audio_broadcast_source **source) create_param.params_count = ARRAY_SIZE(subgroup_param); create_param.params = subgroup_param; create_param.qos = &preset_16_2_1.qos; + create_param.encryption = false; printk("Creating broadcast source with %zu subgroups with %zu streams\n", ARRAY_SIZE(subgroup_param), diff --git a/subsys/bluetooth/audio/broadcast_source.c b/subsys/bluetooth/audio/broadcast_source.c index 1004674fa24..2ce0905f169 100644 --- a/subsys/bluetooth/audio/broadcast_source.c +++ b/subsys/bluetooth/audio/broadcast_source.c @@ -735,6 +735,12 @@ int bt_audio_broadcast_source_create(struct bt_audio_broadcast_source_create_par source->qos = qos; source->packing = param->packing; + source->encryption = param->encryption; + if (source->encryption) { + (void)memcpy(source->broadcast_code, param->broadcast_code, + sizeof(source->broadcast_code)); + } + LOG_DBG("Broadcasting with ID 0x%6X", source->broadcast_id); *out_source = source; @@ -883,6 +889,11 @@ int bt_audio_broadcast_source_start(struct bt_audio_broadcast_source *source, param.packing = source->packing; param.interval = source->qos->interval; param.latency = source->qos->latency; + param.encryption = source->encryption; + if (param.encryption) { + (void)memcpy(param.bcode, source->broadcast_code, + sizeof(param.bcode)); + } err = bt_iso_big_create(adv, ¶m, &source->big); if (err != 0) { diff --git a/subsys/bluetooth/audio/endpoint.h b/subsys/bluetooth/audio/endpoint.h index ab5ff86bce8..65b086dd474 100644 --- a/subsys/bluetooth/audio/endpoint.h +++ b/subsys/bluetooth/audio/endpoint.h @@ -7,6 +7,7 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include #include #include @@ -75,6 +76,7 @@ struct bt_audio_broadcast_stream_data { struct bt_audio_broadcast_source { uint8_t stream_count; uint8_t packing; + bool encryption; uint32_t broadcast_id; /* 24 bit */ struct bt_iso_big *big; @@ -83,6 +85,8 @@ struct bt_audio_broadcast_source { /* The codec specific configured data for each stream in the subgroup */ struct bt_audio_broadcast_stream_data stream_data[BROADCAST_STREAM_CNT]; + uint8_t broadcast_code[BT_BAP_BROADCAST_CODE_SIZE]; + /* The subgroups containing the streams used to create the broadcast source */ sys_slist_t subgroups; }; diff --git a/subsys/bluetooth/shell/audio.c b/subsys/bluetooth/shell/audio.c index 7154057d2cf..2de8c569969 100644 --- a/subsys/bluetooth/shell/audio.c +++ b/subsys/bluetooth/shell/audio.c @@ -1534,7 +1534,7 @@ static int cmd_create_broadcast(const struct shell *sh, size_t argc, struct bt_audio_broadcast_source_stream_param stream_params[ARRAY_SIZE(broadcast_source_streams)]; struct bt_audio_broadcast_source_subgroup_param subgroup_param; - struct bt_audio_broadcast_source_create_param create_param; + struct bt_audio_broadcast_source_create_param create_param = { 0 }; struct named_lc3_preset *named_preset; int err; @@ -1545,12 +1545,51 @@ static int cmd_create_broadcast(const struct shell *sh, size_t argc, named_preset = default_preset; - if (argc > 1) { - named_preset = set_preset(false, 1, &argv[1]); - if (named_preset == NULL) { - shell_error(sh, "Unable to parse named_preset %s", - argv[1]); - return -ENOEXEC; + for (size_t i = 1U; i < argc; i++) { + char *arg = argv[i]; + + if (strcmp(arg, "enc") == 0) { + if (argc > i) { + size_t bcode_len; + + i++; + arg = argv[i]; + + bcode_len = hex2bin(arg, strlen(arg), + create_param.broadcast_code, + sizeof(create_param.broadcast_code)); + + if (bcode_len != sizeof(create_param.broadcast_code)) { + shell_error(sh, "Invalid Broadcast Code Length: %zu", + bcode_len); + + return -ENOEXEC; + } + + create_param.encryption = true; + } else { + shell_help(sh); + + return SHELL_CMD_HELP_PRINTED; + } + } else if (strcmp(arg, "preset") == 0) { + if (argc > i) { + + i++; + arg = argv[i]; + + named_preset = set_preset(false, 1, &arg); + if (named_preset == NULL) { + shell_error(sh, "Unable to parse named_preset %s", + arg); + + return -ENOEXEC; + } + } else { + shell_help(sh); + + return SHELL_CMD_HELP_PRINTED; + } } } @@ -1955,7 +1994,8 @@ SHELL_STATIC_SUBCMD_SET_CREATE(audio_cmds, #if defined(CONFIG_BT_AUDIO_BROADCAST_SOURCE) SHELL_CMD_ARG(select_broadcast, NULL, "", cmd_select_broadcast_source, 2, 0), - SHELL_CMD_ARG(create_broadcast, NULL, "[codec] [preset]", + SHELL_CMD_ARG(create_broadcast, NULL, + "[preset ] [enc ]", cmd_create_broadcast, 1, 2), SHELL_CMD_ARG(start_broadcast, NULL, "", cmd_start_broadcast, 1, 0), SHELL_CMD_ARG(stop_broadcast, NULL, "", cmd_stop_broadcast, 1, 0), diff --git a/tests/bluetooth/bsim_bt/bsim_test_audio/src/broadcast_source_test.c b/tests/bluetooth/bsim_bt/bsim_test_audio/src/broadcast_source_test.c index e97cbfe521b..891be99af94 100644 --- a/tests/bluetooth/bsim_bt/bsim_test_audio/src/broadcast_source_test.c +++ b/tests/bluetooth/bsim_bt/bsim_test_audio/src/broadcast_source_test.c @@ -128,6 +128,7 @@ static int setup_broadcast_source(struct bt_audio_broadcast_source **source) create_param.params = subgroup_params; create_param.qos = &preset_16_2_2.qos; create_param.packing = BT_ISO_PACKING_SEQUENTIAL; + create_param.encryption = false; printk("Creating broadcast source with %zu subgroups and %zu streams\n", ARRAY_SIZE(subgroup_params), ARRAY_SIZE(stream_params));