Re: [systemd-devel] Returning a struct from an sd-bus method
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? Niall On Thu, 23 Aug 2018 at 18:35, Simon McVittie 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@lists.freedesktop.org > https://lists.freedesktop.org/mailman/listinfo/systemd-devel > ___ systemd-devel mailing list systemd-devel@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/systemd-devel
Re: [systemd-devel] Returning a struct from an sd-bus method
On Thu, 23 Aug 2018 at 17:34:14 +0100, Simon McVittie wrote: > 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); Sorry, obviously that's correct when not using a struct. What I meant is that I think this: sd_bus_reply_method_return(m, "(xx)", (int64_t) s->x, (int64_t) s->y); might be the correct way to return a message containing a single struct. smcv ___ systemd-devel mailing list systemd-devel@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/systemd-devel
Re: [systemd-devel] Returning a struct from an sd-bus method
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@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/systemd-devel
[systemd-devel] Returning a struct from an sd-bus method
Hi, I'm trying to return a struct from an sdbus method. I followed the simple example of a service at http://0pointer.net/blog/the-new-sd-bus-api-of-systemd.html. I then created a simple method as follows: struct pack { int x; int y; }; static int foo(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { struct pack s; s.x = 1; s.y = 2; return sd_bus_reply_method_return(m, "(xx)", s); } static const sd_bus_vtable vtable[] = { SD_BUS_VTABLE_START(0), SD_BUS_METHOD("foo", "", "(xx)", foo, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_VTABLE_END }; The rest of the service is as in the example. When I run the service and call foo with busctl I get garbage: (xx) 8589934593 140731098591648 Can someone point me to what I am doing wrong? Thanks, Niall ___ systemd-devel mailing list systemd-devel@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/systemd-devel