1 /*  Bluetooth Audio Common Audio Profile internal header */
2 
3 /*
4  * Copyright (c) 2022-2024 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/conn.h>
21 #include <zephyr/bluetooth/gatt.h>
22 #include <zephyr/sys/atomic.h>
23 #include <zephyr/types.h>
24 
25 bool bt_cap_acceptor_ccid_exist(const struct bt_conn *conn, uint8_t ccid);
26 
27 void bt_cap_initiator_codec_configured(struct bt_cap_stream *cap_stream);
28 void bt_cap_initiator_qos_configured(struct bt_cap_stream *cap_stream);
29 void bt_cap_initiator_enabled(struct bt_cap_stream *cap_stream);
30 void bt_cap_initiator_started(struct bt_cap_stream *cap_stream);
31 void bt_cap_initiator_connected(struct bt_cap_stream *cap_stream);
32 void bt_cap_initiator_metadata_updated(struct bt_cap_stream *cap_stream);
33 void bt_cap_initiator_disabled(struct bt_cap_stream *cap_stream);
34 void bt_cap_initiator_stopped(struct bt_cap_stream *cap_stream);
35 void bt_cap_initiator_released(struct bt_cap_stream *cap_stream);
36 void bt_cap_stream_ops_register_bap(struct bt_cap_stream *cap_stream);
37 void bt_cap_initiator_cp_cb(struct bt_cap_stream *cap_stream, enum bt_bap_ascs_rsp_code rsp_code,
38 			    enum bt_bap_ascs_reason reason);
39 
40 enum bt_cap_common_proc_state {
41 	BT_CAP_COMMON_PROC_STATE_ACTIVE,
42 	BT_CAP_COMMON_PROC_STATE_ABORTED,
43 
44 	BT_CAP_COMMON_PROC_STATE_FLAG_NUM,
45 };
46 
47 enum bt_cap_common_proc_type {
48 	BT_CAP_COMMON_PROC_TYPE_NONE,
49 	BT_CAP_COMMON_PROC_TYPE_START,
50 	BT_CAP_COMMON_PROC_TYPE_UPDATE,
51 	BT_CAP_COMMON_PROC_TYPE_STOP,
52 	BT_CAP_COMMON_PROC_TYPE_BROADCAST_RECEPTION_START,
53 	BT_CAP_COMMON_PROC_TYPE_BROADCAST_RECEPTION_STOP,
54 	BT_CAP_COMMON_PROC_TYPE_DISTRIBUTE_BROADCAST_CODE,
55 	BT_CAP_COMMON_PROC_TYPE_VOLUME_CHANGE,
56 	BT_CAP_COMMON_PROC_TYPE_VOLUME_OFFSET_CHANGE,
57 	BT_CAP_COMMON_PROC_TYPE_VOLUME_MUTE_CHANGE,
58 	BT_CAP_COMMON_PROC_TYPE_MICROPHONE_GAIN_CHANGE,
59 	BT_CAP_COMMON_PROC_TYPE_MICROPHONE_MUTE_CHANGE,
60 };
61 
62 enum bt_cap_common_subproc_type {
63 	BT_CAP_COMMON_SUBPROC_TYPE_NONE,
64 	BT_CAP_COMMON_SUBPROC_TYPE_CODEC_CONFIG,
65 	BT_CAP_COMMON_SUBPROC_TYPE_QOS_CONFIG,
66 	BT_CAP_COMMON_SUBPROC_TYPE_ENABLE,
67 	BT_CAP_COMMON_SUBPROC_TYPE_CONNECT,
68 	BT_CAP_COMMON_SUBPROC_TYPE_START,
69 	BT_CAP_COMMON_SUBPROC_TYPE_META_UPDATE,
70 	BT_CAP_COMMON_SUBPROC_TYPE_DISABLE,
71 	BT_CAP_COMMON_SUBPROC_TYPE_STOP,
72 	BT_CAP_COMMON_SUBPROC_TYPE_RELEASE,
73 };
74 
75 struct bt_cap_initiator_proc_param {
76 	struct bt_cap_stream *stream;
77 	union {
78 		struct {
79 			struct bt_conn *conn;
80 			struct bt_bap_ep *ep;
81 			struct bt_audio_codec_cfg *codec_cfg;
82 			bool connected;
83 		} start;
84 		struct {
85 			/** Codec Specific Capabilities Metadata count */
86 			size_t meta_len;
87 			/** Codec Specific Capabilities Metadata */
88 			uint8_t meta[CONFIG_BT_AUDIO_CODEC_CFG_MAX_METADATA_SIZE];
89 		} meta_update;
90 		struct {
91 			bool release;
92 		} stop;
93 	};
94 	bool in_progress;
95 };
96 
97 #if defined(CONFIG_BT_BAP_BROADCAST_ASSISTANT)
98 struct cap_broadcast_reception_start {
99 
100 	bt_addr_le_t addr;
101 	uint8_t adv_sid;
102 	uint32_t broadcast_id;
103 	uint16_t pa_interval;
104 	uint8_t num_subgroups;
105 	struct bt_bap_bass_subgroup subgroups[CONFIG_BT_BAP_BASS_MAX_SUBGROUPS];
106 };
107 
108 struct cap_broadcast_reception_stop {
109 	uint8_t src_id;
110 	uint8_t num_subgroups;
111 	struct bt_bap_bass_subgroup subgroups[CONFIG_BT_BAP_BASS_MAX_SUBGROUPS];
112 };
113 
114 /* Note that although the broadcast_code will be the same for all
115  * we nevertheless store a separate copy for each sink, for
116  * consistensy in the struct bt_cap_commander_proc_param
117  * There is no memory savings by not having broadcast_code part of the
118  * union: struct cap_broadcast_reception_start uses minimum 20 bytes
119  * and struct cap_distribute_broadcast_code uses 17 bytes
120  */
121 struct cap_distribute_broadcast_code {
122 	uint8_t src_id;
123 	uint8_t broadcast_code[BT_ISO_BROADCAST_CODE_SIZE];
124 };
125 #endif /* CONFIG_BT_BAP_BROADCAST_ASSISTANT */
126 
127 struct bt_cap_commander_proc_param {
128 	struct bt_conn *conn;
129 	union {
130 #if defined(CONFIG_BT_VCP_VOL_CTLR)
131 		struct {
132 			uint8_t volume;
133 		} change_volume;
134 		struct {
135 			bool mute;
136 		} change_vol_mute;
137 #endif /* CONFIG_BT_VCP_VOL_CTLR */
138 #if defined(CONFIG_BT_VCP_VOL_CTLR_VOCS)
139 		struct {
140 			int16_t offset;
141 			struct bt_vocs *vocs;
142 		} change_offset;
143 #endif /* CONFIG_BT_VCP_VOL_CTLR_VOCS */
144 #if defined(CONFIG_BT_BAP_BROADCAST_ASSISTANT)
145 		struct cap_broadcast_reception_start broadcast_reception_start;
146 		struct cap_broadcast_reception_stop broadcast_reception_stop;
147 		struct cap_distribute_broadcast_code distribute_broadcast_code;
148 #endif /* CONFIG_BT_BAP_BROADCAST_ASSISTANT */
149 #if defined(CONFIG_BT_MICP_MIC_CTLR)
150 		struct {
151 			bool mute;
152 		} change_mic_mute;
153 #if defined(CONFIG_BT_MICP_MIC_CTLR_AICS)
154 		struct {
155 			int8_t gain;
156 			struct bt_aics *aics;
157 		} change_gain;
158 #endif /* CONFIG_BT_MICP_MIC_CTLR_AICS */
159 #endif /* CONFIG_BT_MICP_MIC_CTLR */
160 	};
161 };
162 
163 typedef void (*bt_cap_common_discover_func_t)(
164 	struct bt_conn *conn, int err, const struct bt_csip_set_coordinator_set_member *member,
165 	const struct bt_csip_set_coordinator_csis_inst *csis_inst);
166 
167 struct bt_cap_common_proc_param {
168 	union {
169 #if defined(CONFIG_BT_CAP_INITIATOR_UNICAST)
170 		struct bt_cap_initiator_proc_param
171 			initiator[CONFIG_BT_BAP_UNICAST_CLIENT_GROUP_STREAM_COUNT];
172 #endif /* CONFIG_BT_CAP_INITIATOR_UNICAST */
173 #if defined(CONFIG_BT_CAP_COMMANDER)
174 		struct bt_cap_commander_proc_param commander[CONFIG_BT_MAX_CONN];
175 #endif /* CONFIG_BT_CAP_COMMANDER */
176 	};
177 };
178 
179 struct bt_cap_common_proc {
180 	ATOMIC_DEFINE(proc_state_flags, BT_CAP_COMMON_PROC_STATE_FLAG_NUM);
181 	/* Total number of items (streams or connections) in the procedure*/
182 	size_t proc_cnt;
183 	/* Number of items where a subprocedure have been started */
184 	size_t proc_initiated_cnt;
185 	/* Number of items done with the procedure */
186 	size_t proc_done_cnt;
187 	enum bt_cap_common_proc_type proc_type;
188 	int err;
189 	struct bt_conn *failed_conn;
190 	struct bt_cap_common_proc_param proc_param;
191 #if defined(CONFIG_BT_CAP_INITIATOR_UNICAST)
192 	enum bt_cap_common_subproc_type subproc_type;
193 #endif /* CONFIG_BT_CAP_INITIATOR_UNICAST */
194 };
195 
196 struct bt_cap_common_client {
197 	struct bt_conn *conn;
198 	struct bt_gatt_discover_params param;
199 	bt_cap_common_discover_func_t discover_cb_func;
200 	uint16_t csis_start_handle;
201 	const struct bt_csip_set_coordinator_csis_inst *csis_inst;
202 };
203 
204 struct bt_cap_common_proc *bt_cap_common_get_active_proc(void);
205 void bt_cap_common_clear_active_proc(void);
206 void bt_cap_common_start_proc(enum bt_cap_common_proc_type proc_type, size_t proc_cnt);
207 void bt_cap_common_set_subproc(enum bt_cap_common_subproc_type subproc_type);
208 bool bt_cap_common_proc_is_type(enum bt_cap_common_proc_type proc_type);
209 bool bt_cap_common_subproc_is_type(enum bt_cap_common_subproc_type subproc_type);
210 struct bt_conn *bt_cap_common_get_member_conn(enum bt_cap_set_type type,
211 					      const union bt_cap_set_member *member);
212 bool bt_cap_common_proc_is_active(void);
213 bool bt_cap_common_proc_is_aborted(void);
214 bool bt_cap_common_proc_all_handled(void);
215 bool bt_cap_common_proc_is_done(void);
216 void bt_cap_common_abort_proc(struct bt_conn *conn, int err);
217 bool bt_cap_common_conn_in_active_proc(const struct bt_conn *conn);
218 bool bt_cap_common_stream_in_active_proc(const struct bt_cap_stream *cap_stream);
219 void bt_cap_common_disconnected(struct bt_conn *conn, uint8_t reason);
220 struct bt_cap_common_client *bt_cap_common_get_client_by_acl(const struct bt_conn *acl);
221 struct bt_cap_common_client *
222 bt_cap_common_get_client_by_csis(const struct bt_csip_set_coordinator_csis_inst *csis_inst);
223 int bt_cap_common_discover(struct bt_conn *conn, bt_cap_common_discover_func_t func);
224