-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeep_alive_platform.c
185 lines (147 loc) · 3.81 KB
/
keep_alive_platform.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//[email protected] wrote this file.
#include "stdint.h"
#include "stdlib.h"
#include "string.h"
#include "keep_alive_checker.h"
#include "stdio.h"
#include "stdbool.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef void (*tx_cbf)(const uint8_t* send_buf, uint8_t size);
typedef void (*fault_cbf)(void* self);
typedef enum e_opcode
{
KEEP_ALIVE_CHECK = 0x02,
KEEP_ALIVE_CHECK_RETRY,
KEEP_ALIVE_LAST_CHECK,
}e_opcode;
e_opcode code = KEEP_ALIVE_CHECK;
typedef struct
{
uint16_t ticker;
uint16_t response_ticker;
bool bresponse_ticker;
uint16_t response_timeout;
uint16_t timeout;
uint32_t old_rx;
uint32_t old_tx;
tx_cbf tx_cb;
fault_cbf fault_cb;
uint8_t buffer[11];
struct keep_alive_checker_t* kac;
}keep_alive_platform_t;
keep_alive_platform_t* new_platform(void)
{
return (keep_alive_platform_t*)malloc(sizeof(keep_alive_platform_t));
}
void platform_ctr(keep_alive_platform_t* self, uint16_t timeout_ms, tx_cbf tx_cb, fault_cbf fault_cb)
{
self -> timeout = timeout_ms;
self -> tx_cb = tx_cb;
self -> fault_cb = fault_cb;
self -> ticker = 0;
self -> old_rx = 0;
self -> old_tx = 0;
self -> response_ticker = 0;
self -> response_timeout = 3000;
self -> bresponse_ticker = false;
memset(self -> buffer, 0, sizeof(self -> buffer));
self -> kac = new_checker();
checker_ctr(self -> kac);
}
void platform_dtr(keep_alive_platform_t* self)
{
checker_dtr(self -> kac);
free(self -> kac);
}
int is_timeout_overflow(const keep_alive_platform_t* self)
{
return (self -> ticker > self -> timeout);
}
void platform_systick(keep_alive_platform_t* self)
{
self -> ticker++;
if (self -> bresponse_ticker)
{
if (self -> response_ticker++ > self -> response_timeout)
{
if (code == KEEP_ALIVE_CHECK)
{
code = KEEP_ALIVE_CHECK_RETRY;
}
else if (code == KEEP_ALIVE_CHECK_RETRY)
{
code = KEEP_ALIVE_LAST_CHECK;
}
else
{
if (self -> fault_cb != NULL)
{
self -> fault_cb(self);
code = KEEP_ALIVE_CHECK;
checker_ctr(self -> kac);
self -> ticker = 0;
}
}
self -> bresponse_ticker = false;
self -> response_ticker = 0;
}
}
if (is_timeout_overflow(self))
{
uint8_t *buffer = checker_data_packed(self -> kac, code);
self -> tx_cb (buffer, 11);
self -> ticker = 0;
self -> bresponse_ticker = true;
}
}
void add_buf_and_parse(keep_alive_platform_t* self, const uint8_t* buf)
{
struct keep_alive_checker_t *cm = (struct keep_alive_checker_t*)buf;
self -> bresponse_ticker = false;
self -> response_ticker = 0;
if (parse (self -> kac, cm))
{
printf("\r\n True Frame, Inc Tx");
code = KEEP_ALIVE_CHECK;
}
//To Do : move systick func
else
{
if (code == KEEP_ALIVE_CHECK)
{
code = KEEP_ALIVE_CHECK_RETRY;
}
else if (code == KEEP_ALIVE_CHECK_RETRY)
{
code = KEEP_ALIVE_LAST_CHECK;
}
else
{
if (self -> fault_cb != NULL)
{
self -> fault_cb(self);
code = KEEP_ALIVE_CHECK;
checker_ctr(self -> kac);
self -> ticker = 0;
}
}
}
checker_inc_rx(self -> kac);
}
uint32_t get_rx(const keep_alive_platform_t* self)
{
return checker_get_rx(self -> kac);
}
uint32_t get_tx(const keep_alive_platform_t* self)
{
return checker_get_tx(self -> kac);
}
void change_frequency_checker(keep_alive_platform_t* self, uint16_t timeout)
{
self -> timeout = timeout;
}
#ifdef __cplusplus
}
#endif