summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/host_cmd/src/pd_log.c
blob: a6022d8bb1f332ed7c4e668f7cd65ea8bd864c87 (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
/* 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 "event_log.h"
#include "host_command.h"
#include "test/drivers/test_state.h"
#include "usb_pd.h"

/**
 * @brief This is the maximum size of a single log entry.
 *
 * Each entry must contain some common data + up to 16 bytes of additional type
 * specific data.
 */
#define MAX_EVENT_LOG_ENTRY_SIZE (sizeof(struct event_log_entry) + 16)

/**
 * @brief The size of the PD log entry data
 *
 * Logs from the PD include an additional 8 bytes of data to be sent to the AP.
 */
#define PD_LOG_ENTRY_DATA_SIZE (8)

struct pd_log_fixture {
	union {
		uint8_t event_log_buffer[MAX_EVENT_LOG_ENTRY_SIZE];
		struct event_log_entry log_entry;
	};
};

static void *pd_log_setup(void)
{
	static struct pd_log_fixture fixture;

	return &fixture;
}

static void pd_log_before(void *f)
{
	struct pd_log_fixture *fixture = f;

	while (log_dequeue_event(&fixture->log_entry) != 0) {
		if (fixture->log_entry.type == EVENT_LOG_NO_ENTRY) {
			break;
		}
	}
}

ZTEST_SUITE(pd_log, drivers_predicate_post_main, pd_log_setup, pd_log_before,
	    NULL, NULL);

ZTEST_USER(pd_log, test_bad_type)
{
	struct ec_params_pd_write_log_entry params = {
		.type = PD_EVENT_ACC_BASE,
	};
	struct host_cmd_handler_args args = BUILD_HOST_COMMAND_PARAMS(
		EC_CMD_PD_WRITE_LOG_ENTRY, UINT8_C(0), params);

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

ZTEST_USER(pd_log, test_bad_port)
{
	struct ec_params_pd_write_log_entry params = {
		.type = PD_EVENT_MCU_BASE,
	};
	struct host_cmd_handler_args args = BUILD_HOST_COMMAND_PARAMS(
		EC_CMD_PD_WRITE_LOG_ENTRY, UINT8_C(0), params);

	params.port = board_get_usb_pd_port_count() + 1;
	zassert_equal(EC_RES_INVALID_PARAM, host_command_process(&args), NULL);
}

ZTEST_USER_F(pd_log, test_mcu_charge)
{
	struct ec_params_pd_write_log_entry params = {
		.type = PD_EVENT_MCU_CHARGE,
		.port = 0,
	};
	struct host_cmd_handler_args args = BUILD_HOST_COMMAND_PARAMS(
		EC_CMD_PD_WRITE_LOG_ENTRY, UINT8_C(0), params);

	zassert_ok(host_command_process(&args), NULL);
	zassert_equal(sizeof(struct event_log_entry) + PD_LOG_ENTRY_DATA_SIZE,
		      log_dequeue_event(&fixture->log_entry), NULL);
	zassert_equal(params.type, fixture->log_entry.type, NULL);
	zassert_equal(PD_LOG_ENTRY_DATA_SIZE, fixture->log_entry.size, NULL);
	zassert_equal(0, fixture->log_entry.data, NULL);
	zassert_within(0, (int64_t)fixture->log_entry.timestamp, 10,
		       "Expected timestamp %" PRIi64
		       " to be within 10 ms of now",
		       (int64_t)fixture->log_entry.timestamp);
}
ZTEST_USER_F(pd_log, test_mcu_connect)
{
	struct ec_params_pd_write_log_entry params = {
		.type = PD_EVENT_MCU_CONNECT,
		.port = 0,
	};
	struct host_cmd_handler_args args = BUILD_HOST_COMMAND_PARAMS(
		EC_CMD_PD_WRITE_LOG_ENTRY, UINT8_C(0), params);

	zassert_ok(host_command_process(&args), NULL);
	zassert_equal(sizeof(struct event_log_entry),
		      log_dequeue_event(&fixture->log_entry), NULL);
	zassert_equal(params.type, fixture->log_entry.type, NULL);
	zassert_equal(0, fixture->log_entry.size, NULL);
	zassert_equal(0, fixture->log_entry.data, NULL);
	zassert_within(0, (int64_t)fixture->log_entry.timestamp, 10,
		       "Expected timestamp %" PRIi64
		       " to be within 10 ms of now",
		       (int64_t)fixture->log_entry.timestamp);
}

ZTEST_USER_F(pd_log, test_read_log_entry)
{
	uint8_t response_buffer[sizeof(struct ec_response_pd_log) + 16];
	struct ec_response_pd_log *response =
		(struct ec_response_pd_log *)response_buffer;
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND_SIMPLE(EC_CMD_PD_GET_LOG_ENTRY, UINT8_C(0));

	args.response = response;
	args.response_max = sizeof(response_buffer);

	zassert_ok(host_command_process(&args), NULL);
	zassert_equal(sizeof(struct event_log_entry), args.response_size, NULL);
	zassert_equal(PD_EVENT_NO_ENTRY, response->type, NULL);
}