ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
dnc_stream.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 dnc_stream.h
6 * @brief DNC drip-feed engine (PROTOCORE_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 PROTOCORE_DNC_STREAM_H
21#define PROTOCORE_DNC_STREAM_H
22
23#include "protocore_config.h" // the entry point: protocore_types.h for the widths
24
25#if PROTOCORE_ENABLE_DNC
26
27EMBED_BEGIN_DECLS
28
30
31/** @brief Result of a drip-feed. 0 is success; each failure is a distinct code. */
33 DncStreamResult, DNC_STREAM_OK = 0,
34 DNC_STREAM_ERR_ARG = -1, ///< a required argument was null
35 DNC_STREAM_ERR_IO = -2, ///< a send/recv failed, or XOFF never cleared (see PROTOCORE_DNC_XOFF_MAX_POLLS)
36 DNC_STREAM_ERR_ENCODE = -3, ///< a source line had no representation in the tape code, or overran a block
37);
38
39/**
40 * @brief Transport seam: the engine moves bytes only through these, so it runs over TCP, a UART, or
41 * a test mock.
42 * @return send: bytes written (must equal @p len), else < 0.
43 * @return recv: reverse-channel bytes read (>= 0, 0 if none available now), or < 0 on error / close.
44 * While paused by XOFF the engine polls @c recv for XON, so a real transport should pace it
45 * (block briefly when idle) rather than spin.
46 */
47typedef int (*DncSendFn)(void *ctx, const uint8_t *data, size_t len);
48typedef int (*DncRecvFn)(void *ctx, uint8_t *buf, size_t cap);
49
50/**
51 * @brief Drip-feed @p program (plain ASCII G-code, lines separated by LF; a trailing CR is stripped)
52 * as a framed DNC stream over @p send / @p recv, pausing on XOFF.
53 *
54 * Each line must fit PROTOCORE_DNC_LINE_MAX; a longer or untranslatable line fails closed with
55 * ::DNC_STREAM_ERR_ENCODE (nothing partial is left mid-block). An empty @p program still frames the
56 * `%` start/end markers.
57 *
58 * @return ::DNC_STREAM_OK, or a ::DncStreamResult error.
59 */
60DncStreamResult dnc_stream(const DncCfg *cfg, const char *program, size_t prog_len, DncSendFn send, DncRecvFn recv,
61 void *ctx);
62
63EMBED_END_DECLS
64
65#endif // PROTOCORE_ENABLE_DNC
66
67#endif // PROTOCORE_DNC_STREAM_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.
CNC RS-232 DNC (Distributed Numerical Control) drip-feed codec (PROTOCORE_ENABLE_DNC).