1c5b45f16SAbhishek Pandit# sdbusplus 2c5b45f16SAbhishek Pandit 32b0bdd6eSPatrick Williamssdbusplus contains two parts: 42b0bdd6eSPatrick Williams 5*f7944dcbSPatrick Williams1. A C++ library (libsdbusplus) for interacting with D-Bus, built on top of the 6*f7944dcbSPatrick Williams sd-bus library from systemd. 72b0bdd6eSPatrick Williams2. A tool (sdbus++) to generate C++ bindings to simplify the development of 82b0bdd6eSPatrick Williams D-Bus-based applications. 92b0bdd6eSPatrick Williams 102b0bdd6eSPatrick Williams## Dependencies 112b0bdd6eSPatrick Williams 122b0bdd6eSPatrick WilliamsThe sdbusplus library requires sd-bus, which is contained in libsystemd. 132b0bdd6eSPatrick Williams 14*f7944dcbSPatrick WilliamsThe sdbus++ application requires Python 3 and the Python libraries mako and 15*f7944dcbSPatrick Williamsinflection. 162b0bdd6eSPatrick Williams 1718ed2b87SPatrick Williams## Building 1818ed2b87SPatrick Williams 1918ed2b87SPatrick WilliamsThe sdbusplus library is built using meson. 2018ed2b87SPatrick Williams 21d8a19adaSPatrick Williams```sh 2218ed2b87SPatrick Williamsmeson build 2318ed2b87SPatrick Williamscd build 2418ed2b87SPatrick Williamsninja 2518ed2b87SPatrick Williamsninja test 2618ed2b87SPatrick Williamsninja install 2718ed2b87SPatrick Williams``` 2818ed2b87SPatrick Williams 2918ed2b87SPatrick WilliamsOptionally, building the tests and examples can be disabled by passing 30cff540a9SPatrick Williams`-Dtests=disabled` and `-Dexamples=disabled` respectively to `meson`. 3118ed2b87SPatrick Williams 32*f7944dcbSPatrick WilliamsThe sdbus++ application is installed as a standard Python package using 33*f7944dcbSPatrick Williams`setuptools`. 3418ed2b87SPatrick Williams 35d8a19adaSPatrick Williams```sh 3618ed2b87SPatrick Williamscd tools 3718ed2b87SPatrick Williams./setup.py install 3818ed2b87SPatrick Williams``` 3918ed2b87SPatrick Williams 402b0bdd6eSPatrick Williams## C++ library 412b0bdd6eSPatrick Williams 42cff540a9SPatrick WilliamsThe sdbusplus library builds on top of the [sd-bus] library to create a modern 43cff540a9SPatrick WilliamsC++ API for D-Bus. The library attempts to be as lightweight as possible, 44cff540a9SPatrick Williamsusually compiling to exactly the sd-bus API calls that would have been 45cff540a9SPatrick Williamsnecessary, while also providing compile-time type-safety and memory leak 46cff540a9SPatrick Williamsprotection afforded by modern C++ practices. 472b0bdd6eSPatrick Williams 482b0bdd6eSPatrick WilliamsConsider the following code: 49d8a19adaSPatrick Williams 50d8a19adaSPatrick Williams```cpp 518ca6025eSVernon Maueryauto b = bus::new_default_system(); 522b0bdd6eSPatrick Williamsauto m = b.new_method_call("org.freedesktop.login1", 532b0bdd6eSPatrick Williams "/org/freedesktop/login1", 542b0bdd6eSPatrick Williams "org.freedesktop.login1.Manager", 552b0bdd6eSPatrick Williams "ListUsers"); 562b0bdd6eSPatrick Williamsauto reply = b.call(m); 572b0bdd6eSPatrick Williams 582b0bdd6eSPatrick Williamsstd::vector<std::tuple<uint32_t, std::string, message::object_path>> users; 592b0bdd6eSPatrick Williamsreply.read(users); 609cde21ffSPatrick Williams // or 619cde21ffSPatrick Williamsauto users = reply.unpack< 629cde21ffSPatrick Williams std::vector<std::tuple<uint32_t, std::string, message::object_path>>>(); 632b0bdd6eSPatrick Williams``` 642b0bdd6eSPatrick Williams 652b0bdd6eSPatrick WilliamsIn a few, relatively succinct, C++ lines this snippet will create a D-Bus 66*f7944dcbSPatrick Williamsconnection to the system bus, and call the systemd login manager to get a list 67*f7944dcbSPatrick Williamsof active users. The message and bus objects are automatically freed when they 68*f7944dcbSPatrick Williamsleave scope and the message format strings are generated at compile time based 69*f7944dcbSPatrick Williamson the types being read. Compare this to the corresponding server code within 70*f7944dcbSPatrick Williams[logind]. 712b0bdd6eSPatrick Williams 722b0bdd6eSPatrick WilliamsIn general, the library attempts to mimic the naming conventions of the sd-bus 732b0bdd6eSPatrick Williamslibrary: ex. `sd_bus_call` becomes `sdbusplus::bus::call`, 742b0bdd6eSPatrick Williams`sd_bus_get_unique_name` becomes `sdbusplus::bus::get_unique_name`, 75*f7944dcbSPatrick Williams`sd_bus_message_get_signature` becomes `sdbusplus::message::get_signature`, etc. 76*f7944dcbSPatrick WilliamsThis allows a relatively straight-forward translation back to the sd-bus 772b0bdd6eSPatrick Williamsfunctions for looking up the manpage details. 782b0bdd6eSPatrick Williams 79cff540a9SPatrick Williams[sd-bus]: http://0pointer.net/blog/the-new-sd-bus-api-of-systemd.html 80*f7944dcbSPatrick Williams[logind]: 81*f7944dcbSPatrick Williams https://github.com/systemd/systemd/blob/d60c527009133a1ed3d69c14b8c837c790e78d10/src/login/logind-dbus.c#L496 82cff540a9SPatrick Williams 832b0bdd6eSPatrick Williams## Binding generation tool 842b0bdd6eSPatrick Williams 85*f7944dcbSPatrick Williamssdbusplus also contains a bindings generator tool: `sdbus++`. The purpose of a 86*f7944dcbSPatrick Williamsbindings generator is to reduce the boilerplate associated with creating D-Bus 87*f7944dcbSPatrick Williamsserver or client applications. When creating a server application, rather than 88*f7944dcbSPatrick Williamscreating sd-bus vtables and writing C-style functions to handle each vtable 89*f7944dcbSPatrick Williamscallback, you can create a small YAML file to define your D-Bus interface and 90*f7944dcbSPatrick Williamsthe `sdbus++` tool will create a C++ class that implements your D-Bus interface. 91*f7944dcbSPatrick WilliamsThis class has a set of virtual functions for each method and property, which 92*f7944dcbSPatrick Williamsyou can overload to create your own customized behavior for the interface. 932b0bdd6eSPatrick Williams 9477b8aac3SPatrick WilliamsThere are currently two types of YAML files: [interface](docs/yaml/interface.md) 9577b8aac3SPatrick Williamsand [error](docs/yaml/error.md). Interfaces are used to create server and client 9677b8aac3SPatrick WilliamsD-Bus interfaces. Errors are used to define C++ exceptions which can be thrown 9777b8aac3SPatrick Williamsand will automatically turn into D-Bus error responses. 982b0bdd6eSPatrick Williams 992b0bdd6eSPatrick Williams[[D-Bus client bindings are not yet implemented. See openbmc/openbmc#851.]] 1002b0bdd6eSPatrick Williams 1012b0bdd6eSPatrick Williams### Generating bindings 102c5b45f16SAbhishek Pandit 103c5b45f16SAbhishek Pandit## How to use tools/sdbus++ 104c5b45f16SAbhishek Pandit 105c5b45f16SAbhishek PanditThe path of your file will be the interface name. For example, for an interface 1062b0bdd6eSPatrick Williams`org.freedesktop.Example`, you would create the files 1072b0bdd6eSPatrick Williams`org/freedesktop/Example.interface.yaml` and 1082b0bdd6eSPatrick Williams`org/freedesktop/Example.errors.yaml]` for interfaces and errors respectively. 1092b0bdd6eSPatrick WilliamsThese can then be used to generate the server and error bindings: 110d8a19adaSPatrick Williams 111d8a19adaSPatrick Williams```sh 1122b0bdd6eSPatrick Williamssdbus++ interface server-header org.freedesktop.Example > \ 1132b0bdd6eSPatrick Williams org/freedesktop/Example/server.hpp 1142b0bdd6eSPatrick Williamssdbus++ interface server-cpp org.freedesktop.Example > \ 1152b0bdd6eSPatrick Williams org/freedesktop/Example/server.cpp 1162b0bdd6eSPatrick Williamssdbus++ error exception-header org.freedesktop.Example > \ 1172b0bdd6eSPatrick Williams org/freedesktop/Example/error.hpp \ 1182b0bdd6eSPatrick Williamssdbus++ error exception-cpp org.freedesktop.Example > \ 1192b0bdd6eSPatrick Williams org/freedesktop/Example/error.cpp 120c5b45f16SAbhishek Pandit``` 1212b0bdd6eSPatrick Williams 1222b0bdd6eSPatrick WilliamsMarkdown-based documentation can also be generated from the interface and 1232b0bdd6eSPatrick Williamsexception files: 124d8a19adaSPatrick Williams 125d8a19adaSPatrick Williams```sh 1262b0bdd6eSPatrick Williamssdbus++ interface markdown org.freedesktop.Example > \ 1272b0bdd6eSPatrick Williams org/freedesktop/Example.md 1282b0bdd6eSPatrick Williamssdbus++ error markdown org.freedesktop.Example >> \ 1292b0bdd6eSPatrick Williams org/freedesktop/Example.md 1302b0bdd6eSPatrick Williams``` 1312b0bdd6eSPatrick Williams 13218ed2b87SPatrick WilliamsSee the `example/meson.build` for more details. 133703892e2SManojkiran Eda 134703892e2SManojkiran Eda## Installing sdbusplus on custom distributions 135703892e2SManojkiran Eda 136703892e2SManojkiran EdaInstallation of sdbusplus bindings on a custom distribution requires a few 137703892e2SManojkiran Edapackages to be installed prior. Although these packages are the same for several 138703892e2SManojkiran Edadistributions the names of these packages do differ. Below are the packages 139703892e2SManojkiran Edaneeded for Ubuntu and Fedora. 140703892e2SManojkiran Eda 141703892e2SManojkiran Eda### Installation on Ubuntu 142703892e2SManojkiran Eda 143d8a19adaSPatrick Williams```sh 144d8a19adaSPatrick Williamssudo apt install git meson libtool pkg-config g++ libsystemd-dev \ 145d8a19adaSPatrick Williams python3 python3-pip python3-yaml python3-mako python3-inflection 146703892e2SManojkiran Eda``` 147703892e2SManojkiran Eda 148703892e2SManojkiran Eda### Installation on Fedora 149703892e2SManojkiran Eda 150d8a19adaSPatrick Williams```sh 151d8a19adaSPatrick Williamssudo dnf install git meson libtool gcc-c++ pkgconfig systemd-devel \ 152d8a19adaSPatrick Williams python3 python3-pip python3-yaml python3-mako 153703892e2SManojkiran Eda``` 154703892e2SManojkiran Eda 155d8a19adaSPatrick WilliamsInstall the inflection package using the pip utility (on Fedora) 156d8a19adaSPatrick Williams 157d8a19adaSPatrick Williams```sh 158d8a19adaSPatrick Williamspip3 install inflection 159d8a19adaSPatrick Williams``` 160