Dawn Framework 1.0
Universal data acquisition framework for embedded systems
sensor.cxx
1// dawn/src/io/sensor.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/io/sensor.hxx"
7
8#include <cstdio>
9#include <cstring>
10
11using namespace dawn;
12
13int CIOSensor::configureDesc(const CDescObject &desc)
14{
16 size_t offset = 0;
17
18 for (size_t i = 0; i < desc.getSize(); i++)
19 {
20 item = desc.objectCfgItemAtOffset(offset);
21
22 switch (item->cfgid.s.cls)
23 {
25 {
26 switch (item->cfgid.s.id)
27 {
29 {
30 const uint32_t *tmp = reinterpret_cast<const uint32_t *>(&item->data);
31
32 updateInterval = *tmp;
33 offset += 2;
34 break;
35 }
36
38 {
39 const uint32_t *tmp = reinterpret_cast<const uint32_t *>(&item->data);
40
41 measurementPeriod = *tmp;
42 offset += 2;
43 break;
44 }
45
46 default:
47 {
48 offset += cfgCmnOffset(item);
49 break;
50 }
51 }
52 break;
53 }
54
55 default:
56 {
57 DAWNERR("Unsupported sensor config 0x%08" PRIx32 "\n", item->cfgid.v);
58 return -EINVAL;
59 }
60 }
61 }
62
63 return OK;
64}
65
66CIOSensor::~CIOSensor()
67{
68 deinit();
69}
70
71int CIOSensor::validateDtype()
72{
73 int dtypeSize;
74
75#ifdef CONFIG_SENSORS_USE_B16
77 {
78 DAWNERR("Sensor requires DTYPE_B16 when CONFIG_SENSORS_USE_B16 is enabled\n");
79 return -EINVAL;
80 }
81#else
83 {
84 DAWNERR("Sensor requires DTYPE_FLOAT when CONFIG_SENSORS_USE_FLOAT is enabled\n");
85 return -EINVAL;
86 }
87#endif
88
90 if (dtypeSize != sizeof(sensor_data_t))
91 {
92 DAWNERR("Sensor dtype size %d does not match NuttX sensor data size %zu\n",
93 dtypeSize,
94 sizeof(sensor_data_t));
95 return -EINVAL;
96 }
97
98 dsize = dtypeSize;
99
100 return OK;
101}
102
104{
105 int ret;
106
107 ret = validateDtype();
108 if (ret != OK)
109 {
110 return ret;
111 }
112
113 if (info == nullptr)
114 {
115 DAWNERR("Unsupported sensor class %d\n", getCls());
116 return -EINVAL;
117 }
118
119 if (DATA_BUFFER_SIZE < info->rsize)
120 {
121 DAWNERR("Sensor data size %zu exceeds buffer size %zu\n", info->rsize, DATA_BUFFER_SIZE);
122 return -EINVAL;
123 }
124
125 // Configure object
126
127 ret = configureDesc(getDesc());
128 if (ret != OK)
129 {
130 DAWNERR("Sensor configure failed (error %d)\n", ret);
131 return ret;
132 }
133
134 // Get path to sensor
135
136 if (getCmnDevno() == -1)
137 {
138 DAWNERR("SENSOR device number not configured\n");
139 return -EINVAL;
140 }
141
142 snprintf(path, sizeof(path), SENSOR_PATH_FMT, info->path, "", getCmnDevno());
143
144 // Open file
145
146 fd = sensor_open(path);
147 if (fd < 0)
148 {
149 DAWNERR("failed to open file %d\n", -errno);
150 return -errno;
151 }
152
153 // Apply the configured update interval (sample rate). For GNSS this selects
154 // the fix cadence: single (on-demand), continuous (1 s), or periodic. When
155 // not configured (0) the sensor keeps its default rate.
156
157 if (updateInterval > 0)
158 {
159 ret = sensor_set_interval(fd, updateInterval * 1000);
160 if (ret < 0)
161 {
162 DAWNERR("failed to set sensor interval %d\n", ret);
163 return ret;
164 }
165 }
166
167 return OK;
168}
169
171{
172 // Close file
173
174 sensor_close(fd);
175 return OK;
176}
177
178int CIOSensor::getDataImpl(IODataCmn &data, size_t len)
179{
180 uint8_t buf[DATA_BUFFER_SIZE];
181 size_t rsize = info->rsize;
182 int ret;
183
184 // No batch supported
185
186 if (len != 1)
187 {
188 return -ENOTSUP;
189 }
190
191 // Read data into temp buffer (kernel struct always includes timestamp)
192
193 ret = sensor_read(fd, buf, rsize);
194 if (ret < 0)
195 {
196 DAWNERR("sensor_read failed %d\n", ret);
197 return ret;
198 }
199
200 // Copy data portion (skip kernel timestamp)
201
202 std::memcpy(data.getDataPtr(), buf + info->payloadOffset, data.getDataSize());
203
204#ifdef CONFIG_DAWN_IO_TIMESTAMP
205 if (isTimestamp())
206 {
207 io_ts_t *kts = reinterpret_cast<io_ts_t *>(buf);
208 data.getTs() = *kts;
209 }
210#endif
211
212 return OK;
213}
214
215#ifdef CONFIG_DAWN_IO_NOTIFY
216int CIOSensor::getFd() const
217{
218 return fd;
219}
220#endif
221
223{
224 return info->dsize * dsize;
225}
226
228{
229 return info->dsize;
230}
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.
virtual int getFd() const
Get file descriptor for notifications.
Definition common.hxx:444
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:817
size_t cfgCmnOffset(const SObjectCfg::SObjectCfgItem *cfg)
Get offset of configuration item in descriptor.
Definition common.cxx:144
@ IO_CLASS_ANY
Any I/O class.
Definition common.hxx:86
uint32_t measurementPeriod
Sensor measurement period in milliseconds.
Definition sensor.hxx:263
int fd
File descriptor for sensor device.
Definition sensor.hxx:265
int deinit()
De-initialize object.
Definition sensor.cxx:170
@ IO_SENSOR_CFG_MEASPERIOD
Measurement period.
Definition sensor.hxx:36
@ IO_SENSOR_CFG_UPDATEINTERVAL
Data update interval in milliseconds.
Definition sensor.hxx:35
int getDataImpl(IODataCmn &data, size_t len)
Get data implementation (override in derived classes).
Definition sensor.cxx:178
size_t getDataSize() const
Get data size in bytes.
Definition sensor.cxx:222
size_t dsize
Sensor data size in bytes.
Definition sensor.hxx:261
uint32_t updateInterval
Sensor update interval in milliseconds.
Definition sensor.hxx:262
char path[PATH_MAX]
Sensor device file path.
Definition sensor.hxx:264
const SIOSensorMapInfo * info
Sensor metadata.
Definition sensor.hxx:260
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
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 uint64_t & getTs(size_t batch=0)=0
Get timestamp reference for batch.
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.