summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/led_driver/src/led_common.c
blob: 2b113499a05868d089182a3d9860e866479c8165 (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
/* 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 "host_command.h"
#include "led_common.h"
#include "test/drivers/test_state.h"

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

ZTEST(led_common, host_command__query)
{
	/* Gets the brightness range for an LED */

	int ret;
	struct ec_response_led_control response;
	struct ec_params_led_control params = {
		.led_id = EC_LED_ID_RIGHT_LED,
		.flags = EC_LED_FLAGS_QUERY,
	};

	/* Expected brightness levels per color channel for this LED */
	uint8_t expected_brightness_ranges[] = {
		[EC_LED_COLOR_RED] = 0,	  [EC_LED_COLOR_GREEN] = 0,
		[EC_LED_COLOR_BLUE] = 1,  [EC_LED_COLOR_YELLOW] = 0,
		[EC_LED_COLOR_WHITE] = 1, [EC_LED_COLOR_AMBER] = 0,
	};

	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND(EC_CMD_LED_CONTROL, 1, response, params);

	ret = host_command_process(&args);

	zassert_ok(ret, "Host command returned %d", ret);
	zassert_mem_equal(expected_brightness_ranges, response.brightness_range,
			  sizeof(expected_brightness_ranges), NULL);
}

ZTEST(led_common, host_command__invalid_led)
{
	/* Try accessing info on a non-existent LED */

	int ret;
	struct ec_response_led_control response;
	struct ec_params_led_control params = {
		.led_id = EC_LED_ID_COUNT, /* Non-existent */
		.flags = EC_LED_FLAGS_QUERY,
	};

	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND(EC_CMD_LED_CONTROL, 1, response, params);

	ret = host_command_process(&args);

	zassert_equal(EC_RES_INVALID_PARAM, ret, "Host command returned %d",
		      ret);
}

ZTEST(led_common, host_command__supported_channel)
{
	/* Try setting brightness on a color channel that is not supported */

	int ret;
	struct ec_response_led_control response;
	struct ec_params_led_control params = {
		.led_id = EC_LED_ID_RIGHT_LED,
		.flags = 0x00,
		.brightness = {
			/* This LED does not have a red channel */
			[EC_LED_COLOR_RED] = 100,
		},
	};

	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND(EC_CMD_LED_CONTROL, 1, response, params);

	ret = host_command_process(&args);

	zassert_equal(EC_RES_INVALID_PARAM, ret, "Host command returned %d",
		      ret);
}

ZTEST(led_common, host_command__manual_control)
{
	/* Set brightness for an LED directly */

	int ret;
	struct ec_response_led_control response;
	struct ec_params_led_control params = {
		.led_id = EC_LED_ID_RIGHT_LED,
		.flags = 0x00,
		.brightness = {
			[EC_LED_COLOR_BLUE] = 1,
			/* All other color channels off */
		},
	};

	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND(EC_CMD_LED_CONTROL, 1, response, params);

	ret = host_command_process(&args);

	zassert_equal(EC_RES_SUCCESS, ret, "Host command returned %d", ret);
	zassert_true(
		gpio_pin_get_dt(GPIO_DT_FROM_NODELABEL(gpio_ec_chg_led_y_c0)),
		"LED blue channel is not on");
	zassert_false(
		gpio_pin_get_dt(GPIO_DT_FROM_NODELABEL(gpio_ec_chg_led_w_c0)),
		"LED white channel is not off");
}

FAKE_VOID_FUNC(board_led_auto_control);

ZTEST(led_common, host_command__auto_control)
{
	/* Configure an LED for automatic control */

	int ret;
	struct ec_response_led_control response;
	struct ec_params_led_control params = {
		.led_id = EC_LED_ID_RIGHT_LED,
		.flags = EC_LED_FLAGS_AUTO,
	};

	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND(EC_CMD_LED_CONTROL, 1, response, params);

	ret = host_command_process(&args);

	zassert_equal(EC_RES_SUCCESS, ret, "Host command returned %d", ret);
	zassert_equal(1, board_led_auto_control_fake.call_count,
		      "Did not call auto control function.");
}

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

	RESET_FAKE(board_led_auto_control);
}

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