| 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 |
#include "crc16.h" |
| 37 |
#include <sys.h> |
| 38 |
#include <uart.h> |
| 39 |
|
| 40 |
#define SOH 0x01 |
| 41 |
#define STX 0x02 |
| 42 |
#define EOT 0x04 |
| 43 |
#define ACK 0x06 |
| 44 |
#define NAK 0x15 |
| 45 |
#define CAN 0x18 |
| 46 |
#define CTRLZ 0x1A |
| 47 |
|
| 48 |
#define DLY_1S 1000 |
| 49 |
#define MAXRETRANS 1000 |
| 50 |
|
| 51 |
static int check(int crc, const unsigned char *buf, int sz) |
| 52 |
{ |
| 53 |
if (crc) { |
| 54 |
unsigned short crc = crc16_ccitt(buf, sz); |
| 55 |
unsigned short tcrc = (buf[sz]<<8)+buf[sz+1]; |
| 56 |
if (crc == tcrc) |
| 57 |
return 1; |
| 58 |
} |
| 59 |
else { |
| 60 |
int i; |
| 61 |
unsigned char cks = 0; |
| 62 |
for (i = 0; i < sz; ++i) { |
| 63 |
cks += buf[i]; |
| 64 |
} |
| 65 |
if (cks == buf[sz]) |
| 66 |
return 1; |
| 67 |
} |
| 68 |
|
| 69 |
return 0; |
| 70 |
} |
| 71 |
|
| 72 |
static void flushinput(void) |
| 73 |
{ |
| 74 |
while (_inbyte(((DLY_1S)*3)>>1) >= 0) |
| 75 |
; |
| 76 |
} |
| 77 |
|
| 78 |
int xmodemReceive(unsigned char *dest, int destsz) |
| 79 |
{ |
| 80 |
unsigned char xbuff[1030]; |
| 81 |
unsigned char *p; |
| 82 |
int bufsz, crc = 0; |
| 83 |
unsigned char trychar = 'C'; |
| 84 |
unsigned char packetno = 1; |
| 85 |
int i, c, len = 0; |
| 86 |
int retry, retrans = MAXRETRANS; |
| 87 |
|
| 88 |
for(;;) { |
| 89 |
for( retry = 0; retry < MAXRETRANS; ++retry) { |
| 90 |
if (trychar) _outbyte(trychar); |
| 91 |
if ((c = _inbyte((DLY_1S))) >= 0) { |
| 92 |
switch (c) { |
| 93 |
case SOH: |
| 94 |
bufsz = 128; |
| 95 |
goto start_recv; |
| 96 |
case STX: |
| 97 |
bufsz = 1024; |
| 98 |
goto start_recv; |
| 99 |
case EOT: |
| 100 |
flushinput(); |
| 101 |
_outbyte(ACK); |
| 102 |
return len; |
| 103 |
case CAN: |
| 104 |
if ((c = _inbyte(DLY_1S)) == CAN) { |
| 105 |
flushinput(); |
| 106 |
_outbyte(ACK); |
| 107 |
return -1; |
| 108 |
} |
| 109 |
break; |
| 110 |
default: |
| 111 |
break; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
if (trychar == 'C') { trychar = NAK; continue; } |
| 116 |
flushinput(); |
| 117 |
_outbyte(CAN); |
| 118 |
_outbyte(CAN); |
| 119 |
_outbyte(CAN); |
| 120 |
return -2; |
| 121 |
|
| 122 |
start_recv: |
| 123 |
if (trychar == 'C') crc = 1; |
| 124 |
trychar = 0; |
| 125 |
p = xbuff; |
| 126 |
*p++ = c; |
| 127 |
for (i = 0; i < (bufsz+(crc?1:0)+3); ++i) { |
| 128 |
if ((c = _inbyte(DLY_1S)) < 0) goto reject; |
| 129 |
*p++ = c; |
| 130 |
} |
| 131 |
|
| 132 |
if (xbuff[1] == (unsigned char)(~xbuff[2]) && |
| 133 |
(xbuff[1] == packetno || xbuff[1] == (unsigned char)packetno-1) && |
| 134 |
check(crc, &xbuff[3], bufsz)) { |
| 135 |
if (xbuff[1] == packetno) { |
| 136 |
|
| 137 |
|
| 138 |
if (bufsz > 0) { |
| 139 |
sys_memcpy (&dest[len], &xbuff[3], bufsz); |
| 140 |
len += bufsz; |
| 141 |
} |
| 142 |
++packetno; |
| 143 |
retrans = MAXRETRANS+1; |
| 144 |
} |
| 145 |
if (--retrans <= 0) { |
| 146 |
flushinput(); |
| 147 |
_outbyte(CAN); |
| 148 |
_outbyte(CAN); |
| 149 |
_outbyte(CAN); |
| 150 |
return -3; |
| 151 |
} |
| 152 |
_outbyte(ACK); |
| 153 |
continue; |
| 154 |
} |
| 155 |
reject: |
| 156 |
flushinput(); |
| 157 |
_outbyte(NAK); |
| 158 |
} |
| 159 |
} |