aboutsummaryrefslogtreecommitdiff
path: root/embassy-net/src
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-06-13 07:22:04 +0000
committerGitHub <[email protected]>2022-06-13 07:22:04 +0000
commitdb685c04049449ac3e4f256f2e7e26dad550d94c (patch)
treef4ec5de70ec05e793a774049e010935ac45853ed /embassy-net/src
parentfff0a03fe0f9e84209dd40fd8f93790871d03d75 (diff)
parenta8703b75988e1e700af701116464025679d2feb8 (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.rs4
-rw-r--r--embassy-net/src/lib.rs4
-rw-r--r--embassy-net/src/packet_pool.rs7
-rw-r--r--embassy-net/src/stack.rs39
-rw-r--r--embassy-net/src/tcp.rs22
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 @@
1use core::task::Waker; 1use core::task::Waker;
2use smoltcp::phy::Device as SmolDevice; 2
3use smoltcp::phy::DeviceCapabilities; 3use smoltcp::phy::{Device as SmolDevice, DeviceCapabilities};
4use smoltcp::time::Instant as SmolInstant; 4use smoltcp::time::Instant as SmolInstant;
5 5
6use crate::packet_pool::PacketBoxExt; 6use 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
20pub use smoltcp::phy::{DeviceCapabilities, Medium}; 20pub use smoltcp::phy::{DeviceCapabilities, Medium};
21pub use smoltcp::time::Duration as SmolDuration; 21pub use smoltcp::time::{Duration as SmolDuration, Instant as SmolInstant};
22pub use smoltcp::time::Instant as SmolInstant;
23#[cfg(feature = "medium-ethernet")] 22#[cfg(feature = "medium-ethernet")]
24pub use smoltcp::wire::{EthernetAddress, HardwareAddress}; 23pub use smoltcp::wire::{EthernetAddress, HardwareAddress};
25pub use smoltcp::wire::{IpAddress, IpCidr, Ipv4Address, Ipv4Cidr}; 24pub use smoltcp::wire::{IpAddress, IpCidr, Ipv4Address, Ipv4Cidr};
26
27#[cfg(feature = "proto-ipv6")] 25#[cfg(feature = "proto-ipv6")]
28pub use smoltcp::wire::{Ipv6Address, Ipv6Cidr}; 26pub 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 @@
1use as_slice::{AsMutSlice, AsSlice};
2use core::ops::{Deref, DerefMut, Range}; 1use core::ops::{Deref, DerefMut, Range};
3 2
3use as_slice::{AsMutSlice, AsSlice};
4use atomic_pool::{pool, Box}; 4use atomic_pool::{pool, Box};
5 5
6pub const MTU: usize = 1516; 6pub const MTU: usize = 1516;
@@ -41,10 +41,7 @@ pub trait PacketBoxExt {
41 41
42impl PacketBoxExt for PacketBox { 42impl 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 @@
1use core::cell::UnsafeCell; 1use core::cell::UnsafeCell;
2use core::future::Future; 2use core::future::Future;
3use core::task::Context; 3use core::task::{Context, Poll};
4use core::task::Poll; 4
5use embassy::time::{Instant, Timer}; 5use embassy::time::{Instant, Timer};
6use embassy::waitqueue::WakerRegistration; 6use embassy::waitqueue::WakerRegistration;
7use futures::future::poll_fn; 7use futures::future::poll_fn;
@@ -9,19 +9,17 @@ use futures::pin_mut;
9use heapless::Vec; 9use heapless::Vec;
10#[cfg(feature = "dhcpv4")] 10#[cfg(feature = "dhcpv4")]
11use smoltcp::iface::SocketHandle; 11use smoltcp::iface::SocketHandle;
12use smoltcp::iface::{Interface, InterfaceBuilder}; 12use smoltcp::iface::{Interface, InterfaceBuilder, SocketSet, SocketStorage};
13use smoltcp::iface::{SocketSet, SocketStorage};
14#[cfg(feature = "dhcpv4")]
15use smoltcp::socket::dhcpv4;
16use smoltcp::time::Instant as SmolInstant;
17use smoltcp::wire::{IpCidr, Ipv4Address, Ipv4Cidr};
18
19#[cfg(feature = "medium-ethernet")] 13#[cfg(feature = "medium-ethernet")]
20use smoltcp::iface::{Neighbor, NeighborCache, Route, Routes}; 14use smoltcp::iface::{Neighbor, NeighborCache, Route, Routes};
21#[cfg(feature = "medium-ethernet")] 15#[cfg(feature = "medium-ethernet")]
22use smoltcp::phy::{Device as _, Medium}; 16use smoltcp::phy::{Device as _, Medium};
17#[cfg(feature = "dhcpv4")]
18use smoltcp::socket::dhcpv4;
19use smoltcp::time::Instant as SmolInstant;
23#[cfg(feature = "medium-ethernet")] 20#[cfg(feature = "medium-ethernet")]
24use smoltcp::wire::{EthernetAddress, HardwareAddress, IpAddress}; 21use smoltcp::wire::{EthernetAddress, HardwareAddress, IpAddress};
22use smoltcp::wire::{IpCidr, Ipv4Address, Ipv4Cidr};
25 23
26use crate::device::{Device, DeviceAdapter, LinkState}; 24use 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
41impl<const ADDR: usize, const SOCK: usize, const NEIGHBOR: usize> 39impl<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;
2use core::future::Future; 2use core::future::Future;
3use core::mem; 3use core::mem;
4use core::task::Poll; 4use core::task::Poll;
5
5use futures::future::poll_fn; 6use futures::future::poll_fn;
6use smoltcp::iface::{Interface, SocketHandle}; 7use smoltcp::iface::{Interface, SocketHandle};
7use smoltcp::socket::tcp; 8use smoltcp::socket::tcp;
8use smoltcp::time::Duration; 9use smoltcp::time::Duration;
9use smoltcp::wire::IpEndpoint; 10use smoltcp::wire::{IpEndpoint, IpListenEndpoint};
10use smoltcp::wire::IpListenEndpoint;
11 11
12use super::stack::Stack;
12use crate::stack::SocketStack; 13use crate::stack::SocketStack;
13use crate::Device; 14use crate::Device;
14 15
15use 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))]
19pub enum Error { 18pub enum Error {
@@ -57,11 +56,7 @@ pub struct TcpWriter<'a> {
57} 56}
58 57
59impl<'a> TcpSocket<'a> { 58impl<'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());