From a15ed7b34504fa52071b70abd88b881e02b5c536 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Wed, 12 Jul 2023 02:11:19 +0200 Subject: [PATCH] Listen on 'added' signals for calls and sms MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/main.rs | 28 +++++++++++++++++++-- src/modem/mod.rs | 65 ++++++++++++++++++++++++------------------------ 2 files changed, 58 insertions(+), 35 deletions(-) diff --git a/src/main.rs b/src/main.rs index 57eafc1..590bd01 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(()) } diff --git a/src/modem/mod.rs b/src/modem/mod.rs index 18fef60..1b5c9d2 100644 --- a/src/modem/mod.rs +++ b/src/modem/mod.rs @@ -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::>(); - join_all(modems) - .await - .into_iter() - .try_fold(vec![], |mut acc, res| { - let modem = res?; - acc.push(modem); - Ok::>, 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, Error> { - Ok(Modem(ModemProxy::builder(&conn).path(path)?.build().await?)) + pub async fn new(connection: &Connection, path: ObjectPath<'a>) -> Result, 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() } }