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 
38 #pragma once
39 
40 #ifdef __cplusplus
41 extern "C"
42 {
43 #endif
44 
45 #include <stdint.h>
46 #include "proto/crc/tiny_crc.h"
47 #include "hal/tiny_types.h"
48 
57  #define TINY_FD_PRIMARY_ADDR (0)
58 
59  enum
60  {
66 
74 
79  };
80 
84  struct tiny_fd_data_t;
85 
91 
95  typedef enum
96  {
101 
107  typedef enum
108  {
111 
119 
123  typedef enum
124  {
128 
149  typedef void (*tiny_fd_log_frame_cb_t)(void *udata,
150  tiny_fd_handle_t handle,
151  tiny_fd_frame_direction_t direction,
152  tiny_fd_frame_type_t frame_type,
153  tiny_fd_frame_subtype_t frame_subtype,
154  uint8_t ns,
155  uint8_t nr,
156  const uint8_t *data,
157  int len);
158 
162  typedef struct tiny_fd_init_t_
163  {
165  void *pdata;
166 
169 
172 
177  void *buffer;
178 
180  uint16_t buffer_size;
181 
186  uint16_t send_timeout;
187 
192  uint16_t retry_timeout;
193 
197  uint8_t retries;
198 
204  hdlc_crc_t crc_type;
205 
212  uint8_t window_frames;
213 
218  int mtu;
219 
225 
232 
240  uint8_t addr;
241 
249  uint8_t peers_count;
250 
254  uint8_t mode;
255 
256  } tiny_fd_init_t;
257 
269  extern int tiny_fd_init(tiny_fd_handle_t *handle, tiny_fd_init_t *init);
270 
281  extern int tiny_fd_get_status(tiny_fd_handle_t handle);
282 
293  extern int tiny_fd_disconnect(tiny_fd_handle_t handle);
294 
302  extern void tiny_fd_close(tiny_fd_handle_t handle);
303 
315  extern int tiny_fd_get_tx_data(tiny_fd_handle_t handle, void *data, int len, uint32_t timeout);
316 
330  extern int tiny_fd_run_tx(tiny_fd_handle_t handle, write_block_cb_t write_func);
331 
345  extern int tiny_fd_on_rx_data(tiny_fd_handle_t handle, const void *data, int len);
346 
361  extern int tiny_fd_run_rx(tiny_fd_handle_t handle, read_block_cb_t read_func);
362 
391  extern int tiny_fd_send_packet_to(tiny_fd_handle_t handle, uint8_t address, const void *buf, int len, uint32_t timeout);
392 
401  extern int tiny_fd_buffer_size_by_mtu(int mtu, int window);
402 
412  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);
413 
423  extern int tiny_fd_get_mtu(tiny_fd_handle_t handle);
424 
443  extern int tiny_fd_send_to(tiny_fd_handle_t handle, uint8_t address, const void *buf, int len, uint32_t timeout);
444 
451  extern void tiny_fd_set_ka_timeout(tiny_fd_handle_t handle, uint32_t keep_alive);
452 
466  extern int tiny_fd_register_peer(tiny_fd_handle_t handle, uint8_t address);
467 
480  extern int tiny_fd_send(tiny_fd_handle_t handle, const void *buf, int len, uint32_t timeout);
481 
494  extern int tiny_fd_send_packet(tiny_fd_handle_t handle, const void *buf, int len, uint32_t timeout);
495 
500 #ifdef __cplusplus
501 }
502 #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:171
Definition: tiny_fd_int.h:114
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:149
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:168
U-frame.
Definition: tiny_fd.h:99
S-frame.
Definition: tiny_fd.h:98
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:625
int tiny_fd_buffer_size_by_mtu(int mtu, int window)
Returns minimum required buffer size for specified parameters.
Definition: tiny_fd.c:838
U-frame subtype DISC.
Definition: tiny_fd.h:117
Frame is outgoing.
Definition: tiny_fd.h:126
U-frame subtype SNRM.
Definition: tiny_fd.h:116
int tiny_fd_register_peer(tiny_fd_handle_t handle, uint8_t address)
Registers remote peer with specified address.
Definition: tiny_fd.c:960
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:218
int tiny_fd_disconnect(tiny_fd_handle_t handle)
Sends DISC command to remote side.
Definition: tiny_fd.c:933
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:90
uint16_t buffer_size
maximum input buffer size, see tiny_fd_buffer_size_by_mtu()
Definition: tiny_fd.h:180
Frame is incoming.
Definition: tiny_fd.h:125
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:831
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:224
uint8_t window_frames
Number of frames in window, which confirmation may be deferred for.
Definition: tiny_fd.h:212
tiny_fd_log_frame_cb_t log_frame_cb
Callback to log frames.
Definition: tiny_fd.h:231
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:752
uint16_t retry_timeout
timeout for retry operation.
Definition: tiny_fd.h:192
int(* read_block_cb_t)(void *pdata, void *buffer, int size)
The function reads data from communication channel.
Definition: tiny_types.h:185
uint16_t send_timeout
timeout.
Definition: tiny_fd.h:186
int tiny_fd_get_mtu(tiny_fd_handle_t handle)
returns max packet size in bytes.
Definition: tiny_fd.c:879
Normal response mode allows the secondary-to-primary link to be shared without contention, because it has the primary give the secondaries permission to transmit one at a time.
Definition: tiny_fd.h:73
uint8_t peers_count
Maximum number of peers supported by the local station.
Definition: tiny_fd.h:249
This is Tiny HAL implementation for microcontrollers.
U-frame subtype FRMR.
Definition: tiny_fd.h:113
hdlc_crc_t crc_type
crc field type to use on hdlc level.
Definition: tiny_fd.h:204
uint8_t mode
Communication link mode.
Definition: tiny_fd.h:254
I-frame.
Definition: tiny_fd.h:97
void * buffer
buffer to store data during full-duplex protocol operating.
Definition: tiny_fd.h:177
Asynchronous balanced mode - the default mode of HDLC protocol.
Definition: tiny_fd.h:65
U-frame subtype SABM.
Definition: tiny_fd.h:115
U-frame subtype UA.
Definition: tiny_fd.h:112
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:886
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:78
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:728
void tiny_fd_close(tiny_fd_handle_t handle)
stops Tiny Full Duplex state machine
Definition: tiny_fd.c:428
uint8_t addr
Local station address.
Definition: tiny_fd.h:240
void * pdata
user data for block read/write functions
Definition: tiny_fd.h:165
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:304
S-frame subtype RR.
Definition: tiny_fd.h:109
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:872
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:441
tiny_fd_frame_subtype_t
Enumeration of frame subtypes used in Tiny Full Duplex protocol.
Definition: tiny_fd.h:107
U-frame subtype RSET.
Definition: tiny_fd.h:114
tiny_fd_frame_type_t
Enumeration of frame types used in Tiny Full Duplex protocol.
Definition: tiny_fd.h:95
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:845
uint8_t retries
number retries to perform before timeout takes place
Definition: tiny_fd.h:197
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:460
This structure is used for initialization of Tiny Full Duplex protocol.
Definition: tiny_fd.h:162
S-frame subtype REJ.
Definition: tiny_fd.h:110
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:907
tiny_fd_frame_direction_t
Enumeration of frame directions used in Tiny Full Duplex protocol.
Definition: tiny_fd.h:123
int tiny_fd_get_status(tiny_fd_handle_t handle)
Returns status of the connection.
Definition: tiny_fd.c:914