forked from lurk101/pshell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxreceive.c
129 lines (115 loc) · 3.16 KB
/
xreceive.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
/* vi: set sw=4 ts=4: */
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include "crc16.h"
#include "xcommon.h"
#include "xreceive.h"
static xmodem_cb_t callback = NULL;
int getbyte(uint32_t timeout) {
int c = getchar_timeout_us(timeout * 1000);
if (c == PICO_ERROR_TIMEOUT)
return -1;
return c;
}
void putCAN(void) {
for (int n = 0; n < 3; n++)
putchar(CAN);
}
static bool check(bool crc, const uint8_t* buf, int sz) {
if (crc) {
uint16_t crc = crc16_ccitt(buf, sz);
uint16_t tcrc = (buf[sz] << 8) + buf[sz + 1];
if (crc == tcrc)
return true;
} else {
uint8_t cks = 0;
for (int i = 0; i < sz; ++i)
cks += buf[i];
if (cks == buf[sz])
return true;
}
return false;
}
void flushreceive(void) {
while (getbyte(((DLY_1S)*3) >> 1) >= 0)
;
}
static uint8_t xbuff[1030]; /* 1024 for XModem 1k + 3 head chars + 2 crc + nul */
int xmodemReceive(xmodem_cb_t cb) {
uint8_t* p;
int bufsz;
bool crc = false;
uint8_t trychar = 'C';
uint8_t packetno = 1;
int i, c, len = 0;
int retry, retrans = MAXRETRANS;
callback = cb;
for (;;) {
for (retry = 0; retry < 16; ++retry) {
if (trychar)
putchar(trychar);
if ((c = getbyte((DLY_1S)*2)) >= 0) {
switch (c) {
case SOH:
bufsz = 128;
goto start_recv;
case STX:
bufsz = 1024;
goto start_recv;
case EOT:
flushreceive();
putchar(ACK);
return len; /* normal end */
case CAN:
if ((c = getbyte(DLY_1S)) == CAN) {
flushreceive();
putchar(ACK);
return -1; /* canceled by remote */
}
break;
default:
break;
}
}
}
if (trychar == 'C') {
trychar = NAK;
continue;
}
flushreceive();
putCAN();
return -2; /* sync error */
start_recv:
if (trychar == 'C')
crc = true;
trychar = 0;
p = xbuff;
*p++ = c;
for (i = 0; i < (bufsz + (crc ? 1 : 0) + 3); ++i) {
if ((c = getbyte(DLY_1S)) < 0)
goto reject;
*p++ = c;
}
if (xbuff[1] == (uint8_t)(~xbuff[2]) &&
(xbuff[1] == packetno || xbuff[1] == (uint8_t)packetno - 1) &&
check(crc, &xbuff[3], bufsz)) {
if (xbuff[1] == packetno) {
callback(&xbuff[3], bufsz);
++packetno;
retrans = MAXRETRANS + 1;
}
if (--retrans <= 0) {
flushreceive();
putCAN();
return -3; /* too many retry error */
}
putchar(ACK);
continue;
}
reject:
flushreceive();
putchar(NAK);
}
}