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 "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 5 /* 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_sample_init(void)32 static void test_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_sample_tick(bs_time_t HW_device_time)39 static void test_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_count;
47 
48 	bs_trace_info_time(2, "%"PRIu64" packets received, expected >= %i\n",
49 			   total_rx_count, PASS_THRESHOLD);
50 
51 	if (total_rx_count >= PASS_THRESHOLD) {
52 		PASS("PASSED\n");
53 		bs_trace_exit("Done, disconnecting from simulation\n");
54 	} else {
55 		FAIL("FAILED (Did not pass after %i seconds)\n", WAIT_TIME);
56 	}
57 }
58 
59 static const struct bst_test_instance test_sample[] = {
60 	{
61 		.test_id = "central_hr_peripheral_hr",
62 		.test_descr = "Test based on the peripheral and central HR samples. "
63 			      "It expects to be connected to a compatible sample, "
64 			      "waits for " STR(WAIT_TIME) " seconds, and checks how "
65 			      "many packets have been received correctly",
66 		.test_pre_init_f = test_sample_init,
67 		.test_tick_f = test_sample_tick,
68 	},
69 	BSTEST_END_MARKER
70 };
71 
test_sample_install(struct bst_test_list * tests)72 struct bst_test_list *test_sample_install(struct bst_test_list *tests)
73 {
74 	tests = bst_add_tests(tests, test_sample);
75 	return tests;
76 }
77