Federico Fuga

Engineering, Tech, Informatics & science

The dbus-cpp Library, introduction

28 Nov 2015 22:33 UTC C++ Code Linux
While working on a bluetooth based embedded device, I needed to deal with the somehow infamous dbus.







[DBus](https://wiki.freedesktop.org/www/Software/dbus/) and [Bluez5](http://www.bluez.org/) have a very steep learning curve, and if you are don't have access to either [Qt framework](https://www.qt.io/) or [glib framework](https://developer.gnome.org/glib/), you have indeed a problem.







Glib are very complex C libraries that mimick some of the built-in feature of the C++ language, while on the other hand, Qt are very vast and sometime a big overkill. The stunning news is that it seems there aren't many options if you need a C++ only lightweight solution.







It is not true, indeed. Somehow well hidden in the creases of the web, I found an awesome alternative that is at the same time elegant, very well designed, modern and lightweight. The library is named [DBUS-CPP](https://launchpad.net/dbus-cpp).







Be warned: there are two other c++ libraries, one called dbus-c++ and another with the same name. But both are old and unmaintained. You could also try to use the low-level api, through [libdbus](http://dbus.freedesktop.org/doc/api/html/group__DBus.html), but the documentation itself discourage from using it. Before finding the actual solution, I spent some week trying to implement my code this way, and believe in me, even the most obvious operations are long, complex, repetitive, boring and error prone. 







The dbus-cpp library I want to introduce is homed on launchpad, at this url: [https://launchpad.net/dbus-cpp](https://launchpad.net/dbus-cpp)







The library is written in pure [c++11](http://www.stroustrup.com/C++11FAQ.html), it is mostly based on template metaprogramming and for this reason his learning curve is not gentle. But once the basic ideas behind the library are clear, you can appreciate his elegance. Unfortunately there is no documentation and no tutorials, but the developers make great use of unit testing, so there are a lot of test code that can be studied. There are 3 or 4 examples, but they only provide example on how to access a dbus service as client, not on how to implement dbus services.







This post is the first of a series. They are the result of my personal studies and applications on the embedded project. My idea is to fill the gap and provide people some ideas on how to start. dbus-cpp is difficult to find on the web, but the efficiency that can be reached with it makes me think it deserve much more visibility. 







I want to thank the development team leader, Thomas Voß, and his team, for the library and for the time he dedicated me to answer my questions and clarify my doubts. I hope these articles could be a significant contribution.







Later note: linux distributions usually provide earlier version of dbus-cpp. For example, Ubuntu 14.04 LTE provides dbus-cpp 2.0. The latest version available from the sources are 5.0. I strongly suggest to download the latest version from the [source repository](https://bazaar.launchpad.net/~phablet-team/dbus-cpp/trunk/files) (they use bazaar) and compile it. They use CMake as a building tool, so compiling and installing is very easy. On the other hand, Ubuntu 15.10 provides the latest version.