diff options
Diffstat (limited to 'embassy-net')
-rw-r--r-- | embassy-net/src/device.rs | 4 | ||||
-rw-r--r-- | embassy-net/src/lib.rs | 4 | ||||
-rw-r--r-- | embassy-net/src/packet_pool.rs | 7 | ||||
-rw-r--r-- | embassy-net/src/stack.rs | 39 | ||||
-rw-r--r-- | embassy-net/src/tcp.rs | 22 |
5 files changed, 23 insertions, 53 deletions
diff --git a/embassy-net/src/device.rs b/embassy-net/src/device.rs index 99c6a221..c183bd58 100644 --- a/embassy-net/src/device.rs +++ b/embassy-net/src/device.rs @@ -1,6 +1,6 @@ use core::task::Waker; -use smoltcp::phy::Device as SmolDevice; -use smoltcp::phy::DeviceCapabilities; + +use smoltcp::phy::{Device as SmolDevice, DeviceCapabilities}; use smoltcp::time::Instant as SmolInstant; use crate::packet_pool::PacketBoxExt; diff --git a/embassy-net/src/lib.rs b/embassy-net/src/lib.rs index 243dfda8..1c5ba103 100644 --- a/embassy-net/src/lib.rs +++ b/embassy-net/src/lib.rs @@ -18,11 +18,9 @@ pub mod tcp; // smoltcp reexports pub use smoltcp::phy::{DeviceCapabilities, Medium}; -pub use smoltcp::time::Duration as SmolDuration; -pub use smoltcp::time::Instant as SmolInstant; +pub use smoltcp::time::{Duration as SmolDuration, Instant as SmolInstant}; #[cfg(feature = "medium-ethernet")] pub use smoltcp::wire::{EthernetAddress, HardwareAddress}; pub use smoltcp::wire::{IpAddress, IpCidr, Ipv4Address, Ipv4Cidr}; - #[cfg(feature = "proto-ipv6")] pub use smoltcp::wire::{Ipv6Address, Ipv6Cidr}; diff --git a/embassy-net/src/packet_pool.rs b/embassy-net/src/packet_pool.rs index 99311ae7..cb8a1316 100644 --- a/embassy-net/src/packet_pool.rs +++ b/embassy-net/src/packet_pool.rs @@ -1,6 +1,6 @@ -use as_slice::{AsMutSlice, AsSlice}; use core::ops::{Deref, DerefMut, Range}; +use as_slice::{AsMutSlice, AsSlice}; use atomic_pool::{pool, Box}; pub const MTU: usize = 1516; @@ -41,10 +41,7 @@ pub trait PacketBoxExt { impl PacketBoxExt for PacketBox { fn slice(self, range: Range<usize>) -> PacketBuf { - PacketBuf { - packet: self, - range, - } + PacketBuf { packet: self, range } } } diff --git a/embassy-net/src/stack.rs b/embassy-net/src/stack.rs index e28370df..f3b1ff9d 100644 --- a/embassy-net/src/stack.rs +++ b/embassy-net/src/stack.rs @@ -1,7 +1,7 @@ use core::cell::UnsafeCell; use core::future::Future; -use core::task::Context; -use core::task::Poll; +use core::task::{Context, Poll}; + use embassy::time::{Instant, Timer}; use embassy::waitqueue::WakerRegistration; use futures::future::poll_fn; @@ -9,19 +9,17 @@ use futures::pin_mut; use heapless::Vec; #[cfg(feature = "dhcpv4")] use smoltcp::iface::SocketHandle; -use smoltcp::iface::{Interface, InterfaceBuilder}; -use smoltcp::iface::{SocketSet, SocketStorage}; -#[cfg(feature = "dhcpv4")] -use smoltcp::socket::dhcpv4; -use smoltcp::time::Instant as SmolInstant; -use smoltcp::wire::{IpCidr, Ipv4Address, Ipv4Cidr}; - +use smoltcp::iface::{Interface, InterfaceBuilder, SocketSet, SocketStorage}; #[cfg(feature = "medium-ethernet")] use smoltcp::iface::{Neighbor, NeighborCache, Route, Routes}; #[cfg(feature = "medium-ethernet")] use smoltcp::phy::{Device as _, Medium}; +#[cfg(feature = "dhcpv4")] +use smoltcp::socket::dhcpv4; +use smoltcp::time::Instant as SmolInstant; #[cfg(feature = "medium-ethernet")] use smoltcp::wire::{EthernetAddress, HardwareAddress, IpAddress}; +use smoltcp::wire::{IpCidr, Ipv4Address, Ipv4Cidr}; use crate::device::{Device, DeviceAdapter, LinkState}; @@ -38,9 +36,7 @@ pub struct StackResources<const ADDR: usize, const SOCK: usize, const NEIGHBOR: neighbor_cache: [Option<(IpAddress, Neighbor)>; NEIGHBOR], } -impl<const ADDR: usize, const SOCK: usize, const NEIGHBOR: usize> - StackResources<ADDR, SOCK, NEIGHBOR> -{ +impl<const ADDR: usize, const SOCK: usize, const NEIGHBOR: usize> StackResources<ADDR, SOCK, NEIGHBOR> { pub fn new() -> Self { Self { addresses: [IpCidr::new(Ipv4Address::UNSPECIFIED.into(), 32); ADDR], @@ -122,8 +118,7 @@ impl<D: Device + 'static> Stack<D> { let sockets = SocketSet::new(&mut resources.sockets[..]); - let next_local_port = - (random_seed % (LOCAL_PORT_MAX - LOCAL_PORT_MIN) as u64) as u16 + LOCAL_PORT_MIN; + let next_local_port = (random_seed % (LOCAL_PORT_MAX - LOCAL_PORT_MIN) as u64) as u16 + LOCAL_PORT_MIN; let mut inner = Inner { device, @@ -194,11 +189,7 @@ impl SocketStack { #[allow(clippy::absurd_extreme_comparisons)] pub fn get_local_port(&mut self) -> u16 { let res = self.next_local_port; - self.next_local_port = if res >= LOCAL_PORT_MAX { - LOCAL_PORT_MIN - } else { - res + 1 - }; + self.next_local_port = if res >= LOCAL_PORT_MAX { LOCAL_PORT_MIN } else { res + 1 }; res } } @@ -217,10 +208,7 @@ impl<D: Device + 'static> Inner<D> { if medium == Medium::Ethernet { if let Some(gateway) = config.gateway { debug!(" Default gateway: {}", gateway); - s.iface - .routes_mut() - .add_default_ipv4_route(gateway) - .unwrap(); + s.iface.routes_mut().add_default_ipv4_route(gateway).unwrap(); } else { debug!(" Default gateway: None"); s.iface.routes_mut().remove_default_ipv4_route(); @@ -259,10 +247,7 @@ impl<D: Device + 'static> Inner<D> { s.waker.register(cx.waker()); let timestamp = instant_to_smoltcp(Instant::now()); - if s.iface - .poll(timestamp, &mut self.device, &mut s.sockets) - .is_err() - { + if s.iface.poll(timestamp, &mut self.device, &mut s.sockets).is_err() { // If poll() returns error, it may not be done yet, so poll again later. cx.waker().wake_by_ref(); return; diff --git a/embassy-net/src/tcp.rs b/embassy-net/src/tcp.rs index 35ecf1b0..c18391ac 100644 --- a/embassy-net/src/tcp.rs +++ b/embassy-net/src/tcp.rs @@ -2,18 +2,17 @@ use core::cell::UnsafeCell; use core::future::Future; use core::mem; use core::task::Poll; + use futures::future::poll_fn; use smoltcp::iface::{Interface, SocketHandle}; use smoltcp::socket::tcp; use smoltcp::time::Duration; -use smoltcp::wire::IpEndpoint; -use smoltcp::wire::IpListenEndpoint; +use smoltcp::wire::{IpEndpoint, IpListenEndpoint}; +use super::stack::Stack; use crate::stack::SocketStack; use crate::Device; -use super::stack::Stack; - #[derive(PartialEq, Eq, Clone, Copy, Debug)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum Error { @@ -57,11 +56,7 @@ pub struct TcpWriter<'a> { } impl<'a> TcpSocket<'a> { - pub fn new<D: Device>( - stack: &'a Stack<D>, - rx_buffer: &'a mut [u8], - tx_buffer: &'a mut [u8], - ) -> Self { + pub fn new<D: Device>(stack: &'a Stack<D>, rx_buffer: &'a mut [u8], tx_buffer: &'a mut [u8]) -> Self { // safety: not accessed reentrantly. let s = unsafe { &mut *stack.socket.get() }; let rx_buffer: &'static mut [u8] = unsafe { mem::transmute(rx_buffer) }; @@ -91,10 +86,7 @@ impl<'a> TcpSocket<'a> { let local_port = unsafe { &mut *self.io.stack.get() }.get_local_port(); // safety: not accessed reentrantly. - match unsafe { - self.io - .with_mut(|s, i| s.connect(i, remote_endpoint, local_port)) - } { + match unsafe { self.io.with_mut(|s, i| s.connect(i, remote_endpoint, local_port)) } { Ok(()) => {} Err(tcp::ConnectError::InvalidState) => return Err(ConnectError::InvalidState), Err(tcp::ConnectError::Unaddressable) => return Err(ConnectError::NoRoute), @@ -102,9 +94,7 @@ impl<'a> TcpSocket<'a> { futures::future::poll_fn(|cx| unsafe { self.io.with_mut(|s, _| match s.state() { - tcp::State::Closed | tcp::State::TimeWait => { - Poll::Ready(Err(ConnectError::ConnectionReset)) - } + tcp::State::Closed | tcp::State::TimeWait => Poll::Ready(Err(ConnectError::ConnectionReset)), tcp::State::Listen => unreachable!(), tcp::State::SynSent | tcp::State::SynReceived => { s.register_send_waker(cx.waker()); |