forked from dhansel/ArduinoFDC
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXModem.cpp
More file actions
Latest commit
378 lines (348 loc) · 8.53 KB
/
Copy pathXModem.cpp
File metadata and controls
378 lines (348 loc) · 8.53 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// This code was taken from: https://github.com/mgk/arduino-xmodem
// (https://code.google.com/archive/p/arduino-xmodem)
// which was released under GPL V3:
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// -----------------------------------------------------------------------------
#include<stdio.h>
#include<string.h>
#include"XModem.h"
#ifdef UTEST
#include"CppUTestExt/MockSupport.h"
#endif
constunsignedchar XModem::NACK = 21;
constunsignedchar XModem::ACK = 6;
constunsignedchar XModem::SOH = 1;
constunsignedchar XModem::EOT = 4;
constunsignedchar XModem::CAN = 0x18;
constint XModem::receiveDelay=7000;
constint XModem::rcvRetryLimit = 10;
XModem::XModem(int (*recvChar)(int msDelay),
void (*sendData)(constchar *data, int len))
{
this->sendData = sendData;
this->recvChar = recvChar;
this->dataHandler = NULL;
}
XModem::XModem(int (*recvChar)(int msDelay),
void (*sendData)(constchar *data, int len),
bool (*dataHandler)(unsignedlong number, char *buffer, int len))
{
this->sendData = sendData;
this->recvChar = recvChar;
this->dataHandler = dataHandler;
}
boolXModem::dataAvail(int delay)
{
if (this->byte != -1)
returntrue;
if ((this->byte = this->recvChar(delay)) != -1)
returntrue;
else
returnfalse;
}
intXModem::dataRead(int delay)
{
int b;
if(this->byte != -1)
{
b = this->byte;
this->byte = -1;
return b;
}
returnthis->recvChar(delay);
}
voidXModem::dataWrite(char symbol)
{
this->sendData(&symbol, 1);
}
boolXModem::receiveFrameNo()
{
unsignedchar num =
(unsignedchar)this->dataRead(XModem::receiveDelay);
unsignedchar invnum =
(unsignedchar)this->dataRead(XModem::receiveDelay);
this->repeatedBlock = false;
//check for repeated block
if (invnum == (255-num) && num == this->blockNo-1) {
this->repeatedBlock = true;
returntrue;
}
if(num != this-> blockNo || invnum != (255-num))
returnfalse;
else
returntrue;
}
boolXModem::receiveData()
{
for(int i = 0; i < 128; i++) {
int byte = this->dataRead(XModem::receiveDelay);
if(byte != -1)
this->buffer[i] = (unsignedchar)byte;
else
returnfalse;
}
returntrue;
}
boolXModem::checkCrc()
{
unsignedshort frame_crc = ((unsignedchar)this->
dataRead(XModem::receiveDelay)) << 8;
frame_crc |= (unsignedchar)this->dataRead(XModem::receiveDelay);
//now calculate crc on data
unsignedshort crc = this->crc16_ccitt(this->buffer, 128);
if(frame_crc != crc)
returnfalse;
else
returntrue;
}
boolXModem::checkChkSum()
{
unsignedchar frame_chksum = (unsignedchar)this->
dataRead(XModem::receiveDelay);
//calculate chksum
unsignedchar chksum = 0;
for(int i = 0; i< 128; i++) {
chksum += this->buffer[i];
}
if(frame_chksum == chksum)
returntrue;
else
returnfalse;
}
boolXModem::sendNack()
{
this->dataWrite(XModem::NACK);
this->retries++;
if(this->retries < XModem::rcvRetryLimit)
returntrue;
else
returnfalse;
}
boolXModem::receiveFrames(transfer_t transfer)
{
bool handlerOk = true;
this->blockNo = 1;
this->blockNoExt = 1;
this->retries = 0;
while (1) {
char cmd = this->dataRead(1000);
switch(cmd){
case XModem::SOH:
if (!this->receiveFrameNo()) {
if (this->sendNack())
break;
else
returnfalse;
}
if (!this->receiveData()) {
if (this->sendNack())
break;
else
returnfalse;
};
if (transfer == Crc) {
if (!this->checkCrc()) {
if (this->sendNack())
break;
else
returnfalse;
}
} else {
if(!this->checkChkSum()) {
if (this->sendNack())
break;
else
returnfalse;
}
}
//callback
if(handlerOk && this->dataHandler != NULL && this->repeatedBlock == false)
handlerOk = this->dataHandler(this->blockNoExt, this->buffer, 128);
//ack
if( handlerOk )
{
this->dataWrite(XModem::ACK);
if(this->repeatedBlock == false)
{
this->blockNo++;
this->blockNoExt++;
}
this->retries = 0;
}
elseif( !this->sendNack() )
returnfalse;
break;
case XModem::EOT:
this->dataWrite(XModem::ACK);
returntrue;
case XModem::CAN:
//wait second CAN
if(this->dataRead(XModem::receiveDelay) ==
XModem::CAN) {
this->dataWrite(XModem::ACK);
//this->flushInput();
returnfalse;
}
//something wrong
this->dataWrite(XModem::CAN);
this->dataWrite(XModem::CAN);
this->dataWrite(XModem::CAN);
returnfalse;
default:
//something wrong
this->dataWrite(XModem::CAN);
this->dataWrite(XModem::CAN);
this->dataWrite(XModem::CAN);
returnfalse;
}
}
}
voidXModem::init()
{
//set preread byte
this->byte = -1;
}
boolXModem::receive()
{
this->init();
for (int i =0; i < 128; i++)
{
this->dataWrite('C');
if (this->dataAvail(1000))
returnreceiveFrames(Crc);
}
for (int i =0; i < 128; i++)
{
this->dataWrite(XModem::NACK);
if (this->dataAvail(1000))
returnreceiveFrames(ChkSum);
}
returnfalse;
}
unsignedshortXModem::crc16_ccitt(char *buf, int size)
{
unsignedshort crc = 0;
while (--size >= 0) {
int i;
crc ^= (unsignedshort) *buf++ << 8;
for (i = 0; i < 8; i++)
if (crc & 0x8000)
crc = crc << 1 ^ 0x1021;
else
crc <<= 1;
}
return crc;
}
unsignedcharXModem::generateChkSum(constchar *buf, int len)
{
//calculate chksum
unsignedchar chksum = 0;
for(int i = 0; i< len; i++) {
chksum += buf[i];
}
return chksum;
}
boolXModem::transmitFrames(transfer_t transfer)
{
this->blockNo = 1;
this->blockNoExt = 1;
// use this only in unit tetsing
//memset(this->buffer, 'A', 128);
while(1)
{
//get data
if (this->dataHandler != NULL)
{
if( false ==
this->dataHandler(this->blockNoExt, this->buffer+3,
128))
{
//end of transfer
this->dataWrite(XModem::EOT);
//wait ACK
if (this->dataRead(XModem::receiveDelay) ==
XModem::ACK)
returntrue;
else
returnfalse;
}
}
else
{
//cancel transfer - send CAN twice
this->dataWrite(XModem::CAN);
this->dataWrite(XModem::CAN);
//wait ACK
if (this->dataRead(XModem::receiveDelay) ==
XModem::ACK)
returntrue;
else
returnfalse;
}
//SOH
buffer[0] = XModem::SOH;
//frame number
buffer[1] = this->blockNo;
//inv frame number
buffer[2] = (unsignedchar)(255-(this->blockNo));
//(data is already in buffer starting at byte 3)
//checksum or crc
if (transfer == ChkSum) {
buffer[3+128] = this->generateChkSum(buffer+3, 128);
this->sendData(buffer, 3+128+1);
} else {
unsignedshort crc;
crc = this->crc16_ccitt(this->buffer+3, 128);
buffer[3+128+0] = (unsignedchar)(crc >> 8);
buffer[3+128+1] = (unsignedchar)(crc);;
this->sendData(buffer, 3+128+2);
}
//TO DO - wait NACK or CAN or ACK
int ret = this->dataRead(XModem::receiveDelay);
switch(ret)
{
case XModem::ACK: //data is ok - go to next chunk
this->blockNo++;
this->blockNoExt++;
continue;
case XModem::NACK: //resend data
continue;
case XModem::CAN: //abort transmision
returnfalse;
}
}
returnfalse;
}
boolXModem::transmit()
{
int retry = 0;
int sym;
this->init();
//wait for CRC transfer
while(retry < 256)
{
if(this->dataAvail(1000))
{
sym = this->dataRead(1); //data is here - no delay
if(sym == 'C')
returnthis->transmitFrames(Crc);
if(sym == XModem::NACK)
returnthis->transmitFrames(ChkSum);
}
retry++;
}
returnfalse;
}