aboutsummaryrefslogtreecommitdiff
path: root/embassy-net/src/stack.rs
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-04-12 21:00:23 +0200
committerDario Nieuwenhuis <[email protected]>2021-04-12 21:00:23 +0200
commit4f528d8fae0c57fad5c5a57f5a61fd0fc4435044 (patch)
treeb3f135aa2f35a2038726fa013c56351fba2b7a20 /embassy-net/src/stack.rs
parent28c235d7866f2a0defaf235270ade6dbee10fc9e (diff)
Add medium-ip, medium-ethernet Cargo features
Diffstat (limited to 'embassy-net/src/stack.rs')
-rw-r--r--embassy-net/src/stack.rs23
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};
6use embassy::util::ThreadModeMutex; 6use embassy::util::ThreadModeMutex;
7use embassy::util::{Forever, WakerRegistration}; 7use embassy::util::{Forever, WakerRegistration};
8use futures::pin_mut; 8use futures::pin_mut;
9use smoltcp::iface::{InterfaceBuilder, Neighbor, NeighborCache, Route, Routes}; 9use smoltcp::iface::InterfaceBuilder;
10#[cfg(feature = "medium-ethernet")]
11use smoltcp::iface::{Neighbor, NeighborCache, Route, Routes};
10use smoltcp::phy::Device as _; 12use smoltcp::phy::Device as _;
11use smoltcp::phy::Medium; 13use smoltcp::phy::Medium;
12use smoltcp::socket::SocketSetItem; 14use smoltcp::socket::SocketSetItem;
13use smoltcp::time::Instant as SmolInstant; 15use smoltcp::time::Instant as SmolInstant;
14use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address, Ipv4Cidr}; 16#[cfg(feature = "medium-ethernet")]
17use smoltcp::wire::EthernetAddress;
18use smoltcp::wire::{IpAddress, IpCidr, Ipv4Address, Ipv4Cidr};
15 19
16use crate::config::Configurator; 20use crate::config::Configurator;
17use crate::config::Event; 21use crate::config::Event;
@@ -27,9 +31,12 @@ const LOCAL_PORT_MAX: u16 = 65535;
27 31
28struct StackResources { 32struct 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
35static STACK_RESOURCES: Forever<StackResources> = Forever::new(); 42static 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[..]));