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_frames_queue_int.h"
45 
46 #define FD_PEER_BUF_SIZE() ( sizeof(tiny_fd_peer_info_t) )
47 
48 #define FD_MIN_BUF_SIZE(mtu, window) \
49  (sizeof(tiny_fd_data_t) + TINY_ALIGN_STRUCT_VALUE - 1 + \
50  HDLC_MIN_BUF_SIZE(mtu + sizeof(tiny_frame_header_t), HDLC_CRC_16) + \
51  ( 1 * FD_PEER_BUF_SIZE() ) + \
52  (sizeof(tiny_fd_frame_info_t *) + sizeof(tiny_fd_frame_info_t) + mtu \
53  - sizeof(((tiny_fd_frame_info_t *)0)->payload) ) * window + \
54  ( sizeof(tiny_fd_frame_info_t) + sizeof(tiny_fd_frame_info_t *) ) * TINY_FD_U_QUEUE_MAX_SIZE )
55 
56 #define FD_BUF_SIZE_EX(mtu, tx_window, crc, rx_window) \
57  (sizeof(tiny_fd_data_t) + TINY_ALIGN_STRUCT_VALUE - 1 + \
58  HDLC_BUF_SIZE_EX(mtu + sizeof(tiny_frame_header_t), crc, rx_window) + \
59  ( 1 * FD_PEER_BUF_SIZE() ) + \
60  (sizeof(tiny_fd_frame_info_t *) + sizeof(tiny_i_frame_info_t) + mtu \
61  - sizeof(((tiny_fd_frame_info_t *)0)->payload)) * tx_window + \
62  ( sizeof(tiny_fd_frame_info_t) + sizeof(tiny_fd_frame_info_t *) ) * TINY_FD_U_QUEUE_MAX_SIZE)
63 
64  typedef enum
65  {
66  TINY_FD_STATE_IDLE = 0,
67  TINY_FD_STATE_DISCONNECTED,
68  TINY_FD_STATE_CONNECTING,
69  TINY_FD_STATE_CONNECTED,
70  TINY_FD_STATE_DISCONNECTING,
71  } tiny_fd_state_t;
72 
73  typedef struct
74  {
75  tiny_frame_header_t header;
76  uint8_t data1;
77  uint8_t data2;
79 
80  typedef struct
81  {
83  tiny_fd_state_t state;
84  uint8_t addr; // Peer address
85 
86  uint8_t next_nr; // frame waiting to receive
87  uint8_t sent_nr; // frame index last sent back
88  uint8_t sent_reject; // If reject was already sent
89  uint8_t next_ns; // next frame to be sent
90  uint8_t confirm_ns; // next frame to be confirmed
91  uint8_t last_ns; // next free frame in cycle buffer
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
104  {
110  tiny_mutex_t mutex;
111 
113 
114  typedef struct tiny_fd_data_t
115  {
127  uint16_t send_timeout;
129  uint16_t retry_timeout;
131  uint16_t ka_timeout;
133  uint8_t retries;
137  uint8_t peers_count;
141  uint8_t addr;
143  uint8_t next_peer;
145  uint32_t last_marker_ts;
147  uint8_t mode;
151  void *user_data;
152  } tiny_fd_data_t;
153 
154  extern void __tiny_fd_log_frame(
155  tiny_fd_handle_t handle,
156  tiny_fd_frame_direction_t direction,
157  const uint8_t *data,
158  int len);
159 
160 #ifdef __cplusplus
161 }
162 #endif
163 
164 #endif
Definition: tiny_fd_int.h:73
tiny_fd_queue_t i_queue
Storage for all I- frames.
Definition: tiny_fd_int.h:106
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
hdlc_ll_handle_t _hdlc
hdlc information
Definition: tiny_fd_int.h:125
tiny_frames_queue_t frames
Information for frames being processed.
Definition: tiny_fd_int.h:135
on_frame_send_cb_t on_send_cb
Callback to process received frames.
Definition: tiny_fd_int.h:119
Definition: tiny_fd_frames_int.h:50
on_frame_read_cb_t on_read_cb
Callback to process received frames.
Definition: tiny_fd_int.h:117
void * user_data
user specific data
Definition: tiny_fd_int.h:151
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:147
uint16_t send_timeout
Timeout for operations with acknowledge.
Definition: tiny_fd_int.h:127
uint8_t next_peer
Next peer to process.
Definition: tiny_fd_int.h:143
tiny_fd_state_t state
state of hdlc protocol according to ISO & RFC
Definition: tiny_fd_int.h:83
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:129
on_connect_event_cb_t on_connect_event_cb
Callback to get connect/disconnect notification.
Definition: tiny_fd_int.h:121
tiny_fd_log_frame_cb_t log_frame_cb
Callback to log frames.
Definition: tiny_fd_int.h:123
This is Tiny HAL implementation for microcontrollers.
Definition: tiny_fd_int.h:103
uint8_t retries
Number of retries to perform before timeout takes place.
Definition: tiny_fd_int.h:133
tiny_fd_peer_info_t * peers
Information on all peers stations.
Definition: tiny_fd_int.h:139
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:131
Definition: tiny_fd_int.h:80
Definition: tiny_fd_frames_queue_int.h:43
tiny_fd_queue_t s_queue
Storage for all S- and U- service frames.
Definition: tiny_fd_int.h:108
uint8_t peers_count
Peers count supported by the primary device.
Definition: tiny_fd_int.h:137
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:141
tiny_mutex_t mutex
Global mutex.
Definition: tiny_fd_int.h:110
tiny_events_t events
Global events for HDLC protocol.
Definition: tiny_fd_int.h:149
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:145