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::modem::ModemManager;
use futures::{StreamExt, future::join_all};
#[tokio::main]
async fn main() -> Result<(), Error> {
pretty_env_logger::init();
let mut join_handlers = vec![];
let manager = ModemManager::connect().await?;
for modem in manager {
println!("FOO0: {:?}", modem);
println!("FOO0: {:?}", modem.manufacturer().await?);
println!("FOO0: {:?}", modem.model().await?);
println!("FOO0: {:?}", modem.foo().manufacturer().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(())
}

View File

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