Dawn Framework 1.0
Universal data acquisition framework for embedded systems
control.cxx
1// dawn/src/io/control.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/io/control.hxx"
7
8#include <errno.h>
9
10#include "dawn/debug.hxx"
11#include "dawn/io/common.hxx"
12
13using namespace dawn;
14
15int CIOControl::configureDesc(const CDescObject &desc)
16{
17 const SObjectCfg::SObjectCfgItem *item = nullptr;
18 size_t offset = 0;
19 size_t i = 0;
20
21 for (i = 0; i < desc.getSize(); i++)
22 {
23 item = desc.objectCfgItemAtOffset(offset);
24
26 {
27 DAWNERR("unsupported control cfg 0x%" PRIx32 "\n", item->cfgid.v);
28 return -EINVAL;
29 }
30
31 switch (item->cfgid.s.id)
32 {
33 case IO_CONTROL_CFG_ALLOCOBJ:
34 {
35 offset += 1;
36
37 for (size_t j = 0; j < item->cfgid.s.size; j++)
38 {
39 const SObjectCfg::SObjectCfgItem *obj = desc.objectCfgItemAtOffset(offset);
40
41 ids.push_back(static_cast<SObjectId::ObjectId>(obj->cfgid.v));
42
43 offset += 1;
44 }
45
46 break;
47 }
48
49 case IO_CONTROL_CFG_ALLOWED:
50 {
51 const uint32_t *tmp = reinterpret_cast<const uint32_t *>(&item->data);
52
53 allowed = *tmp;
54 offset += 2;
55 break;
56 }
57
58 default:
59 {
60 DAWNERR("unsupported control cfg 0x%" PRIx32 "\n", item->cfgid.v);
61 return -EINVAL;
62 }
63 }
64 }
65
66 return OK;
67}
68
69CIOControl::~CIOControl()
70{
71 deinit();
72}
73
75{
76 int ret;
77
78 // Configure object
79
80 ret = configureDesc(getDesc());
81 if (ret != OK)
82 {
83 return ret;
84 }
85
86 return OK;
87}
88
90{
91 return OK;
92}
93
94int CIOControl::getDataImpl(IODataCmn &data, size_t len)
95{
96 uint8_t *tmp;
97
98 UNUSED(len);
99
100 if (targets.empty())
101 {
102 DAWNERR("no bound targets\n");
103 return -ENOENT;
104 }
105
106 tmp = reinterpret_cast<uint8_t *>(data.getDataPtr());
107 *tmp = static_cast<uint8_t>(targets[0]->getState());
108
109 return OK;
110}
111
113{
114 int ret = OK;
115 uint8_t cmd = *reinterpret_cast<const uint8_t *>(data.getDataPtr());
116
117 if (cmd == 0)
118 {
119 // Stop command
120
121 if (!(allowed & CTRL_ALLOW_STOP))
122 {
123 DAWNERR("stop command not allowed\n");
124 return -EACCES;
125 }
126
127 for (CObject *target : targets)
128 {
129 int r = target->stop();
130
131 if (r < 0)
132 {
133 DAWNERR("stop failed %d\n", r);
134 ret = r;
135 }
136 }
137 }
138 else if (cmd == 1)
139 {
140 // Start command
141
142 if (!(allowed & CTRL_ALLOW_START))
143 {
144 DAWNERR("start command not allowed\n");
145 return -EACCES;
146 }
147
148 for (CObject *target : targets)
149 {
150 int r = target->start();
151
152 if (r < 0)
153 {
154 DAWNERR("start failed %d\n", r);
155 ret = r;
156 }
157 }
158 }
159 else
160 {
161 DAWNERR("invalid control command %d\n", cmd);
162 return -EINVAL;
163 }
164
165 return ret;
166}
167
169{
170 return sizeof(uint8_t);
171}
172
174{
175 return 1;
176}
177
178int CIOControl::bind(CObject *obj)
179{
180 targets.push_back(obj);
181 return OK;
182}
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.
@ IO_CLASS_CONTROL
Control I/O.
Definition common.hxx:93
size_t getDataDim() const
Get data vector dimension.
Definition control.cxx:173
int setDataImpl(IODataCmn &data)
Set data implementation (override in derived classes).
Definition control.cxx:112
int configure()
Configure object from descriptor data.
Definition control.cxx:74
@ CTRL_ALLOW_STOP
Allow stop command.
Definition control.hxx:31
@ CTRL_ALLOW_START
Allow start command.
Definition control.hxx:32
int getDataImpl(IODataCmn &data, size_t len)
Get data implementation (override in derived classes).
Definition control.cxx:94
std::vector< SObjectId::ObjectId > ids
Object IDs to resolve; populated during configure().
Definition control.hxx:79
size_t getDataSize() const
Get data size in bytes.
Definition control.cxx:168
int deinit()
De-initialize object.
Definition control.cxx:89
Base class for all Dawn objects (IOs, Programs, Protocols).
Definition object.hxx:28
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).
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).
uint32_t ObjectId
ObjectID type - single 32-bit value.
Definition objectid.hxx:44
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.