Dawn Framework 1.0
Universal data acquisition framework for embedded systems
tcp.hxx
1// dawn/include/dawn/proto/modbus/tcp.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_TCP_CFG_FIRST = 0,
32 PROTO_MODBUS_TCP_CFG_IOBIND = 1,
33 PROTO_MODBUS_TCP_CFG_PORT = 2,
34 PROTO_MODBUS_TCP_CFG_LAST = 31
35 };
36
37 explicit CProtoModbusTcp(CDescObject &desc)
38 : CProtoCommon(desc)
39 , mbhandle(nullptr)
40 , callbacks()
41 , saddr(CONFIG_DAWN_PROTO_MODBUS_TCP_ADDR)
42 , port(CONFIG_DAWN_PROTO_MODBUS_TCP_PORT)
43 {
44 }
45
46 ~CProtoModbusTcp() override;
47
48#ifdef CONFIG_DAWN_OBJECT_HAS_NAME
49 const char *getClassNameStr() const override
50 {
51 return "modbus_tcp";
52 }
53#endif
54
55 int configure() override;
56 int init() override;
57 int deinit() override;
58 int doStart() override;
59 int doStop() override;
60 bool hasThread() const override;
61
62 constexpr static SObjectId::ObjectId objectId(uint16_t id)
63 {
66 }
67
68 constexpr static SObjectCfg::ObjectCfgId cfgId(bool rw, uint8_t dtype, uint8_t size, uint8_t id)
69 {
72 }
73
74 constexpr static SObjectCfg::ObjectCfgId cfgIdIOBind(uint16_t size)
75 {
76 return CProtoModbusTcp::cfgId(false, SObjectId::DTYPE_ANY, size, PROTO_MODBUS_TCP_CFG_IOBIND);
77 }
78
79 constexpr static SObjectCfg::ObjectCfgId cfgIdPort()
80 {
81 return CProtoModbusTcp::cfgId(true, SObjectId::DTYPE_UINT16, 1, PROTO_MODBUS_TCP_CFG_PORT);
82 }
83
84private:
85 nxmb_handle_t mbhandle;
86 struct nxmb_callbacks_s callbacks;
87 uint8_t saddr;
88 uint16_t port;
89
90 int configureDesc(const CDescObject &desc);
91 void allocObject(SProtoModbusIOBind *alloc);
92 void thread();
93 int modbusInitialize();
94
95 CIOCommon *getIO_(SObjectId::ObjectId id) override
96 {
97 return this->getIO(id);
98 };
99};
100
101} // 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 I/O objects.
Definition common.hxx:27
Base class for all protocol implementations.
Definition common.hxx:23
@ PROTO_CLASS_MODBUS_TCP
Modbus TCP (network implementation).
Definition common.hxx:71
Modbus register management base class (shared RTU/TCP logic).
Definition regs.hxx:30
Modbus TCP protocol implementation.
Definition tcp.hxx:27
int init()
One-time initialize object after bindings are resolved.
Definition tcp.cxx:178
int doStart()
Start implementation hook.
Definition tcp.cxx:218
int doStop()
Stop implementation hook.
Definition tcp.cxx:232
bool hasThread() const
Check if a background thread is active.
Definition tcp.cxx:239
int configure()
Configure object from descriptor data.
Definition tcp.cxx:164
int deinit()
De-initialize object.
Definition tcp.cxx:204
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
I/O binding configuration loaded from descriptor.
Definition regs.hxx:46
@ OBJTYPE_PROTO
Protocol object type.
Definition objectid.hxx:193
@ DTYPE_ANY
Wildcard data type (matches any actual type).
Definition objectid.hxx:68
@ DTYPE_UINT16
Unsigned 16-bit integer (0 to 65535).
Definition objectid.hxx:88
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