diff options
author | Yuval Peress <peress@google.com> | 2022-04-08 12:37:46 -0600 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-04-12 00:17:34 +0000 |
commit | 9e0aca287e03830b09acef8a9cdb9e4c814a0f5c (patch) | |
tree | 839fc6d32a5f7993cc5d67146d2df21c5166387c | |
parent | baf8f7c1aff107f856042acb66dc2ec3f86624a7 (diff) | |
download | chrome-ec-9e0aca287e03830b09acef8a9cdb9e4c814a0f5c.tar.gz |
ztest: motion_sense: add tests for ec_rate subcommand
Add tests for the various code paths of the ec_rate subcommand:
- Invalid sensor number
- Get the current ec_rate
- Set the AP configured ec_rate
BRANCH=none
BUG=b:224614211
TEST=zmake test --coverage test-drivers
Signed-off-by: Yuval Peress <peress@google.com>
Change-Id: Ib49019b2a8e61f863aa0d27107dc9b204ea24bb2
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3579627
Reviewed-by: Fabio Baltieri <fabiobaltieri@google.com>
-rw-r--r-- | common/motion_sense.c | 2 | ||||
-rw-r--r-- | zephyr/test/drivers/include/test/drivers/utils.h | 15 | ||||
-rw-r--r-- | zephyr/test/drivers/src/host_cmd/motion_sense.c | 46 | ||||
-rw-r--r-- | zephyr/test/drivers/src/utils.c | 17 |
4 files changed, 77 insertions, 3 deletions
diff --git a/common/motion_sense.c b/common/motion_sense.c index 46972de886..41e52cd10b 100644 --- a/common/motion_sense.c +++ b/common/motion_sense.c @@ -1154,7 +1154,7 @@ static enum ec_status host_cmd_motion_sense(struct host_cmd_handler_args *args) case MOTIONSENSE_CMD_EC_RATE: sensor = host_sensor_id_to_real_sensor( - in->sensor_odr.sensor_num); + in->ec_rate.sensor_num); if (sensor == NULL) return EC_RES_INVALID_PARAM; diff --git a/zephyr/test/drivers/include/test/drivers/utils.h b/zephyr/test/drivers/include/test/drivers/utils.h index a97e527a90..c8258af6f7 100644 --- a/zephyr/test/drivers/include/test/drivers/utils.h +++ b/zephyr/test/drivers/include/test/drivers/utils.h @@ -248,6 +248,21 @@ int host_cmd_motion_sense_info(uint8_t cmd_version, uint8_t sensor_num, struct ec_response_motion_sense *response); /** + * @brief Call the host command MOTION_SENSE with the ec_rate sub-command + * + * This function performs a read of the current rate by passing + * EC_MOTION_SENSE_NO_VALUE as the data rate. Otherwise, the data rate should be + * updated. + * + * @param sensor_num The sensor index in the motion_sensors array to query + * @param data_rate_ms The new data rate or EC_MOTION_SENSE_NO_VALUE to read + * @param response Pointer to the response data structure to fill on success + * @return The result code from the host command + */ +int host_cmd_motion_sense_ec_rate(uint8_t sensor_num, int data_rate_ms, + struct ec_response_motion_sense *response); + +/** * Run the host command to get the PD discovery responses. * * @param port The USB-C port number diff --git a/zephyr/test/drivers/src/host_cmd/motion_sense.c b/zephyr/test/drivers/src/host_cmd/motion_sense.c index 454ca66dcb..4063007401 100644 --- a/zephyr/test/drivers/src/host_cmd/motion_sense.c +++ b/zephyr/test/drivers/src/host_cmd/motion_sense.c @@ -16,8 +16,13 @@ (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); +static void host_cmd_motion_sense_before(void *state) +{ + motion_sensors[0].config[SENSOR_CONFIG_AP].ec_rate = 1000 * MSEC; +} + +ZTEST_SUITE(host_cmd_motion_sense, drivers_predicate_post_main, NULL, + host_cmd_motion_sense_before, NULL, NULL); ZTEST_USER(host_cmd_motion_sense, test_dump) { @@ -148,3 +153,40 @@ ZTEST_USER(host_cmd_motion_sense, test_get_info_v4__no_read_temp) NULL); } } + +ZTEST_USER(host_cmd_motion_sense, test_get_ec_rate__invalid_sensor_num) +{ + struct ec_response_motion_sense response; + + zassert_equal(host_cmd_motion_sense_ec_rate( + /*sensor_num=*/0xff, + /*data_rate_ms=*/EC_MOTION_SENSE_NO_VALUE, + &response), + EC_RES_INVALID_PARAM, NULL); +} + +ZTEST_USER(host_cmd_motion_sense, test_get_ec_rate) +{ + struct ec_response_motion_sense response; + + zassert_ok(host_cmd_motion_sense_ec_rate( + /*sensor_num=*/0, + /*data_rate_ms=*/EC_MOTION_SENSE_NO_VALUE, + &response), + NULL); + zassert_equal(response.ec_rate.ret, 1000, NULL); +} + +ZTEST_USER(host_cmd_motion_sense, test_set_ec_rate) +{ + struct ec_response_motion_sense response; + + zassert_ok(host_cmd_motion_sense_ec_rate( + /*sensor_num=*/0, /*data_rate_ms=*/2000, &response), + NULL); + /* The command should return the previous rate */ + zassert_equal(response.ec_rate.ret, 1000, NULL); + /* The sensor's AP config value should be updated */ + zassert_equal(motion_sensors[0].config[SENSOR_CONFIG_AP].ec_rate, + 2000 * MSEC, NULL); +} diff --git a/zephyr/test/drivers/src/utils.c b/zephyr/test/drivers/src/utils.c index b86ae52375..a639b63d17 100644 --- a/zephyr/test/drivers/src/utils.c +++ b/zephyr/test/drivers/src/utils.c @@ -142,6 +142,23 @@ int host_cmd_motion_sense_info(uint8_t cmd_version, uint8_t sensor_num, return host_command_process(&args); } +int host_cmd_motion_sense_ec_rate(uint8_t sensor_num, int data_rate_ms, + struct ec_response_motion_sense *response) +{ + struct ec_params_motion_sense params = { + .cmd = MOTIONSENSE_CMD_EC_RATE, + .ec_rate = { + .sensor_num = sensor_num, + .data = data_rate_ms, + }, + }; + struct host_cmd_handler_args args = BUILD_HOST_COMMAND( + EC_CMD_MOTION_SENSE_CMD, 1, *response, params); + + printk("sensor_num=%u/%u\n", params.sensor_odr.sensor_num, sensor_num); + return host_command_process(&args); +} + void host_cmd_typec_discovery(int port, enum typec_partner_type partner_type, void *response, size_t response_size) { |