ether2ser 0.1.0
Ethernet <-> synchronous V.24 bridge firmware for RP2040 + W5500
Loading...
Searching...
No Matches
hdlc_encoder.c File Reference
#include "hdlc_encoder.h"
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "hdlc_common.h"
#include "system/common.h"
Include dependency graph for hdlc_encoder.c:

Go to the source code of this file.

Data Structures

struct  hdlc_encoder_t
 

Macros

#define HDLC_TRY_PUT_BYTE(byte, frame, error_handling)
 
#define HDLC_BIT_STUFF_ONES_LIMIT   5U
 
#define SHIFT_IN_BIT(out_byte, bitorder, bit, bitpos)    (bitorder) ? ((out_byte) | ((bit) << (bitpos))) : ((out_byte) | ((bit) << (7 - (bitpos))))
 
#define ENCODER_TRY_FLUSH_BYTE_OUT(encoder, frame, error_handling)
 
#define ENCODER_ZERO_PAD_AND_DRAIN(encoder, frame, error_handling)
 

Functions

static bool hdlc_escape_if_needed (uint8_t byte, HDLC_FRAME_T *frame)
 
bool hdlc_encode_byte (const uint8_t *payload, const size_t payload_length, HDLC_FRAME_T *frame)
 Encode payload using byte-escaping HDLC compatibility path.
 
static bool hdlc_put_bit (hdlc_encoder_t *encoder, bool bit, HDLC_FRAME_T *frame)
 
static bool hdlc_put_byte (hdlc_encoder_t *encoder, uint8_t byte, HDLC_FRAME_T *frame)
 
bool hdlc_encode (const uint8_t *payload, const size_t payload_length, HDLC_FRAME_T *frame, bool lsb_first)
 Encode payload into an HDLC bit-stuffed frame.
 

Macro Definition Documentation

◆ ENCODER_TRY_FLUSH_BYTE_OUT

#define ENCODER_TRY_FLUSH_BYTE_OUT (   encoder,
  frame,
  error_handling 
)
Value:
do \
{ \
if ((++(encoder)->out_bits_used) == CHAR_BIT) \
{ \
HDLC_TRY_PUT_BYTE(((encoder)->out_byte), (frame), error_handling); \
(encoder)->out_bits_used = 0; \
(encoder)->out_byte = 0; \
} \
} while (0)

Definition at line 114 of file hdlc_encoder.c.

◆ ENCODER_ZERO_PAD_AND_DRAIN

#define ENCODER_ZERO_PAD_AND_DRAIN (   encoder,
  frame,
  error_handling 
)
Value:
do \
{ \
if ((encoder)->out_bits_used > 0) \
{ \
HDLC_TRY_PUT_BYTE((encoder)->out_byte, frame, error_handling); \
(encoder)->out_byte = 0; \
(encoder)->ones_run = 0; \
(encoder)->out_bits_used = 0; \
} \
} while (0)

Definition at line 125 of file hdlc_encoder.c.

◆ HDLC_BIT_STUFF_ONES_LIMIT

#define HDLC_BIT_STUFF_ONES_LIMIT   5U

Definition at line 39 of file hdlc_encoder.c.

◆ HDLC_TRY_PUT_BYTE

#define HDLC_TRY_PUT_BYTE (   byte,
  frame,
  error_handling 
)
Value:
do \
{ \
if ((frame)->length + 1 > (frame)->capacity) \
{ \
goto error_handling; \
} \
(frame)->payload[(frame)->length++] = byte; \
} while (0)

Definition at line 29 of file hdlc_encoder.c.

◆ SHIFT_IN_BIT

#define SHIFT_IN_BIT (   out_byte,
  bitorder,
  bit,
  bitpos 
)     (bitorder) ? ((out_byte) | ((bit) << (bitpos))) : ((out_byte) | ((bit) << (7 - (bitpos))))

Definition at line 111 of file hdlc_encoder.c.

Function Documentation

◆ hdlc_encode()

bool hdlc_encode ( const uint8_t *  payload,
const size_t  payload_length,
HDLC_FRAME_T frame,
bool  lsb_first 
)

Encode payload into an HDLC bit-stuffed frame.

Parameters
payloadInput payload bytes.
payload_lengthNumber of payload bytes.
frameOutput frame descriptor and storage.
lsb_firstBit order selector for bit-wise encoding.
Returns
true on success, false on invalid args or capacity overflow.

Definition at line 171 of file hdlc_encoder.c.

References HDLC_FRAME_T::capacity, ENCODER_ZERO_PAD_AND_DRAIN, hdlc_crc16(), HDLC_FLAG_BYTE, hdlc_put_byte(), HDLC_TRY_PUT_BYTE, HDLC_FRAME_T::length, LOG_DEBUG, hdlc_encoder_t::out_byte, and HDLC_FRAME_T::payload.

Referenced by tx_queue_enqueue_udp_frame().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hdlc_encode_byte()

bool hdlc_encode_byte ( const uint8_t *  payload,
const size_t  payload_length,
HDLC_FRAME_T frame 
)

Encode payload using byte-escaping HDLC compatibility path.

Parameters
payloadInput payload bytes.
payload_lengthNumber of payload bytes.
frameOutput frame descriptor and storage.
Returns
true on success, false on invalid args or capacity overflow.

Definition at line 57 of file hdlc_encoder.c.

References HDLC_FRAME_T::capacity, hdlc_crc16(), hdlc_escape_if_needed(), HDLC_FLAG_BYTE, HDLC_TRY_PUT_BYTE, HDLC_FRAME_T::length, LOG_DEBUG, and HDLC_FRAME_T::payload.

Here is the call graph for this function:

◆ hdlc_escape_if_needed()

static bool hdlc_escape_if_needed ( uint8_t  byte,
HDLC_FRAME_T frame 
)
static

Definition at line 41 of file hdlc_encoder.c.

References HDLC_ESCAPE_BYTE, HDLC_ESCAPE_XOR, HDLC_FLAG_BYTE, and HDLC_TRY_PUT_BYTE.

Referenced by hdlc_encode_byte().

Here is the caller graph for this function:

◆ hdlc_put_bit()

static bool hdlc_put_bit ( hdlc_encoder_t encoder,
bool  bit,
HDLC_FRAME_T frame 
)
static

◆ hdlc_put_byte()

static bool hdlc_put_byte ( hdlc_encoder_t encoder,
uint8_t  byte,
HDLC_FRAME_T frame 
)
static

Definition at line 156 of file hdlc_encoder.c.

References hdlc_put_bit(), and hdlc_encoder_t::lsb_first.

Referenced by hdlc_encode().

Here is the call graph for this function:
Here is the caller graph for this function: