summaryrefslogtreecommitdiff
path: root/schulung_tools/ipc_dbus/ping-server.c
diff options
context:
space:
mode:
Diffstat (limited to 'schulung_tools/ipc_dbus/ping-server.c')
-rw-r--r--schulung_tools/ipc_dbus/ping-server.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/schulung_tools/ipc_dbus/ping-server.c b/schulung_tools/ipc_dbus/ping-server.c
new file mode 100644
index 0000000..78e53c5
--- /dev/null
+++ b/schulung_tools/ipc_dbus/ping-server.c
@@ -0,0 +1,76 @@
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus-glib-lowlevel.h>
+#include <time.h>
+#include <string.h>
+
+static DBusHandlerResult signal_filter(DBusConnection *connection,
+ DBusMessage *message, void *user_data)
+{
+ /* User data is the event loop we are running in */
+ GMainLoop *loop = user_data;
+
+ /* A signal from the bus saying we are about to be disconnected */
+ if (dbus_message_is_signal(message, "org.freedesktop.Local",
+ "Disconnected")) {
+ /* Tell the main loop to quit */
+ g_main_loop_quit (loop);
+ /* We have handled this message, don't pass it on */
+ return DBUS_HANDLER_RESULT_HANDLED;
+
+ } else if (dbus_message_is_signal(message, "de.linutronix.Ping",
+ "Ping")) {
+ struct timespec rx_time;
+ DBusError error;
+ char *s;
+
+ clock_gettime(CLOCK_MONOTONIC, &rx_time);
+
+ dbus_error_init(&error);
+
+ if (dbus_message_get_args(message, &error, DBUS_TYPE_STRING,
+ &s, DBUS_TYPE_INVALID)) {
+ g_print("%ld.%06ld: ping received: %d bytes\n",
+ rx_time.tv_sec, rx_time.tv_nsec / 1000,
+ strlen(s) + 1);
+ } else {
+ g_print("%ld.%06ld: ping received, but error"
+ " getting message: %s\n",
+ rx_time.tv_sec, rx_time.tv_nsec / 1000,
+ error.message);
+ dbus_error_free(&error);
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+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 messages from all objects as no path is specified */
+ dbus_bus_add_match(bus,
+ "type='signal',interface='de.linutronix.Ping'",
+ &error);
+ dbus_connection_add_filter(bus, signal_filter, loop, NULL);
+ g_main_loop_run (loop);
+
+ return 0;
+}