@@ -1,16 +1,19 @@
|
||||
mod trigger;
|
||||
mod wait_for_idle;
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
enum Cli {
|
||||
Trigger(trigger::Cli),
|
||||
WaitForIdle(wait_for_idle::Cli),
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cli = Cli::parse();
|
||||
let result = match cli {
|
||||
Cli::Trigger(cli) => trigger::main(cli),
|
||||
Cli::WaitForIdle(cli) => wait_for_idle::main(cli),
|
||||
};
|
||||
if let Err(err) = result {
|
||||
eprintln!("device: error: {err}");
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
use clap::Parser;
|
||||
use ipc::client::Client;
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
pub struct Cli {}
|
||||
|
||||
pub fn main(_: Cli) -> anyhow::Result<()> {
|
||||
let mut client = Client::connect()?;
|
||||
client.wait_for_idle()?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -201,6 +201,26 @@ pub async fn list_devices(when: When) -> Vec<String> {
|
||||
ret
|
||||
}
|
||||
|
||||
pub async fn wait_for_idle() {
|
||||
loop {
|
||||
let mut waited = false;
|
||||
let keys: Vec<String> = DEVICES.read().unwrap().keys().map(Into::into).collect();
|
||||
for key in keys {
|
||||
let Some(device) = DEVICES.read().unwrap().get(&key).cloned() else {
|
||||
continue;
|
||||
};
|
||||
if device.lock.try_lock().is_ok() {
|
||||
continue;
|
||||
}
|
||||
waited = true;
|
||||
_ = device.lock.lock().await;
|
||||
}
|
||||
if !waited {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn dev_root() -> PathBuf {
|
||||
std::env::var("LXDEVICED_DEV_ROOT")
|
||||
.as_deref()
|
||||
|
||||
@@ -34,6 +34,7 @@ async fn handle_connection(mut connection: Connection) -> std::io::Result<()> {
|
||||
Request::Trigger(set, action) => handler(trigger(set, action)),
|
||||
Request::ReloadRules => handler(reload_rules()),
|
||||
Request::ListDevices(when) => handler(list_devices(when)),
|
||||
Request::WaitForIdle => handler(wait_for_idle()),
|
||||
};
|
||||
let reply = handler.await;
|
||||
connection.send(reply).await?;
|
||||
@@ -57,6 +58,11 @@ async fn list_devices(when: When) -> Result<Vec<String>, Error> {
|
||||
Ok(crate::device::list_devices(when).await)
|
||||
}
|
||||
|
||||
async fn wait_for_idle() -> Result<(), Error> {
|
||||
crate::device::wait_for_idle().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handler<R: Serialize>(
|
||||
fut: impl Future<Output = Result<R, Error>> + Send + 'static,
|
||||
) -> Pin<Box<dyn Future<Output = Result<serde_json::Value, Error>> + Send>> {
|
||||
|
||||
@@ -60,4 +60,8 @@ impl Client {
|
||||
pub fn list_devices(&mut self, when: When) -> Result<Vec<String>, Error> {
|
||||
self.invoke(&Request::ListDevices(when))
|
||||
}
|
||||
|
||||
pub fn wait_for_idle(&mut self) -> Result<(), Error> {
|
||||
self.invoke(&Request::WaitForIdle)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,9 @@ pub enum Request {
|
||||
|
||||
#[serde(rename = "/org.semilabs.os/LxDeviced/ListDevices@LXDEVICED_0.1.0")]
|
||||
ListDevices(When),
|
||||
|
||||
#[serde(rename = "/org.semilabs.os/LxDeviced/WaitForIdle@LXDEVICED_0.1.0")]
|
||||
WaitForIdle,
|
||||
}
|
||||
|
||||
pub fn ser_reply<T: Serialize>(result: Result<T, Error>) -> String {
|
||||
|
||||
Reference in New Issue
Block a user