I have tried to write bulk to an usb device to learn how to use libusb from rust.
To do above I have attempted to do that write some data with bulk mode when only pixel6 is connected.
The following is the code, you may feel it bit disgusting but for simply, Almost of all error handings are omitted.
An error is caused when claim_interface is called.
When claim_interface has called, the error caused that NotFound.
It seems like means there is no interface that assigned to the given interface number according to error reference of rusb.
Why the error is happen?
Log:
[timestamp] [threadID] facility level [function call] <message>
--------------------------------------------------------------------------------
[ 0.005973] [000015a8] libusb: debug [libusb_get_device_list]
[ 0.005981] [000015a8] libusb: debug [libusb_get_device_descriptor]
[ 0.005983] [000015a8] libusb: debug [libusb_open] open 7.8
[ 0.006006] [000015a8] libusb: debug [usbi_add_event_source] add fd 7 events 4
[ 0.006009] [000015a8] libusb: debug [libusb_get_device_descriptor]
[ 0.006011] [000015a8] libusb: debug [libusb_get_config_descriptor] index 0
[ 0.006015] [000015a8] libusb: debug [parse_endpoint] skipping descriptor 0x30
[ 0.006016] [000015a8] libusb: debug [parse_endpoint] skipping descriptor 0x30
[ 0.006021] [000015a8] libusb: debug [libusb_kernel_driver_active] interface 0
[ 0.006023] [000015a8] libusb: debug [libusb_set_configuration] configuration 0
[ 0.007677] [000015a8] libusb: debug [libusb_claim_interface] interface 0
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: NotFound', src/bulk.rs:37:68
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
[ 0.007738] [000015a8] libusb: debug [libusb_close]
[ 0.007743] [000015a8] libusb: debug [usbi_remove_event_source] remove fd 7Code:
use rusb::devices;
use rusb::Direction::{In, Out};
use rusb::TransferType::{Bulk, Control};
use rusb::{open_device_with_vid_pid, TransferType};
use std::time::Duration;
pub fn pixel6_bulk() { write_to_device(6353, 20199, Bulk);
}
fn write_to_device(vendor_id: u16, product_id: u16, ttype: TransferType) { let mut has_kernel_driver: bool = false; if let Some(mut device_handle) = open_device_with_vid_pid(vendor_id, product_id) { let dev = device_handle.device(); let dd = dev.device_descriptor().unwrap(); let num_config = dd.num_configurations(); for p in 0..num_config { let cd = dev.config_descriptor(p).unwrap(); let ifaces = cd.interfaces(); for iface in ifaces { let inumber = iface.number(); let ifds = iface.descriptors(); for ifd in ifds { let setting_number = ifd.setting_number(); for efd in ifd.endpoint_descriptors() { if efd.direction() == Out && efd.transfer_type() == ttype { let buf: &[u8] = "Hello".as_bytes(); let endpoint_address = efd.address(); if device_handle.kernel_driver_active(inumber).unwrap() { has_kernel_driver = match device_handle.detach_kernel_driver(inumber) { Ok(_) => true, _ => false, }; } //let endpoint_number = efd.number(); device_handle.set_active_configuration(p).unwrap(); device_handle.claim_interface(inumber).unwrap(); device_handle .set_alternate_setting(inumber, setting_number) .unwrap(); match device_handle.write_bulk( endpoint_address, buf, Duration::new(2, 0), ) { Ok(n) => println!("{} bytes was written with bulk", n), Err(_) => println!("Write_bulk was failed"), } if has_kernel_driver { match device_handle.attach_kernel_driver(inumber) { Ok(_) => (), Err(_) => println!("Kernel driver attach failed"), } } device_handle.release_interface(inumber).unwrap(); } } } } } } else { panic!("There is no device"); }
} 4 Related questions 599 Why doesn't println! work in Rust unit tests? 348 Why are Rust executables so huge? 508 How do you disable dead code warnings at the crate level in Rust? Related questions 599 Why doesn't println! work in Rust unit tests? 348 Why are Rust executables so huge? 508 How do you disable dead code warnings at the crate level in Rust? 461 How do I print in Rust the type of a variable? 356 How do I split a string in Rust? 408 Why does the Rust compiler not optimize code assuming that two mutable references cannot alias? 278 Why are explicit lifetimes needed in Rust? 303 What's the de-facto way of reading and writing files in Rust 1.x? 0 libusb custom file system 270 How can a Rust program access metadata from its Cargo package? Load 7 more related questions Show fewer related questions Reset to default