commit | 4a46eb5aa05df6da3ba66446a6e1526098e47d0f | [log] [tgz] |
---|---|---|
author | Patrick Williams <patrick@stwcx.xyz> | Tue Nov 23 09:35:56 2021 -0600 |
committer | Patrick Williams <patrick@stwcx.xyz> | Wed Dec 08 17:01:02 2021 +0000 |
tree | 2a1204ae0936364136eeb50d12d8d9230fc24d17 | |
parent | 2404176d3ca9e3b5543204f1986a8e9b153522a4 [diff] |
object: handle diamond inheritance Commit 664922157bbbd14f0ad1692cee5547f60f6c617c added an inheritance to `server::object` to gain access to the bus-pointer. This was observed to cause a compile failure in some applications which had a diamond-inheritance structure due to a nested `object` inheritance: `object<object<iface0, iface1>, object<iface2, iface3>>` These clients probably should not have attempted a nested/diamond because the previous implementation would have resulted in a silent failure to make `action::emit_interface_added` work properly (since object itself doesn't have an `emit_added` function). Improve the `object` so that: - Diamond inheritance is no longer possible with nested inherits. - The `action::emit_interface_added` action works properly with nested inherits. This required adding some template specialization to determine if one of the type arguments to `object` were nested (ie. `object<Args0..., object<Args1...>, Args2...>`) and collapse out the nested `object` inheritance (so that the example acts exactly the same as `object<Args0..., Args1..., Args2...>`). Tested: Enhance the existing `object::emit` test cases to operate on a nested `object` structure. Signed-off-by: Patrick Williams <patrick@stwcx.xyz> Change-Id: I10635a256aafbdc1e42439a49fa61127e06c8e36
sdbusplus contains two parts:
The sdbusplus library requires sd-bus, which is contained in libsystemd.
The sdbus++ application requires Python 3 and the Python libraries mako and inflection.
The sdbusplus library is built using meson.
meson build cd build ninja ninja test ninja install
Optionally, building the tests and examples can be disabled by passing -Dtests=disabled
and -Dexamples=disabled
respectively to `meson.
The sdbus++ application is installed as a standard Python package using setuptools
.
cd tools ./setup.py install
The sdbusplus library builds on top of the sd-bus library to create a modern C++ API for D-Bus. The library attempts to be as lightweight as possible, usually compiling to exactly the sd-bus API calls that would have been necessary, while also providing compile-time type-safety and memory leak protection afforded by modern C++ practices.
Consider the following code:
auto b = bus::new_default_system(); auto m = b.new_method_call("org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", "ListUsers"); auto reply = b.call(m); std::vector<std::tuple<uint32_t, std::string, message::object_path>> users; reply.read(users);
In a few, relatively succinct, C++ lines this snippet will create a D-Bus connection to the system bus, and call the systemd login manager to get a list of active users. The message and bus objects are automatically freed when they leave scope and the message format strings are generated at compile time based on the types being read. Compare this to the corresponding server code within logind.
In general, the library attempts to mimic the naming conventions of the sd-bus library: ex. sd_bus_call
becomes sdbusplus::bus::call
, sd_bus_get_unique_name
becomes sdbusplus::bus::get_unique_name
, sd_bus_message_get_signature
becomes sdbusplus::message::get_signature
, etc. This allows a relatively straight-forward translation back to the sd-bus functions for looking up the manpage details.
sdbusplus also contains a bindings generator tool: sdbus++
. The purpose of a bindings generator is to reduce the boilerplate associated with creating D-Bus server or client applications. When creating a server application, rather than creating sd-bus vtables and writing C-style functions to handle each vtable callback, you can create a small YAML file to define your D-Bus interface and the sdbus++
tool will create a C++ class that implements your D-Bus interface. This class has a set of virtual functions for each method and property, which you can overload to create your own customized behavior for the interface.
There are currently two types of YAML files: interface and error. Interfaces are used to create server and client D-Bus interfaces. Errors are used to define C++ exceptions which can be thrown and will automatically turn into D-Bus error responses.
[[ D-Bus client bindings are not yet implemented. See openbmc/openbmc#851. ]]
The path of your file will be the interface name. For example, for an interface org.freedesktop.Example
, you would create the files org/freedesktop/Example.interface.yaml
and org/freedesktop/Example.errors.yaml]
for interfaces and errors respectively. These can then be used to generate the server and error bindings:
sdbus++ interface server-header org.freedesktop.Example > \ org/freedesktop/Example/server.hpp sdbus++ interface server-cpp org.freedesktop.Example > \ org/freedesktop/Example/server.cpp sdbus++ error exception-header org.freedesktop.Example > \ org/freedesktop/Example/error.hpp \ sdbus++ error exception-cpp org.freedesktop.Example > \ org/freedesktop/Example/error.cpp
Markdown-based documentation can also be generated from the interface and exception files:
sdbus++ interface markdown org.freedesktop.Example > \ org/freedesktop/Example.md sdbus++ error markdown org.freedesktop.Example >> \ org/freedesktop/Example.md
See the example/meson.build
for more details.
Installation of sdbusplus bindings on a custom distribution requires a few packages to be installed prior. Although these packages are the same for several distributions the names of these packages do differ. Below are the packages needed for Ubuntu and Fedora.
sudo apt install git meson libtool pkg-config g++ libsystemd-dev python3 python3-pip python3-yaml python3-mako python3-inflection
sudo dnf install git meson libtool gcc-c++ pkgconfig systemd-devel python3 python3-pip python3-yaml python3-mako
Install the inflection package using the pip utility (on Fedora)
pip3 install inflection