arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
HDLCStream Class Reference

High-Level Data Link Control (HDLC) is a bit-oriented code-transparent synchronous data link layer protocol for reliable, framed, and error-checked communication. More...

#include <HDLCStream.h>

Inheritance diagram for HDLCStream:
Stream Print

Public Member Functions

 HDLCStream (Print &stream, size_t maxFrameSize)
 Construct a new HDLCStream object using a Print for output only.
 
 HDLCStream (Stream &stream, size_t maxFrameSize)
 Construct a new HDLCStream object using a Stream for input and output.
 
int available () override
 Get the number of bytes available to read from the frame buffer.
 
virtual int availableForWrite ()
 
void flush () override
 Flush the output buffer of the underlying stream.
 
 operator bool ()
 
int peek () override
 Not supported.
 
int read () override
 Not supported.
 
size_t readBytes (uint8_t *buffer, size_t length) override
 Read a full frame from the stream into a buffer.
 
size_t write (const uint8_t *data, size_t len) override
 Write multiple bytes to the stream.
 
size_t write (uint8_t b) override
 Not supported.
 

Protected Types

enum  RxState { IDLE , RECEIVING , ESCAPED }
 

Protected Member Functions

uint16_t _crc16 (uint8_t data, uint16_t crc)
 Calculate CRC-CCITT (16-bit)
 
void _processInput ()
 Process incoming bytes, detect frames, validate CRC and prepare data for reading.
 
void _writeEscaped (uint8_t b)
 Write a byte with proper HDLC byte stuffing if needed.
 
size_t writeFrame (const uint8_t *data, size_t len)
 Write a complete HDLC frame with proper framing and CRC.
 

Protected Attributes

size_t _frameLen = 0
 
bool _frameReady = false
 
const size_t _maxFrameSize
 
Vector< uint8_t > _rxBuffer
 
size_t _rxLen = 0
 
size_t _rxPos = 0
 
enum audio_tools::HDLCStream::RxState _rxState = IDLE
 
int _timeout = 10
 
Printp_print = nullptr
 
Streamp_stream = nullptr
 
SingleBuffer< uint8_t > rx_frame_buffer
 
SingleBuffer< uint8_t > tx_frame_buffer
 

Static Protected Attributes

static constexpr uint8_t HDLC_ESC = 0x7D
 
static constexpr uint8_t HDLC_ESC_XOR = 0x20
 
static constexpr uint8_t HDLC_FLAG = 0x7E
 

Detailed Description

High-Level Data Link Control (HDLC) is a bit-oriented code-transparent synchronous data link layer protocol for reliable, framed, and error-checked communication.

This class implements HDLC framing with:

Author
Phil Schatzmann

Constructor & Destructor Documentation

◆ HDLCStream() [1/2]

HDLCStream ( Stream stream,
size_t  maxFrameSize 
)
inline

Construct a new HDLCStream object using a Stream for input and output.

Parameters
streamThe underlying Stream for both reading and writing
maxFrameSizeMaximum size of a single HDLC frame

◆ HDLCStream() [2/2]

HDLCStream ( Print stream,
size_t  maxFrameSize 
)
inline

Construct a new HDLCStream object using a Print for output only.

Parameters
streamThe underlying Print for writing
maxFrameSizeMaximum size of a single HDLC frame

Member Function Documentation

◆ _crc16()

uint16_t _crc16 ( uint8_t  data,
uint16_t  crc 
)
inlineprotected

Calculate CRC-CCITT (16-bit)

Parameters
dataByte to include in CRC calculation
crcCurrent CRC value
Returns
uint16_t Updated CRC value

◆ _writeEscaped()

void _writeEscaped ( uint8_t  b)
inlineprotected

Write a byte with proper HDLC byte stuffing if needed.

Parameters
bByte to write

◆ available()

int available ( )
inlineoverridevirtual

Get the number of bytes available to read from the frame buffer.

Returns
int Number of bytes available

Reimplemented from Stream.

◆ availableForWrite()

virtual int availableForWrite ( )
inlinevirtualinherited

◆ flush()

void flush ( )
inlineoverridevirtual

Flush the output buffer of the underlying stream.

Reimplemented from Print.

◆ peek()

int peek ( )
inlineoverride

Not supported.

Returns
-1

◆ read()

int read ( )
inlineoverride

Not supported.

Returns
-1

◆ readBytes()

size_t readBytes ( uint8_t *  buffer,
size_t  length 
)
inlineoverridevirtual

Read a full frame from the stream into a buffer.

Parameters
bufferDestination buffer to hold the data
lengthMaximum number of bytes to read
Returns
size_t Actual number of bytes read

Reimplemented from Stream.

◆ write() [1/2]

size_t write ( const uint8_t *  data,
size_t  len 
)
inlineoverridevirtual

Write multiple bytes to the stream.

Parameters
dataPointer to the data buffer
lenNumber of bytes to write
Returns
size_t Number of bytes written

Reimplemented from Print.

◆ write() [2/2]

size_t write ( uint8_t  b)
inlineoverride

Not supported.

Parameters
bThe byte to write
Returns
0

◆ writeFrame()

size_t writeFrame ( const uint8_t *  data,
size_t  len 
)
inlineprotected

Write a complete HDLC frame with proper framing and CRC.

Parameters
dataData to be framed
lenLength of data
Returns
size_t Number of bytes in the original data

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