17static bool flow_drain(DncFlow *flow, DncRecvFn recv,
void *ctx)
20 int r = recv(ctx, tmp,
sizeof(tmp));
23 for (
int i = 0; i < r; i++)
24 dnc_flow_feed(flow, tmp[i]);
29static bool emit(DncFlow *flow, DncSendFn send, DncRecvFn recv,
void *ctx,
const uint8_t *data,
size_t n)
31 if (!flow_drain(flow, recv, ctx))
34 while (!dnc_flow_can_send(flow))
38 if (!flow_drain(flow, recv, ctx))
41 return send(ctx, data, n) == (int)n;
45static bool emit_runout(DncFlow *flow, DncSendFn send, DncRecvFn recv,
void *ctx, uint16_t count)
48 memset(zeros, 0,
sizeof(zeros));
51 uint16_t chunk = count <
sizeof(zeros) ? count : (uint16_t)sizeof(zeros);
52 if (!emit(flow, send, recv, ctx, zeros, chunk))
59DncStreamResult dnc_stream(
const DncCfg *cfg,
const char *program,
size_t prog_len, DncSendFn send, DncRecvFn recv,
62 if (!cfg || !send || !recv || (prog_len && !program))
63 return DncStreamResult::DNC_STREAM_ERR_ARG;
70 if (cfg->leader_len && !emit_runout(&flow, send, recv, ctx, cfg->leader_len))
71 return DncStreamResult::DNC_STREAM_ERR_IO;
74 size_t n = dnc_encode_marker(cfg, buf,
sizeof(buf));
76 return DncStreamResult::DNC_STREAM_ERR_ENCODE;
77 if (!emit(&flow, send, recv, ctx, buf, n))
78 return DncStreamResult::DNC_STREAM_ERR_IO;
85 while (j < prog_len && program[j] !=
'\n')
87 size_t line_len = j - i;
88 if (line_len && program[i + line_len - 1] ==
'\r')
90 n = dnc_encode_block(cfg, program + i, line_len, buf,
sizeof(buf));
92 return DncStreamResult::DNC_STREAM_ERR_ENCODE;
93 if (!emit(&flow, send, recv, ctx, buf, n))
94 return DncStreamResult::DNC_STREAM_ERR_IO;
99 n = dnc_encode_marker(cfg, buf,
sizeof(buf));
100 if (!emit(&flow, send, recv, ctx, buf, n))
101 return DncStreamResult::DNC_STREAM_ERR_IO;
104 if (cfg->leader_len && !emit_runout(&flow, send, recv, ctx, cfg->leader_len))
105 return DncStreamResult::DNC_STREAM_ERR_IO;
107 return DncStreamResult::DNC_STREAM_OK;
#define DETWS_DNC_LINE_MAX
Largest G-code block (one line) the DNC decoder reassembles (DETWS_ENABLE_DNC).
#define DETWS_DNC_XOFF_MAX_POLLS
Safety cap on how many times the DNC stream engine polls the reverse channel while paused by an XOFF,...
DNC drip-feed engine (DETWS_ENABLE_DNC) - stream a whole G-code program over a transport,...