1 /*
2  * Copyright 2024 Google LLC
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #ifndef _TESTS_DRIVERS_I2C_I2C_EMUL_INCLUDE_EMULATED_TARGET_H
7 #define _TESTS_DRIVERS_I2C_I2C_EMUL_INCLUDE_EMULATED_TARGET_H
8 
9 #include <functional>
10 
11 #define CUSTOM_FFF_FUNCTION_TEMPLATE(RETURN, FUNCNAME, ...)                                        \
12 	std::function<RETURN(__VA_ARGS__)> FUNCNAME
13 
14 #include <zephyr/devicetree.h>
15 #include <zephyr/drivers/i2c.h>
16 #include <zephyr/fff.h>
17 
18 #define CONTROLLER_LABEL DT_NODELABEL(i2c0)
19 #define TARGET_LABEL(n)  DT_NODELABEL(DT_CAT(i2c, n))
20 #define FORWARD_COUNT    DT_PROP_LEN(CONTROLLER_LABEL, forwards)
21 
22 extern struct i2c_target_callbacks emulated_callbacks[FORWARD_COUNT];
23 extern struct i2c_target_config emulated_target_config[FORWARD_COUNT];
24 
25 /* Declare all the fake functions needed */
26 #define DECLARE_FAKE_TARGET_FUNCTIONS(node_id, prop, n)                                            \
27 	DECLARE_FAKE_VALUE_FUNC(int, target_read_requested_##n, struct i2c_target_config *,        \
28 				uint8_t *);                                                        \
29 	DECLARE_FAKE_VALUE_FUNC(int, target_read_processed_##n, struct i2c_target_config *,        \
30 				uint8_t *);                                                        \
31 	DECLARE_FAKE_VALUE_FUNC(int, target_write_requested_##n, struct i2c_target_config *);      \
32 	DECLARE_FAKE_VALUE_FUNC(int, target_write_received_##n, struct i2c_target_config *,        \
33 				uint8_t);                                                          \
34 	DECLARE_FAKE_VALUE_FUNC(int, target_stop_##n, struct i2c_target_config *);                 \
35 	DECLARE_FAKE_VALUE_FUNC(int, target_buf_read_requested_##n, struct i2c_target_config *,    \
36 				uint8_t **, uint32_t *)                                            \
37 	DECLARE_FAKE_VOID_FUNC(target_buf_write_received_##n, struct i2c_target_config *,          \
38 			       uint8_t *, uint32_t)
39 
40 DT_FOREACH_PROP_ELEM(CONTROLLER_LABEL, forwards, DECLARE_FAKE_TARGET_FUNCTIONS)
41 
42 #define FFF_FAKE_ACTION(node_id, prop, n, fn)                                                      \
43 	do {                                                                                       \
44 		fn(target_read_requested_##n);                                                     \
45 		fn(target_read_processed_##n);                                                     \
46 		fn(target_write_requested_##n);                                                    \
47 		fn(target_write_received_##n);                                                     \
48 		fn(target_stop_##n);                                                               \
49 		fn(target_buf_read_requested_##n);                                                 \
50 		fn(target_buf_write_received_##n);                                                 \
51 	} while (0);
52 
53 #define FFF_FAKES_LIST_FOREACH(fn)                                                                 \
54 	DT_FOREACH_PROP_ELEM_VARGS(CONTROLLER_LABEL, forwards, FFF_FAKE_ACTION, fn)
55 
56 #endif /* _TESTS_DRIVERS_I2C_I2C_EMUL_INCLUDE_EMULATED_TARGET_H */
57