如何通过gdbus发送二维数组?

时间:2017-07-07 15:27:44

标签: arrays glib dbus gdbus gvariant

我试图通过dbus发送一个二维的字符串数组。(我使用gdbus api,使用gdbus-codegen)。
我已经如下定义了dbus接口。

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

我试图以下列方式创建GVariant:

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);

其中pack_string_array将一个c样式字符串数组打包成一个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;
}

然而,当我测试它时,程序崩溃时出现以下错误:

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

如何通过dbus发送字符串数组?

2 个答案:

答案 0 :(得分:0)

用于构建器和变体的签名不匹配:

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

答案 1 :(得分:0)

$ declare -p results
declare -a results='([0]="46" [1]="677" [2]="9" [3]="18" [4]="0" [5]="07")'