Tizen 2.4 SDK Rev6 Release
[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 #define _GNU_SOURCE
23 #include <stdio.h>
24 #include <glib.h>
25 #include "aul_dbus.h"
26 #include "aul.h"
27 #include <sys/time.h>
28 #include <stdlib.h>
29 #include <bundle_internal.h>
30
31 #define MAX_LOCAL_BUFSZ 128
32
33 gboolean result_func(gpointer data)
34 {
35         char *str;
36         DBusMessage *reply;
37         DBusConnection *bus;
38         DBusError error;
39         DBusMessage *msg;
40
41         msg = data;
42
43         dbus_error_init(&error);
44
45         bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
46
47         reply = dbus_message_new_method_return(msg);
48         dbus_message_unref(msg);
49         str = "picture0.jpg";
50
51         dbus_message_append_args(reply, DBUS_TYPE_STRING,
52                                  &str, DBUS_TYPE_INVALID);
53
54         if (dbus_connection_send(bus, reply, NULL) == FALSE)
55                 _E("Failed to reply");
56
57         dbus_message_unref(reply);
58
59         return 0;
60 }
61
62 void create_take_picture_ui(DBusMessage *msg)
63 {
64         /* simulate, take photo and save time is 30 sec.*/
65         g_timeout_add(5 * 1000, result_func, msg);
66 }
67
68 static DBusHandlerResult
69 dbus_handler(DBusConnection *connection,
70              DBusMessage *message, void *user_data)
71 {
72         DBusMessage *msg;
73         char *s;
74         DBusError error;
75         struct timeval tv;
76         struct timeval cur;
77         struct timeval res;
78
79         if (dbus_message_is_method_call(message, INTERFACE_NAME, METHOD_NAME)) {
80                 dbus_error_init(&error);
81                 msg = dbus_message_ref(message);
82                 /* real draw */
83                 if (dbus_message_get_args
84                     (message, &error, DBUS_TYPE_STRING, &s,
85                      DBUS_TYPE_INVALID)) {
86                         sscanf(s, "%ld/%ld", &tv.tv_sec, &tv.tv_usec);
87                         gettimeofday(&cur, NULL);
88                         timersub(&cur, &tv, &res);
89                         printf("=================================\n");
90                         printf("launched time = %ld sec %ld msec\n", res.tv_sec,
91                                res.tv_usec / 1000);
92                         printf("=================================\n");
93                 }
94                 create_take_picture_ui(msg);
95                 return DBUS_HANDLER_RESULT_HANDLED;
96         }
97         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
98 }
99
100 static int init_dbus()
101 {
102         DBusConnection *bus;
103         DBusError error;
104         char rules[MAX_LOCAL_BUFSZ];
105
106         dbus_error_init(&error);
107
108         bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
109         if (!bus)
110                 _E("couldn't access session bus");
111
112         dbus_connection_setup_with_g_main(bus, NULL);
113
114         dbus_bus_request_name(bus, SERVICE_NAME, 0, &error);
115         if (dbus_error_is_set(&error))
116                 _E("couldn't request name");
117
118         snprintf(rules, MAX_LOCAL_BUFSZ,
119                  "path='%s',type='method_call',interface='%s'",
120                  PATH_NAME, INTERFACE_NAME);
121         dbus_bus_add_match(bus, rules, &error);
122
123         if (!dbus_connection_add_filter(bus, dbus_handler, NULL, NULL))
124                 _E("couldn't add filter");
125
126         return 0;
127 }
128
129 static int aul_handler(aul_type type, bundle *kb, void *data)
130 {
131         const char *tmp;
132         struct timeval tv;
133         struct timeval cur;
134         struct timeval res;
135
136         switch (type) {
137         case AUL_START:
138                 printf("app start & add dbus\n");
139                 tmp = bundle_get_val(kb, AUL_K_STARTTIME);
140                 if (tmp != NULL) {
141                         sscanf(tmp, "%ld/%ld", &tv.tv_sec, &tv.tv_usec);
142                         gettimeofday(&cur, NULL);
143                         timersub(&cur, &tv, &res);
144                         printf("=================================\n");
145                         printf("launched time by aul= %ld sec %ld msec\n",
146                                res.tv_sec, res.tv_usec / 1000);
147                         printf("=================================\n");
148                 }
149                 /* DONOT draw if auto-activated by dbus */
150                 init_dbus();
151                 break;
152         case AUL_RESUME:
153                 break;
154         case AUL_TERMINATE:
155                 exit(0);
156                 break;
157         default:
158                 break;
159         }
160         return 0;
161 }
162
163 int main(int argc, char *argv[])
164 {
165         GMainLoop *loop;
166
167         loop = g_main_loop_new(NULL, FALSE);
168
169         if (aul_launch_init(aul_handler, NULL) != AUL_R_OK)
170                 return -1;
171
172         if (aul_launch_argv_handler(argc, argv) < 0)
173                 return -1;
174
175         g_main_loop_run(loop);
176
177         return 0;
178 }
179