diff options
-rw-r--r-- | common/mock/tcpc_mock.c | 99 | ||||
-rw-r--r-- | include/mock/tcpc_mock.h | 17 | ||||
-rw-r--r-- | include/tests/enum_strings.h | 94 | ||||
-rw-r--r-- | test/usb_typec_drp_acc_trysrc.c | 6 |
4 files changed, 175 insertions, 41 deletions
diff --git a/common/mock/tcpc_mock.c b/common/mock/tcpc_mock.c index 8a01c0991d..c0c45a3620 100644 --- a/common/mock/tcpc_mock.c +++ b/common/mock/tcpc_mock.c @@ -2,20 +2,25 @@ * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ - /* Mock for the TCPC interface */ +/* Mock for the TCPC interface */ #include "common.h" #include "console.h" -#include "usb_pd_tcpm.h" -#include "mock/tcpc_mock.h" #include "memory.h" +#include "mock/tcpc_mock.h" +#include "tests/enum_strings.h" +#include "usb_pd_tcpm.h" /* Public API for controlling/inspecting this mock */ struct mock_tcpc_ctrl mock_tcpc; void mock_tcpc_reset(void) { + /* Reset all control values to 0 */ memset(&mock_tcpc, 0, sizeof(mock_tcpc)); + + /* Reset all last viewed variables to -1 to make them invalid */ + memset(&mock_tcpc.last, 0xff, sizeof(mock_tcpc.last)); } static int mock_init(int port) @@ -28,9 +33,8 @@ static int mock_release(int port) return EC_SUCCESS; } -static int mock_get_cc(int port, - enum tcpc_cc_voltage_status *cc1, - enum tcpc_cc_voltage_status *cc2) +static int mock_get_cc(int port, enum tcpc_cc_voltage_status *cc1, + enum tcpc_cc_voltage_status *cc2) { *cc1 = mock_tcpc.cc1; *cc2 = mock_tcpc.cc2; @@ -44,16 +48,41 @@ static int mock_get_vbus_level(int port) static int mock_select_rp_value(int port, int rp) { + mock_tcpc.last.rp = rp; + + if (!mock_tcpc.should_print_call) + return EC_SUCCESS; + + ccprints("[TCPC] Setting TCPM-side Rp to %s", from_tcpc_rp_value(rp)); + return EC_SUCCESS; } static int mock_set_cc(int port, int pull) { + mock_tcpc.last.cc = pull; + + if (mock_tcpc.callbacks.set_cc) + mock_tcpc.callbacks.set_cc(port, pull); + + if (!mock_tcpc.should_print_call) + return EC_SUCCESS; + + ccprints("[TCPC] Setting TCPM-side CC to %s", from_tcpc_cc_pull(pull)); + return EC_SUCCESS; } static int mock_set_polarity(int port, enum tcpc_cc_polarity polarity) { + mock_tcpc.last.polarity = polarity; + + if (!mock_tcpc.should_print_call) + return EC_SUCCESS; + + ccprints("[TCPC] Setting TCPM-side polarity to %s", + from_tcpc_cc_polarity(polarity)); + return EC_SUCCESS; } @@ -66,15 +95,15 @@ static int mock_set_msg_header(int port, int power_role, int data_role) { ++mock_tcpc.num_calls_to_set_header; - mock_tcpc.power_role = power_role; - mock_tcpc.data_role = data_role; + mock_tcpc.last.power_role = power_role; + mock_tcpc.last.data_role = data_role; - if (!mock_tcpc.should_print_header_changes) + if (!mock_tcpc.should_print_call) return EC_SUCCESS; - ccprints("Setting TCPC header to %s %s", - power_role == PD_ROLE_SOURCE ? "SRC" : "SNK", - data_role == PD_ROLE_UFP ? "UFP" : "DFP"); + ccprints("[TCPC] Setting TCPM-side header to %s %s", + from_pd_power_role(power_role), + from_pd_data_role(data_role)); return EC_SUCCESS; } @@ -89,8 +118,8 @@ static int mock_get_message_raw(int port, uint32_t *payload, int *head) return EC_SUCCESS; } -static int mock_transmit(int port, - enum tcpm_transmit_type type, uint16_t header, const uint32_t *data) +static int mock_transmit(int port, enum tcpm_transmit_type type, + uint16_t header, const uint32_t *data) { return EC_SUCCESS; } @@ -109,7 +138,7 @@ __maybe_unused static int mock_drp_toggle(int port) } static int mock_get_chip_info(int port, int live, - struct ec_response_pd_chip_info_v1 **info) + struct ec_response_pd_chip_info_v1 **info) { return EC_SUCCESS; } @@ -134,30 +163,30 @@ void mock_set_frs_enable(int port, int enable) } const struct tcpm_drv mock_tcpc_driver = { - .init = &mock_init, - .release = &mock_release, - .get_cc = &mock_get_cc, - .get_vbus_level = &mock_get_vbus_level, - .select_rp_value = &mock_select_rp_value, - .set_cc = &mock_set_cc, - .set_polarity = &mock_set_polarity, - .set_vconn = &mock_set_vconn, - .set_msg_header = &mock_set_msg_header, - .set_rx_enable = &mock_set_rx_enable, - .get_message_raw = &mock_get_message_raw, - .transmit = &mock_transmit, - .tcpc_alert = &mock_tcpc_alert, - .tcpc_discharge_vbus = &mock_tcpc_discharge_vbus, + .init = &mock_init, + .release = &mock_release, + .get_cc = &mock_get_cc, + .get_vbus_level = &mock_get_vbus_level, + .select_rp_value = &mock_select_rp_value, + .set_cc = &mock_set_cc, + .set_polarity = &mock_set_polarity, + .set_vconn = &mock_set_vconn, + .set_msg_header = &mock_set_msg_header, + .set_rx_enable = &mock_set_rx_enable, + .get_message_raw = &mock_get_message_raw, + .transmit = &mock_transmit, + .tcpc_alert = &mock_tcpc_alert, + .tcpc_discharge_vbus = &mock_tcpc_discharge_vbus, #ifdef CONFIG_USB_PD_DUAL_ROLE_AUTO_TOGGLE - .drp_toggle = &mock_drp_toggle, + .drp_toggle = &mock_drp_toggle, #endif - .get_chip_info = &mock_get_chip_info, + .get_chip_info = &mock_get_chip_info, #ifdef CONFIG_USBC_PPC - .set_snk_ctrl = &mock_set_snk_ctrl, - .set_src_ctrl = &mock_set_src_ctrl, + .set_snk_ctrl = &mock_set_snk_ctrl, + .set_src_ctrl = &mock_set_src_ctrl, #endif #ifdef CONFIG_USB_PD_TCPC_LOW_POWER - .enter_low_power_mode = &mock_enter_low_power_mode, + .enter_low_power_mode = &mock_enter_low_power_mode, #endif - .set_frs_enable = &mock_set_frs_enable, + .set_frs_enable = &mock_set_frs_enable, }; diff --git a/include/mock/tcpc_mock.h b/include/mock/tcpc_mock.h index 0cea10412a..543c89862a 100644 --- a/include/mock/tcpc_mock.h +++ b/include/mock/tcpc_mock.h @@ -12,10 +12,21 @@ struct mock_tcpc_ctrl { enum tcpc_cc_voltage_status cc1; enum tcpc_cc_voltage_status cc2; int vbus_level; - enum pd_power_role power_role; - enum pd_data_role data_role; int num_calls_to_set_header; - int should_print_header_changes; + bool should_print_call; + + /* Set to function pointer if callback is needed for test code */ + struct tcpm_drv callbacks; + + /* Store the latest values that were set on TCPC API */ + struct { + enum pd_power_role power_role; + enum pd_data_role data_role; + enum tcpc_cc_pull cc; + enum tcpc_rp_value rp; + enum tcpc_cc_polarity polarity; + } last; + }; /* Reset this TCPC mock */ diff --git a/include/tests/enum_strings.h b/include/tests/enum_strings.h new file mode 100644 index 0000000000..ece2df362f --- /dev/null +++ b/include/tests/enum_strings.h @@ -0,0 +1,94 @@ +/* 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. + */ + +/* Defines helper function that convert Enums to strings for prints in tests */ + +#include "usb_pd_tcpm.h" +#include "usb_pd.h" + +#ifndef __CROS_EC_TEST_ENUM_STINGS_H +#define __CROS_EC_TEST_ENUM_STINGS_H + +#ifndef TEST_BUILD +#error enum_strings.h can only be used in test builds +#endif + +static inline const char *from_tcpc_rp_value(enum tcpc_rp_value value) +{ + switch (value) { + case TYPEC_RP_USB: + return "USB-DEFAULT"; + case TYPEC_RP_1A5: + return "1A5"; + case TYPEC_RP_3A0: + return "3A0"; + case TYPEC_RP_RESERVED: + return "RESERVED"; + default: + return "UNKNOWN"; + } +} + +static inline const char *from_tcpc_cc_pull(enum tcpc_cc_pull value) +{ + switch (value) { + case TYPEC_CC_RA: + return "RA"; + case TYPEC_CC_RP: + return "RP"; + case TYPEC_CC_RD: + return "RD"; + case TYPEC_CC_OPEN: + return "OPEN"; + case TYPEC_CC_RA_RD: + return "RA_RD"; + default: + return "UNKNOWN"; + } +} + +static inline const char *from_tcpc_cc_polarity(enum tcpc_cc_polarity value) +{ + switch (value) { + case POLARITY_CC1: + return "CC1"; + case POLARITY_CC2: + return "CC2"; + case POLARITY_CC1_DTS: + return "CC1 DTS"; + case POLARITY_CC2_DTS: + return "CC2 DTS"; + default: + return "UNKNOWN"; + } +} + +static inline const char *from_pd_power_role(enum pd_power_role value) +{ + switch (value) { + case PD_ROLE_SINK: + return "SNK"; + case PD_ROLE_SOURCE: + return "SRC"; + default: + return "UNKNOWN"; + } +} + +static inline const char *from_pd_data_role(enum pd_data_role value) +{ + switch (value) { + case PD_ROLE_UFP: + return "UFP"; + case PD_ROLE_DFP: + return "DRP"; + case PD_ROLE_DISCONNECTED: + return "DISCONNECTED"; + default: + return "UNKNOWN"; + } +} + +#endif /* __CROS_EC_TEST_ENUM_STINGS_H */ diff --git a/test/usb_typec_drp_acc_trysrc.c b/test/usb_typec_drp_acc_trysrc.c index 479e103c38..3e8c271ddc 100644 --- a/test/usb_typec_drp_acc_trysrc.c +++ b/test/usb_typec_drp_acc_trysrc.c @@ -96,7 +96,7 @@ __maybe_unused static int test_mux_con_dis_as_snk(void) __maybe_unused static int test_power_role_set(void) { /* Print out header changes for easier debugging */ - mock_tcpc.should_print_header_changes = true; + mock_tcpc.should_print_call = true; /* Update CC lines send state machine event to process */ mock_tcpc.cc1 = TYPEC_CC_VOLT_OPEN; @@ -105,8 +105,8 @@ __maybe_unused static int test_power_role_set(void) task_wait_event(10 * SECOND); /* We are in Attached.SRC now */ - TEST_EQ(mock_tcpc.power_role, PD_ROLE_SOURCE, "%d"); - TEST_EQ(mock_tcpc.data_role, PD_ROLE_DFP, "%d"); + TEST_EQ(mock_tcpc.last.power_role, PD_ROLE_SOURCE, "%d"); + TEST_EQ(mock_tcpc.last.data_role, PD_ROLE_DFP, "%d"); /* * We allow 2 separate calls to update the header since power and data |