Skip to content

Commit

Permalink
Keep D-Bus utility functions in a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed May 13, 2020
1 parent c8835a6 commit f750737
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 151 deletions.
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ bluealsa_SOURCES = \
bluez.c \
bluez-a2dp.c \
bluez-iface.c \
dbus.c \
hci.c \
sbc.c \
sco.c \
Expand Down
1 change: 1 addition & 0 deletions src/ba-transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 6 additions & 5 deletions src/bluealsa-dbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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}}"));
Expand Down Expand Up @@ -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);

}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
30 changes: 12 additions & 18 deletions src/bluez.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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,
};

Expand Down Expand Up @@ -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,
};

Expand Down Expand Up @@ -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;
Expand Down
136 changes: 136 additions & 0 deletions src/dbus.c
Original file line number Diff line number Diff line change
@@ -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 <stdbool.h>
#include <string.h>

/**
* 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;
}
35 changes: 35 additions & 0 deletions src/dbus.h
Original file line number Diff line number Diff line change
@@ -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 <stdbool.h>

#include <gio/gio.h>
#include <glib.h>

#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
5 changes: 3 additions & 2 deletions src/ofono.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions src/sco.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#include <bluetooth/hci_lib.h>
#include <bluetooth/sco.h>

#include <glib.h>

#include "a2dp.h"
#include "ba-device.h"
#include "bluealsa.h"
Expand Down
3 changes: 2 additions & 1 deletion src/upower.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit f750737

Please sign in to comment.