diff --git a/POC_in_rust/Cargo.toml b/POC_in_rust/Cargo.toml index b977416..f2a6aa8 100644 --- a/POC_in_rust/Cargo.toml +++ b/POC_in_rust/Cargo.toml @@ -4,4 +4,6 @@ version = "0.1.0" edition = "2024" [dependencies] +env_logger = "0.11.8" +futures-lite = "2.6.0" nusb = "0.1.13" diff --git a/POC_in_rust/src/main.rs b/POC_in_rust/src/main.rs index 28d439a..d5c39a2 100644 --- a/POC_in_rust/src/main.rs +++ b/POC_in_rust/src/main.rs @@ -1,11 +1,38 @@ -use nusb; +use futures_lite::future::block_on; +use nusb::{transfer::{ RequestBuffer, TransferError }, Interface}; +use std::{thread, time}; static USB_VENDOR: u16 = 0xffff; static USB_PRODUCT: u16 = 0x0035; -fn main() { - let dev = nusb::list_devices().unwrap().find(|dev| dev.vendor_id() == USB_VENDOR && dev.product_id() == USB_PRODUCT).expect("Device not connected!"); - let device = dev.open().expect("failed to open device"); - let interface = device.claim_interface(0).expect("failed to claim interface"); - println!("Success") +fn get_interface() -> Option { + let dev = nusb::list_devices().ok()?.find(|dev| dev.vendor_id() == USB_VENDOR && dev.product_id() == USB_PRODUCT)?; + let device = dev.open().ok()?; + device.detach_and_claim_interface(0).ok() } + +fn main() { + env_logger::init(); + let mut interface = get_interface().unwrap(); + loop { + let buf = RequestBuffer::new(8); + let data = block_on(interface.interrupt_in( + 0x81, buf + )); + match data.status { + Err(TransferError::Disconnected) => { + interface = (||{loop{ + if let Some(intfc) = get_interface(){ + return intfc; + } + let ten_milis = time::Duration::from_millis(10); + thread::sleep(ten_milis); + }})() + } + Ok(()) => { + println!("{:?}", data.data); + } + _ => todo!() + } + } +} \ No newline at end of file