ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
modbus.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file modbus.h
6 * @brief Zero-heap Modbus TCP slave/server (Modbus Application Protocol v1.1b3).
7 *
8 * Split like the CoAP/SNMP services into a pure, host-testable core and an
9 * ESP32-only TCP transport:
10 *
11 * - protocore_modbus_process_adu() takes a complete Modbus TCP ADU (MBAP header + PDU) and
12 * produces the response ADU in a caller buffer - no sockets, no heap. It is
13 * unit-tested on the host (env:native_modbus).
14 * - protocore_modbus_rx() is the ProtoConn::PROTO_MODBUS data handler dispatched by the session layer;
15 * it frames ADUs out of the rx ring and feeds them through
16 * protocore_modbus_process_adu(). The slave keeps no per-connection state (a partial
17 * frame waits in the rx ring), so no accept/close hooks are needed. Open the
18 * port with listen(502, ProtoConn::PROTO_MODBUS).
19 *
20 * The data model is four fixed BSS tables (coils, discrete inputs, holding
21 * registers, input registers). The application reads and writes them with the
22 * accessors below; a write arriving from a client also fires protocore_modbus_on_write().
23 *
24 * Supported function codes: 0x01 Read Coils, 0x02 Read Discrete Inputs,
25 * 0x03 Read Holding Registers, 0x04 Read Input Registers, 0x05 Write Single Coil,
26 * 0x06 Write Single Register, 0x0F Write Multiple Coils, 0x10 Write Multiple
27 * Registers. Any other function code returns exception 0x01 (Illegal Function).
28 *
29 * Modbus has no authentication or encryption - run it only on a trusted network.
30 */
31
32#ifndef PROTOCORE_MODBUS_H
33#define PROTOCORE_MODBUS_H
34
35#include "protocore_config.h" // the entry point: protocore_types.h for the widths
36
37#if PROTOCORE_ENABLE_MODBUS
38
39EMBED_BEGIN_DECLS
40
41// PROTOCORE_MODBUS_BORROW - the bytes this module runs out of - is stated in protocore_config.h, which sums
42// it into its arena. A caller takes them once and passes the pointer to every call. How they
43// are carved is this module's and is never named here.
44
45/** @brief Largest Modbus TCP ADU (7-byte MBAP + 253-byte PDU). */
46#define MODBUS_ADU_MAX 260
47
48/** @brief Modbus function codes (Modbus Application Protocol §6). */
49EMBED_ENUM_BYTE(ModbusFunction, MODBUS_FC_READ_COILS = 0x01, MODBUS_FC_READ_DISCRETE_INPUTS = 0x02,
50 MODBUS_FC_READ_HOLDING_REGS = 0x03, MODBUS_FC_READ_INPUT_REGS = 0x04,
51 MODBUS_FC_WRITE_SINGLE_COIL = 0x05, MODBUS_FC_WRITE_SINGLE_REG = 0x06,
52 MODBUS_FC_WRITE_MULTIPLE_COILS = 0x0F, MODBUS_FC_WRITE_MULTIPLE_REGS = 0x10,
53 MODBUS_FC_MASK_WRITE_REG = 0x16, MODBUS_FC_READ_WRITE_MULTIPLE_REGS = 0x17, );
54
55/** @brief Modbus exception codes (Modbus Application Protocol §7). */
56EMBED_ENUM_BYTE(ModbusException, MODBUS_EX_ILLEGAL_FUNCTION = 0x01, MODBUS_EX_ILLEGAL_DATA_ADDRESS = 0x02,
57 MODBUS_EX_ILLEGAL_DATA_VALUE = 0x03, MODBUS_EX_SERVER_FAILURE = 0x04, );
58
59/**
60 * @brief Notified after a client write is applied to the data model.
61 *
62 * @param fc the write function code (5, 6, 0x0F, or 0x10).
63 * @param start first coil/register address written.
64 * @param count number of coils/registers written.
65 */
66typedef void (*ModbusWriteCb)(uint8_t fc, uint16_t start, uint16_t count);
67
68/** @brief What on_write takes: cb. */
69typedef struct
70{
71 ModbusWriteCb cb;
72} ModbusOnWriteArgs;
73
74/** @brief What get_coil takes: addr. */
75typedef struct
76{
77 uint16_t addr;
78} ModbusGetCoilArgs;
79
80/** @brief What set_coil takes: addr, on. */
81typedef struct
82{
83 uint16_t addr;
84 embed_bool on;
85} ModbusSetCoilArgs;
86
87/** @brief What get_discrete_input takes: addr. */
88typedef struct
89{
90 uint16_t addr;
91} ModbusGetDiscreteInputArgs;
92
93/** @brief What set_discrete_input takes: addr, on. */
94typedef struct
95{
96 uint16_t addr;
97 embed_bool on;
98} ModbusSetDiscreteInputArgs;
99
100/** @brief What get_holding_reg takes: addr. */
101typedef struct
102{
103 uint16_t addr;
104} ModbusGetHoldingRegArgs;
105
106/** @brief What set_holding_reg takes: addr, value. */
107typedef struct
108{
109 uint16_t addr;
110 uint16_t value;
111} ModbusSetHoldingRegArgs;
112
113/** @brief What get_input_reg takes: addr. */
114typedef struct
115{
116 uint16_t addr;
117} ModbusGetInputRegArgs;
118
119/** @brief What set_input_reg takes: addr, value. */
120typedef struct
121{
122 uint16_t addr;
123 uint16_t value;
124} ModbusSetInputRegArgs;
125
126/** @brief What process_adu takes: req, req_len, resp, ... */
127typedef struct
128{
129 const uint8_t *req;
130 size_t req_len;
131 uint8_t *resp;
132 size_t protocore_resp_cap;
133} ModbusProcessAduArgs;
134
135/** @brief What rtu_process_adu takes: req, req_len, resp, ... */
136typedef struct
137{
138 const uint8_t *req;
139 size_t req_len;
140 uint8_t *resp;
141 size_t protocore_resp_cap;
142 uint8_t my_addr;
143} ModbusRtuProcessAduArgs;
144
145/** @brief What rx takes: slot. */
146typedef struct
147{
148 uint8_t slot;
149} ModbusRxArgs;
150
151/** @brief The Layer 5 dispatch record; server/core/proto_handler.h defines it. */
152struct ProtoHandler;
153
154/**
155 * @brief Zero-heap Modbus TCP slave/server (Modbus Application Protocol v1.1b3).
156 *
157 * A caller sets the members a call takes, invokes it through ::Modbus with the bytes it runs
158 * out of, and reads the outcome off the same handle.
159 *
160 * Modbus.server_init(work);
161 *
162 * @var ModbusNs::on_write_args what on_write takes: cb
163 * @var ModbusNs::get_coil_args what get_coil takes: addr
164 * @var ModbusNs::set_coil_args what set_coil takes: addr, on
165 * @var ModbusNs::get_discrete_input_args what get_discrete_input takes: addr
166 * @var ModbusNs::set_discrete_input_args what set_discrete_input takes: addr, on
167 * @var ModbusNs::get_holding_reg_args what get_holding_reg takes: addr
168 * @var ModbusNs::set_holding_reg_args what set_holding_reg takes: addr, value
169 * @var ModbusNs::get_input_reg_args what get_input_reg takes: addr
170 * @var ModbusNs::set_input_reg_args what set_input_reg takes: addr, value
171 * @var ModbusNs::process_adu_args what process_adu takes: req, req_len, resp,
172 * @var ModbusNs::rtu_process_adu_args what rtu_process_adu takes: req, req_len, resp,
173 * @var ModbusNs::rx_args what rx takes: slot
174 * @var ModbusNs::ok a call's true/false outcome
175 * @var ModbusNs::value the value a call reports
176 * @var ModbusNs::n number of response bytes written, or 0 to send nothing
177 * @var ModbusNs::ptr the pointer a call reports
178 * @var ModbusNs::server_init zero the entire data model and clear the write callback
179 * @var ModbusNs::on_write register a callback invoked after each client write (nullable)
180 * @var ModbusNs::get_coil get_coil
181 * @var ModbusNs::set_coil set_coil
182 * @var ModbusNs::get_discrete_input get_discrete_input
183 * @var ModbusNs::set_discrete_input set_discrete_input
184 * @var ModbusNs::get_holding_reg get_holding_reg
185 * @var ModbusNs::set_holding_reg set_holding_reg
186 * @var ModbusNs::get_input_reg get_input_reg
187 * @var ModbusNs::set_input_reg set_input_reg
188 * @var ModbusNs::process_adu process one Modbus TCP ADU and build the response ADU. Parses the ...
189 * @var ModbusNs::rtu_process_adu process one complete Modbus RTU ADU (`[addr][PDU][CRC16]`) for slave
190 * @var ModbusNs::rx frame and process received Modbus ADUs for the connection on slot
191 * @var ModbusNs::handler handler
192 *
193 * @c work is PROTOCORE_MODBUS_BORROW bytes the CALLER took, at an address it knows. It is not held past the call, so
194 * nothing here aliases it. How those bytes are carved is this module's and is never named here.
195 */
196typedef struct
197{
198 ModbusOnWriteArgs on_write_args;
199 ModbusGetCoilArgs get_coil_args;
200 ModbusSetCoilArgs set_coil_args;
201 ModbusGetDiscreteInputArgs get_discrete_input_args;
202 ModbusSetDiscreteInputArgs set_discrete_input_args;
203 ModbusGetHoldingRegArgs get_holding_reg_args;
204 ModbusSetHoldingRegArgs set_holding_reg_args;
205 ModbusGetInputRegArgs get_input_reg_args;
206 ModbusSetInputRegArgs set_input_reg_args;
207 ModbusProcessAduArgs process_adu_args;
208 ModbusRtuProcessAduArgs rtu_process_adu_args;
209 ModbusRxArgs rx_args;
210 embed_bool ok;
211 uint16_t value;
212 size_t n;
213 const struct ProtoHandler *ptr;
214} ModbusVars;
215
216/** @brief The operands and the outcome. */
217extern ModbusVars ModbusV;
218
219/** @brief The entries. */
220typedef struct
221{
222 void (*const server_init)(uint8_t *work);
223 void (*const on_write)(uint8_t *work);
224 void (*const get_coil)(uint8_t *work);
225 void (*const set_coil)(uint8_t *work);
226 void (*const get_discrete_input)(uint8_t *work);
227 void (*const set_discrete_input)(uint8_t *work);
228 void (*const get_holding_reg)(uint8_t *work);
229 void (*const set_holding_reg)(uint8_t *work);
230 void (*const get_input_reg)(uint8_t *work);
231 void (*const set_input_reg)(uint8_t *work);
232 void (*const process_adu)(uint8_t *work);
233 void (*const rtu_process_adu)(uint8_t *work);
234 void (*const rx)(uint8_t *work);
235 void (*const handler)(const uint8_t *work);
236} ModbusNs;
237
238// What the table binds, defined once in the .c and taking one parameter each: everything
239// else an entry needs is an operand in ModbusV or a region of the borrow at a fixed offset.
240void protocore_modbus_server_init(uint8_t *work);
241void protocore_modbus_on_write(uint8_t *work);
242void protocore_modbus_get_coil(uint8_t *work);
243void protocore_modbus_set_coil(uint8_t *work);
244void protocore_modbus_get_discrete_input(uint8_t *work);
245void protocore_modbus_set_discrete_input(uint8_t *work);
246void protocore_modbus_get_holding_reg(uint8_t *work);
247void protocore_modbus_set_holding_reg(uint8_t *work);
248void protocore_modbus_get_input_reg(uint8_t *work);
249void protocore_modbus_set_input_reg(uint8_t *work);
250void protocore_modbus_process_adu(uint8_t *work);
251void protocore_modbus_rtu_process_adu(uint8_t *work);
252void protocore_modbus_rx(uint8_t *work);
253void protocore_modbus_handler(const uint8_t *work);
254
255// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
256// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
257// `Modbus.server_init(work)` resolves to a named function and becomes a DIRECT call. An extern table
258// leaves the call indirect and the symbol live at every level, -O2 -flto included.
259static const ModbusNs Modbus __attribute__((unused)) = {
260 .server_init = protocore_modbus_server_init,
261 .on_write = protocore_modbus_on_write,
262 .get_coil = protocore_modbus_get_coil,
263 .set_coil = protocore_modbus_set_coil,
264 .get_discrete_input = protocore_modbus_get_discrete_input,
265 .set_discrete_input = protocore_modbus_set_discrete_input,
266 .get_holding_reg = protocore_modbus_get_holding_reg,
267 .set_holding_reg = protocore_modbus_set_holding_reg,
268 .get_input_reg = protocore_modbus_get_input_reg,
269 .set_input_reg = protocore_modbus_set_input_reg,
270 .process_adu = protocore_modbus_process_adu,
271 .rtu_process_adu = protocore_modbus_rtu_process_adu,
272 .rx = protocore_modbus_rx,
273 .handler = protocore_modbus_handler,
274};
275
276/**
277 * @brief The PROTOCORE_MODBUS_BORROW bytes this module's state lives in.
278 *
279 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
280 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
281 * walks, so the state lasts the life of the program.
282 *
283 * @return the span.
284 */
285uint8_t *protocore_modbus_span(void);
286
287EMBED_END_DECLS
288
289#endif // PROTOCORE_ENABLE_MODBUS
290
291#endif // PROTOCORE_MODBUS_H
EMBED_ENUM_BYTE(ProtoConn, PROTO_NONE=0, PROTO_HTTP=1, PROTO_TELNET=2, PROTO_SSH=3, PROTO_MODBUS=4, PROTO_OPCUA=5, PROTO_SSH_RFWD=6, PROTO_RELAY=7, PROTO_BRIDGE=8, PROTO_NTRIP_CASTER=9, PROTO_MESH=10, PROTO_UDP=11,)
Application protocol spoken on a listener port or connection slot.
Per-protocol connection event/poll callbacks (the server's dispatch vtable).