There are many websites that deals with Dbus and its use cases. This blog is entirely about how a sample glib dbus binding works and steps for making it work.
The first step for implementing dbus is to have an XML which specifies the Methods to be exposed to the process and its arguments.
Sample XML
<node>
<interface name="Knight.Add.Dbus">
<method name="AddNum">
<arg name="num1" direction="in" type="i"/>
<arg name="num2" direction="in" type="i"/>
<arg name="sum" direction="out" type="i"/>
<arg name="retval" direction="out" type="i"/>
</method>
</interface>
</node>
Step 1: Save the above to add.xml
Here the interface name refers to group of related methods. Just like in OOPS.
method are the functions that needs to be exposed to processes.
arg - Specifies the input argument and output argument and its data types.
There are only few native data types supported by DBUS.
Refer to the url: http://dbus.freedesktop.org/doc/dbus-specification.html
Once you have this xml you can use glib code gen to generate code.
Step 2: Use the python module gdbus-codegen to generate the code by executing
gdbus-codegen --generate-c-code=add
This will result in two files add.[ch]
For each method there will be four functions in the generated file
Two functions for synchrounous call and two for async call.
In the example above they are
Async Call:
knight_add_dbus_call_add_num - This should be called in client side
knight_add_dbus_call_add_num_finish - This should be called in client side on call back.
Sync Call:
knight_add_dbus_call_add_num_sync - This should be called in client side
knight_add_dbus_complete_add_num - This should be in server side
Step 3: Start writing server side code
a)Connect to dbus-daemon
b)Assign a well known name to the bus.
c)Create a new object using the interface provided by generated code.Connect the signals corresponding to the methods to the object. Each method will have handle-<method name>
d)Run in infinite loop.
Sample server Code:
Step 4: Start writing client side code.
a)
The first step for implementing dbus is to have an XML which specifies the Methods to be exposed to the process and its arguments.
Sample XML
<node>
<interface name="Knight.Add.Dbus">
<method name="AddNum">
<arg name="num1" direction="in" type="i"/>
<arg name="num2" direction="in" type="i"/>
<arg name="sum" direction="out" type="i"/>
<arg name="retval" direction="out" type="i"/>
</method>
</interface>
</node>
Step 1: Save the above to add.xml
Here the interface name refers to group of related methods. Just like in OOPS.
method are the functions that needs to be exposed to processes.
arg - Specifies the input argument and output argument and its data types.
There are only few native data types supported by DBUS.
Refer to the url: http://dbus.freedesktop.org/doc/dbus-specification.html
Once you have this xml you can use glib code gen to generate code.
Step 2: Use the python module gdbus-codegen to generate the code by executing
gdbus-codegen --generate-c-code=add
This will result in two files add.[ch]
For each method there will be four functions in the generated file
Two functions for synchrounous call and two for async call.
In the example above they are
Async Call:
knight_add_dbus_call_add_num - This should be called in client side
knight_add_dbus_call_add_num_finish - This should be called in client side on call back.
Sync Call:
knight_add_dbus_call_add_num_sync - This should be called in client side
knight_add_dbus_complete_add_num - This should be in server side
Step 3: Start writing server side code
a)Connect to dbus-daemon
b)Assign a well known name to the bus.
c)Create a new object using the interface provided by generated code.Connect the signals corresponding to the methods to the object. Each method will have handle-<method name>
d)Run in infinite loop.
Sample server Code:
Step 4: Start writing client side code.
a)