summaryrefslogtreecommitdiff
path: root/frameworks/middleware/examples/dbus/ping-client.c
blob: ceb3205bc78611779414c9b29b1a83ab15912a9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <glib.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <time.h>

static gboolean send_ping (DBusConnection *bus);
static const char *v_STRING;

int main (int argc, char **argv)
{
  GMainLoop *loop;
  DBusConnection *bus;
  DBusError error;

  if (argc > 1)
    v_STRING = argv[1];
  else
    v_STRING = "no arg given";

  /* 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, (GSourceFunc)send_ping, bus);

  /* Start the event loop */
  g_main_loop_run (loop);
  return 0;
}

static gboolean send_ping (DBusConnection *bus)
{
  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("%d:%d\n%d:%d\n\n", tx_time.tv_sec, tx_time.tv_nsec/1000,
    done_time.tv_sec, done_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;
}