Manage add/remove aura

Serialize aura config filename
This commit is contained in:
Luke D. Jones
2024-03-18 16:22:18 +13:00
parent 014604724f
commit 5c43c31331
30 changed files with 690 additions and 298 deletions

View File

@@ -1,7 +1,7 @@
use std::cell::UnsafeCell;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use log::{info, warn};
use udev::Device;
@@ -10,12 +10,13 @@ use crate::error::{PlatformError, Result};
#[derive(Debug)]
pub struct HidRaw {
path: UnsafeCell<PathBuf>,
devfs_path: UnsafeCell<PathBuf>,
syspath: PathBuf,
prod_id: String,
}
impl HidRaw {
pub fn new(id_product: &str) -> Result<Self> {
pub fn new(id_product: &str) -> Result<(Self, Device)> {
let mut enumerator = udev::Enumerator::new().map_err(|err| {
warn!("{}", err);
PlatformError::Udev("enumerator failed".into(), err)
@@ -30,20 +31,23 @@ impl HidRaw {
.scan_devices()
.map_err(|e| PlatformError::IoPath("enumerator".to_owned(), e))?
{
if let Some(parent) = device
if let Some(parent_device) = device
.parent_with_subsystem_devtype("usb", "usb_device")
.map_err(|e| {
PlatformError::IoPath(device.devpath().to_string_lossy().to_string(), e)
})?
{
if let Some(parent) = parent.attribute_value("idProduct") {
PlatformError::IoPath(device.devpath().to_string_lossy().to_string(), e)
})? {
if let Some(parent) = parent_device.attribute_value("idProduct") {
if parent == id_product {
if let Some(dev_node) = device.devnode() {
info!("Using device at: {:?} for hidraw control", dev_node);
return Ok(Self {
path: UnsafeCell::new(dev_node.to_owned()),
prod_id: id_product.to_string(),
});
return Ok((
Self {
devfs_path: UnsafeCell::new(dev_node.to_owned()),
prod_id: id_product.to_string(),
syspath: device.syspath().into(),
},
parent_device,
));
}
}
}
@@ -56,10 +60,14 @@ impl HidRaw {
"Using device at: {:?} for <TODO: label control> control",
dev_node
);
return Ok(Self {
path: UnsafeCell::new(dev_node.to_owned()),
prod_id: id_product.to_string(),
});
return Ok((
Self {
devfs_path: UnsafeCell::new(dev_node.to_owned()),
prod_id: id_product.to_string(),
syspath: device.syspath().into(),
},
device,
));
}
}
}
@@ -70,15 +78,51 @@ impl HidRaw {
)))
}
pub fn from_device(device: Device) -> Result<Self> {
if let Some(parent) = device
.parent_with_subsystem_devtype("usb", "usb_device")
.map_err(|e| PlatformError::IoPath(device.devpath().to_string_lossy().to_string(), e))?
{
if let Some(dev_node) = device.devnode() {
if let Some(id_product) = parent.attribute_value("idProduct") {
return Ok(Self {
devfs_path: UnsafeCell::new(dev_node.to_owned()),
prod_id: id_product.to_string_lossy().into(),
syspath: device.syspath().into(),
});
}
}
}
Err(PlatformError::MissingFunction(
"hidraw dev no dev path".to_string(),
))
}
pub fn prod_id(&self) -> &str {
&self.prod_id
}
pub fn devfs_path(&self) -> PathBuf {
unsafe { &*(self.devfs_path.get()) }.clone()
}
pub fn syspath(&self) -> &Path {
&self.syspath
}
pub fn write_bytes(&self, message: &[u8]) -> Result<()> {
let mut path = unsafe { &*(self.path.get()) };
let mut path = unsafe { &*(self.devfs_path.get()) };
let mut file = match OpenOptions::new().write(true).open(path) {
Ok(f) => f,
Err(e) => {
warn!("write_bytes failed for {:?}, trying again: {e}", self.path);
warn!(
"write_bytes failed for {:?}, trying again: {e}",
self.devfs_path
);
unsafe {
*(self.path.get()) = (*(Self::new(&self.prod_id)?.path.get())).clone();
path = &mut *(self.path.get());
*(self.devfs_path.get()) =
(*(Self::new(&self.prod_id)?.0.devfs_path.get())).clone();
path = &mut *(self.devfs_path.get());
}
OpenOptions::new()
.write(true)
@@ -91,7 +135,7 @@ impl HidRaw {
}
pub fn set_wakeup_disabled(&self) -> Result<()> {
let path = unsafe { &*(self.path.get()) };
let path = unsafe { &*(self.devfs_path.get()) };
let mut dev = Device::from_syspath(path)?;
Ok(dev.set_attribute_value("power/wakeup", "disabled")?)
}