summaryrefslogtreecommitdiff
path: root/zephyr/test/tasks/main.c
blob: dc3df59ca769a23b475e2ee30393ed45381c45e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* Copyright 2020 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "ec_tasks.h"
#include "task.h"
#include "timer.h"

#include <stdbool.h>

#include <zephyr/kernel.h>
#include <zephyr/kernel/thread.h>
#include <zephyr/ztest.h>

/* Second for platform/ec task API (in microseconds). */
#define TASK_SEC(s) (s * 1000 * 1000)

K_SEM_DEFINE(task_done1, 0, 1);
K_SEM_DEFINE(task_done2, 0, 1);
K_SEM_DEFINE(test_ready1, 0, 1);
K_SEM_DEFINE(test_ready2, 0, 1);

static void (*task1)(void);
static void (*task2)(void);

static void run_test(void (*task1_run)(void), void (*task2_run)(void))
{
	task1 = task1_run;
	task2 = task2_run;
	k_sem_give(&test_ready1);
	k_sem_give(&test_ready2);
	k_sem_take(&task_done1, K_FOREVER);
	k_sem_take(&task_done2, K_FOREVER);
}

void task1_entry(void *p)
{
	while (1) {
		k_sem_take(&test_ready1, K_FOREVER);
		task1();
		k_sem_give(&task_done1);
	}
}

void task2_entry(void *p)
{
	while (1) {
		k_sem_take(&test_ready2, K_FOREVER);
		task2();
		k_sem_give(&task_done2);
	}
}

/*
 * Unlike Tasks 1 & 2, it is allowed to run Task 3 more than once per
 * call to run_test().  It will call task3_entry_func if set, and wait
 * for the next event.  This is useful to test things like timers,
 * which you are expecting the event to fire at some point in the
 * future, and you want to test that it happens.
 */
static void (*task3_entry_func)(uint32_t event_mask);

void task3_entry(void *p)
{
	uint32_t events = 0;

	for (;;) {
		if (task3_entry_func)
			task3_entry_func(events);
		events = task_wait_event(-1);
	}
}

static void set_event_before_task_start1(void)
{
	const uint32_t events = task_wait_event(TASK_SEC(2));

	zassert_equal(events, 0xAAAA, "Should have 0xAAAA events");
}

static void set_event_before_task_start2(void)
{
	/* Do nothing */
}

static void test_set_event_before_task_start(void)
{
	/* Send event before tasks start */
	task_set_event(TASK_ID_TASK_1, 0xAAAA);

	start_ec_tasks();

	run_test(set_event_before_task_start1, set_event_before_task_start2);
}

static void task_get_current1(void)
{
	zassert_equal(task_get_current(), TASK_ID_TASK_1, "ID matches");
}

static void task_get_current2(void)
{
	zassert_equal(task_get_current(), TASK_ID_TASK_2, "ID matches");
}

static void test_task_get_current(void)
{
	run_test(&task_get_current1, &task_get_current2);
}

static void timeout1(void)
{
	const uint32_t start_ms = k_uptime_get();
	const uint32_t events = task_wait_event(TASK_SEC(2));
	const uint32_t end_ms = k_uptime_get();

	zassert_equal(events, TASK_EVENT_TIMER, "Should have timeout event");
	zassert_within(end_ms - start_ms, 2000, 100, "Timeout for 2 seconds");
}

static void timeout2(void)
{
	/* Do nothing */
}

static void test_timeout(void)
{
	run_test(&timeout1, &timeout2);
}

/*
 * Timer test:
 *   1. Task 1 arms a timer for Task 3 in expiring 2 seconds.
 *   2. Task 2 does nothing.
 *   3. Task 3 validates that the it receives a TASK_EVENT_TIMER event
 *      2 seconds after Task 1 armed the timer (within 100ms
 *      tolerance).
 */
static timestamp_t timer_armed_at;
K_SEM_DEFINE(check_timer_finished, 0, 1);

static void check_timer(uint32_t event_mask)
{
	timestamp_t now = get_time();

	zassert_equal(event_mask & TASK_EVENT_TIMER, TASK_EVENT_TIMER,
		      "Timer event mask should be set");
	zassert_within(now.val - timer_armed_at.val, TASK_SEC(2),
		       TASK_SEC(1) / 10,
		       "Timer should expire at 2 seconds from arm time");
	k_sem_give(&check_timer_finished);
}

static void timer_task_1(void)
{
	timestamp_t timer_timeout;

	timer_armed_at = get_time();

	timer_timeout.val = timer_armed_at.val + TASK_SEC(2);

	task3_entry_func = check_timer;
	zassert_equal(timer_arm(timer_timeout, TASK_ID_TASK_3), EC_SUCCESS,
		      "Setting timer should succeed");
}

static void timer_task_2(void)
{
	/* Do nothing */
}

static void test_timer(void)
{
	run_test(timer_task_1, timer_task_2);
	zassert_equal(k_sem_take(&check_timer_finished, K_SECONDS(4 * 1000)), 0,
		      "Task 3 did not finish within timeout");
	zassert_equal(task3_entry_func, check_timer,
		      "check_timer should have been enabled");
	task3_entry_func = NULL;
}

static void event_delivered1(void)
{
	const uint32_t start_ms = k_uptime_get();
	const uint32_t events = task_wait_event(-1);
	const uint32_t end_ms = k_uptime_get();

	zassert_equal(events, 0x1234, "Verify event bits");
	zassert_within(end_ms - start_ms, 5000, 100, "Waited for 5 seconds");
}

static void event_delivered2(void)
{
	k_sleep(K_SECONDS(5));

	task_set_event(TASK_ID_TASK_1, 0x1234);
}

static void test_event_delivered(void)
{
	run_test(&event_delivered1, &event_delivered2);
}

static void event_mask_not_delivered1(void)
{
	task_set_event(TASK_ID_TASK_2, 0x007F);
}

static void event_mask_not_delivered2(void)
{
	const uint32_t start_ms = k_uptime_get();
	const uint32_t events = task_wait_event_mask(0x0080, TASK_SEC(7));
	const uint32_t end_ms = k_uptime_get();

	zassert_equal(events, TASK_EVENT_TIMER, "Should have timeout event");
	zassert_within(end_ms - start_ms, 7000, 100, "Timeout for 7 seconds");

	const uint32_t leftover_events = task_wait_event(0);

	zassert_equal(leftover_events, 0x007F, "All events should be waiting");
}

static void test_event_mask_not_delivered(void)
{
	run_test(&event_mask_not_delivered1, &event_mask_not_delivered2);
}

static void event_mask_extra1(void)
{
	k_sleep(K_SECONDS(1));

	task_set_event(TASK_ID_TASK_2, 0x00FF);
}

static void event_mask_extra2(void)
{
	const uint32_t start_ms = k_uptime_get();
	const uint32_t events = task_wait_event_mask(0x0001, TASK_SEC(10));
	const uint32_t end_ms = k_uptime_get();

	zassert_equal(events, 0x0001, "Verify only waited for event");
	zassert_within(end_ms - start_ms, 1000, 100, "Timeout for 1 second");

	const uint32_t leftover_events = task_wait_event(0);

	zassert_equal(leftover_events, 0x00FE, "All events should be waiting");
}

static void test_event_mask_extra(void)
{
	run_test(&event_mask_extra1, &event_mask_extra2);
}

static void empty_set_mask1(void)
{
	k_sleep(K_SECONDS(1));
	/*
	 * It is generally invalid to set a 0 event, but this simulates a race
	 * condition and exercises fallback code in task_wait_event
	 */
	task_set_event(TASK_ID_TASK_2, 0);
	k_sleep(K_SECONDS(1));
	task_set_event(TASK_ID_TASK_2, 0x1234);
}

static void empty_set_mask2(void)
{
	const uint32_t start_ms = k_uptime_get();
	const uint32_t events = task_wait_event_mask(0x1234, TASK_SEC(10));
	const uint32_t end_ms = k_uptime_get();

	zassert_equal(events, 0x1234, "Verify only waited for event");
	zassert_within(end_ms - start_ms, 2000, 100, "Timeout for 2 seconds");
}

static void check_task_1_mapping(void)
{
	zassert_equal(TASK_ID_TASK_1, thread_id_to_task_id(k_current_get()));
	zassert_equal(k_current_get(), task_id_to_thread_id(TASK_ID_TASK_1));
}

static void check_task_2_mapping(void)
{
	zassert_equal(TASK_ID_TASK_2, thread_id_to_task_id(k_current_get()));
	zassert_equal(k_current_get(), task_id_to_thread_id(TASK_ID_TASK_2));
}

static void test_thread_to_task_mapping(void)
{
	run_test(&check_task_1_mapping, &check_task_2_mapping);
}

static void test_empty_set_mask(void)
{
	run_test(&empty_set_mask1, &empty_set_mask2);
}

void test_main(void)
{
	/* Note that test_set_event_before_task_start calls start_ec_tasks */
	ztest_test_suite(test_task_shim,
			 ztest_unit_test(test_set_event_before_task_start),
			 ztest_unit_test(test_task_get_current),
			 ztest_unit_test(test_timeout),
			 ztest_unit_test(test_timer),
			 ztest_unit_test(test_event_delivered),
			 ztest_unit_test(test_event_mask_not_delivered),
			 ztest_unit_test(test_event_mask_extra),
			 ztest_unit_test(test_empty_set_mask),
			 ztest_unit_test(test_thread_to_task_mapping));
	ztest_run_test_suite(test_task_shim);
}