[systemd-devel] sd_bus_message_read() failing with -ENXIO and -EINVAL
Mantas Mikulėnas
grawity at gmail.com
Thu Dec 29 07:06:47 UTC 2016
On Thu, Dec 29, 2016 at 8:44 AM, Erik Falor <ewfalor at gmail.com> wrote:
> I wish to use sd-bus to query upowerd for my battery's remaining
> percentage. I
> have begun by adapting the example program from Lennart's blog post
> introducing
> the sd-bus API:
>
> http://0pointer.net/blog/the-new-sd-bus-api-of-systemd.html
>
> However, I cannot succeed in adapting it to my needs. I can read the value
> of
> the Percentage property with
>
> $ busctl introspect org.freedesktop.UPower /org/freedesktop/UPower/
> devices/battery_BAT0
>
> This shows me that the Percentage is a double value, and that the Get()
> method
> of the org.freedesktop.DBus.Properties interface returns a variant value.
>
> I can also retrieve the desired datum with busctl:
>
> $ busctl call org.freedesktop.UPower \
> /org/freedesktop/UPower/devices/battery_BAT0 \
> org.freedesktop.DBus.Properties Get \
> ss org.freedesktop.UPower.Device Percentage
>
> I can make the equivalent method call using the sd-bus API from the C
> program,
> but the program errors out at the call to sd_bus_message_read().
>
> When I tell sd_bus_message_read() that my message is of variant type "v",
> it
> fails with -EINVAL. When I instead give it "d", it fails with -ENXIO.
>
> What does sd_bus_message_read() expect in the 2nd argument? Or am I doing
> something else wrong?
>
variant is a "container" type – you must first
call sd_bus_message_enter_container() before you can access the inner value:
sd_bus_message_enter_container(m, 'v', "d");
sd_bus_message_read(m, "d", &out);
For convenience you also have sd_bus_get_property() which even does the
method call for you.
--
Mantas Mikulėnas <grawity at gmail.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/systemd-devel/attachments/20161229/887601ae/attachment-0001.html>
More information about the systemd-devel
mailing list