diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-05-15 00:38:58 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2023-05-15 00:53:30 +0200 |
| commit | 26d7610554f262c2c25f99fb441e6dbd6abec61f (patch) | |
| tree | ac3c3f548960824a5dbc603b58225132859f03ba /embassy-net | |
| parent | 6e93d193cfdd2982410e106c383ecc1f066fccfb (diff) | |
net: do not use smoltcp Instant/Duration in public API.
Diffstat (limited to 'embassy-net')
| -rw-r--r-- | embassy-net/src/lib.rs | 18 | ||||
| -rw-r--r-- | embassy-net/src/tcp.rs | 12 | ||||
| -rw-r--r-- | embassy-net/src/time.rs | 20 |
3 files changed, 32 insertions, 18 deletions
diff --git a/embassy-net/src/lib.rs b/embassy-net/src/lib.rs index b80784c2b..64f558756 100644 --- a/embassy-net/src/lib.rs +++ b/embassy-net/src/lib.rs | |||
| @@ -12,6 +12,7 @@ mod device; | |||
| 12 | pub mod dns; | 12 | pub mod dns; |
| 13 | #[cfg(feature = "tcp")] | 13 | #[cfg(feature = "tcp")] |
| 14 | pub mod tcp; | 14 | pub mod tcp; |
| 15 | mod time; | ||
| 15 | #[cfg(feature = "udp")] | 16 | #[cfg(feature = "udp")] |
| 16 | pub mod udp; | 17 | pub mod udp; |
| 17 | 18 | ||
| @@ -27,10 +28,6 @@ use heapless::Vec; | |||
| 27 | use smoltcp::iface::{Interface, SocketHandle, SocketSet, SocketStorage}; | 28 | use smoltcp::iface::{Interface, SocketHandle, SocketSet, SocketStorage}; |
| 28 | #[cfg(feature = "dhcpv4")] | 29 | #[cfg(feature = "dhcpv4")] |
| 29 | use smoltcp::socket::dhcpv4::{self, RetryConfig}; | 30 | use smoltcp::socket::dhcpv4::{self, RetryConfig}; |
| 30 | #[cfg(feature = "dhcpv4")] | ||
| 31 | use smoltcp::time::Duration; | ||
| 32 | // smoltcp reexports | ||
| 33 | pub use smoltcp::time::{Duration as SmolDuration, Instant as SmolInstant}; | ||
| 34 | #[cfg(feature = "medium-ethernet")] | 31 | #[cfg(feature = "medium-ethernet")] |
| 35 | pub use smoltcp::wire::{EthernetAddress, HardwareAddress}; | 32 | pub use smoltcp::wire::{EthernetAddress, HardwareAddress}; |
| 36 | pub use smoltcp::wire::{IpAddress, IpCidr, Ipv4Address, Ipv4Cidr}; | 33 | pub use smoltcp::wire::{IpAddress, IpCidr, Ipv4Address, Ipv4Cidr}; |
| @@ -40,6 +37,7 @@ pub use smoltcp::wire::{Ipv6Address, Ipv6Cidr}; | |||
| 40 | pub use smoltcp::{socket::udp::PacketMetadata, wire::IpListenEndpoint}; | 37 | pub use smoltcp::{socket::udp::PacketMetadata, wire::IpListenEndpoint}; |
| 41 | 38 | ||
| 42 | use crate::device::DriverAdapter; | 39 | use crate::device::DriverAdapter; |
| 40 | use crate::time::{instant_from_smoltcp, instant_to_smoltcp}; | ||
| 43 | 41 | ||
| 44 | const LOCAL_PORT_MIN: u16 = 1025; | 42 | const LOCAL_PORT_MIN: u16 = 1025; |
| 45 | const LOCAL_PORT_MAX: u16 = 65535; | 43 | const LOCAL_PORT_MAX: u16 = 65535; |
| @@ -74,7 +72,7 @@ pub struct StaticConfig { | |||
| 74 | #[cfg(feature = "dhcpv4")] | 72 | #[cfg(feature = "dhcpv4")] |
| 75 | #[derive(Debug, Clone, PartialEq, Eq)] | 73 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 76 | pub struct DhcpConfig { | 74 | pub struct DhcpConfig { |
| 77 | pub max_lease_duration: Option<Duration>, | 75 | pub max_lease_duration: Option<embassy_time::Duration>, |
| 78 | pub retry_config: RetryConfig, | 76 | pub retry_config: RetryConfig, |
| 79 | /// Ignore NAKs. | 77 | /// Ignore NAKs. |
| 80 | pub ignore_naks: bool, | 78 | pub ignore_naks: bool, |
| @@ -384,7 +382,7 @@ impl<D: Driver + 'static> Inner<D> { | |||
| 384 | #[cfg(feature = "dhcpv4")] | 382 | #[cfg(feature = "dhcpv4")] |
| 385 | fn apply_dhcp_config(&self, socket: &mut smoltcp::socket::dhcpv4::Socket, config: DhcpConfig) { | 383 | fn apply_dhcp_config(&self, socket: &mut smoltcp::socket::dhcpv4::Socket, config: DhcpConfig) { |
| 386 | socket.set_ignore_naks(config.ignore_naks); | 384 | socket.set_ignore_naks(config.ignore_naks); |
| 387 | socket.set_max_lease_duration(config.max_lease_duration); | 385 | socket.set_max_lease_duration(config.max_lease_duration.map(crate::time::duration_to_smoltcp)); |
| 388 | socket.set_ports(config.server_port, config.client_port); | 386 | socket.set_ports(config.server_port, config.client_port); |
| 389 | socket.set_retry_config(config.retry_config); | 387 | socket.set_retry_config(config.retry_config); |
| 390 | } | 388 | } |
| @@ -465,11 +463,3 @@ impl<D: Driver + 'static> Inner<D> { | |||
| 465 | } | 463 | } |
| 466 | } | 464 | } |
| 467 | } | 465 | } |
| 468 | |||
| 469 | fn instant_to_smoltcp(instant: Instant) -> SmolInstant { | ||
| 470 | SmolInstant::from_millis(instant.as_millis() as i64) | ||
| 471 | } | ||
| 472 | |||
| 473 | fn instant_from_smoltcp(instant: SmolInstant) -> Instant { | ||
| 474 | Instant::from_millis(instant.total_millis() as u64) | ||
| 475 | } | ||
diff --git a/embassy-net/src/tcp.rs b/embassy-net/src/tcp.rs index c3d8764b0..05b8bb54e 100644 --- a/embassy-net/src/tcp.rs +++ b/embassy-net/src/tcp.rs | |||
| @@ -4,11 +4,13 @@ use core::mem; | |||
| 4 | use core::task::Poll; | 4 | use core::task::Poll; |
| 5 | 5 | ||
| 6 | use embassy_net_driver::Driver; | 6 | use embassy_net_driver::Driver; |
| 7 | use embassy_time::Duration; | ||
| 7 | use smoltcp::iface::{Interface, SocketHandle}; | 8 | use smoltcp::iface::{Interface, SocketHandle}; |
| 8 | use smoltcp::socket::tcp; | 9 | use smoltcp::socket::tcp; |
| 9 | use smoltcp::time::Duration; | 10 | pub use smoltcp::socket::tcp::State; |
| 10 | use smoltcp::wire::{IpEndpoint, IpListenEndpoint}; | 11 | use smoltcp::wire::{IpEndpoint, IpListenEndpoint}; |
| 11 | 12 | ||
| 13 | use crate::time::duration_to_smoltcp; | ||
| 12 | use crate::{SocketStack, Stack}; | 14 | use crate::{SocketStack, Stack}; |
| 13 | 15 | ||
| 14 | #[derive(PartialEq, Eq, Clone, Copy, Debug)] | 16 | #[derive(PartialEq, Eq, Clone, Copy, Debug)] |
| @@ -155,11 +157,13 @@ impl<'a> TcpSocket<'a> { | |||
| 155 | } | 157 | } |
| 156 | 158 | ||
| 157 | pub fn set_timeout(&mut self, duration: Option<Duration>) { | 159 | pub fn set_timeout(&mut self, duration: Option<Duration>) { |
| 158 | self.io.with_mut(|s, _| s.set_timeout(duration)) | 160 | self.io |
| 161 | .with_mut(|s, _| s.set_timeout(duration.map(duration_to_smoltcp))) | ||
| 159 | } | 162 | } |
| 160 | 163 | ||
| 161 | pub fn set_keep_alive(&mut self, interval: Option<Duration>) { | 164 | pub fn set_keep_alive(&mut self, interval: Option<Duration>) { |
| 162 | self.io.with_mut(|s, _| s.set_keep_alive(interval)) | 165 | self.io |
| 166 | .with_mut(|s, _| s.set_keep_alive(interval.map(duration_to_smoltcp))) | ||
| 163 | } | 167 | } |
| 164 | 168 | ||
| 165 | pub fn set_hop_limit(&mut self, hop_limit: Option<u8>) { | 169 | pub fn set_hop_limit(&mut self, hop_limit: Option<u8>) { |
| @@ -174,7 +178,7 @@ impl<'a> TcpSocket<'a> { | |||
| 174 | self.io.with(|s, _| s.remote_endpoint()) | 178 | self.io.with(|s, _| s.remote_endpoint()) |
| 175 | } | 179 | } |
| 176 | 180 | ||
| 177 | pub fn state(&self) -> tcp::State { | 181 | pub fn state(&self) -> State { |
| 178 | self.io.with(|s, _| s.state()) | 182 | self.io.with(|s, _| s.state()) |
| 179 | } | 183 | } |
| 180 | 184 | ||
diff --git a/embassy-net/src/time.rs b/embassy-net/src/time.rs new file mode 100644 index 000000000..b98d40fdc --- /dev/null +++ b/embassy-net/src/time.rs | |||
| @@ -0,0 +1,20 @@ | |||
| 1 | #![allow(unused)] | ||
| 2 | |||
| 3 | use embassy_time::{Duration, Instant}; | ||
| 4 | use smoltcp::time::{Duration as SmolDuration, Instant as SmolInstant}; | ||
| 5 | |||
| 6 | pub(crate) fn instant_to_smoltcp(instant: Instant) -> SmolInstant { | ||
| 7 | SmolInstant::from_micros(instant.as_micros() as i64) | ||
| 8 | } | ||
| 9 | |||
| 10 | pub(crate) fn instant_from_smoltcp(instant: SmolInstant) -> Instant { | ||
| 11 | Instant::from_micros(instant.total_micros() as u64) | ||
| 12 | } | ||
| 13 | |||
| 14 | pub(crate) fn duration_to_smoltcp(duration: Duration) -> SmolDuration { | ||
| 15 | SmolDuration::from_micros(duration.as_micros()) | ||
| 16 | } | ||
| 17 | |||
| 18 | pub(crate) fn duration_from_smoltcp(duration: SmolDuration) -> Duration { | ||
| 19 | Duration::from_micros(duration.total_micros()) | ||
| 20 | } | ||
