Introduce driver::HardwareAddress without smoltcp dependency

This commit is contained in:
Ruben De Smet 2023-07-31 10:40:48 +02:00
parent 69c0a89aa5
commit 4afdce4ec5
No known key found for this signature in database
GPG key ID: 1AE26A210C14115B
11 changed files with 54 additions and 26 deletions

View file

@ -216,7 +216,7 @@ where
PWR: OutputPin,
SPI: SpiBusCyw43,
{
let (ch_runner, device) = ch::new(&mut state.ch, [0; 6]);
let (ch_runner, device) = ch::new(&mut state.ch, ch::driver::HardwareAddress::Ethernet([0; 6]));
let state_ch = ch_runner.state_runner();
let mut runner = Runner::new(ch_runner, Bus::new(pwr, spi), &state.ioctl_state, &state.events);

View file

@ -42,7 +42,7 @@ struct StateInner<'d, const MTU: usize> {
struct Shared {
link_state: LinkState,
waker: WakerRegistration,
hardware_address: HardwareAddress,
hardware_address: driver::HardwareAddress,
}
pub struct Runner<'d, const MTU: usize> {
@ -85,7 +85,7 @@ impl<'d, const MTU: usize> Runner<'d, MTU> {
});
}
pub fn set_hardware_address(&mut self, address: HardwareAddress) {
pub fn set_hardware_address(&mut self, address: driver::HardwareAddress) {
self.shared.lock(|s| {
let s = &mut *s.borrow_mut();
s.hardware_address = address;
@ -150,7 +150,15 @@ impl<'d> StateRunner<'d> {
pub fn set_ethernet_address(&self, address: [u8; 6]) {
self.shared.lock(|s| {
let s = &mut *s.borrow_mut();
s.ethernet_address = address;
s.hardware_address = driver::HardwareAddress::Ethernet(address);
s.waker.wake();
});
}
pub fn set_ieee802154_address(&self, address: [u8; 8]) {
self.shared.lock(|s| {
let s = &mut *s.borrow_mut();
s.hardware_address = driver::HardwareAddress::Ieee802154(address);
s.waker.wake();
});
}
@ -206,8 +214,7 @@ impl<'d, const MTU: usize> TxRunner<'d, MTU> {
pub fn new<'d, const MTU: usize, const N_RX: usize, const N_TX: usize>(
state: &'d mut State<MTU, N_RX, N_TX>,
ethernet_address: [u8; 6],
ieee802154_address: [u8; 8],
hardware_address: driver::HardwareAddress,
) -> (Runner<'d, MTU>, Device<'d, MTU>) {
let mut caps = Capabilities::default();
caps.max_transmission_unit = MTU;
@ -223,8 +230,7 @@ pub fn new<'d, const MTU: usize, const N_RX: usize, const N_TX: usize>(
tx: zerocopy_channel::Channel::new(&mut state.tx[..]),
shared: Mutex::new(RefCell::new(Shared {
link_state: LinkState::Down,
ethernet_address,
ieee802154_address,
hardware_address,
waker: WakerRegistration::new(),
})),
});
@ -291,7 +297,7 @@ impl<'d, const MTU: usize> embassy_net_driver::Driver for Device<'d, MTU> {
self.caps.clone()
}
fn hardware_address(&self) -> HardwareAddress {
fn hardware_address(&self) -> driver::HardwareAddress {
self.shared.lock(|s| s.borrow().hardware_address)
}

View file

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

View file

@ -4,7 +4,15 @@
use core::task::Context;
use smoltcp::wire::HardwareAddress;
/// Representation of an hardware address, such as an Ethernet address or an IEEE802.15.4 address.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum HardwareAddress {
/// A six-octet Ethernet address
Ethernet([u8; 6]),
/// An eight-octet IEEE802.15.4 address
Ieee802154([u8; 8]),
}
/// Main `embassy-net` driver API.
///

View file

@ -124,7 +124,7 @@ where
IN: InputPin + Wait,
OUT: OutputPin,
{
let (ch_runner, device) = ch::new(&mut state.ch, [0; 6]);
let (ch_runner, device) = ch::new(&mut state.ch, ch::driver::HardwareAddress::Ethernet([0; 6]));
let state_ch = ch_runner.state_runner();
let mut runner = Runner {

View file

@ -96,7 +96,7 @@ pub async fn new<'a, const N_RX: usize, const N_TX: usize, SPI: SpiDevice, INT:
let mac = W5500::new(spi_dev, mac_addr).await.unwrap();
let (runner, device) = ch::new(&mut state.ch_state, mac_addr);
let (runner, device) = ch::new(&mut state.ch_state, ch::driver::HardwareAddress::Ethernet(mac_addr));
(
device,
Runner {

View file

@ -230,6 +230,18 @@ pub(crate) struct SocketStack {
next_local_port: u16,
}
fn to_smoltcp_hardware_address(addr: driver::HardwareAddress) -> HardwareAddress {
match addr {
#[cfg(feature = "medium-ethernet")]
driver::HardwareAddress::Ethernet(eth) => HardwareAddress::Ethernet(EthernetAddress(eth)),
#[cfg(feature = "medium-ieee802154")]
driver::HardwareAddress::Ieee802154(ieee) => HardwareAddress::Ieee802154(Ieee802154Address::Extended(ieee)),
#[allow(unreachable_patterns)]
_ => panic!("Unsupported address {:?}. Make sure to enable medium-ethernet or medium-ieee802154 in embassy-net's Cargo features.", addr),
}
}
impl<D: Driver + 'static> Stack<D> {
/// Create a new network stack.
pub fn new<const SOCK: usize>(
@ -243,11 +255,11 @@ impl<D: Driver + 'static> Stack<D> {
let hardware_addr = match medium {
#[cfg(feature = "medium-ethernet")]
Medium::Ethernet => device.hardware_address(),
Medium::Ethernet => to_smoltcp_hardware_address(device.hardware_address()),
#[cfg(feature = "medium-ip")]
Medium::Ip => HardwareAddress::Ip,
#[cfg(feature = "medium-ieee802154")]
Medium::Ieee802154 => device.hardware_address(),
Medium::Ieee802154 => to_smoltcp_hardware_address(device.hardware_address()),
#[allow(unreachable_patterns)]
_ => panic!(
"Unsupported medium {:?}. Make sure to enable it in embassy-net's Cargo features.",
@ -338,7 +350,7 @@ impl<D: Driver + 'static> Stack<D> {
/// Get the hardware address of the network interface.
pub fn hardware_address(&self) -> HardwareAddress {
self.with(|_s, i| i.device.hardware_address())
self.with(|_s, i| to_smoltcp_hardware_address(i.device.hardware_address()))
}
/// Get whether the link is up.
@ -744,7 +756,8 @@ impl<D: Driver + 'static> Inner<D> {
if self.device.capabilities().medium == Medium::Ethernet
|| self.device.capabilities().medium == Medium::Ieee802154
{
s.iface.set_hardware_addr(self.device.hardware_address());
s.iface
.set_hardware_addr(to_smoltcp_hardware_address(self.device.hardware_address()));
}
let timestamp = instant_to_smoltcp(Instant::now());

View file

@ -3,7 +3,7 @@
use core::task::Context;
use embassy_net_driver::{Capabilities, LinkState, Medium};
use embassy_net_driver::{Capabilities, HardwareAddress, LinkState, Medium};
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::channel::Channel;
@ -76,7 +76,7 @@ impl<'d> embassy_net_driver::Driver for Driver<'d> {
fn hardware_address(&self) -> HardwareAddress {
// self.mac_addr
HardwareAddress::Ethernet(EthernetAddress([0; 6]))
HardwareAddress::Ieee802154([0; 8])
}
}

View file

@ -8,7 +8,7 @@ pub mod generic_smi;
use core::mem::MaybeUninit;
use core::task::Context;
use embassy_net_driver::{Capabilities, LinkState};
use embassy_net_driver::{Capabilities, HardwareAddress, LinkState};
use embassy_sync::waitqueue::AtomicWaker;
pub use self::_version::{InterruptHandler, *};
@ -88,8 +88,8 @@ impl<'d, T: Instance, P: PHY> embassy_net_driver::Driver for Ethernet<'d, T, P>
}
}
fn ethernet_address(&self) -> [u8; 6] {
self.mac_addr
fn hardware_address(&self) -> HardwareAddress {
HardwareAddress::Ethernet(self.mac_addr)
}
}

View file

@ -87,7 +87,10 @@ impl<'d, D: Driver<'d>> CdcNcmClass<'d, D> {
ethernet_address: [u8; 6],
) -> (Runner<'d, D, MTU>, Device<'d, MTU>) {
let (tx_usb, rx_usb) = self.split();
let (runner, device) = ch::new(&mut state.ch_state, ethernet_address);
let (runner, device) = ch::new(
&mut state.ch_state,
ch::driver::HardwareAddress::Ethernet(ethernet_address),
);
(
Runner {

View file

@ -4,8 +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 embassy_net_driver::{self, Capabilities, Driver, HardwareAddress, LinkState};
use log::*;
pub const SIOCGIFMTU: libc::c_ulong = 0x8921;
@ -182,7 +181,7 @@ impl Driver for TunTapDevice {
}
fn hardware_address(&self) -> HardwareAddress {
HardwareAddress::Ethernet(EthernetAddress([0x02, 0x03, 0x04, 0x05, 0x06, 0x07]))
HardwareAddress::Ethernet([0x02, 0x03, 0x04, 0x05, 0x06, 0x07])
}
}