From 13c42bd8a38b8360102bc065e37a6ecea9a63a6d Mon Sep 17 00:00:00 2001 From: Emil Gydesen Date: Fri, 5 Jul 2024 20:30:09 +0200 Subject: [PATCH] samples: Bluetooth: Use broadcast sink cbs in broadcast sink sample Use the broadcast sink callbacks to set the semaphores rather than the stream callbacks, as the broadcast sink callbacks are better for that as they are called when all streams are ready. Signed-off-by: Emil Gydesen --- .../bluetooth/bap_broadcast_sink/src/main.c | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/samples/bluetooth/bap_broadcast_sink/src/main.c b/samples/bluetooth/bap_broadcast_sink/src/main.c index cee4f5b008c..902670aaee5 100644 --- a/samples/bluetooth/bap_broadcast_sink/src/main.c +++ b/samples/bluetooth/bap_broadcast_sink/src/main.c @@ -536,11 +536,6 @@ static void stream_started_cb(struct bt_bap_stream *stream) #endif /* CONFIG_LIBLC3 */ k_sem_give(&sem_stream_started); - if (k_sem_count_get(&sem_stream_started) == stream_count) { - big_synced = true; - printk("BIG synced\n"); - k_sem_give(&sem_big_synced); - } } static void stream_stopped_cb(struct bt_bap_stream *stream, uint8_t reason) @@ -553,11 +548,6 @@ static void stream_stopped_cb(struct bt_bap_stream *stream, uint8_t reason) if (err != 0) { printk("Failed to take sem_stream_started: %d\n", err); } - - if (k_sem_count_get(&sem_stream_started) != stream_count) { - printk("BIG sync terminated\n"); - big_synced = false; - } } static void stream_recv_cb(struct bt_bap_stream *stream, const struct bt_iso_recv_info *info, @@ -818,9 +808,27 @@ static void syncable_cb(struct bt_bap_broadcast_sink *sink, const struct bt_iso_ } } +static void broadcast_sink_started_cb(struct bt_bap_broadcast_sink *sink) +{ + printk("Broadcast sink %p started\n", sink); + + big_synced = true; + k_sem_give(&sem_big_synced); +} + +static void broadcast_sink_stopped_cb(struct bt_bap_broadcast_sink *sink, uint8_t reason) +{ + printk("Broadcast sink %p stopped with reason 0x%02X\n", sink, reason); + + big_synced = false; + k_sem_give(&sem_broadcast_sink_stopped); +} + static struct bt_bap_broadcast_sink_cb broadcast_sink_cbs = { .base_recv = base_recv_cb, .syncable = syncable_cb, + .started = broadcast_sink_started_cb, + .stopped = broadcast_sink_stopped_cb, }; static void pa_timer_handler(struct k_work *work) @@ -1027,8 +1035,6 @@ static int bis_sync_req_cb(struct bt_conn *conn, return err; } - - k_sem_give(&sem_broadcast_sink_stopped); } broadcaster_broadcast_id = recv_state->broadcast_id; @@ -1242,7 +1248,7 @@ static void bap_pa_sync_terminated_cb(struct bt_le_per_adv_sync *sync, if (info->reason != BT_HCI_ERR_LOCALHOST_TERM_CONN && req_recv_state != NULL) { int err; - if (k_sem_count_get(&sem_stream_connected) > 0) { + if (big_synced) { err = bt_bap_broadcast_sink_stop(broadcast_sink); if (err != 0) { printk("Failed to stop Broadcast Sink: %d\n", err); @@ -1257,8 +1263,6 @@ static void bap_pa_sync_terminated_cb(struct bt_le_per_adv_sync *sync, return; } - - k_sem_give(&sem_broadcast_sink_stopped); } } }