D-Bus API Quick Reference ¶
D-Bus Quick Intro ¶
D-Bus is a protocol, part of the freedesktop project, it is designed easier IPC on linux. It is a beautiful idea thus that for applications want to call functions in another program, they do not need to link against that service, they can simply issue a call on the dbus instead. D-Bus acts like Mediator pattern. DBus has implementations in many programming languages.
A DBus service firstly has a
which looks like this:
, then under this name, there are the
, then through the object path, we get to
for its methods, properties and signals. We demostrate the usage
dbus-send --session --type=method_call --print-reply --dest=org.gnome.Rhythmbox /org/gnome/Rhythmbox/Player org.freedesktop.DBus.Introspectable.Introspect
This command sends a message to Rhythmbox for getting its available
is the convention
method to query the available methods in an interface.
There are two main dbus APIs, the “low level dbus c api” and the “systemd bus” API, the first one is more portable (available on freebsd, OSX through homebrew as well). Nevertheless, the api is not easy to use, that is why I created the tdbus (tiny dbus) project for easier usage.
In general, we touch the most the three parts of the API,
DBus Connection APIs ¶
, as its name pronounced, maintains a connection to the DBus
daemon. To work with it, DBus API provides 3 major routines to work with it;
read, write and dispatch. The most simple case would be simply calling
, it does all three things. But
usually a software with a mainloop which polls on fds, DBus provides callbacks
. For instance, we get
the fd for a
; for Timeout, one can use
timerfd for creating pollable fds. Note that
also has a
, but it is not pollable. For monitoring
, we need to create a fake
DBus Message APIs ¶
The messaging API is a subsystem itself. There is two concepts,
. Remember when we said dbus allows you to call functions?
The signature here is the function argument
Those two provides iterators and sub-iterators, we call
to aquire next element,
for get into sub-types. We can also validate the
with iterators is not particular easy, thus dbus has a shorthand for basic
dbus_message_append_args (message, DBUS_TYPE_INT32, &v_INT32, DBUS_TYPE_STRING, &v_STRING, DBUS_TYPE_INVALID);
For writing basic arguments,
dbus_message_get_args (message, DBUS_TYPE_INT32, &v_INT32, DBUS_TYPE_STRING, &v_STRING, DBUS_TYPE_INVALID);
Creating a DBusServer needs a little additional work. Firstly we need to call
to DBus Daemon to see if the service name is taken,
then all we need to do is simply fill up the
answering the method calls and unregister handler which can be simply blank.
How to answer the method calls is totally the freedom of the server, except
there are two mandatary methods, namely
(quering methods) and
(quering properties) for any usable dbus service.