diff options
Diffstat (limited to 'embassy-net/src/stack.rs')
| -rw-r--r-- | embassy-net/src/stack.rs | 39 |
1 files changed, 12 insertions, 27 deletions
diff --git a/embassy-net/src/stack.rs b/embassy-net/src/stack.rs index e28370df8..f3b1ff9d4 100644 --- a/embassy-net/src/stack.rs +++ b/embassy-net/src/stack.rs | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | use core::cell::UnsafeCell; | 1 | use core::cell::UnsafeCell; |
| 2 | use core::future::Future; | 2 | use core::future::Future; |
| 3 | use core::task::Context; | 3 | use core::task::{Context, Poll}; |
| 4 | use core::task::Poll; | 4 | |
| 5 | use embassy::time::{Instant, Timer}; | 5 | use embassy::time::{Instant, Timer}; |
| 6 | use embassy::waitqueue::WakerRegistration; | 6 | use embassy::waitqueue::WakerRegistration; |
| 7 | use futures::future::poll_fn; | 7 | use futures::future::poll_fn; |
| @@ -9,19 +9,17 @@ use futures::pin_mut; | |||
| 9 | use heapless::Vec; | 9 | use heapless::Vec; |
| 10 | #[cfg(feature = "dhcpv4")] | 10 | #[cfg(feature = "dhcpv4")] |
| 11 | use smoltcp::iface::SocketHandle; | 11 | use smoltcp::iface::SocketHandle; |
| 12 | use smoltcp::iface::{Interface, InterfaceBuilder}; | 12 | use smoltcp::iface::{Interface, InterfaceBuilder, SocketSet, SocketStorage}; |
| 13 | use smoltcp::iface::{SocketSet, SocketStorage}; | ||
| 14 | #[cfg(feature = "dhcpv4")] | ||
| 15 | use smoltcp::socket::dhcpv4; | ||
| 16 | use smoltcp::time::Instant as SmolInstant; | ||
| 17 | use smoltcp::wire::{IpCidr, Ipv4Address, Ipv4Cidr}; | ||
| 18 | |||
| 19 | #[cfg(feature = "medium-ethernet")] | 13 | #[cfg(feature = "medium-ethernet")] |
| 20 | use smoltcp::iface::{Neighbor, NeighborCache, Route, Routes}; | 14 | use smoltcp::iface::{Neighbor, NeighborCache, Route, Routes}; |
| 21 | #[cfg(feature = "medium-ethernet")] | 15 | #[cfg(feature = "medium-ethernet")] |
| 22 | use smoltcp::phy::{Device as _, Medium}; | 16 | use smoltcp::phy::{Device as _, Medium}; |
| 17 | #[cfg(feature = "dhcpv4")] | ||
| 18 | use smoltcp::socket::dhcpv4; | ||
| 19 | use smoltcp::time::Instant as SmolInstant; | ||
| 23 | #[cfg(feature = "medium-ethernet")] | 20 | #[cfg(feature = "medium-ethernet")] |
| 24 | use smoltcp::wire::{EthernetAddress, HardwareAddress, IpAddress}; | 21 | use smoltcp::wire::{EthernetAddress, HardwareAddress, IpAddress}; |
| 22 | use smoltcp::wire::{IpCidr, Ipv4Address, Ipv4Cidr}; | ||
| 25 | 23 | ||
| 26 | use crate::device::{Device, DeviceAdapter, LinkState}; | 24 | use crate::device::{Device, DeviceAdapter, LinkState}; |
| 27 | 25 | ||
| @@ -38,9 +36,7 @@ pub struct StackResources<const ADDR: usize, const SOCK: usize, const NEIGHBOR: | |||
| 38 | neighbor_cache: [Option<(IpAddress, Neighbor)>; NEIGHBOR], | 36 | neighbor_cache: [Option<(IpAddress, Neighbor)>; NEIGHBOR], |
| 39 | } | 37 | } |
| 40 | 38 | ||
| 41 | impl<const ADDR: usize, const SOCK: usize, const NEIGHBOR: usize> | 39 | impl<const ADDR: usize, const SOCK: usize, const NEIGHBOR: usize> StackResources<ADDR, SOCK, NEIGHBOR> { |
| 42 | StackResources<ADDR, SOCK, NEIGHBOR> | ||
| 43 | { | ||
| 44 | pub fn new() -> Self { | 40 | pub fn new() -> Self { |
| 45 | Self { | 41 | Self { |
| 46 | addresses: [IpCidr::new(Ipv4Address::UNSPECIFIED.into(), 32); ADDR], | 42 | addresses: [IpCidr::new(Ipv4Address::UNSPECIFIED.into(), 32); ADDR], |
| @@ -122,8 +118,7 @@ impl<D: Device + 'static> Stack<D> { | |||
| 122 | 118 | ||
| 123 | let sockets = SocketSet::new(&mut resources.sockets[..]); | 119 | let sockets = SocketSet::new(&mut resources.sockets[..]); |
| 124 | 120 | ||
| 125 | let next_local_port = | 121 | let next_local_port = (random_seed % (LOCAL_PORT_MAX - LOCAL_PORT_MIN) as u64) as u16 + LOCAL_PORT_MIN; |
| 126 | (random_seed % (LOCAL_PORT_MAX - LOCAL_PORT_MIN) as u64) as u16 + LOCAL_PORT_MIN; | ||
| 127 | 122 | ||
| 128 | let mut inner = Inner { | 123 | let mut inner = Inner { |
| 129 | device, | 124 | device, |
| @@ -194,11 +189,7 @@ impl SocketStack { | |||
| 194 | #[allow(clippy::absurd_extreme_comparisons)] | 189 | #[allow(clippy::absurd_extreme_comparisons)] |
| 195 | pub fn get_local_port(&mut self) -> u16 { | 190 | pub fn get_local_port(&mut self) -> u16 { |
| 196 | let res = self.next_local_port; | 191 | let res = self.next_local_port; |
| 197 | self.next_local_port = if res >= LOCAL_PORT_MAX { | 192 | self.next_local_port = if res >= LOCAL_PORT_MAX { LOCAL_PORT_MIN } else { res + 1 }; |
| 198 | LOCAL_PORT_MIN | ||
| 199 | } else { | ||
| 200 | res + 1 | ||
| 201 | }; | ||
| 202 | res | 193 | res |
| 203 | } | 194 | } |
| 204 | } | 195 | } |
| @@ -217,10 +208,7 @@ impl<D: Device + 'static> Inner<D> { | |||
| 217 | if medium == Medium::Ethernet { | 208 | if medium == Medium::Ethernet { |
| 218 | if let Some(gateway) = config.gateway { | 209 | if let Some(gateway) = config.gateway { |
| 219 | debug!(" Default gateway: {}", gateway); | 210 | debug!(" Default gateway: {}", gateway); |
| 220 | s.iface | 211 | s.iface.routes_mut().add_default_ipv4_route(gateway).unwrap(); |
| 221 | .routes_mut() | ||
| 222 | .add_default_ipv4_route(gateway) | ||
| 223 | .unwrap(); | ||
| 224 | } else { | 212 | } else { |
| 225 | debug!(" Default gateway: None"); | 213 | debug!(" Default gateway: None"); |
| 226 | s.iface.routes_mut().remove_default_ipv4_route(); | 214 | s.iface.routes_mut().remove_default_ipv4_route(); |
| @@ -259,10 +247,7 @@ impl<D: Device + 'static> Inner<D> { | |||
| 259 | s.waker.register(cx.waker()); | 247 | s.waker.register(cx.waker()); |
| 260 | 248 | ||
| 261 | let timestamp = instant_to_smoltcp(Instant::now()); | 249 | let timestamp = instant_to_smoltcp(Instant::now()); |
| 262 | if s.iface | 250 | if s.iface.poll(timestamp, &mut self.device, &mut s.sockets).is_err() { |
| 263 | .poll(timestamp, &mut self.device, &mut s.sockets) | ||
| 264 | .is_err() | ||
| 265 | { | ||
| 266 | // If poll() returns error, it may not be done yet, so poll again later. | 251 | // If poll() returns error, it may not be done yet, so poll again later. |
| 267 | cx.waker().wake_by_ref(); | 252 | cx.waker().wake_by_ref(); |
| 268 | return; | 253 | return; |
