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

Report actual device file on Linux without udev #122

Merged
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ project adheres to [Semantic Versioning](https://semver.org/).
### Added
### Changed
### Fixed

* Fixes a bug on Linux without udev where `available_ports()` returned wrong
device file paths.
[#122](https://github.com/serialport/serialport-rs/pull/122)

### Removed

## [4.2.2] - 2023-08-03
Expand Down
24 changes: 18 additions & 6 deletions src/posix/enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,11 +462,11 @@ cfg_if! {
use std::io::Read;
use std::path::Path;

/// Enumerating serial ports on non-Linux POSIX platforms is disabled by disabled the "libudev"
/// default feature.
/// Scans `/sys/class/tty` for serial devices (on Linux systems without libudev).
pub fn available_ports() -> Result<Vec<SerialPortInfo>> {
let mut vec = Vec::new();
let sys_path = Path::new("/sys/class/tty/");
let device_path = Path::new("/dev");
let mut s;
for path in sys_path.read_dir().expect("/sys/class/tty/ doesn't exist on this system") {
let raw_path = path?.path().clone();
Expand All @@ -486,10 +486,22 @@ cfg_if! {
}
}

vec.push(SerialPortInfo {
port_name: raw_path.to_string_lossy().to_string(),
port_type: SerialPortType::Unknown,
});
// Generate the device file path `/dev/DEVICE` from the TTY class path
// `/sys/class/tty/DEVICE` and emit a serial device if this path exists. There are
// no further checks (yet) due to `Path::is_file` reports only regular files.
//
// See https://github.com/serialport/serialport-rs/issues/66 for details.
if let Some(file_name) = raw_path.file_name() {
let device_file = device_path.join(file_name);
if !device_file.exists() {
continue;
}

vec.push(SerialPortInfo {
port_name: device_file.to_string_lossy().to_string(),
port_type: SerialPortType::Unknown,
});
}
}
Ok(vec)
}
Expand Down
Loading