xmodem 0.4.0

An implementation of the XMODEM file-transfer protocol.
Documentation
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#![cfg_attr(not(feature = "std"), no_std)]

use core::convert::{From, TryFrom};

#[cfg(not(feature = "std"))]
/// In a `no_std` environment, `std::io` is not available.  We
/// provide a publically available subset of compatible types
/// and traits required by the rest of the library that must be
/// implemented by consumers.
///
/// Refer to the `std::io` documentation for details.
pub mod io {
    use core::{fmt, mem, result};

    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
    pub enum ErrorKind {
        TimedOut,
        Other,
    }

    #[derive(Debug)]
    pub struct Error {
        kind: ErrorKind,
        message: &'static str,
    }

    impl Error {
        pub fn new(kind: ErrorKind, message: &'static str) -> Error {
            Error { kind, message }
        }

        pub fn kind(&self) -> ErrorKind {
            self.kind
        }
    }

    impl fmt::Display for Error {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "IO error {:?}: {}", self.kind, self.message)
        }
    }

    pub type Result<T> = result::Result<T, Error>;

    pub trait Read {
        fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
        fn read_exact(&mut self, buf: &mut [u8]) -> Result<()>;
    }

    pub trait Write {
        fn flush(&mut self) -> Result<()>;
        fn write(&mut self, buf: &[u8]) -> Result<usize>;
        fn write_all(&mut self, buf: &[u8]) -> Result<()>;
    }

    // Credit where due: this is mostly taken from `std::io`.
    impl Write for &mut [u8] {
        #[inline]
        fn write(&mut self, data: &[u8]) -> Result<usize> {
            let n = usize::min(data.len(), self.len());
            let dst = mem::replace(self, &mut []);
            let (a, b) = dst.split_at_mut(n);
            a.copy_from_slice(&data[..n]);
            *self = b;
            Ok(n)
        }

        #[inline]
        fn write_all(&mut self, data: &[u8]) -> Result<()> {
            if self.write(data)? != data.len() {
                return Err(Error::new(ErrorKind::Other, "failed to write whole buffer"));
            }
            Ok(())
        }

        #[inline]
        fn flush(&mut self) -> Result<()> {
            Ok(())
        }
    }
}
#[cfg(feature = "std")]
use std::io;

use io::{Read, Write};

use ::log::{debug, error, info, warn};

// TODO: Send CAN byte after too many errors
// TODO: Handle CAN bytes while sending
// TODO: Implement Error for Error

const SOH: u8 = 0x01;
const STX: u8 = 0x02;
const EOT: u8 = 0x04;
const ACK: u8 = 0x06;
const NAK: u8 = 0x15;
const CAN: u8 = 0x18;
const CRC: u8 = 0x43;

pub type Result<T> = core::result::Result<T, Error>;

#[derive(Debug)]
pub enum Error {
    Io(io::Error),

    /// The number of communications errors exceeded `max_errors` in a
    /// single transmission.
    ExhaustedRetries,

    /// The transmission was canceled by the other end of the channel.
    Canceled,

    /// Data was received that is not appropriate to the transfer state.
    Invalid,

    /// A packet was received with mismatched sequence numbers.
    SequenceMismatch,

    /// A packet was received with an incorrect checksum or CRC16.
    Checksum,
}

impl From<io::Error> for Error {
    fn from(err: io::Error) -> Error {
        Error::Io(err)
    }
}

#[derive(Copy, Clone, Debug)]
pub enum Checksum {
    Standard,
    CRC16,
}

#[derive(Copy, Clone, Debug)]
pub enum BlockLength {
    Standard = 128,
    OneK = 1024,
}

struct XmodemPacket {
    pub seqno: u8,
    data: XmodemBuffer,
}

#[allow(clippy::large_enum_variant)]
enum XmodemBuffer {
    Standard([u8; 128]),
    OneK([u8; 1024]),
}

impl XmodemPacket {
    pub fn new(l: BlockLength, pad: u8) -> Self {
        match l {
            BlockLength::Standard => XmodemPacket {
                seqno: 0,
                data: XmodemBuffer::Standard([pad; 128]),
            },
            BlockLength::OneK => XmodemPacket {
                seqno: 0,
                data: XmodemBuffer::OneK([pad; 1024]),
            },
        }
    }

    fn recv_next<R: Read>(r: &mut R, c: Checksum) -> Result<Option<Self>> {
        let mut ret = match get_byte(r)? {
            SOH => Self::new(BlockLength::Standard, 0),
            STX => Self::new(BlockLength::OneK, 0),
            EOT => return Ok(None),
            _ => return Err(Error::Invalid),
        };

        /*
         * The next two bytes are the packet sequence number mod 256
         * and the 1's complement of that.  If they don't match we'll
         * return an error later; we still need to read the packet
         * to maintain proper transaction state.
         */
        let recv_seqno = get_byte(r)?;
        let recv_seqno1c = get_byte(r)?;

        r.read_exact(ret.as_mut())?;

        let checksum_ok = match c {
            Checksum::Standard => {
                let recv_checksum = get_byte(r)?;
                calc_checksum(ret.as_ref()) == recv_checksum
            }
            Checksum::CRC16 => {
                calc_crc(ret.as_ref()) == u16::from_be_bytes([get_byte(r)?, get_byte(r)?])
            }
        };

        if 0xFF - recv_seqno != recv_seqno1c {
            return Err(Error::SequenceMismatch);
        }

        if checksum_ok {
            ret.seqno = recv_seqno;
            return Ok(Some(ret));
        }

        Err(Error::Checksum)
    }

    fn send<W: Write>(&self, w: &mut W, c: Checksum) -> Result<()> {
        let header: [u8; 3] = [
            match self.data {
                XmodemBuffer::Standard(_) => SOH,
                XmodemBuffer::OneK(_) => STX,
            },
            self.seqno,
            0xFF - self.seqno,
        ];

        debug!("Sending block {}", self.seqno);
        w.write_all(&header)?;
        w.write_all(self.as_ref())?;

        match c {
            Checksum::Standard => {
                w.write_all(&[calc_checksum(self.as_ref())])?;
            }
            Checksum::CRC16 => {
                w.write_all(&calc_crc(self.as_ref()).to_be_bytes())?;
            }
        }

        Ok(())
    }
}

impl AsRef<[u8]> for XmodemPacket {
    fn as_ref(&self) -> &[u8] {
        match self.data {
            XmodemBuffer::Standard(ref b) => b,
            XmodemBuffer::OneK(ref b) => b,
        }
    }
}

impl AsMut<[u8]> for XmodemPacket {
    fn as_mut(&mut self) -> &mut [u8] {
        match self.data {
            XmodemBuffer::Standard(ref mut b) => b,
            XmodemBuffer::OneK(ref mut b) => b,
        }
    }
}

/// Configuration for the XMODEM transfer.
#[derive(Copy, Clone, Debug)]
pub struct Xmodem {
    /// The number of errors that can occur before the communication is
    /// considered a failure. Errors include unexpected bytes and timeouts
    /// waiting for bytes.
    pub max_errors: u32,

    /// The byte used to pad the last block. XMODEM can only send blocks of
    /// a certain size, so if the message is not a multiple of that size
    /// the last block needs to be padded.
    pub pad_byte: u8,

    /// The length of each block. There are only two options: 128-byte
    /// blocks (standard  XMODEM) or 1024-byte blocks (XMODEM-1k).
    pub block_length: BlockLength,

    /// The checksum mode used by XMODEM. This is determined by the
    /// receiver.
    checksum_mode: Checksum,
    errors: u32,
}

impl Xmodem {
    /// Creates the XMODEM config with default parameters.
    pub fn new() -> Self {
        Xmodem {
            max_errors: 16,
            pad_byte: 0x1a,
            block_length: BlockLength::Standard,
            checksum_mode: Checksum::Standard,
            errors: 0,
        }
    }

    /// Starts the XMODEM transmission.
    ///
    /// `dev` should be the serial communication channel (e.g. the serial
    /// device). `stream` should be the message to send (e.g. a file).
    ///
    /// # Timeouts
    /// This method has no way of setting the timeout of `dev`, so it's up
    /// to the caller to set the timeout of the device before calling this
    /// method. Timeouts on receiving bytes will be counted against
    /// `max_errors`, but timeouts on transmitting bytes will be considered
    /// a fatal error.
    pub fn send<D: Read + Write, R: Read>(&mut self, dev: &mut D, stream: &mut R) -> Result<usize> {
        self.errors = 0;

        debug!("Starting XMODEM transfer");
        self.start_send(dev)?;
        debug!("First byte received. Sending stream.");
        let bytes = self.send_stream(dev, stream)?;
        debug!("Sending EOT");
        self.finish_send(dev)?;

        Ok(bytes)
    }

    /// Receive an XMODEM transmission.
    ///
    /// `dev` should be the serial communication channel (e.g. the serial
    /// device). The received data will be written to `outstream`.
    /// `checksum` indicates which checksum mode should be used;
    /// Checksum::Standard is the original wrapping 8-bit checksum; you
    /// probably want CRC16 if the remote supports it (which, in 2021, it's
    /// all but certain to do).
    ///
    /// # Timeouts
    /// This method has no way of setting the timeout of `dev`, so it's up
    /// to the caller to set the timeout of the device before calling this
    /// method. Timeouts on receiving bytes will be counted against
    /// `max_errors`, but timeouts on transmitting bytes will be considered
    /// a fatal error.
    pub fn recv<D: Read + Write, W: Write>(
        &mut self,
        dev: &mut D,
        outstream: &mut W,
        checksum: Checksum,
    ) -> Result<usize> {
        self.errors = 0;
        self.checksum_mode = checksum;
        debug!("Starting XMODEM receive");
        dev.write_all(&[match self.checksum_mode {
            Checksum::Standard => NAK,
            Checksum::CRC16 => CRC,
        }])?;
        debug!("NCG sent. Receiving stream.");
        let mut seqno: u32 = 1;
        let mut bytes: usize = 0;
        loop {
            if self.errors >= self.max_errors {
                error!(
                    "Exhausted max retries ({}) while waiting for data packet {}",
                    self.max_errors, seqno
                );
                dev.write_all(&[CAN, CAN]).unwrap_or_default();
                return Err(Error::ExhaustedRetries);
            }

            let packet = match XmodemPacket::recv_next(dev, self.checksum_mode) {
                Ok(Some(x)) => {
                    if u32::from(x.seqno) != (seqno & 0xFF) {
                        dev.write_all(&[CAN, CAN])?;
                        return Err(Error::Canceled);
                    }
                    x
                }
                Ok(None) => {
                    dev.write_all(&[ACK])?;
                    break;
                }
                Err(Error::Io(e)) => match e.kind() {
                    io::ErrorKind::TimedOut => {
                        self.errors += 1;
                        warn!("Timeout!");
                        continue;
                    }
                    _ => return Err(Error::Io(e)),
                },
                Err(Error::Checksum) => {
                    dev.write_all(&[NAK])?;
                    self.errors += 1;
                    continue;
                }
                Err(Error::SequenceMismatch) => {
                    dev.write_all(&[CAN, CAN])?;

                    /* XXX Is this the right code? */
                    return Err(Error::Canceled);
                }
                Err(Error::Invalid) => {
                    warn!("Unrecognized symbol!");
                    continue;
                }
                Err(e) => return Err(e),
            };

            outstream.write_all(packet.as_ref()).map_err(|e| {
                dev.write_all(&[CAN, CAN]).unwrap_or_default();
                Error::Io(e)
            })?;
            dev.write_all(&[ACK])?;
            seqno = seqno.wrapping_add(1);
            bytes += packet.as_ref().len();
        }

        Ok(bytes)
    }

    fn start_send<D: Read + Write>(&mut self, dev: &mut D) -> Result<()> {
        let mut cancels = 0;
        loop {
            match get_byte_timeout(dev)? {
                Some(NAK) => {
                    debug!("Standard checksum requested");
                    self.checksum_mode = Checksum::Standard;
                    return Ok(());
                }
                Some(CRC) => {
                    debug!("16-bit CRC requested");
                    self.checksum_mode = Checksum::CRC16;
                    return Ok(());
                }
                Some(CAN) => {
                    warn!("Cancel (CAN) byte received");
                    cancels += 1;
                }
                Some(c) => warn!("Unknown byte received at start of XMODEM transfer: {c}"),
                None => warn!("Timed out waiting for start of XMODEM transfer."),
            }

            self.errors += 1;

            if cancels >= 2 {
                error!(
                    "Transmission canceled: received two cancel (CAN) bytes at start of XMODEM transfer"
                );
                return Err(Error::Canceled);
            }

            if self.errors >= self.max_errors {
                error!(
                    "Exhausted max retries ({}) at start of XMODEM transfer.",
                    self.max_errors
                );
                if let Err(err) = dev.write_all(&[CAN]) {
                    warn!("Error sending CAN byte: {err}");
                }
                return Err(Error::ExhaustedRetries);
            }
        }
    }

    fn send_stream<D: Read + Write, R: Read>(
        &mut self,
        dev: &mut D,
        stream: &mut R,
    ) -> Result<usize> {
        let mut seqno: u32 = 0;
        let mut bytes: usize = 0;
        loop {
            let mut packet = XmodemPacket::new(self.block_length, self.pad_byte);

            let n = stream.read(packet.as_mut())?;
            if n == 0 {
                debug!("Reached EOF");
                return Ok(bytes);
            }

            seqno += 1;
            packet.seqno = u8::try_from(seqno & 0xFF).unwrap();
            packet.send(dev, self.checksum_mode)?;
            bytes += n;

            match get_byte_timeout(dev)? {
                Some(ACK) => {
                    debug!("Received ACK for block {seqno}");
                    continue;
                }
                // TODO handle CAN bytes
                Some(b) => {
                    warn!("Expected ACK, got {b}");
                }
                None => warn!("Timeout waiting for ACK for block {seqno}"),
            }

            self.errors += 1;

            if self.errors >= self.max_errors {
                error!(
                    "Exhausted max retries ({}) while sending block {} in XMODEM transfer",
                    self.max_errors, seqno
                );
                return Err(Error::ExhaustedRetries);
            }
        }
    }

    fn finish_send<D: Read + Write>(&mut self, dev: &mut D) -> Result<()> {
        loop {
            dev.write_all(&[EOT])?;

            match get_byte_timeout(dev)? {
                Some(ACK) => {
                    info!("XMODEM transmission successful");
                    return Ok(());
                }
                Some(b) => {
                    warn!("Expected ACK, got {b}");
                }
                None => {
                    warn!("Timeout waiting for ACK for EOT")
                }
            }

            self.errors += 1;

            if self.errors >= self.max_errors {
                error!(
                    "Exhausted max retries ({}) while waiting for ACK for EOT",
                    self.max_errors
                );
                return Err(Error::ExhaustedRetries);
            }
        }
    }
}

impl Default for Xmodem {
    fn default() -> Self {
        Self::new()
    }
}

fn calc_checksum(data: &[u8]) -> u8 {
    data.iter().fold(0, |x, &y| x.wrapping_add(y))
}

fn calc_crc(data: &[u8]) -> u16 {
    crc16::State::<crc16::XMODEM>::calculate(data)
}

fn get_byte<R: Read>(reader: &mut R) -> io::Result<u8> {
    let mut buff = [0];
    reader.read_exact(&mut buff)?;
    Ok(buff[0])
}

/// Turns timeout errors into `Ok(None)`
fn get_byte_timeout<R: Read>(reader: &mut R) -> io::Result<Option<u8>> {
    match get_byte(reader) {
        Ok(c) => Ok(Some(c)),
        Err(err) => {
            if err.kind() == io::ErrorKind::TimedOut {
                Ok(None)
            } else {
                Err(err)
            }
        }
    }
}