Dawn Framework 1.0
Universal data acquisition framework for embedded systems
sensor.hxx
1// dawn/include/dawn/io/sensor.hxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#pragma once
7
8#include "dawn/io/common.hxx"
9#include "dawn/porting/config.hxx"
10#include "dawn/porting/sensors.hxx"
11
12namespace dawn
13{
24 : public CIOCommon
25 , public CIOSensorPorting
26{
27public:
28 enum
29 {
30 IO_SENSOR_CFG_FIRST = 0,
41 IO_SENSOR_CFG_LAST = 31
42 };
43
44 static_assert(IO_SENSOR_CFG_LAST - 1 <= SObjectCfg::ID_MAX);
45
46 explicit CIOSensor(CDescObject &desc)
47 : CIOCommon(desc)
48 , info(getSensorInfo(getCls()))
49 , dsize(4)
52 , fd(-1)
53 {
54 }
55
56 ~CIOSensor() override;
57
58#ifdef CONFIG_DAWN_OBJECT_HAS_NAME
59 const char *getClassNameStr() const override
60 {
61 switch (getCls())
62 {
64 return "accel";
66 return "mag";
68 return "gyro";
70 return "light";
72 return "baro";
74 return "prox";
76 return "humid";
78 return "temp";
80 return "atemp";
82 return "rgb";
84 return "ir";
86 return "uv";
88 return "gas";
90 return "co2";
92 return "tvoc";
94 return "gnss";
96 return "gnss_time";
98 return "gnss_info";
100 return "gnss_sats";
101 default:
102 return "sensor";
103 }
104 }
105#endif
106
107 int configure() override;
108 int deinit() override;
109 int getDataImpl(IODataCmn &data, size_t len) override;
110
111#ifdef CONFIG_DAWN_IO_NOTIFY
112 int getFd() const override;
113#endif
114
115 size_t getDataSize() const override;
116 size_t getDataDim() const override;
117
118 bool isRead() const override
119 {
120 return true;
121 };
122
123 bool isWrite() const override
124 {
125 return false;
126 };
127
128 bool isNotify() const override
129 {
130 return true;
131 };
132
133 bool isBatch() const override
134 {
135 return false;
136 };
137
138 constexpr static SObjectId::ObjectId objectId(uint16_t cls, uint8_t dtype, bool ts, uint16_t inst)
139 {
140 uint8_t flags = 0;
141
142#ifdef CONFIG_DAWN_IO_TIMESTAMP
143 if (ts)
144 {
145 flags |= CIOCommon::IO_FLAGS_TS;
146 }
147#else
148 DAWNASSERT(ts == false, "ts not supported");
149#endif
150
151 return SObjectId::objectId(SObjectId::OBJTYPE_IO, cls, dtype, flags, inst);
152 }
153
154 constexpr static SObjectId::ObjectId objectIdAccel(uint8_t dtype, bool ts, uint16_t inst)
155 {
156 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_ACCELEROMETER, dtype, ts, inst);
157 }
158
159 constexpr static SObjectId::ObjectId objectIdMag(uint8_t dtype, bool ts, uint16_t inst)
160 {
161 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_MAGNETICFIELD, dtype, ts, inst);
162 }
163
164 constexpr static SObjectId::ObjectId objectIdGyro(uint8_t dtype, bool ts, uint16_t inst)
165 {
166 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_GYROSCOPE, dtype, ts, inst);
167 }
168
169 constexpr static SObjectId::ObjectId objectIdLight(uint8_t dtype, bool ts, uint16_t inst)
170 {
171 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_LIGHT, dtype, ts, inst);
172 }
173
174 constexpr static SObjectId::ObjectId objectIdBaro(uint8_t dtype, bool ts, uint16_t inst)
175 {
176 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_BAROMETER, dtype, ts, inst);
177 }
178
179 constexpr static SObjectId::ObjectId objectIdProx(uint8_t dtype, bool ts, uint16_t inst)
180 {
181 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_PROXIMITY, dtype, ts, inst);
182 }
183
184 constexpr static SObjectId::ObjectId objectIdHum(uint8_t dtype, bool ts, uint16_t inst)
185 {
186 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_HUMIDITY, dtype, ts, inst);
187 }
188
189 constexpr static SObjectId::ObjectId objectIdTemp(uint8_t dtype, bool ts, uint16_t inst)
190 {
191 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_TEMPERATURE, dtype, ts, inst);
192 }
193
194 constexpr static SObjectId::ObjectId objectIdAtemp(uint8_t dtype, bool ts, uint16_t inst)
195 {
196 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_ATEMPERATURE, dtype, ts, inst);
197 }
198
199 constexpr static SObjectId::ObjectId objectIdRgb(uint8_t dtype, bool ts, uint16_t inst)
200 {
201 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_RGB, dtype, ts, inst);
202 }
203
204 constexpr static SObjectId::ObjectId objectIdIr(uint8_t dtype, bool ts, uint16_t inst)
205 {
206 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_IR, dtype, ts, inst);
207 }
208
209 constexpr static SObjectId::ObjectId objectIdUv(uint8_t dtype, bool ts, uint16_t inst)
210 {
211 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_UV, dtype, ts, inst);
212 }
213
214 constexpr static SObjectId::ObjectId objectIdGas(uint8_t dtype, bool ts, uint16_t inst)
215 {
216 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_GAS, dtype, ts, inst);
217 }
218
219 constexpr static SObjectId::ObjectId objectIdCo2(uint8_t dtype, bool ts, uint16_t inst)
220 {
221 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_CO2, dtype, ts, inst);
222 }
223
224 constexpr static SObjectId::ObjectId objectIdTvoc(uint8_t dtype, bool ts, uint16_t inst)
225 {
226 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_TVOC, dtype, ts, inst);
227 }
228
229 constexpr static SObjectId::ObjectId objectIdGnss(uint8_t dtype, bool ts, uint16_t inst)
230 {
231 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_GNSS, dtype, ts, inst);
232 }
233
234 constexpr static SObjectId::ObjectId objectIdGnssTime(uint8_t dtype, bool ts, uint16_t inst)
235 {
236 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_GNSS_TIME, dtype, ts, inst);
237 }
238
239 constexpr static SObjectId::ObjectId objectIdGnssInfo(uint8_t dtype, bool ts, uint16_t inst)
240 {
241 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_GNSS_INFO, dtype, ts, inst);
242 }
243
244 constexpr static SObjectId::ObjectId objectIdGnssSats(uint8_t dtype, bool ts, uint16_t inst)
245 {
246 return CIOSensor::objectId(CIOCommon::IO_CLASS_SENSOR_GNSS_SATELLITES, dtype, ts, inst);
247 }
248
249 constexpr static SObjectCfg::ObjectCfgId cfgIdUpdateInterval()
250 {
254 true,
255 1,
257 }
258
259 constexpr static SObjectCfg::ObjectCfgId cfgIdMeasurementPeriod()
260 {
264 true,
265 1,
267 }
268
269protected:
270 // Must fit the largest NuttX sensor event struct read in one go. The GNSS
271 // event (struct sensor_gnss) is the largest at ~72 bytes; keep headroom.
272 constexpr static const size_t DATA_BUFFER_SIZE = 96;
273
274 const SIOSensorMapInfo *info;
275 size_t dsize;
276 uint32_t updateInterval;
278 char path[PATH_MAX] = {};
279 int fd;
280
281 // Validate the configured dtype and set dsize (bytes per element). The base
282 // accepts only the NuttX float sensor_data_t; GNSS subclasses override this
283 // to also accept integer time/satellite fields.
284 virtual int validateDtype();
285
286 int configureDesc(const CDescObject &desc);
287};
288} // Namespace dawn
Descriptor wrapper for individual object configuration.
Base class for all I/O objects.
Definition common.hxx:27
virtual int getFd() const
Get file descriptor for notifications.
Definition common.hxx:447
@ IO_FLAGS_TS
Timestamp support flag.
Definition common.hxx:76
@ IO_CLASS_SENSOR_MAGNETICFIELD
Magnetic field sensor.
Definition common.hxx:114
@ IO_CLASS_ANY
Any I/O class.
Definition common.hxx:86
@ IO_CLASS_SENSOR_LIGHT
Light sensor.
Definition common.hxx:116
@ IO_CLASS_SENSOR_CO2
CO2 concentration sensor.
Definition common.hxx:126
@ IO_CLASS_SENSOR_BAROMETER
Barometer sensor.
Definition common.hxx:117
@ IO_CLASS_SENSOR_HUMIDITY
Humidity sensor.
Definition common.hxx:119
@ IO_CLASS_SENSOR_PROXIMITY
Proximity sensor.
Definition common.hxx:118
@ IO_CLASS_SENSOR_GNSS_TIME
GNSS UTC time (seconds since epoch)
Definition common.hxx:151
@ IO_CLASS_SENSOR_TEMPERATURE
Temperature sensor.
Definition common.hxx:120
@ IO_CLASS_SENSOR_GNSS_INFO
GNSS accuracy + DOP (eph/epv/hdop/pdop/vdop)
Definition common.hxx:152
@ IO_CLASS_SENSOR_UV
Ultraviolet sensor.
Definition common.hxx:124
@ IO_CLASS_SENSOR_GNSS
GNSS position+velocity (lat/lon/alt/speed/course)
Definition common.hxx:150
@ IO_CLASS_SENSOR_ACCELEROMETER
Accelerometer sensor.
Definition common.hxx:113
@ IO_CLASS_SENSOR_RGB
RGB color sensor.
Definition common.hxx:122
@ IO_CLASS_SENSOR_GYROSCOPE
Gyroscope sensor.
Definition common.hxx:115
@ IO_CLASS_SENSOR_GNSS_SATELLITES
GNSS satellites used in fix.
Definition common.hxx:153
@ IO_CLASS_SENSOR_IR
Infrared sensor.
Definition common.hxx:123
@ IO_CLASS_SENSOR_ATEMPERATURE
Ambient temperature sensor.
Definition common.hxx:121
@ IO_CLASS_SENSOR_TVOC
TVOC concentration sensor.
Definition common.hxx:127
@ IO_CLASS_SENSOR_GAS
Gas sensor.
Definition common.hxx:125
Generic sensor interface for various sensor types.
Definition sensor.hxx:26
uint32_t measurementPeriod
Sensor measurement period in milliseconds.
Definition sensor.hxx:277
int fd
File descriptor for sensor device.
Definition sensor.hxx:279
bool isNotify() const
Check if IO supports notifications.
Definition sensor.hxx:128
bool isWrite() const
Check if IO supports write operations.
Definition sensor.hxx:123
int deinit()
De-initialize object.
Definition sensor.cxx:170
@ IO_SENSOR_CFG_GAIN
Analog gain setting.
Definition sensor.hxx:33
@ IO_SENSOR_CFG_UNIT
Measurement unit selection.
Definition sensor.hxx:32
@ IO_SENSOR_CFG_ORIENTATION
Sensor orientation/mounting angle.
Definition sensor.hxx:39
@ IO_SENSOR_CFG_ALERT
Alert threshold configuration.
Definition sensor.hxx:40
@ IO_SENSOR_CFG_BANDWIDTHLP
Low-pass filter bandwidth.
Definition sensor.hxx:37
@ IO_SENSOR_CFG_MEASPERIOD
Measurement period.
Definition sensor.hxx:36
@ IO_SENSOR_CFG_SCALE
Scale/multiplier for raw sensor values.
Definition sensor.hxx:34
@ IO_SENSOR_CFG_BANDWIDTHHP
High-pass filter bandwidth.
Definition sensor.hxx:38
@ IO_SENSOR_CFG_UPDATEINTERVAL
Data update interval in milliseconds.
Definition sensor.hxx:35
@ IO_SENSOR_CFG_ENABLE
Sensor enable/disable configuration.
Definition sensor.hxx:31
int getDataImpl(IODataCmn &data, size_t len)
Get data implementation (override in derived classes).
Definition sensor.cxx:178
bool isBatch() const
Check if IO supports batch operations.
Definition sensor.hxx:133
size_t getDataSize() const
Get data size in bytes.
Definition sensor.cxx:222
size_t dsize
Sensor data size in bytes.
Definition sensor.hxx:275
uint32_t updateInterval
Sensor update interval in milliseconds.
Definition sensor.hxx:276
char path[PATH_MAX]
Sensor device file path.
Definition sensor.hxx:278
const SIOSensorMapInfo * info
Sensor metadata.
Definition sensor.hxx:274
bool isRead() const
Check if IO supports read operations.
Definition sensor.hxx:118
int configure()
Configure object from descriptor data.
Definition sensor.cxx:103
size_t getDataDim() const
Get data vector dimension.
Definition sensor.cxx:227
uint16_t getCls() const
Get object class field.
Definition object.cxx:170
static ObjectCfgId objectCfg(uint8_t type, uint16_t cls, uint8_t dtype, bool rw, uint16_t size, uint8_t id)
Construct 32-bit ConfigID from component fields.
uint32_t ObjectCfgId
ConfigID type - single 32-bit value.
Definition objectcfg.hxx:60
static size_t ID_MAX
Configuration ID field constants and bit shift positions.
Definition objectcfg.hxx:45
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
@ OBJTYPE_IO
Input/Output object type.
Definition objectid.hxx:184
@ DTYPE_UINT32
Unsigned 32-bit integer (0 to 4294967295).
Definition objectid.hxx:96
uint32_t ObjectId
ObjectID type - single 32-bit value.
Definition objectid.hxx:44
static ObjectId objectId(uint8_t type, uint16_t cls, uint8_t dtype, uint8_t flags, uint16_t priv)
Construct 32-bit ObjectID from component fields.
Definition objectid.hxx:290