Check inotify paths are valid. Add dgu/egpu/ac_online checks

This commit is contained in:
Luke D. Jones
2022-09-24 14:34:15 +12:00
parent 30550aaa91
commit 7939b00aa3
17 changed files with 210 additions and 43 deletions

View File

@@ -15,7 +15,8 @@ pub enum PlatformError {
AttrNotFound(String),
MissingFunction(String),
MissingLedBrightNode(String, std::io::Error),
Io(String, std::io::Error),
IoPath(String, std::io::Error),
Io(std::io::Error),
NoAuraKeyboard,
NoAuraNode,
}
@@ -33,9 +34,10 @@ impl fmt::Display for PlatformError {
PlatformError::Write(path, error) => write!(f, "Write {}: {}", path, error),
PlatformError::NotSupported => write!(f, "Not supported"),
PlatformError::AttrNotFound(deets) => write!(f, "Attribute not found: {}", deets),
PlatformError::Io(deets) => write!(f, "std::io error: {}", deets),
PlatformError::MissingFunction(deets) => write!(f, "Missing functionality: {}", deets),
PlatformError::MissingLedBrightNode(path, error) => write!(f, "Led node at {} is missing, please check you have the required patch or dkms module installed: {}", path, error),
PlatformError::Io(path, detail) => write!(f, "std::io error: {} {}", path, detail),
PlatformError::IoPath(path, detail) => write!(f, "{} {}", path, detail),
PlatformError::NoAuraKeyboard => write!(f, "No supported Aura keyboard"),
PlatformError::NoAuraNode => write!(f, "No Aura keyboard node found"),
}
@@ -49,3 +51,9 @@ impl From<rusb::Error> for PlatformError {
PlatformError::USB(err)
}
}
impl From<std::io::Error> for PlatformError {
fn from(err: std::io::Error) -> Self {
PlatformError::Io(err)
}
}

View File

@@ -21,11 +21,13 @@ impl HidRaw {
for device in enumerator
.scan_devices()
.map_err(|e| PlatformError::Io("enumerator".to_owned(), e))?
.map_err(|e| PlatformError::IoPath("enumerator".to_owned(), e))?
{
if let Some(parent) = device
.parent_with_subsystem_devtype("usb", "usb_device")
.map_err(|e| PlatformError::Io(device.devpath().to_string_lossy().to_string(), e))?
.map_err(|e| {
PlatformError::IoPath(device.devpath().to_string_lossy().to_string(), e)
})?
{
if let Some(parent) = parent.attribute_value("idProduct") {
if parent == id_product {
@@ -47,9 +49,9 @@ impl HidRaw {
let mut file = OpenOptions::new()
.write(true)
.open(&self.0)
.map_err(|e| PlatformError::Io(self.0.to_string_lossy().to_string(), e))?;
.map_err(|e| PlatformError::IoPath(self.0.to_string_lossy().to_string(), e))?;
// println!("write: {:02x?}", &message);
file.write_all(message)
.map_err(|e| PlatformError::Io(self.0.to_string_lossy().to_string(), e))
.map_err(|e| PlatformError::IoPath(self.0.to_string_lossy().to_string(), e))
}
}

View File

@@ -45,7 +45,7 @@ pub fn read_attr_bool(device: &Device, attr_name: &str) -> Result<bool> {
pub fn write_attr_bool(device: &mut Device, attr: &str, value: bool) -> Result<()> {
device
.set_attribute_value(attr, &(value as u8).to_string())
.map_err(|e| PlatformError::Io(attr.into(), e))
.map_err(|e| PlatformError::IoPath(attr.into(), e))
}
pub fn read_attr_u8(device: &Device, attr_name: &str) -> Result<u8> {
@@ -59,7 +59,7 @@ pub fn read_attr_u8(device: &Device, attr_name: &str) -> Result<u8> {
pub fn write_attr_u8(device: &mut Device, attr: &str, value: u8) -> Result<()> {
device
.set_attribute_value(attr, &(value).to_string())
.map_err(|e| PlatformError::Io(attr.into(), e))
.map_err(|e| PlatformError::IoPath(attr.into(), e))
}
pub fn read_attr_u8_array(device: &Device, attr_name: &str) -> Result<Vec<u8>> {
@@ -79,7 +79,7 @@ pub fn write_attr_u8_array(device: &mut Device, attr: &str, values: &[u8]) -> Re
let tmp = tmp.trim();
device
.set_attribute_value(attr, &tmp)
.map_err(|e| PlatformError::Io(attr.into(), e))
.map_err(|e| PlatformError::IoPath(attr.into(), e))
}
#[cfg(test)]

View File

@@ -21,9 +21,19 @@ macro_rules! watch_attr {
pub fn fn_name(&self) -> Result<inotify::Inotify> {
let mut path = self.$item.clone();
path.push($attr_name);
let mut inotify = inotify::Inotify::init().unwrap();
inotify.add_watch(path.to_str().unwrap(), inotify::WatchMask::MODIFY).unwrap();
Ok(inotify)
if let Some(path) = path.to_str() {
let mut inotify = inotify::Inotify::init()?;
inotify.add_watch(path, inotify::WatchMask::MODIFY)
.map_err(|e| {
if e.kind() == std::io::ErrorKind::NotFound {
PlatformError::AttrNotFound(format!("{}", $attr_name))
} else {
PlatformError::IoPath(format!("{}", path), e)
}
})?;
return Ok(inotify);
}
Err(PlatformError::AttrNotFound(format!("{}", $attr_name)))
}
});
};

View File

@@ -102,4 +102,5 @@ impl AsusPower {
}
attr_u8!("charge_control_end_threshold", battery);
attr_u8!("online", mains);
}