Passing structure over d-bus - glib - example
John (J5) Palmieri
johnp at redhat.com
Mon Jan 21 13:52:32 PST 2008
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>
More information about the dbus
mailing list