summaryrefslogtreecommitdiff
path: root/zephyr/test/tasks/extra_tasks.c
blob: da89eac3165a148bbc01dadd74bc6a9e904a8812 (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
/* Copyright 2023 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 "host_command.h"
#include "task.h"
#include "zephyr_console_shim.h"

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

k_tid_t get_sysworkq_thread(void);
k_tid_t get_idle_thread(void);

/* Utilities for finding a Zephyr thread by name */
static k_tid_t found_thread;
static void find_thread_by_name_cb(const struct k_thread *thread,
				   void *user_data)
{
	const char *name = (const char *)user_data;

	if (strcmp(k_thread_name_get((k_tid_t)thread), name) == 0) {
		found_thread = (k_tid_t)thread;
	}
}

static k_tid_t find_thread_by_name(const char *name)
{
	found_thread = NULL;
	k_thread_foreach_unlocked(find_thread_by_name_cb, (void *)name);
	return found_thread;
}

/* Utilities for checking asserts */
static bool expect_assert;
static int num_asserts;
void assert_post_action(const char *file, unsigned int line)
{
	num_asserts += 1;
	if (!expect_assert) {
		ztest_test_fail();
	}
}

#define EXPECT_ASSERT(test)                    \
	do {                                   \
		expect_assert = true;          \
		num_asserts = 0;               \
		(test);                        \
		expect_assert = false;         \
		zassert_equal(num_asserts, 1); \
	} while (0)

ZTEST_USER(extra_tasks, test_hostcmd_thread_mapping)
{
	k_tid_t hostcmd_thread;
	k_tid_t main_thread;

#ifdef HAS_TASK_HOSTCMD
#ifdef CONFIG_TASK_HOSTCMD_THREAD_MAIN
	k_thread_name_set(get_main_thread(), "HOSTCMD");
#endif /* CONFIG_TASK_HOSTCMD_THREAD_MAIN */

	hostcmd_thread = find_thread_by_name("HOSTCMD");
	zassert_not_null(hostcmd_thread);
	zassert_equal(hostcmd_thread, get_hostcmd_thread());
	zassert_equal(TASK_ID_HOSTCMD, thread_id_to_task_id(hostcmd_thread));
	zassert_equal(task_id_to_thread_id(TASK_ID_HOSTCMD), hostcmd_thread);

#ifdef CONFIG_TASK_HOSTCMD_THREAD_DEDICATED
	main_thread = find_thread_by_name("main");
	zassert_not_null(main_thread);
	zassert_equal(main_thread, get_main_thread());
	zassert_not_equal(main_thread, hostcmd_thread);
	zassert_equal(TASK_ID_MAIN, thread_id_to_task_id(main_thread));
	zassert_equal(task_id_to_thread_id(TASK_ID_MAIN), main_thread);
#else
	main_thread = get_main_thread();
	zassert_not_null(main_thread);
	zassert_equal(main_thread, hostcmd_thread);
#endif /* CONFIG_TASK_HOSTCMD_THREAD_DEDICATED */

#else /* !HAS_TASK_HOSTCMD */
	hostcmd_thread = find_thread_by_name("HOSTCMD");
	zassert_is_null(hostcmd_thread);
	EXPECT_ASSERT(hostcmd_thread = get_hostcmd_thread());
	zassert_is_null(hostcmd_thread);

	main_thread = find_thread_by_name("main");
	zassert_not_null(main_thread);
	zassert_equal(main_thread, get_main_thread());
#endif /* HAS_TASK_HOSTCMD */
}

ZTEST_USER(extra_tasks, test_sysworkq_thread_mapping)
{
	k_tid_t sysworkq_thread;

	sysworkq_thread = find_thread_by_name("sysworkq");
	zassert_not_null(sysworkq_thread);
	zassert_equal(sysworkq_thread, get_sysworkq_thread());
	zassert_equal(TASK_ID_SYSWORKQ, thread_id_to_task_id(sysworkq_thread));
	zassert_equal(task_id_to_thread_id(TASK_ID_SYSWORKQ), sysworkq_thread);
}

ZTEST_USER(extra_tasks, test_idle_thread_mapping)
{
	k_tid_t idle_thread;

	idle_thread = find_thread_by_name("idle");
	zassert_not_null(idle_thread);
	zassert_equal(idle_thread, get_idle_thread());
	zassert_equal(TASK_ID_IDLE, thread_id_to_task_id(idle_thread));
	zassert_equal(task_id_to_thread_id(TASK_ID_IDLE), idle_thread);
}

ZTEST_USER(extra_tasks, test_shell_thread_to_task_mapping)
{
	k_tid_t shell_thread;

	shell_thread = find_thread_by_name("shell_uart");
	zassert_not_null(shell_thread);
	zassert_equal(shell_thread, get_shell_thread());
	zassert_equal(TASK_ID_SHELL, thread_id_to_task_id(shell_thread));
	zassert_equal(task_id_to_thread_id(TASK_ID_SHELL), shell_thread);
}

ZTEST_USER(extra_tasks, test_invalid_task_id)
{
	k_tid_t thread_id;

	EXPECT_ASSERT(thread_id = task_id_to_thread_id(TASK_ID_INVALID));
	zassert_is_null(thread_id);

	EXPECT_ASSERT(thread_id = task_id_to_thread_id(-1));
	zassert_is_null(thread_id);
}

ZTEST_USER(extra_tasks, test_invalid_thread_id)
{
	task_id_t task_id;

	EXPECT_ASSERT(task_id = thread_id_to_task_id(NULL));
	zassert_equal(task_id, TASK_ID_INVALID);

	EXPECT_ASSERT(task_id = thread_id_to_task_id((k_tid_t)0x1234));
	zassert_equal(task_id, TASK_ID_INVALID);
}

ZTEST_USER(extra_tasks, test_extra_task_enumeration)
{
	for (task_id_t task_id = 0; task_id < TASK_ID_COUNT + EXTRA_TASK_COUNT;
	     task_id++) {
		zassert_not_null(task_id_to_thread_id(task_id));
	}
}

ZTEST_SUITE(extra_tasks, NULL, NULL, NULL, NULL, NULL);