Passing structure over d-bus - glib - example
Paul Forgey
paulf at altusnetworks.com
Mon Jan 21 18:01:08 PST 2008
In dbus-glib, structures are marshaled into and from a GValueArray *.
It's neither convenient nor natural, but dbus-binding-tool isn't much
of a code generator so all but the most simple or common cases are
very thinly wrapped. The mystruct example would have three elements
with types G_TYPE_INT, G_TYPE_STRING and G_TYPE_INT. If you want to
marhsal variants, use a GValue * by itself (G_TYPE_VALUE). There are
even built-in containers to handle dictionaries (GHashTable *) and
arrays (GPtrArray *) of any type, including G_TYPE_VALUE.
I've found the easiest way to hack on what exactly dbus-glib does with
certain complex cases is to just generate the bindings file with dbus-
binding-tool and see what it outputs.
> At no time can D-Bus ever send a NULL however.
That's been a real PITA for us. Sometimes NULL pointers are meant to
mean something.
On Jan 21, 2008, at 1:52 PM, John (J5) Palmieri wrote:
>
> On Mon, 2008-01-21 at 19:55 +0530, Trilok Soni wrote:
>> Hi,
>>
>> I want to pass "structure" over d-bus, I see that d-bus specification
>> mentions how to add that
>> with "(" and ")" notions. But, I am not able to find any example with
>> dbus low-level apis or dbus-glib binding
>> demonstrating that functionality. Can any one please point me the
>> same
>> or example introspection xml
>> file which I could use with dbus-binding tool to generate the dummy
>> sever/client code?
>
> First you need to understand what a structure in D-Bus means. A
> structure is just an array of mixed types. To map a C structure to a
> D-Bus structure you basically need to marshal each element in the
> struct
> into a index withing the array and then demarshal it on the other
> side.
> I'm not totally sure how or if the GLib bindings can do this (there
> is a
> dbus_g_type_struct_set and get methods for creating struct types in
> GValues) but in the c bindings it would look something like this in
> pseudocode:
>
> struct _mystruct
> {
> int a;
> char *b;
> int c;
> } mystruct;
>
> int d;
> DBusMessageIter i, stuct_i;
>
> dbus_message_iter_init_append(mesg, &i);
>
> // append an integer for the fun of it
> dbus_message_append_basic(&i, DBUS_TYPE_INT32, &d);
>
> // open the struct for appending
> dbus_message_iter_open_container(&i, DBUS_TYPE_STRUCT, "isi",
> &struct_i);
>
> // append each element of the struct
> dbus_message_append_basic(&struct_i, DBUS_TYPE_INT32, &a);
> dbus_message_append_basic(&struct_i, DBUS_TYPE_STRING, &b);
> dbus_message_append_basic(&struct_i, DBUS_TYPE_INT32, &c);
>
> // close the struct container
> dbus_message_close_container(&i, &struct_i);
>
> // I'll leave it up to you to figure out how to send the message after
> this
>
>> Apart from this How do we specify "argument" of the method taking
>> "void *" as argument, I have to write a method
>> which take "void *" as argument, what is equivalent in dbus-glib
>> binding type?
>
> You mean a method that can take any type as input. We call that a
> Variant in D-Bus and in GLib you would pass a GValue. The semantics
> are
> a bit different than a void * which can be anything. A Variant is
> just
> a type which holds a value and the type of the value (instead of
> specifying a strict type). At no time can D-Bus ever send a NULL
> however.
>
> --
> John (J5) Palmieri <johnp at redhat.com>
>
> _______________________________________________
> dbus mailing list
> dbus at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/dbus
More information about the dbus
mailing list