-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathxmodem.cpp
More file actions
255 lines (214 loc) · 3.75 KB
/
xmodem.cpp
File metadata and controls
255 lines (214 loc) · 3.75 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
#include "xmodem.h"
XModem::XModem(Stream & terminal)
{
this->retries = 10;
this->terminal = &terminal;
reset();
}
void XModem::reset(void)
{
this->block = 0;
}
uint8_t XModem::octet_available(void)
{
return terminal->available() > 0;
}
void XModem::crc_update(uint8_t octet)
{
crc ^= (uint16_t)octet << 8;
for (uint8_t i = 0; i < 8; i++) {
if (crc & 0x8000) {
crc = crc << 1 ^ 0x1021;
}
else {
crc <<= 1;
}
}
}
void XModem::octet_send(uint8_t octet)
{
terminal->write(octet);
}
uint8_t XModem::octet_receive(void)
{
#define DELAY_TIMEOUT ((16000000 / (100)) * 7) // 7 seconds timeout
#if DELAY_TIMEOUT > 0xFFFFFFFFull
#error TIMEOUT overflow
#endif
uint32_t timeout = DELAY_TIMEOUT;
#undef DELAY_TIMEOUT
while (--timeout) {
if (octet_available()) {
uint8_t octet = terminal->read();
timeout_flag = 0;
return octet;
}
}
timeout_flag = 1;
return 0;
}
enum XModem::block_result XModem::start_send(void)
{
int last = retries;
reset();
do {
uint8_t octet = octet_receive();
if (!timeout_flag) {
if (XSTART == octet) {
return START;
}
else {
return CANCEL;
}
}
} while (--last);
return TIMEOUT;
}
enum XModem::block_result XModem::finish_send(void)
{
uint8_t octet;
octet_send(EOT);
octet = octet_receive();
if (timeout_flag) {
return TIMEOUT;
}
if (ACK != octet) {
return CANCEL;
}
octet_send(ETB);
octet_receive();
return END;
}
enum XModem::block_result XModem::block_send(uint8_t *dst)
{
int last = retries;
uint8_t i, octet;
start:
crc = 0;
octet_send(SOH);
// octets are numbered from 1
octet = (uint8_t)block + 1;
octet_send(octet);
octet_send(255 - octet);
for (i = 0; i < XMODEM_BLOCK_SIZE; i++) {
uint8_t octet = *dst++;
octet_send(octet);
crc_update(octet);
if (octet_available()) {
if (terminal->read() == CAN) {
return CANCEL;
}
}
}
octet_send(crc >> 8);
octet_send(crc);
octet = octet_receive();
if ((octet == NACK) && (--last)) {
goto start;
}
if (octet != ACK) {
if (octet == CAN) {
return CANCEL;
}
return ERROR;
}
block++;
return NEXT;
}
enum XModem::block_result XModem::block_receive(uint8_t *dst)
{
int last = retries;
uint8_t i, octet, receive;
uint8_t preamble = block ? ACK : XSTART;
start:
// for the first block send 'C', for others send ACK
octet_send(preamble);
crc = 0;
octet = octet_receive();
if (timeout_flag) {
if (--last) {
// wait for sender/request resend
goto start;
}
return TIMEOUT;
}
// if done
if (EOT == octet) {
octet_send(ACK);
return END;
}
// if cancelled
if (CAN == octet) {
return CANCEL;
}
// skip spoiled packet
if (SOH != octet) {
skip_nak:
preamble = NACK;
do {
octet_receive();
} while (!timeout_flag);
goto start;
}
// get block number
i = octet_receive();
if (timeout_flag) {
restart:
if (--last) {
// wait for sender/request resend
preamble = NACK;
goto start;
}
return TIMEOUT;
}
// get block number complement
octet = octet_receive();
if (timeout_flag) {
goto restart;
}
// check valid value
if ((uint8_t)(255 - octet) != i) {
goto skip_nak;
}
// check block number
if (i == (uint8_t)block) {
receive = 0;
}
else if (i == (uint8_t)(block + 1)) {
receive = 1;
}
else {
return CANCEL;
}
// receive data
for (i = 0; i < XMODEM_BLOCK_SIZE; i++) {
octet = octet_receive();
crc_update(octet);
if (timeout_flag) {
goto restart;
}
if (receive) {
*dst++ = octet;
}
}
// receive checksum
uint16_t line_crc = octet_receive() << 8;
if (timeout_flag) {
goto restart;
}
line_crc |= octet_receive();
if (timeout_flag) {
goto restart;
}
// compare checksum
if (crc != line_crc) {
goto skip_nak;
}
preamble = ACK;
if (!receive) {
goto start;
}
block++;
last = retries;
return NEXT;
}