From 026e3882d256b6935a5953b0cb4fc8bdacc0707e Mon Sep 17 00:00:00 2001 From: Pauli Virtanen Date: Mon, 5 Apr 2021 14:58:50 +0300 Subject: [PATCH] bluez5: remove battery when RFCOMM connection drops There won't be further battery level updates when RFCOMM connection is down, so remove the battery then. Fix minor things with canceling the provider registration. --- spa/plugins/bluez5/backend-native.c | 2 ++ spa/plugins/bluez5/bluez5-dbus.c | 25 ++++++++++++++++++------- spa/plugins/bluez5/defs.h | 2 ++ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/spa/plugins/bluez5/backend-native.c b/spa/plugins/bluez5/backend-native.c index cb3782a2e..b60b7213e 100644 --- a/spa/plugins/bluez5/backend-native.c +++ b/spa/plugins/bluez5/backend-native.c @@ -174,6 +174,8 @@ static void rfcomm_free(struct rfcomm *rfcomm) spa_hook_remove(&rfcomm->transport_listener); spa_bt_transport_free(rfcomm->transport); } + if (rfcomm->device) + spa_bt_device_report_battery_level(rfcomm->device, SPA_BT_NO_BATTERY); free(rfcomm); } diff --git a/spa/plugins/bluez5/bluez5-dbus.c b/spa/plugins/bluez5/bluez5-dbus.c index 43b14bc4d..c9bc6c980 100644 --- a/spa/plugins/bluez5/bluez5-dbus.c +++ b/spa/plugins/bluez5/bluez5-dbus.c @@ -171,6 +171,13 @@ static void battery_remove(struct spa_bt_device *device) { DBusMessage *m; const char *interface; + if (device->battery_pending_call) { + spa_log_debug(device->monitor->log, "Cancelling and freeing pending battery provider register call"); + dbus_pending_call_cancel(device->battery_pending_call); + dbus_pending_call_unref(device->battery_pending_call); + device->battery_pending_call = NULL; + } + if (!device->adapter->has_battery_provider || !device->has_battery) return; @@ -323,6 +330,11 @@ static void register_battery_provider(struct spa_bt_device *device) DBusMessage *method_call; DBusMessageIter message_iter; + if (device->battery_pending_call) { + spa_log_debug(device->monitor->log, NAME": Already registering battery provider"); + return; + } + method_call = dbus_message_new_method_call( BLUEZ_SERVICE, device->adapter_path, BLUEZ_INTERFACE_BATTERY_PROVIDER_MANAGER, @@ -358,6 +370,7 @@ static void register_battery_provider(struct spa_bt_device *device) spa_log_error(device->monitor->log, "Failed to register battery provider"); dbus_pending_call_cancel(device->battery_pending_call); dbus_pending_call_unref(device->battery_pending_call); + device->battery_pending_call = NULL; } } @@ -662,13 +675,6 @@ static void device_free(struct spa_bt_device *device) spa_log_debug(monitor->log, "%p", device); - if (device->battery_pending_call) { - spa_log_debug(monitor->log, "Cancelling and freeing pending battery provider register call"); - dbus_pending_call_cancel(device->battery_pending_call); - dbus_pending_call_unref(device->battery_pending_call); - device->battery_pending_call = NULL; - } - battery_remove(device); device_stop_timer(device); @@ -3654,6 +3660,11 @@ const struct spa_handle_factory spa_bluez5_dbus_factory = { // Report battery percentage to BlueZ using experimental (BlueZ 5.56) Battery Provider API. No-op if no changes occured. int spa_bt_device_report_battery_level(struct spa_bt_device *device, uint8_t percentage) { + if (percentage == SPA_BT_NO_BATTERY) { + battery_remove(device); + return 0; + } + // BlueZ likely is running without battery provider support, don't try to report battery if (device->adapter->battery_provider_unavailable) return 0; diff --git a/spa/plugins/bluez5/defs.h b/spa/plugins/bluez5/defs.h index 615c2d0e0..2b45e1828 100644 --- a/spa/plugins/bluez5/defs.h +++ b/spa/plugins/bluez5/defs.h @@ -161,6 +161,8 @@ extern "C" { #define SPA_BT_UNKNOWN_DELAY 0 +#define SPA_BT_NO_BATTERY ((uint8_t)255) + /* HFP uses SBC encoding with precisely defined parameters. Hence, the size * of the input (number of PCM samples) and output is known up front. */ #define MSBC_DECODED_SIZE 240