[systemd-devel] EXT: Re: Returning a struct from an sd-bus method
Ray, Ian (GE Healthcare)
ian.ray at ge.com
Fri Aug 24 04:33:36 UTC 2018
> On 23 Aug 2018, at 20.05, Niall Murphy <rinvix at gmail.com> wrote:
>
> Hi, thanks! You are correct about the signature, I was trying various things and pasted in some half complete code, sorry. After changing to pass the fields of the struct as individual parameters it does work correctly.
>
> I guess I would prefer to pass the struct so that you can share a single definition of the struct with both the client and service so as to avoid possible errors where you pass the fields in the wrong order. Also if you have a large struct it's a bit inconvenient to spell out every field on both ends.
>
> I thought it would be possible for sd-bus to marshall my C struct into a D-bus struct. Perhaps that is not possible?
>
Hi!
That would require some form of reflection (in order to know the contents
of an arbitrary struct at run-time) or meta-programming (to generate both
the C code that defines the structure, and the sd-bus code for performing
the marshalling).
> Niall
>
> On Thu, 23 Aug 2018 at 18:35, Simon McVittie <smcv at collabora.com> wrote:
> On Thu, 23 Aug 2018 at 16:52:38 +0200, Niall Murphy wrote:
> > struct pack {
> > int x;
> > int y;
> > };
> ...
> > return sd_bus_reply_method_return(m, "(xx)", s);
>
> Is there a reason why you're returning a struct/tuple? D-Bus methods
> can return as many things as you want[1], unlike C functions, so a more
> conventional return type would be "xx" instead of "(xx)".
>
> You also have a mismatch between your integer types: "x" is a 64-bit signed
> integer (mnemonic: the 64-bit signed and unsigned types are "x" and "t",
> which are the first letters of "sixty" that weren't already used for
> something more important) so they'll expect an int64_t. The type
> signatures for 32-bit integers are "i" and "u", depending on signedness.
> There is no correct type signature for types like int/long/long long
> whose size can vary between platform: integers in D-Bus messages are
> always fixed-size.
>
> Finally, I think the message-building API expects struct members as
> individual arguments, like
>
> sd_bus_reply_method_return(m, "xx", (int64_t) s->x, (int64_t) s->y);
>
> although I could be wrong about that.
>
> smcv
>
> [1] up to arbitrary message size limits measured in megabytes
> _______________________________________________
> systemd-devel mailing list
> systemd-devel at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/systemd-devel
> _______________________________________________
> systemd-devel mailing list
> systemd-devel at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/systemd-devel
More information about the systemd-devel
mailing list