Skip to content

Commit

Permalink
samples: Bluetooth: Use broadcast sink cbs in broadcast sink sample
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Thalley committed Sep 18, 2024
1 parent d7c92ad commit 02fff7e
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions samples/bluetooth/bap_broadcast_sink/src/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Nordic Semiconductor ASA
* Copyright (c) 2022-2024 Nordic Semiconductor ASA
* Copyright (c) 2024 Demant A/S
*
* SPDX-License-Identifier: Apache-2.0
Expand Down Expand Up @@ -517,10 +517,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;
k_sem_give(&sem_big_synced);
}
}

static void stream_stopped_cb(struct bt_bap_stream *stream, uint8_t reason)
Expand All @@ -533,10 +529,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) {
big_synced = false;
}
}

static void stream_recv_cb(struct bt_bap_stream *stream, const struct bt_iso_recv_info *info,
Expand Down Expand Up @@ -851,9 +843,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)
Expand Down Expand Up @@ -1035,8 +1045,6 @@ static int bis_sync_req_cb(struct bt_conn *conn,

return err;
}

k_sem_give(&sem_broadcast_sink_stopped);
}

requested_bis_sync = bis_sync_req[0];
Expand Down Expand Up @@ -1243,7 +1251,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);
Expand All @@ -1258,8 +1266,6 @@ static void bap_pa_sync_terminated_cb(struct bt_le_per_adv_sync *sync,

return;
}

k_sem_give(&sem_broadcast_sink_stopped);
}
}
}
Expand Down

0 comments on commit 02fff7e

Please sign in to comment.