diff options
author | Paul Fagerburg <pfagerburg@google.com> | 2020-08-14 17:20:04 -0600 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2020-08-31 17:06:13 +0000 |
commit | 33db71602297092026bb714f8b038ac0e7ac593d (patch) | |
tree | 4f40eb0fbcc63acd5d8f4387f7d2d554f80ae6b4 /common/mock | |
parent | bd018841f6f2856c949dcf9b6dd462872cd18d7f (diff) | |
download | chrome-ec-33db71602297092026bb714f8b038ac0e7ac593d.tar.gz |
usb_pe_drp: new unit test with mocks
Add a new unit test for usb_pe_drp with mocks for the various
subsystems that it will use.
The fake_prl module is already essentially a mock for the PRL layer,
so just move it into the mocks directory and make it available as
mock to other tests.
BUG=b:163421994
BRANCH=None
TEST=`make runhosttests` succeeds
Signed-off-by: Paul Fagerburg <pfagerburg@google.com>
Change-Id: I2aea3fa0694e8d9e6bae1f47516cb4d5d2a1e714
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2364050
Tested-by: Paul Fagerburg <pfagerburg@chromium.org>
Reviewed-by: Jett Rink <jettrink@chromium.org>
Commit-Queue: Paul Fagerburg <pfagerburg@chromium.org>
Diffstat (limited to 'common/mock')
-rw-r--r-- | common/mock/build.mk | 5 | ||||
-rw-r--r-- | common/mock/dp_alt_mode_mock.c | 37 | ||||
-rw-r--r-- | common/mock/usb_pd_dpm_mock.c | 41 | ||||
-rw-r--r-- | common/mock/usb_pd_mock.c | 34 | ||||
-rw-r--r-- | common/mock/usb_prl_mock.c | 96 | ||||
-rw-r--r-- | common/mock/usb_tc_sm_mock.c | 83 |
6 files changed, 288 insertions, 8 deletions
diff --git a/common/mock/build.mk b/common/mock/build.mk index 1a4a45910d..5f2d548825 100644 --- a/common/mock/build.mk +++ b/common/mock/build.mk @@ -16,4 +16,7 @@ mock-$(HAS_MOCK_TIMER) += timer_mock.o mock-$(HAS_MOCK_USB_MUX) += usb_mux_mock.o mock-$(HAS_MOCK_USB_PD) += usb_pd_mock.o mock-$(HAS_MOCK_USB_PE_SM) += usb_pe_sm_mock.o -mock-$(HAS_MOCK_USB_TC_SM) += usb_tc_sm_mock.o
\ No newline at end of file +mock-$(HAS_MOCK_USB_TC_SM) += usb_tc_sm_mock.o +mock-$(HAS_MOCK_USB_PD_DPM) += usb_pd_dpm_mock.o +mock-$(HAS_MOCK_DP_ALT_MODE) += dp_alt_mode_mock.o +mock-$(HAS_MOCK_USB_PRL) += usb_prl_mock.o diff --git a/common/mock/dp_alt_mode_mock.c b/common/mock/dp_alt_mode_mock.c new file mode 100644 index 0000000000..bc67d57a31 --- /dev/null +++ b/common/mock/dp_alt_mode_mock.c @@ -0,0 +1,37 @@ +/* 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 DisplayPort alternate mode support + * Refer to VESA DisplayPort Alt Mode on USB Type-C Standard, version 2.0, + * section 5.2 + */ + +#include "usb_dp_alt_mode.h" +#include "mock/dp_alt_mode_mock.h" + +#ifdef CONFIG_COMMON_RUNTIME +#define CPRINTF(format, args...) cprintf(CC_USBPD, format, ## args) +#define CPRINTS(format, args...) cprints(CC_USBPD, format, ## args) +#else +#define CPRINTF(format, args...) +#define CPRINTS(format, args...) +#endif + +void mock_dp_alt_mode_reset(void) +{ + /* Nothing to do right now, but in the future ... */ +} + +void dp_init(int port) +{ + CPRINTS("C%d: DP init", port); +} + +void dp_teardown(int port) +{ + CPRINTS("C%d: DP teardown", port); +} + diff --git a/common/mock/usb_pd_dpm_mock.c b/common/mock/usb_pd_dpm_mock.c new file mode 100644 index 0000000000..18778c36f4 --- /dev/null +++ b/common/mock/usb_pd_dpm_mock.c @@ -0,0 +1,41 @@ +/* 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 of Device Policy Manager implementation + * Refer to USB PD 3.0 spec, version 2.0, sections 8.2 and 8.3 + */ + +#include "usb_pd.h" +#include "mock/usb_pd_dpm_mock.h" +#include "memory.h" + +struct mock_dpm_port_t dpm[CONFIG_USB_PD_PORT_MAX_COUNT]; + +void mock_dpm_reset(void) +{ + /* Reset all values to 0. */ + memset(dpm, 0, sizeof(dpm)); +} + +void dpm_init(int port) +{ + dpm[port].mode_entry_done = false; + dpm[port].mode_exit_request = false; +} + +void dpm_vdm_acked(int port, enum tcpm_transmit_type type, int vdo_count, + uint32_t *vdm) +{ +} + +void dpm_vdm_naked(int port, enum tcpm_transmit_type type, uint16_t svid, + uint8_t vdm_cmd) +{ +} + +void dpm_run(int port) +{ +} diff --git a/common/mock/usb_pd_mock.c b/common/mock/usb_pd_mock.c index 3d3e895d6f..653c98c78e 100644 --- a/common/mock/usb_pd_mock.c +++ b/common/mock/usb_pd_mock.c @@ -13,6 +13,12 @@ struct mock_pd_port_t mock_pd_port[CONFIG_USB_PD_PORT_MAX_COUNT]; +void mock_pd_reset(void) +{ + /* Reset all values to 0. */ + memset(mock_pd_port, 0, sizeof(mock_pd_port)); +} + enum pd_dual_role_states pd_get_dual_role(int port) { return PD_DRP_TOGGLE_ON; @@ -33,9 +39,6 @@ enum pd_cc_states pd_get_task_cc_state(int port) return PD_CC_NONE; } -/* TODO remove when usbc_fake is cleaned up */ -#if !defined(CONFIG_USB_DRP_ACC_TRYSRC) && \ - !defined(CONFIG_USB_CTVPD) int pd_is_connected(int port) { return 1; @@ -45,19 +48,18 @@ bool pd_is_disconnected(int port) { return false; } -#endif /* !CONFIG_USB_DRP_ACC_TRYSRC && !CONFIG_USB_CTVPD */ -const uint32_t * const pd_get_src_caps(int port) +__overridable const uint32_t * const pd_get_src_caps(int port) { return NULL; } -uint8_t pd_get_src_cap_cnt(int port) +__overridable uint8_t pd_get_src_cap_cnt(int port) { return 0; } -void pd_set_src_caps(int port, int cnt, uint32_t *src_caps) +__overridable void pd_set_src_caps(int port, int cnt, uint32_t *src_caps) { } @@ -75,3 +77,21 @@ void pd_set_suspend(int port, int suspend) { } +enum tcpc_cc_polarity pd_get_polarity(int port) +{ + return POLARITY_CC1; +} + +void pd_request_data_swap(int port) +{} + +void pd_request_vconn_swap_off(int port) +{} + +void pd_request_vconn_swap_on(int port) +{} + +bool pd_alt_mode_capable(int port) +{ + return false; +} diff --git a/common/mock/usb_prl_mock.c b/common/mock/usb_prl_mock.c new file mode 100644 index 0000000000..1e9c9c9576 --- /dev/null +++ b/common/mock/usb_prl_mock.c @@ -0,0 +1,96 @@ +/* Copyright 2019 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. + * + * Fake Protocol Layer module. + */ +#include <string.h> +#include "common.h" +#include "usb_emsg.h" +#include "usb_prl_sm.h" +#include "mock/usb_prl_mock.h" + +/* Defaults should all be 0 values. */ +struct extended_msg rx_emsg[CONFIG_USB_PD_PORT_MAX_COUNT]; +struct extended_msg tx_emsg[CONFIG_USB_PD_PORT_MAX_COUNT]; + +void mock_prl_reset(void) +{ + /* Reset all values to 0. */ + memset(rx_emsg, 0, sizeof(rx_emsg)); + memset(tx_emsg, 0, sizeof(tx_emsg)); +} + +void prl_end_ams(int port) +{} + +void prl_execute_hard_reset(int port) +{} + +enum pd_rev_type prl_get_rev(int port, enum tcpm_transmit_type partner) +{ + return PD_REV30; +} + +void prl_hard_reset_complete(int port) +{} + +int prl_is_running(int port) +{ + return 1; +} + +__overridable bool prl_is_busy(int port) +{ + return false; +} + +void prl_reset(int port) +{} + +void prl_reset_soft(int port) +{} + +static enum pd_ctrl_msg_type last_ctrl_msg[CONFIG_USB_PD_PORT_MAX_COUNT]; +static enum pd_data_msg_type last_data_msg_type[CONFIG_USB_PD_PORT_MAX_COUNT]; + +void prl_send_ctrl_msg(int port, enum tcpm_transmit_type type, + enum pd_ctrl_msg_type msg) +{ + last_ctrl_msg[port] = msg; +} + +void prl_send_data_msg(int port, enum tcpm_transmit_type type, + enum pd_data_msg_type msg) +{ + last_data_msg_type[port] = msg; +} + +void prl_send_ext_data_msg(int port, enum tcpm_transmit_type type, + enum pd_ext_msg_type msg) +{} + +void prl_set_rev(int port, enum tcpm_transmit_type partner, + enum pd_rev_type rev) +{} + + +enum pd_ctrl_msg_type fake_prl_get_last_sent_ctrl_msg(int port) +{ + return last_ctrl_msg[port]; +} + +void fake_prl_clear_last_sent_ctrl_msg(int port) +{ + last_ctrl_msg[port] = 0; +} + +enum pd_data_msg_type fake_prl_get_last_sent_data_msg_type(int port) +{ + return last_data_msg_type[port]; +} + +void fake_prl_clear_last_sent_data_msg(int port) +{ + last_data_msg_type[port] = 0; +} diff --git a/common/mock/usb_tc_sm_mock.c b/common/mock/usb_tc_sm_mock.c index 884a65ec80..aea8076f09 100644 --- a/common/mock/usb_tc_sm_mock.c +++ b/common/mock/usb_tc_sm_mock.c @@ -30,6 +30,8 @@ void mock_tc_port_reset(void) 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; + mock_tc_port[port].attached_snk = 0; + mock_tc_port[port].attached_src = 0; } } @@ -48,7 +50,88 @@ void typec_select_src_collision_rp(int port, enum tcpc_rp_value rp) mock_tc_port[port].lcl_rp = rp; } +int tc_is_attached_src(int port) +{ + return mock_tc_port[port].attached_src; +} + +int tc_is_attached_snk(int port) +{ + return mock_tc_port[port].attached_snk; +} + +void tc_prs_snk_src_assert_rp(int port) +{ + mock_tc_port[port].attached_snk = 0; + mock_tc_port[port].attached_src = 1; +} + +void tc_prs_src_snk_assert_rd(int port) +{ + mock_tc_port[port].attached_snk = 1; + mock_tc_port[port].attached_src = 0; +} + int typec_update_cc(int port) { return EC_SUCCESS; } + +void tc_set_data_role(int port, enum pd_data_role role) +{ + mock_tc_port[port].data_role = role; +} + +void tc_set_power_role(int port, enum pd_power_role role) +{ + mock_tc_port[port].power_role = role; +} + +int tc_check_vconn_swap(int port) +{ + return 0; +} + +void tc_ctvpd_detected(int port) +{} + +int tc_is_vconn_src(int port) +{ + return 0; +} + +void tc_hard_reset_request(int port) +{ + mock_tc_port_reset(); +} + +void tc_partner_dr_data(int port, int en) +{} + +void tc_partner_dr_power(int port, int en) +{} + +void tc_partner_unconstrainedpower(int port, int en) +{} + +void tc_partner_usb_comm(int port, int en) +{} + +void tc_pd_connection(int port, int en) +{} + +void tc_pr_swap_complete(int port, bool success) +{} + +void tc_src_power_off(int port) +{} + +void tc_start_error_recovery(int port) +{} + +void tc_snk_power_off(int port) +{} + +void tc_request_power_swap(int port) +{ +} |