1 /* main.c - Application main entry point */
2 
3 /*
4  * Copyright (c) 2015-2016 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <stddef.h>
10 
11 #include <zephyr/kernel.h>
12 
13 #include <zephyr/sys/printk.h>
14 #include <zephyr/sys/util.h>
15 
16 #include "bs_types.h"
17 #include "bs_tracing.h"
18 #include "time_machine.h"
19 #include "bstests.h"
20 
21 /* The test case is performing 250 simultaneous connections and managing
22  * parallel control procedures utilizing the available/configured minimum
23  * buffer counts. Hence, two iterations of connect-disconnect should be
24  * sufficient to catch any regressions/buffer leaks.
25  */
26 #define ITERATIONS 2
27 
28 int init_central(uint8_t max_conn, uint8_t iterations);
29 int init_peripheral(uint8_t max_conn, uint8_t iterations);
30 
31 #define FAIL(...)					\
32 	do {						\
33 		bst_result = Failed;			\
34 		bs_trace_error_time_line(__VA_ARGS__);	\
35 	} while (0)
36 
37 #define PASS(...)					\
38 	do {						\
39 		bst_result = Passed;			\
40 		bs_trace_info_time(1, __VA_ARGS__);	\
41 	} while (0)
42 
43 extern enum bst_result_t bst_result;
44 
test_central_main(void)45 static void test_central_main(void)
46 {
47 	int err;
48 
49 	err = init_central(CONFIG_BT_MAX_CONN, ITERATIONS);
50 	if (err) {
51 		goto exit;
52 	}
53 
54 	/* Wait a little so that peripheral side completes the last
55 	 * connection establishment.
56 	 */
57 	k_sleep(K_SECONDS(1));
58 
59 	PASS("Central tests passed\n");
60 
61 	return;
62 
63 exit:
64 	FAIL("Central tests failed (%d)\n", err);
65 	bs_trace_silent_exit(0);
66 }
67 
test_peripheral_main(void)68 static void test_peripheral_main(void)
69 {
70 	int err;
71 
72 	err = init_peripheral(CONFIG_BT_MAX_CONN, ITERATIONS);
73 	if (err) {
74 		goto exit;
75 	}
76 
77 	PASS("Peripheral tests passed\n");
78 
79 	return;
80 
81 exit:
82 	FAIL("Peripheral tests failed (%d)\n", err);
83 	bs_trace_silent_exit(0);
84 }
85 
test_central_multiple_main(void)86 static void test_central_multiple_main(void)
87 {
88 	int err;
89 
90 	err = init_central(20U, ITERATIONS);
91 	if (err) {
92 		goto exit;
93 	}
94 
95 	/* Wait a little so that peripheral side completes the last
96 	 * connection establishment.
97 	 */
98 	k_sleep(K_SECONDS(1));
99 
100 	PASS("Central tests passed\n");
101 
102 	return;
103 
104 exit:
105 	FAIL("Central tests failed (%d)\n", err);
106 	bs_trace_silent_exit(0);
107 }
108 
test_peripheral_single_main(void)109 static void test_peripheral_single_main(void)
110 {
111 	int err;
112 
113 	err = init_peripheral(1U, ITERATIONS);
114 	if (err) {
115 		goto exit;
116 	}
117 
118 	PASS("Peripheral tests passed\n");
119 
120 	return;
121 
122 exit:
123 	FAIL("Peripheral tests failed (%d)\n", err);
124 	bs_trace_silent_exit(0);
125 }
126 
test_central_single_main(void)127 static void test_central_single_main(void)
128 {
129 	int err;
130 
131 	err = init_central(1U, ITERATIONS);
132 	if (err) {
133 		goto exit;
134 	}
135 
136 	PASS("Central tests passed\n");
137 
138 	return;
139 
140 exit:
141 	FAIL("Central tests failed (%d)\n", err);
142 	bs_trace_silent_exit(0);
143 }
144 
test_peripheral_multilink_main(void)145 static void test_peripheral_multilink_main(void)
146 {
147 	int err;
148 
149 	err = init_peripheral(20U, ITERATIONS);
150 	if (err) {
151 		goto exit;
152 	}
153 
154 	k_sleep(K_SECONDS(3));
155 
156 	PASS("Peripheral tests passed\n");
157 
158 	return;
159 
160 exit:
161 	FAIL("Peripheral tests failed (%d)\n", err);
162 	bs_trace_silent_exit(0);
163 }
164 
test_multiple_init(void)165 static void test_multiple_init(void)
166 {
167 	bst_ticker_set_next_tick_absolute(2400e6);
168 	bst_result = In_progress;
169 }
170 
test_multiple_tick(bs_time_t HW_device_time)171 static void test_multiple_tick(bs_time_t HW_device_time)
172 {
173 	if (bst_result != Passed) {
174 		FAIL("Test timeout (not passed after %lu seconds)",
175 		     (unsigned long)(HW_device_time / USEC_PER_SEC));
176 	}
177 
178 	bs_trace_silent_exit(0);
179 }
180 
181 static const struct bst_test_instance test_def[] = {
182 	{
183 		.test_id = "central",
184 		.test_descr = "Central Multilink",
185 		.test_pre_init_f = test_multiple_init,
186 		.test_tick_f = test_multiple_tick,
187 		.test_main_f = test_central_main
188 	},
189 	{
190 		.test_id = "peripheral",
191 		.test_descr = "Peripheral multiple identity",
192 		.test_pre_init_f = test_multiple_init,
193 		.test_tick_f = test_multiple_tick,
194 		.test_main_f = test_peripheral_main
195 	},
196 	{
197 		.test_id = "central_multiple",
198 		.test_descr = "Single Central Multilink device",
199 		.test_pre_init_f = test_multiple_init,
200 		.test_tick_f = test_multiple_tick,
201 		.test_main_f = test_central_multiple_main
202 	},
203 	{
204 		.test_id = "peripheral_single",
205 		.test_descr = "Many Peripheral single link device",
206 		.test_pre_init_f = test_multiple_init,
207 		.test_tick_f = test_multiple_tick,
208 		.test_main_f = test_peripheral_single_main
209 	},
210 	{
211 		.test_id = "central_single",
212 		.test_descr = "Single Central device",
213 		.test_pre_init_f = test_multiple_init,
214 		.test_tick_f = test_multiple_tick,
215 		.test_main_f = test_central_single_main
216 	},
217 	{
218 		.test_id = "peripheral_multilink",
219 		.test_descr = "Peripheral multilink device",
220 		.test_pre_init_f = test_multiple_init,
221 		.test_tick_f = test_multiple_tick,
222 		.test_main_f = test_peripheral_multilink_main
223 	},
224 	BSTEST_END_MARKER
225 };
226 
test_multiple_install(struct bst_test_list * tests)227 struct bst_test_list *test_multiple_install(struct bst_test_list *tests)
228 {
229 	return bst_add_tests(tests, test_def);
230 }
231 
232 bst_test_install_t test_installers[] = {
233 	test_multiple_install,
234 	NULL
235 };
236 
main(void)237 int main(void)
238 {
239 	bst_main();
240 	return 0;
241 }
242