Skip to content

Commit

Permalink
Bluetooth: audio: Add possibility to use static broadcast id
Browse files Browse the repository at this point in the history
Removed the generation of broadcast id inside the stack. It is now up
to the application to generate this by itself. The CAP sample has
been modified to allow either a static broadcast, or a random one.
All of this is handled in the application.

Signed-off-by: Fredrik Danebjer <[email protected]>
  • Loading branch information
fredrikdanebjer authored and aescolar committed Oct 25, 2024
1 parent 7e72d46 commit f970b06
Show file tree
Hide file tree
Showing 25 changed files with 112 additions and 232 deletions.
7 changes: 7 additions & 0 deletions doc/releases/migration-guide-4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,13 @@ Bluetooth Audio
do a search-and-replace for ``bt_audio_codec_qos`` to ``bt_bap_qos_cfg`` and
``BT_AUDIO_CODEC_QOS`` to ``BT_BAP_QOS_CFG``. (:github:`76633`)

* The generation of broadcast ID inside of zephyr stack has been removed, it is now up the
application to generate a broadcast ID. This means that the application can now fully decide
whether to use a static or random broadcast ID. Reusing and statically defining a broadcast ID was
added to the Basic Audio Profile in version 1.0.2, which is the basis for this change. All
instances of :c:func:`bt_cap_initiator_broadcast_get_id` and
:c:func:`bt_bap_broadcast_source_get_id` has been removed(:github:`80228`)

Bluetooth Classic
=================

Expand Down
16 changes: 0 additions & 16 deletions include/zephyr/bluetooth/audio/bap.h
Original file line number Diff line number Diff line change
Expand Up @@ -2184,22 +2184,6 @@ int bt_bap_broadcast_source_stop(struct bt_bap_broadcast_source *source);
*/
int bt_bap_broadcast_source_delete(struct bt_bap_broadcast_source *source);

/**
* @brief Get the broadcast ID of a broadcast source
*
* This will return the 3-octet broadcast ID that should be advertised in the
* extended advertising data with @ref BT_UUID_BROADCAST_AUDIO_VAL as @ref BT_DATA_SVC_DATA16.
*
* See table 3.14 in the Basic Audio Profile v1.0.1 for the structure.
*
* @param[in] source Pointer to the broadcast source.
* @param[out] broadcast_id Pointer to the 3-octet broadcast ID.
*
* @return Zero on success or (negative) error code otherwise.
*/
int bt_bap_broadcast_source_get_id(struct bt_bap_broadcast_source *source,
uint32_t *const broadcast_id);

/**
* @brief Get the Broadcast Audio Stream Endpoint of a broadcast source
*
Expand Down
17 changes: 0 additions & 17 deletions include/zephyr/bluetooth/audio/cap.h
Original file line number Diff line number Diff line change
Expand Up @@ -603,23 +603,6 @@ int bt_cap_initiator_broadcast_audio_stop(struct bt_cap_broadcast_source *broadc
*/
int bt_cap_initiator_broadcast_audio_delete(struct bt_cap_broadcast_source *broadcast_source);

/**
* @brief Get the broadcast ID of a Common Audio Profile broadcast source
*
* This will return the 3-octet broadcast ID that should be advertised in the
* extended advertising data with @ref BT_UUID_BROADCAST_AUDIO_VAL as
* @ref BT_DATA_SVC_DATA16.
*
* See table 3.14 in the Basic Audio Profile v1.0.1 for the structure.
*
* @param[in] broadcast_source Pointer to the broadcast source.
* @param[out] broadcast_id Pointer to the 3-octet broadcast ID.
*
* @return int 0 if on success, errno on error.
*/
int bt_cap_initiator_broadcast_get_id(const struct bt_cap_broadcast_source *broadcast_source,
uint32_t *const broadcast_id);

/**
* @brief Get the Broadcast Audio Stream Endpoint of a Common Audio Profile broadcast source
*
Expand Down
14 changes: 14 additions & 0 deletions samples/bluetooth/bap_broadcast_source/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,18 @@ config BROADCAST_CODE
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.

config STATIC_BROADCAST_ID
bool "Use static broadcast ID"
default y
help
Enabling this option will make the application use static broadcast ID, as opposed to a
randomly generated one.

config BROADCAST_ID
hex "The static broadcast ID to use"
range 0x000000 0xFFFFFF
depends on STATIC_BROADCAST_ID
help
This is the 3-octet broadcast ID advertised if static broadcast IDs are enabled.

source "Kconfig.zephyr"
1 change: 1 addition & 0 deletions samples/bluetooth/bap_broadcast_source/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ CONFIG_BT_ISO_TX_BUF_COUNT=6
CONFIG_BT_ISO_TX_MTU=60

CONFIG_BT_DEVICE_NAME="Broadcast Audio Source"
CONFIG_BROADCAST_ID=0x123456
12 changes: 8 additions & 4 deletions samples/bluetooth/bap_broadcast_source/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,11 +518,15 @@ int main(void)
return 0;
}

err = bt_bap_broadcast_source_get_id(broadcast_source, &broadcast_id);
if (err != 0) {
printk("Unable to get broadcast ID: %d\n", err);
return 0;
#if defined(CONFIG_STATIC_BROADCAST_ID)
broadcast_id = CONFIG_BROADCAST_ID;
#else
err = bt_rand(&broadcast_id, BT_AUDIO_BROADCAST_ID_SIZE);
if (err) {
printk("Unable to generate broadcast ID: %d\n", err);
return err;
}
#endif /* CONFIG_STATIC_BROADCAST_ID */

/* Setup extended advertising data */
net_buf_simple_add_le16(&ad_buf, BT_UUID_BROADCAST_AUDIO_VAL);
Expand Down
14 changes: 14 additions & 0 deletions samples/bluetooth/cap_initiator/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,18 @@ config SAMPLE_BROADCAST
help
If set to true, the sample will start advertising syncable audio streams

config STATIC_BROADCAST_ID
bool "Use static broadcast ID"
default y
help
Enabling this option will make the application use static broadcast ID, as opposed to a
randomly generated one.

config BROADCAST_ID
hex "The static broadcast ID to use"
range 0x000000 0xFFFFFF
depends on STATIC_BROADCAST_ID
help
This is the 3-octet broadcast ID advertised if static broadcast IDs are enabled.

source "Kconfig.zephyr"
1 change: 1 addition & 0 deletions samples/bluetooth/cap_initiator/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ CONFIG_BT_BAP_BROADCAST_SOURCE=y
# Broadcast sources values if enabled by CONFIG_SAMPLE_BROADCAST
CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT=2
CONFIG_BT_BAP_BROADCAST_SRC_SUBGROUP_COUNT=1
CONFIG_BROADCAST_ID=0x123456
10 changes: 7 additions & 3 deletions samples/bluetooth/cap_initiator/src/cap_initiator_broadcast.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,15 @@ static int setup_extended_adv_data(struct bt_cap_broadcast_source *source,
uint32_t broadcast_id;
int err;

err = bt_cap_initiator_broadcast_get_id(source, &broadcast_id);
if (err != 0) {
LOG_ERR("Unable to get broadcast ID: %d", err);
#if defined(CONFIG_STATIC_BROADCAST_ID)
broadcast_id = CONFIG_BROADCAST_ID;
#else
err = bt_rand(&broadcast_id, BT_AUDIO_BROADCAST_ID_SIZE);
if (err) {
printk("Unable to generate broadcast ID: %d\n", err);
return err;
}
#endif /* CONFIG_STATIC_BROADCAST_ID */

/* Setup extended advertising data */
net_buf_simple_add_le16(&ad_buf, BT_UUID_BROADCAST_AUDIO_VAL);
Expand Down
20 changes: 20 additions & 0 deletions samples/bluetooth/pbp_public_broadcast_source/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (c) 2024 Demant A/S
# SPDX-License-Identifier: Apache-2.0

mainmenu "Bluetooth: PBP Broadcast Audio Source"

config STATIC_BROADCAST_ID
bool "Use static broadcast ID"
default y
help
Enabling this option will make the application use static broadcast ID, as opposed to a
randomly generated one.

config BROADCAST_ID
hex "The static broadcast ID to use"
range 0x000000 0xFFFFFF
depends on STATIC_BROADCAST_ID
help
This is the 3-octet broadcast ID advertised if static broadcast IDs are enabled.

source "Kconfig.zephyr"
1 change: 1 addition & 0 deletions samples/bluetooth/pbp_public_broadcast_source/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ CONFIG_BT_BAP_BROADCAST_SRC_SUBGROUP_COUNT=1

CONFIG_BT_EXT_ADV=y
CONFIG_BT_DEVICE_NAME="PBS"
CONFIG_STATIC_BROADCAST_ID=n
11 changes: 7 additions & 4 deletions samples/bluetooth/pbp_public_broadcast_source/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,15 @@ static int setup_extended_adv_data(struct bt_cap_broadcast_source *source,
uint32_t broadcast_id;
int err;

err = bt_cap_initiator_broadcast_get_id(source, &broadcast_id);
if (err != 0) {
printk("Unable to get broadcast ID: %d\n", err);

#if defined(CONFIG_STATIC_BROADCAST_ID)
broadcast_id = CONFIG_BROADCAST_ID;
#else
err = bt_rand(&broadcast_id, BT_AUDIO_BROADCAST_ID_SIZE);
if (err) {
printk("Unable to generate broadcast ID: %d\n", err);
return err;
}
#endif /* CONFIG_STATIC_BROADCAST_ID */

/* Setup extended advertising data */
ext_ad[0].type = BT_DATA_GAP_APPEARANCE;
Expand Down
6 changes: 3 additions & 3 deletions samples/bluetooth/tmap_bms/src/cap_initiator.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ static int setup_extended_adv_data(struct bt_cap_broadcast_source *source,
uint32_t broadcast_id;
int err;

err = bt_cap_initiator_broadcast_get_id(source, &broadcast_id);
if (err != 0) {
printk("Unable to get broadcast ID: %d\n", err);
err = bt_rand(&broadcast_id, BT_AUDIO_BROADCAST_ID_SIZE);
if (err) {
printk("Unable to generate broadcast ID: %d\n", err);
return err;
}

Expand Down
64 changes: 0 additions & 64 deletions subsys/bluetooth/audio/bap_broadcast_source.c
Original file line number Diff line number Diff line change
Expand Up @@ -450,36 +450,6 @@ static bool encode_base(struct bt_bap_broadcast_source *source, struct net_buf_s
return true;
}

static int generate_broadcast_id(struct bt_bap_broadcast_source *source)
{
bool unique;

do {
int err;

err = bt_rand(&source->broadcast_id,
BT_AUDIO_BROADCAST_ID_SIZE);
if (err) {
return err;
}

/* Ensure uniqueness */
unique = true;
for (int i = 0; i < ARRAY_SIZE(broadcast_sources); i++) {
if (&broadcast_sources[i] == source) {
continue;
}

if (broadcast_sources[i].broadcast_id == source->broadcast_id) {
unique = false;
break;
}
}
} while (!unique);

return 0;
}

static void broadcast_source_cleanup(struct bt_bap_broadcast_source *source)
{
struct bt_bap_broadcast_subgroup *subgroup, *next_subgroup;
Expand Down Expand Up @@ -830,12 +800,6 @@ int bt_bap_broadcast_source_create(struct bt_bap_broadcast_source_param *param,
}
}

err = generate_broadcast_id(source);
if (err != 0) {
LOG_DBG("Could not generate broadcast id: %d", err);
return err;
}

/* Finalize state changes and store information */
broadcast_source_set_state(source, BT_BAP_EP_STATE_QOS_CONFIGURED);
source->qos = qos;
Expand All @@ -852,8 +816,6 @@ int bt_bap_broadcast_source_create(struct bt_bap_broadcast_source_param *param,
sizeof(source->broadcast_code));
}

LOG_DBG("Broadcasting with ID 0x%6X", source->broadcast_id);

*out_source = source;

return 0;
Expand Down Expand Up @@ -1197,32 +1159,6 @@ int bt_bap_broadcast_source_delete(struct bt_bap_broadcast_source *source)
return 0;
}

int bt_bap_broadcast_source_get_id(struct bt_bap_broadcast_source *source,
uint32_t *const broadcast_id)
{
enum bt_bap_ep_state broadcast_state;

CHECKIF(source == NULL) {
LOG_DBG("source is NULL");
return -EINVAL;
}

CHECKIF(broadcast_id == NULL) {
LOG_DBG("broadcast_id is NULL");
return -EINVAL;
}

broadcast_state = broadcast_source_get_state(source);
if (broadcast_state == BT_BAP_EP_STATE_IDLE) {
LOG_DBG("Broadcast source invalid state: %u", broadcast_state);
return -EBADMSG;
}

*broadcast_id = source->broadcast_id;

return 0;
}

int bt_bap_broadcast_source_get_base(struct bt_bap_broadcast_source *source,
struct net_buf_simple *base_buf)
{
Expand Down
1 change: 0 additions & 1 deletion subsys/bluetooth/audio/bap_endpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ struct bt_bap_broadcast_source {
uint8_t stream_count;
uint8_t packing;
bool encryption;
uint32_t broadcast_id; /* 24 bit */

struct bt_iso_big *big;
struct bt_bap_qos_cfg *qos;
Expand Down
11 changes: 0 additions & 11 deletions subsys/bluetooth/audio/cap_initiator.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,17 +322,6 @@ int bt_cap_initiator_broadcast_audio_delete(struct bt_cap_broadcast_source *broa
return err;
}

int bt_cap_initiator_broadcast_get_id(const struct bt_cap_broadcast_source *broadcast_source,
uint32_t *const broadcast_id)
{
CHECKIF(broadcast_source == NULL) {
LOG_DBG("broadcast_source is NULL");
return -EINVAL;
}

return bt_bap_broadcast_source_get_id(broadcast_source->bap_broadcast, broadcast_id);
}

int bt_cap_initiator_broadcast_get_base(struct bt_cap_broadcast_source *broadcast_source,
struct net_buf_simple *base_buf)
{
Expand Down
4 changes: 2 additions & 2 deletions subsys/bluetooth/audio/shell/bap.c
Original file line number Diff line number Diff line change
Expand Up @@ -4329,9 +4329,9 @@ static ssize_t nonconnectable_ad_data_add(struct bt_data *data_array,
uint32_t broadcast_id;
int err;

err = bt_bap_broadcast_source_get_id(default_source.bap_source, &broadcast_id);
err = bt_rand(&broadcast_id, BT_AUDIO_BROADCAST_ID_SIZE);
if (err != 0) {
printk("Unable to get broadcast ID: %d\n", err);
printk("Unable to generate broadcast ID: %d\n", err);

return -1;
}
Expand Down
6 changes: 3 additions & 3 deletions subsys/bluetooth/audio/shell/cap_initiator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1447,9 +1447,9 @@ static ssize_t nonconnectable_ad_data_add(struct bt_data *data_array, const size
uint32_t broadcast_id;
int err;

err = bt_cap_initiator_broadcast_get_id(default_source.cap_source, &broadcast_id);
if (err != 0) {
printk("Unable to get broadcast ID: %d\n", err);
err = bt_rand(&broadcast_id, BT_AUDIO_BROADCAST_ID_SIZE);
if (err) {
printk("Unable to generate broadcast ID: %d\n", err);

return -1;
}
Expand Down
Loading

0 comments on commit f970b06

Please sign in to comment.