summaryrefslogtreecommitdiff
path: root/schulung_tools/ipc_dbus/ping-client.c
diff options
context:
space:
mode:
authorJohn Ogness <john.ogness@linutronix.de>2018-02-20 15:20:47 +0100
committerJohn Ogness <john.ogness@linutronix.de>2018-02-20 15:20:47 +0100
commitffce744d7d8a0f7a6846164bfc16fc0753683127 (patch)
treec8fd231f4dc43cf52de4d830356090bfd5032e23 /schulung_tools/ipc_dbus/ping-client.c
parent021b32acd3cec8e255dd47681939797ec64b8206 (diff)
tools: add ipc dbus example
Diffstat (limited to 'schulung_tools/ipc_dbus/ping-client.c')
-rw-r--r--schulung_tools/ipc_dbus/ping-client.c87
1 files changed, 87 insertions, 0 deletions
diff --git a/schulung_tools/ipc_dbus/ping-client.c b/schulung_tools/ipc_dbus/ping-client.c
new file mode 100644
index 0000000..9ebf229
--- /dev/null
+++ b/schulung_tools/ipc_dbus/ping-client.c
@@ -0,0 +1,87 @@
+#include <glib.h>
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus-glib-lowlevel.h>
+#include <time.h>
+#include <stdlib.h>
+#include <malloc.h>
+#include <string.h>
+
+static const char *v_STRING;
+
+static int send_ping(void *bus_ptr)
+{
+ DBusConnection *bus = bus_ptr;
+ DBusMessage *message;
+ struct timespec tx_time;
+ struct timespec done_time;
+
+ message = dbus_message_new_signal("/de/linutronix/Ping",
+ "de.linutronix.Ping", "Ping");
+ /* Append the string to the signal */
+ dbus_message_append_args(message,
+ DBUS_TYPE_STRING, &v_STRING,
+ DBUS_TYPE_INVALID);
+
+ clock_gettime(CLOCK_MONOTONIC, &tx_time);
+
+ /* Send the signal */
+ dbus_connection_send(bus, message, NULL);
+
+ clock_gettime(CLOCK_MONOTONIC, &done_time);
+
+ g_print("%ld.%06ld: ping sent: %d bytes (%ldus)\n",
+ tx_time.tv_sec, tx_time.tv_nsec / 1000, strlen(v_STRING) + 1,
+ ((done_time.tv_sec - tx_time.tv_sec) * 1000000) +
+ (done_time.tv_nsec / 1000) - (tx_time.tv_nsec / 1000));
+
+ /* Free the signal now we have finished with it */
+ dbus_message_unref(message);
+
+ /* Return TRUE to tell the event loop we want to be called again */
+ return TRUE;
+}
+
+int main(int argc, char *argv[])
+{
+ GMainLoop *loop;
+ DBusConnection *bus;
+ DBusError error;
+
+ if (argc > 1) {
+ int val = atoi(argv[1]);
+ char *s;
+ if (val < 1)
+ val = 1;
+ s = malloc(val);
+ memset(s, '#', val);
+ s[val - 1] = 0;
+ v_STRING = s;
+ } else {
+ v_STRING = "default message";
+ }
+
+ /* Create a new event loop to run in */
+ loop = g_main_loop_new(NULL, FALSE);
+
+ /* Get a connection to the session bus */
+ 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);
+
+ /* Every second call send_ping() with the bus as an argument */
+ g_timeout_add(1000, send_ping, bus);
+
+ /* Start the event loop */
+ g_main_loop_run(loop);
+
+ return 0;
+}