serial_line_ip/
lib.rs

1//! Serial Line Internet Protocol (SLIP)
2//!
3//! Pure Rust implementation of [RFC 1055](https://tools.ietf.org/html/rfc1055)
4//! Serial Line IP.
5//!
6//! ## What is SLIP
7//!
8//! SLIP is a very simple packet framing protocol. It is used to convert streams of
9//! bytes into frames and vice versa. It has no addressing, packet types, error
10//! correction or compression. SLIP just solves the problem of framing arbitrary
11//! sized data streams!
12//!
13//! ## Examples
14//!
15//! SLIP can be used to both encode and decode streams of bytes:
16//!
17//! ### Encoding
18//!
19//! The SLIP encoder can process multiple input slices before ending a packet:
20//!
21//! ```
22//! use serial_line_ip::Encoder;
23//!
24//! const INPUT_1: &[u8] = &[0x01, 0x02, 0x03];
25//! const INPUT_2: &[u8] = &[0x04, 0x05, 0x06];
26//! const EXPECTED: &[u8] = &[0xc0,
27//!     0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
28//!     0xc0
29//! ];
30//! let mut output: [u8; 32] = [0; 32];
31//!
32//! let mut slip = Encoder::new();
33//!
34//! let mut totals = slip.encode(INPUT_1, &mut output).unwrap();
35//! let expected_bytes_written = 1 + INPUT_1.len();
36//! assert_eq!(expected_bytes_written, totals.written);
37//!
38//! totals += slip.encode(INPUT_2, &mut output[totals.written..]).unwrap();
39//! let expected_bytes_written = expected_bytes_written + INPUT_2.len();
40//! assert_eq!(expected_bytes_written, totals.written);
41//!
42//! totals += slip.finish(&mut output[totals.written..]).unwrap();
43//! assert_eq!(expected_bytes_written + 1, totals.written);
44//! assert_eq!(EXPECTED, &output[..totals.written]);
45//! ```
46//!
47//! ### Decoding
48//!
49//! Since the length and number of packets in a data stream (byte slice)
50//! is unknown upfront, the length of the input bytes processed, output slice
51//! and an indication if the end of a packet was reached, is provided by the
52//! decoder:
53//!
54//! ```
55//! use serial_line_ip::Decoder;
56//!
57//! const SLIP_ENCODED: [u8; 7] = [
58//!     0xc0,
59//!     0x01, 0x02, 0x03, 0x04, 0x05,
60//!     0xc0
61//! ];
62//! const DATA: [u8; 5] = [0x01, 0x02, 0x03, 0x04, 0x05];
63//!
64//! let mut output: [u8; 32] = [0; 32];
65//! let mut slip = Decoder::new();
66//!
67//! let (input_bytes_processed, output_slice, is_end_of_packet) =
68//!     slip.decode(&SLIP_ENCODED, &mut output).unwrap();
69//!
70//! assert_eq!(SLIP_ENCODED.len(), input_bytes_processed);
71//! assert_eq!(&DATA, output_slice);
72//! assert_eq!(true, is_end_of_packet);
73//! ```
74
75#![deny(missing_docs)]
76#![deny(warnings)]
77#![no_std]
78
79mod decoder;
80mod encoder;
81mod error;
82
83pub use decoder::Decoder;
84pub use encoder::{EncodeTotals, Encoder};
85pub use error::{Error, Result};
86
87/// Frame end
88const END: u8 = 0xC0;
89
90/// Frame escape
91const ESC: u8 = 0xDB;
92
93/// Transposed frame end
94const ESC_END: u8 = 0xDC;
95
96/// Transposed frame escape
97const ESC_ESC: u8 = 0xDD;