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 #include "common.h" /* From echo_client */
15 
16 #if defined(CONFIG_NET_L2_OPENTHREAD)
17 /* Open thread takes ~15 seconds to connect in ideal conditions */
18 #define WAIT_TIME 25 /* Seconds */
19 #else
20 #define WAIT_TIME 20 /* Seconds */
21 #endif
22 
23 #define PASS_THRESHOLD 100 /* Packets */
24 
25 extern enum bst_result_t bst_result;
26 
27 #define FAIL(...)					\
28 	do {						\
29 		bst_result = Failed;			\
30 		bs_trace_error_time_line(__VA_ARGS__);	\
31 	} while (0)
32 
33 #define PASS(...)					\
34 	do {						\
35 		bst_result = Passed;			\
36 		bs_trace_info_time(1, __VA_ARGS__);	\
37 	} while (0)
38 
test_echo_client_init(void)39 static void test_echo_client_init(void)
40 {
41 	/* We set an absolute deadline in 30 seconds */
42 	bst_ticker_set_next_tick_absolute(WAIT_TIME*1e6);
43 	bst_result = In_progress;
44 }
45 
test_echo_client_tick(bs_time_t HW_device_time)46 static void test_echo_client_tick(bs_time_t HW_device_time)
47 {
48 	/*
49 	 * If in WAIT_TIME seconds we did not get enough packets through
50 	 * we consider the test failed
51 	 */
52 
53 	extern struct configs conf;
54 	int packet_count = 0;
55 
56 	if ((IS_ENABLED(CONFIG_NET_TCP)) && IS_ENABLED(CONFIG_NET_IPV6)) {
57 		packet_count = conf.ipv6.tcp.counter;
58 	} else if ((IS_ENABLED(CONFIG_NET_UDP)) && IS_ENABLED(CONFIG_NET_IPV6)) {
59 		packet_count = conf.ipv6.udp.counter;
60 	}
61 
62 	bs_trace_info_time(2, "%i packets received, expected >= %i\n",
63 			   packet_count, PASS_THRESHOLD);
64 
65 	if (packet_count >= PASS_THRESHOLD) {
66 		PASS("echo_client PASSED\n");
67 		bs_trace_exit("Done, disconnecting from simulation\n");
68 	} else {
69 		FAIL("echo_client FAILED (Did not pass after %i seconds)\n",
70 		     WAIT_TIME);
71 	}
72 }
73 
74 static const struct bst_test_instance test_echo_client[] = {
75 	{
76 		.test_id = "echo_client",
77 		.test_descr = "Test based on the echo client sample. "
78 			      "It expects to be connected to a compatible echo server, "
79 			      "waits for " STR(WAIT_TIME) " seconds, and checks how "
80 			      "many packets have been exchanged correctly",
81 		.test_pre_init_f = test_echo_client_init,
82 		.test_tick_f = test_echo_client_tick,
83 	},
84 	BSTEST_END_MARKER
85 };
86 
test_echo_client_install(struct bst_test_list * tests)87 struct bst_test_list *test_echo_client_install(struct bst_test_list *tests)
88 {
89 	tests = bst_add_tests(tests, test_echo_client);
90 	return tests;
91 }
92