1 /* main.c - Application main entry point */
2 
3 /*
4  * Copyright (c) 2023 Codecoup
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <errno.h>
10 #include <stdbool.h>
11 #include <stddef.h>
12 #include <stdint.h>
13 
14 #include <zephyr/bluetooth/audio/audio.h>
15 #include <zephyr/bluetooth/audio/bap_lc3_preset.h>
16 #include <zephyr/bluetooth/audio/lc3.h>
17 #include <zephyr/bluetooth/byteorder.h>
18 #include <zephyr/bluetooth/hci_types.h>
19 #include <zephyr/fff.h>
20 #include <zephyr/sys/byteorder.h>
21 #include <zephyr/sys/util.h>
22 #include <zephyr/ztest_test.h>
23 #include <zephyr/ztest_assert.h>
24 
25 DEFINE_FFF_GLOBALS;
26 
27 ZTEST_SUITE(audio_codec_test_suite, NULL, NULL, NULL, NULL, NULL);
28 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_get_val)29 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_get_val)
30 {
31 	struct bt_audio_codec_cfg codec_cfg =
32 		BT_AUDIO_CODEC_CFG(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000,
33 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_CODEC_CFG_FREQ,
34 							BT_AUDIO_CODEC_CFG_FREQ_16KHZ)},
35 				   {});
36 	const uint8_t expected_data = BT_AUDIO_CODEC_CFG_FREQ_16KHZ;
37 	const uint8_t *data;
38 	int ret;
39 
40 	ret = bt_audio_codec_cfg_get_val(&codec_cfg, BT_AUDIO_CODEC_CFG_FREQ, &data);
41 	zassert_equal(ret, sizeof(expected_data), "Unexpected return value %d", ret);
42 	zassert_equal(data[0], expected_data, "Unexpected return value %d", ret);
43 }
44 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_set_val)45 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_set_val)
46 {
47 	struct bt_audio_codec_cfg codec_cfg =
48 		BT_AUDIO_CODEC_CFG(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000,
49 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_CODEC_CFG_FREQ,
50 							BT_AUDIO_CODEC_CFG_FREQ_16KHZ)},
51 				   {});
52 	const uint8_t new_expected_data = BT_AUDIO_CODEC_CFG_FREQ_48KHZ;
53 	const uint8_t expected_data = BT_AUDIO_CODEC_CFG_FREQ_16KHZ;
54 	const uint8_t *data;
55 	int ret;
56 
57 	ret = bt_audio_codec_cfg_get_val(&codec_cfg, BT_AUDIO_CODEC_CFG_FREQ, &data);
58 	zassert_equal(ret, sizeof(expected_data), "Unexpected return value %d", ret);
59 	zassert_equal(data[0], expected_data, "Unexpected return value %d", ret);
60 
61 	ret = bt_audio_codec_cfg_set_val(&codec_cfg, BT_AUDIO_CODEC_CFG_FREQ,
62 					 &new_expected_data, sizeof(new_expected_data));
63 	zassert_true(ret > 0, "Unexpected return value %d", ret);
64 
65 	ret = bt_audio_codec_cfg_get_val(&codec_cfg, BT_AUDIO_CODEC_CFG_FREQ, &data);
66 	zassert_equal(ret, sizeof(new_expected_data), "Unexpected return value %d", ret);
67 	zassert_equal(data[0], new_expected_data, "Unexpected data value %u", data[0]);
68 }
69 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_set_val_new_value)70 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_set_val_new_value)
71 {
72 	struct bt_audio_codec_cfg codec_cfg =
73 		BT_AUDIO_CODEC_CFG(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {}, {});
74 	const uint8_t new_expected_data = BT_AUDIO_CODEC_CFG_FREQ_48KHZ;
75 	const uint8_t *data;
76 	int ret;
77 
78 	ret = bt_audio_codec_cfg_get_val(&codec_cfg, BT_AUDIO_CODEC_CFG_FREQ, &data);
79 	zassert_equal(ret, -ENODATA, "Unexpected return value %d", ret);
80 
81 	ret = bt_audio_codec_cfg_set_val(&codec_cfg, BT_AUDIO_CODEC_CFG_FREQ,
82 					 &new_expected_data, sizeof(new_expected_data));
83 	zassert_true(ret > 0, "Unexpected return value %d", ret);
84 
85 	ret = bt_audio_codec_cfg_get_val(&codec_cfg, BT_AUDIO_CODEC_CFG_FREQ, &data);
86 	zassert_equal(ret, sizeof(new_expected_data), "Unexpected return value %d", ret);
87 	zassert_equal(data[0], new_expected_data, "Unexpected return value %d", ret);
88 }
89 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_unset_val)90 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_unset_val)
91 {
92 	struct bt_audio_codec_cfg codec_cfg =
93 		BT_AUDIO_CODEC_CFG(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000,
94 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_CODEC_CFG_FREQ,
95 							BT_AUDIO_CODEC_CFG_FREQ_16KHZ)},
96 				   {});
97 	const uint8_t expected_data = BT_AUDIO_CODEC_CFG_FREQ_16KHZ;
98 	const uint8_t *data;
99 	int ret;
100 
101 	ret = bt_audio_codec_cfg_get_val(&codec_cfg, BT_AUDIO_CODEC_CFG_FREQ, &data);
102 	zassert_equal(ret, sizeof(expected_data), "Unexpected return value %d", ret);
103 	zassert_equal(data[0], expected_data, "Unexpected return value %d", ret);
104 
105 	ret = bt_audio_codec_cfg_unset_val(&codec_cfg, BT_AUDIO_CODEC_CFG_FREQ);
106 	zassert_true(ret >= 0, "Unexpected return value %d", ret);
107 
108 	ret = bt_audio_codec_cfg_get_val(&codec_cfg, BT_AUDIO_CODEC_CFG_FREQ, &data);
109 	zassert_equal(ret, -ENODATA, "Unexpected return value %d", ret);
110 }
111 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_freq_to_freq_hz)112 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_freq_to_freq_hz)
113 {
114 	const struct freq_test_input {
115 		enum bt_audio_codec_cfg_freq freq;
116 		uint32_t freq_hz;
117 	} freq_test_inputs[] = {
118 		{.freq = BT_AUDIO_CODEC_CFG_FREQ_8KHZ, .freq_hz = 8000U},
119 		{.freq = BT_AUDIO_CODEC_CFG_FREQ_11KHZ, .freq_hz = 11025U},
120 		{.freq = BT_AUDIO_CODEC_CFG_FREQ_16KHZ, .freq_hz = 16000U},
121 		{.freq = BT_AUDIO_CODEC_CFG_FREQ_22KHZ, .freq_hz = 22050U},
122 		{.freq = BT_AUDIO_CODEC_CFG_FREQ_24KHZ, .freq_hz = 24000U},
123 		{.freq = BT_AUDIO_CODEC_CFG_FREQ_32KHZ, .freq_hz = 32000U},
124 		{.freq = BT_AUDIO_CODEC_CFG_FREQ_44KHZ, .freq_hz = 44100U},
125 		{.freq = BT_AUDIO_CODEC_CFG_FREQ_48KHZ, .freq_hz = 48000U},
126 		{.freq = BT_AUDIO_CODEC_CFG_FREQ_88KHZ, .freq_hz = 88200U},
127 		{.freq = BT_AUDIO_CODEC_CFG_FREQ_96KHZ, .freq_hz = 96000U},
128 		{.freq = BT_AUDIO_CODEC_CFG_FREQ_176KHZ, .freq_hz = 176400U},
129 		{.freq = BT_AUDIO_CODEC_CFG_FREQ_192KHZ, .freq_hz = 192000U},
130 		{.freq = BT_AUDIO_CODEC_CFG_FREQ_384KHZ, .freq_hz = 384000U},
131 	};
132 
133 	for (size_t i = 0U; i < ARRAY_SIZE(freq_test_inputs); i++) {
134 		const struct freq_test_input *fti = &freq_test_inputs[i];
135 
136 		zassert_equal(bt_audio_codec_cfg_freq_to_freq_hz(fti->freq), fti->freq_hz,
137 			      "freq %d was not coverted to %u", fti->freq, fti->freq_hz);
138 		zassert_equal(bt_audio_codec_cfg_freq_hz_to_freq(fti->freq_hz), fti->freq,
139 			      "freq_hz %u was not coverted to %d", fti->freq_hz, fti->freq);
140 	}
141 }
142 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_get_freq)143 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_get_freq)
144 {
145 	const struct bt_bap_lc3_preset preset =
146 		BT_BAP_LC3_UNICAST_PRESET_16_2_1(BT_AUDIO_LOCATION_FRONT_LEFT,
147 						 BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
148 	int ret;
149 
150 	ret = bt_audio_codec_cfg_get_freq(&preset.codec_cfg);
151 	zassert_equal(ret, 0x03, "unexpected return value %d", ret);
152 }
153 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_set_val_new)154 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_set_val_new)
155 {
156 	struct bt_bap_lc3_preset preset = BT_BAP_LC3_UNICAST_PRESET_16_2_1(
157 		BT_AUDIO_LOCATION_FRONT_LEFT, BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
158 	const uint8_t frame_blocks = 0x02;
159 	int ret;
160 
161 	/* Frame blocks are not part of the preset, so we can use that to test adding a new type to
162 	 * the config
163 	 */
164 	ret = bt_audio_codec_cfg_get_frame_blocks_per_sdu(&preset.codec_cfg, false);
165 	zassert_equal(ret, -ENODATA, "Unexpected return value %d", ret);
166 
167 	ret = bt_audio_codec_cfg_set_frame_blocks_per_sdu(&preset.codec_cfg, frame_blocks);
168 	zassert_true(ret > 0, "Unexpected return value %d", ret);
169 
170 	ret = bt_audio_codec_cfg_get_frame_blocks_per_sdu(&preset.codec_cfg, false);
171 	zassert_equal(ret, frame_blocks, "Unexpected return value %d", ret);
172 }
173 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_set_freq)174 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_set_freq)
175 {
176 	struct bt_bap_lc3_preset preset = BT_BAP_LC3_UNICAST_PRESET_16_2_1(
177 		BT_AUDIO_LOCATION_FRONT_LEFT, BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
178 	int ret;
179 
180 	ret = bt_audio_codec_cfg_get_freq(&preset.codec_cfg);
181 	zassert_equal(ret, 0x03, "Unexpected return value %d", ret);
182 
183 	ret = bt_audio_codec_cfg_set_freq(&preset.codec_cfg, BT_AUDIO_CODEC_CFG_FREQ_32KHZ);
184 	zassert_true(ret > 0, "Unexpected return value %d", ret);
185 
186 	ret = bt_audio_codec_cfg_get_freq(&preset.codec_cfg);
187 	zassert_equal(ret, 0x06, "Unexpected return value %d", ret);
188 }
189 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_frame_dur_to_frame_dur_us)190 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_frame_dur_to_frame_dur_us)
191 {
192 	const struct frame_dur_test_input {
193 		enum bt_audio_codec_cfg_frame_dur frame_dur;
194 		uint32_t frame_dur_us;
195 	} frame_dur_test_inputs[] = {
196 		{.frame_dur = BT_AUDIO_CODEC_CFG_DURATION_7_5, .frame_dur_us = 7500U},
197 		{.frame_dur = BT_AUDIO_CODEC_CFG_DURATION_10, .frame_dur_us = 10000U},
198 	};
199 
200 	for (size_t i = 0U; i < ARRAY_SIZE(frame_dur_test_inputs); i++) {
201 		const struct frame_dur_test_input *fdti = &frame_dur_test_inputs[i];
202 
203 		zassert_equal(bt_audio_codec_cfg_frame_dur_to_frame_dur_us(fdti->frame_dur),
204 			      fdti->frame_dur_us, "frame_dur %d was not coverted to %u",
205 			      fdti->frame_dur, fdti->frame_dur_us);
206 		zassert_equal(bt_audio_codec_cfg_frame_dur_us_to_frame_dur(fdti->frame_dur_us),
207 			      fdti->frame_dur, "frame_dur_us %u was not coverted to %d",
208 			      fdti->frame_dur_us, fdti->frame_dur);
209 	}
210 }
211 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_get_frame_dur)212 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_get_frame_dur)
213 {
214 	const struct bt_bap_lc3_preset preset =
215 		BT_BAP_LC3_UNICAST_PRESET_48_2_2(BT_AUDIO_LOCATION_FRONT_LEFT,
216 						 BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
217 	int ret;
218 
219 	ret = bt_audio_codec_cfg_get_frame_dur(&preset.codec_cfg);
220 	zassert_equal(ret, 0x01, "unexpected return value %d", ret);
221 }
222 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_set_frame_dur)223 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_set_frame_dur)
224 {
225 	struct bt_bap_lc3_preset preset = BT_BAP_LC3_UNICAST_PRESET_16_2_1(
226 		BT_AUDIO_LOCATION_FRONT_LEFT, BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
227 	int ret;
228 
229 	ret = bt_audio_codec_cfg_get_frame_dur(&preset.codec_cfg);
230 	zassert_equal(ret, 0x01, "Unexpected return value %d", ret);
231 
232 	ret = bt_audio_codec_cfg_set_frame_dur(&preset.codec_cfg,
233 					       BT_AUDIO_CODEC_CFG_DURATION_7_5);
234 	zassert_true(ret > 0, "Unexpected return value %d", ret);
235 
236 	ret = bt_audio_codec_cfg_get_frame_dur(&preset.codec_cfg);
237 	zassert_equal(ret, 0x00, "Unexpected return value %d", ret);
238 }
239 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_get_chan_allocation)240 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_get_chan_allocation)
241 {
242 	const struct bt_bap_lc3_preset preset = BT_BAP_LC3_UNICAST_PRESET_8_1_1(
243 		BT_AUDIO_LOCATION_FRONT_LEFT, BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
244 	enum bt_audio_location chan_allocation = BT_AUDIO_LOCATION_FRONT_RIGHT;
245 	int err;
246 
247 	err = bt_audio_codec_cfg_get_chan_allocation(&preset.codec_cfg, &chan_allocation, false);
248 	zassert_false(err, "unexpected error %d", err);
249 	zassert_equal(chan_allocation, BT_AUDIO_LOCATION_FRONT_LEFT, "unexpected return value %d",
250 		      chan_allocation);
251 }
252 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_get_chan_allocation_lc3_fallback_true)253 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_get_chan_allocation_lc3_fallback_true)
254 {
255 	struct bt_audio_codec_cfg codec_cfg = {.id = BT_HCI_CODING_FORMAT_LC3};
256 	enum bt_audio_location chan_allocation;
257 	int err;
258 
259 	err = bt_audio_codec_cfg_get_chan_allocation(&codec_cfg, &chan_allocation, true);
260 	zassert_equal(err, 0, "unexpected error %d", err);
261 	zassert_equal(chan_allocation, BT_AUDIO_LOCATION_MONO_AUDIO, "unexpected return value %d",
262 		      chan_allocation);
263 }
264 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_get_chan_allocation_lc3_fallback_false)265 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_get_chan_allocation_lc3_fallback_false)
266 {
267 	struct bt_audio_codec_cfg codec_cfg = {.id = BT_HCI_CODING_FORMAT_LC3};
268 	enum bt_audio_location chan_allocation;
269 	int err;
270 
271 	err = bt_audio_codec_cfg_get_chan_allocation(&codec_cfg, &chan_allocation, false);
272 	zassert_equal(err, -ENODATA, "unexpected error %d", err);
273 }
274 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_get_chan_allocation_fallback_true)275 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_get_chan_allocation_fallback_true)
276 {
277 	struct bt_audio_codec_cfg codec_cfg = {0};
278 	enum bt_audio_location chan_allocation;
279 	int err;
280 
281 	err = bt_audio_codec_cfg_get_chan_allocation(&codec_cfg, &chan_allocation, true);
282 	zassert_equal(err, -ENODATA, "unexpected error %d", err);
283 }
284 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_get_chan_allocation_fallback_false)285 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_get_chan_allocation_fallback_false)
286 {
287 	struct bt_audio_codec_cfg codec_cfg = {0};
288 	enum bt_audio_location chan_allocation;
289 	int err;
290 
291 	err = bt_audio_codec_cfg_get_chan_allocation(&codec_cfg, &chan_allocation, false);
292 	zassert_equal(err, -ENODATA, "unexpected error %d", err);
293 }
294 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_set_chan_allocation)295 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_set_chan_allocation)
296 {
297 	struct bt_bap_lc3_preset preset = BT_BAP_LC3_UNICAST_PRESET_16_2_1(
298 		BT_AUDIO_LOCATION_FRONT_LEFT, BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
299 	enum bt_audio_location chan_allocation;
300 	int err;
301 
302 	err = bt_audio_codec_cfg_get_chan_allocation(&preset.codec_cfg, &chan_allocation, false);
303 	zassert_equal(err, 0, "Unexpected return value %d", err);
304 	zassert_equal(chan_allocation, 0x00000001, "Unexpected chan_allocation value %d",
305 		      chan_allocation);
306 
307 	chan_allocation = BT_AUDIO_LOCATION_FRONT_RIGHT | BT_AUDIO_LOCATION_SIDE_RIGHT |
308 			  BT_AUDIO_LOCATION_TOP_SIDE_RIGHT | BT_AUDIO_LOCATION_RIGHT_SURROUND;
309 	err = bt_audio_codec_cfg_set_chan_allocation(&preset.codec_cfg, chan_allocation);
310 	zassert_true(err > 0, "Unexpected return value %d", err);
311 
312 	err = bt_audio_codec_cfg_get_chan_allocation(&preset.codec_cfg, &chan_allocation, false);
313 	zassert_equal(err, 0, "Unexpected return value %d", err);
314 	zassert_equal(chan_allocation, 0x8080802, "Unexpected chan_allocation value %d",
315 		      chan_allocation);
316 }
317 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_get_octets_per_frame)318 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_get_octets_per_frame)
319 {
320 	const struct bt_bap_lc3_preset preset =
321 		BT_BAP_LC3_UNICAST_PRESET_32_2_2(BT_AUDIO_LOCATION_FRONT_LEFT,
322 						 BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
323 	int ret;
324 
325 	ret = bt_audio_codec_cfg_get_octets_per_frame(&preset.codec_cfg);
326 	zassert_equal(ret, 80u, "unexpected return value %d", ret);
327 }
328 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_set_octets_per_frame)329 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_set_octets_per_frame)
330 {
331 	struct bt_bap_lc3_preset preset = BT_BAP_LC3_UNICAST_PRESET_32_2_2(
332 		BT_AUDIO_LOCATION_FRONT_LEFT, BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
333 	int ret;
334 
335 	ret = bt_audio_codec_cfg_get_octets_per_frame(&preset.codec_cfg);
336 	zassert_equal(ret, 80, "Unexpected return value %d", ret);
337 
338 	ret = bt_audio_codec_cfg_set_octets_per_frame(&preset.codec_cfg, 120);
339 	zassert_true(ret > 0, "Unexpected return value %d", ret);
340 
341 	ret = bt_audio_codec_cfg_get_octets_per_frame(&preset.codec_cfg);
342 	zassert_equal(ret, 120, "Unexpected return value %d", ret);
343 }
344 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_get_frame_blocks_per_sdu)345 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_get_frame_blocks_per_sdu)
346 {
347 	const struct bt_bap_lc3_preset preset =
348 		BT_BAP_LC3_UNICAST_PRESET_48_5_1(BT_AUDIO_LOCATION_FRONT_LEFT,
349 						 BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
350 	int ret;
351 
352 	ret = bt_audio_codec_cfg_get_frame_blocks_per_sdu(&preset.codec_cfg, true);
353 	zassert_equal(ret, 1u, "unexpected return value %d", ret);
354 }
355 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_get_frame_blocks_per_sdu_lc3_fallback_true)356 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_get_frame_blocks_per_sdu_lc3_fallback_true)
357 {
358 	struct bt_audio_codec_cfg codec_cfg = {.id = BT_HCI_CODING_FORMAT_LC3};
359 	int err;
360 
361 	err = bt_audio_codec_cfg_get_frame_blocks_per_sdu(&codec_cfg, true);
362 	zassert_equal(err, 1, "unexpected error %d", err);
363 }
364 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_get_frame_blocks_per_sdu_lc3_fallback_false)365 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_get_frame_blocks_per_sdu_lc3_fallback_false)
366 {
367 	struct bt_audio_codec_cfg codec_cfg = {.id = BT_HCI_CODING_FORMAT_LC3};
368 	int err;
369 
370 	err = bt_audio_codec_cfg_get_frame_blocks_per_sdu(&codec_cfg, false);
371 	zassert_equal(err, -ENODATA, "unexpected error %d", err);
372 }
373 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_get_frame_blocks_per_sdu_fallback_true)374 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_get_frame_blocks_per_sdu_fallback_true)
375 {
376 	struct bt_audio_codec_cfg codec_cfg = {0};
377 	int err;
378 
379 	err = bt_audio_codec_cfg_get_frame_blocks_per_sdu(&codec_cfg, true);
380 	zassert_equal(err, -ENODATA, "unexpected error %d", err);
381 }
382 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_get_frame_blocks_per_sdu_fallback_false)383 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_get_frame_blocks_per_sdu_fallback_false)
384 {
385 	struct bt_audio_codec_cfg codec_cfg = {0};
386 	int err;
387 
388 	err = bt_audio_codec_cfg_get_frame_blocks_per_sdu(&codec_cfg, false);
389 	zassert_equal(err, -ENODATA, "unexpected error %d", err);
390 }
391 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_set_frame_blocks_per_sdu)392 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_set_frame_blocks_per_sdu)
393 {
394 	struct bt_bap_lc3_preset preset = BT_BAP_LC3_UNICAST_PRESET_32_2_2(
395 		BT_AUDIO_LOCATION_FRONT_LEFT, BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
396 	int ret;
397 
398 	ret = bt_audio_codec_cfg_get_frame_blocks_per_sdu(&preset.codec_cfg, true);
399 	zassert_equal(ret, 1, "Unexpected return value %d", ret);
400 
401 	ret = bt_audio_codec_cfg_set_frame_blocks_per_sdu(&preset.codec_cfg, 2U);
402 	zassert_true(ret > 0, "Unexpected return value %d", ret);
403 
404 	ret = bt_audio_codec_cfg_get_frame_blocks_per_sdu(&preset.codec_cfg, true);
405 	zassert_equal(ret, 2, "Unexpected return value %d", ret);
406 }
407 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_get_val)408 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_get_val)
409 {
410 	struct bt_audio_codec_cfg codec_cfg =
411 		BT_AUDIO_CODEC_CFG(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
412 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
413 							BT_AUDIO_PARENTAL_RATING_AGE_10_OR_ABOVE)});
414 	const uint8_t expected_data = BT_AUDIO_PARENTAL_RATING_AGE_10_OR_ABOVE;
415 	const uint8_t *data;
416 	int ret;
417 
418 	ret = bt_audio_codec_cfg_meta_get_val(&codec_cfg, BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
419 					      &data);
420 	zassert_equal(ret, sizeof(expected_data), "Unexpected return value %d", ret);
421 	zassert_equal(data[0], expected_data, "Unexpected return value %d", ret);
422 }
423 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_set_val)424 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_set_val)
425 {
426 	struct bt_audio_codec_cfg codec_cfg =
427 		BT_AUDIO_CODEC_CFG(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
428 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
429 							BT_AUDIO_PARENTAL_RATING_AGE_10_OR_ABOVE)});
430 	const uint8_t new_expected_data = BT_AUDIO_PARENTAL_RATING_AGE_13_OR_ABOVE;
431 	const uint8_t expected_data = BT_AUDIO_PARENTAL_RATING_AGE_10_OR_ABOVE;
432 	const uint8_t *data;
433 	int ret;
434 
435 	ret = bt_audio_codec_cfg_meta_get_val(&codec_cfg, BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
436 					      &data);
437 	zassert_equal(ret, sizeof(expected_data), "Unexpected return value %d", ret);
438 	zassert_equal(data[0], expected_data, "Unexpected return value %d", ret);
439 
440 	ret = bt_audio_codec_cfg_meta_set_val(&codec_cfg, BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
441 					      &new_expected_data, sizeof(new_expected_data));
442 	zassert_true(ret > 0, "Unexpected return value %d", ret);
443 
444 	ret = bt_audio_codec_cfg_meta_get_val(&codec_cfg, BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
445 					      &data);
446 	zassert_equal(ret, sizeof(new_expected_data), "Unexpected return value %d", ret);
447 	zassert_equal(data[0], new_expected_data, "Unexpected data value %u", data[0]);
448 }
449 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_set_val_new)450 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_set_val_new)
451 {
452 	struct bt_audio_codec_cfg codec_cfg =
453 		BT_AUDIO_CODEC_CFG(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {}, {});
454 	const uint8_t new_expected_data = BT_AUDIO_PARENTAL_RATING_AGE_13_OR_ABOVE;
455 	const uint8_t *data;
456 	int ret;
457 
458 	ret = bt_audio_codec_cfg_meta_get_val(&codec_cfg, BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
459 					      &data);
460 	zassert_equal(ret, -ENODATA, "Unexpected return value %d", ret);
461 
462 	ret = bt_audio_codec_cfg_meta_set_val(&codec_cfg, BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
463 					      &new_expected_data, sizeof(new_expected_data));
464 	zassert_true(ret > 0, "Unexpected return value %d", ret);
465 
466 	ret = bt_audio_codec_cfg_meta_get_val(&codec_cfg, BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
467 					      &data);
468 	zassert_equal(ret, sizeof(new_expected_data), "Unexpected return value %d", ret);
469 	zassert_equal(data[0], new_expected_data, "Unexpected return value %d", ret);
470 }
471 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_unset_val)472 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_unset_val)
473 {
474 	struct bt_audio_codec_cfg codec_cfg =
475 		BT_AUDIO_CODEC_CFG(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
476 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
477 							BT_AUDIO_PARENTAL_RATING_AGE_10_OR_ABOVE)});
478 	const uint8_t expected_data = BT_AUDIO_PARENTAL_RATING_AGE_10_OR_ABOVE;
479 	const uint8_t *data;
480 	int ret;
481 
482 	ret = bt_audio_codec_cfg_meta_get_val(&codec_cfg, BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
483 					      &data);
484 	zassert_equal(ret, sizeof(expected_data), "Unexpected return value %d", ret);
485 	zassert_equal(data[0], expected_data, "Unexpected return value %d", ret);
486 
487 	ret = bt_audio_codec_cfg_meta_unset_val(&codec_cfg, BT_AUDIO_METADATA_TYPE_PARENTAL_RATING);
488 	zassert_true(ret >= 0, "Unexpected return value %d", ret);
489 
490 	ret = bt_audio_codec_cfg_meta_get_val(&codec_cfg, BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
491 					      &data);
492 	zassert_equal(ret, -ENODATA, "Unexpected return value %d", ret);
493 }
494 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_get_pref_context)495 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_get_pref_context)
496 {
497 	const enum bt_audio_context ctx =
498 		BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED | BT_AUDIO_CONTEXT_TYPE_MEDIA;
499 	const struct bt_audio_codec_cfg codec_cfg =
500 		BT_AUDIO_CODEC_CFG(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
501 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PREF_CONTEXT,
502 							BT_BYTES_LIST_LE16(ctx))});
503 	int ret;
504 
505 	ret = bt_audio_codec_cfg_meta_get_pref_context(&codec_cfg, false);
506 	zassert_equal(ret, 0x0005, "unexpected return value %d", ret);
507 }
508 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_get_pref_context_lc3_fallback_true)509 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_get_pref_context_lc3_fallback_true)
510 {
511 	struct bt_audio_codec_cfg codec_cfg = {.id = BT_HCI_CODING_FORMAT_LC3};
512 	int err;
513 
514 	err = bt_audio_codec_cfg_meta_get_pref_context(&codec_cfg, true);
515 	zassert_equal(err, BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED, "unexpected error %d", err);
516 }
517 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_get_pref_context_lc3_fallback_false)518 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_get_pref_context_lc3_fallback_false)
519 {
520 	struct bt_audio_codec_cfg codec_cfg = {.id = BT_HCI_CODING_FORMAT_LC3};
521 	int err;
522 
523 	err = bt_audio_codec_cfg_meta_get_pref_context(&codec_cfg, false);
524 	zassert_equal(err, -ENODATA, "unexpected error %d", err);
525 }
526 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_get_pref_context_fallback_true)527 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_get_pref_context_fallback_true)
528 {
529 	struct bt_audio_codec_cfg codec_cfg = {0};
530 	int err;
531 
532 	err = bt_audio_codec_cfg_meta_get_pref_context(&codec_cfg, true);
533 	zassert_equal(err, -ENODATA, "unexpected error %d", err);
534 }
535 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_get_pref_context_fallback_false)536 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_get_pref_context_fallback_false)
537 {
538 	struct bt_audio_codec_cfg codec_cfg = {0};
539 	int err;
540 
541 	err = bt_audio_codec_cfg_meta_get_pref_context(&codec_cfg, true);
542 	zassert_equal(err, -ENODATA, "unexpected error %d", err);
543 }
544 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_set_pref_context)545 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_set_pref_context)
546 {
547 	const enum bt_audio_context ctx =
548 		BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED | BT_AUDIO_CONTEXT_TYPE_MEDIA;
549 	const enum bt_audio_context new_ctx = BT_AUDIO_CONTEXT_TYPE_NOTIFICATIONS;
550 	struct bt_audio_codec_cfg codec_cfg =
551 		BT_AUDIO_CODEC_CFG(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
552 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PREF_CONTEXT,
553 							BT_BYTES_LIST_LE16(ctx))});
554 	int ret;
555 
556 	ret = bt_audio_codec_cfg_meta_get_pref_context(&codec_cfg, false);
557 	zassert_equal(ret, 0x0005, "Unexpected return value %d", ret);
558 
559 	ret = bt_audio_codec_cfg_meta_set_pref_context(&codec_cfg, new_ctx);
560 	zassert_true(ret > 0, "Unexpected return value %d", ret);
561 
562 	ret = bt_audio_codec_cfg_meta_get_pref_context(&codec_cfg, false);
563 	zassert_equal(ret, 0x0100, "Unexpected return value %d", ret);
564 }
565 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_get_stream_context)566 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_get_stream_context)
567 {
568 	const enum bt_audio_context ctx =
569 		BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED | BT_AUDIO_CONTEXT_TYPE_MEDIA;
570 	const struct bt_audio_codec_cfg codec_cfg =
571 		BT_AUDIO_CODEC_CFG(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
572 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_STREAM_CONTEXT,
573 							BT_BYTES_LIST_LE16(ctx))});
574 	int ret;
575 
576 	ret = bt_audio_codec_cfg_meta_get_stream_context(&codec_cfg);
577 	zassert_equal(ret, 0x0005, "unexpected return value %d", ret);
578 }
579 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_set_stream_context)580 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_set_stream_context)
581 {
582 	enum bt_audio_context ctx = BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED | BT_AUDIO_CONTEXT_TYPE_MEDIA;
583 	struct bt_audio_codec_cfg codec_cfg =
584 		BT_AUDIO_CODEC_CFG(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
585 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_STREAM_CONTEXT,
586 							BT_BYTES_LIST_LE16(ctx))});
587 	int ret;
588 
589 	ret = bt_audio_codec_cfg_meta_get_stream_context(&codec_cfg);
590 	zassert_equal(ret, 0x0005, "Unexpected return value %d", ret);
591 
592 	ctx = BT_AUDIO_CONTEXT_TYPE_NOTIFICATIONS;
593 	ret = bt_audio_codec_cfg_meta_set_stream_context(&codec_cfg, ctx);
594 	zassert_true(ret > 0, "Unexpected return value %d", ret);
595 
596 	ret = bt_audio_codec_cfg_meta_get_stream_context(&codec_cfg);
597 	zassert_equal(ret, 0x0100, "Unexpected return value %d", ret);
598 }
599 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_get_program_info)600 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_get_program_info)
601 {
602 	const uint8_t expected_data[] = {'P', 'r', 'o', 'g', 'r', 'a', 'm', ' ',
603 					 'I', 'n', 'f', 'o'};
604 	const struct bt_audio_codec_cfg codec_cfg = BT_AUDIO_CODEC_CFG(
605 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
606 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PROGRAM_INFO,
607 				     'P', 'r', 'o', 'g', 'r', 'a', 'm', ' ',
608 				     'I', 'n', 'f', 'o')});
609 	const uint8_t *program_data;
610 	int ret;
611 
612 	ret = bt_audio_codec_cfg_meta_get_program_info(&codec_cfg, &program_data);
613 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
614 	zassert_mem_equal(expected_data, program_data, ARRAY_SIZE(expected_data));
615 }
616 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_set_program_info)617 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_set_program_info)
618 {
619 	const uint8_t expected_data[] = {'P', 'r', 'o', 'g', 'r', 'a',
620 					 'm', ' ', 'I', 'n', 'f', 'o'};
621 	const uint8_t new_expected_data[] = {'N', 'e', 'w', ' ', 'i', 'n', 'f', 'o'};
622 	struct bt_audio_codec_cfg codec_cfg = BT_AUDIO_CODEC_CFG(
623 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
624 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PROGRAM_INFO, 'P', 'r', 'o', 'g', 'r',
625 				     'a', 'm', ' ', 'I', 'n', 'f', 'o')});
626 	const uint8_t *program_data;
627 	int ret;
628 
629 	ret = bt_audio_codec_cfg_meta_get_program_info(&codec_cfg, &program_data);
630 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
631 	zassert_mem_equal(expected_data, program_data, ARRAY_SIZE(expected_data));
632 
633 	ret = bt_audio_codec_cfg_meta_set_program_info(&codec_cfg, new_expected_data,
634 						       ARRAY_SIZE(new_expected_data));
635 	zassert_true(ret > 0, "Unexpected return value %d", ret);
636 
637 	ret = bt_audio_codec_cfg_meta_get_program_info(&codec_cfg, &program_data);
638 	zassert_equal(ret, ARRAY_SIZE(new_expected_data), "Unexpected return value %d", ret);
639 	zassert_mem_equal(new_expected_data, program_data, ARRAY_SIZE(new_expected_data));
640 }
641 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_get_lang)642 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_get_lang)
643 {
644 	const struct bt_audio_codec_cfg codec_cfg = BT_AUDIO_CODEC_CFG(
645 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
646 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_LANG, 'e', 'n', 'g')});
647 	char expected_data[] = "eng";
648 	const uint8_t *lang;
649 	int ret;
650 
651 	ret = bt_audio_codec_cfg_meta_get_lang(&codec_cfg, &lang);
652 	zassert_equal(ret, 0, "Unexpected return value %d", ret);
653 	zassert_mem_equal(expected_data, lang, BT_AUDIO_LANG_SIZE);
654 }
655 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_set_lang)656 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_set_lang)
657 {
658 	struct bt_audio_codec_cfg codec_cfg = BT_AUDIO_CODEC_CFG(
659 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
660 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_LANG, 'e', 'n', 'g')});
661 	const uint8_t new_expected_data[] = "deu";
662 	char expected_data[] = "eng";
663 	const uint8_t *lang;
664 	int ret;
665 
666 	ret = bt_audio_codec_cfg_meta_get_lang(&codec_cfg, &lang);
667 	zassert_equal(ret, 0, "Unexpected return value %d", ret);
668 	zassert_mem_equal(expected_data, lang, BT_AUDIO_LANG_SIZE);
669 
670 	ret = bt_audio_codec_cfg_meta_set_lang(&codec_cfg, new_expected_data);
671 	zassert_true(ret > 0, "Unexpected return value %d", ret);
672 
673 	ret = bt_audio_codec_cfg_meta_get_lang(&codec_cfg, &lang);
674 	zassert_equal(ret, 0, "Unexpected return value %d", ret);
675 	zassert_mem_equal(new_expected_data, lang, BT_AUDIO_LANG_SIZE);
676 }
677 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_get_ccid_list)678 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_get_ccid_list)
679 {
680 	const uint8_t expected_data[] = {0x05, 0x10, 0x15};
681 	const struct bt_audio_codec_cfg codec_cfg = BT_AUDIO_CODEC_CFG(
682 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
683 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_CCID_LIST, 0x05, 0x10, 0x15)});
684 	const uint8_t *ccid_list;
685 	int ret;
686 
687 	ret = bt_audio_codec_cfg_meta_get_ccid_list(&codec_cfg, &ccid_list);
688 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
689 	zassert_mem_equal(expected_data, ccid_list, ARRAY_SIZE(expected_data));
690 }
691 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_set_ccid_list_shorter)692 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_set_ccid_list_shorter)
693 {
694 	const uint8_t expected_data[] = {0x05, 0x10, 0x15};
695 	const uint8_t new_expected_data[] = {0x25, 0x30};
696 	struct bt_audio_codec_cfg codec_cfg = BT_AUDIO_CODEC_CFG(
697 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
698 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_CCID_LIST, 0x05, 0x10, 0x15)});
699 	const uint8_t *ccid_list;
700 	int ret;
701 
702 	ret = bt_audio_codec_cfg_meta_get_ccid_list(&codec_cfg, &ccid_list);
703 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
704 	zassert_mem_equal(expected_data, ccid_list, ARRAY_SIZE(expected_data));
705 
706 	ret = bt_audio_codec_cfg_meta_set_ccid_list(&codec_cfg, new_expected_data,
707 						    ARRAY_SIZE(new_expected_data));
708 	zassert_true(ret > 0, "Unexpected return value %d", ret);
709 
710 	ret = bt_audio_codec_cfg_meta_get_ccid_list(&codec_cfg, &ccid_list);
711 	zassert_equal(ret, ARRAY_SIZE(new_expected_data), "Unexpected return value %d", ret);
712 	zassert_mem_equal(new_expected_data, ccid_list, ARRAY_SIZE(new_expected_data));
713 }
714 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_set_ccid_list_longer)715 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_set_ccid_list_longer)
716 {
717 	const uint8_t expected_data[] = {0x05, 0x10, 0x15};
718 	const uint8_t new_expected_data[] = {0x25, 0x30, 0x35, 0x40};
719 	struct bt_audio_codec_cfg codec_cfg = BT_AUDIO_CODEC_CFG(
720 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
721 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_CCID_LIST, 0x05, 0x10, 0x15)});
722 	const uint8_t *ccid_list;
723 	int ret;
724 
725 	ret = bt_audio_codec_cfg_meta_get_ccid_list(&codec_cfg, &ccid_list);
726 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
727 	zassert_mem_equal(expected_data, ccid_list, ARRAY_SIZE(expected_data));
728 
729 	ret = bt_audio_codec_cfg_meta_set_ccid_list(&codec_cfg, new_expected_data,
730 						    ARRAY_SIZE(new_expected_data));
731 	zassert_true(ret > 0, "Unexpected return value %d", ret);
732 
733 	ret = bt_audio_codec_cfg_meta_get_ccid_list(&codec_cfg, &ccid_list);
734 	zassert_equal(ret, ARRAY_SIZE(new_expected_data), "Unexpected return value %d", ret);
735 	zassert_mem_equal(new_expected_data, ccid_list, ARRAY_SIZE(new_expected_data));
736 }
737 
738 /* Providing multiple BT_AUDIO_CODEC_DATA to BT_AUDIO_CODEC_CFG without packing it in a macro
739  * cause compile issue, so define a macro to denote 2 types of data for the ccid_list_first tests
740  */
741 #define DOUBLE_CFG_DATA                                                                            \
742 	{                                                                                          \
743 		BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_CCID_LIST, 0x05, 0x10, 0x15),           \
744 			BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,                \
745 					    BT_AUDIO_PARENTAL_RATING_AGE_10_OR_ABOVE)              \
746 	}
747 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_set_ccid_list_first_shorter)748 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_set_ccid_list_first_shorter)
749 {
750 	const uint8_t expected_data[] = {0x05, 0x10, 0x15};
751 	const uint8_t new_expected_data[] = {0x25, 0x30};
752 	struct bt_audio_codec_cfg codec_cfg =
753 		BT_AUDIO_CODEC_CFG(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {}, DOUBLE_CFG_DATA);
754 	const uint8_t *ccid_list;
755 	int ret;
756 
757 	ret = bt_audio_codec_cfg_meta_get_ccid_list(&codec_cfg, &ccid_list);
758 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
759 	zassert_mem_equal(expected_data, ccid_list, ARRAY_SIZE(expected_data));
760 
761 	ret = bt_audio_codec_cfg_meta_get_parental_rating(&codec_cfg);
762 	zassert_equal(ret, 0x07, "Unexpected return value %d", ret);
763 
764 	ret = bt_audio_codec_cfg_meta_set_ccid_list(&codec_cfg, new_expected_data,
765 						    ARRAY_SIZE(new_expected_data));
766 	zassert_true(ret > 0, "Unexpected return value %d", ret);
767 
768 	ret = bt_audio_codec_cfg_meta_get_ccid_list(&codec_cfg, &ccid_list);
769 	zassert_equal(ret, ARRAY_SIZE(new_expected_data), "Unexpected return value %d", ret);
770 	zassert_mem_equal(new_expected_data, ccid_list, ARRAY_SIZE(new_expected_data));
771 
772 	ret = bt_audio_codec_cfg_meta_get_parental_rating(&codec_cfg);
773 	zassert_equal(ret, 0x07, "Unexpected return value %d", ret);
774 }
775 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_set_ccid_list_first_longer)776 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_set_ccid_list_first_longer)
777 {
778 	const uint8_t expected_data[] = {0x05, 0x10, 0x15};
779 	const uint8_t new_expected_data[] = {0x25, 0x30, 0x35, 0x40};
780 	struct bt_audio_codec_cfg codec_cfg =
781 		BT_AUDIO_CODEC_CFG(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {}, DOUBLE_CFG_DATA);
782 	const uint8_t *ccid_list;
783 	int ret;
784 
785 	ret = bt_audio_codec_cfg_meta_get_ccid_list(&codec_cfg, &ccid_list);
786 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
787 	zassert_mem_equal(expected_data, ccid_list, ARRAY_SIZE(expected_data));
788 
789 	ret = bt_audio_codec_cfg_meta_get_parental_rating(&codec_cfg);
790 	zassert_equal(ret, 0x07, "Unexpected return value %d", ret);
791 
792 	ret = bt_audio_codec_cfg_meta_set_ccid_list(&codec_cfg, new_expected_data,
793 						    ARRAY_SIZE(new_expected_data));
794 	zassert_true(ret > 0, "Unexpected return value %d", ret);
795 
796 	ret = bt_audio_codec_cfg_meta_get_ccid_list(&codec_cfg, &ccid_list);
797 	zassert_equal(ret, ARRAY_SIZE(new_expected_data), "Unexpected return value %d", ret);
798 	zassert_mem_equal(new_expected_data, ccid_list, ARRAY_SIZE(new_expected_data));
799 
800 	ret = bt_audio_codec_cfg_meta_get_parental_rating(&codec_cfg);
801 	zassert_equal(ret, 0x07, "Unexpected return value %d", ret);
802 }
803 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_get_parental_rating)804 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_get_parental_rating)
805 {
806 	const struct bt_audio_codec_cfg codec_cfg =
807 		BT_AUDIO_CODEC_CFG(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
808 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
809 							BT_AUDIO_PARENTAL_RATING_AGE_10_OR_ABOVE)});
810 	int ret;
811 
812 	ret = bt_audio_codec_cfg_meta_get_parental_rating(&codec_cfg);
813 	zassert_equal(ret, 0x07, "Unexpected return value %d", ret);
814 }
815 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_set_parental_rating)816 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_set_parental_rating)
817 {
818 	struct bt_audio_codec_cfg codec_cfg =
819 		BT_AUDIO_CODEC_CFG(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
820 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
821 							BT_AUDIO_PARENTAL_RATING_AGE_10_OR_ABOVE)});
822 	int ret;
823 
824 	ret = bt_audio_codec_cfg_meta_get_parental_rating(&codec_cfg);
825 	zassert_equal(ret, 0x07, "Unexpected return value %d", ret);
826 
827 	ret = bt_audio_codec_cfg_meta_set_parental_rating(&codec_cfg,
828 							  BT_AUDIO_PARENTAL_RATING_AGE_13_OR_ABOVE);
829 	zassert_true(ret > 0, "Unexpected return value %d", ret);
830 
831 	ret = bt_audio_codec_cfg_meta_get_parental_rating(&codec_cfg);
832 	zassert_equal(ret, 0x0a, "Unexpected return value %d", ret);
833 }
834 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_get_program_info_uri)835 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_get_program_info_uri)
836 {
837 	const uint8_t expected_data[] = {'e', 'x', 'a', 'm', 'p', 'l', 'e', '.', 'c', 'o', 'm'};
838 	const struct bt_audio_codec_cfg codec_cfg = BT_AUDIO_CODEC_CFG(
839 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
840 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PROGRAM_INFO_URI,
841 				     'e', 'x', 'a', 'm', 'p', 'l', 'e', '.', 'c', 'o', 'm')});
842 	const uint8_t *program_info_uri;
843 	int ret;
844 
845 	ret = bt_audio_codec_cfg_meta_get_program_info_uri(&codec_cfg, &program_info_uri);
846 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
847 	zassert_mem_equal(expected_data, program_info_uri, ARRAY_SIZE(expected_data));
848 }
849 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_set_program_info_uri)850 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_set_program_info_uri)
851 {
852 	const uint8_t expected_data[] = {'e', 'x', 'a', 'm', 'p', 'l', 'e', '.', 'c', 'o', 'm'};
853 	const uint8_t new_expected_data[] = {'n', 'e', 'w', '.', 'c', 'o', 'm'};
854 	struct bt_audio_codec_cfg codec_cfg = BT_AUDIO_CODEC_CFG(
855 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
856 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PROGRAM_INFO_URI, 'e', 'x', 'a', 'm',
857 				     'p', 'l', 'e', '.', 'c', 'o', 'm')});
858 	const uint8_t *program_info_uri;
859 	int ret;
860 
861 	ret = bt_audio_codec_cfg_meta_get_program_info_uri(&codec_cfg, &program_info_uri);
862 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
863 	zassert_mem_equal(expected_data, program_info_uri, ARRAY_SIZE(expected_data));
864 
865 	ret = bt_audio_codec_cfg_meta_set_program_info_uri(&codec_cfg, new_expected_data,
866 							   ARRAY_SIZE(new_expected_data));
867 	zassert_true(ret > 0, "Unexpected return value %d", ret);
868 
869 	ret = bt_audio_codec_cfg_meta_get_program_info_uri(&codec_cfg, &program_info_uri);
870 	zassert_equal(ret, ARRAY_SIZE(new_expected_data), "Unexpected return value %d", ret);
871 	zassert_mem_equal(new_expected_data, program_info_uri, ARRAY_SIZE(new_expected_data));
872 }
873 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_get_audio_active_state)874 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_get_audio_active_state)
875 {
876 	const struct bt_audio_codec_cfg codec_cfg =
877 		BT_AUDIO_CODEC_CFG(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
878 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_AUDIO_STATE,
879 							BT_AUDIO_ACTIVE_STATE_ENABLED)});
880 	int ret;
881 
882 	ret = bt_audio_codec_cfg_meta_get_audio_active_state(&codec_cfg);
883 	zassert_equal(ret, 0x01, "Unexpected return value %d", ret);
884 }
885 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_set_audio_active_state)886 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_set_audio_active_state)
887 {
888 	struct bt_audio_codec_cfg codec_cfg =
889 		BT_AUDIO_CODEC_CFG(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
890 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_AUDIO_STATE,
891 							BT_AUDIO_ACTIVE_STATE_ENABLED)});
892 	int ret;
893 
894 	ret = bt_audio_codec_cfg_meta_get_audio_active_state(&codec_cfg);
895 	zassert_equal(ret, 0x01, "Unexpected return value %d", ret);
896 
897 	ret = bt_audio_codec_cfg_meta_set_audio_active_state(&codec_cfg,
898 							     BT_AUDIO_ACTIVE_STATE_DISABLED);
899 	zassert_true(ret > 0, "Unexpected return value %d", ret);
900 
901 	ret = bt_audio_codec_cfg_meta_get_audio_active_state(&codec_cfg);
902 	zassert_equal(ret, 0x00, "Unexpected return value %d", ret);
903 }
904 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_get_bcast_audio_immediate_rend_flag)905 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_get_bcast_audio_immediate_rend_flag)
906 {
907 	const struct bt_audio_codec_cfg codec_cfg = BT_AUDIO_CODEC_CFG(
908 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
909 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_BROADCAST_IMMEDIATE)});
910 	int ret;
911 
912 	ret = bt_audio_codec_cfg_meta_get_bcast_audio_immediate_rend_flag(&codec_cfg);
913 	zassert_equal(ret, 0, "Unexpected return value %d", ret);
914 }
915 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_set_bcast_audio_immediate_rend_flag)916 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_set_bcast_audio_immediate_rend_flag)
917 {
918 	struct bt_audio_codec_cfg codec_cfg =
919 		BT_AUDIO_CODEC_CFG(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {}, {});
920 	int ret;
921 
922 	ret = bt_audio_codec_cfg_meta_get_bcast_audio_immediate_rend_flag(&codec_cfg);
923 	zassert_equal(ret, -ENODATA, "Unexpected return value %d", ret);
924 
925 	ret = bt_audio_codec_cfg_meta_set_bcast_audio_immediate_rend_flag(&codec_cfg);
926 	zassert_true(ret > 0, "Unexpected return value %d", ret);
927 
928 	ret = bt_audio_codec_cfg_meta_get_bcast_audio_immediate_rend_flag(&codec_cfg);
929 	zassert_equal(ret, 0, "Unexpected return value %d", ret);
930 }
931 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_get_assisted_listening_stream)932 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_get_assisted_listening_stream)
933 {
934 	const struct bt_audio_codec_cfg codec_cfg = BT_AUDIO_CODEC_CFG(
935 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
936 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_ASSISTED_LISTENING_STREAM,
937 				     BT_AUDIO_ASSISTED_LISTENING_STREAM_UNSPECIFIED)});
938 	int ret;
939 
940 	ret = bt_audio_codec_cfg_meta_get_assisted_listening_stream(&codec_cfg);
941 	zassert_equal(ret, 0x00, "Unexpected return value %d", ret);
942 }
943 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_set_assisted_listening_stream)944 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_set_assisted_listening_stream)
945 {
946 	struct bt_audio_codec_cfg codec_cfg =
947 		BT_AUDIO_CODEC_CFG(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {}, {});
948 	int ret;
949 
950 	ret = bt_audio_codec_cfg_meta_get_assisted_listening_stream(&codec_cfg);
951 	zassert_equal(ret, -ENODATA, "Unexpected return value %d", ret);
952 
953 	ret = bt_audio_codec_cfg_meta_set_assisted_listening_stream(
954 		&codec_cfg, BT_AUDIO_ASSISTED_LISTENING_STREAM_UNSPECIFIED);
955 	zassert_true(ret > 0, "Unexpected return value %d", ret);
956 
957 	ret = bt_audio_codec_cfg_meta_get_assisted_listening_stream(&codec_cfg);
958 	zassert_equal(ret, 0x00, "Unexpected return value %d", ret);
959 }
960 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_get_broadcast_name)961 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_get_broadcast_name)
962 {
963 	const uint8_t expected_data[] = {'m', 'y', ' ', 'b', 'c', 'a', 's', 't'};
964 	const struct bt_audio_codec_cfg codec_cfg =
965 		BT_AUDIO_CODEC_CFG(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
966 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_BROADCAST_NAME, 'm',
967 							'y', ' ', 'b', 'c', 'a', 's', 't')});
968 	const uint8_t *broadcast_name;
969 	int ret;
970 
971 	ret = bt_audio_codec_cfg_meta_get_broadcast_name(&codec_cfg, &broadcast_name);
972 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
973 	zassert_mem_equal(expected_data, broadcast_name, ARRAY_SIZE(expected_data));
974 }
975 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_set_broadcast_name)976 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_set_broadcast_name)
977 {
978 	const uint8_t expected_data[] = {'m', 'y', ' ', 'b', 'c', 'a', 's', 't'};
979 	const uint8_t new_expected_data[] = {'n', 'e', 'w', ' ', 'b', 'c', 'a', 's', 't'};
980 	struct bt_audio_codec_cfg codec_cfg =
981 		BT_AUDIO_CODEC_CFG(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
982 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_BROADCAST_NAME, 'm',
983 							'y', ' ', 'b', 'c', 'a', 's', 't')});
984 	const uint8_t *broadcast_name;
985 	int ret;
986 
987 	ret = bt_audio_codec_cfg_meta_get_broadcast_name(&codec_cfg, &broadcast_name);
988 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
989 	zassert_mem_equal(expected_data, broadcast_name, ARRAY_SIZE(expected_data));
990 
991 	ret = bt_audio_codec_cfg_meta_set_broadcast_name(&codec_cfg, new_expected_data,
992 							 ARRAY_SIZE(new_expected_data));
993 	zassert_true(ret > 0, "Unexpected return value %d", ret);
994 
995 	ret = bt_audio_codec_cfg_meta_get_broadcast_name(&codec_cfg, &broadcast_name);
996 	zassert_equal(ret, ARRAY_SIZE(new_expected_data), "Unexpected return value %d", ret);
997 	zassert_mem_equal(new_expected_data, broadcast_name, ARRAY_SIZE(new_expected_data));
998 }
999 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_get_extended)1000 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_get_extended)
1001 {
1002 	const uint8_t expected_data[] = {0x00, 0x01, 0x02, 0x03};
1003 	const struct bt_audio_codec_cfg codec_cfg = BT_AUDIO_CODEC_CFG(
1004 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1005 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_EXTENDED, 0x00, 0x01, 0x02, 0x03)});
1006 	const uint8_t *extended_meta;
1007 	int ret;
1008 
1009 	ret = bt_audio_codec_cfg_meta_get_extended(&codec_cfg, &extended_meta);
1010 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
1011 	zassert_mem_equal(expected_data, extended_meta, ARRAY_SIZE(expected_data));
1012 }
1013 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_set_extended)1014 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_set_extended)
1015 {
1016 	const uint8_t expected_data[] = {0x00, 0x01, 0x02, 0x03};
1017 	const uint8_t new_expected_data[] = {0x04, 0x05};
1018 	struct bt_audio_codec_cfg codec_cfg = BT_AUDIO_CODEC_CFG(
1019 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1020 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_EXTENDED, 0x00, 0x01, 0x02, 0x03)});
1021 	const uint8_t *extended_meta;
1022 	int ret;
1023 
1024 	ret = bt_audio_codec_cfg_meta_get_extended(&codec_cfg, &extended_meta);
1025 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
1026 	zassert_mem_equal(expected_data, extended_meta, ARRAY_SIZE(expected_data));
1027 
1028 	ret = bt_audio_codec_cfg_meta_set_extended(&codec_cfg, new_expected_data,
1029 						   ARRAY_SIZE(new_expected_data));
1030 	zassert_true(ret > 0, "Unexpected return value %d", ret);
1031 
1032 	ret = bt_audio_codec_cfg_meta_get_extended(&codec_cfg, &extended_meta);
1033 	zassert_equal(ret, ARRAY_SIZE(new_expected_data), "Unexpected return value %d", ret);
1034 	zassert_mem_equal(new_expected_data, extended_meta, ARRAY_SIZE(new_expected_data));
1035 }
1036 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_get_vendor)1037 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_get_vendor)
1038 {
1039 	const uint8_t expected_data[] = {0x00, 0x01, 0x02, 0x03};
1040 	const struct bt_audio_codec_cfg codec_cfg = BT_AUDIO_CODEC_CFG(
1041 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1042 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_VENDOR, 0x00, 0x01, 0x02, 0x03)});
1043 	const uint8_t *vendor_meta;
1044 	int ret;
1045 
1046 	ret = bt_audio_codec_cfg_meta_get_vendor(&codec_cfg, &vendor_meta);
1047 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
1048 	zassert_mem_equal(expected_data, vendor_meta, ARRAY_SIZE(expected_data));
1049 }
1050 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cfg_meta_set_vendor)1051 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cfg_meta_set_vendor)
1052 {
1053 	const uint8_t expected_data[] = {0x00, 0x01, 0x02, 0x03};
1054 	const uint8_t new_expected_data[] = {0x04, 0x05, 0x06, 0x07, 0x08};
1055 	struct bt_audio_codec_cfg codec_cfg = BT_AUDIO_CODEC_CFG(
1056 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1057 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_VENDOR, 0x00, 0x01, 0x02, 0x03)});
1058 	const uint8_t *extended_meta;
1059 	int ret;
1060 
1061 	ret = bt_audio_codec_cfg_meta_get_vendor(&codec_cfg, &extended_meta);
1062 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
1063 	zassert_mem_equal(expected_data, extended_meta, ARRAY_SIZE(expected_data));
1064 
1065 	ret = bt_audio_codec_cfg_meta_set_vendor(&codec_cfg, new_expected_data,
1066 						 ARRAY_SIZE(new_expected_data));
1067 	zassert_true(ret > 0, "Unexpected return value %d", ret);
1068 
1069 	ret = bt_audio_codec_cfg_meta_get_vendor(&codec_cfg, &extended_meta);
1070 	zassert_equal(ret, ARRAY_SIZE(new_expected_data), "Unexpected return value %d", ret);
1071 	zassert_mem_equal(new_expected_data, extended_meta, ARRAY_SIZE(new_expected_data));
1072 }
1073 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_get_val)1074 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_get_val)
1075 {
1076 	struct bt_audio_codec_cap codec_cap =
1077 		BT_AUDIO_CODEC_CAP(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000,
1078 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_CODEC_CFG_FREQ,
1079 							BT_AUDIO_CODEC_CFG_FREQ_16KHZ)},
1080 				   {});
1081 	const uint8_t expected_data = BT_AUDIO_CODEC_CFG_FREQ_16KHZ;
1082 	const uint8_t *data;
1083 	int ret;
1084 
1085 	ret = bt_audio_codec_cap_get_val(&codec_cap, BT_AUDIO_CODEC_CAP_TYPE_FREQ, &data);
1086 	zassert_equal(ret, sizeof(expected_data), "Unexpected return value %d", ret);
1087 	zassert_equal(data[0], expected_data, "Unexpected return value %d", ret);
1088 }
1089 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_set_val)1090 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_set_val)
1091 {
1092 	struct bt_audio_codec_cap codec_cap =
1093 		BT_AUDIO_CODEC_CAP(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000,
1094 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_CODEC_CFG_FREQ,
1095 							BT_AUDIO_CODEC_CFG_FREQ_16KHZ)},
1096 				   {});
1097 	const uint8_t new_expected_data = BT_AUDIO_CODEC_CFG_FREQ_48KHZ;
1098 	const uint8_t expected_data = BT_AUDIO_CODEC_CFG_FREQ_16KHZ;
1099 	const uint8_t *data;
1100 	int ret;
1101 
1102 	ret = bt_audio_codec_cap_get_val(&codec_cap, BT_AUDIO_CODEC_CAP_TYPE_FREQ, &data);
1103 	zassert_equal(ret, sizeof(expected_data), "Unexpected return value %d", ret);
1104 	zassert_equal(data[0], expected_data, "Unexpected return value %d", ret);
1105 
1106 	ret = bt_audio_codec_cap_set_val(&codec_cap, BT_AUDIO_CODEC_CAP_TYPE_FREQ,
1107 					 &new_expected_data, sizeof(new_expected_data));
1108 	zassert_true(ret > 0, "Unexpected return value %d", ret);
1109 
1110 	ret = bt_audio_codec_cap_get_val(&codec_cap, BT_AUDIO_CODEC_CAP_TYPE_FREQ, &data);
1111 	zassert_equal(ret, sizeof(new_expected_data), "Unexpected return value %d", ret);
1112 	zassert_equal(data[0], new_expected_data, "Unexpected data value %u", data[0]);
1113 }
1114 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_set_val_new)1115 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_set_val_new)
1116 {
1117 	struct bt_audio_codec_cap codec_cap =
1118 		BT_AUDIO_CODEC_CAP(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {}, {});
1119 	const uint8_t new_expected_data = BT_AUDIO_CODEC_CFG_FREQ_48KHZ;
1120 	const uint8_t *data;
1121 	int ret;
1122 
1123 	ret = bt_audio_codec_cap_get_val(&codec_cap, BT_AUDIO_CODEC_CAP_TYPE_FREQ, &data);
1124 	zassert_equal(ret, -ENODATA, "Unexpected return value %d", ret);
1125 
1126 	ret = bt_audio_codec_cap_set_val(&codec_cap, BT_AUDIO_CODEC_CAP_TYPE_FREQ,
1127 					 &new_expected_data, sizeof(new_expected_data));
1128 	zassert_true(ret > 0, "Unexpected return value %d", ret);
1129 
1130 	ret = bt_audio_codec_cap_get_val(&codec_cap, BT_AUDIO_CODEC_CAP_TYPE_FREQ, &data);
1131 	zassert_equal(ret, sizeof(new_expected_data), "Unexpected return value %d", ret);
1132 	zassert_equal(data[0], new_expected_data, "Unexpected return value %d", ret);
1133 }
1134 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_unset_val)1135 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_unset_val)
1136 {
1137 	struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP(
1138 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000,
1139 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_CODEC_CAP_TYPE_FREQ, BT_AUDIO_CODEC_CAP_FREQ_16KHZ)},
1140 		{});
1141 	const uint8_t expected_data = BT_AUDIO_CODEC_CAP_FREQ_16KHZ;
1142 	const uint8_t *data;
1143 	int ret;
1144 
1145 	ret = bt_audio_codec_cap_get_val(&codec_cap, BT_AUDIO_CODEC_CAP_TYPE_FREQ, &data);
1146 	zassert_equal(ret, sizeof(expected_data), "Unexpected return value %d", ret);
1147 	zassert_equal(data[0], expected_data, "Unexpected return value %d", ret);
1148 
1149 	ret = bt_audio_codec_cap_unset_val(&codec_cap, BT_AUDIO_CODEC_CAP_TYPE_FREQ);
1150 	zassert_true(ret >= 0, "Unexpected return value %d", ret);
1151 
1152 	ret = bt_audio_codec_cap_get_val(&codec_cap, BT_AUDIO_CODEC_CAP_TYPE_FREQ, &data);
1153 	zassert_equal(ret, -ENODATA, "Unexpected return value %d", ret);
1154 }
1155 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_get_freq)1156 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_get_freq)
1157 {
1158 	const struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP_LC3(
1159 		BT_AUDIO_CODEC_CAP_FREQ_16KHZ, BT_AUDIO_CODEC_CAP_DURATION_10,
1160 		BT_AUDIO_CODEC_CAP_CHAN_COUNT_SUPPORT(1), 40U, 120U, 2U,
1161 		(BT_AUDIO_CONTEXT_TYPE_CONVERSATIONAL | BT_AUDIO_CONTEXT_TYPE_MEDIA));
1162 
1163 	int ret;
1164 
1165 	ret = bt_audio_codec_cap_get_freq(&codec_cap);
1166 	zassert_equal(ret, 4, "Unexpected return value %d", ret);
1167 }
1168 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_set_freq)1169 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_set_freq)
1170 {
1171 	struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP_LC3(
1172 		BT_AUDIO_CODEC_CAP_FREQ_16KHZ, BT_AUDIO_CODEC_CAP_DURATION_10,
1173 		BT_AUDIO_CODEC_CAP_CHAN_COUNT_SUPPORT(1), 40U, 120U, 2U,
1174 		(BT_AUDIO_CONTEXT_TYPE_CONVERSATIONAL | BT_AUDIO_CONTEXT_TYPE_MEDIA));
1175 
1176 	int ret;
1177 
1178 	ret = bt_audio_codec_cap_get_freq(&codec_cap);
1179 	zassert_equal(ret, 4, "Unexpected return value %d", ret);
1180 
1181 	ret = bt_audio_codec_cap_set_freq(&codec_cap, BT_AUDIO_CODEC_CAP_FREQ_22KHZ);
1182 	zassert_true(ret > 0, "Unexpected return value %d", ret);
1183 
1184 	ret = bt_audio_codec_cap_get_freq(&codec_cap);
1185 	zassert_equal(ret, 8, "Unexpected return value %d", ret);
1186 }
1187 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_get_frame_dur)1188 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_get_frame_dur)
1189 {
1190 	const struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP_LC3(
1191 		BT_AUDIO_CODEC_CAP_FREQ_16KHZ, BT_AUDIO_CODEC_CAP_DURATION_10,
1192 		BT_AUDIO_CODEC_CAP_CHAN_COUNT_SUPPORT(1), 40U, 120U, 2U,
1193 		(BT_AUDIO_CONTEXT_TYPE_CONVERSATIONAL | BT_AUDIO_CONTEXT_TYPE_MEDIA));
1194 
1195 	int ret;
1196 
1197 	ret = bt_audio_codec_cap_get_frame_dur(&codec_cap);
1198 	zassert_equal(ret, 2, "Unexpected return value %d", ret);
1199 }
1200 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_set_frame_dur)1201 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_set_frame_dur)
1202 {
1203 	struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP_LC3(
1204 		BT_AUDIO_CODEC_CAP_FREQ_16KHZ, BT_AUDIO_CODEC_CAP_DURATION_10,
1205 		BT_AUDIO_CODEC_CAP_CHAN_COUNT_SUPPORT(1), 40U, 120U, 2U,
1206 		(BT_AUDIO_CONTEXT_TYPE_CONVERSATIONAL | BT_AUDIO_CONTEXT_TYPE_MEDIA));
1207 
1208 	int ret;
1209 
1210 	ret = bt_audio_codec_cap_get_frame_dur(&codec_cap);
1211 	zassert_equal(ret, 2, "Unexpected return value %d", ret);
1212 
1213 	ret = bt_audio_codec_cap_set_frame_dur(&codec_cap, BT_AUDIO_CODEC_CAP_DURATION_7_5);
1214 	zassert_true(ret > 0, "Unexpected return value %d", ret);
1215 
1216 	ret = bt_audio_codec_cap_get_frame_dur(&codec_cap);
1217 	zassert_equal(ret, 1, "Unexpected return value %d", ret);
1218 }
1219 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_get_supported_audio_chan_counts)1220 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_get_supported_audio_chan_counts)
1221 {
1222 	const struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP_LC3(
1223 		BT_AUDIO_CODEC_CAP_FREQ_16KHZ, BT_AUDIO_CODEC_CAP_DURATION_10,
1224 		BT_AUDIO_CODEC_CAP_CHAN_COUNT_SUPPORT(2), 40U, 120U, 2U,
1225 		(BT_AUDIO_CONTEXT_TYPE_CONVERSATIONAL | BT_AUDIO_CONTEXT_TYPE_MEDIA));
1226 
1227 	int ret;
1228 
1229 	ret = bt_audio_codec_cap_get_supported_audio_chan_counts(&codec_cap, false);
1230 	zassert_equal(ret, 2, "Unexpected return value %d", ret);
1231 }
1232 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_get_supported_audio_chan_counts_lc3_fallback_true)1233 ZTEST(audio_codec_test_suite,
1234 	test_bt_audio_codec_cap_get_supported_audio_chan_counts_lc3_fallback_true)
1235 {
1236 	struct bt_audio_codec_cap codec_cap = {.id = BT_HCI_CODING_FORMAT_LC3};
1237 	int err;
1238 
1239 	err = bt_audio_codec_cap_get_supported_audio_chan_counts(&codec_cap, true);
1240 	zassert_equal(err, 1, "unexpected error %d", err);
1241 }
1242 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_get_supported_audio_chan_counts_lc3_fallback_false)1243 ZTEST(audio_codec_test_suite,
1244 	test_bt_audio_codec_cap_get_supported_audio_chan_counts_lc3_fallback_false)
1245 {
1246 	struct bt_audio_codec_cap codec_cap = {.id = BT_HCI_CODING_FORMAT_LC3};
1247 	int err;
1248 
1249 	err = bt_audio_codec_cap_get_supported_audio_chan_counts(&codec_cap, false);
1250 	zassert_equal(err, -ENODATA, "unexpected error %d", err);
1251 }
1252 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_get_supported_audio_chan_counts_fallback_true)1253 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_get_supported_audio_chan_counts_fallback_true)
1254 {
1255 	struct bt_audio_codec_cap codec_cap = {0};
1256 	int err;
1257 
1258 	err = bt_audio_codec_cap_get_supported_audio_chan_counts(&codec_cap, true);
1259 	zassert_equal(err, -ENODATA, "unexpected error %d", err);
1260 }
1261 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_get_supported_audio_chan_counts_fallback_false)1262 ZTEST(audio_codec_test_suite,
1263 	test_bt_audio_codec_cap_get_supported_audio_chan_counts_fallback_false)
1264 {
1265 	struct bt_audio_codec_cap codec_cap = {0};
1266 	int err;
1267 
1268 	err = bt_audio_codec_cap_get_supported_audio_chan_counts(&codec_cap, true);
1269 	zassert_equal(err, -ENODATA, "unexpected error %d", err);
1270 }
1271 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_set_supported_audio_chan_counts)1272 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_set_supported_audio_chan_counts)
1273 {
1274 	struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP_LC3(
1275 		BT_AUDIO_CODEC_CAP_FREQ_16KHZ, BT_AUDIO_CODEC_CAP_DURATION_10,
1276 		BT_AUDIO_CODEC_CAP_CHAN_COUNT_SUPPORT(1), 40U, 120U, 2U,
1277 		(BT_AUDIO_CONTEXT_TYPE_CONVERSATIONAL | BT_AUDIO_CONTEXT_TYPE_MEDIA));
1278 
1279 	int ret;
1280 
1281 	ret = bt_audio_codec_cap_get_supported_audio_chan_counts(&codec_cap, false);
1282 	zassert_equal(ret, 1, "Unexpected return value %d", ret);
1283 
1284 	ret = bt_audio_codec_cap_set_supported_audio_chan_counts(
1285 		&codec_cap, BT_AUDIO_CODEC_CAP_CHAN_COUNT_SUPPORT(2));
1286 	zassert_true(ret > 0, "Unexpected return value %d", ret);
1287 
1288 	ret = bt_audio_codec_cap_get_supported_audio_chan_counts(&codec_cap, false);
1289 	zassert_equal(ret, 2, "Unexpected return value %d", ret);
1290 }
1291 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_get_octets_per_frame)1292 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_get_octets_per_frame)
1293 {
1294 	struct bt_audio_codec_octets_per_codec_frame expected = {
1295 		.min = 40U,
1296 		.max = 120U,
1297 	};
1298 	const struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP_LC3(
1299 		BT_AUDIO_CODEC_CAP_FREQ_16KHZ, BT_AUDIO_CODEC_CAP_DURATION_10,
1300 		BT_AUDIO_CODEC_CAP_CHAN_COUNT_SUPPORT(1), 40U, 120U, 2U,
1301 		(BT_AUDIO_CONTEXT_TYPE_CONVERSATIONAL | BT_AUDIO_CONTEXT_TYPE_MEDIA));
1302 	struct bt_audio_codec_octets_per_codec_frame codec_frame;
1303 
1304 	int ret;
1305 
1306 	ret = bt_audio_codec_cap_get_octets_per_frame(&codec_cap, &codec_frame);
1307 	zassert_equal(ret, 0, "Unexpected return value %d", ret);
1308 	zassert_equal(codec_frame.min, expected.min, "Unexpected minimum value %d",
1309 		      codec_frame.min);
1310 	zassert_equal(codec_frame.max, expected.max, "Unexpected maximum value %d",
1311 		      codec_frame.max);
1312 }
1313 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_set_octets_per_frame)1314 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_set_octets_per_frame)
1315 {
1316 	struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP_LC3(
1317 		BT_AUDIO_CODEC_CAP_FREQ_16KHZ, BT_AUDIO_CODEC_CAP_DURATION_10,
1318 		BT_AUDIO_CODEC_CAP_CHAN_COUNT_SUPPORT(1), 40U, 120U, 2U,
1319 		(BT_AUDIO_CONTEXT_TYPE_CONVERSATIONAL | BT_AUDIO_CONTEXT_TYPE_MEDIA));
1320 	struct bt_audio_codec_octets_per_codec_frame codec_frame;
1321 	int ret;
1322 
1323 	ret = bt_audio_codec_cap_get_octets_per_frame(&codec_cap, &codec_frame);
1324 	zassert_equal(ret, 0, "Unexpected return value %d", ret);
1325 	zassert_equal(codec_frame.min, 40U, "Unexpected minimum value %d", codec_frame.min);
1326 	zassert_equal(codec_frame.max, 120U, "Unexpected maximum value %d", codec_frame.max);
1327 
1328 	codec_frame.min = 50U;
1329 	codec_frame.max = 100U;
1330 	ret = bt_audio_codec_cap_set_octets_per_frame(&codec_cap, &codec_frame);
1331 	zassert_true(ret > 0, "Unexpected return value %d", ret);
1332 
1333 	ret = bt_audio_codec_cap_get_octets_per_frame(&codec_cap, &codec_frame);
1334 	zassert_equal(ret, 0, "Unexpected return value %d", ret);
1335 	zassert_equal(codec_frame.min, 50U, "Unexpected minimum value %d", codec_frame.min);
1336 	zassert_equal(codec_frame.max, 100U, "Unexpected maximum value %d", codec_frame.max);
1337 }
1338 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_get_max_codec_frames_per_sdu)1339 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_get_max_codec_frames_per_sdu)
1340 {
1341 	const struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP_LC3(
1342 		BT_AUDIO_CODEC_CAP_FREQ_16KHZ, BT_AUDIO_CODEC_CAP_DURATION_10,
1343 		BT_AUDIO_CODEC_CAP_CHAN_COUNT_SUPPORT(1), 40U, 120U, 2U,
1344 		(BT_AUDIO_CONTEXT_TYPE_CONVERSATIONAL | BT_AUDIO_CONTEXT_TYPE_MEDIA));
1345 
1346 	int ret;
1347 
1348 	ret = bt_audio_codec_cap_get_max_codec_frames_per_sdu(&codec_cap, false);
1349 	zassert_equal(ret, 2, "Unexpected return value %d", ret);
1350 }
1351 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_get_max_codec_frames_per_sdu_lc3_fallback_true)1352 ZTEST(audio_codec_test_suite,
1353 	test_bt_audio_codec_cap_get_max_codec_frames_per_sdu_lc3_fallback_true)
1354 {
1355 	struct bt_audio_codec_cap codec_cap = {.id = BT_HCI_CODING_FORMAT_LC3};
1356 	int err;
1357 
1358 	err = bt_audio_codec_cap_get_max_codec_frames_per_sdu(&codec_cap, true);
1359 	zassert_equal(err, 1, "unexpected error %d", err);
1360 }
1361 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_get_max_codec_frames_per_sdu_lc3_fallback_false)1362 ZTEST(audio_codec_test_suite,
1363 	test_bt_audio_codec_cap_get_max_codec_frames_per_sdu_lc3_fallback_false)
1364 {
1365 	struct bt_audio_codec_cap codec_cap = {.id = BT_HCI_CODING_FORMAT_LC3};
1366 	int err;
1367 
1368 	err = bt_audio_codec_cap_get_max_codec_frames_per_sdu(&codec_cap, false);
1369 	zassert_equal(err, -ENODATA, "unexpected error %d", err);
1370 }
1371 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_get_max_codec_frames_per_sdu_fallback_true)1372 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_get_max_codec_frames_per_sdu_fallback_true)
1373 {
1374 	struct bt_audio_codec_cap codec_cap = {0};
1375 	int err;
1376 
1377 	err = bt_audio_codec_cap_get_max_codec_frames_per_sdu(&codec_cap, true);
1378 	zassert_equal(err, -ENODATA, "unexpected error %d", err);
1379 }
1380 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_get_max_codec_frames_per_sdu_fallback_false)1381 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_get_max_codec_frames_per_sdu_fallback_false)
1382 {
1383 	struct bt_audio_codec_cap codec_cap = {0};
1384 	int err;
1385 
1386 	err = bt_audio_codec_cap_get_max_codec_frames_per_sdu(&codec_cap, true);
1387 	zassert_equal(err, -ENODATA, "unexpected error %d", err);
1388 }
1389 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_set_max_codec_frames_per_sdu)1390 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_set_max_codec_frames_per_sdu)
1391 {
1392 	struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP_LC3(
1393 		BT_AUDIO_CODEC_CAP_FREQ_16KHZ, BT_AUDIO_CODEC_CAP_DURATION_10,
1394 		BT_AUDIO_CODEC_CAP_CHAN_COUNT_SUPPORT(1), 40U, 120U, 2U,
1395 		(BT_AUDIO_CONTEXT_TYPE_CONVERSATIONAL | BT_AUDIO_CONTEXT_TYPE_MEDIA));
1396 
1397 	int ret;
1398 
1399 	ret = bt_audio_codec_cap_get_max_codec_frames_per_sdu(&codec_cap, false);
1400 	zassert_equal(ret, 2, "Unexpected return value %d", ret);
1401 
1402 	ret = bt_audio_codec_cap_set_max_codec_frames_per_sdu(&codec_cap, 4U);
1403 	zassert_true(ret > 0, "Unexpected return value %d", ret);
1404 
1405 	ret = bt_audio_codec_cap_get_max_codec_frames_per_sdu(&codec_cap, false);
1406 	zassert_equal(ret, 4, "Unexpected return value %d", ret);
1407 }
1408 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_get_val)1409 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_get_val)
1410 {
1411 	struct bt_audio_codec_cap codec_cap =
1412 		BT_AUDIO_CODEC_CAP(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1413 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
1414 							BT_AUDIO_PARENTAL_RATING_AGE_10_OR_ABOVE)});
1415 	const uint8_t expected_data = BT_AUDIO_PARENTAL_RATING_AGE_10_OR_ABOVE;
1416 	const uint8_t *data;
1417 	int ret;
1418 
1419 	ret = bt_audio_codec_cap_meta_get_val(&codec_cap, BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
1420 					      &data);
1421 	zassert_equal(ret, sizeof(expected_data), "Unexpected return value %d", ret);
1422 	zassert_equal(data[0], expected_data, "Unexpected return value %d", ret);
1423 }
1424 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_set_val)1425 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_set_val)
1426 {
1427 	struct bt_audio_codec_cap codec_cap =
1428 		BT_AUDIO_CODEC_CAP(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1429 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
1430 							BT_AUDIO_PARENTAL_RATING_AGE_10_OR_ABOVE)});
1431 	const uint8_t new_expected_data = BT_AUDIO_PARENTAL_RATING_AGE_13_OR_ABOVE;
1432 	const uint8_t expected_data = BT_AUDIO_PARENTAL_RATING_AGE_10_OR_ABOVE;
1433 	const uint8_t *data;
1434 	int ret;
1435 
1436 	ret = bt_audio_codec_cap_meta_get_val(&codec_cap, BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
1437 					      &data);
1438 	zassert_equal(ret, sizeof(expected_data), "Unexpected return value %d", ret);
1439 	zassert_equal(data[0], expected_data, "Unexpected return value %d", ret);
1440 
1441 	ret = bt_audio_codec_cap_meta_set_val(&codec_cap, BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
1442 					      &new_expected_data, sizeof(new_expected_data));
1443 	zassert_true(ret > 0, "Unexpected return value %d", ret);
1444 
1445 	ret = bt_audio_codec_cap_meta_get_val(&codec_cap, BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
1446 					      &data);
1447 	zassert_equal(ret, sizeof(new_expected_data), "Unexpected return value %d", ret);
1448 	zassert_equal(data[0], new_expected_data, "Unexpected data value %u", data[0]);
1449 }
1450 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_set_val_new)1451 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_set_val_new)
1452 {
1453 	struct bt_audio_codec_cap codec_cap =
1454 		BT_AUDIO_CODEC_CAP(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {}, {});
1455 	const uint8_t new_expected_data = BT_AUDIO_PARENTAL_RATING_AGE_13_OR_ABOVE;
1456 	const uint8_t *data;
1457 	int ret;
1458 
1459 	ret = bt_audio_codec_cap_meta_get_val(&codec_cap, BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
1460 					      &data);
1461 	zassert_equal(ret, -ENODATA, "Unexpected return value %d", ret);
1462 
1463 	ret = bt_audio_codec_cap_meta_set_val(&codec_cap, BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
1464 					      &new_expected_data, sizeof(new_expected_data));
1465 	zassert_true(ret > 0, "Unexpected return value %d", ret);
1466 
1467 	ret = bt_audio_codec_cap_meta_get_val(&codec_cap, BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
1468 					      &data);
1469 	zassert_equal(ret, sizeof(new_expected_data), "Unexpected return value %d", ret);
1470 	zassert_equal(data[0], new_expected_data, "Unexpected return value %d", ret);
1471 }
1472 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_unset_val_only)1473 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_unset_val_only)
1474 {
1475 	struct bt_audio_codec_cap codec_cap =
1476 		BT_AUDIO_CODEC_CAP(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1477 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
1478 							BT_AUDIO_PARENTAL_RATING_AGE_10_OR_ABOVE)});
1479 	const uint8_t expected_data = BT_AUDIO_PARENTAL_RATING_AGE_10_OR_ABOVE;
1480 	const uint8_t *data;
1481 	int ret;
1482 
1483 	ret = bt_audio_codec_cap_meta_get_val(&codec_cap, BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
1484 					      &data);
1485 	zassert_equal(ret, sizeof(expected_data), "Unexpected return value %d", ret);
1486 	zassert_equal(data[0], expected_data, "Unexpected return value %d", ret);
1487 
1488 	ret = bt_audio_codec_cap_meta_unset_val(&codec_cap, BT_AUDIO_METADATA_TYPE_PARENTAL_RATING);
1489 	zassert_true(ret >= 0, "Unexpected return value %d", ret);
1490 
1491 	ret = bt_audio_codec_cap_meta_get_val(&codec_cap, BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
1492 					      &data);
1493 	zassert_equal(ret, -ENODATA, "Unexpected return value %d", ret);
1494 }
1495 
1496 /* Providing multiple BT_AUDIO_CODEC_DATA to BT_AUDIO_CODEC_CAP without packing it in a macro
1497  * cause compile issue, so define a macro to denote 3 types of metadata for the meta_unset tests
1498  */
1499 #define TRIPLE_META_DATA                                                                           \
1500 	{                                                                                          \
1501 		BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,                        \
1502 				    BT_AUDIO_PARENTAL_RATING_AGE_10_OR_ABOVE),                     \
1503 		BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PREF_CONTEXT,                           \
1504 				    BT_BYTES_LIST_LE16(BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED)),        \
1505 		BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_STREAM_CONTEXT,                         \
1506 				    BT_BYTES_LIST_LE16(BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED))         \
1507 	}
1508 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_unset_val_first)1509 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_unset_val_first)
1510 {
1511 	struct bt_audio_codec_cap codec_cap =
1512 		BT_AUDIO_CODEC_CAP(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {}, TRIPLE_META_DATA);
1513 	const uint8_t expected_data = BT_AUDIO_PARENTAL_RATING_AGE_10_OR_ABOVE;
1514 	const uint8_t *data;
1515 	int ret;
1516 
1517 	ret = bt_audio_codec_cap_meta_get_val(&codec_cap, BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
1518 					      &data);
1519 	zassert_equal(ret, sizeof(expected_data), "Unexpected return value %d", ret);
1520 	zassert_equal(data[0], expected_data, "Unexpected return value %d", ret);
1521 
1522 	ret = bt_audio_codec_cap_meta_unset_val(&codec_cap, BT_AUDIO_METADATA_TYPE_PARENTAL_RATING);
1523 	zassert_true(ret >= 0, "Unexpected return value %d", ret);
1524 
1525 	ret = bt_audio_codec_cap_meta_get_val(&codec_cap, BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
1526 					      &data);
1527 	zassert_equal(ret, -ENODATA, "Unexpected return value %d", ret);
1528 }
1529 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_unset_val_middle)1530 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_unset_val_middle)
1531 {
1532 	struct bt_audio_codec_cap codec_cap =
1533 		BT_AUDIO_CODEC_CAP(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {}, TRIPLE_META_DATA);
1534 	const uint16_t expected_data = BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED;
1535 	const uint8_t *data;
1536 	int ret;
1537 
1538 	ret = bt_audio_codec_cap_meta_get_val(&codec_cap, BT_AUDIO_METADATA_TYPE_PREF_CONTEXT,
1539 					      &data);
1540 	zassert_equal(ret, sizeof(expected_data), "Unexpected return value %d", ret);
1541 	zassert_equal(sys_get_le16(data), expected_data, "Unexpected return value %d", ret);
1542 
1543 	ret = bt_audio_codec_cap_meta_unset_val(&codec_cap, BT_AUDIO_METADATA_TYPE_PREF_CONTEXT);
1544 	zassert_true(ret >= 0, "Unexpected return value %d", ret);
1545 
1546 	ret = bt_audio_codec_cap_meta_get_val(&codec_cap, BT_AUDIO_METADATA_TYPE_PREF_CONTEXT,
1547 					      &data);
1548 	zassert_equal(ret, -ENODATA, "Unexpected return value %d", ret);
1549 }
1550 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_unset_val_last)1551 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_unset_val_last)
1552 {
1553 	struct bt_audio_codec_cap codec_cap =
1554 		BT_AUDIO_CODEC_CAP(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {}, TRIPLE_META_DATA);
1555 	const uint16_t expected_data = BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED;
1556 	const uint8_t *data;
1557 	int ret;
1558 
1559 	ret = bt_audio_codec_cap_meta_get_val(&codec_cap, BT_AUDIO_METADATA_TYPE_STREAM_CONTEXT,
1560 					      &data);
1561 	zassert_equal(ret, sizeof(expected_data), "Unexpected return value %d", ret);
1562 	zassert_equal(sys_get_le16(data), expected_data, "Unexpected return value %d", ret);
1563 
1564 	ret = bt_audio_codec_cap_meta_unset_val(&codec_cap, BT_AUDIO_METADATA_TYPE_STREAM_CONTEXT);
1565 	zassert_true(ret >= 0, "Unexpected return value %d", ret);
1566 
1567 	ret = bt_audio_codec_cap_meta_get_val(&codec_cap, BT_AUDIO_METADATA_TYPE_STREAM_CONTEXT,
1568 					      &data);
1569 	zassert_equal(ret, -ENODATA, "Unexpected return value %d", ret);
1570 }
1571 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_get_pref_context)1572 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_get_pref_context)
1573 {
1574 	const enum bt_audio_context ctx =
1575 		BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED | BT_AUDIO_CONTEXT_TYPE_MEDIA;
1576 	const struct bt_audio_codec_cap codec_cap =
1577 		BT_AUDIO_CODEC_CAP(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1578 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PREF_CONTEXT,
1579 							BT_BYTES_LIST_LE16(ctx))});
1580 	int ret;
1581 
1582 	ret = bt_audio_codec_cap_meta_get_pref_context(&codec_cap);
1583 	zassert_equal(ret, 0x0005, "unexpected return value %d", ret);
1584 }
1585 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_set_pref_context)1586 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_set_pref_context)
1587 {
1588 	const enum bt_audio_context ctx =
1589 		BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED | BT_AUDIO_CONTEXT_TYPE_MEDIA;
1590 	const enum bt_audio_context new_ctx = BT_AUDIO_CONTEXT_TYPE_NOTIFICATIONS;
1591 	struct bt_audio_codec_cap codec_cap =
1592 		BT_AUDIO_CODEC_CAP(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1593 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PREF_CONTEXT,
1594 							BT_BYTES_LIST_LE16(ctx))});
1595 	int ret;
1596 
1597 	ret = bt_audio_codec_cap_meta_get_pref_context(&codec_cap);
1598 	zassert_equal(ret, 0x0005, "Unexpected return value %d", ret);
1599 
1600 	ret = bt_audio_codec_cap_meta_set_pref_context(&codec_cap, new_ctx);
1601 	zassert_true(ret > 0, "Unexpected return value %d", ret);
1602 
1603 	ret = bt_audio_codec_cap_meta_get_pref_context(&codec_cap);
1604 	zassert_equal(ret, 0x0100, "Unexpected return value %d", ret);
1605 }
1606 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_get_stream_context)1607 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_get_stream_context)
1608 {
1609 	const enum bt_audio_context ctx =
1610 		BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED | BT_AUDIO_CONTEXT_TYPE_MEDIA;
1611 	const struct bt_audio_codec_cap codec_cap =
1612 		BT_AUDIO_CODEC_CAP(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1613 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_STREAM_CONTEXT,
1614 							BT_BYTES_LIST_LE16(ctx))});
1615 	int ret;
1616 
1617 	ret = bt_audio_codec_cap_meta_get_stream_context(&codec_cap);
1618 	zassert_equal(ret, 0x0005, "unexpected return value %d", ret);
1619 }
1620 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_set_stream_context)1621 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_set_stream_context)
1622 {
1623 	enum bt_audio_context ctx = BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED | BT_AUDIO_CONTEXT_TYPE_MEDIA;
1624 	struct bt_audio_codec_cap codec_cap =
1625 		BT_AUDIO_CODEC_CAP(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1626 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_STREAM_CONTEXT,
1627 							BT_BYTES_LIST_LE16(ctx))});
1628 	int ret;
1629 
1630 	ret = bt_audio_codec_cap_meta_get_stream_context(&codec_cap);
1631 	zassert_equal(ret, 0x0005, "Unexpected return value %d", ret);
1632 
1633 	ctx = BT_AUDIO_CONTEXT_TYPE_NOTIFICATIONS;
1634 	ret = bt_audio_codec_cap_meta_set_stream_context(&codec_cap, ctx);
1635 	zassert_true(ret > 0, "Unexpected return value %d", ret);
1636 
1637 	ret = bt_audio_codec_cap_meta_get_stream_context(&codec_cap);
1638 	zassert_equal(ret, 0x0100, "Unexpected return value %d", ret);
1639 }
1640 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_get_program_info)1641 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_get_program_info)
1642 {
1643 	const uint8_t expected_data[] = {'P', 'r', 'o', 'g', 'r', 'a', 'm', ' ',
1644 					 'I', 'n', 'f', 'o'};
1645 	const struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP(
1646 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1647 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PROGRAM_INFO,
1648 				     'P', 'r', 'o', 'g', 'r', 'a', 'm', ' ',
1649 				     'I', 'n', 'f', 'o')});
1650 	const uint8_t *program_data;
1651 	int ret;
1652 
1653 	ret = bt_audio_codec_cap_meta_get_program_info(&codec_cap, &program_data);
1654 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
1655 	zassert_mem_equal(expected_data, program_data, ARRAY_SIZE(expected_data));
1656 }
1657 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_set_program_info)1658 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_set_program_info)
1659 {
1660 	const uint8_t expected_data[] = {'P', 'r', 'o', 'g', 'r', 'a',
1661 					 'm', ' ', 'I', 'n', 'f', 'o'};
1662 	const uint8_t new_expected_data[] = {'N', 'e', 'w', ' ', 'i', 'n', 'f', 'o'};
1663 	struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP(
1664 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1665 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PROGRAM_INFO, 'P', 'r', 'o', 'g', 'r',
1666 				     'a', 'm', ' ', 'I', 'n', 'f', 'o')});
1667 	const uint8_t *program_data;
1668 	int ret;
1669 
1670 	ret = bt_audio_codec_cap_meta_get_program_info(&codec_cap, &program_data);
1671 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
1672 	zassert_mem_equal(expected_data, program_data, ARRAY_SIZE(expected_data));
1673 
1674 	ret = bt_audio_codec_cap_meta_set_program_info(&codec_cap, new_expected_data,
1675 						       ARRAY_SIZE(new_expected_data));
1676 	zassert_true(ret > 0, "Unexpected return value %d", ret);
1677 
1678 	ret = bt_audio_codec_cap_meta_get_program_info(&codec_cap, &program_data);
1679 	zassert_equal(ret, ARRAY_SIZE(new_expected_data), "Unexpected return value %d", ret);
1680 	zassert_mem_equal(new_expected_data, program_data, ARRAY_SIZE(new_expected_data));
1681 }
1682 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_get_lang)1683 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_get_lang)
1684 {
1685 	const struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP(
1686 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1687 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_LANG, 'e', 'n', 'g')});
1688 	char expected_data[] = "eng";
1689 	const uint8_t *lang;
1690 	int ret;
1691 
1692 	ret = bt_audio_codec_cap_meta_get_lang(&codec_cap, &lang);
1693 	zassert_equal(ret, 0, "Unexpected return value %d", ret);
1694 	zassert_mem_equal(expected_data, lang, BT_AUDIO_LANG_SIZE);
1695 }
1696 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_set_lang)1697 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_set_lang)
1698 {
1699 	struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP(
1700 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1701 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_LANG, 'e', 'n', 'g')});
1702 	const uint8_t new_expected_data[] = "deu";
1703 	char expected_data[] = "eng";
1704 	const uint8_t *lang;
1705 	int ret;
1706 
1707 	ret = bt_audio_codec_cap_meta_get_lang(&codec_cap, &lang);
1708 	zassert_equal(ret, 0, "Unexpected return value %d", ret);
1709 	zassert_mem_equal(expected_data, lang, BT_AUDIO_LANG_SIZE);
1710 
1711 	ret = bt_audio_codec_cap_meta_set_lang(&codec_cap, new_expected_data);
1712 	zassert_true(ret > 0, "Unexpected return value %d", ret);
1713 
1714 	ret = bt_audio_codec_cap_meta_get_lang(&codec_cap, &lang);
1715 	zassert_equal(ret, 0, "Unexpected return value %d", ret);
1716 	zassert_mem_equal(new_expected_data, lang, BT_AUDIO_LANG_SIZE);
1717 }
1718 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_get_ccid_list)1719 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_get_ccid_list)
1720 {
1721 	const uint8_t expected_data[] = {0x05, 0x10, 0x15};
1722 	const struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP(
1723 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1724 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_CCID_LIST, 0x05, 0x10, 0x15)});
1725 	const uint8_t *ccid_list;
1726 	int ret;
1727 
1728 	ret = bt_audio_codec_cap_meta_get_ccid_list(&codec_cap, &ccid_list);
1729 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
1730 	zassert_mem_equal(expected_data, ccid_list, ARRAY_SIZE(expected_data));
1731 }
1732 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_set_ccid_list)1733 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_set_ccid_list)
1734 {
1735 	const uint8_t expected_data[] = {0x05, 0x10, 0x15};
1736 	const uint8_t new_expected_data[] = {0x25, 0x30};
1737 	struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP(
1738 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1739 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_CCID_LIST, 0x05, 0x10, 0x15)});
1740 	const uint8_t *ccid_list;
1741 	int ret;
1742 
1743 	ret = bt_audio_codec_cap_meta_get_ccid_list(&codec_cap, &ccid_list);
1744 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
1745 	zassert_mem_equal(expected_data, ccid_list, ARRAY_SIZE(expected_data));
1746 
1747 	ret = bt_audio_codec_cap_meta_set_ccid_list(&codec_cap, new_expected_data,
1748 						    ARRAY_SIZE(new_expected_data));
1749 	zassert_true(ret > 0, "Unexpected return value %d", ret);
1750 
1751 	ret = bt_audio_codec_cap_meta_get_ccid_list(&codec_cap, &ccid_list);
1752 	zassert_equal(ret, ARRAY_SIZE(new_expected_data), "Unexpected return value %d", ret);
1753 	zassert_mem_equal(new_expected_data, ccid_list, ARRAY_SIZE(new_expected_data));
1754 }
1755 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_get_parental_rating)1756 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_get_parental_rating)
1757 {
1758 	const struct bt_audio_codec_cap codec_cap =
1759 		BT_AUDIO_CODEC_CAP(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1760 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
1761 							BT_AUDIO_PARENTAL_RATING_AGE_10_OR_ABOVE)});
1762 	int ret;
1763 
1764 	ret = bt_audio_codec_cap_meta_get_parental_rating(&codec_cap);
1765 	zassert_equal(ret, 0x07, "Unexpected return value %d", ret);
1766 }
1767 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_set_parental_rating)1768 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_set_parental_rating)
1769 {
1770 	struct bt_audio_codec_cap codec_cap =
1771 		BT_AUDIO_CODEC_CAP(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1772 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PARENTAL_RATING,
1773 							BT_AUDIO_PARENTAL_RATING_AGE_10_OR_ABOVE)});
1774 	int ret;
1775 
1776 	ret = bt_audio_codec_cap_meta_get_parental_rating(&codec_cap);
1777 	zassert_equal(ret, 0x07, "Unexpected return value %d", ret);
1778 
1779 	ret = bt_audio_codec_cap_meta_set_parental_rating(&codec_cap,
1780 							  BT_AUDIO_PARENTAL_RATING_AGE_13_OR_ABOVE);
1781 	zassert_true(ret > 0, "Unexpected return value %d", ret);
1782 
1783 	ret = bt_audio_codec_cap_meta_get_parental_rating(&codec_cap);
1784 	zassert_equal(ret, 0x0a, "Unexpected return value %d", ret);
1785 }
1786 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_get_program_info_uri)1787 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_get_program_info_uri)
1788 {
1789 	const uint8_t expected_data[] = {'e', 'x', 'a', 'm', 'p', 'l', 'e', '.', 'c', 'o', 'm'};
1790 	const struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP(
1791 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1792 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PROGRAM_INFO_URI,
1793 				     'e', 'x', 'a', 'm', 'p', 'l', 'e', '.', 'c', 'o', 'm')});
1794 	const uint8_t *program_info_uri;
1795 	int ret;
1796 
1797 	ret = bt_audio_codec_cap_meta_get_program_info_uri(&codec_cap, &program_info_uri);
1798 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
1799 	zassert_mem_equal(expected_data, program_info_uri, ARRAY_SIZE(expected_data));
1800 }
1801 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_set_program_info_uri)1802 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_set_program_info_uri)
1803 {
1804 	const uint8_t expected_data[] = {'e', 'x', 'a', 'm', 'p', 'l', 'e', '.', 'c', 'o', 'm'};
1805 	const uint8_t new_expected_data[] = {'n', 'e', 'w', '.', 'c', 'o', 'm'};
1806 	struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP(
1807 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1808 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_PROGRAM_INFO_URI, 'e', 'x', 'a', 'm',
1809 				     'p', 'l', 'e', '.', 'c', 'o', 'm')});
1810 	const uint8_t *program_info_uri;
1811 	int ret;
1812 
1813 	ret = bt_audio_codec_cap_meta_get_program_info_uri(&codec_cap, &program_info_uri);
1814 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
1815 	zassert_mem_equal(expected_data, program_info_uri, ARRAY_SIZE(expected_data));
1816 
1817 	ret = bt_audio_codec_cap_meta_set_program_info_uri(&codec_cap, new_expected_data,
1818 							   ARRAY_SIZE(new_expected_data));
1819 	zassert_true(ret > 0, "Unexpected return value %d", ret);
1820 
1821 	ret = bt_audio_codec_cap_meta_get_program_info_uri(&codec_cap, &program_info_uri);
1822 	zassert_equal(ret, ARRAY_SIZE(new_expected_data), "Unexpected return value %d", ret);
1823 	zassert_mem_equal(new_expected_data, program_info_uri, ARRAY_SIZE(new_expected_data));
1824 }
1825 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_get_audio_active_state)1826 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_get_audio_active_state)
1827 {
1828 	const struct bt_audio_codec_cap codec_cap =
1829 		BT_AUDIO_CODEC_CAP(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1830 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_AUDIO_STATE,
1831 							BT_AUDIO_ACTIVE_STATE_ENABLED)});
1832 	int ret;
1833 
1834 	ret = bt_audio_codec_cap_meta_get_audio_active_state(&codec_cap);
1835 	zassert_equal(ret, 0x01, "Unexpected return value %d", ret);
1836 }
1837 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_set_audio_active_state)1838 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_set_audio_active_state)
1839 {
1840 	struct bt_audio_codec_cap codec_cap =
1841 		BT_AUDIO_CODEC_CAP(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1842 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_AUDIO_STATE,
1843 							BT_AUDIO_ACTIVE_STATE_ENABLED)});
1844 	int ret;
1845 
1846 	ret = bt_audio_codec_cap_meta_get_audio_active_state(&codec_cap);
1847 	zassert_equal(ret, 0x01, "Unexpected return value %d", ret);
1848 
1849 	ret = bt_audio_codec_cap_meta_set_audio_active_state(&codec_cap,
1850 							     BT_AUDIO_ACTIVE_STATE_DISABLED);
1851 	zassert_true(ret > 0, "Unexpected return value %d", ret);
1852 
1853 	ret = bt_audio_codec_cap_meta_get_audio_active_state(&codec_cap);
1854 	zassert_equal(ret, 0x00, "Unexpected return value %d", ret);
1855 }
1856 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_get_bcast_audio_immediate_rend_flag)1857 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_get_bcast_audio_immediate_rend_flag)
1858 {
1859 	const struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP(
1860 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1861 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_BROADCAST_IMMEDIATE)});
1862 	int ret;
1863 
1864 	ret = bt_audio_codec_cap_meta_get_bcast_audio_immediate_rend_flag(&codec_cap);
1865 	zassert_equal(ret, 0, "Unexpected return value %d", ret);
1866 }
1867 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_set_bcast_audio_immediate_rend_flag)1868 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_set_bcast_audio_immediate_rend_flag)
1869 {
1870 	struct bt_audio_codec_cap codec_cap =
1871 		BT_AUDIO_CODEC_CAP(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {}, {});
1872 	int ret;
1873 
1874 	ret = bt_audio_codec_cap_meta_get_bcast_audio_immediate_rend_flag(&codec_cap);
1875 	zassert_equal(ret, -ENODATA, "Unexpected return value %d", ret);
1876 
1877 	ret = bt_audio_codec_cap_meta_set_bcast_audio_immediate_rend_flag(&codec_cap);
1878 	zassert_true(ret > 0, "Unexpected return value %d", ret);
1879 
1880 	ret = bt_audio_codec_cap_meta_get_bcast_audio_immediate_rend_flag(&codec_cap);
1881 	zassert_equal(ret, 0, "Unexpected return value %d", ret);
1882 }
1883 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_get_assisted_listening_stream)1884 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_get_assisted_listening_stream)
1885 {
1886 	const struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP(
1887 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1888 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_ASSISTED_LISTENING_STREAM,
1889 				     BT_AUDIO_ASSISTED_LISTENING_STREAM_UNSPECIFIED)});
1890 	int ret;
1891 
1892 	ret = bt_audio_codec_cap_meta_get_assisted_listening_stream(&codec_cap);
1893 	zassert_equal(ret, 0x00, "Unexpected return value %d", ret);
1894 }
1895 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_set_assisted_listening_stream)1896 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_set_assisted_listening_stream)
1897 {
1898 	struct bt_audio_codec_cap codec_cap =
1899 		BT_AUDIO_CODEC_CAP(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {}, {});
1900 	int ret;
1901 
1902 	ret = bt_audio_codec_cap_meta_get_assisted_listening_stream(&codec_cap);
1903 	zassert_equal(ret, -ENODATA, "Unexpected return value %d", ret);
1904 
1905 	ret = bt_audio_codec_cap_meta_set_assisted_listening_stream(
1906 		&codec_cap, BT_AUDIO_ASSISTED_LISTENING_STREAM_UNSPECIFIED);
1907 	zassert_true(ret > 0, "Unexpected return value %d", ret);
1908 
1909 	ret = bt_audio_codec_cap_meta_get_assisted_listening_stream(&codec_cap);
1910 	zassert_equal(ret, 0x00, "Unexpected return value %d", ret);
1911 }
1912 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_get_broadcast_name)1913 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_get_broadcast_name)
1914 {
1915 	const uint8_t expected_data[] = {'m', 'y', ' ', 'b', 'c', 'a', 's', 't'};
1916 	const struct bt_audio_codec_cap codec_cap =
1917 		BT_AUDIO_CODEC_CAP(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1918 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_BROADCAST_NAME, 'm',
1919 							'y', ' ', 'b', 'c', 'a', 's', 't')});
1920 	const uint8_t *broadcast_name;
1921 	int ret;
1922 
1923 	ret = bt_audio_codec_cap_meta_get_broadcast_name(&codec_cap, &broadcast_name);
1924 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
1925 	zassert_mem_equal(expected_data, broadcast_name, ARRAY_SIZE(expected_data));
1926 }
1927 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_set_broadcast_name)1928 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_set_broadcast_name)
1929 {
1930 	const uint8_t expected_data[] = {'m', 'y', ' ', 'b', 'c', 'a', 's', 't'};
1931 	const uint8_t new_expected_data[] = {'n', 'e', 'w', ' ', 'b', 'c', 'a', 's', 't'};
1932 	struct bt_audio_codec_cap codec_cap =
1933 		BT_AUDIO_CODEC_CAP(BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1934 				   {BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_BROADCAST_NAME, 'm',
1935 							'y', ' ', 'b', 'c', 'a', 's', 't')});
1936 	const uint8_t *broadcast_name;
1937 	int ret;
1938 
1939 	ret = bt_audio_codec_cap_meta_get_broadcast_name(&codec_cap, &broadcast_name);
1940 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
1941 	zassert_mem_equal(expected_data, broadcast_name, ARRAY_SIZE(expected_data));
1942 
1943 	ret = bt_audio_codec_cap_meta_set_broadcast_name(&codec_cap, new_expected_data,
1944 							 ARRAY_SIZE(new_expected_data));
1945 	zassert_true(ret > 0, "Unexpected return value %d", ret);
1946 
1947 	ret = bt_audio_codec_cap_meta_get_broadcast_name(&codec_cap, &broadcast_name);
1948 	zassert_equal(ret, ARRAY_SIZE(new_expected_data), "Unexpected return value %d", ret);
1949 	zassert_mem_equal(new_expected_data, broadcast_name, ARRAY_SIZE(new_expected_data));
1950 }
1951 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_get_extended)1952 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_get_extended)
1953 {
1954 	const uint8_t expected_data[] = {0x00, 0x01, 0x02, 0x03};
1955 	const struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP(
1956 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1957 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_EXTENDED, 0x00, 0x01, 0x02, 0x03)});
1958 	const uint8_t *extended_meta;
1959 	int ret;
1960 
1961 	ret = bt_audio_codec_cap_meta_get_extended(&codec_cap, &extended_meta);
1962 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
1963 	zassert_mem_equal(expected_data, extended_meta, ARRAY_SIZE(expected_data));
1964 }
1965 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_set_extended)1966 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_set_extended)
1967 {
1968 	const uint8_t expected_data[] = {0x00, 0x01, 0x02, 0x03};
1969 	const uint8_t new_expected_data[] = {0x04, 0x05};
1970 	struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP(
1971 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1972 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_EXTENDED, 0x00, 0x01, 0x02, 0x03)});
1973 	const uint8_t *extended_meta;
1974 	int ret;
1975 
1976 	ret = bt_audio_codec_cap_meta_get_extended(&codec_cap, &extended_meta);
1977 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
1978 	zassert_mem_equal(expected_data, extended_meta, ARRAY_SIZE(expected_data));
1979 
1980 	ret = bt_audio_codec_cap_meta_set_extended(&codec_cap, new_expected_data,
1981 						   ARRAY_SIZE(new_expected_data));
1982 	zassert_true(ret > 0, "Unexpected return value %d", ret);
1983 
1984 	ret = bt_audio_codec_cap_meta_get_extended(&codec_cap, &extended_meta);
1985 	zassert_equal(ret, ARRAY_SIZE(new_expected_data), "Unexpected return value %d", ret);
1986 	zassert_mem_equal(new_expected_data, extended_meta, ARRAY_SIZE(new_expected_data));
1987 }
1988 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_get_vendor)1989 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_get_vendor)
1990 {
1991 	const uint8_t expected_data[] = {0x00, 0x01, 0x02, 0x03};
1992 	const struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP(
1993 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
1994 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_VENDOR, 0x00, 0x01, 0x02, 0x03)});
1995 	const uint8_t *vendor_meta;
1996 	int ret;
1997 
1998 	ret = bt_audio_codec_cap_meta_get_vendor(&codec_cap, &vendor_meta);
1999 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
2000 	zassert_mem_equal(expected_data, vendor_meta, ARRAY_SIZE(expected_data));
2001 }
2002 
ZTEST(audio_codec_test_suite,test_bt_audio_codec_cap_meta_set_vendor)2003 ZTEST(audio_codec_test_suite, test_bt_audio_codec_cap_meta_set_vendor)
2004 {
2005 	const uint8_t expected_data[] = {0x00, 0x01, 0x02, 0x03};
2006 	const uint8_t new_expected_data[] = {0x04, 0x05, 0x06, 0x07, 0x08};
2007 	struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP(
2008 		BT_HCI_CODING_FORMAT_LC3, 0x0000, 0x0000, {},
2009 		{BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_VENDOR, 0x00, 0x01, 0x02, 0x03)});
2010 	const uint8_t *extended_meta;
2011 	int ret;
2012 
2013 	ret = bt_audio_codec_cap_meta_get_vendor(&codec_cap, &extended_meta);
2014 	zassert_equal(ret, ARRAY_SIZE(expected_data), "Unexpected return value %d", ret);
2015 	zassert_mem_equal(expected_data, extended_meta, ARRAY_SIZE(expected_data));
2016 
2017 	ret = bt_audio_codec_cap_meta_set_vendor(&codec_cap, new_expected_data,
2018 						 ARRAY_SIZE(new_expected_data));
2019 	zassert_true(ret > 0, "Unexpected return value %d", ret);
2020 
2021 	ret = bt_audio_codec_cap_meta_get_vendor(&codec_cap, &extended_meta);
2022 	zassert_equal(ret, ARRAY_SIZE(new_expected_data), "Unexpected return value %d", ret);
2023 	zassert_mem_equal(new_expected_data, extended_meta, ARRAY_SIZE(new_expected_data));
2024 }
2025