分类: LINUX
2010-01-29 01:21:55
上面的例子看起来有些老了,在ubuntu 8.10上已经编译不起来了, 稍微修改了下
send:
先连接到dbus, 然后构造一个message, 发送出去
#include #include #include #define PING_OBJECT_PATH "/com/burtonini/dbus/ping" #define PING_INTERFACE "com.burtonini.dbus.Signal" static gboolean send_ping(DBusConnection *bus); int main ( int argc, char *argv[] ) { GMainLoop *loop; DBusConnection *bus; DBusError error; loop = g_main_loop_new(NULL, FALSE); dbus_error_init(&error); bus = dbus_bus_get(DBUS_BUS_SESSION, &error); if (!bus) { g_warning("Failed to connect to the D-BUS daemon: %s", error.message); dbus_error_free(&error); return 1; } /*set up this connection to work in a GLib event loop*/ dbus_connection_setup_with_g_main(bus, NULL); g_timeout_add(1000, (GSourceFunc) send_ping, bus); g_main_loop_run(loop); return 0; } /* ---------- end of function main ---------- */ static gboolean send_ping ( DBusConnection *bus ) { DBusMessage *message; message = dbus_message_new_signal (PING_OBJECT_PATH, PING_INTERFACE, "Ping"); if (!message) { g_error("create signal message error\n"); return FALSE; } g_print("start to append arguments\n"); char *msg = "Ping!"; dbus_message_append_args(message, DBUS_TYPE_STRING, &msg, DBUS_TYPE_INVALID); /* Send the signal */ dbus_connection_send(bus, message, NULL); dbus_message_unref(message); g_print("Ping!\n"); /* Return TRUE to tell the event loop we want to be called again*/ return TRUE; } /* ----- end of function send_ping ----- */ |
static DBusHandlerResult signal_filter ( DBusConnection *connection, DBusMessage *message, void *user_da int main ( int argc, char *argv[] ) { GMainLoop *loop; DBusConnection *bus; DBusError error; loop = g_main_loop_new(NULL, FALSE); dbus_error_init(&error); bus = dbus_bus_get(DBUS_BUS_SESSION, &error); if (!bus) { g_warning("Failed to connect to the D-BUS daemon %s", error.message); dbus_error_free(&error); return 1; } dbus_connection_setup_with_g_main(bus, NULL); /* listening to message from all objects as no path is specified*/ dbus_bus_add_match(bus, "type='signal', interface='com.burtonini.dbus.Signal'", NULL); dbus_connection_add_filter(bus, signal_filter, loop, NULL); g_main_loop_run(loop); return 0; } /* ---------- end of function main ---------- */ static DBusHandlerResult signal_filter ( DBusConnection *connection, DBusMessage *message, void *user_da { GMainLoop *loop = user_da if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) { g_main_loop_quit(loop); return DBUS_HANDLER_RESULT_HANDLED; } else if (dbus_message_is_signal(message, "com.burtonini.dbus.Signal", "Ping")) { DBusError error; char *s; dbus_error_init(&error); if (dbus_message_get_args(message, &error, DBUS_TYPE_STRING, &s, DBUS_TYPE_INVALID)) { g_print("Ping received: %s\n", s); /* It seems you needn't to destory the value here, it just comes from the message, but who is responsible for destroying the value? */ // dbus_free(s); } else { g_print("Ping received, but error getting message %s\n", error.message); dbus_error_free(&error); } return DBUS_HANDLER_RESULT_HANDLED; } return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } /* ----- end of function signal_filter ----- */ |