DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
nema_ts2.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 nema_ts2.h
6 * @brief NEMA TS 2 traffic-cabinet SDLC frame codec (DETWS_ENABLE_NEMA_TS2).
7 *
8 * NEMA TS 2 links the devices inside a traffic-signal control cabinet - the controller, the Malfunction
9 * Management Unit (MMU), the Bus Interface Units (BIUs) and detector racks - over a synchronous SDLC
10 * bus. Each transaction is an SDLC frame:
11 *
12 * [address][control][frame-type][data...][FCS-16]
13 *
14 * where the FCS is the HDLC/X.25 CRC-16 (CRC-16/X-25: reflected poly 0x1021, init 0xFFFF, xorout 0xFFFF)
15 * over address..last-data, transmitted low byte first. This builds and validates those frames (the
16 * frame-type identifies command / status / detector frames per the TS 2 frame set); the synchronous
17 * serial PHY and the BIU/detector timing are the hardware-gated part. Pure, zero heap, host-testable.
18 */
19
20#ifndef DETERMINISTICESPASYNCWEBSERVER_NEMA_TS2_H
21#define DETERMINISTICESPASYNCWEBSERVER_NEMA_TS2_H
22
23#include "ServerConfig.h"
24#include <stddef.h>
25#include <stdint.h>
26
27#if DETWS_ENABLE_NEMA_TS2
28
29/** @brief Common TS 2 frame types (the third octet). */
30// NEMA TS2 frame types: wire values compared, so integer constants in a namespacing struct.
31struct NemaTs2
32{
33 static constexpr uint8_t NEMA_TS2_FT_CMD_LOADSWITCH = 0; ///< controller -> BIU: load-switch (signal-head) drivers.
34 static constexpr uint8_t NEMA_TS2_FT_STATUS_LOADSWITCH =
35 128; ///< BIU -> controller: load-switch status (frame type + 128).
36 static constexpr uint8_t NEMA_TS2_FT_CMD_MMU = 3; ///< controller <-> MMU status frame.
37 static constexpr uint8_t NEMA_TS2_FT_DETECTOR = 9; ///< detector BIU -> controller: detector call/status.
38};
39
40/** @brief HDLC/X.25 CRC-16 (CRC-16/X-25) over @p len bytes. */
41uint16_t detws_nema_ts2_crc(const uint8_t *bytes, size_t len);
42
43/**
44 * @brief Build a TS 2 SDLC frame: [address][control][frame_type][data...][FCS lo][FCS hi].
45 * @return the frame length (3 + data_len + 2), or 0 on overflow / bad args.
46 */
47size_t detws_nema_ts2_build(uint8_t address, uint8_t control, uint8_t frame_type, const uint8_t *data, size_t data_len,
48 uint8_t *out, size_t cap);
49
50/** @brief A parsed TS 2 frame (data points into the input). */
51struct NemaTs2Frame
52{
53 uint8_t address;
54 uint8_t control;
55 uint8_t frame_type;
56 const uint8_t *data;
57 size_t data_len;
58};
59
60/** @brief Validate the FCS and parse a TS 2 frame. @return true if the CRC matches and it is well-formed. */
61bool detws_nema_ts2_parse(const uint8_t *frame, size_t len, NemaTs2Frame *out);
62
63#endif // DETWS_ENABLE_NEMA_TS2
64#endif // DETERMINISTICESPASYNCWEBSERVER_NEMA_TS2_H
User-facing configuration for DeterministicESPAsyncWebServer.