summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuval Peress <peress@google.com>2022-04-25 23:08:40 -0600
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-04-29 18:57:45 +0000
commit6ca603504d2e5b466e8f8236dcd11ede8e693fc3 (patch)
tree64bdeb51e91b0462c546032c0039e0f44e5d98ff
parent3c4c678d47ddea8a45c5d7c11c1747c179a4971c (diff)
downloadchrome-ec-6ca603504d2e5b466e8f8236dcd11ede8e693fc3.tar.gz
test: add tests for motion sense calibration host command
BRANCH=none BUG=b:224614211 TEST=zmake test test-drivers Signed-off-by: Yuval Peress <peress@google.com> Change-Id: I3fb8bceb62e495a15d68272fee959590f186d86c Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3607058 Reviewed-by: Abe Levkoy <alevkoy@chromium.org>
-rw-r--r--zephyr/test/drivers/include/test/drivers/utils.h11
-rw-r--r--zephyr/test/drivers/src/host_cmd/motion_sense.c73
-rw-r--r--zephyr/test/drivers/src/utils.c16
3 files changed, 100 insertions, 0 deletions
diff --git a/zephyr/test/drivers/include/test/drivers/utils.h b/zephyr/test/drivers/include/test/drivers/utils.h
index b131b1056e..46a39dbc93 100644
--- a/zephyr/test/drivers/include/test/drivers/utils.h
+++ b/zephyr/test/drivers/include/test/drivers/utils.h
@@ -354,6 +354,17 @@ int host_cmd_motion_sense_scale(uint8_t sensor_num, uint16_t flags,
struct ec_response_motion_sense *response);
/**
+ * @brief Enable/disable sensor calibration via host command
+ *
+ * @param sensor_num The sensor index in the motion_sensors array to query
+ * @param enable Whether to enable or disable the calibration
+ * @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_calib(uint8_t sensor_num, bool enable,
+ 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 170730d3d5..08e794c5d4 100644
--- a/zephyr/test/drivers/src/host_cmd/motion_sense.c
+++ b/zephyr/test/drivers/src/host_cmd/motion_sense.c
@@ -20,6 +20,7 @@ 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 *);
+FAKE_VALUE_FUNC(int, mock_perform_calib, struct motion_sensor_t *, int);
/**
* Get the size needed for a struct ec_response_motion_sense
@@ -40,6 +41,7 @@ static struct host_cmd_motion_sense_fixture fixture = {
.get_offset = mock_get_offset,
.set_scale = mock_set_scale,
.get_scale = mock_get_scale,
+ .perform_calib = mock_perform_calib,
},
};
@@ -58,6 +60,7 @@ static void host_cmd_motion_sense_before(void *state)
RESET_FAKE(mock_get_offset);
RESET_FAKE(mock_set_scale);
RESET_FAKE(mock_get_scale);
+ RESET_FAKE(mock_perform_calib);
FFF_RESET_HISTORY();
motion_sensors[0].config[SENSOR_CONFIG_AP].odr = 0;
@@ -570,3 +573,73 @@ ZTEST_USER_F(host_cmd_motion_sense, test_set_get_scale)
zassert_equal(3, mock_set_scale_fake.arg1_history[0][1], NULL);
zassert_equal(4, mock_set_scale_fake.arg1_history[0][2], NULL);
}
+
+ZTEST_USER(host_cmd_motion_sense, test_calib_invalid_sensor_num)
+{
+ struct ec_response_motion_sense response;
+
+ zassert_equal(EC_RES_INVALID_PARAM,
+ host_cmd_motion_sense_calib(/*sensor_num=*/0xff,
+ /*enable=*/false, &response),
+ NULL);
+}
+
+ZTEST_USER(host_cmd_motion_sense, test_calib_not_in_driver)
+{
+ struct ec_response_motion_sense response;
+ struct accelgyro_drv drv = { 0 };
+
+ motion_sensors[0].drv = &drv;
+ zassert_equal(EC_RES_INVALID_COMMAND,
+ host_cmd_motion_sense_calib(/*sensor_num=*/0,
+ /*enable=*/false, &response),
+ NULL);
+}
+
+ZTEST_USER_F(host_cmd_motion_sense, test_calib_fail)
+{
+ struct ec_response_motion_sense response;
+
+ motion_sensors[0].drv = &this->mock_drv;
+ mock_perform_calib_fake.return_val = 1;
+
+ zassert_equal(1,
+ host_cmd_motion_sense_calib(/*sensor_num=*/0,
+ /*enable=*/false, &response),
+ NULL);
+ zassert_equal(1, mock_perform_calib_fake.call_count, NULL);
+ zassert_false(mock_perform_calib_fake.arg1_history[0], NULL);
+}
+
+ZTEST_USER_F(host_cmd_motion_sense, test_calib_success__fail_get_offset)
+{
+ struct ec_response_motion_sense response;
+
+ motion_sensors[0].drv = &this->mock_drv;
+ mock_perform_calib_fake.return_val = 0;
+ mock_get_offset_fake.return_val = 1;
+
+ zassert_equal(1,
+ host_cmd_motion_sense_calib(/*sensor_num=*/0,
+ /*enable=*/false, &response),
+ NULL);
+ zassert_equal(1, mock_perform_calib_fake.call_count, NULL);
+ zassert_equal(1, mock_get_offset_fake.call_count, NULL);
+ zassert_false(mock_perform_calib_fake.arg1_history[0], NULL);
+}
+
+ZTEST_USER_F(host_cmd_motion_sense, test_calib)
+{
+ struct ec_response_motion_sense response;
+
+ motion_sensors[0].drv = &this->mock_drv;
+ mock_perform_calib_fake.return_val = 0;
+ mock_get_offset_fake.return_val = 0;
+
+ zassert_ok(host_cmd_motion_sense_calib(/*sensor_num=*/0,
+ /*enable=*/true, &response),
+ NULL);
+ zassert_equal(1, mock_perform_calib_fake.call_count, NULL);
+ zassert_equal(1, mock_get_offset_fake.call_count, NULL);
+ zassert_true(mock_perform_calib_fake.arg1_history[0], NULL);
+}
diff --git a/zephyr/test/drivers/src/utils.c b/zephyr/test/drivers/src/utils.c
index bdc52bcae6..0829e4c874 100644
--- a/zephyr/test/drivers/src/utils.c
+++ b/zephyr/test/drivers/src/utils.c
@@ -233,6 +233,22 @@ int host_cmd_motion_sense_scale(uint8_t sensor_num, uint16_t flags,
return host_command_process(&args);
}
+int host_cmd_motion_sense_calib(uint8_t sensor_num, bool enable,
+ struct ec_response_motion_sense *response)
+{
+ struct ec_params_motion_sense params = {
+ .cmd = MOTIONSENSE_CMD_PERFORM_CALIB,
+ .perform_calib = {
+ .sensor_num = sensor_num,
+ .enable = enable,
+ },
+ };
+ 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)
{