tinyproto
tiny_fd_int.h
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 
29 #pragma once
30 
31 #ifndef DOXYGEN_SHOULD_SKIP_THIS
32 
33 #define TINY_FD_U_QUEUE_MAX_SIZE 4
34 
35 #ifdef __cplusplus
36 extern "C"
37 {
38 #endif
39 
40 #include <stdint.h>
41 #include "proto/hdlc/low_level/hdlc.h"
42 #include "proto/hdlc/low_level/hdlc_int.h"
43 #include "hal/tiny_types.h"
44 #include "tiny_fd_i_queue_control_int.h"
45 #include "tiny_fd_frames_queue_int.h"
46 
47 #define FD_PEER_BUF_SIZE() ( sizeof(tiny_fd_peer_info_t) )
48 
49 #define FD_MIN_BUF_SIZE(mtu, window) \
50  (sizeof(tiny_fd_data_t) + TINY_ALIGN_STRUCT_VALUE - 1 + \
51  HDLC_MIN_BUF_SIZE(mtu + sizeof(tiny_frame_header_t), HDLC_CRC_16) + \
52  ( 1 * FD_PEER_BUF_SIZE() ) + \
53  (sizeof(tiny_fd_frame_info_t *) + sizeof(tiny_fd_frame_info_t) + mtu \
54  - sizeof(((tiny_fd_frame_info_t *)0)->payload) ) * window + \
55  ( sizeof(tiny_fd_frame_info_t) + sizeof(tiny_fd_frame_info_t *) ) * TINY_FD_U_QUEUE_MAX_SIZE )
56 
57 #define FD_BUF_SIZE_EX(mtu, tx_window, crc, rx_window) \
58  (sizeof(tiny_fd_data_t) + TINY_ALIGN_STRUCT_VALUE - 1 + \
59  HDLC_BUF_SIZE_EX(mtu + sizeof(tiny_frame_header_t), crc, rx_window) + \
60  ( 1 * FD_PEER_BUF_SIZE() ) + \
61  (sizeof(tiny_fd_frame_info_t *) + sizeof(tiny_i_frame_info_t) + mtu \
62  - sizeof(((tiny_fd_frame_info_t *)0)->payload)) * tx_window + \
63  ( sizeof(tiny_fd_frame_info_t) + sizeof(tiny_fd_frame_info_t *) ) * TINY_FD_U_QUEUE_MAX_SIZE)
64 
65  typedef enum
66  {
67  TINY_FD_STATE_IDLE = 0,
68  TINY_FD_STATE_DISCONNECTED,
69  TINY_FD_STATE_CONNECTING,
70  TINY_FD_STATE_CONNECTED,
71  TINY_FD_STATE_DISCONNECTING,
72  } tiny_fd_state_t;
73 
74  typedef struct
75  {
76  tiny_frame_header_t header;
77  uint8_t data1;
78  uint8_t data2;
80 
81  typedef struct
82  {
84  tiny_fd_state_t state;
85  uint8_t addr; // Peer address
86 
87  uint8_t next_nr; // frame waiting to receive
88  uint8_t sent_nr; // frame index last sent back
89  uint8_t sent_reject; // If reject was already sent
90 
91  i_queue_control_t i_queue_control;
92 
93  uint32_t last_sent_i_ts; // last sent I-frame timestamp
94  uint32_t last_sent_frame_ts; // last sent keep alive timestamp
95  uint32_t last_received_frame_ts; // last keep alive timestamp
96  uint8_t ka_confirmed;
97  uint8_t retries; // Number of retries to perform before timeout takes place
98 
99  tiny_events_t events;
100 
102 
103  typedef struct tiny_fd_data_t
104  {
116  uint16_t send_timeout;
118  uint16_t retry_timeout;
120  uint16_t ka_timeout;
122  uint8_t retries;
126  uint8_t peers_count;
130  uint8_t addr;
132  uint8_t next_peer;
134  uint32_t last_marker_ts;
136  uint8_t mode;
140  void *user_data;
141  } tiny_fd_data_t;
142 
143  extern void __tiny_fd_log_frame(
144  tiny_fd_handle_t handle,
145  tiny_fd_frame_direction_t direction,
146  const uint8_t *data,
147  int len);
148 
149 #ifdef __cplusplus
150 }
151 #endif
152 
153 #endif
Definition: tiny_fd_int.h:74
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:149
hdlc_ll_handle_t _hdlc
hdlc information
Definition: tiny_fd_int.h:114
tiny_frames_queue_t frames
Information for frames being processed.
Definition: tiny_fd_int.h:124
on_frame_send_cb_t on_send_cb
Callback to process received frames.
Definition: tiny_fd_int.h:108
Definition: tiny_fd_frames_int.h:50
Definition: tiny_fd_i_queue_control_int.h:43
on_frame_read_cb_t on_read_cb
Callback to process received frames.
Definition: tiny_fd_int.h:106
void * user_data
user specific data
Definition: tiny_fd_int.h:140
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
uint8_t mode
HDLC mode;.
Definition: tiny_fd_int.h:136
uint16_t send_timeout
Timeout for operations with acknowledge.
Definition: tiny_fd_int.h:116
uint8_t next_peer
Next peer to process.
Definition: tiny_fd_int.h:132
tiny_fd_state_t state
state of hdlc protocol according to ISO & RFC
Definition: tiny_fd_int.h:84
Structure describes configuration of lowest HDLC level Initialize this structure by 0 before passing ...
Definition: hdlc_int.h:65
uint16_t retry_timeout
Timeout before retrying resend I-frames.
Definition: tiny_fd_int.h:118
on_connect_event_cb_t on_connect_event_cb
Callback to get connect/disconnect notification.
Definition: tiny_fd_int.h:110
tiny_fd_log_frame_cb_t log_frame_cb
Callback to log frames.
Definition: tiny_fd_int.h:112
This is Tiny HAL implementation for microcontrollers.
Definition: tiny_fd_frames_queue_int.h:51
uint8_t retries
Number of retries to perform before timeout takes place.
Definition: tiny_fd_int.h:122
tiny_fd_peer_info_t * peers
Information on all peers stations.
Definition: tiny_fd_int.h:128
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
uint16_t ka_timeout
Timeout before sending keep alive HDLC frame (RR)
Definition: tiny_fd_int.h:120
Definition: tiny_fd_int.h:81
uint8_t peers_count
Peers count supported by the primary device.
Definition: tiny_fd_int.h:126
Events group type used by Tiny Protocol implementation.
Definition: cpp_hal.h:60
uint8_t addr
Local address: 0x00 or for primary devices.
Definition: tiny_fd_int.h:130
tiny_events_t events
Global events for HDLC protocol.
Definition: tiny_fd_int.h:138
tiny_fd_frame_direction_t
Enumeration of frame directions used in Tiny Full Duplex protocol.
Definition: tiny_fd.h:123
uint32_t last_marker_ts
Last marker timestamp.
Definition: tiny_fd_int.h:134