summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/button/src/main.c
blob: c26a62dfa1a17f98979fc1b7950e52b8ca73a534 (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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* Copyright 2022 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <zephyr/fff.h>
#include <zephyr/kernel.h>
#include <zephyr/shell/shell.h>
#include <zephyr/ztest.h>

#include "button.h"
#include "console.h"
#include "hooks.h"
#include "mkbp_fifo.h"
#include "power.h"
#include "test/drivers/test_state.h"
#include "timer.h"

/*
 * TODO (b/b/253284635) Timeouts here don't quite align with the button press
 *   duration. This is caused by an issue with the Zephyr scheduling for delayed
 *   work that's causing us to need to sleep longer than "reasonable".
 */

FAKE_VOID_FUNC(chipset_reset, enum chipset_shutdown_reason);

static char *button_debug_state_strings[] = {
	"STATE_DEBUG_NONE", "STATE_DEBUG_CHECK",
	"STATE_STAGING",    "STATE_DEBUG_MODE_ACTIVE",
	"STATE_SYSRQ_PATH", "STATE_WARM_RESET_PATH",
	"STATE_SYSRQ_EXEC", "STATE_WARM_RESET_EXEC",
};

#define ASSERT_DEBUG_STATE(expected)                                          \
	do {                                                                  \
		enum debug_state state = get_button_debug_state();            \
		zassert_equal(expected, state,                                \
			      "Button debug state expected to be %d(%s),"     \
			      " but was %d(%s)",                              \
			      expected, button_debug_state_strings[expected], \
			      state, button_debug_state_strings[state]);      \
	} while (false)

struct button_fixture {
	timestamp_t fake_time;
};

static void *button_setup(void)
{
	static struct button_fixture fixture;

	/* Set the mock clock */
	get_time_mock = &fixture.fake_time;
	return &fixture;
}

static void button_before(void *f)
{
	((struct button_fixture *)f)->fake_time.val = 0;
	reset_button_debug_state();
	button_init();
	/* Sleep for 30s to flush any pending tasks */
	k_sleep(K_SECONDS(30));
	mkbp_clear_fifo();

	RESET_FAKE(chipset_reset);
}

ZTEST_SUITE(button, drivers_predicate_post_main, button_setup, button_before,
	    NULL, NULL);

static inline void pass_time(uint64_t duration_ms)
{
	get_time_mock->val += duration_ms * 1000;
	k_msleep(duration_ms);
}

ZTEST(button, test_press_one_button_no_change)
{
	/* Press the volume-up button for 1/2 a second */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vup 500"));

	/* Wait for the timeout */
	pass_time(11000);
	ASSERT_DEBUG_STATE(STATE_DEBUG_NONE);
}

ZTEST(button, test_press_vup_vdown_too_short)
{
	/* Press both volume-up and volume-down for 1/2 second */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vup 500"));
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vdown 500"));

	/* Let the deferred calls get run (800ms) */
	pass_time(800);
	ASSERT_DEBUG_STATE(STATE_DEBUG_CHECK);

	/* Wait for the timeout */
	pass_time(11000);
	ASSERT_DEBUG_STATE(STATE_DEBUG_NONE);
}

ZTEST(button, test_fail_check_button_released_too_soon)
{
	/* Press both volume-up and volume-down for 0.9 seconds */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vup 9000"));
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vdown 9000"));

	/* Let the deferred calls get run (800ms) */
	pass_time(800);
	ASSERT_DEBUG_STATE(STATE_DEBUG_CHECK);

	/* Wait for the timeout, should put us in staging */
	pass_time(11000);
	ASSERT_DEBUG_STATE(STATE_STAGING);

	/* Wait for the handler to be called and set us to ACTIVE mode */
	pass_time(7000);
	ASSERT_DEBUG_STATE(STATE_DEBUG_MODE_ACTIVE);

	/* Wait for the deadline to pass, putting us back in NONE */
	pass_time(11000);
	ASSERT_DEBUG_STATE(STATE_DEBUG_NONE);
}

ZTEST(button, test_fail_check_button_stuck)
{
	/* Press both volume-up and volume-down for 0.9 seconds */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vup 30000"));
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vdown 30000"));

	/* Let the deferred calls get run (800ms) */
	pass_time(800);
	ASSERT_DEBUG_STATE(STATE_DEBUG_CHECK);

	/* Wait for the timeout, should put us in staging */
	pass_time(11000);
	ASSERT_DEBUG_STATE(STATE_STAGING);

	/* Do a plain sleep to force the error condition of waking up the
	 * handler too early (since the time isn't moving forward).
	 */
	k_msleep(11000);

	/* Now sleep and move the clock forward to timeout the debug process */
	pass_time(11000);
	ASSERT_DEBUG_STATE(STATE_DEBUG_NONE);
}

ZTEST(button, test_activate_sysrq_path_then_timeout)
{
	/* Press both volume-up and volume-down for 1/2 second */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vup 10500"));
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vdown 10500"));

	/* Let the deferred calls get run (800ms) */
	pass_time(800);
	ASSERT_DEBUG_STATE(STATE_DEBUG_CHECK);

	/* Wait for the buttons to be released */
	pass_time(11000);
	ASSERT_DEBUG_STATE(STATE_STAGING);

	/* Wait a bit and check that we activated debug mode */
	pass_time(11000);
	ASSERT_DEBUG_STATE(STATE_DEBUG_MODE_ACTIVE);

	/* Press volume up button to put in sysrq_path */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vup 500"));
	pass_time(800);
	ASSERT_DEBUG_STATE(STATE_STAGING);

	/* Wait for timeout and go into sysrq_path */
	pass_time(500);
	ASSERT_DEBUG_STATE(STATE_SYSRQ_PATH);

	/* Now sleep and move the clock forward to timeout the debug process */
	pass_time(11000);
	ASSERT_DEBUG_STATE(STATE_DEBUG_NONE);
}

ZTEST(button, test_activate_sysrq_path_4_times)
{
	/* Press both volume-up and volume-down for 1/2 second */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vup 10500"));
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vdown 10500"));

	/* Let the deferred calls get run (800ms) */
	pass_time(800);
	ASSERT_DEBUG_STATE(STATE_DEBUG_CHECK);

	/* Wait for the buttons to be released */
	pass_time(11000);
	ASSERT_DEBUG_STATE(STATE_STAGING);

	/* Wait a bit and check that we activated debug mode */
	pass_time(11000);
	ASSERT_DEBUG_STATE(STATE_DEBUG_MODE_ACTIVE);

	/* Press volume up button to put in sysrq_path */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vup 500"));
	pass_time(800);
	ASSERT_DEBUG_STATE(STATE_STAGING);

	/* Wait for timeout and go into sysrq_path */
	pass_time(500);
	ASSERT_DEBUG_STATE(STATE_SYSRQ_PATH);

	/* Press vup again (#2) */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vup 500"));
	pass_time(800);
	pass_time(500);

	/* Press vup again (#3) */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vup 500"));
	pass_time(800);
	pass_time(500);

	/* Press vup again (#4) */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vup 500"));
	pass_time(800);
	pass_time(500);
	ASSERT_DEBUG_STATE(STATE_DEBUG_NONE);
}

ZTEST(button, test_activate_sysrq_exec)
{
	uint32_t event_data = 0;

	/* Press both volume-up and volume-down for 1/2 second */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vup 10500"));
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vdown 10500"));

	/* Let the deferred calls get run (800ms) */
	pass_time(800);
	ASSERT_DEBUG_STATE(STATE_DEBUG_CHECK);

	/* Wait for the buttons to be released */
	pass_time(11000);
	ASSERT_DEBUG_STATE(STATE_STAGING);

	/* Wait a bit and check that we activated debug mode */
	pass_time(11000);
	ASSERT_DEBUG_STATE(STATE_DEBUG_MODE_ACTIVE);

	/* Press volume up button to put in sysrq_path */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vup 500"));
	pass_time(800);
	ASSERT_DEBUG_STATE(STATE_STAGING);

	/* Wait for timeout and go into sysrq_path */
	pass_time(500);
	ASSERT_DEBUG_STATE(STATE_SYSRQ_PATH);

	/* Now sleep and move the clock forward to timeout the debug process */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vdown 500"));
	pass_time(800);
	pass_time(500);
	ASSERT_DEBUG_STATE(STATE_DEBUG_NONE);

	/* Flush all the button events */
	while (mkbp_fifo_get_next_event((uint8_t *)&event_data,
					EC_MKBP_EVENT_BUTTON) > 0)
		;

	/* Check for sysrq event */
	zassert_equal(4, mkbp_fifo_get_next_event((uint8_t *)&event_data,
						  EC_MKBP_EVENT_SYSRQ));
	zassert_equal((uint32_t)'x', event_data);
}

ZTEST(button, test_activate_warm_reset_then_timeout)
{
	/* Press both volume-up and volume-down for 1/2 second */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vup 10500"));
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vdown 10500"));

	/* Let the deferred calls get run (800ms) */
	pass_time(800);
	ASSERT_DEBUG_STATE(STATE_DEBUG_CHECK);

	/* Wait for the buttons to be released */
	pass_time(11000);
	ASSERT_DEBUG_STATE(STATE_STAGING);

	/* Wait a bit and check that we activated debug mode */
	pass_time(11000);
	ASSERT_DEBUG_STATE(STATE_DEBUG_MODE_ACTIVE);

	/* Press volume down button to put in warm_reset_path */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vdown 500"));
	pass_time(800);
	ASSERT_DEBUG_STATE(STATE_STAGING);

	/* Wait for timeout and go into warm_reset_path */
	pass_time(500);
	ASSERT_DEBUG_STATE(STATE_WARM_RESET_PATH);

	/* Now sleep and move the clock forward to timeout the debug process */
	pass_time(11000);
	ASSERT_DEBUG_STATE(STATE_DEBUG_NONE);
}

ZTEST(button, test_activate_warm_reset_exec)
{
	/* Press both volume-up and volume-down for 1/2 second */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vup 10500"));
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vdown 10500"));

	/* Let the deferred calls get run (800ms) */
	pass_time(800);
	ASSERT_DEBUG_STATE(STATE_DEBUG_CHECK);

	/* Wait for the buttons to be released */
	pass_time(11000);
	ASSERT_DEBUG_STATE(STATE_STAGING);

	/* Wait a bit and check that we activated debug mode */
	pass_time(11000);
	ASSERT_DEBUG_STATE(STATE_DEBUG_MODE_ACTIVE);

	/* Press volume down button to put in warm_reset_path */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vdown 500"));
	pass_time(800);
	ASSERT_DEBUG_STATE(STATE_STAGING);

	/* Wait for timeout and go into warm_reset_path */
	pass_time(500);
	ASSERT_DEBUG_STATE(STATE_WARM_RESET_PATH);

	/* Now sleep and move the clock forward to timeout the debug process.
	 * Doing this in two steps verifies that even after the handler executes
	 * "too early" we can still recover via the vup button that's coming
	 * next. This is caused by effectively, sleeping so the scheduler runs,
	 * but not ticking the clock forward yet until the next sleep.
	 */
	k_msleep(11000);
	pass_time(11000);

	zassert_ok(shell_execute_cmd(get_ec_shell(), "button vup 500"));
	pass_time(800);
	ASSERT_DEBUG_STATE(STATE_STAGING);

	pass_time(11000);
	ASSERT_DEBUG_STATE(STATE_DEBUG_NONE);
	zassert_equal(1, chipset_reset_fake.call_count);
	zassert_equal(CHIPSET_RESET_DBG_WARM_REBOOT,
		      chipset_reset_fake.arg0_val);
}