summaryrefslogtreecommitdiff
path: root/zephyr/include/emul/tcpc/emul_tcpci_partner_faulty_snk.h
blob: 7162fc91814c2ded35a2baf3bce8f788391825b8 (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
/* Copyright 2022 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.
 */

/**
 * @file
 *
 * @brief Backend API for USB-C malfunctioning sink device emulator
 */

#ifndef __EMUL_TCPCI_PARTNER_FAULTY_SNK_H
#define __EMUL_TCPCI_PARTNER_FAULTY_SNK_H

#include <drivers/emul.h>
#include "emul/tcpc/emul_tcpci.h"
#include "emul/tcpc/emul_tcpci_partner_common.h"
#include "emul/tcpc/emul_tcpci_partner_snk.h"
#include "usb_pd.h"

/**
 * @brief USB-C malfunctioning sink device emulator backend API
 * @defgroup tcpci_faulty_snk_emul USB-C malfunctioning sink device emulator
 * @{
 *
 * USB-C malfunctioning sink device emulator can be attached to TCPCI emulator.
 * It works as sink device, but it can be configured to not respond to source
 * capability message (by not sending GoodCRC or Request after GoodCRC).
 */

/** Structure describing malfunctioning sink emulator data */
struct tcpci_faulty_snk_emul_data {
	/* List of action to perform */
	struct k_fifo action_list;
};

/** Structure describing standalone malfunctioning device emulator */
struct tcpci_faulty_snk_emul {
	/** Common TCPCI partner data */
	struct tcpci_partner_data common_data;
	/** Operations used by TCPCI emulator */
	struct tcpci_emul_partner_ops ops;
	/** Malfunctioning sink emulator data */
	struct tcpci_faulty_snk_emul_data data;
	/** Sink emulator data */
	struct tcpci_snk_emul_data snk_data;
};

/** Actions that can be performed by malfunctioning sink emulator */
enum tcpci_faulty_snk_action_type {
	/**
	 * Fail to receive SourceCapabilities message. From TCPM point of view,
	 * GoodCRC message is not received.
	 */
	TCPCI_FAULTY_SNK_FAIL_SRC_CAP = BIT(0),
	/**
	 * Ignore to respond to SourceCapabilities message with Request message.
	 * From TCPM point of view, GoodCRC message is received, but Request is
	 * missing.
	 */
	TCPCI_FAULTY_SNK_IGNORE_SRC_CAP = BIT(1),
	/** Discard SourceCapabilities message and send Accept message */
	TCPCI_FAULTY_SNK_DISCARD_SRC_CAP = BIT(2),
};

/** Structure to put in malfunctioning sink emulator action list */
struct tcpci_faulty_snk_action {
	/* Reserved for FIFO */
	void *fifo_reserved;
	/* Actions that emulator should perform */
	uint32_t action_mask;
	/* Number of times to repeat action */
	int count;
};

/* Count of actions which is treated by emulator as infinite */
#define TCPCI_FAULTY_SNK_INFINITE_ACTION	0

/**
 * @brief Initialise USB-C malfunctioning sink device emulator. Need to be
 *        called before any other function that is using common_data.
 *
 * @param emul Pointer to USB-C malfunctioning sink device emulator
 */
void tcpci_faulty_snk_emul_init(struct tcpci_faulty_snk_emul *emul);

/**
 * @brief Initialise USB-C malfunctioning sink device data structure.
 *
 * @param data Pointer to USB-C malfunctioning sink device emulator data
 */
void tcpci_faulty_snk_emul_init_data(struct tcpci_faulty_snk_emul_data *data);

/**
 * @brief Add action to perform by USB-C malfunctioning sink device
 *
 * @param data Pointer to USB-C malfunctioning sink device emulator data
 * @param action Non standard behavior to perform by emulator
 */
void tcpci_faulty_snk_emul_append_action(
	struct tcpci_faulty_snk_emul_data *data,
	struct tcpci_faulty_snk_action *action);

/**
 * @brief Clear all actions of USB-C malfunctioning sink device
 *
 * @param data Pointer to USB-C malfunctioning sink device emulator data
 */
void tcpci_faulty_snk_emul_clear_actions_list(
	struct tcpci_faulty_snk_emul_data *data);

/**
 * @brief Connect emulated device to TCPCI.
 *
 * @param snk_data Pointer to USB-C sink device emulator data
 * @param common_data Pointer to common TCPCI partner data
 * @param ops Pointer to TCPCI partner emulator operations
 * @param tcpci_emul Pointer to TCPCI emulator to connect
 *
 * @return 0 on success
 * @return negative on TCPCI connect error
 */
int tcpci_faulty_snk_emul_connect_to_tcpci(
	struct tcpci_snk_emul_data *snk_data,
	struct tcpci_partner_data *common_data,
	const struct tcpci_emul_partner_ops *ops,
	const struct emul *tcpci_emul);

/**
 * @brief Handle SOP messages as TCPCI dual role device
 *
 * @param data Pointer to USB-C malfunctioning sink device emulator data
 * @param snk_data Pointer to USB-C sink device emulator data
 * @param common_data Pointer to common TCPCI partner data
 * @param ops Pointer to TCPCI partner emulator operations
 * @param msg Pointer to received message
 *
 * @return TCPCI_PARTNER_COMMON_MSG_HANDLED Message was handled
 * @return TCPCI_PARTNER_COMMON_MSG_NOT_HANDLED Message wasn't handled
 */
enum tcpci_partner_handler_res tcpci_faulty_snk_emul_handle_sop_msg(
	struct tcpci_faulty_snk_emul_data *data,
	struct tcpci_snk_emul_data *snk_data,
	struct tcpci_partner_data *common_data,
	const struct tcpci_emul_partner_ops *ops,
	const struct tcpci_emul_msg *msg);

/**
 * @}
 */

#endif /* __EMUL_TCPCI_PARTNER_FAULTY_SNK_H */