Skip to content

Commit

Permalink
udev_hwdb feature for explicitly enabling hwdb
Browse files Browse the repository at this point in the history
  • Loading branch information
tuna-f1sh committed Nov 21, 2023
1 parent 1a23a47 commit 7911e60
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 43 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:
fail-fast: false
matrix:
job:
- { os: ubuntu-latest, target: aarch64-unknown-linux-gnu, use-cross: true }
- { os: ubuntu-latest, target: x86_64-pc-windows-gnu, use-cross: true }
- { os: ubuntu-latest, target: x86_64-unknown-linux-gnu, use-cross: false }
- { os: macos-latest, target: universal-apple-darwin, use-cross: false }
- { os: ubuntu-latest, target: aarch64-unknown-linux-gnu, use-cross: true, feature-flags: "-F udev" }
- { os: ubuntu-latest, target: x86_64-pc-windows-gnu, use-cross: true, feature-flags: "--all-features" }
- { os: ubuntu-latest, target: x86_64-unknown-linux-gnu, use-cross: false, feature-flags: "--all-features" }
- { os: macos-latest, target: universal-apple-darwin, use-cross: false, feature-flags: "--all-features" }
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@v1
Expand Down Expand Up @@ -74,14 +74,14 @@ jobs:
run: |
case ${{ matrix.job.target }} in
universal-apple-*)
cargo build --locked --release --all-features --target=aarch64-apple-darwin
cargo build --locked --release --all-features --target=x86_64-apple-darwin
cargo build --locked --release ${{ matrix.job.feature-flags }} --target=aarch64-apple-darwin
cargo build --locked --release ${{ matrix.job.feature-flags }} --target=x86_64-apple-darwin
mkdir -p target/universal-apple-darwin/release
# merge into universal
lipo -create -output target/universal-apple-darwin/release/cyme target/aarch64-apple-darwin/release/cyme target/x86_64-apple-darwin/release/cyme
;;
*)
${{ env.CARGO_CMD }} build --locked --release --all-features --target=${{ matrix.job.target }}
${{ env.CARGO_CMD }} build --locked --release ${{ matrix.job.feature-flags }} --target=${{ matrix.job.target }}
;;
esac
Expand Down
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,21 @@ diff = "0.1"
assert-json-diff = "2.0.2"

[target.x86_64-unknown-linux-gnu.dependencies]
udev = { version = "^0.8.0", optional = true, features = ["hwdb"] }
udev = { version = "^0.8.0", optional = true }
rusb = { git = "https://github.com/tuna-f1sh/rusb.git", version = "0.9.1" }

[target.arm-unknown-linux-gnueabihf.dependencies]
udev = { version = "^0.8.0", optional = true, features = ["hwdb"] }
udev = { version = "^0.8.0", optional = true }
rusb = { git = "https://github.com/tuna-f1sh/rusb.git", version = "0.9.1" }

[target.aarch64-unknown-linux-gnu.dependencies]
udev = { version = "^0.8.0", optional = true, features = ["hwdb"] }
udev = { version = "^0.8.0", optional = true }
rusb = { git = "https://github.com/tuna-f1sh/rusb.git", version = "0.9.1" }

[features]
libusb = ["dep:rusb"]
udev = ["dep:udev"]
udev_hwdb = ["udev/hwdb"]
usb_test = []
cli_generate = ["dep:clap_complete", "dep:clap_mangen"] # for generating man and completions
default = ["libusb"]
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ pub mod icon;
pub mod lsusb;
pub mod system_profiler;
pub mod types;
#[cfg(target_os = "linux")]
#[cfg(feature = "udev")]
#[cfg(all(target_os = "linux", feature = "udev"))]
pub mod udev;
pub mod usb;

Expand Down
8 changes: 4 additions & 4 deletions src/lsusb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,13 +832,13 @@ pub mod names {
/// Wrapper around [`crate::udev::hwdb_get`] so that it can be 'used' without feature
#[allow(unused_variables)]
fn hwdb_get(modalias: &str, key: &'static str) -> Result<Option<String>, Error> {
#[cfg(all(target_os = "linux", feature = "udev"))]
return crate::udev::hwdb_get(modalias, key);
#[cfg(all(target_os = "linux", feature = "udev_hwdb"))]
return crate::udev::hwdb::hwdb_get(modalias, key);

#[cfg(not(all(target_os = "linux", feature = "udev")))]
#[cfg(not(all(target_os = "linux", feature = "udev_hwdb")))]
return Err(Error::new(
ErrorKind::Unsupported,
"hwdb_get requires 'udev' feature",
"hwdb_get requires 'udev_hwdb' feature",
));
}
}
Expand Down
61 changes: 34 additions & 27 deletions src/udev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,34 +129,41 @@ pub fn get_udev_attribute<T: AsRef<std::ffi::OsStr> + std::fmt::Display>(
.map(|s| s.to_str().unwrap_or("").to_string()))
}

/// Lookup an entry in the udev hwdb given the `modalias` and `key`.
/// udev hwdb lookup functions
///
/// Should act like https://github.com/gregkh/usbutils/blob/master/names.c#L115
///
/// ```
/// use cyme::udev::hwdb_get;
///
/// let modalias = "usb:v1D6Bp0001";
/// let vendor = hwdb_get(&modalias, "ID_VENDOR_FROM_DATABASE").unwrap();
///
/// assert_eq!(vendor, Some("Linux Foundation".into()));
///
/// let modalias = "usb:v*p*d*dc03dsc01dp01*";
/// let vendor = hwdb_get(&modalias, "ID_USB_PROTOCOL_FROM_DATABASE").unwrap();
///
/// assert_eq!(vendor, Some("Keyboard".into()));
/// ```
pub fn hwdb_get(modalias: &str, key: &'static str) -> Result<Option<String>, Error> {
let hwdb = udevlib::Hwdb::new().map_err(|e| {
Error::new(
ErrorKind::Udev,
&format!("Failed to get hwdb: Error({})", e.to_string()),
)
})?;
/// Protected by the `udev_hwdb` feature because 'libudev-sys' excludes hwdb ffi bindings if native udev does not support hwdb
#[cfg(feature = "udev_hwdb")]
pub mod hwdb {
use super::*;
/// Lookup an entry in the udev hwdb given the `modalias` and `key`.
///
/// Should act like https://github.com/gregkh/usbutils/blob/master/names.c#L115
///
/// ```
/// use cyme::udev::hwdb_get;
///
/// let modalias = "usb:v1D6Bp0001";
/// let vendor = hwdb_get(&modalias, "ID_VENDOR_FROM_DATABASE").unwrap();
///
/// assert_eq!(vendor, Some("Linux Foundation".into()));
///
/// let modalias = "usb:v*p*d*dc03dsc01dp01*";
/// let vendor = hwdb_get(&modalias, "ID_USB_PROTOCOL_FROM_DATABASE").unwrap();
///
/// assert_eq!(vendor, Some("Keyboard".into()));
/// ```
pub fn hwdb_get(modalias: &str, key: &'static str) -> Result<Option<String>, Error> {
let hwdb = udevlib::Hwdb::new().map_err(|e| {
Error::new(
ErrorKind::Udev,
&format!("Failed to get hwdb: Error({})", e.to_string()),
)
})?;

Ok(hwdb
.query_one(&modalias.to_string(), &key.to_string())
.map(|s| s.to_str().unwrap_or("").to_string()))
Ok(hwdb
.query_one(&modalias.to_string(), &key.to_string())
.map(|s| s.to_str().unwrap_or("").to_string()))
}
}

#[cfg(test)]
Expand All @@ -173,7 +180,7 @@ mod tests {
}

/// Tests can lookup bInterfaceClass of the root hub, which is always 09
#[cfg_attr(not(feature = "usb_test"), ignore)]
#[cfg_attr(not(feature = "usb_test", feature = "udev_hwdb"), ignore)]
#[test]
fn test_udev_attribute() {
let interface_class = get_udev_attribute("1-0:1.0", "bInterfaceClass").unwrap();
Expand Down

0 comments on commit 7911e60

Please sign in to comment.