/
XModem.java
442 lines (382 loc) · 14 KB
/
XModem.java
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package com.willwinder.universalgcodesender.connection.xmodem;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* This is core Modem class supporting XModem (and some extensions XModem-1K, XModem-CRC), and YModem.<br/>
* YModem support is limited (currently block 0 is ignored).<br/>
* <br/>
* Created by Anton Sirotinkin (aesirot@mail.ru), Moscow 2014 <br/>
* I hope you will find this program useful.<br/>
* You are free to use/modify the code for any purpose, but please leave a reference to me.
* <p>
* Original work from here: https://github.com/aesirot/ymodem
*/
class XModem {
/* Protocol characters used */
protected static final byte SOH = 0x01; /* Start Of Header */
protected static final byte STX = 0x02; /* Start Of Text (used like SOH but means 1024 block size) */
protected static final byte EOT = 0x04; /* End Of Transmission */
protected static final byte ACK = 0x06; /* ACKnowlege */
protected static final byte NAK = 0x15; /* Negative AcKnowlege */
protected static final byte CAN = 0x18; /* CANcel character */
protected static final byte CPM_EOF = 0x1A;
protected static final byte ST_C = 'C';
protected static final int MAX_ERRORS = 10;
protected static final int BLOCK_TIMEOUT = 3000;
protected static final int REQUEST_TIMEOUT = 3000;
protected static final int WAIT_FOR_RECEIVER_TIMEOUT = 60_000;
protected static final int SEND_BLOCK_TIMEOUT = 10_000;
private final InputStream inputStream;
private final OutputStream outputStream;
private final byte[] shortBlockBuffer;
private final byte[] longBlockBuffer;
/**
* Constructor
*
* @param inputStream stream for reading received data from other side
* @param outputStream stream for writing data to other side
*/
public XModem(InputStream inputStream, OutputStream outputStream) {
this.inputStream = inputStream;
this.outputStream = outputStream;
shortBlockBuffer = new byte[128];
longBlockBuffer = new byte[1024];
}
/**
* Wait for receiver request for transmission
*
* @param timer
* @return TRUE if receiver requested CRC-16 checksum, FALSE if 8bit checksum
* @throws java.io.IOException
*/
protected boolean waitReceiverRequest(Timer timer) throws IOException {
int character;
while (true) {
try {
character = readByte(timer);
if (character == NAK)
return false;
if (character == ST_C) {
return true;
}
} catch (TimeoutException e) {
throw new IOException("Timeout waiting for receiver");
}
}
}
/**
* Send data as an input stream
* <p>
* This method support correct thread interruption, when thread is interrupted "cancel of transmission" will be sent.
* So you can move long transmission to other thread and interrupt it according to your algorithm.
*
* @param inputStream a byte array to send
* @param useBlock1K uses a 1K send block (XModem-1K)
* @throws java.io.IOException if the transmission failed
*/
public void send(InputStream inputStream, boolean useBlock1K) throws IOException {
Timer timer = new Timer(WAIT_FOR_RECEIVER_TIMEOUT).start();
boolean useCRC16 = waitReceiverRequest(timer);
CRC crc;
if (useCRC16)
crc = new CRC16();
else
crc = new CRC8();
byte[] block;
if (useBlock1K)
block = new byte[1024];
else
block = new byte[128];
sendDataBlocks(inputStream, 1, crc, block);
sendEOT();
}
protected void sendDataBlocks(InputStream dataStream, int blockNumber, CRC crc, byte[] block) throws IOException {
int dataLength;
while ((dataLength = dataStream.read(block)) != -1) {
sendBlock(blockNumber++, block, dataLength, crc);
}
}
protected void sendEOT() throws IOException {
int errorCount = 0;
Timer timer = new Timer(BLOCK_TIMEOUT);
int character;
while (errorCount < 10) {
sendByte(EOT);
try {
character = readByte(timer.start());
if (character == ACK) {
return;
} else if (character == CAN) {
throw new IOException("Transmission terminated");
}
} catch (TimeoutException ignored) {
}
errorCount++;
}
}
protected void sendBlock(int blockNumber, byte[] block, int dataLength, CRC crc) throws IOException {
int errorCount;
int character;
Timer timer = new Timer(SEND_BLOCK_TIMEOUT);
if (dataLength < block.length) {
// Fill the remaining bytes with EOF:s
for (int i = dataLength; i < block.length; i++) {
block[i] = CPM_EOF;
}
}
errorCount = 0;
while (errorCount < MAX_ERRORS) {
timer.start();
if (block.length == 1024) {
outputStream.write(STX);
} else { //128
outputStream.write(SOH);
}
outputStream.write(blockNumber);
outputStream.write(~blockNumber);
outputStream.write(block);
writeCRC(block, crc);
outputStream.flush();
while (true) {
try {
character = readByte(timer);
if (character == ACK) {
return;
} else if (character == NAK) {
errorCount++;
break;
} else if (character == CAN) {
throw new IOException("Transmission terminated");
}
} catch (TimeoutException e) {
errorCount++;
break;
}
}
}
throw new IOException("Too many errors caught, abandoning transfer");
}
private void writeCRC(byte[] block, CRC crc) throws IOException {
byte[] crcBytes = new byte[crc.getCRCLength()];
long crcValue = crc.calcCRC(block);
for (int i = 0; i < crc.getCRCLength(); i++) {
crcBytes[crc.getCRCLength() - i - 1] = (byte) ((crcValue >> (8 * i)) & 0xFF);
}
outputStream.write(crcBytes);
}
/**
* Receives data
* <p>
* This method support correct thread interruption, when thread is interrupted "cancel of transmission" will be sent.
* So you can move long transmission to other thread and interrupt it according to your algorithm.
*
* @param outputStream the stream to receive the data into
* @throws java.io.IOException if the transmission failed
*/
public void receive(OutputStream outputStream, boolean useCRC16) throws IOException {
int available;
// clean input stream
if ((available = inputStream.available()) > 0) {
inputStream.skip(available);
}
int character = requestTransmissionStart(useCRC16);
CRC crc;
if (useCRC16)
crc = new CRC16();
else
crc = new CRC8();
processDataBlocks(crc, character, outputStream);
}
protected void processDataBlocks(CRC crc, int blockInitialCharacter, OutputStream dataOutput) throws IOException {
// read blocks until EOT
boolean result = false;
boolean shortBlock;
int blockNumber = 1;
byte[] block;
while (true) {
int errorCount = 0;
if (blockInitialCharacter == EOT) {
// end of transmission
sendByte(ACK);
return;
}
//read and process block
shortBlock = (blockInitialCharacter == SOH);
try {
block = readBlock(blockNumber, shortBlock, crc);
dataOutput.write(block);
blockNumber++;
errorCount = 0;
result = true;
sendByte(ACK);
} catch (TimeoutException | InvalidBlockException e) {
errorCount++;
if (errorCount == MAX_ERRORS) {
interruptTransmission();
throw new IOException("Transmission aborted, error count exceeded max");
}
sendByte(NAK);
result = false;
} catch (RepeatedBlockException e) {
//thats ok, accept and wait for next block
sendByte(ACK);
} catch (SynchronizationLostException e) {
//fatal transmission error
interruptTransmission();
throw new IOException("Fatal transmission error", e);
}
//wait for next block
blockInitialCharacter = readNextBlockStart(result);
}
}
protected void sendByte(byte b) throws IOException {
outputStream.write(b);
outputStream.flush();
}
/**
* Request transmission start and return first byte of "first" block from sender (block 1 for XModem, block 0 for YModem)
*
* @param useCRC16
* @return
* @throws java.io.IOException
*/
protected int requestTransmissionStart(boolean useCRC16) throws IOException {
int character;
int errorCount = 0;
byte requestStartByte;
if (!useCRC16) {
requestStartByte = NAK;
} else {
requestStartByte = ST_C;
}
// wait for first block start
Timer timer = new Timer(REQUEST_TIMEOUT);
while (errorCount < MAX_ERRORS) {
// request transmission start (will be repeated after 10 second timeout for 10 times)
sendByte(requestStartByte);
timer.start();
try {
while (true) {
character = readByte(timer);
if (character == SOH || character == STX) {
return character;
}
}
} catch (TimeoutException ignored) {
errorCount++;
}
}
interruptTransmission();
throw new RuntimeException("Timeout, no data received from transmitter");
}
protected int readNextBlockStart(boolean lastBlockResult) throws IOException {
int character;
int errorCount = 0;
Timer timer = new Timer(BLOCK_TIMEOUT);
while (true) {
timer.start();
try {
while (true) {
character = readByte(timer);
if (character == SOH || character == STX || character == EOT) {
return character;
}
}
} catch (TimeoutException ignored) {
// repeat last block result and wait for next block one more time
if (++errorCount < MAX_ERRORS) {
sendByte(lastBlockResult ? ACK : NAK);
} else {
interruptTransmission();
throw new RuntimeException("Timeout, no data received from transmitter");
}
}
}
}
private void shortSleep() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
try {
interruptTransmission();
} catch (IOException ignore) {
}
throw new RuntimeException("Transmission was interrupted", e);
}
}
/**
* send CAN to interrupt seance
*
* @throws java.io.IOException
*/
protected void interruptTransmission() throws IOException {
sendByte(CAN);
sendByte(CAN);
}
protected byte[] readBlock(int blockNumber, boolean shortBlock, CRC crc) throws IOException, TimeoutException, RepeatedBlockException, SynchronizationLostException, InvalidBlockException {
byte[] block;
Timer timer = new Timer(BLOCK_TIMEOUT).start();
if (shortBlock) {
block = shortBlockBuffer;
} else {
block = longBlockBuffer;
}
byte character;
character = readByte(timer);
if (character == blockNumber - 1) {
// this is repeating of last block, possible ACK lost
throw new RepeatedBlockException();
}
if (character != blockNumber) {
// wrong block - fatal loss of synchronization
throw new SynchronizationLostException();
}
character = readByte(timer);
if (character != ~blockNumber) {
throw new InvalidBlockException();
}
// data
for (int i = 0; i < block.length; i++) {
block[i] = readByte(timer);
}
while (true) {
if (inputStream.available() >= crc.getCRCLength()) {
if (crc.calcCRC(block) != readCRC(crc)) {
throw new InvalidBlockException();
}
break;
}
shortSleep();
if (timer.isExpired()) {
throw new TimeoutException();
}
}
return block;
}
private long readCRC(CRC crc) throws IOException {
long checkSum = 0;
for (int j = 0; j < crc.getCRCLength(); j++) {
checkSum = (checkSum << 8) + inputStream.read();
}
return checkSum;
}
private byte readByte(Timer timer) throws IOException, TimeoutException {
while (true) {
if (inputStream.available() > 0) {
int b = inputStream.read();
return (byte) b;
}
if (timer.isExpired()) {
throw new TimeoutException();
}
shortSleep();
}
}
class RepeatedBlockException extends Exception {
}
class SynchronizationLostException extends Exception {
}
class InvalidBlockException extends Exception {
}
}