Upload Tizen2.0 source
[framework/appfw/aul-1.git] / test / dbusapp_test.c
1 /*
2 Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved 
3 PROPRIETARY/CONFIDENTIAL
4 This software is the confidential and proprietary information of 
5 SAMSUNG ELECTRONICS ("Confidential Information"). You agree and acknowledge that 
6 this software is owned by Samsung and you 
7 shall not disclose such Confidential Information and shall 
8 use it only in accordance with the terms of the license agreement 
9 you entered into with SAMSUNG ELECTRONICS.  SAMSUNG make no 
10 representations or warranties about the suitability 
11 of the software, either express or implied, including but not 
12 limited to the implied warranties of merchantability, fitness for 
13 a particular purpose, or non-infringement. 
14 SAMSUNG shall not be liable for any damages suffered by licensee arising out of or 
15 related to this software.
16 */
17
18 #include <stdio.h>
19 #include <glib.h>
20 #include "aul_dbus.h"
21 #include "aul.h"
22 #include <sys/time.h>
23 #include <stdlib.h>
24
25 #define MAX_LOCAL_BUFSZ 128
26
27 gboolean result_func(gpointer data)
28 {
29         char *str;
30         DBusMessage *reply;
31         DBusConnection *bus;
32         DBusError error;
33         DBusMessage *msg;
34
35         msg = data;
36
37         dbus_error_init(&error);
38
39         bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
40
41         reply = dbus_message_new_method_return(msg);
42         dbus_message_unref(msg);
43         str = "picture0.jpg";
44
45         dbus_message_append_args(reply, DBUS_TYPE_STRING,
46                                  &str, DBUS_TYPE_INVALID);
47
48         dbus_connection_send(bus, reply, NULL);
49         dbus_message_unref(reply);
50
51         return 0;
52 }
53
54 void create_take_picture_ui(DBusMessage *msg)
55 {
56         /* simulate, take photo and save time is 30 sec.*/
57         g_timeout_add(5 * 1000, result_func, msg);
58 }
59
60 static DBusHandlerResult
61 dbus_handler(DBusConnection *connection,
62              DBusMessage *message, void *user_data)
63 {
64         DBusMessage *msg;
65         char *s;
66         DBusError error;
67         struct timeval tv;
68         struct timeval cur;
69         struct timeval res;
70
71         if (dbus_message_is_method_call(message, INTERFACE_NAME, METHOD_NAME)) {
72                 dbus_error_init(&error);
73                 msg = dbus_message_ref(message);
74                 /* real draw */
75                 if (dbus_message_get_args
76                     (message, &error, DBUS_TYPE_STRING, &s,
77                      DBUS_TYPE_INVALID)) {
78                         sscanf(s, "%ld/%ld", &tv.tv_sec, &tv.tv_usec);
79                         gettimeofday(&cur, NULL);
80                         timersub(&cur, &tv, &res);
81                         printf("=================================\n");
82                         printf("launched time = %ld sec %ld msec\n", res.tv_sec,
83                                res.tv_usec / 1000);
84                         printf("=================================\n");
85                 }
86                 create_take_picture_ui(msg);
87                 return DBUS_HANDLER_RESULT_HANDLED;
88         }
89         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
90 }
91
92 static int init_dbus()
93 {
94         DBusConnection *bus;
95         DBusError error;
96         char rules[MAX_LOCAL_BUFSZ];
97
98         dbus_error_init(&error);
99
100         bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
101         if (!bus)
102                 _E("couldn't access session bus");
103
104         dbus_connection_setup_with_g_main(bus, NULL);
105
106         dbus_bus_request_name(bus, SERVICE_NAME, 0, &error);
107         if (dbus_error_is_set(&error))
108                 _E("couldn't request name");
109
110         snprintf(rules, MAX_LOCAL_BUFSZ,
111                  "path='%s',type='method_call',interface='%s'",
112                  PATH_NAME, INTERFACE_NAME);
113         dbus_bus_add_match(bus, rules, &error);
114
115         if (!dbus_connection_add_filter(bus, dbus_handler, NULL, NULL))
116                 _E("couldn't add filter");
117
118         return 0;
119 }
120
121 static int aul_handler(aul_type type, bundle *kb, void *data)
122 {
123         const char *tmp;
124         struct timeval tv;
125         struct timeval cur;
126         struct timeval res;
127
128         switch (type) {
129         case AUL_START:
130                 printf("app start & add dbus\n");
131                 tmp = bundle_get_val(kb, AUL_K_STARTTIME);
132                 if (tmp != NULL) {
133                         sscanf(tmp, "%ld/%ld", &tv.tv_sec, &tv.tv_usec);
134                         gettimeofday(&cur, NULL);
135                         timersub(&cur, &tv, &res);
136                         printf("=================================\n");
137                         printf("launched time by aul= %ld sec %ld msec\n",
138                                res.tv_sec, res.tv_usec / 1000);
139                         printf("=================================\n");
140                 }
141                 /* DONOT draw if auto-activated by dbus */
142                 init_dbus();
143                 break;
144         case AUL_RESUME:
145                 break;
146         case AUL_TERMINATE:
147                 exit(0);
148                 break;
149         }
150         return 0;
151 }
152
153 int main(int argc, char *argv[])
154 {
155         GMainLoop *loop;
156
157         loop = g_main_loop_new(NULL, FALSE);
158
159         aul_launch_init(aul_handler, NULL);
160         aul_launch_argv_handler(argc, argv);
161
162         g_main_loop_run(loop);
163
164         return 0;
165 }
166