1 /*
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  * Copyright (c) 2017-2019 Oticon A/S
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include "bs_types.h"
9 #include "bs_tracing.h"
10 #include "bs_utils.h"
11 #include "time_machine.h"
12 #include "bstests.h"
13 
14 #define WAIT_TIME 120 /* Seconds */
15 
16 #define PASS_THRESHOLD 100 /* Audio packets */
17 
18 extern enum bst_result_t bst_result;
19 
20 #define FAIL(...)					\
21 	do {						\
22 		bst_result = Failed;			\
23 		bs_trace_error_time_line(__VA_ARGS__);	\
24 	} while (0)
25 
26 #define PASS(...)					\
27 	do {						\
28 		bst_result = Passed;			\
29 		bs_trace_info_time(1, __VA_ARGS__);	\
30 	} while (0)
31 
test_broadcast_sink_sample_init(void)32 static void test_broadcast_sink_sample_init(void)
33 {
34 	/* We set an absolute deadline in 30 seconds */
35 	bst_ticker_set_next_tick_absolute(WAIT_TIME*1e6);
36 	bst_result = In_progress;
37 }
38 
test_broadcast_sink_sample_tick(bs_time_t HW_device_time)39 static void test_broadcast_sink_sample_tick(bs_time_t HW_device_time)
40 {
41 	/*
42 	 * If in WAIT_TIME seconds we did not get enough packets through
43 	 * we consider the test failed
44 	 */
45 
46 	extern uint64_t total_rx_iso_packet_count;
47 
48 	bs_trace_info_time(2, "%"PRIu64" packets received, expected >= %i\n",
49 			   total_rx_iso_packet_count, PASS_THRESHOLD);
50 
51 	if (total_rx_iso_packet_count >= PASS_THRESHOLD) {
52 		PASS("broadcast_sink PASSED\n");
53 		bs_trace_exit("Done, disconnecting from simulation\n");
54 	} else {
55 		FAIL("broadcast_sink FAILED (Did not pass after %i seconds)\n",
56 		     WAIT_TIME);
57 	}
58 }
59 
60 static const struct bst_test_instance test_sample[] = {
61 	{
62 		.test_id = "bap_broadcast_sink",
63 		.test_descr = "Test based on the broadcast audio sink sample. "
64 			      "It expects to be connected to a compatible broadcast audio source, "
65 			      "waits for " STR(WAIT_TIME) " seconds, and checks how "
66 			      "many ISO packets have been received correctly",
67 		.test_pre_init_f = test_broadcast_sink_sample_init,
68 		.test_tick_f = test_broadcast_sink_sample_tick,
69 	},
70 	BSTEST_END_MARKER};
71 
test_broadcast_sink_test_install(struct bst_test_list * tests)72 struct bst_test_list *test_broadcast_sink_test_install(struct bst_test_list *tests)
73 {
74 	tests = bst_add_tests(tests, test_sample);
75 	return tests;
76 }
77