Skip to content

Commit

Permalink
chore(bluetooth): handle DBus service unknown error
Browse files Browse the repository at this point in the history
  • Loading branch information
mmstick committed Dec 12, 2024
1 parent 4ad35b7 commit a1436eb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
14 changes: 14 additions & 0 deletions cosmic-settings/src/pages/bluetooth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()>>,
Expand Down Expand Up @@ -157,6 +159,7 @@ pub enum Message {
tokio::sync::mpsc::Sender<crate::pages::Message>,
),
DBusError(String),
DBusServiceUnknown,
DeviceFailed(OwnedObjectPath),
DisconnectDevice(OwnedObjectPath),
ForgetDevice(OwnedObjectPath),
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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);
Expand Down
26 changes: 19 additions & 7 deletions cosmic-settings/src/pages/bluetooth/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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...");
}
}
}

0 comments on commit a1436eb

Please sign in to comment.