Skip to main content

edge_nal/
raw.rs

1//! Traits for modeling raw sockets' sending/receiving functionality on embedded devices
2
3use embedded_io_async::ErrorType;
4
5/// A MAC address
6pub type MacAddr = [u8; 6];
7
8/// This trait is implemented by raw sockets and models their datagram receiving functionality.
9pub trait RawReceive: ErrorType {
10    /// Receive a datagram into the provided buffer.
11    ///
12    /// If the received datagram exceeds the buffer's length, it is received regardless, and the
13    /// remaining bytes are discarded. The full datagram size is still indicated in the result,
14    /// allowing the recipient to detect that truncation.
15    ///
16    /// The remote Mac address is given in the result along with the number
17    /// of bytes.
18    async fn receive(&mut self, buffer: &mut [u8]) -> Result<(usize, MacAddr), Self::Error>;
19}
20
21/// This trait is implemented by UDP sockets and models their datagram sending functionality.
22pub trait RawSend: ErrorType {
23    /// Send the provided data to a peer.
24    ///
25    /// A MAC address is provided to specify the destination.
26    /// If the destination mac address contains all `0xff`, the packet is broadcasted.
27    async fn send(&mut self, addr: MacAddr, data: &[u8]) -> Result<(), Self::Error>;
28}
29
30impl<T> RawReceive for &mut T
31where
32    T: RawReceive,
33{
34    async fn receive(&mut self, buffer: &mut [u8]) -> Result<(usize, MacAddr), Self::Error> {
35        (**self).receive(buffer).await
36    }
37}
38
39impl<T> RawSend for &mut T
40where
41    T: RawSend,
42{
43    async fn send(&mut self, addr: MacAddr, data: &[u8]) -> Result<(), Self::Error> {
44        (**self).send(addr, data).await
45    }
46}