DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
df1.h
Go to the documentation of this file.
1// 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 (DETWS_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 DETERMINISTICESPASYNCWEBSERVER_DF1_H
28#define DETERMINISTICESPASYNCWEBSERVER_DF1_H
29
30#include "ServerConfig.h"
31
32#if DETWS_ENABLE_DF1
33
34#include <stddef.h>
35#include <stdint.h>
36
37#define DF1_DLE 0x10
38#define DF1_STX 0x02
39#define DF1_ETX 0x03
40
41/** @brief Which error check the frame carries. */
42enum class Df1Check : uint8_t
43{
44 DF1_CHECK_BCC, ///< 1-octet block check character
45 DF1_CHECK_CRC ///< 2-octet CRC-16
46};
47
48/** @brief BCC: 2's complement of the modulo-256 sum of [data, data+len). */
49uint8_t df1_bcc(const uint8_t *data, size_t len);
50
51/** @brief CRC-16/ARC (poly 0x8005 / 0xA001 reflected, init 0) over [data, data+len). */
52uint16_t df1_crc(const uint8_t *data, size_t len);
53
54/**
55 * @brief Build a full-duplex frame around @p data: DLE STX + stuffed data + DLE ETX + check.
56 * @param check Df1Check::DF1_CHECK_BCC (1 octet) or Df1Check::DF1_CHECK_CRC (2 octets, low byte first; CRC over
57 * the data + ETX).
58 * @return total octets written, or 0 on overflow / bad input.
59 */
60size_t df1_build_frame(uint8_t *buf, size_t cap, const uint8_t *data, size_t data_len, Df1Check check);
61
62/**
63 * @brief Parse + validate a full-duplex frame, un-stuffing the application data.
64 * @param out receives the de-stuffed application data.
65 * @param out_cap capacity of @p out.
66 * @param out_len receives the application-data length.
67 * @return true on a complete, check-valid frame; false on bad framing, truncation, an
68 * out overflow, or a BCC/CRC mismatch.
69 */
70bool df1_parse_frame(const uint8_t *buf, size_t len, Df1Check check, uint8_t *out, size_t out_cap, size_t *out_len);
71
72#endif // DETWS_ENABLE_DF1
73
74#endif // DETERMINISTICESPASYNCWEBSERVER_DF1_H
User-facing configuration for DeterministicESPAsyncWebServer.