Change to dbus-tokio

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2022-07-14 00:13:45 +02:00
parent ff29c267a8
commit 8039503df9
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2
2 changed files with 35 additions and 25 deletions

View file

@ -8,3 +8,5 @@ categories = ["gateway", "bridge", "transport", "modemmanager"]
[dependencies] [dependencies]
mmdbus = "1.18.6" mmdbus = "1.18.6"
dbus-tokio = "0.7.5"
tokio = { version = "1.19.2", features = [ "time", "macros", "rt-multi-thread" ] }

View file

@ -17,12 +17,15 @@ use std::collections::HashMap;
use std::error::Error as StdError; use std::error::Error as StdError;
use std::fmt; use std::fmt;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use tokio::{
task::JoinHandle,
time::Duration,
};
use mmdbus::{ use mmdbus::{
dbus::{ dbus::{
arg::{PropMap, RefArg, Variant}, arg::{PropMap, RefArg, Variant},
blocking::{Connection, stdintf::org_freedesktop_dbus::ObjectManager, Proxy}, nonblock::{SyncConnection, stdintf::org_freedesktop_dbus::ObjectManager, Proxy},
// channel::MatchingReceiver, // channel::MatchingReceiver,
// message::{MatchRule, Message}, // message::{MatchRule, Message},
strings::{Path as DBusPath}, // Interface, Member, strings::{Path as DBusPath}, // Interface, Member,
@ -33,6 +36,8 @@ use mmdbus::{
sms::Sms as SmsAccess, sms::Sms as SmsAccess,
}; };
use dbus_tokio::connection;
// The following source has been used as an example: // The following source has been used as an example:
// https://github.com/soerenmeier/linux-info/blob/master/src/network/modem_manager.rs // https://github.com/soerenmeier/linux-info/blob/master/src/network/modem_manager.rs
@ -43,21 +48,27 @@ const SMSC: &str = "+33609001390"; // SFR
#[derive(Clone)] #[derive(Clone)]
pub struct DBus { pub struct DBus {
pub conn: Arc<Connection>, pub conn: Arc<SyncConnection>,
pub handle: Arc<JoinHandle<()>>,
} }
impl DBus { impl DBus {
pub fn connect() -> Result<Self, Box<dyn StdError>> { pub fn connect() -> Result<Self, Box<dyn StdError>> {
Ok(Connection::new_system() let (resource, conn) = connection::new_system_sync()?;
.map(Arc::new)
.map(|conn| Self { conn })?) let handle: JoinHandle<()> = tokio::spawn(async {
let err = resource.await;
panic!("Lost connection to D-Bus: {}", err);
});
Ok(Self { conn, handle: Arc::new(handle) })
} }
pub fn proxy<'a, 'b, P>(&'b self, path: P) -> Proxy<'a, &'b Connection> pub fn proxy<'a, 'b, P>(&'b self, path: P) -> Proxy<'a, &'b SyncConnection>
where where
P: Into<DBusPath<'a>>, P: Into<DBusPath<'a>>,
{ {
self.conn.with_proxy(MM_NAME, path, TIMEOUT) Proxy::new(MM_NAME, path, TIMEOUT, &*self.conn)
} }
} }
@ -71,20 +82,18 @@ impl ModemManager {
Ok(DBus::connect().map(|dbus| Self { dbus })?) Ok(DBus::connect().map(|dbus| Self { dbus })?)
} }
pub fn modems(&self) -> Result<Vec<Modem>, Box<dyn StdError>> { pub async fn modems(&self) -> Result<Vec<DBusPath<'static>>, Box<dyn StdError>> {
let objects = self.dbus.proxy(MM_PATH).get_managed_objects()?; let objects = self.dbus.proxy(MM_PATH).get_managed_objects().await?;
let modems = objects let modems = objects
.into_iter() .into_iter()
.map(|(path, _)| Modem { .map(|(path, _)| path )
dbus: self.dbus.clone(),
path,
})
.collect(); .collect();
Ok(modems) Ok(modems)
} }
} }
/*
#[derive(Clone)] #[derive(Clone)]
pub struct Modem { pub struct Modem {
dbus: DBus, dbus: DBus,
@ -124,6 +133,7 @@ impl Modem {
Ok(ModemAccess::own_numbers(&self.dbus.proxy(&self.path))?) Ok(ModemAccess::own_numbers(&self.dbus.proxy(&self.path))?)
} }
/*
pub fn list_sms(&self) -> Result<Vec<Sms>, Box<dyn StdError>> { pub fn list_sms(&self) -> Result<Vec<Sms>, Box<dyn StdError>> {
Ok(ModemMessaging::list(&self.dbus.proxy(&self.path))? Ok(ModemMessaging::list(&self.dbus.proxy(&self.path))?
.into_iter() .into_iter()
@ -142,6 +152,7 @@ impl Modem {
pub fn delete_sms(&self, path: DBusPath) -> Result<(), Box<dyn StdError>> { pub fn delete_sms(&self, path: DBusPath) -> Result<(), Box<dyn StdError>> {
Ok(ModemMessaging::delete(&self.dbus.proxy(&self.path), path)?) Ok(ModemMessaging::delete(&self.dbus.proxy(&self.path), path)?)
} }
*/
} }
#[repr(i32)] #[repr(i32)]
@ -192,7 +203,9 @@ impl From<i32> for ModemState {
} }
} }
} }
*/
/*
// include/ModemManager-enums.h MMSmsState // include/ModemManager-enums.h MMSmsState
/// State of a given SMS. /// State of a given SMS.
#[repr(u32)] #[repr(u32)]
@ -299,18 +312,13 @@ impl fmt::Display for Sms {
write!(f, "Sms ({}) {{ number: {}, text: {} }}", state, number, text) write!(f, "Sms ({}) {{ number: {}, text: {} }}", state, number, text)
} }
} }
*/
#[tokio::main]
async fn main() -> Result<(), Box<dyn StdError>> {
let modems = ModemManager::connect()?.modems().await;
println!("Modems: {:?}", modems);
fn main() -> Result<(), Box<dyn StdError>> {
let modems: Vec<Modem> = ModemManager::connect()?.modems()?;
for modem in modems {
println!("Enabled: {:?}", modem.enabled());
println!("Model: {:?}", modem.model());
// modem.create_sms(String::from("0123456789"), String::from("Test 1 2 3"))?.send()?;
let messages = modem.list_sms()?;
for sms in messages {
println!("{}", sms);
}
}
Ok(()) Ok(())
} }