-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeep_alive_checker.c
122 lines (100 loc) · 2.49 KB
/
keep_alive_checker.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
#include "stdint.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef enum e_opcode
{
KEEP_ALIVE_CHECK = 0x02,
KEEP_ALIVE_CHECK_RETRY,
KEEP_ALIVE_LAST_CHECK,
}e_opcode;
/*static e_opcode ret_opcode(int val)
{
switch (val)
{
case 1:
return KEEP_ALIVE_CHECK;
case 2:
return KEEP_ALIVE_CHECK_RETRY;
case 3:
return KEEP_ALIVE_LAST_CHECK;
default:
break;
}
}*/
typedef struct __attribute__((packed))
{
uint8_t start_of_frame;
uint8_t op_code;
uint32_t tx_cnt;
uint32_t rx_cnt;
uint8_t end_of_frame;
}keep_alive_checker_t;
keep_alive_checker_t* new_checker(void)
{
return (keep_alive_checker_t*)malloc(sizeof(keep_alive_checker_t));
}
void checker_ctr(keep_alive_checker_t* self)
{
self -> start_of_frame = 0x01;
self -> tx_cnt = 0;
self -> rx_cnt = 0;
self -> end_of_frame = 0x21;
}
void checker_dtr(keep_alive_checker_t* self)
{
//Nothing
}
uint8_t* checker_data_packed(keep_alive_checker_t* self, int opcode)
{
self -> start_of_frame = 0x01;
self -> op_code = opcode; //ret_opcode(opcode);
self -> tx_cnt = self -> tx_cnt + 1;
self -> rx_cnt = self -> rx_cnt;
self -> end_of_frame = 0x21;
static uint8_t test[11] = {0};
memcpy(test, self, 11);
return test;
}
void checker_inc_tx(keep_alive_checker_t* self)
{
self -> tx_cnt = self -> tx_cnt + 1;
}
void checker_inc_rx(keep_alive_checker_t* self)
{
self -> rx_cnt = self -> rx_cnt + 1;
}
uint32_t checker_get_tx(const keep_alive_checker_t* self)
{
return self -> tx_cnt;
}
uint32_t checker_get_rx(const keep_alive_checker_t* self)
{
return self -> rx_cnt;
}
int parse(keep_alive_checker_t* self, const keep_alive_checker_t* fakeself)
{
int ret = 0;
if (self -> start_of_frame == fakeself -> start_of_frame &&
self -> tx_cnt == fakeself -> tx_cnt &&
self -> rx_cnt == fakeself -> rx_cnt &&
self -> end_of_frame == fakeself -> end_of_frame)
{
printf("\r\n True ");
return (ret = 1);
}
return ret;
///*
// printf("\r\n fakeself %d", fakeself -> start_of_frame);
// printf("\r\n fakeself %d", fakeself -> op_code);
// printf("\r\n fakeself %d", fakeself -> tx_cnt);
// printf("\r\n fakeself %d", fakeself -> rx_cnt);
// printf("\r\n fakeself %d", fakeself -> end_of_frame);
//*/
}
#ifdef __cplusplus
}
#endif