summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/default/src/locate_chip.c
blob: 684254397125585cc1a80f2a2a6f210768bde0ad (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
/* Copyright 2022 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <zephyr/fff.h>
#include <zephyr/shell/shell.h>
#include <zephyr/kernel.h>
#include <zephyr/ztest.h>

#include "test/drivers/test_state.h"
#include "test/drivers/utils.h"
#include "host_command.h"

/**
 * @brief TestPurpose: test the TCPC locate valid case.
 */
ZTEST_USER(locate_chip, test_hc_locate_chip_tcpc)
{
	int ret;
	struct ec_params_locate_chip p;
	struct ec_response_locate_chip r;
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND(EC_CMD_LOCATE_CHIP, 0, r, p);

	p.type = EC_CHIP_TYPE_TCPC;
	p.index = 0;

	ret = host_command_process(&args);

	zassert_equal(ret, EC_RES_SUCCESS, "Unexpected return value: %d", ret);
	zassert_equal(r.bus_type, EC_BUS_TYPE_I2C, "Unexpected bus_type: %d",
		      r.bus_type);
	zassert_equal(r.i2c_info.port, 2, "Unexpected port: %d",
		      r.i2c_info.port);
	zassert_equal(r.i2c_info.addr_flags, 0x82, "Unexpected addr_flags: %d",
		      r.i2c_info.addr_flags);

	p.type = EC_CHIP_TYPE_TCPC;
	p.index = 1;

	ret = host_command_process(&args);

	zassert_equal(ret, EC_RES_SUCCESS, "Unexpected return value: %d", ret);
	zassert_equal(r.bus_type, EC_BUS_TYPE_I2C, "Unexpected bus_type: %d",
		      r.bus_type);
	zassert_equal(r.i2c_info.port, 3, "Unexpected port: %d",
		      r.i2c_info.port);
	zassert_equal(r.i2c_info.addr_flags, 0x0b, "Unexpected addr_flags: %d",
		      r.i2c_info.addr_flags);
}

/**
 * @brief TestPurpose: test the TCPC index overflow case.
 */
ZTEST_USER(locate_chip, test_hc_locate_chip_tcpc_overflow)
{
	int ret;
	struct ec_params_locate_chip p;
	struct ec_response_locate_chip r;
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND(EC_CMD_LOCATE_CHIP, 0, r, p);

	p.type = EC_CHIP_TYPE_TCPC;
	p.index = 10;

	ret = host_command_process(&args);

	zassert_equal(ret, EC_RES_OVERFLOW, "Unexpected return value: %d", ret);
}

/**
 * @brief TestPurpose: test the EEPROM locate valid case.
 */
ZTEST_USER(locate_chip, test_hc_locate_chip_eeprom)
{
	int ret;
	struct ec_params_locate_chip p;
	struct ec_response_locate_chip r;
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND(EC_CMD_LOCATE_CHIP, 0, r, p);

	p.type = EC_CHIP_TYPE_CBI_EEPROM;
	p.index = 0;

	ret = host_command_process(&args);

	zassert_equal(ret, EC_RES_SUCCESS, "Unexpected return value: %d", ret);
	zassert_equal(r.bus_type, EC_BUS_TYPE_I2C, "Unexpected bus_type: %d",
		      r.bus_type);
	zassert_equal(r.i2c_info.port, I2C_PORT_EEPROM, "Unexpected port: %d",
		      r.i2c_info.port);
	zassert_equal(r.i2c_info.addr_flags, I2C_ADDR_EEPROM_FLAGS,
		      "Unexpected addr_flags: %d", r.i2c_info.addr_flags);
}

/**
 * @brief TestPurpose: test the EEPROM index overflow case.
 */
ZTEST_USER(locate_chip, test_hc_locate_chip_eeprom_overflow)
{
	int ret;
	struct ec_params_locate_chip p;
	struct ec_response_locate_chip r;
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND(EC_CMD_LOCATE_CHIP, 0, r, p);

	p.type = EC_CHIP_TYPE_CBI_EEPROM;
	p.index = 1;

	ret = host_command_process(&args);

	zassert_equal(ret, EC_RES_OVERFLOW, "Unexpected return value: %d", ret);
}

/**
 * @brief TestPurpose: test the invalid parameter case.
 */
ZTEST_USER(locate_chip, test_hc_locate_chip_invalid)
{
	int ret;
	struct ec_params_locate_chip p;
	struct ec_response_locate_chip r;
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND(EC_CMD_LOCATE_CHIP, 0, r, p);

	p.type = EC_CHIP_TYPE_COUNT;
	ret = host_command_process(&args);

	zassert_equal(ret, EC_RES_INVALID_PARAM, "Unexpected return value: %d",
		      ret);
}

ZTEST_SUITE(locate_chip, drivers_predicate_post_main, NULL, NULL, NULL, NULL);