-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicDriver.cpp
More file actions
303 lines (261 loc) · 7.65 KB
/
DynamicDriver.cpp
File metadata and controls
303 lines (261 loc) · 7.65 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* Arduino Control Library
* Arduino library to call functions via serial
* based on Christophe Diericx's library
* https://github.com/christophediericx/ArduinoDriver/blob/master/Source/ArduinoDriver/ArduinoListener/ArduinoListener.ino
*
* Name: DynamicDriver
* Author: Alejandro Guardiola
* License: MIT
* Special Thanks to Christophe Diericx
*/
#include "DynamicDriver.h"
#include "arduino.h"
//prototypes
bool expectNumberOfBytesToArrive(byte numberOfBytes, unsigned long timeout);
void WriteError();
unsigned int Fletcher16(byte data[], int count);
size_t getSize(FunctionType type);
const long BAUD_RATE = 115200;
const unsigned int SYNC_TIMEOUT = 30000;//250;
const unsigned int READ_REQUESTPAYLOAD_TIMEOUT = 60000;//1000
const unsigned int SYNC_REQUEST_LENGTH = 4;
const unsigned int CMD_HANDSHAKE_LENGTH = 3;
const byte START_OF_REQUEST_MARKER = 0xfb;
const byte ALL_BYTES_WRITTEN = 0xfa;
const byte START_OF_RESPONSE_MARKER = 0xf9;
const byte ERROR_MARKER = 0xef;
const byte VOID_RETURN_RESULT=0xfc;
const unsigned int PROTOCOL_VERSION_MAJOR = 1;
const unsigned int PROTOCOL_VERSION_MINOR = 2;
const byte CMD_HANDSHAKE_INITIATE = 0x01;
const byte ACK_HANDSHAKE_INITIATE = 0x02;
DynamicDriver::DynamicDriver()
{
}
DynamicDriver::~DynamicDriver()
{
}
void DynamicDriver::setup()
{
Serial.begin(BAUD_RATE);
while (!Serial) { ; }
}
void DynamicDriver::loop()
{
// Reset variables
for (int i = 0; i < 128; i++) { data[i] = 0x00; }
// Try to acquire SYNC. Try to read up to four bytes, and only advance if the pattern matches ff fe fd fc.
// Blocking (the client must retry if getting sync fails).
while (Serial.available() < SYNC_REQUEST_LENGTH) { ; }
if ((syncByte = Serial.read()) != 0xff) return;
if ((syncByte = Serial.read()) != 0xfe) return;
if ((syncByte = Serial.read()) != 0xfd) return;
if ((syncByte = Serial.read()) != 0xfc) return;
// Write out SYNC ACK (fc fd fe ff).
Serial.write(0xfc);
Serial.write(0xfd);
Serial.write(0xfe);
Serial.write(0xff);
Serial.flush();
// Now expect the START_OF_REQUEST_MARKER (0xfb), followed by our command byte, and a length byte
// To acknowledge, we will write out the sequence in reverse (length byte, command byte, START_OF_REQUEST_MARKER)
// We cannot be blocking as the client expects an answer.
if (!expectNumberOfBytesToArrive(CMD_HANDSHAKE_LENGTH, SYNC_TIMEOUT)) return;
if (Serial.read() != START_OF_REQUEST_MARKER) return;
commandByte = Serial.read();
lengthByte = Serial.read();
// Write out acknowledgement.
Serial.write(lengthByte);
Serial.write(commandByte);
Serial.write(START_OF_REQUEST_MARKER);
Serial.flush();
// Read length bytes + 4 (first two bytes are commandByte + length repeated, last two bytes are fletcher16 checksums)
if (!expectNumberOfBytesToArrive(lengthByte + 4, READ_REQUESTPAYLOAD_TIMEOUT)) return;
for (int i = 0; i < lengthByte + 4; i++) { data[i] = Serial.read(); }
fletcherByte1 = data[lengthByte + 2];
fletcherByte2 = data[lengthByte + 3];
// Expect all bytes written package to come in (0xfa), non blocking
if (!expectNumberOfBytesToArrive(1, READ_REQUESTPAYLOAD_TIMEOUT)) return;
if (Serial.read() != ALL_BYTES_WRITTEN) return;
// Packet checks: do fletcher16 checksum!
fletcher16 = Fletcher16(data, lengthByte + 2);
f0 = fletcher16 & 0xff;
f1 = (fletcher16 >> 8) & 0xff;
c0 = 0xff - ((f0 + f1) % 0xff);
c1 = 0xff - ((f0 + c0) % 0xff);
// Sanity check of checksum + command and length values, so that we can trust the entire packet.
if (c0 != fletcherByte1 || c1 != fletcherByte2 || commandByte != data[0] || lengthByte != data[1]) {
WriteError();
return;
}
//Work with the data------------------------------------------------------------------------------------------------------
if (commandByte == CMD_HANDSHAKE_INITIATE)
{
Serial.write(START_OF_RESPONSE_MARKER);
Serial.write(3);
Serial.write(ACK_HANDSHAKE_INITIATE);
Serial.write(PROTOCOL_VERSION_MAJOR);
Serial.write(PROTOCOL_VERSION_MINOR);
Serial.flush();
return;
}
//get The correspond function
Function* function = getFunction(commandByte);
//Serial.write(START_OF_RESPONSE_MARKER);
//Obtain the function parameters
FunctionType returnType = function->getReturnType();
int argumentsCount = function->getArgumentsCount();
FunctionType* arguments = function->getArgumentTypes();
int pos = 2;
//get all arguments
for (int i = 0; i < argumentsCount; i++)
{
argumentsData[i] = &data[pos];
FunctionType argument = arguments[i];
pos += getSize(argument);
}
void* result;
size_t returnTypeSize;
if (returnType == VOIDT)
result = NULL;
else
result = malloc(returnTypeSize=getSize(returnType));
function->execute(argumentsData, result);
//free the result
if (result != NULL)
{
Serial.write(START_OF_RESPONSE_MARKER);
Serial.write(returnTypeSize);
byte buf[4];
int16_t int16;
uint16_t uint16;
switch (returnType)
{
case BOOLEANT:
Serial.write(*(byte*)result);
break;
case CHART:
Serial.write(*(byte*)result);
break;
case UCHART:
Serial.write(*(byte*)result);
break;
case BYTET:
Serial.write(*(byte*)result);
break;
case INTT:
int16 = *(int16_t*)result;
buf[0] = int16 & 255;
buf[1] = (int16 >> 8) & 255;
break;
case UINTT:
uint16 = *(uint16_t*)result;
buf[0] = uint16 & 255;
buf[1] = (uint16 >> 8) & 255;
break;
case WORD:
uint16 = *(uint16_t*)result;
buf[0] = uint16 & 255;
buf[1] = (uint16 >> 8) & 255;
break;
case LONG:
int16 = *(int16_t*)result;
buf[0] = int16 & 255;
buf[1] = (int16 >> 8) & 255;
break;
case ULONG:
uint16 = *(uint16_t*)result;
buf[0] = uint16 & 255;
buf[1] = (uint16 >> 8) & 255;
break;
case SHORT:
int16 = *(int16_t*)result;
buf[0] = int16 & 255;
buf[1] = (int16 >> 8) & 255;
break;
default:
break;
}
Serial.write(buf, returnTypeSize);
free(result);
}
else
{
Serial.write(START_OF_RESPONSE_MARKER);
Serial.write(VOID_RETURN_RESULT);
}
Serial.flush();
}
size_t getSize(FunctionType type)
{
size_t resultSize;
switch (type)
{
case BOOLEANT:
resultSize = sizeof(boolean);
break;
case CHART:
resultSize = sizeof(char);
break;
case UCHART:
resultSize = sizeof(unsigned char);
break;
case BYTET:
resultSize = sizeof(byte);
break;
case INTT:
resultSize = sizeof(int16_t);
break;
case UINTT:
resultSize = sizeof(uint16_t);
break;
case WORD:
resultSize = sizeof(word);
break;
case LONG:
resultSize = sizeof(long long);
break;
case ULONG:
resultSize = sizeof(unsigned long long);
break;
case SHORT:
resultSize = sizeof(unsigned short);
break;
}
return resultSize;
}
bool expectNumberOfBytesToArrive(byte numberOfBytes, unsigned long timeout)
{
unsigned long timeoutStartTicks = millis();
while ((Serial.available() < numberOfBytes) && ((millis() - timeoutStartTicks) < timeout)) { ; }
if (Serial.available() < numberOfBytes) {
// Unable to get sufficient bytes, perhaps one was lost in transportation? Write out three error marker bytes.
WriteError();
return false;
}
return true;
}
void WriteError()
{
for (int i = 0; i < 3; i++) { Serial.write(ERROR_MARKER); }
Serial.flush();
}
unsigned int Fletcher16(byte data[], int count)
{
unsigned int sum1 = 0;
unsigned int sum2 = 0;
unsigned int idx;
for (idx = 0; idx < count; ++idx) {
sum1 = (sum1 + data[idx]) % 255;
sum2 = (sum2 + sum1) % 255;
}
return (sum2 << 8) | sum1;
}
//Function class
Function::Function()
{
}
Function::~Function()
{
}