Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
emul_sensor.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Google LLC
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
11
12#include <zephyr/drivers/emul.h>
15
16#include <stdint.h>
17
25
31
35__subsystem struct emul_sensor_driver_api {
37 int (*set_channel)(const struct emul *target, struct sensor_chan_spec ch,
38 const q31_t *value, int8_t shift);
40 int (*get_sample_range)(const struct emul *target, struct sensor_chan_spec ch, q31_t *lower,
41 q31_t *upper, q31_t *epsilon, int8_t *shift);
43 int (*set_attribute)(const struct emul *target, struct sensor_chan_spec ch,
44 enum sensor_attribute attribute, const void *value);
46 int (*get_attribute_metadata)(const struct emul *target, struct sensor_chan_spec ch,
47 enum sensor_attribute attribute, q31_t *min, q31_t *max,
48 q31_t *increment, int8_t *shift);
49};
53
61static inline bool emul_sensor_backend_is_supported(const struct emul *target)
62{
63 return target && target->backend_api;
64}
65
78static inline int emul_sensor_backend_set_channel(const struct emul *target,
79 struct sensor_chan_spec ch, const q31_t *value,
80 int8_t shift)
81{
82 if (!target || !target->backend_api) {
83 return -ENOTSUP;
84 }
85
86 struct emul_sensor_driver_api *api = (struct emul_sensor_driver_api *)target->backend_api;
87
88 if (api->set_channel) {
89 return api->set_channel(target, ch, value, shift);
90 }
91 return -ENOTSUP;
92}
93
111static inline int emul_sensor_backend_get_sample_range(const struct emul *target,
112 struct sensor_chan_spec ch, q31_t *lower,
113 q31_t *upper, q31_t *epsilon, int8_t *shift)
114{
115 if (!target || !target->backend_api) {
116 return -ENOTSUP;
117 }
118
119 struct emul_sensor_driver_api *api = (struct emul_sensor_driver_api *)target->backend_api;
120
121 if (api->get_sample_range) {
122 return api->get_sample_range(target, ch, lower, upper, epsilon, shift);
123 }
124 return -ENOTSUP;
125}
126
137static inline int emul_sensor_backend_set_attribute(const struct emul *target,
138 struct sensor_chan_spec ch,
139 enum sensor_attribute attribute,
140 const void *value)
141{
142 if (!target || !target->backend_api) {
143 return -ENOTSUP;
144 }
145
146 struct emul_sensor_driver_api *api = (struct emul_sensor_driver_api *)target->backend_api;
147
148 if (api->set_attribute == NULL) {
149 return -ENOTSUP;
150 }
151 return api->set_attribute(target, ch, attribute, value);
152}
153
171static inline int emul_sensor_backend_get_attribute_metadata(const struct emul *target,
172 struct sensor_chan_spec ch,
173 enum sensor_attribute attribute,
174 q31_t *min, q31_t *max,
175 q31_t *increment, int8_t *shift)
176{
177 if (!target || !target->backend_api) {
178 return -ENOTSUP;
179 }
180
181 struct emul_sensor_driver_api *api = (struct emul_sensor_driver_api *)target->backend_api;
182
183 if (api->get_attribute_metadata == NULL) {
184 return -ENOTSUP;
185 }
186 return api->get_attribute_metadata(target, ch, attribute, min, max, increment, shift);
187}
188
Public APIs for the device emulation framework.
int32_t q31_t
32-bit fractional data type in 1.31 format.
Definition types.h:35
static int emul_sensor_backend_get_sample_range(const struct emul *target, struct sensor_chan_spec ch, q31_t *lower, q31_t *upper, q31_t *epsilon, int8_t *shift)
Query an emulator for a channel's supported sample value range and tolerance.
Definition emul_sensor.h:111
static int emul_sensor_backend_get_attribute_metadata(const struct emul *target, struct sensor_chan_spec ch, enum sensor_attribute attribute, q31_t *min, q31_t *max, q31_t *increment, int8_t *shift)
Get metadata about an attribute.
Definition emul_sensor.h:171
static bool emul_sensor_backend_is_supported(const struct emul *target)
Check if a given sensor emulator supports the backend API.
Definition emul_sensor.h:61
static int emul_sensor_backend_set_channel(const struct emul *target, struct sensor_chan_spec ch, const q31_t *value, int8_t shift)
Set an expected value for a given channel on a given sensor emulator.
Definition emul_sensor.h:78
static int emul_sensor_backend_set_attribute(const struct emul *target, struct sensor_chan_spec ch, enum sensor_attribute attribute, const void *value)
Set the emulator's attribute values.
Definition emul_sensor.h:137
sensor_attribute
Sensor attribute types.
Definition sensor.h:384
#define ENOTSUP
Unsupported value.
Definition errno.h:115
#define NULL
Definition iar_missing_defs.h:20
#define min(a, b)
Return smaller value of two provided expressions.
Definition minmax.h:81
#define max(a, b)
Return larger value of two provided expressions.
Definition minmax.h:65
Main header file for sensor driver API.
__INT8_TYPE__ int8_t
Definition stdint.h:72
An emulator instance - represents the target emulated device/peripheral that is interacted with throu...
Definition emul.h:88
const void * backend_api
Address of the API structure exposed by the emulator instance.
Definition emul.h:109
Sensor Channel Specification.
Definition sensor.h:577