I am trying to a byte array over D-Bus using the GDBus bindings. Can you please let me know how I can achive that. I tried googling but didnt help.
Byte array contains a image file so cannot be converted to charbytearray
Any help is appriciated
I did some tests using an XML where I used the type ay
. This works well with the QT binding (generated with qdbusxml2cpp
) where it translates into QByteArray
however it seems that it doesn't work with the glib binding (generated with gdbus-codegen
) where it translates in gchar *
and it seems you lose what's after \0
- because somehow it's handled as a string. However you will find that:
This automatic mapping can be turned off by using the annotation org.gtk.GDBus.C.ForceGVariant - if used then a
GVariant
is always exchanged instead of the corresponding native C type. This annotation may be convenient to use when using bytestrings (type-stringay
) for data that could have embedded NUL bytes.
Which means according to https://developer.gnome.org/gio/stable/gdbus-codegen.html that you could handle it as GVariant
. I tested this by adding the tag for annotation org.gtk.GDBus.C.ForceGVariant
<annotation name="org.gtk.GDBus.C.ForceGVariant" value="true"/>
on each arg and it works.
Actually, instead of using type "ay", you can use "a(y)". The glib binding would translate "a(y)" to GVariant*.
And then you can use "GVariant" handling to deal with the parameter.
e.g. the xml file
<method name="parsePacket">
<arg direction="in" name="message" type="a(y)">
<doc>
<line>type: const Uint8 *</line>
</doc>
</arg>
</method>
Generated method:
gboolean (*handle_parse_packet) (
IDbusObject *object, GDBusMethodInvocation *invocation, GVariant *arg_message);
gboolean idbusobject_call_parse_packet_sync (
IDbusObject *proxy,
GVariant *arg_message,
GCancellable *cancellable,
GError **error);
You can extract and insert data using "GVariant" method.
Insert data at client side:
void parsePacket (unsigned char* arg_message, guint16 arg_length)
{
GVariantBuilder *builder;
GVariant *value;
builder = g_variant_builder_new (G_VARIANT_TYPE ("a(y)"));
for (int i = 0; i < arg_length; i++)
{
g_variant_builder_add (builder, "(y)", arg_message[i]);
}
value = g_variant_new ("a(y)", builder);
g_variant_builder_unref (builder);
idbusobject_call_parse_packet_sync(proxy,
value,
NULL,
NULL);
}
Extract data at server side:
gboolean handleParsePacket (
IDbusObject *object,
GDBusMethodInvocation *invocation,
GVariant *arg_message)
{
unsigned char byteArray[2048];
int actualLength = 0;
GVariantIter *iter;
guchar str;
g_variant_get (arg_message, "a(y)", &iter);
while (g_variant_iter_loop (iter, "(y)", &str))
{
byteArray[actualLength++] = str;
}
g_variant_iter_free (iter);
idbusobject_complete_parse_packet( object, invocation);
return (TRUE);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With