1 /*
2  * Copyright (c) 2021-2025 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <errno.h>
8 #include <stdbool.h>
9 #include <stddef.h>
10 #include <stdint.h>
11 #include <string.h>
12 
13 #include <zephyr/autoconf.h>
14 #include <zephyr/autoconf.h>
15 #include <zephyr/bluetooth/addr.h>
16 #include <zephyr/bluetooth/audio/audio.h>
17 #include <zephyr/bluetooth/audio/bap.h>
18 #include <zephyr/bluetooth/audio/bap_lc3_preset.h>
19 #include <zephyr/bluetooth/audio/lc3.h>
20 #include <zephyr/bluetooth/audio/pacs.h>
21 #include <zephyr/bluetooth/bluetooth.h>
22 #include <zephyr/bluetooth/gap.h>
23 #include <zephyr/bluetooth/hci_types.h>
24 #include <zephyr/bluetooth/iso.h>
25 #include <zephyr/bluetooth/uuid.h>
26 #include <zephyr/kernel.h>
27 #include <zephyr/net_buf.h>
28 #include <zephyr/sys/byteorder.h>
29 #include <zephyr/sys/printk.h>
30 #include <zephyr/sys/util.h>
31 #include <zephyr/sys/util_macro.h>
32 #include <zephyr/toolchain.h>
33 
34 #include "bap_common.h"
35 #include "bap_stream_rx.h"
36 #include "bstests.h"
37 #include "common.h"
38 
39 #if defined(CONFIG_BT_BAP_BROADCAST_SINK)
40 extern enum bst_result_t bst_result;
41 
42 CREATE_FLAG(flag_broadcaster_found);
43 CREATE_FLAG(flag_base_received);
44 CREATE_FLAG(flag_base_metadata_updated);
45 CREATE_FLAG(flag_pa_synced);
46 CREATE_FLAG(flag_syncable);
47 CREATE_FLAG(flag_pa_sync_lost);
48 CREATE_FLAG(flag_pa_request);
49 CREATE_FLAG(flag_bis_sync_requested);
50 CREATE_FLAG(flag_big_sync_mic_failure);
51 CREATE_FLAG(flag_sink_started);
52 
53 static struct bt_bap_broadcast_sink *g_sink;
54 static size_t stream_sync_cnt;
55 static struct bt_le_scan_recv_info broadcaster_info;
56 static bt_addr_le_t broadcaster_addr;
57 static struct bt_le_per_adv_sync *pa_sync;
58 static uint32_t broadcaster_broadcast_id;
59 static struct audio_test_stream broadcast_sink_streams[CONFIG_BT_BAP_BROADCAST_SNK_STREAM_COUNT];
60 static struct bt_bap_stream *streams[ARRAY_SIZE(broadcast_sink_streams)];
61 static uint32_t requested_bis_sync;
62 static const struct bt_bap_scan_delegator_recv_state *req_recv_state;
63 static uint8_t recv_state_broadcast_code[BT_ISO_BROADCAST_CODE_SIZE];
64 
65 #define SUPPORTED_CHAN_COUNTS          BT_AUDIO_CODEC_CAP_CHAN_COUNT_SUPPORT(1, 2)
66 #define SUPPORTED_MIN_OCTETS_PER_FRAME 30
67 #define SUPPORTED_MAX_OCTETS_PER_FRAME 155
68 #define SUPPORTED_MAX_FRAMES_PER_SDU   1
69 
70 /* We support 1 or 2 channels, so the maximum SDU size we support will be 2 times the maximum frame
71  * size per frame we support
72  */
73 #define SUPPORTED_MAX_SDU_SIZE (2 * SUPPORTED_MAX_FRAMES_PER_SDU * SUPPORTED_MAX_OCTETS_PER_FRAME)
74 
75 BUILD_ASSERT(CONFIG_BT_ISO_RX_MTU >= SUPPORTED_MAX_SDU_SIZE);
76 
77 #define SUPPORTED_CONTEXTS (BT_AUDIO_CONTEXT_TYPE_CONVERSATIONAL | BT_AUDIO_CONTEXT_TYPE_MEDIA)
78 
79 static const struct bt_audio_codec_cap codec_cap = BT_AUDIO_CODEC_CAP_LC3(
80 	BT_AUDIO_CODEC_CAP_FREQ_ANY, BT_AUDIO_CODEC_CAP_DURATION_ANY, SUPPORTED_CHAN_COUNTS,
81 	SUPPORTED_MIN_OCTETS_PER_FRAME, SUPPORTED_MAX_OCTETS_PER_FRAME,
82 	SUPPORTED_MAX_FRAMES_PER_SDU, SUPPORTED_CONTEXTS);
83 
84 static K_SEM_DEFINE(sem_stream_started, 0U, ARRAY_SIZE(streams));
85 static K_SEM_DEFINE(sem_stream_stopped, 0U, ARRAY_SIZE(streams));
86 
87 /* Create a mask for the maximum BIS we can sync to using the number of streams
88  * we have. We add an additional 1 since the bis indexes start from 1 and not
89  * 0.
90  */
91 static const uint32_t bis_index_mask = BIT_MASK(ARRAY_SIZE(streams) + 1U);
92 static uint32_t bis_index_bitfield;
93 
valid_base_subgroup(const struct bt_bap_base_subgroup * subgroup)94 static bool valid_base_subgroup(const struct bt_bap_base_subgroup *subgroup)
95 {
96 	struct bt_audio_codec_cfg codec_cfg = {0};
97 	enum bt_audio_location chan_allocation;
98 	uint8_t frames_blocks_per_sdu;
99 	size_t min_sdu_size_required;
100 	uint16_t octets_per_frame;
101 	uint8_t chan_cnt;
102 	int ret;
103 
104 	ret = bt_bap_base_subgroup_codec_to_codec_cfg(subgroup, &codec_cfg);
105 	if (ret < 0) {
106 		printk("Could not get subgroup codec_cfg: %d\n", ret);
107 
108 		return false;
109 	}
110 
111 	if (codec_cfg.id == BT_HCI_CODING_FORMAT_VS) {
112 		return memcmp(&codec_cfg, &vs_codec_cfg, sizeof(codec_cfg)) == 0;
113 	}
114 
115 	ret = bt_audio_codec_cfg_get_freq(&codec_cfg);
116 	if (ret >= 0) {
117 		const int freq = bt_audio_codec_cfg_freq_to_freq_hz(ret);
118 
119 		if (freq < 0) {
120 			printk("Invalid subgroup frequency value: %d (%d)\n", ret, freq);
121 
122 			return false;
123 		}
124 	} else {
125 		printk("Could not get subgroup frequency: %d\n", ret);
126 
127 		return false;
128 	}
129 
130 	ret = bt_audio_codec_cfg_get_frame_dur(&codec_cfg);
131 	if (ret >= 0) {
132 		const int frame_duration_us = bt_audio_codec_cfg_frame_dur_to_frame_dur_us(ret);
133 
134 		if (frame_duration_us < 0) {
135 			printk("Invalid subgroup frame duration value: %d (%d)\n", ret,
136 			       frame_duration_us);
137 
138 			return false;
139 		}
140 	} else {
141 		printk("Could not get subgroup frame duration: %d\n", ret);
142 
143 		return false;
144 	}
145 
146 	ret = bt_audio_codec_cfg_get_chan_allocation(&codec_cfg, &chan_allocation, true);
147 	if (ret == 0) {
148 		chan_cnt = bt_audio_get_chan_count(chan_allocation);
149 	} else {
150 		FAIL("Could not get subgroup channel allocation: %d\n", ret);
151 
152 		return false;
153 	}
154 
155 	if (chan_cnt == 0 || (BIT(chan_cnt - 1) & SUPPORTED_CHAN_COUNTS) == 0) {
156 		printk("Unsupported channel count: %u\n", chan_cnt);
157 
158 		return false;
159 	}
160 
161 	ret = bt_audio_codec_cfg_get_octets_per_frame(&codec_cfg);
162 	if (ret > 0) {
163 		octets_per_frame = (uint16_t)ret;
164 	} else {
165 		printk("Could not get subgroup octets per frame: %d\n", ret);
166 
167 		return false;
168 	}
169 
170 	if (!IN_RANGE(octets_per_frame, SUPPORTED_MIN_OCTETS_PER_FRAME,
171 		      SUPPORTED_MAX_OCTETS_PER_FRAME)) {
172 		printk("Unsupported octets per frame: %u\n", octets_per_frame);
173 
174 		return false;
175 	}
176 
177 	ret = bt_audio_codec_cfg_get_frame_blocks_per_sdu(&codec_cfg, true);
178 	if (ret > 0) {
179 		frames_blocks_per_sdu = (uint8_t)ret;
180 	} else {
181 		FAIL("Could not get frame blocks per SDU: %d\n", ret);
182 
183 		return false;
184 	}
185 
186 	/* An SDU can consist of X frame blocks, each with Y frames (one per channel) of size Z in
187 	 * them. The minimum SDU size required for this is X * Y * Z.
188 	 */
189 	min_sdu_size_required = chan_cnt * octets_per_frame * frames_blocks_per_sdu;
190 	if (min_sdu_size_required > SUPPORTED_MAX_SDU_SIZE) {
191 		printk("With %zu channels and %u octets per frame and %u frames per block, SDUs "
192 		       "shall be at minimum %zu, we only support %d\n",
193 		       chan_cnt, octets_per_frame, frames_blocks_per_sdu, min_sdu_size_required,
194 		       SUPPORTED_MAX_SDU_SIZE);
195 
196 		return false;
197 	}
198 
199 	return true;
200 }
201 
base_subgroup_cb(const struct bt_bap_base_subgroup * subgroup,void * user_data)202 static bool base_subgroup_cb(const struct bt_bap_base_subgroup *subgroup, void *user_data)
203 {
204 	static uint8_t metadata[CONFIG_BT_AUDIO_CODEC_CFG_MAX_METADATA_SIZE];
205 	static size_t metadata_size;
206 	uint8_t *meta;
207 	int ret;
208 
209 	ret = bt_bap_base_get_subgroup_codec_meta(subgroup, &meta);
210 	if (ret < 0) {
211 		FAIL("Could not get subgroup meta: %d\n", ret);
212 		return false;
213 	}
214 
215 	if (TEST_FLAG(flag_base_received) &&
216 	    ((size_t)ret != metadata_size || memcmp(meta, metadata, metadata_size) != 0)) {
217 		printk("Metadata updated\n");
218 		SET_FLAG(flag_base_metadata_updated);
219 	}
220 
221 	metadata_size = (size_t)ret;
222 	(void)memcpy(metadata, meta, metadata_size);
223 
224 	if (!valid_base_subgroup(subgroup)) {
225 		printk("Invalid or unsupported subgroup\n");
226 		return false;
227 	}
228 
229 	return true;
230 }
231 
base_recv_cb(struct bt_bap_broadcast_sink * sink,const struct bt_bap_base * base,size_t base_size)232 static void base_recv_cb(struct bt_bap_broadcast_sink *sink, const struct bt_bap_base *base,
233 			 size_t base_size)
234 {
235 	uint32_t base_bis_index_bitfield = 0U;
236 	int ret;
237 
238 	printk("Received BASE with %d subgroups from broadcast sink %p\n",
239 	       bt_bap_base_get_subgroup_count(base), sink);
240 
241 	ret = bt_bap_base_foreach_subgroup(base, base_subgroup_cb, NULL);
242 	if (ret != 0) {
243 		FAIL("Failed to parse subgroups: %d\n", ret);
244 		return;
245 	}
246 
247 	ret = bt_bap_base_get_bis_indexes(base, &base_bis_index_bitfield);
248 	if (ret != 0) {
249 		FAIL("Failed to BIS indexes: %d\n", ret);
250 		return;
251 	}
252 
253 	if (requested_bis_sync == 0) {
254 		bis_index_bitfield = base_bis_index_bitfield & bis_index_mask;
255 	} else {
256 		if ((requested_bis_sync & base_bis_index_bitfield) != requested_bis_sync) {
257 			FAIL("Assistant has request BIS indexes 0x%08x that is not in the BASE "
258 			     "0x%08x\n",
259 			     requested_bis_sync, base_bis_index_bitfield);
260 			return;
261 		}
262 
263 		bis_index_bitfield = requested_bis_sync & bis_index_mask;
264 	}
265 
266 	SET_FLAG(flag_base_received);
267 }
268 
syncable_cb(struct bt_bap_broadcast_sink * sink,const struct bt_iso_biginfo * biginfo)269 static void syncable_cb(struct bt_bap_broadcast_sink *sink, const struct bt_iso_biginfo *biginfo)
270 {
271 	printk("Broadcast sink %p syncable with%s encryption\n",
272 	       sink, biginfo->encryption ? "" : "out");
273 	SET_FLAG(flag_syncable);
274 }
275 
broadcast_sink_started_cb(struct bt_bap_broadcast_sink * sink)276 static void broadcast_sink_started_cb(struct bt_bap_broadcast_sink *sink)
277 {
278 	printk("Broadcast sink %p started\n", sink);
279 	SET_FLAG(flag_sink_started);
280 }
281 
broadcast_sink_stopped_cb(struct bt_bap_broadcast_sink * sink,uint8_t reason)282 static void broadcast_sink_stopped_cb(struct bt_bap_broadcast_sink *sink, uint8_t reason)
283 {
284 	printk("Broadcast sink %p stopped with reason 0x%02X\n", sink, reason);
285 	UNSET_FLAG(flag_sink_started);
286 
287 	if (reason == BT_HCI_ERR_TERM_DUE_TO_MIC_FAIL) {
288 		SET_FLAG(flag_big_sync_mic_failure);
289 	}
290 }
291 
292 static struct bt_bap_broadcast_sink_cb broadcast_sink_cbs = {
293 	.base_recv = base_recv_cb,
294 	.syncable = syncable_cb,
295 	.started = broadcast_sink_started_cb,
296 	.stopped = broadcast_sink_stopped_cb,
297 };
298 
scan_check_and_sync_broadcast(struct bt_data * data,void * user_data)299 static bool scan_check_and_sync_broadcast(struct bt_data *data, void *user_data)
300 {
301 	const struct bt_le_scan_recv_info *info = user_data;
302 	char le_addr[BT_ADDR_LE_STR_LEN];
303 	struct bt_uuid_16 adv_uuid;
304 	uint32_t broadcast_id;
305 
306 	if (TEST_FLAG(flag_broadcaster_found)) {
307 		/* no-op*/
308 		return false;
309 	}
310 
311 	if (data->type != BT_DATA_SVC_DATA16) {
312 		return true;
313 	}
314 
315 	if (data->data_len < BT_UUID_SIZE_16 + BT_AUDIO_BROADCAST_ID_SIZE) {
316 		return true;
317 	}
318 
319 	if (!bt_uuid_create(&adv_uuid.uuid, data->data, BT_UUID_SIZE_16)) {
320 		return true;
321 	}
322 
323 	if (bt_uuid_cmp(&adv_uuid.uuid, BT_UUID_BROADCAST_AUDIO)) {
324 		return true;
325 	}
326 
327 	broadcast_id = sys_get_le24(data->data + BT_UUID_SIZE_16);
328 
329 	bt_addr_le_to_str(info->addr, le_addr, sizeof(le_addr));
330 
331 	printk("Found broadcaster with ID 0x%06X and addr %s and sid 0x%02X\n", broadcast_id,
332 	       le_addr, info->sid);
333 
334 	SET_FLAG(flag_broadcaster_found);
335 
336 	/* Store info for PA sync parameters */
337 	memcpy(&broadcaster_info, info, sizeof(broadcaster_info));
338 	bt_addr_le_copy(&broadcaster_addr, info->addr);
339 	broadcaster_broadcast_id = broadcast_id;
340 
341 	/* Stop parsing */
342 	return false;
343 }
344 
broadcast_scan_recv(const struct bt_le_scan_recv_info * info,struct net_buf_simple * ad)345 static void broadcast_scan_recv(const struct bt_le_scan_recv_info *info, struct net_buf_simple *ad)
346 {
347 	if (info->interval != 0U) {
348 		bt_data_parse(ad, scan_check_and_sync_broadcast, (void *)info);
349 	}
350 }
351 
352 static struct bt_le_scan_cb bap_scan_cb = {
353 	.recv = broadcast_scan_recv,
354 };
355 
bap_pa_sync_synced_cb(struct bt_le_per_adv_sync * sync,struct bt_le_per_adv_sync_synced_info * info)356 static void bap_pa_sync_synced_cb(struct bt_le_per_adv_sync *sync,
357 				  struct bt_le_per_adv_sync_synced_info *info)
358 {
359 	if (sync == pa_sync) {
360 		printk("PA sync %p synced for broadcast sink with broadcast ID 0x%06X\n", sync,
361 		       broadcaster_broadcast_id);
362 
363 		SET_FLAG(flag_pa_synced);
364 	}
365 }
366 
bap_pa_sync_terminated_cb(struct bt_le_per_adv_sync * sync,const struct bt_le_per_adv_sync_term_info * info)367 static void bap_pa_sync_terminated_cb(struct bt_le_per_adv_sync *sync,
368 				      const struct bt_le_per_adv_sync_term_info *info)
369 {
370 	if (sync == pa_sync) {
371 		printk("PA sync %p lost with reason %u\n", sync, info->reason);
372 		pa_sync = NULL;
373 
374 		SET_FLAG(flag_pa_sync_lost);
375 	}
376 }
377 
378 static struct bt_le_per_adv_sync_cb bap_pa_sync_cb = {
379 	.synced = bap_pa_sync_synced_cb,
380 	.term = bap_pa_sync_terminated_cb,
381 };
382 
pa_sync_req_cb(struct bt_conn * conn,const struct bt_bap_scan_delegator_recv_state * recv_state,bool past_avail,uint16_t pa_interval)383 static int pa_sync_req_cb(struct bt_conn *conn,
384 			  const struct bt_bap_scan_delegator_recv_state *recv_state,
385 			  bool past_avail, uint16_t pa_interval)
386 {
387 	if (recv_state->pa_sync_state == BT_BAP_PA_STATE_SYNCED ||
388 	    recv_state->pa_sync_state == BT_BAP_PA_STATE_INFO_REQ) {
389 		/* Already syncing */
390 		/* TODO: Terminate existing sync and then sync to new?*/
391 		return -EALREADY;
392 	}
393 
394 	req_recv_state = recv_state;
395 
396 	SET_FLAG(flag_pa_request);
397 
398 	return 0;
399 }
400 
pa_sync_term_req_cb(struct bt_conn * conn,const struct bt_bap_scan_delegator_recv_state * recv_state)401 static int pa_sync_term_req_cb(struct bt_conn *conn,
402 			       const struct bt_bap_scan_delegator_recv_state *recv_state)
403 {
404 	if (pa_sync == NULL || recv_state->pa_sync_state == BT_BAP_PA_STATE_NOT_SYNCED) {
405 		return -EALREADY;
406 	}
407 
408 	req_recv_state = recv_state;
409 
410 	UNSET_FLAG(flag_pa_request);
411 
412 	return 0;
413 }
414 
bis_sync_req_cb(struct bt_conn * conn,const struct bt_bap_scan_delegator_recv_state * recv_state,const uint32_t bis_sync_req[CONFIG_BT_BAP_BASS_MAX_SUBGROUPS])415 static int bis_sync_req_cb(struct bt_conn *conn,
416 			   const struct bt_bap_scan_delegator_recv_state *recv_state,
417 			   const uint32_t bis_sync_req[CONFIG_BT_BAP_BASS_MAX_SUBGROUPS])
418 {
419 	req_recv_state = recv_state;
420 
421 	printk("BIS sync request received for %p: 0x%08x\n", recv_state, bis_sync_req[0]);
422 	/* We only care about a single subgroup in this test */
423 	requested_bis_sync = bis_sync_req[0];
424 	broadcaster_broadcast_id = recv_state->broadcast_id;
425 	if (bis_sync_req[0] != 0) {
426 		SET_FLAG(flag_bis_sync_requested);
427 	} else {
428 		UNSET_FLAG(flag_bis_sync_requested);
429 	}
430 
431 	return 0;
432 }
433 
broadcast_code_cb(struct bt_conn * conn,const struct bt_bap_scan_delegator_recv_state * recv_state,const uint8_t broadcast_code[BT_ISO_BROADCAST_CODE_SIZE])434 static void broadcast_code_cb(struct bt_conn *conn,
435 			      const struct bt_bap_scan_delegator_recv_state *recv_state,
436 			      const uint8_t broadcast_code[BT_ISO_BROADCAST_CODE_SIZE])
437 {
438 	req_recv_state = recv_state;
439 
440 	memcpy(recv_state_broadcast_code, broadcast_code, BT_ISO_BROADCAST_CODE_SIZE);
441 }
442 
scanning_state_cb(struct bt_conn * conn,bool is_scanning)443 static void scanning_state_cb(struct bt_conn *conn, bool is_scanning)
444 {
445 	printk("Assistant scanning %s\n", is_scanning ? "started" : "stopped");
446 
447 }
448 
449 static struct bt_bap_scan_delegator_cb scan_delegator_cbs = {
450 	.scanning_state = scanning_state_cb,
451 	.pa_sync_req = pa_sync_req_cb,
452 	.pa_sync_term_req = pa_sync_term_req_cb,
453 	.bis_sync_req = bis_sync_req_cb,
454 	.broadcast_code = broadcast_code_cb,
455 };
456 
validate_stream_codec_cfg(const struct bt_bap_stream * stream)457 static void validate_stream_codec_cfg(const struct bt_bap_stream *stream)
458 {
459 	struct bt_audio_codec_cfg *codec_cfg = stream->codec_cfg;
460 	enum bt_audio_location chan_allocation;
461 	uint8_t frames_blocks_per_sdu;
462 	size_t min_sdu_size_required;
463 	uint16_t octets_per_frame;
464 	uint8_t chan_cnt;
465 	int ret;
466 
467 	if (codec_cfg->id != BT_HCI_CODING_FORMAT_LC3) {
468 		/* We can only validate LC3 codecs */
469 		return;
470 	}
471 
472 	ret = bt_audio_codec_cfg_get_freq(codec_cfg);
473 	if (ret >= 0) {
474 		const int freq = bt_audio_codec_cfg_freq_to_freq_hz(ret);
475 
476 		if (freq < 0) {
477 			FAIL("Invalid frequency value: %d (%d)\n", ret, freq);
478 
479 			return;
480 		}
481 	} else {
482 		FAIL("Could not get frequency: %d\n", ret);
483 
484 		return;
485 	}
486 
487 	ret = bt_audio_codec_cfg_get_frame_dur(codec_cfg);
488 	if (ret >= 0) {
489 		const int frame_duration_us = bt_audio_codec_cfg_frame_dur_to_frame_dur_us(ret);
490 
491 		if (frame_duration_us < 0) {
492 			FAIL("Invalid frame duration value: %d (%d)\n", ret, frame_duration_us);
493 
494 			return;
495 		}
496 	} else {
497 		FAIL("Could not get frame duration: %d\n", ret);
498 
499 		return;
500 	}
501 
502 	/* The broadcast source sets the channel allocation in the BIS to
503 	 * BT_AUDIO_LOCATION_FRONT_LEFT
504 	 */
505 	ret = bt_audio_codec_cfg_get_chan_allocation(codec_cfg, &chan_allocation, true);
506 	if (ret == 0) {
507 		if (chan_allocation != BT_AUDIO_LOCATION_FRONT_CENTER) {
508 			FAIL("Unexpected channel allocation: 0x%08X", chan_allocation);
509 
510 			return;
511 		}
512 
513 		chan_cnt = bt_audio_get_chan_count(chan_allocation);
514 	} else {
515 		FAIL("Could not get subgroup channel allocation: %d\n", ret);
516 
517 		return;
518 	}
519 
520 	if (chan_cnt == 0 || (BIT(chan_cnt - 1) & SUPPORTED_CHAN_COUNTS) == 0) {
521 		FAIL("Unsupported channel count: %u\n", chan_cnt);
522 
523 		return;
524 	}
525 
526 	ret = bt_audio_codec_cfg_get_octets_per_frame(codec_cfg);
527 	if (ret > 0) {
528 		octets_per_frame = (uint16_t)ret;
529 	} else {
530 		FAIL("Could not get subgroup octets per frame: %d\n", ret);
531 
532 		return;
533 	}
534 
535 	if (!IN_RANGE(octets_per_frame, SUPPORTED_MIN_OCTETS_PER_FRAME,
536 		      SUPPORTED_MAX_OCTETS_PER_FRAME)) {
537 		FAIL("Unsupported octets per frame: %u\n", octets_per_frame);
538 
539 		return;
540 	}
541 
542 	ret = bt_audio_codec_cfg_get_frame_blocks_per_sdu(codec_cfg, true);
543 	if (ret > 0) {
544 		frames_blocks_per_sdu = (uint8_t)ret;
545 	} else {
546 		FAIL("Could not get frame blocks per SDU: %d\n", ret);
547 		return;
548 	}
549 
550 	/* An SDU can consist of X frame blocks, each with Y frames (one per channel) of size Z in
551 	 * them. The minimum SDU size required for this is X * Y * Z.
552 	 */
553 	min_sdu_size_required = chan_cnt * octets_per_frame * frames_blocks_per_sdu;
554 	if (min_sdu_size_required > stream->qos->sdu) {
555 		FAIL("With %zu channels and %u octets per frame and %u frames per block, SDUs "
556 		     "shall be at minimum %zu, but the stream has been configured for %u\n",
557 		     chan_cnt, octets_per_frame, frames_blocks_per_sdu, min_sdu_size_required,
558 		     stream->qos->sdu);
559 
560 		return;
561 	}
562 }
563 
stream_started_cb(struct bt_bap_stream * stream)564 static void stream_started_cb(struct bt_bap_stream *stream)
565 {
566 	struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream);
567 	struct bt_bap_ep_info info;
568 	int err;
569 
570 	memset(&test_stream->last_info, 0, sizeof(test_stream->last_info));
571 	test_stream->rx_cnt = 0U;
572 	test_stream->valid_rx_cnt = 0U;
573 
574 	err = bt_bap_ep_get_info(stream->ep, &info);
575 	if (err != 0) {
576 		FAIL("Failed to get EP info: %d\n", err);
577 		return;
578 	}
579 
580 	if (info.state != BT_BAP_EP_STATE_STREAMING) {
581 		FAIL("Unexpected EP state: %d\n", info.state);
582 		return;
583 	}
584 
585 	if (info.dir != BT_AUDIO_DIR_SINK) {
586 		FAIL("Unexpected info.dir: %d\n", info.dir);
587 		return;
588 	}
589 
590 	if (info.can_send) {
591 		FAIL("info.can_send is true\n");
592 		return;
593 	}
594 
595 	if (!info.can_recv) {
596 		FAIL("info.can_recv is false\n");
597 		return;
598 	}
599 
600 	if (info.paired_ep != NULL) {
601 		FAIL("Unexpected info.paired_ep: %p\n", info.paired_ep);
602 		return;
603 	}
604 
605 	printk("Stream %p started\n", stream);
606 	k_sem_give(&sem_stream_started);
607 
608 	validate_stream_codec_cfg(stream);
609 }
610 
stream_stopped_cb(struct bt_bap_stream * stream,uint8_t reason)611 static void stream_stopped_cb(struct bt_bap_stream *stream, uint8_t reason)
612 {
613 	printk("Stream %p stopped with reason 0x%02X\n", stream, reason);
614 	k_sem_give(&sem_stream_stopped);
615 }
616 
617 static struct bt_bap_stream_ops stream_ops = {
618 	.started = stream_started_cb,
619 	.stopped = stream_stopped_cb,
620 	.recv = bap_stream_rx_recv_cb,
621 };
622 
init(void)623 static int init(void)
624 {
625 	static struct bt_pacs_cap cap = {
626 		.codec_cap = &codec_cap,
627 	};
628 	static struct bt_pacs_cap vs_cap = {
629 		.codec_cap = &vs_codec_cap,
630 	};
631 	const struct bt_pacs_register_param pacs_param = {
632 		.snk_pac = true,
633 		.snk_loc = true,
634 		.src_pac = true,
635 		.src_loc = true,
636 	};
637 	int err;
638 
639 	err = bt_enable(NULL);
640 	if (err) {
641 		FAIL("Bluetooth enable failed (err %d)\n", err);
642 		return err;
643 	}
644 
645 	printk("Bluetooth initialized\n");
646 
647 	err = bt_pacs_register(&pacs_param);
648 	if (err) {
649 		FAIL("Could not register PACS (err %d)\n", err);
650 		return err;
651 	}
652 
653 	err = bt_pacs_cap_register(BT_AUDIO_DIR_SINK, &cap);
654 	if (err) {
655 		FAIL("Capability register failed (err %d)\n", err);
656 		return err;
657 	}
658 
659 	err = bt_pacs_cap_register(BT_AUDIO_DIR_SINK, &vs_cap);
660 	if (err) {
661 		FAIL("VS capability register failed (err %d)\n", err);
662 		return err;
663 	}
664 
665 	err = bt_bap_scan_delegator_register(&scan_delegator_cbs);
666 	if (err) {
667 		FAIL("Scan delegator register failed (err %d)\n", err);
668 		return err;
669 	}
670 
671 	/* Test invalid input */
672 	err = bt_bap_broadcast_sink_register_cb(NULL);
673 	if (err == 0) {
674 		FAIL("bt_bap_broadcast_sink_register_cb did not fail with NULL cb\n");
675 		return err;
676 	}
677 
678 	err = bt_bap_broadcast_sink_register_cb(&broadcast_sink_cbs);
679 	if (err != 0) {
680 		FAIL("Sink callback register failed (err %d)\n", err);
681 		return err;
682 	}
683 
684 	bt_le_per_adv_sync_cb_register(&bap_pa_sync_cb);
685 	bt_le_scan_cb_register(&bap_scan_cb);
686 
687 	UNSET_FLAG(flag_broadcaster_found);
688 	UNSET_FLAG(flag_base_received);
689 	UNSET_FLAG(flag_pa_synced);
690 
691 	for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) {
692 		streams[i] = bap_stream_from_audio_test_stream(&broadcast_sink_streams[i]);
693 		bt_bap_stream_cb_register(streams[i], &stream_ops);
694 	}
695 
696 	return 0;
697 }
698 
pa_sync_create(void)699 static int pa_sync_create(void)
700 {
701 	struct bt_le_per_adv_sync_param create_params = {0};
702 
703 	bt_addr_le_copy(&create_params.addr, &broadcaster_addr);
704 	create_params.options = BT_LE_PER_ADV_SYNC_OPT_FILTER_DUPLICATE;
705 	create_params.sid = broadcaster_info.sid;
706 	create_params.skip = PA_SYNC_SKIP;
707 	create_params.timeout = interval_to_sync_timeout(broadcaster_info.interval);
708 
709 	return bt_le_per_adv_sync_create(&create_params, &pa_sync);
710 }
test_pa_sync_delete(void)711 static void test_pa_sync_delete(void)
712 {
713 	int err;
714 
715 	err = bt_le_per_adv_sync_delete(pa_sync);
716 	if (err != 0) {
717 		FAIL("Unable to stop sink: %d", err);
718 		return;
719 	}
720 
721 	pa_sync = NULL;
722 }
723 
test_scan_and_pa_sync(void)724 static void test_scan_and_pa_sync(void)
725 {
726 	int err;
727 
728 	printk("Scanning for broadcast sources\n");
729 	err = bt_le_scan_start(BT_LE_SCAN_ACTIVE, NULL);
730 	if (err != 0) {
731 		FAIL("Unable to start scan for broadcast sources: %d", err);
732 		return;
733 	}
734 
735 	WAIT_FOR_FLAG(flag_broadcaster_found);
736 
737 	printk("Broadcast source found, stopping scan\n");
738 	err = bt_le_scan_stop();
739 	if (err != 0) {
740 		FAIL("bt_le_scan_stop failed with %d\n", err);
741 		return;
742 	}
743 
744 	printk("Scan stopped, attempting to PA sync to the broadcaster with id 0x%06X\n",
745 	       broadcaster_broadcast_id);
746 	err = pa_sync_create();
747 	if (err != 0) {
748 		FAIL("Could not create Broadcast PA sync: %d\n", err);
749 		return;
750 	}
751 
752 	printk("Waiting for PA sync\n");
753 	WAIT_FOR_FLAG(flag_pa_synced);
754 }
755 
test_broadcast_sink_create(void)756 static void test_broadcast_sink_create(void)
757 {
758 	int err;
759 
760 	printk("Creating the broadcast sink\n");
761 	err = bt_bap_broadcast_sink_create(pa_sync, broadcaster_broadcast_id, &g_sink);
762 	if (err != 0) {
763 		FAIL("Unable to create the sink: %d\n", err);
764 		return;
765 	}
766 
767 	printk("Created broadcast sink %p\n", g_sink);
768 }
769 
test_broadcast_sink_create_inval(void)770 static void test_broadcast_sink_create_inval(void)
771 {
772 	int err;
773 
774 	err = bt_bap_broadcast_sink_create(NULL, broadcaster_broadcast_id, &g_sink);
775 	if (err == 0) {
776 		FAIL("bt_bap_broadcast_sink_create did not fail with NULL sink\n");
777 		return;
778 	}
779 
780 	err = bt_bap_broadcast_sink_create(pa_sync, BT_BAP_INVALID_BROADCAST_ID, &g_sink);
781 	if (err == 0) {
782 		FAIL("bt_bap_broadcast_sink_create did not fail with invalid broadcast ID\n");
783 		return;
784 	}
785 
786 	err = bt_bap_broadcast_sink_create(pa_sync, broadcaster_broadcast_id, NULL);
787 	if (err == 0) {
788 		FAIL("bt_bap_broadcast_sink_create did not fail with NULL sink\n");
789 		return;
790 	}
791 }
792 
test_broadcast_sync(const uint8_t broadcast_code[BT_ISO_BROADCAST_CODE_SIZE])793 static void test_broadcast_sync(const uint8_t broadcast_code[BT_ISO_BROADCAST_CODE_SIZE])
794 {
795 	int err;
796 
797 	printk("Syncing sink %p to 0x%08x\n", g_sink, bis_index_bitfield);
798 	err = bt_bap_broadcast_sink_sync(g_sink, bis_index_bitfield, streams, broadcast_code);
799 	if (err != 0) {
800 		FAIL("Unable to sync the sink: %d\n", err);
801 		return;
802 	}
803 
804 	stream_sync_cnt = POPCOUNT(bis_index_bitfield);
805 }
806 
test_broadcast_sync_inval(void)807 static void test_broadcast_sync_inval(void)
808 {
809 	struct bt_bap_stream *tmp_streams[ARRAY_SIZE(streams) + 1] = {0};
810 	uint32_t bis_index;
811 	int err;
812 
813 	err = bt_bap_broadcast_sink_sync(NULL, bis_index_bitfield, streams, NULL);
814 	if (err == 0) {
815 		FAIL("bt_bap_broadcast_sink_sync did not fail with NULL sink\n");
816 		return;
817 	}
818 
819 	bis_index = 0;
820 	err = bt_bap_broadcast_sink_sync(g_sink, bis_index, streams, NULL);
821 	if (err == 0) {
822 		FAIL("bt_bap_broadcast_sink_sync did not fail with invalid BIS indexes: 0x%08X\n",
823 		     bis_index);
824 		return;
825 	}
826 
827 	bis_index = BT_ISO_BIS_INDEX_BIT(BT_ISO_BIS_INDEX_MAX + 1);
828 	err = bt_bap_broadcast_sink_sync(g_sink, bis_index, streams, NULL);
829 	if (err == 0) {
830 		FAIL("bt_bap_broadcast_sink_sync did not fail with invalid BIS indexes: 0x%08X\n",
831 		     bis_index);
832 		return;
833 	}
834 
835 	err = bt_bap_broadcast_sink_sync(g_sink, bis_index, NULL, NULL);
836 	if (err == 0) {
837 		FAIL("bt_bap_broadcast_sink_sync did not fail with NULL streams\n");
838 		return;
839 	}
840 
841 	memcpy(tmp_streams, streams, sizeof(streams));
842 	bis_index = 0U;
843 	for (size_t i = 0U; i < ARRAY_SIZE(tmp_streams); i++) {
844 		bis_index |= BT_ISO_BIS_INDEX_BIT(i);
845 	}
846 
847 	err = bt_bap_broadcast_sink_sync(g_sink, bis_index, tmp_streams, NULL);
848 	if (err == 0) {
849 		FAIL("bt_bap_broadcast_sink_sync did not fail with NULL streams[%zu]\n",
850 		     ARRAY_SIZE(tmp_streams) - 1);
851 		return;
852 	}
853 
854 	bis_index = 0U;
855 	for (size_t i = 0U; i < CONFIG_BT_BAP_BROADCAST_SNK_STREAM_COUNT + 1; i++) {
856 		bis_index |= BT_ISO_BIS_INDEX_BIT(i);
857 	}
858 
859 	err = bt_bap_broadcast_sink_sync(g_sink, bis_index, tmp_streams, NULL);
860 	if (err == 0) {
861 		FAIL("bt_bap_broadcast_sink_sync did not fail with invalid BIS indexes: 0x%08X\n",
862 		     bis_index);
863 		return;
864 	}
865 }
866 
test_broadcast_stop(void)867 static void test_broadcast_stop(void)
868 {
869 	int err;
870 
871 	printk("Stopping broadcast sink %p\n", g_sink);
872 
873 	err = bt_bap_broadcast_sink_stop(g_sink);
874 	if (err != 0) {
875 		FAIL("Unable to stop sink: %d", err);
876 		return;
877 	}
878 
879 	printk("Waiting for %zu streams to be stopped\n", stream_sync_cnt);
880 	for (size_t i = 0U; i < stream_sync_cnt; i++) {
881 		k_sem_take(&sem_stream_stopped, K_FOREVER);
882 	}
883 
884 	WAIT_FOR_UNSET_FLAG(flag_sink_started);
885 }
886 
test_broadcast_stop_inval(void)887 static void test_broadcast_stop_inval(void)
888 {
889 	int err;
890 
891 	err = bt_bap_broadcast_sink_stop(NULL);
892 	if (err == 0) {
893 		FAIL("bt_bap_broadcast_sink_stop did not fail with NULL sink\n");
894 		return;
895 	}
896 }
897 
test_broadcast_delete(void)898 static void test_broadcast_delete(void)
899 {
900 	int err;
901 
902 	err = bt_bap_broadcast_sink_delete(g_sink);
903 	if (err != 0) {
904 		FAIL("Unable to stop sink: %d", err);
905 		return;
906 	}
907 
908 	/* No "sync lost" event is generated when we initialized the disconnect */
909 	g_sink = NULL;
910 }
911 
test_broadcast_delete_inval(void)912 static void test_broadcast_delete_inval(void)
913 {
914 	int err;
915 
916 	err = bt_bap_broadcast_sink_delete(NULL);
917 	if (err == 0) {
918 		FAIL("bt_bap_broadcast_sink_delete did not fail with NULL sink\n");
919 		return;
920 	}
921 }
922 
test_common(void)923 static void test_common(void)
924 {
925 	int err;
926 
927 	err = init();
928 	if (err) {
929 		FAIL("Init failed (err %d)\n", err);
930 		return;
931 	}
932 
933 	test_scan_and_pa_sync();
934 
935 	test_broadcast_sink_create_inval();
936 	test_broadcast_sink_create();
937 
938 	printk("Broadcast source PA synced, waiting for BASE\n");
939 	WAIT_FOR_FLAG(flag_base_received);
940 	printk("BASE received\n");
941 
942 	printk("Waiting for BIG syncable\n");
943 	WAIT_FOR_FLAG(flag_syncable);
944 
945 	test_broadcast_sync_inval();
946 	test_broadcast_sync(NULL);
947 
948 	WAIT_FOR_FLAG(flag_sink_started);
949 
950 	/* Wait for all to be started */
951 	printk("Waiting for %zu streams to be started\n", stream_sync_cnt);
952 	for (size_t i = 0U; i < stream_sync_cnt; i++) {
953 		k_sem_take(&sem_stream_started, K_FOREVER);
954 	}
955 
956 	printk("Waiting for data\n");
957 	WAIT_FOR_FLAG(flag_audio_received);
958 	backchannel_sync_send_all(); /* let other devices know we have received what we wanted */
959 }
960 
test_main(void)961 static void test_main(void)
962 {
963 	test_common();
964 
965 	backchannel_sync_send_all(); /* let the broadcast source know it can stop */
966 
967 	/* The order of PA sync lost and BIG Sync lost is irrelevant
968 	 * and depend on timeout parameters. We just wait for PA first, but
969 	 * either way will work.
970 	 */
971 	printk("Waiting for PA disconnected\n");
972 	WAIT_FOR_FLAG(flag_pa_sync_lost);
973 
974 	printk("Waiting for %zu streams to be stopped\n", stream_sync_cnt);
975 	for (size_t i = 0U; i < stream_sync_cnt; i++) {
976 		k_sem_take(&sem_stream_stopped, K_FOREVER);
977 	}
978 	WAIT_FOR_UNSET_FLAG(flag_sink_started);
979 
980 	PASS("Broadcast sink passed\n");
981 }
982 
test_main_update(void)983 static void test_main_update(void)
984 {
985 	test_common();
986 
987 	/* Ensure that we also see the metadata update */
988 	printk("Waiting for metadata update\n");
989 	WAIT_FOR_FLAG(flag_base_metadata_updated)
990 
991 	backchannel_sync_send_all(); /* let other devices know we have received what we wanted */
992 
993 	backchannel_sync_send_all(); /* let the broadcast source know it can stop */
994 
995 	/* The order of PA sync lost and BIG Sync lost is irrelevant
996 	 * and depend on timeout parameters. We just wait for PA first, but
997 	 * either way will work.
998 	 */
999 	printk("Waiting for PA disconnected\n");
1000 	WAIT_FOR_FLAG(flag_pa_sync_lost);
1001 
1002 	printk("Waiting for %zu streams to be stopped\n", stream_sync_cnt);
1003 	for (size_t i = 0U; i < stream_sync_cnt; i++) {
1004 		k_sem_take(&sem_stream_stopped, K_FOREVER);
1005 	}
1006 	WAIT_FOR_UNSET_FLAG(flag_sink_started);
1007 
1008 	PASS("Broadcast sink passed\n");
1009 }
1010 
test_sink_disconnect(void)1011 static void test_sink_disconnect(void)
1012 {
1013 	test_common();
1014 
1015 	test_broadcast_stop_inval();
1016 	test_broadcast_stop();
1017 
1018 	/* Retry sync*/
1019 	test_broadcast_sync(NULL);
1020 
1021 	WAIT_FOR_FLAG(flag_sink_started);
1022 
1023 	/* Wait for all to be started */
1024 	printk("Waiting for %zu streams to be started\n", stream_sync_cnt);
1025 	for (size_t i = 0U; i < stream_sync_cnt; i++) {
1026 		k_sem_take(&sem_stream_started, K_FOREVER);
1027 	}
1028 
1029 	test_broadcast_stop();
1030 
1031 	test_broadcast_delete_inval();
1032 	test_broadcast_delete();
1033 
1034 	backchannel_sync_send_all(); /* let the broadcast source know it can stop */
1035 
1036 	PASS("Broadcast sink disconnect passed\n");
1037 }
1038 
test_sink_encrypted(void)1039 static void test_sink_encrypted(void)
1040 {
1041 	int err;
1042 
1043 	err = init();
1044 	if (err) {
1045 		FAIL("Init failed (err %d)\n", err);
1046 		return;
1047 	}
1048 
1049 	test_scan_and_pa_sync();
1050 
1051 	test_broadcast_sink_create();
1052 
1053 	printk("Broadcast source PA synced, waiting for BASE\n");
1054 	WAIT_FOR_FLAG(flag_base_received);
1055 	printk("BASE received\n");
1056 
1057 	printk("Waiting for BIG syncable\n");
1058 	WAIT_FOR_FLAG(flag_syncable);
1059 
1060 	test_broadcast_sync(BROADCAST_CODE);
1061 
1062 	WAIT_FOR_FLAG(flag_sink_started);
1063 
1064 	/* Wait for all to be started */
1065 	printk("Waiting for %zu streams to be started\n", stream_sync_cnt);
1066 	for (size_t i = 0U; i < stream_sync_cnt; i++) {
1067 		k_sem_take(&sem_stream_started, K_FOREVER);
1068 	}
1069 
1070 	printk("Waiting for data\n");
1071 	WAIT_FOR_FLAG(flag_audio_received);
1072 
1073 	backchannel_sync_send_all(); /* let other devices know we have received data */
1074 
1075 	backchannel_sync_send_all(); /* let the broadcast source know it can stop */
1076 
1077 	/* The order of PA sync lost and BIG Sync lost is irrelevant
1078 	 * and depend on timeout parameters. We just wait for PA first, but
1079 	 * either way will work.
1080 	 */
1081 	printk("Waiting for PA disconnected\n");
1082 	WAIT_FOR_FLAG(flag_pa_sync_lost);
1083 
1084 	printk("Waiting for %zu streams to be stopped\n", stream_sync_cnt);
1085 	for (size_t i = 0U; i < stream_sync_cnt; i++) {
1086 		k_sem_take(&sem_stream_stopped, K_FOREVER);
1087 	}
1088 
1089 	PASS("Broadcast sink encrypted passed\n");
1090 }
1091 
test_sink_encrypted_incorrect_code(void)1092 static void test_sink_encrypted_incorrect_code(void)
1093 {
1094 	int err;
1095 
1096 	err = init();
1097 	if (err) {
1098 		FAIL("Init failed (err %d)\n", err);
1099 		return;
1100 	}
1101 
1102 	test_scan_and_pa_sync();
1103 
1104 	test_broadcast_sink_create();
1105 
1106 	printk("Broadcast source PA synced, waiting for BASE\n");
1107 	WAIT_FOR_FLAG(flag_base_received);
1108 	printk("BASE received\n");
1109 
1110 	printk("Waiting for BIG syncable\n");
1111 	WAIT_FOR_FLAG(flag_syncable);
1112 
1113 	test_broadcast_sync(INCORRECT_BROADCAST_CODE);
1114 	/* Wait for MIC failure */
1115 	WAIT_FOR_FLAG(flag_big_sync_mic_failure);
1116 
1117 	test_broadcast_sync(BROADCAST_CODE);
1118 
1119 	/* Wait for all to be started */
1120 	printk("Waiting for %zu streams to be started\n", stream_sync_cnt);
1121 	for (size_t i = 0U; i < stream_sync_cnt; i++) {
1122 		k_sem_take(&sem_stream_started, K_FOREVER);
1123 	}
1124 
1125 	printk("Waiting for data\n");
1126 	WAIT_FOR_FLAG(flag_audio_received);
1127 	printk("Data received\n");
1128 
1129 	backchannel_sync_send_all(); /* let other devices know we have received data */
1130 	backchannel_sync_send_all(); /* let the broadcast source know it can stop */
1131 
1132 	PASS("Broadcast sink incorrect code passed\n");
1133 }
1134 
broadcast_sink_with_assistant(void)1135 static void broadcast_sink_with_assistant(void)
1136 {
1137 	struct bt_le_ext_adv *ext_adv;
1138 	int err;
1139 
1140 	err = init();
1141 	if (err) {
1142 		FAIL("Init failed (err %d)\n", err);
1143 		return;
1144 	}
1145 
1146 	setup_connectable_adv(&ext_adv);
1147 	WAIT_FOR_FLAG(flag_connected);
1148 
1149 	printk("Waiting for PA sync request\n");
1150 	WAIT_FOR_FLAG(flag_pa_request);
1151 
1152 	test_scan_and_pa_sync();
1153 	test_broadcast_sink_create();
1154 
1155 	printk("Broadcast source PA synced, waiting for BASE\n");
1156 	WAIT_FOR_FLAG(flag_base_received);
1157 	printk("BASE received\n");
1158 
1159 	printk("Waiting for BIG syncable\n");
1160 	WAIT_FOR_FLAG(flag_syncable);
1161 
1162 	printk("Waiting for BIG sync request\n");
1163 	WAIT_FOR_FLAG(flag_bis_sync_requested);
1164 	test_broadcast_sync(NULL);
1165 
1166 	WAIT_FOR_FLAG(flag_sink_started);
1167 
1168 	/* Wait for all to be started */
1169 	printk("Waiting for %zu streams to be started\n", stream_sync_cnt);
1170 	for (size_t i = 0U; i < stream_sync_cnt; i++) {
1171 		k_sem_take(&sem_stream_started, K_FOREVER);
1172 	}
1173 
1174 	printk("Waiting for data\n");
1175 	WAIT_FOR_FLAG(flag_audio_received);
1176 	backchannel_sync_send_all(); /* let other devices know we have received what we wanted */
1177 
1178 	printk("Waiting for BIG sync terminate request\n");
1179 	WAIT_FOR_UNSET_FLAG(flag_bis_sync_requested);
1180 	test_broadcast_stop();
1181 
1182 	printk("Waiting for PA sync terminate request\n");
1183 	WAIT_FOR_UNSET_FLAG(flag_pa_request);
1184 	test_pa_sync_delete();
1185 	test_broadcast_delete();
1186 
1187 	backchannel_sync_send_all(); /* let the broadcast source know it can stop */
1188 
1189 	PASS("Broadcast sink with assistant passed\n");
1190 }
1191 
broadcast_sink_with_assistant_incorrect_code(void)1192 static void broadcast_sink_with_assistant_incorrect_code(void)
1193 {
1194 	struct bt_le_ext_adv *ext_adv;
1195 	int err;
1196 
1197 	err = init();
1198 	if (err) {
1199 		FAIL("Init failed (err %d)\n", err);
1200 		return;
1201 	}
1202 
1203 	setup_connectable_adv(&ext_adv);
1204 	WAIT_FOR_FLAG(flag_connected);
1205 
1206 	printk("Waiting for PA sync request\n");
1207 	WAIT_FOR_FLAG(flag_pa_request);
1208 
1209 	test_scan_and_pa_sync();
1210 	test_broadcast_sink_create();
1211 
1212 	printk("Broadcast source PA synced, waiting for BASE\n");
1213 	WAIT_FOR_FLAG(flag_base_received);
1214 	printk("BASE received\n");
1215 
1216 	printk("Waiting for BIG syncable\n");
1217 	WAIT_FOR_FLAG(flag_syncable);
1218 
1219 	printk("Waiting for BIG sync request\n");
1220 	WAIT_FOR_FLAG(flag_bis_sync_requested);
1221 	test_broadcast_sync(recv_state_broadcast_code);
1222 	/* Wait for MIC failure */
1223 	WAIT_FOR_FLAG(flag_big_sync_mic_failure);
1224 
1225 	backchannel_sync_send_all(); /* let other devices know we have received data */
1226 
1227 	printk("Waiting for PA sync terminate request\n");
1228 	WAIT_FOR_UNSET_FLAG(flag_pa_request);
1229 	test_pa_sync_delete();
1230 	test_broadcast_delete();
1231 
1232 	backchannel_sync_send_all(); /* let the broadcast source know it can stop */
1233 
1234 	PASS("Broadcast sink with assistant and incorrect code passed\n");
1235 }
1236 
1237 static const struct bst_test_instance test_broadcast_sink[] = {
1238 	{
1239 		.test_id = "broadcast_sink",
1240 		.test_pre_init_f = test_init,
1241 		.test_tick_f = test_tick,
1242 		.test_main_f = test_main,
1243 	},
1244 	{
1245 		.test_id = "broadcast_sink_update",
1246 		.test_pre_init_f = test_init,
1247 		.test_tick_f = test_tick,
1248 		.test_main_f = test_main_update,
1249 	},
1250 	{
1251 		.test_id = "broadcast_sink_disconnect",
1252 		.test_pre_init_f = test_init,
1253 		.test_tick_f = test_tick,
1254 		.test_main_f = test_sink_disconnect,
1255 	},
1256 	{
1257 		.test_id = "broadcast_sink_encrypted",
1258 		.test_pre_init_f = test_init,
1259 		.test_tick_f = test_tick,
1260 		.test_main_f = test_sink_encrypted,
1261 	},
1262 	{
1263 		.test_id = "broadcast_sink_encrypted_incorrect_code",
1264 		.test_pre_init_f = test_init,
1265 		.test_tick_f = test_tick,
1266 		.test_main_f = test_sink_encrypted_incorrect_code,
1267 	},
1268 	{
1269 		.test_id = "broadcast_sink_with_assistant",
1270 		.test_pre_init_f = test_init,
1271 		.test_tick_f = test_tick,
1272 		.test_main_f = broadcast_sink_with_assistant,
1273 	},
1274 	{
1275 		.test_id = "broadcast_sink_with_assistant_incorrect_code",
1276 		.test_pre_init_f = test_init,
1277 		.test_tick_f = test_tick,
1278 		.test_main_f = broadcast_sink_with_assistant_incorrect_code,
1279 	},
1280 	BSTEST_END_MARKER,
1281 };
1282 
test_broadcast_sink_install(struct bst_test_list * tests)1283 struct bst_test_list *test_broadcast_sink_install(struct bst_test_list *tests)
1284 {
1285 	return bst_add_tests(tests, test_broadcast_sink);
1286 }
1287 
1288 #else /* !CONFIG_BT_BAP_BROADCAST_SINK */
1289 
test_broadcast_sink_install(struct bst_test_list * tests)1290 struct bst_test_list *test_broadcast_sink_install(struct bst_test_list *tests)
1291 {
1292 	return tests;
1293 }
1294 
1295 #endif /* CONFIG_BT_BAP_BROADCAST_SINK */
1296