Dawn Framework 1.0
Universal data acquisition framework for embedded systems
sensor_producer.cxx
1// dawn/src/io/sensor_producer.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/io/sensor_producer.hxx"
7
8#include <cstdio>
9#include <cstring>
10
11#include <nuttx/sensors/sensor.h>
12
13using namespace dawn;
14
15int CIOSensorProducer::configureDesc(const CDescObject &desc)
16{
18 size_t offset = 0;
19
20 for (size_t i = 0; i < desc.getSize(); i++)
21 {
22 item = desc.objectCfgItemAtOffset(offset);
23
24 switch (item->cfgid.s.cls)
25 {
41 {
42 if (item->cfgid.s.cls != getCls())
43 {
44 DAWNERR("Sensor producer config class %u does not match object class %u\n",
45 item->cfgid.s.cls,
46 getCls());
47 return -EINVAL;
48 }
49
50 switch (item->cfgid.s.id)
51 {
52 case CIOSensorProducer::IO_SENSOR_PRODUCER_CFG_QUEUE_SIZE:
53 {
54 const uint32_t *tmp = reinterpret_cast<const uint32_t *>(&item->data);
55 queueSize = *tmp;
56 offset += 2;
57 break;
58 }
59
60 case CIOSensorProducer::IO_SENSOR_PRODUCER_CFG_PERSIST:
61 {
62 const uint32_t *tmp = reinterpret_cast<const uint32_t *>(&item->data);
63 persist = (*tmp != 0);
64 offset += 2;
65 break;
66 }
67
68 default:
69 {
70 DAWNERR("Unsupported sensor producer config 0x%08" PRIx32 "\n",
71 item->cfgid.v);
72 return -EINVAL;
73 }
74 }
75 break;
76 }
77
79 {
80 offset += cfgCmnOffset(item);
81 break;
82 }
83
84 default:
85 {
86 DAWNERR("Unsupported sensor producer config 0x%08" PRIx32 "\n", item->cfgid.v);
87 return -EINVAL;
88 }
89 }
90 }
91
92 return OK;
93}
94
95CIOSensorProducer::~CIOSensorProducer()
96{
97 deinit();
98}
99
101{
102 int dtypeSize;
103 int ret;
104
105 if (info == nullptr)
106 {
107 DAWNERR("Unsupported sensor producer class %d\n", getCls());
108 return -EINVAL;
109 }
110
111#ifdef CONFIG_SENSORS_USE_B16
113 {
114 DAWNERR("Sensor producer requires DTYPE_B16 when CONFIG_SENSORS_USE_B16 is enabled\n");
115 return -EINVAL;
116 }
117#else
119 {
120 DAWNERR("Sensor producer requires DTYPE_FLOAT when CONFIG_SENSORS_USE_FLOAT is enabled\n");
121 return -EINVAL;
122 }
123#endif
124
126 if (dtypeSize != sizeof(sensor_data_t))
127 {
128 DAWNERR("Sensor producer dtype size %d does not match NuttX sensor data size %zu\n",
129 dtypeSize,
130 sizeof(sensor_data_t));
131 return -EINVAL;
132 }
133
134 if (info->path == nullptr || info->rsize == 0 || info->dsize == 0 ||
135 info->payloadOffset + getDataSize() > info->rsize)
136 {
137 DAWNERR("Unsupported sensor producer class %d\n", getCls());
138 return -EINVAL;
139 }
140
141 if (DATA_BUFFER_SIZE < info->rsize)
142 {
143 DAWNERR(
144 "Sensor producer event size %zu exceeds buffer size %zu\n", info->rsize, DATA_BUFFER_SIZE);
145 return -EINVAL;
146 }
147
148 ret = configureDesc(getDesc());
149 if (ret != OK)
150 {
151 DAWNERR("Sensor producer configure failed (error %d)\n", ret);
152 return ret;
153 }
154
155 if (getCmnDevno() == -1)
156 {
157 DAWNERR("SENSOR producer device number not configured\n");
158 return -EINVAL;
159 }
160
161 snprintf(path, sizeof(path), SENSOR_PATH_FMT, info->path, "", getCmnDevno());
162
163 ret = sensor_user_register(path, info->rsize, queueSize, persist);
164 if (ret == OK)
165 {
166 registered = true;
167 }
168 else if (ret != -EEXIST)
169 {
170 DAWNERR("failed to register sensor producer %s %d\n", path, ret);
171 return ret;
172 }
173
174 fd = sensor_open_write(path);
175 if (fd < 0)
176 {
177 if (registered)
178 {
179 sensor_user_unregister(path);
180 registered = false;
181 }
182 return fd;
183 }
184
185 return OK;
186}
187
189{
190 if (fd >= 0)
191 {
192 sensor_close(fd);
193 fd = -1;
194 }
195
196 if (registered)
197 {
198 sensor_user_unregister(path);
199 registered = false;
200 }
201
202 return OK;
203}
204
206{
207 uint8_t buf[DATA_BUFFER_SIZE];
208 io_ts_t *eventTs;
209 int ret;
210
211 if (fd < 0)
212 {
213 return -ENODEV;
214 }
215
216 if (data.getDataSize() != getDataSize())
217 {
218 return -EINVAL;
219 }
220
221 std::memset(buf, 0, info->rsize);
222 eventTs = reinterpret_cast<io_ts_t *>(buf);
223 *eventTs = sensor_get_timestamp();
224 std::memcpy(buf + info->payloadOffset, data.getDataPtr(), data.getDataSize());
225
226 ret = sensor_write(fd, buf, info->rsize);
227 if (ret < 0)
228 {
229 DAWNERR("sensor producer write failed %d\n", ret);
230 return ret;
231 }
232
233 if (static_cast<size_t>(ret) != info->rsize)
234 {
235 return -EIO;
236 }
237
238#ifdef CONFIG_DAWN_IO_TIMESTAMP
239 if (isTimestamp())
240 {
241 eventTs = reinterpret_cast<io_ts_t *>(buf);
242 this->ts = *eventTs;
243 }
244#endif
245
246 return OK;
247}
248
250{
251 return info != nullptr ? info->dsize * sizeof(sensor_data_t) : 0;
252}
253
255{
256 return getDataSize() / sizeof(sensor_data_t);
257}
Descriptor wrapper for individual object configuration.
size_t getSize() const
Get number of configuration items for this object.
SObjectCfg::SObjectCfgItem * objectCfgItemAtOffset(size_t offset) const
Get configuration item at specified offset.
bool isTimestamp() const
Check if I/O supports timestamp.
Definition common.cxx:189
int getCmnDevno() const
Get device number for this I/O.
Definition common.hxx:820
size_t cfgCmnOffset(const SObjectCfg::SObjectCfgItem *cfg)
Get offset of configuration item in descriptor.
Definition common.cxx:144
@ IO_CLASS_SENSOR_PRODUCER_MAGNETICFIELD
Magnetic field publisher.
Definition common.hxx:132
@ IO_CLASS_SENSOR_PRODUCER_ACCELEROMETER
Accelerometer publisher.
Definition common.hxx:131
@ IO_CLASS_SENSOR_PRODUCER_LIGHT
Light publisher.
Definition common.hxx:134
@ IO_CLASS_SENSOR_PRODUCER_ATEMPERATURE
Ambient temperature publisher.
Definition common.hxx:139
@ IO_CLASS_ANY
Any I/O class.
Definition common.hxx:86
@ IO_CLASS_SENSOR_PRODUCER_HUMIDITY
Humidity publisher.
Definition common.hxx:137
@ IO_CLASS_SENSOR_PRODUCER_IR
Infrared publisher.
Definition common.hxx:141
@ IO_CLASS_SENSOR_PRODUCER_UV
Ultraviolet publisher.
Definition common.hxx:142
@ IO_CLASS_SENSOR_PRODUCER_GAS
Gas publisher.
Definition common.hxx:143
@ IO_CLASS_SENSOR_PRODUCER_GYROSCOPE
Gyroscope publisher.
Definition common.hxx:133
@ IO_CLASS_SENSOR_PRODUCER_CO2
CO2 concentration publisher.
Definition common.hxx:144
@ IO_CLASS_SENSOR_PRODUCER_RGB
RGB color publisher.
Definition common.hxx:140
@ IO_CLASS_SENSOR_PRODUCER_TEMPERATURE
Temperature publisher.
Definition common.hxx:138
@ IO_CLASS_SENSOR_PRODUCER_TVOC
TVOC concentration publisher.
Definition common.hxx:145
@ IO_CLASS_SENSOR_PRODUCER_PROXIMITY
Proximity publisher.
Definition common.hxx:136
@ IO_CLASS_SENSOR_PRODUCER_BAROMETER
Barometer publisher.
Definition common.hxx:135
size_t getDataDim() const
Get data vector dimension.
int configure()
Configure object from descriptor data.
int setDataImpl(IODataCmn &data)
Set data implementation (override in derived classes).
size_t getDataSize() const
Get data size in bytes.
int deinit()
De-initialize object.
uint16_t getCls() const
Get object class field.
Definition object.cxx:170
CDescObject & getDesc()
Get descriptor object for this object.
Definition object.cxx:190
uint8_t getDtype() const
Get data type field.
Definition object.cxx:175
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
uint64_t io_ts_t
Timestamp data type (uint64_t, typically microseconds since boot).
Definition idata.hxx:16
Base interface for I/O data buffers (static and dynamic).
Definition idata.hxx:21
virtual void * getDataPtr(size_t batch=0)=0
Get pointer to data only (skips timestamp if present).
virtual size_t getDataSize()=0
Get data size in bytes.
Single configuration item within object.
ObjectCfgData_t data[]
Configuration data array (flexible, size from cfgid.s.size).
UObjectCfgId cfgid
Configuration ID header (type, class, id, size, rw, dtype).
EObjectDataType
Data types supported by Dawn framework.
Definition objectid.hxx:61
@ DTYPE_FLOAT
IEEE 754 single-precision floating point (32-bit).
Definition objectid.hxx:112
@ DTYPE_B16
Signed 16.16 fixed-point (32-bit).
Definition objectid.hxx:128
static int getDtypeSize_(const EObjectDataType dtype)
Get byte size for a specific data type.
Definition objectid.cxx:12
ObjectCfgId v
Raw 32-bit ConfigID value (for storage, comparison).
Definition objectcfg.hxx:82
uint32_t cls
Object class (bits 21-29, max 511).
uint32_t id
Configuration identifier (bits 0-4, max 31).
Definition objectcfg.hxx:94
struct dawn::SObjectCfg::UObjectCfgId::@10 s
Bit-field structure for named member access.