tizen 2.0 build
[framework/appfw/aul-1.git] / test / dbusapp_test.c
1 /*
2  *  aul
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <glib.h>
24 #include "aul_dbus.h"
25 #include "aul.h"
26 #include <sys/time.h>
27 #include <stdlib.h>
28
29 #define MAX_LOCAL_BUFSZ 128
30
31 gboolean result_func(gpointer data)
32 {
33         char *str;
34         DBusMessage *reply;
35         DBusConnection *bus;
36         DBusError error;
37         DBusMessage *msg;
38
39         msg = data;
40
41         dbus_error_init(&error);
42
43         bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
44
45         reply = dbus_message_new_method_return(msg);
46         dbus_message_unref(msg);
47         str = "picture0.jpg";
48
49         dbus_message_append_args(reply, DBUS_TYPE_STRING,
50                                  &str, DBUS_TYPE_INVALID);
51
52         dbus_connection_send(bus, reply, NULL);
53         dbus_message_unref(reply);
54
55         return 0;
56 }
57
58 void create_take_picture_ui(DBusMessage *msg)
59 {
60         /* simulate, take photo and save time is 30 sec.*/
61         g_timeout_add(5 * 1000, result_func, msg);
62 }
63
64 static DBusHandlerResult
65 dbus_handler(DBusConnection *connection,
66              DBusMessage *message, void *user_data)
67 {
68         DBusMessage *msg;
69         char *s;
70         DBusError error;
71         struct timeval tv;
72         struct timeval cur;
73         struct timeval res;
74
75         if (dbus_message_is_method_call(message, INTERFACE_NAME, METHOD_NAME)) {
76                 dbus_error_init(&error);
77                 msg = dbus_message_ref(message);
78                 /* real draw */
79                 if (dbus_message_get_args
80                     (message, &error, DBUS_TYPE_STRING, &s,
81                      DBUS_TYPE_INVALID)) {
82                         sscanf(s, "%ld/%ld", &tv.tv_sec, &tv.tv_usec);
83                         gettimeofday(&cur, NULL);
84                         timersub(&cur, &tv, &res);
85                         printf("=================================\n");
86                         printf("launched time = %ld sec %ld msec\n", res.tv_sec,
87                                res.tv_usec / 1000);
88                         printf("=================================\n");
89                 }
90                 create_take_picture_ui(msg);
91                 return DBUS_HANDLER_RESULT_HANDLED;
92         }
93         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
94 }
95
96 static int init_dbus()
97 {
98         DBusConnection *bus;
99         DBusError error;
100         char rules[MAX_LOCAL_BUFSZ];
101
102         dbus_error_init(&error);
103
104         bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
105         if (!bus)
106                 _E("couldn't access session bus");
107
108         dbus_connection_setup_with_g_main(bus, NULL);
109
110         dbus_bus_request_name(bus, SERVICE_NAME, 0, &error);
111         if (dbus_error_is_set(&error))
112                 _E("couldn't request name");
113
114         snprintf(rules, MAX_LOCAL_BUFSZ,
115                  "path='%s',type='method_call',interface='%s'",
116                  PATH_NAME, INTERFACE_NAME);
117         dbus_bus_add_match(bus, rules, &error);
118
119         if (!dbus_connection_add_filter(bus, dbus_handler, NULL, NULL))
120                 _E("couldn't add filter");
121
122         return 0;
123 }
124
125 static int aul_handler(aul_type type, bundle *kb, void *data)
126 {
127         const char *tmp;
128         struct timeval tv;
129         struct timeval cur;
130         struct timeval res;
131
132         switch (type) {
133         case AUL_START:
134                 printf("app start & add dbus\n");
135                 tmp = bundle_get_val(kb, AUL_K_STARTTIME);
136                 if (tmp != NULL) {
137                         sscanf(tmp, "%ld/%ld", &tv.tv_sec, &tv.tv_usec);
138                         gettimeofday(&cur, NULL);
139                         timersub(&cur, &tv, &res);
140                         printf("=================================\n");
141                         printf("launched time by aul= %ld sec %ld msec\n",
142                                res.tv_sec, res.tv_usec / 1000);
143                         printf("=================================\n");
144                 }
145                 /* DONOT draw if auto-activated by dbus */
146                 init_dbus();
147                 break;
148         case AUL_RESUME:
149                 break;
150         case AUL_TERMINATE:
151                 exit(0);
152                 break;
153         }
154         return 0;
155 }
156
157 int main(int argc, char *argv[])
158 {
159         GMainLoop *loop;
160
161         loop = g_main_loop_new(NULL, FALSE);
162
163         aul_launch_init(aul_handler, NULL);
164         aul_launch_argv_handler(argc, argv);
165
166         g_main_loop_run(loop);
167
168         return 0;
169 }
170