1 /*
2  * Copyright (c) 2023-2024 Nordic Semiconductor ASA
3  * Copyright (c) 2017-2019 Oticon A/S
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <zephyr/sys/util_macro.h>
9 
10 #include "bs_types.h"
11 #include "bs_tracing.h"
12 #include "bs_utils.h"
13 #include "time_machine.h"
14 #include "bstests.h"
15 
16 #define WAIT_TIME 10 /* Seconds */
17 
18 #define PASS_THRESHOLD 100 /* Audio packets */
19 
20 extern enum bst_result_t bst_result;
21 
22 #define FAIL(...)                                                                                  \
23 	do {                                                                                       \
24 		bst_result = Failed;                                                               \
25 		bs_trace_error_time_line(__VA_ARGS__);                                             \
26 	} while (0)
27 
28 #define PASS(...)                                                                                  \
29 	do {                                                                                       \
30 		bst_result = Passed;                                                               \
31 		bs_trace_info_time(1, __VA_ARGS__);                                                \
32 	} while (0)
33 
test_cap_acceptor_sample_init(void)34 static void test_cap_acceptor_sample_init(void)
35 {
36 	/* We set an absolute deadline in 30 seconds */
37 	bst_ticker_set_next_tick_absolute(WAIT_TIME * 1e6);
38 	bst_result = In_progress;
39 }
40 
test_cap_acceptor_sample_tick(bs_time_t HW_device_time)41 static void test_cap_acceptor_sample_tick(bs_time_t HW_device_time)
42 {
43 	/*
44 	 * If in WAIT_TIME seconds we did not get enough packets through
45 	 * we consider the test failed
46 	 */
47 
48 	if (IS_ENABLED(CONFIG_SAMPLE_UNICAST)) {
49 		extern uint64_t total_unicast_rx_iso_packet_count;
50 		extern uint64_t total_unicast_tx_iso_packet_count;
51 
52 		bs_trace_info_time(2, "%" PRIu64 " unicast packets received, expected >= %i\n",
53 				   total_unicast_tx_iso_packet_count, PASS_THRESHOLD);
54 		bs_trace_info_time(2, "%" PRIu64 " unicast packets sent, expected >= %i\n",
55 				   total_unicast_tx_iso_packet_count, PASS_THRESHOLD);
56 
57 		if (total_unicast_rx_iso_packet_count < PASS_THRESHOLD ||
58 		    total_unicast_tx_iso_packet_count < PASS_THRESHOLD) {
59 			FAIL("cap_acceptor FAILED with(Did not pass after %d seconds)\n ",
60 			     WAIT_TIME);
61 			return;
62 		}
63 	}
64 
65 	if (IS_ENABLED(CONFIG_SAMPLE_BROADCAST)) {
66 		extern uint64_t total_broadcast_rx_iso_packet_count;
67 
68 		bs_trace_info_time(2, "%" PRIu64 " broadcast packets received, expected >= %i\n",
69 				   total_broadcast_rx_iso_packet_count, PASS_THRESHOLD);
70 
71 		if (total_broadcast_rx_iso_packet_count < PASS_THRESHOLD) {
72 			FAIL("cap_acceptor FAILED with (Did not pass after %d seconds)\n ",
73 			     WAIT_TIME);
74 			return;
75 		}
76 	}
77 
78 	PASS("cap_acceptor PASSED\n");
79 }
80 
81 static const struct bst_test_instance test_sample[] = {
82 	{
83 		.test_id = "cap_acceptor",
84 		.test_descr = "Test based on the unicast client sample. "
85 			      "It expects to be connected to a compatible unicast server, "
86 			      "waits for " STR(WAIT_TIME) " seconds, and checks how "
87 			      "many audio packets have been received correctly",
88 		.test_post_init_f = test_cap_acceptor_sample_init,
89 		.test_tick_f = test_cap_acceptor_sample_tick,
90 	},
91 	BSTEST_END_MARKER
92 };
93 
94 /* TODO: Add test of reconnection */
95 
test_cap_acceptor_sample_install(struct bst_test_list * tests)96 struct bst_test_list *test_cap_acceptor_sample_install(struct bst_test_list *tests)
97 {
98 	tests = bst_add_tests(tests, test_sample);
99 	return tests;
100 }
101