chore(bluetooth): handle DBus service unknown error

This commit is contained in:
Michael Aaron Murphy 2024-12-12 15:00:50 +01:00
parent 4ad35b70a6
commit a1436eb637
No known key found for this signature in database
GPG key ID: B2732D4240C9212C
2 changed files with 33 additions and 7 deletions

View file

@ -47,6 +47,8 @@ pub struct Page {
selected_adapter: Option<OwnedObjectPath>,
heading: String,
devices: HashMap<OwnedObjectPath, Device>,
// Set to true when the org.bluez dbus service is unknown.
bluez_service_unknown: bool,
popup_setting: bool,
popup_device: Option<OwnedObjectPath>,
subscription: Option<tokio::sync::oneshot::Sender<()>>,
@ -157,6 +159,7 @@ pub enum Message {
tokio::sync::mpsc::Sender<crate::pages::Message>,
),
DBusError(String),
DBusServiceUnknown,
DeviceFailed(OwnedObjectPath),
DisconnectDevice(OwnedObjectPath),
ForgetDevice(OwnedObjectPath),
@ -491,6 +494,9 @@ impl Page {
Message::DBusError(why) => {
tracing::error!("dbus connection failed. {why}");
}
Message::DBusServiceUnknown => {
self.bluez_service_unknown = true;
}
};
cosmic::Task::none()
}
@ -575,6 +581,14 @@ fn status() -> Section<crate::pages::Message> {
.show_while::<Page>(|page| !page.adapters.is_empty())
.view::<Page>(move |_binder, page, section| {
let descriptions = &section.descriptions;
if page.bluez_service_unknown {
return widget::text::body(
"The org.bluez DBus service could not be activated. Is bluez installed?",
)
.apply(Element::from);
}
let status = page
.get_selected_adapter()
.map_or(page.active, |(_, adapter)| adapter.enabled);

View file

@ -7,7 +7,7 @@ use std::pin::Pin;
use bluez_zbus::BluetoothDevice;
use cosmic::iced::futures::{SinkExt, StreamExt};
use futures::{channel::mpsc, stream::FusedStream};
use zbus::zvariant::OwnedObjectPath;
use zbus::{fdo, zvariant::OwnedObjectPath};
enum DevicePropertyWatcherTask {
Add(OwnedObjectPath),
@ -201,14 +201,26 @@ pub async fn watch(
}.await;
if let Err(why) = result {
tracing::error!("failed to watch bluetooth event: {why}");
if let Err(why) = tx
_ = tx
.send(bluetooth::Message::DBusError(why.to_string()))
.await
{
tracing::error!("failed to communicate error to app: {why}");
.await;
tracing::error!("failed to watch bluetooth event: {why}.");
// Exit if the dbus service is not found.
if let zbus::Error::FDO(fdo_error) = why {
match *fdo_error {
fdo::Error::ServiceUnknown(_) => {
tracing::error!(
"The org.bluez dbus service is unknown. Is the bluez service installed and activatable?"
);
_ = tx.send(bluetooth::Message::DBusServiceUnknown).await;
return;
}
_ => (),
}
}
tracing::error!("failed to watch bluetooth event: {why}. Restarting...");
}
}
}