summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/host_cmd/src/usb_pd_host_cmd.c
blob: 3afc90760ad9fe7e18c0e2ca6fc1bdaa7fbad15a (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
/* 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/kernel.h>
#include <zephyr/ztest.h>

#include "ec_commands.h"
#include "host_command.h"
#include "test/drivers/test_state.h"
#include "usb_pd.h"

ZTEST_USER(usb_pd_host_cmd, test_hc_pd_host_event_status)
{
	struct ec_response_host_event_status response;
	struct host_cmd_handler_args args = BUILD_HOST_COMMAND_RESPONSE(
		EC_CMD_PD_HOST_EVENT_STATUS, 0, response);

	/* Clear events */
	zassert_ok(host_command_process(&args));

	/* Send arbitrary event */
	pd_send_host_event(1);

	zassert_ok(host_command_process(&args));
	zassert_equal(args.response_size, sizeof(response));
	zassert_true(response.status & 1);

	/* Send again to make sure the host command cleared the event */
	zassert_ok(host_command_process(&args));
	zassert_equal(args.response_size, sizeof(response));
	zassert_equal(response.status, 0);
}

static struct ec_params_usb_pd_rw_hash_entry *
test_find_hc_remote_hash_entry(int dev_id)
{
	for (int i = 0; i < RW_HASH_ENTRIES; i++) {
		if (rw_hash_table[i].dev_id == dev_id) {
			return &rw_hash_table[i];
		}
	}

	return NULL;
}

ZTEST_USER(usb_pd_host_cmd, test_hc_remote_hash_entry__bad_dev_id)
{
	struct ec_params_usb_pd_rw_hash_entry params = {
		/* Dev ID can't be 0 */
		.dev_id = 0,
	};
	struct host_cmd_handler_args args = BUILD_HOST_COMMAND_PARAMS(
		EC_CMD_USB_PD_RW_HASH_ENTRY, 0, params);

	zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM);
}

ZTEST_USER(usb_pd_host_cmd, test_hc_remote_hash_entry__add_entry)
{
	struct ec_params_usb_pd_rw_hash_entry params = {
		/* Arbitrary dev_id */
		.dev_id = 1,
	};
	struct host_cmd_handler_args args = BUILD_HOST_COMMAND_PARAMS(
		EC_CMD_USB_PD_RW_HASH_ENTRY, 0, params);

	memset(rw_hash_table, 0,
	       RW_HASH_ENTRIES * sizeof(struct ec_params_usb_pd_rw_hash_entry));

	zassert_ok(host_command_process(&args));
	zassert_mem_equal(test_find_hc_remote_hash_entry(params.dev_id),
			  &params, sizeof(params));
}

ZTEST_USER(usb_pd_host_cmd, test_hc_remote_hash_entry__update_entry)
{
	int arbitrary_dev_id = 1;
	struct ec_params_usb_pd_rw_hash_entry initial_entry = {
		.dev_id = arbitrary_dev_id,
		/* Arbitrary reserved bytes */
		.reserved = 7,
	};
	struct ec_params_usb_pd_rw_hash_entry update_entry = {
		.dev_id = arbitrary_dev_id,
		/* Arbitrary different reserved bytes */
		.reserved = 3,
	};
	struct host_cmd_handler_args initial_args = BUILD_HOST_COMMAND_PARAMS(
		EC_CMD_USB_PD_RW_HASH_ENTRY, 0, initial_entry);
	struct host_cmd_handler_args update_args = BUILD_HOST_COMMAND_PARAMS(
		EC_CMD_USB_PD_RW_HASH_ENTRY, 0, update_entry);

	memset(rw_hash_table, 0,
	       RW_HASH_ENTRIES * sizeof(struct ec_params_usb_pd_rw_hash_entry));

	zassert_ok(host_command_process(&initial_args));
	zassert_mem_equal(test_find_hc_remote_hash_entry(initial_entry.dev_id),
			  &initial_entry, sizeof(initial_entry));

	zassert_ok(host_command_process(&update_args));
	zassert_mem_equal(test_find_hc_remote_hash_entry(update_entry.dev_id),
			  &update_entry, sizeof(update_entry));
}

ZTEST_USER(usb_pd_host_cmd, test_host_command_hc_pd_ports)
{
	struct ec_response_usb_pd_ports response;
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND_RESPONSE(EC_CMD_USB_PD_PORTS, 0, response);

	zassert_ok(host_command_process(&args));
	zassert_ok(args.result);
	zassert_equal(args.response_size, sizeof(response));
	zassert_equal(response.num_ports, CONFIG_USB_PD_PORT_MAX_COUNT);
}

ZTEST_USER(usb_pd_host_cmd, test_typec_discovery_invalid_args)
{
	struct ec_params_typec_discovery params = {
		.port = 100,
		.partner_type = TYPEC_PARTNER_SOP,
	};
	struct ec_response_typec_discovery response;
	/* A successful EC_CMD_TYPEC_DISCOVERY requires response to be larger
	 * than ec_params_typec_discovery, but this one is expected to fail, so
	 * the response size is irrelevant.
	 */
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND(EC_CMD_TYPEC_DISCOVERY, 0, response, params);

	zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM);

	params.port = 0;
	/* This is not a valid enum value but should be representable. */
	params.partner_type = 5;
	zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM);
}

ZTEST_USER(usb_pd_host_cmd, test_typec_control_invalid_args)
{
	struct ec_params_typec_control params = {
		.port = 0,
		.command = TYPEC_CONTROL_COMMAND_TBT_UFP_REPLY,
	};
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND_PARAMS(EC_CMD_TYPEC_CONTROL, 0, params);

	/* Setting the TBT UFP responses is not supported by default. */
	zassert_equal(host_command_process(&args), EC_RES_UNAVAILABLE);

	/* Neither is mux setting. */
	params.command = TYPEC_CONTROL_COMMAND_USB_MUX_SET;
	zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM);

	/* This is not a valid enum value but should be representable. */
	params.command = 0xff;
	zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM);
}

ZTEST_USER(usb_pd_host_cmd, test_typec_status_invalid_args)
{
	struct ec_params_typec_status params = {
		.port = 100,
	};
	struct ec_response_typec_status response;
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND(EC_CMD_TYPEC_STATUS, 0, response, params);

	/* An invalid port should result in an error. */
	zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM);

	params.port = 0;
	args.response_max = sizeof(struct ec_response_typec_status) - 1;
	zassert_equal(host_command_process(&args), EC_RES_RESPONSE_TOO_BIG);
}

ZTEST_SUITE(usb_pd_host_cmd, drivers_predicate_post_main, NULL, NULL, NULL,
	    NULL);