Skip to content

Commit

Permalink
Increase device initialization delays and timeouts
Browse files Browse the repository at this point in the history
This should help slow USB hubs and KVM switches
References #161
  • Loading branch information
X3n0m0rph59 committed Jun 2, 2023
1 parent f6a5900 commit 5110322
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 117 deletions.
2 changes: 1 addition & 1 deletion eruption-hwutil/src/hwdevices/roccat_aimo_pad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl RoccatAimoPad {
// thread::sleep(Duration::from_millis(10));
// }

thread::sleep(Duration::from_millis(10));
thread::sleep(Duration::from_millis(constants::DEVICE_SETTLE_MILLIS));

Ok(())
}
Expand Down
8 changes: 7 additions & 1 deletion eruption/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,13 @@ pub const NUM_SLOTS: usize = 4;
pub const AFK_TIMEOUT_SECS: u64 = 0;

/// Amount of time that has to pass before we retry sending a command to the LED/control USB sub-device
pub const DEVICE_SETTLE_MILLIS: u64 = 80;
pub const DEVICE_SETTLE_MILLIS: u64 = 100;

/// Amount of time that we pause after sending a command to the LED/control USB sub-device
pub const DEVICE_SHORT_DELAY: u64 = 25;

/// This helps slow USB HUBs and KVM switches to not fail to init the device
pub const DEVICE_SETTLE_DELAY: u64 = 250;

/// Update sensors every n seconds
/// It is recommended to use a prime number value here
Expand Down
12 changes: 6 additions & 6 deletions eruption/src/hwdevices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,7 @@ pub fn get_input_dev_from_udev(usb_vid: u16, usb_pid: u16) -> Result<String> {
} else {
// wait for the device to be available
retry_counter -= 1;
thread::sleep(Duration::from_millis(500));
thread::sleep(Duration::from_millis(constants::DEVICE_SETTLE_DELAY));
}
}

Expand All @@ -1836,7 +1836,7 @@ pub fn get_input_dev_from_udev(usb_vid: u16, usb_pid: u16) -> Result<String> {
} else {
// wait for the enumerator to be available
retry_counter -= 1;
thread::sleep(Duration::from_millis(500));
thread::sleep(Duration::from_millis(constants::DEVICE_SETTLE_DELAY));
}
}
}
Expand All @@ -1849,7 +1849,7 @@ pub fn get_input_dev_from_udev(usb_vid: u16, usb_pid: u16) -> Result<String> {
} else {
// wait for the enumerator to be available
retry_counter -= 1;
thread::sleep(Duration::from_millis(500));
thread::sleep(Duration::from_millis(constants::DEVICE_SETTLE_DELAY));
}
}
}
Expand Down Expand Up @@ -1917,7 +1917,7 @@ pub fn get_input_dev_from_udev(usb_vid: u16, usb_pid: u16) -> Result<String> {
} else {
// wait for the device to be available
retry_counter -= 1;
thread::sleep(Duration::from_millis(500));
thread::sleep(Duration::from_millis(constants::DEVICE_SETTLE_DELAY));
}
}
Expand All @@ -1928,7 +1928,7 @@ pub fn get_input_dev_from_udev(usb_vid: u16, usb_pid: u16) -> Result<String> {
} else {
// wait for the enumerator to be available
retry_counter -= 1;
thread::sleep(Duration::from_millis(500));
thread::sleep(Duration::from_millis(constants::DEVICE_SETTLE_DELAY));
}
}
}
Expand All @@ -1941,7 +1941,7 @@ pub fn get_input_dev_from_udev(usb_vid: u16, usb_pid: u16) -> Result<String> {
} else {
// wait for the enumerator to be available
retry_counter -= 1;
thread::sleep(Duration::from_millis(500));
thread::sleep(Duration::from_millis(constants::DEVICE_SETTLE_DELAY));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions eruption/src/hwdevices/keyboards/corsair_strafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ impl CorsairStrafe {
// } else if !self.is_opened {
// Err(HwDeviceError::DeviceNotOpened {}.into())
// } else {
// thread::sleep(Duration::from_millis(70));
// thread::sleep(Duration::from_millis(constants::DEVICE_SETTLE_DELAY));

// Ok(())
// }
Expand All @@ -330,7 +330,7 @@ impl CorsairStrafe {
} else if !self.is_opened {
Err(HwDeviceError::DeviceNotOpened {}.into())
} else {
thread::sleep(Duration::from_millis(20));
thread::sleep(Duration::from_millis(constants::DEVICE_SETTLE_MILLIS));

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion eruption/src/hwdevices/keyboards/roccat_vulcan_1xx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ impl RoccatVulcan1xx {
Err(HwDeviceError::DeviceNotOpened {}.into())
} else {
// This helps slow USB HUBs and KVM switches to not fail to init the device
thread::sleep(Duration::from_millis(15));
thread::sleep(Duration::from_millis(constants::DEVICE_SETTLE_MILLIS));

loop {
let mut buf: [u8; 4] = [0; 4];
Expand Down
173 changes: 83 additions & 90 deletions eruption/src/hwdevices/keyboards/wooting_two_he_arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,12 @@ impl WootingTwoHeArm {

match result {
Ok(_result) => {
hexdump::hexdump_iter(&report_buffer).for_each(|s| trace!(" {}", s));
hexdump::hexdump_iter(&report_buffer).for_each(|s| debug!(" {}", s));

let mut buf = Vec::with_capacity(RESPONSE_SIZE);
match ctrl_dev.read_timeout(&mut buf, READ_RESPONSE_TIMEOUT) {
Ok(_result) => {
hexdump::hexdump_iter(&buf).for_each(|s| trace!(" {}", s));
hexdump::hexdump_iter(&buf).for_each(|s| debug!(" {}", s));

Ok(())
}
Expand Down Expand Up @@ -533,17 +533,13 @@ impl DeviceTrait for WootingTwoHeArm {
// TODO: Implement firmware version check

// This helps slow USB HUBs and KVM switches to not fail to init the device
thread::sleep(Duration::from_millis(15));
thread::sleep(Duration::from_millis(constants::DEVICE_SETTLE_DELAY));

self.v2_send_feature_report(Command::RESET_ALL_COMMAND as u8, &[0, 0, 0, 0])
.unwrap_or_else(|e| error!("Step 1: {}", e));
self.wait_for_ctrl_dev()
.unwrap_or_else(|e| error!("Wait 1: {}", e));

self.v2_send_feature_report(Command::COLOR_INIT_COMMAND as u8, &[0, 0, 0, 0])
.unwrap_or_else(|e| error!("Step 2: {}", e));
self.wait_for_ctrl_dev()
.unwrap_or_else(|e| error!("Wait 2: {}", e));

self.is_initialized = true;

Expand Down Expand Up @@ -695,83 +691,80 @@ impl KeyboardDeviceTrait for WootingTwoHeArm {
} else if !self.is_initialized {
Err(HwDeviceError::DeviceNotInitialized {}.into())
} else {
// let ctrl_dev = self.ctrl_hiddev.as_ref().lock();
// let ctrl_dev = ctrl_dev.as_ref().unwrap();

// let mut buf = [0; 8];
let ctrl_dev = self.ctrl_hiddev.as_ref().lock();
let ctrl_dev = ctrl_dev.as_ref().unwrap();

// match ctrl_dev.read_timeout(&mut buf, millis) {
// Ok(_size) => {
// if buf.iter().any(|e| *e != 0) {
// hexdump::hexdump_iter(&buf).for_each(|s| debug!(" {}", s));
// }
let mut buf = [0; RESPONSE_SIZE];

// // let fn_down = false;
match ctrl_dev.read_timeout(&mut buf, millis) {
Ok(_size) => {
if buf.iter().any(|e| *e != 0) {
hexdump::hexdump_iter(&buf).for_each(|s| debug!(" {}", s));
}

// // let event = match buf[0..5] {
// // // Key reports, incl. KEY_FN, ..
// // [0x03, 0x00, 0xfb, code, status] => match code {
// // 0x6d if fn_down => KeyboardHidEvent::PreviousSlot,
// let fn_down = false;

// // 0x7d if fn_down => KeyboardHidEvent::NextSlot,
// let event = match buf[0..5] {
// // Key reports, incl. KEY_FN, ..
// [0x03, 0x00, 0xfb, code, status] => match code {
// 0x6d if fn_down => KeyboardHidEvent::PreviousSlot,

// // _ => match status {
// // 0x00 => KeyboardHidEvent::KeyUp {
// // code: keyboard_hid_event_code_from_report(0xfb, code),
// // },
// 0x7d if fn_down => KeyboardHidEvent::NextSlot,

// // 0x01 => KeyboardHidEvent::KeyDown {
// // code: keyboard_hid_event_code_from_report(0xfb, code),
// // },
// _ => match status {
// 0x00 => KeyboardHidEvent::KeyUp {
// code: keyboard_hid_event_code_from_report(0xfb, code),
// },

// // _ => KeyboardHidEvent::Unknown,
// // },
// // },
// 0x01 => KeyboardHidEvent::KeyDown {
// code: keyboard_hid_event_code_from_report(0xfb, code),
// },

// // // CAPS LOCK, Easy Shift+, ..
// // [0x03, 0x00, 0x0a, code, status] => match code {
// // 0x39 | 0xff => match status {
// // 0x00 => KeyboardHidEvent::KeyDown {
// // code: keyboard_hid_event_code_from_report(0x0a, code),
// // },
// _ => KeyboardHidEvent::Unknown,
// },
// },

// // 0x01 => KeyboardHidEvent::KeyUp {
// // code: keyboard_hid_event_code_from_report(0x0a, code),
// // },
// // CAPS LOCK, Easy Shift+, ..
// [0x03, 0x00, 0x0a, code, status] => match code {
// 0x39 | 0xff => match status {
// 0x00 => KeyboardHidEvent::KeyDown {
// code: keyboard_hid_event_code_from_report(0x0a, code),
// },

// // _ => KeyboardHidEvent::Unknown,
// // },
// 0x01 => KeyboardHidEvent::KeyUp {
// code: keyboard_hid_event_code_from_report(0x0a, code),
// },

// // _ => KeyboardHidEvent::Unknown,
// // },
// _ => KeyboardHidEvent::Unknown,
// },

// // _ => KeyboardHidEvent::Unknown,
// // };
// _ => KeyboardHidEvent::Unknown,
// },

// /* match event {
// KeyboardHidEvent::KeyDown { code } => {
// // update our internal representation of the keyboard state
// let index = self.hid_event_code_to_key_index(&code) as usize;
// crate::KEY_STATES.write()[index] = true;
// }
// _ => KeyboardHidEvent::Unknown,
// };

// KeyboardHidEvent::KeyUp { code } => {
// // update our internal representation of the keyboard state
// let index = self.hid_event_code_to_key_index(&code) as usize;
// crate::KEY_STATES.write()[index] = false;
// }
/* match event {
KeyboardHidEvent::KeyDown { code } => {
// update our internal representation of the keyboard state
let index = self.hid_event_code_to_key_index(&code) as usize;
crate::KEY_STATES.write()[index] = true;
}
// _ => { /* ignore other events */ }
// } */
// Ok(event)
// }
KeyboardHidEvent::KeyUp { code } => {
// update our internal representation of the keyboard state
let index = self.hid_event_code_to_key_index(&code) as usize;
crate::KEY_STATES.write()[index] = false;
}
// Err(_) => Err(HwDeviceError::InvalidResult {}.into()),
// }
_ => { /* ignore other events */ }
} */

thread::sleep(Duration::from_millis(millis as u64));
Ok(KeyboardHidEvent::Unknown)
}

Ok(KeyboardHidEvent::Unknown)
Err(_) => Err(HwDeviceError::InvalidResult {}.into()),
}
}
}

Expand Down Expand Up @@ -870,7 +863,7 @@ impl KeyboardDeviceTrait for WootingTwoHeArm {
// }
// }

thread::sleep(Duration::from_millis(10));
thread::sleep(Duration::from_millis(constants::DEVICE_SHORT_DELAY));
}

Err(_) => return Err(HwDeviceError::WriteError {}.into()),
Expand Down Expand Up @@ -1016,35 +1009,35 @@ impl KeyboardDeviceTrait for WootingTwoHeArm {
}
}

// fn keyboard_hid_event_code_from_report(report: u8, code: u8) -> KeyboardHidEventCode {
// match report {
// 0xfb => match code {
// 16 => KeyboardHidEventCode::KEY_F1,
// 24 => KeyboardHidEventCode::KEY_F2,
// 33 => KeyboardHidEventCode::KEY_F3,
// 32 => KeyboardHidEventCode::KEY_F4,
fn keyboard_hid_event_code_from_report(report: u8, code: u8) -> KeyboardHidEventCode {
match report {
0xfb => match code {
16 => KeyboardHidEventCode::KEY_F1,
24 => KeyboardHidEventCode::KEY_F2,
33 => KeyboardHidEventCode::KEY_F3,
32 => KeyboardHidEventCode::KEY_F4,

// 40 => KeyboardHidEventCode::KEY_F5,
// 48 => KeyboardHidEventCode::KEY_F6,
// 56 => KeyboardHidEventCode::KEY_F7,
// 57 => KeyboardHidEventCode::KEY_F8,
40 => KeyboardHidEventCode::KEY_F5,
48 => KeyboardHidEventCode::KEY_F6,
56 => KeyboardHidEventCode::KEY_F7,
57 => KeyboardHidEventCode::KEY_F8,

// 17 => KeyboardHidEventCode::KEY_ESC,
// 119 => KeyboardHidEventCode::KEY_FN,
17 => KeyboardHidEventCode::KEY_ESC,
119 => KeyboardHidEventCode::KEY_FN,

// _ => KeyboardHidEventCode::Unknown(code),
// },
_ => KeyboardHidEventCode::Unknown(code),
},

// 0x0a => match code {
// 57 => KeyboardHidEventCode::KEY_CAPS_LOCK,
// 255 => KeyboardHidEventCode::KEY_EASY_SHIFT,
0x0a => match code {
57 => KeyboardHidEventCode::KEY_CAPS_LOCK,
255 => KeyboardHidEventCode::KEY_EASY_SHIFT,

// _ => KeyboardHidEventCode::Unknown(code),
// },
_ => KeyboardHidEventCode::Unknown(code),
},

// _ => KeyboardHidEventCode::Unknown(code),
// }
// }
_ => KeyboardHidEventCode::Unknown(code),
}
}

/// Map evdev event codes to key indices, for ISO variant
#[rustfmt::skip]
Expand Down
6 changes: 3 additions & 3 deletions eruption/src/hwdevices/mice/roccat_kain_2xx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use tracing::*;
use lazy_static::lazy_static;
use std::any::Any;
use std::collections::HashMap;
use std::thread;
use std::time::Duration;
use std::{mem::size_of, sync::Arc};

use crate::{constants, hwdevices, hwdevices::DeviceStatus};
Expand Down Expand Up @@ -550,8 +552,6 @@ impl DeviceTrait for RoccatKain2xx {

_ => { /* do nothing */ }
}

// thread::sleep(Duration::from_millis(15));
}

Ok(DeviceStatus(table))
Expand All @@ -575,7 +575,7 @@ impl DeviceTrait for RoccatKain2xx {

let result = read_results()?;

// thread::sleep(Duration::from_millis(15));
thread::sleep(Duration::from_millis(constants::DEVICE_SHORT_DELAY));

let buf: [u8; 22] = [
0x08, 0x03, 0x40, 0x00, 0x4b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Expand Down
4 changes: 2 additions & 2 deletions eruption/src/hwdevices/mice/roccat_kone_pro_air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ impl RoccatKoneProAir {

cntr += 1;

thread::sleep(Duration::from_millis(10));
thread::sleep(Duration::from_millis(constants::DEVICE_SHORT_DELAY));
}

Ok(DeviceStatus(table))
Expand Down Expand Up @@ -1206,7 +1206,7 @@ impl MouseDeviceTrait for RoccatKoneProAir {

poll_cntr += 1;

thread::sleep(Duration::from_millis(10));
thread::sleep(Duration::from_millis(constants::DEVICE_SHORT_DELAY));
}
}

Expand Down
Loading

0 comments on commit 5110322

Please sign in to comment.