summaryrefslogtreecommitdiff
path: root/zephyr/test/hooks/hooks.c
blob: 0070f2e6b4f4ed8201dd1325b9694356d37e31df (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
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <stdbool.h>
#include <ztest.h>

#include "ap_power/ap_power.h"
#include "hooks.h"

static bool h1_called;
static bool h2_called;
static bool h3_called;

static void h1(void)
{
	zassert_false(h1_called, "h1 was called, but should not have been");
	zassert_false(h2_called, "h2 was called, but should not have been");
	zassert_false(h3_called, "h3 was called, but should not have been");
	h1_called = true;
}
DECLARE_HOOK(HOOK_TEST_1, h1, HOOK_PRIO_FIRST);

static void h2(void)
{
	zassert_true(h1_called, "h1 was not called, but should have been");
	zassert_false(h2_called, "h2 was called, but should not have been");
	zassert_false(h3_called, "h3 was called, but should not have been");
	h2_called = true;
}
DECLARE_HOOK(HOOK_TEST_1, h2, HOOK_PRIO_DEFAULT);

static void h3(void)
{
	zassert_true(h1_called, "h1 was not called, but should have been");
	zassert_true(h2_called, "h2 was not called, but should have been");
	zassert_false(h3_called, "h3 was called, but should not have been");
	h3_called = true;
}
DECLARE_HOOK(HOOK_TEST_1, h3, HOOK_PRIO_LAST);

static void test_hook_list_multiple(void)
{
	hook_notify(HOOK_TEST_1);
	zassert_true(h1_called, "h1 was not called, but should have been");
	zassert_true(h2_called, "h2 was not called, but should have been");
	zassert_true(h3_called, "h3 was not called, but should have been");
}

static bool h4_called;

static void h4(void)
{
	zassert_false(h4_called, "h4 was called, but should not have been");
	h4_called = true;
}
DECLARE_HOOK(HOOK_TEST_2, h4, HOOK_PRIO_DEFAULT);

static void test_hook_list_single(void)
{
	hook_notify(HOOK_TEST_2);
	zassert_true(h4_called, "h4 was not called, but should have been");
}

static void test_hook_list_empty(void)
{
	hook_notify(HOOK_TEST_3);
}

static bool deferred_func_called;

#define DEFERRED_DELAY_US (500 * 1000)
static void deferred_func(void)
{
	deferred_func_called = true;
}
DECLARE_DEFERRED(deferred_func);

static void test_deferred_func(void)
{
	zassert_false(
		deferred_func_called,
		"The deferred function was called, but should not have been");
	hook_call_deferred(&deferred_func_data, DEFERRED_DELAY_US);
	zassert_false(
		deferred_func_called,
		"The deferred function was called, but should not have been");
	k_usleep(DEFERRED_DELAY_US * 2);
	zassert_true(
		deferred_func_called,
		"The deferred function was not called, but should have been");
}

static bool deferred_func_2_called;

static void deferred_func_2(void)
{
	deferred_func_2_called = true;
}
DECLARE_DEFERRED(deferred_func_2);

/*
 * Test that repeated calls to hook_call_deferred result in the
 * function being pushed out.
 */
static void test_deferred_func_push_out(void)
{
	zassert_false(
		deferred_func_2_called,
		"The deferred function was called, but should not have been");
	hook_call_deferred(&deferred_func_2_data, DEFERRED_DELAY_US);
	hook_call_deferred(&deferred_func_2_data, DEFERRED_DELAY_US * 3);
	k_usleep(DEFERRED_DELAY_US * 2);
	zassert_false(
		deferred_func_2_called,
		"The deferred function was called, but should not have been");
	k_usleep(DEFERRED_DELAY_US * 2);
	zassert_true(
		deferred_func_called,
		"The deferred function was not called, but should have been");
}

static bool deferred_func_3_called;

static void deferred_func_3(void)
{
	deferred_func_3_called = true;
}
DECLARE_DEFERRED(deferred_func_3);

static void test_deferred_func_cancel(void)
{
	zassert_false(
		deferred_func_3_called,
		"The deferred function was called, but should not have been");
	hook_call_deferred(&deferred_func_3_data, DEFERRED_DELAY_US);
	hook_call_deferred(&deferred_func_3_data, -1);
	k_usleep(DEFERRED_DELAY_US * 2);
	zassert_false(
		deferred_func_3_called,
		"The deferred function was called, but should not have been");
}

/*
 * Structure passed to event listeners.
 */
struct events {
	struct ap_power_ev_callback cb;
	enum ap_power_events event;
	int count;
};

/*
 * Common handler.
 * Increment count, and store event received.
 */
static void ev_handler(struct ap_power_ev_callback *callback,
		       struct ap_power_ev_data data)
{
	struct events *ev = CONTAINER_OF(callback, struct events, cb);

	ev->count++;
	ev->event = data.event;
}

static void test_hook_ap_power_events(void)
{
	static struct events cb;

	ap_power_ev_init_callback(&cb.cb, ev_handler, AP_POWER_SUSPEND);
	ap_power_ev_add_callback(&cb.cb);
	hook_notify(HOOK_CHIPSET_SUSPEND);
	zassert_equal(1, cb.count, "Callback not called");
	zassert_equal(AP_POWER_SUSPEND, cb.event, "Wrong event");
	ap_power_ev_remove_callback(&cb.cb);
	hook_notify(HOOK_CHIPSET_SUSPEND);
	zassert_equal(1, cb.count, "Callback called");

	cb.count = 0;
	ap_power_ev_init_callback(&cb.cb, ev_handler,
		AP_POWER_SUSPEND|AP_POWER_RESUME);
	ap_power_ev_add_callback(&cb.cb);
	hook_notify(HOOK_CHIPSET_SUSPEND);
	zassert_equal(1, cb.count, "Callbacks not called");
	zassert_equal(AP_POWER_SUSPEND, cb.event, "Wrong event");
	hook_notify(HOOK_CHIPSET_RESUME);
	zassert_equal(2, cb.count, "Callbacks not called");
	zassert_equal(AP_POWER_RESUME, cb.event, "Wrong event");

	ap_power_ev_remove_events(&cb.cb, AP_POWER_SUSPEND);
	hook_notify(HOOK_CHIPSET_SUSPEND);
	zassert_equal(2, cb.count, "Suspend allback called");

	hook_notify(HOOK_CHIPSET_STARTUP);
	zassert_equal(2, cb.count, "Startup callback called");
	ap_power_ev_add_events(&cb.cb, AP_POWER_STARTUP);
	hook_notify(HOOK_CHIPSET_STARTUP);
	zassert_equal(3, cb.count, "Startup callback not called");
}

void test_main(void)
{
	ztest_test_suite(
		hooks_tests,
		ztest_unit_test(test_hook_list_multiple),
		ztest_unit_test(test_hook_list_single),
		ztest_unit_test(test_hook_list_empty),
		ztest_unit_test(test_deferred_func),
		ztest_unit_test(test_deferred_func_push_out),
		ztest_unit_test(test_deferred_func_cancel),
		ztest_unit_test(test_hook_ap_power_events));

	ztest_run_test_suite(hooks_tests);
}