Skip to content

Commit 98cc1e5

Browse files
committed
Bluetooth: CAP: Implement bt_cap_initiator_broadcast_foreach_stream
Implement the function bt_cap_initiator_broadcast_foreach_stream that allows users to iterate on all BAP streams in a BAP broadcast source. This can be used to easily get references to other broadcast streams in the same group. Signed-off-by: Emil Gydesen <[email protected]>
1 parent 86ad7b3 commit 98cc1e5

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

include/zephyr/bluetooth/audio/cap.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,32 @@ int bt_cap_initiator_broadcast_audio_delete(struct bt_cap_broadcast_source *broa
812812
int bt_cap_initiator_broadcast_get_base(struct bt_cap_broadcast_source *broadcast_source,
813813
struct net_buf_simple *base_buf);
814814

815+
/** Callback function for bt_cap_initiator_broadcast_foreach_stream()
816+
*
817+
* @param stream The audio stream
818+
* @param user_data User data
819+
*
820+
* @retval true Stop iterating.
821+
* @retval false Continue iterating.
822+
*/
823+
typedef bool (*bt_cap_initiator_broadcast_foreach_stream_func_t)(struct bt_cap_stream *stream,
824+
void *user_data);
825+
826+
/**
827+
* @brief Iterate through all streams in a broadcast source
828+
*
829+
* @param broadcast_source The broadcast source.
830+
* @param func The callback function.
831+
* @param user_data User specified data that is sent to the callback function.
832+
*
833+
* @retval 0 Success (even if no streams exists in the group).
834+
* @retval -ECANCELED The @p func returned true.
835+
* @retval -EINVAL @p unicast_group or @p func were NULL.
836+
*/
837+
int bt_cap_initiator_broadcast_foreach_stream(struct bt_cap_broadcast_source *broadcast_source,
838+
bt_cap_initiator_broadcast_foreach_stream_func_t func,
839+
void *user_data);
840+
815841
/** Parameters for bt_cap_handover_unicast_to_broadcast() */
816842
struct bt_cap_handover_unicast_to_broadcast_param {
817843
/** The type of the set. */

subsys/bluetooth/audio/cap_initiator.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,47 @@ int bt_cap_initiator_broadcast_get_base(struct bt_cap_broadcast_source *broadcas
443443
return bt_bap_broadcast_source_get_base(broadcast_source->bap_broadcast, base_buf);
444444
}
445445

446+
struct cap_broadcast_source_foreach_stream_data {
447+
bt_cap_initiator_broadcast_foreach_stream_func_t func;
448+
void *user_data;
449+
};
450+
451+
static bool cap_broadcast_source_foreach_stream_cb(struct bt_bap_stream *bap_stream,
452+
void *user_data)
453+
{
454+
struct cap_broadcast_source_foreach_stream_data *data = user_data;
455+
456+
/* Since we are iterating on a CAP unicast group, we can assume that all streams are CAP
457+
* streams
458+
*/
459+
data->func(CONTAINER_OF(bap_stream, struct bt_cap_stream, bap_stream), data->user_data);
460+
461+
return false;
462+
}
463+
464+
int bt_cap_initiator_broadcast_foreach_stream(struct bt_cap_broadcast_source *broadcast_source,
465+
bt_cap_initiator_broadcast_foreach_stream_func_t func,
466+
void *user_data)
467+
{
468+
struct cap_broadcast_source_foreach_stream_data data = {
469+
.func = func,
470+
.user_data = user_data,
471+
};
472+
473+
if (broadcast_source == NULL) {
474+
LOG_DBG("source is NULL");
475+
return -EINVAL;
476+
}
477+
478+
if (func == NULL) {
479+
LOG_DBG("func is NULL");
480+
return -EINVAL;
481+
}
482+
483+
return bt_bap_broadcast_source_foreach_stream(
484+
broadcast_source->bap_broadcast, cap_broadcast_source_foreach_stream_cb, &data);
485+
}
486+
446487
#endif /* CONFIG_BT_BAP_BROADCAST_SOURCE */
447488

448489
#if defined(CONFIG_BT_BAP_UNICAST_CLIENT)

0 commit comments

Comments
 (0)