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
//! Serial Line Internet Protocol (SLIP)
//!
//! Pure Rust implementation of [RFC 1055](https://tools.ietf.org/html/rfc1055)
//! Serial Line IP.
//!
//! ## What is SLIP
//!
//! SLIP is a very simple packet framing protocol. It is used to convert streams of
//! bytes into frames and vice versa. It has no addressing, packet types, error
//! correction or compression. SLIP just solves the problem of framing arbitrary
//! sized data streams!
//!
//! ## Examples
//!
//! SLIP can be used to both encode and decode streams of bytes:
//!
//! ### Encoding
//!
//! The SLIP encoder can process multiple input slices before ending a packet:
//!
//! ```
//! use serial_line_ip::Encoder;
//!
//! const INPUT_1: &[u8] = &[0x01, 0x02, 0x03];
//! const INPUT_2: &[u8] = &[0x04, 0x05, 0x06];
//! const EXPECTED: &[u8] = &[0xc0,
//!     0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
//!     0xc0
//! ];
//! let mut output: [u8; 32] = [0; 32];
//!
//! let mut slip = Encoder::new();
//!
//! let mut totals = slip.encode(INPUT_1, &mut output).unwrap();
//! let expected_bytes_written = 1 + INPUT_1.len();
//! assert_eq!(expected_bytes_written, totals.written);
//!
//! totals += slip.encode(INPUT_2, &mut output[totals.written..]).unwrap();
//! let expected_bytes_written = expected_bytes_written + INPUT_2.len();
//! assert_eq!(expected_bytes_written, totals.written);
//!
//! totals += slip.finish(&mut output[totals.written..]).unwrap();
//! assert_eq!(expected_bytes_written + 1, totals.written);
//! assert_eq!(EXPECTED, &output[..totals.written]);
//! ```
//!
//! ### Decoding
//!
//! Since the length and number of packets in a data stream (byte slice)
//! is unknown upfront, the length of the input bytes processed, output slice
//! and an indication if the end of a packet was reached, is provided by the
//! decoder:
//!
//! ```
//! use serial_line_ip::Decoder;
//!
//! const SLIP_ENCODED: [u8; 7] = [
//!     0xc0,
//!     0x01, 0x02, 0x03, 0x04, 0x05,
//!     0xc0
//! ];
//! const DATA: [u8; 5] = [0x01, 0x02, 0x03, 0x04, 0x05];
//!
//! let mut output: [u8; 32] = [0; 32];
//! let mut slip = Decoder::new();
//!
//! let (input_bytes_processed, output_slice, is_end_of_packet) =
//!     slip.decode(&SLIP_ENCODED, &mut output).unwrap();
//!
//! assert_eq!(SLIP_ENCODED.len(), input_bytes_processed);
//! assert_eq!(&DATA, output_slice);
//! assert_eq!(true, is_end_of_packet);
//! ```

#![deny(missing_docs)]
#![deny(warnings)]
#![no_std]

mod decoder;
mod encoder;
mod error;

pub use decoder::Decoder;
pub use encoder::{EncodeTotals, Encoder};
pub use error::{Error, Result};

/// Frame end
const END: u8 = 0xC0;

/// Frame escape
const ESC: u8 = 0xDB;

/// Transposed frame end
const ESC_END: u8 = 0xDC;

/// Transposed frame escape
const ESC_ESC: u8 = 0xDD;