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_initiator_sample_init(void)34 static void test_cap_initiator_sample_init(void)
35 {
36 	bst_ticker_set_next_tick_absolute(WAIT_TIME * 1e6);
37 	bst_result = In_progress;
38 }
39 
test_cap_initiator_sample_tick(bs_time_t HW_device_time)40 static void test_cap_initiator_sample_tick(bs_time_t HW_device_time)
41 {
42 	/*
43 	 * If in WAIT_TIME seconds we did not get enough packets through
44 	 * we consider the test failed
45 	 */
46 
47 	if (IS_ENABLED(CONFIG_SAMPLE_UNICAST)) {
48 		extern uint64_t total_rx_iso_packet_count;
49 		extern uint64_t total_unicast_tx_iso_packet_count;
50 
51 		bs_trace_info_time(2, "%" PRIu64 " unicast packets received, expected >= %i\n",
52 				   total_rx_iso_packet_count, PASS_THRESHOLD);
53 		bs_trace_info_time(2, "%" PRIu64 " unicast packets sent, expected >= %i\n",
54 				   total_unicast_tx_iso_packet_count, PASS_THRESHOLD);
55 
56 		if (total_rx_iso_packet_count < PASS_THRESHOLD ||
57 		    total_unicast_tx_iso_packet_count < PASS_THRESHOLD) {
58 			FAIL("cap_initiator FAILED with(Did not pass after %d seconds)\n ",
59 			     WAIT_TIME);
60 			return;
61 		}
62 	}
63 
64 	if (IS_ENABLED(CONFIG_SAMPLE_BROADCAST)) {
65 		extern uint64_t total_broadcast_tx_iso_packet_count;
66 
67 		bs_trace_info_time(2, "%" PRIu64 " broadcast packets sent, expected >= %i\n",
68 				   total_broadcast_tx_iso_packet_count, PASS_THRESHOLD);
69 
70 		if (total_broadcast_tx_iso_packet_count < PASS_THRESHOLD) {
71 			FAIL("cap_initiator FAILED with (Did not pass after %d seconds)\n ",
72 			     WAIT_TIME);
73 			return;
74 		}
75 	}
76 
77 	PASS("cap_initiator PASSED\n");
78 }
79 
80 static const struct bst_test_instance test_sample[] = {
81 	{
82 		.test_id = "cap_initiator",
83 		.test_descr = "Test based on the unicast client sample. "
84 			      "It expects to be connected to a compatible unicast server, "
85 			      "waits for " STR(WAIT_TIME) " seconds, and checks how "
86 			      "many audio packets have been received correctly",
87 		.test_post_init_f = test_cap_initiator_sample_init,
88 		.test_tick_f = test_cap_initiator_sample_tick,
89 	},
90 	BSTEST_END_MARKER
91 };
92 
93 /* TODO: Add test of reconnection */
94 
test_cap_initiator_sample_install(struct bst_test_list * tests)95 struct bst_test_list *test_cap_initiator_sample_install(struct bst_test_list *tests)
96 {
97 	tests = bst_add_tests(tests, test_sample);
98 	return tests;
99 }
100