bluez5: add GDBus-based object monitor framework

This commit is contained in:
Pauli Virtanen 2022-11-14 01:48:27 +02:00 committed by Wim Taymans
parent 8d438d26ab
commit de595a78ff
6 changed files with 393 additions and 6 deletions

View file

@ -42,8 +42,11 @@ if get_option('spa-plugins').allowed()
alsa_dep = dependency('alsa', required: get_option('alsa'))
summary({'ALSA': alsa_dep.found()}, bool_yn: true, section: 'Backend')
bluez_dep = dependency('bluez', version : '>= 4.101', required: get_option('bluez5'))
summary({'Bluetooth audio': bluez_dep.found()}, bool_yn: true, section: 'Backend')
if bluez_dep.found()
gio_dep = dependency('gio-2.0', required : get_option('bluez5'))
gio_unix_dep = dependency('gio-unix-2.0', required : get_option('bluez5'))
bluez_deps_found = bluez_dep.found() and gio_dep.found() and gio_unix_dep.found()
summary({'Bluetooth audio': bluez_deps_found}, bool_yn: true, section: 'Backend')
if bluez_deps_found
sbc_dep = dependency('sbc', required: get_option('bluez5'))
summary({'SBC': sbc_dep.found()}, bool_yn: true, section: 'Bluetooth audio codecs')
ldac_dep = dependency('ldacBT-enc', required : get_option('bluez5-codec-ldac'))

View file

@ -0,0 +1,224 @@
/* Spa midi dbus
*
* Copyright © 2022 Pauli Virtanen
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <gio/gio.h>
#include <spa/utils/defs.h>
#include <spa/utils/string.h>
#include <spa/support/log.h>
#include "dbus-monitor.h"
static void on_clear(struct dbus_monitor *monitor, GDBusProxy *proxy)
{
const struct dbus_monitor_proxy_type *p;
for (p = monitor->proxy_types; p && p->proxy_type != G_TYPE_INVALID ; ++p) {
if (G_TYPE_CHECK_INSTANCE_TYPE(proxy, p->proxy_type)) {
if (p->on_clear)
p->on_clear(monitor, G_DBUS_INTERFACE(proxy));
}
}
}
static void on_g_properties_changed(GDBusProxy *proxy,
GVariant *changed_properties, char **invalidated_properties,
gpointer user_data)
{
struct dbus_monitor *monitor = user_data;
GDBusInterfaceInfo *info = g_dbus_interface_get_info(G_DBUS_INTERFACE(proxy));
const char *name = info ? info->name : NULL;
const struct dbus_monitor_proxy_type *p;
spa_log_trace(monitor->log, "%p: dbus object updated path=%s, name=%s",
monitor, g_dbus_proxy_get_object_path(proxy), name ? name : "<null>");
for (p = monitor->proxy_types; p && p->proxy_type != G_TYPE_INVALID ; ++p) {
if (G_TYPE_CHECK_INSTANCE_TYPE(proxy, p->proxy_type)) {
if (p->on_object_update)
p->on_object_update(monitor, G_DBUS_INTERFACE(proxy));
}
}
}
static void on_interface_added(GDBusObjectManager *self, GDBusObject *object,
GDBusInterface *iface, gpointer user_data)
{
struct dbus_monitor *monitor = user_data;
GDBusInterfaceInfo *info = g_dbus_interface_get_info(iface);
const char *name = info ? info->name : NULL;
spa_log_trace(monitor->log, "%p: dbus interface added path=%s, name=%s",
monitor, g_dbus_object_get_object_path(object), name ? name : "<null>");
if (!g_object_get_data(G_OBJECT(iface), "dbus-monitor-signals-connected")) {
g_object_set_data(G_OBJECT(iface), "dbus-monitor-signals-connected", GUINT_TO_POINTER(1));
g_signal_connect(iface, "g-properties-changed",
G_CALLBACK(on_g_properties_changed),
monitor);
}
on_g_properties_changed(G_DBUS_PROXY(iface),
NULL, NULL, monitor);
}
static void on_object_added(GDBusObjectManager *self, GDBusObject *object,
gpointer user_data)
{
struct dbus_monitor *monitor = user_data;
GList *interfaces = g_dbus_object_get_interfaces(object);
/*
* on_interface_added won't necessarily be called on objects on
* name owner changes, so we have to call it here for all interfaces.
*/
for (GList *lli = g_list_first(interfaces); lli; lli = lli->next) {
on_interface_added(dbus_monitor_manager(monitor),
object, G_DBUS_INTERFACE(lli->data), monitor);
}
g_list_free_full(interfaces, g_object_unref);
}
static void on_notify(GObject *gobject, GParamSpec *pspec, gpointer user_data)
{
struct dbus_monitor *monitor = user_data;
if (spa_streq(pspec->name, "name-owner") && monitor->on_name_owner_change)
monitor->on_name_owner_change(monitor);
}
static GType get_proxy_type(GDBusObjectManagerClient *manager, const gchar *object_path,
const gchar *interface_name, gpointer user_data)
{
struct dbus_monitor *monitor = user_data;
const struct dbus_monitor_proxy_type *p;
for (p = monitor->proxy_types; p && p->proxy_type != G_TYPE_INVALID; ++p) {
if (spa_streq(p->interface_name, interface_name))
return p->proxy_type;
}
return G_TYPE_DBUS_PROXY;
}
static void init_done(GObject *source_object, GAsyncResult *res, gpointer user_data)
{
struct dbus_monitor *monitor = user_data;
GError *error = NULL;
GList *objects;
GObject *ret;
g_clear_object(&monitor->call);
ret = g_async_initable_new_finish(G_ASYNC_INITABLE(source_object), res, &error);
if (!ret) {
spa_log_error(monitor->log, "%p: creating DBus object monitor failed: %s",
monitor, error->message);
g_error_free(error);
return;
}
monitor->manager = G_DBUS_OBJECT_MANAGER_CLIENT(ret);
spa_log_debug(monitor->log, "%p: DBus monitor started", monitor);
g_signal_connect(monitor->manager, "interface-added",
G_CALLBACK(on_interface_added), monitor);
g_signal_connect(monitor->manager, "object-added",
G_CALLBACK(on_object_added), monitor);
g_signal_connect(monitor->manager, "notify",
G_CALLBACK(on_notify), monitor);
/* List all objects now */
objects = g_dbus_object_manager_get_objects(dbus_monitor_manager(monitor));
for (GList *llo = g_list_first(objects); llo; llo = llo->next) {
GList *interfaces = g_dbus_object_get_interfaces(G_DBUS_OBJECT(llo->data));
for (GList *lli = g_list_first(interfaces); lli; lli = lli->next) {
on_interface_added(dbus_monitor_manager(monitor),
G_DBUS_OBJECT(llo->data), G_DBUS_INTERFACE(lli->data),
monitor);
}
g_list_free_full(interfaces, g_object_unref);
}
g_list_free_full(objects, g_object_unref);
}
void dbus_monitor_init(struct dbus_monitor *monitor,
GType client_type,
struct spa_log *log, GDBusConnection *conn,
const char *name, const char *object_path,
const struct dbus_monitor_proxy_type *proxy_types,
void (*on_name_owner_change)(struct dbus_monitor *monitor))
{
GDBusObjectManagerClientFlags flags = G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_DO_NOT_AUTO_START;
size_t i;
spa_zero(*monitor);
monitor->log = log;
monitor->call = g_cancellable_new();
monitor->on_name_owner_change = on_name_owner_change;
spa_zero(monitor->proxy_types);
for (i = 0; proxy_types && proxy_types[i].proxy_type != G_TYPE_INVALID; ++i) {
spa_assert(i < DBUS_MONITOR_MAX_TYPES);
monitor->proxy_types[i] = proxy_types[i];
}
g_async_initable_new_async(client_type, G_PRIORITY_DEFAULT,
monitor->call, init_done, monitor,
"flags", flags, "name", name, "connection", conn,
"object-path", object_path,
"get-proxy-type-func", get_proxy_type,
"get-proxy-type-user-data", monitor,
NULL);
}
void dbus_monitor_clear(struct dbus_monitor *monitor)
{
g_cancellable_cancel(monitor->call);
g_clear_object(&monitor->call);
if (monitor->manager) {
/* Indicate all objects should stop now.
*
* We need a separate hook, because the proxy finalizers
* may be called later asynchronously via e.g. DBus callbacks.
*/
GList *objects = g_dbus_object_manager_get_objects(dbus_monitor_manager(monitor));
for (GList *llo = g_list_first(objects); llo; llo = llo->next) {
GList *interfaces = g_dbus_object_get_interfaces(G_DBUS_OBJECT(llo->data));
for (GList *lli = g_list_first(interfaces); lli; lli = lli->next)
on_clear(monitor, G_DBUS_PROXY(lli->data));
g_list_free_full(interfaces, g_object_unref);
}
g_list_free_full(objects, g_object_unref);
}
g_clear_object(&monitor->manager);
spa_zero(*monitor);
}

View file

@ -0,0 +1,76 @@
/* Spa midi dbus
*
* Copyright © 2022 Pauli Virtanen
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef DBUS_MONITOR_H_
#define DBUS_MONITOR_H_
#include <gio/gio.h>
#include <spa/utils/defs.h>
#include <spa/utils/string.h>
#include <spa/support/log.h>
#define DBUS_MONITOR_MAX_TYPES 16
struct dbus_monitor;
struct dbus_monitor_proxy_type
{
const char *interface_name;
GType proxy_type;
void (*on_object_update)(struct dbus_monitor *monitor, GDBusInterface *iface);
void (*on_clear)(struct dbus_monitor *monitor, GDBusInterface *iface);
};
struct dbus_monitor
{
GDBusObjectManagerClient *manager;
struct spa_log *log;
GCancellable *call;
struct dbus_monitor_proxy_type proxy_types[DBUS_MONITOR_MAX_TYPES+1];
void (*on_name_owner_change)(struct dbus_monitor *monitor);
void *user_data;
};
static inline GDBusObjectManager *dbus_monitor_manager(struct dbus_monitor *monitor)
{
return G_DBUS_OBJECT_MANAGER(monitor->manager);
}
/**
* Create a DBus object monitor, with a given interface to proxy type map.
*
* \param proxy_types Mapping between interface names and watched proxy
* types, terminated by G_TYPE_INVALID.
* \param on_object_update Called for all objects and interfaces on
* startup, and when object properties are modified.
*/
void dbus_monitor_init(struct dbus_monitor *monitor,
GType client_type, struct spa_log *log, GDBusConnection *conn,
const char *name, const char *object_path,
const struct dbus_monitor_proxy_type *proxy_types,
void (*on_name_owner_change)(struct dbus_monitor *monitor));
void dbus_monitor_clear(struct dbus_monitor *monitor);
#endif DBUS_MONITOR_H_

View file

@ -1,4 +1,6 @@
bluez5_deps = [ mathlib, dbus_dep, sbc_dep, bluez_dep ]
gnome = import('gnome')
bluez5_deps = [ mathlib, dbus_dep, glib2_dep, sbc_dep, bluez_dep, gio_dep, gio_unix_dep ]
foreach dep: bluez5_deps
if not dep.found()
subdir_done()
@ -29,7 +31,6 @@ bluez5_sources = [
'bluez5-device.c',
'bluez5-dbus.c',
'hci.c',
'dbus-manager.c',
'dbus-monitor.c',
'midi-enum.c',
'midi-parser.c',
@ -37,6 +38,18 @@ bluez5_sources = [
'midi-server.c',
]
bluez5_interface_src = gnome.gdbus_codegen('bluez5-interface-gen',
sources: 'org.bluez.xml',
interface_prefix : 'org.bluez.',
object_manager: true,
namespace : 'Bluez5',
annotations : [
['org.bluez.GattCharacteristic1.AcquireNotify()', 'org.gtk.GDBus.C.UnixFD', 'true'],
['org.bluez.GattCharacteristic1.AcquireWrite()', 'org.gtk.GDBus.C.UnixFD', 'true'],
]
)
bluez5_sources += [ bluez5_interface_src ]
bluez5_data = ['bluez-hardware.conf']
install_data(bluez5_data, install_dir : spa_datadir / 'bluez5')
@ -159,7 +172,7 @@ test_apps = [
bluez5_test_lib = static_library('bluez5_test_lib',
[ 'midi-parser.c' ],
include_directories : [ configinc ],
dependencies : [ spa_dep, dbus_dep ],
dependencies : [ spa_dep, bluez5_deps ],
install : false
)

View file

@ -0,0 +1,71 @@
<node>
<interface name="org.bluez.Adapter1">
<method name="RegisterApplication">
<arg direction="in" type="o" name="path"/>
<arg direction="in" type="a{sv}" name="options"/>
</method>
</interface>
<interface name="org.bluez.Device1">
<property name="Adapter" type="o" access="read"/>
<property name="Connected" type="b" access="read"/>
<property name="ServicesResolved" type="b" access="read"/>
<property name="Name" type="s" access="read"/>
<property name="Alias" type="s" access="read"/>
<property name="Address" type="s" access="read"/>
<property name="Icon" type="s" access="read"/>
<property name="Class" type="u" access="read"/>
<property name="Appearance" type="q" access="read"/>
</interface>
<interface name="org.bluez.GattManager1">
<method name="RegisterApplication">
<arg direction="in" type="o" name="path"/>
<arg direction="in" type="a{sv}" name="options"/>
</method>
</interface>
<interface name="org.bluez.GattProfile1">
<method name="Release">
</method>
<property name="UUIDs" type="as" access="read"/>
</interface>
<interface name="org.bluez.GattService1">
<property name="UUID" type="s" access="read"/>
<property name="Primary" type="b" access="read"/>
<property name="Device" type="o" access="read"/>
</interface>
<interface name="org.bluez.GattCharacteristic1">
<method name="ReadValue">
<arg direction="in" type="a{sv}" name="options"/>
<arg direction="out" type="ay" name="value"/>
</method>
<method name="AcquireNotify">
<arg direction="in" type="a{sv}" name="options"/>
<arg direction="out" type="h" name="fd"/>
<arg direction="out" type="q" name="mtu"/>
</method>
<method name="AcquireWrite">
<arg direction="in" type="a{sv}" name="options"/>
<arg direction="out" type="h" name="fd"/>
<arg direction="out" type="q" name="mtu"/>
</method>
<property name="UUID" type="s" access="read"/>
<property name="Service" type="o" access="read"/>
<property name="WriteAcquired" type="b" access="read"/>
<property name="NotifyAcquired" type="b" access="read"/>
<property name="Flags" type="as" access="read"/>
</interface>
<interface name="org.bluez.GattDescriptor1">
<method name="ReadValue">
<arg direction="in" type="a{sv}" name="options"/>
<arg direction="out" type="ay" name="value"/>
</method>
<property name="UUID" type="s" access="read"/>
<property name="Characteristic" type="o" access="read"/>
<property name="Flags" type="as" access="read"/>
</interface>
</node>

View file

@ -16,7 +16,7 @@ endif
if get_option('audiotestsrc').allowed()
subdir('audiotestsrc')
endif
if bluez_dep.found()
if bluez_deps_found
subdir('bluez5')
endif
if avcodec_dep.found()