Zephyr Project API 4.2.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
9
10#include <stdint.h>
11
29__subsystem struct emul_sensor_driver_api {
31 int (*set_channel)(const struct emul *target, struct sensor_chan_spec ch,
32 const q31_t *value, int8_t shift);
34 int (*get_sample_range)(const struct emul *target, struct sensor_chan_spec ch, q31_t *lower,
35 q31_t *upper, q31_t *epsilon, int8_t *shift);
37 int (*set_attribute)(const struct emul *target, struct sensor_chan_spec ch,
38 enum sensor_attribute attribute, const void *value);
40 int (*get_attribute_metadata)(const struct emul *target, struct sensor_chan_spec ch,
41 enum sensor_attribute attribute, q31_t *min, q31_t *max,
42 q31_t *increment, int8_t *shift);
43};
55static inline bool emul_sensor_backend_is_supported(const struct emul *target)
56{
57 return target && target->backend_api;
58}
59
72static inline int emul_sensor_backend_set_channel(const struct emul *target,
73 struct sensor_chan_spec ch, const q31_t *value,
74 int8_t shift)
75{
76 if (!target || !target->backend_api) {
77 return -ENOTSUP;
78 }
79
80 struct emul_sensor_driver_api *api = (struct emul_sensor_driver_api *)target->backend_api;
81
82 if (api->set_channel) {
83 return api->set_channel(target, ch, value, shift);
84 }
85 return -ENOTSUP;
86}
87
105static inline int emul_sensor_backend_get_sample_range(const struct emul *target,
106 struct sensor_chan_spec ch, q31_t *lower,
107 q31_t *upper, q31_t *epsilon, int8_t *shift)
108{
109 if (!target || !target->backend_api) {
110 return -ENOTSUP;
111 }
112
113 struct emul_sensor_driver_api *api = (struct emul_sensor_driver_api *)target->backend_api;
114
115 if (api->get_sample_range) {
116 return api->get_sample_range(target, ch, lower, upper, epsilon, shift);
117 }
118 return -ENOTSUP;
119}
120
131static inline int emul_sensor_backend_set_attribute(const struct emul *target,
132 struct sensor_chan_spec ch,
133 enum sensor_attribute attribute,
134 const void *value)
135{
136 if (!target || !target->backend_api) {
137 return -ENOTSUP;
138 }
139
140 struct emul_sensor_driver_api *api = (struct emul_sensor_driver_api *)target->backend_api;
141
142 if (api->set_attribute == NULL) {
143 return -ENOTSUP;
144 }
145 return api->set_attribute(target, ch, attribute, value);
146}
147
165static inline int emul_sensor_backend_get_attribute_metadata(const struct emul *target,
166 struct sensor_chan_spec ch,
167 enum sensor_attribute attribute,
168 q31_t *min, q31_t *max,
169 q31_t *increment, int8_t *shift)
170{
171 if (!target || !target->backend_api) {
172 return -ENOTSUP;
173 }
174
175 struct emul_sensor_driver_api *api = (struct emul_sensor_driver_api *)target->backend_api;
176
177 if (api->get_attribute_metadata == NULL) {
178 return -ENOTSUP;
179 }
180 return api->get_attribute_metadata(target, ch, attribute, min, max, increment, shift);
181}
182
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:105
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:165
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:55
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:72
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:131
sensor_attribute
Sensor attribute types.
Definition sensor.h:313
#define ENOTSUP
Unsupported value.
Definition errno.h:114
#define NULL
Definition iar_missing_defs.h:20
Public APIs for the sensor driver.
__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:82
const void * backend_api
Address of the API structure exposed by the emulator instance.
Definition emul.h:103
Sensor Channel Specification.
Definition sensor.h:451