DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
dnc_stream.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 dnc_stream.h
6 * @brief DNC drip-feed engine (DETWS_ENABLE_DNC) - stream a whole G-code program over a transport,
7 * pacing on reverse-channel XON/XOFF.
8 *
9 * The dnc codec (dnc.h) is pure framing; this drives the exchange: it emits the leader, the `%`
10 * program-start marker, every source line as a block, the `%` end marker, and the trailer, honoring
11 * software flow control - when the controller sends XOFF (DC3) it pauses and waits for XON (DC1)
12 * before the next write. Like the SMB / SMTP engines it works against a send/recv seam, so it is
13 * transport-agnostic (the same engine drip-feeds over a raw TCP socket for "Ethernet DNC" or over a
14 * UART for classic RS-232 DNC) and host-testable with a scripted mock controller.
15 *
16 * @author Douglas Quigg (dstroy0)
17 * @date 2026
18 */
19
20#ifndef DETERMINISTICESPASYNCWEBSERVER_DNC_STREAM_H
21#define DETERMINISTICESPASYNCWEBSERVER_DNC_STREAM_H
22
23#include "ServerConfig.h"
24
25#if DETWS_ENABLE_DNC
26
27#include "dnc.h"
28#include <stddef.h>
29#include <stdint.h>
30
31/** @brief Result of a drip-feed. 0 is success; each failure is a distinct code. */
32enum class DncStreamResult : int32_t
33{
34 DNC_STREAM_OK = 0,
35 DNC_STREAM_ERR_ARG = -1, ///< a required argument was null
36 DNC_STREAM_ERR_IO = -2, ///< a send/recv failed, or XOFF never cleared (see DETWS_DNC_XOFF_MAX_POLLS)
37 DNC_STREAM_ERR_ENCODE = -3, ///< a source line had no representation in the tape code, or overran a block
38};
39
40/**
41 * @brief Transport seam: the engine moves bytes only through these, so it runs over TCP, a UART, or
42 * a test mock.
43 * @return send: bytes written (must equal @p len), else < 0.
44 * @return recv: reverse-channel bytes read (>= 0, 0 if none available now), or < 0 on error / close.
45 * While paused by XOFF the engine polls @c recv for XON, so a real transport should pace it
46 * (block briefly when idle) rather than spin.
47 */
48using DncSendFn = int (*)(void *ctx, const uint8_t *data, size_t len);
49using DncRecvFn = int (*)(void *ctx, uint8_t *buf, size_t cap);
50
51/**
52 * @brief Drip-feed @p program (plain ASCII G-code, lines separated by LF; a trailing CR is stripped)
53 * as a framed DNC stream over @p send / @p recv, pausing on XOFF.
54 *
55 * Each line must fit DETWS_DNC_LINE_MAX; a longer or untranslatable line fails closed with
56 * ::DNC_STREAM_ERR_ENCODE (nothing partial is left mid-block). An empty @p program still frames the
57 * `%` start/end markers.
58 *
59 * @return ::DNC_STREAM_OK, or a ::DncStreamResult error.
60 */
61DncStreamResult dnc_stream(const DncCfg *cfg, const char *program, size_t prog_len, DncSendFn send, DncRecvFn recv,
62 void *ctx);
63
64#endif // DETWS_ENABLE_DNC
65
66#endif // DETERMINISTICESPASYNCWEBSERVER_DNC_STREAM_H
User-facing configuration for DeterministicESPAsyncWebServer.
CNC RS-232 DNC (Distributed Numerical Control) drip-feed codec (DETWS_ENABLE_DNC).