[Foxp][Test] simple echo test with custom payload size
[platform/upstream/dbus.git] / samsung_tools / test_foxp_latency / ping-server.c
1 //gcc -o server serwer.c -Wall -g -O0 `pkg-config --cflags --libs dbus-1`
2
3 #include <dbus/dbus.h>
4 #include <stdio.h>
5
6 #include <errno.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10
11 #define DBUS_NAME "com.samsung.pingpong"
12 #define DBUS_PATH "/com/samsung/pingpong"
13 #define DBUS_IFACE "com.samsung.pingpong"
14
15 DBusConnection *dbus_conn;
16 DBusObjectPathVTable *dbus_vtable;
17
18 static DBusHandlerResult
19 handler_function(DBusConnection *conn, DBusMessage *msg, void *user_data)
20 {
21         DBusMessage *reply;
22
23         DBusError error;
24         dbus_error_init(&error);
25
26         char * ping;
27
28         if (!dbus_message_get_args ( msg,
29                                         &error,
30                                         DBUS_TYPE_STRING,
31                                         &ping,
32                                         DBUS_TYPE_INVALID)) 
33         {
34                 fprintf(stderr, "Error - Invalid ping message!");
35                 reply = dbus_message_new_error(msg, "com.misiek.pingpong.PingError","ping message corrupted");
36         } else {
37                 //printf ("Received from client%s\n", ping);
38                 reply = dbus_message_new_method_return(msg);
39                 dbus_message_append_args (reply, DBUS_TYPE_STRING, &ping, DBUS_TYPE_INVALID);
40         }
41         dbus_connection_send(dbus_conn, reply, NULL);
42         dbus_message_unref(reply);
43
44         return DBUS_HANDLER_RESULT_HANDLED;
45 }
46
47 int
48 init_dbus()
49 {
50         DBusError error;
51         int flag;
52         dbus_error_init(&error);
53
54         dbus_conn = dbus_bus_get_private(DBUS_BUS_SESSION,&error);
55
56         if(dbus_error_is_set(&error)) 
57         {
58                 fprintf(stderr,"Error- could not initizalize dbus session: %s \n", error.message);
59                 return -1;
60         }
61
62         switch(flag = dbus_bus_request_name(dbus_conn, DBUS_NAME, DBUS_NAME_FLAG_DO_NOT_QUEUE, &error))
63         {
64                 case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
65                 case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
66                         //printf("serwer.c request_name flags %d\n",flag);
67                         //printf("serwer.c Name registered as %s\n",DBUS_NAME);
68                         break;
69                 default:
70                         printf("serwer.c Error - could not request name\n");
71                         return -1;
72         }
73
74         dbus_vtable = malloc(sizeof(DBusObjectPathVTable));
75         dbus_vtable->unregister_function = NULL;
76
77         dbus_vtable->message_function = handler_function;
78
79         if(!dbus_connection_register_object_path(dbus_conn,
80                                                 DBUS_PATH,
81                                                 dbus_vtable,
82                                                 NULL)) 
83         {
84                 printf("Error - could not register object path");
85                 return -1;
86         }
87         
88         return 0;
89
90 }
91
92 void
93 shutdown_dbus ()
94 {
95         if (dbus_conn) {
96                 dbus_connection_close(dbus_conn);
97                 free(dbus_vtable);
98         }
99 }
100
101
102 int 
103 main(int argc, char **argv)
104 {
105         
106         if (init_dbus() < 0) {
107                 fprintf(stderr, "serwer.c Error initializing dbus\n");
108         }
109         fprintf(stderr,"Waiting for clients\n");
110
111         while (dbus_connection_read_write(dbus_conn, -1)) {
112                 while (dbus_connection_dispatch( dbus_conn) != DBUS_DISPATCH_COMPLETE){ 
113                 }
114         }
115
116         shutdown_dbus();
117         return 0;
118 }
119