ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
fins.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 fins.h
6 * @brief Omron FINS frame codec (PROTOCORE_ENABLE_FINS) - zero-heap command/response builder +
7 * parser for the Factory Interface Network Service (FINS/UDP), so a device can talk
8 * to an Omron PLC over the shipped UDP transport.
9 *
10 * A FINS message is a 10-octet header then the command code and data:
11 * @code
12 * ICF RSV GCT DNA DA1 DA2 SNA SA1 SA2 SID MRC SRC [params / data...]
13 * @endcode
14 * - ICF: bit 6 = command(0)/response(1), bit 0 = response required(0)/not(1), bit 7 = use
15 * gateway. RSV = 0, GCT = 0x02. DNA/DA1/DA2 = destination net/node/unit; SNA/SA1/SA2 =
16 * source; SID = service id (echoed in the response).
17 * - MRC/SRC are the main/sub command code. A response inserts a 2-octet end code
18 * (MRES/SRES) before its data; MRES = SRES = 0 means normal completion.
19 * - Multi-octet command parameters (addresses, counts) are big-endian.
20 *
21 * FINS/UDP carries this frame directly (UDP provides integrity, so there is no checksum);
22 * FINS/TCP would prepend its own header. This is the message codec; the send is the app's.
23 *
24 * @author Douglas Quigg (dstroy0)
25 * @date 2026
26 */
27
28#ifndef PROTOCORE_FINS_H
29#define PROTOCORE_FINS_H
30
31#include "protocore_config.h" // the entry point: protocore_types.h for the widths
32
33#if PROTOCORE_ENABLE_FINS
34
35EMBED_BEGIN_DECLS
36
37// This module holds nothing between calls, so it carves no borrow and states none. An entry
38// takes one all the same, and never reads it, so every namespace in the tree is invoked the
39// same way.
40
41#define FINS_HEADER_SIZE 10
42
43#define FINS_ICF_COMMAND 0x80 ///< command, response required, gateway
44#define FINS_ICF_RESPONSE 0xC0 ///< response
45#define FINS_ICF_NO_RESPONSE 0x01 ///< OR into ICF: response not required
46
47// Common command codes (MRC, SRC).
48#define FINS_MRC_MEMORY_AREA 0x01
49#define FINS_SRC_MEMORY_AREA_READ 0x01
50#define FINS_SRC_MEMORY_AREA_WRITE 0x02
51#define FINS_MRC_OPERATING_MODE 0x04
52#define FINS_SRC_RUN 0x01
53#define FINS_SRC_STOP 0x02
54
55/** @brief The operating mode requested by a RUN (0401) command. */
56EMBED_ENUM_BYTE(FinsRunMode,
57 FINS_RUN_MODE_MONITOR = 0x02, ///< MONITOR mode (program runs, online edits allowed)
58 FINS_RUN_MODE_RUN = 0x04, ///< RUN mode (program runs, no online edits)
59);
60
61/** @brief The 10-octet FINS routing header. */
62typedef struct
63{
64 uint8_t icf;
65 uint8_t rsv;
66 uint8_t gct;
67 uint8_t dna;
68 uint8_t da1;
69 uint8_t da2; ///< destination network / node / unit
70 uint8_t sna;
71 uint8_t sa1;
72 uint8_t sa2; ///< source network / node / unit
73 uint8_t sid; ///< service id
74} FinsHeader;
75
76/** @brief A parsed command (request side). @ref params points INTO the source buffer. */
77typedef struct
78{
79 FinsHeader header;
80 uint8_t mrc;
81 uint8_t src;
82 const uint8_t *params;
83 size_t params_len;
84} FinsCommand;
85
86/** @brief A parsed response. @ref data points INTO the source buffer. */
87typedef struct
88{
89 FinsHeader header;
90 uint8_t mrc;
91 uint8_t src; ///< echoed command code
92 uint8_t mres;
93 uint8_t sres; ///< end code (0/0 = normal completion)
94 const uint8_t *data;
95 size_t data_len;
96} FinsResponse;
97
98/** @brief What build_command takes: buf, cap, h, mrc, src, params, ... */
99typedef struct
100{
101 uint8_t *buf;
102 size_t cap;
103 const FinsHeader *h;
104 uint8_t mrc;
105 uint8_t src;
106 const uint8_t *params;
107 size_t params_len;
108} FinsBuildCommandArgs;
109
110/** @brief What build_memory_area_read takes: buf, cap, h, area, ... */
111typedef struct
112{
113 uint8_t *buf;
114 size_t cap;
115 const FinsHeader *h;
116 uint8_t area;
117 uint16_t address;
118 uint8_t bit;
119 uint16_t count;
120} FinsBuildMemoryAreaReadArgs;
121
122/** @brief What build_memory_area_write takes: buf, cap, h, area, ... */
123typedef struct
124{
125 uint8_t *buf;
126 size_t cap;
127 const FinsHeader *h;
128 uint8_t area;
129 uint16_t address;
130 uint8_t bit;
131 uint16_t count;
132 const uint8_t *data;
133 size_t data_len;
134} FinsBuildMemoryAreaWriteArgs;
135
136/** @brief What build_run takes: buf, cap, h, mode. */
137typedef struct
138{
139 uint8_t *buf;
140 size_t cap;
141 const FinsHeader *h;
142 FinsRunMode mode;
143} FinsBuildRunArgs;
144
145/** @brief What build_stop takes: buf, cap, h. */
146typedef struct
147{
148 uint8_t *buf;
149 size_t cap;
150 const FinsHeader *h;
151} FinsBuildStopArgs;
152
153/** @brief What parse_command takes: buf, len, out. */
154typedef struct
155{
156 const uint8_t *buf;
157 size_t len;
158 FinsCommand *out;
159} FinsParseCommandArgs;
160
161/** @brief What parse_response takes: buf, len, out. */
162typedef struct
163{
164 const uint8_t *buf;
165 size_t len;
166 FinsResponse *out;
167} FinsParseResponseArgs;
168
169/**
170 * @brief Omron FINS frame codec (PROTOCORE_ENABLE_FINS) - zero-heap command/response builder + parser for the Factory
171 * Interface Network Service (FINS/UDP), so a device can talk to an Omron PLC over the shipped UDP transport.
172 *
173 * A caller sets the members a call takes, invokes it through ::Fins with the bytes it runs
174 * out of, and reads the outcome off the same handle.
175 *
176 * Fins.build_command_args.buf = ...;
177 * Fins.build_command_args.cap = ...;
178 * Fins.build_command_args.h = ...;
179 * Fins.build_command_args.mrc = ...;
180 * Fins.build_command_args.src = ...;
181 * Fins.build_command_args.params = ...;
182 * Fins.build_command_args.params_len = ...;
183 * Fins.build_command(work);
184 * // Fins.n is what the call reports
185 *
186 * @var FinsNs::build_command_args what build_command takes: buf, cap, h, mrc, src, params,
187 * @var FinsNs::build_memory_area_read_args what build_memory_area_read takes: buf, cap, h, area,
188 * @var FinsNs::build_memory_area_write_args what build_memory_area_write takes: buf, cap, h, area,
189 * @var FinsNs::build_run_args what build_run takes: buf, cap, h, mode
190 * @var FinsNs::build_stop_args what build_stop takes: buf, cap, h
191 * @var FinsNs::parse_command_args what parse_command takes: buf, len, out
192 * @var FinsNs::parse_response_args what parse_response takes: buf, len, out
193 * @var FinsNs::ok a call's true/false outcome
194 * @var FinsNs::n total octets written, or 0 on a null data pointer with a nonzero ...
195 * @var FinsNs::build_command build a command frame: header + MRC + SRC + params. Returns total ...
196 * @var FinsNs::build_memory_area_read build a Memory Area Read command (0101): area code, 2-octet word ...
197 * @var FinsNs::build_memory_area_write build a Memory Area Write command (0102): the same area / word ...
198 * @var FinsNs::build_run build a RUN command (0401): switches the PLC to mode. Parameters ...
199 * @var FinsNs::build_stop build a STOP command (0402): switches the PLC to PROGRAM mode ...
200 * @var FinsNs::parse_command parse a command frame (header + MRC + SRC + params)
201 * @var FinsNs::parse_response parse a response frame (header + MRC + SRC + MRES + SRES + data)
202 *
203 * @c work is bytes the CALLER holds. This module reads none of them: it carries nothing
204 * between calls, so there is no state to keep and nothing to wipe. The parameter is there so
205 * a caller drives every namespace the same way.
206 */
207typedef struct
208{
209 FinsBuildCommandArgs build_command_args;
210 FinsBuildMemoryAreaReadArgs build_memory_area_read_args;
211 FinsBuildMemoryAreaWriteArgs build_memory_area_write_args;
212 FinsBuildRunArgs build_run_args;
213 FinsBuildStopArgs build_stop_args;
214 FinsParseCommandArgs parse_command_args;
215 FinsParseResponseArgs parse_response_args;
216 embed_bool ok;
217 size_t n;
218} FinsVars;
219
220/** @brief The operands and the outcome. */
221extern FinsVars FinsV;
222
223/** @brief The entries. */
224typedef struct
225{
226 void (*const build_command)(const uint8_t *work);
227 void (*const build_memory_area_read)(const uint8_t *work);
228 void (*const build_memory_area_write)(const uint8_t *work);
229 void (*const build_run)(const uint8_t *work);
230 void (*const build_stop)(const uint8_t *work);
231 void (*const parse_command)(const uint8_t *work);
232 void (*const parse_response)(const uint8_t *work);
233} FinsNs;
234
235// What the table binds, defined once in the .c and taking one parameter each: everything
236// else an entry needs is an operand in FinsV or a region of the borrow at a fixed offset.
237void protocore_fins_build_command(const uint8_t *work);
238void protocore_fins_build_memory_area_read(const uint8_t *work);
239void protocore_fins_build_memory_area_write(const uint8_t *work);
240void protocore_fins_build_run(const uint8_t *work);
241void protocore_fins_build_stop(const uint8_t *work);
242void protocore_fins_parse_command(const uint8_t *work);
243void protocore_fins_parse_response(const uint8_t *work);
244
245// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
246// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
247// `Fins.build_command(work)` resolves to a named function and becomes a DIRECT call. An extern table
248// leaves the call indirect and the symbol live at every level, -O2 -flto included.
249static const FinsNs Fins __attribute__((unused)) = {
250 .build_command = protocore_fins_build_command,
251 .build_memory_area_read = protocore_fins_build_memory_area_read,
252 .build_memory_area_write = protocore_fins_build_memory_area_write,
253 .build_run = protocore_fins_build_run,
254 .build_stop = protocore_fins_build_stop,
255 .parse_command = protocore_fins_parse_command,
256 .parse_response = protocore_fins_parse_response,
257};
258
259EMBED_END_DECLS
260
261#endif // PROTOCORE_ENABLE_FINS
262
263#endif // PROTOCORE_FINS_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.