diff --git a/spa/plugins/bluez5/bluez5-dbus.c b/spa/plugins/bluez5/bluez5-dbus.c index 94680bb61..a79c1b18b 100644 --- a/spa/plugins/bluez5/bluez5-dbus.c +++ b/spa/plugins/bluez5/bluez5-dbus.c @@ -35,6 +35,7 @@ #include "config.h" #include "codec-loader.h" +#include "dbus-helpers.h" #include "player.h" #include "iso-io.h" #include "defs.h" @@ -225,12 +226,7 @@ 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; - } + cancel_and_unref(&device->battery_pending_call); if (!device->adapter || !device->adapter->has_battery_provider || !device->has_battery) return; @@ -422,9 +418,7 @@ static void register_battery_provider(struct spa_bt_device *device) device->battery_pending_call, on_battery_provider_registered, device, NULL)) { 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; + cancel_and_unref(&device->battery_pending_call); } } @@ -2634,17 +2628,8 @@ void spa_bt_transport_free(struct spa_bt_transport *transport) spa_bt_transport_destroy(transport); - if (transport->acquire_call) { - dbus_pending_call_cancel(transport->acquire_call); - dbus_pending_call_unref(transport->acquire_call); - transport->acquire_call = NULL; - } - - if (transport->volume_call) { - dbus_pending_call_cancel(transport->volume_call); - dbus_pending_call_unref(transport->volume_call); - transport->volume_call = NULL; - } + cancel_and_unref(&transport->acquire_call); + cancel_and_unref(&transport->volume_call); if (transport->fd >= 0) { spa_bt_player_set_state(transport->device->adapter->dummy_player, SPA_BT_PLAYER_STOPPED); @@ -3271,11 +3256,7 @@ static void transport_set_property_volume(struct spa_bt_transport *transport, ui int res = 0; dbus_bool_t ret; - if (transport->volume_call) { - dbus_pending_call_cancel(transport->volume_call); - dbus_pending_call_unref(transport->volume_call); - transport->volume_call = NULL; - } + cancel_and_unref(&transport->volume_call); m = dbus_message_new_method_call(BLUEZ_SERVICE, transport->path, @@ -3614,11 +3595,7 @@ static int do_transport_release(struct spa_bt_transport *transport) spa_bt_transport_set_state(transport, SPA_BT_TRANSPORT_STATE_IDLE); - if (transport->acquire_call) { - dbus_pending_call_cancel(transport->acquire_call); - dbus_pending_call_unref(transport->acquire_call); - transport->acquire_call = NULL; - } + cancel_and_unref(&transport->acquire_call); if (transport->iso_io) { spa_log_debug(monitor->log, "transport %p: remove ISO IO", transport); @@ -3743,10 +3720,7 @@ static void media_codec_switch_free(struct spa_bt_media_codec_switch *sw) media_codec_switch_stop_timer(sw); - if (sw->pending != NULL) { - dbus_pending_call_cancel(sw->pending); - dbus_pending_call_unref(sw->pending); - } + cancel_and_unref(&sw->pending); if (sw->device != NULL) spa_list_remove(&sw->device_link); @@ -5626,10 +5600,7 @@ static int impl_clear(struct spa_handle *handle) monitor->filters_added = false; } - if (monitor->get_managed_objects_call) { - dbus_pending_call_cancel(monitor->get_managed_objects_call); - dbus_pending_call_unref(monitor->get_managed_objects_call); - } + cancel_and_unref(&monitor->get_managed_objects_call); spa_list_consume(t, &monitor->transport_list, link) spa_bt_transport_free(t); diff --git a/spa/plugins/bluez5/dbus-helpers.h b/spa/plugins/bluez5/dbus-helpers.h new file mode 100644 index 000000000..43b9afd2a --- /dev/null +++ b/spa/plugins/bluez5/dbus-helpers.h @@ -0,0 +1,22 @@ +/* Spa Bluez5 DBus helpers */ +/* SPDX-FileCopyrightText: Copyright © 2023 PipeWire authors */ +/* SPDX-License-Identifier: MIT */ + +#ifndef SPA_BLUEZ5_DBUS_HELPERS_H +#define SPA_BLUEZ5_DBUS_HELPERS_H + +#include + +#include + +static inline void cancel_and_unref(DBusPendingCall **pp) +{ + DBusPendingCall *pending_call = spa_steal_ptr(*pp); + + if (pending_call) { + dbus_pending_call_cancel(pending_call); + dbus_pending_call_unref(pending_call); + } +} + +#endif /* SPA_BLUEZ5_DBUS_HELPERS_H */ diff --git a/spa/plugins/bluez5/modemmanager.c b/spa/plugins/bluez5/modemmanager.c index bc740acc6..99d9eb67c 100644 --- a/spa/plugins/bluez5/modemmanager.c +++ b/spa/plugins/bluez5/modemmanager.c @@ -7,6 +7,7 @@ #include +#include "dbus-helpers.h" #include "modemmanager.h" #define DBUS_INTERFACE_OBJECTMANAGER "org.freedesktop.DBus.ObjectManager" @@ -56,8 +57,7 @@ static bool mm_dbus_connection_send_with_reply(struct impl *this, DBusMessage *m ret = dbus_pending_call_set_notify(pending_call, function, user_data, NULL); if (!ret) { spa_log_debug(this->log, "dbus set notify failure"); - dbus_pending_call_cancel(pending_call); - dbus_pending_call_unref(pending_call); + cancel_and_unref(&pending_call); goto out; } @@ -455,13 +455,11 @@ finish: dbus_message_unref(r); } -static void call_free(struct call *call) { +static void call_free(struct call *call) +{ spa_list_remove(&call->link); - if (call->pending != NULL) { - dbus_pending_call_cancel(call->pending); - dbus_pending_call_unref(call->pending); - } + cancel_and_unref(&call->pending); if (call->number) free(call->number); @@ -477,10 +475,7 @@ static void mm_clean_voice(struct impl *this) spa_list_consume(call, &this->call_list, link) call_free(call); - if (this->voice_pending != NULL) { - dbus_pending_call_cancel(this->voice_pending); - dbus_pending_call_unref(this->voice_pending); - } + cancel_and_unref(&this->voice_pending); if (this->ops->set_call_setup) this->ops->set_call_setup(CIND_CALLSETUP_NONE, this->user_data); @@ -1158,10 +1153,7 @@ void mm_unregister(void *data) { struct impl *this = data; - if (this->pending != NULL) { - dbus_pending_call_cancel(this->pending); - dbus_pending_call_unref(this->pending); - } + cancel_and_unref(&this->pending); mm_clean_voice(this); mm_clean_modem3gpp(this); diff --git a/spa/plugins/bluez5/upower.c b/spa/plugins/bluez5/upower.c index 0a4672194..aca111dd5 100644 --- a/spa/plugins/bluez5/upower.c +++ b/spa/plugins/bluez5/upower.c @@ -5,6 +5,7 @@ #include #include +#include "dbus-helpers.h" #include "upower.h" #define UPOWER_SERVICE "org.freedesktop.UPower"