Dawn Framework 1.0
Universal data acquisition framework for embedded systems
common.hxx
1// dawn/include/dawn/io/common.hxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#pragma once
7
8#include <cstdlib>
9
10#include "dawn/common/object.hxx"
11#include "dawn/debug.hxx"
12#include "dawn/io/ddata.hxx"
13#include "dawn/io/inotifier.hxx"
14#include "dawn/io/limits.hxx"
15#include "dawn/porting/config.hxx"
16
17namespace dawn
18{
26class CIOCommon : public CObject
27{
28public:
29#ifdef CONFIG_DAWN_IO_HAS_STATS
36 struct SIOStats
37 {
38 uint32_t read_count;
39 uint32_t write_count;
40 uint32_t error_count;
41 } typedef IOStats;
42
49 const IOStats &getStats() const
50 {
51 return stats;
52 }
53
58 void resetStats()
59 {
60 stats.read_count = 0;
61 stats.write_count = 0;
62 stats.error_count = 0;
63 }
64#endif // CONFIG_DAWN_IO_HAS_STATS
65
73 enum
74 {
76 IO_FLAGS_TS = (1 << 0),
77
80 } typedef EIOFlags;
81
84 enum
85 {
87
88 // Special purpose IOs
89
96
97 // Dummy
98
100
101 // Number generators
102
106
107 // File I/O
108
110
111 // Sensors
112
126
127 // Sensor producers
128
143
144 // there is 59 sensor types in Nuttx now, most likely it grows later
145 // we still have many free slots, but in the future, it there are no free IO slots
146 // we can thing about combining sensor and sensor producer
147
148 // System information
149
158
159 // Slots 49-59 reserved for future system classes (serial number,
160 // firmware version, firmware update, etc.).
161
162 // GPIO
163
169
170 // PWM output
171
173
174 // ADC/DAC
175
180
181 // Ecnoder input
182
185
186 // Virtual IO
187
189
190 // User defined
191
194
196 } typedef EIOClass;
197
198 static_assert(IO_CLASS_LAST - 1 <= SObjectId::CLS_MAX);
199
208 enum
209 {
216 IO_CFG_LAST = 31
217 };
218
221 enum
222 {
225 } typedef EIONotifyType;
226
234 template<uint16_t CLASS_ID, uint8_t DEFAULT_DTYPE>
236 {
237 public:
246 constexpr static SObjectId::ObjectId create(bool ts, uint16_t inst)
247 {
248 uint8_t flags = 0;
249
250#ifdef CONFIG_DAWN_IO_TIMESTAMP
251 if (ts)
252 {
253 flags |= CIOCommon::IO_FLAGS_TS;
254 }
255#else
256 DAWNASSERT(ts == false, "ts not supported");
257#endif
258
259 return SObjectId::objectId(SObjectId::OBJTYPE_IO, CLASS_ID, DEFAULT_DTYPE, flags, inst);
260 }
261
272 bool ts,
273 uint16_t inst)
274 {
275 uint8_t flags = 0;
276
277#ifdef CONFIG_DAWN_IO_TIMESTAMP
278 if (ts)
279 {
280 flags |= CIOCommon::IO_FLAGS_TS;
281 }
282#else
283 DAWNASSERT(ts == false, "ts not supported");
284#endif
285
286 return SObjectId::objectId(SObjectId::OBJTYPE_IO, CLASS_ID, dtype, flags, inst);
287 }
288 };
289
301 template<uint16_t CLASS_ID, uint8_t DEFAULT_DTYPE>
303 {
304 public:
312 constexpr static SObjectId::ObjectId create(uint16_t inst)
313 {
315 }
316
325 constexpr static SObjectId::ObjectId create(SObjectId::EObjectDataType dtype, uint16_t inst)
326 {
327 return IOObjectIdHelper<CLASS_ID, DEFAULT_DTYPE>::create(dtype, false, inst);
328 }
329 };
330
337 explicit CIOCommon(CDescObject &desc);
338
353 int getData(IODataCmn &data, size_t len, size_t offset = 0)
354 {
355 int ret;
356
357#ifdef CONFIG_DAWN_IO_SEEKABLE
358 ret = isSeekable() ? getDataAtImpl(data, len, offset)
359 : (offset == 0 ? getDataImpl(data, len) : -ENOTSUP);
360#else
361 ret = (offset == 0) ? getDataImpl(data, len) : -ENOTSUP;
362#endif
363#ifdef CONFIG_DAWN_IO_HAS_STATS
364 if (ret == OK)
365 {
366 if (offset == 0)
367 {
368 stats.read_count++;
369 }
370 }
371 else if (ret != -ENOTSUP)
372 {
373 stats.error_count++;
374 }
375#endif
376 return ret;
377 }
378
392 int setData(IODataCmn &data, size_t offset = 0)
393 {
394 int ret;
395
396#ifdef CONFIG_DAWN_IO_SEEKABLE
397 ret = isSeekable() ? setDataAtImpl(data, offset) : (offset == 0 ? setDataImpl(data) : -ENOTSUP);
398#else
399 ret = (offset == 0) ? setDataImpl(data) : -ENOTSUP;
400#endif
401#ifdef CONFIG_DAWN_IO_HAS_STATS
402 if (ret == OK)
403 {
404 if (offset == 0)
405 {
406 stats.write_count++;
407 }
408 }
409 else if (ret != -ENOTSUP)
410 {
411 stats.error_count++;
412 }
413#endif
414 return ret;
415 }
416
425 virtual int getFd() const
426 {
427 return -ENOTSUP;
428 };
429
438 virtual size_t getDataSize() const = 0;
439
449 virtual size_t getDataDim() const = 0;
450
457 virtual bool isRead() const = 0;
458
465 virtual bool isWrite() const = 0;
466
473 virtual bool isNotify() const = 0;
474
481 virtual bool isBatch() const = 0;
482
491 bool isTimestamp() const;
492
502 virtual bool isSeekable() const
503 {
504 return false;
505 }
506
507#ifdef CONFIG_DAWN_IO_NOTIFY
517 void bindNotifier(IIONotifier *n);
518
531 int setNotifier(IIONotifier::notifier_cb_t cb, int prio, void *priv);
532#endif
533
554 io_ddata_t *ddata_alloc(size_t batch, size_t chunk_size = 0);
555
567 int getConfig(uint32_t cfgid, uint32_t *data, size_t len)
568 {
569 return this->getObjConfig(cfgid, data, len);
570 };
571
583 int setConfig(uint32_t cfgid, uint32_t *data, size_t len)
584 {
585 return this->setObjConfig(cfgid, data, len);
586 };
587
600 constexpr static SObjectCfg::ObjectCfgId cfgIdCmn(bool rw,
601 uint8_t dtype,
602 uint8_t size,
603 uint8_t id)
604 {
606 SObjectId::OBJTYPE_IO, CIOCommon::IO_CLASS_ANY, dtype, rw, size, id);
607 }
608
616 constexpr static SObjectCfg::ObjectCfgId cfgIdDevno(bool rw = false)
617 {
619 }
620
629 constexpr static SObjectCfg::ObjectCfgId cfgIdLimitMin(uint8_t dtype, uint16_t size)
630 {
631 return CIOCommon::cfgIdCmn(false, dtype, size, IO_CFG_LIMIT_MIN);
632 }
633
642 constexpr static SObjectCfg::ObjectCfgId cfgIdLimitMax(uint8_t dtype, uint16_t size)
643 {
644 return CIOCommon::cfgIdCmn(false, dtype, size, IO_CFG_LIMIT_MAX);
645 }
646
655 constexpr static SObjectCfg::ObjectCfgId cfgIdLimitStep(uint8_t dtype, uint16_t size)
656 {
657 return CIOCommon::cfgIdCmn(false, dtype, size, IO_CFG_LIMIT_STEP);
658 }
659
667 constexpr static SObjectCfg::ObjectCfgId cfgIdNotify(bool rw = false)
668 {
670 }
671
672#ifdef CONFIG_DAWN_IO_NOTIFY
679 uint8_t getNotifyType() const
680 {
681 return notifyType;
682 }
683
690 int getNotifyPrio() const
691 {
692 return notifyPrio;
693 }
694
701 size_t getNotifyBatch() const
702 {
703 return notifyBatch;
704 }
705#endif
706
707protected:
708#ifdef CONFIG_DAWN_IO_NOTIFY
711 IIONotifier *notifier;
712#endif
713
722 virtual int getDataImpl(IODataCmn &data, size_t len)
723 {
724 (void)data;
725 (void)len;
726 return -ENOTSUP;
727 }
728
736 virtual int setDataImpl(IODataCmn &data)
737 {
738 (void)data;
739 return -ENOTSUP;
740 }
741
755 virtual int getDataAtImpl(IODataCmn &data, size_t len, size_t offset)
756 {
757 (void)data;
758 (void)len;
759 (void)offset;
760 return -ENOTSUP;
761 }
762
774 virtual int setDataAtImpl(IODataCmn &data, size_t offset)
775 {
776 (void)data;
777 (void)offset;
778 return -ENOTSUP;
779 }
780
781#ifdef CONFIG_DAWN_IO_NOTIFY
789 int notifyData(io_ddata_t *data);
790#endif
791
798 int getCmnDevno() const
799 {
800 return this->devno;
801 }
802
807 const uint32_t *getCmnLimitMin()
808 {
809 return this->limits.getMin();
810 }
811
816 const uint32_t *getCmnLimitMax()
817 {
818 return this->limits.getMax();
819 }
820
825 const uint32_t *getCmnLimitStep()
826 {
827 return this->limits.getStep();
828 }
829
835 {
836 return this->limits.getWords();
837 }
838
843 const CIOLimits &getCmnLimits() const
844 {
845 return this->limits;
846 }
847
855 size_t cfgCmnOffset(const SObjectCfg::SObjectCfgItem *cfg);
856
863 uint64_t getTimestamp();
864
865private:
866 int devno;
867 CIOLimits limits;
868#ifdef CONFIG_DAWN_IO_NOTIFY
869 uint8_t notifyType;
870 int notifyPrio;
871 size_t notifyBatch;
872#endif
873#ifdef CONFIG_DAWN_IO_HAS_STATS
874 IOStats stats;
875#endif
876
886 int configureDesc(const CDescObject &desc);
887};
888} // Namespace dawn
Descriptor wrapper for individual object configuration.
Template helper for creating ObjectIDs for I/O types without timestamp support.
Definition common.hxx:303
static SObjectId::ObjectId create(SObjectId::EObjectDataType dtype, uint16_t inst)
Create ObjectID with custom data type and no timestamp.
Definition common.hxx:325
static SObjectId::ObjectId create(uint16_t inst)
Create ObjectID with fixed data type and no timestamp.
Definition common.hxx:312
Template helper for creating ObjectIDs for I/O types.
Definition common.hxx:236
static SObjectId::ObjectId create(bool ts, uint16_t inst)
Create ObjectID with default data type.
Definition common.hxx:246
static SObjectId::ObjectId create(SObjectId::EObjectDataType dtype, bool ts, uint16_t inst)
Create ObjectID with custom data type.
Definition common.hxx:271
Base class for all I/O objects.
Definition common.hxx:27
virtual bool isWrite() const =0
Check if IO supports write operations.
static SObjectCfg::ObjectCfgId cfgIdLimitMax(uint8_t dtype, uint16_t size)
Create ConfigID for maximum I/O limits.
Definition common.hxx:642
int getConfig(uint32_t cfgid, uint32_t *data, size_t len)
Get I/O configuration item.
Definition common.hxx:567
int setData(IODataCmn &data, size_t offset=0)
Set data for I/O (public interface with stats tracking).
Definition common.hxx:392
virtual bool isBatch() const =0
Check if IO supports batch operations.
size_t getCmnLimitWords()
Get number of 32-bit words in I/O limit arrays.
Definition common.hxx:834
virtual int getDataAtImpl(IODataCmn &data, size_t len, size_t offset)
Get data at byte offset (override in seekable IOs).
Definition common.hxx:755
int getData(IODataCmn &data, size_t len, size_t offset=0)
Get data from I/O (public interface with stats tracking).
Definition common.hxx:353
const CIOLimits & getCmnLimits() const
Get common IO limits container.
Definition common.hxx:843
static SObjectCfg::ObjectCfgId cfgIdLimitStep(uint8_t dtype, uint16_t size)
Create ConfigID for step I/O limits.
Definition common.hxx:655
const uint32_t * getCmnLimitMin()
Get minimum I/O limit words.
Definition common.hxx:807
static SObjectCfg::ObjectCfgId cfgIdCmn(bool rw, uint8_t dtype, uint8_t size, uint8_t id)
Create ConfigID for I/O common configuration.
Definition common.hxx:600
@ IO_CFG_LIMIT_STEP
Step limit words.
Definition common.hxx:214
@ IO_CFG_NOTIFY
Notifier configuration (type + priority)
Definition common.hxx:215
@ IO_CFG_FIRST
First config ID.
Definition common.hxx:210
@ IO_CFG_LIMIT_MIN
Minimum limit words.
Definition common.hxx:212
@ IO_CFG_LAST
Last config ID.
Definition common.hxx:216
@ IO_CFG_LIMIT_MAX
Maximum limit words.
Definition common.hxx:213
@ IO_CFG_DEVNO
Device number configuration.
Definition common.hxx:211
const uint32_t * getCmnLimitMax()
Get maximum I/O limit words.
Definition common.hxx:816
static SObjectCfg::ObjectCfgId cfgIdDevno(bool rw=false)
Create ConfigID for device number configuration.
Definition common.hxx:616
virtual bool isNotify() const =0
Check if IO supports notifications.
virtual int getFd() const
Get file descriptor for notifications.
Definition common.hxx:425
uint64_t getTimestamp()
Get current timestamp.
Definition common.cxx:194
virtual bool isSeekable() const
Check if IO supports partial (seekable) access.
Definition common.hxx:502
bool isTimestamp() const
Check if I/O supports timestamp.
Definition common.cxx:189
virtual int setDataAtImpl(IODataCmn &data, size_t offset)
Set data at byte offset (override in seekable IOs).
Definition common.hxx:774
EIONotifyType
Notifier type enumeration.
Definition common.hxx:222
@ IO_NOTIFY_POLL
Poll-based notifier (default)
Definition common.hxx:223
@ IO_NOTIFY_STREAM
Stream notifier (blocking read, one per IO)
Definition common.hxx:224
int getCmnDevno() const
Get device number for this I/O.
Definition common.hxx:798
virtual int setDataImpl(IODataCmn &data)
Set data implementation (override in derived classes).
Definition common.hxx:736
virtual size_t getDataDim() const =0
Get data vector dimension.
io_ddata_t * ddata_alloc(size_t batch, size_t chunk_size=0)
Allocate data buffer for this I/O.
Definition common.cxx:247
virtual size_t getDataSize() const =0
Get data size in bytes.
EIOFlags
I/O common flags.
Definition common.hxx:74
@ IO_FLAGS_TS
Timestamp support flag.
Definition common.hxx:76
@ IO_FLAGS_NONE
No flags.
Definition common.hxx:75
virtual int getDataImpl(IODataCmn &data, size_t len)
Get data implementation (override in derived classes).
Definition common.hxx:722
virtual bool isRead() const =0
Check if IO supports read operations.
int setConfig(uint32_t cfgid, uint32_t *data, size_t len)
Set I/O configuration item.
Definition common.hxx:583
size_t cfgCmnOffset(const SObjectCfg::SObjectCfgItem *cfg)
Get offset of configuration item in descriptor.
Definition common.cxx:144
EIOClass
I/O object class types.
Definition common.hxx:85
@ IO_CLASS_SENSOR_PRODUCER_MAGNETICFIELD
Magnetic field publisher.
Definition common.hxx:130
@ IO_CLASS_SENSOR_MAGNETICFIELD
Magnetic field sensor.
Definition common.hxx:114
@ IO_CLASS_SENSOR_PRODUCER_ACCELEROMETER
Accelerometer publisher.
Definition common.hxx:129
@ IO_CLASS_DAC
Digital-to-analog converter.
Definition common.hxx:176
@ IO_CLASS_TRIGGER
Trigger I/O.
Definition common.hxx:91
@ IO_CLASS_PWM
PWM output.
Definition common.hxx:172
@ IO_CLASS_SYSTEM_RESETCAUSE
Reset cause.
Definition common.hxx:153
@ IO_CLASS_ADC_STREAM
ADC stream (batch/high-throughput)
Definition common.hxx:179
@ IO_CLASS_VIRT
Virtual I/O.
Definition common.hxx:188
@ IO_CLASS_SYSTEM_UPTIME
System uptime.
Definition common.hxx:150
@ IO_CLASS_SYSTEM_POWEROFF
Power off control.
Definition common.hxx:154
@ IO_CLASS_USER_START
User-defined types start here.
Definition common.hxx:192
@ IO_CLASS_GPI_SINGLE
Single GPIO input.
Definition common.hxx:164
@ IO_CLASS_SENSOR_PRODUCER_LIGHT
Light publisher.
Definition common.hxx:132
@ IO_CLASS_SENSOR_PRODUCER_CONFIG
Sensor producer configuration IDs.
Definition common.hxx:142
@ IO_CLASS_SENSOR_PRODUCER_ATEMPERATURE
Ambient temperature publisher.
Definition common.hxx:137
@ IO_CLASS_ANY
Any I/O class.
Definition common.hxx:86
@ IO_CLASS_SYSTEM_SYSTEMTIME
System time.
Definition common.hxx:157
@ IO_CLASS_SENSOR_PRODUCER_HUMIDITY
Humidity publisher.
Definition common.hxx:135
@ IO_CLASS_ENCODER_INDEX
Quadrature encoder (position+index)
Definition common.hxx:184
@ IO_CLASS_SENSOR_PRODUCER_IR
Infrared publisher.
Definition common.hxx:139
@ IO_CLASS_ADC_FETCH
ADC fetch (on-demand)
Definition common.hxx:177
@ IO_CLASS_CAPABILITIES
Capabilities bitmask I/O.
Definition common.hxx:94
@ IO_CLASS_SENSOR_LIGHT
Light sensor.
Definition common.hxx:116
@ IO_CLASS_SENSOR_BAROMETER
Barometer sensor.
Definition common.hxx:117
@ IO_CLASS_SENSOR_PRODUCER_UV
Ultraviolet publisher.
Definition common.hxx:140
@ IO_CLASS_SENSOR_HUMIDITY
Humidity sensor.
Definition common.hxx:119
@ IO_CLASS_SENSOR_PROXIMITY
Proximity sensor.
Definition common.hxx:118
@ IO_CLASS_SENSOR_PRODUCER_GAS
Gas publisher.
Definition common.hxx:141
@ IO_CLASS_SENSOR_TEMPERATURE
Temperature sensor.
Definition common.hxx:120
@ IO_CLASS_LAST
Last I/O class marker.
Definition common.hxx:195
@ IO_CLASS_FILE
File system I/O.
Definition common.hxx:109
@ IO_CLASS_SENSOR_PRODUCER_GYROSCOPE
Gyroscope publisher.
Definition common.hxx:131
@ IO_CLASS_CONTROL
Control I/O.
Definition common.hxx:93
@ IO_CLASS_BUTTONS
Button input.
Definition common.hxx:166
@ IO_CLASS_SYSTEM_HOSTNAME
System hostname.
Definition common.hxx:155
@ IO_CLASS_SENSOR_PRODUCER_RGB
RGB color publisher.
Definition common.hxx:138
@ IO_CLASS_SENSOR_PRODUCER_TEMPERATURE
Temperature publisher.
Definition common.hxx:136
@ IO_CLASS_SENSOR_UV
Ultraviolet sensor.
Definition common.hxx:124
@ IO_CLASS_CONFIG
Configuration I/O.
Definition common.hxx:90
@ IO_CLASS_DESC_SELECTOR
Descriptor slot selector I/O.
Definition common.hxx:95
@ IO_CLASS_DUMMY
Dummy I/O (for testing)
Definition common.hxx:99
@ IO_CLASS_SENSOR_ACCELEROMETER
Accelerometer sensor.
Definition common.hxx:113
@ IO_CLASS_SENSOR_RGB
RGB color sensor.
Definition common.hxx:122
@ IO_CLASS_USER
User-defined I/O.
Definition common.hxx:193
@ IO_CLASS_TIMESTAMP
Timestamp generator.
Definition common.hxx:103
@ IO_CLASS_SENSOR_GYROSCOPE
Gyroscope sensor.
Definition common.hxx:115
@ IO_CLASS_SYSTEM_UUID
UUID.
Definition common.hxx:156
@ IO_CLASS_ADC_SYNC
ADC sync (HW-triggered control loop)
Definition common.hxx:178
@ IO_CLASS_ENCODER
Quadrature encoder (position)
Definition common.hxx:183
@ IO_CLASS_GPO_SINGLE
Single GPIO output.
Definition common.hxx:165
@ IO_CLASS_SENSOR_IR
Infrared sensor.
Definition common.hxx:123
@ IO_CLASS_SYSTEM_CPULOAD
CPU load.
Definition common.hxx:151
@ IO_CLASS_SENSOR_PRODUCER_PROXIMITY
Proximity publisher.
Definition common.hxx:134
@ IO_CLASS_SENSOR_ATEMPERATURE
Ambient temperature sensor.
Definition common.hxx:121
@ IO_CLASS_DUMMY_NOTIFY
Timer-driven dummy IO.
Definition common.hxx:105
@ IO_CLASS_LEDS
LED output.
Definition common.hxx:167
@ IO_CLASS_RGBLED
RGB LED output.
Definition common.hxx:168
@ IO_CLASS_RAND
Random number generator.
Definition common.hxx:104
@ IO_CLASS_SENSOR_GAS
Gas sensor.
Definition common.hxx:125
@ IO_CLASS_SENSOR_PRODUCER_BAROMETER
Barometer publisher.
Definition common.hxx:133
@ IO_CLASS_DESCRIPTOR
Descriptor I/O.
Definition common.hxx:92
@ IO_CLASS_SYSTEM_RESET
System reset control.
Definition common.hxx:152
static SObjectCfg::ObjectCfgId cfgIdLimitMin(uint8_t dtype, uint16_t size)
Create ConfigID for minimum I/O limits.
Definition common.hxx:629
const uint32_t * getCmnLimitStep()
Get step I/O limit words.
Definition common.hxx:825
static SObjectCfg::ObjectCfgId cfgIdNotify(bool rw=false)
Create ConfigID for notifier configuration.
Definition common.hxx:667
Common IO runtime limits container and validator.
Definition limits.hxx:24
size_t getWords() const
Get configured limit array size in words.
Definition limits.hxx:140
const uint32_t * getMax() const
Get maximum limit words pointer.
Definition limits.hxx:114
const uint32_t * getStep() const
Get step limit words pointer.
Definition limits.hxx:127
const uint32_t * getMin() const
Get minimum limit words pointer.
Definition limits.hxx:101
Base class for all Dawn objects (IOs, Programs, Protocols).
Definition object.hxx:28
int getObjConfig(SObjectCfg::ObjectCfgId objcfg, uint32_t *data, size_t len)
Get object configuration item.
Definition object.cxx:240
int setObjConfig(SObjectCfg::ObjectCfgId objcfg, uint32_t *data, size_t len)
Set object configuration item.
Definition object.cxx:200
Abstract interface for registering asynchronous I/O notification.
Definition inotifier.hxx:25
int(* notifier_cb_t)(void *priv, io_ddata_t *data)
Notifier callback function type.
Definition inotifier.hxx:37
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
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
Base interface for I/O data buffers (static and dynamic).
Definition idata.hxx:21
Single configuration item within object.
@ OBJTYPE_IO
Input/Output object type.
Definition objectid.hxx:184
EObjectDataType
Data types supported by Dawn framework.
Definition objectid.hxx:61
@ 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
Heap-allocated dynamic I/O data buffer.
Definition ddata.hxx:21