summaryrefslogtreecommitdiff
path: root/test/online_calibration_spoof.c
blob: 44a3b812d98d6ab17da50ca5fd535dc43c44c490 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* Copyright 2020 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "accel_cal.h"
#include "accelgyro.h"
#include "hwtimer.h"
#include "mag_cal.h"
#include "online_calibration.h"
#include "test_util.h"
#include "gyro_cal_init_for_test.h"
#include "gyro_cal.h"
#include "timer.h"
#include <stdio.h>

int mkbp_send_event(uint8_t event_type)
{
	return 1;
}

/*
 * Mocked driver (can be re-used for all sensors).
 */

static int mock_read_temp(const struct motion_sensor_t *s, int *temp)
{
	if (temp)
		*temp = 200;
	return EC_SUCCESS;
}

static struct accelgyro_drv mock_sensor_driver = {
	.read_temp = mock_read_temp,
};

/*
 * Accelerometer, magnetometer, and gyroscope data structs.
 */

static struct accel_cal_algo accel_cal_algos[] = { {
	.newton_fit = NEWTON_FIT(4, 15, FLOAT_TO_FP(0.01f), FLOAT_TO_FP(0.25f),
				 FLOAT_TO_FP(1.0e-8f), 100),
} };

static struct accel_cal accel_cal_data = {
	.still_det =
		STILL_DET(FLOAT_TO_FP(0.00025f), 800 * MSEC, 1200 * MSEC, 5),
	.algos = accel_cal_algos,
	.num_temp_windows = ARRAY_SIZE(accel_cal_algos),
};

static struct mag_cal_t mag_cal_data;

static struct gyro_cal gyro_cal_data;

/*
 * Motion sensor array and count.
 */

struct motion_sensor_t motion_sensors[] = {
	{
		.type = MOTIONSENSE_TYPE_ACCEL,
		.default_range = 4,
		.drv = &mock_sensor_driver,
		.online_calib_data[0] = {
			.type_specific_data = &accel_cal_data,
		},
	},
	{
		.type = MOTIONSENSE_TYPE_MAG,
		.default_range = 4,
		.drv = &mock_sensor_driver,
		.online_calib_data[0] = {
			.type_specific_data = &mag_cal_data,
		},
	},
	{
		.type = MOTIONSENSE_TYPE_GYRO,
		.default_range = 4,
		.drv = &mock_sensor_driver,
		.online_calib_data[0] = {
			.type_specific_data = &gyro_cal_data,
		},
	},
};

const unsigned int motion_sensor_count = ARRAY_SIZE(motion_sensors);

static void spoof_sensor_data(struct motion_sensor_t *s, int x, int y, int z)
{
	struct ec_response_motion_sensor_data data;
	uint32_t timestamp = 0;

	/* Set the data and flags. */
	data.data[X] = x;
	data.data[Y] = y;
	data.data[Z] = z;
	s->flags |= MOTIONSENSE_FLAG_IN_SPOOF_MODE;

	/* Pass the data to online_calibdation. */
	online_calibration_process_data(&data, s, timestamp);
}

/*
 * Begin testing.
 */

static int test_accel_calibration_on_spoof(void)
{
	struct ec_response_online_calibration_data out;

	/* Send spoof data (1, 2, 3). */
	spoof_sensor_data(&motion_sensors[0], 1, 2, 3);

	/* Check that we have new values. */
	TEST_ASSERT(online_calibration_has_new_values());

	/* Get the new values for sensor 0. */
	TEST_ASSERT(online_calibration_read(&motion_sensors[0], &out));

	/* Validate the new values. */
	TEST_EQ(out.data[X], 1, "%d");
	TEST_EQ(out.data[Y], 2, "%d");
	TEST_EQ(out.data[Z], 3, "%d");

	/* Validate that no other sensors have data. */
	TEST_ASSERT(!online_calibration_has_new_values());

	return EC_SUCCESS;
}

static int test_mag_calibration_on_spoof(void)
{
	struct ec_response_online_calibration_data out;

	/* Send spoof data (4, 5, 6). */
	spoof_sensor_data(&motion_sensors[1], 4, 5, 6);

	/* Check that we have new values. */
	TEST_ASSERT(online_calibration_has_new_values());

	/* Get the new values for sensor 0. */
	TEST_ASSERT(online_calibration_read(&motion_sensors[1], &out));

	/* Validate the new values. */
	TEST_EQ(out.data[X], 4, "%d");
	TEST_EQ(out.data[Y], 5, "%d");
	TEST_EQ(out.data[Z], 6, "%d");

	/* Validate that no other sensors have data. */
	TEST_ASSERT(!online_calibration_has_new_values());

	return EC_SUCCESS;
}

static int test_gyro_calibration_on_spoof(void)
{
	struct ec_response_online_calibration_data out;

	/* Send spoof data (7, 8, 9). */
	spoof_sensor_data(&motion_sensors[2], 7, 8, 9);

	/* Check that we have new values. */
	TEST_ASSERT(online_calibration_has_new_values());

	/* Get the new values for sensor 0. */
	TEST_ASSERT(online_calibration_read(&motion_sensors[2], &out));

	/* Validate the new values. */
	TEST_EQ(out.data[X], 7, "%d");
	TEST_EQ(out.data[Y], 8, "%d");
	TEST_EQ(out.data[Z], 9, "%d");

	/* Validate that no other sensors have data. */
	TEST_ASSERT(!online_calibration_has_new_values());

	return EC_SUCCESS;
}

void before_test(void)
{
	online_calibration_init();
	gyro_cal_initialization_for_test(&gyro_cal_data);
}

void run_test(int argc, const char **argv)
{
	test_reset();

	RUN_TEST(test_accel_calibration_on_spoof);
	RUN_TEST(test_mag_calibration_on_spoof);
	RUN_TEST(test_gyro_calibration_on_spoof);

	test_print_result();
}