#usb #linux #sysfs

yuca

Access USB Type-C device information on Linux

1 unstable release

new 0.1.0 Dec 19, 2024

#456 in Unix APIs


Used in yuca-cli

MIT/Apache

95KB
2.5K SLoC

yuca

crates.io docs.rs build status

Yuca is a Rust crate to access USB Type-C device information on Linux.

use std::error::Error;
use futures_lite::StreamExt;
use yuca::{sysfs::*, watcher::*};

fn show() -> Result<(), Box<dyn Error>> {
    for port in Port::collection()?.list()? {
        let port = port.open()?;

        println!("Port: {}", port.path().port);

        let Ok(partner) = port.partner().open() else { continue; };
        println!("  Partner: {}:{}",
            partner.identity().id_header().get()?.0.vendor_id(),
            partner.identity().product().get()?.product_id);
    }

    Ok(())
}

async fn watch() -> Result<(), Box<dyn Error>> {
    let (w, _) = Watcher::spawn_tokio(EventSource::Netlink)?;
    let mut stream = PartnerPath::any_added(&w)?;
    while let Some(path) = stream.next().await {
        let Ok(path) = path else { continue; };
        println!("Partner added to port: {}", path.port);
    }
    Ok(())
}

Terminology

This crate uses a variety of terms from the USB Power Delivery specification, which can be obtained from here. Consult the spec's "Terms and Abbreviations" for complete definitions.

Dependencies

~6–16MB
~220K SLoC