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

Prefer manufacturer and model information from device over udev database #137

Merged
merged 7 commits into from
Dec 11, 2023
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
56 changes: 54 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,67 @@ jobs:

# --------------------------------------------------------------------------
# MSRV
#
# Check at least once per platform.

msrv:
msrv-aarch64-apple-darwin:
uses: ./.github/workflows/build.yaml
with:
disable_extra_builds: true
disable_tests: true
target: aarch64-apple-darwin
toolchain: "1.59.0"

msrv-arm-linux-androideabi:
uses: ./.github/workflows/build.yaml
with:
disable_extra_builds: true
disable_tests: true
target: arm-linux-androideabi
toolchain: "1.59.0"

msrv-x86_64-unknown-freebsd:
uses: ./.github/workflows/build.yaml
with:
disable_extra_builds: true
disable_tests: true
target: x86_64-unknown-freebsd
toolchain: "1.59.0"

msrv-x86_64-unknown-linux-gnu:
uses: ./.github/workflows/build.yaml
with:
disable_extra_builds: true
disable_tests: true
extra_packages: libudev-dev
target: x86_64-unknown-linux-gnu
toolchain: "1.56.1"
toolchain: "1.59.0"

msrv-x86_64-unknown-linux-musl:
uses: ./.github/workflows/build.yaml
with:
disable_extra_builds: true
disable_tests: true
extra_packages: gcc-aarch64-linux-gnu
target: aarch64-unknown-linux-musl
toolchain: "1.59.0"

msrv-x86_64-pc-windows-msvc:
uses: ./.github/workflows/build.yaml
with:
disable_extra_builds: true
disable_tests: true
runs_on: windows-2019
target: x86_64-pc-windows-msvc
toolchain: "1.59.0"

msrv-x86_64-unknown-netbsd:
uses: ./.github/workflows/build.yaml
with:
disable_extra_builds: true
disable_tests: true
target: x86_64-unknown-netbsd
toolchain: "1.59.0"

# --------------------------------------------------------------------------
# cargo-deny
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ project adheres to [Semantic Versioning](https://semver.org/).
handles when starting a child process. In particular this means that a serial
device can be reopened after making SW update of a Tauri application.
[#130](https://github.com/serialport/serialport-rs/pull/130)
* Prefer USB device manufacturer and model information from the actual USB
device over the information from udev's database.
[#137](https://github.com/serialport/serialport-rs/pull/137)
* Raise MSRV from 1.56.1 to 1.59.0.
[#137](https://github.com/serialport/serialport-rs/pull/137)

### Fixed

Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ authors = [
"Bryant Mairs <[email protected]>",
"Jesse Braham <[email protected]>",
]
edition = "2018"
rust-version = "1.56.1"
edition = "2021"
rust-version = "1.59.0"
description = "A cross-platform low-level serial port library."
documentation = "https://docs.rs/serialport"
repository = "https://github.com/serialport/serialport-rs"
Expand All @@ -21,6 +21,7 @@ nix = { version = "0.26", default-features = false, features = ["fs", "ioctl", "

[target.'cfg(all(target_os = "linux", not(target_env = "musl")))'.dependencies]
libudev = { version = "0.3.0", optional = true }
unescaper = "0.1.3"

[target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies]
core-foundation-sys = "0.8.4"
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![crates.io version badge](https://img.shields.io/crates/v/serialport.svg)](https://crates.io/crates/serialport)
[![Documentation](https://docs.rs/serialport/badge.svg)](https://docs.rs/serialport)
[![GitHub workflow status](https://img.shields.io/github/actions/workflow/status/serialport/serialport-rs/ci.yaml?branch=main&logo=github)](https://github.com/serialport/serialport-rs/actions)
[![Minimum Stable Rust Version](https://img.shields.io/badge/Rust-1.56.1-blue?logo=rust)](https://blog.rust-lang.org/2021/11/01/Rust-1.56.1.html)
[![Minimum Stable Rust Version](https://img.shields.io/badge/Rust-1.59.0-blue?logo=rust)](https://blog.rust-lang.org/2022/02/24/Rust-1.59.0.html)

> **Note:** This is a fork of the original
[serialport-rs](https://gitlab.com/susurrus/serialport-rs) project on GitLab. Please note there have
Expand Down Expand Up @@ -109,7 +109,8 @@ can help debug software or hardware errors.

# Dependencies

Rust versions 1.56.1 and higher are supported.
Rust versions 1.59.0 and higher are supported by the library itself. There are
examples requiring newer versions of Rust.

For GNU/Linux `pkg-config` headers are required:

Expand Down
62 changes: 56 additions & 6 deletions src/posix/enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,57 @@ fn udev_hex_property_as_int<T>(
}
}

/// Looks up a property which is provided in two "flavors": Where special charaters and whitespaces
/// are encoded/escaped and where they are replaced (with underscores). This is for example done
/// by udev for manufacturer and model information.
///
/// See
/// https://github.com/systemd/systemd/blob/38c258398427d1f497268e615906759025e51ea6/src/udev/udev-builtin-usb_id.c#L432
/// for details.
#[cfg(all(target_os = "linux", not(target_env = "musl"), feature = "libudev"))]
fn udev_property_encoded_or_replaced_as_string(
d: &libudev::Device,
encoded_key: &str,
replaced_key: &str,
) -> Option<String> {
udev_property_as_string(d, encoded_key)
.and_then(|s| unescaper::unescape(&s).ok())
.or_else(|| udev_property_as_string(d, replaced_key))
.map(udev_restore_spaces)
}

/// Converts the underscores from `udev_replace_whitespace` back to spaces quick and dirtily. We
/// are ignoring the different types of whitespaces and the substitutions from `udev_replace_chars`
/// deliberately for keeping a low profile.
///
/// See
/// https://github.com/systemd/systemd/blob/38c258398427d1f497268e615906759025e51ea6/src/shared/udev-util.c#L281
/// for more details.
#[cfg(all(target_os = "linux", not(target_env = "musl"), feature = "libudev"))]
fn udev_restore_spaces(source: String) -> String {
source.replace('_', " ")
}

#[cfg(all(target_os = "linux", not(target_env = "musl"), feature = "libudev"))]
fn port_type(d: &libudev::Device) -> Result<SerialPortType> {
match d.property_value("ID_BUS").and_then(OsStr::to_str) {
Some("usb") => {
let serial_number = udev_property_as_string(d, "ID_SERIAL_SHORT");
// For devices on the USB, udev also provides manufacturer and product information from
// its hardware dataase. Use this as a fallback if this information is not provided
// from the device itself.
let manufacturer =
udev_property_encoded_or_replaced_as_string(d, "ID_VENDOR_ENC", "ID_VENDOR")
.or_else(|| udev_property_as_string(d, "ID_VENDOR_FROM_DATABASE"));
let product =
udev_property_encoded_or_replaced_as_string(d, "ID_MODEL_ENC", "ID_MODEL")
.or_else(|| udev_property_as_string(d, "ID_MODEL_FROM_DATABASE"));
Ok(SerialPortType::UsbPort(UsbPortInfo {
vid: udev_hex_property_as_int(d, "ID_VENDOR_ID", &u16::from_str_radix)?,
pid: udev_hex_property_as_int(d, "ID_MODEL_ID", &u16::from_str_radix)?,
serial_number,
manufacturer: udev_property_as_string(d, "ID_VENDOR_FROM_DATABASE")
.or_else(|| udev_property_as_string(d, "ID_VENDOR")),
product: udev_property_as_string(d, "ID_MODEL_FROM_DATABASE")
.or_else(|| udev_property_as_string(d, "ID_MODEL")),
manufacturer,
product,
#[cfg(feature = "usbportinfo-interface")]
interface: udev_hex_property_as_int(d, "ID_USB_INTERFACE_NUM", &u8::from_str_radix)
.ok(),
Expand All @@ -106,12 +144,24 @@ fn port_type(d: &libudev::Device) -> Result<SerialPortType> {
.into_iter()
.collect::<Option<Vec<_>>>();
if usb_properties.is_some() {
// For USB devices reported at a PCI bus, there is apparently no fallback
// information from udevs hardware database provided.
let manufacturer = udev_property_encoded_or_replaced_as_string(
d,
"ID_USB_VENDOR_ENC",
"ID_USB_VENDOR",
);
let product = udev_property_encoded_or_replaced_as_string(
d,
"ID_USB_MODEL_ENC",
"ID_USB_MODEL",
);
Ok(SerialPortType::UsbPort(UsbPortInfo {
vid: udev_hex_property_as_int(d, "ID_USB_VENDOR_ID", &u16::from_str_radix)?,
pid: udev_hex_property_as_int(d, "ID_USB_MODEL_ID", &u16::from_str_radix)?,
serial_number: udev_property_as_string(d, "ID_USB_SERIAL_SHORT"),
manufacturer: udev_property_as_string(d, "ID_USB_VENDOR"),
product: udev_property_as_string(d, "ID_USB_MODEL"),
manufacturer,
product,
#[cfg(feature = "usbportinfo-interface")]
interface: udev_hex_property_as_int(
d,
Expand Down
Loading