summaryrefslogtreecommitdiffhomepage
path: root/digital/ucoolib/ucoolib/dev/xmodem/xmodem.cc
blob: 08e3b59cf3828b42e0a9522b381d8a0883b0d396 (plain)
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
// ucoolib - Microcontroller object oriented library. {{{
//
// Copyright (C) 2013 Nicolas Schodet
//
// APBTeam:
//        Web: http://apbteam.org/
//      Email: team AT apbteam DOT org
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// }}}
#include "xmodem.hh"
#include "ucoolib/utils/delay.hh"
#include "ucoolib/common.hh"

namespace ucoo {

static const char xmodem_ack = 0x06;
static const char xmodem_c = 'C';
static const char xmodem_nack = 0x15;
static const char xmodem_eot = 0x04;
static const char xmodem_soh = 0x01;
static const char xmodem_stx = 0x02;
static const char xmodem_can = 0x18;

/// Get character with one second timeout.
static int
xmodem_getc (Stream &s)
{
    for (int timeout = 1000; timeout; timeout--)
    {
        int c = s.getc ();
        if (c != -1)
            return c;
        delay_ms (1);
    }
    return -1;
}

/// Blocking putc.
static void
xmodem_putc (Stream &s, int c)
{
    s.block (true);
    s.putc (c);
    s.block (false);
}

/// Update CRC on received payload.
static uint16_t
xmodem_crc_update (uint16_t crc, uint8_t c)
{
    int i;
    crc = crc ^ (c << 8);
    for (i = 0; i < 8; i++)
    {
        if (crc & 0x8000)
            crc = (crc << 1) ^ 0x1021;
        else
            crc = (crc << 1);
    }
    return crc;
}

int
xmodem_receive (Stream &s, XmodemReceiver &receiver)
{
    int i, length, total = 0;
    int c, c2;
    uint16_t crc;
    bool started = false;
    bool ok = false;
    uint8_t block_number = 1;
    bool dup = false;
    char buf[1024];
    s.block (false);
    while (1)
    {
        // Send ACK, NACK or 'C'.
        if (ok)
            xmodem_putc (s, xmodem_ack);
        else
        {
            // Purge.
            while (xmodem_getc (s) != -1)
                ;
            xmodem_putc (s, started ? xmodem_nack : xmodem_c);
        }
        ok = false;
        // Receive SOH, STX, or EOT.
        c = xmodem_getc (s);
        if (c == xmodem_soh)
            length = 128;
        else if (c == xmodem_stx)
            length = 1024;
        else if (c == xmodem_eot)
            break;
        else
            continue;
        // Receive block number.
        c = xmodem_getc (s);
        if (c == -1)
            continue;
        c2 = xmodem_getc (s);
        if (c2 == -1 || c2 != 255 - c)
            continue;
        else if (started && c == block_number - 1)
            dup = true;
        else if (c != block_number)
            continue;
        // Receive data.
        crc = 0;
        for (i = 0; i < length && c != -1; i++)
        {
            c = xmodem_getc (s);
            crc = xmodem_crc_update (crc, c);
            buf[i] = c;
        }
        if (c == -1)
            continue;
        // Receive CRC.
        c = xmodem_getc (s);
        if (c == -1)
            continue;
        crc = xmodem_crc_update (crc, c);
        c = xmodem_getc (s);
        if (c == -1)
            continue;
        crc = xmodem_crc_update (crc, c);
        if (crc != 0)
            continue;
        // Success, handle data.
        if (!dup)
        {
            started = true;
            block_number++;
            int r = receiver.write (buf, length);
            if (r == -1)
            {
                total = -1;
                break;
            }
            total += length;
        }
        ok = true;
        dup = false;
    }
    xmodem_putc (s, total == -1 ? xmodem_can : xmodem_ack);
    return total;
}

} // namespace ucoo