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 {
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 {

View File

@ -21,4 +21,5 @@ target = "thumbv7em-none-eabi"
features = ["defmt"]
[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 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<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 {
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)
}
}

View File

@ -243,11 +243,11 @@ impl<D: Driver + 'static> Stack<D> {
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<D: Driver + 'static> Stack<D> {
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<D: Driver + 'static> Inner<D> {
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());

View File

@ -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]))
}
}

View File

@ -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]))
}
}