Dawn Framework 1.0
Universal data acquisition framework for embedded systems
rtu.hxx
1// dawn/include/dawn/proto/modbus/rtu.hxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#pragma once
7
8#include "dawn/common/thread.hxx"
9#include "dawn/porting/config.hxx"
10#include "dawn/proto/common.hxx"
11#include "dawn/proto/modbus/regs.hxx"
12#include <nxmodbus/nxmodbus.h>
13
14namespace dawn
15{
24 : public CProtoCommon
25 , public CProtoModbusRegs
26 , protected CThreadedObject
27{
28public:
29 enum
30 {
31 PROTO_MODBUS_CFG_FIRST = 0,
35 PROTO_MODBUS_CFG_LAST = 31
36 };
37
38 explicit CProtoModbusRtu(CDescObject &desc)
39 : CProtoCommon(desc)
40 , mbhandle(nullptr)
41 , callbacks()
42 , saddr(CONFIG_DAWN_PROTO_MODBUS_RTU_ADDR)
43 , parity(CONFIG_DAWN_PROTO_MODBUS_RTU_PARITY)
44 , baud(CONFIG_DAWN_PROTO_MODBUS_RTU_BAUD)
45 , path(CONFIG_DAWN_PROTO_MODBUS_RTU_PATH)
46 {
47 }
48
49 ~CProtoModbusRtu() override;
50
51#ifdef CONFIG_DAWN_OBJECT_HAS_NAME
52 const char *getClassNameStr() const override
53 {
54 return "modbus_rtu";
55 }
56#endif
57
58 int configure() override;
59 int init() override;
60 int deinit() override;
61 int doStart() override;
62 int doStop() override;
63 bool hasThread() const override;
64
65 constexpr static SObjectId::ObjectId objectId(uint16_t id)
66 {
69 }
70
71 constexpr static SObjectCfg::ObjectCfgId cfgId(bool rw, uint8_t dtype, uint8_t size, uint8_t id)
72 {
75 }
76
77 constexpr static SObjectCfg::ObjectCfgId cfgIdIOBind(uint16_t size)
78 {
79 return CProtoModbusRtu::cfgId(false, SObjectId::DTYPE_ANY, size, PROTO_MODBUS_CFG_IOBIND);
80 }
81
82 constexpr static SObjectCfg::ObjectCfgId cfgIdPath(uint16_t size)
83 {
84 return CProtoModbusRtu::cfgId(true, SObjectId::DTYPE_CHAR, size, PROTO_MODBUS_CFG_PATH);
85 }
86
87 constexpr static SObjectCfg::ObjectCfgId cfgIdBaud()
88 {
89 return CProtoModbusRtu::cfgId(true, SObjectId::DTYPE_UINT32, 1, PROTO_MODBUS_CFG_BAUD);
90 }
91
92private:
93 nxmb_handle_t mbhandle;
94 struct nxmb_callbacks_s callbacks;
95 uint8_t saddr;
96 uint8_t parity;
97 uint32_t baud;
98 const char *path;
99
100 int configureDesc(const CDescObject &desc);
101 void allocObject(SProtoModbusIOBind *alloc);
102 void thread();
103 int modbusInitialize();
104
105 CIOCommon *getIO_(SObjectId::ObjectId id) override
106 {
107 return this->getIO(id);
108 };
109};
110
111} // Namespace dawn
CIOCommon * getIO(SObjectId::ObjectId id)
Get an I/O object by ID.
Definition bindable.cxx:41
Descriptor wrapper for individual object configuration.
Base class for all protocol implementations.
Definition common.hxx:23
@ PROTO_CLASS_MODBUS_RTU
Modbus RTU (serial implementation).
Definition common.hxx:67
Modbus register management base class (shared RTU/TCP logic).
Definition regs.hxx:30
Modbus RTU (Remote Terminal Unit) protocol implementation.
Definition rtu.hxx:27
int configure()
Configure object from descriptor data.
Definition rtu.cxx:202
int init()
One-time initialize object after bindings are resolved.
Definition rtu.cxx:218
int doStop()
Stop implementation hook.
Definition rtu.cxx:282
int doStart()
Start implementation hook.
Definition rtu.cxx:266
int deinit()
De-initialize object.
Definition rtu.cxx:250
bool hasThread() const
Check if a background thread is active.
Definition rtu.cxx:291
@ 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
Portable thread owner abstraction for Dawn components.
Definition thread.hxx:32
static ObjectCfgId objectCfg(uint8_t type, uint16_t cls, uint8_t dtype, bool rw, uint16_t size, uint8_t id)
Construct 32-bit ConfigID from component fields.
uint32_t ObjectCfgId
ConfigID type - single 32-bit value.
Definition objectcfg.hxx:60
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
@ OBJTYPE_PROTO
Protocol object type.
Definition objectid.hxx:193
@ DTYPE_ANY
Wildcard data type (matches any actual type).
Definition objectid.hxx:68
@ DTYPE_CHAR
Character/string type (null-terminated, 4-byte aligned).
Definition objectid.hxx:144
@ DTYPE_UINT32
Unsigned 32-bit integer (0 to 4294967295).
Definition objectid.hxx:96
uint32_t ObjectId
ObjectID type - single 32-bit value.
Definition objectid.hxx:44
static ObjectId objectId(uint8_t type, uint16_t cls, uint8_t dtype, uint8_t flags, uint16_t priv)
Construct 32-bit ObjectID from component fields.
Definition objectid.hxx:290