Dawn Framework 1.0
Universal data acquisition framework for embedded systems
regs.hxx
1// dawn/include/dawn/proto/modbus/regs.hxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#pragma once
7
8#include <mutex>
9#include <vector>
10
11#include "dawn/common/object.hxx"
12#include "dawn/porting/config.hxx"
13#include <nxmodbus/nxmodbus.h>
14
15namespace dawn
16{
17// Forward declaration
18
19struct io_ddata_t;
20class CIOCommon;
21
30{
31public:
32 enum
33 {
41 };
42
45 struct
46 {
47 uint32_t type;
48 uint32_t config;
49 uint32_t start;
50 uint32_t size;
51 uint32_t objid[];
52 } typedef SProtoModbusIOBind;
53
56 struct
57 {
59 std::vector<io_ddata_t *> iodata;
60 std::mutex mutex;
61 void *data;
62 size_t regs;
63 size_t size;
64 int ios;
65 } typedef SProtoModbusRegs;
66
68 : initialized(false)
69 {
70 }
71
73
74 void valloc_push_back(SProtoModbusIOBind *alloc);
75 int createRegs();
76 int destroyRegs();
77
78#ifdef CONFIG_DAWN_PROTO_MODBUS_COIL
79 int coilsCb(uint8_t *buf, uint16_t addr, uint16_t ncoils, enum nxmb_regmode_e mode, void *priv);
80#endif
81
82#ifdef CONFIG_DAWN_PROTO_MODBUS_DISCRETE
83 int discreteCb(uint8_t *buf, uint16_t addr, uint16_t ndiscrete, void *priv);
84#endif
85
86#ifdef CONFIG_DAWN_PROTO_MODBUS_INPUT
87 int inputCb(uint8_t *buf, uint16_t addr, uint16_t nregs, void *priv);
88#endif
89
90#ifdef CONFIG_DAWN_PROTO_MODBUS_HOLDING
91 int holdingCb(uint8_t *buf, uint16_t addr, uint16_t nregs, enum nxmb_regmode_e mode, void *priv);
92#endif
93
94private:
95#ifdef CONFIG_DAWN_PROTO_MODBUS_SEEKABLE
96 struct
97 {
98 SProtoModbusRegs *reg;
99 size_t windowRegs;
100 std::vector<size_t> seekOffsets;
101 std::vector<size_t> readProgressWords;
102 } typedef SSeekableState;
103#endif
104
105 std::vector<SProtoModbusIOBind *> valloc;
106#ifdef CONFIG_DAWN_PROTO_MODBUS_COIL
107 std::vector<SProtoModbusRegs *> vcoils;
108#endif
109#ifdef CONFIG_DAWN_PROTO_MODBUS_DISCRETE
110 std::vector<SProtoModbusRegs *> vdiscrete;
111#endif
112#ifdef CONFIG_DAWN_PROTO_MODBUS_INPUT
113 std::vector<SProtoModbusRegs *> vinput;
114#endif
115#ifdef CONFIG_DAWN_PROTO_MODBUS_HOLDING
116 std::vector<SProtoModbusRegs *> vholding;
117#endif
118#ifdef CONFIG_DAWN_PROTO_MODBUS_SEEKABLE
119 std::vector<SSeekableState *> vseekable;
120#endif
121 bool initialized;
122
123 virtual CIOCommon *getIO_(SObjectId::ObjectId id) = 0;
124
125 SProtoModbusRegs *findGroup(uint16_t addr,
126 uint16_t n,
127 const std::vector<SProtoModbusRegs *> &regs);
128
129 const SProtoModbusRegs *hasOverlap(const std::vector<SProtoModbusRegs *> &regs,
130 uint32_t start,
131 size_t count) const;
132
133 void cleanupGroups(std::vector<SProtoModbusRegs *> &groups);
134
135#if defined(CONFIG_DAWN_PROTO_MODBUS_COIL) || defined(CONFIG_DAWN_PROTO_MODBUS_DISCRETE)
136 int regReadWriteCoil(uint8_t *buff, uint16_t n, int index, bool read, SProtoModbusRegs *reg);
137
138 int regReadWriteCoilPacked(uint8_t *buff,
139 uint16_t n,
140 int index,
141 bool read,
142 SProtoModbusRegs *reg);
143#endif
144
145#if defined(CONFIG_DAWN_PROTO_MODBUS_INPUT) || defined(CONFIG_DAWN_PROTO_MODBUS_HOLDING)
146 int regReadWriteHolding(uint8_t *buff, uint16_t n, int index, bool read, SProtoModbusRegs *reg);
147
148 int standardRegRW(uint8_t *buff, uint16_t n, int index, bool read, SProtoModbusRegs *reg);
149
150# ifdef CONFIG_DAWN_PROTO_MODBUS_SEEKABLE
151 int seekableRegRW(uint8_t *buff,
152 uint16_t n,
153 int index,
154 bool read,
155 SProtoModbusRegs *reg,
156 SSeekableState *seekState);
157# endif
158#endif
159
160#ifdef CONFIG_DAWN_PROTO_MODBUS_SEEKABLE
161 SSeekableState *findSeekableState(SProtoModbusRegs *reg);
162#endif
163
164 int finalizeRegGroup(SProtoModbusRegs *tmp,
165 const SProtoModbusIOBind *v,
166 size_t seekWindowRegs,
167 bool addSeekableState);
168};
169
170} // Namespace dawn
Modbus register management base class (shared RTU/TCP logic).
Definition regs.hxx:30
@ MODBUS_TYPE_DISCRETE_PACKED
Read-only bits packed in bytes.
Definition regs.hxx:37
@ MODBUS_TYPE_INPUT
Read-only 16-bit words.
Definition regs.hxx:38
@ MODBUS_TYPE_SEEKABLE
Seekable window over holding regs.
Definition regs.hxx:40
@ MODBUS_TYPE_COIL_PACKED
Read-write bits packed in bytes.
Definition regs.hxx:36
@ MODBUS_TYPE_COIL
Read-write bits (1 byte/bit).
Definition regs.hxx:34
@ MODBUS_TYPE_DISCRETE
Read-only bits (1 byte/bit).
Definition regs.hxx:35
@ MODBUS_TYPE_HOLDING
Read-write 16-bit words.
Definition regs.hxx:39
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
I/O binding configuration loaded from descriptor.
Definition regs.hxx:46
uint32_t type
Register type (MODBUS_TYPE_*).
Definition regs.hxx:47
uint32_t config
Register-type private configuration word.
Definition regs.hxx:48
uint32_t size
Number of I/O objects in this group.
Definition regs.hxx:50
uint32_t start
Start register address (0x0000-0xFFFF).
Definition regs.hxx:49
Runtime register group state.
Definition regs.hxx:57
void * data
Register data buffer.
Definition regs.hxx:61
std::vector< io_ddata_t * > iodata
I/O data for each object.
Definition regs.hxx:59
const SProtoModbusIOBind * cfg
Reference to bind config.
Definition regs.hxx:58
size_t regs
Number of registers.
Definition regs.hxx:62
size_t size
Data buffer size in bytes.
Definition regs.hxx:63
int ios
Number of I/O objects.
Definition regs.hxx:64
std::mutex mutex
Thread-safe access guard.
Definition regs.hxx:60
uint32_t ObjectId
ObjectID type - single 32-bit value.
Definition objectid.hxx:44