diff --git a/Cargo.toml b/Cargo.toml index 23390f1..68da616 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ [package] name = "z-serial" -version = "0.2.2" +version = "0.2.3" edition = "2021" repository = "https://github.com/ZettaScaleLabs/z-serial" homepage = "http://zenoh.io" @@ -39,4 +39,4 @@ log = "0.4" clap = { version = "3.1", features = ["derive"] } env_logger = "0.9.0" tokio = {version = "1.17.0", features = ["full"] } -rand = "0.8" \ No newline at end of file +rand = "0.8" diff --git a/src/lib.rs b/src/lib.rs index 33cf99e..b860097 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,7 @@ // ZettaScale Zenoh Team, // +use std::path::Path; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio_serial::{ClearBuffer, SerialPort, SerialPortBuilderExt, SerialStream}; @@ -334,6 +335,25 @@ impl ZSerial { } } +pub fn get_available_port_names() -> tokio_serial::Result> { + let port_names: Vec = tokio_serial::available_ports()? + .iter() + .map(|info| { + Path::new(&info.port_name) + .file_name() + .map(|os_str| os_str.to_string_lossy().to_string()) + .ok_or_else(|| { + tokio_serial::Error::new( + tokio_serial::ErrorKind::Unknown, + "Unsupported port name", + ) + }) + }) + .collect::, _>>()?; + + Ok(port_names) +} + #[cfg(test)] mod tests { use super::{WireFormat, COBS_BUF_SIZE};