1.dbus通信发送数据的代码例子:
(1)//dbus-send-hello.c
-
cc $(pkg-config --cflags --libs dbus-glib-1) -o dbus-send-hello dbus-send-hello.c && ./dbus-send-hello
-
*/
-
-
int main (int argc, char *argv[])
-
{
-
DBusError dberr;
-
DBusConnection *dbconn;
-
DBusMessage *dbmsg;
-
char *text;
-
-
dbus_error_init (&dberr);
-
dbconn = dbus_bus_get (DBUS_BUS_SESSION, &dberr); //代表连接到会话总线,如果需要连接到
-
//系统总线,简单把DBUS_BUS_SESSION替换为DBUS_BUS_SYSTEM即可。但是有麻烦,因为
-
//系统总线对可以连接到它的用户有限制。可能需要提供一个.service文件指明能连接到某一个指定服务器的权限
-
if (dbus_error_is_set (&dberr)) {
-
fprintf (stderr, "getting session bus failed: %s\n", dberr.message);
-
dbus_error_free (&dberr);
-
return EXIT_FAILURE;
-
}
-
-
dbmsg = dbus_message_new_signal ("/com/wiley/test",
-
"com.wiley.test",
-
"TestSignal");
-
/* 函数原型是
-
DBusMessage* dbus_message_new_signal (const char *path,
-
const char *interface,
-
const char *name) */
-
-
if (dbmsg == NULL) {
-
fprintf (stderr, "Could not create a new signal\n");
-
return EXIT_FAILURE;
-
}
-
-
text = "Hello World";
-
dbus_message_append_args (dbmsg, DBUS_TYPE_STRING, &text, DBUS_TYPE_INVALID);
-
-
dbus_connection_send (dbconn, dbmsg, NULL);
-
printf ("Sending signal to D-Bus\n");
-
-
dbus_message_unref (dbmsg);
-
-
dbus_connection_unref (dbconn);
-
-
return EXIT_SUCCESS;
-
}
(2)重要代码片段
-
const char *v_STRING = "Hello World";
-
dbus_message_append_args (message,
-
DBUS_TYPE_INT32, &v_INT32,
-
DBUS_TYPE_STRING, &v_STRING,
-
DBUS_TYPE_INVALID);
-
const dbus_int32_t array[] = { 1, 2, 3 };
-
const dbus_int32_t *v_ARRAY = array;
-
dbus_message_append_args (message,
-
DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, &v_ARRAY, 3,
-
DBUS_TYPE_INVALID);
-
dbus_bool_t dbus_connection_send ( DBusConnection * connection,
-
DBusMessage * message,
-
dbus_uint32_t * serial
-
)
-
connection the connection.
-
message the message to write.
-
serial return location for message serial, or NULL if you don't care
2.dbus通信接收数据例子:
-
#include <dbus/dbus.h>
-
#include <stdlib.h>
-
#include <stdio.h>
-
-
/*
-
cc $(pkg-config --cflags --libs dbus-glib-1) -o dbus-get-hello dbus-get-hello.c && ./dbus-get-hello
-
*/
-
-
static DBusHandlerResult
-
filter_func (DBusConnection *connection,
-
DBusMessage *message,
-
void *user_data)
-
{
-
dbus_bool_t handled = FALSE;
-
char *signal_text = NULL;
-
-
if (dbus_message_is_signal (message, "com.wiley.test", "TestSignal")) {
-
DBusError dberr;
-
-
dbus_error_init (&dberr);
-
dbus_message_get_args (message, &dberr, DBUS_TYPE_STRING, &signal_text, DBUS_TYPE_INVALID);
-
if (dbus_error_is_set (&dberr)) {
-
fprintf (stderr, "Error getting message args: %s", dberr.message);
-
dbus_error_free (&dberr);
-
} else {
-
DBusConnection *dbconn = (DBusConnection*) user_data;
-
-
printf ("Received TestSignal with value of: '%s'\n", signal_text);
-
-
handled = TRUE;
-
}
-
}
-
-
return (handled ? DBUS_HANDLER_RESULT_HANDLED : DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
-
}
-
-
-
int main (int argc, char *argv[])
-
{
-
DBusError dberr;
-
DBusConnection *dbconn;
-
-
dbus_error_init (&dberr);
-
dbconn = dbus_bus_get (DBUS_BUS_SESSION, &dberr);
-
if (dbus_error_is_set (&dberr)) {
-
fprintf (stderr, "getting session bus failed: %s\n", dberr.message);
-
dbus_error_free (&dberr);
-
return EXIT_FAILURE;
-
}
-
-
dbus_bus_request_name (dbconn, "com.wiley.test",
-
DBUS_NAME_FLAG_REPLACE_EXISTING, &dberr);
-
if (dbus_error_is_set (&dberr)) {
-
fprintf (stderr, "requesting name failed: %s\n", dberr.message);
-
dbus_error_free (&dberr);
-
return EXIT_FAILURE;
-
}
-
-
if (!dbus_connection_add_filter (dbconn, filter_func, NULL, NULL))
-
return EXIT_FAILURE;
-
-
dbus_bus_add_match (dbconn,
-
"type='signal',interface='com.wiley.test'",
-
&dberr);
-
-
if (dbus_error_is_set (&dberr)) {
-
fprintf (stderr, "Could not match: %s", dberr.message);
-
dbus_error_free (&dberr);
-
return EXIT_FAILURE;
-
}
-
-
-
while (dbus_connection_read_write_dispatch (dbconn, -1))
-
; /* empty loop body */
-
-
-
return EXIT_SUCCESS;
-
}
(2)//重要代码片段
-
dbus_bool_t dbus_connection_add_filter ( DBusConnection * connection,
-
DBusHandleMessageFunction function,
-
void * user_data,
-
DBusFreeFunction free_data_function
-
)
-
// connection the connection
-
// function function to handle messages
-
// user_data user data to pass to the function
-
// free_data_function function to use for freeing user data
-
-
//Adds a message filter.
-
-
if (!dbus_connection_add_filter (dbconn, filter_func, NULL, NULL))
-
return EXIT_FAILURE;
-
dbus_bus_add_match (dbconn,
-
"type='signal',interface='com.wiley.test'",
-
&dberr);
-
-
if (dbus_error_is_set (&dberr)) {
-
fprintf (stderr, "Could not match: %s", dberr.message);
-
dbus_error_free (&dberr);
-
return EXIT_FAILURE;
-
}
-
dbus_bool_t dbus_connection_read_write_dispatch ( DBusConnection * connection,
-
int timeout_milliseconds
-
)
-
-
//This function is intended for use with applications that don't want to //write a main loop and deal with DBusWatch and DBusTimeout.
-
-
// An example usage would be:
-
-
while (dbus_connection_read_write_dispatch (connection, -1))
-
; // empty loop body
参考链接:
http://dash1982.javaeye.com/?show_full=true