Skip to content

Commit b233435

Browse files
committed
Bluetooth: CAP: Implement unicast to broadcast handover
Implement the unicast to broadcast handover procedure, as per the Bluetooth CAP specificiation. Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
1 parent 5a06edf commit b233435

File tree

9 files changed

+1320
-53
lines changed

9 files changed

+1320
-53
lines changed

include/zephyr/bluetooth/audio/bap.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,32 @@ int bt_bap_unicast_group_add_streams(struct bt_bap_unicast_group *unicast_group,
16721672
*/
16731673
int bt_bap_unicast_group_delete(struct bt_bap_unicast_group *unicast_group);
16741674

1675+
/** Callback function for bt_bap_unicast_group_foreach_stream()
1676+
*
1677+
* @param stream The audio stream
1678+
* @param user_data User data
1679+
*
1680+
* @retval true Stop iterating.
1681+
* @retval false Continue iterating.
1682+
*/
1683+
typedef bool (*bt_bap_unicast_group_foreach_stream_func_t)(struct bt_bap_stream *stream,
1684+
void *user_data);
1685+
1686+
/**
1687+
* @brief Iterate through all streams in a unicast group
1688+
*
1689+
* @param unicast_group The unicast group
1690+
* @param func The callback function
1691+
* @param user_data User specified data that sent to the callback function
1692+
*
1693+
* @retval 0 Success (even if no streams exists in the group).
1694+
* @retval -ECANCELED Iteration was stopped by the callback function before complete.
1695+
* @retval -EINVAL @p unicast_group or @p func were NULL.
1696+
*/
1697+
int bt_bap_unicast_group_foreach_stream(struct bt_bap_unicast_group *unicast_group,
1698+
bt_bap_unicast_group_foreach_stream_func_t func,
1699+
void *user_data);
1700+
16751701
/** Unicast Client callback structure */
16761702
struct bt_bap_unicast_client_cb {
16771703
/**

include/zephyr/bluetooth/audio/cap.h

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ struct bt_cap_initiator_cb {
147147
*/
148148
void (*broadcast_stopped)(struct bt_cap_broadcast_source *source, uint8_t reason);
149149
#endif /* CONFIG_BT_BAP_BROADCAST_SOURCE */
150+
#if defined(CONFIG_BT_CAP_HANDOVER_SUPPORTED)
151+
/**
152+
* @brief The unicast to broadcast handover procedure has finished
153+
*/
154+
void (*unicast_to_broadcast_complete)(int err, struct bt_conn *conn);
155+
#endif /* CONFIG_BT_CAP_HANDOVER_SUPPORTED */
150156
};
151157

152158
/**
@@ -398,6 +404,32 @@ int bt_cap_unicast_group_add_streams(struct bt_cap_unicast_group *unicast_group,
398404
*/
399405
int bt_cap_unicast_group_delete(struct bt_cap_unicast_group *unicast_group);
400406

407+
/** Callback function for bt_bap_unicast_group_foreach_stream()
408+
*
409+
* @param stream The audio stream
410+
* @param user_data User data
411+
*
412+
* @retval true Stop iterating.
413+
* @retval false Continue iterating.
414+
*/
415+
typedef bool (*bt_cap_unicast_group_foreach_stream_func_t)(struct bt_cap_stream *stream,
416+
void *user_data);
417+
418+
/**
419+
* @brief Iterate through all streams in a unicast group
420+
*
421+
* @param unicast_group The unicast group
422+
* @param func The callback function
423+
* @param user_data User specified data that sent to the callback function
424+
*
425+
* @retval 0 Success (even if no streams exists in the group).
426+
* @retval -ECANCELED Iteration was stopped by the callback function before complete.
427+
* @retval -EINVAL @p unicast_group or @p func were NULL.
428+
*/
429+
int bt_cap_unicast_group_foreach_stream(struct bt_cap_unicast_group *unicast_group,
430+
bt_cap_unicast_group_foreach_stream_func_t func,
431+
void *user_data);
432+
401433
/** Stream specific parameters for the bt_cap_initiator_unicast_audio_start() function */
402434
struct bt_cap_unicast_audio_start_stream_param {
403435
/** Coordinated or ad-hoc set member. */
@@ -788,30 +820,30 @@ int bt_cap_initiator_broadcast_get_base(struct bt_cap_broadcast_source *broadcas
788820

789821
/** Parameters for bt_cap_initiator_unicast_to_broadcast() */
790822
struct bt_cap_unicast_to_broadcast_param {
823+
/** The type of the set. */
824+
enum bt_cap_set_type type;
825+
791826
/** The source unicast group with the streams. */
792-
struct bt_bap_unicast_group *unicast_group;
827+
struct bt_cap_unicast_group *unicast_group;
793828

794829
/**
795-
* @brief Whether or not to encrypt the streams.
830+
* @brief The advertising set to use for the broadcast source
796831
*
797-
* If set to true, then the broadcast code in @p broadcast_code
798-
* will be used to encrypt the streams.
832+
* This shall remain valid until the procedure has completed.
799833
*/
800-
bool encrypt;
834+
/* TBD: Is this necessary? Should we, or the app, start the source? */
835+
struct bt_le_ext_adv *ext_adv;
836+
837+
uint8_t sid;
838+
uint16_t pa_interval;
839+
uint32_t broadcast_id;
801840

802841
/**
803-
* @brief 16-octet broadcast code.
804-
*
805-
* Only valid if @p encrypt is true.
842+
* @brief Broadcast source parameters
806843
*
807-
* If the value is a string or a the value is less than 16 octets,
808-
* the remaining octets shall be 0.
809-
*
810-
* Example:
811-
* The string "Broadcast Code" shall be
812-
* [42 72 6F 61 64 63 61 73 74 20 43 6F 64 65 00 00]
844+
* These parameters shall remain valid until the procedure has completed.
813845
*/
814-
uint8_t broadcast_code[BT_ISO_BROADCAST_CODE_SIZE];
846+
struct bt_cap_initiator_broadcast_create_param *broadcast_create_param;
815847
};
816848

817849
/**
@@ -820,10 +852,7 @@ struct bt_cap_unicast_to_broadcast_param {
820852
* The streams in the unicast group will be stopped and the unicast group
821853
* will be deleted. This can only be done for source streams.
822854
*
823-
* @note @kconfig{CONFIG_BT_CAP_INITIATOR},
824-
* @kconfig{CONFIG_BT_BAP_UNICAST_CLIENT} and
825-
* @kconfig{CONFIG_BT_BAP_BROADCAST_SOURCE} must be enabled for this function
826-
* to be enabled.
855+
* @kconfig_dep{CONFIG_BT_CAP_HANDOVER_SUPPORTED}
827856
*
828857
* @param param The parameters for the handover.
829858
* @param source The resulting broadcast source.
@@ -864,10 +893,7 @@ struct bt_cap_broadcast_to_unicast_param {
864893
* The streams in the broadcast source will be stopped and the broadcast source
865894
* will be deleted.
866895
*
867-
* @note @kconfig{CONFIG_BT_CAP_INITIATOR},
868-
* @kconfig{CONFIG_BT_BAP_UNICAST_CLIENT} and
869-
* @kconfig{CONFIG_BT_BAP_BROADCAST_SOURCE} must be enabled for this function
870-
* to be enabled.
896+
* @kconfig_dep{CONFIG_BT_CAP_HANDOVER_SUPPORTED}
871897
*
872898
* @param[in] param The parameters for the handover.
873899
* @param[out] unicast_group The resulting broadcast source.

subsys/bluetooth/audio/Kconfig.cap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,7 @@ config BT_CAP_COMMANDER
4848
BT_MCC
4949
help
5050
Enabling this will enable the CAP Initiator role.
51+
52+
config BT_CAP_HANDOVER_SUPPORTED
53+
def_bool BT_CAP_COMMANDER && BT_CAP_INITIATOR && BT_BAP_BROADCAST_ASSISTANT && \
54+
BT_BAP_BROADCAST_SOURCE && BT_BAP_UNICAST_CLIENT

subsys/bluetooth/audio/bap_unicast_client.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3143,6 +3143,33 @@ int bt_bap_unicast_group_delete(struct bt_bap_unicast_group *unicast_group)
31433143
return 0;
31443144
}
31453145

3146+
int bt_bap_unicast_group_foreach_stream(struct bt_bap_unicast_group *unicast_group,
3147+
bt_bap_unicast_group_foreach_stream_func_t func,
3148+
void *user_data)
3149+
{
3150+
struct bt_bap_stream *stream, *next;
3151+
3152+
if (unicast_group == NULL) {
3153+
LOG_DBG("unicast_group is NULL");
3154+
return -EINVAL;
3155+
}
3156+
3157+
if (func == NULL) {
3158+
LOG_DBG("func is NULL");
3159+
return -EINVAL;
3160+
}
3161+
3162+
SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&unicast_group->streams, stream, next, _node) {
3163+
const bool stop = func(stream, user_data);
3164+
3165+
if (stop) {
3166+
return -ECANCELED;
3167+
}
3168+
}
3169+
3170+
return 0;
3171+
}
3172+
31463173
int bt_bap_unicast_client_config(struct bt_bap_stream *stream,
31473174
const struct bt_audio_codec_cfg *codec_cfg)
31483175
{

0 commit comments

Comments
 (0)