1 /*
2  * Copyright (c) 2023 Google LLC
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief Backend APIs for the fuel gauge emulators.
10  */
11 
12 #ifndef ZEPHYR_INCLUDE_DRIVERS_EMUL_FUEL_GAUGE_H_
13 #define ZEPHYR_INCLUDE_DRIVERS_EMUL_FUEL_GAUGE_H_
14 
15 #include <stdint.h>
16 #include <zephyr/drivers/emul.h>
17 #include <zephyr/drivers/fuel_gauge.h>
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 /**
24  * @brief Fuel gauge backend emulator APIs
25  * @defgroup fuel_gauge_emulator_backend Fuel gauge backend emulator APIs
26  * @ingroup io_interfaces
27  * @{
28  */
29 
30 /**
31  * @cond INTERNAL_HIDDEN
32  *
33  * These are for internal use only, so skip these in public documentation.
34  */
35 __subsystem struct fuel_gauge_emul_driver_api {
36 	int (*set_battery_charging)(const struct emul *emul, uint32_t uV, int uA);
37 	int (*is_battery_cutoff)(const struct emul *emul, bool *cutoff);
38 };
39 /**
40  * @endcond
41  */
42 
43 /**
44  * @brief Set charging for fuel gauge associated battery.
45  *
46  * Set how much the battery associated with a fuel gauge IC is charging or discharging. Where
47  * voltage is always positive and a positive or negative current denotes charging or discharging,
48  * respectively.
49  *
50  * @param target Pointer to the emulator structure for the fuel gauge emulator instance.
51  * @param uV Microvolts describing the battery voltage.
52  * @param uA Microamps describing the battery current where negative is discharging.
53  *
54  * @retval 0 If successful.
55  * @retval -EINVAL if mV or mA are 0.
56  */
57 __syscall int emul_fuel_gauge_set_battery_charging(const struct emul *target, uint32_t uV, int uA);
z_impl_emul_fuel_gauge_set_battery_charging(const struct emul * target,uint32_t uV,int uA)58 static inline int z_impl_emul_fuel_gauge_set_battery_charging(const struct emul *target,
59 							      uint32_t uV, int uA)
60 {
61 	const struct fuel_gauge_emul_driver_api *backend_api =
62 		(const struct fuel_gauge_emul_driver_api *)target->backend_api;
63 
64 	if (backend_api->set_battery_charging == 0) {
65 		return -ENOTSUP;
66 	}
67 
68 	return backend_api->set_battery_charging(target, uV, uA);
69 }
70 
71 /**
72  * @brief Check if the battery has been cut off.
73  *
74  * @param target Pointer to the emulator structure for the fuel gauge emulator instance.
75  * @param cutoff Pointer to bool storing variable.
76  *
77  * @retval 0 If successful.
78  * @retval -ENOTSUP if not supported by emulator.
79  */
80 __syscall int emul_fuel_gauge_is_battery_cutoff(const struct emul *target, bool *cutoff);
z_impl_emul_fuel_gauge_is_battery_cutoff(const struct emul * target,bool * cutoff)81 static inline int z_impl_emul_fuel_gauge_is_battery_cutoff(const struct emul *target, bool *cutoff)
82 {
83 	const struct fuel_gauge_emul_driver_api *backend_api =
84 		(const struct fuel_gauge_emul_driver_api *)target->backend_api;
85 
86 	if (backend_api->is_battery_cutoff == 0) {
87 		return -ENOTSUP;
88 	}
89 	return backend_api->is_battery_cutoff(target, cutoff);
90 }
91 
92 #ifdef __cplusplus
93 }
94 #endif
95 
96 #include <syscalls/emul_fuel_gauge.h>
97 
98 /**
99  * @}
100  */
101 
102 #endif /* ZEPHYR_INCLUDE_DRIVERS_EMUL_FUEL_GAUGE_H_*/
103