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 | |
| parent | 539c007b44bb67feee6db7d021887faa14c62e8b (diff) | |
net: update smoltcp
| -rw-r--r-- | embassy-net/Cargo.toml | 4 | ||||
| -rw-r--r-- | embassy-net/src/config/dhcp.rs | 19 | ||||
| -rw-r--r-- | embassy-net/src/config/mod.rs | 5 | ||||
| -rw-r--r-- | embassy-net/src/config/statik.rs | 9 | ||||
| -rw-r--r-- | embassy-net/src/lib.rs | 3 | ||||
| -rw-r--r-- | embassy-net/src/stack.rs | 31 | ||||
| -rw-r--r-- | embassy-net/src/tcp_socket.rs | 50 | ||||
| -rw-r--r-- | examples/std/src/bin/net.rs | 2 | ||||
| -rw-r--r-- | examples/std/src/serial_port.rs | 2 | ||||
| -rw-r--r-- | examples/std/src/tuntap.rs | 3 |
10 files changed, 55 insertions, 73 deletions
diff --git a/embassy-net/Cargo.toml b/embassy-net/Cargo.toml index 4b932ceb3..a62bdde4d 100644 --- a/embassy-net/Cargo.toml +++ b/embassy-net/Cargo.toml | |||
| @@ -34,8 +34,8 @@ futures = { version = "0.3.17", default-features = false, features = | |||
| 34 | atomic-pool = "0.2.1" | 34 | atomic-pool = "0.2.1" |
| 35 | 35 | ||
| 36 | [dependencies.smoltcp] | 36 | [dependencies.smoltcp] |
| 37 | git = "https://github.com/bobmcwhirter/smoltcp" | 37 | git = "https://github.com/smoltcp-rs/smoltcp" |
| 38 | rev = "faf81d21daae16b650b16e59a8422a8283e8a302" | 38 | rev = "453183f8a1d16daf2f6739b565d3dc7ac93b662e" |
| 39 | default-features = false | 39 | default-features = false |
| 40 | features = [ | 40 | features = [ |
| 41 | "proto-ipv4", | 41 | "proto-ipv4", |
diff --git a/embassy-net/src/config/dhcp.rs b/embassy-net/src/config/dhcp.rs index 8bbcd8176..298657ed6 100644 --- a/embassy-net/src/config/dhcp.rs +++ b/embassy-net/src/config/dhcp.rs | |||
| @@ -1,10 +1,11 @@ | |||
| 1 | use heapless::Vec; | 1 | use heapless::Vec; |
| 2 | use smoltcp::socket::{Dhcpv4Event, Dhcpv4Socket, SocketHandle}; | 2 | use smoltcp::iface::SocketHandle; |
| 3 | use smoltcp::socket::{Dhcpv4Event, Dhcpv4Socket}; | ||
| 3 | use smoltcp::time::Instant; | 4 | use smoltcp::time::Instant; |
| 4 | 5 | ||
| 5 | use super::*; | 6 | use super::*; |
| 6 | use crate::device::LinkState; | 7 | use crate::device::LinkState; |
| 7 | use crate::{Interface, SocketSet}; | 8 | use crate::Interface; |
| 8 | 9 | ||
| 9 | pub struct DhcpConfigurator { | 10 | pub struct DhcpConfigurator { |
| 10 | handle: Option<SocketHandle>, | 11 | handle: Option<SocketHandle>, |
| @@ -17,20 +18,16 @@ impl DhcpConfigurator { | |||
| 17 | } | 18 | } |
| 18 | 19 | ||
| 19 | impl Configurator for DhcpConfigurator { | 20 | impl Configurator for DhcpConfigurator { |
| 20 | fn poll( | 21 | fn poll(&mut self, iface: &mut Interface, _timestamp: Instant) -> Event { |
| 21 | &mut self, | ||
| 22 | iface: &mut Interface, | ||
| 23 | sockets: &mut SocketSet, | ||
| 24 | _timestamp: Instant, | ||
| 25 | ) -> Event { | ||
| 26 | if self.handle.is_none() { | 22 | if self.handle.is_none() { |
| 27 | let handle = sockets.add(Dhcpv4Socket::new()); | 23 | let handle = iface.add_socket(Dhcpv4Socket::new()); |
| 28 | self.handle = Some(handle) | 24 | self.handle = Some(handle) |
| 29 | } | 25 | } |
| 30 | 26 | ||
| 31 | let mut socket = sockets.get::<Dhcpv4Socket>(self.handle.unwrap()); | ||
| 32 | |||
| 33 | let link_up = iface.device_mut().device.link_state() == LinkState::Up; | 27 | let link_up = iface.device_mut().device.link_state() == LinkState::Up; |
| 28 | |||
| 29 | let socket = iface.get_socket::<Dhcpv4Socket>(self.handle.unwrap()); | ||
| 30 | |||
| 34 | if !link_up { | 31 | if !link_up { |
| 35 | socket.reset(); | 32 | socket.reset(); |
| 36 | return Event::Deconfigured; | 33 | return Event::Deconfigured; |
diff --git a/embassy-net/src/config/mod.rs b/embassy-net/src/config/mod.rs index 0f1886ae8..eb1b6636a 100644 --- a/embassy-net/src/config/mod.rs +++ b/embassy-net/src/config/mod.rs | |||
| @@ -2,7 +2,7 @@ use heapless::Vec; | |||
| 2 | use smoltcp::time::Instant; | 2 | use smoltcp::time::Instant; |
| 3 | use smoltcp::wire::{Ipv4Address, Ipv4Cidr}; | 3 | use smoltcp::wire::{Ipv4Address, Ipv4Cidr}; |
| 4 | 4 | ||
| 5 | use crate::{Interface, SocketSet}; | 5 | use crate::Interface; |
| 6 | 6 | ||
| 7 | mod statik; | 7 | mod statik; |
| 8 | pub use statik::StaticConfigurator; | 8 | pub use statik::StaticConfigurator; |
| @@ -31,6 +31,5 @@ pub struct Config { | |||
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | pub trait Configurator { | 33 | pub trait Configurator { |
| 34 | fn poll(&mut self, iface: &mut Interface, sockets: &mut SocketSet, timestamp: Instant) | 34 | fn poll(&mut self, iface: &mut Interface, timestamp: Instant) -> Event; |
| 35 | -> Event; | ||
| 36 | } | 35 | } |
diff --git a/embassy-net/src/config/statik.rs b/embassy-net/src/config/statik.rs index 9a530717b..e614db73b 100644 --- a/embassy-net/src/config/statik.rs +++ b/embassy-net/src/config/statik.rs | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | use smoltcp::time::Instant; | 1 | use smoltcp::time::Instant; |
| 2 | 2 | ||
| 3 | use super::*; | 3 | use super::*; |
| 4 | use crate::{Interface, SocketSet}; | 4 | use crate::Interface; |
| 5 | 5 | ||
| 6 | pub struct StaticConfigurator { | 6 | pub struct StaticConfigurator { |
| 7 | config: Config, | 7 | config: Config, |
| @@ -18,12 +18,7 @@ impl StaticConfigurator { | |||
| 18 | } | 18 | } |
| 19 | 19 | ||
| 20 | impl Configurator for StaticConfigurator { | 20 | impl Configurator for StaticConfigurator { |
| 21 | fn poll( | 21 | fn poll(&mut self, _iface: &mut Interface, _timestamp: Instant) -> Event { |
| 22 | &mut self, | ||
| 23 | _iface: &mut Interface, | ||
| 24 | _sockets: &mut SocketSet, | ||
| 25 | _timestamp: Instant, | ||
| 26 | ) -> Event { | ||
| 27 | if self.returned { | 22 | if self.returned { |
| 28 | Event::NoChange | 23 | Event::NoChange |
| 29 | } else { | 24 | } else { |
diff --git a/embassy-net/src/lib.rs b/embassy-net/src/lib.rs index db2a7ebd4..307179556 100644 --- a/embassy-net/src/lib.rs +++ b/embassy-net/src/lib.rs | |||
| @@ -26,7 +26,6 @@ pub use tcp_socket::TcpSocket; | |||
| 26 | pub use smoltcp::phy::{DeviceCapabilities, Medium}; | 26 | pub use smoltcp::phy::{DeviceCapabilities, Medium}; |
| 27 | pub use smoltcp::time::Duration as SmolDuration; | 27 | pub use smoltcp::time::Duration as SmolDuration; |
| 28 | pub use smoltcp::time::Instant as SmolInstant; | 28 | pub use smoltcp::time::Instant as SmolInstant; |
| 29 | pub use smoltcp::wire::{IpAddress, IpCidr, Ipv4Address, Ipv4Cidr}; | 29 | pub use smoltcp::wire::{HardwareAddress, IpAddress, IpCidr, Ipv4Address, Ipv4Cidr}; |
| 30 | pub type Interface = smoltcp::iface::Interface<'static, device::DeviceAdapter>; | 30 | pub type Interface = smoltcp::iface::Interface<'static, device::DeviceAdapter>; |
| 31 | pub type SocketSet = smoltcp::socket::SocketSet<'static>; | ||
| 32 | pub use smoltcp::{Error, Result}; | 31 | pub use smoltcp::{Error, Result}; |
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, |
diff --git a/embassy-net/src/tcp_socket.rs b/embassy-net/src/tcp_socket.rs index bb1b626e7..39bdd0c18 100644 --- a/embassy-net/src/tcp_socket.rs +++ b/embassy-net/src/tcp_socket.rs | |||
| @@ -4,7 +4,7 @@ use core::pin::Pin; | |||
| 4 | use core::task::{Context, Poll}; | 4 | use core::task::{Context, Poll}; |
| 5 | use embassy::io; | 5 | use embassy::io; |
| 6 | use embassy::io::{AsyncBufRead, AsyncWrite}; | 6 | use embassy::io::{AsyncBufRead, AsyncWrite}; |
| 7 | use smoltcp::socket::SocketHandle; | 7 | use smoltcp::iface::{Context as SmolContext, SocketHandle}; |
| 8 | use smoltcp::socket::TcpSocket as SyncTcpSocket; | 8 | use smoltcp::socket::TcpSocket as SyncTcpSocket; |
| 9 | use smoltcp::socket::{TcpSocketBuffer, TcpState}; | 9 | use smoltcp::socket::{TcpSocketBuffer, TcpState}; |
| 10 | use smoltcp::time::Duration; | 10 | use smoltcp::time::Duration; |
| @@ -25,7 +25,7 @@ impl<'a> TcpSocket<'a> { | |||
| 25 | let handle = Stack::with(|stack| { | 25 | let handle = Stack::with(|stack| { |
| 26 | let rx_buffer: &'static mut [u8] = unsafe { mem::transmute(rx_buffer) }; | 26 | let rx_buffer: &'static mut [u8] = unsafe { mem::transmute(rx_buffer) }; |
| 27 | let tx_buffer: &'static mut [u8] = unsafe { mem::transmute(tx_buffer) }; | 27 | let tx_buffer: &'static mut [u8] = unsafe { mem::transmute(tx_buffer) }; |
| 28 | stack.sockets.add(SyncTcpSocket::new( | 28 | stack.iface.add_socket(SyncTcpSocket::new( |
| 29 | TcpSocketBuffer::new(rx_buffer), | 29 | TcpSocketBuffer::new(rx_buffer), |
| 30 | TcpSocketBuffer::new(tx_buffer), | 30 | TcpSocketBuffer::new(tx_buffer), |
| 31 | )) | 31 | )) |
| @@ -42,10 +42,10 @@ impl<'a> TcpSocket<'a> { | |||
| 42 | T: Into<IpEndpoint>, | 42 | T: Into<IpEndpoint>, |
| 43 | { | 43 | { |
| 44 | let local_port = Stack::with(|stack| stack.get_local_port()); | 44 | let local_port = Stack::with(|stack| stack.get_local_port()); |
| 45 | self.with(|s| s.connect(remote_endpoint, local_port))?; | 45 | self.with(|s, cx| s.connect(cx, remote_endpoint, local_port))?; |
| 46 | 46 | ||
| 47 | futures::future::poll_fn(|cx| { | 47 | futures::future::poll_fn(|cx| { |
| 48 | self.with(|s| match s.state() { | 48 | self.with(|s, _| match s.state() { |
| 49 | TcpState::Closed | TcpState::TimeWait => Poll::Ready(Err(Error::Unaddressable)), | 49 | TcpState::Closed | TcpState::TimeWait => Poll::Ready(Err(Error::Unaddressable)), |
| 50 | TcpState::Listen => Poll::Ready(Err(Error::Illegal)), | 50 | TcpState::Listen => Poll::Ready(Err(Error::Illegal)), |
| 51 | TcpState::SynSent | TcpState::SynReceived => { | 51 | TcpState::SynSent | TcpState::SynReceived => { |
| @@ -62,10 +62,10 @@ impl<'a> TcpSocket<'a> { | |||
| 62 | where | 62 | where |
| 63 | T: Into<IpEndpoint>, | 63 | T: Into<IpEndpoint>, |
| 64 | { | 64 | { |
| 65 | self.with(|s| s.listen(local_endpoint))?; | 65 | self.with(|s, _| s.listen(local_endpoint))?; |
| 66 | 66 | ||
| 67 | futures::future::poll_fn(|cx| { | 67 | futures::future::poll_fn(|cx| { |
| 68 | self.with(|s| match s.state() { | 68 | self.with(|s, _| match s.state() { |
| 69 | TcpState::Closed | TcpState::TimeWait => Poll::Ready(Err(Error::Unaddressable)), | 69 | TcpState::Closed | TcpState::TimeWait => Poll::Ready(Err(Error::Unaddressable)), |
| 70 | TcpState::Listen => Poll::Ready(Ok(())), | 70 | TcpState::Listen => Poll::Ready(Ok(())), |
| 71 | TcpState::SynSent | TcpState::SynReceived => { | 71 | TcpState::SynSent | TcpState::SynReceived => { |
| @@ -79,50 +79,52 @@ impl<'a> TcpSocket<'a> { | |||
| 79 | } | 79 | } |
| 80 | 80 | ||
| 81 | pub fn set_timeout(&mut self, duration: Option<Duration>) { | 81 | pub fn set_timeout(&mut self, duration: Option<Duration>) { |
| 82 | self.with(|s| s.set_timeout(duration)) | 82 | self.with(|s, _| s.set_timeout(duration)) |
| 83 | } | 83 | } |
| 84 | 84 | ||
| 85 | pub fn set_keep_alive(&mut self, interval: Option<Duration>) { | 85 | pub fn set_keep_alive(&mut self, interval: Option<Duration>) { |
| 86 | self.with(|s| s.set_keep_alive(interval)) | 86 | self.with(|s, _| s.set_keep_alive(interval)) |
| 87 | } | 87 | } |
| 88 | 88 | ||
| 89 | pub fn set_hop_limit(&mut self, hop_limit: Option<u8>) { | 89 | pub fn set_hop_limit(&mut self, hop_limit: Option<u8>) { |
| 90 | self.with(|s| s.set_hop_limit(hop_limit)) | 90 | self.with(|s, _| s.set_hop_limit(hop_limit)) |
| 91 | } | 91 | } |
| 92 | 92 | ||
| 93 | pub fn local_endpoint(&self) -> IpEndpoint { | 93 | pub fn local_endpoint(&self) -> IpEndpoint { |
| 94 | self.with(|s| s.local_endpoint()) | 94 | self.with(|s, _| s.local_endpoint()) |
| 95 | } | 95 | } |
| 96 | 96 | ||
| 97 | pub fn remote_endpoint(&self) -> IpEndpoint { | 97 | pub fn remote_endpoint(&self) -> IpEndpoint { |
| 98 | self.with(|s| s.remote_endpoint()) | 98 | self.with(|s, _| s.remote_endpoint()) |
| 99 | } | 99 | } |
| 100 | 100 | ||
| 101 | pub fn state(&self) -> TcpState { | 101 | pub fn state(&self) -> TcpState { |
| 102 | self.with(|s| s.state()) | 102 | self.with(|s, _| s.state()) |
| 103 | } | 103 | } |
| 104 | 104 | ||
| 105 | pub fn close(&mut self) { | 105 | pub fn close(&mut self) { |
| 106 | self.with(|s| s.close()) | 106 | self.with(|s, _| s.close()) |
| 107 | } | 107 | } |
| 108 | 108 | ||
| 109 | pub fn abort(&mut self) { | 109 | pub fn abort(&mut self) { |
| 110 | self.with(|s| s.abort()) | 110 | self.with(|s, _| s.abort()) |
| 111 | } | 111 | } |
| 112 | 112 | ||
| 113 | pub fn may_send(&self) -> bool { | 113 | pub fn may_send(&self) -> bool { |
| 114 | self.with(|s| s.may_send()) | 114 | self.with(|s, _| s.may_send()) |
| 115 | } | 115 | } |
| 116 | 116 | ||
| 117 | pub fn may_recv(&self) -> bool { | 117 | pub fn may_recv(&self) -> bool { |
| 118 | self.with(|s| s.may_recv()) | 118 | self.with(|s, _| s.may_recv()) |
| 119 | } | 119 | } |
| 120 | 120 | ||
| 121 | fn with<R>(&self, f: impl FnOnce(&mut SyncTcpSocket) -> R) -> R { | 121 | fn with<R>(&self, f: impl FnOnce(&mut SyncTcpSocket, &mut SmolContext) -> R) -> R { |
| 122 | Stack::with(|stack| { | 122 | Stack::with(|stack| { |
| 123 | let res = { | 123 | let res = { |
| 124 | let mut s = stack.sockets.get::<SyncTcpSocket>(self.handle); | 124 | let (s, cx) = stack |
| 125 | f(&mut *s) | 125 | .iface |
| 126 | .get_socket_and_context::<SyncTcpSocket>(self.handle); | ||
| 127 | f(s, cx) | ||
| 126 | }; | 128 | }; |
| 127 | stack.wake(); | 129 | stack.wake(); |
| 128 | res | 130 | res |
| @@ -138,7 +140,7 @@ fn to_ioerr(_err: Error) -> io::Error { | |||
| 138 | impl<'a> Drop for TcpSocket<'a> { | 140 | impl<'a> Drop for TcpSocket<'a> { |
| 139 | fn drop(&mut self) { | 141 | fn drop(&mut self) { |
| 140 | Stack::with(|stack| { | 142 | Stack::with(|stack| { |
| 141 | stack.sockets.remove(self.handle); | 143 | stack.iface.remove_socket(self.handle); |
| 142 | }) | 144 | }) |
| 143 | } | 145 | } |
| 144 | } | 146 | } |
| @@ -148,10 +150,10 @@ impl<'a> AsyncBufRead for TcpSocket<'a> { | |||
| 148 | self: Pin<&'z mut Self>, | 150 | self: Pin<&'z mut Self>, |
| 149 | cx: &mut Context<'_>, | 151 | cx: &mut Context<'_>, |
| 150 | ) -> Poll<io::Result<&'z [u8]>> { | 152 | ) -> Poll<io::Result<&'z [u8]>> { |
| 151 | self.with(|socket| match socket.peek(1 << 30) { | 153 | self.with(|s, _| match s.peek(1 << 30) { |
| 152 | // No data ready | 154 | // No data ready |
| 153 | Ok(buf) if buf.is_empty() => { | 155 | Ok(buf) if buf.is_empty() => { |
| 154 | socket.register_recv_waker(cx.waker()); | 156 | s.register_recv_waker(cx.waker()); |
| 155 | Poll::Pending | 157 | Poll::Pending |
| 156 | } | 158 | } |
| 157 | // Data ready! | 159 | // Data ready! |
| @@ -176,7 +178,7 @@ impl<'a> AsyncBufRead for TcpSocket<'a> { | |||
| 176 | // even if we're "reading" 0 bytes. | 178 | // even if we're "reading" 0 bytes. |
| 177 | return; | 179 | return; |
| 178 | } | 180 | } |
| 179 | self.with(|s| s.recv(|_| (amt, ()))).unwrap() | 181 | self.with(|s, _| s.recv(|_| (amt, ()))).unwrap() |
| 180 | } | 182 | } |
| 181 | } | 183 | } |
| 182 | 184 | ||
| @@ -186,7 +188,7 @@ impl<'a> AsyncWrite for TcpSocket<'a> { | |||
| 186 | cx: &mut Context<'_>, | 188 | cx: &mut Context<'_>, |
| 187 | buf: &[u8], | 189 | buf: &[u8], |
| 188 | ) -> Poll<io::Result<usize>> { | 190 | ) -> Poll<io::Result<usize>> { |
| 189 | self.with(|s| match s.send_slice(buf) { | 191 | self.with(|s, _| match s.send_slice(buf) { |
| 190 | // Not ready to send (no space in the tx buffer) | 192 | // Not ready to send (no space in the tx buffer) |
| 191 | Ok(0) => { | 193 | Ok(0) => { |
| 192 | s.register_send_waker(cx.waker()); | 194 | s.register_send_waker(cx.waker()); |
diff --git a/examples/std/src/bin/net.rs b/examples/std/src/bin/net.rs index 8abcc586f..be78e3a7e 100644 --- a/examples/std/src/bin/net.rs +++ b/examples/std/src/bin/net.rs | |||
| @@ -67,7 +67,7 @@ async fn main_task(spawner: Spawner) { | |||
| 67 | 67 | ||
| 68 | socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); | 68 | socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); |
| 69 | 69 | ||
| 70 | let remote_endpoint = (Ipv4Address::new(192, 168, 69, 74), 8000); | 70 | let remote_endpoint = (Ipv4Address::new(192, 168, 69, 100), 8000); |
| 71 | info!("connecting to {:?}...", remote_endpoint); | 71 | info!("connecting to {:?}...", remote_endpoint); |
| 72 | let r = socket.connect(remote_endpoint).await; | 72 | let r = socket.connect(remote_endpoint).await; |
| 73 | if let Err(e) = r { | 73 | if let Err(e) = r { |
diff --git a/examples/std/src/serial_port.rs b/examples/std/src/serial_port.rs index aadaeb81d..6825cbebc 100644 --- a/examples/std/src/serial_port.rs +++ b/examples/std/src/serial_port.rs | |||
| @@ -9,7 +9,7 @@ pub struct SerialPort { | |||
| 9 | } | 9 | } |
| 10 | 10 | ||
| 11 | impl SerialPort { | 11 | impl SerialPort { |
| 12 | pub fn new<'a, P: ?Sized + nix::NixPath>( | 12 | pub fn new<P: ?Sized + nix::NixPath>( |
| 13 | path: &P, | 13 | path: &P, |
| 14 | baudrate: termios::BaudRate, | 14 | baudrate: termios::BaudRate, |
| 15 | ) -> io::Result<Self> { | 15 | ) -> io::Result<Self> { |
diff --git a/examples/std/src/tuntap.rs b/examples/std/src/tuntap.rs index 4d30118fb..7ab08539f 100644 --- a/examples/std/src/tuntap.rs +++ b/examples/std/src/tuntap.rs | |||
| @@ -170,8 +170,7 @@ impl crate::Device for TunTapDevice { | |||
| 170 | Err(e) if e.kind() == io::ErrorKind::WouldBlock => { | 170 | Err(e) if e.kind() == io::ErrorKind::WouldBlock => { |
| 171 | let ready = if let Some(w) = self.waker.as_ref() { | 171 | let ready = if let Some(w) = self.waker.as_ref() { |
| 172 | let mut cx = Context::from_waker(w); | 172 | let mut cx = Context::from_waker(w); |
| 173 | let ready = self.device.poll_readable(&mut cx).is_ready(); | 173 | self.device.poll_readable(&mut cx).is_ready() |
| 174 | ready | ||
| 175 | } else { | 174 | } else { |
| 176 | false | 175 | false |
| 177 | }; | 176 | }; |
