diff options
| author | John Ogness <john.ogness@linutronix.de> | 2018-02-20 15:20:47 +0100 |
|---|---|---|
| committer | John Ogness <john.ogness@linutronix.de> | 2018-02-20 15:20:47 +0100 |
| commit | ffce744d7d8a0f7a6846164bfc16fc0753683127 (patch) | |
| tree | c8fd231f4dc43cf52de4d830356090bfd5032e23 | |
| parent | 021b32acd3cec8e255dd47681939797ec64b8206 (diff) | |
tools: add ipc dbus example
| -rw-r--r-- | schulung_tools/ipc_dbus/AUTHORS | 0 | ||||
| -rw-r--r-- | schulung_tools/ipc_dbus/ChangeLog | 0 | ||||
| -rw-r--r-- | schulung_tools/ipc_dbus/Makefile.am | 9 | ||||
| -rw-r--r-- | schulung_tools/ipc_dbus/NEWS | 0 | ||||
| -rw-r--r-- | schulung_tools/ipc_dbus/README | 13 | ||||
| -rwxr-xr-x | schulung_tools/ipc_dbus/autogen.sh | 3 | ||||
| -rw-r--r-- | schulung_tools/ipc_dbus/configure.ac | 24 | ||||
| -rw-r--r-- | schulung_tools/ipc_dbus/ping-client.c | 87 | ||||
| -rw-r--r-- | schulung_tools/ipc_dbus/ping-server.c | 76 |
9 files changed, 212 insertions, 0 deletions
diff --git a/schulung_tools/ipc_dbus/AUTHORS b/schulung_tools/ipc_dbus/AUTHORS new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/schulung_tools/ipc_dbus/AUTHORS diff --git a/schulung_tools/ipc_dbus/ChangeLog b/schulung_tools/ipc_dbus/ChangeLog new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/schulung_tools/ipc_dbus/ChangeLog diff --git a/schulung_tools/ipc_dbus/Makefile.am b/schulung_tools/ipc_dbus/Makefile.am new file mode 100644 index 0000000..d33b83c --- /dev/null +++ b/schulung_tools/ipc_dbus/Makefile.am @@ -0,0 +1,9 @@ +bin_PROGRAMS = pingserver pingclient + +pingserver_SOURCES = ping-server.c +pingserver_CFLAGS = $(DBUS_CFLAGS) +pingserver_LDADD = $(DBUS_LIBS) + +pingclient_SOURCES = ping-client.c +pingclient_CFLAGS = $(DBUS_CFLAGS) +pingclient_LDADD = $(DBUS_LIBS) diff --git a/schulung_tools/ipc_dbus/NEWS b/schulung_tools/ipc_dbus/NEWS new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/schulung_tools/ipc_dbus/NEWS diff --git a/schulung_tools/ipc_dbus/README b/schulung_tools/ipc_dbus/README new file mode 100644 index 0000000..f670f7e --- /dev/null +++ b/schulung_tools/ipc_dbus/README @@ -0,0 +1,13 @@ +# pin current shell to CPU1 +tasket -p 2 $$ + +# start pingserver in the background +# (should start dbus-daemon session) +./pingserver & + +# make sure dbus-daemon is also pinned to CPU1 +taskset -p 2 `pidof dbus-daemon` + +# send 1MiB messages to the pingserver via dbus +# (take note of the timestamps) +./pingclient 1048576 diff --git a/schulung_tools/ipc_dbus/autogen.sh b/schulung_tools/ipc_dbus/autogen.sh new file mode 100755 index 0000000..06467b3 --- /dev/null +++ b/schulung_tools/ipc_dbus/autogen.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +exec autoreconf -v -f -i diff --git a/schulung_tools/ipc_dbus/configure.ac b/schulung_tools/ipc_dbus/configure.ac new file mode 100644 index 0000000..bf99b42 --- /dev/null +++ b/schulung_tools/ipc_dbus/configure.ac @@ -0,0 +1,24 @@ +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +AC_PREREQ([2.69]) +AC_INIT([dbus-example], [1.0], [manut@linutronix.de]) +AC_CONFIG_SRCDIR([ping-server.c]) +AM_INIT_AUTOMAKE([dist-bzip2]) +AC_CONFIG_HEADERS([config.h]) + +# Checks for programs. +AC_PROG_CC + +# Checks for libraries. +PKG_CHECK_MODULES([DBUS], [dbus-glib-1]) +# Checks for header files. + +# Checks for typedefs, structures, and compiler characteristics. + +# Checks for library functions. +AC_CHECK_FUNCS([clock_gettime]) + +AC_CONFIG_FILES([Makefile]) + +AC_OUTPUT 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; +} 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; +} |
