1 /*
2  * Copyright 2024 Google LLC
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #include "emulated_target.hpp"
7 
8 DEFINE_FFF_GLOBALS;
9 
10 #define DEFINE_FAKE_TARGET_FUNCTION(node_id, prop, n)                                              \
11 	DEFINE_FAKE_VALUE_FUNC(int, target_read_requested_##n, struct i2c_target_config *,         \
12 			       uint8_t *);                                                         \
13 	DEFINE_FAKE_VALUE_FUNC(int, target_read_processed_##n, struct i2c_target_config *,         \
14 			       uint8_t *);                                                         \
15 	DEFINE_FAKE_VALUE_FUNC(int, target_write_requested_##n, struct i2c_target_config *);       \
16 	DEFINE_FAKE_VALUE_FUNC(int, target_write_received_##n, struct i2c_target_config *,         \
17 			       uint8_t);                                                           \
18 	DEFINE_FAKE_VALUE_FUNC(int, target_stop_##n, struct i2c_target_config *);                  \
19 	DEFINE_FAKE_VALUE_FUNC(int, target_buf_read_requested_##n, struct i2c_target_config *,     \
20 				uint8_t **, uint32_t *)                                            \
21 	DEFINE_FAKE_VOID_FUNC(target_buf_write_received_##n, struct i2c_target_config *,           \
22 			       uint8_t *, uint32_t)
23 
24 DT_FOREACH_PROP_ELEM(CONTROLLER_LABEL, forwards, DEFINE_FAKE_TARGET_FUNCTION);
25 
26 /* clang-format off */
27 #define DEFINE_EMULATED_CALLBACK(node_id, prop, n)                                                 \
28 	{                                                                                          \
29 		.write_requested = target_write_requested_##n,                                     \
30 		.read_requested = target_read_requested_##n,                                       \
31 		.write_received = target_write_received_##n,                                       \
32 		.read_processed = target_read_processed_##n,                                       \
33 		COND_CODE_1(CONFIG_I2C_TARGET_BUFFER_MODE,                                         \
34 			    (.buf_write_received = target_buf_write_received_##n, ), ())           \
35 		COND_CODE_1(CONFIG_I2C_TARGET_BUFFER_MODE,                                         \
36 				(.buf_read_requested = target_buf_read_requested_##n, ), ())       \
37 		.stop = target_stop_##n,                                                           \
38 	},
39 /* clang-format on */
40 
41 struct i2c_target_callbacks emulated_callbacks[FORWARD_COUNT] = {
42 	DT_FOREACH_PROP_ELEM(CONTROLLER_LABEL, forwards, DEFINE_EMULATED_CALLBACK)};
43 
44 #define DEFINE_EMULATED_TARGET_CONFIG(node_id, prop, n)                                            \
45 	{                                                                                          \
46 		.flags = 0,                                                                        \
47 		.address = DT_PHA_BY_IDX(node_id, prop, n, addr),                                  \
48 		.callbacks = &emulated_callbacks[n],                                               \
49 	},
50 
51 struct i2c_target_config emulated_target_config[FORWARD_COUNT] = {
52 	DT_FOREACH_PROP_ELEM(CONTROLLER_LABEL, forwards, DEFINE_EMULATED_TARGET_CONFIG)};
53