2021-03-02 21:20:00 +01:00
|
|
|
use core::task::Waker;
|
2021-02-03 05:09:37 +01:00
|
|
|
use smoltcp::phy::Device as SmolDevice;
|
|
|
|
use smoltcp::phy::DeviceCapabilities;
|
|
|
|
use smoltcp::time::Instant as SmolInstant;
|
|
|
|
|
2021-04-07 19:06:45 +02:00
|
|
|
use crate::packet_pool::PacketBoxExt;
|
2021-02-12 01:48:21 +01:00
|
|
|
use crate::Result;
|
2021-03-02 21:20:00 +01:00
|
|
|
use crate::{Packet, PacketBox, PacketBuf};
|
2021-02-03 05:09:37 +01:00
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Clone, Copy)]
|
|
|
|
pub enum LinkState {
|
|
|
|
Down,
|
|
|
|
Up,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Device {
|
|
|
|
fn is_transmit_ready(&mut self) -> bool;
|
|
|
|
fn transmit(&mut self, pkt: PacketBuf);
|
|
|
|
fn receive(&mut self) -> Option<PacketBuf>;
|
|
|
|
|
|
|
|
fn register_waker(&mut self, waker: &Waker);
|
|
|
|
fn capabilities(&mut self) -> DeviceCapabilities;
|
|
|
|
fn link_state(&mut self) -> LinkState;
|
2021-03-02 21:20:00 +01:00
|
|
|
fn ethernet_address(&mut self) -> [u8; 6];
|
2021-02-03 05:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct DeviceAdapter {
|
|
|
|
pub device: &'static mut dyn Device,
|
|
|
|
caps: DeviceCapabilities,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DeviceAdapter {
|
|
|
|
pub(crate) fn new(device: &'static mut dyn Device) -> Self {
|
|
|
|
Self {
|
|
|
|
caps: device.capabilities(),
|
|
|
|
device,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> SmolDevice<'a> for DeviceAdapter {
|
|
|
|
type RxToken = RxToken;
|
|
|
|
type TxToken = TxToken<'a>;
|
|
|
|
|
|
|
|
fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> {
|
2021-06-14 23:06:12 +02:00
|
|
|
let tx_pkt = PacketBox::new(Packet::new())?;
|
2021-02-03 05:09:37 +01:00
|
|
|
let rx_pkt = self.device.receive()?;
|
|
|
|
let rx_token = RxToken { pkt: rx_pkt };
|
|
|
|
let tx_token = TxToken {
|
|
|
|
device: self.device,
|
|
|
|
pkt: tx_pkt,
|
|
|
|
};
|
|
|
|
|
|
|
|
Some((rx_token, tx_token))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct a transmit token.
|
|
|
|
fn transmit(&'a mut self) -> Option<Self::TxToken> {
|
|
|
|
if !self.device.is_transmit_ready() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let tx_pkt = PacketBox::new(Packet::new())?;
|
|
|
|
Some(TxToken {
|
|
|
|
device: self.device,
|
|
|
|
pkt: tx_pkt,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a description of device capabilities.
|
|
|
|
fn capabilities(&self) -> DeviceCapabilities {
|
|
|
|
self.caps.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RxToken {
|
|
|
|
pkt: PacketBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl smoltcp::phy::RxToken for RxToken {
|
|
|
|
fn consume<R, F>(mut self, _timestamp: SmolInstant, f: F) -> Result<R>
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut [u8]) -> Result<R>,
|
|
|
|
{
|
|
|
|
f(&mut self.pkt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TxToken<'a> {
|
|
|
|
device: &'a mut dyn Device,
|
|
|
|
pkt: PacketBox,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> smoltcp::phy::TxToken for TxToken<'a> {
|
2021-03-02 21:20:00 +01:00
|
|
|
fn consume<R, F>(self, _timestamp: SmolInstant, len: usize, f: F) -> Result<R>
|
2021-02-03 05:09:37 +01:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut [u8]) -> Result<R>,
|
|
|
|
{
|
|
|
|
let mut buf = self.pkt.slice(0..len);
|
|
|
|
let r = f(&mut buf)?;
|
|
|
|
self.device.transmit(buf);
|
|
|
|
Ok(r)
|
|
|
|
}
|
|
|
|
}
|