2024-06-17 13:26:55 +02:00
|
|
|
use color_eyre::Result;
|
2025-08-16 15:31:06 +02:00
|
|
|
use color_eyre::eyre::Ok;
|
2025-08-10 16:37:55 +02:00
|
|
|
use futures_lite::StreamExt;
|
2025-09-03 18:11:56 +02:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2025-08-14 18:14:31 +02:00
|
|
|
use std::sync::Arc;
|
2025-09-03 11:29:40 +02:00
|
|
|
use tokio::sync::{RwLock, broadcast};
|
2025-09-03 18:11:56 +02:00
|
|
|
use tokio::task::JoinHandle;
|
2025-09-03 11:29:40 +02:00
|
|
|
use tracing::debug;
|
2025-08-10 16:37:55 +02:00
|
|
|
use zbus::Connection;
|
2025-09-02 23:23:44 +02:00
|
|
|
use zbus::zvariant::ObjectPath;
|
2024-06-17 13:26:55 +02:00
|
|
|
|
2025-08-17 21:15:10 +02:00
|
|
|
use crate::clients::ClientResult;
|
2025-08-16 15:31:06 +02:00
|
|
|
use crate::clients::networkmanager::dbus::{DbusProxy, DeviceDbusProxy};
|
2025-09-02 20:42:26 +02:00
|
|
|
use crate::clients::networkmanager::event::{ClientToModuleEvent, ModuleToClientEvent};
|
2025-08-14 18:14:31 +02:00
|
|
|
use crate::{register_fallible_client, spawn};
|
2024-06-17 13:26:55 +02:00
|
|
|
|
2025-08-16 15:31:06 +02:00
|
|
|
pub mod dbus;
|
2025-08-14 18:14:31 +02:00
|
|
|
pub mod event;
|
2024-06-17 13:26:55 +02:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
2025-08-14 18:14:31 +02:00
|
|
|
pub struct Client {
|
2025-09-03 12:41:20 +02:00
|
|
|
inner: &'static ClientInner,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Client {
|
|
|
|
fn new() -> Client {
|
|
|
|
let inner = Box::leak(Box::new(ClientInner::new()));
|
|
|
|
Client { inner }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(&self) -> Result<()> {
|
|
|
|
self.inner.run()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn subscribe(&self) -> broadcast::Receiver<ClientToModuleEvent> {
|
|
|
|
self.inner.subscribe()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_sender(&self) -> broadcast::Sender<ModuleToClientEvent> {
|
|
|
|
self.inner.get_sender()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct ClientInner {
|
2025-09-02 20:42:26 +02:00
|
|
|
controller_sender: broadcast::Sender<ClientToModuleEvent>,
|
|
|
|
sender: broadcast::Sender<ModuleToClientEvent>,
|
2025-09-03 12:41:20 +02:00
|
|
|
devices: RwLock<HashSet<ObjectPath<'static>>>,
|
2025-09-03 18:11:56 +02:00
|
|
|
watchers: RwLock<HashMap<ObjectPath<'static>, Device>>,
|
|
|
|
// TODO: Maybe find some way to late-init a dbus connection here
|
|
|
|
// so we can just clone it when we need it instead of awaiting it every time
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Device {
|
|
|
|
state_watcher: JoinHandle<Result<()>>,
|
2024-06-17 13:26:55 +02:00
|
|
|
}
|
|
|
|
|
2025-09-03 12:41:20 +02:00
|
|
|
impl ClientInner {
|
|
|
|
fn new() -> ClientInner {
|
2025-09-02 20:42:26 +02:00
|
|
|
let (controller_sender, _) = broadcast::channel(64);
|
|
|
|
let (sender, _) = broadcast::channel(8);
|
2025-09-03 18:11:56 +02:00
|
|
|
let devices = RwLock::new(HashSet::new());
|
|
|
|
let watchers = RwLock::new(HashMap::new());
|
2025-09-03 12:41:20 +02:00
|
|
|
ClientInner {
|
2025-09-02 20:42:26 +02:00
|
|
|
controller_sender,
|
|
|
|
sender,
|
2025-09-03 12:41:20 +02:00
|
|
|
devices,
|
2025-09-03 18:11:56 +02:00
|
|
|
watchers,
|
2025-09-03 12:41:20 +02:00
|
|
|
}
|
2024-06-17 13:26:55 +02:00
|
|
|
}
|
|
|
|
|
2025-09-03 12:41:20 +02:00
|
|
|
fn run(&'static self) -> Result<()> {
|
2025-09-03 11:29:40 +02:00
|
|
|
debug!("Client running");
|
|
|
|
|
2025-09-03 12:41:20 +02:00
|
|
|
spawn(self.watch_devices_list());
|
2025-09-02 20:42:26 +02:00
|
|
|
|
2025-09-02 22:20:54 +02:00
|
|
|
let receiver = self.sender.subscribe();
|
2025-09-03 12:41:20 +02:00
|
|
|
spawn(self.handle_received_events(receiver));
|
2024-06-17 13:26:55 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2025-09-03 18:11:56 +02:00
|
|
|
fn subscribe(&self) -> broadcast::Receiver<ClientToModuleEvent> {
|
2025-09-02 20:42:26 +02:00
|
|
|
self.controller_sender.subscribe()
|
|
|
|
}
|
|
|
|
|
2025-09-03 18:11:56 +02:00
|
|
|
fn get_sender(&self) -> broadcast::Sender<ModuleToClientEvent> {
|
2025-09-02 20:42:26 +02:00
|
|
|
self.sender.clone()
|
2024-06-17 13:26:55 +02:00
|
|
|
}
|
|
|
|
|
2025-09-03 12:41:20 +02:00
|
|
|
async fn watch_devices_list(&'static self) -> Result<()> {
|
|
|
|
debug!("D-Bus devices list watcher starting");
|
|
|
|
|
|
|
|
let dbus_connection = Connection::system().await?;
|
|
|
|
let root = DbusProxy::new(&dbus_connection).await?;
|
|
|
|
|
|
|
|
let mut devices_changes = root.receive_all_devices_changed().await;
|
|
|
|
while let Some(devices_change) = devices_changes.next().await {
|
|
|
|
// The new list of devices from dbus, not to be confused with the added devices below
|
|
|
|
let new_devices = devices_change
|
|
|
|
.get()
|
|
|
|
.await?
|
|
|
|
.iter()
|
|
|
|
.map(ObjectPath::to_owned)
|
|
|
|
.collect::<HashSet<_>>();
|
|
|
|
|
2025-09-03 18:11:56 +02:00
|
|
|
// TODO: Use `self.watchers` instead of `self.devices`, which requires creating all property watchers straightaway
|
|
|
|
|
2025-09-03 12:41:20 +02:00
|
|
|
// Atomic read-then-write of `devices`
|
|
|
|
let mut devices_locked = self.devices.write().await;
|
|
|
|
let devices_snapshot = devices_locked.clone();
|
|
|
|
(*devices_locked).clone_from(&new_devices);
|
|
|
|
drop(devices_locked);
|
|
|
|
|
|
|
|
let added_devices = new_devices.difference(&devices_snapshot);
|
|
|
|
for added_device in added_devices {
|
|
|
|
spawn(self.watch_device(added_device.to_owned()));
|
|
|
|
}
|
2024-06-17 13:26:55 +02:00
|
|
|
|
2025-09-03 12:41:20 +02:00
|
|
|
// TODO: Inform module of removed devices
|
2025-09-03 18:11:56 +02:00
|
|
|
let removed_devices = devices_snapshot.difference(&new_devices);
|
|
|
|
for removed_device in removed_devices {
|
|
|
|
let mut watchers = self.watchers.write().await;
|
|
|
|
let device = watchers.get(removed_device).unwrap();
|
|
|
|
device.state_watcher.abort();
|
|
|
|
watchers.remove(removed_device);
|
|
|
|
|
|
|
|
debug!("D-bus device state watcher for {} stopped", removed_device);
|
|
|
|
}
|
2025-09-02 22:20:54 +02:00
|
|
|
}
|
|
|
|
|
2025-09-03 12:41:20 +02:00
|
|
|
Ok(())
|
2025-09-02 22:20:54 +02:00
|
|
|
}
|
|
|
|
|
2025-09-03 12:41:20 +02:00
|
|
|
async fn handle_received_events(
|
|
|
|
&'static self,
|
|
|
|
mut receiver: broadcast::Receiver<ModuleToClientEvent>,
|
|
|
|
) -> Result<()> {
|
2025-09-03 18:11:56 +02:00
|
|
|
let dbus_connection = Connection::system().await?;
|
|
|
|
|
2025-09-03 12:41:20 +02:00
|
|
|
while let Result::Ok(event) = receiver.recv().await {
|
|
|
|
match event {
|
|
|
|
ModuleToClientEvent::NewController => {
|
|
|
|
debug!("Client received NewController event");
|
|
|
|
|
|
|
|
// We create a local clone here to avoid holding the lock for too long
|
|
|
|
let devices_snapshot = self.devices.read().await.clone();
|
|
|
|
|
|
|
|
for device_path in devices_snapshot {
|
|
|
|
let device = DeviceDbusProxy::new(&dbus_connection, device_path).await?;
|
|
|
|
|
|
|
|
let interface = device.interface().await?.to_string();
|
|
|
|
let r#type = device.device_type().await?;
|
|
|
|
let new_state = device.state().await?;
|
|
|
|
self.controller_sender
|
|
|
|
.send(ClientToModuleEvent::DeviceChanged {
|
|
|
|
interface,
|
|
|
|
r#type,
|
|
|
|
new_state,
|
|
|
|
})?;
|
|
|
|
}
|
2025-09-02 22:20:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-09-03 12:41:20 +02:00
|
|
|
|
|
|
|
Ok(())
|
2025-09-02 22:20:54 +02:00
|
|
|
}
|
|
|
|
|
2025-09-03 12:41:20 +02:00
|
|
|
async fn watch_device(&'static self, path: ObjectPath<'static>) -> Result<()> {
|
2025-09-03 18:11:56 +02:00
|
|
|
debug_assert!(!self.watchers.read().await.contains_key(&path));
|
2025-09-02 22:20:54 +02:00
|
|
|
|
2025-09-03 18:11:56 +02:00
|
|
|
let state_watcher = spawn(self.watch_device_state(path.clone()));
|
|
|
|
self.watchers
|
|
|
|
.write()
|
|
|
|
.await
|
|
|
|
.insert(path, Device { state_watcher });
|
2025-08-16 15:31:06 +02:00
|
|
|
|
2025-09-03 12:41:20 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2025-08-16 15:31:06 +02:00
|
|
|
|
2025-09-03 18:11:56 +02:00
|
|
|
async fn watch_device_state(&'static self, path: ObjectPath<'_>) -> Result<()> {
|
|
|
|
let dbus_connection = Connection::system().await?;
|
|
|
|
let device = DeviceDbusProxy::new(&dbus_connection, path.clone()).await?;
|
2025-09-03 12:41:20 +02:00
|
|
|
|
|
|
|
debug!("D-Bus device state watcher for {} starting", path);
|
|
|
|
|
|
|
|
let interface = device.interface().await?;
|
|
|
|
let r#type = device.device_type().await?;
|
|
|
|
|
|
|
|
// Send an event communicating the initial state
|
|
|
|
let new_state = device.state().await?;
|
|
|
|
self.controller_sender
|
|
|
|
.send(ClientToModuleEvent::DeviceChanged {
|
|
|
|
interface: interface.to_string(),
|
|
|
|
r#type: r#type.clone(),
|
|
|
|
new_state,
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let mut state_changes = device.receive_state_changed().await;
|
|
|
|
while let Some(state_change) = state_changes.next().await {
|
|
|
|
let new_state = state_change.get().await?;
|
|
|
|
self.controller_sender
|
|
|
|
.send(ClientToModuleEvent::DeviceChanged {
|
|
|
|
interface: interface.to_string(),
|
|
|
|
r#type: r#type.clone(),
|
|
|
|
new_state,
|
|
|
|
})?;
|
|
|
|
}
|
2025-08-16 15:31:06 +02:00
|
|
|
|
2025-09-03 12:41:20 +02:00
|
|
|
debug!("D-Bus device state watcher for {} ended", path);
|
2025-08-16 15:31:06 +02:00
|
|
|
|
2025-09-03 12:41:20 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2025-09-03 11:29:40 +02:00
|
|
|
|
2025-09-03 12:41:20 +02:00
|
|
|
pub fn create_client() -> ClientResult<Client> {
|
|
|
|
let client = Arc::new(Client::new());
|
|
|
|
client.run()?;
|
|
|
|
Ok(client)
|
2025-08-16 15:31:06 +02:00
|
|
|
}
|
|
|
|
|
2024-08-04 19:04:15 +02:00
|
|
|
register_fallible_client!(Client, network_manager);
|