Bluetooth: BAP: Add broadcast source callback structs

These callbacks are trigger for changes that affect the entire
broadcast source, such as the BIG started and terminated events.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
This commit is contained in:
Emil Gydesen 2024-07-05 10:21:40 +02:00 committed by Benjamin Cabé
commit ab9ee0817d
10 changed files with 416 additions and 45 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2023 Nordic Semiconductor ASA
* Copyright (c) 2021-2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -35,6 +35,8 @@
#define SUPPORTED_MAX_FRAMES_PER_SDU 1
#if defined(CONFIG_BT_BAP_BROADCAST_SOURCE)
CREATE_FLAG(flag_source_started);
/* When BROADCAST_ENQUEUE_COUNT > 1 we can enqueue enough buffers to ensure that
* the controller is never idle
*/
@ -62,8 +64,8 @@ static uint8_t bis_codec_data[] = {
BT_BYTES_LIST_LE32(BT_AUDIO_LOCATION_FRONT_CENTER)),
};
static K_SEM_DEFINE(sem_started, 0U, ARRAY_SIZE(broadcast_source_streams));
static K_SEM_DEFINE(sem_stopped, 0U, ARRAY_SIZE(broadcast_source_streams));
static K_SEM_DEFINE(sem_stream_started, 0U, ARRAY_SIZE(broadcast_source_streams));
static K_SEM_DEFINE(sem_stream_stopped, 0U, ARRAY_SIZE(broadcast_source_streams));
static void validate_stream_codec_cfg(const struct bt_bap_stream *stream)
{
@ -174,7 +176,7 @@ static void validate_stream_codec_cfg(const struct bt_bap_stream *stream)
}
}
static void started_cb(struct bt_bap_stream *stream)
static void stream_started_cb(struct bt_bap_stream *stream)
{
struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream);
struct bt_bap_ep_info info;
@ -216,13 +218,13 @@ static void started_cb(struct bt_bap_stream *stream)
printk("Stream %p started\n", stream);
validate_stream_codec_cfg(stream);
k_sem_give(&sem_started);
k_sem_give(&sem_stream_started);
}
static void stopped_cb(struct bt_bap_stream *stream, uint8_t reason)
static void steam_stopped_cb(struct bt_bap_stream *stream, uint8_t reason)
{
printk("Stream %p stopped with reason 0x%02X\n", stream, reason);
k_sem_give(&sem_stopped);
k_sem_give(&sem_stream_stopped);
}
static void stream_sent_cb(struct bt_bap_stream *stream)
@ -265,11 +267,23 @@ static void stream_sent_cb(struct bt_bap_stream *stream)
}
static struct bt_bap_stream_ops stream_ops = {
.started = started_cb,
.stopped = stopped_cb,
.started = stream_started_cb,
.stopped = steam_stopped_cb,
.sent = stream_sent_cb,
};
static void source_started_cb(struct bt_bap_broadcast_source *source)
{
printk("Broadcast source %p started\n", source);
SET_FLAG(flag_source_started);
}
static void source_stopped_cb(struct bt_bap_broadcast_source *source, uint8_t reason)
{
printk("Broadcast source %p stopped with reason 0x%02X\n", source, reason);
UNSET_FLAG(flag_source_started);
}
static int setup_broadcast_source(struct bt_bap_broadcast_source **source, bool encryption)
{
struct bt_bap_broadcast_source_stream_param
@ -467,10 +481,12 @@ static void test_broadcast_source_start(struct bt_bap_broadcast_source *source,
}
/* Wait for all to be started */
printk("Waiting for streams to be started\n");
printk("Waiting for %zu streams to be started\n", ARRAY_SIZE(broadcast_source_streams));
for (size_t i = 0U; i < ARRAY_SIZE(broadcast_source_streams); i++) {
k_sem_take(&sem_started, K_FOREVER);
k_sem_take(&sem_stream_started, K_FOREVER);
}
WAIT_FOR_FLAG(flag_source_started);
}
static void test_broadcast_source_update_metadata(struct bt_bap_broadcast_source *source,
@ -520,10 +536,12 @@ static void test_broadcast_source_stop(struct bt_bap_broadcast_source *source)
}
/* Wait for all to be stopped */
printk("Waiting for streams to be stopped\n");
printk("Waiting for %zu streams to be stopped\n", ARRAY_SIZE(broadcast_source_streams));
for (size_t i = 0U; i < ARRAY_SIZE(broadcast_source_streams); i++) {
k_sem_take(&sem_stopped, K_FOREVER);
k_sem_take(&sem_stream_stopped, K_FOREVER);
}
WAIT_FOR_UNSET_FLAG(flag_source_started);
}
static void test_broadcast_source_delete(struct bt_bap_broadcast_source *source)
@ -564,10 +582,12 @@ static int stop_extended_adv(struct bt_le_ext_adv *adv)
return 0;
}
static void test_main(void)
static void init(void)
{
struct bt_bap_broadcast_source *source;
struct bt_le_ext_adv *adv;
static struct bt_bap_broadcast_source_cb broadcast_source_cb = {
.started = source_started_cb,
.stopped = source_stopped_cb,
};
int err;
err = bt_enable(NULL);
@ -578,6 +598,21 @@ static void test_main(void)
printk("Bluetooth initialized\n");
err = bt_bap_broadcast_source_register_cb(&broadcast_source_cb);
if (err != 0) {
FAIL("Failed to register broadcast source callbacks (err %d)\n", err);
return;
}
}
static void test_main(void)
{
struct bt_bap_broadcast_source *source;
struct bt_le_ext_adv *adv;
int err;
init();
err = setup_broadcast_source(&source, false);
if (err != 0) {
FAIL("Unable to setup broadcast source: %d\n", err);
@ -650,13 +685,7 @@ static void test_main_encrypted(void)
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");
init();
err = setup_broadcast_source(&source, true);
if (err != 0) {