tinyproto
tiny_fd.h
Go to the documentation of this file.
1 /*
2  Copyright 2019-2025 (C) Alexey Dynda
3 
4  This file is part of Tiny Protocol Library.
5 
6  GNU General Public License Usage
7 
8  Protocol Library is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  Protocol Library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License
19  along with Protocol Library. If not, see <http://www.gnu.org/licenses/>.
20 
21  Commercial License Usage
22 
23  Licensees holding valid commercial Tiny Protocol licenses may use this file in
24  accordance with the commercial license agreement provided in accordance with
25  the terms contained in a written agreement between you and Alexey Dynda.
26  For further information contact via email on github account.
27 */
28 
43 #pragma once
44 
45 #ifdef __cplusplus
46 extern "C"
47 {
48 #endif
49 
50 #include <stdint.h>
51 #include "proto/crc/tiny_crc.h"
52 #include "hal/tiny_types.h"
53 
80  #define TINY_FD_PRIMARY_ADDR (0)
81 
82  enum
83  {
89 
98 
103  };
104 
108  struct tiny_fd_data_t;
109 
115 
119  typedef enum
120  {
125 
131  typedef enum
132  {
137 
147 
151  typedef enum
152  {
156 
177  typedef void (*tiny_fd_log_frame_cb_t)(void *udata,
178  tiny_fd_handle_t handle,
179  tiny_fd_frame_direction_t direction,
180  tiny_fd_frame_type_t frame_type,
181  tiny_fd_frame_subtype_t frame_subtype,
182  uint8_t ns,
183  uint8_t nr,
184  const uint8_t *data,
185  int len);
186 
194  typedef struct tiny_fd_init_t_
195  {
197  void *pdata;
198 
201 
204 
209  void *buffer;
210 
212  uint16_t buffer_size;
213 
218  uint16_t send_timeout;
219 
224  uint16_t retry_timeout;
225 
229  uint8_t retries;
230 
236  hdlc_crc_t crc_type;
237 
244  uint8_t window_frames;
245 
250  int mtu;
251 
257 
264 
272  uint8_t addr;
273 
281  uint8_t peers_count;
282 
286  uint8_t mode;
287 
294 
295  } tiny_fd_init_t;
296 
308  extern int tiny_fd_init(tiny_fd_handle_t *handle, tiny_fd_init_t *init);
309 
320  extern int tiny_fd_get_status(tiny_fd_handle_t handle);
321 
332  extern int tiny_fd_disconnect(tiny_fd_handle_t handle);
333 
341  extern void tiny_fd_close(tiny_fd_handle_t handle);
342 
354  extern int tiny_fd_get_tx_data(tiny_fd_handle_t handle, void *data, int len, uint32_t timeout);
355 
369  extern int tiny_fd_run_tx(tiny_fd_handle_t handle, write_block_cb_t write_func);
370 
384  extern int tiny_fd_on_rx_data(tiny_fd_handle_t handle, const void *data, int len);
385 
400  extern int tiny_fd_run_rx(tiny_fd_handle_t handle, read_block_cb_t read_func);
401 
430  extern int tiny_fd_send_packet_to(tiny_fd_handle_t handle, uint8_t address, const void *buf, int len, uint32_t timeout);
431 
440  extern int tiny_fd_buffer_size_by_mtu(int mtu, int window);
441 
451  extern int tiny_fd_buffer_size_by_mtu_ex(uint8_t peers_count, int mtu, int tx_window, hdlc_crc_t crc_type, int rx_window);
452 
462  extern int tiny_fd_get_mtu(tiny_fd_handle_t handle);
463 
482  extern int tiny_fd_send_to(tiny_fd_handle_t handle, uint8_t address, const void *buf, int len, uint32_t timeout);
483 
490  extern void tiny_fd_set_ka_timeout(tiny_fd_handle_t handle, uint32_t keep_alive);
491 
505  extern int tiny_fd_register_peer(tiny_fd_handle_t handle, uint8_t address);
506 
519  extern int tiny_fd_send(tiny_fd_handle_t handle, const void *buf, int len, uint32_t timeout);
520 
533  extern int tiny_fd_send_packet(tiny_fd_handle_t handle, const void *buf, int len, uint32_t timeout);
534 
552  extern int tiny_fd_send_ui_packet_to(tiny_fd_handle_t handle, uint8_t address, const void *buf, int len);
553 
566  extern int tiny_fd_send_ui_packet(tiny_fd_handle_t handle, const void *buf, int len);
567 
572 #ifdef __cplusplus
573 }
574 #endif
on_frame_send_cb_t on_send_cb
Callback to get notification of sent frames. Callback is called from tiny_fd_run_tx() context...
Definition: tiny_fd.h:203
Definition: tiny_fd_int.h:103
void(* tiny_fd_log_frame_cb_t)(void *udata, tiny_fd_handle_t handle, tiny_fd_frame_direction_t direction, tiny_fd_frame_type_t frame_type, tiny_fd_frame_subtype_t frame_subtype, uint8_t ns, uint8_t nr, const uint8_t *data, int len)
tiny_fd_log_frame_cb_t is a callback function, which is called every time frame is sent or received...
Definition: tiny_fd.h:177
on_frame_read_cb_t on_read_cb
callback function to process incoming frames. Callback is called from tiny_fd_run_rx() context...
Definition: tiny_fd.h:200
U-frame subtype DM.
Definition: tiny_fd.h:139
U-frame.
Definition: tiny_fd.h:123
S-frame.
Definition: tiny_fd.h:122
int tiny_fd_get_tx_data(tiny_fd_handle_t handle, void *data, int len, uint32_t timeout)
runs tx processing to fill specified buffer with data.
Definition: tiny_fd.c:665
int tiny_fd_buffer_size_by_mtu(int mtu, int window)
Returns minimum required buffer size for specified parameters.
Definition: tiny_fd.c:921
U-frame subtype DISC.
Definition: tiny_fd.h:144
Frame is outgoing.
Definition: tiny_fd.h:154
U-frame subtype SNRM.
Definition: tiny_fd.h:143
int tiny_fd_register_peer(tiny_fd_handle_t handle, uint8_t address)
Registers remote peer with specified address.
Definition: tiny_fd.c:1043
struct tiny_fd_init_t_ tiny_fd_init_t
This structure is used for initialization of Tiny Full Duplex protocol.
int mtu
Maximum transmission unit in bytes.
Definition: tiny_fd.h:250
int tiny_fd_disconnect(tiny_fd_handle_t handle)
Sends DISC command to remote side.
Definition: tiny_fd.c:1016
void(* on_connect_event_cb_t)(void *udata, uint8_t address, bool connected)
on_connect_event_cb_t is a callback function, which is called every time connection is established or...
Definition: tiny_types.h:232
void(* on_frame_send_cb_t)(void *udata, uint8_t address, const uint8_t *pdata, int size)
on_frame_send_cb_t is a callback function, which is called every time new frame is sent...
Definition: tiny_types.h:223
struct tiny_fd_data_t * tiny_fd_handle_t
This handle points to service data, required for full-duplex functioning.
Definition: tiny_fd.h:114
uint16_t buffer_size
maximum input buffer size, see tiny_fd_buffer_size_by_mtu()
Definition: tiny_fd.h:212
Frame is incoming.
Definition: tiny_fd.h:153
int tiny_fd_send_packet(tiny_fd_handle_t handle, const void *buf, int len, uint32_t timeout)
Sends packet to primary station.
Definition: tiny_fd.c:869
on_connect_event_cb_t on_connect_event_cb
Callback to get the notification when connect or disconnect event takes place.
Definition: tiny_fd.h:256
uint8_t window_frames
Number of frames in window, which confirmation may be deferred for.
Definition: tiny_fd.h:244
tiny_fd_log_frame_cb_t log_frame_cb
Callback to log frames.
Definition: tiny_fd.h:263
int tiny_fd_send_packet_to(tiny_fd_handle_t handle, uint8_t address, const void *buf, int len, uint32_t timeout)
Sends userdata over full-duplex protocol.
Definition: tiny_fd.c:792
uint16_t retry_timeout
timeout for retry operation.
Definition: tiny_fd.h:224
int(* read_block_cb_t)(void *pdata, void *buffer, int size)
The function reads data from communication channel.
Definition: tiny_types.h:185
on_frame_read_cb_t on_read_ui_cb
Callback to process incoming UI (Unnumbered Information) frames.
Definition: tiny_fd.h:293
uint16_t send_timeout
timeout.
Definition: tiny_fd.h:218
int tiny_fd_get_mtu(tiny_fd_handle_t handle)
returns max packet size in bytes.
Definition: tiny_fd.c:962
Normal Response Mode allows the primary station to control communication with one or more secondary s...
Definition: tiny_fd.h:97
uint8_t peers_count
Maximum number of peers supported by the local station.
Definition: tiny_fd.h:281
This is Tiny HAL implementation for microcontrollers.
U-frame subtype FRMR.
Definition: tiny_fd.h:140
hdlc_crc_t crc_type
crc field type to use on hdlc level.
Definition: tiny_fd.h:236
uint8_t mode
Communication link mode.
Definition: tiny_fd.h:286
I-frame.
Definition: tiny_fd.h:121
void * buffer
buffer to store data during full-duplex protocol operating.
Definition: tiny_fd.h:209
Asynchronous balanced mode - the default mode of HDLC protocol.
Definition: tiny_fd.h:88
U-frame subtype UI (Unnumbered Information)
Definition: tiny_fd.h:145
U-frame subtype SABM.
Definition: tiny_fd.h:142
U-frame subtype UA.
Definition: tiny_fd.h:138
S-frame subtype RNR.
Definition: tiny_fd.h:134
int tiny_fd_send_to(tiny_fd_handle_t handle, uint8_t address, const void *buf, int len, uint32_t timeout)
Sends userdata over full-duplex protocol.
Definition: tiny_fd.c:969
void(* on_frame_read_cb_t)(void *udata, uint8_t address, uint8_t *pdata, int size)
on_frame_read_cb_t is a callback function, which is called every time new frame is received...
Definition: tiny_types.h:213
int(* write_block_cb_t)(void *pdata, const void *buffer, int size)
The function writes data to communication channel port.
Definition: tiny_types.h:174
Asynchronous response mode.
Definition: tiny_fd.h:102
int tiny_fd_run_tx(tiny_fd_handle_t handle, write_block_cb_t write_func)
sends tx data to the communication channel via user callback write_func().
Definition: tiny_fd.c:768
void tiny_fd_close(tiny_fd_handle_t handle)
stops Tiny Full Duplex state machine
Definition: tiny_fd.c:418
uint8_t addr
Local station address.
Definition: tiny_fd.h:272
void * pdata
user data for block read/write functions
Definition: tiny_fd.h:197
int tiny_fd_init(tiny_fd_handle_t *handle, tiny_fd_init_t *init)
Initialized communication for Tiny Full Duplex protocol.
Definition: tiny_fd.c:293
int tiny_fd_send_ui_packet_to(tiny_fd_handle_t handle, uint8_t address, const void *buf, int len)
Sends UI (Unnumbered Information) frame over full-duplex protocol.
Definition: tiny_fd.c:876
S-frame subtype RR.
Definition: tiny_fd.h:133
int tiny_fd_send_ui_packet(tiny_fd_handle_t handle, const void *buf, int len)
Sends UI (Unnumbered Information) frame to primary station.
Definition: tiny_fd.c:914
void tiny_fd_set_ka_timeout(tiny_fd_handle_t handle, uint32_t keep_alive)
Sets keep alive timeout in milliseconds.
Definition: tiny_fd.c:955
int tiny_fd_on_rx_data(tiny_fd_handle_t handle, const void *data, int len)
runs rx bytes processing for specified buffer.
Definition: tiny_fd.c:431
tiny_fd_frame_subtype_t
Enumeration of frame subtypes used in Tiny Full Duplex protocol.
Definition: tiny_fd.h:131
U-frame subtype RSET.
Definition: tiny_fd.h:141
tiny_fd_frame_type_t
Enumeration of frame types used in Tiny Full Duplex protocol.
Definition: tiny_fd.h:119
int tiny_fd_buffer_size_by_mtu_ex(uint8_t peers_count, int mtu, int tx_window, hdlc_crc_t crc_type, int rx_window)
Returns minimum required buffer size for specified parameters.
Definition: tiny_fd.c:928
uint8_t retries
number retries to perform before timeout takes place
Definition: tiny_fd.h:229
int tiny_fd_run_rx(tiny_fd_handle_t handle, read_block_cb_t read_func)
reads rx data from the communication channel via user callback read_func()
Definition: tiny_fd.c:450
This structure is used for initialization of Tiny Full Duplex protocol.
Definition: tiny_fd.h:194
S-frame subtype REJ.
Definition: tiny_fd.h:135
int tiny_fd_send(tiny_fd_handle_t handle, const void *buf, int len, uint32_t timeout)
Sends userdata over full-duplex protocol to primary station.
Definition: tiny_fd.c:990
S-frame subtype SREJ (Selective Reject)
Definition: tiny_fd.h:136
tiny_fd_frame_direction_t
Enumeration of frame directions used in Tiny Full Duplex protocol.
Definition: tiny_fd.h:151
int tiny_fd_get_status(tiny_fd_handle_t handle)
Returns status of the connection.
Definition: tiny_fd.c:997