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_dec Struct Reference

#include <hdlc.h>

Public Member Functions

 hdlc_dec (int _minframesize, int _maxframesize, bool _invert)
 
void reset ()
 
void begin_frame ()
 
u8decode (u8 **ppin, int count, int *pdatasize, int *hdlc_errors, int *fcs_errors)
 

Public Attributes

bool debug
 

Private Member Functions

void crc16_byte (u8 data)
 

Private Attributes

int minframesize
 
int maxframesize
 
u8 invertmask
 
u8framebuf
 
u8 shiftreg
 
bool inframe
 
u8 byte_out
 
int nbits_out
 
int framesize
 
u16 crc16
 

Static Private Attributes

static const u16 crc16_init = 0xffff
 
static const u16 crc16_poly = 0x8408
 
static const u16 crc16_check = 0x0f47
 

Detailed Description

Definition at line 27 of file hdlc.h.

Constructor & Destructor Documentation

◆ hdlc_dec()

leansdr::hdlc_dec::hdlc_dec ( int  _minframesize,
int  _maxframesize,
bool  _invert 
)
inline

Definition at line 30 of file hdlc.h.

References reset().

Referenced by leansdr::hdlc_sync::hdlc_sync().

32  : minframesize(_minframesize),
33  maxframesize(_maxframesize),
34  invertmask(_invert ? 0xff : 0),
35  framebuf(new u8[maxframesize]),
36  debug(false)
37  {
38  reset();
39  }
unsigned char u8
Definition: framework.h:453
void reset()
Definition: hdlc.h:41
int minframesize
Definition: hdlc.h:165
u8 * framebuf
Definition: hdlc.h:167
int maxframesize
Definition: hdlc.h:165
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Function Documentation

◆ begin_frame()

void leansdr::hdlc_dec::begin_frame ( )
inline

Definition at line 47 of file hdlc.h.

References crc16, crc16_init, and framesize.

Referenced by decode().

48  {
49  framesize = 0;
50  crc16 = crc16_init;
51  }
static const u16 crc16_init
Definition: hdlc.h:176
+ Here is the caller graph for this function:

◆ crc16_byte()

void leansdr::hdlc_dec::crc16_byte ( u8  data)
inlineprivate

Definition at line 180 of file hdlc.h.

References crc16_poly.

Referenced by decode().

181  {
182  crc16 ^= data;
183 
184  for (int bit = 8; bit--;)
185  {
186  crc16 = (crc16 & 1) ? (crc16 >> 1) ^ crc16_poly : (crc16 >> 1);
187  }
188  }
static const u16 crc16_poly
Definition: hdlc.h:177
+ Here is the caller graph for this function:

◆ decode()

u8* leansdr::hdlc_dec::decode ( u8 **  ppin,
int  count,
int *  pdatasize,
int *  hdlc_errors,
int *  fcs_errors 
)
inline

Definition at line 59 of file hdlc.h.

References begin_frame(), byte_out, crc16, crc16_byte(), crc16_check, debug, framebuf, framesize, inframe, invertmask, maxframesize, minframesize, nbits_out, and shiftreg.

60  {
61  *hdlc_errors = 0;
62  *fcs_errors = 0;
63  *pdatasize = -1;
64  u8 *pin = *ppin, *pend = pin + count;
65 
66  for (; pin < pend; ++pin)
67  {
68  u8 byte_in = (*pin) ^ invertmask;
69 
70  for (int bits = 8; bits--; byte_in <<= 1)
71  {
72  u8 bit_in = byte_in & 128;
73  shiftreg = (shiftreg >> 1) | bit_in;
74 
75  if (!inframe)
76  {
77  if (shiftreg == 0x7e)
78  { // HDLC flag 01111110
79  inframe = true;
80  nbits_out = 0;
81  begin_frame();
82  }
83  }
84  else
85  {
86  if ((shiftreg & 0xfe) == 0x7c)
87  { // 0111110x HDLC stuffing
88  // Unstuff this 0
89  }
90  else if (shiftreg == 0x7e)
91  { // 01111110 HDLC flag
92  if (nbits_out != 7)
93  {
94  // Not at byte boundary
95  if (debug)
96  fprintf(stderr, "^");
97  ++*hdlc_errors;
98  }
99  else
100  {
101  // Checksum
102  crc16 ^= 0xffff;
103  if (framesize < 2 || framesize < minframesize || crc16 != crc16_check)
104  {
105  if (debug)
106  fprintf(stderr, "!");
107  ++*hdlc_errors;
108  // Do not report random noise as FCS errors
109  if (framesize >= minframesize)
110  ++*fcs_errors;
111  }
112  else
113  {
114  if (debug)
115  fprintf(stderr, "_");
116  // This will trigger output, but we finish the byte first.
117  *pdatasize = framesize - 2;
118  }
119  }
120  nbits_out = 0;
121  begin_frame();
122  // Keep processing up to 7 remaining bits from byte_in.
123  // Special cases 0111111 and 1111111 cannot affect *pdatasize.
124  }
125  else if (shiftreg == 0xfe)
126  { // 11111110 HDLC invalid
127  if (framesize)
128  {
129  if (debug)
130  fprintf(stderr, "^");
131  ++*hdlc_errors;
132  }
133  inframe = false;
134  }
135  else
136  { // Data bit
137  byte_out = (byte_out >> 1) | bit_in; // HDLC is LSB first
138  ++nbits_out;
139  if (nbits_out == 8)
140  {
141  if (framesize < maxframesize)
142  {
145  }
146  nbits_out = 0;
147  }
148  }
149  } // inframe
150  } // bits
151  if (*pdatasize != -1)
152  {
153  // Found a complete frame
154  *ppin = pin + 1;
155  return framebuf;
156  }
157  }
158 
159  *ppin = pin;
160  return NULL;
161  }
void crc16_byte(u8 data)
Definition: hdlc.h:180
static const u16 crc16_check
Definition: hdlc.h:178
unsigned char u8
Definition: framework.h:453
bool inframe
Definition: hdlc.h:170
int minframesize
Definition: hdlc.h:165
u8 * framebuf
Definition: hdlc.h:167
void begin_frame()
Definition: hdlc.h:47
int maxframesize
Definition: hdlc.h:165
+ Here is the call graph for this function:

◆ reset()

void leansdr::hdlc_dec::reset ( )
inline

Definition at line 41 of file hdlc.h.

References inframe, and shiftreg.

Referenced by hdlc_dec().

42  {
43  shiftreg = 0;
44  inframe = false;
45  }
bool inframe
Definition: hdlc.h:170
+ Here is the caller graph for this function:

Member Data Documentation

◆ byte_out

u8 leansdr::hdlc_dec::byte_out
private

Definition at line 171 of file hdlc.h.

Referenced by decode().

◆ crc16

u16 leansdr::hdlc_dec::crc16
private

Definition at line 174 of file hdlc.h.

Referenced by begin_frame(), and decode().

◆ crc16_check

const u16 leansdr::hdlc_dec::crc16_check = 0x0f47
staticprivate

Definition at line 178 of file hdlc.h.

Referenced by decode().

◆ crc16_init

const u16 leansdr::hdlc_dec::crc16_init = 0xffff
staticprivate

Definition at line 176 of file hdlc.h.

Referenced by begin_frame().

◆ crc16_poly

const u16 leansdr::hdlc_dec::crc16_poly = 0x8408
staticprivate

Definition at line 177 of file hdlc.h.

Referenced by crc16_byte().

◆ debug

bool leansdr::hdlc_dec::debug

Definition at line 191 of file hdlc.h.

Referenced by decode().

◆ framebuf

u8* leansdr::hdlc_dec::framebuf
private

Definition at line 167 of file hdlc.h.

Referenced by decode().

◆ framesize

int leansdr::hdlc_dec::framesize
private

Definition at line 173 of file hdlc.h.

Referenced by begin_frame(), and decode().

◆ inframe

bool leansdr::hdlc_dec::inframe
private

Definition at line 170 of file hdlc.h.

Referenced by decode(), and reset().

◆ invertmask

u8 leansdr::hdlc_dec::invertmask
private

Definition at line 166 of file hdlc.h.

Referenced by decode().

◆ maxframesize

int leansdr::hdlc_dec::maxframesize
private

Definition at line 165 of file hdlc.h.

Referenced by decode(), leansdr::hdlc_sync::hdlc_sync(), and leansdr::hdlc_sync::run().

◆ minframesize

int leansdr::hdlc_dec::minframesize
private

Definition at line 165 of file hdlc.h.

Referenced by decode(), and leansdr::hdlc_sync::hdlc_sync().

◆ nbits_out

int leansdr::hdlc_dec::nbits_out
private

Definition at line 172 of file hdlc.h.

Referenced by decode().

◆ shiftreg

u8 leansdr::hdlc_dec::shiftreg
private

Definition at line 169 of file hdlc.h.

Referenced by decode(), and reset().


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