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
72{
73 int dtypeSize;
74 int ret;
75
76#ifdef CONFIG_SENSORS_USE_B16
78 {
79 DAWNERR("Sensor requires DTYPE_B16 when CONFIG_SENSORS_USE_B16 is enabled\n");
80 return -EINVAL;
81 }
82#else
84 {
85 DAWNERR("Sensor requires DTYPE_FLOAT when CONFIG_SENSORS_USE_FLOAT is enabled\n");
86 return -EINVAL;
87 }
88#endif
89
91 if (dtypeSize != sizeof(sensor_data_t))
92 {
93 DAWNERR("Sensor dtype size %d does not match NuttX sensor data size %zu\n",
94 dtypeSize,
95 sizeof(sensor_data_t));
96 return -EINVAL;
97 }
98
99 dsize = dtypeSize;
100
101 if (info == nullptr)
102 {
103 DAWNERR("Unsupported sensor class %d\n", getCls());
104 return -EINVAL;
105 }
106
107 if (DATA_BUFFER_SIZE < info->rsize)
108 {
109 DAWNERR("Sensor data size %zu exceeds buffer size %zu\n", info->rsize, DATA_BUFFER_SIZE);
110 return -EINVAL;
111 }
112
113 // Configure object
114
115 ret = configureDesc(getDesc());
116 if (ret != OK)
117 {
118 DAWNERR("Sensor configure failed (error %d)\n", ret);
119 return ret;
120 }
121
122 // Get path to sensor
123
124 if (getCmnDevno() == -1)
125 {
126 DAWNERR("SENSOR device number not configured\n");
127 return -EINVAL;
128 }
129
130 snprintf(path, sizeof(path), SENSOR_PATH_FMT, info->path, "", getCmnDevno());
131
132 // Open file
133
134 fd = sensor_open(path);
135 if (fd < 0)
136 {
137 DAWNERR("failed to open file %d\n", -errno);
138 return -errno;
139 }
140
141 return OK;
142}
143
145{
146 // Close file
147
148 sensor_close(fd);
149 return OK;
150}
151
152int CIOSensor::getDataImpl(IODataCmn &data, size_t len)
153{
154 uint8_t buf[DATA_BUFFER_SIZE];
155 size_t rsize = info->rsize;
156 int ret;
157
158 // No batch supported
159
160 if (len != 1)
161 {
162 return -ENOTSUP;
163 }
164
165 // Read data into temp buffer (kernel struct always includes timestamp)
166
167 ret = sensor_read(fd, buf, rsize);
168 if (ret < 0)
169 {
170 DAWNERR("sensor_read failed %d\n", ret);
171 return ret;
172 }
173
174 // Copy data portion (skip kernel timestamp)
175
176 std::memcpy(data.getDataPtr(), buf + info->payloadOffset, data.getDataSize());
177
178#ifdef CONFIG_DAWN_IO_TIMESTAMP
179 if (isTimestamp())
180 {
181 io_ts_t *kts = reinterpret_cast<io_ts_t *>(buf);
182 data.getTs() = *kts;
183 }
184#endif
185
186 return OK;
187}
188
189#ifdef CONFIG_DAWN_IO_NOTIFY
190int CIOSensor::getFd() const
191{
192 return fd;
193}
194#endif
195
197{
198 return info->dsize * dsize;
199}
200
202{
203 return info->dsize;
204}
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:425
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:798
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
int deinit()
De-initialize object.
Definition sensor.cxx:144
int getDataImpl(IODataCmn &data, size_t len)
Get data implementation (override in derived classes).
Definition sensor.cxx:152
size_t getDataSize() const
Get data size in bytes.
Definition sensor.cxx:196
@ IO_SENSOR_CFG_MEASPERIOD
Measurement period.
Definition sensor.hxx:36
@ IO_SENSOR_CFG_UPDATEINTERVAL
Data update interval in milliseconds.
Definition sensor.hxx:35
int configure()
Configure object from descriptor data.
Definition sensor.cxx:71
size_t getDataDim() const
Get data vector dimension.
Definition sensor.cxx:201
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.