-
Notifications
You must be signed in to change notification settings - Fork 12
/
xmodem.c
268 lines (213 loc) · 5.11 KB
/
xmodem.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
/* Basic XMODEM implementation for Raspberry Pi Pico
*
* This only implements the old-school checksum, not CRC,
* and only supports 128-byte blocks. But the whole point
* of XMODEM was to be simple, and this works fine for ~32K
* transfers over USB-serial.
*/
#include "xmodem.h"
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
const int XMODEM_SOH = 1;
const int XMODEM_EOT = 4;
const int XMODEM_ACK = 6;
const int XMODEM_DLE = 0x10;
const int XMODEM_NAK = 0x15;
const int XMODEM_CAN = 0x18;
const int XMODEM_BLOCKSIZE = 128;
xmodem_config_t xmodem_config =
{
1, /* logLevel */
true, /* useCrc */
false /* useEscape */
};
static char gLogBuffer[65536];
static int gLogPos = 0;
static void xmodem_log(char *s)
{
if (gLogPos + strlen(s) + 2 >= sizeof gLogBuffer)
{
gLogPos = sizeof gLogBuffer;
return;
}
strcpy(gLogBuffer + gLogPos, s);
gLogPos += strlen(s);
gLogBuffer[gLogPos++] = '\n';
gLogBuffer[gLogPos] = 0;
}
void xmodem_dumplog()
{
if (gLogPos)
{
puts(gLogBuffer);
}
}
static void xmodem_clearlog()
{
gLogPos = 0;
gLogBuffer[gLogPos] = 0;
}
void xmodem_set_config(xmodem_mode_t mode)
{
bzero(&xmodem_config, sizeof xmodem_config);
switch (mode)
{
case XMODEM_MODE_ORIGINAL:
xmodem_config.useEscape = false;
xmodem_config.useCrc = false;
break;
case XMODEM_MODE_CRC:
xmodem_config.useEscape = false;
xmodem_config.useCrc = true;
break;
}
}
int xmodem_receive(void* outputBuffer, size_t bufferSize, const char* message, bool (*inputhandler)())
{
char logBuffer[1024];
xmodem_clearlog();
int sizeReceived = 0;
int packetNumber = 1;
bool eof = false;
bool can = false;
bool error = false;
/* Receive a file */
while (true)
{
absolute_time_t nextPrintTime = get_absolute_time();
/* Receive next packet */
while (true)
{
if (sizeReceived == 0 && absolute_time_diff_us(nextPrintTime, get_absolute_time()) > 0)
{
xmodem_dumplog();
if (message) puts(message);
if (xmodem_config.useCrc)
{
putchar(8);
putchar('C');
}
else
{
putchar(XMODEM_NAK);
}
nextPrintTime = make_timeout_time_ms(3000);
}
int c = getchar_timeout_us(1000);
if (c == PICO_ERROR_TIMEOUT) continue;
if (c == XMODEM_EOT || c == XMODEM_SOH || c == XMODEM_CAN)
{
eof = (c == XMODEM_EOT);
can = (c == XMODEM_CAN);
break;
}
else if (inputhandler && inputhandler(c))
{
return -1;
}
else if (xmodem_config.logLevel >= 1)
{
sprintf(logBuffer, "Unexected character %d received - expected SOH or EOT", c);
xmodem_log(logBuffer);
}
}
if (eof)
{
if (xmodem_config.logLevel >= 2) xmodem_log("EOT => ACK");
putchar(XMODEM_ACK);
break;
}
if (can)
{
if (xmodem_config.logLevel >= 1) xmodem_log("CAN => ACK");
putchar(XMODEM_ACK);
break;
}
if (xmodem_config.logLevel >= 2)
{
sprintf(logBuffer, "Got SOH for packet %d", packetNumber);
xmodem_log(logBuffer);
}
if (sizeReceived + XMODEM_BLOCKSIZE > bufferSize)
{
error = true;
xmodem_log("Output buffer full");
for (int i = 0; i < 8; ++i)
putchar(XMODEM_CAN);
while (getchar_timeout_us(1000) != PICO_ERROR_TIMEOUT);
break;
}
bool timeout = false;
absolute_time_t timeoutTime = make_timeout_time_ms(1000);
int checksum = 0;
bool escape = false;
char buffer[2+XMODEM_BLOCKSIZE+2];
int bufpos = 0;
while (bufpos < 2+XMODEM_BLOCKSIZE + (xmodem_config.useCrc ? 2 : 1) && !timeout)
{
if (absolute_time_diff_us(timeoutTime, get_absolute_time()) > 0)
{
if (xmodem_config.logLevel >= 1) xmodem_log("Timeout");
timeout = true;
break;
}
int c = getchar_timeout_us(1000);
if (c == PICO_ERROR_TIMEOUT) continue;
if (xmodem_config.logLevel >= 3)
{
sprintf(logBuffer, "Got %d", c);
xmodem_log(logBuffer);
}
bool isData = (bufpos >= 2) && (bufpos < 2+XMODEM_BLOCKSIZE);
if (xmodem_config.useEscape && isData && c == XMODEM_DLE)
{
escape = true;
continue;
}
if (escape) c ^= 0x40;
escape = false;
buffer[bufpos++] = c;
if (isData)
{
if (xmodem_config.useCrc)
{
checksum = checksum ^ (int)c << 8;
for (int i = 0; i < 8; ++i)
if (checksum & 0x8000)
checksum = checksum << 1 ^ 0x1021;
else
checksum = checksum << 1;
}
else
{
checksum += c;
}
}
}
bool wrongPacket = (buffer[0] != (char)packetNumber);
bool badPacketInv = (buffer[1] != (char)(255-buffer[0]));
bool badChecksum = (buffer[2+XMODEM_BLOCKSIZE] != (char)checksum);
if (xmodem_config.useCrc)
{
badChecksum = (buffer[2+XMODEM_BLOCKSIZE] != (char)(checksum>>8))
|| (buffer[2+XMODEM_BLOCKSIZE+1] != (char)checksum);
}
if (timeout || wrongPacket || badPacketInv || badChecksum)
{
if (xmodem_config.logLevel >= 1) xmodem_log("NAK");
putchar(XMODEM_NAK);
continue;
}
if (xmodem_config.logLevel >= 2) xmodem_log("ACK");
putchar(XMODEM_ACK);
memcpy(outputBuffer+sizeReceived, buffer+2, XMODEM_BLOCKSIZE);
sizeReceived += XMODEM_BLOCKSIZE;
packetNumber++;
}
puts("");
xmodem_dumplog();
xmodem_clearlog();
if (can || error) return -1;
return sizeReceived;
}