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

Go to the source code of this file.

Data Structures

struct  hdlc_decoder_t
 

Macros

#define HDLC_BIT_STUFF_ONES_LIMIT   5U
 
#define HDLC_DEC_BYTE_IDX(raw_bit_index)   (1 + ((raw_bit_index) / CHAR_BIT))
 
#define HDLC_DEC_BIT_IDX(raw_bit_index)   ((raw_bit_index) % CHAR_BIT)
 
#define HDLC_DEC_BIT_POS(decoder)
 
#define HDLC_DEC_GET_OUT_BIT(decoder)    (frame->payload[HDLC_DEC_BYTE_IDX((decoder)->raw_bit_index)] >> HDLC_DEC_BIT_POS((decoder)) & 1)
 

Enumerations

enum  hdlc_decoder_bit_type_t { HDLC_BIT_OK , HDLC_BIT_EOF , HDLC_BIT_ERR }
 

Functions

bool hdlc_decode_byte (const HDLC_FRAME_T *frame, uint8_t *payload, const size_t out_capacity, size_t *payload_length)
 Decode a byte-escaped HDLC frame compatibility path.
 
static hdlc_decoder_bit_type_t hdlc_get_bit (hdlc_decoder_t *decoder, const HDLC_FRAME_T *frame, uint8_t *out_bit)
 
static hdlc_decoder_bit_type_t hdlc_get_byte (hdlc_decoder_t *decoder, const HDLC_FRAME_T *frame, uint8_t *byte)
 
bool hdlc_decode (const HDLC_FRAME_T *frame, uint8_t *payload, const size_t out_capacity, size_t *payload_length, bool lsb_first)
 Decode an HDLC bit-stuffed frame.
 
void hdlc_decode_stats_snapshot (hdlc_decode_stats_t *out_stats)
 Copy cumulative HDLC decode statistics.
 
void hdlc_decode_stats_reset (void)
 Reset cumulative HDLC decode statistics.
 

Variables

static hdlc_decode_stats_t hdlc_decode_stats = {0}
 

Macro Definition Documentation

◆ HDLC_BIT_STUFF_ONES_LIMIT

#define HDLC_BIT_STUFF_ONES_LIMIT   5U

Definition at line 27 of file hdlc_decoder.c.

◆ HDLC_DEC_BIT_IDX

#define HDLC_DEC_BIT_IDX (   raw_bit_index)    ((raw_bit_index) % CHAR_BIT)

Definition at line 104 of file hdlc_decoder.c.

◆ HDLC_DEC_BIT_POS

#define HDLC_DEC_BIT_POS (   decoder)
Value:
((decoder)->lsb_first ? (HDLC_DEC_BIT_IDX((decoder)->raw_bit_index)) \
: ((7 - HDLC_DEC_BIT_IDX((decoder)->raw_bit_index))))
#define HDLC_DEC_BIT_IDX(raw_bit_index)

Definition at line 105 of file hdlc_decoder.c.

◆ HDLC_DEC_BYTE_IDX

#define HDLC_DEC_BYTE_IDX (   raw_bit_index)    (1 + ((raw_bit_index) / CHAR_BIT))

Definition at line 103 of file hdlc_decoder.c.

◆ HDLC_DEC_GET_OUT_BIT

#define HDLC_DEC_GET_OUT_BIT (   decoder)     (frame->payload[HDLC_DEC_BYTE_IDX((decoder)->raw_bit_index)] >> HDLC_DEC_BIT_POS((decoder)) & 1)

Definition at line 108 of file hdlc_decoder.c.

Enumeration Type Documentation

◆ hdlc_decoder_bit_type_t

Enumerator
HDLC_BIT_OK 
HDLC_BIT_EOF 
HDLC_BIT_ERR 

Definition at line 36 of file hdlc_decoder.c.

Function Documentation

◆ hdlc_decode()

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

Decode an HDLC bit-stuffed frame.

Parameters
frameInput HDLC frame.
payloadOutput payload buffer.
out_capacityCapacity of payload in bytes.
payload_lengthOutput payload length.
lsb_firstBit order selector matching encoder/PIO path.
Returns
true on successful decode and CRC validation.

Definition at line 171 of file hdlc_decoder.c.

References hdlc_decode_stats_t::crc_mismatch, HDLC_BIT_EOF, HDLC_BIT_ERR, hdlc_crc16(), hdlc_decode_stats, HDLC_FLAG_BYTE, hdlc_get_byte(), hdlc_decode_stats_t::invalid_frame, HDLC_FRAME_T::length, LOG_DEBUG, HDLC_FRAME_T::payload, hdlc_decode_stats_t::payload_too_long, hdlc_decoder_t::raw_bit_index, hdlc_decode_stats_t::too_short, and hdlc_decode_stats_t::unstuff_error.

Referenced by decode_hdlc_to_udp_tx(), and event_dispatch().

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

◆ hdlc_decode_byte()

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

Decode a byte-escaped HDLC frame compatibility path.

Parameters
frameInput HDLC frame.
payloadOutput payload buffer.
out_capacityCapacity of payload in bytes.
payload_lengthOutput payload length.
Returns
true on successful decode and CRC validation.

Definition at line 45 of file hdlc_decoder.c.

References hdlc_decode_stats_t::crc_mismatch, hdlc_crc16(), hdlc_decode_stats, HDLC_ESCAPE_BYTE, HDLC_ESCAPE_XOR, HDLC_FLAG_BYTE, hdlc_decode_stats_t::invalid_frame, HDLC_FRAME_T::length, LOG_DEBUG, HDLC_FRAME_T::payload, hdlc_decode_stats_t::payload_too_long, and hdlc_decode_stats_t::too_short.

Here is the call graph for this function:

◆ hdlc_decode_stats_reset()

void hdlc_decode_stats_reset ( void  )

Reset cumulative HDLC decode statistics.

Definition at line 246 of file hdlc_decoder.c.

References hdlc_decode_stats.

◆ hdlc_decode_stats_snapshot()

void hdlc_decode_stats_snapshot ( hdlc_decode_stats_t out_stats)

Copy cumulative HDLC decode statistics.

Parameters
out_statsDestination pointer.

Definition at line 237 of file hdlc_decoder.c.

References hdlc_decode_stats.

Referenced by update_statistics().

Here is the caller graph for this function:

◆ hdlc_get_bit()

static hdlc_decoder_bit_type_t hdlc_get_bit ( hdlc_decoder_t decoder,
const HDLC_FRAME_T frame,
uint8_t *  out_bit 
)
static

◆ hdlc_get_byte()

static hdlc_decoder_bit_type_t hdlc_get_byte ( hdlc_decoder_t decoder,
const HDLC_FRAME_T frame,
uint8_t *  byte 
)
static

Definition at line 152 of file hdlc_decoder.c.

References HDLC_BIT_OK, hdlc_get_bit(), and hdlc_decoder_t::lsb_first.

Referenced by hdlc_decode().

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

Variable Documentation

◆ hdlc_decode_stats

hdlc_decode_stats_t hdlc_decode_stats = {0}
static