How to send a two dimensional array over gdbus?

1.7k views Asked by At

I'm trying to send a two dimendional array of strings over dbus.( I'm using the gdbus api, with gdbus-codegen).
I have defined the dbus interface as follows.

<method name="foo">
            <arg name="count" direction="out" type="i" />
            <arg name="array" direction="out" type="a(ia(s))" />
</method>

I'm trying to create the GVariant in the following manner:

GVariantBuilder *builder;
GVariant *variant;
builder = g_variant_builder_new(G_VARIANT_TYPE("a(ia(s))"));
for (int i = 0; i < count; i++)
{
    g_variant_builder_add(builder, "(ia(s))", 
                          options[i].num_supported,
                          pack_string_array(options[i].num_supported, options[i].supported_values));
}

variant = g_variant_new("a(a(s))", builder);
my_interface_complete_foo(interface, invocation, count, variant);

where pack_string_array packs an array of c style strings into a gvariant.

GVariant* pack_string_array(int num_val, char **val)
{
    GVariantBuilder *builder;
    GVariant *values;
    builder = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
    for (int i = 0; i < num_val; i++)
    {
        g_message("%s", val[i]);
        g_variant_builder_add(builder, "(s)", val[i]);
    }

    if (num_val == 0)
        g_variant_builder_add(builder, "(s)", "NA");

    values = g_variant_new("a(s)", builder);
    return values;
}

However when I test it,the program crashes with the following errors:

 GLib-CRITICAL **: g_variant_builder_end: assertion 'ensure_valid_builder (builder)' failed
g_variant_get_type: assertion 'value != NULL' failed

How can I send a array of string arrays over the dbus?

2

There are 2 answers

0
Andrei On
GVariantBuilder* builder = g_variant_builder_new(G_VARIANT_TYPE("a(ia(s))"));

for (int i = 0; i < count; i++) {

    GVariantBuilder* builderStr = g_variant_builder_new(G_VARIANT_TYPE("a(s)"));
    for (int j = 0; j < nr_of_string; j++) {
        g_variant_builder_add(builderStr, "(s)", "Test");
    }
    g_variant_builder_add(builder, "(ia(s))", 32, **builderStr**);
}

GVariant* retVariant = g_variant_builder_end(builder);
0
Jussi Kukkonen On

The signatures used for the builder and the variant do not match:

builder = g_variant_builder_new(G_VARIANT_TYPE("a(ia(s))"));
...
variant = g_variant_new("a(a(s))", builder);