Skip to content

Commit 6e86b67

Browse files
Thalleykartben
authored andcommitted
samples: Bluetooth: Use broadcast source cbs in broadcast source sample
Use the broadcast source callbacks to set the semaphores rather than the stream callbacks, as the broadcast source callbacks are better for that as they are called when all streams are ready. Signed-off-by: Emil Gydesen <[email protected]>
1 parent 829519d commit 6e86b67

File tree

1 file changed

+31
-17
lines changed
  • samples/bluetooth/bap_broadcast_source/src

1 file changed

+31
-17
lines changed

samples/bluetooth/bap_broadcast_source/src/main.c

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ static int16_t send_pcm_data[MAX_NUM_SAMPLES];
153153
static uint16_t seq_num;
154154
static bool stopping;
155155

156-
static K_SEM_DEFINE(sem_started, 0U, ARRAY_SIZE(streams));
157-
static K_SEM_DEFINE(sem_stopped, 0U, ARRAY_SIZE(streams));
156+
static K_SEM_DEFINE(sem_started, 0U, 1U);
157+
static K_SEM_DEFINE(sem_stopped, 0U, 1U);
158158

159159
#define BROADCAST_SOURCE_LIFETIME 120U /* seconds */
160160

@@ -365,12 +365,6 @@ static void stream_started_cb(struct bt_bap_stream *stream)
365365

366366
source_stream->seq_num = 0U;
367367
source_stream->sent_cnt = 0U;
368-
k_sem_give(&sem_started);
369-
}
370-
371-
static void stream_stopped_cb(struct bt_bap_stream *stream, uint8_t reason)
372-
{
373-
k_sem_give(&sem_stopped);
374368
}
375369

376370
static void stream_sent_cb(struct bt_bap_stream *stream)
@@ -387,7 +381,9 @@ static void stream_sent_cb(struct bt_bap_stream *stream)
387381
}
388382

389383
static struct bt_bap_stream_ops stream_ops = {
390-
.started = stream_started_cb, .stopped = stream_stopped_cb, .sent = stream_sent_cb};
384+
.started = stream_started_cb,
385+
.sent = stream_sent_cb,
386+
};
391387

392388
static int setup_broadcast_source(struct bt_bap_broadcast_source **source)
393389
{
@@ -439,8 +435,24 @@ static int setup_broadcast_source(struct bt_bap_broadcast_source **source)
439435
return 0;
440436
}
441437

438+
static void source_started_cb(struct bt_bap_broadcast_source *source)
439+
{
440+
printk("Broadcast source %p started\n", source);
441+
k_sem_give(&sem_started);
442+
}
443+
444+
static void source_stopped_cb(struct bt_bap_broadcast_source *source, uint8_t reason)
445+
{
446+
printk("Broadcast source %p stopped with reason 0x%02X\n", source, reason);
447+
k_sem_give(&sem_stopped);
448+
}
449+
442450
int main(void)
443451
{
452+
static struct bt_bap_broadcast_source_cb broadcast_source_cb = {
453+
.started = source_started_cb,
454+
.stopped = source_stopped_cb,
455+
};
444456
struct bt_le_ext_adv *adv;
445457
int err;
446458

@@ -451,6 +463,12 @@ int main(void)
451463
}
452464
printk("Bluetooth initialized\n");
453465

466+
err = bt_bap_broadcast_source_register_cb(&broadcast_source_cb);
467+
if (err != 0) {
468+
printk("Failed to register broadcast source callbacks (err %d)\n", err);
469+
return 0;
470+
}
471+
454472
for (size_t i = 0U; i < ARRAY_SIZE(send_pcm_data); i++) {
455473
/* Initialize mock data */
456474
send_pcm_data[i] = i;
@@ -581,10 +599,8 @@ int main(void)
581599
return 0;
582600
}
583601

584-
/* Wait for all to be started */
585-
for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) {
586-
k_sem_take(&sem_started, K_FOREVER);
587-
}
602+
/* Wait for broadcast source to be started */
603+
k_sem_take(&sem_started, K_FOREVER);
588604
printk("Broadcast source started\n");
589605

590606
/* Initialize sending */
@@ -608,10 +624,8 @@ int main(void)
608624
return 0;
609625
}
610626

611-
/* Wait for all to be stopped */
612-
for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) {
613-
k_sem_take(&sem_stopped, K_FOREVER);
614-
}
627+
/* Wait for broadcast source to be stopped */
628+
k_sem_take(&sem_stopped, K_FOREVER);
615629
printk("Broadcast source stopped\n");
616630

617631
printk("Deleting broadcast source\n");

0 commit comments

Comments
 (0)