diff --git a/Cargo.toml b/Cargo.toml index a315d89..11693d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +hidapi = "1.2.6" diff --git a/src/main.rs b/src/main.rs index e7a11a9..cc5b478 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,77 @@ -fn main() { - println!("Hello, world!"); +extern crate hidapi; + +use hidapi::*; + +fn ReceiveData(device: &HidDevice) -> Result,()> { + let mut usb_buf = vec![0u8; 65]; + let mut err_cnt = 3; + let mut chk_sum = 0; + + loop { + usb_buf[0] = 0x01; + match device.get_feature_report(&mut usb_buf[..]) { + Ok(res) => { + if usb_buf[0] == 0x1 { + for i in 0..res { + chk_sum ^= usb_buf[i]; + } + if chk_sum == 0 { + break Err(()); + } + break Ok(usb_buf); + } + }, + Err(_) => { + err_cnt -= 1; + if err_cnt == 0 { + break Err(()); + } + } + } + } +} + +fn SendData(device: &HidDevice, data: &[u8]) { + let mut usb_buf = [0u8; 65]; + let mut err_cnt = 3; + + loop { + let mut chk_sum = 0; + usb_buf[0] = 0x01; + for i in 1..data.len() { + usb_buf[i + 1] = data[i]; + chk_sum ^= data[i]; + } + + usb_buf[data.len() + 1] = chk_sum; + match device.send_feature_report(&usb_buf[..]) { + Ok(_) => break, + Err(_) => { + err_cnt -= 1; + if err_cnt == 0 { + break; + } + () + } + } + } +} + +fn main() { + println!("Get the version data from the keyboard:"); + + let api = HidApi::new().unwrap(); + + // connect to the device using VID and PID + let (VID, PID) = (0x24f0, 0x2037); + let device = api.open(VID, PID).unwrap(); + + let usb_init = [0xea, 0x02, 0xb0]; + SendData(&device, &usb_init[..]); + match ReceiveData(&device) { + Ok(data) => { + println!("It worked!") + }, + Err(_) => () + } }