-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathraw.rs
46 lines (40 loc) · 1.57 KB
/
raw.rs
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
//! Traits for modeling raw sockets' sending/receiving functionality on embedded devices
use embedded_io_async::ErrorType;
/// A MAC address
pub type MacAddr = [u8; 6];
/// This trait is implemented by raw sockets and models their datagram receiving functionality.
pub trait RawReceive: ErrorType {
/// Receive a datagram into the provided buffer.
///
/// If the received datagram exceeds the buffer's length, it is received regardless, and the
/// remaining bytes are discarded. The full datagram size is still indicated in the result,
/// allowing the recipient to detect that truncation.
///
/// The remote Mac address is given in the result along with the number
/// of bytes.
async fn receive(&mut self, buffer: &mut [u8]) -> Result<(usize, MacAddr), Self::Error>;
}
/// This trait is implemented by UDP sockets and models their datagram sending functionality.
pub trait RawSend: ErrorType {
/// Send the provided data to a peer.
///
/// A MAC address is provided to specify the destination.
/// If the destination mac address contains all `0xff`, the packet is broadcasted.
async fn send(&mut self, addr: MacAddr, data: &[u8]) -> Result<(), Self::Error>;
}
impl<T> RawReceive for &mut T
where
T: RawReceive,
{
async fn receive(&mut self, buffer: &mut [u8]) -> Result<(usize, MacAddr), Self::Error> {
(**self).receive(buffer).await
}
}
impl<T> RawSend for &mut T
where
T: RawSend,
{
async fn send(&mut self, addr: MacAddr, data: &[u8]) -> Result<(), Self::Error> {
(**self).send(addr, data).await
}
}