net: do not use smoltcp Instant/Duration in public API.

This commit is contained in:
Dario Nieuwenhuis 2023-05-15 00:38:58 +02:00
parent 6e93d193cf
commit 26d7610554
11 changed files with 41 additions and 26 deletions

View File

@ -12,6 +12,7 @@ mod device;
pub mod dns; pub mod dns;
#[cfg(feature = "tcp")] #[cfg(feature = "tcp")]
pub mod tcp; pub mod tcp;
mod time;
#[cfg(feature = "udp")] #[cfg(feature = "udp")]
pub mod udp; pub mod udp;
@ -27,10 +28,6 @@ use heapless::Vec;
use smoltcp::iface::{Interface, SocketHandle, SocketSet, SocketStorage}; use smoltcp::iface::{Interface, SocketHandle, SocketSet, SocketStorage};
#[cfg(feature = "dhcpv4")] #[cfg(feature = "dhcpv4")]
use smoltcp::socket::dhcpv4::{self, RetryConfig}; use smoltcp::socket::dhcpv4::{self, RetryConfig};
#[cfg(feature = "dhcpv4")]
use smoltcp::time::Duration;
// smoltcp reexports
pub use smoltcp::time::{Duration as SmolDuration, Instant as SmolInstant};
#[cfg(feature = "medium-ethernet")] #[cfg(feature = "medium-ethernet")]
pub use smoltcp::wire::{EthernetAddress, HardwareAddress}; pub use smoltcp::wire::{EthernetAddress, HardwareAddress};
pub use smoltcp::wire::{IpAddress, IpCidr, Ipv4Address, Ipv4Cidr}; pub use smoltcp::wire::{IpAddress, IpCidr, Ipv4Address, Ipv4Cidr};
@ -40,6 +37,7 @@ pub use smoltcp::wire::{Ipv6Address, Ipv6Cidr};
pub use smoltcp::{socket::udp::PacketMetadata, wire::IpListenEndpoint}; pub use smoltcp::{socket::udp::PacketMetadata, wire::IpListenEndpoint};
use crate::device::DriverAdapter; use crate::device::DriverAdapter;
use crate::time::{instant_from_smoltcp, instant_to_smoltcp};
const LOCAL_PORT_MIN: u16 = 1025; const LOCAL_PORT_MIN: u16 = 1025;
const LOCAL_PORT_MAX: u16 = 65535; const LOCAL_PORT_MAX: u16 = 65535;
@ -74,7 +72,7 @@ pub struct StaticConfig {
#[cfg(feature = "dhcpv4")] #[cfg(feature = "dhcpv4")]
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct DhcpConfig { pub struct DhcpConfig {
pub max_lease_duration: Option<Duration>, pub max_lease_duration: Option<embassy_time::Duration>,
pub retry_config: RetryConfig, pub retry_config: RetryConfig,
/// Ignore NAKs. /// Ignore NAKs.
pub ignore_naks: bool, pub ignore_naks: bool,
@ -384,7 +382,7 @@ impl<D: Driver + 'static> Inner<D> {
#[cfg(feature = "dhcpv4")] #[cfg(feature = "dhcpv4")]
fn apply_dhcp_config(&self, socket: &mut smoltcp::socket::dhcpv4::Socket, config: DhcpConfig) { fn apply_dhcp_config(&self, socket: &mut smoltcp::socket::dhcpv4::Socket, config: DhcpConfig) {
socket.set_ignore_naks(config.ignore_naks); socket.set_ignore_naks(config.ignore_naks);
socket.set_max_lease_duration(config.max_lease_duration); socket.set_max_lease_duration(config.max_lease_duration.map(crate::time::duration_to_smoltcp));
socket.set_ports(config.server_port, config.client_port); socket.set_ports(config.server_port, config.client_port);
socket.set_retry_config(config.retry_config); socket.set_retry_config(config.retry_config);
} }
@ -465,11 +463,3 @@ impl<D: Driver + 'static> Inner<D> {
} }
} }
} }
fn instant_to_smoltcp(instant: Instant) -> SmolInstant {
SmolInstant::from_millis(instant.as_millis() as i64)
}
fn instant_from_smoltcp(instant: SmolInstant) -> Instant {
Instant::from_millis(instant.total_millis() as u64)
}

View File

@ -4,11 +4,13 @@ use core::mem;
use core::task::Poll; use core::task::Poll;
use embassy_net_driver::Driver; use embassy_net_driver::Driver;
use embassy_time::Duration;
use smoltcp::iface::{Interface, SocketHandle}; use smoltcp::iface::{Interface, SocketHandle};
use smoltcp::socket::tcp; use smoltcp::socket::tcp;
use smoltcp::time::Duration; pub use smoltcp::socket::tcp::State;
use smoltcp::wire::{IpEndpoint, IpListenEndpoint}; use smoltcp::wire::{IpEndpoint, IpListenEndpoint};
use crate::time::duration_to_smoltcp;
use crate::{SocketStack, Stack}; use crate::{SocketStack, Stack};
#[derive(PartialEq, Eq, Clone, Copy, Debug)] #[derive(PartialEq, Eq, Clone, Copy, Debug)]
@ -155,11 +157,13 @@ impl<'a> TcpSocket<'a> {
} }
pub fn set_timeout(&mut self, duration: Option<Duration>) { pub fn set_timeout(&mut self, duration: Option<Duration>) {
self.io.with_mut(|s, _| s.set_timeout(duration)) self.io
.with_mut(|s, _| s.set_timeout(duration.map(duration_to_smoltcp)))
} }
pub fn set_keep_alive(&mut self, interval: Option<Duration>) { pub fn set_keep_alive(&mut self, interval: Option<Duration>) {
self.io.with_mut(|s, _| s.set_keep_alive(interval)) self.io
.with_mut(|s, _| s.set_keep_alive(interval.map(duration_to_smoltcp)))
} }
pub fn set_hop_limit(&mut self, hop_limit: Option<u8>) { pub fn set_hop_limit(&mut self, hop_limit: Option<u8>) {
@ -174,7 +178,7 @@ impl<'a> TcpSocket<'a> {
self.io.with(|s, _| s.remote_endpoint()) self.io.with(|s, _| s.remote_endpoint())
} }
pub fn state(&self) -> tcp::State { pub fn state(&self) -> State {
self.io.with(|s, _| s.state()) self.io.with(|s, _| s.state())
} }

20
embassy-net/src/time.rs Normal file
View File

@ -0,0 +1,20 @@
#![allow(unused)]
use embassy_time::{Duration, Instant};
use smoltcp::time::{Duration as SmolDuration, Instant as SmolInstant};
pub(crate) fn instant_to_smoltcp(instant: Instant) -> SmolInstant {
SmolInstant::from_micros(instant.as_micros() as i64)
}
pub(crate) fn instant_from_smoltcp(instant: SmolInstant) -> Instant {
Instant::from_micros(instant.total_micros() as u64)
}
pub(crate) fn duration_to_smoltcp(duration: Duration) -> SmolDuration {
SmolDuration::from_micros(duration.as_micros())
}
pub(crate) fn duration_from_smoltcp(duration: SmolDuration) -> Duration {
Duration::from_micros(duration.total_micros())
}

View File

@ -132,7 +132,7 @@ async fn main(spawner: Spawner) {
loop { loop {
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); socket.set_timeout(Some(embassy_time::Duration::from_secs(10)));
info!("Listening on TCP:1234..."); info!("Listening on TCP:1234...");
if let Err(e) = socket.accept(1234).await { if let Err(e) = socket.accept(1234).await {

View File

@ -114,7 +114,7 @@ async fn main(spawner: Spawner) {
loop { loop {
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); socket.set_timeout(Some(embassy_time::Duration::from_secs(10)));
info!("Listening on TCP:1234..."); info!("Listening on TCP:1234...");
if let Err(e) = socket.accept(1234).await { if let Err(e) = socket.accept(1234).await {

View File

@ -6,6 +6,7 @@ use clap::Parser;
use embassy_executor::{Executor, Spawner}; use embassy_executor::{Executor, Spawner};
use embassy_net::tcp::TcpSocket; use embassy_net::tcp::TcpSocket;
use embassy_net::{Config, Ipv4Address, Ipv4Cidr, Stack, StackResources}; use embassy_net::{Config, Ipv4Address, Ipv4Cidr, Stack, StackResources};
use embassy_time::Duration;
use embedded_io::asynch::Write; use embedded_io::asynch::Write;
use heapless::Vec; use heapless::Vec;
use log::*; use log::*;
@ -75,7 +76,7 @@ async fn main_task(spawner: Spawner) {
let mut tx_buffer = [0; 4096]; let mut tx_buffer = [0; 4096];
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); socket.set_timeout(Some(Duration::from_secs(10)));
let remote_endpoint = (Ipv4Address::new(192, 168, 69, 100), 8000); let remote_endpoint = (Ipv4Address::new(192, 168, 69, 100), 8000);
info!("connecting to {:?}...", remote_endpoint); info!("connecting to {:?}...", remote_endpoint);

View File

@ -126,7 +126,7 @@ async fn main(spawner: Spawner) {
loop { loop {
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); socket.set_timeout(Some(embassy_time::Duration::from_secs(10)));
info!("Listening on TCP:1234..."); info!("Listening on TCP:1234...");
if let Err(e) = socket.accept(1234).await { if let Err(e) = socket.accept(1234).await {

View File

@ -91,7 +91,7 @@ async fn main(spawner: Spawner) -> ! {
loop { loop {
let mut socket = TcpSocket::new(&stack, &mut rx_buffer, &mut tx_buffer); let mut socket = TcpSocket::new(&stack, &mut rx_buffer, &mut tx_buffer);
socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); socket.set_timeout(Some(embassy_time::Duration::from_secs(10)));
let remote_endpoint = (Ipv4Address::new(10, 42, 0, 1), 8000); let remote_endpoint = (Ipv4Address::new(10, 42, 0, 1), 8000);
info!("connecting..."); info!("connecting...");

View File

@ -110,7 +110,7 @@ async fn main(spawner: Spawner) -> ! {
loop { loop {
let mut socket = TcpSocket::new(&stack, &mut rx_buffer, &mut tx_buffer); let mut socket = TcpSocket::new(&stack, &mut rx_buffer, &mut tx_buffer);
socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); socket.set_timeout(Some(embassy_time::Duration::from_secs(10)));
let remote_endpoint = (Ipv4Address::new(10, 42, 0, 1), 8000); let remote_endpoint = (Ipv4Address::new(10, 42, 0, 1), 8000);
info!("connecting..."); info!("connecting...");

View File

@ -92,7 +92,7 @@ async fn main(spawner: Spawner) -> ! {
loop { loop {
let mut socket = TcpSocket::new(&stack, &mut rx_buffer, &mut tx_buffer); let mut socket = TcpSocket::new(&stack, &mut rx_buffer, &mut tx_buffer);
socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); socket.set_timeout(Some(embassy_time::Duration::from_secs(10)));
let remote_endpoint = (Ipv4Address::new(10, 42, 0, 1), 8000); let remote_endpoint = (Ipv4Address::new(10, 42, 0, 1), 8000);
info!("connecting..."); info!("connecting...");

View File

@ -121,7 +121,7 @@ async fn main(spawner: Spawner) {
loop { loop {
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10))); socket.set_timeout(Some(embassy_time::Duration::from_secs(10)));
info!("Listening on TCP:1234..."); info!("Listening on TCP:1234...");
if let Err(e) = socket.accept(1234).await { if let Err(e) = socket.accept(1234).await {