summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/dps/src/dps.c
blob: d445767df45aa8b47545e31d683ddcad44daa051 (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
/* 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/ztest.h>

#include "builtin/stdio.h"
#include "console.h"
#include "dps.h"
#include "test/drivers/test_state.h"
#include "timer.h"

struct dps_fixture {
	struct dps_config_t saved_config;
	int saved_debug_level;
};

static void *dps_config_setup(void)
{
	static struct dps_fixture fixture;

	fixture.saved_config = *dps_get_config();
	fixture.saved_debug_level = *dps_get_debug_level();

	return &fixture;
}

static void dps_config_before(void *data)
{
	dps_enable(true);
}

static void dps_config_after(void *data)
{
	struct dps_fixture *f = (struct dps_fixture *)data;

	*dps_get_config() = f->saved_config;
	*dps_get_debug_level() = f->saved_debug_level;
	dps_enable(true);
}

ZTEST_F(dps, test_enable)
{
	zassert_true(dps_is_enabled(), NULL);
	dps_enable(false);
	zassert_false(dps_is_enabled(), NULL);
	dps_enable(true);
	zassert_true(dps_is_enabled(), NULL);
}

ZTEST_F(dps, test_config)
{
	struct dps_config_t *config = dps_get_config();

	zassert_true(config->k_less_pwr <= config->k_more_pwr, NULL);
	zassert_true(config->k_less_pwr > 0 && config->k_less_pwr < 100, NULL);
	zassert_true(config->k_more_pwr > 0 && config->k_more_pwr < 100, NULL);

	zassert_ok(dps_init(), NULL);
	*config = fixture->saved_config;

	config->k_less_pwr = config->k_more_pwr + 1;
	zassert_equal(dps_init(), EC_ERROR_INVALID_CONFIG, NULL);
	*config = fixture->saved_config;

	config->k_more_pwr = 101;
	zassert_equal(dps_init(), EC_ERROR_INVALID_CONFIG, NULL);
	*config = fixture->saved_config;
}

ZTEST(dps, console_cmd__print_info)
{
	/* Print current status to console */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "dps"), NULL);
}

ZTEST(dps, console_cmd__enable)
{
	/* Disable DPS first, then try enabling */
	dps_enable(false);
	zassert_false(dps_is_enabled(), NULL);

	zassert_ok(shell_execute_cmd(get_ec_shell(), "dps en"), NULL);

	zassert_true(dps_is_enabled(), NULL);
}

ZTEST(dps, console_cmd__disable)
{
	/* Should already by enabled due to before() function */
	zassume_true(dps_is_enabled(), NULL);

	zassert_ok(shell_execute_cmd(get_ec_shell(), "dps dis"), NULL);

	zassert_false(dps_is_enabled(), NULL);
}

ZTEST(dps, console_cmd__fakepwr_print)
{
	/* Print current fake power status to console */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "dps fakepwr"), NULL);
}

ZTEST(dps, console_cmd__fakepwr_enable_disable)
{
	zassume_false(dps_is_fake_enabled(),
		      "fakepwr shouldn't be enabled by default");

	zassert_ok(shell_execute_cmd(get_ec_shell(), "dps fakepwr 100 200"),
		   NULL);
	zassert_true(dps_is_fake_enabled(), NULL);
	zassert_equal(100, dps_get_fake_mv(), "Got fake_mv=%d",
		      dps_get_fake_mv());
	zassert_equal(200, dps_get_fake_ma(), "Got fake_ma=%d",
		      dps_get_fake_ma());

	zassert_ok(shell_execute_cmd(get_ec_shell(), "dps fakepwr dis"), NULL);
	zassert_false(dps_is_fake_enabled(), NULL);
}

ZTEST(dps, console_cmd__fakepwr_invalid)
{
	/* Various invalid parameters */
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps fakepwr 100"), NULL);
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps fakepwr -100 -200"),
		   NULL);
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps fakepwr 100 -200"),
		   NULL);
}

ZTEST(dps, console_cmd__debuglevel)
{
	zassert_ok(shell_execute_cmd(get_ec_shell(), "dps debug 999"), NULL);

	zassert_equal(999, *dps_get_debug_level(), "Debug level is %d",
		      *dps_get_debug_level());
}

ZTEST(dps, console_cmd__setkmore)
{
	struct dps_config_t *config = dps_get_config();
	char cmd[32];

	/* Try some invalid requests first */
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkmore"), NULL);
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkmore 101"),
		   NULL);
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkmore 0"), NULL);
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkmore -1"), NULL);

	zassert_true(crec_snprintf(cmd, sizeof(cmd), "dps setkmore %d",
				   config->k_less_pwr - 1) > 0,
		     NULL);
	zassert_ok(!shell_execute_cmd(get_ec_shell(), cmd), NULL);

	/* Adjust k_more_pwr to be one over k_less_pwr */
	zassert_true(crec_snprintf(cmd, sizeof(cmd), "dps setkmore %d",
				   config->k_less_pwr + 1) > 0,
		     NULL);
	zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL);

	zassert_equal(config->k_less_pwr + 1, config->k_more_pwr,
		      "k_more_pwr is %d but should be %d", config->k_more_pwr,
		      config->k_less_pwr + 1);
}

ZTEST(dps, console_cmd__setkless)
{
	struct dps_config_t *config = dps_get_config();
	char cmd[32];

	/* Try some invalid requests first */
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkless"), NULL);
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkless 101"),
		   NULL);
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkless 0"), NULL);
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkless -1"), NULL);

	zassert_true(crec_snprintf(cmd, sizeof(cmd), "dps setkless %d",
				   config->k_more_pwr + 1) > 0,
		     NULL);
	zassert_ok(!shell_execute_cmd(get_ec_shell(), cmd), NULL);

	/* Adjust k_less_pwr to be one under k_more_pwr */
	zassert_true(crec_snprintf(cmd, sizeof(cmd), "dps setkless %d",
				   config->k_more_pwr - 1) > 0,
		     NULL);
	zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL);

	zassert_equal(config->k_more_pwr - 1, config->k_less_pwr,
		      "k_less_pwr is %d but should be %d", config->k_less_pwr,
		      config->k_more_pwr - 1);
}

ZTEST(dps, console_cmd__setksample)
{
	struct dps_config_t *config = dps_get_config();

	/* Try some invalid requests first */
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setksample"), NULL);
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setksample -1"),
		   NULL);

	zassert_ok(shell_execute_cmd(get_ec_shell(), "dps setksample 999"),
		   NULL);

	zassert_equal(999, config->k_sample, "k_sample is %d",
		      config->k_sample);
}

ZTEST(dps, console_cmd__setkwindow)
{
	struct dps_config_t *config = dps_get_config();

	/* Try some invalid requests first */
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkwin"), NULL);
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps setkwin -1"), NULL);

	zassert_ok(shell_execute_cmd(get_ec_shell(), "dps setkwin 4"), NULL);

	zassert_equal(4, config->k_window, "k_window is %d", config->k_window);
}

ZTEST(dps, console_cmd__settcheck)
{
	struct dps_config_t *config = dps_get_config();

	/* Try some invalid requests first */
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps settcheck"), NULL);
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps settcheck -1"),
		   NULL);

	zassert_ok(shell_execute_cmd(get_ec_shell(), "dps settcheck 5"), NULL);

	zassert_equal(5 * SECOND, config->t_check, "t_check is %d",
		      config->t_check);
}

ZTEST(dps, console_cmd__settstable)
{
	struct dps_config_t *config = dps_get_config();

	/* Try some invalid requests first */
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps settstable"), NULL);
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps settstable -1"),
		   NULL);

	zassert_ok(shell_execute_cmd(get_ec_shell(), "dps settstable 6"), NULL);

	zassert_equal(6 * SECOND, config->t_stable, "t_stable is %d",
		      config->t_stable);
}

ZTEST(dps, console_cmd__invalid)
{
	/* Non-existent subcommand should fail */
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "dps foobar xyz"), NULL);
}

ZTEST_SUITE(dps, drivers_predicate_pre_main, dps_config_setup,
	    dps_config_before, dps_config_after, NULL);