Dawn Framework 1.0
Universal data acquisition framework for embedded systems
rtu.cxx
1// dawn/src/proto/modbus/rtu.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/proto/modbus/rtu.hxx"
7
8#include <unistd.h>
9
10#include "dawn/io/common.hxx"
11
12using namespace dawn;
13
14int CProtoModbusRtu::configureDesc(const CDescObject &desc)
15{
16 SObjectCfg::SObjectCfgItem *item = nullptr;
17 size_t offset = 0;
18
19 for (size_t i = 0; i < desc.getSize(); i++)
20 {
21 item = desc.objectCfgItemNext(offset);
22
24 {
25 DAWNERR("Unsupported Modbus RTU config 0x%08" PRIx32 "\n", item->cfgid.v);
26 return -EINVAL;
27 }
28
29 switch (item->cfgid.s.id)
30 {
32 {
33 for (size_t j = 0; j < item->cfgid.s.size;)
34 {
35 SProtoModbusIOBind *tmp = reinterpret_cast<SProtoModbusIOBind *>(item->data + j);
36
37 allocObject(tmp);
38
39 j += sizeof(SProtoModbusIOBind) / 4 + tmp->size;
40 }
41
42 break;
43 }
44
46 {
47 path = reinterpret_cast<const char *>(&item->data);
48 break;
49 }
50
52 {
53 baud = static_cast<uint32_t>(item->data[0]);
54 break;
55 }
56
57 default:
58 {
59 DAWNERR("Unsupported Modbus RTU config 0x%08" PRIx32 "\n", item->cfgid.v);
60 return -EINVAL;
61 }
62 }
63 }
64
65 return OK;
66}
67
68void CProtoModbusRtu::allocObject(SProtoModbusIOBind *alloc)
69{
70 for (size_t i = 0; i < alloc->size; i++)
71 {
72 DAWNINFO("allocate object 0x%" PRIx32 "\n", alloc->objid[i]);
73
74 // Allocate object in map
75
76 setObjectMapItem(alloc->objid[i], nullptr);
77 }
78
79 // Store config for later
80
81 valloc_push_back(alloc);
82};
83
84void CProtoModbusRtu::thread()
85{
86 DAWNINFO("start modbus thread\n");
87
88 /* Open the transport (nxmb_enable) here rather than in init(): a removable
89 * USB serial device (CDC/ACM) returns -ENOTCONN until the USB host has
90 * enumerated it, so wait for the host without blocking descriptor load.
91 */
92
93 for (;;)
94 {
95 int ret = nxmb_enable(mbhandle);
96 if (ret >= 0)
97 {
98 break;
99 }
100
101 if (ret != -ENOTCONN)
102 {
103 DAWNERR("ERROR: nxmb_enable failed: %d\n", ret);
104 workerThread().markThreadFinished();
105 return;
106 }
107
108 if (workerThread().shouldQuit())
109 {
110 workerThread().markThreadFinished();
111 return;
112 }
113
114 usleep(100000);
115 }
116
117 do
118 {
119 int ret;
120
121 /* Poll */
122
123 ret = nxmb_poll(mbhandle);
124 if (ret < 0 && ret != -ETIMEDOUT && ret != -EAGAIN)
125 {
126 DAWNERR("nxmb_poll error: %d\n", ret);
127 break;
128 }
129 }
130 while (!workerThread().shouldQuit());
131
132 // Mark thread quit done
133
134 workerThread().markThreadFinished();
135}
136
137int CProtoModbusRtu::modbusInitialize()
138{
139 struct nxmb_config_s config;
140 int ret;
141
142 memset(&config, 0, sizeof(config));
143 config.unit_id = saddr;
144 config.is_client = false;
145 config.mode = NXMB_MODE_RTU;
146
147 switch (parity)
148 {
149 case 0:
150 case 'N':
151 config.transport.serial.parity = NXMB_PAR_NONE;
152 break;
153 case 1:
154 case 'O':
155 config.transport.serial.parity = NXMB_PAR_ODD;
156 break;
157 case 2:
158 case 'E':
159 config.transport.serial.parity = NXMB_PAR_EVEN;
160 break;
161 default:
162 DAWNERR("ERROR: Invalid parity setting: %d\n", parity);
163 return -EINVAL;
164 }
165
166 config.transport.serial.devpath = path;
167 config.transport.serial.baudrate = baud;
168
169 ret = nxmb_create(&mbhandle, &config);
170 if (ret < 0)
171 {
172 DAWNERR("ERROR: nxmb_create failed: %d\n", ret);
173 return ret;
174 }
175
176 memset(&callbacks, 0, sizeof(callbacks));
177#ifdef CONFIG_DAWN_PROTO_MODBUS_COIL
178 callbacks.coil_cb = [](FAR uint8_t *buf,
179 uint16_t addr,
180 uint16_t ncoils,
181 enum nxmb_regmode_e mode,
182 FAR void *priv) -> int
183 { return static_cast<CProtoModbusRegs *>(priv)->coilsCb(buf, addr, ncoils, mode, priv); };
184#endif
185#ifdef CONFIG_DAWN_PROTO_MODBUS_DISCRETE
186 callbacks.discrete_cb =
187 [](FAR uint8_t *buf, uint16_t addr, uint16_t ndiscrete, FAR void *priv) -> int
188 { return static_cast<CProtoModbusRegs *>(priv)->discreteCb(buf, addr, ndiscrete, priv); };
189#endif
190#ifdef CONFIG_DAWN_PROTO_MODBUS_INPUT
191 callbacks.input_cb = [](FAR uint8_t *buf, uint16_t addr, uint16_t nregs, FAR void *priv) -> int
192 { return static_cast<CProtoModbusRegs *>(priv)->inputCb(buf, addr, nregs, priv); };
193#endif
194#ifdef CONFIG_DAWN_PROTO_MODBUS_HOLDING
195 callbacks.holding_cb = [](FAR uint8_t *buf,
196 uint16_t addr,
197 uint16_t nregs,
198 enum nxmb_regmode_e mode,
199 FAR void *priv) -> int
200 { return static_cast<CProtoModbusRegs *>(priv)->holdingCb(buf, addr, nregs, mode, priv); };
201#endif
202#ifdef CONFIG_DAWN_PROTO_MODBUS_SEEKABLE
203 callbacks.file_cb = [](uint8_t *buf,
204 uint16_t file,
205 uint16_t record,
206 uint16_t nregs,
207 enum nxmb_regmode_e mode,
208 void *priv) -> int
209 { return static_cast<CProtoModbusRegs *>(priv)->fileCb(buf, file, record, nregs, mode); };
210#endif
211 callbacks.priv = static_cast<CProtoModbusRegs *>(this);
212
213 ret = nxmb_set_callbacks(mbhandle, &callbacks);
214 if (ret < 0)
215 {
216 DAWNERR("ERROR: nxmb_set_callbacks failed: %d\n", ret);
217 nxmb_destroy(mbhandle);
218 return ret;
219 }
220
221 /* nxmb_enable() opens the transport device; it is deferred to thread() so a
222 * not-yet-connected removable USB serial device (CDC/ACM) waits for the USB
223 * host without blocking the descriptor load.
224 */
225
226 return OK;
227}
228
229//***************************************************************************
230// Public Methods
231//***************************************************************************
232
233CProtoModbusRtu::~CProtoModbusRtu()
234{
235 deinit();
236}
237
239{
240 int ret;
241
242 // Configure object
243
244 ret = configureDesc(getDesc());
245 if (ret != OK)
246 {
247 DAWNERR("Modbus RTU configure failed (error %d)\n", ret);
248 return ret;
249 }
250
251 return OK;
252}
253
255{
256 int ret;
257
258 // NxModbus supports multi-instance, no singleton management needed
259
260 // Initialize modbus stack
261
262 ret = modbusInitialize();
263 if (ret < 0)
264 {
265 return ret;
266 }
267
268 // Create modbus registers after IO bindings are resolved
269
270 ret = createRegs();
271 if (ret < 0)
272 {
273 DAWNERR("failed to create registers %d\n", ret);
274 if (mbhandle != NULL)
275 {
276 nxmb_disable(mbhandle);
277 nxmb_destroy(mbhandle);
278 mbhandle = NULL;
279 }
280 return ret;
281 }
282
283 return OK;
284}
285
287{
288 destroyRegs();
289
290 // Release hardware resources
291
292 if (mbhandle != NULL)
293 {
294 nxmb_disable(mbhandle);
295 nxmb_destroy(mbhandle);
296 mbhandle = NULL;
297 }
298
299 return OK;
300}
301
303{
304 int ret;
305
306 // Start thread
307
308 ret = startWorkerThread([this]() { thread(); });
309 if (ret < 0)
310 {
311 DAWNERR("failed to start thread %d\n", ret);
312 return ret;
313 }
314
315 return OK;
316};
317
319{
320 /* Stop thread */
321
323
324 return OK;
325};
326
328{
329 return workerThreadRunning();
330}
void setObjectMapItem(SObjectId::ObjectId id, CObject *obj)
Set an item in the object map.
Definition bindable.cxx:23
Descriptor wrapper for individual object configuration.
size_t getSize() const
Get number of configuration items for this object.
SObjectCfg::SObjectCfgItem * objectCfgItemNext(size_t &offset) const
Get config item at current offset and advance past it.
CDescObject & getDesc()
Get descriptor object for this object.
Definition object.cxx:190
@ PROTO_CLASS_MODBUS_RTU
Modbus RTU (serial implementation).
Definition common.hxx:67
@ PROTO_MODBUS_CFG_IOBIND
I/O object binding (register map).
Definition rtu.hxx:32
@ PROTO_MODBUS_CFG_PATH
Serial device path.
Definition rtu.hxx:33
@ PROTO_MODBUS_CFG_BAUD
Serial baud rate.
Definition rtu.hxx:34
int configure()
Configure object from descriptor data.
Definition rtu.cxx:238
int init()
One-time initialize object after bindings are resolved.
Definition rtu.cxx:254
int doStop()
Stop implementation hook.
Definition rtu.cxx:318
int doStart()
Start implementation hook.
Definition rtu.cxx:302
int deinit()
De-initialize object.
Definition rtu.cxx:286
bool hasThread() const
Check if a background thread is active.
Definition rtu.cxx:327
bool workerThreadRunning() const
Check if the worker thread is running.
Definition thread.hxx:269
int stopWorkerThread()
Stop the worker thread.
Definition thread.hxx:258
int startWorkerThread(Func &&func)
Start the worker thread with a given function.
Definition thread.hxx:246
CThreadedObject & workerThread()
Get a reference to this thread controller.
Definition thread.hxx:280
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
I/O binding configuration loaded from descriptor.
Definition regs.hxx:46
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
struct dawn::SObjectCfg::UObjectCfgId::@334307326077356333363326153167141302007100111136 s
Bit-field structure for named member access.
uint32_t size
Configuration data size in 32-bit words (bits 5-14, max 1023).