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 10 /* 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_unicast_client_sample_init(void)32 static void test_unicast_client_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_unicast_client_sample_tick(bs_time_t HW_device_time)39 static void test_unicast_client_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 unicast_audio_recv_ctr;
47 
48 	bs_trace_info_time(2, "%"PRIu64" packets received, expected >= %i\n",
49 			   unicast_audio_recv_ctr, PASS_THRESHOLD);
50 
51 	if (unicast_audio_recv_ctr >= PASS_THRESHOLD) {
52 		PASS("unicast_client PASSED\n");
53 		bs_trace_exit("Done, disconnecting from simulation\n");
54 	} else {
55 		FAIL("unicast_client 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 = "unicast_client",
63 		.test_descr = "Test based on the unicast client sample. "
64 			      "It expects to be connected to a compatible unicast server, "
65 			      "waits for " STR(WAIT_TIME) " seconds, and checks how "
66 			      "many audio packets have been received correctly",
67 		.test_pre_init_f = test_unicast_client_sample_init,
68 		.test_tick_f = test_unicast_client_sample_tick,
69 	},
70 	BSTEST_END_MARKER
71 };
72 
test_unicast_client_sample_install(struct bst_test_list * tests)73 struct bst_test_list *test_unicast_client_sample_install(struct bst_test_list *tests)
74 {
75 	tests = bst_add_tests(tests, test_sample);
76 	return tests;
77 }
78