diff --git a/src/main.rs b/src/main.rs index 92c2add3..d044598d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,60 +1,111 @@ extern crate rusb; -use rusb::Direction; +use rusb::{DeviceHandle, Error}; use std::time::Duration; -fn main() -> Result<(), Box> { - let mut context = rusb::Context::new().unwrap(); +static LED_MSG_LEN: usize = 17; +static LED_SPEED_BYTES: [u8; 3] = [0xe1, 0xeb, 0xf5]; +static LED_INIT1: [u8; 2] = [0x5d, 0xb9]; +static LED_INIT2: &'static str = "]ASUS Tech.Inc."; // 0x5d ASUS Tech.Inc. +static LED_INIT3: [u8; 6] = [0x5d, 0x05, 0x20, 0x31, 0x00, 0x08]; +static LED_INIT4: &'static str = "^ASUS Tech.Inc."; // 0x5e ASUS Tech.Inc. +static LED_INIT5: [u8; 6] = [0x5e, 0x05, 0x20, 0x31, 0x00, 0x08]; +static LED_SET: [u8; 2] = [0x5d, 0xb5]; +static LED_APPLY: [u8; 2] = [0x5d, 0xb4]; +static LED_BRIGHT_BYTES: [u8; 6] = [0x5a, 0xba, 0xc5, 0xc4, 0, 0]; // Pos 4 = set brightness +fn get_device(vendor: u16, product: u16) -> Result, Error> { for device in rusb::devices().unwrap().iter() { let device_desc = device.device_descriptor().unwrap(); - if device_desc.vendor_id() == 0x0B05 && device_desc.product_id() == 0x1866 { - let mut handle = device.open()?; - handle.set_active_configuration(1).unwrap_or(()); + if device_desc.vendor_id() == vendor && device_desc.product_id() == product { + return device.open(); + } + } + Err(Error::NoDevice) +} - let config = device.config_descriptor(0).unwrap(); - // Interface with outputs - let mut keyboard_out = 0; - let mut led_config = 0; - for iface in config.interfaces() { - for desc in iface.descriptors() { - for endpoint in desc.endpoint_descriptors() { - if endpoint.direction() == Direction::In && endpoint.address() == 0x83 { - keyboard_out = desc.interface_number(); - } else if endpoint.direction() == Direction::Out - && endpoint.address() == 0x81 - { - led_config = desc.interface_number(); - } - } - } - } - let iface_out = config.interfaces().nth(keyboard_out as usize).unwrap(); - let iface_led = config.interfaces().nth(led_config as usize).unwrap(); +fn main() -> Result<(), Box> { + let mut handle = get_device(0x0B05, 0x1866)?; + handle.set_active_configuration(0).unwrap_or(()); - if handle.kernel_driver_active(0).unwrap() { - handle.detach_kernel_driver(0).unwrap(); - } - - handle.claim_interface(iface_out.number()).unwrap(); - handle.claim_interface(iface_led.number()).unwrap(); - - // WRITE LED - - // READ KEYBOARD - let mut buf = [0; 32]; - loop { - match handle.read_interrupt(0x83, &mut buf, Duration::new(5, 0)) { - Ok(_) => println!("{:?}", buf), - Err(err) => { - // RELEASE - // not needed handle.release_interface(interface.number())?; - // handle.attach_kernel_driver(interface.number())?; - return Err(Box::new(err)); - } + let config = handle.device().config_descriptor(0).unwrap(); + // Interface with outputs + let mut keyboard_iface_num = 0; + let mut led_iface_num = 0; + for iface in config.interfaces() { + for desc in iface.descriptors() { + for endpoint in desc.endpoint_descriptors() { + if endpoint.address() == 0x83 { + keyboard_iface_num = desc.interface_number(); + } else if endpoint.address() == 0x81 { + led_iface_num = desc.interface_number(); } } } } - Ok(()) + + if handle.kernel_driver_active(keyboard_iface_num).unwrap() { + handle.detach_kernel_driver(keyboard_iface_num).unwrap(); + } + if handle.kernel_driver_active(led_iface_num).unwrap() { + handle.detach_kernel_driver(led_iface_num).unwrap(); + } + + let iface_out = config + .interfaces() + .nth(keyboard_iface_num as usize) + .unwrap(); + let iface_led = config.interfaces().nth(led_iface_num as usize).unwrap(); + + handle.claim_interface(iface_out.number()).unwrap(); + handle.claim_interface(iface_led.number()).unwrap(); + + // WRITE LED + // bmRequestType = 0x21 + // bRequest = 0x09 + // wValue = 0x035D + // wIndex = 0 + println!("BEGIN INIT"); + handle.write_control(0x21, 0x09, 0x035D, 0, &LED_INIT1, Duration::new(0, 5))?; + handle.write_control( + 0x21, + 0x09, + 0x035D, + 0, + &LED_INIT2.as_bytes(), + Duration::new(0, 5), + )?; + handle.write_control(0x21, 0x09, 0x035D, 0, &LED_INIT3, Duration::new(0, 5))?; + handle.write_control( + 0x21, + 0x09, + 0x035D, + 0, + &LED_INIT4.as_bytes(), + Duration::new(0, 5), + )?; + handle.write_control(0x21, 0x09, 0x035D, 0, &LED_INIT5, Duration::new(0, 5))?; + println!("END INIT"); + + println!("BEGIN BRIGHT"); + let mut bright = LED_BRIGHT_BYTES.clone(); + bright[4] = 1; + println!("{:x?}", bright); + handle.write_control(0x21, 0x09, 0x035D, 0, &bright, Duration::new(0, 5))?; + println!("END BRIGHT"); + handle.write_control(0x21, 0x09, 0x035D, 0, &LED_SET, Duration::new(0, 5))?; + handle.write_control(0x21, 0x09, 0x035D, 0, &LED_APPLY, Duration::new(0, 5))?; + println!("END"); + + // READ KEYBOARD + let mut buf = [0; 32]; + loop { + match handle.read_interrupt(0x83, &mut buf, Duration::new(0, 5)) { + Ok(_) => println!("{:?}", buf), + Err(err) => match err { + Error::Timeout => {} + _ => return Err(Box::new(err)), + }, + } + } }