ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
df1.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 df1.h
6 * @brief Allen-Bradley DF1 full-duplex frame codec (PROTOCORE_ENABLE_DF1) - zero-heap framing +
7 * DLE byte-stuffing + BCC/CRC for the Rockwell serial PLC link layer.
8 *
9 * A DF1 full-duplex message frame (AB pub. 1770-6.5.16):
10 * @code
11 * DLE STX <application data, DLE bytes doubled> DLE ETX BCC | CRC
12 * @endcode
13 * - A data byte equal to DLE (0x10) is transmitted twice (DLE DLE); the doubled DLE is
14 * counted only once in the BCC/CRC.
15 * - BCC: the 2's complement of the modulo-256 sum of the application data bytes (the ETX
16 * is NOT included). One octet.
17 * - CRC: CRC-16 (poly X16+X15+X2+X0 = 0x8005, init 0x0000, reflected) over the application
18 * data bytes AND the ETX byte. Two octets, transmitted low byte first.
19 *
20 * This is the data-link framing layer; the DST/SRC/CMD/STS/TNS application header lives
21 * inside the application data. Field definitions verified against the 1770-6.5.16 manual.
22 *
23 * @author Douglas Quigg (dstroy0)
24 * @date 2026
25 */
26
27#ifndef PROTOCORE_DF1_H
28#define PROTOCORE_DF1_H
29
30#include "protocore_config.h" // the entry point: protocore_types.h for the widths
31
32#if PROTOCORE_ENABLE_DF1
33
34EMBED_BEGIN_DECLS
35
36// This module holds nothing between calls, so it carves no borrow and states none. An entry
37// takes one all the same, and never reads it, so every namespace in the tree is invoked the
38// same way.
39
40#define DF1_DLE 0x10
41#define DF1_STX 0x02
42#define DF1_ETX 0x03
43
44/** @brief Which error check the frame carries. */
45EMBED_ENUM_BYTE(Df1Check,
46 DF1_CHECK_BCC, ///< 1-octet block check character
47 DF1_CHECK_CRC ///< 2-octet CRC-16
48);
49
50/** @brief What bcc takes: data, len. */
51typedef struct
52{
53 const uint8_t *data;
54 size_t len;
55} Df1BccArgs;
56
57/** @brief What crc takes: data, len. */
58typedef struct
59{
60 const uint8_t *data;
61 size_t len;
62} Df1CrcArgs;
63
64/** @brief What build_frame takes: buf, cap, data, data_len, check. */
65typedef struct
66{
67 uint8_t *buf;
68 size_t cap;
69 const uint8_t *data;
70 size_t data_len;
71 Df1Check check; ///< DF1_CHECK_BCC (1 octet) or DF1_CHECK_CRC (2 octets, low byte first; CRC over the data + ETX)
72} Df1BuildFrameArgs;
73
74/** @brief What parse_frame takes: buf, len, check, out, out_cap, ... */
75typedef struct
76{
77 const uint8_t *buf;
78 size_t len;
79 Df1Check check;
80 uint8_t *out; ///< receives the de-stuffed application data
81 size_t out_cap; ///< capacity of out
82 size_t *out_len; ///< receives the application-data length
83} Df1ParseFrameArgs;
84
85/**
86 * @brief Allen-Bradley DF1 full-duplex frame codec (PROTOCORE_ENABLE_DF1) - zero-heap framing + DLE byte-stuffing +
87 * BCC/CRC for the Rockwell serial PLC link layer.
88 *
89 * A caller sets the members a call takes, invokes it through ::Df1 with the bytes it runs
90 * out of, and reads the outcome off the same handle.
91 *
92 * Df1.bcc_args.data = ...;
93 * Df1.bcc_args.len = ...;
94 * Df1.bcc(work);
95 * // Df1.value is what the call reports
96 *
97 * @var Df1Ns::bcc_args what bcc takes: data, len
98 * @var Df1Ns::crc_args what crc takes: data, len
99 * @var Df1Ns::build_frame_args what build_frame takes: buf, cap, data, data_len, check
100 * @var Df1Ns::parse_frame_args what parse_frame takes: buf, len, check, out, out_cap,
101 * @var Df1Ns::ok true on a complete, check-valid frame; false on bad framing, ...
102 * @var Df1Ns::value the value a call reports
103 * @var Df1Ns::u16 what a call reports
104 * @var Df1Ns::n total octets written, or 0 on overflow / bad input
105 * @var Df1Ns::bcc BCC: 2's complement of the modulo-256 sum of [data, data+len)
106 * @var Df1Ns::crc CRC-16/ARC (poly 0x8005 / 0xA001 reflected, init 0) over [data, ...
107 * @var Df1Ns::build_frame build a full-duplex frame around data: DLE STX + stuffed data + DLE ...
108 * @var Df1Ns::parse_frame parse + validate a full-duplex frame, un-stuffing the application ...
109 *
110 * @c work is bytes the CALLER holds. This module reads none of them: it carries nothing
111 * between calls, so there is no state to keep and nothing to wipe. The parameter is there so
112 * a caller drives every namespace the same way.
113 */
114typedef struct
115{
116 Df1BccArgs bcc_args;
117 Df1CrcArgs crc_args;
118 Df1BuildFrameArgs build_frame_args;
119 Df1ParseFrameArgs parse_frame_args;
120 embed_bool ok;
121 uint8_t value;
122 uint16_t u16;
123 size_t n;
124} Df1Vars;
125
126/** @brief The operands and the outcome. */
127extern Df1Vars Df1V;
128
129/** @brief The entries. */
130typedef struct
131{
132 void (*const bcc)(const uint8_t *work);
133 void (*const crc)(const uint8_t *work);
134 void (*const build_frame)(uint8_t *work);
135 void (*const parse_frame)(uint8_t *work);
136} Df1Ns;
137
138// What the table binds, defined once in the .c and taking one parameter each: everything
139// else an entry needs is an operand in Df1V or a region of the borrow at a fixed offset.
140void protocore_df1_bcc(const uint8_t *work);
141void protocore_df1_crc(const uint8_t *work);
142void protocore_df1_build_frame(uint8_t *work);
143void protocore_df1_parse_frame(uint8_t *work);
144
145// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
146// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
147// `Df1.bcc(work)` resolves to a named function and becomes a DIRECT call. An extern table
148// leaves the call indirect and the symbol live at every level, -O2 -flto included.
149static const Df1Ns Df1 __attribute__((unused)) = {
150 .bcc = protocore_df1_bcc,
151 .crc = protocore_df1_crc,
152 .build_frame = protocore_df1_build_frame,
153 .parse_frame = protocore_df1_parse_frame,
154};
155
156EMBED_END_DECLS
157
158#endif // PROTOCORE_ENABLE_DF1
159
160#endif // PROTOCORE_DF1_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.