diff options
author | Tristan Honscheid <honscheid@google.com> | 2021-10-18 11:42:25 -0600 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2021-10-18 21:39:23 +0000 |
commit | 705fd9970f12bce63f8caa9371f633488e6432ee (patch) | |
tree | ff8ddd8f3266fb995006a06a96a9af0fd6b0131d | |
parent | 78f5a2e13645460cd313d6487903e709f0d1143e (diff) | |
download | chrome-ec-705fd9970f12bce63f8caa9371f633488e6432ee.tar.gz |
zephyr: Test accelgyro_bmi260.c for unsupported operations
Add a test that covers parts of the code where we validate sensor types.
Also made a modification to accelgyro_bmi260.c where I moved the sensor
type check up to the top of the func so we fail before trying to set
the data rate (which doesn't work for magnetometers).
BRANCH=None
BUG=b:184856157
TEST=zmake configure --test zephyr/test/drivers
Signed-off-by: Tristan Honscheid <honscheid@google.com>
Change-Id: I170087b8bfdc1212972d00010c90789967a6415b
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3229076
Reviewed-by: Jeremy Bettis <jbettis@chromium.org>
-rw-r--r-- | driver/accelgyro_bmi260.c | 14 | ||||
-rw-r--r-- | zephyr/test/drivers/src/bmi260.c | 41 |
2 files changed, 51 insertions, 4 deletions
diff --git a/driver/accelgyro_bmi260.c b/driver/accelgyro_bmi260.c index 9e48ab13ae..fe0bf0e0cc 100644 --- a/driver/accelgyro_bmi260.c +++ b/driver/accelgyro_bmi260.c @@ -254,6 +254,12 @@ static int perform_calib(struct motion_sensor_t *s, int enable) if (!enable) return EC_SUCCESS; + + /* We only support accelerometers and gyroscopes */ + if (s->type != MOTIONSENSE_TYPE_ACCEL && + s->type != MOTIONSENSE_TYPE_GYRO) + return EC_RES_INVALID_PARAM; + rate = bmi_get_data_rate(s); ret = set_data_rate(s, 100000, 0); if (ret) @@ -269,10 +275,12 @@ static int perform_calib(struct motion_sensor_t *s, int enable) break; case MOTIONSENSE_TYPE_GYRO: break; + /* LCOV_EXCL_START */ default: - /* Not supported on Magnetometer */ - ret = EC_RES_INVALID_PARAM; - goto end_perform_calib; + /* Unreachable due to sensor type check above. */ + ASSERT(false); + break; + /* LCOV_EXCL_STOP */ } /* Get the calibrated offset */ diff --git a/zephyr/test/drivers/src/bmi260.c b/zephyr/test/drivers/src/bmi260.c index 3c56844bb5..964a83d678 100644 --- a/zephyr/test/drivers/src/bmi260.c +++ b/zephyr/test/drivers/src/bmi260.c @@ -1853,6 +1853,44 @@ static void test_bmi_gyr_fifo(void) NULL); } +static void test_unsupported_configs(void) +{ + /* + * This test checks that we properly handle passing in invalid sensor + * types or attempting unsupported operations on certain sensor types. + */ + + struct motion_sensor_t ms_fake; + + /* Part 1: + * Setting offset on anything that is not an accel or gyro is an error. + * Make a copy of the accelerometer motion sensor struct and modify its + * type to magnetometer for this test. + */ + memcpy(&ms_fake, &motion_sensors[BMI_ACC_SENSOR_ID], sizeof(ms_fake)); + ms_fake.type = MOTIONSENSE_TYPE_MAG; + + int16_t offset[3]; + int ret = + ms_fake.drv->set_offset(&ms_fake, (const int16_t *)&offset, 0); + zassert_equal( + ret, EC_RES_INVALID_PARAM, + "Expected a return code of %d (EC_RES_INVALID_PARAM) but got %d", + EC_RES_INVALID_PARAM, ret); + + /* Part 2: + * Running a calibration on a magnetometer is also not supported. + */ + memcpy(&ms_fake, &motion_sensors[BMI_ACC_SENSOR_ID], sizeof(ms_fake)); + ms_fake.type = MOTIONSENSE_TYPE_MAG; + + ret = ms_fake.drv->perform_calib(&ms_fake, 1); + zassert_equal( + ret, EC_RES_INVALID_PARAM, + "Expected a return code of %d (EC_RES_INVALID_PARAM) but got %d", + EC_RES_INVALID_PARAM, ret); +} + void test_suite_bmi260(void) { ztest_test_suite(bmi260, @@ -1873,6 +1911,7 @@ void test_suite_bmi260(void) ztest_user_unit_test(test_bmi_gyr_perform_calib), ztest_user_unit_test(test_bmi_init), ztest_user_unit_test(test_bmi_acc_fifo), - ztest_user_unit_test(test_bmi_gyr_fifo)); + ztest_user_unit_test(test_bmi_gyr_fifo), + ztest_user_unit_test(test_unsupported_configs)); ztest_run_test_suite(bmi260); } |