use async_trait::async_trait;
use zbus::Connection;
use crate::dbus::{NMBluetoothProxy, NMDeviceProxy, NMProxy};
use crate::monitoring::transport::ActiveTransport;
use crate::try_log;
use crate::types::constants::{device_state, device_type};
pub(crate) struct Bluetooth;
#[async_trait]
impl ActiveTransport for Bluetooth {
type Output = String;
async fn current(conn: &Connection) -> Option<Self::Output> {
current_bluetooth_bdaddr(conn).await
}
}
pub(crate) async fn current_bluetooth_bdaddr(conn: &Connection) -> Option<String> {
let nm = try_log!(NMProxy::new(conn).await, "Failed to create NM proxy");
let devices = try_log!(nm.get_devices().await, "Failed to get devices");
for dp in devices {
let dev_builder = try_log!(
NMDeviceProxy::builder(conn).path(dp.clone()),
"Failed to create device proxy builder"
);
let dev = try_log!(dev_builder.build().await, "Failed to build device proxy");
let dev_type = try_log!(dev.device_type().await, "Failed to get device type");
if dev_type != device_type::BLUETOOTH {
continue;
}
let state = try_log!(dev.state().await, "Failed to get device state");
if state != device_state::ACTIVATED {
continue;
}
let bt_builder = try_log!(
NMBluetoothProxy::builder(conn).path(dp.clone()),
"Failed to create Bluetooth proxy builder"
);
let bt = try_log!(bt_builder.build().await, "Failed to build Bluetooth proxy");
if let Ok(bdaddr) = bt.hw_address().await {
return Some(bdaddr);
}
}
None
}
#[allow(dead_code)]
pub(crate) async fn current_bluetooth_info(conn: &Connection) -> Option<(String, u32)> {
let nm = try_log!(NMProxy::new(conn).await, "Failed to create NM proxy");
let devices = try_log!(nm.get_devices().await, "Failed to get devices");
for dp in devices {
let dev_builder = try_log!(
NMDeviceProxy::builder(conn).path(dp.clone()),
"Failed to create device proxy builder"
);
let dev = try_log!(dev_builder.build().await, "Failed to build device proxy");
let dev_type = try_log!(dev.device_type().await, "Failed to get device type");
if dev_type != device_type::BLUETOOTH {
continue;
}
let state = try_log!(dev.state().await, "Failed to get device state");
if state != device_state::ACTIVATED {
continue;
}
let bt_builder = try_log!(
NMBluetoothProxy::builder(conn).path(dp.clone()),
"Failed to create Bluetooth proxy builder"
);
let bt = try_log!(bt_builder.build().await, "Failed to build Bluetooth proxy");
if let (Ok(bdaddr), Ok(capabilities)) = (bt.hw_address().await, bt.bt_capabilities().await)
{
return Some((bdaddr, capabilities));
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bluetooth_struct_exists() {
let _bt = Bluetooth;
}
}