aboutsummaryrefslogtreecommitdiff
path: root/embassy-net/src/device.rs
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2022-05-23 03:50:43 +0200
committerDario Nieuwenhuis <[email protected]>2022-05-25 19:56:22 +0200
commita5aea995a802fea8fc1b3e4b5fe47bd6d1fca2a4 (patch)
tree0fcb4c01914347eff5b3be44b284aa9432e28678 /embassy-net/src/device.rs
parent36a1f203648dcb402727ea3eb5d30cf1f6993795 (diff)
WIP embassy-net v2
Diffstat (limited to 'embassy-net/src/device.rs')
-rw-r--r--embassy-net/src/device.rs50
1 files changed, 38 insertions, 12 deletions
diff --git a/embassy-net/src/device.rs b/embassy-net/src/device.rs
index 1f4fa5208..99c6a2212 100644
--- a/embassy-net/src/device.rs
+++ b/embassy-net/src/device.rs
@@ -12,24 +12,50 @@ pub enum LinkState {
12 Up, 12 Up,
13} 13}
14 14
15// 'static required due to the "fake GAT" in smoltcp::phy::Device.
16// https://github.com/smoltcp-rs/smoltcp/pull/572
15pub trait Device { 17pub trait Device {
16 fn is_transmit_ready(&mut self) -> bool; 18 fn is_transmit_ready(&mut self) -> bool;
17 fn transmit(&mut self, pkt: PacketBuf); 19 fn transmit(&mut self, pkt: PacketBuf);
18 fn receive(&mut self) -> Option<PacketBuf>; 20 fn receive(&mut self) -> Option<PacketBuf>;
19 21
20 fn register_waker(&mut self, waker: &Waker); 22 fn register_waker(&mut self, waker: &Waker);
21 fn capabilities(&mut self) -> DeviceCapabilities; 23 fn capabilities(&self) -> DeviceCapabilities;
22 fn link_state(&mut self) -> LinkState; 24 fn link_state(&mut self) -> LinkState;
23 fn ethernet_address(&self) -> [u8; 6]; 25 fn ethernet_address(&self) -> [u8; 6];
24} 26}
25 27
26pub struct DeviceAdapter { 28impl<T: ?Sized + Device> Device for &'static mut T {
27 pub device: &'static mut dyn Device, 29 fn is_transmit_ready(&mut self) -> bool {
30 T::is_transmit_ready(self)
31 }
32 fn transmit(&mut self, pkt: PacketBuf) {
33 T::transmit(self, pkt)
34 }
35 fn receive(&mut self) -> Option<PacketBuf> {
36 T::receive(self)
37 }
38 fn register_waker(&mut self, waker: &Waker) {
39 T::register_waker(self, waker)
40 }
41 fn capabilities(&self) -> DeviceCapabilities {
42 T::capabilities(self)
43 }
44 fn link_state(&mut self) -> LinkState {
45 T::link_state(self)
46 }
47 fn ethernet_address(&self) -> [u8; 6] {
48 T::ethernet_address(self)
49 }
50}
51
52pub struct DeviceAdapter<D: Device> {
53 pub device: D,
28 caps: DeviceCapabilities, 54 caps: DeviceCapabilities,
29} 55}
30 56
31impl DeviceAdapter { 57impl<D: Device> DeviceAdapter<D> {
32 pub(crate) fn new(device: &'static mut dyn Device) -> Self { 58 pub(crate) fn new(device: D) -> Self {
33 Self { 59 Self {
34 caps: device.capabilities(), 60 caps: device.capabilities(),
35 device, 61 device,
@@ -37,16 +63,16 @@ impl DeviceAdapter {
37 } 63 }
38} 64}
39 65
40impl<'a> SmolDevice<'a> for DeviceAdapter { 66impl<'a, D: Device + 'static> SmolDevice<'a> for DeviceAdapter<D> {
41 type RxToken = RxToken; 67 type RxToken = RxToken;
42 type TxToken = TxToken<'a>; 68 type TxToken = TxToken<'a, D>;
43 69
44 fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> { 70 fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> {
45 let tx_pkt = PacketBox::new(Packet::new())?; 71 let tx_pkt = PacketBox::new(Packet::new())?;
46 let rx_pkt = self.device.receive()?; 72 let rx_pkt = self.device.receive()?;
47 let rx_token = RxToken { pkt: rx_pkt }; 73 let rx_token = RxToken { pkt: rx_pkt };
48 let tx_token = TxToken { 74 let tx_token = TxToken {
49 device: self.device, 75 device: &mut self.device,
50 pkt: tx_pkt, 76 pkt: tx_pkt,
51 }; 77 };
52 78
@@ -61,7 +87,7 @@ impl<'a> SmolDevice<'a> for DeviceAdapter {
61 87
62 let tx_pkt = PacketBox::new(Packet::new())?; 88 let tx_pkt = PacketBox::new(Packet::new())?;
63 Some(TxToken { 89 Some(TxToken {
64 device: self.device, 90 device: &mut self.device,
65 pkt: tx_pkt, 91 pkt: tx_pkt,
66 }) 92 })
67 } 93 }
@@ -85,12 +111,12 @@ impl smoltcp::phy::RxToken for RxToken {
85 } 111 }
86} 112}
87 113
88pub struct TxToken<'a> { 114pub struct TxToken<'a, D: Device> {
89 device: &'a mut dyn Device, 115 device: &'a mut D,
90 pkt: PacketBox, 116 pkt: PacketBox,
91} 117}
92 118
93impl<'a> smoltcp::phy::TxToken for TxToken<'a> { 119impl<'a, D: Device> smoltcp::phy::TxToken for TxToken<'a, D> {
94 fn consume<R, F>(self, _timestamp: SmolInstant, len: usize, f: F) -> smoltcp::Result<R> 120 fn consume<R, F>(self, _timestamp: SmolInstant, len: usize, f: F) -> smoltcp::Result<R>
95 where 121 where
96 F: FnOnce(&mut [u8]) -> smoltcp::Result<R>, 122 F: FnOnce(&mut [u8]) -> smoltcp::Result<R>,