From 783213157bb0b1ce00fea54efae549bf7dbe483f Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Mon, 19 Feb 2024 16:24:55 +0100 Subject: [PATCH 1/2] feat: Add option to list all ports --- espflash/src/cli/mod.rs | 3 +++ espflash/src/cli/serial.rs | 28 ++++++++++++++++------------ espflash/src/error.rs | 2 +- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/espflash/src/cli/mod.rs b/espflash/src/cli/mod.rs index 790bb4ae..33d9a2f5 100644 --- a/espflash/src/cli/mod.rs +++ b/espflash/src/cli/mod.rs @@ -61,6 +61,9 @@ pub struct ConnectArgs { /// Require confirmation before auto-connecting to a recognized device. #[arg(short = 'C', long)] pub confirm_port: bool, + /// List all available ports. + #[arg(long)] + pub list_all_ports: bool, /// Do not use the RAM stub for loading #[arg(long)] pub no_stub: bool, diff --git a/espflash/src/cli/serial.rs b/espflash/src/cli/serial.rs index badbd045..c4ebbed5 100644 --- a/espflash/src/cli/serial.rs +++ b/espflash/src/cli/serial.rs @@ -33,7 +33,7 @@ pub fn get_serial_port_info( // doesn't work (on Windows) with "dummy" device paths like `COM4`. That's // the reason we need to handle Windows/Posix differently. - let ports = detect_usb_serial_ports().unwrap_or_default(); + let ports = detect_usb_serial_ports(matches.list_all_ports).unwrap_or_default(); if let Some(serial) = &matches.port { find_serial_port(&ports, serial) @@ -108,7 +108,7 @@ fn find_serial_port(ports: &[SerialPortInfo], name: &str) -> Result Result> { +fn detect_usb_serial_ports(_list_all_ports: bool) -> Result> { use std::{ fs::{read_link, read_to_string}, path::PathBuf, @@ -166,20 +166,24 @@ fn detect_usb_serial_ports() -> Result> { /// Returns a vector with available USB serial ports. #[cfg(not(all(target_os = "linux", target_env = "musl")))] -fn detect_usb_serial_ports() -> Result> { +fn detect_usb_serial_ports(list_all_ports: bool) -> Result> { let ports = available_ports().into_diagnostic()?; let ports = ports .into_iter() .filter(|port_info| { - matches!( - &port_info.port_type, - SerialPortType::UsbPort(..) | - // Allow PciPort. The user may want to use it. - // The port might have been misdetected by the system as PCI. - SerialPortType::PciPort | - // Good luck. - SerialPortType::Unknown - ) + if list_all_ports { + matches!( + &port_info.port_type, + SerialPortType::UsbPort(..) | + // Allow PciPort. The user may want to use it. + // The port might have been misdetected by the system as PCI. + SerialPortType::PciPort | + // Good luck. + SerialPortType::Unknown + ) + } else { + matches!(&port_info.port_type, SerialPortType::UsbPort(..)) + } }) .collect::>(); diff --git a/espflash/src/error.rs b/espflash/src/error.rs index 47d4489a..1b8e71ee 100644 --- a/espflash/src/error.rs +++ b/espflash/src/error.rs @@ -115,7 +115,7 @@ pub enum Error { #[error("No serial ports could be detected")] #[diagnostic( code(espflash::no_serial), - help("Make sure you have connected a device to the host system") + help("Make sure you have connected a device to the host system. If the device is connected but not listed, try using the `--list-all-ports` flag.") )] NoSerial, From b3e1abba64417372f72c63bf140b54a72bcfa0a0 Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Mon, 19 Feb 2024 16:41:36 +0100 Subject: [PATCH 2/2] docs: Update changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d65ce3df..4a042a39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,10 +8,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added +- Add `--list-all-ports` connection argument to avoid serial port filtering (#590) ### Fixed ### Changed +- Non-linux-musl: Only list the available USB Ports by default (#590) + ### Removed