summaryrefslogtreecommitdiff
path: root/common/mock/tcpm_mock.c
blob: 2c212cf8c9d7d68cc5380512ed389b051f45728e (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
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
/* Mock for the TCPM interface */

#include "common.h"
#include "console.h"
#include "memory.h"
#include "mock/tcpm_mock.h"

#ifndef TEST_BUILD
#error "Mocks should only be in the test build."
#endif

struct mock_tcpm_t mock_tcpm[CONFIG_USB_PD_PORT_MAX_COUNT];

/**
 * Gets the next waiting RX message.
 *
 * @param port Type-C port number
 * @param payload Pointer to location to copy payload of PD message
 * @param header The header of PD message
 *
 * @return EC_SUCCESS or error
 */
int tcpm_dequeue_message(int port, uint32_t *payload, int *header)
{
	if (!tcpm_has_pending_message(port))
		return EC_ERROR_BUSY;

	*header = mock_tcpm[port].mock_header;
	memcpy(payload, mock_tcpm[port].mock_rx_chk_buf,
		sizeof(mock_tcpm[port].mock_rx_chk_buf));

	return EC_SUCCESS;
}

/**
 * Returns true if the tcpm has RX messages waiting to be consumed.
 */
int tcpm_has_pending_message(int port)
{
	return mock_tcpm[port].mock_has_pending_message;
}

/**
 * Resets all mock TCPM ports
 */
void mock_tcpm_reset(void)
{
	int port;

	for (port = 0 ; port < CONFIG_USB_PD_PORT_MAX_COUNT ; ++port)
		mock_tcpm[port].mock_has_pending_message = 0;
}

/**
 * Sets up a message to be received, with optional data payload. If cnt==0,
 * then data can be NULL.
 */
void mock_tcpm_rx_msg(int port, uint16_t header, int cnt, const uint32_t *data)
{
	mock_tcpm[port].mock_header = header;
	if (cnt > 0) {
		int idx;

		for (idx = 0 ; (idx < cnt) && (idx < MOCK_CHK_BUF_SIZE) ; ++idx)
			mock_tcpm[port].mock_rx_chk_buf[idx] = data[idx];
	}
	mock_tcpm[port].mock_has_pending_message = 1;
}