17static bool flow_drain(DncFlow *flow, DncRecvFn recv,
void *ctx)
20 int r = recv(ctx, tmp,
sizeof(tmp));
25 for (
int i = 0; i < r; i++)
27 pc_dnc_flow_feed(flow, tmp[i]);
33static bool emit(DncFlow *flow, DncSendFn send, DncRecvFn recv,
void *ctx,
const uint8_t *data,
size_t n)
35 if (!flow_drain(flow, recv, ctx))
40 while (!pc_dnc_flow_can_send(flow))
46 if (!flow_drain(flow, recv, ctx))
51 return send(ctx, data, n) == (int)n;
55static bool emit_runout(DncFlow *flow, DncSendFn send, DncRecvFn recv,
void *ctx, uint16_t count)
58 memset(zeros, 0,
sizeof(zeros));
61 uint16_t chunk = count <
sizeof(zeros) ? count : (uint16_t)sizeof(zeros);
62 if (!emit(flow, send, recv, ctx, zeros, chunk))
71DncStreamResult dnc_stream(
const DncCfg *cfg,
const char *program,
size_t prog_len, DncSendFn send, DncRecvFn recv,
74 if (!cfg || !send || !recv || (prog_len && !program))
76 return DncStreamResult::DNC_STREAM_ERR_ARG;
80 pc_dnc_flow_init(&flow);
84 if (cfg->leader_len && !emit_runout(&flow, send, recv, ctx, cfg->leader_len))
86 return DncStreamResult::DNC_STREAM_ERR_IO;
93 size_t n = pc_dnc_encode_marker(cfg, buf,
sizeof(buf));
96 return DncStreamResult::DNC_STREAM_ERR_ENCODE;
98 if (!emit(&flow, send, recv, ctx, buf, n))
100 return DncStreamResult::DNC_STREAM_ERR_IO;
108 while (j < prog_len && program[j] !=
'\n')
112 size_t line_len = j - i;
113 if (line_len && program[i + line_len - 1] ==
'\r')
117 n = pc_dnc_encode_block(cfg, program + i, line_len, buf,
sizeof(buf));
120 return DncStreamResult::DNC_STREAM_ERR_ENCODE;
122 if (!emit(&flow, send, recv, ctx, buf, n))
124 return DncStreamResult::DNC_STREAM_ERR_IO;
130 n = pc_dnc_encode_marker(cfg, buf,
sizeof(buf));
131 if (!emit(&flow, send, recv, ctx, buf, n))
133 return DncStreamResult::DNC_STREAM_ERR_IO;
137 if (cfg->leader_len && !emit_runout(&flow, send, recv, ctx, cfg->leader_len))
139 return DncStreamResult::DNC_STREAM_ERR_IO;
142 return DncStreamResult::DNC_STREAM_OK;
DNC drip-feed engine (PC_ENABLE_DNC) - stream a whole G-code program over a transport,...
#define PC_DNC_XOFF_MAX_POLLS
Safety cap on how many times the DNC stream engine polls the reverse channel while paused by an XOFF,...
#define PC_DNC_LINE_MAX
Largest G-code block (one line) the DNC decoder reassembles (PC_ENABLE_DNC).