Zephyr API Documentation  3.6.0
A Scalable Open Source RTOS
3.6.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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
28__subsystem struct emul_sensor_backend_api {
30 int (*set_channel)(const struct emul *target, enum sensor_channel ch, const q31_t *value,
31 int8_t shift);
33 int (*get_sample_range)(const struct emul *target, enum sensor_channel ch, q31_t *lower,
34 q31_t *upper, q31_t *epsilon, int8_t *shift);
36 int (*set_attribute)(const struct emul *target, enum sensor_channel ch,
37 enum sensor_attribute attribute, const void *value);
39 int (*get_attribute_metadata)(const struct emul *target, enum sensor_channel ch,
40 enum sensor_attribute attribute, q31_t *min, q31_t *max,
41 q31_t *increment, int8_t *shift);
42};
54static inline bool emul_sensor_backend_is_supported(const struct emul *target)
55{
56 return target && target->backend_api;
57}
58
71static inline int emul_sensor_backend_set_channel(const struct emul *target, enum sensor_channel ch,
72 const q31_t *value, int8_t shift)
73{
74 if (!target || !target->backend_api) {
75 return -ENOTSUP;
76 }
77
78 struct emul_sensor_backend_api *api = (struct emul_sensor_backend_api *)target->backend_api;
79
80 if (api->set_channel) {
81 return api->set_channel(target, ch, value, shift);
82 }
83 return -ENOTSUP;
84}
85
103static inline int emul_sensor_backend_get_sample_range(const struct emul *target,
104 enum sensor_channel ch, q31_t *lower,
105 q31_t *upper, q31_t *epsilon, int8_t *shift)
106{
107 if (!target || !target->backend_api) {
108 return -ENOTSUP;
109 }
110
111 struct emul_sensor_backend_api *api = (struct emul_sensor_backend_api *)target->backend_api;
112
113 if (api->get_sample_range) {
114 return api->get_sample_range(target, ch, lower, upper, epsilon, shift);
115 }
116 return -ENOTSUP;
117}
118
129static inline int emul_sensor_backend_set_attribute(const struct emul *target,
130 enum sensor_channel ch,
131 enum sensor_attribute attribute,
132 const void *value)
133{
134 if (!target || !target->backend_api) {
135 return -ENOTSUP;
136 }
137
138 struct emul_sensor_backend_api *api = (struct emul_sensor_backend_api *)target->backend_api;
139
140 if (api->set_attribute == NULL) {
141 return -ENOTSUP;
142 }
143 return api->set_attribute(target, ch, attribute, value);
144}
145
163static inline int emul_sensor_backend_get_attribute_metadata(const struct emul *target,
164 enum sensor_channel ch,
165 enum sensor_attribute attribute,
166 q31_t *min, q31_t *max,
167 q31_t *increment, int8_t *shift)
168{
169 if (!target || !target->backend_api) {
170 return -ENOTSUP;
171 }
172
173 struct emul_sensor_backend_api *api = (struct emul_sensor_backend_api *)target->backend_api;
174
175 if (api->get_attribute_metadata == NULL) {
176 return -ENOTSUP;
177 }
178 return api->get_attribute_metadata(target, ch, attribute, min, max, increment, shift);
179}
180
int32_t q31_t
32-bit fractional data type in 1.31 format.
Definition: types.h:35
static int emul_sensor_backend_set_attribute(const struct emul *target, enum sensor_channel ch, enum sensor_attribute attribute, const void *value)
Set the emulator's attribute values.
Definition: emul_sensor.h:129
static int emul_sensor_backend_set_channel(const struct emul *target, enum sensor_channel 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:71
static int emul_sensor_backend_get_attribute_metadata(const struct emul *target, enum sensor_channel 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:163
static int emul_sensor_backend_get_sample_range(const struct emul *target, enum sensor_channel 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:103
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:54
sensor_attribute
Sensor attribute types.
Definition: sensor.h:290
sensor_channel
Sensor channels.
Definition: sensor.h:59
#define ENOTSUP
Unsupported value.
Definition: errno.h:115
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:78
const void * backend_api
Address of the API structure exposed by the emulator instance.
Definition: emul.h:97