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

Go to the source code of this file.

Macros

#define HDLC_BITS_PER_BYTE   8U
 
#define HDLC_SYNC_MIN_FRAME_SIZE_BYTES   4U
 
#define HDLC_SYNC_MAX_REASONABLE_FRAME_SIZE_BYTES   2048U
 
#define HDLC_SYNC_SHORT_BUFFER_SEARCH_LIMIT   64U
 

Enumerations

enum  HDLC_SYNC_POLL_STEP_RESULT_T { HDLC_SYNC_POLL_STEP_PROGRESS , HDLC_SYNC_POLL_STEP_STOP , HDLC_SYNC_POLL_STEP_FRAME_READY , HDLC_SYNC_POLL_STEP_ERROR }
 

Functions

static void hdlc_sync_drop_prefix (HDLC_SYNC_ACCUMULATOR_T *accumulator, size_t drop_count)
 
static void hdlc_sync_reset_hunting_state (HDLC_SYNC_ACCUMULATOR_T *accumulator)
 
static void hdlc_sync_reject_oversized_candidate (HDLC_SYNC_ACCUMULATOR_T *accumulator, HDLC_FRAME_T *out_frame, size_t *scan_index)
 
static bool hdlc_sync_get_aligned_byte (const HDLC_SYNC_ACCUMULATOR_T *accumulator, size_t raw_index, uint8_t bit_offset, bool shift_right, uint8_t *out_byte)
 
static bool hdlc_sync_find_opening_candidate (const HDLC_SYNC_ACCUMULATOR_T *accumulator, size_t scan_index, bool allow_left_shift, size_t *out_start_index, uint8_t *out_bit_pos, bool *out_shift_right)
 
static bool hdlc_sync_find_complete_candidate_short (const HDLC_SYNC_ACCUMULATOR_T *accumulator, size_t scan_index, size_t *out_start_index, uint8_t *out_bit_pos, bool *out_shift_right)
 
static HDLC_SYNC_POLL_STEP_RESULT_T hdlc_sync_step_hunting (HDLC_SYNC_ACCUMULATOR_T *accumulator, HDLC_FRAME_T *out_frame, size_t *scan_index)
 
static HDLC_SYNC_POLL_STEP_RESULT_T hdlc_sync_step_syncing (HDLC_SYNC_ACCUMULATOR_T *accumulator, HDLC_FRAME_T *out_frame, size_t *scan_index, e2s_error_t *out_error)
 
static HDLC_SYNC_POLL_STEP_RESULT_T hdlc_sync_step_synced (HDLC_SYNC_ACCUMULATOR_T *accumulator, HDLC_FRAME_T *out_frame, size_t *scan_index, e2s_error_t *out_error)
 
static void hdlc_sync_finalize_poll_no_frame (HDLC_SYNC_ACCUMULATOR_T *accumulator, size_t scan_index)
 
void hdlc_sync_acc_init (HDLC_SYNC_ACCUMULATOR_T *accumulator, uint8_t sync_byte)
 Initialize HDLC sync accumulator state.
 
bool hdlc_sync_acc_process_byte (HDLC_SYNC_ACCUMULATOR_T *accumulator, uint8_t byte)
 Append one received raw byte to the accumulator.
 
e2s_error_t hdlc_sync_acc_poll (HDLC_SYNC_ACCUMULATOR_T *accumulator, HDLC_FRAME_T *out_frame)
 Poll accumulator for an aligned HDLC frame candidate.
 
void hdlc_sync_acc_consume_candidate (HDLC_SYNC_ACCUMULATOR_T *accumulator, bool accept)
 Consume current candidate and advance accumulator window.
 

Macro Definition Documentation

◆ HDLC_BITS_PER_BYTE

#define HDLC_BITS_PER_BYTE   8U

Definition at line 26 of file hdlc_sync.c.

◆ HDLC_SYNC_MAX_REASONABLE_FRAME_SIZE_BYTES

#define HDLC_SYNC_MAX_REASONABLE_FRAME_SIZE_BYTES   2048U

Definition at line 28 of file hdlc_sync.c.

◆ HDLC_SYNC_MIN_FRAME_SIZE_BYTES

#define HDLC_SYNC_MIN_FRAME_SIZE_BYTES   4U

Definition at line 27 of file hdlc_sync.c.

◆ HDLC_SYNC_SHORT_BUFFER_SEARCH_LIMIT

#define HDLC_SYNC_SHORT_BUFFER_SEARCH_LIMIT   64U

Definition at line 29 of file hdlc_sync.c.

Enumeration Type Documentation

◆ HDLC_SYNC_POLL_STEP_RESULT_T

Enumerator
HDLC_SYNC_POLL_STEP_PROGRESS 
HDLC_SYNC_POLL_STEP_STOP 
HDLC_SYNC_POLL_STEP_FRAME_READY 
HDLC_SYNC_POLL_STEP_ERROR 

Definition at line 57 of file hdlc_sync.c.

Function Documentation

◆ hdlc_sync_acc_consume_candidate()

void hdlc_sync_acc_consume_candidate ( HDLC_SYNC_ACCUMULATOR_T accumulator,
bool  accept 
)

Consume current candidate and advance accumulator window.

Parameters
accumulatorAccumulator instance.
acceptAcceptance hint for candidate handling.

Definition at line 622 of file hdlc_sync.c.

References HDLC_SYNC_ACCUMULATOR_T::candidate_end, HDLC_SYNC_ACCUMULATOR_T::candidate_start, HDLC_SYNC_ACCUMULATOR_T::candidate_valid, HDLC_SYNC_ACCUMULATOR_T::consume_count, HDLC_SYNC_ACCUMULATOR_T::hardcap_drop_bytes, HDLC_SYNC_ACCUMULATOR_T::hardcap_drop_events, hdlc_sync_drop_prefix(), hdlc_sync_reset_hunting_state(), HDLC_SYNC_ACCUMULATOR_T::position, HDLC_SYNC_ACCUMULATOR_T::processed, and RX_HDLC_SYNC_MAX_BUFFER_SIZE.

Referenced by decode_hdlc_to_udp_tx().

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

◆ hdlc_sync_acc_init()

◆ hdlc_sync_acc_poll()

e2s_error_t hdlc_sync_acc_poll ( HDLC_SYNC_ACCUMULATOR_T accumulator,
HDLC_FRAME_T out_frame 
)

Poll accumulator for an aligned HDLC frame candidate.

Parameters
accumulatorAccumulator instance.
out_frameOutput aligned frame buffer.
Returns
E2S_ERR_HDLC_ACC_FRAME_READY when a candidate is available, otherwise status code.

Definition at line 549 of file hdlc_sync.c.

References HDLC_SYNC_ACCUMULATOR_T::bit_offset, HDLC_FRAME_T::capacity, E2S_ERR_HDLC_ACC_FRAME_READY, E2S_OK, hdlc_sync_finalize_poll_no_frame(), HDLC_SYNC_POLL_STEP_ERROR, HDLC_SYNC_POLL_STEP_FRAME_READY, HDLC_SYNC_POLL_STEP_PROGRESS, HDLC_SYNC_POLL_STEP_STOP, HDLC_SYNC_STATE_HUNTING, HDLC_SYNC_STATE_SYNCED, HDLC_SYNC_STATE_SYNCING, hdlc_sync_step_hunting(), hdlc_sync_step_synced(), hdlc_sync_step_syncing(), HDLC_FRAME_T::payload, HDLC_SYNC_ACCUMULATOR_T::position, HDLC_SYNC_ACCUMULATOR_T::processed, and HDLC_SYNC_ACCUMULATOR_T::state.

Referenced by drain_hdlc_frames_to_udp().

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

◆ hdlc_sync_acc_process_byte()

bool hdlc_sync_acc_process_byte ( HDLC_SYNC_ACCUMULATOR_T accumulator,
uint8_t  byte 
)

Append one received raw byte to the accumulator.

Parameters
accumulatorAccumulator instance.
byteReceived raw byte.
Returns
true if appended, false if buffer full or invalid args.

Definition at line 527 of file hdlc_sync.c.

References HDLC_SYNC_ACCUMULATOR_T::buffer, HDLC_SYNC_ACCUMULATOR_T::position, and RX_HDLC_SYNC_MAX_BUFFER_SIZE.

Referenced by drain_rx_until_empty().

Here is the caller graph for this function:

◆ hdlc_sync_drop_prefix()

static void hdlc_sync_drop_prefix ( HDLC_SYNC_ACCUMULATOR_T accumulator,
size_t  drop_count 
)
static

◆ hdlc_sync_finalize_poll_no_frame()

static void hdlc_sync_finalize_poll_no_frame ( HDLC_SYNC_ACCUMULATOR_T accumulator,
size_t  scan_index 
)
static

Definition at line 483 of file hdlc_sync.c.

References hdlc_sync_drop_prefix(), HDLC_SYNC_STATE_HUNTING, HDLC_SYNC_ACCUMULATOR_T::position, HDLC_SYNC_ACCUMULATOR_T::processed, and HDLC_SYNC_ACCUMULATOR_T::state.

Referenced by hdlc_sync_acc_poll().

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

◆ hdlc_sync_find_complete_candidate_short()

static bool hdlc_sync_find_complete_candidate_short ( const HDLC_SYNC_ACCUMULATOR_T accumulator,
size_t  scan_index,
size_t *  out_start_index,
uint8_t *  out_bit_pos,
bool *  out_shift_right 
)
static

Definition at line 248 of file hdlc_sync.c.

References HDLC_BITS_PER_BYTE, hdlc_sync_get_aligned_byte(), HDLC_SYNC_MIN_FRAME_SIZE_BYTES, HDLC_SYNC_SHORT_BUFFER_SEARCH_LIMIT, HDLC_SYNC_ACCUMULATOR_T::position, and HDLC_SYNC_ACCUMULATOR_T::sync_byte.

Referenced by hdlc_sync_step_hunting().

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

◆ hdlc_sync_find_opening_candidate()

static bool hdlc_sync_find_opening_candidate ( const HDLC_SYNC_ACCUMULATOR_T accumulator,
size_t  scan_index,
bool  allow_left_shift,
size_t *  out_start_index,
uint8_t *  out_bit_pos,
bool *  out_shift_right 
)
static

Definition at line 177 of file hdlc_sync.c.

References HDLC_BITS_PER_BYTE, hdlc_sync_get_aligned_byte(), HDLC_SYNC_ACCUMULATOR_T::position, and HDLC_SYNC_ACCUMULATOR_T::sync_byte.

Referenced by hdlc_sync_step_hunting().

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

◆ hdlc_sync_get_aligned_byte()

static bool hdlc_sync_get_aligned_byte ( const HDLC_SYNC_ACCUMULATOR_T accumulator,
size_t  raw_index,
uint8_t  bit_offset,
bool  shift_right,
uint8_t *  out_byte 
)
static

◆ hdlc_sync_reject_oversized_candidate()

static void hdlc_sync_reject_oversized_candidate ( HDLC_SYNC_ACCUMULATOR_T accumulator,
HDLC_FRAME_T out_frame,
size_t *  scan_index 
)
static

Definition at line 108 of file hdlc_sync.c.

References HDLC_SYNC_ACCUMULATOR_T::candidate_start, hdlc_sync_drop_prefix(), hdlc_sync_reset_hunting_state(), HDLC_FRAME_T::length, and HDLC_SYNC_ACCUMULATOR_T::position.

Referenced by hdlc_sync_step_synced(), and hdlc_sync_step_syncing().

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

◆ hdlc_sync_reset_hunting_state()

◆ hdlc_sync_step_hunting()

◆ hdlc_sync_step_synced()

◆ hdlc_sync_step_syncing()