summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/keyboard_scan/src/keyboard_backlight.c
blob: 2865d7efc347a55c36db19e1ace730fe2dce9cf7 (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 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 "console.h"
#include "host_command.h"
#include "keyboard_backlight.h"
#include "pwm_mock.h"
#include "test/drivers/test_state.h"

#include <stdint.h>
#include <string.h>

#include <zephyr/drivers/pwm.h>
#include <zephyr/kernel.h>
#include <zephyr/shell/shell_dummy.h>
#include <zephyr/ztest.h>
#include <zephyr/ztest_assert.h>

#define KBLIGHT_PWM_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(cros_ec_kblight_pwm)

extern const struct kblight_drv kblight_pwm;

/**
 * @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;

	zassert_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;

	zassert_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)
{
	zassert_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);
}

ZTEST(keyboard_backlight, set_backlight__device_not_ready)
{
	const struct pwm_dt_spec kblight_pwm_dt =
		PWM_DT_SPEC_GET(KBLIGHT_PWM_NODE);
	const struct device *pwm_dev = kblight_pwm_dt.dev;
	int initial_duty;
	int initialized_saved;

	initial_duty = pwm_mock_get_duty(pwm_dev, kblight_pwm_dt.channel);

	initialized_saved = pwm_dev->state->initialized;
	pwm_dev->state->initialized = 0;

	zassert_ok(kblight_pwm.set(initial_duty + 10), NULL);
	zassert_equal(initial_duty,
		      pwm_mock_get_duty(pwm_dev, kblight_pwm_dt.channel), NULL);

	pwm_dev->state->initialized = initialized_saved;
}

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);