Tiny protocol  0.9.3
Tiny communication protocol for microcontrollers
tiny_fd_int.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 
20 #pragma once
21 
22 #ifndef DOXYGEN_SHOULD_SKIP_THIS
23 
24 #define TINY_FD_U_QUEUE_MAX_SIZE 4
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #include <stdint.h>
31 #include "proto/hdlc/tiny_hdlc.h"
32 #include "proto/hal/tiny_types.h"
33 
34 #define FD_MIN_BUF_SIZE(mtu,window) (sizeof(tiny_fd_data_t) + \
35  (sizeof(tiny_i_frame_info_t *) + sizeof(tiny_i_frame_info_t) + mtu) * ( window + 1 ))
36 
37 #define FD_MTU_SIZE(bsize,window) ( (bsize - sizeof(tiny_fd_data_t)) / (window + 1) - \
38  (sizeof(tiny_i_frame_info_t *) + sizeof(tiny_i_frame_info_t)) )
39 
40 typedef enum
41 {
42  TINY_FD_STATE_DISCONNECTED,
43  TINY_FD_STATE_CONNECTING,
44  TINY_FD_STATE_CONNECTED_ABM,
45 } tiny_fd_state_t;
46 
47 typedef struct
48 {
49  uint8_t address;
50  uint8_t control;
51 } tiny_frame_header_t;
52 
53 typedef struct
54 {
55  tiny_frame_header_t header;
56 } tiny_s_frame_info_t;
57 
58 typedef struct
59 {
60  tiny_frame_header_t header;
61  uint8_t data1;
62  uint8_t data2;
63  uint8_t data3;
64 } tiny_u_frame_info_t;
65 
66 typedef struct
67 {
68  uint8_t type;
69  int len;
70  tiny_frame_header_t header;
71  uint8_t user_payload;
72 } tiny_i_frame_info_t;
73 
74 typedef struct
75 {
76  int len;
77  union
78  {
79  tiny_s_frame_info_t s_frame;
80  tiny_u_frame_info_t u_frame;
81  };
82 } tiny_frame_info_t;
83 
84 typedef struct
85 {
86  tiny_i_frame_info_t **i_frames;
87  uint8_t max_i_frames;
88  uint8_t head_ptr;
89 
90  uint8_t *rx_buffer;
91  uint8_t *tx_buffer;
92  int mtu;
93 
94  tiny_mutex_t mutex;
95  uint8_t next_nr; // frame waiting to receive
96  uint8_t sent_nr; // frame index last sent back
97  uint8_t sent_reject; // If reject was already sent
98  uint8_t next_ns; // next frame to be sent
99  uint8_t confirm_ns; // next frame to be confirmed
100  uint8_t last_ns; // next free frame in cycle buffer
101 
102  uint32_t last_i_ts; // last sent I-frame timestamp
103  uint32_t last_ka_ts; // last keep alive timestamp
104  uint8_t ka_confirmed;
105 
106  uint8_t retries; // Number of retries to perform before timeout takes place
107 
108  tiny_events_t events;
109 } tiny_frames_info_t;
110 
111 typedef struct tiny_fd_data_t
112 {
114  hdlc_struct_t _hdlc;
116  tiny_fd_state_t state;
118  write_block_cb_t write_func;
120  read_block_cb_t read_func;
122  on_frame_cb_t on_frame_cb;
124  on_frame_cb_t on_sent_cb;
126  uint16_t send_timeout;
128  uint16_t retry_timeout;
130  uint16_t ka_timeout;
132  uint8_t retries;
134  tiny_frames_info_t frames;
135  struct
136  {
137  tiny_frame_info_t queue[TINY_FD_U_QUEUE_MAX_SIZE];
138  uint8_t queue_ptr;
139  uint8_t queue_len;
140  } s_u_frames;
142  void * user_data;
143 } tiny_fd_data_t;
144 
145 #ifdef __cplusplus
146 }
147 #endif
148 
149 #endif
int(* read_block_cb_t)(void *pdata, void *buffer, int size)
Definition: tiny_types.h:139
void(* on_frame_cb_t)(void *handle, uint16_t uid, uint8_t *pdata, int size)
Definition: tiny_types.h:152
Tiny protocol Types.
Definition: tiny_hdlc.h:65
int(* write_block_cb_t)(void *pdata, const void *buffer, int size)
Definition: tiny_types.h:128