leodos_protocols/datalink/framing/mod.rs
1//! Transfer frame definitions for the data link layer.
2//!
3//! Contains the protocol data units (TC, TM, AOS, Proximity-1,
4//! USLP) that carry user data and control information across the
5//! space link.
6//!
7//! The [`FrameWrite`] and [`FrameRead`] traits abstract over
8//! frame formats, allowing the link layer to work with any frame
9//! type (SDLP TC/TM, USLP, Proximity-1). Both own their frame
10//! buffers internally, preventing buffer-mismatch bugs.
11
12/// Push failed: the frame cannot accept this packet.
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum PushError {
15 /// The current frame is full but the packet would fit
16 /// after a flush.
17 Full,
18 /// The packet exceeds the maximum data field length and
19 /// can never fit in any frame.
20 TooLarge,
21}
22
23/// Accumulates packets into a transfer frame.
24///
25/// Owns the frame buffer internally.
26/// [`push()`](FrameWrite::push) writes packet data at the
27/// current offset. [`finish()`](FrameWrite::finish) stamps the
28/// header and returns a borrow of the completed frame.
29pub trait FrameWrite {
30 /// Error type for frame construction.
31 type Error;
32
33 /// Returns `true` if no packets have been pushed yet.
34 fn is_empty(&self) -> bool;
35
36 /// Push a packet into the frame at the current offset.
37 fn push(&mut self, data: &[u8]) -> Result<(), PushError>;
38
39 /// Stamp the frame header and return the finished frame.
40 /// Resets internal state for the next frame.
41 fn finish(&mut self) -> Result<&[u8], Self::Error>;
42}
43
44/// Extracts the data field from a received transfer frame.
45///
46/// Owns the frame buffer internally.
47/// [`buffer_mut()`](FrameRead::buffer_mut) provides write
48/// access for the coding layer to fill.
49/// [`feed()`](FrameRead::feed) validates the header.
50/// [`data_field()`](FrameRead::data_field) returns the raw
51/// data field — packet extraction is handled by
52/// [`DatalinkReader`](super::super::link::framed::DatalinkReader).
53pub trait FrameRead {
54 /// Error type for frame parsing.
55 type Error;
56
57 /// Returns a mutable reference to the internal buffer
58 /// for the coding layer to write received data into.
59 fn buffer_mut(&mut self) -> &mut [u8];
60
61 /// Validate the frame header for `len` bytes of data
62 /// in the buffer and prepare for data field access.
63 fn feed(&mut self, len: usize) -> Result<(), Self::Error>;
64
65 /// Returns the data field of the most recently fed frame.
66 ///
67 /// Returns an empty slice before the first `feed()` call.
68 fn data_field(&self) -> &[u8];
69}
70
71/// Space Data Link Protocol frame definitions (TC, TM, AOS).
72pub mod sdlp;
73/// Unified Space Data Link Protocol (CCSDS 732.1-B-3).
74pub mod uslp;