Fix issue with trying to find dev nodes before they are available

This commit is contained in:
Luke D Jones
2020-08-28 21:02:17 +12:00
parent b70137eec8
commit ee020085f8

View File

@@ -132,8 +132,8 @@ impl CtrlKbdBacklight {
#[inline]
pub fn new(id_product: &str, supported_modes: Vec<u8>) -> Result<Self, std::io::Error> {
Ok(CtrlKbdBacklight {
led_node: Self::scan_led_node(id_product)?,
kbd_node: Self::scan_kbd_node(id_product)?,
led_node: Self::get_node_failover(id_product, Self::scan_led_node)?,
kbd_node: Self::get_node_failover(id_product, Self::scan_kbd_node)?,
// brightness node path should always be constant but this *might* change?
bright_node: "/sys/class/leds/asus::kbd_backlight/brightness".to_string(),
supported_modes,
@@ -141,6 +141,28 @@ impl CtrlKbdBacklight {
})
}
fn get_node_failover(id_product: &str, fun: fn(&str) -> Result<String, std::io::Error>) -> Result<String, std::io::Error> {
for n in 0..2 {
match fun(id_product) {
Ok(o) => return Ok(o),
Err(e) => {
if n > 0 {
warn!("Looking for node: {}", e.to_string());
std::thread::sleep(std::time::Duration::from_secs(1));
} else {
return Err(e);
}
}
}
}
// Shouldn't be possible to reach this...
let err = std::io::Error::new(
std::io::ErrorKind::NotFound,
"node not found",
);
Err(err)
}
fn scan_led_node(id_product: &str) -> Result<String, std::io::Error> {
let mut enumerator = udev::Enumerator::new()?;
enumerator.match_subsystem("hidraw")?;