ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
ads.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 ads.h
6 * @brief Beckhoff ADS / AMS protocol codec (PROTOCORE_ENABLE_ADS) - zero-heap request builders +
7 * response parsers for TwinCAT PLCs over TCP 48898 (the PC-based-control protocol).
8 *
9 * ADS (Automation Device Specification) rides on AMS (Automation Message Specification). Every
10 * multi-octet field is LITTLE-endian. A frame is an AMS/TCP header (6 octets) + an AMS header
11 * (32 octets) + the command payload:
12 * @code
13 * AMS/TCP header (6)
14 * 00 00 reserved
15 * LL LL LL LL length of everything after this field (AMS header + payload)
16 * AMS header (32)
17 * target net id (6) e.g. 5.18....1.1 (AMSNetId, six octets in order)
18 * target port (2) e.g. 851 = the first TwinCAT 3 PLC runtime
19 * source net id (6)
20 * source port (2)
21 * cmd id (2) 1 ReadDeviceInfo 2 Read 3 Write 4 ReadState 5 WriteControl
22 * 6 AddNotification 7 DeleteNotification 8 Notification 9 ReadWrite
23 * state flags (2) 0x0004 request, 0x0005 response (bit0 = response, bit2 = ADS command)
24 * data length (4) cbData - octets of payload that follow the AMS header
25 * error code (4) AMS error (0 = success)
26 * invoke id (4) caller-chosen, echoed in the response to correlate it
27 * payload (cbData) command-specific (see the per-command builders/parsers below)
28 * @endcode
29 *
30 * AMS header field order (target-before-source), command ids, and state flags verified against
31 * the Beckhoff InfoSys AMS/ADS specification. Payload layouts cross-checked with Beckhoff's own
32 * open-source ADS library, `pyads`, and Apache PLC4X. Pure codec, host-tested - the caller owns
33 * the TCP connection (`protocore_client_*`) and the AMS route registration on the target router.
34 *
35 * @author Douglas Quigg (dstroy0)
36 * @date 2026
37 */
38
39#ifndef PROTOCORE_ADS_H
40#define PROTOCORE_ADS_H
41
42#include "protocore_config.h" // the entry point: protocore_types.h for the widths
43
44#if PROTOCORE_ENABLE_ADS
45
46EMBED_BEGIN_DECLS
47
48// This module holds nothing between calls, so it carves no borrow and states none. An entry
49// takes one all the same, and never reads it, so every namespace in the tree is invoked the
50// same way.
51
52#define ADS_TCP_PORT 48898 ///< AMS/TCP listening port (0xBF02)
53#define ADS_AMSTCP_HDR_LEN 6 ///< reserved(2) + length(4)
54#define ADS_AMS_HDR_LEN 32 ///< target/source net id + port, cmd, flags, cbData, error, invoke
55#define ADS_HDR_LEN 38 ///< ADS_AMSTCP_HDR_LEN + ADS_AMS_HDR_LEN (payload starts here)
56#define ADS_NET_ID_LEN 6 ///< an AMSNetId is six octets
57#define ADS_DEVICE_NAME_LEN 16 ///< ReadDeviceInfo device-name field width
58
59/// AMS header state-flag bits (octets 18-19). A TCP request is ADS_STATE_ADS_COMMAND; a response
60/// ORs in ADS_STATE_RESPONSE.
61#define ADS_STATE_RESPONSE 0x0001 ///< set on a response, clear on a request
62#define ADS_STATE_NO_RETURN 0x0002 ///< no response expected
63#define ADS_STATE_ADS_COMMAND 0x0004 ///< ADS command (set for TCP)
64#define ADS_STATE_SYS_COMMAND 0x0008 ///< system command
65#define ADS_STATE_HIGH_PRIO 0x0010 ///< high priority
66#define ADS_STATE_TIMESTAMP 0x0020 ///< a timestamp is appended
67#define ADS_STATE_UDP 0x0040 ///< carried over UDP
68#define ADS_STATE_INIT_COMMAND 0x0080
69#define ADS_STATE_BROADCAST 0x8000
70#define ADS_STATE_REQUEST ADS_STATE_ADS_COMMAND ///< 0x0004
71#define ADS_STATE_REPLY (ADS_STATE_ADS_COMMAND | ADS_STATE_RESPONSE) ///< 0x0005
72
73/// Well-known ADS index groups for symbol access (dedup of the magic constants).
74#define ADS_IGRP_SYM_HND_BY_NAME 0xF003 ///< ReadWrite name -> handle
75#define ADS_IGRP_SYM_VAL_BY_HANDLE 0xF005 ///< Read/Write value by handle
76#define ADS_IGRP_SYM_RELEASE_HANDLE 0xF006 ///< Write to release a handle
77#define ADS_IGRP_SYM_INFO_BY_NAME_EX 0xF009
78#define ADS_IGRP_SYM_UPLOAD 0xF00B
79#define ADS_IGRP_SYM_UPLOAD_INFO 0xF00F
80#define ADS_IGRP_IO_IMAGE_RW_IB 0xF020 ///< %I input image, bit offset
81#define ADS_IGRP_IO_IMAGE_RW_OB 0xF030 ///< %Q output image, bit offset
82#define ADS_IGRP_PLC_RW_M 0x4020 ///< %M flag memory, byte offset
83#define ADS_IGRP_PLC_RW_RB 0x4030 ///< retain memory
84
85EMBED_ENUM_BYTE(AdsCommand, ADS_COMMAND_INVALID = 0x0000, ADS_COMMAND_READ_DEVICE_INFO = 0x0001,
86 ADS_COMMAND_READ = 0x0002, ADS_COMMAND_WRITE = 0x0003, ADS_COMMAND_READ_STATE = 0x0004,
87 ADS_COMMAND_WRITE_CONTROL = 0x0005, ADS_COMMAND_ADD_NOTIFICATION = 0x0006,
88 ADS_COMMAND_DEL_NOTIFICATION = 0x0007, ADS_COMMAND_NOTIFICATION = 0x0008,
89 ADS_COMMAND_READ_WRITE = 0x0009, );
90
91EMBED_ENUM_BYTE(AdsState, ADS_STATE_INVALID = 0, ADS_STATE_IDLE = 1, ADS_STATE_RESET = 2, ADS_STATE_INIT = 3,
92 ADS_STATE_START = 4, ADS_STATE_RUN = 5, ADS_STATE_STOP = 6, ADS_STATE_SAVE_CONFIG = 7,
93 ADS_STATE_LOAD_CONFIG = 8, ADS_STATE_POWER_FAILURE = 9, ADS_STATE_POWER_GOOD = 10, ADS_STATE_ERROR = 11,
94 ADS_STATE_SHUTDOWN = 12, ADS_STATE_SUSPEND = 13, ADS_STATE_RESUME = 14, ADS_STATE_CONFIG = 15,
95 ADS_STATE_RECONFIG = 16, );
96
97EMBED_ENUM_BYTE(AdsTransMode, ADS_TRANS_MODE_NO_TRANS = 0, ADS_TRANS_MODE_CLIENT_CYCLE = 1,
98 ADS_TRANS_MODE_CLIENT_ON_CHANGE = 2,
99 ADS_TRANS_MODE_SERVER_CYCLE = 3, ///< server sends every CycleTime
100 ADS_TRANS_MODE_SERVER_ON_CHANGE = 4, ///< server sends when the value changes
101);
102
103typedef struct
104{
105 uint8_t net_id[ADS_NET_ID_LEN];
106 uint16_t port;
107} AdsAmsAddr;
108
109typedef struct
110{
111 AdsAmsAddr target;
112 AdsAmsAddr source;
113 uint32_t invoke_id;
114} AdsRequest;
115
116typedef struct
117{
118 AdsAmsAddr target;
119 AdsAmsAddr source;
120 AdsCommand cmd;
121 uint16_t state_flags;
122 uint32_t data_len; ///< cbData
123 uint32_t error_code; ///< AMS error (0 = success)
124 uint32_t invoke_id;
125 const uint8_t *data; ///< -> payload (into the caller's buffer)
126} AdsAmsHeader;
127
128typedef struct
129{
130 uint32_t result; ///< ADS error code (0 = success)
131 const uint8_t *data;
132 uint32_t len;
133} AdsReadResult;
134
135typedef struct
136{
137 uint32_t result;
138 uint16_t protocore_ads_state;
139 uint16_t device_state;
140} AdsReadStateResult;
141
142typedef struct
143{
144 uint32_t result;
145 uint8_t version_major;
146 uint8_t version_minor;
147 uint16_t version_build;
148 char device_name[ADS_DEVICE_NAME_LEN + 1]; ///< NUL-terminated copy of the 16-octet field
149} AdsDeviceInfo;
150
151typedef void (*AdsNotificationSampleFn)(uint32_t notification_handle, const uint8_t *sample, uint32_t sample_len,
152 uint64_t timestamp, void *user);
153
154/** @brief What build_read_device_info takes: buf, cap, r. */
155typedef struct
156{
157 uint8_t *buf;
158 size_t cap;
159 const AdsRequest *r;
160} AdsBuildReadDeviceInfoArgs;
161
162/** @brief What build_read_state takes: buf, cap, r. */
163typedef struct
164{
165 uint8_t *buf;
166 size_t cap;
167 const AdsRequest *r;
168} AdsBuildReadStateArgs;
169
170/** @brief What build_read takes: buf, cap, r, index_group, ... */
171typedef struct
172{
173 uint8_t *buf;
174 size_t cap;
175 const AdsRequest *r;
176 uint32_t index_group;
177 uint32_t index_offset;
178 uint32_t read_len;
179} AdsBuildReadArgs;
180
181/** @brief What build_write takes: buf, cap, r, index_group, ... */
182typedef struct
183{
184 uint8_t *buf;
185 size_t cap;
186 const AdsRequest *r;
187 uint32_t index_group;
188 uint32_t index_offset;
189 const uint8_t *data;
190 uint32_t len;
191} AdsBuildWriteArgs;
192
193/** @brief What build_read_write takes: buf, cap, r, index_group, ... */
194typedef struct
195{
196 uint8_t *buf;
197 size_t cap;
198 const AdsRequest *r;
199 uint32_t index_group;
200 uint32_t index_offset;
201 uint32_t read_len;
202 const uint8_t *write_data;
203 uint32_t write_len;
204} AdsBuildReadWriteArgs;
205
206/** @brief What build_write_control takes: buf, cap, r, ... */
207typedef struct
208{
209 uint8_t *buf;
210 size_t cap;
211 const AdsRequest *r;
212 uint16_t protocore_ads_state;
213 uint16_t device_state;
214 const uint8_t *data;
215 uint32_t len;
216} AdsBuildWriteControlArgs;
217
218/** @brief What build_add_notification takes: buf, cap, r, ... */
219typedef struct
220{
221 uint8_t *buf;
222 size_t cap;
223 const AdsRequest *r;
224 uint32_t index_group;
225 uint32_t index_offset;
226 uint32_t length;
227 AdsTransMode mode;
228 uint32_t max_delay;
229 uint32_t cycle_time;
230} AdsBuildAddNotificationArgs;
231
232/** @brief What build_del_notification takes: buf, cap, r, ... */
233typedef struct
234{
235 uint8_t *buf;
236 size_t cap;
237 const AdsRequest *r;
238 uint32_t notification_handle;
239} AdsBuildDelNotificationArgs;
240
241/** @brief What parse_ams_header takes: buf, len, out. */
242typedef struct
243{
244 const uint8_t *buf;
245 size_t len;
246 AdsAmsHeader *out;
247} AdsParseAmsHeaderArgs;
248
249/** @brief What parse_read takes: data, data_len, out. */
250typedef struct
251{
252 const uint8_t *data;
253 size_t data_len;
254 AdsReadResult *out;
255} AdsParseReadArgs;
256
257/** @brief What parse_result takes: data, data_len, result. */
258typedef struct
259{
260 const uint8_t *data;
261 size_t data_len;
262 uint32_t *result;
263} AdsParseResultArgs;
264
265/** @brief What parse_read_state takes: data, data_len, out. */
266typedef struct
267{
268 const uint8_t *data;
269 size_t data_len;
270 AdsReadStateResult *out;
271} AdsParseReadStateArgs;
272
273/** @brief What parse_read_device_info takes: data, data_len, out. */
274typedef struct
275{
276 const uint8_t *data;
277 size_t data_len;
278 AdsDeviceInfo *out;
279} AdsParseReadDeviceInfoArgs;
280
281/** @brief What parse_add_notification takes: data, data_len, result, ... */
282typedef struct
283{
284 const uint8_t *data;
285 size_t data_len;
286 uint32_t *result;
287 uint32_t *handle;
288} AdsParseAddNotificationArgs;
289
290/** @brief What parse_notification takes: data, data_len, on_sample, ... */
291typedef struct
292{
293 const uint8_t *data;
294 size_t data_len;
295 AdsNotificationSampleFn on_sample;
296 void *user;
297} AdsParseNotificationArgs;
298
299/**
300 * @brief Beckhoff ADS / AMS protocol codec (PROTOCORE_ENABLE_ADS) - zero-heap request builders + response parsers for
301 * TwinCAT PLCs over TCP 48898 (the PC-based-control protocol).
302 *
303 * A caller sets the members a call takes, invokes it through ::Ads with the bytes it runs
304 * out of, and reads the outcome off the same handle.
305 *
306 * Ads.build_read_device_info_args.buf = ...;
307 * Ads.build_read_device_info_args.cap = ...;
308 * Ads.build_read_device_info_args.r = ...;
309 * Ads.build_read_device_info(work);
310 * // Ads.n is what the call reports
311 *
312 * @var AdsNs::build_read_device_info_args what build_read_device_info takes: buf, cap, r
313 * @var AdsNs::build_read_state_args what build_read_state takes: buf, cap, r
314 * @var AdsNs::build_read_args what build_read takes: buf, cap, r, index_group,
315 * @var AdsNs::build_write_args what build_write takes: buf, cap, r, index_group,
316 * @var AdsNs::build_read_write_args what build_read_write takes: buf, cap, r, index_group,
317 * @var AdsNs::build_write_control_args what build_write_control takes: buf, cap, r,
318 * @var AdsNs::build_add_notification_args what build_add_notification takes: buf, cap, r,
319 * @var AdsNs::build_del_notification_args what build_del_notification takes: buf, cap, r,
320 * @var AdsNs::parse_ams_header_args what parse_ams_header takes: buf, len, out
321 * @var AdsNs::parse_read_args what parse_read takes: data, data_len, out
322 * @var AdsNs::parse_result_args what parse_result takes: data, data_len, result
323 * @var AdsNs::parse_read_state_args what parse_read_state takes: data, data_len, out
324 * @var AdsNs::parse_read_device_info_args what parse_read_device_info takes: data, data_len, out
325 * @var AdsNs::parse_add_notification_args what parse_add_notification takes: data, data_len, result,
326 * @var AdsNs::parse_notification_args what parse_notification takes: data, data_len, on_sample,
327 * @var AdsNs::ok a call's true/false outcome
328 * @var AdsNs::n the count a call reports
329 * @var AdsNs::build_read_device_info build_read_device_info
330 * @var AdsNs::build_read_state build_read_state
331 * @var AdsNs::build_read build_read
332 * @var AdsNs::build_write build_write
333 * @var AdsNs::build_read_write build_read_write
334 * @var AdsNs::build_write_control build_write_control
335 * @var AdsNs::build_add_notification build_add_notification
336 * @var AdsNs::build_del_notification build_del_notification
337 * @var AdsNs::parse_ams_header parse_ams_header
338 * @var AdsNs::parse_read parse_read
339 * @var AdsNs::parse_result parse_result
340 * @var AdsNs::parse_read_state parse_read_state
341 * @var AdsNs::parse_read_device_info parse_read_device_info
342 * @var AdsNs::parse_add_notification parse_add_notification
343 * @var AdsNs::parse_notification parse_notification
344 *
345 * @c work is bytes the CALLER holds. This module reads none of them: it carries nothing
346 * between calls, so there is no state to keep and nothing to wipe. The parameter is there so
347 * a caller drives every namespace the same way.
348 */
349typedef struct
350{
351 AdsBuildReadDeviceInfoArgs build_read_device_info_args;
352 AdsBuildReadStateArgs build_read_state_args;
353 AdsBuildReadArgs build_read_args;
354 AdsBuildWriteArgs build_write_args;
355 AdsBuildReadWriteArgs build_read_write_args;
356 AdsBuildWriteControlArgs build_write_control_args;
357 AdsBuildAddNotificationArgs build_add_notification_args;
358 AdsBuildDelNotificationArgs build_del_notification_args;
359 AdsParseAmsHeaderArgs parse_ams_header_args;
360 AdsParseReadArgs parse_read_args;
361 AdsParseResultArgs parse_result_args;
362 AdsParseReadStateArgs parse_read_state_args;
363 AdsParseReadDeviceInfoArgs parse_read_device_info_args;
364 AdsParseAddNotificationArgs parse_add_notification_args;
365 AdsParseNotificationArgs parse_notification_args;
366 embed_bool ok;
367 size_t n;
368} AdsVars;
369
370/** @brief The operands and the outcome. */
371extern AdsVars AdsV;
372
373/** @brief The entries. */
374typedef struct
375{
376 void (*const build_read_device_info)(const uint8_t *work);
377 void (*const build_read_state)(const uint8_t *work);
378 void (*const build_read)(const uint8_t *work);
379 void (*const build_write)(const uint8_t *work);
380 void (*const build_read_write)(const uint8_t *work);
381 void (*const build_write_control)(const uint8_t *work);
382 void (*const build_add_notification)(const uint8_t *work);
383 void (*const build_del_notification)(const uint8_t *work);
384 void (*const parse_ams_header)(const uint8_t *work);
385 void (*const parse_read)(const uint8_t *work);
386 void (*const parse_result)(const uint8_t *work);
387 void (*const parse_read_state)(const uint8_t *work);
388 void (*const parse_read_device_info)(const uint8_t *work);
389 void (*const parse_add_notification)(const uint8_t *work);
390 void (*const parse_notification)(const uint8_t *work);
391} AdsNs;
392
393// What the table binds, defined once in the .c and taking one parameter each: everything
394// else an entry needs is an operand in AdsV or a region of the borrow at a fixed offset.
395void protocore_ads_build_read_device_info(const uint8_t *work);
396void protocore_ads_build_read_state(const uint8_t *work);
397void protocore_ads_build_read(const uint8_t *work);
398void protocore_ads_build_write(const uint8_t *work);
399void protocore_ads_build_read_write(const uint8_t *work);
400void protocore_ads_build_write_control(const uint8_t *work);
401void protocore_ads_build_add_notification(const uint8_t *work);
402void protocore_ads_build_del_notification(const uint8_t *work);
403void protocore_ads_parse_ams_header(const uint8_t *work);
404void protocore_ads_parse_read(const uint8_t *work);
405void protocore_ads_parse_result(const uint8_t *work);
406void protocore_ads_parse_read_state(const uint8_t *work);
407void protocore_ads_parse_read_device_info(const uint8_t *work);
408void protocore_ads_parse_add_notification(const uint8_t *work);
409void protocore_ads_parse_notification(const uint8_t *work);
410
411// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
412// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
413// `Ads.build_read_device_info(work)` resolves to a named function and becomes a DIRECT call. An extern table
414// leaves the call indirect and the symbol live at every level, -O2 -flto included.
415static const AdsNs Ads __attribute__((unused)) = {
416 .build_read_device_info = protocore_ads_build_read_device_info,
417 .build_read_state = protocore_ads_build_read_state,
418 .build_read = protocore_ads_build_read,
419 .build_write = protocore_ads_build_write,
420 .build_read_write = protocore_ads_build_read_write,
421 .build_write_control = protocore_ads_build_write_control,
422 .build_add_notification = protocore_ads_build_add_notification,
423 .build_del_notification = protocore_ads_build_del_notification,
424 .parse_ams_header = protocore_ads_parse_ams_header,
425 .parse_read = protocore_ads_parse_read,
426 .parse_result = protocore_ads_parse_result,
427 .parse_read_state = protocore_ads_parse_read_state,
428 .parse_read_device_info = protocore_ads_parse_read_device_info,
429 .parse_add_notification = protocore_ads_parse_add_notification,
430 .parse_notification = protocore_ads_parse_notification,
431};
432
433EMBED_END_DECLS
434
435#endif // PROTOCORE_ENABLE_ADS
436
437#endif // PROTOCORE_ADS_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.