summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuval Peress <peress@google.com>2022-08-23 23:09:55 -0600
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-08-24 20:13:15 +0000
commit4c8ba5d95b22713dbd9f3367162c4d99eeb43ca3 (patch)
treecbed88587190638aa6f8aee61636eae744912cea
parent5540ef68843f9a7b5d1b80a89b30a2966bbd3724 (diff)
downloadchrome-ec-4c8ba5d95b22713dbd9f3367162c4d99eeb43ca3.tar.gz
test: verify battery cut off host command v0 works
Check both paths of the battery cut off host command (v0) by emulating an I2C failure when writing sb (address 0). BRANCH=none BUG=b:236075096 TEST=twister -s zephyr/test/drivers/drivers.default Signed-off-by: Yuval Peress <peress@google.com> Change-Id: Id50a8dc29f6f1c336287c1ae85244ef333ec623b Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3853763 Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com> Reviewed-by: Jeremy Bettis <jbettis@chromium.org>
-rw-r--r--zephyr/include/emul/emul_smart_battery.h2
-rw-r--r--zephyr/test/drivers/default/CMakeLists.txt1
-rw-r--r--zephyr/test/drivers/default/src/host_cmd/battery_cut_off.c70
3 files changed, 73 insertions, 0 deletions
diff --git a/zephyr/include/emul/emul_smart_battery.h b/zephyr/include/emul/emul_smart_battery.h
index 27fc8835eb..cde58dd8c8 100644
--- a/zephyr/include/emul/emul_smart_battery.h
+++ b/zephyr/include/emul/emul_smart_battery.h
@@ -17,6 +17,8 @@
#include <zephyr/drivers/i2c_emul.h>
#include <stdint.h>
+#include "emul/emul_common_i2c.h"
+
/**
* @brief Smart Battery emulator backend API
* @defgroup sbat_emul Smart Battery emulator
diff --git a/zephyr/test/drivers/default/CMakeLists.txt b/zephyr/test/drivers/default/CMakeLists.txt
index 1a16d4456c..5e917f6559 100644
--- a/zephyr/test/drivers/default/CMakeLists.txt
+++ b/zephyr/test/drivers/default/CMakeLists.txt
@@ -30,6 +30,7 @@ target_sources(app PRIVATE
src/cros_cbi.c
src/espi.c
src/gpio.c
+ src/host_cmd/battery_cut_off.c
src/host_cmd/get_pd_port_caps.c
src/host_cmd/host_event_commands.c
src/host_cmd/host_event_commands_deprecated.c
diff --git a/zephyr/test/drivers/default/src/host_cmd/battery_cut_off.c b/zephyr/test/drivers/default/src/host_cmd/battery_cut_off.c
new file mode 100644
index 0000000000..d6faeeccca
--- /dev/null
+++ b/zephyr/test/drivers/default/src/host_cmd/battery_cut_off.c
@@ -0,0 +1,70 @@
+/* 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 <zephyr/drivers/emul.h>
+#include <zephyr/ztest.h>
+
+#include "battery.h"
+#include "emul/emul_common_i2c.h"
+#include "emul/emul_smart_battery.h"
+#include "hooks.h"
+#include "host_command.h"
+#include "test/drivers/test_state.h"
+#include "test/drivers/utils.h"
+
+struct host_cmd_battery_cut_off_fixture {
+ const struct emul *emul;
+ struct i2c_common_emul_data *i2c_emul;
+};
+
+static void *host_cmd_battery_cut_off_setup(void)
+{
+ static struct host_cmd_battery_cut_off_fixture fixture = {
+ .emul = EMUL_DT_GET(DT_NODELABEL(battery)),
+ };
+
+ fixture.i2c_emul = emul_smart_battery_get_i2c_common_data(fixture.emul);
+
+ return &fixture;
+}
+
+static void host_cmd_battery_cut_off_after(void *f)
+{
+ struct host_cmd_battery_cut_off_fixture *fixture = f;
+
+ i2c_common_emul_set_write_fail_reg(fixture->i2c_emul,
+ I2C_COMMON_EMUL_NO_FAIL_REG);
+ set_ac_enabled(true);
+ hook_notify(HOOK_AC_CHANGE);
+ k_msleep(500);
+}
+
+ZTEST_SUITE(host_cmd_battery_cut_off, drivers_predicate_post_main,
+ host_cmd_battery_cut_off_setup, NULL,
+ host_cmd_battery_cut_off_after, NULL);
+
+ZTEST_USER_F(host_cmd_battery_cut_off, test_fail_sb_write)
+{
+ int rv;
+ struct host_cmd_handler_args args =
+ BUILD_HOST_COMMAND_SIMPLE(EC_CMD_BATTERY_CUT_OFF, UINT8_C(0));
+
+ /* Force a failure on the battery i2c write to 0x00 */
+ i2c_common_emul_set_write_fail_reg(fixture->i2c_emul, 0);
+
+ rv = host_command_process(&args);
+ zassert_equal(EC_RES_ERROR, rv, "Expected 0, but got %d", rv);
+}
+
+ZTEST_USER(host_cmd_battery_cut_off, test_cutoff_battery)
+{
+ int rv;
+ struct host_cmd_handler_args args =
+ BUILD_HOST_COMMAND_SIMPLE(EC_CMD_BATTERY_CUT_OFF, UINT8_C(0));
+
+ rv = host_command_process(&args);
+ zassert_equal(EC_RES_SUCCESS, rv, "Expected 0, but got %d", rv);
+ zassert_true(battery_is_cut_off(), NULL);
+}