diff --git a/embassy-net-driver-channel/src/lib.rs b/embassy-net-driver-channel/src/lib.rs index e101bf38..53b66afb 100644 --- a/embassy-net-driver-channel/src/lib.rs +++ b/embassy-net-driver-channel/src/lib.rs @@ -42,8 +42,7 @@ struct StateInner<'d, const MTU: usize> { struct Shared { link_state: LinkState, waker: WakerRegistration, - ethernet_address: [u8; 6], - ieee802154_address: [u8; 8], + hardware_address: HardwareAddress, } pub struct Runner<'d, const MTU: usize> { @@ -86,18 +85,10 @@ impl<'d, const MTU: usize> Runner<'d, MTU> { }); } - pub fn set_ethernet_address(&mut self, address: [u8; 6]) { + pub fn set_hardware_address(&mut self, address: HardwareAddress) { self.shared.lock(|s| { let s = &mut *s.borrow_mut(); - s.ethernet_address = address; - s.waker.wake(); - }); - } - - pub fn set_ieee802154_address(&mut self, address: [u8; 8]) { - self.shared.lock(|s| { - let s = &mut *s.borrow_mut(); - s.ieee802154_address = address; + s.hardware_address = address; s.waker.wake(); }); } @@ -300,12 +291,8 @@ impl<'d, const MTU: usize> embassy_net_driver::Driver for Device<'d, MTU> { self.caps.clone() } - fn ethernet_address(&self) -> [u8; 6] { - self.shared.lock(|s| s.borrow().ethernet_address) - } - - fn ieee802154_address(&self) -> [u8; 8] { - self.shared.lock(|s| s.borrow().ieee802154_address) + fn hardware_address(&self) -> HardwareAddress { + self.shared.lock(|s| s.borrow().hardware_address) } fn link_state(&mut self, cx: &mut Context) -> LinkState { diff --git a/embassy-net-driver/Cargo.toml b/embassy-net-driver/Cargo.toml index da6d9ad6..1af0d61b 100644 --- a/embassy-net-driver/Cargo.toml +++ b/embassy-net-driver/Cargo.toml @@ -21,4 +21,5 @@ target = "thumbv7em-none-eabi" features = ["defmt"] [dependencies] -defmt = { version = "0.3", optional = true } \ No newline at end of file +defmt = { version = "0.3", optional = true } +smoltcp = { version = "0.10", default-features = false } diff --git a/embassy-net-driver/src/lib.rs b/embassy-net-driver/src/lib.rs index beb1a1c7..93a02e46 100644 --- a/embassy-net-driver/src/lib.rs +++ b/embassy-net-driver/src/lib.rs @@ -4,6 +4,8 @@ use core::task::Context; +use smoltcp::wire::HardwareAddress; + /// Main `embassy-net` driver API. /// /// This is essentially an interface for sending and receiving raw network frames. @@ -51,11 +53,8 @@ pub trait Driver { /// Get a description of device capabilities. fn capabilities(&self) -> Capabilities; - /// Get the device's Ethernet address. - fn ethernet_address(&self) -> [u8; 6]; - - /// Get the device's IEEE 802.15.4 address. - fn ieee802154_address(&self) -> [u8; 8]; + /// Get the device's hardware address. + fn hardware_address(&self) -> HardwareAddress; } impl Driver for &mut T { @@ -78,11 +77,8 @@ impl Driver for &mut T { fn link_state(&mut self, cx: &mut Context) -> LinkState { T::link_state(self, cx) } - fn ethernet_address(&self) -> [u8; 6] { - T::ethernet_address(self) - } - fn ieee802154_address(&self) -> [u8; 8] { - T::ieee802154_address(self) + fn hardware_address(&self) -> HardwareAddress { + T::hardware_address(self) } } diff --git a/embassy-net/src/lib.rs b/embassy-net/src/lib.rs index 53693563..efd820a6 100644 --- a/embassy-net/src/lib.rs +++ b/embassy-net/src/lib.rs @@ -243,11 +243,11 @@ impl Stack { let hardware_addr = match medium { #[cfg(feature = "medium-ethernet")] - Medium::Ethernet => HardwareAddress::Ethernet(EthernetAddress(device.ethernet_address())), + Medium::Ethernet => device.hardware_address(), #[cfg(feature = "medium-ip")] Medium::Ip => HardwareAddress::Ip, #[cfg(feature = "medium-ieee802154")] - Medium::Ieee802154 => HardwareAddress::Ieee802154(Ieee802154Address::Extended(device.ieee802154_address())), + Medium::Ieee802154 => device.hardware_address(), #[allow(unreachable_patterns)] _ => panic!( "Unsupported medium {:?}. Make sure to enable it in embassy-net's Cargo features.", @@ -336,9 +336,9 @@ impl Stack { f(&mut *self.socket.borrow_mut(), &mut *self.inner.borrow_mut()) } - /// Get the MAC address of the network interface. - pub fn ethernet_address(&self) -> [u8; 6] { - self.with(|_s, i| i.device.ethernet_address()) + /// Get the hardware address of the network interface. + pub fn hardware_address(&self) -> HardwareAddress { + self.with(|_s, i| i.device.hardware_address()) } /// Get whether the link is up. @@ -740,18 +740,11 @@ impl Inner { fn poll(&mut self, cx: &mut Context<'_>, s: &mut SocketStack) { s.waker.register(cx.waker()); - #[cfg(feature = "medium-ethernet")] - if self.device.capabilities().medium == Medium::Ethernet { - s.iface.set_hardware_addr(HardwareAddress::Ethernet(EthernetAddress( - self.device.ethernet_address(), - ))); - } - - #[cfg(feature = "medium-ieee802154")] - if self.device.capabilities().medium == Medium::Ieee802154 { - s.iface.set_hardware_addr(HardwareAddress::Ieee802154(Ieee802154Address::Extended( - self.device.ieee802154_address(), - ))); + #[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))] + if self.device.capabilities().medium == Medium::Ethernet + || self.device.capabilities().medium == Medium::Ieee802154 + { + s.iface.set_hardware_addr(self.device.hardware_address()); } let timestamp = instant_to_smoltcp(Instant::now()); diff --git a/embassy-stm32-wpan/src/mac/driver.rs b/embassy-stm32-wpan/src/mac/driver.rs index fffbb9ed..06ff0cf6 100644 --- a/embassy-stm32-wpan/src/mac/driver.rs +++ b/embassy-stm32-wpan/src/mac/driver.rs @@ -73,10 +73,10 @@ impl<'d> embassy_net_driver::Driver for Driver<'d> { LinkState::Down } - fn ethernet_address(&self) -> [u8; 6] { + fn hardware_address(&self) -> HardwareAddress { // self.mac_addr - [0; 6] + HardwareAddress::Ethernet(EthernetAddress([0; 6])) } } diff --git a/examples/std/src/tuntap.rs b/examples/std/src/tuntap.rs index d918a2e6..932bdaf9 100644 --- a/examples/std/src/tuntap.rs +++ b/examples/std/src/tuntap.rs @@ -4,6 +4,7 @@ use std::os::unix::io::{AsRawFd, RawFd}; use std::task::Context; use async_io::Async; +use embassy_net::HardwareAddress; use embassy_net_driver::{self, Capabilities, Driver, LinkState}; use log::*; @@ -180,8 +181,8 @@ impl Driver for TunTapDevice { LinkState::Up } - fn ethernet_address(&self) -> [u8; 6] { - [0x02, 0x03, 0x04, 0x05, 0x06, 0x07] + fn hardware_address(&self) -> HardwareAddress { + HardwareAddress::Ethernet(EthernetAddress([0x02, 0x03, 0x04, 0x05, 0x06, 0x07])) } }