diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-06-13 07:22:04 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-06-13 07:22:04 +0000 |
| commit | db685c04049449ac3e4f256f2e7e26dad550d94c (patch) | |
| tree | f4ec5de70ec05e793a774049e010935ac45853ed /embassy-net/src | |
| parent | fff0a03fe0f9e84209dd40fd8f93790871d03d75 (diff) | |
| parent | a8703b75988e1e700af701116464025679d2feb8 (diff) | |
Merge #808
808: Add rustfmt.toml with some nice settings. r=lulf a=Dirbaio
Co-authored-by: Dario Nieuwenhuis <[email protected]>
Diffstat (limited to 'embassy-net/src')
| -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 99c6a2212..c183bd58a 100644 --- a/embassy-net/src/device.rs +++ b/embassy-net/src/device.rs | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | use core::task::Waker; | 1 | use core::task::Waker; |
| 2 | use smoltcp::phy::Device as SmolDevice; | 2 | |
| 3 | use smoltcp::phy::DeviceCapabilities; | 3 | use smoltcp::phy::{Device as SmolDevice, DeviceCapabilities}; |
| 4 | use smoltcp::time::Instant as SmolInstant; | 4 | use smoltcp::time::Instant as SmolInstant; |
| 5 | 5 | ||
| 6 | use crate::packet_pool::PacketBoxExt; | 6 | use crate::packet_pool::PacketBoxExt; |
diff --git a/embassy-net/src/lib.rs b/embassy-net/src/lib.rs index 243dfda88..1c5ba103a 100644 --- a/embassy-net/src/lib.rs +++ b/embassy-net/src/lib.rs | |||
| @@ -18,11 +18,9 @@ pub mod tcp; | |||
| 18 | 18 | ||
| 19 | // smoltcp reexports | 19 | // smoltcp reexports |
| 20 | pub use smoltcp::phy::{DeviceCapabilities, Medium}; | 20 | pub use smoltcp::phy::{DeviceCapabilities, Medium}; |
| 21 | pub use smoltcp::time::Duration as SmolDuration; | 21 | pub use smoltcp::time::{Duration as SmolDuration, Instant as SmolInstant}; |
| 22 | pub use smoltcp::time::Instant as SmolInstant; | ||
| 23 | #[cfg(feature = "medium-ethernet")] | 22 | #[cfg(feature = "medium-ethernet")] |
| 24 | pub use smoltcp::wire::{EthernetAddress, HardwareAddress}; | 23 | pub use smoltcp::wire::{EthernetAddress, HardwareAddress}; |
| 25 | pub use smoltcp::wire::{IpAddress, IpCidr, Ipv4Address, Ipv4Cidr}; | 24 | pub use smoltcp::wire::{IpAddress, IpCidr, Ipv4Address, Ipv4Cidr}; |
| 26 | |||
| 27 | #[cfg(feature = "proto-ipv6")] | 25 | #[cfg(feature = "proto-ipv6")] |
| 28 | pub use smoltcp::wire::{Ipv6Address, Ipv6Cidr}; | 26 | pub use smoltcp::wire::{Ipv6Address, Ipv6Cidr}; |
diff --git a/embassy-net/src/packet_pool.rs b/embassy-net/src/packet_pool.rs index 99311ae74..cb8a1316c 100644 --- a/embassy-net/src/packet_pool.rs +++ b/embassy-net/src/packet_pool.rs | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | use as_slice::{AsMutSlice, AsSlice}; | ||
| 2 | use core::ops::{Deref, DerefMut, Range}; | 1 | use core::ops::{Deref, DerefMut, Range}; |
| 3 | 2 | ||
| 3 | use as_slice::{AsMutSlice, AsSlice}; | ||
| 4 | use atomic_pool::{pool, Box}; | 4 | use atomic_pool::{pool, Box}; |
| 5 | 5 | ||
| 6 | pub const MTU: usize = 1516; | 6 | pub const MTU: usize = 1516; |
| @@ -41,10 +41,7 @@ pub trait PacketBoxExt { | |||
| 41 | 41 | ||
| 42 | impl PacketBoxExt for PacketBox { | 42 | impl PacketBoxExt for PacketBox { |
| 43 | fn slice(self, range: Range<usize>) -> PacketBuf { | 43 | fn slice(self, range: Range<usize>) -> PacketBuf { |
| 44 | PacketBuf { | 44 | PacketBuf { packet: self, range } |
| 45 | packet: self, | ||
| 46 | range, | ||
| 47 | } | ||
| 48 | } | 45 | } |
| 49 | } | 46 | } |
| 50 | 47 | ||
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; |
diff --git a/embassy-net/src/tcp.rs b/embassy-net/src/tcp.rs index 35ecf1b0c..c18391ace 100644 --- a/embassy-net/src/tcp.rs +++ b/embassy-net/src/tcp.rs | |||
| @@ -2,18 +2,17 @@ use core::cell::UnsafeCell; | |||
| 2 | use core::future::Future; | 2 | use core::future::Future; |
| 3 | use core::mem; | 3 | use core::mem; |
| 4 | use core::task::Poll; | 4 | use core::task::Poll; |
| 5 | |||
| 5 | use futures::future::poll_fn; | 6 | use futures::future::poll_fn; |
| 6 | use smoltcp::iface::{Interface, SocketHandle}; | 7 | use smoltcp::iface::{Interface, SocketHandle}; |
| 7 | use smoltcp::socket::tcp; | 8 | use smoltcp::socket::tcp; |
| 8 | use smoltcp::time::Duration; | 9 | use smoltcp::time::Duration; |
| 9 | use smoltcp::wire::IpEndpoint; | 10 | use smoltcp::wire::{IpEndpoint, IpListenEndpoint}; |
| 10 | use smoltcp::wire::IpListenEndpoint; | ||
| 11 | 11 | ||
| 12 | use super::stack::Stack; | ||
| 12 | use crate::stack::SocketStack; | 13 | use crate::stack::SocketStack; |
| 13 | use crate::Device; | 14 | use crate::Device; |
| 14 | 15 | ||
| 15 | use super::stack::Stack; | ||
| 16 | |||
| 17 | #[derive(PartialEq, Eq, Clone, Copy, Debug)] | 16 | #[derive(PartialEq, Eq, Clone, Copy, Debug)] |
| 18 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 17 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 19 | pub enum Error { | 18 | pub enum Error { |
| @@ -57,11 +56,7 @@ pub struct TcpWriter<'a> { | |||
| 57 | } | 56 | } |
| 58 | 57 | ||
| 59 | impl<'a> TcpSocket<'a> { | 58 | impl<'a> TcpSocket<'a> { |
| 60 | pub fn new<D: Device>( | 59 | pub fn new<D: Device>(stack: &'a Stack<D>, rx_buffer: &'a mut [u8], tx_buffer: &'a mut [u8]) -> Self { |
| 61 | stack: &'a Stack<D>, | ||
| 62 | rx_buffer: &'a mut [u8], | ||
| 63 | tx_buffer: &'a mut [u8], | ||
| 64 | ) -> Self { | ||
| 65 | // safety: not accessed reentrantly. | 60 | // safety: not accessed reentrantly. |
| 66 | let s = unsafe { &mut *stack.socket.get() }; | 61 | let s = unsafe { &mut *stack.socket.get() }; |
| 67 | let rx_buffer: &'static mut [u8] = unsafe { mem::transmute(rx_buffer) }; | 62 | let rx_buffer: &'static mut [u8] = unsafe { mem::transmute(rx_buffer) }; |
| @@ -91,10 +86,7 @@ impl<'a> TcpSocket<'a> { | |||
| 91 | let local_port = unsafe { &mut *self.io.stack.get() }.get_local_port(); | 86 | let local_port = unsafe { &mut *self.io.stack.get() }.get_local_port(); |
| 92 | 87 | ||
| 93 | // safety: not accessed reentrantly. | 88 | // safety: not accessed reentrantly. |
| 94 | match unsafe { | 89 | match unsafe { self.io.with_mut(|s, i| s.connect(i, remote_endpoint, local_port)) } { |
| 95 | self.io | ||
| 96 | .with_mut(|s, i| s.connect(i, remote_endpoint, local_port)) | ||
| 97 | } { | ||
| 98 | Ok(()) => {} | 90 | Ok(()) => {} |
| 99 | Err(tcp::ConnectError::InvalidState) => return Err(ConnectError::InvalidState), | 91 | Err(tcp::ConnectError::InvalidState) => return Err(ConnectError::InvalidState), |
| 100 | Err(tcp::ConnectError::Unaddressable) => return Err(ConnectError::NoRoute), | 92 | Err(tcp::ConnectError::Unaddressable) => return Err(ConnectError::NoRoute), |
| @@ -102,9 +94,7 @@ impl<'a> TcpSocket<'a> { | |||
| 102 | 94 | ||
| 103 | futures::future::poll_fn(|cx| unsafe { | 95 | futures::future::poll_fn(|cx| unsafe { |
| 104 | self.io.with_mut(|s, _| match s.state() { | 96 | self.io.with_mut(|s, _| match s.state() { |
| 105 | tcp::State::Closed | tcp::State::TimeWait => { | 97 | tcp::State::Closed | tcp::State::TimeWait => Poll::Ready(Err(ConnectError::ConnectionReset)), |
| 106 | Poll::Ready(Err(ConnectError::ConnectionReset)) | ||
| 107 | } | ||
| 108 | tcp::State::Listen => unreachable!(), | 98 | tcp::State::Listen => unreachable!(), |
| 109 | tcp::State::SynSent | tcp::State::SynReceived => { | 99 | tcp::State::SynSent | tcp::State::SynReceived => { |
| 110 | s.register_send_waker(cx.waker()); | 100 | s.register_send_waker(cx.waker()); |
