Skip to content

Commit

Permalink
Add list-all-ports flag and filter the default list (#590)
Browse files Browse the repository at this point in the history
* feat: Add option to list all ports

* docs: Update changelog
  • Loading branch information
SergioGasquez authored Feb 23, 2024
1 parent f21829a commit cd49141
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ 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)
- Allow config file to live in parent folder (#595)

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

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

Expand Down
3 changes: 3 additions & 0 deletions espflash/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 16 additions & 12 deletions espflash/src/cli/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -108,7 +108,7 @@ fn find_serial_port(ports: &[SerialPortInfo], name: &str) -> Result<SerialPortIn
/// Linux we can do some manual parsing of sysfs to get the relevant bits
/// without udev
#[cfg(all(target_os = "linux", target_env = "musl"))]
fn detect_usb_serial_ports() -> Result<Vec<SerialPortInfo>> {
fn detect_usb_serial_ports(_list_all_ports: bool) -> Result<Vec<SerialPortInfo>> {
use std::{
fs::{read_link, read_to_string},
path::PathBuf,
Expand Down Expand Up @@ -166,20 +166,24 @@ fn detect_usb_serial_ports() -> Result<Vec<SerialPortInfo>> {

/// Returns a vector with available USB serial ports.
#[cfg(not(all(target_os = "linux", target_env = "musl")))]
fn detect_usb_serial_ports() -> Result<Vec<SerialPortInfo>> {
fn detect_usb_serial_ports(list_all_ports: bool) -> Result<Vec<SerialPortInfo>> {
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::<Vec<_>>();

Expand Down
2 changes: 1 addition & 1 deletion espflash/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down

0 comments on commit cd49141

Please sign in to comment.