Tiny protocol  0.9.3
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 
40 #ifdef CONFIG_ENABLE_FCS32
42  typedef uint32_t crc_t;
43 #else
44  typedef uint16_t crc_t;
45 #endif
46 
51 typedef enum
52 {
54  HDLC_CRC_8 = 8,
55  HDLC_CRC_16 = 16,
56  HDLC_CRC_32 = 32,
57  HDLC_CRC_OFF = 0xFF,
58 } hdlc_crc_t;
59 
65 typedef struct _hdlc_handle_t
66 {
78 
90  int (*on_frame_read)(void *user_data, void *data, int len);
91 
102  int (*on_frame_sent)(void *user_data, const void *data, int len);
103 
107  void *rx_buf;
108 
113 
120 
126 
128  void *user_data;
129 
130 #ifndef DOXYGEN_SHOULD_SKIP_THIS
131 
132  tiny_events_t events;
133  struct
134  {
135  uint8_t *data;
136  int (*state)( struct _hdlc_handle_t *handle, const uint8_t *data, int len );
137  uint8_t escape;
138  } rx;
139  struct
140  {
141  const uint8_t *origin_data;
142  const uint8_t *data;
143  int len;
144  crc_t crc;
145  uint8_t escape;
146  int (*state)( struct _hdlc_handle_t *handle );
147  } tx;
148 #endif
150 
151 //------------------------ GENERIC FUNCIONS ------------------------------
152 
160 hdlc_handle_t hdlc_init( hdlc_struct_t *hdlc_info );
161 
167 int hdlc_close( hdlc_handle_t handle );
168 
175 void hdlc_reset( hdlc_handle_t handle );
176 
177 //------------------------ RX FUNCIONS ------------------------------
178 
199 int hdlc_run_rx( hdlc_handle_t handle, const void *data, int len, int *error );
200 
213 int hdlc_run_rx_until_read( hdlc_handle_t handle, read_block_cb_t readcb, void *user_data, uint16_t timeout );
214 
222 void hdlc_set_rx_buffer( hdlc_handle_t handle, void *data, int size);
223 
224 //------------------------ TX FUNCIONS ------------------------------
225 
239 int hdlc_run_tx( hdlc_handle_t handle );
240 
282 int hdlc_send( hdlc_handle_t handle, const void *data, int len, uint32_t timeout );
283 
288 #ifdef __cplusplus
289 }
290 #endif
291 
hdlc_crc_t
Definition: tiny_hdlc.h:51
int rx_buf_size
Definition: tiny_hdlc.h:112
If default is specified HDLC will auto select CRC option.
Definition: tiny_hdlc.h:53
write_block_cb_t send_tx
Definition: tiny_hdlc.h:77
struct _hdlc_handle_t * hdlc_handle_t
hdlc handle
int(* on_frame_read)(void *user_data, void *data, int len)
Definition: tiny_hdlc.h:90
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:535
bool multithread_mode
Definition: tiny_hdlc.h:125
int hdlc_run_tx(hdlc_handle_t handle)
Definition: tiny_hdlc.c:265
hdlc_crc_t crc_type
Definition: tiny_hdlc.h:119
hdlc_handle_t hdlc_init(hdlc_struct_t *hdlc_info)
Definition: tiny_hdlc.c:57
void hdlc_reset(hdlc_handle_t handle)
Definition: tiny_hdlc.c:96
int(* read_block_cb_t)(void *pdata, void *buffer, int size)
Definition: tiny_types.h:139
CCITT-32.
Definition: tiny_hdlc.h:56
void * rx_buf
Definition: tiny_hdlc.h:107
Simple sum of all bytes in user payload.
Definition: tiny_hdlc.h:54
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:338
int(* on_frame_sent)(void *user_data, const void *data, int len)
Definition: tiny_hdlc.h:102
Definition: tiny_hdlc.h:65
int hdlc_close(hdlc_handle_t handle)
Definition: tiny_hdlc.c:82
Disable CRC field.
Definition: tiny_hdlc.h:57
int(* write_block_cb_t)(void *pdata, const void *buffer, int size)
Definition: tiny_types.h:128
void * user_data
Definition: tiny_hdlc.h:128
CCITT-16.
Definition: tiny_hdlc.h:55
void hdlc_set_rx_buffer(hdlc_handle_t handle, void *data, int size)
Definition: tiny_hdlc.c:529
int hdlc_run_rx(hdlc_handle_t handle, const void *data, int len, int *error)
Definition: tiny_hdlc.c:505