diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-11-26 04:12:14 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2021-11-26 04:12:14 +0100 |
| commit | c257893da9f31337861a59e71022609e1bbaad95 (patch) | |
| tree | 802bdbe7c395579481ce368874918a577434148a /embassy-net/src/stack.rs | |
| parent | 539c007b44bb67feee6db7d021887faa14c62e8b (diff) | |
net: update smoltcp
Diffstat (limited to 'embassy-net/src/stack.rs')
| -rw-r--r-- | embassy-net/src/stack.rs | 31 |
1 files changed, 11 insertions, 20 deletions
diff --git a/embassy-net/src/stack.rs b/embassy-net/src/stack.rs index 4faf94953..610260a8e 100644 --- a/embassy-net/src/stack.rs +++ b/embassy-net/src/stack.rs | |||
| @@ -7,31 +7,31 @@ use embassy::time::{Instant, Timer}; | |||
| 7 | use embassy::waitqueue::WakerRegistration; | 7 | use embassy::waitqueue::WakerRegistration; |
| 8 | use futures::pin_mut; | 8 | use futures::pin_mut; |
| 9 | use smoltcp::iface::InterfaceBuilder; | 9 | use smoltcp::iface::InterfaceBuilder; |
| 10 | use smoltcp::iface::SocketStorage; | ||
| 10 | #[cfg(feature = "medium-ethernet")] | 11 | #[cfg(feature = "medium-ethernet")] |
| 11 | use smoltcp::iface::{Neighbor, NeighborCache, Route, Routes}; | 12 | use smoltcp::iface::{Neighbor, NeighborCache, Route, Routes}; |
| 12 | #[cfg(feature = "medium-ethernet")] | 13 | #[cfg(feature = "medium-ethernet")] |
| 13 | use smoltcp::phy::Device as _; | 14 | use smoltcp::phy::Device as _; |
| 14 | #[cfg(feature = "medium-ethernet")] | 15 | #[cfg(feature = "medium-ethernet")] |
| 15 | use smoltcp::phy::Medium; | 16 | use smoltcp::phy::Medium; |
| 16 | use smoltcp::socket::SocketSetItem; | ||
| 17 | use smoltcp::time::Instant as SmolInstant; | 17 | use smoltcp::time::Instant as SmolInstant; |
| 18 | #[cfg(feature = "medium-ethernet")] | 18 | #[cfg(feature = "medium-ethernet")] |
| 19 | use smoltcp::wire::EthernetAddress; | 19 | use smoltcp::wire::EthernetAddress; |
| 20 | #[cfg(feature = "medium-ethernet")] | 20 | #[cfg(feature = "medium-ethernet")] |
| 21 | use smoltcp::wire::IpAddress; | 21 | use smoltcp::wire::IpAddress; |
| 22 | use smoltcp::wire::{IpCidr, Ipv4Address, Ipv4Cidr}; | 22 | use smoltcp::wire::{HardwareAddress, IpCidr, Ipv4Address, Ipv4Cidr}; |
| 23 | 23 | ||
| 24 | use crate::config::Configurator; | 24 | use crate::config::Configurator; |
| 25 | use crate::config::Event; | 25 | use crate::config::Event; |
| 26 | use crate::device::{Device, DeviceAdapter, LinkState}; | 26 | use crate::device::{Device, DeviceAdapter, LinkState}; |
| 27 | use crate::{Interface, SocketSet}; | 27 | use crate::Interface; |
| 28 | 28 | ||
| 29 | const LOCAL_PORT_MIN: u16 = 1025; | 29 | const LOCAL_PORT_MIN: u16 = 1025; |
| 30 | const LOCAL_PORT_MAX: u16 = 65535; | 30 | const LOCAL_PORT_MAX: u16 = 65535; |
| 31 | 31 | ||
| 32 | pub struct StackResources<const ADDR: usize, const SOCK: usize, const NEIGHBOR: usize> { | 32 | pub struct StackResources<const ADDR: usize, const SOCK: usize, const NEIGHBOR: usize> { |
| 33 | addresses: [IpCidr; ADDR], | 33 | addresses: [IpCidr; ADDR], |
| 34 | sockets: [Option<SocketSetItem<'static>>; SOCK], | 34 | sockets: [SocketStorage<'static>; SOCK], |
| 35 | 35 | ||
| 36 | #[cfg(feature = "medium-ethernet")] | 36 | #[cfg(feature = "medium-ethernet")] |
| 37 | routes: [Option<(IpCidr, Route)>; 1], | 37 | routes: [Option<(IpCidr, Route)>; 1], |
| @@ -43,11 +43,9 @@ impl<const ADDR: usize, const SOCK: usize, const NEIGHBOR: usize> | |||
| 43 | StackResources<ADDR, SOCK, NEIGHBOR> | 43 | StackResources<ADDR, SOCK, NEIGHBOR> |
| 44 | { | 44 | { |
| 45 | pub fn new() -> Self { | 45 | pub fn new() -> Self { |
| 46 | const NONE_SOCKET: Option<SocketSetItem<'static>> = None; | ||
| 47 | |||
| 48 | Self { | 46 | Self { |
| 49 | addresses: [IpCidr::new(Ipv4Address::UNSPECIFIED.into(), 32); ADDR], | 47 | addresses: [IpCidr::new(Ipv4Address::UNSPECIFIED.into(), 32); ADDR], |
| 50 | sockets: [NONE_SOCKET; SOCK], | 48 | sockets: [SocketStorage::EMPTY; SOCK], |
| 51 | #[cfg(feature = "medium-ethernet")] | 49 | #[cfg(feature = "medium-ethernet")] |
| 52 | routes: [None; 1], | 50 | routes: [None; 1], |
| 53 | #[cfg(feature = "medium-ethernet")] | 51 | #[cfg(feature = "medium-ethernet")] |
| @@ -59,8 +57,7 @@ impl<const ADDR: usize, const SOCK: usize, const NEIGHBOR: usize> | |||
| 59 | static STACK: ThreadModeMutex<RefCell<Option<Stack>>> = ThreadModeMutex::new(RefCell::new(None)); | 57 | static STACK: ThreadModeMutex<RefCell<Option<Stack>>> = ThreadModeMutex::new(RefCell::new(None)); |
| 60 | 58 | ||
| 61 | pub(crate) struct Stack { | 59 | pub(crate) struct Stack { |
| 62 | iface: Interface, | 60 | pub iface: Interface, |
| 63 | pub sockets: SocketSet, | ||
| 64 | link_up: bool, | 61 | link_up: bool, |
| 65 | config_up: bool, | 62 | config_up: bool, |
| 66 | next_local_port: u16, | 63 | next_local_port: u16, |
| @@ -94,10 +91,7 @@ impl Stack { | |||
| 94 | #[cfg(feature = "medium-ethernet")] | 91 | #[cfg(feature = "medium-ethernet")] |
| 95 | let medium = self.iface.device().capabilities().medium; | 92 | let medium = self.iface.device().capabilities().medium; |
| 96 | 93 | ||
| 97 | match self | 94 | match self.configurator.poll(&mut self.iface, timestamp) { |
| 98 | .configurator | ||
| 99 | .poll(&mut self.iface, &mut self.sockets, timestamp) | ||
| 100 | { | ||
| 101 | Event::NoChange => {} | 95 | Event::NoChange => {} |
| 102 | Event::Configured(config) => { | 96 | Event::Configured(config) => { |
| 103 | debug!("Acquired IP configuration:"); | 97 | debug!("Acquired IP configuration:"); |
| @@ -141,7 +135,7 @@ impl Stack { | |||
| 141 | self.waker.register(cx.waker()); | 135 | self.waker.register(cx.waker()); |
| 142 | 136 | ||
| 143 | let timestamp = instant_to_smoltcp(Instant::now()); | 137 | let timestamp = instant_to_smoltcp(Instant::now()); |
| 144 | if self.iface.poll(&mut self.sockets, timestamp).is_err() { | 138 | if self.iface.poll(timestamp).is_err() { |
| 145 | // If poll() returns error, it may not be done yet, so poll again later. | 139 | // If poll() returns error, it may not be done yet, so poll again later. |
| 146 | cx.waker().wake_by_ref(); | 140 | cx.waker().wake_by_ref(); |
| 147 | return; | 141 | return; |
| @@ -160,7 +154,7 @@ impl Stack { | |||
| 160 | self.poll_configurator(timestamp) | 154 | self.poll_configurator(timestamp) |
| 161 | } | 155 | } |
| 162 | 156 | ||
| 163 | if let Some(poll_at) = self.iface.poll_at(&self.sockets, timestamp) { | 157 | if let Some(poll_at) = self.iface.poll_at(timestamp) { |
| 164 | let t = Timer::at(instant_from_smoltcp(poll_at)); | 158 | let t = Timer::at(instant_from_smoltcp(poll_at)); |
| 165 | pin_mut!(t); | 159 | pin_mut!(t); |
| 166 | if t.poll(cx).is_ready() { | 160 | if t.poll(cx).is_ready() { |
| @@ -194,20 +188,18 @@ pub fn init<const ADDR: usize, const SOCK: usize, const NEIGH: usize>( | |||
| 194 | [0, 0, 0, 0, 0, 0] | 188 | [0, 0, 0, 0, 0, 0] |
| 195 | }; | 189 | }; |
| 196 | 190 | ||
| 197 | let mut b = InterfaceBuilder::new(DeviceAdapter::new(device)); | 191 | let mut b = InterfaceBuilder::new(DeviceAdapter::new(device), &mut resources.sockets[..]); |
| 198 | b = b.ip_addrs(&mut resources.addresses[..]); | 192 | b = b.ip_addrs(&mut resources.addresses[..]); |
| 199 | 193 | ||
| 200 | #[cfg(feature = "medium-ethernet")] | 194 | #[cfg(feature = "medium-ethernet")] |
| 201 | if medium == Medium::Ethernet { | 195 | if medium == Medium::Ethernet { |
| 202 | b = b.ethernet_addr(EthernetAddress(ethernet_addr)); | 196 | b = b.hardware_addr(HardwareAddress::Ethernet(EthernetAddress(ethernet_addr))); |
| 203 | b = b.neighbor_cache(NeighborCache::new(&mut resources.neighbor_cache[..])); | 197 | b = b.neighbor_cache(NeighborCache::new(&mut resources.neighbor_cache[..])); |
| 204 | b = b.routes(Routes::new(&mut resources.routes[..])); | 198 | b = b.routes(Routes::new(&mut resources.routes[..])); |
| 205 | } | 199 | } |
| 206 | 200 | ||
| 207 | let iface = b.finalize(); | 201 | let iface = b.finalize(); |
| 208 | 202 | ||
| 209 | let sockets = SocketSet::new(&mut resources.sockets[..]); | ||
| 210 | |||
| 211 | let local_port = loop { | 203 | let local_port = loop { |
| 212 | let mut res = [0u8; 2]; | 204 | let mut res = [0u8; 2]; |
| 213 | rand(&mut res); | 205 | rand(&mut res); |
| @@ -219,7 +211,6 @@ pub fn init<const ADDR: usize, const SOCK: usize, const NEIGH: usize>( | |||
| 219 | 211 | ||
| 220 | let stack = Stack { | 212 | let stack = Stack { |
| 221 | iface, | 213 | iface, |
| 222 | sockets, | ||
| 223 | link_up: false, | 214 | link_up: false, |
| 224 | config_up: false, | 215 | config_up: false, |
| 225 | configurator, | 216 | configurator, |
