SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
leansdr::hdlc_sync Struct Reference

#include <hdlc.h>

+ Inheritance diagram for leansdr::hdlc_sync:
+ Collaboration diagram for leansdr::hdlc_sync:

Public Member Functions

 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)
 
void run ()
 
- Public Member Functions inherited from leansdr::runnable
 runnable (scheduler *_sch, const char *name)
 
- Public Member Functions inherited from leansdr::runnable_common
 runnable_common (const char *_name)
 
virtual ~runnable_common ()
 
virtual void shutdown ()
 

Public Attributes

int resync_period
 
bool header16
 
hdlc_decdec
 
int errhist [NERRHIST]
 
- Public Attributes inherited from leansdr::runnable_common
const char * name
 

Private Member Functions

void output_frame (u8 *f, int size)
 

Private Attributes

int minframesize
 
int maxframesize
 
int chunk_size
 
pipereader< u8in
 
pipewriter< u8out
 
pipewriter< int > * lock_out
 
pipewriter< int > * framecount_out
 
pipewriter< int > * fcserrcount_out
 
pipewriter< int > * hdlcbytecount_out
 
pipewriter< int > * databytecount_out
 
struct {
   hdlc_dec *   dec
 
   int   errhist [NERRHIST]
 
syncs [NSYNCS]
 
int errslot
 
int cur_sync
 
int resync_phase
 
bool lock_state
 

Static Private Attributes

static const int NSYNCS = 2
 
static const int NERRHIST = 2
 

Additional Inherited Members

- Protected Attributes inherited from leansdr::runnable
schedulersch
 

Detailed Description

Definition at line 197 of file hdlc.h.

Constructor & Destructor Documentation

◆ hdlc_sync()

leansdr::hdlc_sync::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 
)
inline

Definition at line 199 of file hdlc.h.

References leansdr::scheduler::debug, leansdr::hdlc_dec::hdlc_dec(), leansdr::hdlc_dec::maxframesize, and leansdr::hdlc_dec::minframesize.

208  : runnable(sch, "hdlc_sync"),
209  minframesize(_minframesize),
210  maxframesize(_maxframesize),
212  in(_in),
213  out(_out, _maxframesize + chunk_size),
214  lock_out(opt_writer(_lock_out)),
215  framecount_out(opt_writer(_framecount_out)),
216  fcserrcount_out(opt_writer(_fcserrcount_out)),
217  hdlcbytecount_out(opt_writer(_hdlcbytecount_out)),
218  databytecount_out(opt_writer(_databytecount_out)),
219  cur_sync(0),
220  resync_phase(0),
221  lock_state(false),
222  resync_period(32),
223  header16(false)
224  {
225  for (int s = 0; s < NSYNCS; ++s)
226  {
227  syncs[s].dec = new hdlc_dec(minframesize, maxframesize, s != 0);
228 
229  for (int h = 0; h < NERRHIST; ++h)
230  syncs[s].errhist[h] = 0;
231  }
232 
233  syncs[cur_sync].dec->debug = sch->debug;
234  errslot = 0;
235  }
int errhist[NERRHIST]
Definition: hdlc.h:380
static const int NSYNCS
Definition: hdlc.h:374
runnable(scheduler *_sch, const char *name)
Definition: framework.h:193
pipewriter< int > * hdlcbytecount_out
Definition: hdlc.h:373
static const int NERRHIST
Definition: hdlc.h:375
int resync_phase
Definition: hdlc.h:385
pipewriter< int > * lock_out
Definition: hdlc.h:371
struct leansdr::hdlc_sync::@7 syncs[NSYNCS]
pipewriter< int > * framecount_out
Definition: hdlc.h:372
pipereader< u8 > in
Definition: hdlc.h:369
pipewriter< T > * opt_writer(pipebuf< T > *buf, unsigned long min_write=1)
Definition: framework.h:329
pipewriter< int > * fcserrcount_out
Definition: hdlc.h:372
scheduler * sch
Definition: framework.h:199
bool lock_state
Definition: hdlc.h:386
int maxframesize
Definition: hdlc.h:367
int resync_period
Definition: hdlc.h:389
int minframesize
Definition: hdlc.h:367
pipewriter< int > * databytecount_out
Definition: hdlc.h:373
pipewriter< u8 > out
Definition: hdlc.h:370
+ Here is the call graph for this function:

Member Function Documentation

◆ output_frame()

void leansdr::hdlc_sync::output_frame ( u8 f,
int  size 
)
inlineprivate

Definition at line 353 of file hdlc.h.

References leansdr::opt_write().

354  {
355  if (header16)
356  {
357  // Removed 16-bit CRC, add 16-bit prefix -> Still <= maxframesize.
358  out.write(size >> 8);
359  out.write(size & 255);
360  }
361 
362  memcpy(out.wr(), f, size);
363  out.written(size);
365  }
void opt_write(pipewriter< T > *p, T val)
Definition: framework.h:341
pipewriter< int > * framecount_out
Definition: hdlc.h:372
void written(unsigned long n)
Definition: framework.h:308
void write(const T &e)
Definition: framework.h:319
pipewriter< u8 > out
Definition: hdlc.h:370
+ Here is the call graph for this function:

◆ run()

void leansdr::hdlc_sync::run ( )
inlinevirtual

Reimplemented from leansdr::runnable_common.

Definition at line 237 of file hdlc.h.

References leansdr::hdlc_dec::maxframesize, leansdr::opt_writable(), and leansdr::opt_write().

238  {
240  {
241  return;
242  }
243 
244  bool previous_lock_state = lock_state;
245  int fcserrcount = 0, framecount = 0;
246  int hdlcbytecount = 0, databytecount = 0;
247 
248  // Note: hdlc_dec may already hold one frame ready for output.
249  while ((long)in.readable() >= chunk_size && (long)out.writable() >= maxframesize + chunk_size)
250  {
251  if (!resync_phase)
252  {
253  // Once every resync_phase, try all decoders
254  for (int s = 0; s < NSYNCS; ++s)
255  {
256  if (s != cur_sync)
257  syncs[s].dec->reset();
258 
259  syncs[s].errhist[errslot] = 0;
260 
261  for (u8 *pin = in.rd(), *pend = pin + chunk_size; pin < pend;)
262  {
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;
266 
267  if (s == cur_sync)
268  {
269  if (f)
270  {
271  lock_state = true;
272  output_frame(f, datasize);
273  databytecount += datasize;
274  ++framecount;
275  }
276 
277  fcserrcount += fcs_errors;
278  framecount += fcs_errors;
279  }
280  }
281  }
282 
283  errslot = (errslot + 1) % NERRHIST;
284  // Switch to another sync option ?
285  // Compare total error counts over about NERRHIST frames.
286  int total_errors[NSYNCS];
287 
288  for (int s = 0; s < NSYNCS; ++s)
289  {
290  total_errors[s] = 0;
291 
292  for (int h = 0; h < NERRHIST; ++h)
293  total_errors[s] += syncs[s].errhist[h];
294  }
295 
296  int best = cur_sync;
297 
298  for (int s = 0; s < NSYNCS; ++s)
299  if (total_errors[s] < total_errors[best])
300  best = s;
301 
302  if (best != cur_sync)
303  {
304  lock_state = false;
305 
306  if (sch->debug)
307  fprintf(stderr, "[%d:%d->%d:%d]", cur_sync, total_errors[cur_sync], best, total_errors[best]);
308 
309  // No verbose messages on candidate syncs
310  syncs[cur_sync].dec->debug = false;
311  cur_sync = best;
312  syncs[cur_sync].dec->debug = sch->debug;
313  }
314  }
315  else
316  {
317  // Use only the currently selected decoder
318  for (u8 *pin = in.rd(), *pend = pin + chunk_size; pin < pend;)
319  {
320  int datasize, hdlc_errors, fcs_errors;
321  u8 *f = syncs[cur_sync].dec->decode(&pin, pend - pin, &datasize, &hdlc_errors, &fcs_errors);
322 
323  if (f)
324  {
325  lock_state = true;
326  output_frame(f, datasize);
327  databytecount += datasize;
328  ++framecount;
329  }
330 
331  fcserrcount += fcs_errors;
332  framecount += fcs_errors;
333  }
334  } // resync_phase
335 
336  in.read(chunk_size);
337  hdlcbytecount += chunk_size;
338 
339  if (++resync_phase >= resync_period)
340  resync_phase = 0;
341  } // Work to do
342 
343  if (lock_state != previous_lock_state)
344  opt_write(lock_out, lock_state ? 1 : 0);
345 
346  opt_write(framecount_out, framecount);
347  opt_write(fcserrcount_out, fcserrcount);
348  opt_write(hdlcbytecount_out, hdlcbytecount);
349  opt_write(databytecount_out, databytecount);
350  }
int errhist[NERRHIST]
Definition: hdlc.h:380
static const int NSYNCS
Definition: hdlc.h:374
unsigned char u8
Definition: framework.h:453
pipewriter< int > * hdlcbytecount_out
Definition: hdlc.h:373
static const int NERRHIST
Definition: hdlc.h:375
int resync_phase
Definition: hdlc.h:385
pipewriter< int > * lock_out
Definition: hdlc.h:371
struct leansdr::hdlc_sync::@7 syncs[NSYNCS]
void opt_write(pipewriter< T > *p, T val)
Definition: framework.h:341
void output_frame(u8 *f, int size)
Definition: hdlc.h:353
pipewriter< int > * framecount_out
Definition: hdlc.h:372
bool opt_writable(pipewriter< T > *p, int n=1)
Definition: framework.h:335
pipereader< u8 > in
Definition: hdlc.h:369
pipewriter< int > * fcserrcount_out
Definition: hdlc.h:372
scheduler * sch
Definition: framework.h:199
bool lock_state
Definition: hdlc.h:386
int maxframesize
Definition: hdlc.h:367
int resync_period
Definition: hdlc.h:389
pipewriter< int > * databytecount_out
Definition: hdlc.h:373
pipewriter< u8 > out
Definition: hdlc.h:370
void read(unsigned long n)
Definition: framework.h:367
+ Here is the call graph for this function:

Member Data Documentation

◆ chunk_size

int leansdr::hdlc_sync::chunk_size
private

Definition at line 368 of file hdlc.h.

◆ cur_sync

int leansdr::hdlc_sync::cur_sync
private

Definition at line 384 of file hdlc.h.

◆ databytecount_out

pipewriter<int> * leansdr::hdlc_sync::databytecount_out
private

Definition at line 373 of file hdlc.h.

◆ dec

hdlc_dec* leansdr::hdlc_sync::dec

Definition at line 379 of file hdlc.h.

◆ errhist

int leansdr::hdlc_sync::errhist[NERRHIST]

Definition at line 380 of file hdlc.h.

◆ errslot

int leansdr::hdlc_sync::errslot
private

Definition at line 383 of file hdlc.h.

◆ fcserrcount_out

pipewriter<int> * leansdr::hdlc_sync::fcserrcount_out
private

Definition at line 372 of file hdlc.h.

◆ framecount_out

pipewriter<int>* leansdr::hdlc_sync::framecount_out
private

Definition at line 372 of file hdlc.h.

◆ hdlcbytecount_out

pipewriter<int>* leansdr::hdlc_sync::hdlcbytecount_out
private

Definition at line 373 of file hdlc.h.

◆ header16

bool leansdr::hdlc_sync::header16

Definition at line 390 of file hdlc.h.

◆ in

pipereader<u8> leansdr::hdlc_sync::in
private

Definition at line 369 of file hdlc.h.

◆ lock_out

pipewriter<int>* leansdr::hdlc_sync::lock_out
private

Definition at line 371 of file hdlc.h.

◆ lock_state

bool leansdr::hdlc_sync::lock_state
private

Definition at line 386 of file hdlc.h.

◆ maxframesize

int leansdr::hdlc_sync::maxframesize
private

Definition at line 367 of file hdlc.h.

◆ minframesize

int leansdr::hdlc_sync::minframesize
private

Definition at line 367 of file hdlc.h.

◆ NERRHIST

const int leansdr::hdlc_sync::NERRHIST = 2
staticprivate

Definition at line 375 of file hdlc.h.

◆ NSYNCS

const int leansdr::hdlc_sync::NSYNCS = 2
staticprivate

Definition at line 374 of file hdlc.h.

◆ out

pipewriter<u8> leansdr::hdlc_sync::out
private

Definition at line 370 of file hdlc.h.

◆ resync_period

int leansdr::hdlc_sync::resync_period

Definition at line 389 of file hdlc.h.

◆ resync_phase

int leansdr::hdlc_sync::resync_phase
private

Definition at line 385 of file hdlc.h.

◆ syncs

struct { ... } leansdr::hdlc_sync::syncs[NSYNCS]

The documentation for this struct was generated from the following file: