Passing structure over DBUS API

John (J5) Palmieri johnp at redhat.com
Thu Apr 3 11:03:30 PDT 2008


On Thu, 2008-04-03 at 08:49 -0700, Wilson Khoo wrote:
> Hi
>     I am new with Dbus and I am not using any Binding tool. I am attempting to pass a structure similar to Glib GArray over DBUS. How can I do that.
> Is there any example out there can 1 point me to? Can someone explain the function dbus_message_iter_open_container and dbus_message_iter_append_fixed_array. The 3rd argument used in dbus_message_iter_open_container is contained_signature, whats a contained_signature? Must the contained_signature match the arguments passed into dbus_message_iter_append_fixed_array?
> 
> I have tried the below and it kept crashing on me in the dbus_message_iter_append_fixed_array.
> 
> static void send(char *num)
> {
>    DBusMessage         *msg;
>    DBusMessageIter     args, arrayIter;   
>    char                        *num_unicode;
>    int                             len = strlen(num);              
> 
>    //convert num to unicode
>    num_unicode  = malloc(len *2); 
>    for (int count=0; count < len; count++)
>    {
>         num_unicode[2*count]       = 0;
>         num_unicode[(2*count) + 1] = num[count];
>    }
> 
>    len = len * 2;
> 
>     dbus_message_new_method_call( ...);
> 
>    // append arguments
>    dbus_message_iter_init_append(msg, &args);
>  
>    if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_BYTE, &len)) 
>    {
>        fprintf(stderr, "Out Of Memory!\n"); 
>        exit(1);
>    }
> 
>    if (!dbus_message_iter_open_container(&args, DBUS_TYPE_ARRAY, "ay", &arrayIter))
>    {
>         fprintf(stderr, "Out Of Memory!\n"); 
>        exit(1);
>    }

I think you only have a one dimensional array here so the correct
signature is "y" which is the content of the array.
 
dbus_message_iter_open_container(&args, DBUS_TYPE_ARRAY, "y",
&arrayIter)

>    if (!dbus_message_iter_append_fixed_array (&arrayIter, DBUS_TYPE_ARRAY, &num_unicode, sizeof(num_unicode ))
>    {
>         fprintf (stderr, "No memory!\n");
>    }

Your type here would be DBUS_TYPE_BYTE and sizeof(num_unicode) gives you
the size of the pointer not the size of the array.  You want len * 2
though I would assign that to a descriptive variable just to have nicer
code.  So this line should be 

dbus_message_iter_append_fixed_array (&arrayIter, DBUS_TYPE_BYTE,
&num_unicode, len * 2)

Don't forget to close the iter once you are done appending to it.

-- 
John (J5) Palmieri <johnp at redhat.com>



More information about the dbus mailing list