diff options
-rw-r--r-- | zephyr/test/drivers/include/utils.h | 13 | ||||
-rw-r--r-- | zephyr/test/drivers/src/host_cmd/motion_sense.c | 66 | ||||
-rw-r--r-- | zephyr/test/drivers/src/utils.c | 16 |
3 files changed, 95 insertions, 0 deletions
diff --git a/zephyr/test/drivers/include/utils.h b/zephyr/test/drivers/include/utils.h index fb335fa764..12de1fa789 100644 --- a/zephyr/test/drivers/include/utils.h +++ b/zephyr/test/drivers/include/utils.h @@ -211,6 +211,19 @@ host_cmd_get_charge_control(void) return response; } +/** + * @brief Call the host command MOTION_SENSE with the dump sub-command + * + * Note: this function uses the zassume_ API. It will skip the test if the host + * command fails. + * + * @param max_sensor_count The maximum number of sensor data objects to populate + * in the response object. + * @param response Pointer to the response object to fill. + */ +void host_cmd_motion_sense_dump(int max_sensor_count, + struct ec_response_motion_sense *response); + #define GPIO_ACOK_OD_NODE DT_NODELABEL(gpio_acok_od) #define GPIO_ACOK_OD_PIN DT_GPIO_PIN(GPIO_ACOK_OD_NODE, gpios) diff --git a/zephyr/test/drivers/src/host_cmd/motion_sense.c b/zephyr/test/drivers/src/host_cmd/motion_sense.c new file mode 100644 index 0000000000..46a3540ad0 --- /dev/null +++ b/zephyr/test/drivers/src/host_cmd/motion_sense.c @@ -0,0 +1,66 @@ +/* Copyright 2022 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <ztest.h> + +#include "motion_sense.h" +#include "test_state.h" +#include "utils.h" + +/** + * Get the size needed for a struct ec_response_motion_sense + */ +#define RESPONSE_MOTION_SENSE_BUFFER_SIZE(n) \ + (sizeof(struct ec_response_motion_sense) + \ + n * sizeof(struct ec_response_motion_sensor_data)) + +ZTEST_SUITE(host_cmd_motion_sense, drivers_predicate_post_main, NULL, NULL, + NULL, NULL); + +ZTEST_USER(host_cmd_motion_sense, test_dump) +{ + uint8_t response_buffer[RESPONSE_MOTION_SENSE_BUFFER_SIZE( + ALL_MOTION_SENSORS)]; + struct ec_response_motion_sense *result = + (struct ec_response_motion_sense *)response_buffer; + + /* Set up the motion sensor data */ + for (int i = 0; i < ALL_MOTION_SENSORS; ++i) { + motion_sensors[i].xyz[0] = i; + motion_sensors[i].xyz[1] = i + 1; + motion_sensors[i].xyz[2] = i + 2; + } + host_cmd_motion_sense_dump(ALL_MOTION_SENSORS, result); + + zassert_equal(result->dump.module_flags, MOTIONSENSE_MODULE_FLAG_ACTIVE, + NULL); + zassert_equal(result->dump.sensor_count, ALL_MOTION_SENSORS, NULL); + + /* + * Test the values returned in the dump. Normally we shouldn't be doing + * tests in a loop, but since the number of sensors (as well as the + * order) is adjustable by devicetree, it would be too difficult to hard + * code here. + */ + for (int i = 0; i < ALL_MOTION_SENSORS; ++i) { + zassert_equal(result->dump.sensor[i].flags, + MOTIONSENSE_SENSOR_FLAG_PRESENT, NULL); + zassert_equal(result->dump.sensor[i].data[0], i, NULL); + zassert_equal(result->dump.sensor[i].data[1], i + 1, NULL); + zassert_equal(result->dump.sensor[i].data[2], i + 2, NULL); + } +} + +ZTEST_USER(host_cmd_motion_sense, test_dump__large_max_sensor_count) +{ + uint8_t response_buffer[RESPONSE_MOTION_SENSE_BUFFER_SIZE( + ALL_MOTION_SENSORS)]; + struct ec_response_motion_sense *result = + (struct ec_response_motion_sense *)response_buffer; + + host_cmd_motion_sense_dump(ALL_MOTION_SENSORS + 1, result); + + zassert_equal(result->dump.sensor_count, ALL_MOTION_SENSORS, NULL); +} diff --git a/zephyr/test/drivers/src/utils.c b/zephyr/test/drivers/src/utils.c index cf680dcc6d..e11da5c238 100644 --- a/zephyr/test/drivers/src/utils.c +++ b/zephyr/test/drivers/src/utils.c @@ -95,3 +95,19 @@ void disconnect_source_from_port(const struct emul *tcpci_emul, isl923x_emul_set_adc_vbus(charger_emul, 0); k_sleep(K_SECONDS(1)); } + +void host_cmd_motion_sense_dump(int max_sensor_count, + struct ec_response_motion_sense *response) +{ + struct ec_params_motion_sense params = { + .cmd = MOTIONSENSE_CMD_DUMP, + .dump = { + .max_sensor_count = max_sensor_count, + }, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_MOTION_SENSE_CMD, 4, *response, params); + + zassume_ok(host_command_process(&args), + "Failed to get motion_sense dump"); +} |