1 /* @file
2  * @brief Internal APIs for Audio Endpoint handling
3 
4  * Copyright (c) 2020 Intel Corporation
5  * Copyright (c) 2022-2023 Nordic Semiconductor ASA
6  *
7  * SPDX-License-Identifier: Apache-2.0
8  */
9 
10 #include <stdbool.h>
11 #include <stdint.h>
12 
13 #include <zephyr/autoconf.h>
14 #include <zephyr/bluetooth/audio/audio.h>
15 #include <zephyr/bluetooth/audio/bap.h>
16 #include <zephyr/bluetooth/bluetooth.h>
17 #include <zephyr/bluetooth/iso.h>
18 #include <zephyr/kernel.h>
19 #include <zephyr/sys/atomic.h>
20 #include <zephyr/sys/slist.h>
21 #include <zephyr/types.h>
22 
23 #include "ascs_internal.h"
24 #include "bap_stream.h"
25 
26 #if defined(CONFIG_BT_BAP_UNICAST_CLIENT)
27 #define UNICAST_GROUP_CNT	 CONFIG_BT_BAP_UNICAST_CLIENT_GROUP_COUNT
28 #define UNICAST_GROUP_STREAM_CNT CONFIG_BT_BAP_UNICAST_CLIENT_GROUP_STREAM_COUNT
29 #else /* !CONFIG_BT_BAP_UNICAST_CLIENT */
30 #define UNICAST_GROUP_CNT 0
31 #define UNICAST_GROUP_STREAM_CNT 0
32 #endif /* CONFIG_BT_BAP_UNICAST_CLIENT */
33 #if defined(CONFIG_BT_BAP_BROADCAST_SOURCE)
34 #define BROADCAST_STREAM_CNT CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT
35 #else /* !CONFIG_BT_BAP_BROADCAST_SOURCE */
36 #define BROADCAST_STREAM_CNT 0
37 #endif /* CONFIG_BT_BAP_BROADCAST_SOURCE */
38 
39 /* Temp struct declarations to handle circular dependencies */
40 struct bt_bap_unicast_group;
41 struct bt_bap_broadcast_source;
42 struct bt_bap_broadcast_sink;
43 
44 struct bt_bap_ep {
45 	uint8_t  dir;
46 	uint8_t  cig_id;
47 	uint8_t  cis_id;
48 	struct bt_ascs_ase_status status;
49 	struct bt_bap_stream *stream;
50 	struct bt_audio_codec_cfg codec_cfg;
51 	struct bt_bap_qos_cfg qos;
52 	struct bt_bap_qos_cfg_pref qos_pref;
53 	struct bt_bap_iso *iso;
54 
55 	/* unicast stopped reason */
56 	uint8_t reason;
57 
58 	/* Used by the unicast server and client */
59 	bool receiver_ready;
60 
61 	/* TODO: Create a union to reduce memory usage */
62 	struct bt_bap_unicast_group *unicast_group;
63 	struct bt_bap_broadcast_source *broadcast_source;
64 	struct bt_bap_broadcast_sink *broadcast_sink;
65 };
66 
67 struct bt_bap_unicast_group_cig_param {
68 	uint32_t c_to_p_interval;
69 	uint32_t p_to_c_interval;
70 	uint16_t c_to_p_latency;
71 	uint16_t p_to_c_latency;
72 	uint8_t framing;
73 	uint8_t packing;
74 #if defined(CONFIG_BT_ISO_TEST_PARAMS)
75 	uint8_t c_to_p_ft;
76 	uint8_t p_to_c_ft;
77 	uint16_t iso_interval;
78 #endif /* CONFIG_BT_ISO_TEST_PARAMS */
79 };
80 
81 struct bt_bap_unicast_group {
82 	/* Group-wide QoS used to create the CIG */
83 	struct bt_bap_unicast_group_cig_param cig_param;
84 
85 	/* Unicast group fields */
86 	uint8_t index;
87 	bool allocated;
88 	/* Used to determine whether any stream in this group has been started which will prevent
89 	 * reconfiguring it
90 	 */
91 	bool has_been_connected;
92 	struct bt_iso_cig *cig;
93 	/* The ISO API for CIG creation requires an array of pointers to ISO channels */
94 	struct bt_iso_chan *cis[UNICAST_GROUP_STREAM_CNT];
95 	sys_slist_t streams;
96 };
97 
98 #if CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE > 0
99 struct bt_audio_broadcast_stream_data {
100 	/** Codec Specific Data len */
101 	size_t data_len;
102 	/** Codec Specific Data */
103 	uint8_t data[CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE];
104 };
105 #endif /* CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE > 0 */
106 
107 struct bt_bap_broadcast_source {
108 	uint8_t stream_count;
109 	uint8_t packing;
110 	bool encryption;
111 
112 	struct bt_iso_big *big;
113 	struct bt_bap_qos_cfg *qos;
114 #if defined(CONFIG_BT_ISO_TEST_PARAMS)
115 	/* Stored advanced parameters */
116 	uint8_t irc;
117 	uint8_t pto;
118 	uint16_t iso_interval;
119 #endif /* CONFIG_BT_ISO_TEST_PARAMS */
120 
121 #if CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE > 0
122 	/* The codec specific configured data for each stream in the subgroup */
123 	struct bt_audio_broadcast_stream_data stream_data[BROADCAST_STREAM_CNT];
124 #endif /* CONFIG_BT_AUDIO_CODEC_CFG_MAX_DATA_SIZE > 0 */
125 	uint8_t broadcast_code[BT_ISO_BROADCAST_CODE_SIZE];
126 
127 	/* The complete codec specific configured data for each stream in the subgroup.
128 	 * This contains both the subgroup and the BIS-specific data for each stream.
129 	 */
130 	struct bt_audio_codec_cfg codec_cfg[BROADCAST_STREAM_CNT];
131 
132 	/* The subgroups containing the streams used to create the broadcast source */
133 	sys_slist_t subgroups;
134 };
135 
136 enum bt_bap_broadcast_sink_flag {
137 	/** Sink has been initialized */
138 	BT_BAP_BROADCAST_SINK_FLAG_INITIALIZED,
139 
140 	/** BIGInfo has been received */
141 	BT_BAP_BROADCAST_SINK_FLAG_BIGINFO_RECEIVED,
142 
143 	/** Periodic Advertising is syncing */
144 	BT_BAP_BROADCAST_SINK_FLAG_SYNCING,
145 
146 	/** Broadcast sink instance is scanning */
147 	BT_BAP_BROADCAST_SINK_FLAG_SCANNING,
148 
149 	/** The BIG is encrypted */
150 	BT_BAP_BROADCAST_SINK_FLAG_BIG_ENCRYPTED,
151 
152 	/** The Scan Delegator Source ID is valid */
153 	BT_BAP_BROADCAST_SINK_FLAG_SRC_ID_VALID,
154 
155 	/** Total number of flags */
156 	BT_BAP_BROADCAST_SINK_FLAG_NUM_FLAGS,
157 };
158 
159 struct bt_bap_broadcast_sink_subgroup {
160 	uint32_t bis_indexes;
161 };
162 
163 struct bt_bap_broadcast_sink_bis {
164 	uint8_t index;
165 	struct bt_iso_chan *chan;
166 	struct bt_audio_codec_cfg codec_cfg;
167 };
168 
169 #if defined(CONFIG_BT_BAP_BROADCAST_SINK)
170 struct bt_bap_broadcast_sink {
171 	uint8_t index; /* index of broadcast_snks array */
172 	uint8_t stream_count;
173 	uint8_t bass_src_id;
174 	uint8_t subgroup_count;
175 	uint16_t iso_interval;
176 	uint16_t biginfo_num_bis;
177 	uint32_t broadcast_id; /* 24 bit */
178 	uint32_t indexes_bitfield;
179 	uint32_t valid_indexes_bitfield; /* based on codec support */
180 	struct bt_bap_qos_cfg qos_cfg;
181 	struct bt_le_per_adv_sync *pa_sync;
182 	struct bt_iso_big *big;
183 	struct bt_bap_broadcast_sink_bis bis[CONFIG_BT_BAP_BROADCAST_SNK_STREAM_COUNT];
184 	struct bt_bap_broadcast_sink_subgroup subgroups[CONFIG_BT_BAP_BROADCAST_SNK_SUBGROUP_COUNT];
185 	const struct bt_bap_scan_delegator_recv_state *recv_state;
186 	/* The streams used to create the broadcast sink */
187 	sys_slist_t streams;
188 
189 	/** Flags */
190 	ATOMIC_DEFINE(flags, BT_BAP_BROADCAST_SINK_FLAG_NUM_FLAGS);
191 };
192 #endif /* CONFIG_BT_BAP_BROADCAST_SINK */
193 
bt_bap_ep_state_str(uint8_t state)194 static inline const char *bt_bap_ep_state_str(uint8_t state)
195 {
196 	switch (state) {
197 	case BT_BAP_EP_STATE_IDLE:
198 		return "idle";
199 	case BT_BAP_EP_STATE_CODEC_CONFIGURED:
200 		return "codec-configured";
201 	case BT_BAP_EP_STATE_QOS_CONFIGURED:
202 		return "qos-configured";
203 	case BT_BAP_EP_STATE_ENABLING:
204 		return "enabling";
205 	case BT_BAP_EP_STATE_STREAMING:
206 		return "streaming";
207 	case BT_BAP_EP_STATE_DISABLING:
208 		return "disabling";
209 	case BT_BAP_EP_STATE_RELEASING:
210 		return "releasing";
211 	default:
212 		return "unknown";
213 	}
214 }
215 
216 bool bt_bap_ep_is_broadcast_snk(const struct bt_bap_ep *ep);
217 bool bt_bap_ep_is_broadcast_src(const struct bt_bap_ep *ep);
218 bool bt_bap_ep_is_unicast_client(const struct bt_bap_ep *ep);
219