Skip to main content

leodos_protocols/datalink/
mod.rs

1//! Implements the CCSDS Data Link Protocols (Layer 2).
2
3use core::future::Future;
4
5/// Transfer frame definitions (SDLP, USLP).
6pub mod framing;
7/// CCSDS Space Packet Protocol (SPP) definitions.
8pub mod spp;
9/// Async link channels for sending and receiving frames.
10pub mod link;
11/// Hop-by-hop reliable frame delivery (COP-1).
12pub mod reliability;
13/// Frame-level encryption and authentication (SDLS).
14pub mod security;
15
16// ── Layer boundary traits ──────────────────────────────────────
17
18/// Send direction of the data link layer.
19pub trait DatalinkWrite {
20    /// Error type for write operations.
21    type Error: core::error::Error;
22
23    /// Write data over the link.
24    fn write(&mut self, data: &[u8]) -> impl Future<Output = Result<(), Self::Error>>;
25}
26
27/// Receive direction of the data link layer.
28pub trait DatalinkRead {
29    /// Error type for read operations.
30    type Error: core::error::Error;
31
32    /// Read data from the link into `buffer`.
33    fn read(&mut self, buffer: &mut [u8]) -> impl Future<Output = Result<usize, Self::Error>>;
34}
35
36/// A bidirectional data link that can be split into
37/// independent read and write halves.
38pub trait Datalink {
39    /// Error type for read operations.
40    type ReadError: core::error::Error;
41    /// Error type for write operations.
42    type WriteError: core::error::Error;
43    /// Read half type.
44    type Reader<'a>: DatalinkRead<Error = Self::ReadError>
45    where
46        Self: 'a;
47    /// Write half type.
48    type Writer<'a>: DatalinkWrite<Error = Self::WriteError>
49    where
50        Self: 'a;
51
52    /// Split into independent read and write halves.
53    fn split(&mut self) -> (Self::Reader<'_>, Self::Writer<'_>);
54}
55
56impl<R: DatalinkRead, W: DatalinkWrite> Datalink for (R, W) {
57    type ReadError = R::Error;
58    type WriteError = W::Error;
59    type Reader<'a> = &'a mut R where Self: 'a;
60    type Writer<'a> = &'a mut W where Self: 'a;
61
62    fn split(&mut self) -> (&mut R, &mut W) {
63        (&mut self.0, &mut self.1)
64    }
65}
66
67impl<T: DatalinkRead + ?Sized> DatalinkRead for &mut T {
68    type Error = T::Error;
69
70    async fn read(
71        &mut self,
72        buffer: &mut [u8],
73    ) -> Result<usize, Self::Error> {
74        T::read(self, buffer).await
75    }
76}
77
78impl<T: DatalinkWrite + ?Sized> DatalinkWrite for &mut T {
79    type Error = T::Error;
80
81    async fn write(
82        &mut self,
83        data: &[u8],
84    ) -> Result<(), Self::Error> {
85        T::write(self, data).await
86    }
87}