Tiny protocol  0.9.3
Tiny communication protocol for microcontrollers
tiny_hdlc.h
1 #pragma once
2 
3 #include "proto/hal/tiny_types.h"
4 #include <stdint.h>
5 #include <stdbool.h>
6 
7 #ifdef __cplusplus
8 extern "C"
9 {
10 #endif
11 
22 #ifdef CONFIG_ENABLE_FCS32
24  typedef uint32_t crc_t;
25 #else
26  typedef uint16_t crc_t;
27 #endif
28 
33 typedef enum
34 {
36  HDLC_CRC_8 = 8,
37  HDLC_CRC_16 = 16,
38  HDLC_CRC_32 = 32,
39  HDLC_CRC_OFF = 0xFF,
40 } hdlc_crc_t;
41 
47 typedef struct _hdlc_handle_t
48 {
60 
72  int (*on_frame_read)(void *user_data, void *data, int len);
73 
84  int (*on_frame_sent)(void *user_data, const void *data, int len);
85 
89  void *rx_buf;
90 
95 
102 
108 
110  void *user_data;
111 
112 #ifndef DOXYGEN_SHOULD_SKIP_THIS
113 
114  tiny_mutex_t send_mutex;
115  tiny_events_t events;
116  struct
117  {
118  uint8_t *data;
119  int (*state)( struct _hdlc_handle_t *handle, const uint8_t *data, int len );
120  uint8_t escape;
121  } rx;
122  struct
123  {
124  const uint8_t *origin_data;
125  const uint8_t *data;
126  int len;
127  crc_t crc;
128  uint8_t escape;
129  int (*state)( struct _hdlc_handle_t *handle );
130  } tx;
131 #endif
133 
134 //------------------------ GENERIC FUNCIONS ------------------------------
135 
143 hdlc_handle_t hdlc_init( hdlc_struct_t *hdlc_info );
144 
150 int hdlc_close( hdlc_handle_t handle );
151 
158 void hdlc_reset( hdlc_handle_t handle );
159 
160 //------------------------ RX FUNCIONS ------------------------------
161 
181 int hdlc_run_rx( hdlc_handle_t handle, const void *data, int len, int *error );
182 
195 int hdlc_run_rx_until_read( hdlc_handle_t handle, read_block_cb_t readcb, void *user_data, uint16_t timeout );
196 
204 void hdlc_set_rx_buffer( hdlc_handle_t handle, void *data, int size);
205 
206 //------------------------ TX FUNCIONS ------------------------------
207 
221 int hdlc_run_tx( hdlc_handle_t handle );
222 
264 int hdlc_send( hdlc_handle_t handle, const void *data, int len, uint32_t timeout );
265 
270 #ifdef __cplusplus
271 }
272 #endif
273 
hdlc_crc_t
Definition: tiny_hdlc.h:33
int rx_buf_size
Definition: tiny_hdlc.h:94
If default is specified HDLC will auto select CRC option.
Definition: tiny_hdlc.h:35
write_block_cb_t send_tx
Definition: tiny_hdlc.h:59
struct _hdlc_handle_t * hdlc_handle_t
hdlc handle
int(* on_frame_read)(void *user_data, void *data, int len)
Definition: tiny_hdlc.h:72
int hdlc_run_rx_until_read(hdlc_handle_t handle, read_block_cb_t readcb, void *user_data, uint16_t timeout)
Definition: tiny_hdlc.c:522
bool multithread_mode
Definition: tiny_hdlc.h:107
int hdlc_run_tx(hdlc_handle_t handle)
Definition: tiny_hdlc.c:248
hdlc_crc_t crc_type
Definition: tiny_hdlc.h:101
hdlc_handle_t hdlc_init(hdlc_struct_t *hdlc_info)
Definition: tiny_hdlc.c:38
void hdlc_reset(hdlc_handle_t handle)
Definition: tiny_hdlc.c:79
int(* read_block_cb_t)(void *pdata, void *buffer, int size)
Definition: tiny_types.h:139
CCITT-32.
Definition: tiny_hdlc.h:38
void * rx_buf
Definition: tiny_hdlc.h:89
Simple sum of all bytes in user payload.
Definition: tiny_hdlc.h:36
struct _hdlc_handle_t hdlc_struct_t
Tiny protocol Types.
int hdlc_send(hdlc_handle_t handle, const void *data, int len, uint32_t timeout)
Definition: tiny_hdlc.c:321
int(* on_frame_sent)(void *user_data, const void *data, int len)
Definition: tiny_hdlc.h:84
Definition: tiny_hdlc.h:47
int hdlc_close(hdlc_handle_t handle)
Definition: tiny_hdlc.c:64
Disable CRC field.
Definition: tiny_hdlc.h:39
int(* write_block_cb_t)(void *pdata, const void *buffer, int size)
Definition: tiny_types.h:128
void * user_data
Definition: tiny_hdlc.h:110
CCITT-16.
Definition: tiny_hdlc.h:37
void hdlc_set_rx_buffer(hdlc_handle_t handle, void *data, int size)
Definition: tiny_hdlc.c:516
int hdlc_run_rx(hdlc_handle_t handle, const void *data, int len, int *error)
Definition: tiny_hdlc.c:490