17 #ifndef LEANSDR_HDLC_H 18 #define LEANSDR_HDLC_H 59 u8 *
decode(
u8 **ppin,
int count,
int *pdatasize,
int *hdlc_errors,
int *fcs_errors)
64 u8 *pin = *ppin, *pend = pin + count;
66 for (; pin < pend; ++pin)
70 for (
int bits = 8; bits--; byte_in <<= 1)
72 u8 bit_in = byte_in & 128;
106 fprintf(stderr,
"!");
115 fprintf(stderr,
"_");
130 fprintf(stderr,
"^");
151 if (*pdatasize != -1)
184 for (
int bit = 8; bit--;)
186 crc16 = (crc16 & 1) ? (crc16 >> 1) ^
crc16_poly : (crc16 >> 1);
213 out(_out, _maxframesize + chunk_size),
216 fcserrcount_out(
opt_writer(_fcserrcount_out)),
217 hdlcbytecount_out(
opt_writer(_hdlcbytecount_out)),
218 databytecount_out(
opt_writer(_databytecount_out)),
225 for (
int s = 0; s < NSYNCS; ++s)
229 for (
int h = 0; h < NERRHIST; ++h)
230 syncs[s].errhist[h] = 0;
233 syncs[cur_sync].dec->debug = sch->
debug;
244 bool previous_lock_state = lock_state;
245 int fcserrcount = 0, framecount = 0;
246 int hdlcbytecount = 0, databytecount = 0;
249 while ((
long)in.readable() >= chunk_size && (long)out.writable() >=
maxframesize + chunk_size)
254 for (
int s = 0; s < NSYNCS; ++s)
257 syncs[s].dec->reset();
259 syncs[s].errhist[errslot] = 0;
261 for (
u8 *pin = in.rd(), *pend = pin + chunk_size; pin < pend;)
263 int datasize, hdlc_errors, fcs_errors;
264 u8 *f = syncs[s].dec->decode(&pin, pend - pin, &datasize, &hdlc_errors, &fcs_errors);
265 syncs[s].errhist[errslot] += hdlc_errors;
272 output_frame(f, datasize);
273 databytecount += datasize;
277 fcserrcount += fcs_errors;
278 framecount += fcs_errors;
283 errslot = (errslot + 1) % NERRHIST;
286 int total_errors[NSYNCS];
288 for (
int s = 0; s < NSYNCS; ++s)
292 for (
int h = 0; h < NERRHIST; ++h)
293 total_errors[s] += syncs[s].errhist[h];
298 for (
int s = 0; s < NSYNCS; ++s)
299 if (total_errors[s] < total_errors[best])
302 if (best != cur_sync)
307 fprintf(stderr,
"[%d:%d->%d:%d]", cur_sync, total_errors[cur_sync], best, total_errors[best]);
310 syncs[cur_sync].dec->debug =
false;
312 syncs[cur_sync].dec->debug = sch->debug;
318 for (
u8 *pin = in.rd(), *pend = pin + chunk_size; pin < pend;)
320 int datasize, hdlc_errors, fcs_errors;
321 u8 *f = syncs[cur_sync].dec->decode(&pin, pend - pin, &datasize, &hdlc_errors, &fcs_errors);
326 output_frame(f, datasize);
327 databytecount += datasize;
331 fcserrcount += fcs_errors;
332 framecount += fcs_errors;
337 hdlcbytecount += chunk_size;
339 if (++resync_phase >= resync_period)
343 if (lock_state != previous_lock_state)
348 opt_write(hdlcbytecount_out, hdlcbytecount);
349 opt_write(databytecount_out, databytecount);
358 out.write(size >> 8);
359 out.write(size & 255);
362 memcpy(out.wr(), f, size);
374 static const int NSYNCS = 2;
375 static const int NERRHIST = 2;
380 int errhist[NERRHIST];
396 #endif // LEANSDR_HDLC_H
static const u16 crc16_init
hdlc_sync(scheduler *sch, pipebuf< u8 > &_in, pipebuf< u8 > &_out, int _minframesize, int _maxframesize, pipebuf< int > *_lock_out=NULL, pipebuf< int > *_framecount_out=NULL, pipebuf< int > *_fcserrcount_out=NULL, pipebuf< int > *_hdlcbytecount_out=NULL, pipebuf< int > *_databytecount_out=NULL)
static const u16 crc16_check
pipewriter< int > * hdlcbytecount_out
pipewriter< int > * lock_out
void opt_write(pipewriter< T > *p, T val)
void output_frame(u8 *f, int size)
pipewriter< int > * framecount_out
bool opt_writable(pipewriter< T > *p, int n=1)
pipewriter< T > * opt_writer(pipebuf< T > *buf, unsigned long min_write=1)
u8 * decode(u8 **ppin, int count, int *pdatasize, int *hdlc_errors, int *fcs_errors)
static const u16 crc16_poly
hdlc_dec(int _minframesize, int _maxframesize, bool _invert)