Tiny protocol  0.11.0
Tiny communication protocol for microcontrollers
tiny_hdlc.h
1 /*
2  Copyright 2019-2020 (C) Alexey Dynda
3 
4  This file is part of Tiny Protocol Library.
5 
6  Protocol Library is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Protocol Library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with Protocol Library. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #pragma once
20 
21 #include "proto/hal/tiny_types.h"
22 #include <stdint.h>
23 #include <stdbool.h>
24 
25 #ifdef __cplusplus
26 extern "C"
27 {
28 #endif
29 
31 #define TINY_HDLC_FILL_BYTE 0xFF
32 
43 #ifdef CONFIG_ENABLE_FCS32
45  typedef uint32_t crc_t;
46 #else
47  typedef uint16_t crc_t;
48 #endif
49 
54 typedef enum
55 {
57  HDLC_CRC_8 = 8,
58  HDLC_CRC_16 = 16,
59  HDLC_CRC_32 = 32,
60  HDLC_CRC_OFF = 0xFF,
61 } hdlc_crc_t;
62 
68 typedef struct _hdlc_handle_t
69 {
81 
93  int (*on_frame_read)(void *user_data, void *data, int len);
94 
105  int (*on_frame_sent)(void *user_data, const void *data, int len);
106 
110  void *rx_buf;
111 
116 
123 
129 
131  void *user_data;
132 
133 #ifndef DOXYGEN_SHOULD_SKIP_THIS
134 
135  tiny_events_t events;
136  struct
137  {
138  uint8_t *data;
139  int (*state)( struct _hdlc_handle_t *handle, const uint8_t *data, int len );
140  uint8_t escape;
141  } rx;
142  struct
143  {
144  void *user_data;
145  uint8_t *out_buffer;
146  int out_buffer_len;
147  const uint8_t *origin_data;
148  const uint8_t *data;
149  int len;
150  crc_t crc;
151  uint8_t escape;
152  int (*state)( struct _hdlc_handle_t *handle );
153  } tx;
154 #endif
156 
157 //------------------------ GENERIC FUNCIONS ------------------------------
158 
166 hdlc_handle_t hdlc_init( hdlc_struct_t *hdlc_info );
167 
173 int hdlc_close( hdlc_handle_t handle );
174 
181 void hdlc_reset( hdlc_handle_t handle );
182 
183 //------------------------ RX FUNCIONS ------------------------------
184 
211 int hdlc_run_rx( hdlc_handle_t handle, const void *data, int len, int *error );
212 
225 int hdlc_run_rx_until_read( hdlc_handle_t handle, read_block_cb_t readcb, void *user_data, uint16_t timeout );
226 
234 void hdlc_set_rx_buffer( hdlc_handle_t handle, void *data, int size);
235 
236 //------------------------ TX FUNCIONS ------------------------------
237 
251 int hdlc_run_tx( hdlc_handle_t handle );
252 
263 int hdlc_get_tx_data( hdlc_handle_t handle, void *data, int len );
264 
306 int hdlc_send( hdlc_handle_t handle, const void *data, int len, uint32_t timeout );
307 
312 #ifdef __cplusplus
313 }
314 #endif
315 
hdlc_crc_t
Definition: tiny_hdlc.h:54
int rx_buf_size
Definition: tiny_hdlc.h:115
If default is specified HDLC will auto select CRC option.
Definition: tiny_hdlc.h:56
write_block_cb_t send_tx
Definition: tiny_hdlc.h:80
struct _hdlc_handle_t * hdlc_handle_t
hdlc handle
int(* on_frame_read)(void *user_data, void *data, int len)
Definition: tiny_hdlc.h:93
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:595
bool multithread_mode
Definition: tiny_hdlc.h:128
int hdlc_run_tx(hdlc_handle_t handle)
Definition: tiny_hdlc.c:275
hdlc_crc_t crc_type
Definition: tiny_hdlc.h:122
hdlc_handle_t hdlc_init(hdlc_struct_t *hdlc_info)
Definition: tiny_hdlc.c:59
void hdlc_reset(hdlc_handle_t handle)
Definition: tiny_hdlc.c:104
int(* read_block_cb_t)(void *pdata, void *buffer, int size)
Definition: tiny_types.h:141
CCITT-32.
Definition: tiny_hdlc.h:59
void * rx_buf
Definition: tiny_hdlc.h:110
Simple sum of all bytes in user payload.
Definition: tiny_hdlc.h:57
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:389
int(* on_frame_sent)(void *user_data, const void *data, int len)
Definition: tiny_hdlc.h:105
Definition: tiny_hdlc.h:68
int hdlc_close(hdlc_handle_t handle)
Definition: tiny_hdlc.c:90
Disable CRC field.
Definition: tiny_hdlc.h:60
int(* write_block_cb_t)(void *pdata, const void *buffer, int size)
Definition: tiny_types.h:130
void * user_data
Definition: tiny_hdlc.h:131
CCITT-16.
Definition: tiny_hdlc.h:58
void hdlc_set_rx_buffer(hdlc_handle_t handle, void *data, int size)
Definition: tiny_hdlc.c:589
int hdlc_run_rx(hdlc_handle_t handle, const void *data, int len, int *error)
Definition: tiny_hdlc.c:561
int hdlc_get_tx_data(hdlc_handle_t handle, void *data, int len)
Definition: tiny_hdlc.c:313