From 4f528d8fae0c57fad5c5a57f5a61fd0fc4435044 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Mon, 12 Apr 2021 21:00:23 +0200 Subject: [PATCH] Add medium-ip, medium-ethernet Cargo features --- ci.sh | 9 ++++++--- embassy-net-examples/Cargo.toml | 5 +++-- embassy-net/Cargo.toml | 14 ++++---------- embassy-net/src/lib.rs | 7 ++++--- embassy-net/src/stack.rs | 23 +++++++++++++++++++---- 5 files changed, 36 insertions(+), 22 deletions(-) diff --git a/ci.sh b/ci.sh index 385379dec..f43ea5e1a 100755 --- a/ci.sh +++ b/ci.sh @@ -3,11 +3,14 @@ set -euxo pipefail # build for std -(cd embassy-net; cargo build --features log) +(cd embassy-net; cargo build --no-default-features --features log,medium-ethernet,tcp) +(cd embassy-net; cargo build --no-default-features --features log,medium-ethernet,tcp,dhcpv4) +(cd embassy-net; cargo build --no-default-features --features log,medium-ip,tcp) +(cd embassy-net; cargo build --no-default-features --features log,medium-ethernet,medium-ip,tcp,dhcpv4) # build for embedded -(cd embassy-net; cargo build --target thumbv7em-none-eabi --features log) -(cd embassy-net; cargo build --target thumbv7em-none-eabi --features defmt,smoltcp/defmt) +(cd embassy-net; cargo build --target thumbv7em-none-eabi --no-default-features --features log,medium-ethernet,medium-ip,tcp,dhcpv4) +(cd embassy-net; cargo build --target thumbv7em-none-eabi --no-default-features --features defmt,smoltcp/defmt,medium-ethernet,medium-ip,tcp,dhcpv4) # build examples (cd embassy-net-examples; cargo build) diff --git a/embassy-net-examples/Cargo.toml b/embassy-net-examples/Cargo.toml index 796c44745..0a63a3bbd 100644 --- a/embassy-net-examples/Cargo.toml +++ b/embassy-net-examples/Cargo.toml @@ -8,11 +8,12 @@ edition = "2018" heapless = { version = "0.5.6", default-features = false } embassy = { version = "0.1.0", features=["std", "log"] } embassy-std = { version = "0.1.0" } -embassy-net = { version = "0.1.0", path = "../embassy-net", features=["std", "log", "tcp", "dhcpv4"] } +embassy-net = { version = "0.1.0", path = "../embassy-net", features=["std", "log", "medium-ethernet", "tcp", "dhcpv4"] } env_logger = "0.8.2" log = "0.4.11" futures = "0.3.8" libc = "0.2.81" async-io = "1.3.1" smoltcp = { version = "0.7.0", default-features = false } -clap = { version = "3.0.0-beta.2", features = ["derive"] } \ No newline at end of file +clap = { version = "3.0.0-beta.2", features = ["derive"] } +rand_core = { version = "0.6.0", features = ["std"] } diff --git a/embassy-net/Cargo.toml b/embassy-net/Cargo.toml index dccd7984d..b419250be 100644 --- a/embassy-net/Cargo.toml +++ b/embassy-net/Cargo.toml @@ -13,7 +13,9 @@ defmt-warn = [] defmt-error = [] tcp = ["smoltcp/socket-tcp"] -dhcpv4 = ["smoltcp/socket-dhcpv4"] +dhcpv4 = ["medium-ethernet", "smoltcp/socket-dhcpv4"] +medium-ethernet = ["smoltcp/medium-ethernet"] +medium-ip = ["smoltcp/medium-ip"] [dependencies] @@ -36,15 +38,7 @@ version = "0.7.0" #rev = "00952e2c5cdf5667a1dfb6142258055f58d3851c" default-features = false features = [ - "medium-ethernet", - "medium-ip", "proto-ipv4", - #"proto-dhcpv4", - #"proto-igmp", - #"proto-ipv6", - #"socket-raw", - #"socket-icmp", - #"socket-udp", - #"socket-tcp", + "socket", "async", ] diff --git a/embassy-net/src/lib.rs b/embassy-net/src/lib.rs index edfb2c26d..efa5f3cb1 100644 --- a/embassy-net/src/lib.rs +++ b/embassy-net/src/lib.rs @@ -12,9 +12,10 @@ mod device; mod packet_pool; mod stack; -pub use config::{ - Config, Configurator, DhcpConfigurator, Event as ConfigEvent, StaticConfigurator, -}; +#[cfg(feature = "dhcpv4")] +pub use config::DhcpConfigurator; +pub use config::{Config, Configurator, Event as ConfigEvent, StaticConfigurator}; + pub use device::{Device, LinkState}; pub use packet_pool::{Packet, PacketBox, PacketBoxExt, PacketBuf}; pub use stack::{init, is_init, run}; diff --git a/embassy-net/src/stack.rs b/embassy-net/src/stack.rs index 9b0dd54d4..83cd71707 100644 --- a/embassy-net/src/stack.rs +++ b/embassy-net/src/stack.rs @@ -6,12 +6,16 @@ use embassy::time::{Instant, Timer}; use embassy::util::ThreadModeMutex; use embassy::util::{Forever, WakerRegistration}; use futures::pin_mut; -use smoltcp::iface::{InterfaceBuilder, Neighbor, NeighborCache, Route, Routes}; +use smoltcp::iface::InterfaceBuilder; +#[cfg(feature = "medium-ethernet")] +use smoltcp::iface::{Neighbor, NeighborCache, Route, Routes}; use smoltcp::phy::Device as _; use smoltcp::phy::Medium; use smoltcp::socket::SocketSetItem; use smoltcp::time::Instant as SmolInstant; -use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address, Ipv4Cidr}; +#[cfg(feature = "medium-ethernet")] +use smoltcp::wire::EthernetAddress; +use smoltcp::wire::{IpAddress, IpCidr, Ipv4Address, Ipv4Cidr}; use crate::config::Configurator; use crate::config::Event; @@ -27,9 +31,12 @@ const LOCAL_PORT_MAX: u16 = 65535; struct StackResources { addresses: [IpCidr; ADDRESSES_LEN], - neighbor_cache: [Option<(IpAddress, Neighbor)>; NEIGHBOR_CACHE_LEN], sockets: [Option>; SOCKETS_LEN], + + #[cfg(feature = "medium-ethernet")] routes: [Option<(IpCidr, Route)>; 1], + #[cfg(feature = "medium-ethernet")] + neighbor_cache: [Option<(IpAddress, Neighbor)>; NEIGHBOR_CACHE_LEN], } static STACK_RESOURCES: Forever = Forever::new(); @@ -79,6 +86,7 @@ impl Stack { debug!(" IP address: {}", config.address); set_ipv4_addr(&mut self.iface, config.address); + #[cfg(feature = "medium-ethernet")] if medium == Medium::Ethernet { if let Some(gateway) = config.gateway { debug!(" Default gateway: {}", gateway); @@ -98,6 +106,7 @@ impl Stack { Event::Deconfigured => { debug!("Lost IP configuration"); set_ipv4_addr(&mut self.iface, Ipv4Cidr::new(Ipv4Address::UNSPECIFIED, 0)); + #[cfg(feature = "medium-ethernet")] if medium == Medium::Ethernet { self.iface.routes_mut().remove_default_ipv4_route(); } @@ -156,12 +165,17 @@ pub fn init(device: &'static mut dyn Device, configurator: &'static mut dyn Conf const NONE_SOCKET: Option> = None; let res = STACK_RESOURCES.put(StackResources { addresses: [IpCidr::new(Ipv4Address::UNSPECIFIED.into(), 32)], - neighbor_cache: [None; NEIGHBOR_CACHE_LEN], sockets: [NONE_SOCKET; SOCKETS_LEN], + + #[cfg(feature = "medium-ethernet")] routes: [None; 1], + #[cfg(feature = "medium-ethernet")] + neighbor_cache: [None; NEIGHBOR_CACHE_LEN], }); let medium = device.capabilities().medium; + + #[cfg(feature = "medium-ethernet")] let ethernet_addr = if medium == Medium::Ethernet { device.ethernet_address() } else { @@ -171,6 +185,7 @@ pub fn init(device: &'static mut dyn Device, configurator: &'static mut dyn Conf let mut b = InterfaceBuilder::new(DeviceAdapter::new(device)); b = b.ip_addrs(&mut res.addresses[..]); + #[cfg(feature = "medium-ethernet")] if medium == Medium::Ethernet { b = b.ethernet_addr(EthernetAddress(ethernet_addr)); b = b.neighbor_cache(NeighborCache::new(&mut res.neighbor_cache[..]));