In this post we will talk about DBus introspection and we will
implement a simple DBus server application.
As you may see through d-feet, many DBus objects implement the
org.freedesktop.DBus.Introspectable interface. This interface
lets other apps know which interfaces does an object implement and
how these interfaces are composed. The
org.freedesktop.DBus.Introspectable interface has only one
The Introspect method, called on an object, returns an XML
description of the interfaces implemented. This is an example:
The XML given from DBus instrospection is really straightforward. The
are collection of interfaces and every interfaces has methods. For
every method every argument is announced with his direction and his
Ok. Let’s start to code.
This is the main function. I used some already discussed dbus
functions but I also used three functions. We already talked about
connection names and we said that every application connected to a
DBus bus gets a unique name. If the application want to be reached by
other application it must get a “well-known” name. To get a well-known
name we want to use the dbus_bus_request_name.
The dbus_connection_try_register_object_path tries to
register a new object for the current application. The object path is
specified in the second argument and the third argument is a coupe of
callback functions, in which I use only the message_functions
there are called when a message directing to our object is received on
the bus. We will see the tutorial_messages function later.
The next new function is dbus_connection_read_write_dispatch
which reads a message from the DBus queue, blocking until a message is
available, and dispatch the message to the interested object.
This is our classical error checking function:
This is the heart of our server. This function reads the message using
dbus_message_get_member and then routes the message to the
appropriate function. If this function returns
“DBUS_HANDLER_RESULT_HANDLED” then the DBus library doesn’t consider
other handlers, instead if the functions returns
“DBUS_HANDLER_RESULT_NOT_YET_HANDLED” then the library will process
Here is the functions who responds to the introspection calls. We only
respond with the XML data:
Our sample object has a “Sum” call which we implement here:
We use the dbus_message_get_args to read the incoming
messages and the dbus_message_new_method_return to create a
reply message based on the query message. The
dbus_connection_send function gets the message in the output
This is our first DBus server! You can test it with d-feel double clicking on the Sum method and giving two number as 32,33. It will answer with the correct sum.