1 /* test_ase_state_transition.c - ASE state transition tests */
2 
3 /*
4  * Copyright (c) 2023 Codecoup
5  * Copyright (c) 2024 Demant A/S
6  *
7  * SPDX-License-Identifier: Apache-2.0
8  */
9 
10 #include <stdlib.h>
11 #include <zephyr/kernel.h>
12 #include <zephyr/types.h>
13 #include <zephyr/bluetooth/audio/audio.h>
14 #include <zephyr/bluetooth/audio/bap.h>
15 #include <zephyr/bluetooth/conn.h>
16 #include <zephyr/bluetooth/gatt.h>
17 #include <zephyr/sys/util_macro.h>
18 #include <zephyr/ztest_assert.h>
19 
20 #include "bap_unicast_server.h"
21 #include "bap_unicast_server_expects.h"
22 #include "bap_stream.h"
23 #include "bap_stream_expects.h"
24 #include "conn.h"
25 #include "gatt.h"
26 #include "gatt_expects.h"
27 #include "iso.h"
28 
29 #include "test_common.h"
30 
31 #define test_sink_ase_state_transition_fixture test_ase_state_transition_fixture
32 #define test_source_ase_state_transition_fixture test_ase_state_transition_fixture
33 
34 static const struct bt_bap_qos_cfg_pref qos_pref =
35 	BT_BAP_QOS_CFG_PREF(true, BT_GAP_LE_PHY_2M, 0x02, 10, 40000, 40000, 40000, 40000);
36 
37 struct test_ase_state_transition_fixture {
38 	struct bt_conn conn;
39 	struct bt_bap_stream stream;
40 	struct {
41 		uint8_t id;
42 		const struct bt_gatt_attr *attr;
43 	} ase;
44 };
45 
test_sink_ase_state_transition_setup(void)46 static void *test_sink_ase_state_transition_setup(void)
47 {
48 	struct test_ase_state_transition_fixture *fixture;
49 
50 	fixture = malloc(sizeof(*fixture));
51 	zassert_not_null(fixture);
52 
53 	return fixture;
54 }
55 
test_ase_snk_state_transition_before(void * f)56 static void test_ase_snk_state_transition_before(void *f)
57 {
58 	struct test_ase_state_transition_fixture *fixture =
59 		(struct test_ase_state_transition_fixture *) f;
60 	struct bt_bap_unicast_server_register_param param = {
61 		CONFIG_BT_ASCS_MAX_ASE_SNK_COUNT,
62 		CONFIG_BT_ASCS_MAX_ASE_SRC_COUNT
63 	};
64 	int err;
65 
66 	err = bt_bap_unicast_server_register(&param);
67 	zassert_equal(err, 0, "unexpected err response %d", err);
68 
69 	err = bt_bap_unicast_server_register_cb(&mock_bap_unicast_server_cb);
70 	zassert_equal(err, 0, "unexpected err response %d", err);
71 
72 	memset(fixture, 0, sizeof(struct test_ase_state_transition_fixture));
73 	test_conn_init(&fixture->conn);
74 	test_ase_snk_get(1, &fixture->ase.attr);
75 	if (fixture->ase.attr != NULL) {
76 		fixture->ase.id = test_ase_id_get(fixture->ase.attr);
77 	}
78 
79 	bt_bap_stream_cb_register(&fixture->stream, &mock_bap_stream_ops);
80 }
81 
test_ase_src_state_transition_before(void * f)82 static void test_ase_src_state_transition_before(void *f)
83 {
84 	struct test_ase_state_transition_fixture *fixture =
85 		(struct test_ase_state_transition_fixture *) f;
86 	struct bt_bap_unicast_server_register_param param = {
87 		CONFIG_BT_ASCS_MAX_ASE_SNK_COUNT,
88 		CONFIG_BT_ASCS_MAX_ASE_SRC_COUNT
89 	};
90 	int err;
91 
92 	err = bt_bap_unicast_server_register(&param);
93 	zassert_equal(err, 0, "unexpected err response %d", err);
94 
95 	err = bt_bap_unicast_server_register_cb(&mock_bap_unicast_server_cb);
96 	zassert_equal(err, 0, "unexpected err response %d", err);
97 
98 	memset(fixture, 0, sizeof(struct test_ase_state_transition_fixture));
99 	test_conn_init(&fixture->conn);
100 	test_ase_src_get(CONFIG_BT_ASCS_MAX_ASE_SRC_COUNT, &fixture->ase.attr);
101 	if (fixture->ase.attr != NULL) {
102 		fixture->ase.id = test_ase_id_get(fixture->ase.attr);
103 	}
104 
105 	bt_bap_stream_cb_register(&fixture->stream, &mock_bap_stream_ops);
106 }
107 
test_ase_state_transition_after(void * f)108 static void test_ase_state_transition_after(void *f)
109 {
110 	int err;
111 
112 	err = bt_bap_unicast_server_unregister_cb(&mock_bap_unicast_server_cb);
113 	zassert_equal(err, 0, "unexpected err response %d", err);
114 
115 	/* Sleep to trigger any pending state changes from unregister_cb */
116 	k_sleep(K_SECONDS(1));
117 
118 	err = bt_bap_unicast_server_unregister();
119 	zassert_equal(err, 0, "Unexpected err response %d", err);
120 }
121 
test_ase_state_transition_teardown(void * f)122 static void test_ase_state_transition_teardown(void *f)
123 {
124 	free(f);
125 }
126 
127 ZTEST_SUITE(test_sink_ase_state_transition, NULL, test_sink_ase_state_transition_setup,
128 	    test_ase_snk_state_transition_before, test_ase_state_transition_after,
129 	    test_ase_state_transition_teardown);
130 
ZTEST_F(test_sink_ase_state_transition,test_client_idle_to_codec_configured)131 ZTEST_F(test_sink_ase_state_transition, test_client_idle_to_codec_configured)
132 {
133 	struct bt_bap_stream *stream = &fixture->stream;
134 	struct bt_conn *conn = &fixture->conn;
135 	uint8_t ase_id = fixture->ase.id;
136 
137 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
138 
139 	test_ase_control_client_config_codec(conn, ase_id, stream);
140 
141 	/* Verification */
142 	expect_bt_bap_unicast_server_cb_config_called_once(conn, EMPTY, BT_AUDIO_DIR_SINK, EMPTY);
143 	expect_bt_bap_stream_ops_configured_called_once(stream, EMPTY);
144 }
ZTEST_F(test_sink_ase_state_transition,test_client_codec_configured_to_qos_configured)145 ZTEST_F(test_sink_ase_state_transition, test_client_codec_configured_to_qos_configured)
146 {
147 	struct bt_bap_stream *stream = &fixture->stream;
148 	struct bt_conn *conn = &fixture->conn;
149 	uint8_t ase_id = fixture->ase.id;
150 
151 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
152 
153 	test_preamble_state_codec_configured(conn, ase_id, stream);
154 
155 	test_ase_control_client_config_qos(conn, ase_id);
156 
157 	/* Verification */
158 	expect_bt_bap_unicast_server_cb_qos_called_once(stream, EMPTY);
159 	expect_bt_bap_stream_ops_qos_set_called_once(stream);
160 	expect_bt_bap_stream_ops_disabled_not_called();
161 }
162 
ZTEST_F(test_sink_ase_state_transition,test_client_qos_configured_to_enabling)163 ZTEST_F(test_sink_ase_state_transition, test_client_qos_configured_to_enabling)
164 {
165 	struct bt_bap_stream *stream = &fixture->stream;
166 	struct bt_conn *conn = &fixture->conn;
167 	uint8_t ase_id = fixture->ase.id;
168 
169 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
170 
171 	test_preamble_state_qos_configured(conn, ase_id, stream);
172 
173 	test_ase_control_client_enable(conn, ase_id);
174 
175 	/* Verification */
176 	expect_bt_bap_unicast_server_cb_enable_called_once(stream, EMPTY, EMPTY);
177 	expect_bt_bap_stream_ops_enabled_called_once(stream);
178 }
179 
ZTEST_F(test_sink_ase_state_transition,test_client_enabling_to_qos_configured)180 ZTEST_F(test_sink_ase_state_transition, test_client_enabling_to_qos_configured)
181 {
182 	struct bt_bap_stream *stream = &fixture->stream;
183 	struct bt_conn *conn = &fixture->conn;
184 	uint8_t ase_id = fixture->ase.id;
185 
186 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
187 
188 	test_preamble_state_enabling(conn, ase_id, stream);
189 
190 	test_ase_control_client_disable(conn, ase_id);
191 
192 	/* Verification */
193 	expect_bt_bap_unicast_server_cb_disable_called_once(stream);
194 	expect_bt_bap_stream_ops_qos_set_called_once(stream);
195 	expect_bt_bap_stream_ops_disabled_called_once(stream);
196 }
197 
ZTEST_F(test_sink_ase_state_transition,test_client_qos_configured_to_releasing)198 ZTEST_F(test_sink_ase_state_transition, test_client_qos_configured_to_releasing)
199 {
200 	struct bt_bap_stream *stream = &fixture->stream;
201 	struct bt_conn *conn = &fixture->conn;
202 	uint8_t ase_id = fixture->ase.id;
203 
204 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
205 
206 	test_preamble_state_qos_configured(conn, ase_id, stream);
207 
208 	test_ase_control_client_release(conn, ase_id);
209 
210 	/* Verification */
211 	expect_bt_bap_unicast_server_cb_release_called_once(stream);
212 	expect_bt_bap_stream_ops_released_called_once(stream);
213 }
214 
ZTEST_F(test_sink_ase_state_transition,test_client_codec_configured_to_codec_configured)215 ZTEST_F(test_sink_ase_state_transition, test_client_codec_configured_to_codec_configured)
216 {
217 	struct bt_bap_stream *stream = &fixture->stream;
218 	struct bt_conn *conn = &fixture->conn;
219 	uint8_t ase_id = fixture->ase.id;
220 
221 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
222 
223 	test_preamble_state_codec_configured(conn, ase_id, stream);
224 
225 	test_ase_control_client_config_codec(conn, ase_id, stream);
226 
227 	/* Verification */
228 	expect_bt_bap_unicast_server_cb_config_not_called();
229 	expect_bt_bap_unicast_server_cb_reconfig_called_once(stream, BT_AUDIO_DIR_SINK, EMPTY);
230 	expect_bt_bap_stream_ops_configured_called_once(stream, EMPTY);
231 }
232 
ZTEST_F(test_sink_ase_state_transition,test_client_qos_configured_to_qos_configured)233 ZTEST_F(test_sink_ase_state_transition, test_client_qos_configured_to_qos_configured)
234 {
235 	struct bt_bap_stream *stream = &fixture->stream;
236 	struct bt_conn *conn = &fixture->conn;
237 	uint8_t ase_id = fixture->ase.id;
238 
239 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
240 
241 	test_preamble_state_qos_configured(conn, ase_id, stream);
242 
243 	test_ase_control_client_config_qos(conn, ase_id);
244 
245 	/* Verification */
246 	expect_bt_bap_unicast_server_cb_qos_called_once(stream, EMPTY);
247 	expect_bt_bap_stream_ops_qos_set_called_once(stream);
248 	expect_bt_bap_stream_ops_disabled_not_called();
249 }
250 
ZTEST_F(test_sink_ase_state_transition,test_client_qos_configured_to_codec_configured)251 ZTEST_F(test_sink_ase_state_transition, test_client_qos_configured_to_codec_configured)
252 {
253 	struct bt_bap_stream *stream = &fixture->stream;
254 	struct bt_conn *conn = &fixture->conn;
255 	uint8_t ase_id = fixture->ase.id;
256 
257 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
258 
259 	test_preamble_state_qos_configured(conn, ase_id, stream);
260 
261 	test_ase_control_client_config_codec(conn, ase_id, stream);
262 
263 	/* Verification */
264 	expect_bt_bap_unicast_server_cb_reconfig_called_once(stream, BT_AUDIO_DIR_SINK, EMPTY);
265 	expect_bt_bap_stream_ops_configured_called_once(stream, EMPTY);
266 }
267 
ZTEST_F(test_sink_ase_state_transition,test_client_codec_configured_to_releasing)268 ZTEST_F(test_sink_ase_state_transition, test_client_codec_configured_to_releasing)
269 {
270 	struct bt_bap_stream *stream = &fixture->stream;
271 	struct bt_conn *conn = &fixture->conn;
272 	uint8_t ase_id = fixture->ase.id;
273 
274 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
275 
276 	test_preamble_state_codec_configured(conn, ase_id, stream);
277 
278 	test_ase_control_client_release(conn, ase_id);
279 
280 	/* Verification */
281 	expect_bt_bap_unicast_server_cb_release_called_once(stream);
282 	expect_bt_bap_stream_ops_released_called_once(stream);
283 }
284 
ZTEST_F(test_sink_ase_state_transition,test_client_enabling_to_releasing)285 ZTEST_F(test_sink_ase_state_transition, test_client_enabling_to_releasing)
286 {
287 	struct bt_bap_stream *stream = &fixture->stream;
288 	struct bt_conn *conn = &fixture->conn;
289 	uint8_t ase_id = fixture->ase.id;
290 
291 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
292 
293 	test_preamble_state_enabling(conn, ase_id, stream);
294 
295 	test_ase_control_client_release(conn, ase_id);
296 
297 	/* Verification */
298 	expect_bt_bap_unicast_server_cb_release_called_once(stream);
299 	expect_bt_bap_stream_ops_released_called_once(stream);
300 	expect_bt_bap_stream_ops_disabled_not_called();
301 }
302 
ZTEST_F(test_sink_ase_state_transition,test_client_enabling_to_enabling)303 ZTEST_F(test_sink_ase_state_transition, test_client_enabling_to_enabling)
304 {
305 	struct bt_bap_stream *stream = &fixture->stream;
306 	struct bt_conn *conn = &fixture->conn;
307 	uint8_t ase_id = fixture->ase.id;
308 
309 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
310 
311 	test_preamble_state_enabling(conn, ase_id, stream);
312 
313 	test_ase_control_client_update_metadata(conn, ase_id);
314 
315 	/* Verification */
316 	expect_bt_bap_unicast_server_cb_metadata_called_once(stream, EMPTY, EMPTY);
317 	expect_bt_bap_stream_ops_metadata_updated_called_once(stream);
318 	expect_bt_bap_stream_ops_disabled_not_called();
319 }
320 
ZTEST_F(test_sink_ase_state_transition,test_client_streaming_to_releasing)321 ZTEST_F(test_sink_ase_state_transition, test_client_streaming_to_releasing)
322 {
323 	struct bt_bap_stream *stream = &fixture->stream;
324 	struct bt_conn *conn = &fixture->conn;
325 	uint8_t ase_id = fixture->ase.id;
326 	struct bt_iso_chan *chan;
327 
328 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
329 
330 	test_preamble_state_streaming(conn, ase_id, stream, &chan, false);
331 
332 	test_ase_control_client_release(conn, ase_id);
333 
334 	/* Client disconnects the ISO */
335 	mock_bt_iso_disconnected(chan, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
336 
337 	/* Verification */
338 	expect_bt_bap_unicast_server_cb_release_called_once(stream);
339 	expect_bt_bap_stream_ops_stopped_called_once(stream, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
340 	expect_bt_bap_stream_ops_released_called_once(stream);
341 	expect_bt_bap_stream_ops_disabled_not_called();
342 	expect_bt_bap_stream_ops_disconnected_called_once(stream);
343 }
344 
ZTEST_F(test_sink_ase_state_transition,test_client_streaming_to_streaming)345 ZTEST_F(test_sink_ase_state_transition, test_client_streaming_to_streaming)
346 {
347 	struct bt_bap_stream *stream = &fixture->stream;
348 	struct bt_conn *conn = &fixture->conn;
349 	uint8_t ase_id = fixture->ase.id;
350 	struct bt_iso_chan *chan;
351 
352 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
353 
354 	test_preamble_state_streaming(conn, ase_id, stream, &chan, false);
355 
356 	test_ase_control_client_update_metadata(conn, ase_id);
357 
358 	/* Verification */
359 	expect_bt_bap_unicast_server_cb_metadata_called_once(stream, EMPTY, EMPTY);
360 	expect_bt_bap_stream_ops_metadata_updated_called_once(stream);
361 	expect_bt_bap_stream_ops_disabled_not_called();
362 }
363 
ZTEST_F(test_sink_ase_state_transition,test_client_streaming_to_qos_configured)364 ZTEST_F(test_sink_ase_state_transition, test_client_streaming_to_qos_configured)
365 {
366 	struct bt_bap_stream *stream = &fixture->stream;
367 	struct bt_conn *conn = &fixture->conn;
368 	uint8_t ase_id = fixture->ase.id;
369 	struct bt_iso_chan *chan;
370 
371 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
372 
373 	test_preamble_state_streaming(conn, ase_id, stream, &chan, false);
374 
375 	test_ase_control_client_disable(conn, ase_id);
376 
377 	/* Verification */
378 	expect_bt_bap_unicast_server_cb_disable_called_once(stream);
379 	expect_bt_bap_stream_ops_stopped_called_once(stream, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
380 	expect_bt_bap_stream_ops_qos_set_called_once(stream);
381 	expect_bt_bap_stream_ops_disabled_called_once(stream);
382 }
383 
ZTEST_F(test_sink_ase_state_transition,test_server_idle_to_codec_configured)384 ZTEST_F(test_sink_ase_state_transition, test_server_idle_to_codec_configured)
385 {
386 	struct bt_audio_codec_cfg codec_cfg = BT_AUDIO_CODEC_LC3_CONFIG(
387 		BT_AUDIO_CODEC_CFG_FREQ_16KHZ, BT_AUDIO_CODEC_CFG_DURATION_10,
388 		BT_AUDIO_LOCATION_FRONT_LEFT, 40U, 1, BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
389 	struct bt_bap_stream *stream = &fixture->stream;
390 	struct bt_conn *conn = &fixture->conn;
391 	int err;
392 
393 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
394 
395 	err = bt_bap_unicast_server_config_ase(conn, stream, &codec_cfg, &qos_pref);
396 	zassert_false(err < 0, "bt_bap_unicast_server_config_ase returned err %d", err);
397 
398 	/* Verification */
399 	expect_bt_bap_unicast_server_cb_config_not_called();
400 	expect_bt_bap_stream_ops_configured_called_once(stream, EMPTY);
401 }
402 
ZTEST_F(test_sink_ase_state_transition,test_server_codec_configured_to_codec_configured)403 ZTEST_F(test_sink_ase_state_transition, test_server_codec_configured_to_codec_configured)
404 {
405 	struct bt_audio_codec_cfg codec_cfg = BT_AUDIO_CODEC_LC3_CONFIG(
406 		BT_AUDIO_CODEC_CFG_FREQ_16KHZ, BT_AUDIO_CODEC_CFG_DURATION_10,
407 		BT_AUDIO_LOCATION_FRONT_LEFT, 40U, 1, BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
408 	struct bt_bap_stream *stream = &fixture->stream;
409 	struct bt_conn *conn = &fixture->conn;
410 	uint8_t ase_id = fixture->ase.id;
411 	int err;
412 
413 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
414 
415 	test_preamble_state_codec_configured(conn, ase_id, stream);
416 
417 	err = bt_bap_stream_reconfig(stream, &codec_cfg);
418 	zassert_false(err < 0, "bt_bap_stream_reconfig returned err %d", err);
419 
420 	/* Verification */
421 	expect_bt_bap_unicast_server_cb_reconfig_called_once(stream, BT_AUDIO_DIR_SINK, EMPTY);
422 	expect_bt_bap_stream_ops_configured_called_once(stream, EMPTY);
423 }
424 
ZTEST_F(test_sink_ase_state_transition,test_server_codec_configured_to_releasing)425 ZTEST_F(test_sink_ase_state_transition, test_server_codec_configured_to_releasing)
426 {
427 	struct bt_bap_stream *stream = &fixture->stream;
428 	struct bt_conn *conn = &fixture->conn;
429 	uint8_t ase_id = fixture->ase.id;
430 	int err;
431 
432 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
433 
434 	test_preamble_state_codec_configured(conn, ase_id, stream);
435 
436 	err = bt_bap_stream_release(stream);
437 	zassert_false(err < 0, "bt_bap_stream_release returned err %d", err);
438 
439 	/* Verification */
440 	expect_bt_bap_unicast_server_cb_release_called_once(stream);
441 	expect_bt_bap_stream_ops_released_called_once(stream);
442 }
443 
ZTEST_F(test_sink_ase_state_transition,test_server_qos_configured_to_codec_configured)444 ZTEST_F(test_sink_ase_state_transition, test_server_qos_configured_to_codec_configured)
445 {
446 	struct bt_audio_codec_cfg codec_cfg = BT_AUDIO_CODEC_LC3_CONFIG(
447 		BT_AUDIO_CODEC_CFG_FREQ_16KHZ, BT_AUDIO_CODEC_CFG_DURATION_10,
448 		BT_AUDIO_LOCATION_FRONT_LEFT, 40U, 1, BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
449 	struct bt_bap_stream *stream = &fixture->stream;
450 	struct bt_conn *conn = &fixture->conn;
451 	uint8_t ase_id = fixture->ase.id;
452 	int err;
453 
454 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
455 
456 	test_preamble_state_qos_configured(conn, ase_id, stream);
457 
458 	err = bt_bap_stream_reconfig(stream, &codec_cfg);
459 	zassert_false(err < 0, "bt_bap_stream_reconfig returned err %d", err);
460 
461 	/* Verification */
462 	expect_bt_bap_unicast_server_cb_reconfig_called_once(stream, BT_AUDIO_DIR_SINK, EMPTY);
463 	expect_bt_bap_stream_ops_configured_called_once(stream, EMPTY);
464 }
465 
ZTEST_F(test_sink_ase_state_transition,test_server_qos_configured_to_releasing)466 ZTEST_F(test_sink_ase_state_transition, test_server_qos_configured_to_releasing)
467 {
468 	struct bt_bap_stream *stream = &fixture->stream;
469 	struct bt_conn *conn = &fixture->conn;
470 	uint8_t ase_id = fixture->ase.id;
471 	int err;
472 
473 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
474 
475 	test_preamble_state_qos_configured(conn, ase_id, stream);
476 
477 	err = bt_bap_stream_release(stream);
478 	zassert_false(err < 0, "bt_bap_stream_release returned err %d", err);
479 
480 	/* Verification */
481 	expect_bt_bap_unicast_server_cb_release_called_once(stream);
482 	expect_bt_bap_stream_ops_released_called_once(stream);
483 }
484 
ZTEST_F(test_sink_ase_state_transition,test_server_enabling_to_releasing)485 ZTEST_F(test_sink_ase_state_transition, test_server_enabling_to_releasing)
486 {
487 	struct bt_bap_stream *stream = &fixture->stream;
488 	struct bt_conn *conn = &fixture->conn;
489 	uint8_t ase_id = fixture->ase.id;
490 	int err;
491 
492 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
493 
494 	test_preamble_state_enabling(conn, ase_id, stream);
495 
496 	err = bt_bap_stream_release(stream);
497 	zassert_false(err < 0, "bt_bap_stream_release returned err %d", err);
498 
499 	/* Verification */
500 	expect_bt_bap_unicast_server_cb_release_called_once(stream);
501 	expect_bt_bap_stream_ops_released_called_once(stream);
502 	expect_bt_bap_stream_ops_disabled_not_called();
503 }
504 
ZTEST_F(test_sink_ase_state_transition,test_server_enabling_to_enabling)505 ZTEST_F(test_sink_ase_state_transition, test_server_enabling_to_enabling)
506 {
507 	const uint8_t meta[] = {
508 		BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_STREAM_CONTEXT,
509 				    BT_BYTES_LIST_LE16(BT_AUDIO_CONTEXT_TYPE_MEDIA)),
510 	};
511 	struct bt_bap_stream *stream = &fixture->stream;
512 	struct bt_conn *conn = &fixture->conn;
513 	uint8_t ase_id = fixture->ase.id;
514 	int err;
515 
516 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
517 
518 	test_preamble_state_enabling(conn, ase_id, stream);
519 
520 	err = bt_bap_stream_metadata(stream, meta, ARRAY_SIZE(meta));
521 	zassert_false(err < 0, "bt_bap_stream_metadata returned err %d", err);
522 
523 	/* Verification */
524 	expect_bt_bap_unicast_server_cb_metadata_called_once(stream, EMPTY, EMPTY);
525 	expect_bt_bap_stream_ops_metadata_updated_called_once(stream);
526 	expect_bt_bap_stream_ops_disabled_not_called();
527 }
528 
ZTEST_F(test_sink_ase_state_transition,test_server_enabling_to_qos_configured)529 ZTEST_F(test_sink_ase_state_transition, test_server_enabling_to_qos_configured)
530 {
531 	struct bt_bap_stream *stream = &fixture->stream;
532 	struct bt_conn *conn = &fixture->conn;
533 	uint8_t ase_id = fixture->ase.id;
534 	int err;
535 
536 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
537 
538 	test_preamble_state_enabling(conn, ase_id, stream);
539 
540 	err = bt_bap_stream_disable(stream);
541 	zassert_false(err < 0, "bt_bap_stream_disable returned err %d", err);
542 
543 	/* Verification */
544 	expect_bt_bap_unicast_server_cb_disable_called_once(stream);
545 	expect_bt_bap_stream_ops_qos_set_called_once(stream);
546 	expect_bt_bap_stream_ops_disabled_called_once(stream);
547 }
548 
ZTEST_F(test_sink_ase_state_transition,test_server_enabling_to_streaming)549 ZTEST_F(test_sink_ase_state_transition, test_server_enabling_to_streaming)
550 {
551 	struct bt_bap_stream *stream = &fixture->stream;
552 	struct bt_conn *conn = &fixture->conn;
553 	uint8_t ase_id = fixture->ase.id;
554 	struct bt_iso_chan *chan;
555 	int err;
556 
557 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
558 
559 	test_preamble_state_enabling(conn, ase_id, stream);
560 
561 	err = mock_bt_iso_accept(conn, 0x01, 0x01, &chan);
562 	zassert_equal(0, err, "Failed to connect iso: err %d", err);
563 
564 	err = bt_bap_stream_start(stream);
565 	zassert_false(err < 0, "bt_bap_stream_start returned err %d", err);
566 
567 	/* Verification */
568 	expect_bt_bap_stream_ops_connected_called_once(stream);
569 	expect_bt_bap_stream_ops_started_called_once(stream);
570 	expect_bt_bap_stream_ops_disabled_not_called();
571 	/* XXX: unicast_server_cb->start is not called for Sink ASE */
572 }
573 
ZTEST_F(test_sink_ase_state_transition,test_server_streaming_to_streaming)574 ZTEST_F(test_sink_ase_state_transition, test_server_streaming_to_streaming)
575 {
576 	const uint8_t meta[] = {
577 		BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_STREAM_CONTEXT,
578 				    BT_BYTES_LIST_LE16(BT_AUDIO_CONTEXT_TYPE_MEDIA)),
579 	};
580 	struct bt_bap_stream *stream = &fixture->stream;
581 	struct bt_conn *conn = &fixture->conn;
582 	uint8_t ase_id = fixture->ase.id;
583 	struct bt_iso_chan *chan;
584 	int err;
585 
586 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
587 
588 	test_preamble_state_streaming(conn, ase_id, stream, &chan, false);
589 
590 	err = bt_bap_stream_metadata(stream, meta, ARRAY_SIZE(meta));
591 	zassert_false(err < 0, "bt_bap_stream_metadata returned err %d", err);
592 
593 	/* Verification */
594 	expect_bt_bap_unicast_server_cb_metadata_called_once(stream, EMPTY, EMPTY);
595 	expect_bt_bap_stream_ops_metadata_updated_called_once(stream);
596 	expect_bt_bap_stream_ops_disabled_not_called();
597 }
598 
ZTEST_F(test_sink_ase_state_transition,test_server_streaming_to_qos_configured)599 ZTEST_F(test_sink_ase_state_transition, test_server_streaming_to_qos_configured)
600 {
601 	struct bt_bap_stream *stream = &fixture->stream;
602 	struct bt_conn *conn = &fixture->conn;
603 	uint8_t ase_id = fixture->ase.id;
604 	struct bt_iso_chan *chan;
605 	int err;
606 
607 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
608 
609 	test_preamble_state_streaming(conn, ase_id, stream, &chan, false);
610 
611 	err = bt_bap_stream_disable(stream);
612 	zassert_false(err < 0, "bt_bap_stream_disable returned err %d", err);
613 
614 	/* Verification */
615 	expect_bt_bap_unicast_server_cb_disable_called_once(stream);
616 	expect_bt_bap_stream_ops_stopped_called_once(stream, BT_HCI_ERR_LOCALHOST_TERM_CONN);
617 	expect_bt_bap_stream_ops_qos_set_called_once(stream);
618 	expect_bt_bap_stream_ops_disabled_called_once(stream);
619 }
620 
ZTEST_F(test_sink_ase_state_transition,test_server_streaming_to_releasing)621 ZTEST_F(test_sink_ase_state_transition, test_server_streaming_to_releasing)
622 {
623 	struct bt_bap_stream *stream = &fixture->stream;
624 	struct bt_conn *conn = &fixture->conn;
625 	uint8_t ase_id = fixture->ase.id;
626 	struct bt_iso_chan *chan;
627 	int err;
628 
629 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SNK);
630 
631 	test_preamble_state_streaming(conn, ase_id, stream, &chan, false);
632 
633 	err = bt_bap_stream_release(stream);
634 	zassert_false(err < 0, "bt_bap_stream_release returned err %d", err);
635 
636 	/* Client disconnects the ISO */
637 	mock_bt_iso_disconnected(chan, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
638 
639 	/* Verification */
640 	expect_bt_bap_unicast_server_cb_release_called_once(stream);
641 	expect_bt_bap_stream_ops_stopped_called_once(stream, BT_HCI_ERR_LOCALHOST_TERM_CONN);
642 	expect_bt_bap_stream_ops_released_called_once(stream);
643 	expect_bt_bap_stream_ops_disabled_not_called();
644 	expect_bt_bap_stream_ops_disconnected_called_once(stream);
645 }
646 
test_source_ase_state_transition_setup(void)647 static void *test_source_ase_state_transition_setup(void)
648 {
649 	struct test_ase_state_transition_fixture *fixture;
650 
651 	fixture = malloc(sizeof(*fixture));
652 	zassert_not_null(fixture);
653 
654 	memset(fixture, 0, sizeof(*fixture));
655 	test_conn_init(&fixture->conn);
656 	test_ase_src_get(CONFIG_BT_ASCS_MAX_ASE_SRC_COUNT, &fixture->ase.attr);
657 	if (fixture->ase.attr != NULL) {
658 		fixture->ase.id = test_ase_id_get(fixture->ase.attr);
659 	}
660 
661 	return fixture;
662 }
663 
664 ZTEST_SUITE(test_source_ase_state_transition, NULL, test_source_ase_state_transition_setup,
665 	    test_ase_src_state_transition_before, test_ase_state_transition_after,
666 	    test_ase_state_transition_teardown);
667 
ZTEST_F(test_source_ase_state_transition,test_client_idle_to_codec_configured)668 ZTEST_F(test_source_ase_state_transition, test_client_idle_to_codec_configured)
669 {
670 	struct bt_bap_stream *stream = &fixture->stream;
671 	struct bt_conn *conn = &fixture->conn;
672 	uint8_t ase_id = fixture->ase.id;
673 
674 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
675 
676 	test_ase_control_client_config_codec(conn, ase_id, stream);
677 
678 	/* Verification */
679 	expect_bt_bap_unicast_server_cb_config_called_once(conn, EMPTY, BT_AUDIO_DIR_SOURCE, EMPTY);
680 	expect_bt_bap_stream_ops_configured_called_once(stream, EMPTY);
681 }
682 
ZTEST_F(test_source_ase_state_transition,test_client_codec_configured_to_qos_configured)683 ZTEST_F(test_source_ase_state_transition, test_client_codec_configured_to_qos_configured)
684 {
685 	struct bt_bap_stream *stream = &fixture->stream;
686 	struct bt_conn *conn = &fixture->conn;
687 	uint8_t ase_id = fixture->ase.id;
688 
689 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
690 
691 	test_preamble_state_codec_configured(conn, ase_id, stream);
692 
693 	test_ase_control_client_config_qos(conn, ase_id);
694 
695 	/* Verification */
696 	expect_bt_bap_unicast_server_cb_qos_called_once(stream, EMPTY);
697 	expect_bt_bap_stream_ops_qos_set_called_once(stream);
698 	expect_bt_bap_stream_ops_disabled_not_called();
699 }
700 
ZTEST_F(test_source_ase_state_transition,test_client_qos_configured_to_enabling)701 ZTEST_F(test_source_ase_state_transition, test_client_qos_configured_to_enabling)
702 {
703 	struct bt_bap_stream *stream = &fixture->stream;
704 	struct bt_conn *conn = &fixture->conn;
705 	uint8_t ase_id = fixture->ase.id;
706 
707 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
708 
709 	test_preamble_state_qos_configured(conn, ase_id, stream);
710 
711 	test_ase_control_client_enable(conn, ase_id);
712 
713 	/* Verification */
714 	expect_bt_bap_unicast_server_cb_enable_called_once(stream, EMPTY, EMPTY);
715 	expect_bt_bap_stream_ops_enabled_called_once(stream);
716 }
717 
ZTEST_F(test_source_ase_state_transition,test_client_enabling_to_disabling)718 ZTEST_F(test_source_ase_state_transition, test_client_enabling_to_disabling)
719 {
720 	struct bt_bap_stream *stream = &fixture->stream;
721 	struct bt_conn *conn = &fixture->conn;
722 	uint8_t ase_id = fixture->ase.id;
723 
724 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
725 
726 	test_preamble_state_enabling(conn, ase_id, stream);
727 
728 	test_ase_control_client_disable(conn, ase_id);
729 
730 	/* Verification */
731 	expect_bt_bap_unicast_server_cb_disable_called_once(stream);
732 	expect_bt_bap_stream_ops_disabled_called_once(stream);
733 }
734 
ZTEST_F(test_source_ase_state_transition,test_client_qos_configured_to_releasing)735 ZTEST_F(test_source_ase_state_transition, test_client_qos_configured_to_releasing)
736 {
737 	struct bt_bap_stream *stream = &fixture->stream;
738 	struct bt_conn *conn = &fixture->conn;
739 	uint8_t ase_id = fixture->ase.id;
740 
741 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
742 
743 	test_preamble_state_qos_configured(conn, ase_id, stream);
744 
745 	test_ase_control_client_release(conn, ase_id);
746 
747 	/* Verification */
748 	expect_bt_bap_unicast_server_cb_release_called_once(stream);
749 	expect_bt_bap_stream_ops_released_called_once(stream);
750 }
751 
ZTEST_F(test_source_ase_state_transition,test_client_enabling_to_streaming)752 ZTEST_F(test_source_ase_state_transition, test_client_enabling_to_streaming)
753 {
754 	struct bt_bap_stream *stream = &fixture->stream;
755 	struct bt_conn *conn = &fixture->conn;
756 	uint8_t ase_id = fixture->ase.id;
757 	struct bt_iso_chan *chan;
758 	int err;
759 
760 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
761 
762 	test_preamble_state_enabling(conn, ase_id, stream);
763 
764 	err = mock_bt_iso_accept(conn, 0x01, 0x01, &chan);
765 	zassert_equal(0, err, "Failed to connect iso: err %d", err);
766 
767 	test_ase_control_client_receiver_start_ready(conn, ase_id);
768 
769 	/* Verification */
770 	expect_bt_bap_unicast_server_cb_start_called_once(stream);
771 	expect_bt_bap_stream_ops_connected_called_once(stream);
772 	expect_bt_bap_stream_ops_started_called_once(stream);
773 	expect_bt_bap_stream_ops_disabled_not_called();
774 }
775 
ZTEST_F(test_source_ase_state_transition,test_client_codec_configured_to_codec_configured)776 ZTEST_F(test_source_ase_state_transition, test_client_codec_configured_to_codec_configured)
777 {
778 	struct bt_bap_stream *stream = &fixture->stream;
779 	struct bt_conn *conn = &fixture->conn;
780 	uint8_t ase_id = fixture->ase.id;
781 
782 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
783 
784 	test_preamble_state_codec_configured(conn, ase_id, stream);
785 
786 	test_ase_control_client_config_codec(conn, ase_id, stream);
787 
788 	/* Verification */
789 	expect_bt_bap_unicast_server_cb_reconfig_called_once(stream, BT_AUDIO_DIR_SOURCE, EMPTY);
790 	expect_bt_bap_stream_ops_configured_called_once(stream, EMPTY);
791 }
792 
ZTEST_F(test_source_ase_state_transition,test_client_qos_configured_to_qos_configured)793 ZTEST_F(test_source_ase_state_transition, test_client_qos_configured_to_qos_configured)
794 {
795 	struct bt_bap_stream *stream = &fixture->stream;
796 	struct bt_conn *conn = &fixture->conn;
797 	uint8_t ase_id = fixture->ase.id;
798 
799 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
800 
801 	test_preamble_state_qos_configured(conn, ase_id, stream);
802 
803 	test_ase_control_client_config_qos(conn, ase_id);
804 
805 	/* Verification */
806 	expect_bt_bap_unicast_server_cb_qos_called_once(stream, EMPTY);
807 	expect_bt_bap_stream_ops_qos_set_called_once(stream);
808 	expect_bt_bap_stream_ops_disabled_not_called();
809 }
810 
ZTEST_F(test_source_ase_state_transition,test_client_qos_configured_to_codec_configured)811 ZTEST_F(test_source_ase_state_transition, test_client_qos_configured_to_codec_configured)
812 {
813 	struct bt_bap_stream *stream = &fixture->stream;
814 	struct bt_conn *conn = &fixture->conn;
815 	uint8_t ase_id = fixture->ase.id;
816 
817 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
818 
819 	test_preamble_state_qos_configured(conn, ase_id, stream);
820 
821 	test_ase_control_client_config_codec(conn, ase_id, stream);
822 
823 	/* Verification */
824 	expect_bt_bap_unicast_server_cb_reconfig_called_once(stream, BT_AUDIO_DIR_SOURCE, EMPTY);
825 	expect_bt_bap_stream_ops_configured_called_once(stream, EMPTY);
826 }
827 
ZTEST_F(test_source_ase_state_transition,test_client_codec_configured_to_releasing)828 ZTEST_F(test_source_ase_state_transition, test_client_codec_configured_to_releasing)
829 {
830 	struct bt_bap_stream *stream = &fixture->stream;
831 	struct bt_conn *conn = &fixture->conn;
832 	uint8_t ase_id = fixture->ase.id;
833 
834 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
835 
836 	test_preamble_state_codec_configured(conn, ase_id, stream);
837 
838 	test_ase_control_client_release(conn, ase_id);
839 
840 	/* Verification */
841 	expect_bt_bap_unicast_server_cb_release_called_once(stream);
842 	expect_bt_bap_stream_ops_released_called_once(stream);
843 }
844 
ZTEST_F(test_source_ase_state_transition,test_client_enabling_to_releasing)845 ZTEST_F(test_source_ase_state_transition, test_client_enabling_to_releasing)
846 {
847 	struct bt_bap_stream *stream = &fixture->stream;
848 	struct bt_conn *conn = &fixture->conn;
849 	uint8_t ase_id = fixture->ase.id;
850 
851 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
852 
853 	test_preamble_state_enabling(conn, ase_id, stream);
854 
855 	test_ase_control_client_release(conn, ase_id);
856 
857 	/* Verification */
858 	expect_bt_bap_unicast_server_cb_release_called_once(stream);
859 	expect_bt_bap_stream_ops_released_called_once(stream);
860 	expect_bt_bap_stream_ops_disabled_not_called();
861 }
862 
ZTEST_F(test_source_ase_state_transition,test_client_enabling_to_enabling)863 ZTEST_F(test_source_ase_state_transition, test_client_enabling_to_enabling)
864 {
865 	struct bt_bap_stream *stream = &fixture->stream;
866 	struct bt_conn *conn = &fixture->conn;
867 	uint8_t ase_id = fixture->ase.id;
868 
869 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
870 
871 	test_preamble_state_enabling(conn, ase_id, stream);
872 
873 	test_ase_control_client_update_metadata(conn, ase_id);
874 
875 	/* Verification */
876 	expect_bt_bap_unicast_server_cb_metadata_called_once(stream, EMPTY, EMPTY);
877 	expect_bt_bap_stream_ops_metadata_updated_called_once(stream);
878 	expect_bt_bap_stream_ops_disabled_not_called();
879 }
880 
ZTEST_F(test_source_ase_state_transition,test_client_streaming_to_releasing)881 ZTEST_F(test_source_ase_state_transition, test_client_streaming_to_releasing)
882 {
883 	struct bt_bap_stream *stream = &fixture->stream;
884 	struct bt_conn *conn = &fixture->conn;
885 	uint8_t ase_id = fixture->ase.id;
886 	struct bt_iso_chan *chan;
887 
888 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
889 
890 	test_preamble_state_streaming(conn, ase_id, stream, &chan, true);
891 
892 	test_ase_control_client_release(conn, ase_id);
893 
894 	/* Client disconnects the ISO */
895 	mock_bt_iso_disconnected(chan, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
896 
897 	/* Verification */
898 	expect_bt_bap_unicast_server_cb_release_called_once(stream);
899 	expect_bt_bap_stream_ops_stopped_called_once(stream, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
900 	expect_bt_bap_stream_ops_released_called_once(stream);
901 	expect_bt_bap_stream_ops_disabled_not_called();
902 	expect_bt_bap_stream_ops_disconnected_called_once(stream);
903 }
904 
ZTEST_F(test_source_ase_state_transition,test_client_streaming_to_streaming)905 ZTEST_F(test_source_ase_state_transition, test_client_streaming_to_streaming)
906 {
907 	struct bt_bap_stream *stream = &fixture->stream;
908 	struct bt_conn *conn = &fixture->conn;
909 	uint8_t ase_id = fixture->ase.id;
910 	struct bt_iso_chan *chan;
911 
912 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
913 
914 	test_preamble_state_streaming(conn, ase_id, stream, &chan, true);
915 
916 	test_ase_control_client_update_metadata(conn, ase_id);
917 
918 	/* Verification */
919 	expect_bt_bap_unicast_server_cb_metadata_called_once(stream, EMPTY, EMPTY);
920 	expect_bt_bap_stream_ops_metadata_updated_called_once(stream);
921 	expect_bt_bap_stream_ops_disabled_not_called();
922 }
923 
ZTEST_F(test_source_ase_state_transition,test_client_streaming_to_disabling)924 ZTEST_F(test_source_ase_state_transition, test_client_streaming_to_disabling)
925 {
926 	struct bt_bap_stream *stream = &fixture->stream;
927 	struct bt_conn *conn = &fixture->conn;
928 	uint8_t ase_id = fixture->ase.id;
929 	struct bt_iso_chan *chan;
930 
931 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
932 
933 	test_preamble_state_streaming(conn, ase_id, stream, &chan, true);
934 
935 	test_ase_control_client_disable(conn, ase_id);
936 
937 	/* Verification */
938 	expect_bt_bap_unicast_server_cb_disable_called_once(stream);
939 	expect_bt_bap_stream_ops_stopped_called_once(stream, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
940 	expect_bt_bap_stream_ops_disabled_called_once(stream);
941 }
942 
ZTEST_F(test_source_ase_state_transition,test_client_enabling_to_disabling_to_qos_configured)943 ZTEST_F(test_source_ase_state_transition, test_client_enabling_to_disabling_to_qos_configured)
944 {
945 	struct bt_bap_stream *stream = &fixture->stream;
946 	struct bt_conn *conn = &fixture->conn;
947 	uint8_t ase_id = fixture->ase.id;
948 
949 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
950 
951 	test_preamble_state_enabling(conn, ase_id, stream);
952 	test_ase_control_client_disable(conn, ase_id);
953 	expect_bt_bap_stream_ops_disabled_called_once(stream);
954 
955 	test_mocks_reset();
956 
957 	test_ase_control_client_receiver_stop_ready(conn, ase_id);
958 
959 	/* Verification */
960 	expect_bt_bap_unicast_server_cb_stop_called_once(stream);
961 	expect_bt_bap_stream_ops_qos_set_called_once(stream);
962 	expect_bt_bap_stream_ops_disabled_not_called();
963 }
964 
ZTEST_F(test_source_ase_state_transition,test_client_streaming_to_disabling_to_qos_configured)965 ZTEST_F(test_source_ase_state_transition, test_client_streaming_to_disabling_to_qos_configured)
966 {
967 	struct bt_bap_stream *stream = &fixture->stream;
968 	struct bt_conn *conn = &fixture->conn;
969 	uint8_t ase_id = fixture->ase.id;
970 	struct bt_iso_chan *chan;
971 
972 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
973 
974 	test_preamble_state_streaming(conn, ase_id, stream, &chan, true);
975 	test_ase_control_client_disable(conn, ase_id);
976 
977 	/* Verify that stopped callback was called by disable */
978 	expect_bt_bap_stream_ops_disabled_called_once(stream);
979 	expect_bt_bap_stream_ops_stopped_called_once(stream, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
980 
981 
982 	test_mocks_reset();
983 
984 	test_ase_control_client_receiver_stop_ready(conn, ase_id);
985 
986 	/* Verification */
987 	expect_bt_bap_unicast_server_cb_stop_called_once(stream);
988 	expect_bt_bap_stream_ops_qos_set_called_once(stream);
989 	expect_bt_bap_stream_ops_disabled_not_called();
990 }
991 
ZTEST_F(test_source_ase_state_transition,test_server_idle_to_codec_configured)992 ZTEST_F(test_source_ase_state_transition, test_server_idle_to_codec_configured)
993 {
994 	struct bt_audio_codec_cfg codec_cfg = BT_AUDIO_CODEC_LC3_CONFIG(
995 		BT_AUDIO_CODEC_CFG_FREQ_16KHZ, BT_AUDIO_CODEC_CFG_DURATION_10,
996 		BT_AUDIO_LOCATION_FRONT_LEFT, 40U, 1, BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
997 	struct bt_bap_stream *stream = &fixture->stream;
998 	struct bt_conn *conn = &fixture->conn;
999 	int err;
1000 
1001 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
1002 
1003 	err = bt_bap_unicast_server_config_ase(conn, stream, &codec_cfg, &qos_pref);
1004 	zassert_false(err < 0, "bt_bap_unicast_server_config_ase returned err %d", err);
1005 
1006 	/* Verification */
1007 	expect_bt_bap_unicast_server_cb_config_not_called();
1008 	expect_bt_bap_stream_ops_configured_called_once(stream, EMPTY);
1009 }
1010 
ZTEST_F(test_source_ase_state_transition,test_server_codec_configured_to_codec_configured)1011 ZTEST_F(test_source_ase_state_transition, test_server_codec_configured_to_codec_configured)
1012 {
1013 	struct bt_audio_codec_cfg codec_cfg = BT_AUDIO_CODEC_LC3_CONFIG(
1014 		BT_AUDIO_CODEC_CFG_FREQ_16KHZ, BT_AUDIO_CODEC_CFG_DURATION_10,
1015 		BT_AUDIO_LOCATION_FRONT_LEFT, 40U, 1, BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
1016 	struct bt_bap_stream *stream = &fixture->stream;
1017 	struct bt_conn *conn = &fixture->conn;
1018 	uint8_t ase_id = fixture->ase.id;
1019 	int err;
1020 
1021 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
1022 
1023 	test_preamble_state_codec_configured(conn, ase_id, stream);
1024 
1025 	err = bt_bap_stream_reconfig(stream, &codec_cfg);
1026 	zassert_false(err < 0, "bt_bap_stream_reconfig returned err %d", err);
1027 
1028 	/* Verification */
1029 	expect_bt_bap_unicast_server_cb_reconfig_called_once(stream, BT_AUDIO_DIR_SOURCE, EMPTY);
1030 	expect_bt_bap_stream_ops_configured_called_once(stream, EMPTY);
1031 }
1032 
ZTEST_F(test_source_ase_state_transition,test_server_codec_configured_to_releasing)1033 ZTEST_F(test_source_ase_state_transition, test_server_codec_configured_to_releasing)
1034 {
1035 	struct bt_bap_stream *stream = &fixture->stream;
1036 	struct bt_conn *conn = &fixture->conn;
1037 	uint8_t ase_id = fixture->ase.id;
1038 	int err;
1039 
1040 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
1041 
1042 	test_preamble_state_codec_configured(conn, ase_id, stream);
1043 
1044 	err = bt_bap_stream_release(stream);
1045 	zassert_false(err < 0, "bt_bap_stream_release returned err %d", err);
1046 
1047 	/* Verification */
1048 	expect_bt_bap_unicast_server_cb_release_called_once(stream);
1049 	expect_bt_bap_stream_ops_released_called_once(stream);
1050 }
1051 
ZTEST_F(test_source_ase_state_transition,test_server_qos_configured_to_codec_configured)1052 ZTEST_F(test_source_ase_state_transition, test_server_qos_configured_to_codec_configured)
1053 {
1054 	struct bt_audio_codec_cfg codec_cfg = BT_AUDIO_CODEC_LC3_CONFIG(
1055 		BT_AUDIO_CODEC_CFG_FREQ_16KHZ, BT_AUDIO_CODEC_CFG_DURATION_10,
1056 		BT_AUDIO_LOCATION_FRONT_LEFT, 40U, 1, BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED);
1057 	struct bt_bap_stream *stream = &fixture->stream;
1058 	struct bt_conn *conn = &fixture->conn;
1059 	uint8_t ase_id = fixture->ase.id;
1060 	int err;
1061 
1062 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
1063 
1064 	test_preamble_state_qos_configured(conn, ase_id, stream);
1065 
1066 	err = bt_bap_stream_reconfig(stream, &codec_cfg);
1067 	zassert_false(err < 0, "bt_bap_stream_reconfig returned err %d", err);
1068 
1069 	/* Verification */
1070 	expect_bt_bap_unicast_server_cb_reconfig_called_once(stream, BT_AUDIO_DIR_SOURCE, EMPTY);
1071 	expect_bt_bap_stream_ops_configured_called_once(stream, EMPTY);
1072 }
1073 
ZTEST_F(test_source_ase_state_transition,test_server_qos_configured_to_releasing)1074 ZTEST_F(test_source_ase_state_transition, test_server_qos_configured_to_releasing)
1075 {
1076 	struct bt_bap_stream *stream = &fixture->stream;
1077 	struct bt_conn *conn = &fixture->conn;
1078 	uint8_t ase_id = fixture->ase.id;
1079 	int err;
1080 
1081 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
1082 
1083 	test_preamble_state_qos_configured(conn, ase_id, stream);
1084 
1085 	err = bt_bap_stream_release(stream);
1086 	zassert_false(err < 0, "bt_bap_stream_release returned err %d", err);
1087 
1088 	/* Verification */
1089 	expect_bt_bap_unicast_server_cb_release_called_once(stream);
1090 	expect_bt_bap_stream_ops_released_called_once(stream);
1091 }
1092 
ZTEST_F(test_source_ase_state_transition,test_server_enabling_to_releasing)1093 ZTEST_F(test_source_ase_state_transition, test_server_enabling_to_releasing)
1094 {
1095 	struct bt_bap_stream *stream = &fixture->stream;
1096 	struct bt_conn *conn = &fixture->conn;
1097 	uint8_t ase_id = fixture->ase.id;
1098 	int err;
1099 
1100 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
1101 
1102 	test_preamble_state_enabling(conn, ase_id, stream);
1103 
1104 	err = bt_bap_stream_release(stream);
1105 	zassert_false(err < 0, "bt_bap_stream_release returned err %d", err);
1106 
1107 	/* Verification */
1108 	expect_bt_bap_unicast_server_cb_release_called_once(stream);
1109 	expect_bt_bap_stream_ops_released_called_once(stream);
1110 	expect_bt_bap_stream_ops_disabled_not_called();
1111 }
1112 
ZTEST_F(test_source_ase_state_transition,test_server_enabling_to_enabling)1113 ZTEST_F(test_source_ase_state_transition, test_server_enabling_to_enabling)
1114 {
1115 	const uint8_t meta[] = {
1116 		BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_STREAM_CONTEXT,
1117 				    BT_BYTES_LIST_LE16(BT_AUDIO_CONTEXT_TYPE_MEDIA)),
1118 	};
1119 	struct bt_bap_stream *stream = &fixture->stream;
1120 	struct bt_conn *conn = &fixture->conn;
1121 	uint8_t ase_id = fixture->ase.id;
1122 	int err;
1123 
1124 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
1125 
1126 	test_preamble_state_enabling(conn, ase_id, stream);
1127 
1128 	err = bt_bap_stream_metadata(stream, meta, ARRAY_SIZE(meta));
1129 	zassert_false(err < 0, "bt_bap_stream_metadata returned err %d", err);
1130 
1131 	/* Verification */
1132 	expect_bt_bap_unicast_server_cb_metadata_called_once(stream, EMPTY, EMPTY);
1133 	expect_bt_bap_stream_ops_metadata_updated_called_once(stream);
1134 	expect_bt_bap_stream_ops_disabled_not_called();
1135 }
1136 
ZTEST_F(test_source_ase_state_transition,test_server_enabling_to_disabling)1137 ZTEST_F(test_source_ase_state_transition, test_server_enabling_to_disabling)
1138 {
1139 	struct bt_bap_stream *stream = &fixture->stream;
1140 	struct bt_conn *conn = &fixture->conn;
1141 	uint8_t ase_id = fixture->ase.id;
1142 	int err;
1143 
1144 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
1145 
1146 	test_preamble_state_enabling(conn, ase_id, stream);
1147 
1148 	err = bt_bap_stream_disable(stream);
1149 	zassert_false(err < 0, "bt_bap_stream_disable returned err %d", err);
1150 
1151 	/* Verification */
1152 	expect_bt_bap_unicast_server_cb_disable_called_once(stream);
1153 	expect_bt_bap_stream_ops_disabled_called_once(stream);
1154 }
1155 
ZTEST_F(test_source_ase_state_transition,test_server_streaming_to_streaming)1156 ZTEST_F(test_source_ase_state_transition, test_server_streaming_to_streaming)
1157 {
1158 	const uint8_t meta[] = {
1159 		BT_AUDIO_CODEC_DATA(BT_AUDIO_METADATA_TYPE_STREAM_CONTEXT,
1160 				    BT_BYTES_LIST_LE16(BT_AUDIO_CONTEXT_TYPE_MEDIA)),
1161 	};
1162 	struct bt_bap_stream *stream = &fixture->stream;
1163 	struct bt_conn *conn = &fixture->conn;
1164 	uint8_t ase_id = fixture->ase.id;
1165 	struct bt_iso_chan *chan;
1166 	int err;
1167 
1168 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
1169 
1170 	test_preamble_state_streaming(conn, ase_id, stream, &chan, true);
1171 
1172 	err = bt_bap_stream_metadata(stream, meta, ARRAY_SIZE(meta));
1173 	zassert_false(err < 0, "bt_bap_stream_metadata returned err %d", err);
1174 
1175 	/* Verification */
1176 	expect_bt_bap_unicast_server_cb_metadata_called_once(stream, EMPTY, EMPTY);
1177 	expect_bt_bap_stream_ops_metadata_updated_called_once(stream);
1178 	expect_bt_bap_stream_ops_disabled_not_called();
1179 }
1180 
ZTEST_F(test_source_ase_state_transition,test_server_streaming_to_disabling)1181 ZTEST_F(test_source_ase_state_transition, test_server_streaming_to_disabling)
1182 {
1183 	struct bt_bap_stream *stream = &fixture->stream;
1184 	struct bt_conn *conn = &fixture->conn;
1185 	uint8_t ase_id = fixture->ase.id;
1186 	struct bt_iso_chan *chan;
1187 	int err;
1188 
1189 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
1190 
1191 	test_preamble_state_streaming(conn, ase_id, stream, &chan, true);
1192 
1193 	err = bt_bap_stream_disable(stream);
1194 	zassert_false(err < 0, "bt_bap_stream_disable returned err %d", err);
1195 
1196 	/* Verification */
1197 	expect_bt_bap_unicast_server_cb_disable_called_once(stream);
1198 	expect_bt_bap_stream_ops_stopped_called_once(stream, BT_HCI_ERR_LOCALHOST_TERM_CONN);
1199 	expect_bt_bap_stream_ops_disabled_called_once(stream);
1200 }
1201 
ZTEST_F(test_source_ase_state_transition,test_server_streaming_to_releasing)1202 ZTEST_F(test_source_ase_state_transition, test_server_streaming_to_releasing)
1203 {
1204 	struct bt_bap_stream *stream = &fixture->stream;
1205 	struct bt_conn *conn = &fixture->conn;
1206 	uint8_t ase_id = fixture->ase.id;
1207 	struct bt_iso_chan *chan;
1208 	int err;
1209 
1210 	Z_TEST_SKIP_IFNDEF(CONFIG_BT_ASCS_ASE_SRC);
1211 
1212 	test_preamble_state_streaming(conn, ase_id, stream, &chan, true);
1213 
1214 	err = bt_bap_stream_release(stream);
1215 	zassert_false(err < 0, "bt_bap_stream_release returned err %d", err);
1216 
1217 	/* Client disconnects the ISO */
1218 	mock_bt_iso_disconnected(chan, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
1219 
1220 	/* Verification */
1221 	expect_bt_bap_unicast_server_cb_release_called_once(stream);
1222 	expect_bt_bap_stream_ops_stopped_called_once(stream, BT_HCI_ERR_LOCALHOST_TERM_CONN);
1223 	expect_bt_bap_stream_ops_released_called_once(stream);
1224 	expect_bt_bap_stream_ops_disabled_not_called();
1225 	expect_bt_bap_stream_ops_disconnected_called_once(stream);
1226 }
1227