aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2023-05-15 00:38:58 +0200
committerDario Nieuwenhuis <[email protected]>2023-05-15 00:53:30 +0200
commit26d7610554f262c2c25f99fb441e6dbd6abec61f (patch)
treeac3c3f548960824a5dbc603b58225132859f03ba
parent6e93d193cfdd2982410e106c383ecc1f066fccfb (diff)
net: do not use smoltcp Instant/Duration in public API.
-rw-r--r--embassy-net/src/lib.rs18
-rw-r--r--embassy-net/src/tcp.rs12
-rw-r--r--embassy-net/src/time.rs20
-rw-r--r--examples/nrf52840/src/bin/usb_ethernet.rs2
-rw-r--r--examples/rp/src/bin/usb_ethernet.rs2
-rw-r--r--examples/std/src/bin/net.rs3
-rw-r--r--examples/stm32f4/src/bin/usb_ethernet.rs2
-rw-r--r--examples/stm32f7/src/bin/eth.rs2
-rw-r--r--examples/stm32h5/src/bin/eth.rs2
-rw-r--r--examples/stm32h7/src/bin/eth.rs2
-rw-r--r--examples/stm32l5/src/bin/usb_ethernet.rs2
11 files changed, 41 insertions, 26 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;
12pub mod dns; 12pub mod dns;
13#[cfg(feature = "tcp")] 13#[cfg(feature = "tcp")]
14pub mod tcp; 14pub mod tcp;
15mod time;
15#[cfg(feature = "udp")] 16#[cfg(feature = "udp")]
16pub mod udp; 17pub mod udp;
17 18
@@ -27,10 +28,6 @@ use heapless::Vec;
27use smoltcp::iface::{Interface, SocketHandle, SocketSet, SocketStorage}; 28use smoltcp::iface::{Interface, SocketHandle, SocketSet, SocketStorage};
28#[cfg(feature = "dhcpv4")] 29#[cfg(feature = "dhcpv4")]
29use smoltcp::socket::dhcpv4::{self, RetryConfig}; 30use smoltcp::socket::dhcpv4::{self, RetryConfig};
30#[cfg(feature = "dhcpv4")]
31use smoltcp::time::Duration;
32// smoltcp reexports
33pub use smoltcp::time::{Duration as SmolDuration, Instant as SmolInstant};
34#[cfg(feature = "medium-ethernet")] 31#[cfg(feature = "medium-ethernet")]
35pub use smoltcp::wire::{EthernetAddress, HardwareAddress}; 32pub use smoltcp::wire::{EthernetAddress, HardwareAddress};
36pub use smoltcp::wire::{IpAddress, IpCidr, Ipv4Address, Ipv4Cidr}; 33pub use smoltcp::wire::{IpAddress, IpCidr, Ipv4Address, Ipv4Cidr};
@@ -40,6 +37,7 @@ pub use smoltcp::wire::{Ipv6Address, Ipv6Cidr};
40pub use smoltcp::{socket::udp::PacketMetadata, wire::IpListenEndpoint}; 37pub use smoltcp::{socket::udp::PacketMetadata, wire::IpListenEndpoint};
41 38
42use crate::device::DriverAdapter; 39use crate::device::DriverAdapter;
40use crate::time::{instant_from_smoltcp, instant_to_smoltcp};
43 41
44const LOCAL_PORT_MIN: u16 = 1025; 42const LOCAL_PORT_MIN: u16 = 1025;
45const LOCAL_PORT_MAX: u16 = 65535; 43const 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)]
76pub struct DhcpConfig { 74pub 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
469fn instant_to_smoltcp(instant: Instant) -> SmolInstant {
470 SmolInstant::from_millis(instant.as_millis() as i64)
471}
472
473fn 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;
4use core::task::Poll; 4use core::task::Poll;
5 5
6use embassy_net_driver::Driver; 6use embassy_net_driver::Driver;
7use embassy_time::Duration;
7use smoltcp::iface::{Interface, SocketHandle}; 8use smoltcp::iface::{Interface, SocketHandle};
8use smoltcp::socket::tcp; 9use smoltcp::socket::tcp;
9use smoltcp::time::Duration; 10pub use smoltcp::socket::tcp::State;
10use smoltcp::wire::{IpEndpoint, IpListenEndpoint}; 11use smoltcp::wire::{IpEndpoint, IpListenEndpoint};
11 12
13use crate::time::duration_to_smoltcp;
12use crate::{SocketStack, Stack}; 14use 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
3use embassy_time::{Duration, Instant};
4use smoltcp::time::{Duration as SmolDuration, Instant as SmolInstant};
5
6pub(crate) fn instant_to_smoltcp(instant: Instant) -> SmolInstant {
7 SmolInstant::from_micros(instant.as_micros() as i64)
8}
9
10pub(crate) fn instant_from_smoltcp(instant: SmolInstant) -> Instant {
11 Instant::from_micros(instant.total_micros() as u64)
12}
13
14pub(crate) fn duration_to_smoltcp(duration: Duration) -> SmolDuration {
15 SmolDuration::from_micros(duration.as_micros())
16}
17
18pub(crate) fn duration_from_smoltcp(duration: SmolDuration) -> Duration {
19 Duration::from_micros(duration.total_micros())
20}
diff --git a/examples/nrf52840/src/bin/usb_ethernet.rs b/examples/nrf52840/src/bin/usb_ethernet.rs
index b8a72313a..786025c43 100644
--- a/examples/nrf52840/src/bin/usb_ethernet.rs
+++ b/examples/nrf52840/src/bin/usb_ethernet.rs
@@ -132,7 +132,7 @@ async fn main(spawner: Spawner) {
132 132
133 loop { 133 loop {
134 let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); 134 let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
135 socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); 135 socket.set_timeout(Some(embassy_time::Duration::from_secs(10)));
136 136
137 info!("Listening on TCP:1234..."); 137 info!("Listening on TCP:1234...");
138 if let Err(e) = socket.accept(1234).await { 138 if let Err(e) = socket.accept(1234).await {
diff --git a/examples/rp/src/bin/usb_ethernet.rs b/examples/rp/src/bin/usb_ethernet.rs
index 66a6ed4d0..431db63eb 100644
--- a/examples/rp/src/bin/usb_ethernet.rs
+++ b/examples/rp/src/bin/usb_ethernet.rs
@@ -114,7 +114,7 @@ async fn main(spawner: Spawner) {
114 114
115 loop { 115 loop {
116 let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); 116 let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
117 socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); 117 socket.set_timeout(Some(embassy_time::Duration::from_secs(10)));
118 118
119 info!("Listening on TCP:1234..."); 119 info!("Listening on TCP:1234...");
120 if let Err(e) = socket.accept(1234).await { 120 if let Err(e) = socket.accept(1234).await {
diff --git a/examples/std/src/bin/net.rs b/examples/std/src/bin/net.rs
index e018e18c9..d93616254 100644
--- a/examples/std/src/bin/net.rs
+++ b/examples/std/src/bin/net.rs
@@ -6,6 +6,7 @@ use clap::Parser;
6use embassy_executor::{Executor, Spawner}; 6use embassy_executor::{Executor, Spawner};
7use embassy_net::tcp::TcpSocket; 7use embassy_net::tcp::TcpSocket;
8use embassy_net::{Config, Ipv4Address, Ipv4Cidr, Stack, StackResources}; 8use embassy_net::{Config, Ipv4Address, Ipv4Cidr, Stack, StackResources};
9use embassy_time::Duration;
9use embedded_io::asynch::Write; 10use embedded_io::asynch::Write;
10use heapless::Vec; 11use heapless::Vec;
11use log::*; 12use log::*;
@@ -75,7 +76,7 @@ async fn main_task(spawner: Spawner) {
75 let mut tx_buffer = [0; 4096]; 76 let mut tx_buffer = [0; 4096];
76 let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); 77 let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
77 78
78 socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); 79 socket.set_timeout(Some(Duration::from_secs(10)));
79 80
80 let remote_endpoint = (Ipv4Address::new(192, 168, 69, 100), 8000); 81 let remote_endpoint = (Ipv4Address::new(192, 168, 69, 100), 8000);
81 info!("connecting to {:?}...", remote_endpoint); 82 info!("connecting to {:?}...", remote_endpoint);
diff --git a/examples/stm32f4/src/bin/usb_ethernet.rs b/examples/stm32f4/src/bin/usb_ethernet.rs
index db9e18393..9131e5896 100644
--- a/examples/stm32f4/src/bin/usb_ethernet.rs
+++ b/examples/stm32f4/src/bin/usb_ethernet.rs
@@ -126,7 +126,7 @@ async fn main(spawner: Spawner) {
126 126
127 loop { 127 loop {
128 let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); 128 let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
129 socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); 129 socket.set_timeout(Some(embassy_time::Duration::from_secs(10)));
130 130
131 info!("Listening on TCP:1234..."); 131 info!("Listening on TCP:1234...");
132 if let Err(e) = socket.accept(1234).await { 132 if let Err(e) = socket.accept(1234).await {
diff --git a/examples/stm32f7/src/bin/eth.rs b/examples/stm32f7/src/bin/eth.rs
index 9febb14e6..b947361ac 100644
--- a/examples/stm32f7/src/bin/eth.rs
+++ b/examples/stm32f7/src/bin/eth.rs
@@ -91,7 +91,7 @@ async fn main(spawner: Spawner) -> ! {
91 loop { 91 loop {
92 let mut socket = TcpSocket::new(&stack, &mut rx_buffer, &mut tx_buffer); 92 let mut socket = TcpSocket::new(&stack, &mut rx_buffer, &mut tx_buffer);
93 93
94 socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); 94 socket.set_timeout(Some(embassy_time::Duration::from_secs(10)));
95 95
96 let remote_endpoint = (Ipv4Address::new(10, 42, 0, 1), 8000); 96 let remote_endpoint = (Ipv4Address::new(10, 42, 0, 1), 8000);
97 info!("connecting..."); 97 info!("connecting...");
diff --git a/examples/stm32h5/src/bin/eth.rs b/examples/stm32h5/src/bin/eth.rs
index 6d650da9e..b2e252fc7 100644
--- a/examples/stm32h5/src/bin/eth.rs
+++ b/examples/stm32h5/src/bin/eth.rs
@@ -110,7 +110,7 @@ async fn main(spawner: Spawner) -> ! {
110 loop { 110 loop {
111 let mut socket = TcpSocket::new(&stack, &mut rx_buffer, &mut tx_buffer); 111 let mut socket = TcpSocket::new(&stack, &mut rx_buffer, &mut tx_buffer);
112 112
113 socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); 113 socket.set_timeout(Some(embassy_time::Duration::from_secs(10)));
114 114
115 let remote_endpoint = (Ipv4Address::new(10, 42, 0, 1), 8000); 115 let remote_endpoint = (Ipv4Address::new(10, 42, 0, 1), 8000);
116 info!("connecting..."); 116 info!("connecting...");
diff --git a/examples/stm32h7/src/bin/eth.rs b/examples/stm32h7/src/bin/eth.rs
index 541e49762..61bb7e37b 100644
--- a/examples/stm32h7/src/bin/eth.rs
+++ b/examples/stm32h7/src/bin/eth.rs
@@ -92,7 +92,7 @@ async fn main(spawner: Spawner) -> ! {
92 loop { 92 loop {
93 let mut socket = TcpSocket::new(&stack, &mut rx_buffer, &mut tx_buffer); 93 let mut socket = TcpSocket::new(&stack, &mut rx_buffer, &mut tx_buffer);
94 94
95 socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); 95 socket.set_timeout(Some(embassy_time::Duration::from_secs(10)));
96 96
97 let remote_endpoint = (Ipv4Address::new(10, 42, 0, 1), 8000); 97 let remote_endpoint = (Ipv4Address::new(10, 42, 0, 1), 8000);
98 info!("connecting..."); 98 info!("connecting...");
diff --git a/examples/stm32l5/src/bin/usb_ethernet.rs b/examples/stm32l5/src/bin/usb_ethernet.rs
index 98ec0e836..6c5645a41 100644
--- a/examples/stm32l5/src/bin/usb_ethernet.rs
+++ b/examples/stm32l5/src/bin/usb_ethernet.rs
@@ -121,7 +121,7 @@ async fn main(spawner: Spawner) {
121 121
122 loop { 122 loop {
123 let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); 123 let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
124 socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); 124 socket.set_timeout(Some(embassy_time::Duration::from_secs(10)));
125 125
126 info!("Listening on TCP:1234..."); 126 info!("Listening on TCP:1234...");
127 if let Err(e) = socket.accept(1234).await { 127 if let Err(e) = socket.accept(1234).await {