diff options
author | Paul Fagerburg <pfagerburg@google.com> | 2020-07-06 14:33:45 -0600 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2020-07-20 23:32:08 +0000 |
commit | 565fa5f2fd66c8eb64bb8c009a865380029e6666 (patch) | |
tree | fbdc8b3036d8b1c4260d3519509bd82225d66aef /common/mock | |
parent | 395e6112a995c017c899ecb3f594cb675a516f7e (diff) | |
download | chrome-ec-565fa5f2fd66c8eb64bb8c009a865380029e6666.tar.gz |
TCPMv2: add unit tests for packet discard logic
Add unit tests for the packet discard logic for packets while going
into SinkTxNG.
BUG=b:160622527, b:161174072
BRANCH=None
TEST=`TEST_LIST_HOST=usb_prl make runhosttests`
Signed-off-by: Paul Fagerburg <pfagerburg@chromium.org>
Change-Id: Ied7689d2fe40da93069e58b53ea32e053ab887b8
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2300690
Reviewed-by: Jett Rink <jettrink@chromium.org>
Diffstat (limited to 'common/mock')
-rw-r--r-- | common/mock/tcpm_mock.c | 54 | ||||
-rw-r--r-- | common/mock/usb_pe_sm_mock.c | 22 | ||||
-rw-r--r-- | common/mock/usb_tc_sm_mock.c | 16 |
3 files changed, 88 insertions, 4 deletions
diff --git a/common/mock/tcpm_mock.c b/common/mock/tcpm_mock.c index efd07a9967..81534ad8be 100644 --- a/common/mock/tcpm_mock.c +++ b/common/mock/tcpm_mock.c @@ -9,14 +9,60 @@ #include "memory.h" #include "mock/tcpm_mock.h" +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) { - /* TODO flesh out the mock*/ - return 0; + 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) { - /* TODO flesh out the mock*/ - return 0; + 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; } diff --git a/common/mock/usb_pe_sm_mock.c b/common/mock/usb_pe_sm_mock.c index 881d834b25..a62f9709f5 100644 --- a/common/mock/usb_pe_sm_mock.c +++ b/common/mock/usb_pe_sm_mock.c @@ -19,6 +19,28 @@ struct mock_pe_port_t mock_pe_port[CONFIG_USB_PD_PORT_MAX_COUNT]; +/** + * Resets all mock PE ports to initial values + */ +void mock_pe_port_reset(void) +{ + int port; + + for (port = 0 ; port < CONFIG_USB_PD_PORT_MAX_COUNT ; ++port) { + mock_pe_port[port].mock_pe_error = -1; + /* These mock variable only get set to 1 by various functions, + * so initialize them to 0. Tests can verify they are still 0 + * if that's part of the pass criteria. + */ + mock_pe_port[port].mock_pe_message_received = 0; + mock_pe_port[port].mock_pe_message_sent = 0; + mock_pe_port[port].mock_pe_message_discarded = 0; + mock_pe_port[port].mock_got_soft_reset = 0; + mock_pe_port[port].mock_pe_got_hard_reset = 0; + mock_pe_port[port].mock_pe_hard_reset_sent = 0; + } +} + void pe_report_error(int port, enum pe_error e, enum tcpm_transmit_type type) { mock_pe_port[port].mock_pe_error = e; diff --git a/common/mock/usb_tc_sm_mock.c b/common/mock/usb_tc_sm_mock.c index 88326cebe1..884a65ec80 100644 --- a/common/mock/usb_tc_sm_mock.c +++ b/common/mock/usb_tc_sm_mock.c @@ -17,6 +17,22 @@ struct mock_tc_port_t mock_tc_port[CONFIG_USB_PD_PORT_MAX_COUNT]; +void mock_tc_port_reset(void) +{ + int port; + + for (port = 0 ; port < CONFIG_USB_PD_PORT_MAX_COUNT ; ++port) { + mock_tc_port[port].rev = PD_REV30; + mock_tc_port[port].pd_enable = 0; + mock_tc_port[port].power_role = PD_ROLE_SINK; + mock_tc_port[port].data_role = PD_ROLE_DISCONNECTED; + mock_tc_port[port].msg_tx_id = 0; + mock_tc_port[port].msg_rx_id = 0; + mock_tc_port[port].sop = TCPC_TX_INVALID; + mock_tc_port[port].lcl_rp = TYPEC_RP_RESERVED; + } +} + enum pd_cable_plug tc_get_cable_plug(int port) { return PD_PLUG_FROM_DFP_UFP; |