Use HardwareAddress in Driver

This commit is contained in:
Ruben De Smet 2023-07-28 16:19:24 +02:00
parent c3ba08ffb6
commit 69c0a89aa5
No known key found for this signature in database
GPG Key ID: 1AE26A210C14115B
6 changed files with 28 additions and 50 deletions

View File

@ -42,8 +42,7 @@ struct StateInner<'d, const MTU: usize> {
struct Shared { struct Shared {
link_state: LinkState, link_state: LinkState,
waker: WakerRegistration, waker: WakerRegistration,
ethernet_address: [u8; 6], hardware_address: HardwareAddress,
ieee802154_address: [u8; 8],
} }
pub struct Runner<'d, const MTU: usize> { 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| { self.shared.lock(|s| {
let s = &mut *s.borrow_mut(); let s = &mut *s.borrow_mut();
s.ethernet_address = address; s.hardware_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.waker.wake(); s.waker.wake();
}); });
} }
@ -300,12 +291,8 @@ impl<'d, const MTU: usize> embassy_net_driver::Driver for Device<'d, MTU> {
self.caps.clone() self.caps.clone()
} }
fn ethernet_address(&self) -> [u8; 6] { fn hardware_address(&self) -> HardwareAddress {
self.shared.lock(|s| s.borrow().ethernet_address) self.shared.lock(|s| s.borrow().hardware_address)
}
fn ieee802154_address(&self) -> [u8; 8] {
self.shared.lock(|s| s.borrow().ieee802154_address)
} }
fn link_state(&mut self, cx: &mut Context) -> LinkState { fn link_state(&mut self, cx: &mut Context) -> LinkState {

View File

@ -22,3 +22,4 @@ features = ["defmt"]
[dependencies] [dependencies]
defmt = { version = "0.3", optional = true } defmt = { version = "0.3", optional = true }
smoltcp = { version = "0.10", default-features = false }

View File

@ -4,6 +4,8 @@
use core::task::Context; use core::task::Context;
use smoltcp::wire::HardwareAddress;
/// Main `embassy-net` driver API. /// Main `embassy-net` driver API.
/// ///
/// This is essentially an interface for sending and receiving raw network frames. /// 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. /// Get a description of device capabilities.
fn capabilities(&self) -> Capabilities; fn capabilities(&self) -> Capabilities;
/// Get the device's Ethernet address. /// Get the device's hardware address.
fn ethernet_address(&self) -> [u8; 6]; fn hardware_address(&self) -> HardwareAddress;
/// Get the device's IEEE 802.15.4 address.
fn ieee802154_address(&self) -> [u8; 8];
} }
impl<T: ?Sized + Driver> Driver for &mut T { impl<T: ?Sized + Driver> Driver for &mut T {
@ -78,11 +77,8 @@ impl<T: ?Sized + Driver> Driver for &mut T {
fn link_state(&mut self, cx: &mut Context) -> LinkState { fn link_state(&mut self, cx: &mut Context) -> LinkState {
T::link_state(self, cx) T::link_state(self, cx)
} }
fn ethernet_address(&self) -> [u8; 6] { fn hardware_address(&self) -> HardwareAddress {
T::ethernet_address(self) T::hardware_address(self)
}
fn ieee802154_address(&self) -> [u8; 8] {
T::ieee802154_address(self)
} }
} }

View File

@ -243,11 +243,11 @@ impl<D: Driver + 'static> Stack<D> {
let hardware_addr = match medium { let hardware_addr = match medium {
#[cfg(feature = "medium-ethernet")] #[cfg(feature = "medium-ethernet")]
Medium::Ethernet => HardwareAddress::Ethernet(EthernetAddress(device.ethernet_address())), Medium::Ethernet => device.hardware_address(),
#[cfg(feature = "medium-ip")] #[cfg(feature = "medium-ip")]
Medium::Ip => HardwareAddress::Ip, Medium::Ip => HardwareAddress::Ip,
#[cfg(feature = "medium-ieee802154")] #[cfg(feature = "medium-ieee802154")]
Medium::Ieee802154 => HardwareAddress::Ieee802154(Ieee802154Address::Extended(device.ieee802154_address())), Medium::Ieee802154 => device.hardware_address(),
#[allow(unreachable_patterns)] #[allow(unreachable_patterns)]
_ => panic!( _ => panic!(
"Unsupported medium {:?}. Make sure to enable it in embassy-net's Cargo features.", "Unsupported medium {:?}. Make sure to enable it in embassy-net's Cargo features.",
@ -336,9 +336,9 @@ impl<D: Driver + 'static> Stack<D> {
f(&mut *self.socket.borrow_mut(), &mut *self.inner.borrow_mut()) f(&mut *self.socket.borrow_mut(), &mut *self.inner.borrow_mut())
} }
/// Get the MAC address of the network interface. /// Get the hardware address of the network interface.
pub fn ethernet_address(&self) -> [u8; 6] { pub fn hardware_address(&self) -> HardwareAddress {
self.with(|_s, i| i.device.ethernet_address()) self.with(|_s, i| i.device.hardware_address())
} }
/// Get whether the link is up. /// Get whether the link is up.
@ -740,18 +740,11 @@ impl<D: Driver + 'static> Inner<D> {
fn poll(&mut self, cx: &mut Context<'_>, s: &mut SocketStack) { fn poll(&mut self, cx: &mut Context<'_>, s: &mut SocketStack) {
s.waker.register(cx.waker()); s.waker.register(cx.waker());
#[cfg(feature = "medium-ethernet")] #[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
if self.device.capabilities().medium == Medium::Ethernet { if self.device.capabilities().medium == Medium::Ethernet
s.iface.set_hardware_addr(HardwareAddress::Ethernet(EthernetAddress( || self.device.capabilities().medium == Medium::Ieee802154
self.device.ethernet_address(), {
))); s.iface.set_hardware_addr(self.device.hardware_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(),
)));
} }
let timestamp = instant_to_smoltcp(Instant::now()); let timestamp = instant_to_smoltcp(Instant::now());

View File

@ -73,10 +73,10 @@ impl<'d> embassy_net_driver::Driver for Driver<'d> {
LinkState::Down LinkState::Down
} }
fn ethernet_address(&self) -> [u8; 6] { fn hardware_address(&self) -> HardwareAddress {
// self.mac_addr // self.mac_addr
[0; 6] HardwareAddress::Ethernet(EthernetAddress([0; 6]))
} }
} }

View File

@ -4,6 +4,7 @@ use std::os::unix::io::{AsRawFd, RawFd};
use std::task::Context; use std::task::Context;
use async_io::Async; use async_io::Async;
use embassy_net::HardwareAddress;
use embassy_net_driver::{self, Capabilities, Driver, LinkState}; use embassy_net_driver::{self, Capabilities, Driver, LinkState};
use log::*; use log::*;
@ -180,8 +181,8 @@ impl Driver for TunTapDevice {
LinkState::Up LinkState::Up
} }
fn ethernet_address(&self) -> [u8; 6] { fn hardware_address(&self) -> HardwareAddress {
[0x02, 0x03, 0x04, 0x05, 0x06, 0x07] HardwareAddress::Ethernet(EthernetAddress([0x02, 0x03, 0x04, 0x05, 0x06, 0x07]))
} }
} }