-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.cpp
executable file
·174 lines (158 loc) · 3.63 KB
/
protocol.cpp
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
#include "protocol.h"
namespace protocol {
packet_decoder::packet_decoder()
{
this->reset();
}
void packet_decoder::process_packets()
{
if (this->new_data_available)
{
this->new_data_available = false;
switch (this->current_state)
{
case pkt_state::pkt_state_idle:
{
if (PACKET_SYNC_0_CHAR == this->last_received_char)
{
this->current_state =
pkt_state::pkt_state_expecting_start_sync1;
}
}
break;
case pkt_state::pkt_state_expecting_start_sync1:
{
if (PACKET_SYNC_1_CHAR == this->last_received_char)
{
this->current_state =
pkt_state::pkt_state_expecting_start_sync2;
}
else
{
this->set_error(error_code::bad_sync);
this->reset();
}
}
break;
case pkt_state::pkt_state_expecting_start_sync2:
{
if (PACKET_SYNC_2_CHAR == this->last_received_char)
{
this->current_state =
pkt_state::pkt_state_expecting_start_sync3;
}
else
{
this->set_error(error_code::bad_sync);
this->reset();
}
}
break;
case pkt_state::pkt_state_expecting_start_sync3:
{
if (PACKET_SYNC_3_CHAR == this->last_received_char)
{
this->current_state = pkt_state::pkt_state_expecting_length;
}
else
{
this->set_error(error_code::bad_sync);
this->reset();
}
}
break;
case pkt_state::pkt_state_expecting_length:
{
this->payload_length = this->last_received_char;
if (this->payload_length
&& (this->payload_length <= PAYLOAD_BUFFER_SIZE))
{
this->current_state = pkt_state::pkt_state_expecting_payload;
}
else
{
this->set_error(error_code::invalid_length);
this->reset();
}
}
break;
case pkt_state::pkt_state_expecting_payload:
{
this->received_payload_buffer[this->received_payload_index] =
this->last_received_char;
if (++this->received_payload_index == this->payload_length)
{
this->expected_crc16 = calc_crc16(
this->received_payload_buffer,
this->received_payload_index);
this->received_payload_buffer[this->received_payload_index] =
'\0';
this->current_state = pkt_state::pkt_state_expecting_crc1;
}
}
break;
case pkt_state::pkt_state_expecting_crc1:
{
this->crc16 = this->last_received_char << 8;
this->current_state = pkt_state::pkt_state_expecting_crc2;
}
break;
case pkt_state::pkt_state_expecting_crc2:
{
this->crc16 |= this->last_received_char;
if (this->expected_crc16 == this->crc16)
{
this->current_state = pkt_state::pkt_state_expecting_terminator;
}
else
{
this->set_error(error_code::bad_crc);
this->reset();
}
}
break;
case pkt_state::pkt_state_expecting_terminator:
{
if (PACKET_TERMINATOR_CHAR == this->last_received_char)
{
this->process_payload(this->received_payload_buffer,
this->received_payload_index);
}
else
{
this->set_error(error_code::bad_terminator);
}
this->reset();
}
break;
}
} // end switch
else
{ // timeout
if (++this->iteration_counter == cmd_timeout)
{
this->reset();
}
}
}
void packet_decoder::reset()
{
this->current_state = pkt_state::pkt_state_idle;
this->received_payload_index = 0;
this->iteration_counter = 0;
this->crc16 = 0;
}
uint16_t calc_crc16(const unsigned char* data_p, uint8_t length)
{
uint8_t x;
uint16_t crc = 0xFFFF;
while (length--)
{
x = crc >> 8 ^ *data_p++;
x ^= x >> 4;
crc = (crc << 8) ^ ((uint16_t)(x << 12)) ^ ((uint16_t)(x << 5))
^ ((uint16_t) x);
}
return crc;
}
} // namespace packets