summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Honscheid <honscheid@google.com>2022-09-12 19:02:33 -0600
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-09-16 03:27:39 +0000
commit055da16f465af00cb6e15cf74a0336860af5dcf8 (patch)
tree3cafa8948f2e1924c5e7ef180c7b59fdb0760d96
parent5ea7789ecc0a7ae60bfc2584324a0d22bd933002 (diff)
downloadchrome-ec-055da16f465af00cb6e15cf74a0336860af5dcf8.tar.gz
zephyr: tests: Test host command EC_CMD_MKBP_INFO (mkbp_info.c)
* Move a utility function in to a common source to share it between tests * Add a missing header to the KB emul header * Expose two functions to access button and switch capabilities * Reset KB emul after the keyboard scan tests since they left some keys pressed down. * Fix period in copyright headers because the presubmit hooks made me. * Test all of the branches of the EC_CMD_MKBP_INFO host command BRANCH=None BUG=b:236075259 TEST=./twister Signed-off-by: Tristan Honscheid <honscheid@google.com> Change-Id: I99110a5cff066bea366fce28d2a9a92811608c56 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3893389 Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com> Reviewed-by: Jeremy Bettis <jbettis@chromium.org>
-rw-r--r--common/mkbp_info.c6
-rw-r--r--include/mkbp_info.h7
-rw-r--r--zephyr/include/emul/emul_kb_raw.h4
-rw-r--r--zephyr/test/drivers/keyboard_scan/CMakeLists.txt6
-rw-r--r--zephyr/test/drivers/keyboard_scan/include/keyboard_test_utils.h19
-rw-r--r--zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c13
-rw-r--r--zephyr/test/drivers/keyboard_scan/src/keyboard_test_utils.c19
-rw-r--r--zephyr/test/drivers/keyboard_scan/src/mkbp_info.c232
8 files changed, 293 insertions, 13 deletions
diff --git a/common/mkbp_info.c b/common/mkbp_info.c
index 17443b22b0..c195eda888 100644
--- a/common/mkbp_info.c
+++ b/common/mkbp_info.c
@@ -1,4 +1,4 @@
-/* Copyright 2021 The ChromiumOS Authors
+/* 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.
*/
@@ -25,7 +25,7 @@ __overridable int mkbp_support_volume_buttons(void)
#endif
}
-static uint32_t get_supported_buttons(void)
+test_export_static uint32_t get_supported_buttons(void)
{
uint32_t val = 0;
@@ -44,7 +44,7 @@ static uint32_t get_supported_buttons(void)
return val;
}
-static uint32_t get_supported_switches(void)
+test_export_static uint32_t get_supported_switches(void)
{
uint32_t val = 0;
diff --git a/include/mkbp_info.h b/include/mkbp_info.h
index 2fbb639300..61378f9c22 100644
--- a/include/mkbp_info.h
+++ b/include/mkbp_info.h
@@ -1,4 +1,4 @@
-/* Copyright 2022 The ChromiumOS Authors
+/* 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.
*/
@@ -19,4 +19,9 @@
*/
__override_proto int mkbp_support_volume_buttons(void);
+#ifdef TEST_BUILD
+uint32_t get_supported_buttons(void);
+uint32_t get_supported_switches(void);
+#endif /* TEST_BUILD */
+
#endif /* __CROS_EC_MKBP_INFO_H */
diff --git a/zephyr/include/emul/emul_kb_raw.h b/zephyr/include/emul/emul_kb_raw.h
index db012d7db6..1660ccefd4 100644
--- a/zephyr/include/emul/emul_kb_raw.h
+++ b/zephyr/include/emul/emul_kb_raw.h
@@ -1,8 +1,10 @@
-/* Copyright 2022 The ChromiumOS Authors
+/* 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 <stdint.h>
+
/**
* @file
*
diff --git a/zephyr/test/drivers/keyboard_scan/CMakeLists.txt b/zephyr/test/drivers/keyboard_scan/CMakeLists.txt
index dc57dbe259..0def2a5ff9 100644
--- a/zephyr/test/drivers/keyboard_scan/CMakeLists.txt
+++ b/zephyr/test/drivers/keyboard_scan/CMakeLists.txt
@@ -4,5 +4,11 @@
target_sources(app PRIVATE
src/keyboard_scan.c
+ src/keyboard_test_utils.c
src/mkbp_event.c
+ src/mkbp_info.c
+)
+
+target_include_directories(app PRIVATE
+ ${CMAKE_CURRENT_SOURCE_DIR}/include
)
diff --git a/zephyr/test/drivers/keyboard_scan/include/keyboard_test_utils.h b/zephyr/test/drivers/keyboard_scan/include/keyboard_test_utils.h
new file mode 100644
index 0000000000..0117fea09c
--- /dev/null
+++ b/zephyr/test/drivers/keyboard_scan/include/keyboard_test_utils.h
@@ -0,0 +1,19 @@
+/* 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.
+ */
+
+/**
+ * @brief Press or release a key through the keyboard emulator
+ *
+ * @param row Key row
+ * @param col Key column
+ * @param pressed 1 if pressed, 0 otherwise
+ * @return int 0 if successful
+ */
+int emulate_keystate(int row, int col, int pressed);
+
+/**
+ * @brief Clears any pressed keys in the keyboard emulator
+ */
+void clear_emulated_keys(void);
diff --git a/zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c b/zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c
index 2f5bb1015f..c7955ec655 100644
--- a/zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c
+++ b/zephyr/test/drivers/keyboard_scan/src/keyboard_scan.c
@@ -1,4 +1,4 @@
-/* Copyright 2022 The ChromiumOS Authors
+/* 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.
*/
@@ -14,16 +14,10 @@
#include "console.h"
#include "host_command.h"
#include "keyboard_scan.h"
+#include "keyboard_test_utils.h"
#include "test/drivers/test_mocks.h"
#include "test/drivers/test_state.h"
-int emulate_keystate(int row, int col, int pressed)
-{
- const struct device *dev = DEVICE_DT_GET(DT_NODELABEL(cros_kb_raw));
-
- return emul_kb_raw_set_kbstate(dev, row, col, pressed);
-}
-
ZTEST(keyboard_scan, test_boot_key)
{
const struct device *dev = DEVICE_DT_GET(DT_NODELABEL(cros_kb_raw));
@@ -409,6 +403,9 @@ static void reset_keyboard(void *data)
/* Turn off key state change printing */
keyboard_scan_set_print_state_changes(0);
+ /* Reset KB emulator */
+ clear_emulated_keys();
+
/* Reset all mocks. */
RESET_FAKE(key_state_changed);
RESET_FAKE(system_is_locked);
diff --git a/zephyr/test/drivers/keyboard_scan/src/keyboard_test_utils.c b/zephyr/test/drivers/keyboard_scan/src/keyboard_test_utils.c
new file mode 100644
index 0000000000..7b49bd1df4
--- /dev/null
+++ b/zephyr/test/drivers/keyboard_scan/src/keyboard_test_utils.c
@@ -0,0 +1,19 @@
+/* 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 <emul/emul_kb_raw.h>
+#include <zephyr/drivers/emul.h>
+
+const static struct device *dev = DEVICE_DT_GET(DT_NODELABEL(cros_kb_raw));
+
+int emulate_keystate(int row, int col, int pressed)
+{
+ return emul_kb_raw_set_kbstate(dev, row, col, pressed);
+}
+
+void clear_emulated_keys(void)
+{
+ emul_kb_raw_reset(dev);
+}
diff --git a/zephyr/test/drivers/keyboard_scan/src/mkbp_info.c b/zephyr/test/drivers/keyboard_scan/src/mkbp_info.c
new file mode 100644
index 0000000000..b0d64eb1da
--- /dev/null
+++ b/zephyr/test/drivers/keyboard_scan/src/mkbp_info.c
@@ -0,0 +1,232 @@
+/* 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/ztest.h>
+#include <zephyr/fff.h>
+#include <emul/emul_kb_raw.h>
+
+#include "console.h"
+#include "host_command.h"
+#include "keyboard_scan.h"
+#include "keyboard_test_utils.h"
+#include "mkbp_info.h"
+#include "mkbp_input_devices.h"
+#include "test/drivers/test_state.h"
+
+ZTEST(mkbp_info, host_command_mkbp_info__keyboard_info)
+{
+ /* Get the number of keyboard rows and columns */
+
+ int ret;
+ struct ec_response_mkbp_info response;
+ struct ec_params_mkbp_info request = {
+ .info_type = EC_MKBP_INFO_KBD,
+ };
+
+ struct host_cmd_handler_args args =
+ BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request);
+
+ ret = host_command_process(&args);
+ zassert_equal(EC_SUCCESS, ret, "Host command failed: %d", ret);
+ zassert_equal(KEYBOARD_ROWS, response.rows, NULL);
+ zassert_equal(KEYBOARD_COLS_MAX, response.cols, NULL);
+}
+
+ZTEST(mkbp_info, host_command_mkbp_info__supported_buttons)
+{
+ /* Get the set of supported buttons */
+
+ int ret;
+ union ec_response_get_next_data response;
+ struct ec_params_mkbp_info request = {
+ .info_type = EC_MKBP_INFO_SUPPORTED,
+ .event_type = EC_MKBP_EVENT_BUTTON,
+ };
+
+ struct host_cmd_handler_args args =
+ BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request);
+
+ ret = host_command_process(&args);
+ zassert_equal(EC_SUCCESS, ret, "Host command failed: %d", ret);
+ zassert_equal(get_supported_buttons(), response.buttons, NULL);
+}
+
+ZTEST(mkbp_info, host_command_mkbp_info__supported_switches)
+{
+ /* Get the set of supported switches */
+
+ int ret;
+ union ec_response_get_next_data response;
+ struct ec_params_mkbp_info request = {
+ .info_type = EC_MKBP_INFO_SUPPORTED,
+ .event_type = EC_MKBP_EVENT_SWITCH,
+ };
+
+ struct host_cmd_handler_args args =
+ BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request);
+
+ ret = host_command_process(&args);
+ zassert_equal(EC_SUCCESS, ret, "Host command failed: %d", ret);
+ zassert_equal(get_supported_switches(), response.switches, NULL);
+}
+
+ZTEST(mkbp_info, host_command_mkbp_info__supported_invalid)
+{
+ /* Request support info on a non-existent type of input device. */
+
+ int ret;
+ union ec_response_get_next_data response;
+ struct ec_params_mkbp_info request = {
+ .info_type = EC_MKBP_INFO_SUPPORTED,
+ .event_type = EC_MKBP_EVENT_COUNT, /* Unsupported */
+ };
+
+ struct host_cmd_handler_args args =
+ BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request);
+
+ ret = host_command_process(&args);
+ zassert_equal(EC_RES_INVALID_PARAM, ret,
+ "Host command didn't fail properly: %d", ret);
+}
+
+ZTEST(mkbp_info, host_command_mkbp_info__current_keyboard_matrix)
+{
+ /* Hold down a key so we can validate the returned keyboard matrix state
+ */
+ const struct device *dev = DEVICE_DT_GET(DT_NODELABEL(cros_kb_raw));
+
+ emul_kb_raw_set_kbstate(dev, KEYBOARD_ROW_KEY_R, KEYBOARD_COL_KEY_R, 1);
+ keyboard_scan_init();
+
+ k_sleep(K_MSEC(100));
+
+ /* Get the current keyboard matrix state */
+
+ int ret;
+ union ec_response_get_next_data response;
+ struct ec_params_mkbp_info request = {
+ .info_type = EC_MKBP_INFO_CURRENT,
+ .event_type = EC_MKBP_EVENT_KEY_MATRIX,
+ };
+
+ struct host_cmd_handler_args args =
+ BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request);
+
+ ret = host_command_process(&args);
+ zassert_equal(EC_SUCCESS, ret, "Host command failed: %d", ret);
+
+ zassert_true(response.key_matrix[KEYBOARD_COL_KEY_R] &
+ KEYBOARD_MASK_KEY_R,
+ "Expected key is not pressed");
+}
+
+ZTEST(mkbp_info, host_command_mkbp_info__current_host_events)
+{
+ int ret;
+ union ec_response_get_next_data response;
+ struct ec_params_mkbp_info request = {
+ .info_type = EC_MKBP_INFO_CURRENT,
+ .event_type = EC_MKBP_EVENT_HOST_EVENT,
+ };
+
+ struct host_cmd_handler_args args =
+ BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request);
+
+ ret = host_command_process(&args);
+ zassert_equal(EC_SUCCESS, ret, "Host command failed: %d", ret);
+ zassert_equal((uint32_t)host_get_events(), response.host_event, NULL);
+}
+
+ZTEST(mkbp_info, host_command_mkbp_info__current_host_events64)
+{
+ int ret;
+ union ec_response_get_next_data response;
+ struct ec_params_mkbp_info request = {
+ .info_type = EC_MKBP_INFO_CURRENT,
+ .event_type = EC_MKBP_EVENT_HOST_EVENT64,
+ };
+
+ struct host_cmd_handler_args args =
+ BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request);
+
+ ret = host_command_process(&args);
+ zassert_equal(EC_SUCCESS, ret, "Host command failed: %d", ret);
+ zassert_equal(host_get_events(), response.host_event64, NULL);
+}
+
+ZTEST(mkbp_info, host_command_mkbp_info__current_buttons)
+{
+ int ret;
+ union ec_response_get_next_data response;
+ struct ec_params_mkbp_info request = {
+ .info_type = EC_MKBP_INFO_CURRENT,
+ .event_type = EC_MKBP_EVENT_BUTTON,
+ };
+
+ struct host_cmd_handler_args args =
+ BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request);
+
+ ret = host_command_process(&args);
+ zassert_equal(EC_SUCCESS, ret, "Host command failed: %d", ret);
+ zassert_equal(mkbp_get_button_state(), response.buttons, NULL);
+}
+
+ZTEST(mkbp_info, host_command_mkbp_info__current_switches)
+{
+ int ret;
+ union ec_response_get_next_data response;
+ struct ec_params_mkbp_info request = {
+ .info_type = EC_MKBP_INFO_CURRENT,
+ .event_type = EC_MKBP_EVENT_SWITCH,
+ };
+
+ struct host_cmd_handler_args args =
+ BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request);
+
+ ret = host_command_process(&args);
+ zassert_equal(EC_SUCCESS, ret, "Host command failed: %d", ret);
+ zassert_equal(mkbp_get_switch_state(), response.switches, NULL);
+}
+
+ZTEST(mkbp_info, host_command_mkbp_info__current_invalid)
+{
+ int ret;
+ union ec_response_get_next_data response;
+ struct ec_params_mkbp_info request = {
+ .info_type = EC_MKBP_INFO_CURRENT,
+ .event_type = EC_MKBP_EVENT_COUNT, /* Unsupported */
+ };
+
+ struct host_cmd_handler_args args =
+ BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request);
+
+ ret = host_command_process(&args);
+ zassert_equal(EC_RES_INVALID_PARAM, ret, "Host command failed: %d",
+ ret);
+}
+
+ZTEST(mkbp_info, host_command_mkbp_info__invalid)
+{
+ int ret;
+ union ec_response_get_next_data response;
+ struct ec_params_mkbp_info request = {
+ .info_type = -1, /* Unsupported */
+ };
+
+ struct host_cmd_handler_args args =
+ BUILD_HOST_COMMAND(EC_CMD_MKBP_INFO, 0, response, request);
+
+ ret = host_command_process(&args);
+ zassert_equal(EC_RES_ERROR, ret, "Host command failed: %d", ret);
+}
+
+static void reset(void *data)
+{
+ ARG_UNUSED(data);
+
+ /* Release any pressed keys in the emulator */
+ clear_emulated_keys();
+}
+
+ZTEST_SUITE(mkbp_info, drivers_predicate_post_main, NULL, reset, reset, NULL);