diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-04-12 21:00:23 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2021-04-12 21:00:23 +0200 |
| commit | 4f528d8fae0c57fad5c5a57f5a61fd0fc4435044 (patch) | |
| tree | b3f135aa2f35a2038726fa013c56351fba2b7a20 /embassy-net/src/stack.rs | |
| parent | 28c235d7866f2a0defaf235270ade6dbee10fc9e (diff) | |
Add medium-ip, medium-ethernet Cargo features
Diffstat (limited to 'embassy-net/src/stack.rs')
| -rw-r--r-- | embassy-net/src/stack.rs | 23 |
1 files changed, 19 insertions, 4 deletions
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}; | |||
| 6 | use embassy::util::ThreadModeMutex; | 6 | use embassy::util::ThreadModeMutex; |
| 7 | use embassy::util::{Forever, WakerRegistration}; | 7 | use embassy::util::{Forever, WakerRegistration}; |
| 8 | use futures::pin_mut; | 8 | use futures::pin_mut; |
| 9 | use smoltcp::iface::{InterfaceBuilder, Neighbor, NeighborCache, Route, Routes}; | 9 | use smoltcp::iface::InterfaceBuilder; |
| 10 | #[cfg(feature = "medium-ethernet")] | ||
| 11 | use smoltcp::iface::{Neighbor, NeighborCache, Route, Routes}; | ||
| 10 | use smoltcp::phy::Device as _; | 12 | use smoltcp::phy::Device as _; |
| 11 | use smoltcp::phy::Medium; | 13 | use smoltcp::phy::Medium; |
| 12 | use smoltcp::socket::SocketSetItem; | 14 | use smoltcp::socket::SocketSetItem; |
| 13 | use smoltcp::time::Instant as SmolInstant; | 15 | use smoltcp::time::Instant as SmolInstant; |
| 14 | use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address, Ipv4Cidr}; | 16 | #[cfg(feature = "medium-ethernet")] |
| 17 | use smoltcp::wire::EthernetAddress; | ||
| 18 | use smoltcp::wire::{IpAddress, IpCidr, Ipv4Address, Ipv4Cidr}; | ||
| 15 | 19 | ||
| 16 | use crate::config::Configurator; | 20 | use crate::config::Configurator; |
| 17 | use crate::config::Event; | 21 | use crate::config::Event; |
| @@ -27,9 +31,12 @@ const LOCAL_PORT_MAX: u16 = 65535; | |||
| 27 | 31 | ||
| 28 | struct StackResources { | 32 | struct StackResources { |
| 29 | addresses: [IpCidr; ADDRESSES_LEN], | 33 | addresses: [IpCidr; ADDRESSES_LEN], |
| 30 | neighbor_cache: [Option<(IpAddress, Neighbor)>; NEIGHBOR_CACHE_LEN], | ||
| 31 | sockets: [Option<SocketSetItem<'static>>; SOCKETS_LEN], | 34 | sockets: [Option<SocketSetItem<'static>>; SOCKETS_LEN], |
| 35 | |||
| 36 | #[cfg(feature = "medium-ethernet")] | ||
| 32 | routes: [Option<(IpCidr, Route)>; 1], | 37 | routes: [Option<(IpCidr, Route)>; 1], |
| 38 | #[cfg(feature = "medium-ethernet")] | ||
| 39 | neighbor_cache: [Option<(IpAddress, Neighbor)>; NEIGHBOR_CACHE_LEN], | ||
| 33 | } | 40 | } |
| 34 | 41 | ||
| 35 | static STACK_RESOURCES: Forever<StackResources> = Forever::new(); | 42 | static STACK_RESOURCES: Forever<StackResources> = Forever::new(); |
| @@ -79,6 +86,7 @@ impl Stack { | |||
| 79 | debug!(" IP address: {}", config.address); | 86 | debug!(" IP address: {}", config.address); |
| 80 | set_ipv4_addr(&mut self.iface, config.address); | 87 | set_ipv4_addr(&mut self.iface, config.address); |
| 81 | 88 | ||
| 89 | #[cfg(feature = "medium-ethernet")] | ||
| 82 | if medium == Medium::Ethernet { | 90 | if medium == Medium::Ethernet { |
| 83 | if let Some(gateway) = config.gateway { | 91 | if let Some(gateway) = config.gateway { |
| 84 | debug!(" Default gateway: {}", gateway); | 92 | debug!(" Default gateway: {}", gateway); |
| @@ -98,6 +106,7 @@ impl Stack { | |||
| 98 | Event::Deconfigured => { | 106 | Event::Deconfigured => { |
| 99 | debug!("Lost IP configuration"); | 107 | debug!("Lost IP configuration"); |
| 100 | set_ipv4_addr(&mut self.iface, Ipv4Cidr::new(Ipv4Address::UNSPECIFIED, 0)); | 108 | set_ipv4_addr(&mut self.iface, Ipv4Cidr::new(Ipv4Address::UNSPECIFIED, 0)); |
| 109 | #[cfg(feature = "medium-ethernet")] | ||
| 101 | if medium == Medium::Ethernet { | 110 | if medium == Medium::Ethernet { |
| 102 | self.iface.routes_mut().remove_default_ipv4_route(); | 111 | self.iface.routes_mut().remove_default_ipv4_route(); |
| 103 | } | 112 | } |
| @@ -156,12 +165,17 @@ pub fn init(device: &'static mut dyn Device, configurator: &'static mut dyn Conf | |||
| 156 | const NONE_SOCKET: Option<SocketSetItem<'static>> = None; | 165 | const NONE_SOCKET: Option<SocketSetItem<'static>> = None; |
| 157 | let res = STACK_RESOURCES.put(StackResources { | 166 | let res = STACK_RESOURCES.put(StackResources { |
| 158 | addresses: [IpCidr::new(Ipv4Address::UNSPECIFIED.into(), 32)], | 167 | addresses: [IpCidr::new(Ipv4Address::UNSPECIFIED.into(), 32)], |
| 159 | neighbor_cache: [None; NEIGHBOR_CACHE_LEN], | ||
| 160 | sockets: [NONE_SOCKET; SOCKETS_LEN], | 168 | sockets: [NONE_SOCKET; SOCKETS_LEN], |
| 169 | |||
| 170 | #[cfg(feature = "medium-ethernet")] | ||
| 161 | routes: [None; 1], | 171 | routes: [None; 1], |
| 172 | #[cfg(feature = "medium-ethernet")] | ||
| 173 | neighbor_cache: [None; NEIGHBOR_CACHE_LEN], | ||
| 162 | }); | 174 | }); |
| 163 | 175 | ||
| 164 | let medium = device.capabilities().medium; | 176 | let medium = device.capabilities().medium; |
| 177 | |||
| 178 | #[cfg(feature = "medium-ethernet")] | ||
| 165 | let ethernet_addr = if medium == Medium::Ethernet { | 179 | let ethernet_addr = if medium == Medium::Ethernet { |
| 166 | device.ethernet_address() | 180 | device.ethernet_address() |
| 167 | } else { | 181 | } else { |
| @@ -171,6 +185,7 @@ pub fn init(device: &'static mut dyn Device, configurator: &'static mut dyn Conf | |||
| 171 | let mut b = InterfaceBuilder::new(DeviceAdapter::new(device)); | 185 | let mut b = InterfaceBuilder::new(DeviceAdapter::new(device)); |
| 172 | b = b.ip_addrs(&mut res.addresses[..]); | 186 | b = b.ip_addrs(&mut res.addresses[..]); |
| 173 | 187 | ||
| 188 | #[cfg(feature = "medium-ethernet")] | ||
| 174 | if medium == Medium::Ethernet { | 189 | if medium == Medium::Ethernet { |
| 175 | b = b.ethernet_addr(EthernetAddress(ethernet_addr)); | 190 | b = b.ethernet_addr(EthernetAddress(ethernet_addr)); |
| 176 | b = b.neighbor_cache(NeighborCache::new(&mut res.neighbor_cache[..])); | 191 | b = b.neighbor_cache(NeighborCache::new(&mut res.neighbor_cache[..])); |
