1 /** @file
2  *  @brief Header for Bluetooth BAP.
3  *
4  * Copyright (c) 2020 Bose Corporation
5  * Copyright (c) 2021-2023 Nordic Semiconductor ASA
6  *
7  * SPDX-License-Identifier: Apache-2.0
8  */
9 
10 #ifndef ZEPHYR_INCLUDE_BLUETOOTH_AUDIO_BAP_
11 #define ZEPHYR_INCLUDE_BLUETOOTH_AUDIO_BAP_
12 
13 /**
14  * @brief Bluetooth Basic Audio Profile (BAP)
15  * @defgroup bt_bap Bluetooth Basic Audio Profile
16  * @ingroup bluetooth
17  * @{
18  */
19 
20 #include <zephyr/bluetooth/conn.h>
21 #include <zephyr/bluetooth/audio/audio.h>
22 
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 
29 #if defined(CONFIG_BT_BAP_SCAN_DELEGATOR)
30 #define BT_BAP_SCAN_DELEGATOR_MAX_METADATA_LEN CONFIG_BT_BAP_SCAN_DELEGATOR_MAX_METADATA_LEN
31 #define BT_BAP_SCAN_DELEGATOR_MAX_SUBGROUPS    CONFIG_BT_BAP_SCAN_DELEGATOR_MAX_SUBGROUPS
32 #else
33 #define BT_BAP_SCAN_DELEGATOR_MAX_METADATA_LEN 0
34 #define BT_BAP_SCAN_DELEGATOR_MAX_SUBGROUPS    0
35 #endif
36 
37 /** The minimum size of a Broadcast Audio Source Endpoint (BASE)
38  * 2 octets UUID
39  * 3 octets presentation delay
40  * 1 octet number of subgroups (which is minimum 1)
41  * 1 octet number of BIS (which is minimum 1)
42  * 5 octets codec_id
43  * 1 octet codec configuration length (which may be 0)
44  * 1 octet metadata length (which may be 0)
45  * 1 octet BIS index
46  * 1 octet BIS specific codec configuration length (which may be 0)
47  */
48 #define BT_BAP_BASE_MIN_SIZE 16
49 
50 /** The minimum size of a bt_bap_base_bis_data */
51 #define BT_BAP_BASE_BIS_DATA_MIN_SIZE 2 /* index and length */
52 
53 /** Periodic advertising state reported by the Scan Delegator */
54 enum bt_bap_pa_state {
55 	/** The periodic advertising has not been synchronized */
56 	BT_BAP_PA_STATE_NOT_SYNCED = 0x00,
57 
58 	/** Waiting for SyncInfo from Broadcast Assistant */
59 	BT_BAP_PA_STATE_INFO_REQ = 0x01,
60 
61 	/** Synchronized to periodic advertising */
62 	BT_BAP_PA_STATE_SYNCED = 0x02,
63 
64 	/** Failed to synchronized to periodic advertising */
65 	BT_BAP_PA_STATE_FAILED = 0x03,
66 
67 	/** No periodic advertising sync transfer receiver from Broadcast Assistant */
68 	BT_BAP_PA_STATE_NO_PAST = 0x04,
69 };
70 
71 /** Broadcast Isochronous Group encryption state reported by the Scan Delegator */
72 enum bt_bap_big_enc_state {
73 	/** The Broadcast Isochronous Group not encrypted */
74 	BT_BAP_BIG_ENC_STATE_NO_ENC = 0x00,
75 
76 	/** The Broadcast Isochronous Group broadcast code requested */
77 	BT_BAP_BIG_ENC_STATE_BCODE_REQ = 0x01,
78 
79 	/** The Broadcast Isochronous Group decrypted */
80 	BT_BAP_BIG_ENC_STATE_DEC = 0x02,
81 
82 	/** The Broadcast Isochronous Group bad broadcast code */
83 	BT_BAP_BIG_ENC_STATE_BAD_CODE = 0x03,
84 };
85 
86 /** Broadcast Audio Scan Service (BASS) specific ATT error codes */
87 enum bt_bap_bass_att_err {
88 	/** Opcode not supported */
89 	BT_BAP_BASS_ERR_OPCODE_NOT_SUPPORTED = 0x80,
90 
91 	/** Invalid source ID supplied */
92 	BT_BAP_BASS_ERR_INVALID_SRC_ID = 0x81,
93 };
94 
95 /** Value indicating that the periodic advertising interval is unknown */
96 #define BT_BAP_PA_INTERVAL_UNKNOWN             0xFFFF
97 
98 /** @brief Broadcast Assistant no BIS sync preference
99  *
100  * Value indicating that the Broadcast Assistant has no preference to which BIS
101  * the Scan Delegator syncs to
102  */
103 #define BT_BAP_BIS_SYNC_NO_PREF                0xFFFFFFFF
104 
105 #if defined(CONFIG_BT_BAP_BROADCAST_SINK)
106 /* TODO: Since these are also used for the broadcast assistant,
107  * they should not be tied to the broadcast sink
108  */
109 #define BROADCAST_SNK_STREAM_CNT   CONFIG_BT_BAP_BROADCAST_SNK_STREAM_COUNT
110 #define BROADCAST_SNK_SUBGROUP_CNT CONFIG_BT_BAP_BROADCAST_SNK_SUBGROUP_COUNT
111 #else /* !CONFIG_BT_BAP_BROADCAST_SINK */
112 #define BROADCAST_SNK_STREAM_CNT   0
113 #define BROADCAST_SNK_SUBGROUP_CNT 0
114 #endif /* CONFIG_BT_BAP_BROADCAST_SINK*/
115 
116 /** Endpoint states */
117 enum bt_bap_ep_state {
118 	/** Audio Stream Endpoint Idle state */
119 	BT_BAP_EP_STATE_IDLE = 0x00,
120 
121 	/** Audio Stream Endpoint Codec Configured state */
122 	BT_BAP_EP_STATE_CODEC_CONFIGURED = 0x01,
123 
124 	/** Audio Stream Endpoint QoS Configured state */
125 	BT_BAP_EP_STATE_QOS_CONFIGURED = 0x02,
126 
127 	/** Audio Stream Endpoint Enabling state */
128 	BT_BAP_EP_STATE_ENABLING = 0x03,
129 
130 	/** Audio Stream Endpoint Streaming state */
131 	BT_BAP_EP_STATE_STREAMING = 0x04,
132 
133 	/** Audio Stream Endpoint Disabling state */
134 	BT_BAP_EP_STATE_DISABLING = 0x05,
135 
136 	/** Audio Stream Endpoint Streaming state */
137 	BT_BAP_EP_STATE_RELEASING = 0x06,
138 };
139 
140 /**
141  * @brief Response Status Code
142  *
143  * These are sent by the server to the client when a stream operation is
144  * requested.
145  */
146 enum bt_bap_ascs_rsp_code {
147 	/** Server completed operation successfully */
148 	BT_BAP_ASCS_RSP_CODE_SUCCESS = 0x00,
149 	/** Server did not support operation by client */
150 	BT_BAP_ASCS_RSP_CODE_NOT_SUPPORTED = 0x01,
151 	/** Server rejected due to invalid operation length */
152 	BT_BAP_ASCS_RSP_CODE_INVALID_LENGTH = 0x02,
153 	/** Invalid ASE ID */
154 	BT_BAP_ASCS_RSP_CODE_INVALID_ASE = 0x03,
155 	/** Invalid ASE state */
156 	BT_BAP_ASCS_RSP_CODE_INVALID_ASE_STATE = 0x04,
157 	/** Invalid operation for direction */
158 	BT_BAP_ASCS_RSP_CODE_INVALID_DIR = 0x05,
159 	/** Capabilities not supported by server */
160 	BT_BAP_ASCS_RSP_CODE_CAP_UNSUPPORTED = 0x06,
161 	/** Configuration parameters not supported by server */
162 	BT_BAP_ASCS_RSP_CODE_CONF_UNSUPPORTED = 0x07,
163 	/** Configuration parameters rejected by server */
164 	BT_BAP_ASCS_RSP_CODE_CONF_REJECTED = 0x08,
165 	/** Invalid Configuration parameters */
166 	BT_BAP_ASCS_RSP_CODE_CONF_INVALID = 0x09,
167 	/** Unsupported metadata */
168 	BT_BAP_ASCS_RSP_CODE_METADATA_UNSUPPORTED = 0x0a,
169 	/** Metadata rejected by server */
170 	BT_BAP_ASCS_RSP_CODE_METADATA_REJECTED = 0x0b,
171 	/** Invalid metadata */
172 	BT_BAP_ASCS_RSP_CODE_METADATA_INVALID = 0x0c,
173 	/** Server has insufficient resources */
174 	BT_BAP_ASCS_RSP_CODE_NO_MEM = 0x0d,
175 	/** Unspecified error */
176 	BT_BAP_ASCS_RSP_CODE_UNSPECIFIED = 0x0e,
177 };
178 
179 /**
180  * @brief Response Reasons
181  *
182  * These are used if the @ref bt_bap_ascs_rsp_code value is
183  * @ref BT_BAP_ASCS_RSP_CODE_CONF_UNSUPPORTED, @ref BT_BAP_ASCS_RSP_CODE_CONF_REJECTED or
184  * @ref BT_BAP_ASCS_RSP_CODE_CONF_INVALID.
185  */
186 enum bt_bap_ascs_reason {
187 	/** No reason */
188 	BT_BAP_ASCS_REASON_NONE = 0x00,
189 	/** Codec ID */
190 	BT_BAP_ASCS_REASON_CODEC = 0x01,
191 	/** Codec configuration */
192 	BT_BAP_ASCS_REASON_CODEC_DATA = 0x02,
193 	/** SDU interval */
194 	BT_BAP_ASCS_REASON_INTERVAL = 0x03,
195 	/** Framing */
196 	BT_BAP_ASCS_REASON_FRAMING = 0x04,
197 	/** PHY */
198 	BT_BAP_ASCS_REASON_PHY = 0x05,
199 	/** Maximum SDU size*/
200 	BT_BAP_ASCS_REASON_SDU = 0x06,
201 	/** RTN */
202 	BT_BAP_ASCS_REASON_RTN = 0x07,
203 	/** Max transport latency */
204 	BT_BAP_ASCS_REASON_LATENCY = 0x08,
205 	/** Presendation delay */
206 	BT_BAP_ASCS_REASON_PD = 0x09,
207 	/** Invalid CIS mapping */
208 	BT_BAP_ASCS_REASON_CIS = 0x0a,
209 };
210 
211 /** @brief Structure storing values of fields of ASE Control Point notification. */
212 struct bt_bap_ascs_rsp {
213 	/** @brief Value of the Response Code field. */
214 	enum bt_bap_ascs_rsp_code code;
215 
216 	/**
217 	 * @brief Value of the Reason field.
218 	 *
219 	 * The meaning of this value depend on the Response Code field.
220 	 */
221 	union {
222 		/**
223 		 * @brief Response reason
224 		 *
225 		 * If the Response Code is one of the following:
226 		 * - @ref BT_BAP_ASCS_RSP_CODE_CONF_UNSUPPORTED
227 		 * - @ref BT_BAP_ASCS_RSP_CODE_CONF_REJECTED
228 		 * - @ref BT_BAP_ASCS_RSP_CODE_CONF_INVALID
229 		 * all values from @ref bt_bap_ascs_reason can be used.
230 		 *
231 		 * If the Response Code is one of the following:
232 		 * - @ref BT_BAP_ASCS_RSP_CODE_SUCCESS
233 		 * - @ref BT_BAP_ASCS_RSP_CODE_NOT_SUPPORTED
234 		 * - @ref BT_BAP_ASCS_RSP_CODE_INVALID_LENGTH
235 		 * - @ref BT_BAP_ASCS_RSP_CODE_INVALID_ASE
236 		 * - @ref BT_BAP_ASCS_RSP_CODE_INVALID_ASE_STATE
237 		 * - @ref BT_BAP_ASCS_RSP_CODE_INVALID_DIR
238 		 * - @ref BT_BAP_ASCS_RSP_CODE_CAP_UNSUPPORTED
239 		 * - @ref BT_BAP_ASCS_RSP_CODE_NO_MEM
240 		 * - @ref BT_BAP_ASCS_RSP_CODE_UNSPECIFIED
241 		 * only value @ref BT_BAP_ASCS_REASON_NONE shall be used.
242 		 */
243 		enum bt_bap_ascs_reason reason;
244 
245 		/**
246 		 * @brief Response metadata type
247 		 *
248 		 * If the Response Code is one of the following:
249 		 * - @ref BT_BAP_ASCS_RSP_CODE_METADATA_UNSUPPORTED
250 		 * - @ref BT_BAP_ASCS_RSP_CODE_METADATA_REJECTED
251 		 * - @ref BT_BAP_ASCS_RSP_CODE_METADATA_INVALID
252 		 * the value of the Metadata Type shall be used.
253 		 */
254 		enum bt_audio_metadata_type metadata_type;
255 	};
256 };
257 
258 /**
259  * @brief Macro used to initialise the object storing values of ASE Control Point notification.
260  *
261  * @param c Response Code field
262  * @param r Reason field - @ref bt_bap_ascs_reason or @ref bt_audio_metadata_type (see notes in
263  *          @ref bt_bap_ascs_rsp).
264  */
265 #define BT_BAP_ASCS_RSP(c, r) (struct bt_bap_ascs_rsp) { .code = c, .reason = r }
266 
267 /** @brief Abstract Audio Broadcast Source structure. */
268 struct bt_bap_broadcast_source;
269 
270 /** @brief Abstract Audio Broadcast Sink structure. */
271 struct bt_bap_broadcast_sink;
272 
273 /** @brief Abstract Audio Unicast Group structure. */
274 struct bt_bap_unicast_group;
275 
276 /** @brief Abstract Audio Endpoint structure. */
277 struct bt_bap_ep;
278 
279 /* TODO: Replace with struct bt_bap_base_subgroup */
280 /** Struct to hold subgroup specific information for the receive state */
281 struct bt_bap_scan_delegator_subgroup {
282 	/** BIS synced bitfield */
283 	uint32_t bis_sync;
284 
285 	/** Length of the metadata */
286 	uint8_t metadata_len;
287 
288 	/** The metadata */
289 	uint8_t metadata[BT_BAP_SCAN_DELEGATOR_MAX_METADATA_LEN];
290 };
291 
292 /** Represents the Broadcast Audio Scan Service receive state */
293 struct bt_bap_scan_delegator_recv_state {
294 	/** The source ID  */
295 	uint8_t src_id;
296 
297 	/** The Bluetooth address */
298 	bt_addr_le_t addr;
299 
300 	/** The advertising set ID*/
301 	uint8_t adv_sid;
302 
303 	/** The periodic adverting sync state */
304 	enum bt_bap_pa_state pa_sync_state;
305 
306 	/** The broadcast isochronous group encryption state */
307 	enum bt_bap_big_enc_state encrypt_state;
308 
309 	/** The 24-bit broadcast ID */
310 	uint32_t broadcast_id;
311 
312 	/** @brief The bad broadcast code
313 	 *
314 	 * Only valid if encrypt_state is @ref BT_BAP_BIG_ENC_STATE_BCODE_REQ
315 	 */
316 	uint8_t bad_code[BT_AUDIO_BROADCAST_CODE_SIZE];
317 
318 	/** Number of subgroups */
319 	uint8_t num_subgroups;
320 
321 	/** Subgroup specific information */
322 	struct bt_bap_scan_delegator_subgroup subgroups[BT_BAP_SCAN_DELEGATOR_MAX_SUBGROUPS];
323 };
324 
325 struct bt_bap_scan_delegator_cb {
326 	/**
327 	 * @brief Receive state updated
328 	 *
329 	 * @param conn       Pointer to the connection to a remote device if
330 	 *                   the change was caused by it, otherwise NULL.
331 	 * @param recv_state Pointer to the receive state that was updated.
332 	 *
333 	 * @return 0 in case of success or negative value in case of error.
334 	 */
335 	void (*recv_state_updated)(struct bt_conn *conn,
336 				   const struct bt_bap_scan_delegator_recv_state *recv_state);
337 
338 	/**
339 	 * @brief Periodic advertising sync request
340 	 *
341 	 * Request from peer device to synchronize with the periodic advertiser
342 	 * denoted by the @p recv_state. To notify the Broadcast Assistant about
343 	 * any pending sync
344 	 *
345 	 * @param[in]  conn        Pointer to the connection requesting the
346 	 *                         periodic advertising sync.
347 	 * @param[in]  recv_state  Pointer to the receive state that is being
348 	 *                         requested for periodic advertising sync.
349 	 * @param[in]  past_avail  True if periodic advertising sync transfer is
350 	 *                         available.
351 	 * @param[in]  pa_interval The periodic advertising interval.
352 	 * @param[out] past_sync   Set to true if syncing via periodic
353 	 *                         advertising sync transfer, false otherwise.
354 	 *                         If @p past_avail is false, this value is
355 	 *                         ignored.
356 	 *
357 	 * @return 0 in case of accept, or other value to reject.
358 	 */
359 	int (*pa_sync_req)(struct bt_conn *conn,
360 			   const struct bt_bap_scan_delegator_recv_state *recv_state,
361 			   bool past_avail, uint16_t pa_interval);
362 
363 	/**
364 	 * @brief Periodic advertising sync termination request
365 	 *
366 	 * Request from peer device to terminate the periodic advertiser sync
367 	 * denoted by the @p recv_state.
368 	 *
369 	 * @param conn        Pointer to the connection requesting the periodic
370 	 *                    advertising sync termination.
371 	 * @param recv_state  Pointer to the receive state that is being
372 	 *                    requested for periodic advertising sync.
373 	 *
374 	 * @return 0 in case of success or negative value in case of error.
375 	 */
376 	int (*pa_sync_term_req)(struct bt_conn *conn,
377 				const struct bt_bap_scan_delegator_recv_state *recv_state);
378 
379 	/**
380 	 * @brief Broadcast code received
381 	 *
382 	 * Broadcast code received from a broadcast assistant
383 	 *
384 	 * @param conn           Pointer to the connection providing the
385 	 *                       broadcast code.
386 	 * @param recv_state     Pointer to the receive state the broadcast code
387 	 *                       is being provided for.
388 	 * @param broadcast_code The 16-octet broadcast code
389 	 */
390 	void (*broadcast_code)(struct bt_conn *conn,
391 			       const struct bt_bap_scan_delegator_recv_state *recv_state,
392 			       const uint8_t broadcast_code[BT_AUDIO_BROADCAST_CODE_SIZE]);
393 
394 	/**
395 	 * @brief Broadcast Isochronous Stream synchronize request
396 	 *
397 	 * Request from Broadcast Assistant device to modify the Broadcast
398 	 * Isochronous Stream states. The request shall be fulfilled with
399 	 * accordance to the @p bis_sync_req within reasonable time. The
400 	 * Broadcast Assistant may also request fewer, or none, indexes to
401 	 * be synchronized.
402 	 *
403 	 * @param[in]  conn          Pointer to the connection of the
404 	 *                           Broadcast Assistant requesting the sync.
405 	 * @param[in]  recv_state    Pointer to the receive state that is being
406 	 *                           requested for the sync.
407 	 * @param[in]  bis_sync_req  Array of bitfields of which BIS indexes
408 	 *                           that is requested to sync for each subgroup
409 	 *                           by the Broadcast Assistant.
410 	 *
411 	 * @return 0 in case of accept, or other value to reject.
412 	 */
413 	int (*bis_sync_req)(struct bt_conn *conn,
414 			    const struct bt_bap_scan_delegator_recv_state *recv_state,
415 			    const uint32_t bis_sync_req[BT_BAP_SCAN_DELEGATOR_MAX_SUBGROUPS]);
416 };
417 
418 /** Structure holding information of audio stream endpoint */
419 struct bt_bap_ep_info {
420 	/** The ID of the endpoint */
421 	uint8_t id;
422 
423 	/** The state of the endpoint */
424 	enum bt_bap_ep_state state;
425 
426 	/** Capabilities type */
427 	enum bt_audio_dir dir;
428 
429 	/** @brief True if the stream associated with the endpoint is able to send data */
430 	bool can_send;
431 
432 	/** Pointer to paired endpoint if the endpoint is part of a bidirectional CIS,
433 	 *  otherwise NULL
434 	 */
435 	struct bt_bap_ep *paired_ep;
436 };
437 
438 /**
439  * @brief Return structure holding information of audio stream endpoint
440  *
441  * @param ep   The audio stream endpoint object.
442  * @param info The structure object to be filled with the info.
443  *
444  * @return 0 in case of success or negative value in case of error.
445  */
446 int bt_bap_ep_get_info(const struct bt_bap_ep *ep, struct bt_bap_ep_info *info);
447 
448 /**
449  * @brief Basic Audio Profile stream structure.
450  *
451  * Streams represents a stream configuration of a Remote Endpoint and a Local Capability.
452  *
453  * @note Streams are unidirectional but can be paired with other streams to use a bidirectional
454  * connected isochronous stream.
455  */
456 struct bt_bap_stream {
457 	/** Connection reference */
458 	struct bt_conn *conn;
459 
460 	/** Endpoint reference */
461 	struct bt_bap_ep *ep;
462 
463 	/** Codec Configuration */
464 	struct bt_audio_codec_cfg *codec_cfg;
465 
466 	/** QoS Configuration */
467 	struct bt_audio_codec_qos *qos;
468 
469 	/** Audio stream operations */
470 	struct bt_bap_stream_ops *ops;
471 
472 #if defined(CONFIG_BT_BAP_UNICAST_CLIENT)
473 	/**
474 	 * @brief Audio ISO reference
475 	 *
476 	 * This is only used for Unicast Client streams, and is handled internally.
477 	 */
478 	struct bt_bap_iso *bap_iso;
479 #endif /* CONFIG_BT_BAP_UNICAST_CLIENT */
480 
481 	/** Unicast or Broadcast group - Used internally */
482 	void *group;
483 
484 	/** Stream user data */
485 	void *user_data;
486 
487 	/* Internally used list node */
488 	sys_snode_t _node;
489 };
490 
491 /** @brief Stream operation. */
492 struct bt_bap_stream_ops {
493 #if defined(CONFIG_BT_BAP_UNICAST)
494 	/**
495 	 * @brief Stream configured callback
496 	 *
497 	 * Configured callback is called whenever an Audio Stream has been configured.
498 	 *
499 	 * @param stream Stream object that has been configured.
500 	 * @param pref   Remote QoS preferences.
501 	 */
502 	void (*configured)(struct bt_bap_stream *stream,
503 			   const struct bt_audio_codec_qos_pref *pref);
504 
505 	/**
506 	 * @brief Stream QoS set callback
507 	 *
508 	 * QoS set callback is called whenever an Audio Stream Quality of Service has been set or
509 	 * updated.
510 	 *
511 	 * @param stream Stream object that had its QoS updated.
512 	 */
513 	void (*qos_set)(struct bt_bap_stream *stream);
514 
515 	/**
516 	 * @brief Stream enabled callback
517 	 *
518 	 * Enabled callback is called whenever an Audio Stream has been enabled.
519 	 *
520 	 * @param stream Stream object that has been enabled.
521 	 */
522 	void (*enabled)(struct bt_bap_stream *stream);
523 
524 	/**
525 	 * @brief Stream metadata updated callback
526 	 *
527 	 * Metadata Updated callback is called whenever an Audio Stream's metadata has been
528 	 * updated.
529 	 *
530 	 *  @param stream Stream object that had its metadata updated.
531 	 */
532 	void (*metadata_updated)(struct bt_bap_stream *stream);
533 
534 	/**
535 	 * @brief Stream disabled callback
536 	 *
537 	 * Disabled callback is called whenever an Audio Stream has been disabled.
538 	 *
539 	 *  @param stream Stream object that has been disabled.
540 	 */
541 	void (*disabled)(struct bt_bap_stream *stream);
542 
543 	/**
544 	 * @brief Stream released callback
545 	 *
546 	 * Released callback is called whenever a Audio Stream has been released and can be
547 	 * deallocated.
548 	 *
549 	 * @param stream Stream object that has been released.
550 	 */
551 	void (*released)(struct bt_bap_stream *stream);
552 #endif /* CONFIG_BT_BAP_UNICAST */
553 
554 	/**
555 	 * @brief Stream started callback
556 	 *
557 	 * Started callback is called whenever an Audio Stream has been started
558 	 * and will be usable for streaming.
559 	 *
560 	 * @param stream Stream object that has been started.
561 	 */
562 	void (*started)(struct bt_bap_stream *stream);
563 
564 	/**
565 	 * @brief Stream stopped callback
566 	 *
567 	 * Stopped callback is called whenever an Audio Stream has been stopped.
568 	 *
569 	 * @param stream Stream object that has been stopped.
570 	 * @param reason BT_HCI_ERR_* reason for the disconnection.
571 	 */
572 	void (*stopped)(struct bt_bap_stream *stream, uint8_t reason);
573 
574 #if defined(CONFIG_BT_AUDIO_RX)
575 	/**
576 	 * @brief Stream audio HCI receive callback.
577 	 *
578 	 * This callback is only used if the ISO data path is HCI.
579 	 *
580 	 * @param stream Stream object.
581 	 * @param info   Pointer to the metadata for the buffer. The lifetime of the pointer is
582 	 *               linked to the lifetime of the net_buf. Metadata such as sequence number and
583 	 *               timestamp can be provided by the bluetooth controller.
584 	 * @param buf    Buffer containing incoming audio data.
585 	 */
586 	void (*recv)(struct bt_bap_stream *stream, const struct bt_iso_recv_info *info,
587 		     struct net_buf *buf);
588 #endif /* CONFIG_BT_AUDIO_RX */
589 
590 #if defined(CONFIG_BT_AUDIO_TX)
591 	/**
592 	 * @brief Stream audio HCI sent callback
593 	 *
594 	 * If this callback is provided it will be called whenever a SDU has been completely sent,
595 	 * or otherwise flushed due to transmission issues.
596 	 *
597 	 * This callback is only used if the ISO data path is HCI.
598 	 *
599 	 * @param chan The channel which has sent data.
600 	 */
601 	void (*sent)(struct bt_bap_stream *stream);
602 #endif /* CONFIG_BT_AUDIO_TX */
603 };
604 
605 /**
606  * @brief Register Audio callbacks for a stream.
607  *
608  * Register Audio callbacks for a stream.
609  *
610  * @param stream Stream object.
611  * @param ops    Stream operations structure.
612  */
613 void bt_bap_stream_cb_register(struct bt_bap_stream *stream, struct bt_bap_stream_ops *ops);
614 
615 /**
616  * @brief Configure Audio Stream
617  *
618  * This procedure is used by a client to configure a new stream using the
619  * remote endpoint, local capability and codec configuration.
620  *
621  * @param conn Connection object
622  * @param stream Stream object being configured
623  * @param ep Remote Audio Endpoint being configured
624  * @param codec_cfg Codec configuration
625  *
626  * @return Allocated Audio Stream object or NULL in case of error.
627  */
628 int bt_bap_stream_config(struct bt_conn *conn, struct bt_bap_stream *stream, struct bt_bap_ep *ep,
629 			 struct bt_audio_codec_cfg *codec_cfg);
630 
631 /**
632  * @brief Reconfigure Audio Stream
633  *
634  * This procedure is used by a unicast client or unicast server to reconfigure
635  * a stream to use a different local codec configuration.
636  *
637  * This can only be done for unicast streams.
638  *
639  * @param stream Stream object being reconfigured
640  * @param codec_cfg Codec configuration
641  *
642  * @return 0 in case of success or negative value in case of error.
643  */
644 int bt_bap_stream_reconfig(struct bt_bap_stream *stream, struct bt_audio_codec_cfg *codec_cfg);
645 
646 /**
647  * @brief Configure Audio Stream QoS
648  *
649  * This procedure is used by a client to configure the Quality of Service of streams in a unicast
650  * group. All streams in the group for the specified @p conn will have the Quality of Service
651  * configured. This shall only be used to configure unicast streams.
652  *
653  * @param conn  Connection object
654  * @param group Unicast group object
655  *
656  * @return 0 in case of success or negative value in case of error.
657  */
658 int bt_bap_stream_qos(struct bt_conn *conn, struct bt_bap_unicast_group *group);
659 
660 /**
661  * @brief Enable Audio Stream
662  *
663  * This procedure is used by a client to enable a stream.
664  *
665  * This shall only be called for unicast streams, as broadcast streams will always be enabled once
666  * created.
667  *
668  * @param stream Stream object
669  * @param meta Metadata
670  * @param meta_len Metadata length
671  *
672  * @return 0 in case of success or negative value in case of error.
673  */
674 int bt_bap_stream_enable(struct bt_bap_stream *stream, const uint8_t meta[], size_t meta_len);
675 
676 /**
677  * @brief Change Audio Stream Metadata
678  *
679  * This procedure is used by a unicast client or unicast server to change the metadata of a stream.
680  *
681  * @param stream Stream object
682  * @param meta Metadata
683  * @param meta_len Metadata length
684  *
685  * @return 0 in case of success or negative value in case of error.
686  */
687 int bt_bap_stream_metadata(struct bt_bap_stream *stream, const uint8_t meta[], size_t meta_len);
688 
689 /**
690  * @brief Disable Audio Stream
691  *
692  * This procedure is used by a unicast client or unicast server to disable a stream.
693  *
694  * This shall only be called for unicast streams, as broadcast streams will
695  * always be enabled once created.
696  *
697  * @param stream Stream object
698  *
699  * @return 0 in case of success or negative value in case of error.
700  */
701 int bt_bap_stream_disable(struct bt_bap_stream *stream);
702 
703 /**
704  * @brief Start Audio Stream
705  *
706  * This procedure is used by a unicast client or unicast server to make a stream start streaming.
707  *
708  * For the unicast client, this will connect the CIS for the stream before
709  * sending the start command.
710  *
711  * For the unicast server, this will put a @ref BT_AUDIO_DIR_SINK stream into the streaming state if
712  * the CIS is connected (initialized by the unicast client). If the CIS is not connected yet, the
713  * stream will go into the streaming state as soon as the CIS is connected.
714  * @ref BT_AUDIO_DIR_SOURCE streams will go into the streaming state when the unicast client sends
715  * the Receiver Start Ready operation, which will trigger the @ref bt_bap_unicast_server_cb.start()
716  * callback.
717  *
718  * This shall only be called for unicast streams.
719  *
720  * Broadcast sinks will always be started once synchronized, and broadcast
721  * source streams shall be started with bt_bap_broadcast_source_start().
722  *
723  * @param stream Stream object
724  *
725  * @return 0 in case of success or negative value in case of error.
726  */
727 int bt_bap_stream_start(struct bt_bap_stream *stream);
728 
729 /**
730  * @brief Stop Audio Stream
731  *
732  * This procedure is used by a client to make a stream stop streaming.
733  *
734  * This shall only be called for unicast streams.
735  * Broadcast sinks cannot be stopped.
736  * Broadcast sources shall be stopped with bt_bap_broadcast_source_stop().
737  *
738  * @param stream Stream object
739  *
740  * @return 0 in case of success or negative value in case of error.
741  */
742 int bt_bap_stream_stop(struct bt_bap_stream *stream);
743 
744 /**
745  * @brief Release Audio Stream
746  *
747  * This procedure is used by a unicast client or unicast server to release a unicast stream.
748  *
749  * Broadcast sink streams cannot be released, but can be deleted by bt_bap_broadcast_sink_delete().
750  * Broadcast source streams cannot be released, but can be deleted by
751  * bt_bap_broadcast_source_delete().
752  *
753  * @param stream Stream object
754  *
755  * @return 0 in case of success or negative value in case of error.
756  */
757 int bt_bap_stream_release(struct bt_bap_stream *stream);
758 
759 /**
760  * @brief Send data to Audio stream
761  *
762  * Send data from buffer to the stream.
763  *
764  * @note Support for sending must be supported, determined by @kconfig{CONFIG_BT_AUDIO_TX}.
765  *
766  * @param stream   Stream object.
767  * @param buf      Buffer containing data to be sent.
768  * @param seq_num  Packet Sequence number. This value shall be incremented for each call to this
769  *                 function and at least once per SDU interval for a specific channel.
770  * @param ts       Timestamp of the SDU in microseconds (us). This value can be used to transmit
771  *                 multiple SDUs in the same SDU interval in a CIG or BIG. Can be omitted by using
772  *                 @ref BT_ISO_TIMESTAMP_NONE which will simply enqueue the ISO SDU in a FIFO
773  *                 manner.
774  *
775  * @return Bytes sent in case of success or negative value in case of error.
776  */
777 int bt_bap_stream_send(struct bt_bap_stream *stream, struct net_buf *buf, uint16_t seq_num,
778 		       uint32_t ts);
779 
780 /**
781  * @brief Get ISO transmission timing info for a Basic Audio Profile stream
782  *
783  * Reads timing information for transmitted ISO packet on an ISO channel.
784  * The HCI_LE_Read_ISO_TX_Sync HCI command is used to retrieve this information from the controller.
785  *
786  * @note An SDU must have already been successfully transmitted on the ISO channel
787  * for this function to return successfully.
788  * Support for sending must be supported, determined by @kconfig{CONFIG_BT_AUDIO_TX}.
789  *
790  * @param[in]  stream Stream object.
791  * @param[out] info   Transmit info object.
792  *
793  * @retval 0 on success
794  * @retval -EINVAL if the stream is invalid, if the stream is not configured for sending or if it is
795  *         not connected with a isochronous stream
796  * @retval Any return value from bt_iso_chan_get_tx_sync()
797  */
798 int bt_bap_stream_get_tx_sync(struct bt_bap_stream *stream, struct bt_iso_tx_info *info);
799 
800 /**
801  * @defgroup bt_bap_unicast_server BAP Unicast Server APIs
802  * @ingroup bt_bap
803  * @{
804  */
805 
806 /** Unicast Server callback structure */
807 struct bt_bap_unicast_server_cb {
808 	/**
809 	 * @brief Endpoint config request callback
810 	 *
811 	 * Config callback is called whenever an endpoint is requested to be
812 	 * configured
813 	 *
814 	 * @param[in]  conn      Connection object.
815 	 * @param[in]  ep        Local Audio Endpoint being configured.
816 	 * @param[in]  dir       Direction of the endpoint.
817 	 * @param[in]  codec_cfg Codec configuration.
818 	 * @param[out] stream    Pointer to stream that will be configured for the endpoint.
819 	 * @param[out] pref      Pointer to a QoS preference object that shall be populated with
820 	 *                       values. Invalid values will reject the codec configuration request.
821 	 * @param[out] rsp       Object for the ASE operation response. Only used if the return
822 	 *                       value is non-zero.
823 	 *
824 	 * @return 0 in case of success or negative value in case of error.
825 	 */
826 	int (*config)(struct bt_conn *conn, const struct bt_bap_ep *ep, enum bt_audio_dir dir,
827 		      const struct bt_audio_codec_cfg *codec_cfg, struct bt_bap_stream **stream,
828 		      struct bt_audio_codec_qos_pref *const pref, struct bt_bap_ascs_rsp *rsp);
829 
830 	/**
831 	 * @brief Stream reconfig request callback
832 	 *
833 	 * Reconfig callback is called whenever an Audio Stream needs to be
834 	 * reconfigured with different codec configuration.
835 	 *
836 	 * @param[in]  stream    Stream object being reconfigured.
837 	 * @param[in]  dir       Direction of the endpoint.
838 	 * @param[in]  codec_cfg Codec configuration.
839 	 * @param[out] pref      Pointer to a QoS preference object that shall be populated with
840 	 *                       values. Invalid values will reject the codec configuration request.
841 	 * @param[out] rsp       Object for the ASE operation response. Only used if the return
842 	 *                       value is non-zero.
843 	 *
844 	 * @return 0 in case of success or negative value in case of error.
845 	 */
846 	int (*reconfig)(struct bt_bap_stream *stream, enum bt_audio_dir dir,
847 			const struct bt_audio_codec_cfg *codec_cfg,
848 			struct bt_audio_codec_qos_pref *const pref, struct bt_bap_ascs_rsp *rsp);
849 
850 	/**
851 	 * @brief Stream QoS request callback
852 	 *
853 	 * QoS callback is called whenever an Audio Stream Quality of
854 	 * Service needs to be configured.
855 	 *
856 	 * @param[in]  stream  Stream object being reconfigured.
857 	 * @param[in]  qos     Quality of Service configuration.
858 	 * @param[out] rsp     Object for the ASE operation response. Only used if the return
859 	 *                     value is non-zero.
860 	 *
861 	 * @return 0 in case of success or negative value in case of error.
862 	 */
863 	int (*qos)(struct bt_bap_stream *stream, const struct bt_audio_codec_qos *qos,
864 		   struct bt_bap_ascs_rsp *rsp);
865 
866 	/**
867 	 * @brief Stream Enable request callback
868 	 *
869 	 * Enable callback is called whenever an Audio Stream is requested to be enabled to stream.
870 	 *
871 	 * @param[in]  stream      Stream object being enabled.
872 	 * @param[in]  meta        Metadata entries.
873 	 * @param[in]  meta_len    Length of metadata.
874 	 * @param[out] rsp         Object for the ASE operation response. Only used if the return
875 	 *                         value is non-zero.
876 	 *
877 	 * @return 0 in case of success or negative value in case of error.
878 	 */
879 	int (*enable)(struct bt_bap_stream *stream, const uint8_t meta[], size_t meta_len,
880 		      struct bt_bap_ascs_rsp *rsp);
881 
882 	/**
883 	 * @brief Stream Start request callback
884 	 *
885 	 * Start callback is called whenever an Audio Stream is requested to start streaming.
886 	 *
887 	 * @param[in]  stream Stream object.
888 	 * @param[out] rsp    Object for the ASE operation response. Only used if the return
889 	 *                    value is non-zero.
890 	 *
891 	 * @return 0 in case of success or negative value in case of error.
892 	 */
893 	int (*start)(struct bt_bap_stream *stream, struct bt_bap_ascs_rsp *rsp);
894 
895 	/**
896 	 * @brief Stream Metadata update request callback
897 	 *
898 	 * Metadata callback is called whenever an Audio Stream is requested to update its metadata.
899 	 *
900 	 * @param[in]  stream       Stream object.
901 	 * @param[in]  meta         Metadata entries.
902 	 * @param[in]  meta_len     Length of metadata.
903 	 * @param[out] rsp          Object for the ASE operation response. Only used if the return
904 	 *                          value is non-zero.
905 	 *
906 	 * @return 0 in case of success or negative value in case of error.
907 	 */
908 	int (*metadata)(struct bt_bap_stream *stream, const uint8_t meta[], size_t meta_len,
909 			struct bt_bap_ascs_rsp *rsp);
910 
911 	/**
912 	 * @brief Stream Disable request callback
913 	 *
914 	 * Disable callback is called whenever an Audio Stream is requested to disable the stream.
915 	 *
916 	 * @param[in]  stream Stream object being disabled.
917 	 * @param[out] rsp    Object for the ASE operation response. Only used if the return
918 	 *                    value is non-zero.
919 	 *
920 	 * @return 0 in case of success or negative value in case of error.
921 	 */
922 	int (*disable)(struct bt_bap_stream *stream, struct bt_bap_ascs_rsp *rsp);
923 
924 	/**
925 	 * @brief Stream Stop callback
926 	 *
927 	 * Stop callback is called whenever an Audio Stream is requested to stop streaming.
928 	 *
929 	 * @param[in]  stream Stream object.
930 	 * @param[out] rsp    Object for the ASE operation response. Only used if the return
931 	 *                    value is non-zero.
932 	 *
933 	 * @return 0 in case of success or negative value in case of error.
934 	 */
935 	int (*stop)(struct bt_bap_stream *stream, struct bt_bap_ascs_rsp *rsp);
936 
937 	/**
938 	 * @brief Stream release callback
939 	 *
940 	 * Release callback is called whenever a new Audio Stream needs to be released and thus
941 	 * deallocated.
942 	 *
943 	 * @param[in]  stream Stream object.
944 	 * @param[out] rsp    Object for the ASE operation response. Only used if the return
945 	 *                    value is non-zero.
946 	 *
947 	 * @return 0 in case of success or negative value in case of error.
948 	 */
949 	int (*release)(struct bt_bap_stream *stream, struct bt_bap_ascs_rsp *rsp);
950 };
951 
952 /**
953  * @brief Register unicast server callbacks.
954  *
955  * Only one callback structure can be registered, and attempting to
956  * registering more than one will result in an error.
957  *
958  * @param cb  Unicast server callback structure.
959  *
960  * @return 0 in case of success or negative value in case of error.
961  */
962 int bt_bap_unicast_server_register_cb(const struct bt_bap_unicast_server_cb *cb);
963 
964 /**
965  * @brief Unregister unicast server callbacks.
966  *
967  * May only unregister a callback structure that has previously been
968  * registered by bt_bap_unicast_server_register_cb().
969  *
970  * @param cb  Unicast server callback structure.
971  *
972  * @return 0 in case of success or negative value in case of error.
973  */
974 int bt_bap_unicast_server_unregister_cb(const struct bt_bap_unicast_server_cb *cb);
975 
976 /**
977  * @typedef bt_bap_ep_func_t
978  * @brief The callback function called for each endpoint.
979  *
980  * @param ep The structure object with endpoint info.
981  * @param user_data Data to pass to the function.
982  */
983 typedef void (*bt_bap_ep_func_t)(struct bt_bap_ep *ep, void *user_data);
984 
985 /**
986  * @brief Iterate through all endpoints of the given connection.
987  *
988  * @param conn Connection object
989  * @param func Function to call for each endpoint.
990  * @param user_data Data to pass to the callback function.
991  */
992 void bt_bap_unicast_server_foreach_ep(struct bt_conn *conn, bt_bap_ep_func_t func, void *user_data);
993 
994 /**
995  * @brief Initialize and configure a new ASE.
996  *
997  * @param conn Connection object
998  * @param stream Configured stream object to be attached to the ASE
999  * @param codec_cfg Codec configuration
1000  * @param qos_pref Audio Stream Quality of Service Preference
1001  *
1002  * @return 0 in case of success or negative value in case of error.
1003  */
1004 int bt_bap_unicast_server_config_ase(struct bt_conn *conn, struct bt_bap_stream *stream,
1005 				     struct bt_audio_codec_cfg *codec_cfg,
1006 				     const struct bt_audio_codec_qos_pref *qos_pref);
1007 
1008 /** @} */ /* End of group bt_bap_unicast_server */
1009 
1010 /**
1011  * @defgroup bt_bap_unicast_client BAP Unicast Client APIs
1012  * @ingroup bt_bap
1013  * @{
1014  */
1015 
1016 /** Parameter struct for each stream in the unicast group */
1017 struct bt_bap_unicast_group_stream_param {
1018 	/** Pointer to a stream object. */
1019 	struct bt_bap_stream *stream;
1020 
1021 	/** The QoS settings for the stream object. */
1022 	struct bt_audio_codec_qos *qos;
1023 };
1024 
1025 /** @brief Parameter struct for the unicast group functions
1026  *
1027  * Parameter struct for the bt_bap_unicast_group_create() and
1028  * bt_bap_unicast_group_add_streams() functions.
1029  */
1030 struct bt_bap_unicast_group_stream_pair_param {
1031 	/** Pointer to a receiving stream parameters. */
1032 	struct bt_bap_unicast_group_stream_param *rx_param;
1033 
1034 	/** Pointer to a transmiting stream parameters. */
1035 	struct bt_bap_unicast_group_stream_param *tx_param;
1036 };
1037 
1038 struct bt_bap_unicast_group_param {
1039 	/** The number of parameters in @p params */
1040 	size_t params_count;
1041 
1042 	/** Array of stream parameters */
1043 	struct bt_bap_unicast_group_stream_pair_param *params;
1044 
1045 	/** @brief Unicast Group packing mode.
1046 	 *
1047 	 *  @ref BT_ISO_PACKING_SEQUENTIAL or @ref BT_ISO_PACKING_INTERLEAVED.
1048 	 *
1049 	 *  @note This is a recommendation to the controller, which the controller may ignore.
1050 	 */
1051 	uint8_t packing;
1052 };
1053 
1054 /**
1055  * @brief Create audio unicast group.
1056  *
1057  * Create a new audio unicast group with one or more audio streams as a unicast client. Streams in
1058  * a unicast group shall share the same interval, framing and latency (see @ref bt_audio_codec_qos).
1059  *
1060  * @param[in]  param          The unicast group create parameters.
1061  * @param[out] unicast_group  Pointer to the unicast group created.
1062  *
1063  * @return Zero on success or (negative) error code otherwise.
1064  */
1065 int bt_bap_unicast_group_create(struct bt_bap_unicast_group_param *param,
1066 				struct bt_bap_unicast_group **unicast_group);
1067 
1068 /**
1069  * @brief Add streams to a unicast group as a unicast client
1070  *
1071  * This function can be used to add additional streams to a  bt_bap_unicast_group.
1072  *
1073  * This can be called at any time before any of the streams in the group has been started
1074  * (see bt_bap_stream_ops.started()).
1075  * This can also be called after the streams have been stopped (see bt_bap_stream_ops.stopped()).
1076  *
1077  * Once a stream has been added to a unicast group, it cannot be removed. To remove a stream from a
1078  * group, the group must be deleted with bt_bap_unicast_group_delete(), but this will require all
1079  * streams in the group to be released first.
1080  *
1081  * @param unicast_group  Pointer to the unicast group
1082  * @param params         Array of stream parameters with streams being added to the group.
1083  * @param num_param      Number of paramers in @p params.
1084  *
1085  * @return 0 in case of success or negative value in case of error.
1086  */
1087 int bt_bap_unicast_group_add_streams(struct bt_bap_unicast_group *unicast_group,
1088 				     struct bt_bap_unicast_group_stream_pair_param params[],
1089 				     size_t num_param);
1090 
1091 /**
1092  * @brief Delete audio unicast group.
1093  *
1094  * Delete a audio unicast group as a client. All streams in the group shall
1095  * be in the idle or configured state.
1096  *
1097  * @param unicast_group  Pointer to the unicast group to delete
1098  *
1099  * @return Zero on success or (negative) error code otherwise.
1100  */
1101 int bt_bap_unicast_group_delete(struct bt_bap_unicast_group *unicast_group);
1102 
1103 /** Unicast Client callback structure */
1104 struct bt_bap_unicast_client_cb {
1105 	/**
1106 	 * @brief Remote Unicast Server Audio Locations
1107 	 *
1108 	 * This callback is called whenever the audio locations is read from
1109 	 * the server or otherwise notified to the client.
1110 	 *
1111 	 * @param conn  Connection to the remote unicast server.
1112 	 * @param dir   Direction of the location.
1113 	 * @param loc   The location bitfield value.
1114 	 *
1115 	 * @return 0 in case of success or negative value in case of error.
1116 	 */
1117 	void (*location)(struct bt_conn *conn, enum bt_audio_dir dir, enum bt_audio_location loc);
1118 
1119 	/**
1120 	 * @brief Remote Unicast Server Available Contexts
1121 	 *
1122 	 * This callback is called whenever the available contexts are read
1123 	 * from the server or otherwise notified to the client.
1124 	 *
1125 	 * @param conn     Connection to the remote unicast server.
1126 	 * @param snk_ctx  The sink context bitfield value.
1127 	 * @param src_ctx  The source context bitfield value.
1128 	 *
1129 	 * @return 0 in case of success or negative value in case of error.
1130 	 */
1131 	void (*available_contexts)(struct bt_conn *conn, enum bt_audio_context snk_ctx,
1132 				   enum bt_audio_context src_ctx);
1133 
1134 	/**
1135 	 * @brief Callback function for bt_bap_stream_config() and bt_bap_stream_reconfig().
1136 	 *
1137 	 * Called when the codec configure operation is completed on the server.
1138 	 *
1139 	 * @param stream   Stream the operation was performed on.
1140 	 * @param rsp_code Response code.
1141 	 * @param reason   Reason code.
1142 	 */
1143 	void (*config)(struct bt_bap_stream *stream, enum bt_bap_ascs_rsp_code rsp_code,
1144 		       enum bt_bap_ascs_reason reason);
1145 
1146 	/**
1147 	 * @brief Callback function for bt_bap_stream_qos().
1148 	 *
1149 	 * Called when the QoS configure operation is completed on the server.
1150 	 * This will be called for each stream in the group that was being QoS
1151 	 * configured.
1152 	 *
1153 	 * @param stream   Stream the operation was performed on. May be NULL if there is no stream
1154 	 *                 associated with the ASE ID sent by the server.
1155 	 * @param rsp_code Response code.
1156 	 * @param reason   Reason code.
1157 	 */
1158 	void (*qos)(struct bt_bap_stream *stream, enum bt_bap_ascs_rsp_code rsp_code,
1159 		    enum bt_bap_ascs_reason reason);
1160 
1161 	/**
1162 	 * @brief Callback function for bt_bap_stream_enable().
1163 	 *
1164 	 * Called when the enable operation is completed on the server.
1165 	 *
1166 	 * @param stream   Stream the operation was performed on. May be NULL if there is no stream
1167 	 *                 associated with the ASE ID sent by the server.
1168 	 * @param rsp_code Response code.
1169 	 * @param reason   Reason code.
1170 	 */
1171 	void (*enable)(struct bt_bap_stream *stream, enum bt_bap_ascs_rsp_code rsp_code,
1172 		       enum bt_bap_ascs_reason reason);
1173 
1174 	/**
1175 	 * @brief Callback function for bt_bap_stream_start().
1176 	 *
1177 	 * Called when the start operation is completed on the server. This will
1178 	 * only be called if the stream supplied to bt_bap_stream_start() is
1179 	 * for a @ref BT_AUDIO_DIR_SOURCE endpoint.
1180 	 *
1181 	 * @param stream   Stream the operation was performed on. May be NULL if there is no stream
1182 	 *                 associated with the ASE ID sent by the server.
1183 	 * @param rsp_code Response code.
1184 	 * @param reason   Reason code.
1185 	 */
1186 	void (*start)(struct bt_bap_stream *stream, enum bt_bap_ascs_rsp_code rsp_code,
1187 		      enum bt_bap_ascs_reason reason);
1188 
1189 	/**
1190 	 * @brief Callback function for bt_bap_stream_stop().
1191 	 *
1192 	 * Called when the stop operation is completed on the server. This will
1193 	 * only be called if the stream supplied to bt_bap_stream_stop() is
1194 	 * for a @ref BT_AUDIO_DIR_SOURCE endpoint.
1195 	 *
1196 	 * @param stream   Stream the operation was performed on. May be NULL if there is no stream
1197 	 *                 associated with the ASE ID sent by the server.
1198 	 * @param rsp_code Response code.
1199 	 * @param reason   Reason code.
1200 	 */
1201 	void (*stop)(struct bt_bap_stream *stream, enum bt_bap_ascs_rsp_code rsp_code,
1202 		     enum bt_bap_ascs_reason reason);
1203 
1204 	/**
1205 	 * @brief Callback function for bt_bap_stream_disable().
1206 	 *
1207 	 * Called when the disable operation is completed on the server.
1208 	 *
1209 	 * @param stream   Stream the operation was performed on. May be NULL if there is no stream
1210 	 *                 associated with the ASE ID sent by the server.
1211 	 * @param rsp_code Response code.
1212 	 * @param reason   Reason code.
1213 	 */
1214 	void (*disable)(struct bt_bap_stream *stream, enum bt_bap_ascs_rsp_code rsp_code,
1215 			enum bt_bap_ascs_reason reason);
1216 
1217 	/**
1218 	 * @brief Callback function for bt_bap_stream_metadata().
1219 	 *
1220 	 * Called when the metadata operation is completed on the server.
1221 	 *
1222 	 * @param stream   Stream the operation was performed on. May be NULL if there is no stream
1223 	 *                 associated with the ASE ID sent by the server.
1224 	 * @param rsp_code Response code.
1225 	 * @param reason   Reason code.
1226 	 */
1227 	void (*metadata)(struct bt_bap_stream *stream, enum bt_bap_ascs_rsp_code rsp_code,
1228 			 enum bt_bap_ascs_reason reason);
1229 
1230 	/**
1231 	 * @brief Callback function for bt_bap_stream_release().
1232 	 *
1233 	 * Called when the release operation is completed on the server.
1234 	 *
1235 	 * @param stream   Stream the operation was performed on. May be NULL if there is no stream
1236 	 *                 associated with the ASE ID sent by the server.
1237 	 * @param rsp_code Response code.
1238 	 * @param reason   Reason code.
1239 	 */
1240 	void (*release)(struct bt_bap_stream *stream, enum bt_bap_ascs_rsp_code rsp_code,
1241 			enum bt_bap_ascs_reason reason);
1242 
1243 	/**
1244 	 * @brief Remote Published Audio Capability (PAC) record discovered
1245 	 *
1246 	 * Called when a PAC record has been discovered as part of the discovery procedure.
1247 	 *
1248 	 * The @p codec is only valid while in the callback, so the values must be stored by the
1249 	 * receiver if future use is wanted.
1250 	 *
1251 	 * @param conn      Connection to the remote unicast server.
1252 	 * @param dir       The type of remote endpoints and capabilities discovered.
1253 	 * @param codec_cap Remote capabilities.
1254 	 *
1255 	 * If discovery procedure has complete both @p codec and @p ep are set to NULL.
1256 	 */
1257 	void (*pac_record)(struct bt_conn *conn, enum bt_audio_dir dir,
1258 			   const struct bt_audio_codec_cap *codec_cap);
1259 
1260 	/**
1261 	 * @brief Remote Audio Stream Endoint (ASE) discovered
1262 	 *
1263 	 * Called when an ASE has been discovered as part of the discovery procedure.
1264 	 *
1265 	 * @param conn     Connection to the remote unicast server.
1266 	 * @param dir      The type of remote endpoints and capabilities discovered.
1267 	 * @param ep       Remote endpoint.
1268 	 *
1269 	 * If discovery procedure has complete both @p codec and @p ep are set to NULL.
1270 	 */
1271 	void (*endpoint)(struct bt_conn *conn, enum bt_audio_dir dir, struct bt_bap_ep *ep);
1272 
1273 	/**
1274 	 * @brief BAP discovery callback function.
1275 	 *
1276 	 * If discovery procedure has completed @p ep is set to NULL and @p err is 0.
1277 	 *
1278 	 * @param conn     Connection to the remote unicast server.
1279 	 * @param err      Error value. 0 on success, GATT error on positive value or errno on
1280 	 *                 negative value.
1281 	 * @param dir      The type of remote endpoints and capabilities discovered.
1282 	 *
1283 	 * If discovery procedure has complete both @p codec and @p ep are set to NULL.
1284 	 */
1285 	void (*discover)(struct bt_conn *conn, int err, enum bt_audio_dir dir);
1286 };
1287 
1288 /**
1289  * @brief Register unicast client callbacks.
1290  *
1291  * Only one callback structure can be registered, and attempting to
1292  * registering more than one will result in an error.
1293  *
1294  * @param cb  Unicast client callback structure.
1295  *
1296  * @return 0 in case of success or negative value in case of error.
1297  */
1298 int bt_bap_unicast_client_register_cb(const struct bt_bap_unicast_client_cb *cb);
1299 
1300 /**
1301  * @brief Discover remote capabilities and endpoints
1302  *
1303  * This procedure is used by a client to discover remote capabilities and
1304  * endpoints and notifies via params callback.
1305  *
1306  * @param conn   Connection object
1307  * @param dir    The type of remote endpoints and capabilities to discover.
1308  */
1309 int bt_bap_unicast_client_discover(struct bt_conn *conn, enum bt_audio_dir dir);
1310 
1311 /** @} */ /* End of group bt_bap_unicast_client */
1312 /**
1313  * @brief BAP Broadcast APIs
1314  * @defgroup bt_bap_broadcast BAP Broadcast  APIs
1315  * @ingroup bt_bap
1316  * @{
1317  */
1318 
1319 struct bt_bap_base_bis_data {
1320 	/* Unique index of the BIS */
1321 	uint8_t index;
1322 #if CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE > 0
1323 	/** Codec Specific Data length. */
1324 	size_t data_len;
1325 	/** Codec Specific Data */
1326 	uint8_t data[CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE];
1327 #endif /* CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE > 0 */
1328 };
1329 
1330 struct bt_bap_base_subgroup {
1331 	/* Number of BIS in the subgroup */
1332 	size_t bis_count;
1333 	/** Codec information for the subgroup
1334 	 *
1335 	 *  If the data_len of the codec is 0, then codec specific data may be
1336 	 *  found for each BIS in the bis_data.
1337 	 */
1338 	struct bt_audio_codec_cfg codec_cfg;
1339 	/* Array of BIS specific data for each BIS in the subgroup */
1340 	struct bt_bap_base_bis_data bis_data[BROADCAST_SNK_STREAM_CNT];
1341 };
1342 
1343 struct bt_bap_base {
1344 	/** @brief QoS Presentation Delay in microseconds
1345 	 *
1346 	 *  Value range 0 to @ref BT_AUDIO_PD_MAX.
1347 	 */
1348 	uint32_t pd;
1349 
1350 	/* Number of subgroups in the BASE */
1351 	size_t subgroup_count;
1352 
1353 	/* Array of subgroups in the BASE */
1354 	struct bt_bap_base_subgroup subgroups[BROADCAST_SNK_SUBGROUP_CNT];
1355 };
1356 
1357 /** @brief Decode a Broadcast Audio Source Endpoint (BASE) from advertising data
1358  *
1359  *  The BASE is sent via periodic advertising, and can be decoded into a
1360  *  bt_bap_base using this function.
1361  *
1362  *  @param data The periodic advertising data
1363  *  @param base The output struct to put the decode BASE in
1364  *
1365  *  @return 0 in case of success or negative errno value in case of error.
1366  */
1367 int bt_bap_decode_base(struct bt_data *data, struct bt_bap_base *base);
1368 
1369 /** @} */ /* End of group bt_bap_broadcast */
1370 
1371 /**
1372  * @brief BAP Broadcast Source APIs
1373  * @defgroup bt_bap_broadcast_source BAP Broadcast Source APIs
1374  * @ingroup bt_bap_broadcast
1375  * @{
1376  */
1377 
1378 /** Broadcast Source stream parameters */
1379 struct bt_bap_broadcast_source_stream_param {
1380 	/** Audio stream */
1381 	struct bt_bap_stream *stream;
1382 
1383 #if CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE > 0
1384 	/**
1385 	 * @brief The number of elements in the @p data array.
1386 	 *
1387 	 * The BIS specific data may be omitted and this set to 0.
1388 	 */
1389 	size_t data_len;
1390 
1391 	/** BIS Codec Specific Configuration */
1392 	uint8_t *data;
1393 #endif /* CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE > 0 */
1394 };
1395 
1396 /** Broadcast Source subgroup parameters*/
1397 struct bt_bap_broadcast_source_subgroup_param {
1398 	/** The number of parameters in @p stream_params */
1399 	size_t params_count;
1400 
1401 	/** Array of stream parameters */
1402 	struct bt_bap_broadcast_source_stream_param *params;
1403 
1404 	/** Subgroup Codec configuration. */
1405 	struct bt_audio_codec_cfg *codec_cfg;
1406 };
1407 
1408 /** Broadcast Source create parameters */
1409 struct bt_bap_broadcast_source_param {
1410 	/** The number of parameters in @p subgroup_params */
1411 	size_t params_count;
1412 
1413 	/** Array of stream parameters */
1414 	struct bt_bap_broadcast_source_subgroup_param *params;
1415 
1416 	/** Quality of Service configuration. */
1417 	struct bt_audio_codec_qos *qos;
1418 
1419 	/**
1420 	 * @brief Broadcast Source packing mode.
1421 	 *
1422 	 * @ref BT_ISO_PACKING_SEQUENTIAL or @ref BT_ISO_PACKING_INTERLEAVED.
1423 	 *
1424 	 * @note This is a recommendation to the controller, which the controller may ignore.
1425 	 */
1426 	uint8_t packing;
1427 
1428 	/** Whether or not to encrypt the streams. */
1429 	bool encryption;
1430 
1431 	/**
1432 	 * @brief Broadcast code
1433 	 *
1434 	 * If the value is a string or a the value is less than 16 octets,
1435 	 * the remaining octets shall be 0.
1436 	 *
1437 	 * Example:
1438 	 *   The string "Broadcast Code" shall be
1439 	 *   [42 72 6F 61 64 63 61 73 74 20 43 6F 64 65 00 00]
1440 	 */
1441 	uint8_t broadcast_code[BT_AUDIO_BROADCAST_CODE_SIZE];
1442 };
1443 
1444 /**
1445  * @brief Create audio broadcast source.
1446  *
1447  * Create a new audio broadcast source with one or more audio streams.
1448  *
1449  * The broadcast source will be visible for scanners once this has been called,
1450  * and the device will advertise audio announcements.
1451  *
1452  * No audio data can be sent until bt_bap_broadcast_source_start() has been called and no audio
1453  * information (BIGInfo) will be visible to scanners (see @ref bt_le_per_adv_sync_cb).
1454  *
1455  * @param[in]  param       Pointer to parameters used to create the broadcast source.
1456  * @param[out] source      Pointer to the broadcast source created
1457  *
1458  * @return Zero on success or (negative) error code otherwise.
1459  */
1460 int bt_bap_broadcast_source_create(struct bt_bap_broadcast_source_param *param,
1461 				   struct bt_bap_broadcast_source **source);
1462 
1463 /**
1464  * @brief Reconfigure audio broadcast source.
1465  *
1466  * Reconfigure an audio broadcast source with a new codec and codec quality of
1467  * service parameters. This can only be done when the source is stopped.
1468  *
1469  * Since this may modify the Broadcast Audio Source Endpoint (BASE),
1470  * bt_bap_broadcast_source_get_base() should be called after this to get the new BASE information.
1471  *
1472  * If the @p param.params_count is smaller than the number of subgroups that have been created in
1473  * the Broadcast Source, only the first @p param.params_count subgroups are updated. If a stream
1474  * exist in a subgroup not part of @p param, then that stream is left as is (i.e. it is not removed;
1475  * the only way to remove a stream from a Broadcast Source is to recreate the Broadcast Source).
1476  *
1477  * @param source      Pointer to the broadcast source
1478  * @param param       Pointer to parameters used to reconfigure the broadcast source.
1479  *
1480  * @return Zero on success or (negative) error code otherwise.
1481  */
1482 int bt_bap_broadcast_source_reconfig(struct bt_bap_broadcast_source *source,
1483 				     struct bt_bap_broadcast_source_param *param);
1484 
1485 /**
1486  * @brief Modify the metadata of an audio broadcast source.
1487  *
1488  * Modify the metadata an audio broadcast source. This can only be done when the source is started.
1489  * To update the metadata in the stopped state, use bt_bap_broadcast_source_reconfig().
1490  *
1491  * @param source      Pointer to the broadcast source.
1492  * @param meta        Metadata.
1493  * @param meta_len    Length of metadata.
1494  *
1495  * @return Zero on success or (negative) error code otherwise.
1496  */
1497 int bt_bap_broadcast_source_update_metadata(struct bt_bap_broadcast_source *source,
1498 					    const uint8_t meta[], size_t meta_len);
1499 
1500 /**
1501  * @brief Start audio broadcast source.
1502  *
1503  * Start an audio broadcast source with one or more audio streams.
1504  * The broadcast source will start advertising BIGInfo, and audio data can be streamed.
1505  *
1506  * @param source      Pointer to the broadcast source
1507  * @param adv         Pointer to an extended advertising set with periodic advertising configured.
1508  *
1509  * @return Zero on success or (negative) error code otherwise.
1510  */
1511 int bt_bap_broadcast_source_start(struct bt_bap_broadcast_source *source,
1512 				  struct bt_le_ext_adv *adv);
1513 
1514 /**
1515  * @brief Stop audio broadcast source.
1516  *
1517  * Stop an audio broadcast source.
1518  * The broadcast source will stop advertising BIGInfo, and audio data can no longer be streamed.
1519  *
1520  * @param source      Pointer to the broadcast source
1521  *
1522  * @return Zero on success or (negative) error code otherwise.
1523  */
1524 int bt_bap_broadcast_source_stop(struct bt_bap_broadcast_source *source);
1525 
1526 /**
1527  * @brief Delete audio broadcast source.
1528  *
1529  * Delete an audio broadcast source.
1530  * The broadcast source will stop advertising entirely, and the source can no longer be used.
1531  *
1532  * @param source      Pointer to the broadcast source
1533  *
1534  * @return Zero on success or (negative) error code otherwise.
1535  */
1536 int bt_bap_broadcast_source_delete(struct bt_bap_broadcast_source *source);
1537 
1538 /**
1539  * @brief Get the broadcast ID of a broadcast source
1540  *
1541  * This will return the 3-octet broadcast ID that should be advertised in the
1542  * extended advertising data with @ref BT_UUID_BROADCAST_AUDIO_VAL as @ref BT_DATA_SVC_DATA16.
1543  *
1544  * See table 3.14 in the Basic Audio Profile v1.0.1 for the structure.
1545  *
1546  * @param[in]  source        Pointer to the broadcast source.
1547  * @param[out] broadcast_id  Pointer to the 3-octet broadcast ID.
1548  *
1549  * @return Zero on success or (negative) error code otherwise.
1550  */
1551 int bt_bap_broadcast_source_get_id(struct bt_bap_broadcast_source *source,
1552 				   uint32_t *const broadcast_id);
1553 
1554 /**
1555  * @brief Get the Broadcast Audio Stream Endpoint of a broadcast source
1556  *
1557  * This will encode the BASE of a broadcast source into a buffer, that can be used for
1558  * advertisement. The encoded BASE will thus be encoded as little-endian. The BASE shall be put into
1559  * the periodic advertising data (see bt_le_per_adv_set_data()).
1560  *
1561  * See table 3.15 in the Basic Audio Profile v1.0.1 for the structure.
1562  *
1563  * @param source        Pointer to the broadcast source.
1564  * @param base_buf      Pointer to a buffer where the BASE will be inserted.
1565  *
1566  * @return Zero on success or (negative) error code otherwise.
1567  */
1568 int bt_bap_broadcast_source_get_base(struct bt_bap_broadcast_source *source,
1569 				     struct net_buf_simple *base_buf);
1570 
1571 /** @} */ /* End of bt_bap_broadcast_source */
1572 
1573 /**
1574  * @brief BAP Broadcast Sink APIs
1575  * @defgroup bt_bap_broadcast_sink BAP Broadcast Sink APIs
1576  * @ingroup bt_bap_broadcast
1577  * @{
1578  */
1579 
1580 /** Broadcast Audio Sink callback structure */
1581 struct bt_bap_broadcast_sink_cb {
1582 	/** @brief Broadcast Audio Source Endpoint (BASE) received
1583 	 *
1584 	 *  Callback for when we receive a BASE from a broadcaster after
1585 	 *  syncing to the broadcaster's periodic advertising.
1586 	 *
1587 	 *  @param sink          Pointer to the sink structure.
1588 	 *  @param base          Broadcast Audio Source Endpoint (BASE).
1589 	 */
1590 	void (*base_recv)(struct bt_bap_broadcast_sink *sink, const struct bt_bap_base *base);
1591 
1592 	/** @brief Broadcast sink is syncable
1593 	 *
1594 	 *  Called whenever a broadcast sink is not synchronized to audio, but the audio is
1595 	 *  synchronizable. This is inferred when a BIGInfo report is received.
1596 	 *
1597 	 *  Once this callback has been called, it is possible to call
1598 	 *  bt_bap_broadcast_sink_sync() to synchronize to the audio stream(s).
1599 	 *
1600 	 *  @param sink          Pointer to the sink structure.
1601 	 *  @param encrypted     Whether or not the broadcast is encrypted
1602 	 */
1603 	void (*syncable)(struct bt_bap_broadcast_sink *sink, bool encrypted);
1604 
1605 	/* Internally used list node */
1606 	sys_snode_t _node;
1607 };
1608 
1609 /** @brief Register Broadcast sink callbacks
1610  * *
1611  *  @param cb  Broadcast sink callback structure.
1612  */
1613 int bt_bap_broadcast_sink_register_cb(struct bt_bap_broadcast_sink_cb *cb);
1614 
1615 /** @brief Create a Broadcast Sink from a periodic advertising sync
1616  *
1617  *  This should only be done after verifying that the periodic advertising sync
1618  *  is from a Broadcast Source.
1619  *
1620  *  The created Broadcast Sink will need to be supplied to
1621  *  bt_bap_broadcast_sink_sync() in order to synchronize to the broadcast
1622  *  audio.
1623  *
1624  *  bt_bap_broadcast_sink_cb.pa_synced() will be called with the Broadcast
1625  *  Sink object created if this is successful.
1626  *
1627  *  @param      pa_sync       Pointer to the periodic advertising sync object.
1628  *  @param      broadcast_id  24-bit broadcast ID.
1629  *  @param[out] sink          Pointer to the Broadcast Sink created.
1630  *
1631  *  @return 0 in case of success or errno value in case of error.
1632  */
1633 int bt_bap_broadcast_sink_create(struct bt_le_per_adv_sync *pa_sync, uint32_t broadcast_id,
1634 				 struct bt_bap_broadcast_sink **sink);
1635 
1636 /** @brief Sync to a broadcaster's audio
1637  *
1638  *  @param sink               Pointer to the sink object from the base_recv callback.
1639  *  @param indexes_bitfield   Bitfield of the BIS index to sync to. To sync to e.g. BIS index 1 and
1640  *                            2, this should have the value of BIT(1) | BIT(2).
1641  *  @param streams            Stream object pointers to be used for the receiver. If multiple BIS
1642  *                            indexes shall be synchronized, multiple streams shall be provided.
1643  *  @param broadcast_code     The 16-octet broadcast code. Shall be supplied if the broadcast is
1644  *                            encrypted (see @ref bt_bap_broadcast_sink_cb.syncable).
1645  *                            If the value is a string or a the value is less
1646  *                            than 16 octets, the remaining octets shall be 0.
1647  *
1648  *                            Example:
1649  *                            The string "Broadcast Code" shall be
1650  *                            [42 72 6F 61 64 63 61 73 74 20 43 6F 64 65 00 00]
1651  *
1652  *  @return 0 in case of success or negative value in case of error.
1653  */
1654 int bt_bap_broadcast_sink_sync(struct bt_bap_broadcast_sink *sink, uint32_t indexes_bitfield,
1655 			       struct bt_bap_stream *streams[], const uint8_t broadcast_code[16]);
1656 
1657 /** @brief Stop audio broadcast sink.
1658  *
1659  *  Stop an audio broadcast sink.
1660  *  The broadcast sink will stop receiving BIGInfo, and audio data can no longer be streamed.
1661  *
1662  *  @param sink      Pointer to the broadcast sink
1663  *
1664  *  @return Zero on success or (negative) error code otherwise.
1665  */
1666 int bt_bap_broadcast_sink_stop(struct bt_bap_broadcast_sink *sink);
1667 
1668 /** @brief Release a broadcast sink
1669  *
1670  *  Once a broadcast sink has been allocated after the pa_synced callback, it can be deleted using
1671  *  this function. If the sink has synchronized to any broadcast audio streams, these must first be
1672  *  stopped using bt_bap_stream_stop.
1673  *
1674  *  @param sink Pointer to the sink object to delete.
1675  *
1676  *  @return 0 in case of success or negative value in case of error.
1677  */
1678 int bt_bap_broadcast_sink_delete(struct bt_bap_broadcast_sink *sink);
1679 
1680 /** @} */ /* End of group bt_bap_broadcast_sink */
1681 
1682 /**
1683  * @brief Register the callbacks for the Basic Audio Profile Scan Delegator
1684  *
1685  * @param cb Pointer to the callback struct
1686  */
1687 void bt_bap_scan_delegator_register_cb(struct bt_bap_scan_delegator_cb *cb);
1688 
1689 /**
1690  * @brief Set the periodic advertising sync state to syncing
1691  *
1692  * Set the periodic advertising sync state for a receive state to syncing,
1693  * notifying Broadcast Assistants.
1694  *
1695  * @param src_id    The source id used to identify the receive state.
1696  * @param pa_state  The Periodic Advertising sync state to set.
1697  *                  BT_BAP_PA_STATE_NOT_SYNCED and BT_BAP_PA_STATE_SYNCED is
1698  *                  not necessary to provide, as they are handled internally.
1699  *
1700  * @return int    Error value. 0 on success, errno on fail.
1701  */
1702 int bt_bap_scan_delegator_set_pa_state(uint8_t src_id,
1703 				       enum bt_bap_pa_state pa_state);
1704 
1705 /**
1706  * @brief Set the sync state of a receive state in the server
1707  *
1708  * @param src_id         The source id used to identify the receive state.
1709  * @param bis_synced     Array of bitfields to set the BIS sync state for each
1710  *                       subgroup.
1711  * @return int           Error value. 0 on success, ERRNO on fail.
1712  */
1713 int bt_bap_scan_delegator_set_bis_sync_state(
1714 	uint8_t src_id,
1715 	uint32_t bis_synced[BT_BAP_SCAN_DELEGATOR_MAX_SUBGROUPS]);
1716 
1717 struct bt_bap_scan_delegator_add_src_param {
1718 	/** The periodic adverting sync */
1719 	struct bt_le_per_adv_sync *pa_sync;
1720 
1721 	/** The broadcast isochronous group encryption state */
1722 	enum bt_bap_big_enc_state encrypt_state;
1723 
1724 	/** The 24-bit broadcast ID */
1725 	uint32_t broadcast_id;
1726 
1727 	/** Number of subgroups */
1728 	uint8_t num_subgroups;
1729 
1730 	/** Subgroup specific information */
1731 	struct bt_bap_scan_delegator_subgroup subgroups[BT_BAP_SCAN_DELEGATOR_MAX_SUBGROUPS];
1732 };
1733 
1734 /**
1735  * @brief Add a receive state source locally
1736  *
1737  * This will notify any connected clients about the new source. This allows them
1738  * to modify and even remove it.
1739  *
1740  * If @kconfig{CONFIG_BT_BAP_BROADCAST_SINK} is enabled, any Broadcast Sink
1741  * sources are autonomously added.
1742  *
1743  * @param param The parameters for adding the new source
1744  *
1745  * @return int  errno on failure, or source ID on success.
1746  */
1747 int bt_bap_scan_delegator_add_src(const struct bt_bap_scan_delegator_add_src_param *param);
1748 
1749 struct bt_bap_scan_delegator_mod_src_param {
1750 	/** The periodic adverting sync */
1751 	uint8_t src_id;
1752 
1753 	/** The broadcast isochronous group encryption state */
1754 	enum bt_bap_big_enc_state encrypt_state;
1755 
1756 	/** The 24-bit broadcast ID */
1757 	uint32_t broadcast_id;
1758 
1759 	/** Number of subgroups */
1760 	uint8_t num_subgroups;
1761 
1762 	/**
1763 	 * @brief Subgroup specific information
1764 	 *
1765 	 * If a subgroup's metadata_len is set to 0, the existing metadata
1766 	 * for the subgroup will remain unchanged
1767 	 */
1768 	struct bt_bap_scan_delegator_subgroup subgroups[BT_BAP_SCAN_DELEGATOR_MAX_SUBGROUPS];
1769 };
1770 
1771 /**
1772  * @brief Add a receive state source locally
1773  *
1774  * This will notify any connected clients about the new source. This allows them
1775  * to modify and even remove it.
1776  *
1777  * If @kconfig{CONFIG_BT_BAP_BROADCAST_SINK} is enabled, any Broadcast Sink
1778  * sources are autonomously modifed.
1779  *
1780  * @param param The parameters for adding the new source
1781  *
1782  * @return int  errno on failure, or source ID on success.
1783  */
1784 int bt_bap_scan_delegator_mod_src(const struct bt_bap_scan_delegator_mod_src_param *param);
1785 
1786 /**
1787  * @brief Remove a receive state source
1788  *
1789  * This will remove the receive state. If the receive state periodic advertising
1790  * is synced, bt_bap_scan_delegator_cb.pa_sync_term_req() will be called.
1791  *
1792  * If @kconfig{CONFIG_BT_BAP_BROADCAST_SINK} is enabled, any Broadcast Sink
1793  * sources are autonomously removed.
1794  *
1795  * @param src_id The source ID to remove
1796  *
1797  * @return int   Error value. 0 on success, errno on fail.
1798  */
1799 int bt_bap_scan_delegator_rem_src(uint8_t src_id);
1800 
1801 /** Callback function for Scan Delegator receive state search functions
1802  *
1803  * @param recv_state The receive state.
1804  * @param user_data  User data.
1805  *
1806  * @retval true to stop iterating. If this is used in the context of
1807  *         bt_bap_scan_delegator_find_state(), the recv_state will be returned by
1808  *         bt_bap_scan_delegator_find_state()
1809  * @retval false to continue iterating
1810  */
1811 typedef bool (*bt_bap_scan_delegator_state_func_t)(
1812 	const struct bt_bap_scan_delegator_recv_state *recv_state, void *user_data);
1813 
1814 /** @brief Iterate through all existing receive states
1815  *
1816  * @param func      The callback function
1817  * @param user_data User specified data that sent to the callback function
1818  */
1819 void bt_bap_scan_delegator_foreach_state(bt_bap_scan_delegator_state_func_t func,
1820 					 void *user_data);
1821 
1822 /** @brief Find and return a receive state based on a compare function
1823  *
1824  * @param func      The compare callback function
1825  * @param user_data User specified data that sent to the callback function
1826  *
1827  * @return The first receive state where the @p func returned true, or NULL
1828  */
1829 const struct bt_bap_scan_delegator_recv_state *bt_bap_scan_delegator_find_state(
1830 	bt_bap_scan_delegator_state_func_t func, void *user_data);
1831 
1832 /******************************** CLIENT API ********************************/
1833 
1834 /**
1835  * @brief Callback function for writes.
1836  *
1837  * @param conn    The connection to the peer device.
1838  * @param err     Error value. 0 on success, GATT error on fail.
1839  */
1840 typedef void (*bt_bap_broadcast_assistant_write_cb)(struct bt_conn *conn,
1841 						    int err);
1842 
1843 struct bt_bap_broadcast_assistant_cb {
1844 	/**
1845 	 * @brief Callback function for bt_bap_broadcast_assistant_discover.
1846 	 *
1847 	 * @param conn              The connection that was used to discover
1848 	 *                          Broadcast Audio Scan Service.
1849 	 * @param err               Error value. 0 on success,
1850 	 *                          GATT error or ERRNO on fail.
1851 	 * @param recv_state_count  Number of receive states on the server.
1852 	 */
1853 	void (*discover)(struct bt_conn *conn, int err,
1854 			 uint8_t recv_state_count);
1855 
1856 	/**
1857 	 * @brief Callback function for Broadcast Audio Scan Service client scan results
1858 	 *
1859 	 * Called when the scanner finds an advertiser that advertises the
1860 	 * BT_UUID_BROADCAST_AUDIO UUID.
1861 	 *
1862 	 * @param info          Advertiser information.
1863 	 * @param broadcast_id  24-bit broadcast ID.
1864 	 */
1865 	void (*scan)(const struct bt_le_scan_recv_info *info,
1866 		     uint32_t broadcast_id);
1867 
1868 	/**
1869 	 * @brief Callback function for when a receive state is read or updated
1870 	 *
1871 	 * Called whenever a receive state is read or updated.
1872 	 *
1873 	 * @param conn     The connection to the Broadcast Audio Scan Service server.
1874 	 * @param err      Error value. 0 on success, GATT error on fail.
1875 	 * @param state    The receive state or NULL if the receive state is empty.
1876 	 */
1877 	void (*recv_state)(struct bt_conn *conn, int err,
1878 			   const struct bt_bap_scan_delegator_recv_state *state);
1879 
1880 	/**
1881 	 * @brief Callback function for when a receive state is removed.
1882 	 *
1883 	 * @param conn     The connection to the Broadcast Audio Scan Service server.
1884 	 * @param err      Error value. 0 on success, GATT error on fail.
1885 	 * @param src_id   The receive state.
1886 	 */
1887 	void (*recv_state_removed)(struct bt_conn *conn, int err,
1888 				   uint8_t src_id);
1889 
1890 	/**
1891 	 * @brief Callback function for bt_bap_broadcast_assistant_scan_start().
1892 	 *
1893 	 * @param conn    The connection to the peer device.
1894 	 * @param err     Error value. 0 on success, GATT error on fail.
1895 	 */
1896 	void (*scan_start)(struct bt_conn *conn, int err);
1897 
1898 	/**
1899 	 * @brief Callback function for bt_bap_broadcast_assistant_scan_stop().
1900 	 *
1901 	 * @param conn    The connection to the peer device.
1902 	 * @param err     Error value. 0 on success, GATT error on fail.
1903 	 */
1904 	void (*scan_stop)(struct bt_conn *conn, int err);
1905 
1906 	/**
1907 	 * @brief Callback function for bt_bap_broadcast_assistant_add_src().
1908 	 *
1909 	 * @param conn    The connection to the peer device.
1910 	 * @param err     Error value. 0 on success, GATT error on fail.
1911 	 */
1912 	void (*add_src)(struct bt_conn *conn, int err);
1913 
1914 	/**
1915 	 * @brief Callback function for bt_bap_broadcast_assistant_mod_src().
1916 	 *
1917 	 * @param conn    The connection to the peer device.
1918 	 * @param err     Error value. 0 on success, GATT error on fail.
1919 	 */
1920 	void (*mod_src)(struct bt_conn *conn, int err);
1921 
1922 	/**
1923 	 * @brief Callback function for bt_bap_broadcast_assistant_broadcast_code().
1924 	 *
1925 	 * @param conn    The connection to the peer device.
1926 	 * @param err     Error value. 0 on success, GATT error on fail.
1927 	 */
1928 	void (*broadcast_code)(struct bt_conn *conn, int err);
1929 
1930 	/**
1931 	 * @brief Callback function for bt_bap_broadcast_assistant_rem_src().
1932 	 *
1933 	 * @param conn    The connection to the peer device.
1934 	 * @param err     Error value. 0 on success, GATT error on fail.
1935 	 */
1936 	void (*rem_src)(struct bt_conn *conn, int err);
1937 };
1938 
1939 /**
1940  * @brief Discover Broadcast Audio Scan Service on the server.
1941  *
1942  * Warning: Only one connection can be active at any time; discovering for a
1943  * new connection, will delete all previous data.
1944  *
1945  * @param conn  The connection
1946  * @return int  Error value. 0 on success, GATT error or ERRNO on fail.
1947  */
1948 int bt_bap_broadcast_assistant_discover(struct bt_conn *conn);
1949 
1950 /**
1951  * @brief Scan start for BISes for a remote server.
1952  *
1953  * This will let the Broadcast Audio Scan Service server know that this device
1954  * is actively scanning for broadcast sources.
1955  * The function can optionally also start scanning, if the caller does not want
1956  * to start scanning itself.
1957  *
1958  * Scan results, if @p start_scan is true, is sent to the
1959  * bt_bap_broadcast_assistant_scan_cb callback.
1960  *
1961  * @param conn          Connection to the Broadcast Audio Scan Service server.
1962  *                      Used to let the server know that we are scanning.
1963  * @param start_scan    Start scanning if true. If false, the application should
1964  *                      enable scan itself.
1965  * @return int          Error value. 0 on success, GATT error or ERRNO on fail.
1966  */
1967 int bt_bap_broadcast_assistant_scan_start(struct bt_conn *conn,
1968 					  bool start_scan);
1969 
1970 /**
1971  * @brief Stop remote scanning for BISes for a server.
1972  *
1973  * @param conn   Connection to the server.
1974  * @return int   Error value. 0 on success, GATT error or ERRNO on fail.
1975  */
1976 int bt_bap_broadcast_assistant_scan_stop(struct bt_conn *conn);
1977 
1978 /**
1979  * @brief Registers the callbacks used by Broadcast Audio Scan Service client.
1980  */
1981 void bt_bap_broadcast_assistant_register_cb(struct bt_bap_broadcast_assistant_cb *cb);
1982 
1983 /** Parameters for adding a source to a Broadcast Audio Scan Service server */
1984 struct bt_bap_broadcast_assistant_add_src_param {
1985 	/** Address of the advertiser. */
1986 	bt_addr_le_t addr;
1987 
1988 	/** SID of the advertising set. */
1989 	uint8_t adv_sid;
1990 
1991 	/** Whether to sync to periodic advertisements. */
1992 	bool pa_sync;
1993 
1994 	/** 24-bit broadcast ID */
1995 	uint32_t broadcast_id;
1996 
1997 	/**
1998 	 * @brief Periodic advertising interval in milliseconds.
1999 	 *
2000 	 * BT_BAP_PA_INTERVAL_UNKNOWN if unknown.
2001 	 */
2002 	uint16_t pa_interval;
2003 
2004 	/** Number of subgroups */
2005 	uint8_t num_subgroups;
2006 
2007 	/** Pointer to array of subgroups */
2008 	struct bt_bap_scan_delegator_subgroup *subgroups;
2009 };
2010 
2011 /**
2012  * @brief Add a source on the server.
2013  *
2014  * @param conn          Connection to the server.
2015  * @param param         Parameter struct.
2016  *
2017  * @return Error value. 0 on success, GATT error or ERRNO on fail.
2018  */
2019 int bt_bap_broadcast_assistant_add_src(
2020 	struct bt_conn *conn, const struct bt_bap_broadcast_assistant_add_src_param *param);
2021 
2022 /** Parameters for modifying a source */
2023 struct bt_bap_broadcast_assistant_mod_src_param {
2024 	/** Source ID of the receive state. */
2025 	uint8_t src_id;
2026 
2027 	/** Whether to sync to periodic advertisements. */
2028 	bool pa_sync;
2029 
2030 	/**
2031 	 * @brief Periodic advertising interval.
2032 	 *
2033 	 * BT_BAP_PA_INTERVAL_UNKNOWN if unknown.
2034 	 */
2035 	uint16_t pa_interval;
2036 
2037 	/** Number of subgroups */
2038 	uint8_t num_subgroups;
2039 
2040 	/** Pointer to array of subgroups */
2041 	struct bt_bap_scan_delegator_subgroup *subgroups;
2042 };
2043 
2044 /** @brief Modify a source on the server.
2045  *
2046  *  @param conn          Connection to the server.
2047  *  @param param         Parameter struct.
2048  *
2049  *  @return Error value. 0 on success, GATT error or ERRNO on fail.
2050  */
2051 int bt_bap_broadcast_assistant_mod_src(
2052 	struct bt_conn *conn, const struct bt_bap_broadcast_assistant_mod_src_param *param);
2053 
2054 /** @brief Set a broadcast code to the specified receive state.
2055  *
2056  *  @param conn            Connection to the server.
2057  *  @param src_id          Source ID of the receive state.
2058  *  @param broadcast_code  The broadcast code.
2059  *
2060  *  @return Error value. 0 on success, GATT error or ERRNO on fail.
2061  */
2062 int bt_bap_broadcast_assistant_set_broadcast_code(
2063 	struct bt_conn *conn, uint8_t src_id,
2064 	const uint8_t broadcast_code[BT_AUDIO_BROADCAST_CODE_SIZE]);
2065 
2066 /** @brief Remove a source from the server.
2067  *
2068  *  @param conn            Connection to the server.
2069  *  @param src_id          Source ID of the receive state.
2070  *
2071  *  @return Error value. 0 on success, GATT error or ERRNO on fail.
2072  */
2073 int bt_bap_broadcast_assistant_rem_src(struct bt_conn *conn, uint8_t src_id);
2074 
2075 /** @brief Read the specified receive state from the server.
2076  *
2077  *  @param conn     Connection to the server.
2078  *  @param idx      The index of the receive start (0 up to the value from
2079  *                 bt_bap_broadcast_assistant_discover_cb)
2080  *
2081  *  @return Error value. 0 on success, GATT error or ERRNO on fail.
2082  */
2083 int bt_bap_broadcast_assistant_read_recv_state(struct bt_conn *conn,
2084 					       uint8_t idx);
2085 
2086 /** @} */ /* end of bt_bap */
2087 
2088 #ifdef __cplusplus
2089 }
2090 #endif
2091 
2092 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_AUDIO_BAP_ */
2093