diff --git a/embassy-net/src/icmp.rs b/embassy-net/src/icmp.rs index 4cce5db50..ba206a465 100644 --- a/embassy-net/src/icmp.rs +++ b/embassy-net/src/icmp.rs @@ -252,24 +252,24 @@ impl Drop for IcmpSocket<'_> { pub mod ping { //! Ping utilities. - //! - //! This module allows for an easy ICMP Echo message interface used to + //! + //! This module allows for an easy ICMP Echo message interface used to //! ping devices with an [ICMP Socket](IcmpSocket). - //! + //! //! ## Usage - //! + //! //! ``` //! use core::net::Ipv4Addr; //! use core::str::FromStr; - //! + //! //! use embassy_net::icmp::ping::{PingManager, PingParams}; //! use embassy_net::icmp::PacketMetadata; - //! + //! //! let mut rx_buffer = [0; 256]; //! let mut tx_buffer = [0; 256]; //! let mut rx_meta = [PacketMetadata::EMPTY]; //! let mut tx_meta = [PacketMetadata::EMPTY]; - //! + //! //! let mut ping_manager = PingManager::new(stack, &mut rx_meta, &mut rx_buffer, &mut tx_meta, &mut tx_buffer); //! let addr = "192.168.8.1"; //! let mut ping_params = PingParams::new(Ipv4Addr::from_str(addr).unwrap()); @@ -280,16 +280,18 @@ pub mod ping { //! }; //! ``` - use super::*; use core::net::{IpAddr, Ipv6Addr}; + use embassy_time::{Duration, Instant, Timer, WithTimeout}; + use super::*; + /// Error returned by [`ping()`](PingManager::ping). #[derive(PartialEq, Eq, Clone, Copy, Debug)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum PingError { /// The target did not respond. - /// + /// /// The packet was sent but the Reply packet has not been recieved /// in the timeout set by [`set_timeout()`](PingParams::set_timeout). DestinationHostUnreachable, @@ -339,7 +341,7 @@ pub mod ping { impl<'d> PingManager<'d> { /// Creates a new instance of [`PingManager`] with a [`Stack`] instance /// and the buffers used for RX and TX. - /// + /// /// **note**: This does not yet creates the ICMP socket. pub fn new( stack: Stack<'d>, @@ -387,7 +389,7 @@ pub mod ping { // Make sure each ping takes at least 1 second to respect standards let rate_limit_start = Instant::now(); - // make a single ping + // make a single ping // - shorts out errors // - select the ip version let ping_duration = match params.target().unwrap() { @@ -564,7 +566,6 @@ pub mod ping { } } - /// Parameters for configuring the ping operation. /// /// This struct provides various configuration options for performing ICMP ping operations, @@ -653,7 +654,7 @@ pub mod ping { } /// Sets the hop limit that will be used by the socket with [`set_hop_limit()`](IcmpSocket::set_hop_limit). - /// + /// /// **Note**: A hop limit of [`Some(0)`](Some()) is equivalent to a hop limit of [`None`]. pub fn set_hop_limit(&mut self, hop_limit: Option) -> &mut Self { let mut hop_limit = hop_limit; @@ -669,9 +670,9 @@ pub mod ping { self.hop_limit } - /// Sets the count used for specifying the number of pings done on one + /// Sets the count used for specifying the number of pings done on one /// [`ping()`](PingManager::ping) call. - /// + /// /// **Note**: A count of 0 will be set as 1. pub fn set_count(&mut self, count: u16) -> &mut Self { let mut count = count; @@ -682,7 +683,7 @@ pub mod ping { self } - /// Retrieve the count used for specifying the number of pings done on one + /// Retrieve the count used for specifying the number of pings done on one /// [`ping()`](PingManager::ping) call. pub fn count(&self) -> u16 { self.count diff --git a/embassy-net/src/lib.rs b/embassy-net/src/lib.rs index 1bb112252..693a39ed5 100644 --- a/embassy-net/src/lib.rs +++ b/embassy-net/src/lib.rs @@ -15,6 +15,8 @@ pub(crate) mod fmt; #[cfg(feature = "dns")] pub mod dns; mod driver_util; +#[cfg(feature = "icmp")] +pub mod icmp; #[cfg(feature = "raw")] pub mod raw; #[cfg(feature = "tcp")] @@ -22,8 +24,6 @@ pub mod tcp; mod time; #[cfg(feature = "udp")] pub mod udp; -#[cfg(feature = "icmp")] -pub mod icmp; use core::cell::RefCell; use core::future::{poll_fn, Future}; diff --git a/examples/rp/src/bin/ethernet_w5500_icmp.rs b/examples/rp/src/bin/ethernet_w5500_icmp.rs index a07cdf88d..5f336b579 100644 --- a/examples/rp/src/bin/ethernet_w5500_icmp.rs +++ b/examples/rp/src/bin/ethernet_w5500_icmp.rs @@ -1,5 +1,5 @@ //! This example implements an echo (ping) with an ICMP Socket and using defmt to report the results. -//! +//! //! Although there is a better way to execute pings using the child module ping of the icmp module, //! this example allows for other icmp messages like `Destination unreachable` to be sent aswell. //! @@ -106,16 +106,12 @@ async fn main(spawner: Spawner) { // Send the packet and store the starting instant to mesure latency later let start = socket - .send_to_with( - icmp_repr.buffer_len(), - cfg.gateway.unwrap(), - |buf| { - // Create and populate the packet buffer allocated by `send_to_with` - let mut icmp_packet = Icmpv4Packet::new_unchecked(buf); - icmp_repr.emit(&mut icmp_packet, &ChecksumCapabilities::default()); - Instant::now() // Return the instant where the packet was sent - }, - ) + .send_to_with(icmp_repr.buffer_len(), cfg.gateway.unwrap(), |buf| { + // Create and populate the packet buffer allocated by `send_to_with` + let mut icmp_packet = Icmpv4Packet::new_unchecked(buf); + icmp_repr.emit(&mut icmp_packet, &ChecksumCapabilities::default()); + Instant::now() // Return the instant where the packet was sent + }) .await .unwrap(); @@ -123,7 +119,12 @@ async fn main(spawner: Spawner) { socket .recv_with(|(buf, addr)| { let packet = Icmpv4Packet::new_checked(buf).unwrap(); - info!("Recieved {:?} from {} in {}ms", packet.data(), addr, start.elapsed().as_millis()); + info!( + "Recieved {:?} from {} in {}ms", + packet.data(), + addr, + start.elapsed().as_millis() + ); }) .await .unwrap(); diff --git a/examples/rp/src/bin/ethernet_w5500_icmp_ping.rs b/examples/rp/src/bin/ethernet_w5500_icmp_ping.rs index 0d83e3831..0724311f9 100644 --- a/examples/rp/src/bin/ethernet_w5500_icmp_ping.rs +++ b/examples/rp/src/bin/ethernet_w5500_icmp_ping.rs @@ -100,7 +100,7 @@ async fn main(spawner: Spawner) { // Create the ping manager instance let mut ping_manager = PingManager::new(stack, &mut rx_meta, &mut rx_buffer, &mut tx_meta, &mut tx_buffer); let addr = "192.168.8.1"; // Address to ping to - // Create the PingParams with the target address + // Create the PingParams with the target address let mut ping_params = PingParams::new(Ipv4Addr::from_str(addr).unwrap()); // (optional) Set custom properties of the ping ping_params.set_payload(b"Hello, Ping!"); // custom payload @@ -118,7 +118,7 @@ async fn main(spawner: Spawner) { Ok(time) => { info!("{} is online\n- latency: {}ms\n", ip_addr, time.as_millis()); total_online_hosts += 1; - }, + } _ => continue, } }