-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinyTransfer.cpp
258 lines (208 loc) · 7.34 KB
/
tinyTransfer.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//
// Created by dylan on 8/12/23.
//
#include <cstring>
#include "tinyTransfer.h"
extern "C" {
#include "heatshrink_encoder.h"
}
/** Static compression encoder and decoder */
static heatshrink_encoder hs_encoder;
uint16_t fletcher16(const uint8_t* data, uint64_t length){
uint32_t c0, c1;
/* Found by solving for c1 overflow: */
/* n > 0 and n * (n+1) / 2 * (2^8-1) < (2^32-1). */
for (c0 = c1 = 0; length > 0; ) {
uint64_t blocklen = length;
if (blocklen > 5802) {
blocklen = 5802;
}
length -= blocklen;
do {
c0 = c0 + *data++;
c1 = c1 + c0;
} while (--blocklen);
c0 = c0 % 255;
c1 = c1 % 255;
}
return (c1 << 8 | c0);
}
TinyTransferUpdatePacket::TinyTransferUpdatePacket(uint8_t* _data, uint16_t _length, uint32_t _packetId, char* _log, uint16_t _logSize, bool compressed, bool isIntegrator) {
packetFlags = 0;
if (compressed) {
heatshrink_encoder_reset(&hs_encoder);
size_t count = 0;
uint32_t sunk = 0;
uint32_t polled = 0;
while (sunk < _length) {
heatshrink_encoder_sink(&hs_encoder, &_data[sunk], _length - sunk, &count);
sunk += count;
if (sunk >= _length) {
heatshrink_encoder_finish(&hs_encoder);
}
HSE_poll_res pres;
do {
pres = heatshrink_encoder_poll(&hs_encoder, &payload[polled], sizeof(TinyTransferUpdatePacket) - polled, &count);
polled += count;
} while (pres == HSER_POLL_MORE);
if (sunk == _length) {
heatshrink_encoder_finish(&hs_encoder);
}
}
payloadSize = polled;
packetFlags |= TINY_TRANSFER_UPDATE_FLAGS_COMPRESSED;
}
else {
memcpy(payload, _data, _length);
payloadSize = _length;
packetFlags &= ~TINY_TRANSFER_UPDATE_FLAGS_COMPRESSED;
}
// Copy log (only for hamster packets)
if (!isIntegrator) {
memcpy(log, _log, _logSize);
logSize = _logSize;
} else {
logSize = 0;
}
if (isIntegrator) {
packetFlags |= TINY_TRANSFER_UPDATE_FLAGS_INTEGRATOR_PACK;
}
packetId = _packetId;
payloadChecksum = fletcher16(payload, payloadSize);
headerChecksum = fletcher16(header, sizeof(header));
}
uint16_t TinyTransferUpdatePacket::TinyTransferUpdatePacket::serialize(uint8_t* output) {
memcpy(output, header, sizeof(header));
memcpy(output + sizeof(header), &headerChecksum, sizeof(headerChecksum));
memcpy(output + sizeof(header) + sizeof(headerChecksum), payload, payloadSize);
memcpy(output + sizeof(header) + sizeof(headerChecksum) + payloadSize, log, logSize);
return sizeof(header) + sizeof(headerChecksum) + payloadSize + logSize;
}
TinyTransferUpdateParser::TinyTransferUpdateParser() {
init();
}
void TinyTransferUpdateParser::init(){
completedPacket = inputPacket;
state = TINY_TRANSFER_PARSER_SEARCHING_FOR_SOH;
soh = 0;
inputPacket = TinyTransferUpdatePacket();
position = 0;
}
bool TinyTransferUpdateParser::processByte(uint8_t byte){
if(state == TINY_TRANSFER_PARSER_SEARCHING_FOR_SOH){
soh = soh >> 8;
soh |= (byte << 24);
if(soh == TINY_TRANSFER_UPDATE_SOH){
state = TINY_TRANSFER_PARSER_HEADER;
memcpy(inputPacket.header, &soh, sizeof(soh));
position = sizeof(soh);
}
}
else if (state == TINY_TRANSFER_PARSER_HEADER){
inputPacket.header[position] = byte;
position++;
if(position >= sizeof(TinyTransferUpdatePacket::header)){
state = TINY_TRANSFER_PARSER_HEADER_CHECKSUM;
position = 0;
}
}
else if (state == TINY_TRANSFER_PARSER_HEADER_CHECKSUM){
((uint8_t*)(&inputPacket.headerChecksum))[position] = byte;
position++;
if(position == sizeof(inputPacket.headerChecksum)){
uint16_t redo_checksum = fletcher16(inputPacket.header, sizeof(TinyTransferRPCPacket::header));
if(redo_checksum == inputPacket.headerChecksum){
if (inputPacket.payloadSize == 0) {
init();
return true;
}
state = TINY_TRANSFER_PARSER_PAYLOAD;
position = 0;
}
else {
init();
}
}
}
else if (state == TINY_TRANSFER_PARSER_PAYLOAD) {
inputPacket.payload[position] = byte;
if(position >= inputPacket.payloadSize){
///send packet or something or return true
init();
return true;
}
position++;
}
return false;
}
TinyTransferRPCPacket::TinyTransferRPCPacket() {
}
TinyTransferRPCPacket::TinyTransferRPCPacket(uint8_t* _data) {
memcpy(header, _data, sizeof(header));
memcpy(&headerChecksum, _data + sizeof(header), sizeof(headerChecksum));
uint16_t copyLength = procArgsLength > TINY_TRANSFER_RPC_MAX_ARGS_SIZE ? TINY_TRANSFER_RPC_MAX_ARGS_SIZE : procArgsLength;
memcpy(args, _data + sizeof(header) + sizeof(headerChecksum), copyLength);
}
bool TinyTransferRPCPacket::isValid() {
bool sohCheck = startOfHeader == TINY_TRANSFER_RPC_SOH;
bool headerPass = fletcher16(header, sizeof(header)) == headerChecksum;
bool argsPass = fletcher16(args, procArgsLength) == procArgsChecksum && procArgsLength <= TINY_TRANSFER_RPC_MAX_ARGS_SIZE;
return sohCheck && headerPass && argsPass;
}
TinyTransferRPCParser::TinyTransferRPCParser() {
init();
}
void TinyTransferRPCParser::init(){
completedPacket = inputPacket;
state = TINY_TRANSFER_PARSER_SEARCHING_FOR_SOH;
soh = 0;
inputPacket = TinyTransferRPCPacket();
position = 0;
}
bool TinyTransferRPCParser::processByte(uint8_t byte){
if(state == TINY_TRANSFER_PARSER_SEARCHING_FOR_SOH){
soh = soh >> 8;
soh |= (byte << 24);
if(soh == TINY_TRANSFER_RPC_SOH){
state = TINY_TRANSFER_PARSER_HEADER;
memcpy(inputPacket.header, &soh, sizeof(soh));
position = sizeof(soh);
}
}
else if (state == TINY_TRANSFER_PARSER_HEADER){
inputPacket.header[position] = byte;
position++;
if(position >= sizeof(TinyTransferRPCPacket::header)){
state = TINY_TRANSFER_PARSER_HEADER_CHECKSUM;
position = 0;
}
}
else if (state == TINY_TRANSFER_PARSER_HEADER_CHECKSUM){
((uint8_t*)(&inputPacket.headerChecksum))[position] = byte;
position++;
if(position == sizeof(inputPacket.headerChecksum)){
uint16_t redo_checksum = fletcher16(inputPacket.header, sizeof(TinyTransferRPCPacket::header));
if(redo_checksum == inputPacket.headerChecksum){
if (inputPacket.procArgsLength == 0) {
init();
return true;
}
state = TINY_TRANSFER_PARSER_PAYLOAD;
position = 0;
}
else {
init();
}
}
}
else if (state == TINY_TRANSFER_PARSER_PAYLOAD) {
inputPacket.args[position] = byte;
if(position >= inputPacket.procArgsLength){
///send packet or something or return true
init();
return true;
}
position++;
}
return false;
}