Dbus如何发送接受一个结构题数据
这样的一个结构题数据如果用Dbus发送和接受呢?
比如
typedef struct student
{
int sid;
char name[30];
}ST_STUDENT;
我的Dbus封装借口可以发送char* 可是传入结构题就不能接受数据了。 问题也定位出来了, 望牛人解答啊。
代码如下:
#include <dbus/dbus.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mydbus.h"
//#include "debug.h"
/*
* Connect to the DBUS bus and send a broadcast signal
*/
int sendsignal(const char* siginterfacename, const char* signame, const void* sigvalue)
{
DBusMessage* msg;
DBusMessageIter args;
DBusConnection* conn;
DBusError err;
int ret;
dbus_uint32_t serial = 0;
printf("Sending signal...\n");
// initialise the error value
dbus_error_init(&err);
// connect to the DBUS system bus, and check for errors
conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Connection Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (NULL == conn) {
return DBUS_GET_CONNECTION_ERROR;
}
// register our name on the bus, and check for errors
ret = dbus_bus_request_name(conn, "test.signal.source", DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Name Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
return DBUS_REQUSET_NAME_ERROR;
}
// create a signal & check for errors
msg = dbus_message_new_signal("/test/signal/Object", // object name of the signal
siginterfacename, // interface name of the signal
signame); // name of the signal
if (NULL == msg)
{
fprintf(stderr, "Message Null\n");
return DBUS_MESSAGE_NEW_SIGNAL_ERROR;
}
// append arguments onto signal
dbus_message_iter_init_append(msg, &args);
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &sigvalue)) {
fprintf(stderr, "Out Of Memory!\n");
return DBUS_MESSAGE_ITER_APPEND_BASIC_ERROR;
}
// send the message and flush the connection
if (!dbus_connection_send(conn, msg, &serial)) {
fprintf(stderr, "Out Of Memory!\n");
return DBUS_SEND_MESSAGE_ERROR;
}
dbus_connection_flush(conn);
printf("Signal Sent\n");
// free the message
dbus_message_unref(msg);
return DBUS_NO_ERROR;
}
/*
* Listens for signals on the bus
*/
int receive(const char* siginterfacename, const char* signame, void** recvsigvalue)
{
DBusMessage* msg;
DBusMessageIter args;
DBusConnection* conn;
DBusError err;
char dbus_match[BUFSIZE];
int ret;
//char *sigvalue;
int flag = 0;
printf("Listening for signals\n");
// initialise the errors
dbus_error_init(&err);
// connect to the bus and check for errors
conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Connection Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (NULL == conn) {
return DBUS_GET_CONNECTION_ERROR;
}
// request our name on the bus and check for errors
ret = dbus_bus_request_name(conn, "test.signal.sink", DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Name Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
return DBUS_REQUSET_NAME_ERROR;
}
// add a rule for which messages we want to see
sprintf(dbus_match, "type='signal',interface='%s'", siginterfacename);
dbus_bus_add_match(conn, dbus_match, &err); // see signals from the given interface
dbus_connection_flush(conn);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Match Error (%s)\n", err.message);
return DBUS_ADD_MATCH_ERROR;
}
printf("Match rule sent\n");
// loop listening for signals being emmitted
while (true) {
// non blocking read of the next available message
dbus_connection_read_write(conn, 0);
msg = dbus_connection_pop_message(conn);
// loop again if we haven't read a message
if (NULL == msg) {
sleep(1);
continue;
}
printf("listen...\n");
// check if the message is a signal from the correct interface and with the correct name
if (dbus_message_is_signal(msg, siginterfacename, signame)) {
printf("signal has found\n");
// read the parameters
if (!dbus_message_iter_init(msg, &args))
fprintf(stderr, "Message Has No Parameters\n");
else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args))
fprintf(stderr, "Argument is not string!\n");
else
dbus_message_iter_get_basic(&args, recvsigvalue);
printf("Got Signal with value successd\n");
flag = 1;
}
// free the message
dbus_message_unref(msg);
if (flag) break;
}
return DBUS_NO_ERROR;
}
好像在发送数据时候的 dbus_message_is_signal(msg, siginterfacename, signame) 堵塞了, 相同的代码char*数据就没有堵塞。 有高人能看出问题, 或者给个传送结构题数据的示例代码吗?