summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/keyboard_scan/src/keyboard_backlight.c
blob: 149f25dfddb701e5090eb1f4a8f9c6c0dcc0988b (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
/* 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 <stdint.h>
#include <string.h>
#include <zephyr/ztest.h>
#include <zephyr/kernel.h>
#include <zephyr/shell/shell_dummy.h>
#include <zephyr/ztest_assert.h>

#include "console.h"
#include "host_command.h"
#include "keyboard_backlight.h"
#include "test/drivers/test_state.h"

/**
 * @brief Send host command to set the backlight percentage
 *
 * @param percent Backlight intensity, from 0 to 100 (inclusive).
 * @return uint16_t Host command return code
 */
static uint16_t set_backlight_percent_helper(uint8_t percent)
{
	struct ec_params_pwm_set_keyboard_backlight params = {
		.percent = percent
	};
	struct host_cmd_handler_args args = BUILD_HOST_COMMAND_PARAMS(
		EC_CMD_PWM_SET_KEYBOARD_BACKLIGHT, 0, params);

	return host_command_process(&args);
}

ZTEST(keyboard_backlight, host_command_set_backlight__normal)
{
	/* Set the backlight intensity level to this and verify */
	uint8_t expected_percentage = 50;

	zassert_ok(set_backlight_percent_helper(expected_percentage), NULL);
	zassert_equal(expected_percentage, kblight_get(), NULL);
}

ZTEST(keyboard_backlight, host_command_set_backlight__out_of_range)
{
	/* Too high */
	uint8_t expected_percentage = 101;

	zassert_equal(EC_RES_ERROR,
		      set_backlight_percent_helper(expected_percentage), NULL);
}

ZTEST(keyboard_backlight, host_command_get_backlight__normal)
{
	/* Set this backlight intensity and verify via host command */
	uint8_t expected_percentage = 50;
	int ret;

	zassume_ok(set_backlight_percent_helper(expected_percentage), NULL);

	/* Brief delay to allow a deferred function to enable the backlight */
	k_sleep(K_MSEC(50));

	struct ec_response_pwm_get_keyboard_backlight response;
	struct host_cmd_handler_args args = BUILD_HOST_COMMAND_RESPONSE(
		EC_CMD_PWM_GET_KEYBOARD_BACKLIGHT, 0, response);

	ret = host_command_process(&args);
	zassert_ok(ret, "Host command failed: %d", ret);
	zassert_equal(expected_percentage, response.percent, NULL);
	zassert_equal(1, response.enabled, "Got 0x%02x", response.enabled);
}

ZTEST(keyboard_backlight, console_command__noargs)
{
	/* Command should print current status. Set backlight on and to 70% */

	const char *outbuffer;
	size_t buffer_size;

	zassume_ok(set_backlight_percent_helper(70), NULL);
	k_sleep(K_MSEC(50));

	/* With no args, print current state */
	shell_backend_dummy_clear_output(get_ec_shell());
	zassert_ok(shell_execute_cmd(get_ec_shell(), "kblight"), NULL);
	outbuffer =
		shell_backend_dummy_get_output(get_ec_shell(), &buffer_size);

	zassert_ok(!strstr(outbuffer, "Keyboard backlight: 70% enabled: 1"),
		   "Actual string: `%s`", outbuffer);
}

ZTEST(keyboard_backlight, console_command__set_on)
{
	/* Command should enable backlight to given intensity */

	zassert_ok(shell_execute_cmd(get_ec_shell(), "kblight 65"), NULL);
	zassert_equal(65, kblight_get(), NULL);
	zassert_equal(1, kblight_get_current_enable(), NULL);
}

ZTEST(keyboard_backlight, console_command__set_off)
{
	zassume_ok(set_backlight_percent_helper(40), NULL);
	k_sleep(K_MSEC(50));

	/* Turn back off */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "kblight 0"), NULL);
	zassert_equal(0, kblight_get(), NULL);
	zassert_equal(0, kblight_get_current_enable(), NULL);
}

ZTEST(keyboard_backlight, console_command__bad_params)
{
	zassert_equal(EC_ERROR_PARAM1,
		      shell_execute_cmd(get_ec_shell(), "kblight NaN"), NULL);
	zassert_equal(EC_ERROR_PARAM1,
		      shell_execute_cmd(get_ec_shell(), "kblight -1"), NULL);
	zassert_equal(EC_ERROR_PARAM1,
		      shell_execute_cmd(get_ec_shell(), "kblight 101"), NULL);
}

static void reset(void *data)
{
	ARG_UNUSED(data);

	/* Reset the backlight to off and 0% brightness */
	kblight_set(0);
	kblight_enable(0);
}

ZTEST_SUITE(keyboard_backlight, drivers_predicate_post_main, NULL, reset, reset,
	    NULL);