diff options
author | Yuval Peress <peress@chromium.org> | 2019-10-29 15:02:37 -0600 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2020-02-13 00:56:05 +0000 |
commit | 88808c3dda819f9da22272850d2b56ab1e538c9f (patch) | |
tree | 614c8f28cb17c232ac74ac252b615fcecd554ba6 /test/online_calibration.c | |
parent | 4acc29890130946ad574ea0f314b65497efb7ce2 (diff) | |
download | chrome-ec-88808c3dda819f9da22272850d2b56ab1e538c9f.tar.gz |
common: Migrate online calibration to own module
This change moves the code that handles caching the temperature
(which is the first step in online calibration) into a new
compilational unit.
TEST=None
BRANCH=None
BUG=b:138303429,chromium:1023858
Change-Id: Ib1fe3d2234dc2436e2bbfd4febd22196e5cdafef
Signed-off-by: Yuval Peress <peress@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1906340
Reviewed-by: Jack Rosenthal <jrosenth@chromium.org>
Diffstat (limited to 'test/online_calibration.c')
-rw-r--r-- | test/online_calibration.c | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/test/online_calibration.c b/test/online_calibration.c new file mode 100644 index 0000000000..f9fe0ee068 --- /dev/null +++ b/test/online_calibration.c @@ -0,0 +1,134 @@ +/* Copyright 2020 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 "online_calibration.h" +#include "test_util.h" +#include "hwtimer.h" +#include "timer.h" +#include "accelgyro.h" + +struct mock_read_temp_result { + void *s; + int temp; + int ret; + int used_count; + struct mock_read_temp_result *next; +}; + +static struct mock_read_temp_result *mock_read_temp_results; + +static int mock_read_temp(const struct motion_sensor_t *s, int *temp) +{ + struct mock_read_temp_result *ptr = mock_read_temp_results; + + while (ptr) { + if (ptr->s == s) { + if (ptr->ret == EC_SUCCESS) + *temp = ptr->temp; + ptr->used_count++; + return ptr->ret; + } + ptr = ptr->next; + } + + return EC_ERROR_UNKNOWN; +} + +static struct accelgyro_drv mock_sensor_driver = { + .read_temp = mock_read_temp, +}; + +static struct accelgyro_drv empty_sensor_driver = {}; + +struct motion_sensor_t motion_sensors[] = { + [BASE] = { + .drv = &mock_sensor_driver, + }, + [LID] = { + .drv = &empty_sensor_driver, + }, +}; + +const unsigned int motion_sensor_count = ARRAY_SIZE(motion_sensors); + +static int test_read_temp_on_stage(void) +{ + struct mock_read_temp_result expected = { &motion_sensors[BASE], 200, + EC_SUCCESS, 0, NULL }; + struct ec_response_motion_sensor_data data; + int rc; + + mock_read_temp_results = &expected; + data.sensor_num = 0; + rc = online_calibration_process_data( + &data, &motion_sensors[0], __hw_clock_source_read()); + + TEST_EQ(rc, EC_SUCCESS, "%d"); + TEST_EQ(expected.used_count, 1, "%d"); + + return EC_SUCCESS; +} + +static int test_read_temp_from_cache_on_stage(void) +{ + struct mock_read_temp_result expected = { &motion_sensors[BASE], 200, + EC_SUCCESS, 0, NULL }; + struct ec_response_motion_sensor_data data; + int rc; + + mock_read_temp_results = &expected; + data.sensor_num = 0; + rc = online_calibration_process_data( + &data, &motion_sensors[0], __hw_clock_source_read()); + TEST_EQ(rc, EC_SUCCESS, "%d"); + + rc = online_calibration_process_data( + &data, &motion_sensors[0], __hw_clock_source_read()); + TEST_EQ(rc, EC_SUCCESS, "%d"); + + TEST_EQ(expected.used_count, 1, "%d"); + + return EC_SUCCESS; +} + +static int test_read_temp_twice_after_cache_stale(void) +{ + struct mock_read_temp_result expected = { &motion_sensors[BASE], 200, + EC_SUCCESS, 0, NULL }; + struct ec_response_motion_sensor_data data; + int rc; + + mock_read_temp_results = &expected; + data.sensor_num = 0; + rc = online_calibration_process_data( + &data, &motion_sensors[0], __hw_clock_source_read()); + TEST_EQ(rc, EC_SUCCESS, "%d"); + + sleep(2); + rc = online_calibration_process_data( + &data, &motion_sensors[0], __hw_clock_source_read()); + TEST_EQ(rc, EC_SUCCESS, "%d"); + + TEST_EQ(expected.used_count, 2, "%d"); + + return EC_SUCCESS; +} + +void before_test(void) +{ + mock_read_temp_results = NULL; + online_calibration_init(); +} + +void run_test(void) +{ + test_reset(); + + RUN_TEST(test_read_temp_on_stage); + RUN_TEST(test_read_temp_from_cache_on_stage); + RUN_TEST(test_read_temp_twice_after_cache_stale); + + test_print_result(); +} |