Listen on 'added' signals for calls and sms

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2023-07-12 02:11:19 +02:00
parent 54db069e94
commit a15ed7b345
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2
2 changed files with 58 additions and 35 deletions

View file

@ -19,15 +19,39 @@ mod modem;
use crate::error::Error; use crate::error::Error;
use crate::modem::ModemManager; use crate::modem::ModemManager;
use futures::{StreamExt, future::join_all};
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Error> { async fn main() -> Result<(), Error> {
pretty_env_logger::init(); pretty_env_logger::init();
let mut join_handlers = vec![];
let manager = ModemManager::connect().await?; let manager = ModemManager::connect().await?;
for modem in manager { for modem in manager {
println!("FOO0: {:?}", modem); println!("FOO0: {:?}", modem);
println!("FOO0: {:?}", modem.manufacturer().await?); println!("FOO0: {:?}", modem.foo().manufacturer().await?);
println!("FOO0: {:?}", modem.model().await?); println!("FOO0: {:?}", modem.foo().model().await?);
println!("FOO1: {:?}", modem.messaging().list().await?);
let mut sms_added_stream = modem.messaging().receive_added().await?;
let mut call_added_stream = modem.voice().receive_call_added().await?;
let handle_sms = tokio::spawn(async move {
while let Some(sms) = sms_added_stream.next().await {
println!("BAR0: {:?}", sms);
} }
});
let handle_call = tokio::spawn(async move {
while let Some(call) = call_added_stream.next().await {
println!("MEH0: {:?}", call);
}
});
join_handlers.push(handle_sms);
join_handlers.push(handle_call);
}
let _ = join_all(join_handlers).await;
Ok(()) Ok(())
} }

View file

@ -16,12 +16,10 @@
mod introspect; mod introspect;
use crate::error::Error; use crate::error::Error;
use crate::modem::introspect::ModemProxy; use crate::modem::introspect::{ModemProxy, MessagingProxy, VoiceProxy};
use std::fmt; use std::fmt;
use std::ops::{Deref, DerefMut};
use futures::future::join_all;
use zbus::{fdo::ObjectManagerProxy, zvariant::ObjectPath, Connection}; use zbus::{fdo::ObjectManagerProxy, zvariant::ObjectPath, Connection};
const MM_NAME: &str = "org.freedesktop.ModemManager1"; const MM_NAME: &str = "org.freedesktop.ModemManager1";
@ -48,20 +46,13 @@ impl<'a> ModemManager<'a> {
.path(MM_PATH)? .path(MM_PATH)?
.build() .build()
.await?; .await?;
let modems = proxy
.get_managed_objects() let mut res = vec![];
.await? for (path, _) in proxy.get_managed_objects().await? {
.into_iter() res.push(Modem::new(&conn, path.into()).await?)
.map(|(path, _)| Modem::new(&conn, path.into())) }
.collect::<Vec<_>>();
join_all(modems) Ok(res)
.await
.into_iter()
.try_fold(vec![], |mut acc, res| {
let modem = res?;
acc.push(modem);
Ok::<Vec<Modem<'_>>, Error>(acc)
})
} }
} }
@ -83,34 +74,42 @@ impl<'a> IntoIterator for ModemManager<'a> {
} }
#[derive(Clone)] #[derive(Clone)]
pub struct Modem<'a>(ModemProxy<'a>); pub struct Modem<'a> {
modem: ModemProxy<'a>,
messaging: MessagingProxy<'a>,
voice: VoiceProxy<'a>,
path: ObjectPath<'a>,
}
impl<'a> Modem<'a> { impl<'a> Modem<'a> {
pub async fn new(conn: &Connection, path: ObjectPath<'a>) -> Result<Modem<'a>, Error> { pub async fn new(connection: &Connection, path: ObjectPath<'a>) -> Result<Modem<'a>, Error> {
Ok(Modem(ModemProxy::builder(&conn).path(path)?.build().await?)) Ok(Modem {
} modem: ModemProxy::builder(connection).path(path.clone())?.build().await?,
messaging: MessagingProxy::builder(connection).path(path.clone())?.build().await?,
voice: VoiceProxy::builder(connection).path(path.clone())?.build().await?,
path,
})
} }
impl<'a> Deref for Modem<'a> { pub fn foo(&self) -> &ModemProxy<'a> {
type Target = ModemProxy<'a>; &self.modem
fn deref(&self) -> &Self::Target {
&self.0
}
} }
impl<'a> DerefMut for Modem<'a> { pub fn messaging(&self) -> &MessagingProxy<'a> {
fn deref_mut(&mut self) -> &mut Self::Target { &self.messaging
&mut self.0 }
pub fn voice(&self) -> &VoiceProxy<'a> {
&self.voice
} }
} }
impl fmt::Debug for Modem<'_> { impl fmt::Debug for Modem<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Modem") f.debug_struct("Modem")
.field("destination", &self.inner().destination()) .field("destination", &self.foo().destination())
.field("path", &self.inner().path()) .field("path", &self.foo().inner().path())
.field("interface", &self.inner().interface()) .field("interface", &self.foo().inner().interface())
.finish_non_exhaustive() .finish_non_exhaustive()
} }
} }