1 /*
2  * Copyright 2024 Google LLC
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #ifndef INCLUDE_ZEPHYR_DRIVERS_EMUL_BBRAM_H_
7 #define INCLUDE_ZEPHYR_DRIVERS_EMUL_BBRAM_H_
8 
9 #include <zephyr/drivers/emul.h>
10 
11 #include <stdint.h>
12 
13 /**
14  * @brief BBRAM emulator backend API
15  * @defgroup bbram_emulator_backend BBRAM emulator backend API
16  * @ingroup io_interfaces
17  * @{
18  */
19 
20 /**
21  * @cond INTERNAL_HIDDEN
22  *
23  * These are for internal use only, so skip these in public documentation.
24  */
25 
26 __subsystem struct emul_bbram_driver_api {
27 	/** Sets the data */
28 	int (*set_data)(const struct emul *target, size_t offset, size_t count,
29 			const uint8_t *data);
30 	/** Checks the data */
31 	int (*get_data)(const struct emul *target, size_t offset, size_t count, uint8_t *data);
32 };
33 
34 /**
35  * @endcond
36  */
37 
38 /**
39  * @brief Set the expected data in the bbram region
40  *
41  * @param target Pointer to the emulator instance to operate on
42  * @param offset Offset within the memory to set
43  * @param count Number of bytes to write
44  * @param data The data to write
45  * @return 0 if successful
46  * @return -ENOTSUP if no backend API or if the set_data function isn't implemented
47  * @return -ERANGE if the destination address is out of range.
48  */
emul_bbram_backend_set_data(const struct emul * target,size_t offset,size_t count,const uint8_t * data)49 static inline int emul_bbram_backend_set_data(const struct emul *target, size_t offset,
50 					      size_t count, const uint8_t *data)
51 {
52 	if (target == NULL || target->backend_api == NULL) {
53 		return -ENOTSUP;
54 	}
55 
56 	struct emul_bbram_driver_api *api = (struct emul_bbram_driver_api *)target->backend_api;
57 
58 	if (api->set_data == NULL) {
59 		return -ENOTSUP;
60 	}
61 
62 	return api->set_data(target, offset, count, data);
63 }
64 
65 /**
66  * @brief Get the expected data in the bbram region
67  *
68  * @param target Pointer to the emulator instance to operate on
69  * @param offset Offset within the memory to get
70  * @param count Number of bytes to read
71  * @param data The data buffer to hold the result
72  * @return 0 if successful
73  * @return -ENOTSUP if no backend API or if the get_data function isn't implemented
74  * @return -ERANGE if the address is out of range.
75  */
emul_bbram_backend_get_data(const struct emul * target,size_t offset,size_t count,uint8_t * data)76 static inline int emul_bbram_backend_get_data(const struct emul *target, size_t offset,
77 					      size_t count, uint8_t *data)
78 {
79 	if (target == NULL || target->backend_api == NULL) {
80 		return -ENOTSUP;
81 	}
82 
83 	struct emul_bbram_driver_api *api = (struct emul_bbram_driver_api *)target->backend_api;
84 
85 	if (api->get_data == NULL) {
86 		return -ENOTSUP;
87 	}
88 
89 	return api->get_data(target, offset, count, data);
90 }
91 
92 /**
93  * @}
94  */
95 
96 #endif /* INCLUDE_ZEPHYR_DRIVERS_EMUL_BBRAM_H_ */
97