diff options
Diffstat (limited to 'zephyr/test/drivers/default/src')
98 files changed, 2195 insertions, 895 deletions
diff --git a/zephyr/test/drivers/default/src/battery.c b/zephyr/test/drivers/default/src/battery.c index 2e06725af5..02f85d9662 100644 --- a/zephyr/test/drivers/default/src/battery.c +++ b/zephyr/test/drivers/default/src/battery.c @@ -3,26 +3,78 @@ * found in the LICENSE file. */ -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> -#include <zephyr/drivers/gpio.h> -#include <zephyr/drivers/gpio/gpio_emul.h> - #include "battery.h" +#include "battery_fuel_gauge.h" +#include "emul/emul_common_i2c.h" +#include "emul/emul_smart_battery.h" #include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +#include <zephyr/drivers/emul.h> +#include <zephyr/drivers/gpio.h> +#include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/fff.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #define GPIO_BATT_PRES_ODL_PATH DT_PATH(named_gpios, ec_batt_pres_odl) #define GPIO_BATT_PRES_ODL_PORT DT_GPIO_PIN(GPIO_BATT_PRES_ODL_PATH, gpios) +FAKE_VALUE_FUNC(int, battery2_write_func, const struct emul *, int, uint8_t, + int, void *); +FAKE_VALUE_FUNC(int, battery2_read_func, const struct emul *, int, uint8_t *, + int, void *); + +bool authenticate_battery_type(int index, const char *manuf_name); + +extern int battery_fuel_gauge_type_override; + +struct battery_fixture { + struct i2c_common_emul_data *battery_i2c_common; + i2c_common_emul_finish_write_func finish_write_func; + i2c_common_emul_start_read_func start_read_func; +}; + +static void *battery_setup(void) +{ + static struct battery_fixture fixture; + static const struct emul *emul = EMUL_DT_GET(DT_NODELABEL(battery)); + + fixture.battery_i2c_common = + emul_smart_battery_get_i2c_common_data(emul); + + return &fixture; +} + +static void battery_before(void *data) +{ + struct battery_fixture *fixture = data; + + RESET_FAKE(battery2_write_func); + RESET_FAKE(battery2_read_func); + fixture->finish_write_func = fixture->battery_i2c_common->finish_write; + fixture->start_read_func = fixture->battery_i2c_common->start_read; +} + static void battery_after(void *data) { + struct battery_fixture *fixture = data; const struct device *dev = DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_BATT_PRES_ODL_PATH, gpios)); /* Set default state (battery is present) */ gpio_emul_input_set(dev, GPIO_BATT_PRES_ODL_PORT, 0); + battery_fuel_gauge_type_override = -1; + + i2c_common_emul_set_write_func(fixture->battery_i2c_common, NULL, NULL); + i2c_common_emul_set_read_func(fixture->battery_i2c_common, NULL, NULL); + fixture->battery_i2c_common->finish_write = fixture->finish_write_func; + fixture->battery_i2c_common->start_read = fixture->start_read_func; } +ZTEST_SUITE(battery, drivers_predicate_post_main, battery_setup, battery_before, + battery_after, NULL); + ZTEST_USER(battery, test_battery_is_present_gpio) { const struct device *dev = @@ -37,5 +89,172 @@ ZTEST_USER(battery, test_battery_is_present_gpio) zassert_equal(BP_NO, battery_is_present()); } -ZTEST_SUITE(battery, drivers_predicate_post_main, NULL, NULL, battery_after, - NULL); +ZTEST(battery, test_authenticate_battery_type) +{ + /* Invalid index */ + zassert_false(authenticate_battery_type(BATTERY_TYPE_COUNT, NULL)); + /* Use fuel-gauge 1's manufacturer name for index 0 */ + zassert_false(authenticate_battery_type( + 0, board_battery_info[1].fuel_gauge.manuf_name)); + /* Use the correct manufacturer name, but wrong device name (because the + * index is 1 and not 0) + */ + zassert_false(authenticate_battery_type( + 1, board_battery_info[1].fuel_gauge.manuf_name)); +} + +ZTEST(battery, test_board_get_default_battery_type) +{ + zassert_equal(DEFAULT_BATTERY_TYPE, board_get_default_battery_type()); +} + +ZTEST_F(battery, test_board_cutoff_actuates_driver) +{ + /* We check the return type because board_is_cut_off() is set outside of + * board_cut_off_battery() and may be changed by other factors. + */ + + /* Try with invalid battery type */ + battery_fuel_gauge_type_override = BATTERY_TYPE_COUNT; + zassert_equal(EC_RES_ERROR, board_cut_off_battery()); + + /* Setup error conditions for battery 1*/ + battery_fuel_gauge_type_override = 1; + fixture->battery_i2c_common->finish_write = NULL; + i2c_common_emul_set_write_func(fixture->battery_i2c_common, + battery2_write_func, NULL); + + /* Check that i2c error returns EC_RES_ERROR */ + battery2_write_func_fake.return_val = -1; + zassert_equal(EC_RES_ERROR, board_cut_off_battery()); + + /* Check for OK when i2c succeeds */ + battery2_write_func_fake.return_val = 0; + zassert_ok(board_cut_off_battery()); +} + +ZTEST_F(battery, test_sleep) +{ + /* Try with invalid battery type */ + battery_fuel_gauge_type_override = BATTERY_TYPE_COUNT; + zassert_equal(EC_ERROR_UNKNOWN, battery_sleep_fuel_gauge()); + + /* Check 1st battery (lgc,ac17a8m) */ + battery_fuel_gauge_type_override = 0; + zassert_equal(EC_ERROR_UNIMPLEMENTED, battery_sleep_fuel_gauge()); + + /* Check 2nd battery (panasonic,ap15l5j) */ + battery_fuel_gauge_type_override = 1; + fixture->battery_i2c_common->finish_write = NULL; + i2c_common_emul_set_write_func(fixture->battery_i2c_common, + battery2_write_func, NULL); + zassert_ok(battery_sleep_fuel_gauge()); +} + +struct battery2_read_data { + size_t count; + const uint8_t *values; +}; + +static int battery2_read(const struct emul *target, int reg, uint8_t *val, + int bytes, void *d) +{ + struct battery2_read_data *data = d; + + if (bytes < data->count) { + *val = data->values[bytes]; + } + + return 0; +} + +ZTEST(battery, test_is_charge_fet_disabled__invalid_battery_type) +{ + battery_fuel_gauge_type_override = BATTERY_TYPE_COUNT; + zassert_equal(-1, battery_is_charge_fet_disabled()); +} + +ZTEST(battery, test_is_charge_fet_disabled__cfet_mask_is_0) +{ + battery_fuel_gauge_type_override = 2; + zassert_equal(0, battery_is_charge_fet_disabled()); +} + +ZTEST_F(battery, test_is_charge_fet_disabled__i2c_error) +{ + /* Set the battery to battery 1 */ + battery_fuel_gauge_type_override = 1; + + /* Override the finish_write common callback since we don't actually + * want to be messing with the emulator. + */ + fixture->battery_i2c_common->finish_write = NULL; + + /* Set up an error condition for battery 1 to fail writing to i2c */ + battery2_write_func_fake.return_val = -1; + i2c_common_emul_set_write_func(fixture->battery_i2c_common, + battery2_write_func, NULL); + + /* Verify the error */ + zassert_equal(-1, battery_is_charge_fet_disabled()); +} + +ZTEST_F(battery, test_is_charge_fet_disabled) +{ + /* Custom data expected to be read from the battery */ + static const uint8_t values[] = { 0x20, 0x54, 0x00, 0x00, + 0x00, 0x00, 0x00 }; + static struct battery2_read_data data = { + .count = ARRAY_SIZE(values), + .values = values, + }; + + /* Set up the fake read function */ + battery2_read_func_fake.custom_fake = battery2_read; + i2c_common_emul_set_read_func(fixture->battery_i2c_common, + battery2_read_func, &data); + + /* Override the finish_write and start_read common callback since we + * don't actually want to be messing with the emulator. + */ + fixture->battery_i2c_common->finish_write = NULL; + fixture->battery_i2c_common->start_read = NULL; + + zassert_equal(1, battery_is_charge_fet_disabled()); +} + +ZTEST(battery, test_get_disconnect_state__invalid_battery_type) +{ + battery_fuel_gauge_type_override = BATTERY_TYPE_COUNT; + zassert_equal(BATTERY_DISCONNECT_ERROR, battery_get_disconnect_state()); +} + +ZTEST_F(battery, test_get_disconnect_state__fail_i2c_read) +{ + /* Use battery 0 */ + battery_fuel_gauge_type_override = 0; + + /* Configure i2c to fail on read */ + battery2_read_func_fake.return_val = -1; + i2c_common_emul_set_read_func(fixture->battery_i2c_common, + battery2_read_func, NULL); + + /* Check for disconnect error */ + zassert_equal(BATTERY_DISCONNECT_ERROR, battery_get_disconnect_state()); +} + +ZTEST_F(battery, test_get_disconnect_state) +{ + static const uint8_t values[] = { 0x00, 0x20 }; + static struct battery2_read_data data = { + .count = ARRAY_SIZE(values), + .values = values, + }; + + /* Enable i2c reads and set them to always return 0x2000 */ + battery2_read_func_fake.custom_fake = battery2_read; + i2c_common_emul_set_read_func(fixture->battery_i2c_common, + battery2_read_func, &data); + + zassert_equal(BATTERY_DISCONNECTED, battery_get_disconnect_state()); +} diff --git a/zephyr/test/drivers/default/src/bb_retimer.c b/zephyr/test/drivers/default/src/bb_retimer.c index 1b1ec4bbbc..78d483817a 100644 --- a/zephyr/test/drivers/default/src/bb_retimer.c +++ b/zephyr/test/drivers/default/src/bb_retimer.c @@ -3,25 +3,24 @@ * found in the LICENSE file. */ -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> -#include <zephyr/drivers/gpio.h> -#include <zephyr/drivers/gpio/gpio_emul.h> - +#include "chipset.h" #include "common.h" +#include "driver/retimer/bb_retimer.h" #include "ec_tasks.h" #include "emul/emul_bb_retimer.h" #include "emul/emul_common_i2c.h" #include "hooks.h" #include "i2c.h" #include "test/drivers/stubs.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" #include "usb_prl_sm.h" #include "usb_tc_sm.h" -#include "chipset.h" -#include "driver/retimer/bb_retimer.h" -#include "test/drivers/test_state.h" -#include "test/drivers/utils.h" +#include <zephyr/drivers/gpio.h> +#include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #define GPIO_USB_C1_LS_EN_PATH DT_PATH(named_gpios, usb_c1_ls_en) #define GPIO_USB_C1_LS_EN_PORT DT_GPIO_PIN(GPIO_USB_C1_LS_EN_PATH, gpios) @@ -66,7 +65,7 @@ ZTEST_USER(bb_retimer_no_tasks, test_bb_set_state) /* Set UFP role for whole test */ tc_set_data_role(USBC_PORT_C1, PD_ROLE_UFP); - zassume_equal(PD_ROLE_UFP, pd_get_data_role(USBC_PORT_C1)); + zassert_equal(PD_ROLE_UFP, pd_get_data_role(USBC_PORT_C1)); /* Test none mode */ bb_emul_set_reg(emul, BB_RETIMER_REG_CONNECTION_STATE, 0x12144678); @@ -162,7 +161,8 @@ ZTEST_USER(bb_retimer_no_tasks, test_bb_set_state) zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); exp_conn = BB_RETIMER_USB_DATA_ROLE | - BB_RETIMER_DATA_CONNECTION_PRESENT; + BB_RETIMER_DATA_CONNECTION_PRESENT | + BB_RETIMER_DP_CONNECTION; zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx", exp_conn, conn); @@ -175,7 +175,8 @@ ZTEST_USER(bb_retimer_no_tasks, test_bb_set_state) zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); exp_conn = BB_RETIMER_USB_DATA_ROLE | - BB_RETIMER_DATA_CONNECTION_PRESENT | BB_RETIMER_IRQ_HPD; + BB_RETIMER_DATA_CONNECTION_PRESENT | + BB_RETIMER_DP_CONNECTION | BB_RETIMER_IRQ_HPD; zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx", exp_conn, conn); @@ -188,7 +189,8 @@ ZTEST_USER(bb_retimer_no_tasks, test_bb_set_state) zassert_false(ack_required, "ACK is never required for BB retimer"); conn = bb_emul_get_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); exp_conn = BB_RETIMER_USB_DATA_ROLE | - BB_RETIMER_DATA_CONNECTION_PRESENT | BB_RETIMER_HPD_LVL; + BB_RETIMER_DATA_CONNECTION_PRESENT | + BB_RETIMER_DP_CONNECTION | BB_RETIMER_HPD_LVL; zassert_equal(exp_conn, conn, "Expected state 0x%lx, got 0x%lx", exp_conn, conn); } @@ -251,7 +253,7 @@ ZTEST_USER(bb_retimer_no_tasks, test_bb_set_dfp_state) set_test_runner_tid(); tc_set_data_role(USBC_PORT_C1, PD_ROLE_DFP); - zassume_equal(PD_ROLE_DFP, pd_get_data_role(USBC_PORT_C1)); + zassert_equal(PD_ROLE_DFP, pd_get_data_role(USBC_PORT_C1)); /* Test PD mux none mode with DFP should clear all bits in state */ bb_emul_set_reg(emul, BB_RETIMER_REG_CONNECTION_STATE, 0x12144678); diff --git a/zephyr/test/drivers/default/src/bc12.c b/zephyr/test/drivers/default/src/bc12.c deleted file mode 100644 index db1819ec4c..0000000000 --- a/zephyr/test/drivers/default/src/bc12.c +++ /dev/null @@ -1,277 +0,0 @@ -/* Copyright 2021 The ChromiumOS Authors - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> -#include <zephyr/drivers/gpio.h> -#include <zephyr/drivers/gpio/gpio_emul.h> - -#include "emul/emul_pi3usb9201.h" - -#include "timer.h" -#include "usb_charge.h" -#include "battery.h" -#include "extpower.h" -#include "test/drivers/stubs.h" -#include "test/drivers/test_state.h" -#include "test/drivers/utils.h" - -#include <zephyr/logging/log.h> -LOG_MODULE_REGISTER(test_drivers_bc12, LOG_LEVEL_DBG); - -#define EMUL_NODE DT_NODELABEL(pi3usb9201_emul) - -/* Control_1 register bit definitions */ -#define PI3USB9201_REG_CTRL_1_INT_MASK BIT(0) -#define PI3USB9201_REG_CTRL_1_MODE_SHIFT 1 -#define PI3USB9201_REG_CTRL_1_MODE_MASK \ - (0x7 << PI3USB9201_REG_CTRL_1_MODE_SHIFT) - -/* Control_2 register bit definitions */ -#define PI3USB9201_REG_CTRL_2_AUTO_SW BIT(1) -#define PI3USB9201_REG_CTRL_2_START_DET BIT(3) - -/* Host status register bit definitions */ -#define PI3USB9201_REG_HOST_STS_BC12_DET BIT(0) -#define PI3USB9201_REG_HOST_STS_DEV_PLUG BIT(1) -#define PI3USB9201_REG_HOST_STS_DEV_UNPLUG BIT(2) - -enum pi3usb9201_mode { - PI3USB9201_POWER_DOWN, - PI3USB9201_SDP_HOST_MODE, - PI3USB9201_DCP_HOST_MODE, - PI3USB9201_CDP_HOST_MODE, - PI3USB9201_CLIENT_MODE, - PI3USB9201_RESERVED_1, - PI3USB9201_RESERVED_2, - PI3USB9201_USB_PATH_ON, -}; - -enum pi3usb9201_client_sts { - CHG_OTHER = 0, - CHG_2_4A, - CHG_2_0A, - CHG_1_0A, - CHG_RESERVED, - CHG_CDP, - CHG_SDP, - CHG_DCP, -}; - -struct bc12_status { - enum charge_supplier supplier; - int current_limit; -}; - -static const struct bc12_status bc12_chg_limits[] = { - [CHG_OTHER] = { .supplier = CHARGE_SUPPLIER_OTHER, - .current_limit = 500 }, - [CHG_2_4A] = { .supplier = CHARGE_SUPPLIER_PROPRIETARY, - .current_limit = USB_CHARGER_MAX_CURR_MA }, - [CHG_2_0A] = { .supplier = CHARGE_SUPPLIER_PROPRIETARY, - .current_limit = USB_CHARGER_MAX_CURR_MA }, - [CHG_1_0A] = { .supplier = CHARGE_SUPPLIER_PROPRIETARY, - .current_limit = 1000 }, - [CHG_RESERVED] = { .supplier = CHARGE_SUPPLIER_NONE, - .current_limit = 0 }, - [CHG_CDP] = { .supplier = CHARGE_SUPPLIER_BC12_CDP, - .current_limit = USB_CHARGER_MAX_CURR_MA }, - [CHG_SDP] = { .supplier = CHARGE_SUPPLIER_BC12_SDP, - .current_limit = 500 }, -#if defined(CONFIG_CHARGE_RAMP_SW) || defined(CONFIG_CHARGE_RAMP_HW) - [CHG_DCP] = { .supplier = CHARGE_SUPPLIER_BC12_DCP, - .current_limit = USB_CHARGER_MAX_CURR_MA }, -#else - [CHG_DCP] = { .supplier = CHARGE_SUPPLIER_BC12_DCP, - .current_limit = 500 }, -#endif -}; - -#define GPIO_BATT_PRES_ODL_PATH DT_PATH(named_gpios, ec_batt_pres_odl) -#define GPIO_BATT_PRES_ODL_PORT DT_GPIO_PIN(GPIO_BATT_PRES_ODL_PATH, gpios) - -static void test_bc12_pi3usb9201_host_mode(void) -{ - const struct emul *emul = EMUL_DT_GET(EMUL_NODE); - uint8_t a, b; - - /* - * Pretend that the USB-C Port Manager (TCPMv2) has set the port data - * role to DFP. - */ - usb_charger_task_set_event(0, USB_CHG_EVENT_DR_DFP); - msleep(1); - /* - * Expect the pi3usb9201 driver to configure CDP host mode and unmask - * interrupts. - */ - pi3usb9201_emul_get_reg(emul, PI3USB9201_REG_CTRL_1, &a); - b = PI3USB9201_CDP_HOST_MODE << PI3USB9201_REG_CTRL_1_MODE_SHIFT; - zassert_equal(a, b); - - /* Pretend that a device has been plugged in. */ - msleep(500); - pi3usb9201_emul_set_reg(emul, PI3USB9201_REG_HOST_STS, - PI3USB9201_REG_HOST_STS_DEV_PLUG); - usb_charger_task_set_event(0, USB_CHG_EVENT_BC12); - msleep(1); - /* Expect the pi3usb9201 driver to configure SDP host mode. */ - pi3usb9201_emul_get_reg(emul, PI3USB9201_REG_CTRL_1, &a); - b = PI3USB9201_SDP_HOST_MODE << PI3USB9201_REG_CTRL_1_MODE_SHIFT; - zassert_equal(a, b); - pi3usb9201_emul_set_reg(emul, PI3USB9201_REG_HOST_STS, 0); - - /* Pretend that a device has been unplugged. */ - msleep(500); - pi3usb9201_emul_set_reg(emul, PI3USB9201_REG_HOST_STS, - PI3USB9201_REG_HOST_STS_DEV_UNPLUG); - usb_charger_task_set_event(0, USB_CHG_EVENT_BC12); - msleep(1); - /* Expect the pi3usb9201 driver to configure CDP host mode. */ - pi3usb9201_emul_get_reg(emul, PI3USB9201_REG_CTRL_1, &a); - b = PI3USB9201_CDP_HOST_MODE << PI3USB9201_REG_CTRL_1_MODE_SHIFT; - zassert_equal(a, b); - pi3usb9201_emul_set_reg(emul, PI3USB9201_REG_HOST_STS, 0); -} - -static void -test_bc12_pi3usb9201_client_mode(enum pi3usb9201_client_sts detect_result, - enum charge_supplier supplier, - int current_limit) -{ - const struct emul *emul = EMUL_DT_GET(EMUL_NODE); - uint8_t a, b; - int port, voltage; - - /* - * Pretend that the USB-C Port Manager (TCPMv2) has set the port data - * role to UFP and decided charging from the port is allowed. - */ - msleep(500); - usb_charger_task_set_event(0, USB_CHG_EVENT_DR_UFP); - charge_manager_update_dualrole(USBC_PORT_C0, CAP_DEDICATED); - msleep(1); - /* - * Expect the pi3usb9201 driver to configure client mode and start - * detection. - */ - pi3usb9201_emul_get_reg(emul, PI3USB9201_REG_CTRL_1, &a); - b = PI3USB9201_CLIENT_MODE << PI3USB9201_REG_CTRL_1_MODE_SHIFT; - zassert_equal(a, b); - pi3usb9201_emul_get_reg(emul, PI3USB9201_REG_CTRL_2, &a); - b = PI3USB9201_REG_CTRL_2_START_DET; - zassert_equal(a, b); - - /* Pretend that detection completed. */ - msleep(500); - pi3usb9201_emul_set_reg(emul, PI3USB9201_REG_CLIENT_STS, - 1 << detect_result); - usb_charger_task_set_event(0, USB_CHG_EVENT_BC12); - msleep(1); - /* Expect the pi3usb9201 driver to clear the start bit. */ - pi3usb9201_emul_get_reg(emul, PI3USB9201_REG_CTRL_2, &a); - zassert_equal(a, 0); - pi3usb9201_emul_set_reg(emul, PI3USB9201_REG_CLIENT_STS, 0); - /* - * Expect the charge manager to select the detected BC1.2 supplier. - */ - port = CHARGE_PORT_NONE; - voltage = 0; - if (supplier != CHARGE_SUPPLIER_NONE) { - port = USBC_PORT_C0; - voltage = USB_CHARGER_VOLTAGE_MV; - } - /* Wait for the charge port to update. */ - msleep(500); - zassert_equal(charge_manager_get_active_charge_port(), port); - zassert_equal(charge_manager_get_supplier(), supplier); - zassert_equal(charge_manager_get_charger_current(), current_limit, - NULL); - zassert_equal(charge_manager_get_charger_voltage(), voltage); - - /* - * Pretend that the USB-C Port Manager (TCPMv2) has set the port data - * role to disconnected. - */ - msleep(500); - usb_charger_task_set_event(0, USB_CHG_EVENT_CC_OPEN); - msleep(1); - /* - * Expect the pi3usb9201 driver to configure power down mode and mask - * interrupts. - */ - pi3usb9201_emul_get_reg(emul, PI3USB9201_REG_CTRL_1, &a); - b = PI3USB9201_POWER_DOWN << PI3USB9201_REG_CTRL_1_MODE_SHIFT; - b |= PI3USB9201_REG_CTRL_1_INT_MASK; - zassert_equal(a, b); - /* Expect the charge manager to have no active supplier. */ - zassert_equal(charge_manager_get_active_charge_port(), CHARGE_PORT_NONE, - NULL); - zassert_equal(charge_manager_get_supplier(), CHARGE_SUPPLIER_NONE, - NULL); - zassert_equal(charge_manager_get_charger_current(), 0); - zassert_equal(charge_manager_get_charger_voltage(), 0); -} - -/* - * PI3USB9201 is a dual-role BC1.2 charger detector/advertiser used on USB - * ports. It can be programmed to operate in host mode or client mode through - * I2C. When operating as a host, PI3USB9201 enables BC1.2 SDP/CDP/DCP - * advertisement to the attached USB devices via the D+/- connection. When - * operating as a client, PI3USB9201 starts BC1.2 detection to detect the - * attached host type. In both host mode and client mode, the detection results - * are reported through I2C to the controller. - */ -ZTEST_USER(bc12, test_bc12_pi3usb9201) -{ - const struct device *batt_pres_dev = - DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_BATT_PRES_ODL_PATH, gpios)); - const struct emul *emul = EMUL_DT_GET(EMUL_NODE); - uint8_t a, b; - - /* Pretend we have battery and AC so charging works normally. */ - zassert_ok(gpio_emul_input_set(batt_pres_dev, GPIO_BATT_PRES_ODL_PORT, - 0), - NULL); - zassert_equal(BP_YES, battery_is_present()); - set_ac_enabled(true); - - /* Wait long enough for TCPMv2 to be idle. */ - msleep(2000); - - /* - * Pretend that the USB-C Port Manager (TCPMv2) has set the port data - * role to disconnected. - */ - usb_charger_task_set_event(0, USB_CHG_EVENT_CC_OPEN); - usb_charger_task_set_event(1, USB_CHG_EVENT_CC_OPEN); - msleep(1); - /* - * Expect the pi3usb9201 driver to configure power down mode and mask - * interrupts. - */ - pi3usb9201_emul_get_reg(emul, PI3USB9201_REG_CTRL_1, &a); - b = PI3USB9201_POWER_DOWN << PI3USB9201_REG_CTRL_1_MODE_SHIFT; - b |= PI3USB9201_REG_CTRL_1_INT_MASK; - zassert_equal(a, b); - - test_bc12_pi3usb9201_host_mode(); - - for (int c = CHG_OTHER; c <= CHG_DCP; c++) { - test_bc12_pi3usb9201_client_mode( - c, bc12_chg_limits[c].supplier, - bc12_chg_limits[c].current_limit); - } -} - -/* - * TODO(b/216660795): Cleanup state using a teardown_fn - */ -static void bc12_after(void *unused) -{ - set_ac_enabled(false); -} - -ZTEST_SUITE(bc12, drivers_predicate_post_main, NULL, NULL, bc12_after, NULL); diff --git a/zephyr/test/drivers/default/src/bma2x2.c b/zephyr/test/drivers/default/src/bma2x2.c index ce6665790d..b66fc9d056 100644 --- a/zephyr/test/drivers/default/src/bma2x2.c +++ b/zephyr/test/drivers/default/src/bma2x2.c @@ -3,19 +3,18 @@ * found in the LICENSE file. */ -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> - +#include "accelgyro.h" #include "common.h" -#include "i2c.h" +#include "driver/accel_bma2x2.h" #include "emul/emul_bma255.h" #include "emul/emul_common_i2c.h" - -#include "accelgyro.h" +#include "i2c.h" #include "motion_sense.h" -#include "driver/accel_bma2x2.h" #include "test/drivers/test_state.h" +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> + /** How accurate comparision of vectors should be. */ #define V_EPS 8 diff --git a/zephyr/test/drivers/default/src/bmi160.c b/zephyr/test/drivers/default/src/bmi160.c index 01957672f7..257040b6dd 100644 --- a/zephyr/test/drivers/default/src/bmi160.c +++ b/zephyr/test/drivers/default/src/bmi160.c @@ -3,20 +3,19 @@ * found in the LICENSE file. */ -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> - #include "common.h" -#include "i2c.h" +#include "driver/accelgyro_bmi160.h" +#include "driver/accelgyro_bmi_common.h" #include "emul/emul_bmi.h" #include "emul/emul_common_i2c.h" -#include "test/drivers/test_mocks.h" - +#include "i2c.h" #include "motion_sense_fifo.h" -#include "driver/accelgyro_bmi160.h" -#include "driver/accelgyro_bmi_common.h" +#include "test/drivers/test_mocks.h" #include "test/drivers/test_state.h" +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> + #define BMI_NODE DT_NODELABEL(accel_bmi160) #define BMI_ACC_SENSOR_ID SENSOR_ID(DT_NODELABEL(ms_bmi160_accel)) #define BMI_GYR_SENSOR_ID SENSOR_ID(DT_NODELABEL(ms_bmi160_gyro)) @@ -2139,9 +2138,9 @@ static void bmi160_before(void *fixture) gyr_ms->rot_standard_ref = NULL; acc_ms->rot_standard_ref = NULL; - zassume_equal(EC_SUCCESS, acc_ms->drv->set_data_rate(acc_ms, 50000, 0), + zassert_equal(EC_SUCCESS, acc_ms->drv->set_data_rate(acc_ms, 50000, 0), NULL); - zassume_equal(EC_SUCCESS, gyr_ms->drv->set_data_rate(gyr_ms, 50000, 0), + zassert_equal(EC_SUCCESS, gyr_ms->drv->set_data_rate(gyr_ms, 50000, 0), NULL); } diff --git a/zephyr/test/drivers/default/src/bmi260.c b/zephyr/test/drivers/default/src/bmi260.c index 5e44d1c499..1bc45b3883 100644 --- a/zephyr/test/drivers/default/src/bmi260.c +++ b/zephyr/test/drivers/default/src/bmi260.c @@ -3,21 +3,20 @@ * found in the LICENSE file. */ -#include <zephyr/fff.h> -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> - #include "common.h" -#include "i2c.h" +#include "driver/accelgyro_bmi260.h" +#include "driver/accelgyro_bmi_common.h" #include "emul/emul_bmi.h" #include "emul/emul_common_i2c.h" - +#include "i2c.h" #include "motion_sense_fifo.h" -#include "driver/accelgyro_bmi260.h" -#include "driver/accelgyro_bmi_common.h" #include "test/drivers/test_mocks.h" #include "test/drivers/test_state.h" +#include <zephyr/fff.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> + #define BMI_NODE DT_NODELABEL(accel_bmi260) #define BMI_ACC_SENSOR_ID SENSOR_ID(DT_NODELABEL(ms_bmi260_accel)) #define BMI_GYR_SENSOR_ID SENSOR_ID(DT_NODELABEL(ms_bmi260_gyro)) diff --git a/zephyr/test/drivers/default/src/bmi_common.c b/zephyr/test/drivers/default/src/bmi_common.c index 302c59d27e..bdd3999d65 100644 --- a/zephyr/test/drivers/default/src/bmi_common.c +++ b/zephyr/test/drivers/default/src/bmi_common.c @@ -3,15 +3,15 @@ * found in the LICENSE file. */ -#include <zephyr/fff.h> -#include <zephyr/ztest.h> - #include "accelgyro_bmi_common.h" -#include "i2c.h" #include "emul/emul_bmi.h" #include "emul/emul_common_i2c.h" +#include "i2c.h" #include "test/drivers/test_state.h" +#include <zephyr/fff.h> +#include <zephyr/ztest.h> + #define BMI_NODE DT_NODELABEL(accel_bmi160) #define BMI_ACC_SENSOR_ID SENSOR_ID(DT_NODELABEL(ms_bmi160_accel)) diff --git a/zephyr/test/drivers/default/src/charge_manager.c b/zephyr/test/drivers/default/src/charge_manager.c index 2729816a76..d5affff95e 100644 --- a/zephyr/test/drivers/default/src/charge_manager.c +++ b/zephyr/test/drivers/default/src/charge_manager.c @@ -3,12 +3,12 @@ * found in the LICENSE file. */ -#include <zephyr/ztest.h> - #include "charge_manager.h" #include "ec_commands.h" #include "test/drivers/test_state.h" +#include <zephyr/ztest.h> + ZTEST_SUITE(charge_manager, drivers_predicate_post_main, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/drivers/default/src/charge_state_prevent_power_on.c b/zephyr/test/drivers/default/src/charge_state_prevent_power_on.c new file mode 100644 index 0000000000..ce47ff8042 --- /dev/null +++ b/zephyr/test/drivers/default/src/charge_state_prevent_power_on.c @@ -0,0 +1,91 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "charge_state.h" +#include "charge_state_v2.h" +#include "test/drivers/test_state.h" + +#include <zephyr/ztest.h> + +/* Test external variable defined in charge_state_v2 */ +extern int charge_prevent_power_on_automatic_power_on; + +struct charge_state_prevent_power_on_fixture { + struct charge_state_data charge_state_backup; + int automatic_power_on; +}; + +static void *setup(void) +{ + static struct charge_state_prevent_power_on_fixture fixture; + + return &fixture; +} + +static void before(void *f) +{ + struct charge_state_prevent_power_on_fixture *fixture = f; + + /* Backup the current state */ + fixture->charge_state_backup = *charge_get_status(); + fixture->automatic_power_on = + charge_prevent_power_on_automatic_power_on; + + /* Reset the automatic_power_on global */ + charge_prevent_power_on_automatic_power_on = 1; +} + +static void after(void *f) +{ + struct charge_state_prevent_power_on_fixture *fixture = f; + + /* Restore the state from 'before' */ + *charge_get_status() = fixture->charge_state_backup; + charge_prevent_power_on_automatic_power_on = + fixture->automatic_power_on; +} + +ZTEST_SUITE(charge_state_prevent_power_on, drivers_predicate_post_main, setup, + before, after, NULL); + +ZTEST(charge_state_prevent_power_on, test_allow_power_on) +{ + struct batt_params *params = &charge_get_status()->batt; + + /* Force a call to refresh the battery parameters */ + params->is_present = BP_NOT_SURE; + /* Set the charge state to be high enough */ + params->state_of_charge = + CONFIG_PLATFORM_EC_CHARGER_MIN_BAT_PCT_FOR_POWER_ON; + + /* Verify that we can power on when the power button was pressed */ + zassert_false(charge_prevent_power_on(true)); +} + +ZTEST(charge_state_prevent_power_on, test_low_charge) +{ + struct batt_params *params = &charge_get_status()->batt; + + /* Force a low charge state */ + params->state_of_charge = + CONFIG_PLATFORM_EC_CHARGER_MIN_BAT_PCT_FOR_POWER_ON - 1; + + /* Verify that we cannot power on during an automatic power-on */ + zassert_true(charge_prevent_power_on(false)); +} + +ZTEST(charge_state_prevent_power_on, test_consuming_full_input_current) +{ + struct batt_params *params = &charge_get_status()->batt; + + params->state_of_charge = 50; + zassert_true(charge_is_consuming_full_input_current()); + + params->state_of_charge = 0; + zassert_false(charge_is_consuming_full_input_current()); + + params->state_of_charge = 100; + zassert_false(charge_is_consuming_full_input_current()); +} diff --git a/zephyr/test/drivers/default/src/chipset.c b/zephyr/test/drivers/default/src/chipset.c new file mode 100644 index 0000000000..89ba761c23 --- /dev/null +++ b/zephyr/test/drivers/default/src/chipset.c @@ -0,0 +1,101 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "chipset.h" +#include "console.h" +#include "test/drivers/test_state.h" + +#include <stdint.h> + +#include <zephyr/fff.h> +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +#include <ap_power/ap_power.h> +#include <ap_power/ap_power_events.h> + +ZTEST(chipset, test_get_ap_reset_stats__bad_pointers) +{ + zassert_equal(EC_ERROR_INVAL, get_ap_reset_stats(NULL, 0, NULL)); +} + +ZTEST(chipset, test_get_ap_reset_stats__happy_path) +{ + struct ap_reset_log_entry reset_log_entries[4]; + uint32_t actual_reset_count, reset_count; + + memset(reset_log_entries, 0, sizeof(reset_log_entries)); + + /* Report two AP resets */ + report_ap_reset(CHIPSET_RESET_AP_WATCHDOG); + report_ap_reset(CHIPSET_RESET_HANG_REBOOT); + + zassert_equal(EC_SUCCESS, + get_ap_reset_stats(reset_log_entries, + ARRAY_SIZE(reset_log_entries), + &reset_count)); + + /* Check the reset causes. The reset entry log is not a FIFO, so we get + * the last two empty slots followed by the two we triggered above. + */ + zassert_equal(0, reset_log_entries[0].reset_cause); + zassert_equal(0, reset_log_entries[1].reset_cause); + zassert_equal(CHIPSET_RESET_AP_WATCHDOG, + reset_log_entries[2].reset_cause); + zassert_equal(CHIPSET_RESET_HANG_REBOOT, + reset_log_entries[3].reset_cause); + + /* Check reset count */ + actual_reset_count = test_chipset_get_ap_resets_since_ec_boot(); + zassert_equal(actual_reset_count, reset_count, + "Found %d resets, expected %d", reset_count, + actual_reset_count); +} + +ZTEST(chipset, test_console_cmd_apreset) +{ + struct ap_reset_log_entry reset_log_entries[4]; + uint32_t reset_count; + + zassert_ok(shell_execute_cmd(get_ec_shell(), "apreset")); + + /* Make sure an AP reset happened. The expected reset log entry is at + * index 3 because we read out 3 empty slots first. + */ + zassert_ok(get_ap_reset_stats(reset_log_entries, + ARRAY_SIZE(reset_log_entries), + &reset_count)); + + zassert_equal(CHIPSET_RESET_CONSOLE_CMD, + reset_log_entries[3].reset_cause); +} + +ZTEST(chipset, test_console_cmd_apshutdown) +{ + struct ap_reset_log_entry reset_log_entries[4]; + uint32_t reset_count; + + zassert_ok(shell_execute_cmd(get_ec_shell(), "apshutdown")); + + /* Make sure an AP reset happened. The expected reset log entry is at + * index 3 because we read out 3 empty slots first. + */ + zassert_ok(get_ap_reset_stats(reset_log_entries, + ARRAY_SIZE(reset_log_entries), + &reset_count)); + + zassert_equal(CHIPSET_SHUTDOWN_CONSOLE_CMD, + reset_log_entries[3].reset_cause); +} + +static void reset(void *arg) +{ + ARG_UNUSED(arg); + + test_chipset_corrupt_reset_log_checksum(); + init_reset_log(); +} + +ZTEST_SUITE(chipset, drivers_predicate_post_main, NULL, reset, reset, NULL); diff --git a/zephyr/test/drivers/default/src/console.c b/zephyr/test/drivers/default/src/console.c index 9e6c55c198..c5148baf76 100644 --- a/zephyr/test/drivers/default/src/console.c +++ b/zephyr/test/drivers/default/src/console.c @@ -3,15 +3,15 @@ * found in the LICENSE file. */ -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> -#include <zephyr/shell/shell_dummy.h> - #include "builtin/stdio.h" -#include "test/drivers/test_state.h" #include "console.h" -#include "uart.h" #include "ec_commands.h" +#include "test/drivers/test_state.h" +#include "uart.h" + +#include <zephyr/kernel.h> +#include <zephyr/shell/shell_dummy.h> +#include <zephyr/ztest.h> ZTEST_USER(console, test_printf_overflow) { diff --git a/zephyr/test/drivers/default/src/console_cmd/accelinfo.c b/zephyr/test/drivers/default/src/console_cmd/accelinfo.c index 39556fab43..aac63c01b7 100644 --- a/zephyr/test/drivers/default/src/console_cmd/accelinfo.c +++ b/zephyr/test/drivers/default/src/console_cmd/accelinfo.c @@ -3,15 +3,15 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "config.h" #include "console.h" #include "ec_commands.h" #include "test/drivers/test_state.h" #include "timer.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + static void console_cmd_accelinfo_after(void *fixture) { ARG_UNUSED(fixture); diff --git a/zephyr/test/drivers/default/src/console_cmd/accelinit.c b/zephyr/test/drivers/default/src/console_cmd/accelinit.c index e722409298..2633c05f58 100644 --- a/zephyr/test/drivers/default/src/console_cmd/accelinit.c +++ b/zephyr/test/drivers/default/src/console_cmd/accelinit.c @@ -3,16 +3,16 @@ * found in the LICENSE file. */ -#include <zephyr/fff.h> -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "accelgyro.h" #include "console.h" #include "motion_sense.h" #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include <zephyr/fff.h> +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + FAKE_VALUE_FUNC(int, mock_init, struct motion_sensor_t *); struct console_cmd_accelinit_fixture { diff --git a/zephyr/test/drivers/default/src/console_cmd/accelrange.c b/zephyr/test/drivers/default/src/console_cmd/accelrange.c index 57e81eada4..04ec7e6004 100644 --- a/zephyr/test/drivers/default/src/console_cmd/accelrange.c +++ b/zephyr/test/drivers/default/src/console_cmd/accelrange.c @@ -3,10 +3,6 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/devicetree.h> -#include <zephyr/ztest.h> - #include "console.h" #include "driver/accel_bma2x2.h" #include "ec_commands.h" @@ -16,6 +12,10 @@ #include "motion_sense.h" #include "test/drivers/test_state.h" +#include <zephyr/devicetree.h> +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + #define EMUL_NODE DT_NODELABEL(bma_emul) #define BMA_ORD DT_DEP_ORD(EMUL_LABEL) diff --git a/zephyr/test/drivers/default/src/console_cmd/accelrate.c b/zephyr/test/drivers/default/src/console_cmd/accelrate.c index ca5bcf338a..3a5b2cd9bf 100644 --- a/zephyr/test/drivers/default/src/console_cmd/accelrate.c +++ b/zephyr/test/drivers/default/src/console_cmd/accelrate.c @@ -3,15 +3,15 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "console.h" #include "ec_commands.h" #include "motion_sense.h" #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + static int original_sensor_0_s0_config_odr; static void *console_cmd_accelrate_setup(void) diff --git a/zephyr/test/drivers/default/src/console_cmd/accelread.c b/zephyr/test/drivers/default/src/console_cmd/accelread.c index 4a129badc1..e622faf58f 100644 --- a/zephyr/test/drivers/default/src/console_cmd/accelread.c +++ b/zephyr/test/drivers/default/src/console_cmd/accelread.c @@ -3,10 +3,6 @@ * found in the LICENSE file. */ -#include <zephyr/fff.h> -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "accelgyro.h" #include "console.h" #include "ec_commands.h" @@ -14,6 +10,10 @@ #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include <zephyr/fff.h> +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + FAKE_VALUE_FUNC(int, mock_read, const struct motion_sensor_t *, int *); FAKE_VALUE_FUNC(int, mock_set_data_rate, const struct motion_sensor_t *, int, int); diff --git a/zephyr/test/drivers/default/src/console_cmd/accelres.c b/zephyr/test/drivers/default/src/console_cmd/accelres.c index 38bd4ed697..21c0075b0f 100644 --- a/zephyr/test/drivers/default/src/console_cmd/accelres.c +++ b/zephyr/test/drivers/default/src/console_cmd/accelres.c @@ -3,10 +3,6 @@ * found in the LICENSE file. */ -#include <zephyr/fff.h> -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "accelgyro.h" #include "console.h" #include "driver/accel_bma2x2.h" @@ -14,6 +10,10 @@ #include "motion_sense.h" #include "test/drivers/test_state.h" +#include <zephyr/fff.h> +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + FAKE_VALUE_FUNC(int, set_resolution, const struct motion_sensor_t *, int, int); struct console_cmd_accelres_fixture { diff --git a/zephyr/test/drivers/default/src/console_cmd/accelspoof.c b/zephyr/test/drivers/default/src/console_cmd/accelspoof.c index 50f8c1e650..fe11525531 100644 --- a/zephyr/test/drivers/default/src/console_cmd/accelspoof.c +++ b/zephyr/test/drivers/default/src/console_cmd/accelspoof.c @@ -3,14 +3,14 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "console.h" #include "ec_commands.h" #include "motion_sense.h" #include "test/drivers/test_state.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + static void console_cmd_accelspoof_after(void *fixture) { ARG_UNUSED(fixture); diff --git a/zephyr/test/drivers/default/src/console_cmd/adc.c b/zephyr/test/drivers/default/src/console_cmd/adc.c index 85dfda939a..b366db36f6 100644 --- a/zephyr/test/drivers/default/src/console_cmd/adc.c +++ b/zephyr/test/drivers/default/src/console_cmd/adc.c @@ -3,14 +3,14 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "console.h" #include "ec_commands.h" #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + /* Default adc command, lists out channels */ ZTEST_USER(console_cmd_adc, test_adc_noname) { diff --git a/zephyr/test/drivers/default/src/console_cmd/battery.c b/zephyr/test/drivers/default/src/console_cmd/battery.c index 9c3e21fcf1..f5fa78d684 100644 --- a/zephyr/test/drivers/default/src/console_cmd/battery.c +++ b/zephyr/test/drivers/default/src/console_cmd/battery.c @@ -3,10 +3,6 @@ * found in the LICENSE file. */ -#include <zephyr/drivers/emul.h> -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "battery_smart.h" #include "console.h" #include "ec_commands.h" @@ -15,6 +11,10 @@ #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include <zephyr/drivers/emul.h> +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + struct console_cmd_battery_fixture { const struct emul *emul; struct i2c_common_emul_data *i2c_emul; diff --git a/zephyr/test/drivers/default/src/console_cmd/button.c b/zephyr/test/drivers/default/src/console_cmd/button.c index 9272b2ce2d..0df137e90b 100644 --- a/zephyr/test/drivers/default/src/console_cmd/button.c +++ b/zephyr/test/drivers/default/src/console_cmd/button.c @@ -3,14 +3,14 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "console.h" #include "ec_commands.h" #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + ZTEST_USER(console_cmd_button, test_button_no_arg) { int rv; diff --git a/zephyr/test/drivers/default/src/console_cmd/cbi.c b/zephyr/test/drivers/default/src/console_cmd/cbi.c index 495ffd7e4c..297431657e 100644 --- a/zephyr/test/drivers/default/src/console_cmd/cbi.c +++ b/zephyr/test/drivers/default/src/console_cmd/cbi.c @@ -3,12 +3,12 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "console.h" #include "test/drivers/test_state.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + static void set_wp(bool value) { const struct gpio_dt_spec *wp = GPIO_DT_FROM_NODELABEL(gpio_wp_l); diff --git a/zephyr/test/drivers/default/src/console_cmd/charge_manager.c b/zephyr/test/drivers/default/src/console_cmd/charge_manager.c index a4679ddd25..698db8b316 100644 --- a/zephyr/test/drivers/default/src/console_cmd/charge_manager.c +++ b/zephyr/test/drivers/default/src/console_cmd/charge_manager.c @@ -3,9 +3,6 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "charge_manager.h" #include "console.h" #include "emul/emul_isl923x.h" @@ -15,6 +12,9 @@ #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + struct console_cmd_charge_manager_fixture { struct tcpci_partner_data sink_5v_3a; struct tcpci_snk_emul_data sink_ext; @@ -111,7 +111,7 @@ ZTEST_USER(console_cmd_charge_manager, test_chgoverride_invalid_port) { char cmd[256]; - zassume_true(sprintf(cmd, "chgoverride %d", CHARGE_PORT_COUNT) > 0, + zassert_true(sprintf(cmd, "chgoverride %d", CHARGE_PORT_COUNT) > 0, NULL); zassert_equal(shell_execute_cmd(get_ec_shell(), cmd), EC_ERROR_PARAM1, NULL); diff --git a/zephyr/test/drivers/default/src/console_cmd/charge_state.c b/zephyr/test/drivers/default/src/console_cmd/charge_state.c index 7ee91049ae..8cfd11324f 100644 --- a/zephyr/test/drivers/default/src/console_cmd/charge_state.c +++ b/zephyr/test/drivers/default/src/console_cmd/charge_state.c @@ -3,9 +3,6 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "charge_state.h" #include "charge_state_v2.h" #include "console.h" @@ -13,6 +10,9 @@ #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + ZTEST_USER(console_cmd_charge_state, test_idle_too_few_args) { int rv; @@ -116,7 +116,7 @@ ZTEST_USER(console_cmd_charge_state, test_debug_on_show_charging_progress) charging_progress_displayed(); /* Enable debug printing */ - zassume_ok(shell_execute_cmd(get_ec_shell(), "chgstate debug on"), + zassert_ok(shell_execute_cmd(get_ec_shell(), "chgstate debug on"), NULL); /* Sleep at least 1 full iteration of the charge state loop */ @@ -159,6 +159,16 @@ ZTEST_USER(console_cmd_charge_state, test_sustain_invalid_params) zassert_equal(shell_execute_cmd(get_ec_shell(), "chgstate sustain 50 101"), EC_ERROR_INVAL, NULL); + + /* Verify invalid lower bound (not a number) */ + zassert_equal(shell_execute_cmd(get_ec_shell(), + "chgstate sustain a 50"), + EC_ERROR_PARAM2); + + /* Verify invalid uppoer bound (not a number) */ + zassert_equal(shell_execute_cmd(get_ec_shell(), + "chgstate sustain 30 a"), + EC_ERROR_PARAM3); } struct console_cmd_charge_state_fixture { @@ -206,7 +216,7 @@ ZTEST_USER_F(console_cmd_charge_state, test_idle_on_from_normal) fixture->tcpci_emul, fixture->charger_emul); /* Verify that we're in "normal" mode */ - zassume_equal(get_chg_ctrl_mode(), CHARGE_CONTROL_NORMAL); + zassert_equal(get_chg_ctrl_mode(), CHARGE_CONTROL_NORMAL); /* Move to idle */ zassert_ok(shell_execute_cmd(get_ec_shell(), "chgstate idle on")); @@ -220,11 +230,11 @@ ZTEST_USER_F(console_cmd_charge_state, test_normal_from_idle) fixture->tcpci_emul, fixture->charger_emul); /* Verify that we're in "normal" mode */ - zassume_equal(get_chg_ctrl_mode(), CHARGE_CONTROL_NORMAL); + zassert_equal(get_chg_ctrl_mode(), CHARGE_CONTROL_NORMAL); /* Move to idle */ - zassume_ok(shell_execute_cmd(get_ec_shell(), "chgstate idle on")); - zassume_equal(get_chg_ctrl_mode(), CHARGE_CONTROL_IDLE); + zassert_ok(shell_execute_cmd(get_ec_shell(), "chgstate idle on")); + zassert_equal(get_chg_ctrl_mode(), CHARGE_CONTROL_IDLE); /* Move back to normal */ zassert_ok(shell_execute_cmd(get_ec_shell(), "chgstate idle off"), @@ -239,7 +249,7 @@ ZTEST_USER_F(console_cmd_charge_state, test_discharge_on) fixture->tcpci_emul, fixture->charger_emul); /* Verify that we're in "normal" mode */ - zassume_equal(get_chg_ctrl_mode(), CHARGE_CONTROL_NORMAL); + zassert_equal(get_chg_ctrl_mode(), CHARGE_CONTROL_NORMAL); /* Enable discharge */ zassert_ok(shell_execute_cmd(get_ec_shell(), "chgstate discharge on"), @@ -254,12 +264,12 @@ ZTEST_USER_F(console_cmd_charge_state, test_discharge_off) fixture->tcpci_emul, fixture->charger_emul); /* Verify that we're in "normal" mode */ - zassume_equal(get_chg_ctrl_mode(), CHARGE_CONTROL_NORMAL); + zassert_equal(get_chg_ctrl_mode(), CHARGE_CONTROL_NORMAL); /* Enable discharge */ - zassume_ok(shell_execute_cmd(get_ec_shell(), "chgstate discharge on"), + zassert_ok(shell_execute_cmd(get_ec_shell(), "chgstate discharge on"), NULL); - zassume_equal(get_chg_ctrl_mode(), CHARGE_CONTROL_DISCHARGE); + zassert_equal(get_chg_ctrl_mode(), CHARGE_CONTROL_DISCHARGE); /* Disable discharge */ zassert_ok(shell_execute_cmd(get_ec_shell(), "chgstate discharge off"), diff --git a/zephyr/test/drivers/default/src/console_cmd/chargen.c b/zephyr/test/drivers/default/src/console_cmd/chargen.c index 649a1b0b1f..473ad78abb 100644 --- a/zephyr/test/drivers/default/src/console_cmd/chargen.c +++ b/zephyr/test/drivers/default/src/console_cmd/chargen.c @@ -3,17 +3,18 @@ * found in the LICENSE file. */ -#include <zephyr/drivers/emul.h> -#include <zephyr/shell/shell_dummy.h> -#include <zephyr/ztest.h> -#include <stdio.h> -#include <zephyr/drivers/uart/serial_test.h> - #include "console.h" -#include "uart.h" +#include "system.h" #include "test/drivers/test_state.h" #include "test/drivers/utils.h" -#include "system.h" +#include "uart.h" + +#include <stdio.h> + +#include <zephyr/drivers/emul.h> +#include <zephyr/drivers/uart/serial_test.h> +#include <zephyr/shell/shell_dummy.h> +#include <zephyr/ztest.h> const char expected_output[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" diff --git a/zephyr/test/drivers/default/src/console_cmd/charger.c b/zephyr/test/drivers/default/src/console_cmd/charger.c index 9adda29a8d..420ef67fe4 100644 --- a/zephyr/test/drivers/default/src/console_cmd/charger.c +++ b/zephyr/test/drivers/default/src/console_cmd/charger.c @@ -3,15 +3,15 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "console.h" #include "dptf.h" #include "ec_commands.h" #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + /* Tests which need no fixture */ ZTEST_USER(console_cmd_charger, test_default_dump) { @@ -81,7 +81,7 @@ ZTEST_USER(console_cmd_charger, test_good_input_current) zassert_ok(shell_execute_cmd(get_ec_shell(), "charger input 1000"), "Failed to set input current"); - zassume_ok(charger_get_input_current_limit(0, &input_current), + zassert_ok(charger_get_input_current_limit(0, &input_current), "Failed to get input current"); zassert_equal(input_current, 1000, "Input current not set in charger: %d", input_current); @@ -117,7 +117,7 @@ static void *console_cmd_charger_setup(void) static struct console_cmd_charger_fixture fixture; /* Assume we have one charger at index 0 */ - zassume_true(board_get_charger_chip_count() > 0, + zassert_true(board_get_charger_chip_count() > 0, "Insufficient chargers found"); /* Get references for the emulators */ @@ -158,7 +158,7 @@ ZTEST_USER_F(console_cmd_charger, test_good_current) /* Give the charger task time to pick up the manual current */ k_sleep(K_SECONDS(1)); - zassume_ok(charger_get_current(0, ¤t), "Failed to get current"); + zassert_ok(charger_get_current(0, ¤t), "Failed to get current"); zassert_equal(current, 1000, "Current not set in charger: %d", current); } @@ -176,7 +176,7 @@ ZTEST_USER_F(console_cmd_charger, test_good_voltage) /* Give the charger task time to pick up the manual voltage */ k_sleep(K_SECONDS(1)); - zassume_ok(charger_get_voltage(0, &voltage), "Failed to get voltage"); + zassert_ok(charger_get_voltage(0, &voltage), "Failed to get voltage"); zassert_equal(voltage, 3000, "Voltage not set in charger: %d", voltage); } diff --git a/zephyr/test/drivers/default/src/console_cmd/crash.c b/zephyr/test/drivers/default/src/console_cmd/crash.c index bc0b5d0254..4218aa74d6 100644 --- a/zephyr/test/drivers/default/src/console_cmd/crash.c +++ b/zephyr/test/drivers/default/src/console_cmd/crash.c @@ -3,14 +3,14 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "builtin/assert.h" #include "console.h" #include "test/drivers/test_mocks.h" #include "test/drivers/test_state.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + ZTEST_SUITE(console_cmd_crash, drivers_predicate_post_main, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/drivers/default/src/console_cmd/cutoff.c b/zephyr/test/drivers/default/src/console_cmd/cutoff.c index 00ce40660f..2b5a9c67d2 100644 --- a/zephyr/test/drivers/default/src/console_cmd/cutoff.c +++ b/zephyr/test/drivers/default/src/console_cmd/cutoff.c @@ -3,9 +3,6 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "battery.h" #include "console.h" #include "ec_commands.h" @@ -13,6 +10,9 @@ #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + static void console_cmd_cutoff_after(void *unused) { ARG_UNUSED(unused); @@ -58,7 +58,7 @@ ZTEST_USER(console_cmd_cutoff, test_clear_pending_shutdown) { int rv = shell_execute_cmd(get_ec_shell(), "cutoff at-shutdown"); - zassume_true(extpower_is_present(), NULL); + zassert_true(extpower_is_present(), NULL); zassert_equal(EC_RES_SUCCESS, rv, "Expected %d, but got %d", EC_RES_SUCCESS, rv); diff --git a/zephyr/test/drivers/default/src/console_cmd/ec_features.c b/zephyr/test/drivers/default/src/console_cmd/ec_features.c index b33ca565bc..48e6eec819 100644 --- a/zephyr/test/drivers/default/src/console_cmd/ec_features.c +++ b/zephyr/test/drivers/default/src/console_cmd/ec_features.c @@ -3,23 +3,27 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - +#include "builtin/stdio.h" #include "console.h" #include "host_command.h" #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + ZTEST_SUITE(console_cmd_ec_features, drivers_predicate_post_main, NULL, NULL, NULL, NULL); ZTEST_USER(console_cmd_ec_features, test_feat) { - char expected[32]; + char expected[50]; + int ret; - snprintf(expected, sizeof(expected), " 0-31: 0x%08x\r\n32-63: 0x%08x", - get_feature_flags0(), get_feature_flags1()); + ret = snprintf(expected, sizeof(expected), + " 0-31: 0x%08x\r\n32-63: 0x%08x", get_feature_flags0(), + get_feature_flags1()); + zassert_true(ret >= 0 && ret < sizeof(expected)); CHECK_CONSOLE_CMD("feat", expected, EC_SUCCESS); } diff --git a/zephyr/test/drivers/default/src/console_cmd/gpio.c b/zephyr/test/drivers/default/src/console_cmd/gpio.c index 164f272e27..c4b83b8ab4 100644 --- a/zephyr/test/drivers/default/src/console_cmd/gpio.c +++ b/zephyr/test/drivers/default/src/console_cmd/gpio.c @@ -3,14 +3,14 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "console.h" #include "ec_commands.h" #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + ZTEST_USER(console_cmd_gpio, test_read_invoke_success) { zassert_ok(shell_execute_cmd(get_ec_shell(), "gpioget test"), NULL); diff --git a/zephyr/test/drivers/default/src/console_cmd/hcdebug.c b/zephyr/test/drivers/default/src/console_cmd/hcdebug.c index 71adb02690..68dc6c72be 100644 --- a/zephyr/test/drivers/default/src/console_cmd/hcdebug.c +++ b/zephyr/test/drivers/default/src/console_cmd/hcdebug.c @@ -3,13 +3,13 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "console.h" #include "ec_commands.h" #include "test/drivers/test_state.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + static void console_cmd_hcdebug_after(void *fixture) { ARG_UNUSED(fixture); diff --git a/zephyr/test/drivers/default/src/console_cmd/hibdelay.c b/zephyr/test/drivers/default/src/console_cmd/hibdelay.c index c72a2bf66a..8275ece94d 100644 --- a/zephyr/test/drivers/default/src/console_cmd/hibdelay.c +++ b/zephyr/test/drivers/default/src/console_cmd/hibdelay.c @@ -3,13 +3,13 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "console.h" #include "ec_commands.h" #include "test/drivers/test_state.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + ZTEST_SUITE(console_cmd_hibdelay, drivers_predicate_post_main, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/drivers/default/src/console_cmd/hostevent.c b/zephyr/test/drivers/default/src/console_cmd/hostevent.c index af9b37edd1..85717e88d1 100644 --- a/zephyr/test/drivers/default/src/console_cmd/hostevent.c +++ b/zephyr/test/drivers/default/src/console_cmd/hostevent.c @@ -3,15 +3,15 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "console.h" #include "ec_commands.h" #include "include/lpc.h" #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + #ifdef CONFIG_HOST_EVENT64 #define HOSTEVENT_PRINT_FORMAT "016" PRIx64 #else @@ -52,7 +52,7 @@ static int console_cmd_hostevent(const char *subcommand, host_event_t mask) "hostevent %s 0x%" HOSTEVENT_PRINT_FORMAT, subcommand, mask); - zassume_between_inclusive(rv, 0, CONFIG_SHELL_CMD_BUFF_SIZE, + zassert_between_inclusive(rv, 0, CONFIG_SHELL_CMD_BUFF_SIZE, "hostevent console command too long"); return shell_execute_cmd(get_ec_shell(), cmd_buf); diff --git a/zephyr/test/drivers/default/src/console_cmd/i2c_portmap.c b/zephyr/test/drivers/default/src/console_cmd/i2c_portmap.c index 4b2ec548a2..7651127218 100644 --- a/zephyr/test/drivers/default/src/console_cmd/i2c_portmap.c +++ b/zephyr/test/drivers/default/src/console_cmd/i2c_portmap.c @@ -3,13 +3,13 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "console.h" #include "ec_commands.h" #include "test/drivers/test_state.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + ZTEST_SUITE(console_cmd_i2c_portmap, drivers_predicate_post_main, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/drivers/default/src/console_cmd/md.c b/zephyr/test/drivers/default/src/console_cmd/md.c index c8c3e2c130..0db049a635 100644 --- a/zephyr/test/drivers/default/src/console_cmd/md.c +++ b/zephyr/test/drivers/default/src/console_cmd/md.c @@ -3,12 +3,12 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "console.h" #include "test/drivers/test_state.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + ZTEST_SUITE(console_cmd_md, drivers_predicate_post_main, NULL, NULL, NULL, NULL); @@ -36,7 +36,7 @@ ZTEST_USER(console_cmd_md, test_default_count) uint8_t memory[] = { 0x01, 0x02, 0x03, 0x04 }; char cmd[128] = { 0 }; - zassume_true(sprintf(cmd, "md %" PRIuPTR, (uintptr_t)memory) != 0, + zassert_true(sprintf(cmd, "md %" PRIuPTR, (uintptr_t)memory) != 0, NULL); zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); } @@ -46,7 +46,7 @@ ZTEST_USER(console_cmd_md, test_count_arg) uint8_t memory[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; char cmd[128] = { 0 }; - zassume_true(sprintf(cmd, "md %" PRIuPTR " 2", (uintptr_t)memory) != 0, + zassert_true(sprintf(cmd, "md %" PRIuPTR " 2", (uintptr_t)memory) != 0, NULL); zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); } @@ -56,7 +56,7 @@ ZTEST_USER(console_cmd_md, test_byte_format) uint8_t memory[] = { 0x01, 0x02, 0x03, 0x04 }; char cmd[128] = { 0 }; - zassume_true(sprintf(cmd, "md .b %" PRIuPTR, (uintptr_t)memory) != 0, + zassert_true(sprintf(cmd, "md .b %" PRIuPTR, (uintptr_t)memory) != 0, NULL); zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); } @@ -66,7 +66,7 @@ ZTEST_USER(console_cmd_md, test_half_format) uint8_t memory[] = { 0x01, 0x02, 0x03, 0x04 }; char cmd[128] = { 0 }; - zassume_true(sprintf(cmd, "md .h %" PRIuPTR, (uintptr_t)memory) != 0, + zassert_true(sprintf(cmd, "md .h %" PRIuPTR, (uintptr_t)memory) != 0, NULL); zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); } @@ -76,7 +76,7 @@ ZTEST_USER(console_cmd_md, test_string_format) char memory[] = "hello world"; char cmd[128] = { 0 }; - zassume_true(sprintf(cmd, "md .s %" PRIuPTR " 12", (uintptr_t)memory) != + zassert_true(sprintf(cmd, "md .s %" PRIuPTR " 12", (uintptr_t)memory) != 0, NULL); zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); diff --git a/zephyr/test/drivers/default/src/console_cmd/panic_output.c b/zephyr/test/drivers/default/src/console_cmd/panic_output.c index 7cc809e835..0d98ec6d08 100644 --- a/zephyr/test/drivers/default/src/console_cmd/panic_output.c +++ b/zephyr/test/drivers/default/src/console_cmd/panic_output.c @@ -3,15 +3,15 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "console.h" #include "ec_commands.h" #include "panic.h" #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + /* Test panicinfo when a panic hasn't occurred */ ZTEST_USER(console_cmd_panic_output, test_panicinfo) { diff --git a/zephyr/test/drivers/default/src/console_cmd/port80.c b/zephyr/test/drivers/default/src/console_cmd/port80.c index 792895eb27..1d7eb2ad03 100644 --- a/zephyr/test/drivers/default/src/console_cmd/port80.c +++ b/zephyr/test/drivers/default/src/console_cmd/port80.c @@ -8,18 +8,17 @@ * @brief Unit Tests for ESPI port 80 console command */ -#include <zephyr/logging/log.h> -#include <zephyr/shell/shell.h> -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> - #include "console.h" #include "ec_commands.h" #include "port80.h" - #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include <zephyr/kernel.h> +#include <zephyr/logging/log.h> +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + /** * @brief TestPurpose: Verify port 80 console commands * diff --git a/zephyr/test/drivers/default/src/console_cmd/power_button.c b/zephyr/test/drivers/default/src/console_cmd/power_button.c index 92d0aeaf78..d9900e8667 100644 --- a/zephyr/test/drivers/default/src/console_cmd/power_button.c +++ b/zephyr/test/drivers/default/src/console_cmd/power_button.c @@ -4,6 +4,7 @@ */ #include <zephyr/ztest.h> + #include <console.h> ZTEST_SUITE(console_cmd_power_button, NULL, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/drivers/default/src/console_cmd/powerindebug.c b/zephyr/test/drivers/default/src/console_cmd/powerindebug.c index 9f52a9b569..946960ea86 100644 --- a/zephyr/test/drivers/default/src/console_cmd/powerindebug.c +++ b/zephyr/test/drivers/default/src/console_cmd/powerindebug.c @@ -3,14 +3,14 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "console.h" #include "ec_commands.h" #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + ZTEST_USER(console_cmd_powerindebug, test_no_params) { zassert_ok(shell_execute_cmd(get_ec_shell(), "powerindebug"), diff --git a/zephyr/test/drivers/default/src/console_cmd/pwr_avg.c b/zephyr/test/drivers/default/src/console_cmd/pwr_avg.c new file mode 100644 index 0000000000..b21755a465 --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/pwr_avg.c @@ -0,0 +1,47 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "battery.h" +#include "console.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +#include <stdio.h> + +#include <zephyr/shell/shell.h> +#include <zephyr/shell/shell_dummy.h> +#include <zephyr/ztest.h> + +ZTEST_SUITE(console_cmd_pwr_avg, drivers_predicate_post_main, NULL, NULL, NULL, + NULL); + +ZTEST_USER(console_cmd_pwr_avg, test_too_many_args) +{ + zassert_equal(EC_ERROR_PARAM_COUNT, + shell_execute_cmd(get_ec_shell(), "pwr_avg 5")); +} + +ZTEST_USER(console_cmd_pwr_avg, test_printout) +{ + int mv = battery_get_avg_voltage(); + int ma = battery_get_avg_current(); + char expected_output[1024]; + const char *buffer; + size_t buffer_size; + + shell_backend_dummy_clear_output(get_ec_shell()); + zassert_ok(shell_execute_cmd(get_ec_shell(), "pwr_avg")); + + buffer = shell_backend_dummy_get_output(get_ec_shell(), &buffer_size); + + sprintf(expected_output, "mv = %d", mv); + zassert_true(strstr(buffer, expected_output)); + + sprintf(expected_output, "ma = %d", ma); + zassert_true(strstr(buffer, expected_output)); + + sprintf(expected_output, "mw = %d", mv * ma / 1000); + zassert_true(strstr(buffer, expected_output)); +} diff --git a/zephyr/test/drivers/default/src/console_cmd/rtc.c b/zephyr/test/drivers/default/src/console_cmd/rtc.c index 80530129af..974d3709b7 100644 --- a/zephyr/test/drivers/default/src/console_cmd/rtc.c +++ b/zephyr/test/drivers/default/src/console_cmd/rtc.c @@ -3,14 +3,14 @@ * found in the LICENSE file. */ -#include <zephyr/ztest.h> - #include "console.h" #include "ec_commands.h" #include "system.h" #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include <zephyr/ztest.h> + ZTEST_USER(console_cmd_rtc, test_rtc_no_arg) { char expected_buffer[32]; diff --git a/zephyr/test/drivers/default/src/console_cmd/rw.c b/zephyr/test/drivers/default/src/console_cmd/rw.c index aac37680f5..1560254f92 100644 --- a/zephyr/test/drivers/default/src/console_cmd/rw.c +++ b/zephyr/test/drivers/default/src/console_cmd/rw.c @@ -3,12 +3,12 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "console.h" #include "test/drivers/test_state.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + ZTEST_SUITE(console_cmd_rw, drivers_predicate_post_main, NULL, NULL, NULL, NULL); @@ -43,15 +43,15 @@ ZTEST_USER(console_cmd_rw, test_read) uint8_t memory[] = { 0x01, 0x02, 0x03, 0x04 }; char cmd[128] = { 0 }; - zassume_true(sprintf(cmd, "rw .b %" PRIuPTR, (uintptr_t)memory) != 0, + zassert_true(sprintf(cmd, "rw .b %" PRIuPTR, (uintptr_t)memory) != 0, NULL); zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); - zassume_true(sprintf(cmd, "rw .h %" PRIuPTR, (uintptr_t)memory) != 0, + zassert_true(sprintf(cmd, "rw .h %" PRIuPTR, (uintptr_t)memory) != 0, NULL); zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); - zassume_true(sprintf(cmd, "rw %" PRIuPTR, (uintptr_t)memory) != 0, + zassert_true(sprintf(cmd, "rw %" PRIuPTR, (uintptr_t)memory) != 0, NULL); zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); } @@ -71,7 +71,7 @@ ZTEST_USER(console_cmd_rw, test_write) uint8_t memory[4] = { 0 }; char cmd[128] = { 0 }; - zassume_true(sprintf(cmd, "rw .b %" PRIuPTR " 1", (uintptr_t)memory) != + zassert_true(sprintf(cmd, "rw .b %" PRIuPTR " 1", (uintptr_t)memory) != 0, NULL); zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); @@ -81,7 +81,7 @@ ZTEST_USER(console_cmd_rw, test_write) zassert_equal(0, memory[3], "memory[3] was %u", memory[3]); memset(memory, 0, 4); - zassume_true(sprintf(cmd, "rw .h %" PRIuPTR " 258", + zassert_true(sprintf(cmd, "rw .h %" PRIuPTR " 258", (uintptr_t)memory) != 0, NULL); zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); @@ -91,7 +91,7 @@ ZTEST_USER(console_cmd_rw, test_write) zassert_equal(0, memory[3], "memory[3] was %u", memory[3]); memset(memory, 0, 4); - zassume_true(sprintf(cmd, "rw %" PRIuPTR " 16909060", + zassert_true(sprintf(cmd, "rw %" PRIuPTR " 16909060", (uintptr_t)memory) != 0, NULL); zassert_ok(shell_execute_cmd(get_ec_shell(), cmd), NULL); diff --git a/zephyr/test/drivers/default/src/console_cmd/shared_mem.c b/zephyr/test/drivers/default/src/console_cmd/shared_mem.c index e7b9396509..ed796124e4 100644 --- a/zephyr/test/drivers/default/src/console_cmd/shared_mem.c +++ b/zephyr/test/drivers/default/src/console_cmd/shared_mem.c @@ -3,14 +3,14 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "console.h" #include "shared_mem.h" #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + ZTEST_SUITE(console_cmd_shared_mem, drivers_predicate_post_main, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/drivers/default/src/console_cmd/sleepmask.c b/zephyr/test/drivers/default/src/console_cmd/sleepmask.c index 6ae017dc66..40d174e6af 100644 --- a/zephyr/test/drivers/default/src/console_cmd/sleepmask.c +++ b/zephyr/test/drivers/default/src/console_cmd/sleepmask.c @@ -3,14 +3,14 @@ * found in the LICENSE file. */ -#include <zephyr/drivers/emul.h> -#include <zephyr/shell/shell_dummy.h> -#include <zephyr/ztest.h> - #include "console.h" +#include "system.h" #include "test/drivers/test_state.h" #include "test/drivers/utils.h" -#include "system.h" + +#include <zephyr/drivers/emul.h> +#include <zephyr/shell/shell_dummy.h> +#include <zephyr/ztest.h> ZTEST_USER(console_cmd_sleepmask, test_no_args) { diff --git a/zephyr/test/drivers/default/src/console_cmd/sleeptimeout.c b/zephyr/test/drivers/default/src/console_cmd/sleeptimeout.c index d802eb5948..3bd8a39f3f 100644 --- a/zephyr/test/drivers/default/src/console_cmd/sleeptimeout.c +++ b/zephyr/test/drivers/default/src/console_cmd/sleeptimeout.c @@ -3,14 +3,14 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "console.h" #include "ec_commands.h" #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + ZTEST_USER(console_cmd_sleeptimeout, test_no_params) { zassert_ok(shell_execute_cmd(get_ec_shell(), "sleeptimeout"), diff --git a/zephyr/test/drivers/default/src/console_cmd/switch.c b/zephyr/test/drivers/default/src/console_cmd/switch.c new file mode 100644 index 0000000000..f533ebe7b9 --- /dev/null +++ b/zephyr/test/drivers/default/src/console_cmd/switch.c @@ -0,0 +1,31 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "console.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + +ZTEST_SUITE(console_cmd_switch, drivers_predicate_post_main, NULL, NULL, NULL, + NULL); + +ZTEST_USER(console_cmd_switch, test_mmapinfo) +{ + uint8_t *memmap_switches = host_get_memmap(EC_MEMMAP_SWITCHES); + uint8_t before = *memmap_switches; + char expected[32]; + + *memmap_switches = 0x3; + snprintf(expected, sizeof(expected), "memmap switches = 0x%x", + *memmap_switches); + + CHECK_CONSOLE_CMD("mmapinfo", expected, EC_SUCCESS); + CHECK_CONSOLE_CMD("mmapinfo", "lid_open", EC_SUCCESS); + CHECK_CONSOLE_CMD("mmapinfo", "powerbtn", EC_SUCCESS); + + *memmap_switches = before; +} diff --git a/zephyr/test/drivers/default/src/console_cmd/sysinfo.c b/zephyr/test/drivers/default/src/console_cmd/sysinfo.c index 3aeef6510c..ba14e9158f 100644 --- a/zephyr/test/drivers/default/src/console_cmd/sysinfo.c +++ b/zephyr/test/drivers/default/src/console_cmd/sysinfo.c @@ -3,16 +3,16 @@ * found in the LICENSE file. */ -#include <zephyr/drivers/emul.h> -#include <zephyr/fff.h> -#include <zephyr/shell/shell_dummy.h> -#include <zephyr/ztest.h> - #include "console.h" +#include "system.h" #include "test/drivers/test_mocks.h" #include "test/drivers/test_state.h" #include "test/drivers/utils.h" -#include "system.h" + +#include <zephyr/drivers/emul.h> +#include <zephyr/fff.h> +#include <zephyr/shell/shell_dummy.h> +#include <zephyr/ztest.h> ZTEST_USER(console_cmd_sysinfo, test_no_args) { diff --git a/zephyr/test/drivers/default/src/console_cmd/tcpci_dump.c b/zephyr/test/drivers/default/src/console_cmd/tcpci_dump.c index 9652519cab..fc8533280b 100644 --- a/zephyr/test/drivers/default/src/console_cmd/tcpci_dump.c +++ b/zephyr/test/drivers/default/src/console_cmd/tcpci_dump.c @@ -3,14 +3,14 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "console.h" #include "ec_commands.h" #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + ZTEST_USER(console_cmd_tcpci_dump, test_no_params) { int rv = shell_execute_cmd(get_ec_shell(), "tcpci_dump"); @@ -38,7 +38,7 @@ static void console_cmd_tcpci_dump_begin(void *data) ARG_UNUSED(data); /* Assume we have at least one TCPC */ - zassume_true(board_get_charger_chip_count() > 0, + zassert_true(board_get_charger_chip_count() > 0, "Insufficient TCPCs found"); } diff --git a/zephyr/test/drivers/default/src/console_cmd/usb_pd_console.c b/zephyr/test/drivers/default/src/console_cmd/usb_pd_console.c index d521f7bb06..859205f403 100644 --- a/zephyr/test/drivers/default/src/console_cmd/usb_pd_console.c +++ b/zephyr/test/drivers/default/src/console_cmd/usb_pd_console.c @@ -3,17 +3,17 @@ * found in the LICENSE file. */ -#include <zephyr/shell/shell.h> -#include <zephyr/ztest.h> - #include "console.h" #include "ec_commands.h" #include "test/drivers/test_state.h" #include "test/drivers/utils.h" #include "usb_pd.h" -#include "usb_pd_dpm.h" +#include "usb_pd_dpm_sm.h" #include "usb_prl_sm.h" +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + static void console_cmd_usb_pd_after(void *fixture) { ARG_UNUSED(fixture); @@ -188,6 +188,10 @@ ZTEST_USER(console_cmd_usb_pd, test_bistsharemode) rv = shell_execute_cmd(get_ec_shell(), "pd bistsharemode disable"); zassert_equal(rv, EC_SUCCESS, "Expected %d, but got %d", EC_SUCCESS, rv); + + rv = shell_execute_cmd(get_ec_shell(), "pd bistsharemode foo"); + zassert_equal(rv, EC_ERROR_PARAM2, "Expected %d, but got %d", + EC_ERROR_PARAM2, rv); } ZTEST_USER(console_cmd_usb_pd, test_hard) @@ -264,6 +268,22 @@ ZTEST_USER(console_cmd_usb_pd, test_dualrole) rv = shell_execute_cmd(get_ec_shell(), "pd 0 dualrole x"); zassert_equal(rv, EC_ERROR_PARAM4, "Expected %d, but got %d", EC_ERROR_PARAM4, rv); + + pd_set_dual_role(0, PD_DRP_TOGGLE_OFF); + CHECK_CONSOLE_CMD("pd 0 dualrole", "dual-role toggling: off", + EC_SUCCESS); + + pd_set_dual_role(0, PD_DRP_FREEZE); + CHECK_CONSOLE_CMD("pd 0 dualrole", "dual-role toggling: freeze", + EC_SUCCESS); + + pd_set_dual_role(0, PD_DRP_FORCE_SINK); + CHECK_CONSOLE_CMD("pd 0 dualrole", "dual-role toggling: force sink", + EC_SUCCESS); + + pd_set_dual_role(0, PD_DRP_FORCE_SOURCE); + CHECK_CONSOLE_CMD("pd 0 dualrole", "dual-role toggling: force source", + EC_SUCCESS); } ZTEST_USER(console_cmd_usb_pd, test_state) @@ -293,6 +313,15 @@ ZTEST_USER(console_cmd_usb_pd, test_timer) rv); } +ZTEST_USER(console_cmd_usb_pd, test_cc) +{ + char expected_output[10]; + + snprintf(expected_output, sizeof(expected_output), "C0 CC%d", + pd_get_task_cc_state(0)); + CHECK_CONSOLE_CMD("pd 0 cc", expected_output, EC_SUCCESS); +} + static void set_device_vdo(int port, enum tcpci_msg_type type) { union tbt_mode_resp_device device_resp; diff --git a/zephyr/test/drivers/default/src/console_cmd/vboot_hash.c b/zephyr/test/drivers/default/src/console_cmd/vboot_hash.c index b475f344c8..bdc9dddc66 100644 --- a/zephyr/test/drivers/default/src/console_cmd/vboot_hash.c +++ b/zephyr/test/drivers/default/src/console_cmd/vboot_hash.c @@ -2,12 +2,6 @@ * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <string.h> -#include <zephyr/fff.h> -#include <zephyr/shell/shell_dummy.h> -#include <zephyr/sys/util.h> -#include <zephyr/ztest.h> - #include "console.h" #include "flash.h" #include "printf.h" @@ -16,6 +10,13 @@ #include "test/drivers/test_state.h" #include "vboot_hash.h" +#include <string.h> + +#include <zephyr/fff.h> +#include <zephyr/shell/shell_dummy.h> +#include <zephyr/sys/util.h> +#include <zephyr/ztest.h> + #define CUSTOM_HASH_LENGTH (32) struct console_cmd_hash_fixture { diff --git a/zephyr/test/drivers/default/src/console_cmd/version.c b/zephyr/test/drivers/default/src/console_cmd/version.c index 932cc51449..e3606b5d0e 100644 --- a/zephyr/test/drivers/default/src/console_cmd/version.c +++ b/zephyr/test/drivers/default/src/console_cmd/version.c @@ -3,14 +3,14 @@ * found in the LICENSE file. */ -#include <zephyr/drivers/emul.h> -#include <zephyr/shell/shell_dummy.h> -#include <zephyr/ztest.h> - #include "console.h" +#include "system.h" #include "test/drivers/test_state.h" #include "test/drivers/utils.h" -#include "system.h" + +#include <zephyr/drivers/emul.h> +#include <zephyr/shell/shell_dummy.h> +#include <zephyr/ztest.h> ZTEST_USER(console_cmd_version, test_no_args) { diff --git a/zephyr/test/drivers/default/src/console_cmd/waitms.c b/zephyr/test/drivers/default/src/console_cmd/waitms.c index 0d03ee7414..703e233568 100644 --- a/zephyr/test/drivers/default/src/console_cmd/waitms.c +++ b/zephyr/test/drivers/default/src/console_cmd/waitms.c @@ -3,13 +3,14 @@ * found in the LICENSE file. */ +#include "console.h" +#include "timer.h" + #include <stdio.h> + #include <zephyr/shell/shell.h> #include <zephyr/ztest.h> -#include "console.h" -#include "timer.h" - static void test_int(int ms) { char cmd[32]; diff --git a/zephyr/test/drivers/default/src/cros_cbi.c b/zephyr/test/drivers/default/src/cros_cbi.c index e92765cb52..2669abe21e 100644 --- a/zephyr/test/drivers/default/src/cros_cbi.c +++ b/zephyr/test/drivers/default/src/cros_cbi.c @@ -3,12 +3,12 @@ * found in the LICENSE file. */ -#include <zephyr/device.h> -#include <zephyr/ztest.h> - #include "cros_cbi.h" #include "test/drivers/test_state.h" +#include <zephyr/device.h> +#include <zephyr/ztest.h> + ZTEST(cros_cbi, test_check_match) { int value; diff --git a/zephyr/test/drivers/default/src/espi.c b/zephyr/test/drivers/default/src/espi.c index ac6c93f3fc..e526980ef1 100644 --- a/zephyr/test/drivers/default/src/espi.c +++ b/zephyr/test/drivers/default/src/espi.c @@ -3,11 +3,6 @@ * found in the LICENSE file. */ -#include <string.h> -#include <zephyr/fff.h> -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> - #include "ec_commands.h" #include "gpio.h" #include "host_command.h" @@ -16,6 +11,12 @@ #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include <string.h> + +#include <zephyr/fff.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> + #define PORT 0 #define AC_OK_OD_GPIO_NAME "acok_od" @@ -214,7 +215,7 @@ ZTEST_USER(espi, test_host_command_gpio_set) BUILD_HOST_COMMAND_PARAMS(EC_CMD_GPIO_SET, 0, p); /* Force value to 1 to see change */ - zassume_ok(gpio_pin_set_dt(gp, 1), NULL); + zassert_ok(gpio_pin_set_dt(gp, 1), NULL); zassert_ok(host_command_process(&args), NULL); zassert_equal(gpio_pin_get_dt(gp), p.val, NULL); diff --git a/zephyr/test/drivers/default/src/flash.c b/zephyr/test/drivers/default/src/flash.c index 6794ca2878..e0c6743cde 100644 --- a/zephyr/test/drivers/default/src/flash.c +++ b/zephyr/test/drivers/default/src/flash.c @@ -3,13 +3,6 @@ * found in the LICENSE file. */ -#include <zephyr/drivers/emul.h> -#include <zephyr/drivers/gpio.h> -#include <zephyr/drivers/gpio/gpio_emul.h> -#include <zephyr/kernel.h> -#include <zephyr/shell/shell_dummy.h> -#include <zephyr/ztest.h> - #include "ec_commands.h" #include "emul/emul_flash.h" #include "flash.h" @@ -17,6 +10,13 @@ #include "system.h" #include "test/drivers/test_state.h" +#include <zephyr/drivers/emul.h> +#include <zephyr/drivers/gpio.h> +#include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/kernel.h> +#include <zephyr/shell/shell_dummy.h> +#include <zephyr/ztest.h> + #define WP_L_GPIO_PATH DT_PATH(named_gpios, wp_l) static int gpio_wp_l_set(int value) @@ -135,6 +135,17 @@ ZTEST_USER(flash, test_hostcmd_flash_protect_wp_deasserted) response.flags); } +ZTEST_USER(flash, test_hostcmd_flash_read__overflow) +{ + struct ec_params_flash_read params = { + .size = 32, + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_PARAMS(EC_CMD_FLASH_READ, 0, params); + + zassert_equal(EC_RES_OVERFLOW, host_command_process(&args)); +} + #define TEST_BUF_SIZE 0x100 ZTEST_USER(flash, test_hostcmd_flash_write_and_erase) @@ -252,7 +263,7 @@ ZTEST_USER(flash, test_hostcmd_flash_region_info_active_invalid) zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM, NULL); } -ZTEST_USER(flash, test_hostcmd_flash_info) +ZTEST_USER(flash, test_hostcmd_flash_info_1) { struct ec_response_flash_info_1 response; struct host_cmd_handler_args args = @@ -280,6 +291,50 @@ ZTEST_USER(flash, test_hostcmd_flash_info) "response.write_ideal_size = %d", response.write_ideal_size); } +ZTEST_USER(flash, test_hostcmd_flash_info_2) +{ + uint8_t response_buffer[sizeof(struct ec_response_flash_info_2) + + sizeof(struct ec_flash_bank)]; + struct ec_response_flash_info_2 *response = + (struct ec_response_flash_info_2 *)response_buffer; + struct ec_params_flash_info_2 params = { + .num_banks_desc = 1, + }; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND(EC_CMD_FLASH_INFO, 2, *response, params); + + /* Get the flash info. */ + zassert_ok(host_command_process(&args), NULL); + zassert_equal(response->flash_size, + CONFIG_FLASH_SIZE_BYTES - EC_FLASH_REGION_START, "got %d", + response->flash_size); + zassert_equal(response->flags, 0, "got %d", response->flags); + zassert_equal( + response->write_ideal_size, + (args.response_max - sizeof(struct ec_params_flash_write)) & + ~(CONFIG_FLASH_WRITE_SIZE - 1), + "got %d", response->write_ideal_size); + zassert_equal(response->num_banks_total, 1, "got %d", + response->num_banks_total); + zassert_equal(response->num_banks_desc, 1, "got %d", + response->num_banks_desc); + zassert_equal(response->banks[0].count, + CONFIG_FLASH_SIZE_BYTES / CONFIG_FLASH_BANK_SIZE, + "got %d", response->banks[0].count); + zassert_equal(response->banks[0].size_exp, + __fls(CONFIG_FLASH_BANK_SIZE), "got %d", + response->banks[0].size_exp); + zassert_equal(response->banks[0].write_size_exp, + __fls(CONFIG_FLASH_WRITE_SIZE), "got %d", + response->banks[0].write_size_exp); + zassert_equal(response->banks[0].erase_size_exp, + __fls(CONFIG_FLASH_ERASE_SIZE), "got %d", + response->banks[0].erase_size_exp); + zassert_equal(response->banks[0].protect_size_exp, + __fls(CONFIG_FLASH_BANK_SIZE), "got %d", + response->banks[0].protect_size_exp); +} + ZTEST_USER(flash, test_console_cmd_flash_info) { const struct shell *shell_zephyr = get_ec_shell(); @@ -309,12 +364,15 @@ ZTEST_USER(flash, test_console_cmd_flash_info) sprintf(format_buffer, "Protect: %4d B", CONFIG_FLASH_BANK_SIZE); zassert_not_null(strstr(outbuffer, format_buffer)); - zassert_not_null(strstr(outbuffer, "wp_gpio_asserted")); + zassert_not_null(strstr(outbuffer, "wp_gpio_asserted: ON")); + zassert_not_null(strstr(outbuffer, "ro_at_boot: OFF")); + zassert_not_null(strstr(outbuffer, "all_at_boot: OFF")); + zassert_not_null(strstr(outbuffer, "ro_now: OFF")); + zassert_not_null(strstr(outbuffer, "all_now: OFF")); + zassert_not_null(strstr(outbuffer, "STUCK: OFF")); + zassert_not_null(strstr(outbuffer, "INCONSISTENT: OFF")); + zassert_not_null(strstr(outbuffer, "UNKNOWN_ERROR: OFF")); zassert_not_null(strstr(outbuffer, "Protected now")); - /* - * TODO(b/254926324): Fake crec_flash_get_protect() to get more - * flag messages. - */ } ZTEST_USER(flash, test_console_cmd_flashwp__invalid) @@ -414,7 +472,7 @@ static void setup_flash_region_helper(uint32_t offset, uint32_t size, int rv; rv = host_command_process(&erase_args); - zassume_ok(rv, "Got %d", rv); + zassert_ok(rv, "Got %d", rv); if (make_write) { /* Sized for flash_write header plus one byte of data */ @@ -434,7 +492,7 @@ static void setup_flash_region_helper(uint32_t offset, uint32_t size, /* Write one byte at start of region */ out_buf[sizeof(*write_params)] = 0xec; - zassume_ok(host_command_process(&write_args), NULL); + zassert_ok(host_command_process(&write_args), NULL); } } diff --git a/zephyr/test/drivers/default/src/gpio.c b/zephyr/test/drivers/default/src/gpio.c index 3dd62aaa05..7304324532 100644 --- a/zephyr/test/drivers/default/src/gpio.c +++ b/zephyr/test/drivers/default/src/gpio.c @@ -8,23 +8,27 @@ * @brief Unit Tests for GPIO. */ -#include <zephyr/device.h> - -#include <zephyr/drivers/gpio/gpio_emul.h> -#include <zephyr/logging/log.h> -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> - #include "common.h" #include "ec_tasks.h" #include "gpio.h" #include "gpio/gpio.h" #include "gpio/gpio_int.h" #include "test/drivers/stubs.h" -#include "util.h" +#include "test/drivers/test_mocks.h" #include "test/drivers/test_state.h" +#include "util.h" + +#include <zephyr/device.h> +#include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/kernel.h> +#include <zephyr/logging/log.h> +#include <zephyr/ztest.h> extern bool gpio_test_interrupt_triggered; + +/* Function signature for shim/src/gpio.c test_export_static */ +int init_gpios(const struct device *unused); + /** * @brief TestPurpose: Verify Zephyr to EC GPIO bitmask conversion. * @@ -135,14 +139,22 @@ ZTEST(gpio, test_legacy_gpio_get_set_level) { enum gpio_signal signal = GPIO_SIGNAL(DT_NODELABEL(gpio_test)); int level; + /* Test invalid signal */ gpio_set_level(GPIO_COUNT, 0); zassert_equal(0, gpio_get_level(GPIO_COUNT), "Expected level==0"); + /* Test valid signal */ gpio_set_level(signal, 0); + zassert_ok(gpio_or_ioex_get_level(signal, &level)); zassert_equal(0, gpio_get_level(signal), "Expected level==0"); + zassert_equal(0, level); + gpio_set_level(signal, 1); + zassert_ok(gpio_or_ioex_get_level(signal, &level)); zassert_equal(1, gpio_get_level(signal), "Expected level==1"); + zassert_equal(1, level); + level = gpio_get_ternary(signal); gpio_set_level_verbose(CC_CHIPSET, signal, 0); zassert_equal(0, gpio_get_level(signal), "Expected level==0"); @@ -369,6 +381,62 @@ ZTEST(gpio, test_gpio_reset) flags); } +ZTEST(gpio, test_gpio_reset_port) +{ + const struct device *port = + DEVICE_DT_GET(DT_GPIO_CTLR(DT_NODELABEL(gpio_test), gpios)); + enum gpio_signal signal = GPIO_SIGNAL(DT_NODELABEL(gpio_test)); + gpio_flags_t flags; + gpio_flags_t flags_at_start[GPIO_COUNT]; + + /* Snapshot of GPIO flags before testing */ + for (int i = 0; i < GPIO_COUNT; i++) + flags_at_start[i] = gpio_helper_get_flags(i); + + /* Test reset on invalid signal */ + gpio_reset_port(NULL); + + /* Verify flags didn't change */ + for (int i = 0; i < GPIO_COUNT; i++) { + flags = gpio_helper_get_flags(i); + zassert_equal(flags_at_start[i], flags, + "%s[%d] flags_at_start=0x%x, flags=0x%x", + gpio_get_name(i), i, flags_at_start[i], flags); + } + + /* Test reset on valid signal */ + gpio_set_flags(signal, GPIO_OUTPUT); + flags = gpio_helper_get_flags(signal); + zassert_equal(flags, GPIO_OUTPUT, "Flags set 0x%x", flags); + + gpio_reset_port(port); + + flags = gpio_helper_get_flags(signal); + zassert_equal(flags, gpio_get_default_flags(signal), "Flags set 0x%x", + flags); + + for (int i = 0; i < GPIO_COUNT; ++i) { + gpio_set_flags(i, flags_at_start[i]); + } +} + +ZTEST(gpio, test_gpio_set_flags_by_mask) +{ + gpio_set_flags_by_mask(0, BIT(27), GPIO_OUTPUT); + zassert_equal(gpio_configure_port_pin_fake.call_count, 1); + zassert_equal(gpio_configure_port_pin_fake.arg0_val, 0); + zassert_equal(gpio_configure_port_pin_fake.arg1_val, 27); + zassert_equal(gpio_configure_port_pin_fake.arg2_val, + convert_to_zephyr_flags(GPIO_OUTPUT)); +} + +ZTEST(gpio, test_init_gpios_fail_on_unused_pins_custom_func) +{ + gpio_config_unused_pins_fake.return_val = -1; + + zassert_equal(-1, init_gpios(NULL)); +} + /** * @brief TestPurpose: Verify GPIO enable/disable interrupt. * @@ -418,4 +486,4 @@ static void gpio_before(void *state) /** * @brief Test Suite: Verifies GPIO functionality. */ -ZTEST_SUITE(gpio, drivers_predicate_post_main, NULL, gpio_before, NULL, NULL); +ZTEST_SUITE(gpio, drivers_predicate_pre_main, NULL, gpio_before, NULL, NULL); diff --git a/zephyr/test/drivers/default/src/i2c.c b/zephyr/test/drivers/default/src/i2c.c index caced4aedf..ca27fedbb2 100644 --- a/zephyr/test/drivers/default/src/i2c.c +++ b/zephyr/test/drivers/default/src/i2c.c @@ -3,14 +3,14 @@ * found in the LICENSE file. */ -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> - #include "ec_commands.h" #include "host_command.h" #include "i2c.h" #include "test/drivers/test_state.h" +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> + ZTEST_USER(i2c, test_i2c_set_speed_success) { struct ec_response_i2c_control response; @@ -52,12 +52,29 @@ ZTEST_USER(i2c, test_i2c_set_speed_success) "response.cmd_response.speed_khz = %d", response.cmd_response.speed_khz); + /* Set the speed to 1000. */ + set_params.cmd_params.speed_khz = 1000; + zassert_ok(host_command_process(&set_args), NULL); + zassert_ok(set_args.result, NULL); + zassert_equal(set_args.response_size, sizeof(response), NULL); + zassert_equal(response.cmd_response.speed_khz, 400, + "response.cmd_response.speed_khz = %d", + response.cmd_response.speed_khz); + + /* Get the speed to verify. */ + zassert_ok(host_command_process(&get_args), NULL); + zassert_ok(get_args.result, NULL); + zassert_equal(get_args.response_size, sizeof(response), NULL); + zassert_equal(response.cmd_response.speed_khz, 1000, + "response.cmd_response.speed_khz = %d", + response.cmd_response.speed_khz); + /* Set the speed back to 100. */ set_params.cmd_params.speed_khz = 100; zassert_ok(host_command_process(&set_args), NULL); zassert_ok(set_args.result, NULL); zassert_equal(set_args.response_size, sizeof(response), NULL); - zassert_equal(response.cmd_response.speed_khz, 400, + zassert_equal(response.cmd_response.speed_khz, 1000, "response.cmd_response.speed_khz = %d", response.cmd_response.speed_khz); } diff --git a/zephyr/test/drivers/default/src/i2c_passthru.c b/zephyr/test/drivers/default/src/i2c_passthru.c index 67e1317876..afefc4b553 100644 --- a/zephyr/test/drivers/default/src/i2c_passthru.c +++ b/zephyr/test/drivers/default/src/i2c_passthru.c @@ -3,15 +3,25 @@ * found in the LICENSE file. */ -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> - #include "driver/ln9310.h" #include "ec_commands.h" #include "host_command.h" #include "i2c.h" +#include "test/drivers/test_mocks.h" #include "test/drivers/test_state.h" +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> + +FAKE_VALUE_FUNC(int, board_allow_i2c_passthru, const struct i2c_cmd_desc_t *); + +int board_allow_i2c_passthru_custom_fake(const struct i2c_cmd_desc_t *cmd_desc) +{ + /* Only allow passthru on I2C_PORT_USB_C0 */ + return i2c_get_device_for_port(cmd_desc->port) == + i2c_get_device_for_port(I2C_PORT_USB_C0); +} + ZTEST_USER(i2c_passthru, test_read_without_write) { uint8_t param_buf[sizeof(struct ec_params_i2c_passthru) + @@ -40,6 +50,55 @@ ZTEST_USER(i2c_passthru, test_read_without_write) sizeof(struct ec_response_i2c_passthru), NULL); } +ZTEST_USER(i2c_passthru, test_passthru_invalid_params) +{ + uint16_t tcpc_addr = DT_REG_ADDR(DT_NODELABEL(tcpci_emul)); + uint8_t *out_data; + uint8_t param_buf[sizeof(struct ec_params_i2c_passthru) + + 2 * sizeof(struct ec_params_i2c_passthru_msg) + 1]; + uint8_t response_buf[sizeof(struct ec_response_i2c_passthru) + 2]; + struct ec_params_i2c_passthru *passthru_params = + (struct ec_params_i2c_passthru *)¶m_buf; + struct host_cmd_handler_args args = + BUILD_HOST_COMMAND_SIMPLE(EC_CMD_I2C_PASSTHRU, 0); + + passthru_params->port = I2C_PORT_USB_C0; + passthru_params->num_msgs = 2; + passthru_params->msg[0].addr_flags = tcpc_addr; + passthru_params->msg[0].len = 1; + passthru_params->msg[1].addr_flags = tcpc_addr | EC_I2C_FLAG_READ; + passthru_params->msg[1].len = 2; /* 2 byte vendor ID */ + + /* Write data follows the passthru messages */ + out_data = (uint8_t *)&passthru_params->msg[2]; + out_data[0] = 0; /* TCPC_REG_VENDOR_ID 0x0 */ + + args.params = ¶m_buf; + args.params_size = sizeof(param_buf); + args.response = &response_buf; + args.response_max = sizeof(response_buf); + + /* Set the params_size to smaller than struct ec_params_i2c_passthru */ + args.params_size = 1; + zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM); + + /* Set the params_size so it truncates the 2nd I2C message */ + args.params_size = sizeof(struct ec_params_i2c_passthru) + + sizeof(struct ec_params_i2c_passthru_msg); + zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM); + + /* Don't provide enough room for the response */ + args.params_size = sizeof(param_buf); + args.response_max = sizeof(struct ec_response_i2c_passthru) + 1; + zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM); + + /* Don't provide the write data */ + args.response_max = sizeof(response_buf); + args.params_size = sizeof(struct ec_params_i2c_passthru) + + 2 * sizeof(struct ec_params_i2c_passthru_msg); + zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM); +} + ZTEST_USER(i2c_passthru, test_passthru_protect) { struct ec_response_i2c_passthru_protect response; @@ -107,10 +166,166 @@ ZTEST_USER(i2c_passthru, test_passthru_protect_tcpcs) }; struct host_cmd_handler_args enable_args = BUILD_HOST_COMMAND_PARAMS( EC_CMD_I2C_PASSTHRU_PROTECT, 0, enable_params); + uint16_t tcpc_addr = DT_REG_ADDR(DT_NODELABEL(tcpci_emul)); + uint8_t *out_data; + uint8_t param_buf[sizeof(struct ec_params_i2c_passthru) + + 2 * sizeof(struct ec_params_i2c_passthru_msg) + 1]; + uint8_t response_buf[sizeof(struct ec_response_i2c_passthru) + 2]; + struct ec_params_i2c_passthru *passthru_params = + (struct ec_params_i2c_passthru *)¶m_buf; + struct ec_response_i2c_passthru *passthru_response = + (struct ec_response_i2c_passthru *)&response_buf; + struct host_cmd_handler_args passthru_args = + BUILD_HOST_COMMAND_SIMPLE(EC_CMD_I2C_PASSTHRU, 0); + + /* If the system is unlocked, TCPC protection is disabled */ + system_is_locked_fake.return_val = false; + + /* Protect the all TCPC buses */ + zassert_ok(host_command_process(&enable_args), NULL); + zassert_ok(enable_args.result, NULL); + + passthru_params->port = I2C_PORT_USB_C0; + passthru_params->num_msgs = 2; + passthru_params->msg[0].addr_flags = tcpc_addr; + passthru_params->msg[0].len = 1; + passthru_params->msg[1].addr_flags = tcpc_addr | EC_I2C_FLAG_READ; + passthru_params->msg[1].len = 2; /* 2 byte vendor ID */ + + /* Write data follows the passthru messages */ + out_data = (uint8_t *)&passthru_params->msg[2]; + out_data[0] = 0; /* TCPC_REG_VENDOR_ID 0x0 */ + + passthru_args.params = ¶m_buf; + passthru_args.params_size = sizeof(param_buf); + passthru_args.response = &response_buf; + passthru_args.response_max = sizeof(response_buf); + + zassert_ok(host_command_process(&passthru_args)); + zassert_ok(passthru_args.result); + zassert_ok(passthru_response->i2c_status); + zassert_equal(passthru_args.response_size, + sizeof(struct ec_response_i2c_passthru) + 2, NULL); + + /* Now attempt TCPC protection while the system is locked */ + system_is_locked_fake.return_val = true; /* Protect the all TCPC buses */ zassert_ok(host_command_process(&enable_args), NULL); zassert_ok(enable_args.result, NULL); + + zassert_equal(host_command_process(&passthru_args), + EC_RES_ACCESS_DENIED); +} + +ZTEST_USER(i2c_passthru, test_passthru_restricted) +{ + uint16_t tcpc_addr = DT_REG_ADDR(DT_NODELABEL(tcpci_emul)); + uint16_t ps8xxx_addr = DT_REG_ADDR(DT_NODELABEL(ps8xxx_emul)); + uint8_t *out_data; + uint8_t tcpc_param_buf[sizeof(struct ec_params_i2c_passthru) + + 2 * sizeof(struct ec_params_i2c_passthru_msg) + + 1]; + uint8_t tcpc_rsp_buf[sizeof(struct ec_response_i2c_passthru) + 2]; + struct ec_params_i2c_passthru *tcpc_params = + (struct ec_params_i2c_passthru *)&tcpc_param_buf; + struct ec_response_i2c_passthru *tcpc_response = + (struct ec_response_i2c_passthru *)&tcpc_rsp_buf; + struct host_cmd_handler_args tcpc_args = + BUILD_HOST_COMMAND_SIMPLE(EC_CMD_I2C_PASSTHRU, 0); + + uint8_t ps8xxx_param_buf[sizeof(struct ec_params_i2c_passthru) + + 2 * sizeof(struct ec_params_i2c_passthru_msg) + + 1]; + uint8_t ps8xxx_rsp_buf[sizeof(struct ec_response_i2c_passthru) + 2]; + struct ec_params_i2c_passthru *ps8xxx_params = + (struct ec_params_i2c_passthru *)&ps8xxx_param_buf; + struct ec_response_i2c_passthru *ps8xxx_response = + (struct ec_response_i2c_passthru *)&ps8xxx_rsp_buf; + struct host_cmd_handler_args ps8xxx_args = + BUILD_HOST_COMMAND_SIMPLE(EC_CMD_I2C_PASSTHRU, 0); + + if (!IS_ENABLED(CONFIG_PLATFORM_EC_I2C_PASSTHRU_RESTRICTED)) { + ztest_test_skip(); + return; + } + + /* + * Setup passthru command to the TCPCI emulator - which is always + * permitted by our board_allow_i2c_passthru() fake. + */ + tcpc_params->port = I2C_PORT_USB_C0; + tcpc_params->num_msgs = 2; + tcpc_params->msg[0].addr_flags = tcpc_addr; + tcpc_params->msg[0].len = 1; + tcpc_params->msg[1].addr_flags = tcpc_addr | EC_I2C_FLAG_READ; + tcpc_params->msg[1].len = 2; /* 2 byte vendor ID */ + + /* Write data follows the passthru messages */ + out_data = (uint8_t *)&tcpc_params->msg[2]; + out_data[0] = 0; /* TCPC_REG_VENDOR_ID 0x0 */ + + tcpc_args.params = &tcpc_param_buf; + tcpc_args.params_size = sizeof(tcpc_param_buf); + tcpc_args.response = &tcpc_rsp_buf; + tcpc_args.response_max = sizeof(tcpc_rsp_buf); + + /* + * Setup passthru command to the PS8xxx emulator, which should be + * rejected when the system is locked. + */ + ps8xxx_params->port = I2C_PORT_USB_C1; + ps8xxx_params->num_msgs = 2; + ps8xxx_params->msg[0].addr_flags = ps8xxx_addr; + ps8xxx_params->msg[0].len = 1; + ps8xxx_params->msg[1].addr_flags = ps8xxx_addr | EC_I2C_FLAG_READ; + ps8xxx_params->msg[1].len = 2; /* 2-byte vendor ID */ + + /* Write data follows the passthru messages */ + out_data = (uint8_t *)&ps8xxx_params->msg[2]; + out_data[0] = 0; /* TCPC_REG_VENDOR_ID 0x0 */ + + ps8xxx_args.params = &ps8xxx_param_buf; + ps8xxx_args.params_size = sizeof(ps8xxx_param_buf); + ps8xxx_args.response = &ps8xxx_rsp_buf; + ps8xxx_args.response_max = sizeof(ps8xxx_rsp_buf); + + /* Install our board_allow_i2c_passthru() handler */ + board_allow_i2c_passthru_fake.custom_fake = + board_allow_i2c_passthru_custom_fake; + + /* When the system is unlocked, no restrictions apply */ + system_is_locked_fake.return_val = false; + + zassert_ok(host_command_process(&tcpc_args)); + zassert_ok(tcpc_args.result); + zassert_ok(tcpc_response->i2c_status); + zassert_equal(tcpc_args.response_size, + sizeof(struct ec_response_i2c_passthru) + 2, NULL); + + zassert_ok(host_command_process(&ps8xxx_args)); + zassert_ok(ps8xxx_args.result); + zassert_ok(ps8xxx_response->i2c_status); + zassert_equal(ps8xxx_args.response_size, + sizeof(struct ec_response_i2c_passthru) + 2, NULL); + + /* Lock the system which enables board_allow_i2c_passthru() */ + system_is_locked_fake.return_val = true; + + zassert_ok(host_command_process(&tcpc_args)); + zassert_ok(tcpc_args.result); + zassert_ok(tcpc_response->i2c_status); + zassert_equal(tcpc_args.response_size, + sizeof(struct ec_response_i2c_passthru) + 2, NULL); + + zassert_equal(host_command_process(&ps8xxx_args), EC_RES_ACCESS_DENIED); +} + +static void i2c_passthru_before(void *state) +{ + ARG_UNUSED(state); + RESET_FAKE(board_allow_i2c_passthru); + board_allow_i2c_passthru_fake.return_val = 1; } static void i2c_passthru_after(void *state) @@ -119,5 +334,5 @@ static void i2c_passthru_after(void *state) i2c_passthru_protect_reset(); } -ZTEST_SUITE(i2c_passthru, drivers_predicate_post_main, NULL, NULL, - i2c_passthru_after, NULL); +ZTEST_SUITE(i2c_passthru, drivers_predicate_post_main, NULL, + i2c_passthru_before, i2c_passthru_after, NULL); diff --git a/zephyr/test/drivers/default/src/integration/usbc/usb.c b/zephyr/test/drivers/default/src/integration/usbc/usb.c index 9fc8f66505..31c8887bf1 100644 --- a/zephyr/test/drivers/default/src/integration/usbc/usb.c +++ b/zephyr/test/drivers/default/src/integration/usbc/usb.c @@ -3,14 +3,10 @@ * found in the LICENSE file. */ -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> -#include <zephyr/drivers/gpio/gpio_emul.h> - #include "battery_smart.h" +#include "driver/tcpm/ps8xxx_public.h" #include "ec_commands.h" #include "ec_tasks.h" -#include "driver/tcpm/ps8xxx_public.h" #include "emul/emul_isl923x.h" #include "emul/emul_smart_battery.h" #include "emul/tcpc/emul_ps8xxx.h" @@ -27,6 +23,10 @@ #include "test/usb_pe.h" #include "usb_tc_sm.h" +#include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> + #define BATTERY_NODE DT_NODELABEL(battery) #define GPIO_AC_OK_PATH DT_PATH(named_gpios, acok_od) diff --git a/zephyr/test/drivers/default/src/integration/usbc/usb_20v_3a_pd_charger.c b/zephyr/test/drivers/default/src/integration/usbc/usb_20v_3a_pd_charger.c index 07b1b5143e..f2afa5be9a 100644 --- a/zephyr/test/drivers/default/src/integration/usbc/usb_20v_3a_pd_charger.c +++ b/zephyr/test/drivers/default/src/integration/usbc/usb_20v_3a_pd_charger.c @@ -3,8 +3,6 @@ * found in the LICENSE file. */ -#include <zephyr/ztest.h> - #include "battery_smart.h" #include "emul/emul_isl923x.h" #include "emul/emul_smart_battery.h" @@ -13,6 +11,8 @@ #include "test/drivers/utils.h" #include "usb_pd.h" +#include <zephyr/ztest.h> + #define BATTERY_NODE DT_NODELABEL(battery) struct usb_attach_20v_3a_pd_charger_fixture { @@ -26,7 +26,7 @@ static inline void connect_charger_to_port(struct usb_attach_20v_3a_pd_charger_fixture *fixture) { set_ac_enabled(true); - zassume_ok(tcpci_partner_connect_to_tcpci(&fixture->charger_20v, + zassert_ok(tcpci_partner_connect_to_tcpci(&fixture->charger_20v, fixture->tcpci_emul), NULL); @@ -43,7 +43,7 @@ static inline void disconnect_charger_from_port( struct usb_attach_20v_3a_pd_charger_fixture *fixture) { set_ac_enabled(false); - zassume_ok(tcpci_emul_disconnect_partner(fixture->tcpci_emul)); + zassert_ok(tcpci_emul_disconnect_partner(fixture->tcpci_emul)); isl923x_emul_set_adc_vbus(fixture->charger_emul, 0); k_sleep(K_SECONDS(1)); } @@ -88,7 +88,7 @@ ZTEST(usb_attach_20v_3a_pd_charger, test_battery_is_charging) const struct emul *emul = EMUL_DT_GET(BATTERY_NODE); uint16_t battery_status; - zassume_ok(sbat_emul_get_word_val(emul, SB_BATTERY_STATUS, + zassert_ok(sbat_emul_get_word_val(emul, SB_BATTERY_STATUS, &battery_status), NULL); zassert_equal(battery_status & STATUS_DISCHARGING, 0, @@ -176,9 +176,9 @@ ZTEST_F(usb_attach_20v_3a_pd_charger, test_disconnect_charge_state) "Max charge current expected 0mA, but was %dmA", charge_state.get_state.chg_current); zassert_equal(charge_state.get_state.chg_input_current, - CONFIG_PLATFORM_EC_CHARGER_INPUT_CURRENT, + CONFIG_PLATFORM_EC_CHARGER_DEFAULT_CURRENT_LIMIT, "Charge input current limit expected %dmA, but was %dmA", - CONFIG_PLATFORM_EC_CHARGER_INPUT_CURRENT, + CONFIG_PLATFORM_EC_CHARGER_DEFAULT_CURRENT_LIMIT, charge_state.get_state.chg_input_current); } diff --git a/zephyr/test/drivers/default/src/integration/usbc/usb_5v_3a_pd_sink.c b/zephyr/test/drivers/default/src/integration/usbc/usb_5v_3a_pd_sink.c index e3584470a0..6795e72c11 100644 --- a/zephyr/test/drivers/default/src/integration/usbc/usb_5v_3a_pd_sink.c +++ b/zephyr/test/drivers/default/src/integration/usbc/usb_5v_3a_pd_sink.c @@ -3,9 +3,6 @@ * found in the LICENSE file. */ -#include <stdint.h> -#include <zephyr/ztest.h> - #include "battery_smart.h" #include "emul/emul_isl923x.h" #include "emul/emul_smart_battery.h" @@ -16,6 +13,10 @@ #include "timer.h" #include "usb_pd.h" +#include <stdint.h> + +#include <zephyr/ztest.h> + struct usb_attach_5v_3a_pd_sink_fixture { struct tcpci_partner_data sink_5v_3a; struct tcpci_snk_emul_data snk_ext; @@ -85,7 +86,7 @@ ZTEST(usb_attach_5v_3a_pd_sink, test_battery_is_discharging) const struct emul *emul = EMUL_DT_GET(DT_NODELABEL(battery)); uint16_t battery_status; - zassume_ok(sbat_emul_get_word_val(emul, SB_BATTERY_STATUS, + zassert_ok(sbat_emul_get_word_val(emul, SB_BATTERY_STATUS, &battery_status)); zassert_equal(battery_status & STATUS_DISCHARGING, STATUS_DISCHARGING, "Battery is not discharging: %d", battery_status); @@ -158,9 +159,9 @@ ZTEST_F(usb_attach_5v_3a_pd_sink, test_disconnect_charge_state) "Max charge current expected 0mA, but was %dmA", charge_state.get_state.chg_current); zassert_equal(charge_state.get_state.chg_input_current, - CONFIG_PLATFORM_EC_CHARGER_INPUT_CURRENT, + CONFIG_PLATFORM_EC_CHARGER_DEFAULT_CURRENT_LIMIT, "Charge input current limit expected %dmA, but was %dmA", - CONFIG_PLATFORM_EC_CHARGER_INPUT_CURRENT, + CONFIG_PLATFORM_EC_CHARGER_DEFAULT_CURRENT_LIMIT, charge_state.get_state.chg_input_current); } diff --git a/zephyr/test/drivers/default/src/integration/usbc/usb_5v_3a_pd_source.c b/zephyr/test/drivers/default/src/integration/usbc/usb_5v_3a_pd_source.c index c73cf26f37..c57054881d 100644 --- a/zephyr/test/drivers/default/src/integration/usbc/usb_5v_3a_pd_source.c +++ b/zephyr/test/drivers/default/src/integration/usbc/usb_5v_3a_pd_source.c @@ -3,10 +3,6 @@ * found in the LICENSE file. */ -#include <zephyr/sys/byteorder.h> -#include <zephyr/sys/slist.h> -#include <zephyr/ztest.h> - #include "battery.h" #include "battery_smart.h" #include "dps.h" @@ -19,11 +15,18 @@ #include "test/drivers/utils.h" #include "usb_pd.h" +#include <zephyr/shell/shell_dummy.h> +#include <zephyr/sys/byteorder.h> +#include <zephyr/sys/slist.h> +#include <zephyr/ztest.h> + #define BATTERY_NODE DT_NODELABEL(battery) #define GPIO_BATT_PRES_ODL_PATH DT_PATH(named_gpios, ec_batt_pres_odl) #define GPIO_BATT_PRES_ODL_PORT DT_GPIO_PIN(GPIO_BATT_PRES_ODL_PATH, gpios) +#define TEST_PORT 0 + struct usb_attach_5v_3a_pd_source_fixture { struct tcpci_partner_data source_5v_3a; struct tcpci_src_emul_data src_ext; @@ -36,8 +39,8 @@ static void *usb_attach_5v_3a_pd_source_setup(void) static struct usb_attach_5v_3a_pd_source_fixture test_fixture; /* Get references for the emulators */ - test_fixture.tcpci_emul = EMUL_GET_USBC_BINDING(0, tcpc); - test_fixture.charger_emul = EMUL_GET_USBC_BINDING(0, chg); + test_fixture.tcpci_emul = EMUL_GET_USBC_BINDING(TEST_PORT, tcpc); + test_fixture.charger_emul = EMUL_GET_USBC_BINDING(TEST_PORT, chg); /* Initialized the charger to supply 5V and 3A */ tcpci_partner_init(&test_fixture.source_5v_3a, PD_REV20); @@ -70,7 +73,7 @@ static void control_battery_present(bool present) DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_BATT_PRES_ODL_PATH, gpios)); /* 0 means battery present */ - zassume_ok(gpio_emul_input_set(dev, GPIO_BATT_PRES_ODL_PORT, !present)); + zassert_ok(gpio_emul_input_set(dev, GPIO_BATT_PRES_ODL_PORT, !present)); } ZTEST_SUITE(usb_attach_5v_3a_pd_source, drivers_predicate_post_main, @@ -82,7 +85,7 @@ ZTEST(usb_attach_5v_3a_pd_source, test_battery_is_charging) const struct emul *emul = EMUL_DT_GET(BATTERY_NODE); uint16_t battery_status; - zassume_ok(sbat_emul_get_word_val(emul, SB_BATTERY_STATUS, + zassert_ok(sbat_emul_get_word_val(emul, SB_BATTERY_STATUS, &battery_status)); zassert_equal(battery_status & STATUS_DISCHARGING, 0, "Battery is discharging: %d", battery_status); @@ -90,7 +93,8 @@ ZTEST(usb_attach_5v_3a_pd_source, test_battery_is_charging) ZTEST(usb_attach_5v_3a_pd_source, test_charge_state) { - struct ec_response_charge_state state = host_cmd_charge_state(0); + struct ec_response_charge_state state = + host_cmd_charge_state(TEST_PORT); zassert_true(state.get_state.ac, "AC_OK not triggered"); zassert_true(state.get_state.chg_voltage > 0, @@ -161,16 +165,16 @@ ZTEST_F(usb_attach_5v_3a_pd_source, test_disconnect_charge_state) struct ec_response_charge_state charge_state; disconnect_source_from_port(fixture->tcpci_emul, fixture->charger_emul); - charge_state = host_cmd_charge_state(0); + charge_state = host_cmd_charge_state(TEST_PORT); zassert_false(charge_state.get_state.ac, "AC_OK not triggered"); zassert_equal(charge_state.get_state.chg_current, 0, "Max charge current expected 0mA, but was %dmA", charge_state.get_state.chg_current); zassert_equal(charge_state.get_state.chg_input_current, - CONFIG_PLATFORM_EC_CHARGER_INPUT_CURRENT, + CONFIG_PLATFORM_EC_CHARGER_DEFAULT_CURRENT_LIMIT, "Charge input current limit expected %dmA, but was %dmA", - CONFIG_PLATFORM_EC_CHARGER_INPUT_CURRENT, + CONFIG_PLATFORM_EC_CHARGER_DEFAULT_CURRENT_LIMIT, charge_state.get_state.chg_input_current); } @@ -321,3 +325,39 @@ ZTEST_F(usb_attach_5v_3a_pd_source, test_dps_enable) dps_enable(true); zassert_true(dps_is_enabled()); } + +ZTEST_F(usb_attach_5v_3a_pd_source, test_dps_info) +{ + const struct shell *shell_zephyr = get_ec_shell(); + + const char *outbuffer; + uint32_t buffer_size; + /* Arbitrary array size for sprintf should not need this amount */ + char format_buffer[100]; + + shell_backend_dummy_clear_output(shell_zephyr); + + /* Print current status to console */ + zassert_ok(shell_execute_cmd(shell_zephyr, "dps"), NULL); + outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size); + + /* Should include extra information about the charging port */ + /* Charging Port */ + sprintf(format_buffer, "C%d", TEST_PORT); + zassert_not_null(strstr(outbuffer, format_buffer)); + /* We are a sink to a 5v3a source, so check requested mv/ma */ + sprintf(format_buffer, "Requested: %dmV/%dmA", 5000, 3000); + zassert_not_null(strstr(outbuffer, format_buffer)); + /* + * Measured input power is shown (values vary so not asserting on + * numbers) + */ + zassert_not_null(strstr(outbuffer, "Measured:")); + /* Efficient Voltage - Value varies based on battery*/ + zassert_not_null(strstr(outbuffer, "Efficient:")); + /* Battery Design Voltage (varies based on battery) */ + zassert_not_null(strstr(outbuffer, "Batt:")); + /* PDMaxMV */ + sprintf(format_buffer, "PDMaxMV: %dmV", pd_get_max_voltage()); + zassert_not_null(strstr(outbuffer, format_buffer)); +} diff --git a/zephyr/test/drivers/default/src/integration/usbc/usb_attach_src_snk.c b/zephyr/test/drivers/default/src/integration/usbc/usb_attach_src_snk.c index 0b65f9b135..401a89cb11 100644 --- a/zephyr/test/drivers/default/src/integration/usbc/usb_attach_src_snk.c +++ b/zephyr/test/drivers/default/src/integration/usbc/usb_attach_src_snk.c @@ -3,24 +3,24 @@ * found in the LICENSE file. */ -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> -#include <zephyr/drivers/gpio/gpio_emul.h> - +#include "driver/tcpm/ps8xxx_public.h" #include "ec_commands.h" #include "ec_tasks.h" -#include "driver/tcpm/ps8xxx_public.h" #include "emul/emul_isl923x.h" #include "emul/tcpc/emul_ps8xxx.h" #include "emul/tcpc/emul_tcpci.h" #include "emul/tcpc/emul_tcpci_partner_snk.h" #include "emul/tcpc/emul_tcpci_partner_src.h" #include "host_command.h" -#include "test/drivers/stubs.h" #include "tcpm/tcpci.h" -#include "test/usb_pe.h" -#include "test/drivers/utils.h" +#include "test/drivers/stubs.h" #include "test/drivers/test_state.h" +#include "test/drivers/utils.h" +#include "test/usb_pe.h" + +#include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #define SNK_PORT USBC_PORT_C0 #define SRC_PORT USBC_PORT_C1 @@ -117,21 +117,21 @@ static void attach_src_snk_common_before(struct emul_state *my_emul_state) /* TODO(b/217737667): Remove driver specific code. */ isl923x_emul_set_adc_vbus(charger_emul, 0); - zassume_ok(tcpc_config[SNK_PORT].drv->init(SNK_PORT)); + zassert_ok(tcpc_config[SNK_PORT].drv->init(SNK_PORT)); /* * Arbitrary FW ver. The emulator should really be setting this * during its init. */ tcpci_emul_set_reg(tcpci_emul_snk, PS8XXX_REG_FW_REV, 0x31); - zassume_ok(tcpc_config[SRC_PORT].drv->init(SRC_PORT)); + zassert_ok(tcpc_config[SRC_PORT].drv->init(SRC_PORT)); pd_set_suspend(SNK_PORT, false); pd_set_suspend(SRC_PORT, false); /* Reset to disconnected state. */ - zassume_ok(tcpci_emul_disconnect_partner(tcpci_emul_src)); - zassume_ok(tcpci_emul_disconnect_partner(tcpci_emul_snk)); + zassert_ok(tcpci_emul_disconnect_partner(tcpci_emul_src)); + zassert_ok(tcpci_emul_disconnect_partner(tcpci_emul_snk)); /* Set chipset to ON, this will set TCPM to DRP */ test_set_chipset_to_s0(); @@ -177,7 +177,7 @@ static void attach_emulated_snk(struct emul_state *my_emul_state) tcpci_emul_set_reg(tcpci_emul_snk, TCPC_REG_EXT_STATUS, TCPC_REG_EXT_STATUS_SAFE0V); - zassume_ok(tcpci_partner_connect_to_tcpci(my_snk, tcpci_emul_snk), + zassert_ok(tcpci_partner_connect_to_tcpci(my_snk, tcpci_emul_snk), NULL); /* TODO(b/214401892): Check why need to give time TCPM to spin */ @@ -207,7 +207,7 @@ static void attach_emulated_src(struct emul_state *my_emul_state) tcpci_emul_set_reg(tcpci_emul_src, TCPC_REG_EXT_STATUS, TCPC_REG_EXT_STATUS_SAFE0V); - zassume_ok(tcpci_partner_connect_to_tcpci(my_src, tcpci_emul_src), + zassert_ok(tcpci_partner_connect_to_tcpci(my_src, tcpci_emul_src), NULL); isl923x_emul_set_adc_vbus(charger_emul, DEFAULT_VBUS_MV); } @@ -542,7 +542,7 @@ struct usb_detach_test_fixture { static void integration_usb_test_detach(const struct emul *e) { - zassume_ok(tcpci_emul_disconnect_partner(e)); + zassert_ok(tcpci_emul_disconnect_partner(e)); } static void integration_usb_test_sink_detach(struct emul_state *fixture) diff --git a/zephyr/test/drivers/default/src/integration/usbc/usb_pd_bist_shared.c b/zephyr/test/drivers/default/src/integration/usbc/usb_pd_bist_shared.c index 47e26f97d6..36a417d22c 100644 --- a/zephyr/test/drivers/default/src/integration/usbc/usb_pd_bist_shared.c +++ b/zephyr/test/drivers/default/src/integration/usbc/usb_pd_bist_shared.c @@ -3,8 +3,6 @@ * found in the LICENSE file. */ -#include <zephyr/ztest.h> - #include "emul/emul_isl923x.h" #include "emul/emul_smart_battery.h" #include "emul/tcpc/emul_tcpci_partner_snk.h" @@ -15,6 +13,8 @@ #include "usb_pd.h" #include "util.h" +#include <zephyr/ztest.h> + struct usb_pd_bist_shared_fixture { struct tcpci_partner_data sink_5v_500ma; struct tcpci_snk_emul_data snk_ext_500ma; @@ -99,7 +99,7 @@ ZTEST_F(usb_pd_bist_shared, verify_bist_shared_mode) /* Start up BIST shared test mode */ bist_data = BDO(BDO_MODE_SHARED_ENTER, 0); - zassume_ok(tcpci_partner_send_data_msg(&fixture->sink_5v_500ma, + zassert_ok(tcpci_partner_send_data_msg(&fixture->sink_5v_500ma, PD_DATA_BIST, &bist_data, 1, 0), "Failed to send BIST enter message"); @@ -116,7 +116,7 @@ ZTEST_F(usb_pd_bist_shared, verify_bist_shared_mode) /* Leave BIST shared test mode */ bist_data = BDO(BDO_MODE_SHARED_EXIT, 0); - zassume_ok(tcpci_partner_send_data_msg(&fixture->sink_5v_500ma, + zassert_ok(tcpci_partner_send_data_msg(&fixture->sink_5v_500ma, PD_DATA_BIST, &bist_data, 1, 0), "Failed to send BIST exit message"); @@ -152,7 +152,7 @@ ZTEST_F(usb_pd_bist_shared, verify_bist_shared_no_snk_entry) /* Have the source send the BIST Enter Mode */ bist_data = BDO(BDO_MODE_SHARED_ENTER, 0); - zassume_ok(tcpci_partner_send_data_msg(&fixture->src, PD_DATA_BIST, + zassert_ok(tcpci_partner_send_data_msg(&fixture->src, PD_DATA_BIST, &bist_data, 1, 0), "Failed to send BIST enter message"); @@ -181,7 +181,7 @@ ZTEST_F(usb_pd_bist_shared, verify_bist_shared_exit_no_action) tcpci_snk_emul_clear_last_5v_cap(&fixture->snk_ext_500ma); bist_data = BDO(BDO_MODE_SHARED_EXIT, 0); - zassume_ok(tcpci_partner_send_data_msg(&fixture->sink_5v_500ma, + zassert_ok(tcpci_partner_send_data_msg(&fixture->sink_5v_500ma, PD_DATA_BIST, &bist_data, 1, 0), "Failed to send BIST exit message"); @@ -198,7 +198,7 @@ ZTEST_F(usb_pd_bist_shared, verify_control_bist_shared_mode) uint32_t f5v_cap; host_cmd_typec_control_bist_share_mode(USBC_PORT_C0, 1); - zassume_ok(tcpci_partner_send_control_msg(&fixture->sink_5v_500ma, + zassert_ok(tcpci_partner_send_control_msg(&fixture->sink_5v_500ma, PD_CTRL_GET_SOURCE_CAP, 0), "Failed to send get src cap"); /* wait tSenderResponse (26 ms) */ diff --git a/zephyr/test/drivers/default/src/integration/usbc/usb_pd_ctrl_msg.c b/zephyr/test/drivers/default/src/integration/usbc/usb_pd_ctrl_msg.c index e35e2a7ec1..1d3da4921f 100644 --- a/zephyr/test/drivers/default/src/integration/usbc/usb_pd_ctrl_msg.c +++ b/zephyr/test/drivers/default/src/integration/usbc/usb_pd_ctrl_msg.c @@ -3,9 +3,6 @@ * found in the LICENSE file. */ -#include <stdint.h> -#include <zephyr/ztest.h> - #include "common.h" #include "ec_tasks.h" #include "emul/emul_isl923x.h" @@ -17,6 +14,10 @@ #include "test/usb_pe.h" #include "usb_pd.h" +#include <stdint.h> + +#include <zephyr/ztest.h> + #define TEST_USB_PORT 0 BUILD_ASSERT(TEST_USB_PORT == USBC_PORT_C0); @@ -59,13 +60,13 @@ tcpci_drp_emul_connect_partner(struct tcpci_partner_data *partner_emul, tcpci_tcpc_alert(TEST_USB_PORT); - zassume_ok(tcpci_partner_connect_to_tcpci(partner_emul, tcpci_emul), + zassert_ok(tcpci_partner_connect_to_tcpci(partner_emul, tcpci_emul), NULL); } static void disconnect_partner(struct usb_pd_ctrl_msg_test_fixture *fixture) { - zassume_ok(tcpci_emul_disconnect_partner(fixture->tcpci_emul)); + zassert_ok(tcpci_emul_disconnect_partner(fixture->tcpci_emul)); k_sleep(K_SECONDS(1)); } diff --git a/zephyr/test/drivers/default/src/integration/usbc/usb_pd_rev3.c b/zephyr/test/drivers/default/src/integration/usbc/usb_pd_rev3.c index fdea74a691..781a0cc06a 100644 --- a/zephyr/test/drivers/default/src/integration/usbc/usb_pd_rev3.c +++ b/zephyr/test/drivers/default/src/integration/usbc/usb_pd_rev3.c @@ -3,10 +3,6 @@ * found in the LICENSE file. */ -#include <zephyr/sys/byteorder.h> -#include <zephyr/sys/slist.h> -#include <zephyr/ztest.h> - #include "battery.h" #include "battery_smart.h" #include "chipset.h" @@ -21,6 +17,10 @@ #include "usb_pd.h" #include "util.h" +#include <zephyr/sys/byteorder.h> +#include <zephyr/sys/slist.h> +#include <zephyr/ztest.h> + struct usb_attach_5v_3a_pd_source_rev3_fixture { struct tcpci_partner_data source_5v_3a; struct tcpci_src_emul_data src_ext; @@ -62,11 +62,11 @@ static void usb_attach_5v_3a_pd_source_before(void *data) /* Clear Alert and Status receive checks */ tcpci_src_emul_clear_alert_received(&fixture->src_ext); tcpci_src_emul_clear_status_received(&fixture->src_ext); - zassume_false(fixture->src_ext.alert_received); - zassume_false(fixture->src_ext.status_received); + zassert_false(fixture->src_ext.alert_received); + zassert_false(fixture->src_ext.status_received); /* Initial check on power state */ - zassume_true(chipset_in_state(CHIPSET_STATE_ON)); + zassert_true(chipset_in_state(CHIPSET_STATE_ON)); } static void usb_attach_5v_3a_pd_source_after(void *data) @@ -112,8 +112,8 @@ ZTEST_F(usb_attach_5v_3a_pd_source_rev3, test_batt_cap) /* See pe_give_battery_cap_entry() in common/usbc/usb_pe_drp_sm.c */ - zassume_true(battery_is_present(), "Battery must be present"); - zassume_true(IS_ENABLED(HAS_TASK_HOSTCMD) && + zassert_true(battery_is_present(), "Battery must be present"); + zassert_true(IS_ENABLED(HAS_TASK_HOSTCMD) && *host_get_memmap(EC_MEMMAP_BATTERY_VERSION) != 0, "Cannot access battery data"); @@ -171,7 +171,7 @@ ZTEST_F(usb_attach_5v_3a_pd_source_rev3, test_batt_cap_invalid) ZTEST_F(usb_attach_5v_3a_pd_source_rev3, verify_alert_msg) { - zassume_equal(pd_broadcast_alert_msg(ADO_OTP_EVENT), EC_SUCCESS); + zassert_equal(pd_broadcast_alert_msg(ADO_OTP_EVENT), EC_SUCCESS); k_sleep(K_SECONDS(2)); zassert_true(fixture->src_ext.alert_received); @@ -186,8 +186,8 @@ ZTEST_F(usb_attach_5v_3a_pd_source_rev3, verify_alert_on_power_state_change) zassert_true(fixture->src_ext.status_received); tcpci_src_emul_clear_alert_received(&fixture->src_ext); tcpci_src_emul_clear_status_received(&fixture->src_ext); - zassume_false(fixture->src_ext.alert_received); - zassume_false(fixture->src_ext.status_received); + zassert_false(fixture->src_ext.alert_received); + zassert_false(fixture->src_ext.status_received); /* Shutdown and check partner received Alert and Status messages */ hook_notify(HOOK_CHIPSET_SHUTDOWN); @@ -196,8 +196,8 @@ ZTEST_F(usb_attach_5v_3a_pd_source_rev3, verify_alert_on_power_state_change) zassert_true(fixture->src_ext.status_received); tcpci_src_emul_clear_alert_received(&fixture->src_ext); tcpci_src_emul_clear_status_received(&fixture->src_ext); - zassume_false(fixture->src_ext.alert_received); - zassume_false(fixture->src_ext.status_received); + zassert_false(fixture->src_ext.alert_received); + zassert_false(fixture->src_ext.status_received); /* Startup and check partner received Alert and Status messages */ hook_notify(HOOK_CHIPSET_STARTUP); @@ -206,8 +206,8 @@ ZTEST_F(usb_attach_5v_3a_pd_source_rev3, verify_alert_on_power_state_change) zassert_true(fixture->src_ext.status_received); tcpci_src_emul_clear_alert_received(&fixture->src_ext); tcpci_src_emul_clear_status_received(&fixture->src_ext); - zassume_false(fixture->src_ext.alert_received); - zassume_false(fixture->src_ext.status_received); + zassert_false(fixture->src_ext.alert_received); + zassert_false(fixture->src_ext.status_received); /* Resume and check partner received Alert and Status messages */ hook_notify(HOOK_CHIPSET_RESUME); @@ -247,9 +247,9 @@ ZTEST_F(usb_attach_5v_3a_pd_source_rev3, /* Clear alert and status flags set during shutdown */ tcpci_src_emul_clear_alert_received(&fixture->src_ext); tcpci_src_emul_clear_status_received(&fixture->src_ext); - zassume_false(fixture->src_ext.alert_received); - zassume_false(fixture->src_ext.status_received); - zassume_true(chipset_in_state(CHIPSET_STATE_ANY_OFF)); + zassert_false(fixture->src_ext.alert_received); + zassert_false(fixture->src_ext.status_received); + zassert_true(chipset_in_state(CHIPSET_STATE_ANY_OFF)); /* While in S5/G3 expect nothing on invalid (too long) press */ ado = ADO_EXTENDED_ALERT_EVENT | ADO_POWER_BUTTON_PRESS; @@ -283,9 +283,9 @@ ZTEST_F(usb_attach_5v_3a_pd_source_rev3, verify_startup_on_pd_button_press) /* Clear alert and status flags set during shutdown */ tcpci_src_emul_clear_alert_received(&fixture->src_ext); tcpci_src_emul_clear_status_received(&fixture->src_ext); - zassume_false(fixture->src_ext.alert_received); - zassume_false(fixture->src_ext.status_received); - zassume_true(chipset_in_state(CHIPSET_STATE_ANY_OFF)); + zassert_false(fixture->src_ext.alert_received); + zassert_false(fixture->src_ext.status_received); + zassert_true(chipset_in_state(CHIPSET_STATE_ANY_OFF)); /* While in S5/G3 expect Alert->Get_Status->Status on valid press */ ado = ADO_EXTENDED_ALERT_EVENT | ADO_POWER_BUTTON_PRESS; diff --git a/zephyr/test/drivers/default/src/isl923x.c b/zephyr/test/drivers/default/src/isl923x.c index e17dbbe797..1393466c76 100644 --- a/zephyr/test/drivers/default/src/isl923x.c +++ b/zephyr/test/drivers/default/src/isl923x.c @@ -3,21 +3,21 @@ * found in the LICENSE file. */ -#include <zephyr/ztest.h> -#include <zephyr/drivers/emul.h> -#include <zephyr/fff.h> - #include "battery.h" #include "battery_smart.h" -#include "test/drivers/charger_utils.h" #include "driver/charger/isl923x.h" #include "driver/charger/isl923x_public.h" #include "emul/emul_common_i2c.h" #include "emul/emul_isl923x.h" #include "system.h" +#include "test/drivers/charger_utils.h" #include "test/drivers/test_mocks.h" #include "test/drivers/test_state.h" +#include <zephyr/drivers/emul.h> +#include <zephyr/fff.h> +#include <zephyr/ztest.h> + BUILD_ASSERT(CONFIG_CHARGER_SENSE_RESISTOR == 10 || CONFIG_CHARGER_SENSE_RESISTOR == 5); @@ -681,9 +681,9 @@ ZTEST(isl923x, test_init_late_jump) isl923x_drv.get_input_current_limit(CHARGER_NUM, &input_current), "Could not read input current limit."); - zassert_equal(CONFIG_CHARGER_INPUT_CURRENT, input_current, + zassert_equal(CONFIG_CHARGER_DEFAULT_CURRENT_LIMIT, input_current, "Input current (%d) not at (%d)", input_current, - CONFIG_CHARGER_INPUT_CURRENT); + CONFIG_CHARGER_DEFAULT_CURRENT_LIMIT); } ZTEST(isl923x, test_isl923x_is_acok) diff --git a/zephyr/test/drivers/default/src/led.c b/zephyr/test/drivers/default/src/led.c index e89a3d8b66..3f727599b4 100644 --- a/zephyr/test/drivers/default/src/led.c +++ b/zephyr/test/drivers/default/src/led.c @@ -3,12 +3,6 @@ * found in the LICENSE file. */ -#include <zephyr/ztest.h> -#include <zephyr/ztest_assert.h> - -#include <zephyr/device.h> -#include <zephyr/drivers/pwm.h> - #include "ec_commands.h" #include "led.h" #include "led_common.h" @@ -16,6 +10,11 @@ #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include <zephyr/device.h> +#include <zephyr/drivers/pwm.h> +#include <zephyr/ztest.h> +#include <zephyr/ztest_assert.h> + ZTEST_SUITE(pwm_led_driver, drivers_predicate_post_main, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/drivers/default/src/lid_angle.c b/zephyr/test/drivers/default/src/lid_angle.c index 568057d95a..3611e176d4 100644 --- a/zephyr/test/drivers/default/src/lid_angle.c +++ b/zephyr/test/drivers/default/src/lid_angle.c @@ -3,13 +3,13 @@ * found in the LICENSE file. */ -#include <zephyr/ztest.h> - #include "ec_commands.h" #include "lid_angle.h" #include "test/drivers/test_mocks.h" #include "test/drivers/test_state.h" +#include <zephyr/ztest.h> + #define LID_ANGLE_MIN_LARGE_ANGLE 0 #define LID_ANGLE_MAX_LARGE_ANGLE 360 diff --git a/zephyr/test/drivers/default/src/lid_switch.c b/zephyr/test/drivers/default/src/lid_switch.c index b5da1f6608..2fe7daa85a 100644 --- a/zephyr/test/drivers/default/src/lid_switch.c +++ b/zephyr/test/drivers/default/src/lid_switch.c @@ -3,18 +3,19 @@ * found in the LICENSE file. */ -#include <zephyr/ztest.h> +#include "ec_commands.h" +#include "host_command.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + #include <zephyr/drivers/emul.h> #include <zephyr/drivers/gpio.h> #include <zephyr/drivers/gpio/gpio_emul.h> -#include <lid_switch.h> #include <zephyr/shell/shell_dummy.h> -#include <console.h> +#include <zephyr/ztest.h> -#include "test/drivers/test_state.h" -#include "test/drivers/utils.h" -#include "ec_commands.h" -#include "host_command.h" +#include <console.h> +#include <lid_switch.h> #define LID_GPIO_PATH DT_PATH(named_gpios, lid_open_ec) #define LID_GPIO_PIN DT_GPIO_PIN(LID_GPIO_PATH, gpios) @@ -49,8 +50,8 @@ static void *lid_switch_setup(void) static void lid_switch_before(void *unused) { /* Make sure that interrupt fire at the next lid open/close */ - zassume_ok(emul_lid_close()); - zassume_ok(emul_lid_open()); + zassert_ok(emul_lid_close()); + zassert_ok(emul_lid_open()); k_sleep(K_MSEC(100)); } diff --git a/zephyr/test/drivers/default/src/lis2dw12.c b/zephyr/test/drivers/default/src/lis2dw12.c index 8f5e30e559..4cacf725ba 100644 --- a/zephyr/test/drivers/default/src/lis2dw12.c +++ b/zephyr/test/drivers/default/src/lis2dw12.c @@ -3,13 +3,14 @@ * found in the LICENSE file. */ -#include <zephyr/ztest.h> -#include <zephyr/drivers/emul.h> #include "driver/accel_lis2dw12.h" #include "emul/emul_common_i2c.h" #include "emul/emul_lis2dw12.h" #include "test/drivers/test_state.h" +#include <zephyr/drivers/emul.h> +#include <zephyr/ztest.h> + #define LIS2DW12_NODELABEL DT_NODELABEL(ms_lis2dw12_accel) #define LIS2DW12_SENSOR_ID SENSOR_ID(LIS2DW12_NODELABEL) #define LIS2DW12_EMUL_NODE DT_NODELABEL(lis2dw12_emul) diff --git a/zephyr/test/drivers/default/src/ln9310.c b/zephyr/test/drivers/default/src/ln9310.c index 414c62287c..509d6831ec 100644 --- a/zephyr/test/drivers/default/src/ln9310.c +++ b/zephyr/test/drivers/default/src/ln9310.c @@ -3,17 +3,17 @@ * found in the LICENSE file. */ -#include <zephyr/ztest.h> -#include <zephyr/drivers/emul.h> -#include <zephyr/kernel.h> -#include <zephyr/ztest_assert.h> -#include <zephyr/drivers/i2c_emul.h> - #include "driver/ln9310.h" -#include "emul/emul_ln9310.h" #include "emul/emul_common_i2c.h" -#include "timer.h" +#include "emul/emul_ln9310.h" #include "test/drivers/test_state.h" +#include "timer.h" + +#include <zephyr/drivers/emul.h> +#include <zephyr/drivers/i2c_emul.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> +#include <zephyr/ztest_assert.h> /* * TODO(b/201420132): Implement approach for tests to immediately schedule work diff --git a/zephyr/test/drivers/default/src/locate_chip.c b/zephyr/test/drivers/default/src/locate_chip.c index 6842543971..c54031c5df 100644 --- a/zephyr/test/drivers/default/src/locate_chip.c +++ b/zephyr/test/drivers/default/src/locate_chip.c @@ -3,15 +3,15 @@ * found in the LICENSE file. */ +#include "host_command.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" + #include <zephyr/fff.h> -#include <zephyr/shell/shell.h> #include <zephyr/kernel.h> +#include <zephyr/shell/shell.h> #include <zephyr/ztest.h> -#include "test/drivers/test_state.h" -#include "test/drivers/utils.h" -#include "host_command.h" - /** * @brief TestPurpose: test the TCPC locate valid case. */ diff --git a/zephyr/test/drivers/default/src/motion_sense/motion_sense.c b/zephyr/test/drivers/default/src/motion_sense/motion_sense.c index 42ee056874..8291e0e1c6 100644 --- a/zephyr/test/drivers/default/src/motion_sense/motion_sense.c +++ b/zephyr/test/drivers/default/src/motion_sense/motion_sense.c @@ -3,11 +3,13 @@ * found in the LICENSE file. */ -#include <zephyr/ztest.h> - #include "motion_sense.h" #include "test/drivers/test_state.h" +#include <zephyr/ztest.h> + +extern enum chipset_state_mask sensor_active; + ZTEST_SUITE(motion_sense, drivers_predicate_post_main, NULL, NULL, NULL, NULL); ZTEST_USER(motion_sense, ec_motion_sensor_fill_values) @@ -33,3 +35,17 @@ ZTEST_USER(motion_sense, ec_motion_sensor_clamp_i16) zassert_equal(ec_motion_sensor_clamp_i16(INT16_MIN - 1), INT16_MIN, NULL); } + +ZTEST_USER(motion_sense, ec_motion_sense_get_ec_config) +{ + /* illegal state, should be translated to S5 */ + sensor_active = 42; + zassert_equal(motion_sense_get_ec_config(), SENSOR_CONFIG_EC_S5); + /* all valid states */ + sensor_active = SENSOR_ACTIVE_S0; + zassert_equal(motion_sense_get_ec_config(), SENSOR_CONFIG_EC_S0); + sensor_active = SENSOR_ACTIVE_S3; + zassert_equal(motion_sense_get_ec_config(), SENSOR_CONFIG_EC_S3); + sensor_active = SENSOR_ACTIVE_S5; + zassert_equal(motion_sense_get_ec_config(), SENSOR_CONFIG_EC_S5); +} diff --git a/zephyr/test/drivers/default/src/panic.c b/zephyr/test/drivers/default/src/panic.c index 7dcb18e4cf..a2addc786d 100644 --- a/zephyr/test/drivers/default/src/panic.c +++ b/zephyr/test/drivers/default/src/panic.c @@ -8,18 +8,17 @@ * @brief Unit Tests for panic. */ -#include <zephyr/device.h> - -#include <zephyr/logging/log.h> -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> - #include "common.h" #include "ec_tasks.h" #include "panic.h" #include "test/drivers/stubs.h" #include "test/drivers/test_state.h" +#include <zephyr/device.h> +#include <zephyr/kernel.h> +#include <zephyr/logging/log.h> +#include <zephyr/ztest.h> + struct panic_test_fixture { struct panic_data saved_pdata; }; diff --git a/zephyr/test/drivers/default/src/panic_output.c b/zephyr/test/drivers/default/src/panic_output.c index 210c862901..f815744d8c 100644 --- a/zephyr/test/drivers/default/src/panic_output.c +++ b/zephyr/test/drivers/default/src/panic_output.c @@ -3,12 +3,12 @@ * found in the LICENSE file. */ -#include <zephyr/ztest.h> - #include "panic.h" #include "test/drivers/test_mocks.h" #include "test/drivers/test_state.h" +#include <zephyr/ztest.h> + ZTEST_SUITE(panic_output, drivers_predicate_post_main, NULL, NULL, NULL, NULL); ZTEST(panic_output, test_panic_printf) diff --git a/zephyr/test/drivers/default/src/port80.c b/zephyr/test/drivers/default/src/port80.c index 8563c2e478..6bde1392c7 100644 --- a/zephyr/test/drivers/default/src/port80.c +++ b/zephyr/test/drivers/default/src/port80.c @@ -8,19 +8,18 @@ * @brief Unit Tests for ESPI port 80 writes */ -#include <zephyr/logging/log.h> -#include <zephyr/shell/shell.h> -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> - #include "console.h" #include "ec_commands.h" #include "host_command.h" #include "port80.h" - #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include <zephyr/kernel.h> +#include <zephyr/logging/log.h> +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + /* * Flush any existing writes. */ diff --git a/zephyr/test/drivers/default/src/power_common.c b/zephyr/test/drivers/default/src/power_common.c index f96fed9f05..2b8ba4ac88 100644 --- a/zephyr/test/drivers/default/src/power_common.c +++ b/zephyr/test/drivers/default/src/power_common.c @@ -3,31 +3,30 @@ * found in the LICENSE file. */ -#include <string.h> -#include <zephyr/ztest.h> -#include <zephyr/drivers/gpio.h> -#include <zephyr/drivers/gpio/gpio_emul.h> -#include <zephyr/shell/shell.h> -#include <zephyr/shell/shell_dummy.h> -#include <zephyr/shell/shell_uart.h> - +#include "battery.h" +#include "battery_smart.h" #include "chipset.h" #include "common.h" +#include "ec_tasks.h" +#include "emul/emul_common_i2c.h" +#include "emul/emul_smart_battery.h" #include "extpower.h" #include "hooks.h" #include "host_command.h" #include "power.h" -#include "test/drivers/stubs.h" #include "task.h" -#include "ec_tasks.h" +#include "test/drivers/stubs.h" #include "test/drivers/test_state.h" +#include "test/drivers/utils.h" -#include "emul/emul_common_i2c.h" -#include "emul/emul_smart_battery.h" +#include <string.h> -#include "battery.h" -#include "battery_smart.h" -#include "test/drivers/utils.h" +#include <zephyr/drivers/gpio.h> +#include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/shell/shell.h> +#include <zephyr/shell/shell_dummy.h> +#include <zephyr/shell/shell_uart.h> +#include <zephyr/ztest.h> #define BATTERY_NODE DT_NODELABEL(battery) @@ -488,6 +487,19 @@ ZTEST(power_common, power_console_cmd) } /** + * Test powerinfo console command + */ +ZTEST_USER(power_common, powerinfo_console_cmd) +{ + char expected_buffer[32]; + + snprintf(expected_buffer, sizeof(expected_buffer), "power state %d", + power_get_state()); + + CHECK_CONSOLE_CMD("powerinfo", expected_buffer, EC_SUCCESS); +} + +/** * Common setup for hibernation delay tests. Smart discharge zone is setup, * battery is set in safe zone (which trigger hibernation), power state is * set to G3 and AC is disabled. system_hibernate mock is reset. diff --git a/zephyr/test/drivers/default/src/ppc_sn5s330.c b/zephyr/test/drivers/default/src/ppc_sn5s330.c index eb595939fc..664d9fed36 100644 --- a/zephyr/test/drivers/default/src/ppc_sn5s330.c +++ b/zephyr/test/drivers/default/src/ppc_sn5s330.c @@ -3,20 +3,20 @@ * found in the LICENSE file. */ -#include <zephyr/kernel.h> -#include <zephyr/device.h> -#include <zephyr/devicetree.h> -#include <zephyr/drivers/emul.h> -#include <zephyr/ztest.h> -#include <zephyr/fff.h> - #include "driver/ppc/sn5s330.h" #include "driver/ppc/sn5s330_public.h" #include "emul/emul_common_i2c.h" #include "emul/emul_sn5s330.h" -#include "usbc_ppc.h" #include "test/drivers/test_mocks.h" #include "test/drivers/test_state.h" +#include "usbc_ppc.h" + +#include <zephyr/device.h> +#include <zephyr/devicetree.h> +#include <zephyr/drivers/emul.h> +#include <zephyr/fff.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> /** This must match the index of the sn5s330 in ppc_chips[] */ #define SN5S330_PORT 0 diff --git a/zephyr/test/drivers/default/src/ppc_syv682x.c b/zephyr/test/drivers/default/src/ppc_syv682x.c index ba2e68f2ff..6e1602804e 100644 --- a/zephyr/test/drivers/default/src/ppc_syv682x.c +++ b/zephyr/test/drivers/default/src/ppc_syv682x.c @@ -3,24 +3,24 @@ * found in the LICENSE file. */ +#include "emul/emul_common_i2c.h" +#include "emul/emul_syv682x.h" +#include "syv682x.h" +#include "test/drivers/stubs.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" +#include "timer.h" +#include "usbc_ppc.h" + #include <zephyr/device.h> #include <zephyr/devicetree/gpio.h> -#include <zephyr/drivers/gpio/gpio_emul.h> #include <zephyr/drivers/gpio.h> +#include <zephyr/drivers/gpio/gpio_emul.h> #include <zephyr/fff.h> #include <zephyr/kernel.h> #include <zephyr/ztest.h> #include <zephyr/ztest_assert.h> -#include "emul/emul_common_i2c.h" -#include "emul/emul_syv682x.h" -#include "test/drivers/stubs.h" -#include "syv682x.h" -#include "timer.h" -#include "test/drivers/test_state.h" -#include "test/drivers/utils.h" -#include "usbc_ppc.h" - #define SYV682X_NODE DT_NODELABEL(syv682x_emul) #define GPIO_USB_C1_FRS_EN_PATH DT_PATH(named_gpios, usb_c1_frs_en) @@ -46,10 +46,10 @@ static void *syv682x_test_setup(void) fixture.ppc_emul = EMUL_DT_GET(SYV682X_NODE); fixture.common_data = emul_syv682x_get_i2c_common_data(fixture.ppc_emul); - zassume_not_null(fixture.ppc_emul, NULL); + zassert_not_null(fixture.ppc_emul, NULL); fixture.frs_en_gpio_port = DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_USB_C1_FRS_EN_PATH, gpios)); - zassume_not_null(fixture.frs_en_gpio_port, NULL); + zassert_not_null(fixture.frs_en_gpio_port, NULL); fixture.frs_en_gpio_pin = DT_GPIO_PIN(GPIO_USB_C1_FRS_EN_PATH, gpios); return &fixture; @@ -62,7 +62,7 @@ static void syv682x_test_after(void *data) struct i2c_common_emul_data *common_data = fixture->common_data; /* Disable the power path and clear interrupt conditions. */ - zassume_ok(syv682x_emul_set_reg(emul, SYV682X_CONTROL_1_REG, + zassert_ok(syv682x_emul_set_reg(emul, SYV682X_CONTROL_1_REG, SYV682X_CONTROL_1_PWR_ENB), NULL); syv682x_emul_set_condition(emul, SYV682X_STATUS_NONE, @@ -120,14 +120,14 @@ ZTEST_F(ppc_syv682x, test_syv682x_init_dead_battery) * With a dead battery, the device powers up sinking VBUS, and the * driver should keep that going. */ - zassume_ok(syv682x_emul_set_reg(fixture->ppc_emul, + zassert_ok(syv682x_emul_set_reg(fixture->ppc_emul, SYV682X_CONTROL_1_REG, SYV682X_CONTROL_1_CH_SEL), NULL); syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_VSAFE_5V, SYV682X_CONTROL_4_NONE); zassert_ok(ppc_init(syv682x_port), "PPC init failed"); - zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + zassert_ok(syv682x_emul_get_reg(fixture->ppc_emul, SYV682X_CONTROL_1_REG, ®), NULL); zassert_true(reg & SYV682X_CONTROL_1_CH_SEL, @@ -144,14 +144,14 @@ ZTEST_F(ppc_syv682x, test_syv682x_init_vsafe0v) uint8_t reg; /* With VBUS at vSafe0V, init should set the default configuration. */ - zassume_ok(syv682x_emul_set_reg(fixture->ppc_emul, + zassert_ok(syv682x_emul_set_reg(fixture->ppc_emul, SYV682X_CONTROL_1_REG, SYV682X_CONTROL_1_PWR_ENB), NULL); syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_VSAFE_0V, SYV682X_CONTROL_4_NONE); zassert_ok(ppc_init(syv682x_port), "PPC init failed"); - zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + zassert_ok(syv682x_emul_get_reg(fixture->ppc_emul, SYV682X_CONTROL_1_REG, ®), NULL); check_control_1_default_init(reg); @@ -162,14 +162,14 @@ ZTEST_F(ppc_syv682x, test_syv682x_init_sink_disabled) uint8_t reg; /* With sink disabled, init should do the same thing. */ - zassume_ok(syv682x_emul_set_reg(fixture->ppc_emul, + zassert_ok(syv682x_emul_set_reg(fixture->ppc_emul, SYV682X_CONTROL_1_REG, SYV682X_CONTROL_1_CH_SEL), NULL); syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_VSAFE_0V, SYV682X_CONTROL_4_NONE); zassert_ok(ppc_init(syv682x_port), "PPC init failed"); - zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + zassert_ok(syv682x_emul_get_reg(fixture->ppc_emul, SYV682X_CONTROL_1_REG, ®), NULL); check_control_1_default_init(reg); @@ -181,7 +181,7 @@ ZTEST_F(ppc_syv682x, test_syv682x_init_common) int ilim; zassert_ok(ppc_init(syv682x_port), "PPC init failed"); - zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + zassert_ok(syv682x_emul_get_reg(fixture->ppc_emul, SYV682X_CONTROL_1_REG, ®), NULL); @@ -196,7 +196,7 @@ ZTEST_F(ppc_syv682x, test_syv682x_init_common) ilim = (reg & SYV682X_5V_ILIM_MASK) >> SYV682X_5V_ILIM_BIT_SHIFT; zassert_equal(ilim, CONFIG_PLATFORM_EC_USB_PD_PULLUP, "Default init, but 5V current limit set to %d", ilim); - zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + zassert_ok(syv682x_emul_get_reg(fixture->ppc_emul, SYV682X_CONTROL_2_REG, ®), NULL); zassert_equal(reg, @@ -205,14 +205,14 @@ ZTEST_F(ppc_syv682x, test_syv682x_init_common) << SYV682X_DSG_RON_SHIFT) | (SYV682X_DSG_TIME_50MS << SYV682X_DSG_TIME_SHIFT), "Default init, but CONTROL_2 is 0x%x", reg); - zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + zassert_ok(syv682x_emul_get_reg(fixture->ppc_emul, SYV682X_CONTROL_3_REG, ®), NULL); zassert_equal(reg, (SYV682X_OVP_23_7 << SYV682X_OVP_BIT_SHIFT) | SYV682X_RVS_MASK, "Default init, but CONTROL_3 is 0x%x", reg); - zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + zassert_ok(syv682x_emul_get_reg(fixture->ppc_emul, SYV682X_CONTROL_4_REG, ®), NULL); zassert_equal(reg & ~SYV682X_CONTROL_4_INT_MASK, @@ -226,7 +226,7 @@ ZTEST_F(ppc_syv682x, test_syv682x_vbus_source_enable) zassert_ok(ppc_vbus_source_enable(syv682x_port, true), "VBUS enable failed"); - zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + zassert_ok(syv682x_emul_get_reg(fixture->ppc_emul, SYV682X_CONTROL_1_REG, ®), "Reading CONTROL_1 failed"); zassert_equal(reg & SYV682X_CONTROL_1_PWR_ENB, 0, @@ -245,7 +245,7 @@ ZTEST_F(ppc_syv682x, test_syv682x_vbus_source_disable) ZTEST_F(ppc_syv682x, test_syv682x_interrupt_source_oc) { - zassume_ok(ppc_vbus_source_enable(syv682x_port, true), + zassert_ok(ppc_vbus_source_enable(syv682x_port, true), "VBUS enable failed"); /* An OC event less than 100 ms should not cause VBUS to turn off. */ syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_OC_5V, @@ -266,7 +266,7 @@ ZTEST_F(ppc_syv682x, test_syv682x_interrupt_tsd) * (The device will have already physically disabled them.) The state of * the sink path is not part of the driver's API. */ - zassume_ok(ppc_vbus_source_enable(syv682x_port, true), + zassert_ok(ppc_vbus_source_enable(syv682x_port, true), "Source enable failed"); syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_TSD, SYV682X_CONTROL_4_NONE); @@ -278,7 +278,7 @@ ZTEST_F(ppc_syv682x, test_syv682x_interrupt_tsd) ZTEST_F(ppc_syv682x, test_syv682x_interrupt_vbus_ovp) { /* An OVP event should cause the driver to disable the source path. */ - zassume_ok(ppc_vbus_source_enable(syv682x_port, true), + zassert_ok(ppc_vbus_source_enable(syv682x_port, true), "Source enable failed"); syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_OVP, SYV682X_CONTROL_4_NONE); @@ -296,12 +296,12 @@ ZTEST_F(ppc_syv682x, test_syv682x_interrupt_vbus_hv_oc) * re-enable the sink path until the OC count limit is reached, at which * point the driver should leave it disabled. */ - zassume_ok(ppc_vbus_sink_enable(syv682x_port, true), + zassert_ok(ppc_vbus_sink_enable(syv682x_port, true), "Sink enable failed"); syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_OC_HV, SYV682X_CONTROL_4_NONE); msleep(1); - zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + zassert_ok(syv682x_emul_get_reg(fixture->ppc_emul, SYV682X_CONTROL_1_REG, ®), "Reading CONTROL_1 failed"); zassert_equal(reg & SYV682X_CONTROL_1_PWR_ENB, 0, @@ -310,7 +310,7 @@ ZTEST_F(ppc_syv682x, test_syv682x_interrupt_vbus_hv_oc) SYV682X_CONTROL_4_NONE); /* Alert GPIO doesn't change so wait for delayed syv682x interrupt */ msleep(15); - zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + zassert_ok(syv682x_emul_get_reg(fixture->ppc_emul, SYV682X_CONTROL_1_REG, ®), "Reading CONTROL_1 failed"); zassert_equal(reg & SYV682X_CONTROL_1_PWR_ENB, 0, @@ -319,7 +319,7 @@ ZTEST_F(ppc_syv682x, test_syv682x_interrupt_vbus_hv_oc) SYV682X_CONTROL_4_NONE); /* Alert GPIO doesn't change so wait for delayed syv682x interrupt */ msleep(15); - zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + zassert_ok(syv682x_emul_get_reg(fixture->ppc_emul, SYV682X_CONTROL_1_REG, ®), "Reading CONTROL_1 failed"); zassert_equal(reg & SYV682X_CONTROL_1_PWR_ENB, @@ -339,14 +339,14 @@ ZTEST_F(ppc_syv682x, test_syv682x_interrupt_vconn_oc) syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_NONE, SYV682X_CONTROL_4_VCONN_OCP); msleep(1); - zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + zassert_ok(syv682x_emul_get_reg(fixture->ppc_emul, SYV682X_CONTROL_4_REG, ®), "Reading CONTROL_4 failed"); zassert_true(reg & (SYV682X_CONTROL_4_VCONN1 | SYV682X_CONTROL_4_VCONN2), "VCONN disabled after initial VCONN OC"); msleep(50); - zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + zassert_ok(syv682x_emul_get_reg(fixture->ppc_emul, SYV682X_CONTROL_4_REG, ®), "Reading CONTROL_4 failed"); zassert_true(reg & (SYV682X_CONTROL_4_VCONN1 | @@ -357,7 +357,7 @@ ZTEST_F(ppc_syv682x, test_syv682x_interrupt_vconn_oc) * should turn VCONN off. */ msleep(60); - zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + zassert_ok(syv682x_emul_get_reg(fixture->ppc_emul, SYV682X_CONTROL_4_REG, ®), "Reading CONTROL_4 failed"); zassert_false(reg & (SYV682X_CONTROL_4_VCONN1 | @@ -379,7 +379,7 @@ ZTEST_F(ppc_syv682x, test_syv682x_interrupt_vconn_ov) syv682x_emul_set_condition(fixture->ppc_emul, SYV682X_STATUS_NONE, SYV682X_CONTROL_4_VBAT_OVP); msleep(1); - zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + zassert_ok(syv682x_emul_get_reg(fixture->ppc_emul, SYV682X_CONTROL_4_REG, ®), "Reading CONTROL_4 failed"); zassert_true(reg & SYV682X_CONTROL_4_CC1_BPS, @@ -402,7 +402,7 @@ ZTEST_F(ppc_syv682x, test_syv682x_frs_enable) * polarity. Disabling FRS should enable both CC lines. */ ppc_vbus_sink_enable(syv682x_port, true); - zassume_false(ppc_is_sourcing_vbus(syv682x_port), + zassert_false(ppc_is_sourcing_vbus(syv682x_port), "PPC is sourcing VBUS after sink enabled"); ppc_set_polarity(syv682x_port, 0 /* CC1 */); ppc_set_frs_enable(syv682x_port, true); @@ -424,14 +424,14 @@ ZTEST_F(ppc_syv682x, test_syv682x_frs_disable) uint8_t reg; ppc_vbus_sink_enable(syv682x_port, true); - zassume_false(ppc_is_sourcing_vbus(syv682x_port), + zassert_false(ppc_is_sourcing_vbus(syv682x_port), "PPC is sourcing VBUS after sink enabled"); ppc_set_polarity(syv682x_port, 0 /* CC1 */); ppc_set_frs_enable(syv682x_port, false); zassert_equal(gpio_emul_output_get(gpio_dev, fixture->frs_en_gpio_pin), 0, "FRS disabled, but FRS GPIO not deasserted"); - zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + zassert_ok(syv682x_emul_get_reg(fixture->ppc_emul, SYV682X_CONTROL_4_REG, ®), "Reading CONTROL_4 failed"); zassert_equal( @@ -463,7 +463,7 @@ ZTEST_F(ppc_syv682x, test_syv682x_source_current_limit_usb_default) zassert_ok(ppc_set_vbus_source_current_limit(syv682x_port, TYPEC_RP_USB), "Could not set source current limit"); - zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + zassert_ok(syv682x_emul_get_reg(fixture->ppc_emul, SYV682X_CONTROL_1_REG, ®), "Reading CONTROL_1 failed"); ilim_val = (reg & SYV682X_5V_ILIM_MASK) >> SYV682X_5V_ILIM_BIT_SHIFT; @@ -479,7 +479,7 @@ ZTEST_F(ppc_syv682x, test_syv682x_source_current_limit_1500ma) zassert_ok(ppc_set_vbus_source_current_limit(syv682x_port, TYPEC_RP_1A5), "Could not set source current limit"); - zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + zassert_ok(syv682x_emul_get_reg(fixture->ppc_emul, SYV682X_CONTROL_1_REG, ®), "Reading CONTROL_1 failed"); ilim_val = (reg & SYV682X_5V_ILIM_MASK) >> SYV682X_5V_ILIM_BIT_SHIFT; @@ -495,7 +495,7 @@ ZTEST_F(ppc_syv682x, test_syv682x_source_current_limit_3000ma) zassert_ok(ppc_set_vbus_source_current_limit(syv682x_port, TYPEC_RP_3A0), "Could not set source current limit"); - zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + zassert_ok(syv682x_emul_get_reg(fixture->ppc_emul, SYV682X_CONTROL_1_REG, ®), "Reading CONTROL_1 failed"); ilim_val = (reg & SYV682X_5V_ILIM_MASK) >> SYV682X_5V_ILIM_BIT_SHIFT; @@ -561,7 +561,7 @@ ZTEST_F(ppc_syv682x, test_syv682x_vbus_sink_enable_trivial) * If VBUS source is already enabled, disabling VBUS sink should * trivially succeed. */ - zassume_ok(ppc_vbus_source_enable(syv682x_port, true), + zassert_ok(ppc_vbus_source_enable(syv682x_port, true), "VBUS enable failed"); zassert_ok(ppc_vbus_sink_enable(syv682x_port, false), "Sink disable failed"); @@ -576,11 +576,11 @@ ZTEST_F(ppc_syv682x, test_syv682x_vbus_sink_enable_power_path) * After enabling VBUS sink, the HV power path should be enabled in sink * mode with the configured current limit. */ - zassume_ok(ppc_vbus_source_enable(syv682x_port, false), + zassert_ok(ppc_vbus_source_enable(syv682x_port, false), "VBUS enable failed"); zassert_ok(ppc_vbus_sink_enable(syv682x_port, true), "Sink disable failed"); - zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + zassert_ok(syv682x_emul_get_reg(fixture->ppc_emul, SYV682X_CONTROL_1_REG, ®), NULL); zassert_true(reg & SYV682X_CONTROL_1_CH_SEL, @@ -598,14 +598,14 @@ ZTEST_F(ppc_syv682x, test_syv682x_vbus_sink_disable) { uint8_t reg; - zassume_ok(ppc_vbus_source_enable(syv682x_port, false), + zassert_ok(ppc_vbus_source_enable(syv682x_port, false), "VBUS enable failed"); zassert_ok(ppc_vbus_sink_enable(syv682x_port, true), "Sink disable failed"); zassert_ok(ppc_vbus_sink_enable(syv682x_port, false), "Sink disable failed"); - zassume_ok(syv682x_emul_get_reg(fixture->ppc_emul, + zassert_ok(syv682x_emul_get_reg(fixture->ppc_emul, SYV682X_CONTROL_1_REG, ®), NULL); zassert_true(reg & SYV682X_CONTROL_1_PWR_ENB, diff --git a/zephyr/test/drivers/default/src/ps8xxx.c b/zephyr/test/drivers/default/src/ps8xxx.c index 2aa1844c10..cb0ac7d2d5 100644 --- a/zephyr/test/drivers/default/src/ps8xxx.c +++ b/zephyr/test/drivers/default/src/ps8xxx.c @@ -3,22 +3,21 @@ * found in the LICENSE file. */ -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> - #include "common.h" +#include "driver/tcpm/ps8xxx.h" +#include "driver/tcpm/ps8xxx_public.h" #include "emul/emul_common_i2c.h" -#include "emul/tcpc/emul_tcpci.h" #include "emul/tcpc/emul_ps8xxx.h" -#include "timer.h" +#include "emul/tcpc/emul_tcpci.h" #include "i2c.h" +#include "tcpm/tcpci.h" #include "test/drivers/stubs.h" #include "test/drivers/tcpci_test_common.h" - -#include "tcpm/tcpci.h" -#include "driver/tcpm/ps8xxx.h" -#include "driver/tcpm/ps8xxx_public.h" #include "test/drivers/test_state.h" +#include "timer.h" + +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #define PS8XXX_EMUL_NODE DT_NODELABEL(ps8xxx_emul) @@ -1411,7 +1410,7 @@ static void ps8805_before(void *state) board_set_ps8xxx_product_id(PS8805_PRODUCT_ID); ps8xxx_emul_set_product_id(ps8xxx_emul, PS8805_PRODUCT_ID); setup_no_fail_all(); - zassume_equal(EC_SUCCESS, ps8xxx_tcpm_drv.init(USBC_PORT_C1)); + zassert_equal(EC_SUCCESS, ps8xxx_tcpm_drv.init(USBC_PORT_C1)); } static void ps8805_after(void *state) @@ -1436,7 +1435,7 @@ static void ps8815_before(void *state) ps8xxx_emul_set_reg_id(ps8xxx_emul, PS8815_REG_ID); ps8xxx_emul_set_product_id(ps8xxx_emul, PS8815_PRODUCT_ID); setup_no_fail_all(); - zassume_equal(EC_SUCCESS, ps8xxx_tcpm_drv.init(USBC_PORT_C1)); + zassert_equal(EC_SUCCESS, ps8xxx_tcpm_drv.init(USBC_PORT_C1)); } static void ps8815_after(void *state) @@ -1461,7 +1460,7 @@ static void ps8745_before(void *state) ps8xxx_emul_set_product_id(ps8xxx_emul, PS8815_PRODUCT_ID); ps8xxx_emul_set_reg_id(ps8xxx_emul, PS8745_REG_ID); setup_no_fail_all(); - zassume_equal(EC_SUCCESS, ps8xxx_tcpm_drv.init(USBC_PORT_C1), NULL); + zassert_equal(EC_SUCCESS, ps8xxx_tcpm_drv.init(USBC_PORT_C1), NULL); } static void ps8745_after(void *state) diff --git a/zephyr/test/drivers/default/src/smart.c b/zephyr/test/drivers/default/src/smart.c index 9db292ac96..a26d84b93c 100644 --- a/zephyr/test/drivers/default/src/smart.c +++ b/zephyr/test/drivers/default/src/smart.c @@ -3,21 +3,20 @@ * found in the LICENSE file. */ -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> -#include <zephyr/shell/shell.h> -#include <zephyr/shell/shell_uart.h> - +#include "battery.h" +#include "battery_smart.h" #include "common.h" #include "console.h" -#include "i2c.h" #include "emul/emul_common_i2c.h" #include "emul/emul_smart_battery.h" - -#include "battery.h" -#include "battery_smart.h" +#include "i2c.h" #include "test/drivers/test_state.h" +#include <zephyr/kernel.h> +#include <zephyr/shell/shell.h> +#include <zephyr/shell/shell_uart.h> +#include <zephyr/ztest.h> + #define BATTERY_NODE DT_NODELABEL(battery) /** Test all simple getters */ diff --git a/zephyr/test/drivers/default/src/stm_mems_common.c b/zephyr/test/drivers/default/src/stm_mems_common.c index f7c59105b0..09f1cf8506 100644 --- a/zephyr/test/drivers/default/src/stm_mems_common.c +++ b/zephyr/test/drivers/default/src/stm_mems_common.c @@ -3,11 +3,6 @@ * found in the LICENSE file. */ -#include <zephyr/ztest.h> -#include <zephyr/device.h> -#include <zephyr/devicetree.h> -#include <errno.h> - #include "common.h" #include "driver/stm_mems_common.h" #include "emul/emul_common_i2c.h" @@ -15,6 +10,12 @@ #include "i2c/i2c.h" #include "test/drivers/test_state.h" +#include <errno.h> + +#include <zephyr/device.h> +#include <zephyr/devicetree.h> +#include <zephyr/ztest.h> + #define MOCK_EMUL EMUL_DT_GET(DT_NODELABEL(i2c_mock)) #define COMMON_DATA emul_i2c_mock_get_i2c_common_data(MOCK_EMUL) diff --git a/zephyr/test/drivers/default/src/tablet_mode.c b/zephyr/test/drivers/default/src/tablet_mode.c index d600d26072..773f2c2bf8 100644 --- a/zephyr/test/drivers/default/src/tablet_mode.c +++ b/zephyr/test/drivers/default/src/tablet_mode.c @@ -3,16 +3,16 @@ * found in the LICENSE file. */ -#include <zephyr/fff.h> -#include <zephyr/shell/shell.h> -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> - #include "console.h" #include "tablet_mode.h" #include "test/drivers/test_state.h" #include "test/drivers/utils.h" +#include <zephyr/fff.h> +#include <zephyr/kernel.h> +#include <zephyr/shell/shell.h> +#include <zephyr/ztest.h> + static void tabletmode_before(void *state) { ARG_UNUSED(state); @@ -34,27 +34,31 @@ ZTEST_USER(tabletmode, test_tablet_set_mode) int ret; ret = tablet_get_mode(); - zassert_equal(ret, 0, "unexepcted tablet initial mode: %d", ret); + zassert_equal(ret, 0, "unexpected tablet initial mode: %d", ret); tablet_set_mode(1, TABLET_TRIGGER_LID); - ret = tablet_get_mode(); - zassert_equal(ret, 1, "unexepcted tablet mode: %d", ret); + zassert_equal(ret, 1, "unexpected tablet mode: %d", ret); tablet_set_mode(1, TABLET_TRIGGER_BASE); - ret = tablet_get_mode(); - zassert_equal(ret, 1, "unexepcted tablet mode: %d", ret); + zassert_equal(ret, 1, "unexpected tablet mode: %d", ret); + /** + * Tablet mode should remain enabled, since both _LID and _BASE were set + * previously, and this only clears _LID. + */ tablet_set_mode(0, TABLET_TRIGGER_LID); - ret = tablet_get_mode(); - zassert_equal(ret, 1, "unexepcted tablet mode: %d", ret); + zassert_equal(ret, 1, "unexpected tablet mode: %d", ret); + /** + * Both _LID and _BASE are now cleared, so DUT is no longer in tablet + * mode. + */ tablet_set_mode(0, TABLET_TRIGGER_BASE); - ret = tablet_get_mode(); - zassert_equal(ret, 0, "unexepcted tablet mode: %d", ret); + zassert_equal(ret, 0, "unexpected tablet mode: %d", ret); } /** @@ -65,13 +69,13 @@ ZTEST_USER(tabletmode, test_tablet_disable) int ret; ret = tablet_get_mode(); - zassert_equal(ret, 0, "unexepcted tablet initial mode: %d", ret); + zassert_equal(ret, 0, "unexpected tablet initial mode: %d", ret); tablet_disable(); tablet_set_mode(1, TABLET_TRIGGER_LID); ret = tablet_get_mode(); - zassert_equal(ret, 0, "unexepcted tablet mode: %d", ret); + zassert_equal(ret, 0, "unexpected tablet mode: %d", ret); } /** @@ -82,28 +86,28 @@ ZTEST_USER(tabletmode, test_settabletmode_on_off) int ret; ret = tablet_get_mode(); - zassert_equal(ret, 0, "unexepcted tablet initial mode: %d", ret); + zassert_equal(ret, 0, "unexpected tablet initial mode: %d", ret); ret = shell_execute_cmd(get_ec_shell(), "tabletmode"); - zassert_equal(ret, EC_SUCCESS, "unexepcted command return status: %d", + zassert_equal(ret, EC_SUCCESS, "unexpected command return status: %d", ret); ret = tablet_get_mode(); - zassert_equal(ret, 0, "unexepcted tablet mode: %d", ret); + zassert_equal(ret, 0, "unexpected tablet mode: %d", ret); ret = shell_execute_cmd(get_ec_shell(), "tabletmode on"); - zassert_equal(ret, EC_SUCCESS, "unexepcted command return status: %d", + zassert_equal(ret, EC_SUCCESS, "unexpected command return status: %d", ret); ret = tablet_get_mode(); - zassert_equal(ret, 1, "unexepcted tablet mode: %d", ret); + zassert_equal(ret, 1, "unexpected tablet mode: %d", ret); ret = shell_execute_cmd(get_ec_shell(), "tabletmode off"); - zassert_equal(ret, EC_SUCCESS, "unexepcted command return status: %d", + zassert_equal(ret, EC_SUCCESS, "unexpected command return status: %d", ret); ret = tablet_get_mode(); - zassert_equal(ret, 0, "unexepcted tablet mode: %d", ret); + zassert_equal(ret, 0, "unexpected tablet mode: %d", ret); } /** @@ -115,28 +119,28 @@ ZTEST_USER(tabletmode, test_settabletmode_forced) int ret; ret = tablet_get_mode(); - zassert_equal(ret, 0, "unexepcted tablet initial mode: %d", ret); + zassert_equal(ret, 0, "unexpected tablet initial mode: %d", ret); ret = shell_execute_cmd(get_ec_shell(), "tabletmode on"); - zassert_equal(ret, EC_SUCCESS, "unexepcted command return status: %d", + zassert_equal(ret, EC_SUCCESS, "unexpected command return status: %d", ret); ret = tablet_get_mode(); - zassert_equal(ret, 1, "unexepcted tablet mode: %d", ret); + zassert_equal(ret, 1, "unexpected tablet mode: %d", ret); tablet_set_mode(0, TABLET_TRIGGER_LID); ret = tablet_get_mode(); - zassert_equal(ret, 1, "unexepcted tablet mode: %d", ret); + zassert_equal(ret, 1, "unexpected tablet mode: %d", ret); ret = shell_execute_cmd(get_ec_shell(), "tabletmode reset"); - zassert_equal(ret, EC_SUCCESS, "unexepcted command return status: %d", + zassert_equal(ret, EC_SUCCESS, "unexpected command return status: %d", ret); tablet_set_mode(0, TABLET_TRIGGER_LID); ret = tablet_get_mode(); - zassert_equal(ret, 0, "unexepcted tablet mode: %d", ret); + zassert_equal(ret, 0, "unexpected tablet mode: %d", ret); } /** @@ -149,7 +153,7 @@ ZTEST_USER(tabletmode, test_settabletmode_too_many_args) ret = shell_execute_cmd(get_ec_shell(), "tabletmode too many arguments"); zassert_equal(ret, EC_ERROR_PARAM_COUNT, - "unexepcted command return status: %d", ret); + "unexpected command return status: %d", ret); } /** @@ -161,7 +165,7 @@ ZTEST_USER(tabletmode, test_settabletmode_unknown_arg) ret = shell_execute_cmd(get_ec_shell(), "tabletmode X"); zassert_equal(ret, EC_ERROR_PARAM1, - "unexepcted command return status: %d", ret); + "unexpected command return status: %d", ret); } ZTEST_SUITE(tabletmode, drivers_predicate_post_main, NULL, tabletmode_before, diff --git a/zephyr/test/drivers/default/src/task.c b/zephyr/test/drivers/default/src/task.c new file mode 100644 index 0000000000..4c4087c5a1 --- /dev/null +++ b/zephyr/test/drivers/default/src/task.c @@ -0,0 +1,84 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "task.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" +#include "timer.h" + +#include <zephyr/ztest.h> + +struct tasks_fixture { + timestamp_t fake_time; +}; + +static void *setup(void) +{ + static struct tasks_fixture fixture; + + return &fixture; +} + +static void before(void *f) +{ + struct tasks_fixture *fixture = f; + + fixture->fake_time.val = 0; +} + +static void after(void *f) +{ + ARG_UNUSED(f); + + get_time_mock = NULL; +} + +ZTEST_SUITE(tasks, drivers_predicate_post_main, setup, before, after, NULL); + +ZTEST(tasks, test_enable_irq) +{ + arch_irq_disable(0); + task_enable_irq(0); + zassert_true(arch_irq_is_enabled(0)); +} + +ZTEST(tasks, test_interrupt_context) +{ + zassert_false(in_interrupt_context()); +} + +ZTEST_F(tasks, test_timer_arm_before_now) +{ + timestamp_t deadline = { + .val = 5, + }; + + fixture->fake_time.val = 15; + get_time_mock = &fixture->fake_time; + + zassert_ok(timer_arm(deadline, TASK_ID_MOTIONSENSE)); + zassert_equal(*task_get_event_bitmap(TASK_ID_MOTIONSENSE) & + TASK_EVENT_TIMER, + TASK_EVENT_TIMER); +} + +ZTEST_F(tasks, test_timer_arm_busy) +{ + timestamp_t deadline = { + .val = UINT64_C(5000000), + }; + + fixture->fake_time.val = 0; + get_time_mock = &fixture->fake_time; + + zassert_ok(timer_arm(deadline, TASK_ID_MOTIONSENSE)); + zassert_equal(EC_ERROR_BUSY, timer_arm(deadline, TASK_ID_MOTIONSENSE)); +} + +ZTEST(tasks, test_get_event_bitmap_invalid_tid) +{ + zassert_is_null( + task_get_event_bitmap(TASK_ID_COUNT + EXTRA_TASK_COUNT)); +} diff --git a/zephyr/test/drivers/default/src/tcpci.c b/zephyr/test/drivers/default/src/tcpci.c index db0ad076ab..f648b43a96 100644 --- a/zephyr/test/drivers/default/src/tcpci.c +++ b/zephyr/test/drivers/default/src/tcpci.c @@ -3,25 +3,27 @@ * found in the LICENSE file. */ -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> -#include <zephyr/drivers/gpio.h> -#include <zephyr/drivers/gpio/gpio_emul.h> - #include "common.h" #include "ec_tasks.h" #include "emul/emul_common_i2c.h" #include "emul/tcpc/emul_tcpci.h" #include "hooks.h" #include "i2c.h" +#include "tcpm/tcpci.h" #include "test/drivers/stubs.h" #include "test/drivers/tcpci_test_common.h" - -#include "tcpm/tcpci.h" #include "test/drivers/test_state.h" +#include <zephyr/drivers/gpio.h> +#include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> + #define TCPCI_EMUL_NODE DT_NODELABEL(tcpci_emul) +/* Convenience pointer directly to the TCPCI mux under test */ +static struct usb_mux *tcpci_usb_mux; + /** Test TCPCI init and vbus level */ ZTEST(tcpci, test_generic_tcpci_init) { @@ -288,13 +290,13 @@ ZTEST(tcpci, test_generic_tcpci_debug_accessory) /* Setup TCPCI usb mux to behave as it is used only for usb mux */ static void set_usb_mux_not_tcpc(void) { - usbc0_mux0.flags = USB_MUX_FLAG_NOT_TCPC; + tcpci_usb_mux->flags = USB_MUX_FLAG_NOT_TCPC; } /* Setup TCPCI usb mux to behave as it is used for usb mux and TCPC */ static void set_usb_mux_tcpc(void) { - usbc0_mux0.flags = 0; + tcpci_usb_mux->flags = 0; } /** Test TCPCI mux init */ @@ -531,7 +533,6 @@ void validate_mux_read_write16(const struct usb_mux *tcpci_usb_mux) /** Test usb_mux read/write APIs */ ZTEST(tcpci, test_usb_mux_read_write) { - struct usb_mux *tcpci_usb_mux = &usbc0_mux0; const int flags_restore = tcpci_usb_mux->flags; /* Configure mux read/writes for TCPC APIs */ @@ -552,6 +553,8 @@ static void *tcpci_setup(void) &tcpci_tcpm_usb_mux_driver, "Invalid config of usb_muxes in test/drivers/src/stubs.c"); + tcpci_usb_mux = (struct usb_mux *)usb_muxes[USBC_PORT_C0].mux; + return NULL; } diff --git a/zephyr/test/drivers/default/src/tcpci_test_common.c b/zephyr/test/drivers/default/src/tcpci_test_common.c index bbc1e3f8b0..06f03d4834 100644 --- a/zephyr/test/drivers/default/src/tcpci_test_common.c +++ b/zephyr/test/drivers/default/src/tcpci_test_common.c @@ -3,15 +3,14 @@ * found in the LICENSE file. */ -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> - #include "common.h" #include "emul/emul_common_i2c.h" #include "emul/tcpc/emul_tcpci.h" +#include "tcpm/tcpci.h" #include "test/drivers/tcpci_test_common.h" -#include "tcpm/tcpci.h" +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> /** Check TCPC register value */ void check_tcpci_reg_f(const struct emul *emul, int reg, uint16_t exp_val, @@ -1020,7 +1019,7 @@ void test_tcpci_hard_reset_reinit(const struct emul *emul, uint16_t power_status_mask; uint16_t alert_mask; - zassume_equal(EC_SUCCESS, drv->init(port), NULL); + zassert_equal(EC_SUCCESS, drv->init(port), NULL); tcpci_emul_get_reg(emul, TCPC_REG_POWER_STATUS_MASK, &power_status_mask); tcpci_emul_get_reg(emul, TCPC_REG_ALERT_MASK, &alert_mask); diff --git a/zephyr/test/drivers/default/src/tcpm_header.c b/zephyr/test/drivers/default/src/tcpm_header.c new file mode 100644 index 0000000000..e03e09aaa5 --- /dev/null +++ b/zephyr/test/drivers/default/src/tcpm_header.c @@ -0,0 +1,235 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "tcpm/tcpm.h" +#include "test/drivers/stubs.h" +#include "test/drivers/test_state.h" + +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> + +#define TCPM_TEST_PORT USBC_PORT_C0 + +FAKE_VALUE_FUNC(int, set_vconn, int, int); +FAKE_VALUE_FUNC(int, reset_bist_type_2, int); +FAKE_VALUE_FUNC(int, debug_accessory, int, bool); +FAKE_VALUE_FUNC(int, debug_detach, int); +FAKE_VALUE_FUNC(int, hard_reset_reinit, int); +FAKE_VALUE_FUNC(int, set_frs_enable, int, int); +FAKE_VOID_FUNC(tcpc_dump_std_registers, int); + +struct tcpm_header_fixture { + /* The original driver pointer that gets restored after the tests */ + const struct tcpm_drv *saved_driver_ptr; + /* Mock driver that gets substituted */ + struct tcpm_drv mock_driver; + /* Saved tcpc config flags that get restored after the tests */ + uint32_t saved_tcpc_flags; +}; + +ZTEST_F(tcpm_header, test_tcpm_header_drv_set_vconn_failure) +{ + int res; + + tcpc_config[TCPM_TEST_PORT].flags = TCPC_FLAGS_CONTROL_VCONN; + + fixture->mock_driver.set_vconn = set_vconn; + set_vconn_fake.return_val = -1; + + res = tcpm_set_vconn(TCPM_TEST_PORT, true); + + zassert_true(set_vconn_fake.call_count > 0); + zassert_equal(-1, res); +} + +ZTEST_F(tcpm_header, test_tcpm_header_reset_bist_type_2__unimplemented) +{ + zassert_ok(tcpm_reset_bist_type_2(TCPM_TEST_PORT)); +} + +ZTEST_F(tcpm_header, test_tcpm_header_reset_bist_type_2__implemented) +{ + int res; + const int driver_return_code = 7458; /* arbitrary */ + + fixture->mock_driver.reset_bist_type_2 = reset_bist_type_2; + reset_bist_type_2_fake.return_val = driver_return_code; + res = tcpm_reset_bist_type_2(TCPM_TEST_PORT); + + zassert_equal(1, reset_bist_type_2_fake.call_count); + zassert_equal(TCPM_TEST_PORT, reset_bist_type_2_fake.arg0_history[0]); + zassert_equal(driver_return_code, res); +} + +ZTEST_F(tcpm_header, test_tcpm_header_debug_accessory__unimplemented) +{ + zassert_ok(tcpm_debug_accessory(TCPM_TEST_PORT, true)); + zassert_ok(tcpm_debug_accessory(TCPM_TEST_PORT, false)); +} + +ZTEST_F(tcpm_header, test_tcpm_header_debug_accessory__implemented) +{ + int res; + const int driver_return_code = 7458; /* arbitrary */ + + fixture->mock_driver.debug_accessory = debug_accessory; + debug_accessory_fake.return_val = driver_return_code; + res = tcpm_debug_accessory(TCPM_TEST_PORT, true); + + zassert_equal(1, debug_accessory_fake.call_count); + zassert_equal(TCPM_TEST_PORT, debug_accessory_fake.arg0_history[0]); + zassert_true(debug_accessory_fake.arg1_history[0]); + zassert_equal(driver_return_code, res); +} + +ZTEST_F(tcpm_header, test_tcpm_header_debug_detach__unimplemented) +{ + zassert_ok(tcpm_debug_detach(TCPM_TEST_PORT)); +} + +ZTEST_F(tcpm_header, test_tcpm_header_debug_detach__implemented) +{ + int res; + const int driver_return_code = 7458; /* arbitrary */ + + fixture->mock_driver.debug_detach = debug_detach; + debug_detach_fake.return_val = driver_return_code; + res = tcpm_debug_detach(TCPM_TEST_PORT); + + zassert_equal(1, debug_detach_fake.call_count); + zassert_equal(TCPM_TEST_PORT, debug_detach_fake.arg0_history[0]); + zassert_equal(driver_return_code, res); +} + +ZTEST_F(tcpm_header, test_tcpm_header_hard_reset_reinit__unimplemented) +{ + int res; + + res = tcpm_hard_reset_reinit(TCPM_TEST_PORT); + + zassert_equal(EC_ERROR_UNIMPLEMENTED, res); +} + +ZTEST_F(tcpm_header, test_tcpm_header_hard_reset_reinit__implemented) +{ + int res; + const int driver_return_code = 7458; /* arbitrary */ + + fixture->mock_driver.hard_reset_reinit = hard_reset_reinit; + hard_reset_reinit_fake.return_val = driver_return_code; + res = tcpm_hard_reset_reinit(TCPM_TEST_PORT); + + zassert_equal(1, hard_reset_reinit_fake.call_count); + zassert_equal(TCPM_TEST_PORT, hard_reset_reinit_fake.arg0_history[0]); + zassert_equal(driver_return_code, res); +} + +ZTEST_F(tcpm_header, test_tcpm_header_tcpc_has_frs_control__flag) +{ + Z_TEST_SKIP_IFNDEF(CONFIG_PLATFORM_EC_USB_PD_FRS); + Z_TEST_SKIP_IFDEF(CONFIG_PLATFORM_EC_USB_PD_FRS_TCPC); + + /* Determined by tcpc flag when USB_PD_FRS_TCPC is not set. */ + + tcpc_config[TCPM_TEST_PORT].flags = 0; + zassert_equal(0, tcpm_tcpc_has_frs_control(TCPM_TEST_PORT)); + + tcpc_config[TCPM_TEST_PORT].flags = TCPC_FLAGS_CONTROL_FRS; + zassert_equal(1, tcpm_tcpc_has_frs_control(TCPM_TEST_PORT)); +} + +ZTEST_F(tcpm_header, test_tcpm_header_set_frs_enable__unimplemented) +{ + Z_TEST_SKIP_IFNDEF(CONFIG_PLATFORM_EC_USB_PD_FRS); + + zassert_ok(tcpm_set_frs_enable(TCPM_TEST_PORT, 1)); + zassert_ok(tcpm_set_frs_enable(TCPM_TEST_PORT, 0)); +} + +ZTEST_F(tcpm_header, test_tcpm_header_set_frs_enable__implemented) +{ + int res; + const int driver_return_code = 7458; /* arbitrary */ + + Z_TEST_SKIP_IFNDEF(CONFIG_PLATFORM_EC_USB_PD_FRS); + + fixture->mock_driver.set_frs_enable = set_frs_enable; + set_frs_enable_fake.return_val = driver_return_code; + res = tcpm_set_frs_enable(TCPM_TEST_PORT, 1); + + zassert_equal(1, set_frs_enable_fake.call_count); + zassert_equal(TCPM_TEST_PORT, set_frs_enable_fake.arg0_history[0]); + zassert_equal(1, set_frs_enable_fake.arg1_history[0]); + zassert_equal(driver_return_code, res); +} + +ZTEST_F(tcpm_header, test_tcpm_header_tcpc_get_bist_test_mode__unimplemented) +{ + int res; + bool enabled = true; /* Should be overwritten to false */ + + res = tcpc_get_bist_test_mode(TCPM_TEST_PORT, &enabled); + + zassert_equal(EC_ERROR_UNIMPLEMENTED, res); + zassert_false(enabled); +} + +ZTEST_F(tcpm_header, test_tcpm_header_get_chip_info__unimplemented) +{ + zassert_equal(EC_ERROR_UNIMPLEMENTED, + tcpm_get_chip_info(TCPM_TEST_PORT, 0, NULL)); +} + +ZTEST_F(tcpm_header, test_tcpm_header_dump_registers__std) +{ + Z_TEST_SKIP_IFNDEF(CONFIG_PLATFORM_EC_CONSOLE_CMD_TCPC_DUMP); + + /* + * The driver does not implement dump_registers, so the + * standard ones should be dumped instead. + */ + tcpm_dump_registers(TCPM_TEST_PORT); + + zassert_equal(1, tcpc_dump_std_registers_fake.call_count); + zassert_equal(TCPM_TEST_PORT, + tcpc_dump_std_registers_fake.arg0_history[0]); +} + +static void *tcpm_header_setup(void) +{ + static struct tcpm_header_fixture fixture; + + return &fixture; +} + +static void tcpm_header_before(void *state) +{ + struct tcpm_header_fixture *fixture = state; + + RESET_FAKE(set_vconn); + RESET_FAKE(reset_bist_type_2); + RESET_FAKE(debug_accessory); + RESET_FAKE(debug_detach); + RESET_FAKE(hard_reset_reinit); + RESET_FAKE(set_frs_enable); + RESET_FAKE(tcpc_dump_std_registers); + + fixture->mock_driver = (struct tcpm_drv){ 0 }; + fixture->saved_driver_ptr = tcpc_config[TCPM_TEST_PORT].drv; + tcpc_config[TCPM_TEST_PORT].drv = &fixture->mock_driver; + + fixture->saved_tcpc_flags = tcpc_config[TCPM_TEST_PORT].flags; +} + +static void tcpm_header_after(void *state) +{ + struct tcpm_header_fixture *fixture = state; + + tcpc_config[TCPM_TEST_PORT].drv = fixture->saved_driver_ptr; + tcpc_config[TCPM_TEST_PORT].flags = fixture->saved_tcpc_flags; +} + +ZTEST_SUITE(tcpm_header, drivers_predicate_pre_main, tcpm_header_setup, + tcpm_header_before, tcpm_header_after, NULL); diff --git a/zephyr/test/drivers/default/src/tcs3400.c b/zephyr/test/drivers/default/src/tcs3400.c index 045451169e..dba8dc5c56 100644 --- a/zephyr/test/drivers/default/src/tcs3400.c +++ b/zephyr/test/drivers/default/src/tcs3400.c @@ -3,19 +3,18 @@ * found in the LICENSE file. */ -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> - #include "common.h" -#include "i2c.h" -#include "emul/emul_tcs3400.h" +#include "driver/als_tcs3400.h" #include "emul/emul_common_i2c.h" - +#include "emul/emul_tcs3400.h" +#include "i2c.h" #include "motion_sense.h" #include "motion_sense_fifo.h" -#include "driver/als_tcs3400.h" #include "test/drivers/test_state.h" +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> + #define TCS_NODE DT_NODELABEL(tcs_emul) #define TCS_CLR_SENSOR_ID SENSOR_ID(DT_NODELABEL(tcs3400_clear)) #define TCS_RGB_SENSOR_ID SENSOR_ID(DT_NODELABEL(tcs3400_rgb)) diff --git a/zephyr/test/drivers/default/src/temp_sensor.c b/zephyr/test/drivers/default/src/temp_sensor.c index 61441a0229..83d9aa3812 100644 --- a/zephyr/test/drivers/default/src/temp_sensor.c +++ b/zephyr/test/drivers/default/src/temp_sensor.c @@ -3,19 +3,24 @@ * found in the LICENSE file. */ -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> -#include <zephyr/drivers/adc.h> -#include <zephyr/drivers/adc/adc_emul.h> -#include <zephyr/drivers/gpio.h> -#include <zephyr/drivers/gpio/gpio_emul.h> - -#include <math.h> - #include "common.h" +#include "driver/temp_sensor/pct2075.h" +#include "emul/emul_pct2075.h" +#include "math_util.h" #include "temp_sensor.h" #include "temp_sensor/temp_sensor.h" #include "test/drivers/test_state.h" +#include "timer.h" + +#include <math.h> + +#include <zephyr/drivers/adc.h> +#include <zephyr/drivers/adc/adc_emul.h> +#include <zephyr/drivers/emul.h> +#include <zephyr/drivers/gpio.h> +#include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> #define GPIO_PG_EC_DSW_PWROK_PATH DT_PATH(named_gpios, pg_ec_dsw_pwrok) #define GPIO_PG_EC_DSW_PWROK_PORT DT_GPIO_PIN(GPIO_PG_EC_DSW_PWROK_PATH, gpios) @@ -110,6 +115,11 @@ ZTEST_USER(temp_sensor, test_temp_sensor_pg_pin) named_temp_pp3300_regulator)), &temp), NULL); + zassert_equal( + EC_ERROR_NOT_POWERED, + temp_sensor_read(TEMP_SENSOR_ID(DT_NODELABEL(named_pct2075)), + &temp), + NULL); /* power ADC */ zassert_ok(gpio_emul_input_set(gpio_dev, GPIO_EC_PG_PIN_TEMP_PORT, 1), @@ -179,12 +189,92 @@ ZTEST_USER(temp_sensor, test_temp_sensor_read) } } +/** Test if temp_sensor_read() returns temperature on success for PCT2075 */ +ZTEST_USER(temp_sensor, test_temp_sensor_pct2075) +{ + int temp; + const struct emul *dev = EMUL_DT_GET(DT_NODELABEL(pct2075_emul)); + int mk[] = { + MILLI_CELSIUS_TO_MILLI_KELVIN(127000), + MILLI_CELSIUS_TO_MILLI_KELVIN(126850), + MILLI_CELSIUS_TO_MILLI_KELVIN(125), + MILLI_CELSIUS_TO_MILLI_KELVIN(0), + MILLI_CELSIUS_TO_MILLI_KELVIN(-125), + MILLI_CELSIUS_TO_MILLI_KELVIN(-54875), + MILLI_CELSIUS_TO_MILLI_KELVIN(-55000), + }; + + for (int i = 0; i < ARRAY_SIZE(mk); i++) { + pct2075_emul_set_temp(dev, mk[i]); + /* Highly dependent on current implementation. The sensor + * update temperature in the 1 second periodic hook, so + * we need to wait for it. + */ + msleep(1100); + zassert_equal(EC_SUCCESS, + temp_sensor_read(TEMP_SENSOR_ID(DT_NODELABEL( + named_pct2075)), + &temp)); + zassert_equal(MILLI_KELVIN_TO_KELVIN(mk[i]), temp); + } +} + +/** Test if temperature is not updated on I2C read fail. + * The test highly dependent on current implementation - temp_sensor_read + * doesn't return an error on the i2c read fail, which can/should be changed + * in the future. + */ +ZTEST_USER(temp_sensor, test_temp_sensor_pct2075_fail) +{ + const struct emul *dev = EMUL_DT_GET(DT_NODELABEL(pct2075_emul)); + struct pct2075_data *data = (struct pct2075_data *)dev->data; + int mk1 = 373000, mk2 = 273000; + int temp; + + /* Set initial temperature */ + pct2075_emul_set_temp(dev, mk1); + msleep(1100); + + zassert_equal(EC_SUCCESS, temp_sensor_read(TEMP_SENSOR_ID(DT_NODELABEL( + named_pct2075)), + &temp)); + /* Make sure the temperature is read correctly */ + zassert_equal(MILLI_KELVIN_TO_KELVIN(mk1), temp); + + /* Set I2C fail on the temperature register */ + i2c_common_emul_set_read_fail_reg(&data->common, PCT2075_REG_TEMP); + pct2075_emul_set_temp(dev, mk2); + /* Wait for potential update */ + msleep(1100); + + /* Make sure the temperature is not changed */ + zassert_equal(EC_SUCCESS, temp_sensor_read(TEMP_SENSOR_ID(DT_NODELABEL( + named_pct2075)), + &temp)); + zassert_equal(MILLI_KELVIN_TO_KELVIN(mk1), temp); + + /* Restore I2C */ + i2c_common_emul_set_read_fail_reg(&data->common, + I2C_COMMON_EMUL_NO_FAIL_REG); + /* Wait for update */ + msleep(1100); + /* Make sure the temperature is updated */ + zassert_equal(EC_SUCCESS, temp_sensor_read(TEMP_SENSOR_ID(DT_NODELABEL( + named_pct2075)), + &temp)); + zassert_equal(MILLI_KELVIN_TO_KELVIN(mk2), temp); +} + static void *temp_sensor_setup(void) { const struct device *dev = DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_PG_EC_DSW_PWROK_PATH, gpios)); const struct device *dev_pin = DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_EC_PG_PIN_TEMP_PATH, gpios)); + const struct emul *pct2075_dev = + EMUL_DT_GET(DT_NODELABEL(pct2075_emul)); + struct pct2075_data *pct2075_data = + (struct pct2075_data *)pct2075_dev->data; zassert_not_null(dev, NULL); /* Before tests make sure that power pins are set. */ @@ -193,6 +283,9 @@ static void *temp_sensor_setup(void) zassert_ok(gpio_emul_input_set(dev_pin, GPIO_EC_PG_PIN_TEMP_PORT, 1), NULL); + i2c_common_emul_set_read_fail_reg(&pct2075_data->common, + I2C_COMMON_EMUL_NO_FAIL_REG); + return NULL; } diff --git a/zephyr/test/drivers/default/src/thermistor.c b/zephyr/test/drivers/default/src/thermistor.c index 417b482d99..edbe7acad5 100644 --- a/zephyr/test/drivers/default/src/thermistor.c +++ b/zephyr/test/drivers/default/src/thermistor.c @@ -3,18 +3,19 @@ * found in the LICENSE file. */ -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> +#include "../driver/temp_sensor/thermistor.h" +#include "common.h" +#include "temp_sensor/temp_sensor.h" +#include "test/drivers/test_state.h" + #include <zephyr/drivers/adc.h> #include <zephyr/drivers/adc/adc_emul.h> #include <zephyr/drivers/gpio.h> #include <zephyr/drivers/gpio/gpio_emul.h> -#include <temp_sensor.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> -#include "common.h" -#include "../driver/temp_sensor/thermistor.h" -#include "temp_sensor/temp_sensor.h" -#include "test/drivers/test_state.h" +#include <temp_sensor.h> #define GPIO_PG_EC_DSW_PWROK_PATH DT_PATH(named_gpios, pg_ec_dsw_pwrok) #define GPIO_PG_EC_DSW_PWROK_PORT DT_GPIO_PIN(GPIO_PG_EC_DSW_PWROK_PATH, gpios) @@ -58,6 +59,10 @@ ZTEST_USER(thermistor, test_thermistor_power_pin) sensor_idx++) { const struct temp_sensor_t *sensor = &temp_sensors[sensor_idx]; + /* Skip for sensors that are not thermistors */ + if (sensor->zephyr_info->thermistor == NULL) + continue; + zassert_ok(adc_emul_const_value_set(adc_dev, sensor->idx, A_VALID_VOLTAGE), "adc_emul_value_func_set() failed on %s", @@ -72,6 +77,10 @@ ZTEST_USER(thermistor, test_thermistor_power_pin) sensor_idx++) { const struct temp_sensor_t *sensor = &temp_sensors[sensor_idx]; + /* Skip for sensors that are not thermistors */ + if (sensor->zephyr_info->thermistor == NULL) + continue; + zassert_equal(EC_ERROR_NOT_POWERED, sensor->zephyr_info->read(sensor, &temp), "%s failed", sensor->name); @@ -85,6 +94,10 @@ ZTEST_USER(thermistor, test_thermistor_power_pin) sensor_idx++) { const struct temp_sensor_t *sensor = &temp_sensors[sensor_idx]; + /* Skip for sensors that are not thermistors */ + if (sensor->zephyr_info->thermistor == NULL) + continue; + zassert_equal(EC_SUCCESS, sensor->zephyr_info->read(sensor, &temp), "%s failed", sensor->name); @@ -113,6 +126,10 @@ ZTEST_USER(thermistor, test_thermistor_adc_read_error) sensor_idx++) { const struct temp_sensor_t *sensor = &temp_sensors[sensor_idx]; + /* Skip for sensors that are not thermistors */ + if (sensor->zephyr_info->thermistor == NULL) + continue; + zassert_ok(adc_emul_value_func_set(adc_dev, sensor->idx, adc_error_func, NULL), "adc_emul_value_func_set() failed on %s", @@ -123,6 +140,10 @@ ZTEST_USER(thermistor, test_thermistor_adc_read_error) sensor_idx++) { const struct temp_sensor_t *sensor = &temp_sensors[sensor_idx]; + /* Skip for sensors that are not thermistors */ + if (sensor->zephyr_info->thermistor == NULL) + continue; + zassert_equal(EC_ERROR_UNKNOWN, sensor->zephyr_info->read(sensor, &temp), "%s failed", sensor->name); @@ -265,10 +286,16 @@ ZTEST_USER(thermistor, test_thermistors_adc_temperature_conversion) const static int reference_res_arr[] = { DT_FOREACH_STATUS_OKAY( THERMISTOR_COMPAT, GET_THERMISTOR_REF_RES) }; - for (sensor_idx = 0; sensor_idx < NAMED_TEMP_SENSORS_SIZE; sensor_idx++) + for (sensor_idx = 0; sensor_idx < NAMED_TEMP_SENSORS_SIZE; + sensor_idx++) { + /* Skip for sensors that are not thermistors */ + if (temp_sensors[sensor_idx].zephyr_info->thermistor == NULL) + continue; + do_thermistor_test(&temp_sensors[sensor_idx], reference_mv_arr[sensor_idx], reference_res_arr[sensor_idx]); + } } ZTEST_USER(thermistor, test_device_nodes_enabled) @@ -312,6 +339,10 @@ static void thermistor_cleanup(void *state) for (sensor_idx = 0; sensor_idx < NAMED_TEMP_SENSORS_SIZE; sensor_idx++) { + /* Skip for sensors that are not thermistors */ + if (temp_sensors[sensor_idx].zephyr_info->thermistor == NULL) + continue; + /* Setup ADC to return 27*C (300K) which is reasonable value */ adc_emul_const_value_set( adc_dev, temp_sensors[sensor_idx].idx, diff --git a/zephyr/test/drivers/default/src/uart_hostcmd.c b/zephyr/test/drivers/default/src/uart_hostcmd.c index 5e1e200fa9..907dba929f 100644 --- a/zephyr/test/drivers/default/src/uart_hostcmd.c +++ b/zephyr/test/drivers/default/src/uart_hostcmd.c @@ -3,13 +3,13 @@ * found in the LICENSE file. */ -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> - #include "console.h" #include "host_command.h" -#include "uart.h" #include "test/drivers/test_state.h" +#include "uart.h" + +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> /** Messages used in test */ static const char msg1[] = "test"; diff --git a/zephyr/test/drivers/default/src/usb_mux.c b/zephyr/test/drivers/default/src/usb_mux.c index 62d39e28d4..0e89cf2398 100644 --- a/zephyr/test/drivers/default/src/usb_mux.c +++ b/zephyr/test/drivers/default/src/usb_mux.c @@ -3,33 +3,33 @@ * found in the LICENSE file. */ -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> -#include <zephyr/drivers/gpio.h> -#include <zephyr/drivers/gpio/gpio_emul.h> -#include <zephyr/shell/shell.h> -#include <zephyr/shell/shell_uart.h> - #include "common.h" #include "ec_commands.h" #include "ec_tasks.h" -#include <zephyr/fff.h> #include "hooks.h" #include "host_command.h" #include "i2c.h" -#include "test/drivers/stubs.h" #include "task.h" #include "tcpm/ps8xxx_public.h" #include "tcpm/tcpci.h" +#include "test/drivers/stubs.h" +#include "test/drivers/test_state.h" +#include "test/drivers/utils.h" +#include "usb_mux.h" #include "usb_prl_sm.h" #include "usb_tc_sm.h" -#include "usb_mux.h" -#include "test/drivers/test_state.h" -#include "test/drivers/utils.h" +#include <zephyr/drivers/gpio.h> +#include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/fff.h> +#include <zephyr/kernel.h> +#include <zephyr/shell/shell.h> +#include <zephyr/shell/shell_uart.h> +#include <zephyr/ztest.h> /** Copy of original usb_muxes[USB_PORT_C1] */ -struct usb_mux_chain usb_mux_c1; +static struct usb_mux_chain usb_mux_c1; +static struct usb_mux *usbc1_virtual_usb_mux; /** Number of usb mux proxies in chain */ #define NUM_OF_PROXY 3 @@ -344,6 +344,26 @@ struct usb_mux_chain proxy_chain_0 = { .next = &proxy_chain_1, }; +static void find_virtual_mux(void) +{ + const struct usb_mux_chain *mux_chain; + + mux_chain = &usb_muxes[1]; + usbc1_virtual_usb_mux = NULL; + while (mux_chain) { + if (mux_chain->mux && + mux_chain->mux->driver == &virtual_usb_mux_driver) { + usbc1_virtual_usb_mux = + (struct usb_mux *)mux_chain->mux; + break; + } + mux_chain = mux_chain->next; + } + + __ASSERT(usbc1_virtual_usb_mux, + "USB-C port 1 must contain a virtual mux"); +} + /** Setup first 3 usb muxes of port 1 with proxy */ static void setup_usb_mux_proxy_chain(void) { @@ -693,13 +713,13 @@ ZTEST(usb_uninit_mux, test_usb_mux_hpd_update) mux_state_t exp_mode, mode, virt_mode; /* Get current state of virtual usb mux and set mock */ - usbc1_virtual_usb_mux.driver->get(&usbc1_virtual_usb_mux, &virt_mode); + usbc1_virtual_usb_mux->driver->get(usbc1_virtual_usb_mux, &virt_mode); /* Test no hpd level and no irq */ exp_mode = virt_mode; usb_mux_hpd_update(USBC_PORT_C1, exp_mode); /* Check if virtual usb mux mode is updated correctly */ - usbc1_virtual_usb_mux.driver->get(&usbc1_virtual_usb_mux, &mode); + usbc1_virtual_usb_mux->driver->get(usbc1_virtual_usb_mux, &mode); zassert_equal(exp_mode, mode, "virtual mux mode is 0x%x (!= 0x%x)", mode, exp_mode); CHECK_PROXY_FAKE_CALL_CNT(proxy_init, NUM_OF_PROXY); @@ -711,7 +731,7 @@ ZTEST(usb_uninit_mux, test_usb_mux_hpd_update) exp_mode = virt_mode | USB_PD_MUX_HPD_LVL | USB_PD_MUX_HPD_IRQ; usb_mux_hpd_update(USBC_PORT_C1, exp_mode); /* Check if virtual usb mux mode is updated correctly */ - usbc1_virtual_usb_mux.driver->get(&usbc1_virtual_usb_mux, &mode); + usbc1_virtual_usb_mux->driver->get(usbc1_virtual_usb_mux, &mode); zassert_equal(exp_mode, mode, "virtual mux mode is 0x%x (!= 0x%x)", mode, exp_mode); CHECK_PROXY_FAKE_CALL_CNT(proxy_init, 0); @@ -723,7 +743,7 @@ ZTEST(usb_uninit_mux, test_usb_mux_hpd_update) exp_mode = virt_mode | USB_PD_MUX_HPD_IRQ; usb_mux_hpd_update(USBC_PORT_C1, exp_mode); /* Check if virtual usb mux mode is updated correctly */ - usbc1_virtual_usb_mux.driver->get(&usbc1_virtual_usb_mux, &mode); + usbc1_virtual_usb_mux->driver->get(usbc1_virtual_usb_mux, &mode); zassert_equal(exp_mode, mode, "virtual mux mode is 0x%x (!= 0x%x)", mode, exp_mode); CHECK_PROXY_FAKE_CALL_CNT(proxy_init, 0); @@ -735,7 +755,7 @@ ZTEST(usb_uninit_mux, test_usb_mux_hpd_update) exp_mode = virt_mode | USB_PD_MUX_HPD_LVL; usb_mux_hpd_update(USBC_PORT_C1, exp_mode); /* Check if virtual usb mux mode is updated correctly */ - usbc1_virtual_usb_mux.driver->get(&usbc1_virtual_usb_mux, &mode); + usbc1_virtual_usb_mux->driver->get(usbc1_virtual_usb_mux, &mode); zassert_equal(exp_mode, mode, "virtual mux mode is 0x%x (!= 0x%x)", mode, exp_mode); CHECK_PROXY_FAKE_CALL_CNT(proxy_init, 0); @@ -916,6 +936,7 @@ ZTEST(usb_init_mux, test_usb_mux_typec_command) void usb_uninit_mux_before(void *state) { ARG_UNUSED(state); + find_virtual_mux(); setup_usb_mux_proxy_chain(); set_test_runner_tid(); @@ -935,6 +956,7 @@ void usb_uninit_mux_after(void *state) void usb_init_mux_before(void *state) { ARG_UNUSED(state); + find_virtual_mux(); setup_usb_mux_proxy_chain(); set_test_runner_tid(); diff --git a/zephyr/test/drivers/default/src/util.c b/zephyr/test/drivers/default/src/util.c index 32f989bb0f..342ce9b971 100644 --- a/zephyr/test/drivers/default/src/util.c +++ b/zephyr/test/drivers/default/src/util.c @@ -2,11 +2,11 @@ * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ -#include <zephyr/ztest.h> -#include <zephyr/fff.h> - -#include "util.h" #include "test/drivers/test_state.h" +#include "util.h" + +#include <zephyr/fff.h> +#include <zephyr/ztest.h> ZTEST(util, reverse) { diff --git a/zephyr/test/drivers/default/src/vboot_hash.c b/zephyr/test/drivers/default/src/vboot_hash.c index 04f535a89c..61d4260a23 100644 --- a/zephyr/test/drivers/default/src/vboot_hash.c +++ b/zephyr/test/drivers/default/src/vboot_hash.c @@ -3,14 +3,15 @@ * found in the LICENSE file. */ -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> -#include <sha256.h> - #include "ec_commands.h" #include "host_command.h" #include "test/drivers/test_state.h" +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> + +#include <sha256.h> + ZTEST_USER(vboot_hash, test_hostcmd_abort) { struct ec_response_vboot_hash response; diff --git a/zephyr/test/drivers/default/src/virtual_battery.c b/zephyr/test/drivers/default/src/virtual_battery.c index fc29e39777..3b265443e8 100644 --- a/zephyr/test/drivers/default/src/virtual_battery.c +++ b/zephyr/test/drivers/default/src/virtual_battery.c @@ -3,15 +3,19 @@ * found in the LICENSE file. */ -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> - #include "battery.h" #include "battery_smart.h" #include "ec_commands.h" #include "emul/emul_smart_battery.h" +#include "gpio.h" #include "host_command.h" #include "test/drivers/test_state.h" +#include "virtual_battery.h" + +#include <zephyr/drivers/emul.h> +#include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/kernel.h> +#include <zephyr/ztest.h> void copy_memmap_string(uint8_t *dest, int offset, int len); @@ -226,11 +230,11 @@ ZTEST_USER(virtual_battery, test_read_regs) word = virtual_battery_read16(SB_SPECIFICATION_INFO); zassert_equal(expected, word, "%d != %d", expected, word); - zassume_ok(battery_status(&expected)); + zassert_ok(battery_status(&expected)); word = virtual_battery_read16(SB_BATTERY_STATUS); zassert_equal(expected, word, "%d != %d", expected, word); - zassume_ok(battery_design_voltage(&expected)); + zassert_ok(battery_design_voltage(&expected)); word = virtual_battery_read16(SB_DESIGN_VOLTAGE); zassert_equal(expected, word, "%d != %d", expected, word); @@ -285,3 +289,144 @@ ZTEST(virtual_battery, test_read_data_from_host_memmap) ZTEST_SUITE(virtual_battery, drivers_predicate_post_main, NULL, NULL, NULL, NULL); + +ZTEST(virtual_battery_direct, test_bad_reg_write) +{ + struct ec_response_i2c_passthru resp; + + /* Start with a zero-length write. The state machine is expecting a + * register address to be written, so this will fail. + */ + zassert_equal(EC_ERROR_INVAL, + virtual_battery_handler(&resp, 0, NULL, 0, 0, + /* write_len = */ 0, NULL)); + + zassert_equal(EC_I2C_STATUS_NAK, resp.i2c_status); +} + +ZTEST(virtual_battery_direct, test_aborted_write) +{ + struct ec_response_i2c_passthru resp; + int error_code; + + /* Arbitrary packet of bytes */ + const uint8_t packet[] = { 0xAA, 0xBB, 0xCC }; + + /* Start with a length 1 write to set a register address. */ + zassert_ok(virtual_battery_handler(&resp, 0, &error_code, 0, 0, + /* write_len = */ 1, &packet[0])); + + /* Now write two more bytes successfully... */ + zassert_ok(virtual_battery_handler(&resp, 0, &error_code, 0, 0, + /* write_len = */ 1, &packet[1])); + zassert_ok(error_code); + + zassert_ok(virtual_battery_handler(&resp, 0, &error_code, 0, 0, + /* write_len = */ 1, &packet[2])); + zassert_ok(error_code); + + /* ...and abruptly write 0 bytes. This will cause an error */ + zassert_equal(EC_ERROR_INVAL, + virtual_battery_handler(&resp, 0, &error_code, 0, 0, + /* write_len = */ 0, NULL)); + + zassert_equal(EC_I2C_STATUS_NAK, resp.i2c_status); +} + +ZTEST(virtual_battery_direct, test_aborted_read) +{ + struct ec_response_i2c_passthru resp; + int error_code; + + /* Arbitrary packet to set a register plus a buffer to read to */ + const uint8_t write_packet[] = { SB_MANUFACTURER_NAME }; + uint8_t read_packet[3] = { 0 }; + + /* Start with a length 1 write to set a register address. */ + zassert_ok(virtual_battery_handler(&resp, 0, &error_code, 0, 0, + /* write_len = */ 1, + &write_packet[0])); + + /* Now read two bytes successfully... */ + zassert_ok(virtual_battery_handler(&resp, 0, &error_code, 0, + /* read_len = */ 1, 0, + &read_packet[0])); + zassert_ok(error_code); + + zassert_ok(virtual_battery_handler(&resp, 0, &error_code, 0, + /* read_len = */ 1, 0, + &read_packet[1])); + zassert_ok(error_code); + + /* ...and abruptly read 0 bytes. This will cause an error */ + zassert_equal(EC_ERROR_INVAL, + virtual_battery_handler(&resp, 0, &error_code, 0, + /* read_len = */ 0, 0, + &read_packet[2])); + + zassert_equal(EC_I2C_STATUS_NAK, resp.i2c_status); +} + +ZTEST(virtual_battery_direct, test_read_bad_reg) +{ + struct ec_response_i2c_passthru resp; + int error_code; + + /* Try to read from an invalid register */ + const uint8_t write_packet[] = { 0xFF }; + uint8_t read_packet[3] = { 0 }; + + /* Start with a length 1 write to set a register address. */ + zassert_ok(virtual_battery_handler(&resp, 0, &error_code, 0, 0, + /* write_len = */ 1, + &write_packet[0])); + + /* Now try to read */ + zassert_equal(EC_ERROR_INVAL, + virtual_battery_handler(&resp, 0, &error_code, 0, + /* read_len = */ 1, 0, + &read_packet[0])); + zassert_equal(EC_ERROR_INVAL, error_code); +} + +#define GPIO_BATT_PRES_ODL_PATH DT_PATH(named_gpios, ec_batt_pres_odl) +#define GPIO_BATT_PRES_ODL_PORT DT_GPIO_PIN(GPIO_BATT_PRES_ODL_PATH, gpios) + +static int set_battery_present(bool batt_present) +{ + const struct device *batt_pres_dev = + DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_BATT_PRES_ODL_PATH, gpios)); + + return gpio_emul_input_set(batt_pres_dev, GPIO_BATT_PRES_ODL_PORT, + !batt_present); +} + +ZTEST(virtual_battery_direct, test_no_battery) +{ + struct ec_response_i2c_passthru resp; + + set_battery_present(false); + + /* Arbitrary packet of bytes */ + const uint8_t packet[] = { 0xAA, 0xBB, 0xCC }; + + /* Attempt a valid write operation, which will fail due to no battery */ + zassert_equal(EC_ERROR_INVAL, + virtual_battery_handler(&resp, 0, NULL, 0, 0, + /* write_len = */ 1, &packet[0])); + + zassert_equal(EC_I2C_STATUS_NAK, resp.i2c_status); +} + +static void virtual_battery_direct_reset(void *arg) +{ + reset_parse_state(); + + set_battery_present(true); +} + +/* The virtual_battery_direct suite tests the virtual battery handler directly + * without performing I2C ops. This makes it easier to test certain corner-cases + */ +ZTEST_SUITE(virtual_battery_direct, drivers_predicate_post_main, NULL, + virtual_battery_direct_reset, virtual_battery_direct_reset, NULL); diff --git a/zephyr/test/drivers/default/src/vstore.c b/zephyr/test/drivers/default/src/vstore.c index b4264aaeb3..923d14ff00 100644 --- a/zephyr/test/drivers/default/src/vstore.c +++ b/zephyr/test/drivers/default/src/vstore.c @@ -3,19 +3,20 @@ * found in the LICENSE file. */ +#include "ec_commands.h" +#include "host_command.h" +#include "system.h" +#include "system_fake.h" +#include "test/drivers/test_state.h" +#include "vstore.h" + #include <setjmp.h> -#include <console.h> #include <zephyr/fff.h> #include <zephyr/shell/shell_dummy.h> #include <zephyr/ztest.h> -#include "ec_commands.h" -#include "host_command.h" -#include "system.h" -#include "system_fake.h" -#include "vstore.h" -#include "test/drivers/test_state.h" +#include <console.h> ZTEST_SUITE(vstore, drivers_predicate_post_main, NULL, NULL, NULL, NULL); diff --git a/zephyr/test/drivers/default/src/watchdog.c b/zephyr/test/drivers/default/src/watchdog.c index 958aa3eaaa..a82800a6de 100644 --- a/zephyr/test/drivers/default/src/watchdog.c +++ b/zephyr/test/drivers/default/src/watchdog.c @@ -8,20 +8,19 @@ * @brief Unit Tests for watchdog. */ -#include <zephyr/device.h> -#include <zephyr/drivers/watchdog.h> - -#include <zephyr/logging/log.h> -#include <zephyr/kernel.h> -#include <zephyr/ztest.h> - #include "common.h" #include "ec_tasks.h" -#include <zephyr/fff.h> #include "hooks.h" #include "test/drivers/stubs.h" -#include "watchdog.h" #include "test/drivers/test_state.h" +#include "watchdog.h" + +#include <zephyr/device.h> +#include <zephyr/drivers/watchdog.h> +#include <zephyr/fff.h> +#include <zephyr/kernel.h> +#include <zephyr/logging/log.h> +#include <zephyr/ztest.h> #define wdt DEVICE_DT_GET(DT_CHOSEN(cros_ec_watchdog)) |