Tiny protocol  0.9.3
Tiny communication protocol for microcontrollers
tiny_fd_int.h
1 /*
2  Copyright 2019 (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 ns_queue_ptr;
89 
90  tiny_frame_info_t queue[TINY_FD_U_QUEUE_MAX_SIZE];
91  uint8_t queue_ptr;
92  uint8_t queue_len;
93  uint8_t *rx_buffer;
94  uint8_t *tx_buffer;
95  int mtu;
96 
97  tiny_mutex_t mutex;
98  uint8_t next_nr; // frame waiting to receive
99  uint8_t sent_nr; // frame index last sent back
100  uint8_t sent_reject; // If reject was already sent
101  uint8_t next_ns; // next frame to be sent
102  uint8_t confirm_ns; // next frame to be confirmed
103  uint8_t last_ns; // next free frame in cycle buffer
104  uint32_t last_i_ts; // last sent I-frame timestamp
105  uint32_t last_ka_ts; // last keep alive timestamp
106  uint8_t ka_confirmed;
107 
108  uint8_t retries; // Number of retries to perform before timeout takes place
109 
110  tiny_events_t events;
111 } tiny_frames_info_t;
112 
113 typedef struct tiny_fd_data_t
114 {
116  hdlc_struct_t _hdlc;
118  tiny_fd_state_t state;
120  write_block_cb_t write_func;
122  read_block_cb_t read_func;
124  on_frame_cb_t on_frame_cb;
126  on_frame_cb_t on_sent_cb;
128  uint16_t send_timeout;
130  uint16_t retry_timeout;
132  uint16_t ka_timeout;
134  uint8_t retries;
136  tiny_frames_info_t frames;
138  void * user_data;
139 } tiny_fd_data_t;
140 
141 #ifdef __cplusplus
142 }
143 #endif
144 
145 #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:47
int(* write_block_cb_t)(void *pdata, const void *buffer, int size)
Definition: tiny_types.h:128