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]
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::fmt;
use std::sync::Arc;
use std::time::Duration;
use tokio::{
task::JoinHandle,
time::Duration,
};
use mmdbus::{
dbus::{
arg::{PropMap, RefArg, Variant},
blocking::{Connection, stdintf::org_freedesktop_dbus::ObjectManager, Proxy},
nonblock::{SyncConnection, stdintf::org_freedesktop_dbus::ObjectManager, Proxy},
// channel::MatchingReceiver,
// message::{MatchRule, Message},
strings::{Path as DBusPath}, // Interface, Member,
@ -33,6 +36,8 @@ use mmdbus::{
sms::Sms as SmsAccess,
};
use dbus_tokio::connection;
// The following source has been used as an example:
// https://github.com/soerenmeier/linux-info/blob/master/src/network/modem_manager.rs
@ -43,21 +48,27 @@ const SMSC: &str = "+33609001390"; // SFR
#[derive(Clone)]
pub struct DBus {
pub conn: Arc<Connection>,
pub conn: Arc<SyncConnection>,
pub handle: Arc<JoinHandle<()>>,
}
impl DBus {
pub fn connect() -> Result<Self, Box<dyn StdError>> {
Ok(Connection::new_system()
.map(Arc::new)
.map(|conn| Self { conn })?)
let (resource, conn) = connection::new_system_sync()?;
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
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 })?)
}
pub fn modems(&self) -> Result<Vec<Modem>, Box<dyn StdError>> {
let objects = self.dbus.proxy(MM_PATH).get_managed_objects()?;
pub async fn modems(&self) -> Result<Vec<DBusPath<'static>>, Box<dyn StdError>> {
let objects = self.dbus.proxy(MM_PATH).get_managed_objects().await?;
let modems = objects
.into_iter()
.map(|(path, _)| Modem {
dbus: self.dbus.clone(),
path,
})
.map(|(path, _)| path )
.collect();
Ok(modems)
}
}
/*
#[derive(Clone)]
pub struct Modem {
dbus: DBus,
@ -124,6 +133,7 @@ impl Modem {
Ok(ModemAccess::own_numbers(&self.dbus.proxy(&self.path))?)
}
/*
pub fn list_sms(&self) -> Result<Vec<Sms>, Box<dyn StdError>> {
Ok(ModemMessaging::list(&self.dbus.proxy(&self.path))?
.into_iter()
@ -142,6 +152,7 @@ impl Modem {
pub fn delete_sms(&self, path: DBusPath) -> Result<(), Box<dyn StdError>> {
Ok(ModemMessaging::delete(&self.dbus.proxy(&self.path), path)?)
}
*/
}
#[repr(i32)]
@ -192,7 +203,9 @@ impl From<i32> for ModemState {
}
}
}
*/
/*
// include/ModemManager-enums.h MMSmsState
/// State of a given SMS.
#[repr(u32)]
@ -299,18 +312,13 @@ impl fmt::Display for Sms {
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(())
}