summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/src/host_cmd/motion_sense.c
blob: 46a3540ad06236c27b383770ebf6f36694936f54 (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
/* Copyright 2022 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 <ztest.h>

#include "motion_sense.h"
#include "test_state.h"
#include "utils.h"

/**
 * Get the size needed for a struct ec_response_motion_sense
 */
#define RESPONSE_MOTION_SENSE_BUFFER_SIZE(n)       \
	(sizeof(struct ec_response_motion_sense) + \
	 n * sizeof(struct ec_response_motion_sensor_data))

ZTEST_SUITE(host_cmd_motion_sense, drivers_predicate_post_main, NULL, NULL,
	    NULL, NULL);

ZTEST_USER(host_cmd_motion_sense, test_dump)
{
	uint8_t response_buffer[RESPONSE_MOTION_SENSE_BUFFER_SIZE(
		ALL_MOTION_SENSORS)];
	struct ec_response_motion_sense *result =
		(struct ec_response_motion_sense *)response_buffer;

	/* Set up the motion sensor data */
	for (int i = 0; i < ALL_MOTION_SENSORS; ++i) {
		motion_sensors[i].xyz[0] = i;
		motion_sensors[i].xyz[1] = i + 1;
		motion_sensors[i].xyz[2] = i + 2;
	}
	host_cmd_motion_sense_dump(ALL_MOTION_SENSORS, result);

	zassert_equal(result->dump.module_flags, MOTIONSENSE_MODULE_FLAG_ACTIVE,
		      NULL);
	zassert_equal(result->dump.sensor_count, ALL_MOTION_SENSORS, NULL);

	/*
	 * Test the values returned in the dump. Normally we shouldn't be doing
	 * tests in a loop, but since the number of sensors (as well as the
	 * order) is adjustable by devicetree, it would be too difficult to hard
	 * code here.
	 */
	for (int i = 0; i < ALL_MOTION_SENSORS; ++i) {
		zassert_equal(result->dump.sensor[i].flags,
			      MOTIONSENSE_SENSOR_FLAG_PRESENT, NULL);
		zassert_equal(result->dump.sensor[i].data[0], i, NULL);
		zassert_equal(result->dump.sensor[i].data[1], i + 1, NULL);
		zassert_equal(result->dump.sensor[i].data[2], i + 2, NULL);
	}
}

ZTEST_USER(host_cmd_motion_sense, test_dump__large_max_sensor_count)
{
	uint8_t response_buffer[RESPONSE_MOTION_SENSE_BUFFER_SIZE(
		ALL_MOTION_SENSORS)];
	struct ec_response_motion_sense *result =
		(struct ec_response_motion_sense *)response_buffer;

	host_cmd_motion_sense_dump(ALL_MOTION_SENSORS + 1, result);

	zassert_equal(result->dump.sensor_count, ALL_MOTION_SENSORS, NULL);
}