Skip to content

Commit cd49141

Browse files
Add list-all-ports flag and filter the default list (#590)
* feat: Add option to list all ports * docs: Update changelog
1 parent f21829a commit cd49141

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased]
99

1010
### Added
11+
- Add `--list-all-ports` connection argument to avoid serial port filtering (#590)
1112
- Allow config file to live in parent folder (#595)
1213

1314
### Fixed
1415
- Change the `hard_reset` sequence to fix Windows issues (#594)
1516

1617
### Changed
18+
- Non-linux-musl: Only list the available USB Ports by default (#590)
1719
- `FlashData::new` now returns `crate::Error` (#591)
1820
- Moved `reset_after_flash` method to `reset` module (#594)
1921

espflash/src/cli/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ pub struct ConnectArgs {
6161
/// Require confirmation before auto-connecting to a recognized device.
6262
#[arg(short = 'C', long)]
6363
pub confirm_port: bool,
64+
/// List all available ports.
65+
#[arg(long)]
66+
pub list_all_ports: bool,
6467
/// Do not use the RAM stub for loading
6568
#[arg(long)]
6669
pub no_stub: bool,

espflash/src/cli/serial.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn get_serial_port_info(
3333
// doesn't work (on Windows) with "dummy" device paths like `COM4`. That's
3434
// the reason we need to handle Windows/Posix differently.
3535

36-
let ports = detect_usb_serial_ports().unwrap_or_default();
36+
let ports = detect_usb_serial_ports(matches.list_all_ports).unwrap_or_default();
3737

3838
if let Some(serial) = &matches.port {
3939
find_serial_port(&ports, serial)
@@ -108,7 +108,7 @@ fn find_serial_port(ports: &[SerialPortInfo], name: &str) -> Result<SerialPortIn
108108
/// Linux we can do some manual parsing of sysfs to get the relevant bits
109109
/// without udev
110110
#[cfg(all(target_os = "linux", target_env = "musl"))]
111-
fn detect_usb_serial_ports() -> Result<Vec<SerialPortInfo>> {
111+
fn detect_usb_serial_ports(_list_all_ports: bool) -> Result<Vec<SerialPortInfo>> {
112112
use std::{
113113
fs::{read_link, read_to_string},
114114
path::PathBuf,
@@ -166,20 +166,24 @@ fn detect_usb_serial_ports() -> Result<Vec<SerialPortInfo>> {
166166

167167
/// Returns a vector with available USB serial ports.
168168
#[cfg(not(all(target_os = "linux", target_env = "musl")))]
169-
fn detect_usb_serial_ports() -> Result<Vec<SerialPortInfo>> {
169+
fn detect_usb_serial_ports(list_all_ports: bool) -> Result<Vec<SerialPortInfo>> {
170170
let ports = available_ports().into_diagnostic()?;
171171
let ports = ports
172172
.into_iter()
173173
.filter(|port_info| {
174-
matches!(
175-
&port_info.port_type,
176-
SerialPortType::UsbPort(..) |
177-
// Allow PciPort. The user may want to use it.
178-
// The port might have been misdetected by the system as PCI.
179-
SerialPortType::PciPort |
180-
// Good luck.
181-
SerialPortType::Unknown
182-
)
174+
if list_all_ports {
175+
matches!(
176+
&port_info.port_type,
177+
SerialPortType::UsbPort(..) |
178+
// Allow PciPort. The user may want to use it.
179+
// The port might have been misdetected by the system as PCI.
180+
SerialPortType::PciPort |
181+
// Good luck.
182+
SerialPortType::Unknown
183+
)
184+
} else {
185+
matches!(&port_info.port_type, SerialPortType::UsbPort(..))
186+
}
183187
})
184188
.collect::<Vec<_>>();
185189

espflash/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub enum Error {
115115
#[error("No serial ports could be detected")]
116116
#[diagnostic(
117117
code(espflash::no_serial),
118-
help("Make sure you have connected a device to the host system")
118+
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.")
119119
)]
120120
NoSerial,
121121

0 commit comments

Comments
 (0)