Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove special handling of MUSL Linux now that we no longer use libudev #749

Merged
merged 1 commit into from
Feb 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 0 additions & 66 deletions espflash/src/cli/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,73 +105,7 @@ fn find_serial_port(ports: &[SerialPortInfo], name: &str) -> Result<SerialPortIn
}
}

/// Serialport's auto-detect doesn't provide any port information when using
/// MUSL 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(_list_all_ports: bool) -> Result<Vec<SerialPortInfo>> {
use std::{
fs::{read_link, read_to_string},
path::{Path, PathBuf},
};

use serialport::UsbPortInfo;

let ports = available_ports().into_diagnostic()?;
let ports = ports
.into_iter()
.filter_map(|port_info| {
// With musl, the paths we get are `/sys/class/tty/*` or `/dev/*`
// In case of `/dev/*` we transform them into `/sys/class/tty/*`
let path = match AsRef::<Path>::as_ref(&port_info.port_name).strip_prefix("/dev/") {
Ok(rem) => PathBuf::from("/sys/class/tty/").join(rem),
Err(_) => PathBuf::from(&port_info.port_name),
};

// This will give something like:
// `/sys/devices/pci0000:00/0000:00:07.1/0000:0c:00.3/usb5/5-3/5-3.1/5-3.1:1.0/
// ttyUSB0/tty/ttyUSB0`
let mut parent_dev = path.canonicalize().ok()?;

// Walk up 3 dirs to get to the device hosting the tty:
// `/sys/devices/pci0000:00/0000:00:07.1/0000:0c:00.3/usb5/5-3/5-3.1/5-3.1:1.0`
parent_dev.pop();
parent_dev.pop();
parent_dev.pop();

// Check that the device is using the usb subsystem
read_link(parent_dev.join("subsystem"))
.ok()
.filter(|subsystem| subsystem.ends_with("usb"))?;

let interface = read_to_string(parent_dev.join("interface"))
.ok()
.map(|s| s.trim().to_string());

// /sys/devices/pci0000:00/0000:00:07.1/0000:0c:00.3/usb5/5-3/5-3.1
parent_dev.pop();

let vid = read_to_string(parent_dev.join("idVendor")).ok()?;
let pid = read_to_string(parent_dev.join("idProduct")).ok()?;

Some(SerialPortInfo {
port_type: SerialPortType::UsbPort(UsbPortInfo {
vid: u16::from_str_radix(vid.trim(), 16).ok()?,
pid: u16::from_str_radix(pid.trim(), 16).ok()?,
product: interface,
serial_number: None,
manufacturer: None,
}),
port_name: format!("/dev/{}", path.file_name()?.to_str()?),
})
})
.collect::<Vec<_>>();

Ok(ports)
}

/// Returns a vector with available USB serial ports.
#[cfg(not(all(target_os = "linux", target_env = "musl")))]
fn detect_usb_serial_ports(list_all_ports: bool) -> Result<Vec<SerialPortInfo>> {
let ports = available_ports().into_diagnostic()?;
let ports = ports
Expand Down
Loading