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