Rust library and applications for the ChargerLAB POWER-Z KM003C USB-C power analyzer.
km003c-rs provides a cross-platform Rust implementation for communicating with the KM003C device, enabling real-time USB-C power analysis and USB Power Delivery message capture.
- Dual interface support: Vendor (bulk, ~0.6ms latency) or HID (interrupt)
- Cross-platform support using
nusb - Asynchronous communication with Tokio
- Automatic device discovery, initialization, and authentication
- Real-time voltage, current, and power measurements
- Two modes:
- Simple ADC: Single-shot readings with temperature and statistics
- AdcQueue streaming: High-speed continuous streaming (2, 10, 50, 1000 SPS)
- USB data line voltage measurements (D+, D-)
- USB-C CC line voltage measurements (CC1, CC2)
- Capture and parse USB PD messages
- Connection/disconnection event detection
- Full PD message parsing using the
usbpdcrate - Support for SPR and EPR source capabilities
- Chunked message reassembly for EPR
- Model, firmware version, hardware version
- Serial number and UUID
- Hardware ID and authentication level
Core library providing:
- Device communication and automatic initialization
- Streaming authentication (required for AdcQueue)
- ADC and AdcQueue data parsing
- USB PD event parsing
Command-line tools:
adc_simple- Single-shot ADC readings with device infoadc_queue_simple- AdcQueue streaming demotest_usbpd- USB PD negotiation capture
GUI application featuring:
- Real-time voltage/current/power plots
- AdcQueue streaming with configurable sample rates
- Adjustable time window (2s to 5min or all data)
- Device info panel with auth status
- Connect/disconnect control
Python bindings for parsing KM003C data structures.
- Rust 1.75+ (uses let-else and let-chains)
- USB access permissions (udev rules on Linux)
- POWER-Z KM003C device
git clone https://github.com/okhsunrog/km003c-rs.git
cd km003c-rs
cargo build --releaseCreate udev rules for non-root access:
sudo cp 71-powerz-km003c.rules /etc/udev/rules.d/
sudo udevadm control --reload-rules
sudo udevadm triggerThe rules use the uaccess tag for secure, dynamic access to logged-in users.
cargo run --bin adc_simplecargo run --bin adc_queue_simple -- --rate 50 --duration 10cargo run --bin test_usbpdcargo run --bin km003c-eguiuse km003c_lib::{DeviceConfig, KM003C, GraphSampleRate};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Connect with vendor interface (Full mode - includes init and auth)
let mut device = KM003C::new(DeviceConfig::vendor()).await?;
// Access device info (always available in Full mode)
let state = device.state().unwrap();
println!("{}", state); // Pretty-printed device info
println!("AdcQueue enabled: {}", state.adcqueue_enabled);
// Simple ADC reading
let adc = device.request_adc_data().await?;
println!("Voltage: {:.3} V", adc.vbus_v);
println!("Current: {:.3} A", adc.ibus_a);
// AdcQueue streaming (if authenticated)
if device.adcqueue_enabled() {
device.start_graph_mode(GraphSampleRate::Sps50).await?;
// ... poll for samples ...
device.stop_graph_mode().await?;
}
Ok(())
}// Vendor interface (Full mode) - recommended, fastest
let config = DeviceConfig::vendor();
// HID interface (Basic mode) - most compatible, ADC/PD polling only
let config = DeviceConfig::hid();
// Skip USB reset (default on macOS for compatibility)
let config = DeviceConfig::vendor().skip_reset();This implementation is based on reverse engineering documented at: km003c-protocol-research
The research repository contains:
- Complete protocol specification
- USB transport documentation
- PCAPNG captures and analysis tools
- Firmware analysis notes
- Device discovery and dual-interface communication
- Automatic initialization and streaming authentication
- Simple ADC measurements
- AdcQueue high-speed streaming (2-1000 SPS)
- USB PD message capture and parsing
- Memory read for device info/calibration
- Real-time GUI with plotting
- Linux (primary development platform)
- macOS (uses
--no-resetby default for compatibility) - Windows (uses cross-platform
nusb)
- Rust: 1.75+ (stable)
- Platforms: Linux, Windows, macOS
- Hardware: POWER-Z KM003C
Contributions welcome! See the research repository for protocol details.
MIT License - see LICENSE file.
- km003c-protocol-research - Protocol reverse engineering
- usbpd - Rust USB PD protocol library