Dawn Framework 1.0
Universal data acquisition framework for embedded systems
rand.cxx
1// dawn/src/io/rand.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/io/rand.hxx"
7
8#include <fcntl.h>
9#include <new>
10#include <unistd.h>
11
12using namespace dawn;
13
14#define RAND_DEVPATH "/dev/urandom"
15
16int CIORand::configureDesc(const CDescObject &desc)
17{
18 const SObjectCfg::SObjectCfgItem *item = nullptr;
19 size_t offset = 0;
20 size_t i = 0;
21
22 for (i = 0; i < desc.getSize(); i++)
23 {
24 item = desc.objectCfgItemAtOffset(offset);
25
26 switch (item->cfgid.s.cls)
27 {
29 {
30 switch (item->cfgid.s.id)
31 {
33 {
34 const uint32_t *tmp = &item->data[0];
35
36 timfd_interval(*tmp);
37
38 // Cfgid + data
39
40 offset += 1 + item->cfgid.s.size;
41 break;
42 }
43
44 default:
45 {
46 DAWNERR("unsupported rand objectCfg 0x%" PRIx32 "\n", item->cfgid.v);
47 DAWNERR("Unsupported random source type\n");
48 return -EINVAL;
49 }
50 }
51
52 break;
53 }
54
56 {
57 offset += cfgCmnOffset(item);
58 break;
59 }
60
61 default:
62 {
63 DAWNERR("unsupported rand cfg 0x08%" PRIx32 "\n", item->cfgid.v);
64 return -EINVAL;
65 }
66 }
67 }
68
69 return OK;
70}
71
72CIORand::~CIORand()
73{
74 deinit();
75}
76
78{
79 int ret;
80
81 val = new (std::nothrow) uint8_t[sizeof(tlen)]();
82 if (!val)
83 {
84 DAWNERR("new failed\n");
85 return -ENOMEM;
86 }
87
88 // Configure
89
90 ret = configureDesc(getDesc());
91 if (ret != OK)
92 {
93 return ret;
94 }
95
96 fd = open(RAND_DEVPATH, O_RDONLY);
97 if (fd < 0)
98 {
99 DAWNERR("failed to open %s\n", "/dev/urandom");
100 return -errno;
101 }
102
103 return timfd_init();
104}
105
107{
108 // Stop IO
109
110 stop();
111
112 // Free data buffer
113
114 if (val)
115 {
116 free(val);
117 }
118
119 return OK;
120}
121
122int CIORand::getDataImpl(IODataCmn &data, size_t len)
123{
124 for (size_t i = 0; i < len; i++)
125 {
126 ssize_t ret;
127
128 ret = read(fd, data.getDataPtr(i), tlen);
129 if (ret < 0)
130 {
131 DAWNERR("rand read failed %d\n", -errno);
132 }
133
134#ifdef CONFIG_DAWN_IO_RAND
135 // This is here only to be consistent across IO implementations
136
137 if (isRand())
138 {
139 data.getTs() = getTimestamp();
140 }
141#endif
142 }
143
144 // Clear notification event from fd
145
146 timfd_ack();
147
148 return OK;
149}
150
151#ifdef CONFIG_DAWN_IO_NOTIFY
152int CIORand::getFd() const
153{
154 return timfd_fd();
155};
156#endif
157
159{
160 return timfd_start();
161}
162
164{
165 return timfd_stop();
166}
167
169{
170 // Data size
171
172 return tlen;
173}
174
176{
177 // Data dimmention
178
179 return 1;
180}
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
uint64_t getTimestamp()
Get current timestamp.
Definition common.cxx:194
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
@ IO_CLASS_RAND
Random number generator.
Definition common.hxx:104
int getDataImpl(IODataCmn &data, size_t len)
Get data implementation (override in derived classes).
Definition rand.cxx:122
@ IO_RAND_CFG_INTERVAL
Interval configuration in microseconds.
Definition rand.hxx:30
size_t getDataSize() const
Get data size in bytes.
Definition rand.cxx:168
int configure()
Configure object from descriptor data.
Definition rand.cxx:77
size_t getDataDim() const
Get data vector dimension.
Definition rand.cxx:175
int doStart()
Start implementation hook.
Definition rand.cxx:158
int doStop()
Stop implementation hook.
Definition rand.cxx:163
int deinit()
De-initialize object.
Definition rand.cxx:106
int stop()
Stop object.
Definition object.hxx:145
CDescObject & getDesc()
Get descriptor object for this object.
Definition object.cxx:190
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
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.
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).
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
uint32_t size
Configuration data size in 32-bit words (bits 5-14, max 1023).
struct dawn::SObjectCfg::UObjectCfgId::@10 s
Bit-field structure for named member access.