ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
simatic.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 simatic.h
6 * @brief Siemens SIMATIC serial point-to-point link (PROTOCORE_ENABLE_SIMATIC) - the 3964R link protocol +
7 * the RK512 computer-link telegrams, zero-heap.
8 *
9 * The pre-Ethernet Siemens point-to-point link, two layers:
10 *
11 * - **3964R** - a byte-oriented, half-duplex link protocol (S5/S7 PtP CP modules CP 341 / CP 441 /
12 * CP 524 / CP 525). A block is framed @code STX <data, DLE bytes doubled> DLE ETX [BCC] @endcode
13 * with an interactive per-block handshake: the sender emits STX and waits for the receiver's DLE
14 * (ready) before sending the block, then waits for a final DLE (ok) or NAK (retry). On a simultaneous
15 * STX collision the low-priority station yields. The "R" variant appends a BCC - the longitudinal XOR
16 * (even parity) of every character of the block after STX, i.e. the stuffed data and the terminating
17 * DLE ETX. A payload byte equal to DLE (0x10) is doubled (transparency); a doubled DLE contributes
18 * 0x10 ^ 0x10 = 0 to the XOR. So it does not change the BCC.
19 *
20 * - **RK512** - fixed-header request/reaction telegrams carried as the 3964R block payload: SEND (write
21 * words to the partner) and FETCH (read words from the partner), addressing a data block / flag / I-O
22 * area by number + word offset + count. Siemens words are big-endian.
23 *
24 * Pure, host-tested (native_simatic) against an independent python 3964R+RK512 reference peer; the
25 * RS-232 / RS-485 UART is the application's (like the other serial-bus codecs). Control-char handshake,
26 * QVZ/ZVZ timeouts, priority arbitration, BCC, and the RK512 header layout follow the Siemens "3964(R)
27 * transmission protocol" and "RK 512 computer link" CP-module manuals.
28 *
29 * @author Douglas Quigg (dstroy0)
30 * @date 2026
31 */
32
33#ifndef PROTOCORE_SIMATIC_H
34#define PROTOCORE_SIMATIC_H
35
36#include "protocore_config.h" // the entry point: protocore_types.h for the widths
37
38#if PROTOCORE_ENABLE_SIMATIC
39
40EMBED_BEGIN_DECLS
41
42// PROTOCORE_SIMATIC_BORROW - the bytes this module runs out of - is stated in protocore_config.h, which sums
43// it into its arena. A caller takes them once and passes the pointer to every call. How they
44// are carved is this module's and is never named here.
45
46// 3964R control characters (wire bytes).
47#define SIMATIC_STX 0x02
48#define SIMATIC_DLE 0x10
49#define SIMATIC_ETX 0x03
50#define SIMATIC_NAK 0x15
51
52// 3964R retry limits: Siemens RepetitionAttempts and BuildupAttempts, both default 6.
53#define SIMATIC_MAX_BLOCK_RETRY 6
54#define SIMATIC_MAX_CONN_RETRY 6
55
56/** @brief Link state (one job in flight; half-duplex). */
57EMBED_ENUM_BYTE(Simatic3964State,
58 SIMATIC3964_STATE_IDLE, ///< nothing in flight
59 SIMATIC3964_STATE_TX_AWAIT_CONN, ///< sent STX, awaiting the partner's connect DLE (QVZ)
60 SIMATIC3964_STATE_TX_AWAIT_END, ///< sent the block, awaiting the partner's end DLE / NAK (QVZ)
61 SIMATIC3964_STATE_RX_COLLECT ///< replied DLE to a partner STX, collecting the block (ZVZ per-char)
62);
63
64/** @brief Sink for one outbound byte (the state machine writes to the UART through this). */
65typedef void (*Simatic3964TxFn)(void *user, uint8_t byte);
66
67/** @brief Delivery of a fully received, check-valid block payload. */
68typedef void (*Simatic3964RxFn)(void *user, const uint8_t *data, size_t len);
69
70/**
71 * @brief 3964R link owner - all link state in one named context (no file-scope mutable). The tx/rx buffers
72 * are fixed BSS; @p user is threaded to the callbacks.
73 */
74typedef struct
75{
76 Simatic3964State state;
77 embed_bool high_priority; ///< the priority bit; on an STX collision the low-priority side yields to receive
78 embed_bool with_bcc; ///< the "R" (BCC) variant
79 Simatic3964TxFn tx; ///< outbound-byte sink
80 Simatic3964RxFn rx; ///< received-block delivery
81 void *user; ///< passed to tx / rx
82
83 uint8_t txbuf[PROTOCORE_SIMATIC_BLOCK_MAX]; ///< the block body being sent (built once, re-sent on retry)
84 size_t txlen;
85 uint8_t rxbuf[PROTOCORE_SIMATIC_BLOCK_MAX]; ///< raw inbound block body (pre un-stuffing)
86 size_t rxpos;
87
88 uint8_t block_retries; ///< block resends this connection (max 6)
89 uint8_t conn_retries; ///< connection reattempts (max 6)
90 uint32_t deadline_ms; ///< QVZ (handshake) / ZVZ (inter-char) expiry
91 embed_bool prev_dle; ///< rx terminator scan: previous rx byte was an un-paired DLE
92 embed_bool await_bcc; ///< rx: DLE ETX seen, the next byte is the BCC (R variant)
93} Simatic3964Ctx;
94
95/** @brief RK512 job / telegram identifier (the "Kennung" command byte). */
96EMBED_ENUM_BYTE(Rk512Cmd,
97 RK512_CMD_SEND = 0x00, ///< write words to the partner
98 RK512_CMD_FETCH = 0x01, ///< read words from the partner
99 RK512_CMD_REACTION = 0x02 ///< the partner's reaction (acknowledge) telegram
100);
101
102/** @brief RK512 memory area code (the operand area a job addresses). */
103EMBED_ENUM_BYTE(Rk512Area,
104 RK512_AREA_DB = 0x01, ///< data block (DBNR selects which)
105 RK512_AREA_DX = 0x02, ///< extended data block
106 RK512_AREA_MB = 0x03, ///< flag / marker (M)
107 RK512_AREA_EB = 0x04, ///< process input image (E)
108 RK512_AREA_AB = 0x05, ///< process output image (A)
109 RK512_AREA_PB = 0x06, ///< peripheral / I-O
110 RK512_AREA_ZB = 0x07, ///< counter (Z)
111 RK512_AREA_TB = 0x08 ///< timer (T)
112);
113
114/** @brief A decoded RK512 header. */
115typedef struct
116{
117 Rk512Cmd cmd;
118 Rk512Area area;
119 uint8_t dbnr; ///< data-block number (when area is DB/DX)
120 uint16_t addr; ///< start word offset (DBADR)
121 uint16_t count; ///< word count (ANZ)
122} Rk512Header;
123
124/** @brief What bcc_3964r takes: data, len. */
125typedef struct
126{
127 const uint8_t *data;
128 size_t len;
129} SimaticBcc3964rArgs;
130
131/** @brief What build_block_3964r takes: buf, cap, data, len, with_bcc. */
132typedef struct
133{
134 uint8_t *buf;
135 size_t cap;
136 const uint8_t *data;
137 size_t len;
138 embed_bool with_bcc;
139} SimaticBuildBlock3964rArgs;
140
141/** @brief What parse_block_3964r takes: buf, len, with_bcc, out, ... */
142typedef struct
143{
144 const uint8_t *buf;
145 size_t len;
146 embed_bool with_bcc;
147 uint8_t *out; ///< receives the de-stuffed payload
148 size_t out_cap; ///< capacity of out
149 size_t *out_len; ///< receives the payload length
150} SimaticParseBlock3964rArgs;
151
152/** @brief What init_3964r takes: ctx, high_priority, with_bcc, tx, ... */
153typedef struct
154{
155 Simatic3964Ctx *ctx;
156 embed_bool high_priority;
157 embed_bool with_bcc;
158 Simatic3964TxFn tx;
159 Simatic3964RxFn rx;
160 void *user;
161} SimaticInit3964rArgs;
162
163/** @brief What send_3964r takes: ctx, data, len, now_ms. */
164typedef struct
165{
166 Simatic3964Ctx *ctx;
167 const uint8_t *data;
168 size_t len;
169 uint32_t now_ms;
170} SimaticSend3964rArgs;
171
172/** @brief What rx_byte_3964r takes: ctx, b, now_ms. */
173typedef struct
174{
175 Simatic3964Ctx *ctx;
176 uint8_t b;
177 uint32_t now_ms;
178} SimaticRxByte3964rArgs;
179
180/** @brief What tick_3964r takes: ctx, now_ms. */
181typedef struct
182{
183 Simatic3964Ctx *ctx;
184 uint32_t now_ms;
185} SimaticTick3964rArgs;
186
187/** @brief What idle_3964r takes: ctx. */
188typedef struct
189{
190 const Simatic3964Ctx *ctx;
191} SimaticIdle3964rArgs;
192
193/** @brief What build_send_rk512 takes: buf, cap, area, dbnr, addr, ... */
194typedef struct
195{
196 uint8_t *buf;
197 size_t cap;
198 Rk512Area area;
199 uint8_t dbnr;
200 uint16_t addr;
201 const uint16_t *words;
202 uint16_t wcount;
203} SimaticBuildSendRk512Args;
204
205/** @brief What build_fetch_rk512 takes: buf, cap, area, dbnr, addr, ... */
206typedef struct
207{
208 uint8_t *buf;
209 size_t cap;
210 Rk512Area area;
211 uint8_t dbnr;
212 uint16_t addr;
213 uint16_t wcount;
214} SimaticBuildFetchRk512Args;
215
216/** @brief What build_reaction_rk512 takes: buf, cap, status. */
217typedef struct
218{
219 uint8_t *buf;
220 size_t cap;
221 uint16_t status;
222} SimaticBuildReactionRk512Args;
223
224/** @brief What parse_header_rk512 takes: buf, len, out. */
225typedef struct
226{
227 const uint8_t *buf;
228 size_t len;
229 Rk512Header *out;
230} SimaticParseHeaderRk512Args;
231
232/** @brief What parse_reaction_rk512 takes: buf, len, status, data, ... */
233typedef struct
234{
235 const uint8_t *buf;
236 size_t len;
237 uint16_t *status;
238 const uint8_t **data;
239 size_t *dlen;
240} SimaticParseReactionRk512Args;
241
242/**
243 * @brief Siemens SIMATIC serial point-to-point link (PROTOCORE_ENABLE_SIMATIC) - the 3964R link protocol + the RK512
244 * computer-link telegrams, zero-heap.
245 *
246 * A caller sets the members a call takes, invokes it through ::Simatic with the bytes it runs
247 * out of, and reads the outcome off the same handle.
248 *
249 * Simatic.bcc_3964r_args.data = ...;
250 * Simatic.bcc_3964r_args.len = ...;
251 * Simatic.bcc_3964r(work);
252 * // Simatic.value is what the call reports
253 *
254 * @var SimaticNs::bcc_3964r_args what bcc_3964r takes: data, len
255 * @var SimaticNs::build_block_3964r_args what build_block_3964r takes: buf, cap, data, len, with_bcc
256 * @var SimaticNs::parse_block_3964r_args what parse_block_3964r takes: buf, len, with_bcc, out,
257 * @var SimaticNs::init_3964r_args what init_3964r takes: ctx, high_priority, with_bcc, tx,
258 * @var SimaticNs::send_3964r_args what send_3964r takes: ctx, data, len, now_ms
259 * @var SimaticNs::rx_byte_3964r_args what rx_byte_3964r takes: ctx, b, now_ms
260 * @var SimaticNs::tick_3964r_args what tick_3964r takes: ctx, now_ms
261 * @var SimaticNs::idle_3964r_args what idle_3964r takes: ctx
262 * @var SimaticNs::build_send_rk512_args what build_send_rk512 takes: buf, cap, area, dbnr, addr,
263 * @var SimaticNs::build_fetch_rk512_args what build_fetch_rk512 takes: buf, cap, area, dbnr, addr,
264 * @var SimaticNs::build_reaction_rk512_args what build_reaction_rk512 takes: buf, cap, status
265 * @var SimaticNs::parse_header_rk512_args what parse_header_rk512 takes: buf, len, out
266 * @var SimaticNs::parse_reaction_rk512_args what parse_reaction_rk512 takes: buf, len, status, data,
267 * @var SimaticNs::ok true on a complete, check-valid block; false on bad framing, a lone ...
268 * @var SimaticNs::value the value a call reports
269 * @var SimaticNs::n octets written, or 0 on overflow / bad input
270 * @var SimaticNs::bcc_3964r 3964R BCC: the longitudinal XOR (even parity) over len bytes at ...
271 * @var SimaticNs::build_block_3964r build the 3964R block body: DLE-stuffed data, then DLE ETX, then ...
272 * @var SimaticNs::parse_block_3964r parse + validate a 3964R block body (the bytes after STX): un-stuff ...
273 * @var SimaticNs::init_3964r initialize the link. high_priority: one end true, the other false ...
274 * @var SimaticNs::send_3964r start sending data (one job in flight). Emits STX and arms the ...
275 * @var SimaticNs::rx_byte_3964r feed one inbound byte at now_ms; drives the handshake / block ...
276 * @var SimaticNs::tick_3964r drive timeouts (QVZ/ZVZ) + retries; call periodically with the ...
277 * @var SimaticNs::idle_3964r true when no job is in flight and no block is being received
278 * @var SimaticNs::build_send_rk512 build a SEND telegram header + the wcount big-endian data words at ...
279 * @var SimaticNs::build_fetch_rk512 build a FETCH telegram header (no data words - the partner returns ...
280 * @var SimaticNs::build_reaction_rk512 build a reaction (acknowledge) telegram carrying status (0 = ok)
281 * @var SimaticNs::parse_header_rk512 parse an RK512 header off a telegram. true on a complete, valid ...
282 * @var SimaticNs::parse_reaction_rk512 parse a reaction telegram: the status word, and (for a FETCH ...
283 *
284 * @c work is PROTOCORE_SIMATIC_BORROW bytes the CALLER took, at an address it knows. It is not held past the call, so
285 * nothing here aliases it. How those bytes are carved is this module's and is never named here.
286 */
287typedef struct
288{
289 SimaticBcc3964rArgs bcc_3964r_args;
290 SimaticBuildBlock3964rArgs build_block_3964r_args;
291 SimaticParseBlock3964rArgs parse_block_3964r_args;
292 SimaticInit3964rArgs init_3964r_args;
293 SimaticSend3964rArgs send_3964r_args;
294 SimaticRxByte3964rArgs rx_byte_3964r_args;
295 SimaticTick3964rArgs tick_3964r_args;
296 SimaticIdle3964rArgs idle_3964r_args;
297 SimaticBuildSendRk512Args build_send_rk512_args;
298 SimaticBuildFetchRk512Args build_fetch_rk512_args;
299 SimaticBuildReactionRk512Args build_reaction_rk512_args;
300 SimaticParseHeaderRk512Args parse_header_rk512_args;
301 SimaticParseReactionRk512Args parse_reaction_rk512_args;
302 embed_bool ok;
303 uint8_t value;
304 size_t n;
305} SimaticVars;
306
307/** @brief The operands and the outcome. */
308extern SimaticVars SimaticV;
309
310/** @brief The entries. */
311typedef struct
312{
313 void (*const bcc_3964r)(const uint8_t *work);
314 void (*const build_block_3964r)(const uint8_t *work);
315 void (*const parse_block_3964r)(uint8_t *work);
316 void (*const init_3964r)(const uint8_t *work);
317 void (*const send_3964r)(uint8_t *work);
318 void (*const rx_byte_3964r)(uint8_t *work);
319 void (*const tick_3964r)(const uint8_t *work);
320 void (*const idle_3964r)(const uint8_t *work);
321 void (*const build_send_rk512)(const uint8_t *work);
322 void (*const build_fetch_rk512)(const uint8_t *work);
323 void (*const build_reaction_rk512)(const uint8_t *work);
324 void (*const parse_header_rk512)(const uint8_t *work);
325 void (*const parse_reaction_rk512)(const uint8_t *work);
326} SimaticNs;
327
328// What the table binds, defined once in the .c and taking one parameter each: everything
329// else an entry needs is an operand in SimaticV or a region of the borrow at a fixed offset.
330void protocore_simatic_bcc_3964r(const uint8_t *work);
331void protocore_simatic_build_block_3964r(const uint8_t *work);
332void protocore_simatic_parse_block_3964r(uint8_t *work);
333void protocore_simatic_init_3964r(const uint8_t *work);
334void protocore_simatic_send_3964r(uint8_t *work);
335void protocore_simatic_rx_byte_3964r(uint8_t *work);
336void protocore_simatic_tick_3964r(const uint8_t *work);
337void protocore_simatic_idle_3964r(const uint8_t *work);
338void protocore_simatic_build_send_rk512(const uint8_t *work);
339void protocore_simatic_build_fetch_rk512(const uint8_t *work);
340void protocore_simatic_build_reaction_rk512(const uint8_t *work);
341void protocore_simatic_parse_header_rk512(const uint8_t *work);
342void protocore_simatic_parse_reaction_rk512(const uint8_t *work);
343
344// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
345// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
346// `Simatic.bcc_3964r(work)` resolves to a named function and becomes a DIRECT call. An extern table
347// leaves the call indirect and the symbol live at every level, -O2 -flto included.
348static const SimaticNs Simatic __attribute__((unused)) = {
349 .bcc_3964r = protocore_simatic_bcc_3964r,
350 .build_block_3964r = protocore_simatic_build_block_3964r,
351 .parse_block_3964r = protocore_simatic_parse_block_3964r,
352 .init_3964r = protocore_simatic_init_3964r,
353 .send_3964r = protocore_simatic_send_3964r,
354 .rx_byte_3964r = protocore_simatic_rx_byte_3964r,
355 .tick_3964r = protocore_simatic_tick_3964r,
356 .idle_3964r = protocore_simatic_idle_3964r,
357 .build_send_rk512 = protocore_simatic_build_send_rk512,
358 .build_fetch_rk512 = protocore_simatic_build_fetch_rk512,
359 .build_reaction_rk512 = protocore_simatic_build_reaction_rk512,
360 .parse_header_rk512 = protocore_simatic_parse_header_rk512,
361 .parse_reaction_rk512 = protocore_simatic_parse_reaction_rk512,
362};
363
364/**
365 * @brief The PROTOCORE_SIMATIC_BORROW bytes this module's state lives in.
366 *
367 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
368 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
369 * walks, so the state lasts the life of the program.
370 *
371 * @return the span.
372 */
373uint8_t *protocore_simatic_span(void);
374
375EMBED_END_DECLS
376
377#endif // PROTOCORE_ENABLE_SIMATIC
378
379#endif // PROTOCORE_SIMATIC_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.
#define PROTOCORE_SIMATIC_BLOCK_MAX
3964R block-body buffer size (built/received bytes: DLE-stuffed payload + DLE ETX + BCC).