[Foxp][Test] simple echo test with custom payload size
[platform/upstream/dbus.git] / samsung_tools / test_foxp_latency / ping-client.c
1 #include <stdio.h>
2
3 #include <dbus/dbus.h>
4
5 #include <string.h>
6 #include <sys/time.h>
7 #include <stdlib.h>
8
9 #define DBUS_NAME "com.samsung.pingpong"
10 #define DBUS_PATH "/com/samsung/pingpong"
11 #define DBUS_IFACE "com.samsung.pingpong"
12
13 DBusConnection *dbus_conn;
14 struct timeval tv_start, tv_end;
15 unsigned int message_serial;
16 long int iterations = 0;
17 long int avg = 0;
18 long int sum = 0;
19 static char* ping = "pingping";
20 int MSG_SIZE  = 1024*1024;
21 //#define MSG_SIZE 3 * 1024 * 1024
22
23 void
24 shutdown_dbus ()
25 {
26         if (dbus_conn) {
27                 dbus_connection_close (dbus_conn);
28         }
29 }
30
31 DBusHandlerResult handler(DBusConnection *conn, DBusMessage *msg, void *user_data)
32 {
33         //char buffer[1024];
34         DBusError error;
35         const char *dbus_data;
36         long int delta = 0;
37         DBusMessage *message;
38         
39         if (dbus_message_get_reply_serial (msg) != message_serial) {
40                 return DBUS_HANDLER_RESULT_HANDLED;
41         }
42
43         dbus_error_init (&error);
44         if(!dbus_message_get_args (msg,&error,DBUS_TYPE_STRING,&dbus_data,DBUS_TYPE_INVALID))   {
45                 fprintf (stderr,"error: %s\n",error.message);
46                 return -1;
47         } else {
48                 if(dbus_data[5] != 'v') {
49                   fprintf (stderr,"error: string content not right! \n");
50                   return -1;
51                 }
52                   
53                 gettimeofday (&tv_end, NULL);
54                 delta = (1000000*tv_end.tv_sec + tv_end.tv_usec) - (1000000*tv_start.tv_sec + tv_start.tv_usec);
55                 //printf ("delta: %ld us\n", delta);    
56                 sum += delta;
57                 iterations++;
58                 //if(iterations == 10) {
59                         avg = sum / iterations;
60                         printf ("avg RTT: %ld us\n", avg);
61                         shutdown_dbus ();
62                 //}
63                 gettimeofday (&tv_start, NULL);
64                 message = dbus_message_new_method_call (DBUS_NAME, DBUS_PATH, DBUS_IFACE, "PING");
65                 dbus_message_append_args (message, DBUS_TYPE_STRING, &ping, DBUS_TYPE_INVALID);
66                 dbus_connection_send (dbus_conn, message, &message_serial);
67                 dbus_message_unref (message);
68         }
69         return DBUS_HANDLER_RESULT_HANDLED;
70 }
71
72 int
73 init_dbus ()
74 {
75         DBusError error;
76         dbus_error_init (&error);
77         
78         dbus_conn = dbus_bus_get_private(DBUS_BUS_SESSION, &error);
79
80         if (dbus_error_is_set (&error)) {
81                 fprintf (stderr, "Couldn't initialize DBus: %s\n", error.message);
82
83                 return -1;
84         }
85
86         return 0;
87 }
88
89 int
90 main (int argc, char **argv)
91 {
92         
93         DBusMessage *message;
94         int i;
95         
96         if (argc > 1)
97                 MSG_SIZE = atoi(argv[1]);
98         
99         if (init_dbus () < 0) {
100                 fprintf (stderr, "Cannot initialize DBus\n");
101                 return 1;
102         }
103
104         dbus_connection_add_filter (dbus_conn, handler, NULL, NULL);
105
106         ping = malloc(MSG_SIZE);
107         for(i = 0; i < MSG_SIZE; i++) ping[i] = 'v';
108         ping[MSG_SIZE-1] = '\0';
109         //printf("MSG_SIZE: %i\n", MSG_SIZE);
110         
111         message = dbus_message_new_method_call (DBUS_NAME, DBUS_PATH, DBUS_IFACE, "PING");
112         dbus_message_append_args (message, DBUS_TYPE_STRING, &ping, DBUS_TYPE_INVALID);
113         dbus_connection_send (dbus_conn, message, &message_serial);
114
115         gettimeofday (&tv_start, NULL);
116
117         dbus_message_unref (message);
118         while (dbus_connection_read_write (dbus_conn, -1)) {
119                 while (dbus_connection_dispatch (dbus_conn) != DBUS_DISPATCH_COMPLETE) {
120                 }
121         }
122         free(ping);
123
124         return 0;
125 }