summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuval Peress <peress@google.com>2022-04-25 22:54:53 -0600
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-04-29 18:57:41 +0000
commit3c4c678d47ddea8a45c5d7c11c1747c179a4971c (patch)
tree9eb2b84274d3749c6cb20970e1ea17641dd5125b
parenta588afd712c638e75df5c0b5b90d860d9f8a55c6 (diff)
downloadchrome-ec-3c4c678d47ddea8a45c5d7c11c1747c179a4971c.tar.gz
test: add tests for motion sense scale host command
BRANCH=none BUG=b:224614211 TEST=zmake test test-drivers Signed-off-by: Yuval Peress <peress@google.com> Change-Id: I4aa0b58b1d89a0c5e73fb1a7c586584dc4515066 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3607057 Reviewed-by: Sam Hurst <shurst@google.com>
-rw-r--r--zephyr/test/drivers/include/test/drivers/utils.h22
-rw-r--r--zephyr/test/drivers/src/host_cmd/motion_sense.c111
-rw-r--r--zephyr/test/drivers/src/utils.c20
3 files changed, 153 insertions, 0 deletions
diff --git a/zephyr/test/drivers/include/test/drivers/utils.h b/zephyr/test/drivers/include/test/drivers/utils.h
index 2346d02bb9..b131b1056e 100644
--- a/zephyr/test/drivers/include/test/drivers/utils.h
+++ b/zephyr/test/drivers/include/test/drivers/utils.h
@@ -332,6 +332,28 @@ int host_cmd_motion_sense_offset(uint8_t sensor_num, uint16_t flags,
struct ec_response_motion_sense *response);
/**
+ * @brief Call the host command MOTION_SENSE with the sensor scale sub-command
+ *
+ * This function attempts to set the scale if the flags field includes
+ * MOTION_SENSE_SET_OFFSET. Otherwise, the temperature and scales are ignored.
+ * The response field will include the current (after modification) scales and
+ * temperature.
+ *
+ * @param sensor_num The sensor index in the motion_sensors array to query
+ * @param flags The flags to pass to the host command
+ * @param temperature The temperature at which the scales were attained (set)
+ * @param scale_x The X scale to set
+ * @param scale_y The Y scale to set
+ * @param scale_z The Z scale to set
+ * @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_scale(uint8_t sensor_num, uint16_t flags,
+ int16_t temperature, int16_t scale_x,
+ int16_t scale_y, int16_t scale_z,
+ 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 3bfc39096b..170730d3d5 100644
--- a/zephyr/test/drivers/src/host_cmd/motion_sense.c
+++ b/zephyr/test/drivers/src/host_cmd/motion_sense.c
@@ -16,6 +16,10 @@ FAKE_VALUE_FUNC(int, mock_set_offset, const struct motion_sensor_t *,
const int16_t *, int16_t);
FAKE_VALUE_FUNC(int, mock_get_offset, const struct motion_sensor_t *, int16_t *,
int16_t *);
+FAKE_VALUE_FUNC(int, mock_set_scale, const struct motion_sensor_t *,
+ const uint16_t *, int16_t);
+FAKE_VALUE_FUNC(int, mock_get_scale, const struct motion_sensor_t *, uint16_t *,
+ int16_t *);
/**
* Get the size needed for a struct ec_response_motion_sense
@@ -34,6 +38,8 @@ static struct host_cmd_motion_sense_fixture fixture = {
.set_range = mock_set_range,
.set_offset = mock_set_offset,
.get_offset = mock_get_offset,
+ .set_scale = mock_set_scale,
+ .get_scale = mock_get_scale,
},
};
@@ -50,6 +56,8 @@ static void host_cmd_motion_sense_before(void *state)
RESET_FAKE(mock_set_range);
RESET_FAKE(mock_set_offset);
RESET_FAKE(mock_get_offset);
+ RESET_FAKE(mock_set_scale);
+ RESET_FAKE(mock_get_scale);
FFF_RESET_HISTORY();
motion_sensors[0].config[SENSOR_CONFIG_AP].odr = 0;
@@ -459,3 +467,106 @@ ZTEST_USER_F(host_cmd_motion_sense, test_get_offset)
zassert_equal(3, mock_set_offset_fake.arg1_history[0][1], NULL);
zassert_equal(4, mock_set_offset_fake.arg1_history[0][2], NULL);
}
+
+ZTEST_USER(host_cmd_motion_sense, test_scale_invalid_sensor_num)
+{
+ struct ec_response_motion_sense response;
+
+ zassert_equal(EC_RES_INVALID_PARAM,
+ host_cmd_motion_sense_scale(
+ /*sensor_num=*/0xff,
+ /*flags=*/0,
+ /*temperature=*/1, /*scale_x=*/2,
+ /*scale_y=*/3, /*scale_z=*/4, &response),
+ NULL);
+}
+
+ZTEST_USER(host_cmd_motion_sense, test_get_scale_not_in_driver)
+{
+ struct ec_response_motion_sense response;
+ struct accelgyro_drv drv = *motion_sensors[0].drv;
+
+ drv.get_scale = NULL;
+ motion_sensors[0].drv = &drv;
+
+ zassert_equal(EC_RES_INVALID_COMMAND,
+ host_cmd_motion_sense_scale(
+ /*sensor_num=*/0,
+ /*flags=*/0,
+ /*temperature=*/1, /*scale_x=*/2,
+ /*scale_y=*/3, /*scale_z=*/4, &response),
+ NULL);
+}
+
+ZTEST_USER(host_cmd_motion_sense, test_set_scale_not_in_driver)
+{
+ struct ec_response_motion_sense response;
+ struct accelgyro_drv drv = *motion_sensors[0].drv;
+
+ drv.set_scale = NULL;
+ motion_sensors[0].drv = &drv;
+
+ zassert_equal(EC_RES_INVALID_COMMAND,
+ host_cmd_motion_sense_scale(
+ /*sensor_num=*/0,
+ /*flags=*/MOTION_SENSE_SET_OFFSET,
+ /*temperature=*/1, /*scale_x=*/2,
+ /*scale_y=*/3, /*scale_z=*/4, &response),
+ NULL);
+}
+
+ZTEST_USER_F(host_cmd_motion_sense, test_get_scale_fail)
+{
+ struct ec_response_motion_sense response;
+
+ motion_sensors[0].drv = &this->mock_drv;
+ mock_get_scale_fake.return_val = 1;
+
+ zassert_equal(1,
+ host_cmd_motion_sense_scale(
+ /*sensor_num=*/0,
+ /*flags=*/0,
+ /*temperature=*/1, /*scale_x=*/2,
+ /*scale_y=*/3, /*scale_z=*/4, &response),
+ NULL);
+ zassert_equal(1, mock_get_scale_fake.call_count, NULL);
+}
+
+ZTEST_USER_F(host_cmd_motion_sense, test_set_scale_fail)
+{
+ struct ec_response_motion_sense response;
+
+ motion_sensors[0].drv = &this->mock_drv;
+ mock_set_scale_fake.return_val = 1;
+
+ zassert_equal(1,
+ host_cmd_motion_sense_scale(
+ /*sensor_num=*/0,
+ /*flags=*/MOTION_SENSE_SET_OFFSET,
+ /*temperature=*/1, /*scale_x=*/2,
+ /*scale_y=*/3, /*scale_z=*/4, &response),
+ NULL);
+ zassert_equal(1, mock_set_scale_fake.call_count, NULL);
+}
+
+ZTEST_USER_F(host_cmd_motion_sense, test_set_get_scale)
+{
+ struct ec_response_motion_sense response;
+
+ motion_sensors[0].drv = &this->mock_drv;
+ mock_set_scale_fake.return_val = 0;
+ mock_get_scale_fake.return_val = 0;
+
+ zassert_ok(host_cmd_motion_sense_scale(
+ /*sensor_num=*/0,
+ /*flags=*/MOTION_SENSE_SET_OFFSET,
+ /*temperature=*/1, /*scale_x=*/2,
+ /*scale_y=*/3, /*scale_z=*/4, &response),
+ NULL);
+ zassert_equal(1, mock_set_scale_fake.call_count, NULL);
+ zassert_equal(1, mock_get_scale_fake.call_count, NULL);
+ zassert_equal(1, mock_set_scale_fake.arg2_history[0], NULL);
+ zassert_equal(2, mock_set_scale_fake.arg1_history[0][0], NULL);
+ zassert_equal(3, mock_set_scale_fake.arg1_history[0][1], NULL);
+ zassert_equal(4, mock_set_scale_fake.arg1_history[0][2], NULL);
+}
diff --git a/zephyr/test/drivers/src/utils.c b/zephyr/test/drivers/src/utils.c
index 393bb87a13..bdc52bcae6 100644
--- a/zephyr/test/drivers/src/utils.c
+++ b/zephyr/test/drivers/src/utils.c
@@ -213,6 +213,26 @@ int host_cmd_motion_sense_offset(uint8_t sensor_num, uint16_t flags,
return host_command_process(&args);
}
+int host_cmd_motion_sense_scale(uint8_t sensor_num, uint16_t flags,
+ int16_t temperature, int16_t scale_x,
+ int16_t scale_y, int16_t scale_z,
+ struct ec_response_motion_sense *response)
+{
+ struct ec_params_motion_sense params = {
+ .cmd = MOTIONSENSE_CMD_SENSOR_SCALE,
+ .sensor_scale = {
+ .sensor_num = sensor_num,
+ .flags = flags,
+ .temp = temperature,
+ .scale = { scale_x, scale_y, scale_z },
+ },
+ };
+ struct host_cmd_handler_args args = BUILD_HOST_COMMAND(
+ EC_CMD_MOTION_SENSE_CMD, 1, *response, params);
+
+ return host_command_process(&args);
+}
+
void host_cmd_typec_discovery(int port, enum typec_partner_type partner_type,
void *response, size_t response_size)
{