27#define HDLC_BIT_STUFF_ONES_LIMIT 5U
46 size_t* payload_length)
48 if (frame == NULL || payload == NULL || payload_length == NULL || out_capacity == 0 ||
56 bool found_escape =
false;
57 size_t outbyte_ctr = 0;
58 for (
size_t frame_cntr = 1; frame_cntr < frame->
length - 1; frame_cntr++)
65 if (outbyte_ctr >= out_capacity)
82 uint16_t crc16 = payload[outbyte_ctr - 2] << 8 | payload[outbyte_ctr - 1];
83 *payload_length = (outbyte_ctr - 2);
86 uint16_t recovered_crc =
hdlc_crc16(payload, *payload_length);
87 if (crc16 != recovered_crc)
103#define HDLC_DEC_BYTE_IDX(raw_bit_index) (1 + ((raw_bit_index) / CHAR_BIT))
104#define HDLC_DEC_BIT_IDX(raw_bit_index) ((raw_bit_index) % CHAR_BIT)
105#define HDLC_DEC_BIT_POS(decoder) \
106 ((decoder)->lsb_first ? (HDLC_DEC_BIT_IDX((decoder)->raw_bit_index)) \
107 : ((7 - HDLC_DEC_BIT_IDX((decoder)->raw_bit_index))))
108#define HDLC_DEC_GET_OUT_BIT(decoder) \
109 (frame->payload[HDLC_DEC_BYTE_IDX((decoder)->raw_bit_index)] >> HDLC_DEC_BIT_POS((decoder)) & 1)
114 if (!decoder || !frame || !out_bit)
118 size_t raw_bits_total = (frame->
length > 2) ? (frame->
length - 2) * CHAR_BIT : 0;
156 for (uint8_t i = 0; i < CHAR_BIT; i++)
158 size_t bit_position = 0;
165 bit_position = decoder->
lsb_first ? i : (CHAR_BIT - 1U - i);
166 *
byte |= bit << bit_position;
172 size_t* payload_length,
bool lsb_first)
174 if (frame == NULL || payload == NULL || payload_length == NULL || out_capacity == 0 ||
183 size_t outbyte_ctr = 0;
185 .
raw_bit_index = 0, .ones_run = 0, .skip_next_zero =
false, .lsb_first = lsb_first};
200 if (outbyte_ctr >= out_capacity)
206 payload[outbyte_ctr++] = out_byte;
215 uint16_t crc16 = payload[outbyte_ctr - 2] << CHAR_BIT | payload[outbyte_ctr - 1];
216 *payload_length = (outbyte_ctr - 2);
219 uint16_t recovered_crc =
hdlc_crc16(payload, *payload_length);
220 if (crc16 != recovered_crc)
239 if (out_stats == NULL)
uint16_t hdlc_crc16(const uint8_t *payload, size_t num_bytes)
Compute HDLC CRC16 (FCS) over a payload.
static hdlc_decode_stats_t hdlc_decode_stats
void hdlc_decode_stats_reset(void)
Reset cumulative HDLC decode statistics.
static hdlc_decoder_bit_type_t hdlc_get_bit(hdlc_decoder_t *decoder, const HDLC_FRAME_T *frame, uint8_t *out_bit)
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.
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.
#define HDLC_DEC_GET_OUT_BIT(decoder)
static hdlc_decoder_bit_type_t hdlc_get_byte(hdlc_decoder_t *decoder, const HDLC_FRAME_T *frame, uint8_t *byte)
#define HDLC_BIT_STUFF_ONES_LIMIT
Generic HDLC frame buffer descriptor.
Cumulative HDLC decode failure reason counters.
uint64_t payload_too_long