Debian Codegraph @

src/driver/xmodem/xmodem.c

raw symbols SCIP
1 /*
2 * Copyright 2001-2019 Georges Menie (www.menie.org)
3 * All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /* this code needs standard functions memcpy() and memset()
29 and input/output functions _inbyte() and _outbyte().
30
31 the prototypes of the input/output functions are:
32 int _inbyte(unsigned short timeout); // msec timeout
33 void _outbyte(int c);
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]; /* 1024 for XModem 1k + 3 head chars + 2 crc + nul */
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; /* normal end */
103 case CAN:
104 if ((c = _inbyte(DLY_1S)) == CAN) {
105 flushinput();
106 _outbyte(ACK);
107 return -1; /* canceled by remote */
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; /* sync error */
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 // register int count = destsz - len;
137 // if (count > bufsz) count = bufsz;
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; /* too many retry error */
151 }
152 _outbyte(ACK);
153 continue;
154 }
155 reject:
156 flushinput();
157 _outbyte(NAK);
158 }
159 }