6#include "dawn/proto/modbus/rtu.hxx"
10#include "dawn/io/common.hxx"
14int CProtoModbusRtu::configureDesc(
const CDescObject &desc)
16 SObjectCfg::SObjectCfgItem *item =
nullptr;
19 for (
size_t i = 0; i < desc.
getSize(); i++)
25 DAWNERR(
"Unsupported Modbus RTU config 0x%08" PRIx32
"\n", item->
cfgid.
v);
47 path =
reinterpret_cast<const char *
>(&item->
data);
53 baud =
static_cast<uint32_t
>(item->
data[0]);
59 DAWNERR(
"Unsupported Modbus RTU config 0x%08" PRIx32
"\n", item->
cfgid.
v);
68void CProtoModbusRtu::allocObject(SProtoModbusIOBind *alloc)
70 for (
size_t i = 0; i < alloc->size; i++)
72 DAWNINFO(
"allocate object 0x%" PRIx32
"\n", alloc->objid[i]);
81 valloc_push_back(alloc);
84void CProtoModbusRtu::thread()
86 DAWNINFO(
"start modbus thread\n");
95 int ret = nxmb_enable(mbhandle);
101 if (ret != -ENOTCONN)
103 DAWNERR(
"ERROR: nxmb_enable failed: %d\n", ret);
123 ret = nxmb_poll(mbhandle);
124 if (ret < 0 && ret != -ETIMEDOUT && ret != -EAGAIN)
126 DAWNERR(
"nxmb_poll error: %d\n", ret);
137int CProtoModbusRtu::modbusInitialize()
139 struct nxmb_config_s config;
142 memset(&config, 0,
sizeof(config));
143 config.unit_id = saddr;
144 config.is_client =
false;
145 config.mode = NXMB_MODE_RTU;
151 config.transport.serial.parity = NXMB_PAR_NONE;
155 config.transport.serial.parity = NXMB_PAR_ODD;
159 config.transport.serial.parity = NXMB_PAR_EVEN;
162 DAWNERR(
"ERROR: Invalid parity setting: %d\n", parity);
166 config.transport.serial.devpath = path;
167 config.transport.serial.baudrate = baud;
169 ret = nxmb_create(&mbhandle, &config);
172 DAWNERR(
"ERROR: nxmb_create failed: %d\n", ret);
176 memset(&callbacks, 0,
sizeof(callbacks));
177#ifdef CONFIG_DAWN_PROTO_MODBUS_COIL
178 callbacks.coil_cb = [](FAR uint8_t *buf,
181 enum nxmb_regmode_e mode,
182 FAR
void *priv) ->
int
183 {
return static_cast<CProtoModbusRegs *
>(priv)->coilsCb(buf, addr, ncoils, mode, priv); };
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); };
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); };
194#ifdef CONFIG_DAWN_PROTO_MODBUS_HOLDING
195 callbacks.holding_cb = [](FAR uint8_t *buf,
198 enum nxmb_regmode_e mode,
199 FAR
void *priv) ->
int
200 {
return static_cast<CProtoModbusRegs *
>(priv)->holdingCb(buf, addr, nregs, mode, priv); };
202#ifdef CONFIG_DAWN_PROTO_MODBUS_SEEKABLE
203 callbacks.file_cb = [](uint8_t *buf,
207 enum nxmb_regmode_e mode,
209 {
return static_cast<CProtoModbusRegs *
>(priv)->fileCb(buf, file, record, nregs, mode); };
211 callbacks.priv =
static_cast<CProtoModbusRegs *
>(
this);
213 ret = nxmb_set_callbacks(mbhandle, &callbacks);
216 DAWNERR(
"ERROR: nxmb_set_callbacks failed: %d\n", ret);
217 nxmb_destroy(mbhandle);
233CProtoModbusRtu::~CProtoModbusRtu()
244 ret = configureDesc(
getDesc());
247 DAWNERR(
"Modbus RTU configure failed (error %d)\n", ret);
262 ret = modbusInitialize();
273 DAWNERR(
"failed to create registers %d\n", ret);
274 if (mbhandle != NULL)
276 nxmb_disable(mbhandle);
277 nxmb_destroy(mbhandle);
292 if (mbhandle != NULL)
294 nxmb_disable(mbhandle);
295 nxmb_destroy(mbhandle);
311 DAWNERR(
"failed to start thread %d\n", ret);
void setObjectMapItem(SObjectId::ObjectId id, CObject *obj)
Set an item in the object map.
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.
@ PROTO_CLASS_MODBUS_RTU
Modbus RTU (serial implementation).
@ PROTO_MODBUS_CFG_IOBIND
I/O object binding (register map).
@ PROTO_MODBUS_CFG_PATH
Serial device path.
@ PROTO_MODBUS_CFG_BAUD
Serial baud rate.
int configure()
Configure object from descriptor data.
int init()
One-time initialize object after bindings are resolved.
int doStop()
Stop implementation hook.
int doStart()
Start implementation hook.
int deinit()
De-initialize object.
bool hasThread() const
Check if a background thread is active.
bool workerThreadRunning() const
Check if the worker thread is running.
int stopWorkerThread()
Stop the worker thread.
int startWorkerThread(Func &&func)
Start the worker thread with a given function.
CThreadedObject & workerThread()
Get a reference to this thread controller.
Out-of-tree user-extension hooks for Dawn.
I/O binding configuration loaded from descriptor.
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).
uint32_t cls
Object class (bits 21-29, max 511).
uint32_t id
Configuration identifier (bits 0-4, max 31).
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).