ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
devicenet.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 devicenet.h
6 * @brief DeviceNet link-adaptation codec (PROTOCORE_ENABLE_DEVICENET) - the CAN-specific layer of
7 * "CIP over CAN".
8 *
9 * DeviceNet (ODVA) carries CIP over classic CAN. The CIP application layer (services, EPATH,
10 * data) is the same one the EtherNet/IP codec uses, so build the message body with the
11 * existing `Cip.*` functions (`PROTOCORE_ENABLE_CIP`). This module supplies the DeviceNet-specific
12 * link adaptation that is NOT part of CIP:
13 *
14 * - The 11-bit CAN **identifier** as a Message Group (1..4) + Message ID + MAC ID, per the
15 * DeviceNet identifier allocation:
16 * @code
17 * Group 1: 0 MsgID(4) SourceMAC(6) ids 0x000-0x3FF
18 * Group 2: 10 MAC(6) MsgID(3) ids 0x400-0x5FF
19 * Group 3: 11 MsgID(3) SourceMAC(6) ids 0x600-0x7BF
20 * Group 4: 11111 MsgID(6) ids 0x7C0-0x7EF
21 * @endcode
22 * - The explicit-message **header octet** (FRAG | XID | MAC ID).
23 * - The **fragmentation protocol** (type + modulo-64 count) and a reassembler for explicit
24 * messages longer than one 8-octet frame.
25 *
26 * Pure and host-tested. Drive it from the ESP32 TWAI peripheral or an MCP2515 over SPI to
27 * bridge a DeviceNet segment onto Wi-Fi.
28 *
29 * @author Douglas Quigg (dstroy0)
30 * @date 2026
31 */
32
33#ifndef PROTOCORE_DEVICENET_H
34#define PROTOCORE_DEVICENET_H
35
36#include "protocore_config.h" // the entry point: protocore_types.h for the widths
37
38#if PROTOCORE_ENABLE_DEVICENET
39
40EMBED_BEGIN_DECLS
41
42// This module holds nothing between calls, so it carves no borrow and states none. An entry
43// takes one all the same, and never reads it, so every namespace in the tree is invoked the
44// same way.
45
46// Message-group identifier bases / field widths.
47#define DEVICENET_G1_BASE 0x000u ///< Message Group 1 (0x000-0x3FF)
48#define DEVICENET_G2_BASE 0x400u ///< Message Group 2 (0x400-0x5FF)
49#define DEVICENET_G3_BASE 0x600u ///< Message Group 3 (0x600-0x7BF)
50#define DEVICENET_G4_BASE 0x7C0u ///< Message Group 4 (0x7C0-0x7EF)
51#define DEVICENET_MAC_MASK 0x3Fu ///< MAC IDs are 0..63
52
53// Common Group 2 message IDs (predefined master/slave connection set).
54#define DEVICENET_G2_UNCONNECTED_EXPLICIT_REQ 4u ///< unconnected explicit request to a slave
55#define DEVICENET_G2_EXPLICIT_RESPONSE 3u ///< explicit / unconnected response from a slave
56#define DEVICENET_G2_POLL_COMMAND 5u ///< Poll command / change-of-state to a slave
57#define DEVICENET_G2_DUP_MAC_CHECK 7u ///< Duplicate MAC ID check
58
59// Explicit-message header octet fields.
60#define DEVICENET_HDR_FRAG 0x80u ///< this body is fragmented (a fragmentation octet follows)
61#define DEVICENET_HDR_XID 0x40u ///< transaction-id bit
62
63// Fragmentation octet: type in the top 2 bits, modulo-64 count in the low 6.
64#define DEVICENET_FRAG_FIRST 0x00u ///< first fragment
65#define DEVICENET_FRAG_MIDDLE 0x40u ///< middle fragment
66#define DEVICENET_FRAG_LAST 0x80u ///< last fragment
67#define DEVICENET_FRAG_ACK 0xC0u ///< fragment acknowledge
68#define DEVICENET_FRAG_TYPE_MASK 0xC0u
69#define DEVICENET_FRAG_COUNT_MASK 0x3Fu
70
71/** @brief DeviceNet message groups. */
72EMBED_ENUM_BYTE(DeviceNetGroup, DEVICENET_GROUP_1 = 1, DEVICENET_GROUP_2 = 2, DEVICENET_GROUP_3 = 3,
73 DEVICENET_GROUP_4 = 4, );
74
75/** @brief A decoded DeviceNet identifier. */
76typedef struct
77{
78 DeviceNetGroup group;
79 uint8_t msg_id; ///< message id within the group
80 uint8_t mac_id; ///< source / node MAC id (0..63; not present for Group 4)
81} DeviceNetId;
82
83/** @brief Result of feeding a frame to the fragmentation reassembler. */
84EMBED_ENUM_BYTE(DeviceNetFragResult, DEVICENET_FRAG_IGNORED = 0, DEVICENET_FRAG_STARTED, DEVICENET_FRAG_PROGRESS,
85 DEVICENET_FRAG_COMPLETE, DEVICENET_FRAG_ERR, );
86
87/** @brief Fragmented-message reassembly context. */
88typedef struct
89{
90 embed_bool active;
91 uint8_t next_count; ///< next expected modulo-64 fragment count
92 uint16_t len; ///< octets stored so far
93 uint8_t buf[PROTOCORE_DEVICENET_MSG_MAX]; ///< reassembled body (excludes the fragmentation octets)
94} DeviceNetFragRx;
95
96#include "shared/can/can.h" // CanFrame: the type a parameter points at
97
98/** @brief What encode_id takes: id, group, msg_id, mac_id. */
99typedef struct
100{
101 uint32_t *id;
102 DeviceNetGroup group;
103 uint8_t msg_id;
104 uint8_t mac_id;
105} DevicenetEncodeIdArgs;
106
107/** @brief What decode_id takes: can_id, out. */
108typedef struct
109{
110 uint32_t can_id;
111 DeviceNetId *out;
112} DevicenetDecodeIdArgs;
113
114/** @brief What msg_header takes: frag, xid, mac_id. */
115typedef struct
116{
117 embed_bool frag;
118 embed_bool xid;
119 uint8_t mac_id;
120} DevicenetMsgHeaderArgs;
121
122/** @brief What frag_octet takes: type, count. */
123typedef struct
124{
125 uint8_t type;
126 uint8_t count;
127} DevicenetFragOctetArgs;
128
129/** @brief What build_explicit takes: out, group, msg_id, mac_id, ... */
130typedef struct
131{
132 CanFrame *out;
133 DeviceNetGroup group;
134 uint8_t msg_id;
135 uint8_t mac_id;
136 const uint8_t *body;
137 uint8_t body_len;
138} DevicenetBuildExplicitArgs;
139
140/** @brief What build_fragment takes: out, group, msg_id, mac_id, xid, ... */
141typedef struct
142{
143 CanFrame *out;
144 DeviceNetGroup group;
145 uint8_t msg_id;
146 uint8_t mac_id;
147 embed_bool xid;
148 uint8_t frag_type;
149 uint8_t frag_count;
150 const uint8_t *data;
151 uint8_t data_len;
152} DevicenetBuildFragmentArgs;
153
154/** @brief What frag_reset takes: rx. */
155typedef struct
156{
157 DeviceNetFragRx *rx;
158} DevicenetFragResetArgs;
159
160/** @brief What frag_feed takes: rx, body, body_len. */
161typedef struct
162{
163 DeviceNetFragRx *rx;
164 const uint8_t *body;
165 uint8_t body_len;
166} DevicenetFragFeedArgs;
167
168/**
169 * @brief DeviceNet link-adaptation codec (PROTOCORE_ENABLE_DEVICENET) - the CAN-specific layer of "CIP over CAN".
170 * DeviceNet (ODVA) carries CIP over classic CAN.
171 *
172 * A caller sets the members a call takes, invokes it through ::Devicenet with the bytes it runs
173 * out of, and reads the outcome off the same handle.
174 *
175 * Devicenet.encode_id_args.id = ...;
176 * Devicenet.encode_id_args.group = ...;
177 * Devicenet.encode_id_args.msg_id = ...;
178 * Devicenet.encode_id_args.mac_id = ...;
179 * Devicenet.encode_id(work);
180 * // Devicenet.ok is what the call reports
181 *
182 * @var DevicenetNs::encode_id_args what encode_id takes: id, group, msg_id, mac_id
183 * @var DevicenetNs::decode_id_args what decode_id takes: can_id, out
184 * @var DevicenetNs::msg_header_args what msg_header takes: frag, xid, mac_id
185 * @var DevicenetNs::frag_octet_args what frag_octet takes: type, count
186 * @var DevicenetNs::build_explicit_args what build_explicit takes: out, group, msg_id, mac_id,
187 * @var DevicenetNs::build_fragment_args what build_fragment takes: out, group, msg_id, mac_id, xid,
188 * @var DevicenetNs::frag_reset_args what frag_reset takes: rx
189 * @var DevicenetNs::frag_feed_args what frag_feed takes: rx, body, body_len
190 * @var DevicenetNs::ok true on success; false on a null out, data_len > 6, a null data ...
191 * @var DevicenetNs::value the value a call reports
192 * @var DevicenetNs::frag what a call reports
193 * @var DevicenetNs::encode_id encode a DeviceNet 11-bit CAN id. mac_id is ignored for Group 4
194 * @var DevicenetNs::decode_id decode an 11-bit CAN id into its DeviceNet group / message id / MAC ...
195 * @var DevicenetNs::msg_header compose the explicit-message header octet (FRAG / XID / MAC id)
196 * @var DevicenetNs::frag_octet compose a fragmentation octet from a type (DEVICENET_FRAG_*) and a ...
197 * @var DevicenetNs::build_explicit build a single-frame explicit message: [header octet][body...] at ...
198 * @var DevicenetNs::build_fragment build one fragment of a fragmented explicit message (the sender ...
199 * @var DevicenetNs::frag_reset reset a reassembly context to idle
200 * @var DevicenetNs::frag_feed feed a received frame's body (the octets after the CAN id) to the ...
201 *
202 * @c work is bytes the CALLER holds. This module reads none of them: it carries nothing
203 * between calls, so there is no state to keep and nothing to wipe. The parameter is there so
204 * a caller drives every namespace the same way.
205 */
206typedef struct
207{
208 DevicenetEncodeIdArgs encode_id_args;
209 DevicenetDecodeIdArgs decode_id_args;
210 DevicenetMsgHeaderArgs msg_header_args;
211 DevicenetFragOctetArgs frag_octet_args;
212 DevicenetBuildExplicitArgs build_explicit_args;
213 DevicenetBuildFragmentArgs build_fragment_args;
214 DevicenetFragResetArgs frag_reset_args;
215 DevicenetFragFeedArgs frag_feed_args;
216 embed_bool ok;
217 uint8_t value;
218 DeviceNetFragResult frag;
219} DevicenetVars;
220
221/** @brief The operands and the outcome. */
222extern DevicenetVars DevicenetV;
223
224/** @brief The entries. */
225typedef struct
226{
227 void (*const encode_id)(const uint8_t *work);
228 void (*const decode_id)(const uint8_t *work);
229 void (*const msg_header)(const uint8_t *work);
230 void (*const frag_octet)(const uint8_t *work);
231 void (*const build_explicit)(const uint8_t *work);
232 void (*const build_fragment)(const uint8_t *work);
233 void (*const frag_reset)(const uint8_t *work);
234 void (*const frag_feed)(const uint8_t *work);
235} DevicenetNs;
236
237// What the table binds, defined once in the .c and taking one parameter each: everything
238// else an entry needs is an operand in DevicenetV or a region of the borrow at a fixed offset.
239void protocore_devicenet_encode_id(const uint8_t *work);
240void protocore_devicenet_decode_id(const uint8_t *work);
241void protocore_devicenet_msg_header(const uint8_t *work);
242void protocore_devicenet_frag_octet(const uint8_t *work);
243void protocore_devicenet_build_explicit(const uint8_t *work);
244void protocore_devicenet_build_fragment(const uint8_t *work);
245void protocore_devicenet_frag_reset(const uint8_t *work);
246void protocore_devicenet_frag_feed(const uint8_t *work);
247
248// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
249// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
250// `Devicenet.encode_id(work)` resolves to a named function and becomes a DIRECT call. An extern table
251// leaves the call indirect and the symbol live at every level, -O2 -flto included.
252static const DevicenetNs Devicenet __attribute__((unused)) = {
253 .encode_id = protocore_devicenet_encode_id,
254 .decode_id = protocore_devicenet_decode_id,
255 .msg_header = protocore_devicenet_msg_header,
256 .frag_octet = protocore_devicenet_frag_octet,
257 .build_explicit = protocore_devicenet_build_explicit,
258 .build_fragment = protocore_devicenet_build_fragment,
259 .frag_reset = protocore_devicenet_frag_reset,
260 .frag_feed = protocore_devicenet_frag_feed,
261};
262
263EMBED_END_DECLS
264
265#endif // PROTOCORE_ENABLE_DEVICENET
266
267#endif // PROTOCORE_DEVICENET_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.
Shared CAN 2.0 frame type for the CAN-based industrial codecs (one source of truth).
Definition can.h:44