[go: up one dir, main page]

Menu

[r1]: / xmodem.c  Maximize  Restore  History

Download this file

564 lines (500 with data), 12.0 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
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/**************************************************************************
*
* Written by Cencong Su (cencong.su@gmail.com)
*
***************************************************************************
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above author notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above author notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
**********************************************************************//*!
*
* @file xmodem.c
*
* @author Cencong Su
*
* @brief Implement xmodem protocol
*
* @details This module needs the following libray functions:
* 1> memcpy
* 2> memset
* 3> malloc and free
* Target dependent code is marked with special comment "Target Dependent :"
*
*
***************************************************************************/
#include <common.h>
#include <stdlib.h>
#include "xmodem.h"
#include "crc16.h"
/** Target dependent : header files */
#include "fnet_serial.h"
/*-----------------------------------------------------------------------------------*/
/* -----------------------------Target Dependent : code ---------------------------*/
/**
* Initiate the xmodem module, serial port etc.
* Leave empty here
*
* @param NONE
* @return NONE
*/
void xmodem_init()
{
}
/**
* Get one byte from xmodem serial port
*
* @param[in] timeout timeout value for getting one byte from
* serial port unit:ms
*
* @retval >0 data from xmodem serial port, other receive wrong
*/
int getbyte_timeout(uint16_t timeout)
{
int c = 0, len;
uint32_t time_cnt = fnet_timer_ms();
while ((fnet_timer_ms() - time_cnt) < timeout)
{
c = fnet_getchar();
if (c >= 0)
{
return c;
}
}
return -1;
}
/**
* Send one byte to xmodem serial port
*
* @param[in] ch data sent out
*
* @return None
*/
void sendbyte(int ch)
{
fnet_putchar(ch);
}
/**
* flush serial port buffer
*
* @param NONE
* @return NONE
*/
void flush_input()
{
while(getbyte_timeout(DLY_1S/3) >= 0);
}
/**
* Send out a group of data
* Implement this function here is because some platform will provide
* similar function to send continuous serial data
*
* @param data data address
* @param len data length
*
* @return NONE
*/
void xmodem_send_data(uint8_t * data, uint16_t len)
{
uint16_t i;
for (i = 0; i < len; i++)
{
sendbyte(data[i]);
}
}
/* -------------------Target dependent code END --------------------*/
/*-----------------------------------------------------------------------------------*/
/**
* Cancel xmodem transmission
*
* @param NONE
* @return NONE
*/
void xmodem_cancel()
{
sendbyte(CAN);
sendbyte(CAN);
sendbyte(CAN);
}
/**
* Receive one xmodem packet with timeout
*
* @param[out] dest address of memory to place received data
* @param[in] timeout keep trying to get data until timeout
* @param[in] crc_flag mechanism to check data integrity, 1:CRC, 0:Check sum
*
* @return Received data length
*/
uint16_t xmodem_recv_pkt(uint8_t *dest, uint32_t timeout, uint8_t crc_flag)
{
int ch;
uint16_t cnt = 0;
ch = getbyte_timeout(timeout);
if(ch >= 0)
{
dest[cnt++] = ch;
while(cnt < ((crc_flag) ? sizeof(XMODEM_PACKET) : (sizeof(XMODEM_PACKET) - 1)))
{
//Try to receive one byte
ch = getbyte_timeout(DLY_1S);
if (ch >= 0)
{
dest[cnt++] = ch;
}
else
{
/*If the no data receive then keep trying to get data until timeout,
If data already arrives, then received char<0 means end of packet,
return received number*/
return cnt;
}
}
return cnt;
}
else
{
return 0;
}
}
/**
* Check xmodem packet integrity
*
* @param[in] crc_flag Check mechanism, 1:CRC, 0:Check sum
* @param[in] pkt xmodem packet
*
* @return TRUE: data is OK, FALSE: data wrong
*/
bool xmodem_check(uint8_t crc_flag, XMODEM_PACKET_PTR pkt)
{
uint16 i, crc;
uint8_t cks = 0;
if (crc_flag)
{
crc = crc16_ccitt(pkt->data, PACKET_LEN);
hsb2lsb(crc);
if (crc == pkt->chk_code.crc)
{
return TRUE;
}
}
else
{
for (i = 0; i < PACKET_LEN; ++i)
{
cks += pkt->data[i];
}
if (cks == pkt->chk_code.chksum)
{
return TRUE;
}
}
return FALSE;
}
/**
* Parse one xmodem packet
*
* @param[in] data xmodem packet address
* @param[in] len packet length
* @param[in] crc_flag Data check mechanism, 1: CRC, 0: Check sum
*
* @ret PARS_OK Data is ok
* @ret PARS_NAK Need to response with "NAK"
* @ret PARS_EOT Receive "EOT"
* @ret PARS_CAN Receive "CAN"
*
* @see xmodem_parse_pkt_ret
*/
xmodem_parse_pkt_ret xmodem_parse_pkt(uint8_t * data, uint32_t len,
uint8_t crc_flag, uint8_t recv_pkt_no)
{
XMODEM_PACKET_PTR pkt = (XMODEM_PACKET_PTR) data;
switch (data[0])
{
case SOH:
if ((pkt->packet_num == recv_pkt_no)
&& (pkt->pkt_num_cmplemt == (uint8_t) ~recv_pkt_no)
&& (len == ((crc_flag) ?sizeof(XMODEM_PACKET) : (sizeof(XMODEM_PACKET) - 1)))
&& (xmodem_check(crc_flag, pkt) == 1))
{
return PARS_OK;
}
else
{
return PARS_ERR;
}
break;
case EOT:
return PARS_EOT;
case CAN:
return PARS_CAN;
default:
return PARS_ERR;
}
}
/**
* xmodem receive entry
*
* @param[in] dest destination address to store received data
* @param[in] destsz destination area size
*
* @return actual data size copied to destination
*/
uint32_t xmodem_recv(uint8_t *dest, uint32_t destsz)
{
XMODEM_PACKET_PTR xdm_pkt;
uint8_t crc_flag = 1, i, recv_pkt_no = 1;
uint32_t recv_len, dest_index = 0, destsz_left = 0;
xmodem_parse_pkt_ret xdmParsRet;
flush_input();
xdm_pkt = (XMODEM_PACKET_PTR) malloc(sizeof(XMODEM_PACKET));
if (xdm_pkt == NULL)
{
printf("Can't allocate memory for new xmodem packet in %s!\n",
__FUNCTION__);
goto RETURN;
}
//Send out first byte and try to receive the acknowledge packet
for (i = 0; i < MAX_RETRANS; i++)
{
//Try to communicate with remote: send 'C' -> NAK -> 'C' -> NAK...
sendbyte((crc_flag) ? USE_CRC : NAK);
recv_len = xmodem_recv_pkt((uint8_t *) xdm_pkt, (2 * DLY_1S), crc_flag);
if (recv_len > 0)
{
//Tera term first xmodem packet will contain wrong data
//flush serial port buffer to be compatible with teara term
flush_input();
break;
}
else
{
if( i == (MAX_RETRANS/2))
{
crc_flag = !crc_flag;
}
//Already try the maximum times to communicate with remote, cancel link
if (i == (MAX_RETRANS - 1))
{
xmodem_cancel();
goto RETURN;
}
}
}
while (1)
{
xdmParsRet = xmodem_parse_pkt((uint8_t *) xdm_pkt, recv_len, crc_flag,
recv_pkt_no);
switch (xdmParsRet)
{
case PARS_OK:
recv_pkt_no++;
destsz_left = destsz - dest_index;
if (destsz_left < PACKET_LEN)
{
memcpy(dest + dest_index, xdm_pkt->data, destsz_left);
dest_index += destsz_left;
xmodem_cancel();
goto RETURN;
}
else
{
memcpy(dest + dest_index, xdm_pkt->data, PACKET_LEN);
dest_index += PACKET_LEN;
sendbyte(ACK);
}
break;
case PARS_CAN:
sendbyte(ACK);
goto RETURN;
case PARS_EOT:
sendbyte(ACK);
goto RETURN;
case PARS_ERR:
default:
flush_input();
sendbyte(NAK);
break;
}
recv_len = xmodem_recv_pkt((uint8_t *) xdm_pkt, (10 * DLY_1S),
crc_flag);
if (recv_len <= 0)
{
xmodem_cancel();
goto RETURN;
}
}
RETURN:
free(xdm_pkt);
return dest_index;
}
/**
* Start xmodem transmit
*
* @param[in] crc_flag check data integrity method: 1 - crc, 0 - checksum
* @param[in] data_src address of data be sent
* @param[in] data_sz size of data be sent
*
* @ret >0 actual sent data length
* @ret -1 remote cancel
* @ret -2 communication timeout
*
* @see REMOTE_CANCEL,COM_TIMEOUT
*/
int xmodem_start_trans(uint8_t crc_flag, uint8_t *dat_src, int data_sz)
{
XMODEM_PACKET xmodem_pkt;
uint8_t packetno = 1, i;
int sent_len = 0, left_num;
int ch;
START_TRANS:
memset(&xmodem_pkt, 0, sizeof(xmodem_pkt));
left_num = data_sz - sent_len;
xmodem_pkt.soh = SOH;
xmodem_pkt.packet_num = packetno;
xmodem_pkt.pkt_num_cmplemt = ~packetno;
//Still have left bytes to send
if (left_num > 0)
{
if (left_num >= PACKET_LEN)
{
memcpy(xmodem_pkt.data, &dat_src[sent_len], PACKET_LEN);
}
else if (left_num < PACKET_LEN)
{
//The remain data can't fill one packet, so fill it with "CTRL-Z(0x1a)"
memcpy(xmodem_pkt.data, &dat_src[sent_len], left_num);
memset(xmodem_pkt.data + left_num, CTRLZ, PACKET_LEN - left_num);
}
//Calculate check code, checksum/CRC
if (crc_flag)
{
xmodem_pkt.chk_code.crc = crc16_ccitt(xmodem_pkt.data, PACKET_LEN);
//Exchange the lsb and hsb byte, hsb output first
hsb2lsb(xmodem_pkt.chk_code.crc);
}
else
{
for (i = 0; i < PACKET_LEN; i++)
{
xmodem_pkt.chk_code.chksum += xmodem_pkt.data[i];
}
}
//Transmit the packet
for (i = 0; i < MAX_RETRANS; i++)
{
xmodem_send_data((uint8_t *) &xmodem_pkt,
sizeof(xmodem_pkt) - (crc_flag ? 0 : 1));
//Read response byte from remote
if ((ch = getbyte_timeout(DLY_1S * 10)) >= 0)
{
switch (ch)
{
case ACK:
packetno++;
sent_len += PACKET_LEN;
goto START_TRANS;
case CAN:
if ((ch = getbyte_timeout(DLY_1S)) == CAN)
{
sendbyte(ACK);
return REMOTE_CANCEL; /* canceled by remote */
}
break;
case NAK:
default:
break;
}
}
if(i == MAX_RETRANS -1)
{
//Has retransmit the maximum times without response from remote
xmodem_cancel();
return COM_TIMEOUT;
}
}
}
else
{
for (i = 0; i < MAX_RETRANS; i++)
{
sendbyte(EOT);
if ((ch = getbyte_timeout((DLY_1S) << 1)) == ACK)
break;
}
return (ch == ACK) ? sent_len : COM_TIMEOUT;
}
}
/**
* Transmit entry
*
* @param[in] src address of data be sent
* @param[in] srcsz size of data be sent
*
* @ret >0 actual sent data length
* @ret -1 remote cancel
* @ret -2 communication timeout
*
* @see REMOTE_CANCEL,COM_TIMEOUT
*/
int xmodemTransmit(unsigned char *src, int srcsz)
{
int ch = 0;
flush_input();
//Try to receive the first byte from remote
ch = getbyte_timeout(DLY_1S * 30);
if (ch >= 0)
{
switch (ch)
{
case USE_CRC:
return xmodem_start_trans(1, src, srcsz);
case NAK:
return xmodem_start_trans(0, src, srcsz);
case CAN:
if ((ch = getbyte_timeout(DLY_1S)) == CAN)
{
//Canceled by remote
sendbyte(ACK);
return REMOTE_CANCEL;
}
break;
default:
printf("Unrecognized xmodem command byte: %d\n", ch);
break;
}
}
xmodem_cancel();
return COM_TIMEOUT; /* no sync */
}
/**
* Xmodem module test sample
*
* @param NONE
*
* @return NONE
*/
void xmodem_test()
{
int st;
uint8_t buf[128];
st = xmodem_recv(buf, 128);
xmodemTransmit(buf, st);
}