D-Bus low-lewel API - sending an array of structures
Bogdan Lotko
boguslaw.lotko at chello.at
Sat May 7 02:13:41 PDT 2011
Hello,
Could somebody please analyze the piece of source code below.
I'd like to send a message containing an array of structures using
low-level API. The sent message shall be received in Qt.
For the test purposes the structure is a two-string structure, the array
contains 10 entries.
The receiver side (Qt) defines following parameter for the slot method
(Q_DECLARE METATYPE are omitted here):
structure
{
QString str1;
QString str2;
} twoString;
typedef QVector<twoString> structArr;
void slotMethod( structArr arr );
Now to the sender. I have to use low-level API :(.
Running the example below I get the following run-time error:
-------------------PASSED_0
-------------------PASSED_1
process 6585: Array or variant type requires that type struct be
written, but begin_struct was written
D-Bus not built with -rdynamic so unable to print a backtrace
And now the source code:
gboolean arrayOfStructSignal( MyClass *self )
{
// unique number
dbus_uint32_t serialNr = 0;
// DBusMessage
DBusMessage *msg;
// Dbus Message Iterators - main, array, structure
DBusMessageIter iter,
arr,
strct;
// Connection
DBusConnection *conn;
gchar buffer[BUFF_SIZE_SMALL];
const char *buff = buffer;
if( NULL == self )
{
return false;
}
conn = self->m_conn;
assert( NULL != conn );
// create a signal and check for errors
msg = dbus_message_new_signal( "/myObj",
"my.test.Interface",
"arrOfStructSignal" );
if (NULL == msg)
{
return false;
}
dbus_message_iter_init_append( msg, &iter );
// open the array of structures
dbus_message_iter_open_container( &iter,
DBUS_TYPE_ARRAY,
"r",
&arr );
cout << "-------------------PASSED_0 " << endl;
// array of 10 structures a(ss) - for test
for( int i = 0; i < 10; i++ )
{
cout << "-------------------PASSED_1 " << endl;
// Open the a structure within an array
// Here run-time error !!!!!!!!!!!!!!!!!!!
dbus_message_iter_open_container( &arr,
DBUS_TYPE_STRUCT,
NULL,
&strct );
cout << "-------------------PASSED_2 " << endl;
memset( buffer, 0, BUFF_SIZE_SMALL );
sprintf(buffer, "test 0 %d", i );
// append first string to the structure
if( !dbus_message_iter_append_basic(&strct, DBUS_TYPE_STRING, &buff) )
{
dbus_message_unref(msg);
return false;
}
cout << "-------------------PASSED_3 " << endl;
memset( buffer, 0, BUFF_SIZE_SMALL );
sprintf(buffer, "test 0 %d", i );
// append second string to the structure
if( !dbus_message_iter_append_basic(&strct, DBUS_TYPE_STRING, &buff) )
{
dbus_message_unref(msg);
return false;
}
cout << "-------------------PASSED_4 " << endl;
// close structure container
dbus_message_iter_close_container( &arr, &strct );
}
// close struct container
dbus_message_iter_close_container( &iter, &arr );
// send the message and flush the connection
if( !dbus_connection_send(conn, msg, &serialNr))
{
dbus_message_unref(msg);
return false;
}
dbus_connection_flush(conn);
// free the message
dbus_message_unref(msg);
return true;
}
Thanks for your help
regards,
Bogdan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/dbus/attachments/20110507/323357d5/attachment.html>
More information about the dbus
mailing list