Skip to content

Commit

Permalink
applications: nrf5340_audio: Rewrite adv population for broadcast_source
Browse files Browse the repository at this point in the history
	While adding the manufacturer ID in the advertisement packet it
	was noticed that the adv packet for unicast_client and
	broadcast_source are done in completely different ways.
	This rewrites both to make them closer and with a view
	to setting these later with a shell.

Signed-off-by: Graham Wacey <[email protected]>
  • Loading branch information
gWacey committed Jun 4, 2024
1 parent 0e99b70 commit ecfba7c
Show file tree
Hide file tree
Showing 8 changed files with 404 additions and 148 deletions.
150 changes: 143 additions & 7 deletions applications/nrf5340_audio/broadcast_source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,53 @@ K_THREAD_STACK_DEFINE(le_audio_msg_sub_thread_stack, CONFIG_LE_AUDIO_MSG_SUB_STA

static enum stream_state strm_state = STATE_PAUSED;

/* Buffer for the UUIDs. */
NET_BUF_SIMPLE_DEFINE_STATIC(uuid_data, CONFIG_EXT_ADV_UUID_BUF_MAX);

/* Buffer for periodic advertising BASE data. */
NET_BUF_SIMPLE_DEFINE_STATIC(base_data, 128);

/* Extended advertising buffer. */
static struct bt_data ext_adv_buf[CONFIG_EXT_ADV_BUF_MAX];

/* Periodic advertising buffer. */
static struct bt_data per_adv_buf;

#if (CONFIG_AURACAST)
/* Total size of the PBA buffer includes the 16-bit UUID, 8-bit features and the
* meta data size.
*/
#define BROADCAST_SRC_PBA_BUF_SIZE \
(BROADCAST_SOURCE_PBA_HEADER_SIZE + CONFIG_BT_AUDIO_BROADCAST_PBA_METADATA_SIZE)

/* Number of metadata items that can be assigned. */
#define BROADCAST_SOURCE_PBA_METADATA_VACANT \
(CONFIG_BT_AUDIO_BROADCAST_PBA_METADATA_SIZE / (sizeof(struct bt_data)))

/* Make sure pba_buf is large enough for a 16bit UUID and meta data
* (any addition to pba_buf requires an increase of this value)
*/
uint8_t pba_data[BROADCAST_SRC_PBA_BUF_SIZE];

/**
* @brief Broadcast source static extended advertising data.
*/
static struct broadcast_source_ext_adv_data ext_adv_data = {
.uuid_buf = &uuid_data,
.pba_metadata_vacant_cnt = BROADCAST_SOURCE_PBA_METADATA_VACANT,
.pba_buf = pba_data};
#else
/**
* @brief Broadcast source static extended advertising data.
*/
static struct broadcast_source_ext_adv_data ext_adv_data = {.uuid_buf = &uuid_data};
#endif /* (CONFIG_AURACAST) */

/**
* @brief Broadcast source static periodic advertising data.
*/
static struct broadcast_source_per_adv_data per_adv_data = {.base_buf = &base_data};

/* Function for handling all stream state changes */
static void stream_state_set(enum stream_state stream_state_new)
{
Expand Down Expand Up @@ -285,6 +332,92 @@ static int zbus_link_producers_observers(void)
return 0;
}

/*
* @brief The following configures the data for the extended advertising.
* This includes the Broadcast Audio Announcements [BAP 3.7.2.1] and Broadcast_ID
* [BAP 3.7.2.1.1] in the AUX_ADV_IND Extended Announcements.
*
* @param ext_adv_data Pointer to the extended advertising buffers.
* @param ext_adv_buf Pointer to the bt_data used for extended advertising.
* @param ext_adv_buf_size Size of @p ext_adv_buf.
* @param ext_adv_count Pointer to the number of elements added to @p adv_buf.
*
* @return 0 for success, error otherwise.
*/
static int ext_adv_populate(struct broadcast_source_ext_adv_data *ext_adv_data,
struct bt_data *ext_adv_buf, size_t ext_adv_buf_size,
size_t *ext_adv_count)
{
int ret;
size_t ext_adv_buf_cnt = 0;

ext_adv_buf[ext_adv_buf_cnt].type = BT_DATA_UUID16_SOME;
ext_adv_buf[ext_adv_buf_cnt].data = ext_adv_data->uuid_buf->data;
ext_adv_buf_cnt++;

ret = bt_mgmt_manufacturer_uuid_populate(ext_adv_data->uuid_buf,
CONFIG_BT_DEVICE_MANUFACTURER_ID);
if (ret) {
LOG_ERR("Failed to add adv data with manufacturer ID: %d", ret);
return ret;
}

ret = broadcast_source_ext_adv_populate(ext_adv_data, &ext_adv_buf[ext_adv_buf_cnt],
ext_adv_buf_size - ext_adv_buf_cnt);
if (ret < 0) {
LOG_ERR("Failed to add ext adv data for broadcast source: %d", ret);
return ret;
}

ext_adv_buf_cnt += ret;

/* Add the number of UUIDs */
ext_adv_buf[0].data_len = ext_adv_data->uuid_buf->len;

LOG_DBG("Size of adv data: %d, num_elements: %d", sizeof(struct bt_data) * ext_adv_buf_cnt,
ext_adv_buf_cnt);

*ext_adv_count = ext_adv_buf_cnt;

return 0;
}

/*
* @brief The following configures the data for the periodic advertising.
* This includes the Basic Audio Announcement, including the
* BASE [BAP 3.7.2.2] and BIGInfo.
*
* @param pre_adv_data Pointer to the periodic advertising buffers.
* @param per_adv_buf Pointer to the bt_data used for periodic advertising.
* @param per_adv_buf_size Size of @p ext_adv_buf.
* @param per_adv_count Pointer to the number of elements added to @p adv_buf.
*
* @return 0 for success, error otherwise.
*/
static int per_adv_populate(struct broadcast_source_per_adv_data *pre_adv_data,
struct bt_data *per_adv_buf, size_t per_adv_buf_size,
size_t *per_adv_count)
{
int ret;
size_t per_adv_buf_cnt = 0;

ret = broadcast_source_per_adv_populate(pre_adv_data, per_adv_buf,
per_adv_buf_size - per_adv_buf_cnt);
if (ret < 0) {
LOG_ERR("Failed to add per adv data for broadcast source: %d", ret);
return ret;
}

per_adv_buf_cnt += ret;

LOG_DBG("Size of per adv data: %d, num_elements: %d",
sizeof(struct bt_data) * per_adv_buf_cnt, per_adv_buf_cnt);

*per_adv_count = per_adv_buf_cnt;

return 0;
}

uint8_t stream_state_get(void)
{
return strm_state;
Expand Down Expand Up @@ -315,20 +448,18 @@ void streamctrl_send(void const *const data, size_t size, uint8_t num_ch)
int main(void)
{
int ret;
static const struct bt_data *ext_adv;
static const struct bt_data *per_adv;

LOG_DBG("nRF5340 APP core started");

size_t ext_adv_buf_cnt = 0;
size_t per_adv_buf_cnt = 0;

ret = nrf5340_audio_dk_init();
ERR_CHK(ret);

ret = nrf5340_audio_common_init();
ERR_CHK(ret);

size_t ext_adv_size = 0;
size_t per_adv_size = 0;

ret = zbus_subscribers_create();
ERR_CHK_MSG(ret, "Failed to create zbus subscriber threads");

Expand All @@ -343,9 +474,14 @@ int main(void)
CONFIG_BT_AUDIO_BITRATE_BROADCAST_SRC, VALUE_NOT_SET);
ERR_CHK_MSG(ret, "Failed to set sample- and bitrate");

broadcast_source_adv_get(&ext_adv, &ext_adv_size, &per_adv, &per_adv_size);
ret = ext_adv_populate(&ext_adv_data, ext_adv_buf, ARRAY_SIZE(ext_adv_buf),
&ext_adv_buf_cnt);
ERR_CHK(ret);

ret = per_adv_populate(&per_adv_data, &per_adv_buf, 1, &per_adv_buf_cnt);
ERR_CHK(ret);

ret = bt_mgmt_adv_start(ext_adv, ext_adv_size, per_adv, per_adv_size, false);
ret = bt_mgmt_adv_start(ext_adv_buf, ext_adv_buf_cnt, &per_adv_buf, per_adv_buf_cnt, false);
ERR_CHK_MSG(ret, "Failed to start advertiser");

return 0;
Expand Down
19 changes: 19 additions & 0 deletions applications/nrf5340_audio/src/bluetooth/bt_management/bt_mgmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,25 @@ static int local_identity_addr_print(void)
return 0;
}

int bt_mgmt_adv_buffer_put(struct bt_data *const adv_buf, int *index, size_t adv_buf_vacant,
size_t data_len, uint8_t type, void *data)
{
if (adv_buf == NULL || index == NULL || data_len == 0) {
return -EINVAL;
}

if ((adv_buf_vacant - *index) <= 0) {
return -ENOMEM;
}

adv_buf[*index].type = type;
adv_buf[*index].data_len = data_len;
adv_buf[*index].data = data;
(*index)++;

return 0;
}

int bt_mgmt_bonding_clear(void)
{
int ret;
Expand Down
15 changes: 15 additions & 0 deletions applications/nrf5340_audio/src/bluetooth/bt_management/bt_mgmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ enum bt_mgmt_scan_type {

#define BRDCAST_ID_NOT_USED (BT_AUDIO_BROADCAST_ID_MAX + 1)

/**
* @brief Load advertising data into an advertising buffer.
*
* @param[out] adv_buf Pointer to the advertising buffer to load.
* @param[in/out] index Next free index in the advertising buffer.
* @param[in] adv_buf_vacant Number of free advertising buffers.
* @param[in] type Type of the data.
* @param[in] data_len Length of the data.
* @param[in] data Data to store in the buffer, can be a pointer or value.
*
* @return 0 if success, error otherwise.
*/
int bt_mgmt_adv_buffer_put(struct bt_data *const adv_buf, int *index, size_t adv_buf_vacant,
size_t data_len, uint8_t type, void *data);

/**
* @brief Start scanning for advertisements.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ config BT_AUDIO_BROADCAST_ID_FIXED
Fixed broadcast ID; 3 octets. Will only be used if BT_AUDIO_USE_BROADCAST_ID_RANDOM=n.
Only use for debugging.

config BT_AUDIO_BROADCAST_PBA_METADATA_SIZE
hex "Configure PBA meta data buffer size"
depends on TRANSPORT_BIS && AURACAST
default 0x0010
help
Configure the maximum size of the Public Broadcast Announcement meata data buffer in octets.
This is the number of meta data LVT records, or the number of meta data items multiplied by
the size of the LTV (sizeof(bt_data)). Configurable option that doesn't follow any preset.
Allows for more flexibility.

config BT_AUDIO_BROADCAST_PARENTAL_RATING
hex "Parental rating"
depends on TRANSPORT_BIS
Expand Down
Loading

0 comments on commit ecfba7c

Please sign in to comment.