forked from sysrun/Reflecta
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectaFramesSerial.cpp
More file actions
261 lines (221 loc) · 7.11 KB
/
ReflectaFramesSerial.cpp
File metadata and controls
261 lines (221 loc) · 7.11 KB
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
259
260
261
/*
ReflectaFramesSerial.cpp - Library for sending frames of information from a Microcontroller to a PC over a serial port.
*/
#include "Reflecta.h"
namespace reflectaFrames {
// SLIP (http://www.ietf.org/rfc/rfc1055.txt) protocol special character
// definitions used to find end of frame when using a streaming communications
// protocol
enum EscapeCharacters {
End = 0xC0,
Escape = 0xDB,
EscapedEnd = 0xDC,
EscapedEscape = 0xDD
};
enum ReadState {
FrameStart,
ReadingFrame,
FrameEnded,
FrameInvalid
};
// Checksum for the incoming frame, calculated byte by byte using XOR.
// Compared against the checksum byte which is stored in the last byte of the
// frame.
uint8_t readChecksum = 0;
// Checksum for the outgoing frame, calculated byte by byte using XOR. Added
// to the payload as the last byte of the frame.
uint8_t writeChecksum = 0;
// Sequence number of the incoming frames. Compared against the sequence
// number at the beginning of the incoming frame to detect out of sequence
// frames which would point towards lost data or corrupted data.
uint8_t readSequence = 0;
// Sequence number of the outgoing frames.
uint8_t writeSequence = 0;
// protocol parser escape state -- set when the ESC character is detected so
// the next character will be de-escaped
bool escaped = false;
// protocol parser state
int state = FrameStart;
frameBufferAllocationFunction frameBufferAllocationCallback = NULL;
frameReceivedFunction frameReceivedCallback = NULL;
void setFrameReceivedCallback(frameReceivedFunction frameReceived) {
frameReceivedCallback = frameReceived;
}
void setBufferAllocationCallback(
frameBufferAllocationFunction frameBufferAllocation) {
frameBufferAllocationCallback = frameBufferAllocation;
}
void writeEscaped(uint8_t b) {
switch (b) {
case End:
Serial.write(Escape);
Serial.write(EscapedEnd);
break;
case Escape:
Serial.write(Escape);
Serial.write(EscapedEscape);
break;
default:
Serial.write(b);
break;
}
writeChecksum ^= b;
}
uint8_t sendFrame(uint8_t* frame, uint8_t frameLength) {
writeChecksum = 0;
writeEscaped(writeSequence);
for (uint8_t index = 0; index < frameLength; index++) {
writeEscaped(frame[index]);
}
writeEscaped(writeChecksum);
Serial.write(End);
// On Teensies, use the extended send_now to perform an undelayed send
#ifdef USBserial_h_
Serial.send_now();
#endif
return writeSequence++;
}
void sendEvent(reflecta::FunctionId type, uint8_t code) {
uint8_t buffer[2];
buffer[0] = type;
buffer[1] = code;
sendFrame(buffer, 2);
}
void sendMessage(const char* message) {
uint8_t messageLength = strlen(message);
char buffer[reflecta::kFrameSizeMax];
if (2 + messageLength > reflecta::kFrameSizeMax) {
reflectaFrames::sendEvent(reflecta::Error, reflecta::BufferOverflow);
return;
}
buffer[0] = reflecta::Message;
buffer[1] = messageLength;
memcpy(buffer + 2, message, messageLength);
// Strip off the trailing '\0' that Arduino String.getBytes insists on
// postpending
sendFrame((uint8_t*)buffer, messageLength + 2);
}
int readUnescaped(uint8_t *b) {
*b = Serial.read();
if (escaped) {
switch (*b) {
case EscapedEnd:
*b = End;
break;
case EscapedEscape:
*b = Escape;
break;
default:
sendEvent(reflecta::Warning, reflecta:: UnexpectedEscape);
state = FrameInvalid;
break;
}
escaped = false;
readChecksum ^= *b;
} else {
if (*b == Escape) {
escaped = true;
return 0; // read escaped value on next pass
}
if (*b == End) {
switch (state) {
case FrameInvalid:
readChecksum = 0;
state = FrameStart;
break;
case ReadingFrame:
state = FrameEnded;
break;
default:
sendEvent(reflecta::Warning, reflecta::UnexpectedEnd);
state = FrameInvalid;
break;
}
} else {
readChecksum ^= *b;
}
}
return 1;
}
const uint8_t frameBufferSourceLength = 64;
uint8_t* frameBufferSource = NULL;
// Default frame buffer allocator for when caller does not set one.
uint8_t frameBufferAllocation(uint8_t** frameBuffer) {
*frameBuffer = frameBufferSource;
return frameBufferSourceLength;
}
void reset() {
readSequence = 0;
writeSequence = 0;
Serial.flush();
}
void setup(int speed) {
if (frameBufferAllocationCallback == NULL) {
frameBufferSource = reinterpret_cast<uint8_t*>(
malloc(frameBufferSourceLength));
frameBufferAllocationCallback = frameBufferAllocation;
}
Serial.begin(speed);
Serial.flush();
}
uint8_t* frameBuffer;
uint8_t frameBufferLength;
uint8_t frameIndex = 0;
uint8_t sequence;
uint32_t lastFrameReceived;
void loop() {
uint8_t b;
while (Serial.available()) {
if (readUnescaped(&b)) {
switch (state) {
case FrameInvalid:
break;
case FrameStart:
sequence = b;
if (readSequence++ != sequence) {
// Only send an out of sequence warning if the time between frames
// is < 10 seconds. This is because we have no 'port opened/port
// closed' API on Arduino to tell when a connection has been
// physically reset by the host
if (lastFrameReceived - millis() < 10000) {
char message[30];
snprintf(message, 30, "Expected %x received %x", readSequence - 1, sequence);
sendMessage(message);
sendEvent(reflecta::Warning, reflecta::OutOfSequence);
}
readSequence = sequence + 1;
}
frameBufferLength = frameBufferAllocationCallback(&frameBuffer);
frameIndex = 0; // Reset the buffer pointer to beginning
readChecksum = sequence;
state = ReadingFrame;
break;
case ReadingFrame:
if (frameIndex == frameBufferLength) {
sendEvent(reflecta::Error, reflecta::BufferOverflow);
state = FrameInvalid;
readChecksum = 0;
} else {
frameBuffer[frameIndex++] = b;
}
break;
case FrameEnded:
lastFrameReceived = millis();
// zero expected because finally XOR'd with itself
if (readChecksum == 0) {
// TODO(jay): add a MessageReceived callback too
if (frameReceivedCallback != NULL) {
frameReceivedCallback(sequence, frameIndex - 1, frameBuffer);
}
} else {
sendEvent(reflecta::Warning, reflecta::CrcMismatch);
state = FrameInvalid;
readChecksum = 0;
}
state = FrameStart;
break;
}
}
}
}
} // namespace reflectaFrames