Tiny protocol  0.9.0
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 typedef enum
38 {
39  TINY_FD_STATE_DISCONNECTED,
40  TINY_FD_STATE_CONNECTING,
41  TINY_FD_STATE_CONNECTED_ABM,
42 } tiny_fd_state_t;
43 
44 typedef struct
45 {
46  uint8_t address;
47  uint8_t control;
48 } tiny_frame_header_t;
49 
50 typedef struct
51 {
52  tiny_frame_header_t header;
53 } tiny_s_frame_info_t;
54 
55 typedef struct
56 {
57  tiny_frame_header_t header;
58  uint8_t data1;
59  uint8_t data2;
60  uint8_t data3;
61 } tiny_u_frame_info_t;
62 
63 typedef struct
64 {
65  uint8_t type;
66  int len;
67  tiny_frame_header_t header;
68  uint8_t user_payload;
69 } tiny_i_frame_info_t;
70 
71 typedef struct
72 {
73  int len;
74  union
75  {
76  tiny_s_frame_info_t s_frame;
77  tiny_u_frame_info_t u_frame;
78  };
79 } tiny_frame_info_t;
80 
81 typedef struct
82 {
83  tiny_i_frame_info_t **i_frames;
84  uint8_t max_i_frames;
85  uint8_t ns_offset;
86 
87  tiny_frame_info_t queue[TINY_FD_U_QUEUE_MAX_SIZE];
88  uint8_t queue_ptr;
89  uint8_t queue_len;
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  uint32_t last_i_ts; // last sent I-frame timestamp
102  uint32_t last_ka_ts; // last keep alive timestamp
103  uint8_t ka_confirmed;
104 
105  uint8_t retries; // Number of retries to perform before timeout takes place
106 
107  tiny_events_t events;
108 } tiny_frames_info_t;
109 
110 typedef struct tiny_fd_data_t
111 {
113  hdlc_struct_t _hdlc;
115  tiny_fd_state_t state;
117  write_block_cb_t write_func;
119  read_block_cb_t read_func;
121  on_frame_cb_t on_frame_cb;
123  on_frame_cb_t on_sent_cb;
125  uint16_t send_timeout;
127  uint16_t retry_timeout;
129  uint16_t ka_timeout;
131  uint8_t retries;
133  tiny_frames_info_t frames;
135  void * user_data;
136 } tiny_fd_data_t;
137 
138 #ifdef __cplusplus
139 }
140 #endif
141 
142 #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