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