From f750737f253cc4017facddf2d15268fcc1ac82cb Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Wed, 13 May 2020 21:22:01 +0200 Subject: [PATCH] Keep D-Bus utility functions in a separate file --- src/Makefile.am | 1 + src/ba-transport.c | 1 + src/bluealsa-dbus.c | 11 ++-- src/bluez.c | 30 ++++------ src/dbus.c | 136 ++++++++++++++++++++++++++++++++++++++++++++ src/dbus.h | 35 ++++++++++++ src/ofono.c | 5 +- src/sco.c | 2 - src/upower.c | 3 +- src/utils.c | 113 ------------------------------------ src/utils.h | 10 ---- test/server-mock.c | 1 + test/test-ba.c | 1 + test/test-io.c | 1 + test/test-rfcomm.c | 1 + utils/aplay/dbus.c | 1 + 16 files changed, 201 insertions(+), 151 deletions(-) create mode 100644 src/dbus.c create mode 100644 src/dbus.h diff --git a/src/Makefile.am b/src/Makefile.am index 43efc697c..e6ad5482e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -24,6 +24,7 @@ bluealsa_SOURCES = \ bluez.c \ bluez-a2dp.c \ bluez-iface.c \ + dbus.c \ hci.c \ sbc.c \ sco.c \ diff --git a/src/ba-transport.c b/src/ba-transport.c index 769d799a7..9eb9e4f7d 100644 --- a/src/ba-transport.c +++ b/src/ba-transport.c @@ -34,6 +34,7 @@ #include "bluealsa.h" #include "bluealsa-dbus.h" #include "bluez-iface.h" +#include "dbus.h" #include "hci.h" #include "hfp.h" #include "sco.h" diff --git a/src/bluealsa-dbus.c b/src/bluealsa-dbus.c index fe76a4271..f23df9905 100644 --- a/src/bluealsa-dbus.c +++ b/src/bluealsa-dbus.c @@ -30,6 +30,7 @@ #include "ba-device.h" #include "bluealsa-iface.h" #include "bluealsa.h" +#include "dbus.h" #include "hfp.h" #include "utils.h" #include "shared/defs.h" @@ -120,8 +121,7 @@ static void ba_variant_populate_pcm(GVariantBuilder *props, const struct ba_tran g_variant_builder_add(props, "{sv}", "Volume", ba_variant_new_pcm_volume(pcm)); } -static void bluealsa_manager_get_pcms(GDBusMethodInvocation *inv, void *userdata) { - (void)userdata; +static void bluealsa_manager_get_pcms(GDBusMethodInvocation *inv) { GVariantBuilder pcms; g_variant_builder_init(&pcms, G_VARIANT_TYPE("a{oa{sv}}")); @@ -188,9 +188,10 @@ static void bluealsa_manager_method_call(GDBusConnection *conn, const char *send (void)sender; (void)path; (void)params; + (void)userdata; if (strcmp(method, "GetPCMs") == 0) - bluealsa_manager_get_pcms(invocation, userdata); + bluealsa_manager_get_pcms(invocation); } @@ -578,7 +579,7 @@ void bluealsa_dbus_pcm_update(struct ba_transport_pcm *pcm, unsigned int mask) { g_variant_builder_add(&props, "{sv}", "Volume", ba_variant_new_pcm_volume(pcm)); g_dbus_connection_emit_signal(config.dbus, NULL, pcm->ba_dbus_path, - "org.freedesktop.DBus.Properties", "PropertiesChanged", + DBUS_IFACE_PROPERTIES, "PropertiesChanged", g_variant_new("(sa{sv}as)", BLUEALSA_IFACE_PCM, &props, NULL), NULL); g_variant_builder_clear(&props); @@ -627,7 +628,7 @@ void bluealsa_dbus_rfcomm_update(struct ba_rfcomm *r, unsigned int mask) { g_variant_builder_add(&props, "{sv}", "Battery", ba_variant_new_device_battery(r->sco->d)); g_dbus_connection_emit_signal(config.dbus, NULL, r->ba_dbus_path, - "org.freedesktop.DBus.Properties", "PropertiesChanged", + DBUS_IFACE_PROPERTIES, "PropertiesChanged", g_variant_new("(sa{sv}as)", BLUEALSA_IFACE_RFCOMM, &props, NULL), NULL); g_variant_builder_clear(&props); diff --git a/src/bluez.c b/src/bluez.c index b32089943..5bc3af02b 100644 --- a/src/bluez.c +++ b/src/bluez.c @@ -34,6 +34,7 @@ #include "bluealsa-dbus.h" #include "bluez-a2dp.h" #include "bluez-iface.h" +#include "dbus.h" #include "hci.h" #include "sbc.h" #include "sco.h" @@ -478,6 +479,8 @@ static void bluez_endpoint_select_configuration(GDBusMethodInvocation *inv, void g_free(capabilities); } +static void bluez_register_a2dp_all(struct ba_adapter *adapter); + static void bluez_endpoint_set_configuration(GDBusMethodInvocation *inv, void *userdata) { const char *sender = g_dbus_method_invocation_get_sender(inv); @@ -722,6 +725,7 @@ static void bluez_endpoint_set_configuration(GDBusMethodInvocation *inv, void *u dbus_obj->connected = true; g_dbus_method_invocation_return_value(inv, NULL); + bluez_register_a2dp_all(a); goto final; fail: @@ -788,8 +792,6 @@ static void bluez_endpoint_release(GDBusMethodInvocation *inv, void *userdata) { g_object_unref(inv); } -static void bluez_register_a2dp_all(struct ba_adapter *adapter); - static void bluez_endpoint_method_call(GDBusConnection *conn, const char *sender, const char *path, const char *interface, const char *method, GVariant *params, GDBusMethodInvocation *invocation, void *userdata) { @@ -800,18 +802,10 @@ static void bluez_endpoint_method_call(GDBusConnection *conn, const char *sender (void)interface; (void)params; - struct dbus_object_data *dbus_obj = userdata; - struct ba_adapter *a; - if (strcmp(method, "SelectConfiguration") == 0) bluez_endpoint_select_configuration(invocation, userdata); - else if (strcmp(method, "SetConfiguration") == 0) { + else if (strcmp(method, "SetConfiguration") == 0) bluez_endpoint_set_configuration(invocation, userdata); - if ((a = ba_adapter_lookup(dbus_obj->hci_dev_id)) != NULL) { - bluez_register_a2dp_all(a); - ba_adapter_unref(a); - } - } else if (strcmp(method, "ClearConfiguration") == 0) bluez_endpoint_clear_configuration(invocation, userdata); else if (strcmp(method, "Release") == 0) @@ -828,7 +822,7 @@ static struct dbus_object_data *bluez_create_media_endpoint_object( const char *path, GError **error) { - static GDBusInterfaceVTable vtable = { + static const GDBusInterfaceVTable vtable = { .method_call = bluez_endpoint_method_call, }; @@ -1141,7 +1135,7 @@ static struct dbus_object_data *bluez_create_profile_object( const char *path, GError **error) { - static GDBusInterfaceVTable vtable = { + static const GDBusInterfaceVTable vtable = { .method_call = bluez_profile_method_call, }; @@ -1606,18 +1600,18 @@ static void bluez_signal_name_owner_changed(GDBusConnection *conn, const char *s int bluez_subscribe_signals(void) { g_dbus_connection_signal_subscribe(config.dbus, BLUEZ_SERVICE, - "org.freedesktop.DBus.ObjectManager", "InterfacesAdded", NULL, NULL, + DBUS_IFACE_OBJECT_MANAGER, "InterfacesAdded", NULL, NULL, G_DBUS_SIGNAL_FLAGS_NONE, bluez_signal_interfaces_added, NULL, NULL); g_dbus_connection_signal_subscribe(config.dbus, BLUEZ_SERVICE, - "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved", NULL, NULL, + DBUS_IFACE_OBJECT_MANAGER, "InterfacesRemoved", NULL, NULL, G_DBUS_SIGNAL_FLAGS_NONE, bluez_signal_interfaces_removed, NULL, NULL); g_dbus_connection_signal_subscribe(config.dbus, BLUEZ_SERVICE, - "org.freedesktop.DBus.Properties", "PropertiesChanged", NULL, BLUEZ_IFACE_MEDIA_TRANSPORT, + DBUS_IFACE_PROPERTIES, "PropertiesChanged", NULL, BLUEZ_IFACE_MEDIA_TRANSPORT, G_DBUS_SIGNAL_FLAGS_NONE, bluez_signal_transport_changed, NULL, NULL); - g_dbus_connection_signal_subscribe(config.dbus, "org.freedesktop.DBus", - "org.freedesktop.DBus", "NameOwnerChanged", NULL, BLUEZ_SERVICE, + g_dbus_connection_signal_subscribe(config.dbus, DBUS_SERVICE, + DBUS_IFACE_DBUS, "NameOwnerChanged", NULL, BLUEZ_SERVICE, G_DBUS_SIGNAL_FLAGS_NONE, bluez_signal_name_owner_changed, NULL, NULL); return 0; diff --git a/src/dbus.c b/src/dbus.c new file mode 100644 index 000000000..8a9b6292c --- /dev/null +++ b/src/dbus.c @@ -0,0 +1,136 @@ +/* + * BlueALSA - dbus.c + * Copyright (c) 2016-2020 Arkadiusz Bokowy + * + * This file is a part of bluez-alsa. + * + * This project is licensed under the terms of the MIT license. + * + */ + +#include "dbus.h" + +#include +#include + +/** + * Get managed objects of a given D-Bus service. + * + * @param conn D-Bus connection handler. + * @param name The name of known D-Bus service. + * @param path The path which shall be inspected. + * @param error NULL GError pointer. + * @return On success this function returns variant iterator with the list of + * managed D-Bus objects. After usage, the returned iterator shall be freed + * with g_variant_iter_free(). On error, NULL is returned. */ +GVariantIter *g_dbus_get_managed_objects(GDBusConnection *conn, + const char *name, const char *path, GError **error) { + + GDBusMessage *msg = NULL, *rep = NULL; + GVariantIter *objects = NULL; + + msg = g_dbus_message_new_method_call(name, path, + DBUS_IFACE_OBJECT_MANAGER, "GetManagedObjects"); + + if ((rep = g_dbus_connection_send_message_with_reply_sync(conn, msg, + G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, error)) == NULL) + goto fail; + + if (g_dbus_message_get_message_type(rep) == G_DBUS_MESSAGE_TYPE_ERROR) { + g_dbus_message_to_gerror(rep, error); + goto fail; + } + + g_variant_get(g_dbus_message_get_body(rep), "(a{oa{sa{sv}}})", &objects); + +fail: + + if (msg != NULL) + g_object_unref(msg); + if (rep != NULL) + g_object_unref(rep); + + return objects; +} + +/** + * Get a property of a given D-Bus interface. + * + * @param conn D-Bus connection handler. + * @param service Valid D-Bus service name. + * @param path Valid D-Bus object path. + * @param interface Interface with the given property. + * @param property The property name. + * @param error NULL GError pointer. + * @return On success this function returns variant containing property value. + * After usage, returned variant shall be freed with g_variant_unref(). On + * error, NULL is returned. */ +GVariant *g_dbus_get_property(GDBusConnection *conn, const char *service, + const char *path, const char *interface, const char *property, + GError **error) { + + GDBusMessage *msg = NULL, *rep = NULL; + GVariant *value = NULL; + + msg = g_dbus_message_new_method_call(service, path, DBUS_IFACE_PROPERTIES, "Get"); + g_dbus_message_set_body(msg, g_variant_new("(ss)", interface, property)); + + if ((rep = g_dbus_connection_send_message_with_reply_sync(conn, msg, + G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, error)) == NULL) + goto fail; + + if (g_dbus_message_get_message_type(rep) == G_DBUS_MESSAGE_TYPE_ERROR) { + g_dbus_message_to_gerror(rep, error); + goto fail; + } + + g_variant_get(g_dbus_message_get_body(rep), "(v)", &value); + +fail: + + if (msg != NULL) + g_object_unref(msg); + if (rep != NULL) + g_object_unref(rep); + + return value; +} + +/** + * Set a property of a given D-Bus interface. + * + * @param conn D-Bus connection handler. + * @param service Valid D-Bus service name. + * @param path Valid D-Bus object path. + * @param interface Interface with the given property. + * @param property The property name. + * @param value Variant containing property value. + * @param error NULL GError pointer. + * @return On success this function returns true. */ +bool g_dbus_set_property(GDBusConnection *conn, const char *service, + const char *path, const char *interface, const char *property, + const GVariant *value, GError **error) { + + GDBusMessage *msg = NULL, *rep = NULL; + + msg = g_dbus_message_new_method_call(service, path, DBUS_IFACE_PROPERTIES, "Set"); + g_dbus_message_set_body(msg, g_variant_new("(ssv)", interface, property, value)); + + if ((rep = g_dbus_connection_send_message_with_reply_sync(conn, msg, + G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, error)) == NULL) + goto fail; + + if (g_dbus_message_get_message_type(rep) == G_DBUS_MESSAGE_TYPE_ERROR) { + g_dbus_message_to_gerror(rep, error); + goto fail; + } + +fail: + + if (msg != NULL) + g_object_unref(msg); + if (rep != NULL) + g_object_unref(rep); + + return error == NULL; +} diff --git a/src/dbus.h b/src/dbus.h new file mode 100644 index 000000000..f6b4e28b8 --- /dev/null +++ b/src/dbus.h @@ -0,0 +1,35 @@ +/* + * BlueALSA - dbus.h + * Copyright (c) 2016-2020 Arkadiusz Bokowy + * + * This file is a part of bluez-alsa. + * + * This project is licensed under the terms of the MIT license. + * + */ + +#ifndef BLUEALSA_DBUS_H_ +#define BLUEALSA_DBUS_H_ + +#include + +#include +#include + +#define DBUS_SERVICE "org.freedesktop.DBus" + +#define DBUS_IFACE_DBUS DBUS_SERVICE +#define DBUS_IFACE_OBJECT_MANAGER DBUS_SERVICE ".ObjectManager" +#define DBUS_IFACE_PROPERTIES DBUS_SERVICE ".Properties" + +GVariantIter *g_dbus_get_managed_objects(GDBusConnection *conn, + const char *name, const char *path, GError **error); + +GVariant *g_dbus_get_property(GDBusConnection *conn, const char *service, + const char *path, const char *interface, const char *property, + GError **error); +bool g_dbus_set_property(GDBusConnection *conn, const char *service, + const char *path, const char *interface, const char *property, + const GVariant *value, GError **error); + +#endif diff --git a/src/ofono.c b/src/ofono.c index fda5ff8dc..173deb066 100644 --- a/src/ofono.c +++ b/src/ofono.c @@ -41,6 +41,7 @@ #include "ba-transport.h" #include "bluealsa.h" #include "bluealsa-dbus.h" +#include "dbus.h" #include "hci.h" #include "hfp.h" #include "ofono-iface.h" @@ -607,8 +608,8 @@ int ofono_subscribe_signals(void) { OFONO_IFACE_HF_AUDIO_MANAGER, "CardRemoved", NULL, NULL, G_DBUS_SIGNAL_FLAGS_NONE, ofono_signal_card_removed, NULL, NULL); - g_dbus_connection_signal_subscribe(config.dbus, "org.freedesktop.DBus", - "org.freedesktop.DBus", "NameOwnerChanged", NULL, OFONO_SERVICE, + g_dbus_connection_signal_subscribe(config.dbus, DBUS_SERVICE, + DBUS_IFACE_DBUS, "NameOwnerChanged", NULL, OFONO_SERVICE, G_DBUS_SIGNAL_FLAGS_NONE, ofono_signal_name_owner_changed, NULL, NULL); return 0; diff --git a/src/sco.c b/src/sco.c index 5516d6951..71d2d888f 100644 --- a/src/sco.c +++ b/src/sco.c @@ -24,8 +24,6 @@ #include #include -#include - #include "a2dp.h" #include "ba-device.h" #include "bluealsa.h" diff --git a/src/upower.c b/src/upower.c index bf9170f84..67bde2634 100644 --- a/src/upower.c +++ b/src/upower.c @@ -25,6 +25,7 @@ #include "ba-rfcomm.h" #include "ba-transport.h" #include "bluealsa.h" +#include "dbus.h" #include "utils.h" #include "shared/log.h" @@ -123,7 +124,7 @@ static void upower_signal_display_device_changed(GDBusConnection *conn, const ch int upower_subscribe_signals(void) { g_dbus_connection_signal_subscribe(config.dbus, UPOWER_SERVICE, - "org.freedesktop.DBus.Properties", "PropertiesChanged", UPOWER_PATH_DISPLAY_DEVICE, + DBUS_IFACE_PROPERTIES, "PropertiesChanged", UPOWER_PATH_DISPLAY_DEVICE, NULL, G_DBUS_SIGNAL_FLAGS_NONE, upower_signal_display_device_changed, NULL, NULL); return 0; diff --git a/src/utils.c b/src/utils.c index f1d9f7619..d28a1930a 100644 --- a/src/utils.c +++ b/src/utils.c @@ -150,119 +150,6 @@ const char *g_dbus_transport_type_to_bluez_object_path(struct ba_transport_type return "/"; } -/** - * Get managed objects of a given D-Bus service. */ -GVariantIter *g_dbus_get_managed_objects(GDBusConnection *conn, - const char *name, const char *path, GError **error) { - - GDBusMessage *msg = NULL, *rep = NULL; - GVariantIter *objects = NULL; - - msg = g_dbus_message_new_method_call(name, path, - "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); - - if ((rep = g_dbus_connection_send_message_with_reply_sync(conn, msg, - G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, error)) == NULL) - goto fail; - - if (g_dbus_message_get_message_type(rep) == G_DBUS_MESSAGE_TYPE_ERROR) { - g_dbus_message_to_gerror(rep, error); - goto fail; - } - - g_variant_get(g_dbus_message_get_body(rep), "(a{oa{sa{sv}}})", &objects); - -fail: - - if (msg != NULL) - g_object_unref(msg); - if (rep != NULL) - g_object_unref(rep); - - return objects; -} - -/** - * Get a property of a given D-Bus interface. - * - * @param conn D-Bus connection handler. - * @param service Valid D-Bus service name. - * @param path Valid D-Bus object path. - * @param interface Interface with the given property. - * @param property The property name. - * @param error NULL GError pointer. - * @return On success this function returns variant containing property value. - * Otherwise, NULL is returned. */ -GVariant *g_dbus_get_property(GDBusConnection *conn, const char *service, - const char *path, const char *interface, const char *property, - GError **error) { - - GDBusMessage *msg = NULL, *rep = NULL; - GVariant *value = NULL; - - msg = g_dbus_message_new_method_call(service, path, "org.freedesktop.DBus.Properties", "Get"); - g_dbus_message_set_body(msg, g_variant_new("(ss)", interface, property)); - - if ((rep = g_dbus_connection_send_message_with_reply_sync(conn, msg, - G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, error)) == NULL) - goto fail; - - if (g_dbus_message_get_message_type(rep) == G_DBUS_MESSAGE_TYPE_ERROR) { - g_dbus_message_to_gerror(rep, error); - goto fail; - } - - g_variant_get(g_dbus_message_get_body(rep), "(v)", &value); - -fail: - - if (msg != NULL) - g_object_unref(msg); - if (rep != NULL) - g_object_unref(rep); - - return value; -} - -/** - * Set a property of a given D-Bus interface. - * - * @param conn D-Bus connection handler. - * @param service Valid D-Bus service name. - * @param path Valid D-Bus object path. - * @param interface Interface with the given property. - * @param property The property name. - * @param value Variant containing property value. - * @param error NULL GError pointer. - * @return On success this function returns true. */ -bool g_dbus_set_property(GDBusConnection *conn, const char *service, - const char *path, const char *interface, const char *property, - const GVariant *value, GError **error) { - - GDBusMessage *msg = NULL, *rep = NULL; - - msg = g_dbus_message_new_method_call(service, path, "org.freedesktop.DBus.Properties", "Set"); - g_dbus_message_set_body(msg, g_variant_new("(ssv)", interface, property, value)); - - if ((rep = g_dbus_connection_send_message_with_reply_sync(conn, msg, - G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, error)) == NULL) - goto fail; - - if (g_dbus_message_get_message_type(rep) == G_DBUS_MESSAGE_TYPE_ERROR) { - g_dbus_message_to_gerror(rep, error); - goto fail; - } - -fail: - - if (msg != NULL) - g_object_unref(msg); - if (rep != NULL) - g_object_unref(rep); - - return error == NULL; -} - /** * Sanitize D-Bus object path. * diff --git a/src/utils.h b/src/utils.h index b455502d4..d82f9dff3 100644 --- a/src/utils.h +++ b/src/utils.h @@ -21,7 +21,6 @@ #include -#include #include #include "ba-transport.h" @@ -30,15 +29,6 @@ int g_dbus_bluez_object_path_to_hci_dev_id(const char *path); bdaddr_t *g_dbus_bluez_object_path_to_bdaddr(const char *path, bdaddr_t *addr); const char *g_dbus_transport_type_to_bluez_object_path(struct ba_transport_type type); -GVariantIter *g_dbus_get_managed_objects(GDBusConnection *conn, - const char *name, const char *path, GError **error); -GVariant *g_dbus_get_property(GDBusConnection *conn, const char *service, - const char *path, const char *interface, const char *property, - GError **error); -bool g_dbus_set_property(GDBusConnection *conn, const char *service, - const char *path, const char *interface, const char *property, - const GVariant *value, GError **error); - char *g_variant_sanitize_object_path(char *path); bool g_variant_validate_value(GVariant *value, const GVariantType *type, const char *name); diff --git a/test/server-mock.c b/test/server-mock.c index 279537de6..1e980b01d 100644 --- a/test/server-mock.c +++ b/test/server-mock.c @@ -35,6 +35,7 @@ #include "../src/bluealsa-iface.c" #include "../src/bluealsa.c" #include "../src/bluez-a2dp.c" +#include "../src/dbus.c" #include "../src/hci.c" #include "../src/msbc.c" #include "../src/sbc.c" diff --git a/test/test-ba.c b/test/test-ba.c index a7fd94590..94526df3e 100644 --- a/test/test-ba.c +++ b/test/test-ba.c @@ -19,6 +19,7 @@ #include "../src/ba-transport.c" #include "../src/bluealsa.c" #include "../src/bluez-a2dp.c" +#include "../src/dbus.c" #include "../src/hci.c" #include "../src/utils.c" #include "../src/shared/log.c" diff --git a/test/test-io.c b/test/test-io.c index 01144aad0..bf216e96c 100644 --- a/test/test-io.c +++ b/test/test-io.c @@ -25,6 +25,7 @@ #include "../src/ba-transport.c" #include "../src/bluealsa.c" #include "../src/bluez-a2dp.c" +#include "../src/dbus.c" #include "../src/hci.c" #include "../src/msbc.c" #include "../src/sbc.c" diff --git a/test/test-rfcomm.c b/test/test-rfcomm.c index 3b1ac055a..17581cdfa 100644 --- a/test/test-rfcomm.c +++ b/test/test-rfcomm.c @@ -22,6 +22,7 @@ #include "../src/ba-rfcomm.c" #include "../src/ba-transport.c" #include "../src/bluealsa.c" +#include "../src/dbus.c" #include "../src/at.c" #include "../src/hci.c" #include "../src/utils.c" diff --git a/utils/aplay/dbus.c b/utils/aplay/dbus.c index 5deea60b0..c06f1629b 100644 --- a/utils/aplay/dbus.c +++ b/utils/aplay/dbus.c @@ -11,6 +11,7 @@ #include "dbus.h" #include +#include static bool dbus_message_iter_get_basic_boolean(DBusMessageIter *iter) { dbus_bool_t tmp = FALSE;