Reputation: 2263
I'm creating a service on D-Bus using gdbus
and gdbus-codegen
.
Introspection is this:
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<interface name="com.example.foo">
<property name="Bar" type="s" access="readwrite" />
</interface>
</node>
I'm executing gdbus-codegen
like this:
gdbus-codegen --interface-prefix com.example --generate-c-code=foo foo.xml
And my main.cpp looks like this:
#include <iostream>
#include "foo.h"
void OnBarChanged(GObject * gobject, GParamSpec * pspec, gpointer user_data)
{
std::cout << "Bar: " << foo_get_bar((Foo *)gobject) << std::endl;
}
void OnBusNameAquired(GDBusConnection * connection,
const gchar * name,
gpointer user_data)
{
Foo * foo = foo_skeleton_new();
g_signal_connect(foo, "notify::bar", G_CALLBACK(&OnBarChanged), NULL);
g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(foo),
connection,
"/com/example/foo",
NULL);
}
int main()
{
std::cout << "Testing DBus properties" << std::endl;
GMainLoop * loop;
loop = g_main_loop_new(NULL, FALSE);
g_bus_own_name(G_BUS_TYPE_SESSION,
"com.example.foo",
G_BUS_NAME_OWNER_FLAGS_NONE,
NULL,
OnBusNameAquired,
NULL,
NULL,
NULL);
g_main_loop_run(loop);
return 0;
}
This works as expected and I'm able to set and get properties using:
gdbus call --session --dest com.example.foo --object-path /com/example/foo --method org.freedesktop.DBus.Properties.Set "com.example.foo" "Bar" "<'baz'>"
and
gdbus call --session --dest com.example.foo --object-path /com/example/foo --method org.freedesktop.DBus.Properties.Get "com.example.foo" "Bar"
(<'baz'>,)
The problem:
I want to validate the setting of the property synchronously and be able to return an error if it fails. How can this be accomplished using the gdbus-codegen
-generated code?
PS:
The code leaks and is generally not production ready. I'm fine with that for now :-)
Edit
After continues research, it seems like the D-Bus properties are using the underlying GObject
property feature. Is it possible to install a custom validator when all of this is set up by the gdbus-codegen
-code?
Upvotes: 4
Views: 1611
Reputation: 5713
Connect to the GDBusInterfaceSkeleton::g-authorize-method
signal from your foo
skeleton. Your callback will be invoked for every D-Bus method invocation handled by your exported object — you can match on org.freedesktop.DBus.Properties.Set
calls and do the validation then.
There’s an example of this (for arbitrary method calls, not D-Bus property setting; but the principle is the same) in flatpak: https://github.com/flatpak/flatpak/blob/c915f73b41688a7dc2ec7f0ab2fbcf1a7c738841/system-helper/flatpak-system-helper.c#L1192
Upvotes: 1