1 /* Bluetooth Audio Common Audio Profile internal header */ 2 3 /* 4 * Copyright (c) 2022-2025 Nordic Semiconductor ASA 5 * 6 * SPDX-License-Identifier: Apache-2.0 7 */ 8 9 #include <stdbool.h> 10 #include <stddef.h> 11 #include <stdint.h> 12 #include <stdint.h> 13 14 #include <zephyr/autoconf.h> 15 #include <zephyr/bluetooth/audio/audio.h> 16 #include <zephyr/bluetooth/audio/bap.h> 17 #include <zephyr/bluetooth/audio/cap.h> 18 #include <zephyr/bluetooth/audio/csip.h> 19 #include <zephyr/bluetooth/addr.h> 20 #include <zephyr/bluetooth/bluetooth.h> 21 #include <zephyr/bluetooth/conn.h> 22 #include <zephyr/bluetooth/gatt.h> 23 #include <zephyr/bluetooth/iso.h> 24 #include <zephyr/sys/atomic.h> 25 #include <zephyr/sys/util.h> 26 #include <zephyr/sys/util_macro.h> 27 #include <zephyr/types.h> 28 29 bool bt_cap_acceptor_ccid_exist(const struct bt_conn *conn, uint8_t ccid); 30 bool bt_cap_acceptor_ccids_exist(const struct bt_conn *conn, const uint8_t ccids[], 31 uint8_t ccid_cnt); 32 33 void bt_cap_initiator_codec_configured(struct bt_cap_stream *cap_stream); 34 void bt_cap_initiator_qos_configured(struct bt_cap_stream *cap_stream); 35 void bt_cap_initiator_enabled(struct bt_cap_stream *cap_stream); 36 void bt_cap_initiator_started(struct bt_cap_stream *cap_stream); 37 void bt_cap_initiator_connected(struct bt_cap_stream *cap_stream); 38 void bt_cap_initiator_metadata_updated(struct bt_cap_stream *cap_stream); 39 void bt_cap_initiator_disabled(struct bt_cap_stream *cap_stream); 40 void bt_cap_initiator_stopped(struct bt_cap_stream *cap_stream); 41 void bt_cap_initiator_released(struct bt_cap_stream *cap_stream); 42 void bt_cap_stream_ops_register_bap(struct bt_cap_stream *cap_stream); 43 void bt_cap_initiator_cp_cb(struct bt_cap_stream *cap_stream, enum bt_bap_ascs_rsp_code rsp_code, 44 enum bt_bap_ascs_reason reason); 45 46 enum bt_cap_common_proc_state { 47 BT_CAP_COMMON_PROC_STATE_ACTIVE, 48 BT_CAP_COMMON_PROC_STATE_ABORTED, 49 /* While the handover is technically a procedure, and arguably should use 50 * `bt_cap_common_set_proc`, we set the it as a flag instead so that we can rely on the API 51 * functions to perform their procedures 52 */ 53 BT_CAP_COMMON_PROC_STATE_HANDOVER, 54 55 BT_CAP_COMMON_PROC_STATE_FLAG_NUM, 56 }; 57 58 enum bt_cap_common_proc_type { 59 BT_CAP_COMMON_PROC_TYPE_NONE, 60 BT_CAP_COMMON_PROC_TYPE_START, 61 BT_CAP_COMMON_PROC_TYPE_UPDATE, 62 BT_CAP_COMMON_PROC_TYPE_STOP, 63 BT_CAP_COMMON_PROC_TYPE_BROADCAST_RECEPTION_START, 64 BT_CAP_COMMON_PROC_TYPE_BROADCAST_RECEPTION_STOP, 65 BT_CAP_COMMON_PROC_TYPE_DISTRIBUTE_BROADCAST_CODE, 66 BT_CAP_COMMON_PROC_TYPE_VOLUME_CHANGE, 67 BT_CAP_COMMON_PROC_TYPE_VOLUME_OFFSET_CHANGE, 68 BT_CAP_COMMON_PROC_TYPE_VOLUME_MUTE_CHANGE, 69 BT_CAP_COMMON_PROC_TYPE_MICROPHONE_GAIN_CHANGE, 70 BT_CAP_COMMON_PROC_TYPE_MICROPHONE_MUTE_CHANGE, 71 }; 72 73 enum bt_cap_common_subproc_type { 74 BT_CAP_COMMON_SUBPROC_TYPE_NONE, 75 BT_CAP_COMMON_SUBPROC_TYPE_CODEC_CONFIG, 76 BT_CAP_COMMON_SUBPROC_TYPE_QOS_CONFIG, 77 BT_CAP_COMMON_SUBPROC_TYPE_ENABLE, 78 BT_CAP_COMMON_SUBPROC_TYPE_CONNECT, 79 BT_CAP_COMMON_SUBPROC_TYPE_START, 80 BT_CAP_COMMON_SUBPROC_TYPE_META_UPDATE, 81 BT_CAP_COMMON_SUBPROC_TYPE_DISABLE, 82 BT_CAP_COMMON_SUBPROC_TYPE_STOP, 83 BT_CAP_COMMON_SUBPROC_TYPE_RELEASE, 84 }; 85 86 struct bt_cap_unicast_group { 87 struct bt_bap_unicast_group *bap_unicast_group; 88 }; 89 90 struct bt_cap_initiator_proc_param { 91 struct bt_cap_stream *stream; 92 union { 93 struct { 94 struct bt_conn *conn; 95 struct bt_bap_ep *ep; 96 struct bt_audio_codec_cfg *codec_cfg; 97 bool connected; 98 } start; 99 struct { 100 /** Codec Specific Capabilities Metadata count */ 101 size_t meta_len; 102 /** Codec Specific Capabilities Metadata */ 103 uint8_t meta[CONFIG_BT_AUDIO_CODEC_CFG_MAX_METADATA_SIZE]; 104 } meta_update; 105 struct { 106 bool release; 107 } stop; 108 }; 109 bool in_progress; 110 }; 111 112 #if defined(CONFIG_BT_BAP_BROADCAST_ASSISTANT) 113 struct cap_broadcast_reception_start { 114 115 bt_addr_le_t addr; 116 uint8_t adv_sid; 117 uint32_t broadcast_id; 118 uint16_t pa_interval; 119 uint8_t num_subgroups; 120 struct bt_bap_bass_subgroup subgroups[CONFIG_BT_BAP_BASS_MAX_SUBGROUPS]; 121 }; 122 123 struct cap_broadcast_reception_stop { 124 uint8_t src_id; 125 uint8_t num_subgroups; 126 struct bt_bap_bass_subgroup subgroups[CONFIG_BT_BAP_BASS_MAX_SUBGROUPS]; 127 }; 128 129 /* Note that although the broadcast_code will be the same for all 130 * we nevertheless store a separate copy for each sink, for 131 * consistency in the struct bt_cap_commander_proc_param 132 * There is no memory savings by not having broadcast_code part of the 133 * union: struct cap_broadcast_reception_start uses minimum 20 bytes 134 * and struct cap_distribute_broadcast_code uses 17 bytes 135 */ 136 struct cap_distribute_broadcast_code { 137 uint8_t src_id; 138 uint8_t broadcast_code[BT_ISO_BROADCAST_CODE_SIZE]; 139 }; 140 #endif /* CONFIG_BT_BAP_BROADCAST_ASSISTANT */ 141 142 struct bt_cap_commander_proc_param { 143 struct bt_conn *conn; 144 union { 145 #if defined(CONFIG_BT_VCP_VOL_CTLR) 146 struct { 147 uint8_t volume; 148 } change_volume; 149 struct { 150 bool mute; 151 } change_vol_mute; 152 #endif /* CONFIG_BT_VCP_VOL_CTLR */ 153 #if defined(CONFIG_BT_VCP_VOL_CTLR_VOCS) 154 struct { 155 int16_t offset; 156 struct bt_vocs *vocs; 157 } change_offset; 158 #endif /* CONFIG_BT_VCP_VOL_CTLR_VOCS */ 159 #if defined(CONFIG_BT_BAP_BROADCAST_ASSISTANT) 160 struct cap_broadcast_reception_start broadcast_reception_start; 161 struct cap_broadcast_reception_stop broadcast_reception_stop; 162 struct cap_distribute_broadcast_code distribute_broadcast_code; 163 #endif /* CONFIG_BT_BAP_BROADCAST_ASSISTANT */ 164 #if defined(CONFIG_BT_MICP_MIC_CTLR) 165 struct { 166 bool mute; 167 } change_mic_mute; 168 #if defined(CONFIG_BT_MICP_MIC_CTLR_AICS) 169 struct { 170 int8_t gain; 171 struct bt_aics *aics; 172 } change_gain; 173 #endif /* CONFIG_BT_MICP_MIC_CTLR_AICS */ 174 #endif /* CONFIG_BT_MICP_MIC_CTLR */ 175 }; 176 }; 177 178 #if defined(CONFIG_BT_CAP_HANDOVER) 179 struct bt_cap_handover_proc_param { 180 union { 181 struct { 182 /* Struct containing the converted unicast group configuration */ 183 struct bt_cap_initiator_broadcast_create_param *broadcast_create_param; 184 /* The resulting broadcast source */ 185 struct bt_cap_broadcast_source *broadcast_source; 186 /* The advertising set to start the source on */ 187 struct bt_le_ext_adv *ext_adv; 188 /* The source unicast group with the streams. */ 189 struct bt_cap_unicast_group *unicast_group; 190 /* Set type */ 191 enum bt_cap_set_type type; 192 193 /* The SID of the ext_adv */ 194 uint8_t sid; 195 /* The PA interval of the ext_adv */ 196 uint16_t pa_interval; 197 /* The broadcast ID the broadcast source will use */ 198 uint32_t broadcast_id; 199 200 struct bt_cap_commander_broadcast_reception_start_member_param 201 reception_start_member_params[CONFIG_BT_MAX_CONN]; 202 } unicast_to_broadcast; 203 /* TODO: Add unicast broadcast_to_unicast params */ 204 }; 205 }; 206 #endif /* CONFIG_BT_CAP_HANDOVER */ 207 208 typedef void (*bt_cap_common_discover_func_t)( 209 struct bt_conn *conn, int err, const struct bt_csip_set_coordinator_set_member *member, 210 const struct bt_csip_set_coordinator_csis_inst *csis_inst); 211 212 struct bt_cap_common_proc_param { 213 union { 214 #if defined(CONFIG_BT_CAP_INITIATOR_UNICAST) 215 struct bt_cap_initiator_proc_param 216 initiator[CONFIG_BT_BAP_UNICAST_CLIENT_GROUP_STREAM_COUNT]; 217 #endif /* CONFIG_BT_CAP_INITIATOR_UNICAST */ 218 #if defined(CONFIG_BT_CAP_COMMANDER) 219 struct bt_cap_commander_proc_param commander[CONFIG_BT_MAX_CONN]; 220 #endif /* CONFIG_BT_CAP_COMMANDER */ 221 }; 222 #if defined(CONFIG_BT_CAP_HANDOVER) 223 /* The handover parameters cannot be part of the union as they need to exist while we are 224 * performing CAP initiator and CAP Commander procedures 225 */ 226 struct bt_cap_handover_proc_param handover; 227 #endif /* CONFIG_BT_CAP_HANDOVER */ 228 }; 229 230 struct bt_cap_common_proc { 231 ATOMIC_DEFINE(proc_state_flags, BT_CAP_COMMON_PROC_STATE_FLAG_NUM); 232 /* Total number of items (streams or connections) in the procedure*/ 233 size_t proc_cnt; 234 /* Number of items where a subprocedure have been started */ 235 size_t proc_initiated_cnt; 236 /* Number of items done with the procedure */ 237 size_t proc_done_cnt; 238 enum bt_cap_common_proc_type proc_type; 239 int err; 240 struct bt_conn *failed_conn; 241 struct bt_cap_common_proc_param proc_param; 242 #if defined(CONFIG_BT_CAP_INITIATOR_UNICAST) 243 enum bt_cap_common_subproc_type subproc_type; 244 #endif /* CONFIG_BT_CAP_INITIATOR_UNICAST */ 245 }; 246 247 struct bt_cap_common_client { 248 struct bt_conn *conn; 249 struct bt_gatt_discover_params param; 250 bt_cap_common_discover_func_t discover_cb_func; 251 uint16_t csis_start_handle; 252 const struct bt_csip_set_coordinator_csis_inst *csis_inst; 253 }; 254 255 struct bt_cap_common_proc *bt_cap_common_get_active_proc(void); 256 void bt_cap_common_clear_active_proc(void); 257 void bt_cap_common_set_proc(enum bt_cap_common_proc_type proc_type, size_t proc_cnt); 258 void bt_cap_common_set_subproc(enum bt_cap_common_subproc_type subproc_type); 259 void bt_cap_common_set_handover_active(void); 260 bool bt_cap_common_handover_is_active(void); 261 bool bt_cap_common_proc_is_type(enum bt_cap_common_proc_type proc_type); 262 bool bt_cap_common_subproc_is_type(enum bt_cap_common_subproc_type subproc_type); 263 struct bt_conn *bt_cap_common_get_member_conn(enum bt_cap_set_type type, 264 const union bt_cap_set_member *member); 265 bool bt_cap_common_test_and_set_proc_active(void); 266 bool bt_cap_common_proc_is_active(void); 267 bool bt_cap_common_proc_is_aborted(void); 268 bool bt_cap_common_proc_all_handled(void); 269 bool bt_cap_common_proc_is_done(void); 270 void bt_cap_common_abort_proc(struct bt_conn *conn, int err); 271 bool bt_cap_common_conn_in_active_proc(const struct bt_conn *conn); 272 bool bt_cap_common_stream_in_active_proc(const struct bt_cap_stream *cap_stream); 273 void bt_cap_common_disconnected(struct bt_conn *conn, uint8_t reason); 274 struct bt_cap_common_client *bt_cap_common_get_client_by_acl(const struct bt_conn *acl); 275 struct bt_cap_common_client * 276 bt_cap_common_get_client_by_csis(const struct bt_csip_set_coordinator_csis_inst *csis_inst); 277 int bt_cap_common_discover(struct bt_conn *conn, bt_cap_common_discover_func_t func); 278 279 bool bt_cap_initiator_broadcast_audio_start_valid_param( 280 const struct bt_cap_initiator_broadcast_create_param *param); 281 bool bt_cap_initiator_valid_unicast_audio_stop_param( 282 const struct bt_cap_unicast_audio_stop_param *param); 283 int cap_initiator_unicast_audio_stop(const struct bt_cap_unicast_audio_stop_param *param); 284 285 int cap_commander_broadcast_reception_start( 286 const struct bt_cap_commander_broadcast_reception_start_param *param); 287 288 void bt_cap_handover_proc_complete(void); 289 bool bt_cap_handover_is_handover_broadcast_source( 290 const struct bt_cap_broadcast_source *cap_broadcast_source); 291 void bt_cap_handover_unicast_to_broadcast_setup_broadcast(void); 292 void bt_cap_handover_unicast_to_broadcast_reception_start(void); 293 void bt_cap_handover_unicast_audio_stopped(void); 294 void bt_cap_handover_broadcast_source_stopped(uint8_t reason); 295