From cf69e095a410c95e74021ba274f1448765314e45 Mon Sep 17 00:00:00 2001 From: Jeremy Bettis Date: Wed, 2 Feb 2022 16:02:12 -0700 Subject: zephyr: Support use of zephyr "dummy" console Right now we only use the serial console in ec zephyr, however if you want to check the output of a console command in a test, that is not sufficient. Add test only get_ec_shell() function that will give you the active shell, whichever backend it might use. Change console.c to setup the serial or dummy backends depending on Kconfig. Removed all calls to shell_backend_uart_get_ptr(), except for 1 in console.c:init_ec_shell(). Switch test/drivers to SHELL_BACKEND_DUMMY. Added some better failure logging in utils.c. BRANCH=None BUG=b:214256453 TEST=zmake testall Signed-off-by: Jeremy Bettis Change-Id: Ibafc37cc4363d1f20a919fa5e38325bd6a071eec Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3433630 Tested-by: Jeremy Bettis Auto-Submit: Jeremy Bettis Reviewed-by: Simon Glass Commit-Queue: Simon Glass --- include/console.h | 7 +++++++ zephyr/shim/src/console.c | 34 +++++++++++++++++++++++++--------- zephyr/test/drivers/prj.conf | 2 ++ zephyr/test/drivers/src/power_common.c | 6 +++--- zephyr/test/drivers/src/smart.c | 24 ++++++++++++------------ zephyr/test/drivers/src/usb_mux.c | 18 +++++++++--------- zephyr/test/drivers/src/utils.c | 10 +++++----- 7 files changed, 63 insertions(+), 38 deletions(-) diff --git a/include/console.h b/include/console.h index 09155c9696..ad2fbe92a2 100644 --- a/include/console.h +++ b/include/console.h @@ -32,6 +32,13 @@ void uart_shell_start(void); * the PD threads, but higher than the HOSTCMD thread. See shimmed_task_id.h */ void uart_shell_set_priority(int prio); + +#ifdef TEST_BUILD +/* Gets the pointer to the zephyr shell, since it might not always be + * the uart backend. + */ +const struct shell *get_ec_shell(void); +#endif #else static inline int uart_shell_stop(void) { diff --git a/zephyr/shim/src/console.c b/zephyr/shim/src/console.c index 8b3d49cc04..d9c2554535 100644 --- a/zephyr/shim/src/console.c +++ b/zephyr/shim/src/console.c @@ -6,6 +6,9 @@ #include #include #include +#ifdef CONFIG_SHELL_BACKEND_DUMMY +#include +#endif #include #include #include @@ -101,7 +104,7 @@ int uart_shell_stop(void) k_poll_signal_init(&shell_uninit_signal); /* Stop the shell */ - shell_uninit(shell_backend_uart_get_ptr(), shell_uninit_callback); + shell_uninit(shell_zephyr, shell_uninit_callback); /* Wait for the shell to be turned off, the signal will wake us */ k_poll(&event, 1, K_FOREVER); @@ -115,18 +118,18 @@ static const struct shell_backend_config_flags shell_cfg_flags = static void shell_init_from_work(struct k_work *work) { - bool log_backend = CONFIG_SHELL_BACKEND_SERIAL_LOG_LEVEL > 0; - uint32_t level; + bool log_backend = 1; + uint32_t level = CONFIG_LOG_MAX_LEVEL; ARG_UNUSED(work); - if (CONFIG_SHELL_BACKEND_SERIAL_LOG_LEVEL > LOG_LEVEL_DBG) { - level = CONFIG_LOG_MAX_LEVEL; - } else { +#ifdef CONFIG_SHELL_BACKEND_SERIAL + log_backend = CONFIG_SHELL_BACKEND_SERIAL_LOG_LEVEL > 0; + if (CONFIG_SHELL_BACKEND_SERIAL_LOG_LEVEL <= LOG_LEVEL_DBG) level = CONFIG_SHELL_BACKEND_SERIAL_LOG_LEVEL; - } +#endif /* Initialize the shell and re-enable both RX and TX */ - shell_init(shell_backend_uart_get_ptr(), uart_shell_dev, + shell_init(shell_zephyr, uart_shell_dev, shell_cfg_flags, log_backend, level); /* @@ -213,10 +216,23 @@ static int init_ec_console(const struct device *unused) static int init_ec_shell(const struct device *unused) { - shell_zephyr = shell_backend_uart_get_ptr(); +#if defined(CONFIG_SHELL_BACKEND_SERIAL) + shell_zephyr = shell_backend_uart_get_ptr(); +#elif defined(CONFIG_SHELL_BACKEND_DUMMY) + shell_zephyr = shell_backend_dummy_get_ptr(); +#else +#error A shell backend must be enabled +#endif return 0; } SYS_INIT(init_ec_shell, PRE_KERNEL_1, 50); +#ifdef TEST_BUILD +const struct shell *get_ec_shell(void) +{ + return shell_zephyr; +} +#endif + void uart_tx_start(void) { } diff --git a/zephyr/test/drivers/prj.conf b/zephyr/test/drivers/prj.conf index 145cfb4369..8fde002209 100644 --- a/zephyr/test/drivers/prj.conf +++ b/zephyr/test/drivers/prj.conf @@ -27,6 +27,8 @@ CONFIG_DEBUG=y CONFIG_DEBUG_INFO=y CONFIG_DEBUG_OPTIMIZATIONS=y CONFIG_PLATFORM_EC_CONSOLE_DEBUG=y +CONFIG_SHELL_BACKEND_DUMMY=y +CONFIG_SHELL_BACKEND_SERIAL=n CONFIG_PLATFORM_EC=y CONFIG_CROS_EC=y diff --git a/zephyr/test/drivers/src/power_common.c b/zephyr/test/drivers/src/power_common.c index 04250f6b77..d24537ba34 100644 --- a/zephyr/test/drivers/src/power_common.c +++ b/zephyr/test/drivers/src/power_common.c @@ -628,20 +628,20 @@ ZTEST(power_common_hibernation, test_power_cmd_hibernation_delay) /* Test success on call without argument */ zassert_equal(EC_SUCCESS, - shell_execute_cmd(shell_backend_uart_get_ptr(), + shell_execute_cmd(get_ec_shell(), "hibdelay"), NULL); /* Test error on hibernation delay argument that is not a number */ zassert_equal(EC_ERROR_PARAM1, - shell_execute_cmd(shell_backend_uart_get_ptr(), + shell_execute_cmd(get_ec_shell(), "hibdelay test1"), NULL); /* Set hibernate delay */ h_delay = 3; zassert_equal(EC_SUCCESS, - shell_execute_cmd(shell_backend_uart_get_ptr(), + shell_execute_cmd(get_ec_shell(), "hibdelay 3"), NULL); diff --git a/zephyr/test/drivers/src/smart.c b/zephyr/test/drivers/src/smart.c index 70a242abf4..08a4eafedd 100644 --- a/zephyr/test/drivers/src/smart.c +++ b/zephyr/test/drivers/src/smart.c @@ -434,28 +434,28 @@ ZTEST_USER(smart_battery, test_battery_fake_charge) /* Success on command with no argument */ zassert_equal(EC_SUCCESS, - shell_execute_cmd(shell_backend_uart_get_ptr(), + shell_execute_cmd(get_ec_shell(), "battfake"), NULL); /* Fail on command with argument which is not a number */ zassert_equal(EC_ERROR_PARAM1, - shell_execute_cmd(shell_backend_uart_get_ptr(), + shell_execute_cmd(get_ec_shell(), "battfake test"), NULL); /* Fail on command with charge level above 100% */ zassert_equal(EC_ERROR_PARAM1, - shell_execute_cmd(shell_backend_uart_get_ptr(), + shell_execute_cmd(get_ec_shell(), "battfake 123"), NULL); /* Fail on command with charge level below 0% */ zassert_equal(EC_ERROR_PARAM1, - shell_execute_cmd(shell_backend_uart_get_ptr(), + shell_execute_cmd(get_ec_shell(), "battfake -23"), NULL); /* Set fake charge level */ fake_charge = 65; zassert_equal(EC_SUCCESS, - shell_execute_cmd(shell_backend_uart_get_ptr(), + shell_execute_cmd(get_ec_shell(), "battfake 65"), NULL); /* Test that fake charge level is applied */ @@ -483,7 +483,7 @@ ZTEST_USER(smart_battery, test_battery_fake_charge) /* Disable fake charge level */ zassert_equal(EC_SUCCESS, - shell_execute_cmd(shell_backend_uart_get_ptr(), + shell_execute_cmd(get_ec_shell(), "battfake -1"), NULL); /* Test that fake charge level is not applied */ @@ -511,28 +511,28 @@ ZTEST_USER(smart_battery, test_battery_fake_temperature) /* Success on command with no argument */ zassert_equal(EC_SUCCESS, - shell_execute_cmd(shell_backend_uart_get_ptr(), + shell_execute_cmd(get_ec_shell(), "batttempfake"), NULL); /* Fail on command with argument which is not a number */ zassert_equal(EC_ERROR_PARAM1, - shell_execute_cmd(shell_backend_uart_get_ptr(), + shell_execute_cmd(get_ec_shell(), "batttempfake test"), NULL); /* Fail on command with too high temperature (above 500.0 K) */ zassert_equal(EC_ERROR_PARAM1, - shell_execute_cmd(shell_backend_uart_get_ptr(), + shell_execute_cmd(get_ec_shell(), "batttempfake 5001"), NULL); /* Fail on command with too low temperature (below 0 K) */ zassert_equal(EC_ERROR_PARAM1, - shell_execute_cmd(shell_backend_uart_get_ptr(), + shell_execute_cmd(get_ec_shell(), "batttempfake -23"), NULL); /* Set fake temperature */ fake_temp = 2840; zassert_equal(EC_SUCCESS, - shell_execute_cmd(shell_backend_uart_get_ptr(), + shell_execute_cmd(get_ec_shell(), "batttempfake 2840"), NULL); /* Test that fake temperature is applied */ @@ -544,7 +544,7 @@ ZTEST_USER(smart_battery, test_battery_fake_temperature) /* Disable fake temperature */ zassert_equal(EC_SUCCESS, - shell_execute_cmd(shell_backend_uart_get_ptr(), + shell_execute_cmd(get_ec_shell(), "batttempfake -1"), NULL); /* Test that fake temperature is not applied */ diff --git a/zephyr/test/drivers/src/usb_mux.c b/zephyr/test/drivers/src/usb_mux.c index d9ab2901a3..447cd39050 100644 --- a/zephyr/test/drivers/src/usb_mux.c +++ b/zephyr/test/drivers/src/usb_mux.c @@ -820,7 +820,7 @@ ZTEST(usb_init_mux, test_usb_mux_typec_command) /* Test error on command with no argument */ zassert_equal(EC_ERROR_PARAM_COUNT, - shell_execute_cmd(shell_backend_uart_get_ptr(), + shell_execute_cmd(get_ec_shell(), "typec"), NULL); /* @@ -829,17 +829,17 @@ ZTEST(usb_init_mux, test_usb_mux_typec_command) * without accessing cprints output. */ zassert_equal(EC_SUCCESS, - shell_execute_cmd(shell_backend_uart_get_ptr(), + shell_execute_cmd(get_ec_shell(), "typec debug"), NULL); /* Test error on port argument that is not a number */ zassert_equal(EC_ERROR_PARAM1, - shell_execute_cmd(shell_backend_uart_get_ptr(), + shell_execute_cmd(get_ec_shell(), "typec test1"), NULL); /* Test error on invalid port number */ zassert_equal(EC_ERROR_PARAM1, - shell_execute_cmd(shell_backend_uart_get_ptr(), + shell_execute_cmd(get_ec_shell(), "typec 5"), NULL); /* @@ -848,7 +848,7 @@ ZTEST(usb_init_mux, test_usb_mux_typec_command) */ set_proxy_get_mux_state_seq(USB_PD_MUX_TBT_COMPAT_ENABLED); zassert_equal(EC_SUCCESS, - shell_execute_cmd(shell_backend_uart_get_ptr(), + shell_execute_cmd(get_ec_shell(), "typec 1"), NULL); CHECK_PROXY_FAKE_CALL_CNT(proxy_get, NUM_OF_PROXY); @@ -856,7 +856,7 @@ ZTEST(usb_init_mux, test_usb_mux_typec_command) reset_proxy_fakes(); exp_mode = USB_PD_MUX_NONE; zassert_equal(EC_SUCCESS, - shell_execute_cmd(shell_backend_uart_get_ptr(), + shell_execute_cmd(get_ec_shell(), "typec 1 none"), NULL); CHECK_PROXY_FAKE_CALL_CNT_MUX_STATE(proxy_set, NUM_OF_PROXY, exp_mode); /* Mux will enter low power mode */ @@ -870,7 +870,7 @@ ZTEST(usb_init_mux, test_usb_mux_typec_command) reset_proxy_fakes(); exp_mode = USB_PD_MUX_USB_ENABLED | polarity; zassert_equal(EC_SUCCESS, - shell_execute_cmd(shell_backend_uart_get_ptr(), + shell_execute_cmd(get_ec_shell(), "typec 1 usb"), NULL); CHECK_PROXY_FAKE_CALL_CNT_MUX_STATE(proxy_set, NUM_OF_PROXY, exp_mode); /* Mux will exit low power mode */ @@ -880,7 +880,7 @@ ZTEST(usb_init_mux, test_usb_mux_typec_command) reset_proxy_fakes(); exp_mode = USB_PD_MUX_DP_ENABLED | polarity; zassert_equal(EC_SUCCESS, - shell_execute_cmd(shell_backend_uart_get_ptr(), + shell_execute_cmd(get_ec_shell(), "typec 1 dp"), NULL); CHECK_PROXY_FAKE_CALL_CNT_MUX_STATE(proxy_set, NUM_OF_PROXY, exp_mode); @@ -888,7 +888,7 @@ ZTEST(usb_init_mux, test_usb_mux_typec_command) reset_proxy_fakes(); exp_mode = USB_PD_MUX_USB_ENABLED | USB_PD_MUX_DP_ENABLED | polarity; zassert_equal(EC_SUCCESS, - shell_execute_cmd(shell_backend_uart_get_ptr(), + shell_execute_cmd(get_ec_shell(), "typec 1 dock"), NULL); CHECK_PROXY_FAKE_CALL_CNT_MUX_STATE(proxy_set, NUM_OF_PROXY, exp_mode); } diff --git a/zephyr/test/drivers/src/utils.c b/zephyr/test/drivers/src/utils.c index 83b465b84c..1cb49c6382 100644 --- a/zephyr/test/drivers/src/utils.c +++ b/zephyr/test/drivers/src/utils.c @@ -48,15 +48,14 @@ void test_set_chipset_to_s0(void) NULL); /* The easiest way to power on seems to be the shell command. */ - zassert_equal(EC_SUCCESS, - shell_execute_cmd(shell_backend_uart_get_ptr(), - "power on"), + zassert_equal(EC_SUCCESS, shell_execute_cmd(get_ec_shell(), "power on"), NULL); k_sleep(K_SECONDS(1)); /* Check if chipset is in correct state */ - zassert_equal(POWER_S0, power_get_state(), NULL); + zassert_equal(POWER_S0, power_get_state(), "Expected S0, got %d", + power_get_state()); } void test_set_chipset_to_g3(void) @@ -65,5 +64,6 @@ void test_set_chipset_to_g3(void) chipset_force_shutdown(CHIPSET_RESET_KB_SYSRESET); k_sleep(K_SECONDS(20)); /* Check if chipset is in correct state */ - zassert_equal(POWER_G3, power_get_state(), NULL); + zassert_equal(POWER_G3, power_get_state(), "Expected G3, got %d", + power_get_state()); } -- cgit v1.2.1