6#include "dawn/proto/modbus/tcp.hxx"
8#include "dawn/io/common.hxx"
12int CProtoModbusTcp::configureDesc(
const CDescObject &desc)
17 for (
size_t i = 0; i < desc.
getSize(); i++)
23 DAWNERR(
"Unsupported Modbus TCP config 0x%08" PRIx32
"\n", item->
cfgid.
v);
29 case PROTO_MODBUS_TCP_CFG_IOBIND:
33 SProtoModbusIOBind *tmp =
reinterpret_cast<SProtoModbusIOBind *
>(item->
data + j);
36 j +=
sizeof(SProtoModbusIOBind) / 4 + tmp->size;
42 case PROTO_MODBUS_TCP_CFG_PORT:
44 port =
static_cast<uint16_t
>(item->
data[0]);
50 DAWNERR(
"Unsupported Modbus TCP config 0x%08" PRIx32
"\n", item->
cfgid.
v);
59void CProtoModbusTcp::allocObject(SProtoModbusIOBind *alloc)
61 for (
size_t i = 0; i < alloc->size; i++)
63 DAWNINFO(
"allocate object 0x%" PRIx32
"\n", alloc->objid[i]);
68 valloc_push_back(alloc);
71void CProtoModbusTcp::thread()
73 DAWNINFO(
"start modbus tcp thread\n");
79 ret = nxmb_poll(mbhandle);
80 if (ret < 0 && ret != -ETIMEDOUT && ret != -EAGAIN && ret != -ECONNRESET)
82 DAWNERR(
"nxmb_poll error: %d\n", ret);
91int CProtoModbusTcp::modbusInitialize()
93 struct nxmb_config_s config;
96 memset(&config, 0,
sizeof(config));
97 config.unit_id = saddr;
98 config.is_client =
false;
99 config.mode = NXMB_MODE_TCP;
100 config.transport.tcp.port = port;
101 config.transport.tcp.host = NULL;
102 config.transport.tcp.bindaddr = NULL;
104 ret = nxmb_create(&mbhandle, &config);
107 DAWNERR(
"ERROR: nxmb_create failed: %d\n", ret);
111 memset(&callbacks, 0,
sizeof(callbacks));
112#ifdef CONFIG_DAWN_PROTO_MODBUS_COIL
113 callbacks.coil_cb = [](FAR uint8_t *buf,
116 enum nxmb_regmode_e mode,
117 FAR
void *priv) ->
int
118 {
return static_cast<CProtoModbusRegs *
>(priv)->coilsCb(buf, addr, ncoils, mode, priv); };
120#ifdef CONFIG_DAWN_PROTO_MODBUS_DISCRETE
121 callbacks.discrete_cb =
122 [](FAR uint8_t *buf, uint16_t addr, uint16_t ndiscrete, FAR
void *priv) ->
int
123 {
return static_cast<CProtoModbusRegs *
>(priv)->discreteCb(buf, addr, ndiscrete, priv); };
125#ifdef CONFIG_DAWN_PROTO_MODBUS_INPUT
126 callbacks.input_cb = [](FAR uint8_t *buf, uint16_t addr, uint16_t nregs, FAR
void *priv) ->
int
127 {
return static_cast<CProtoModbusRegs *
>(priv)->inputCb(buf, addr, nregs, priv); };
129#ifdef CONFIG_DAWN_PROTO_MODBUS_HOLDING
130 callbacks.holding_cb = [](FAR uint8_t *buf,
133 enum nxmb_regmode_e mode,
134 FAR
void *priv) ->
int
135 {
return static_cast<CProtoModbusRegs *
>(priv)->holdingCb(buf, addr, nregs, mode, priv); };
139 ret = nxmb_set_callbacks(mbhandle, &callbacks);
142 DAWNERR(
"ERROR: nxmb_set_callbacks failed: %d\n", ret);
143 nxmb_destroy(mbhandle);
147 ret = nxmb_enable(mbhandle);
150 DAWNERR(
"ERROR: nxmb_enable failed: %d\n", ret);
151 nxmb_destroy(mbhandle);
159CProtoModbusTcp::~CProtoModbusTcp()
168 ret = configureDesc(
getDesc());
171 DAWNERR(
"Modbus TCP configure failed (error %d)\n", ret);
182 ret = modbusInitialize();
191 DAWNERR(
"failed to create registers %d\n", ret);
192 if (mbhandle != NULL)
194 nxmb_disable(mbhandle);
195 nxmb_destroy(mbhandle);
208 if (mbhandle != NULL)
210 nxmb_disable(mbhandle);
211 nxmb_destroy(mbhandle);
225 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_TCP
Modbus TCP (network implementation).
Modbus register management base class (shared RTU/TCP logic).
int init()
One-time initialize object after bindings are resolved.
int doStart()
Start implementation hook.
int doStop()
Stop implementation hook.
bool hasThread() const
Check if a background thread is active.
int configure()
Configure object from descriptor data.
int deinit()
De-initialize object.
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.
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).
uint32_t cls
Object class (bits 21-29, max 511).
uint32_t id
Configuration identifier (bits 0-4, max 31).
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.