-
Notifications
You must be signed in to change notification settings - Fork 0
/
ymodem.c
493 lines (398 loc) · 11.7 KB
/
ymodem.c
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#ifdef __cplusplus
extern "C" {
#endif
#include "ymodem.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#if defined USE_TINYPRINTF
#include "tinyprintf.h"
#define printf tinyprintf
#define snprintf tinysnprintf
#endif
#define PACKET_DATA_INDEX (3)
#define PACKET_START_INDEX (0)
#define PACKET_NUMBER_INDEX (1)
#define PACKET_CNUMBER_INDEX (2)
#define SOH ((uint8_t)0x01) /* start of 128-byte data packet */
#define STX ((uint8_t)0x02) /* start of 1024-byte data packet */
#define EOT ((uint8_t)0x04) /* end of transmission */
#define ACK ((uint8_t)0x06) /* acknowledge */
#define NAK ((uint8_t)0x15) /* negative acknowledge */
#define CA ((uint32_t)0x18) /* two of these in succession aborts transfer */
#define CRC16 ((uint8_t)0x43) /* 'C' == 0x43, request 16-bit CRC */
#define NEGATIVE_BYTE ((uint8_t)0xFF)
#define ABORT1 ((uint8_t)0x41) /* 'A' == 0x41, abort by user */
#define ABORT2 ((uint8_t)0x61) /* 'a' == 0x61, abort by user */
#define DOWNLOAD_TIMEOUT ((uint32_t)1000) /* One second retry delay */
#define MAX_ERRORS ((uint32_t)10)
static YMODEM_STATUS_EN ymodem_WaitReceiverReady(YMODEM_HANDLER *ymodem);
static YMODEM_STATUS_EN ymodem_SendFileInfo(YMODEM_HANDLER *ymodem, FILE_INFO_ST *file_info);
static YMODEM_STATUS_EN ymodem_SendData(YMODEM_HANDLER *ymodem, uint8_t *data, size_t size);
static YMODEM_STATUS_EN ymodem_EndSend(YMODEM_HANDLER *ymodem);
static YMODEM_STATUS_EN ymodem_WaitFileInfo(YMODEM_HANDLER *ymodem, FILE_INFO_ST *file_info);
static YMODEM_STATUS_EN ymodem_ReceiveData(YMODEM_HANDLER *ymodem);
static YMODEM_STATUS_EN ymodem_EndReceive(YMODEM_HANDLER *ymodem);
static void ymodem_c(YMODEM_HANDLER *ymodem);
static void ymodem_ack(YMODEM_HANDLER *ymodem);
static void ymodem_ack_c(YMODEM_HANDLER *ymodem);
static void ymodem_nack(YMODEM_HANDLER *ymodem);
static void ymodem_abort(YMODEM_HANDLER *ymodem);
static void ymodem_eot(YMODEM_HANDLER *ymodem);
static uint16_t Cal_CRC16(const uint8_t* p_data, uint32_t size);
static uint8_t DataPacketCheck(uint8_t *packet, size_t packet_size);
void YMODEM_Init(YMODEM_HANDLER *ymodem, YMODE_DRIVER_ST *driver)
{
ymodem->read_block = driver->read_block;
ymodem->write = driver->write;
ymodem->fileinfo_handler = driver->fileinfo_handler;
ymodem->receive_data_handler = driver->receive_data_handler;
}
YMODEM_STATUS_EN YMODEM_SendFile(YMODEM_HANDLER *ymodem, FILE_INFO_ST *file_info, uint8_t *data, size_t size)
{
YMODEM_STATUS_EN Ret;
Ret = ymodem_WaitReceiverReady(ymodem);
if(YMODEM_ERROR == Ret)
goto error;
Ret = ymodem_SendFileInfo(ymodem, file_info);
if(YMODEM_ERROR == Ret)
goto error;
Ret = ymodem_SendData(ymodem, data, size);
if(YMODEM_ERROR == Ret)
goto error;
Ret = ymodem_EndSend(ymodem);
if(YMODEM_ERROR == Ret)
goto error;
error:
return Ret;
}
static YMODEM_STATUS_EN ymodem_WaitReceiverReady(YMODEM_HANDLER *ymodem)
{
uint32_t ErrorCount = 0;
for(;;)
{
if(ymodem->read_block(ymodem->data, PACKET_BUFF_SIZE, DOWNLOAD_TIMEOUT))
{
if('C' == ymodem->data[0])
{
return YMODEM_OK;
}
}
if(++ErrorCount > MAX_ERRORS)
{
return YMODEM_ERROR;
}
}
return YMODEM_ERROR;
}
static YMODEM_STATUS_EN ymodem_SendFileInfo(YMODEM_HANDLER *ymodem, FILE_INFO_ST *file_info)
{
memset(ymodem->data, 0x00, PACKET_OVERHEAD_SIZE + PACKET_SIZE_128B);
ymodem->data[PACKET_START_INDEX] = SOH;
ymodem->data[PACKET_NUMBER_INDEX] = 0x00;
ymodem->data[PACKET_CNUMBER_INDEX] = 0xFF;
strncpy((char*)ymodem->data + PACKET_DATA_INDEX, file_info->name, FILE_NAME_LENGTH);
ymodem->data[PACKET_DATA_INDEX + strlen(file_info->name)] = '\0';
snprintf((char*)ymodem->data + PACKET_DATA_INDEX + strlen(file_info->name) + 1, 128, "%zd", file_info->size);
uint16_t crc16 = Cal_CRC16(ymodem->data + PACKET_HEADER_SIZE, PACKET_SIZE_128B);
ymodem->data[PACKET_DATA_INDEX + PACKET_SIZE_128B] = crc16 >> 8;
ymodem->data[PACKET_DATA_INDEX + PACKET_SIZE_128B + 1] = crc16 & 0x00FF;
printf("send filename: %s, file size: %zd\r\n", file_info->name, file_info->size);
uint32_t ErrorCount = 0;
for(;;)
{
ymodem->write(ymodem->data, PACKET_OVERHEAD_SIZE + PACKET_SIZE_128B);
uint8_t ch[2];
if(ymodem->read_block(ch, 2, DOWNLOAD_TIMEOUT))
{
if(ACK == ch[0] && 'C' == ch[1])
{
return YMODEM_OK;
}
if(++ErrorCount > MAX_ERRORS)
{
return YMODEM_ERROR;
}
}
}
return YMODEM_ERROR;
}
static YMODEM_STATUS_EN ymodem_SendData(YMODEM_HANDLER *ymodem, uint8_t *data, size_t size)
{
uint32_t idx = 0;
size_t BytesRemained = size;
size_t PacketDataSize = 0;
ymodem->data[PACKET_START_INDEX] = SOH;
do
{
if(BytesRemained >= PACKET_SIZE_128B)
{
PacketDataSize = PACKET_SIZE_128B;
BytesRemained -= PACKET_SIZE_128B;
}
else
{
PacketDataSize = BytesRemained;
BytesRemained = 0;
}
++idx;
ymodem->data[PACKET_NUMBER_INDEX] = idx;
ymodem->data[PACKET_CNUMBER_INDEX] = ~idx;
memset(ymodem->data + PACKET_HEADER_SIZE, 0, PACKET_SIZE_128B);
memcpy(ymodem->data + PACKET_HEADER_SIZE, data + PACKET_SIZE_128B * (idx - 1), PacketDataSize);
uint16_t crc16 = Cal_CRC16(ymodem->data + PACKET_HEADER_SIZE, PACKET_SIZE_128B);
ymodem->data[PACKET_DATA_INDEX + PACKET_SIZE_128B] = crc16 >> 8;
ymodem->data[PACKET_DATA_INDEX + PACKET_SIZE_128B + 1] = crc16 & 0x00FF;
printf("send data packet: %u, %u\r\n", ymodem->data[0], ymodem->data[1]);
uint32_t ErrorCount = 0;
for(;;)
{
ymodem->write(ymodem->data, PACKET_OVERHEAD_SIZE + PACKET_SIZE_128B);
uint8_t ch;
if(ymodem->read_block(&ch, 1, DOWNLOAD_TIMEOUT))
{
if(ACK == ch)
{
break;
}
}
if(++ErrorCount > MAX_ERRORS)
{
return YMODEM_ERROR;
}
}
}
while(BytesRemained);
return YMODEM_OK;
}
static YMODEM_STATUS_EN ymodem_EndSend(YMODEM_HANDLER *ymodem)
{
printf("end send!\r\n");
uint32_t ErrorCount = 0;
for(;;)
{
ymodem_eot(ymodem);
uint8_t ch[2];
if(ymodem->read_block(ch, 2, DOWNLOAD_TIMEOUT))
{
if(ACK == ch[0] && 'C' == ch[1])
{
break;
}
}
if(++ErrorCount > MAX_ERRORS)
{
return YMODEM_ERROR;
}
}
ymodem->data[PACKET_NUMBER_INDEX] = 0x00;
ymodem->data[PACKET_CNUMBER_INDEX] = 0xFF;
memset(ymodem->data + PACKET_HEADER_SIZE, 0, PACKET_SIZE_128B);
uint16_t crc16 = Cal_CRC16(ymodem->data + PACKET_HEADER_SIZE, PACKET_SIZE_128B);
ymodem->data[PACKET_DATA_INDEX + PACKET_SIZE_128B] = crc16 >> 8;
ymodem->data[PACKET_DATA_INDEX + PACKET_SIZE_128B + 1] = crc16 & 0x00FF;
ErrorCount = 0;
for(;;)
{
ymodem->write(ymodem->data, PACKET_OVERHEAD_SIZE + PACKET_SIZE_128B);
uint8_t ch[1];
if(ymodem->read_block(ch, 2, DOWNLOAD_TIMEOUT))
{
if(ACK == ch[0])
{
break;
}
}
if(++ErrorCount > MAX_ERRORS)
{
return YMODEM_ERROR;
}
}
return YMODEM_OK;
}
YMODEM_STATUS_EN YMODEM_ReceiveFile(YMODEM_HANDLER *ymodem, FILE_INFO_ST *file_info)
{
YMODEM_STATUS_EN Ret;
Ret = ymodem_WaitFileInfo(ymodem, file_info);
if(YMODEM_ERROR == Ret)
goto error;
else if(YMODEM_TIMEOUT == Ret)
return Ret;
Ret = ymodem_ReceiveData(ymodem);
if(YMODEM_ERROR == Ret)
goto error;
Ret = ymodem_EndReceive(ymodem);
if(YMODEM_ERROR == Ret)
goto error;
return Ret;
error:
ymodem_abort(ymodem);
return Ret;
}
static YMODEM_STATUS_EN ymodem_WaitFileInfo(YMODEM_HANDLER *ymodem, FILE_INFO_ST *file_info)
{
uint_fast8_t ErrorCount = 0;
ymodem_c(ymodem);
while(!ymodem->read_block(ymodem->data, PACKET_BUFF_SIZE, DOWNLOAD_TIMEOUT))
{
printf("wait file info...\r\n");
if(ErrorCount++ > MAX_ERRORS)
return YMODEM_TIMEOUT;
ymodem_c(ymodem);
}
printf("%02X %02X\r\n", ymodem->data[0], ymodem->data[1]);
if(SOH == ymodem->data[PACKET_START_INDEX])
{
char *p = (char*)ymodem->data + PACKET_DATA_INDEX;
strncpy(file_info->name, p, FILE_NAME_LENGTH);
file_info->size = strtoul(p + strlen(p) + 1, NULL, 10);
printf("receive filename: %s, file size: %zd\r\n", file_info->name, file_info->size);
if(ymodem->fileinfo_handler)
ymodem->fileinfo_handler(file_info);
ymodem_ack_c(ymodem);
return YMODEM_OK;
}
return YMODEM_ERROR;
}
static YMODEM_STATUS_EN ymodem_ReceiveData(YMODEM_HANDLER *ymodem)
{
uint32_t packet_idx = 0;
size_t packet_size;
size_t packet_offset = 0;
uint32_t ErrorCount = 0;
for(;;)
{
if(ymodem->read_block(ymodem->data, PACKET_BUFF_SIZE, DOWNLOAD_TIMEOUT))
{
printf("%02X %02X\r\n", ymodem->data[0], ymodem->data[1]);
if(SOH == ymodem->data[PACKET_START_INDEX])
{
packet_size = PACKET_SIZE_128B;
}
else if(STX == ymodem->data[PACKET_START_INDEX])
{
packet_size = PACKET_SIZE_1KB;
}
else if(EOT == ymodem->data[PACKET_START_INDEX])
{
ymodem_ack_c(ymodem);
return YMODEM_OK;
}
else
{
goto packet_error;
}
if(++packet_idx != ymodem->data[PACKET_NUMBER_INDEX])
{
goto packet_error;
}
if(DataPacketCheck(ymodem->data, packet_size))
{
ErrorCount = 0;
if(ymodem->receive_data_handler)
ymodem->receive_data_handler(packet_offset, ymodem->data + PACKET_DATA_INDEX, packet_size);
packet_offset += packet_size;
}
else
{
goto packet_error;
}
ymodem_ack(ymodem);
}
packet_error:
if(++ErrorCount > MAX_ERRORS)
{
return YMODEM_ERROR;
}
}
return YMODEM_ERROR;
}
static YMODEM_STATUS_EN ymodem_EndReceive(YMODEM_HANDLER *ymodem)
{
uint32_t ErrorCount = 0;
for(;;)
{
if(ymodem->read_block(ymodem->data, PACKET_BUFF_SIZE, DOWNLOAD_TIMEOUT))
{
ymodem_ack(ymodem);
return YMODEM_OK;
}
if(++ErrorCount > MAX_ERRORS)
{
return YMODEM_ERROR;
}
}
return YMODEM_ERROR;
}
static void ymodem_putc(YMODEM_HANDLER *ymodem, char ch)
{
ymodem->write((uint8_t*)&ch, 1);
}
static void ymodem_c(YMODEM_HANDLER *ymodem)
{
ymodem_putc(ymodem, 'C');
}
static void ymodem_ack(YMODEM_HANDLER *ymodem)
{
ymodem_putc(ymodem, ACK);
}
static void ymodem_ack_c(YMODEM_HANDLER *ymodem)
{
ymodem_putc(ymodem, ACK);
ymodem_putc(ymodem, 'C');
}
static void ymodem_eot(YMODEM_HANDLER *ymodem)
{
ymodem_putc(ymodem, EOT);
}
static void ymodem_nack(YMODEM_HANDLER *ymodem)
{
ymodem_putc(ymodem, NAK);
}
static void ymodem_abort(YMODEM_HANDLER *ymodem)
{
uint8_t i = 8;
while(i--)
ymodem_putc(ymodem, CA);
}
static uint16_t UpdateCRC16(uint16_t crc_in, uint8_t byte)
{
uint32_t crc = crc_in;
uint32_t in = byte | 0x100;
do
{
crc <<= 1;
in <<= 1;
if(in & 0x100)
++crc;
if(crc & 0x10000)
crc ^= 0x1021;
}
while(!(in & 0x10000));
return crc & 0xffffu;
}
static uint16_t Cal_CRC16(const uint8_t* p_data, uint32_t size)
{
uint32_t crc = 0;
const uint8_t* dataEnd = p_data+size;
while(p_data < dataEnd)
crc = UpdateCRC16(crc, *p_data++);
crc = UpdateCRC16(crc, 0);
crc = UpdateCRC16(crc, 0);
return crc&0xffffu;
}
static uint8_t DataPacketCheck(uint8_t *packet, size_t packet_size)
{
if(!((SOH == packet[PACKET_START_INDEX]) || (STX == packet[PACKET_START_INDEX])))
return 0;
if(packet[PACKET_NUMBER_INDEX] != (packet[PACKET_CNUMBER_INDEX] ^ 0xFF))
return 0;
uint16_t crc = packet[packet_size + PACKET_DATA_INDEX] << 8;
crc += packet[packet_size + PACKET_DATA_INDEX + 1];
if(crc != Cal_CRC16(packet + PACKET_DATA_INDEX, packet_size))
return 0;
return 1;
}
#ifdef __cplusplus
}
#endif