Fix memory leaks
[platform/core/uifw/voice-control.git] / client / vc_dbus.c
1 /*
2 * Copyright (c) 2011-2015 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "vc_command.h"
18 #include "vc_dbus.h"
19 #include "vc_main.h"
20
21
22 static int g_waiting_time = 3000;
23
24 static Ecore_Fd_Handler* g_fd_handler = NULL;
25
26 static DBusConnection* g_conn_sender = NULL;
27 static DBusConnection* g_conn_listener = NULL;
28
29 extern int __vc_cb_error(int reason, int daemon_pid, char* msg);
30
31 extern void __vc_cb_result();
32
33 extern int __vc_cb_service_state(int state);
34
35 extern int __vc_cb_manager_pid(int manager_pid);
36
37
38 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
39 {
40         if (NULL == g_conn_listener)    return ECORE_CALLBACK_RENEW;
41
42         dbus_connection_read_write_dispatch(g_conn_listener, 50);
43
44         while (1) {
45                 DBusMessage* msg = NULL;
46                 msg = dbus_connection_pop_message(g_conn_listener);
47
48                 /* loop again if we haven't read a message */
49                 if (NULL == msg) {
50                         break;
51                 }
52
53                 DBusError err;
54                 dbus_error_init(&err);
55
56                 char if_name[64] = {0, };
57                 snprintf(if_name, 64, "%s", VC_CLIENT_SERVICE_INTERFACE);
58
59                 if (dbus_message_is_method_call(msg, if_name, VCD_METHOD_HELLO)) {
60                         SLOG(LOG_DEBUG, TAG_VCC, "@@@ Get Hello");
61                         int pid = 0;
62                         int response = -1;
63
64                         dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &pid, DBUS_TYPE_INVALID);
65
66                         if (dbus_error_is_set(&err)) {
67                                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
68                                 dbus_error_free(&err);
69                         }
70
71                         if (pid > 0) {
72                                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc get hello : pid(%d) ", pid);
73                                 response = 1;
74                         } else {
75                                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc get hello : invalid pid ");
76                         }
77
78                         DBusMessage* reply = NULL;
79                         reply = dbus_message_new_method_return(msg);
80
81                         if (NULL != reply) {
82                                 dbus_message_append_args(reply, DBUS_TYPE_INT32, &response, DBUS_TYPE_INVALID);
83
84                                 if (!dbus_connection_send(g_conn_listener, reply, NULL))
85                                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc get hello : fail to send reply");
86                                 else
87                                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc get hello : result(%d)", response);
88
89                                 dbus_connection_flush(g_conn_listener);
90                                 dbus_message_unref(reply);
91                         } else {
92                                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc get hello : fail to create reply message");
93                         }
94
95                         SLOG(LOG_DEBUG, TAG_VCC, "@@@");
96                 } /* VCD_METHOD_HELLO */
97
98                 else if (dbus_message_is_signal(msg, if_name, VCD_METHOD_SET_SERVICE_STATE)) {
99                         int state = 0;
100
101                         dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID);
102                         if (dbus_error_is_set(&err)) {
103                                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Get arguments error (%s)", err.message);
104                                 dbus_error_free(&err);
105                         }
106
107                         SLOG(LOG_DEBUG, TAG_VCC, "@@ state changed : %d", state);
108
109                         __vc_cb_service_state(state);
110
111                 } /* VCD_METHOD_SET_SERVICE_STATE */
112
113                 else if (dbus_message_is_method_call(msg, if_name, VCD_METHOD_RESULT)) {
114                         SLOG(LOG_DEBUG, TAG_VCC, "@@@ Get Client Result");
115
116                         __vc_cb_result();
117
118                         SLOG(LOG_DEBUG, TAG_VCC, "@@@");
119
120                 } /* VCD_METHOD_RESULT */
121                 else if (dbus_message_is_signal(msg, if_name, VCD_METHOD_SEND_MANAGER_PID)) {
122                         int manager_pid = 0;
123
124                         dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &manager_pid, DBUS_TYPE_INVALID);
125                         if (dbus_error_is_set(&err)) {
126                                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Get arguments error (%s)", err.message);
127                                 dbus_error_free(&err);
128                         }
129
130                         SLOG(LOG_DEBUG, TAG_VCC, "@@ manager pid is changed : %d", manager_pid);
131
132                         __vc_cb_manager_pid(manager_pid);
133
134                 } /* VCD_METHOD_SEND_MANAGER_PID */
135
136                 else if (dbus_message_is_signal(msg, if_name, VCD_METHOD_ERROR)) {
137                         SLOG(LOG_DEBUG, TAG_VCC, "@@@ Get Error");
138                         int reason;
139                         int daemon_pid;
140                         char* err_msg;
141
142                         dbus_message_get_args(msg, &err,
143                                 DBUS_TYPE_INT32, &reason,
144                                 DBUS_TYPE_INT32, &daemon_pid,
145                                 DBUS_TYPE_STRING, &err_msg,
146                                 DBUS_TYPE_INVALID);
147
148                         if (dbus_error_is_set(&err)) {
149                                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc Get Error message : Get arguments error (%s)", err.message);
150                                 dbus_error_free(&err);
151                         } else {
152                                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc Get Error message : reason(%d), daemon_pid(%d), msg(%s)", reason, daemon_pid, err_msg);
153                                 __vc_cb_error(reason, daemon_pid, err_msg);
154                         }
155
156                         SLOG(LOG_DEBUG, TAG_VCC, "@@@");
157                 } /* VCD_METHOD_ERROR */
158
159                 else if (dbus_message_is_signal(msg, "org.freedesktop.DBus", "NameOwnerChanged")) {
160                         SLOG(LOG_DEBUG, TAG_VCC, "@@@ Owner Changed");
161                         DBusError err;
162                         dbus_error_init(&err);
163                         /* remove a rule for daemon error */
164                         char rule_err[256] = {0, };
165                         snprintf(rule_err, 256, "sender='org.freedesktop.DBus',path='/org/freedesktop/DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',type='signal',arg0='%s'", VC_SERVER_SERVICE_INTERFACE);
166                         dbus_bus_remove_match(g_conn_listener, rule_err, &err);
167                         dbus_connection_flush(g_conn_listener);
168
169                         if (dbus_error_is_set(&err)) {
170                                 SLOG(LOG_ERROR, TAG_VCC, "Match Error (%s)", err.message);
171                                 dbus_error_free(&err);
172                         }
173                         __vc_cb_error(VC_ERROR_SERVICE_RESET, -1, "Daemon Reset");
174                         SLOG(LOG_DEBUG, TAG_VCC, "@@@");
175                 } /* NameOwnerChanged */
176
177                 else {
178                         SLOG(LOG_DEBUG, TAG_VCC, "Message is NOT valid");
179                         dbus_message_unref(msg);
180                         break;
181                 }
182
183                 /* free the message */
184                 dbus_message_unref(msg);
185         } /* while(1) */
186
187         return ECORE_CALLBACK_PASS_ON;
188 }
189
190 static void __vc_dbus_connection_free()
191 {
192         if (NULL != g_conn_listener) {
193                 dbus_connection_close(g_conn_listener);
194                 g_conn_listener = NULL;
195         }
196         if (NULL != g_conn_sender) {
197                 dbus_connection_close(g_conn_sender);
198                 g_conn_sender = NULL;
199         }
200 }
201
202 int vc_dbus_open_connection()
203 {
204         if (NULL != g_conn_sender && NULL != g_conn_listener) {
205                 SLOG(LOG_WARN, TAG_VCC, "already existed connection ");
206                 return 0;
207         }
208
209         DBusError err;
210         int ret;
211
212         /* initialise the error value */
213         dbus_error_init(&err);
214
215         /* connect to the DBUS system bus, and check for errors */
216         g_conn_sender = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
217
218         if (dbus_error_is_set(&err)) {
219                 SLOG(LOG_ERROR, TAG_VCC, "Dbus Connection Error (%s)", err.message);
220                 dbus_error_free(&err);
221         }
222
223         if (NULL == g_conn_sender) {
224                 SLOG(LOG_ERROR, TAG_VCC, "Fail to get dbus connection ");
225                 return VC_ERROR_OPERATION_FAILED;
226         }
227
228         g_conn_listener = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
229
230         if (dbus_error_is_set(&err)) {
231                 SLOG(LOG_ERROR, TAG_VCC, "Dbus Connection Error (%s)", err.message);
232                 dbus_error_free(&err);
233         }
234
235         if (NULL == g_conn_listener) {
236                 SLOG(LOG_ERROR, TAG_VCC, "Fail to get dbus connection ");
237                 __vc_dbus_connection_free();
238                 return VC_ERROR_OPERATION_FAILED;
239         }
240
241         int pid = getpid();
242
243         char service_name[64];
244         memset(service_name, '\0', 64);
245         snprintf(service_name, 64, "%s%d", VC_CLIENT_SERVICE_NAME, pid);
246
247         SLOG(LOG_DEBUG, TAG_VCC, "service name is %s", service_name);
248
249         /* register our name on the bus, and check for errors */
250         ret = dbus_bus_request_name(g_conn_listener, service_name, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
251
252         if (dbus_error_is_set(&err)) {
253                 SLOG(LOG_ERROR, TAG_VCC, "Name Error (%s)", err.message);
254                 dbus_error_free(&err);
255         }
256
257         if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
258                 SLOG(LOG_ERROR, TAG_VCC, "fail dbus_bus_request_name()");
259                 __vc_dbus_connection_free();
260                 return -2;
261         }
262
263         if (NULL != g_fd_handler) {
264                 SLOG(LOG_WARN, TAG_VCC, "The handler already exists.");
265                 __vc_dbus_connection_free();
266                 return 0;
267         }
268
269         char rule[128] = {0, };
270         snprintf(rule, 128, "type='signal',interface='%s'", VC_CLIENT_SERVICE_INTERFACE);
271
272         /* add a rule for which messages we want to see */
273         dbus_bus_add_match(g_conn_listener, rule, &err);
274         dbus_connection_flush(g_conn_listener);
275
276         if (dbus_error_is_set(&err)) {
277                 SLOG(LOG_ERROR, TAG_VCC, "Match Error (%s)", err.message);
278                 dbus_error_free(&err);
279                 __vc_dbus_connection_free();
280                 return VC_ERROR_OPERATION_FAILED;
281         }
282
283         int fd = 0;
284         if (1 != dbus_connection_get_unix_fd(g_conn_listener, &fd)) {
285                 SLOG(LOG_ERROR, TAG_VCC, "fail to get fd from dbus ");
286                 __vc_dbus_connection_free();
287                 return VC_ERROR_OPERATION_FAILED;
288         } else {
289                 SLOG(LOG_DEBUG, TAG_VCC, "Get fd from dbus : %d", fd);
290         }
291
292         g_fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)listener_event_callback, g_conn_listener, NULL, NULL);
293         if (NULL == g_fd_handler) {
294                 SLOG(LOG_ERROR, TAG_VCC, "fail to get fd handler from ecore ");
295                 __vc_dbus_connection_free();
296                 return VC_ERROR_OPERATION_FAILED;
297         }
298
299         return 0;
300 }
301
302 int vc_dbus_close_connection()
303 {
304         DBusError err;
305         dbus_error_init(&err);
306
307         if (NULL != g_fd_handler) {
308                 ecore_main_fd_handler_del(g_fd_handler);
309                 g_fd_handler = NULL;
310         }
311
312         int pid = getpid();
313
314         char service_name[64];
315         memset(service_name, '\0', 64);
316         snprintf(service_name, 64, "%s%d", VC_CLIENT_SERVICE_NAME, pid);
317
318         dbus_bus_release_name(g_conn_listener, service_name, &err);
319
320         if (dbus_error_is_set(&err)) {
321                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
322                 dbus_error_free(&err);
323         }
324
325         __vc_dbus_connection_free();
326
327         return 0;
328 }
329
330 int vc_dbus_reconnect()
331 {
332         if (!g_conn_sender || !g_conn_listener) {
333                 vc_dbus_close_connection();
334
335                 if (0 != vc_dbus_open_connection()) {
336                         SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Fail to reconnect");
337                         return -1;
338                 }
339
340                 SLOG(LOG_DEBUG, TAG_VCC, "[DBUS] Reconnect");
341                 return 0;
342         }
343
344         bool sender_connected = dbus_connection_get_is_connected(g_conn_sender);
345         bool listener_connected = dbus_connection_get_is_connected(g_conn_listener);
346         SLOG(LOG_WARN, TAG_VCC, "[DBUS] Sender(%s) Listener(%s)",
347                  sender_connected ? "Connected" : "Not connected", listener_connected ? "Connected" : "Not connected");
348
349         if (false == sender_connected || false == listener_connected) {
350                 vc_dbus_close_connection();
351
352                 if (0 != vc_dbus_open_connection()) {
353                         SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Fail to reconnect");
354                         return -1;
355                 }
356
357                 SLOG(LOG_DEBUG, TAG_VCC, "[DBUS] Reconnect");
358         }
359
360         return 0;
361 }
362
363 static int __dbus_check()
364 {
365         if (NULL == g_conn_sender) {
366                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] NULL connection");
367                 return vc_dbus_reconnect();
368         }
369         return 0;
370 }
371
372 int vc_dbus_request_hello()
373 {
374         if (0 != __dbus_check()) {
375                 return VC_ERROR_OPERATION_FAILED;
376         }
377
378         DBusMessage* msg;
379
380         msg = dbus_message_new_method_call(
381                           VC_SERVER_SERVICE_NAME,
382                           VC_SERVER_SERVICE_OBJECT_PATH,
383                           VC_SERVER_SERVICE_INTERFACE,
384                           VC_METHOD_HELLO);
385
386         if (NULL == msg) {
387                 SLOG(LOG_ERROR, TAG_VCC, "@@ Request vc hello : Fail to make message");
388                 return VC_ERROR_OPERATION_FAILED;
389         }
390
391         DBusError err;
392         dbus_error_init(&err);
393
394         DBusMessage* result_msg = NULL;
395         int result = 0;
396
397         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, 500, &err);
398
399         if (dbus_error_is_set(&err)) {
400                 dbus_error_free(&err);
401         }
402
403         dbus_message_unref(msg);
404
405         if (NULL != result_msg) {
406                 dbus_message_unref(result_msg);
407                 result = 0;
408         } else {
409                 result = VC_ERROR_TIMED_OUT;
410         }
411
412         return result;
413 }
414
415
416 int vc_dbus_request_initialize(int pid, int* mgr_pid, int* service_state, int* daemon_pid)
417 {
418         if (0 != __dbus_check()) {
419                 return VC_ERROR_OPERATION_FAILED;
420         }
421
422         DBusMessage* msg;
423
424         msg = dbus_message_new_method_call(
425                           VC_SERVER_SERVICE_NAME,
426                           VC_SERVER_SERVICE_OBJECT_PATH,
427                           VC_SERVER_SERVICE_INTERFACE,
428                           VC_METHOD_INITIALIZE);
429
430         if (NULL == msg) {
431                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc initialize : Fail to make message ");
432                 return VC_ERROR_OPERATION_FAILED;
433         } else {
434                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc initialize : pid(%d)", pid);
435         }
436
437         dbus_message_append_args(msg,
438                         DBUS_TYPE_INT32, &pid,
439                         DBUS_TYPE_INVALID);
440
441         DBusError err;
442         dbus_error_init(&err);
443
444         DBusMessage* result_msg;
445         int result = VC_ERROR_OPERATION_FAILED;
446
447         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
448         dbus_message_unref(msg);
449
450         if (dbus_error_is_set(&err)) {
451                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
452                 dbus_error_free(&err);
453         }
454
455         if (NULL != result_msg) {
456                 int tmp = -1;
457                 int tmp_service_state = 0;
458                 int tmp_daemon_pid = 0;
459                 dbus_message_get_args(result_msg, &err,
460                         DBUS_TYPE_INT32, &result,
461                         DBUS_TYPE_INT32, &tmp,
462                         DBUS_TYPE_INT32, &tmp_service_state,
463                         DBUS_TYPE_INT32, &tmp_daemon_pid,
464                         DBUS_TYPE_INVALID);
465
466                 if (dbus_error_is_set(&err)) {
467                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
468                         dbus_error_free(&err);
469                         result = VC_ERROR_OPERATION_FAILED;
470                 }
471
472                 dbus_message_unref(result_msg);
473
474                 if (0 == result) {
475                         *mgr_pid = tmp;
476                         *service_state = tmp_service_state;
477                         *daemon_pid = tmp_daemon_pid;
478
479                         /* add a rule for daemon error */
480                         char rule_err[256] = {0, };
481                         snprintf(rule_err, 256, "sender='org.freedesktop.DBus',path='/org/freedesktop/DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',type='signal',arg0='%s'", VC_SERVER_SERVICE_INTERFACE);
482                         dbus_bus_add_match(g_conn_listener, rule_err, &err);
483                         dbus_connection_flush(g_conn_listener);
484
485                         if (dbus_error_is_set(&err)) {
486                                 SLOG(LOG_ERROR, TAG_VCC, "Match Error (%s)", err.message);
487                                 dbus_error_free(&err);
488                                 return VC_ERROR_OPERATION_FAILED;
489                         }
490
491                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc initialize : result = %d mgr = %d service = %d daemon_pid = %d", result, *mgr_pid, *service_state, *daemon_pid);
492                 } else {
493                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc initialize : result = %d", result);
494                 }
495         } else {
496                 SLOG(LOG_ERROR, TAG_VCC, "@@ Result message is NULL ");
497                 vc_dbus_reconnect();
498                 result = VC_ERROR_TIMED_OUT;
499         }
500
501         return result;
502 }
503
504 int vc_dbus_request_finalize(int pid)
505 {
506         DBusError err;
507         dbus_error_init(&err);
508
509         if (0 != __dbus_check()) {
510                 return VC_ERROR_OPERATION_FAILED;
511         }
512
513         /* remove a rule for daemon error */
514         char rule_err[256] = {0, };
515         snprintf(rule_err, 256, "sender='org.freedesktop.DBus',path='/org/freedesktop/DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',type='signal',arg0='%s'", VC_SERVER_SERVICE_INTERFACE);
516         dbus_bus_remove_match(g_conn_listener, rule_err, &err);
517         dbus_connection_flush(g_conn_listener);
518
519         if (dbus_error_is_set(&err)) {
520                 SLOG(LOG_ERROR, TAG_VCC, "Match Error (%s)", err.message);
521                 dbus_error_free(&err);
522         }
523
524         DBusMessage* msg;
525
526         msg = dbus_message_new_method_call(
527                           VC_SERVER_SERVICE_NAME,
528                           VC_SERVER_SERVICE_OBJECT_PATH,
529                           VC_SERVER_SERVICE_INTERFACE,
530                           VC_METHOD_FINALIZE);
531
532         if (NULL == msg) {
533                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc finalize : Fail to make message ");
534                 return VC_ERROR_OPERATION_FAILED;
535         } else {
536                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc finalize : pid(%d)", pid);
537         }
538
539         dbus_message_append_args(msg, DBUS_TYPE_INT32, &pid, DBUS_TYPE_INVALID);
540
541         DBusMessage* result_msg;
542         int result = VC_ERROR_OPERATION_FAILED;
543
544         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
545         dbus_message_unref(msg);
546
547         if (dbus_error_is_set(&err)) {
548                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
549                 dbus_error_free(&err);
550         }
551
552         if (NULL != result_msg) {
553                 dbus_message_get_args(result_msg, &err,
554                                                           DBUS_TYPE_INT32, &result,
555                                                           DBUS_TYPE_INVALID);
556
557                 if (dbus_error_is_set(&err)) {
558                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
559                         dbus_error_free(&err);
560                         result = VC_ERROR_OPERATION_FAILED;
561                 }
562
563                 dbus_message_unref(result_msg);
564
565                 if (0 == result) {
566                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc finalize : result = %d", result);
567                 } else {
568                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc finalize : result = %d", result);
569                 }
570         } else {
571                 SLOG(LOG_ERROR, TAG_VCC, "@@ Result message is NULL ");
572                 vc_dbus_reconnect();
573                 result = VC_ERROR_TIMED_OUT;
574         }
575
576         return result;
577 }
578
579 int vc_dbus_request_set_exclusive_command(int pid, bool value)
580 {
581         if (0 != __dbus_check()) {
582                 return VC_ERROR_OPERATION_FAILED;
583         }
584
585         DBusMessage* msg;
586
587         msg = dbus_message_new_method_call(
588                           VC_SERVER_SERVICE_NAME,
589                           VC_SERVER_SERVICE_OBJECT_PATH,
590                           VC_SERVER_SERVICE_INTERFACE,
591                           VC_METHOD_SET_EXCLUSIVE_CMD);
592
593         if (NULL == msg) {
594                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc set exclusive command : Fail to make message");
595                 return VC_ERROR_OPERATION_FAILED;
596         } else {
597                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc set exclusive command : pid(%d)", pid);
598         }
599
600         int temp = value;
601
602         dbus_message_append_args(msg,
603                                                          DBUS_TYPE_INT32, &pid,
604                                                          DBUS_TYPE_INT32, &temp,
605                                                          DBUS_TYPE_INVALID);
606
607         DBusError err;
608         dbus_error_init(&err);
609
610         DBusMessage* result_msg;
611         int result = VC_ERROR_OPERATION_FAILED;
612
613         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
614         dbus_message_unref(msg);
615
616         if (dbus_error_is_set(&err)) {
617                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
618                 dbus_error_free(&err);
619         }
620
621         if (NULL != result_msg) {
622                 dbus_message_get_args(result_msg, &err,
623                                                           DBUS_TYPE_INT32, &result,
624                                                           DBUS_TYPE_INVALID);
625
626                 if (dbus_error_is_set(&err)) {
627                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
628                         dbus_error_free(&err);
629                         result = VC_ERROR_OPERATION_FAILED;
630                 }
631                 dbus_message_unref(result_msg);
632
633                 if (0 == result) {
634                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc set exclusive command : result = %d", result);
635                 } else {
636                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc set exclusive command : result = %d", result);
637                 }
638         } else {
639                 SLOG(LOG_ERROR, TAG_VCC, "@@ Result message is NULL");
640                 vc_dbus_reconnect();
641                 result = VC_ERROR_TIMED_OUT;
642         }
643
644         return result;
645 }
646
647 int vc_dbus_request_set_command(int pid, vc_cmd_type_e cmd_type)
648 {
649         if (0 != __dbus_check()) {
650                 return VC_ERROR_OPERATION_FAILED;
651         }
652
653         DBusMessage* msg;
654
655         msg = dbus_message_new_method_call(
656                           VC_SERVER_SERVICE_NAME,
657                           VC_SERVER_SERVICE_OBJECT_PATH,
658                           VC_SERVER_SERVICE_INTERFACE,
659                           VC_METHOD_SET_COMMAND);
660
661         if (NULL == msg) {
662                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc set command : Fail to make message");
663                 return VC_ERROR_OPERATION_FAILED;
664         } else {
665                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc set command : pid(%d)", pid);
666         }
667
668         dbus_message_append_args(msg,
669                                                          DBUS_TYPE_INT32, &pid,
670                                                          DBUS_TYPE_INT32, &cmd_type,
671                                                          DBUS_TYPE_INVALID);
672
673         DBusError err;
674         dbus_error_init(&err);
675
676         DBusMessage* result_msg;
677         int result = VC_ERROR_OPERATION_FAILED;
678
679         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
680         dbus_message_unref(msg);
681
682         if (dbus_error_is_set(&err)) {
683                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
684                 dbus_error_free(&err);
685         }
686
687         if (NULL != result_msg) {
688                 dbus_message_get_args(result_msg, &err,
689                                                           DBUS_TYPE_INT32, &result,
690                                                           DBUS_TYPE_INVALID);
691
692                 if (dbus_error_is_set(&err)) {
693                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
694                         dbus_error_free(&err);
695                         result = VC_ERROR_OPERATION_FAILED;
696                 }
697                 dbus_message_unref(result_msg);
698
699                 if (0 == result) {
700                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc set command : result = %d", result);
701                 } else {
702                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc set command : result = %d", result);
703                 }
704         } else {
705                 SLOG(LOG_ERROR, TAG_VCC, "@@ Result message is NULL");
706                 vc_dbus_reconnect();
707                 result = VC_ERROR_TIMED_OUT;
708         }
709
710         return result;
711 }
712
713 int vc_dbus_request_unset_command(int pid, vc_cmd_type_e cmd_type)
714 {
715         if (0 != __dbus_check()) {
716                 return VC_ERROR_OPERATION_FAILED;
717         }
718
719         DBusMessage* msg;
720
721         msg = dbus_message_new_method_call(
722                           VC_SERVER_SERVICE_NAME,
723                           VC_SERVER_SERVICE_OBJECT_PATH,
724                           VC_SERVER_SERVICE_INTERFACE,
725                           VC_METHOD_UNSET_COMMAND);
726
727         if (NULL == msg) {
728                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc unset command : Fail to make message");
729                 return VC_ERROR_OPERATION_FAILED;
730         } else {
731                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc unset command : pid(%d), type(%d)", pid, cmd_type);
732         }
733
734         dbus_message_append_args(msg,
735                                                          DBUS_TYPE_INT32, &pid,
736                                                          DBUS_TYPE_INT32, &cmd_type,
737                                                          DBUS_TYPE_INVALID);
738
739         DBusError err;
740         dbus_error_init(&err);
741
742         DBusMessage* result_msg;
743         int result = VC_ERROR_OPERATION_FAILED;
744
745         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
746         dbus_message_unref(msg);
747
748         if (dbus_error_is_set(&err)) {
749                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
750                 dbus_error_free(&err);
751         }
752
753         if (NULL != result_msg) {
754                 dbus_message_get_args(result_msg, &err,
755                                                           DBUS_TYPE_INT32, &result,
756                                                           DBUS_TYPE_INVALID);
757
758                 if (dbus_error_is_set(&err)) {
759                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
760                         dbus_error_free(&err);
761                         result = VC_ERROR_OPERATION_FAILED;
762                 }
763                 dbus_message_unref(result_msg);
764
765                 if (0 == result) {
766                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc unset command : result = %d", result);
767                 } else {
768                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc unset command : result = %d", result);
769                 }
770         } else {
771                 SLOG(LOG_ERROR, TAG_VCC, "@@ Result message is NULL");
772                 vc_dbus_reconnect();
773                 result = VC_ERROR_TIMED_OUT;
774         }
775
776         return result;
777 }
778
779 int vc_dbus_set_foreground(int pid, bool value)
780 {
781         if (0 != __dbus_check()) {
782                 return VC_ERROR_OPERATION_FAILED;
783         }
784
785         DBusMessage* msg = NULL;
786         int tmp_value = 0;
787
788         tmp_value = (int)value;
789
790         msg = dbus_message_new_signal(
791                 VC_MANAGER_SERVICE_OBJECT_PATH,
792                 VC_MANAGER_SERVICE_INTERFACE,
793                 VCC_MANAGER_METHOD_SET_FOREGROUND);
794
795         if (NULL == msg) {
796                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc set foreground to manager : Fail to make message");
797                 return VC_ERROR_OPERATION_FAILED;
798         } else {
799                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc set foreground to manager : client pid(%d), value(%s)", pid, tmp_value ? "true" : "false");
800         }
801
802         dbus_message_append_args(msg,
803                 DBUS_TYPE_INT32, &pid,
804                 DBUS_TYPE_INT32, &tmp_value,
805                 DBUS_TYPE_INVALID);
806
807         if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
808                 SLOG(LOG_ERROR, TAG_VCC, "[Dbus ERROR] Fail to Send");
809                 return VC_ERROR_OPERATION_FAILED;
810         }
811
812         dbus_message_unref(msg);
813
814         msg = NULL;
815         msg = dbus_message_new_method_call(
816                 VC_SERVER_SERVICE_NAME,
817                 VC_SERVER_SERVICE_OBJECT_PATH,
818                 VC_SERVER_SERVICE_INTERFACE,
819                 VC_METHOD_SET_FOREGROUND);
820
821         if (NULL == msg) {
822                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc set foreground to daemon : Fail to make message");
823                 return VC_ERROR_OPERATION_FAILED;
824         } else {
825                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc set foreground to daemon : client pid(%d), value(%s)", pid, tmp_value ? "true" : "false");
826         }
827
828         dbus_message_append_args(msg,
829                 DBUS_TYPE_INT32, &pid,
830                 DBUS_TYPE_INT32, &tmp_value,
831                 DBUS_TYPE_INVALID);
832
833         dbus_message_set_no_reply(msg, TRUE);
834
835         if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
836                 SLOG(LOG_ERROR, TAG_VCC, "[Dbus ERROR] Fail to Send");
837                 return VC_ERROR_OPERATION_FAILED;
838         }
839
840         dbus_connection_flush(g_conn_sender);
841
842         dbus_message_unref(msg);
843
844         return 0;
845 }
846
847 int vc_dbus_request_dialog(int pid, const char* disp_text, const char* utt_text, bool continuous)
848 {
849         if (NULL == g_conn_sender) {
850                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] NULL connection");
851                 if (0 != vc_dbus_reconnect()) {
852                         SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Fail to reconnect");
853                 }
854         }
855
856         DBusMessage* msg;
857         int tmp_continue = 0;
858
859         tmp_continue = (int)continuous;
860
861         msg = dbus_message_new_method_call(
862                 VC_SERVER_SERVICE_NAME,
863                 VC_SERVER_SERVICE_OBJECT_PATH,
864                 VC_SERVER_SERVICE_INTERFACE,
865                 VC_METHOD_DIALOG);
866
867         if (NULL == msg) {
868                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc request dialog to manager : Fail to make message");
869                 return VC_ERROR_OPERATION_FAILED;
870         } else {
871                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc request dialog to manager : client pid(%d), disp_text(%s), utt_text(%s), continuous(%d)", getpid(), disp_text, utt_text, continuous);
872         }
873
874         dbus_message_append_args(msg,
875                                 DBUS_TYPE_INT32, &pid,
876                                 DBUS_TYPE_STRING, &disp_text,
877                                 DBUS_TYPE_STRING, &utt_text,
878                                 DBUS_TYPE_INT32, &tmp_continue,
879                                 DBUS_TYPE_INVALID);
880
881         dbus_message_set_no_reply(msg, TRUE);
882
883         if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
884                 SLOG(LOG_ERROR, TAG_VCC, "[Dbus ERROR] Fail to Send");
885                 return VC_ERROR_OPERATION_FAILED;
886         } else {
887                 SLOG(LOG_DEBUG, TAG_VCC, "[Dbus DEBUG] Success to Send");
888                 dbus_connection_flush(g_conn_sender);
889         }
890
891         dbus_message_unref(msg);
892
893         return 0;
894
895 /*
896         DBusError err;
897         dbus_error_init(&err);
898
899         DBusMessage* result_msg;
900         int result = VC_ERROR_OPERATION_FAILED;
901
902         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
903         dbus_message_unref(msg);
904
905         if (dbus_error_is_set(&err)) {
906                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
907                 dbus_error_free(&err);
908         }
909
910         if (NULL != result_msg) {
911                 dbus_message_get_args(result_msg, &err,
912                                         DBUS_TYPE_INT32, &result,
913                                         DBUS_TYPE_INVALID);
914
915                 if (dbus_error_is_set(&err)) {
916                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
917                         dbus_error_free(&err);
918                         result = VC_ERROR_OPERATION_FAILED;
919                 }
920                 dbus_message_unref(result_msg);
921
922                 if (0 == result) {
923                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc set command : result = %d", result);
924                 } else {
925                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc set command : result = %d", result);
926                 }
927         } else {
928                 SLOG(LOG_ERROR, TAG_VCC, "@@ Result message is NULL");
929                 vc_dbus_reconnect();
930                 result = VC_ERROR_TIMED_OUT;
931         }
932
933         return result;
934 */
935 }
936
937 int vc_dbus_request_is_system_command_valid(int pid, bool* is_sys_cmd_valid)
938 {
939         if (NULL == g_conn_sender) {
940                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] NULL connection");
941                 if (0 != vc_dbus_reconnect()) {
942                         SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Fail to reconnect");
943                 }
944         }
945
946         DBusMessage* msg;
947
948         msg = dbus_message_new_method_call(
949                           VC_SERVER_SERVICE_NAME,
950                           VC_SERVER_SERVICE_OBJECT_PATH,
951                           VC_SERVER_SERVICE_INTERFACE,
952                           VC_METHOD_IS_SYS_COMMAND_VALID);
953
954         if (NULL == msg) {
955                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc is system command valid : Fail to make message");
956                 return VC_ERROR_OPERATION_FAILED;
957         } else {
958                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc is system command valid : pid(%d)", pid);
959         }
960
961         dbus_message_append_args(msg,
962                                 DBUS_TYPE_INT32, &pid,
963                                 DBUS_TYPE_INVALID);
964
965         DBusError err;
966         dbus_error_init(&err);
967         int tmp_sys_cmd = 0;
968
969         DBusMessage* result_msg;
970         int result = VC_ERROR_OPERATION_FAILED;
971
972         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
973         dbus_message_unref(msg);
974
975         if (dbus_error_is_set(&err)) {
976                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
977                 dbus_error_free(&err);
978         }
979
980         if (NULL != result_msg) {
981                 dbus_message_get_args(result_msg, &err,
982                                         DBUS_TYPE_INT32, &result,
983                                         DBUS_TYPE_INT32, &tmp_sys_cmd,
984                                         DBUS_TYPE_INVALID);
985
986                 if (dbus_error_is_set(&err)) {
987                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
988                         dbus_error_free(&err);
989                         result = VC_ERROR_OPERATION_FAILED;
990                 }
991                 dbus_message_unref(result_msg);
992
993                 if (0 == result) {
994                         *is_sys_cmd_valid = (bool)tmp_sys_cmd;
995                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc is system command valid : result = %d, is_sys_cmd_valid = %d", result, *is_sys_cmd_valid);
996                 } else {
997                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc is system command valid : result = %d", result);
998                 }
999         } else {
1000                 SLOG(LOG_ERROR, TAG_VCC, "@@ Result message is NULL");
1001                 vc_dbus_reconnect();
1002                 result = VC_ERROR_TIMED_OUT;
1003         }
1004
1005         return result;
1006 }
1007
1008
1009 #if 0
1010 int vc_dbus_request_start(int pid, int silence)
1011 {
1012         DBusMessage* msg;
1013
1014         /* create a signal & check for errors */
1015         msg = dbus_message_new_method_call(
1016                           VC_SERVER_SERVICE_NAME,
1017                           VC_SERVER_SERVICE_OBJECT_PATH,
1018                           VC_SERVER_SERVICE_INTERFACE,
1019                           VC_METHOD_REQUEST_START);
1020
1021         if (NULL == msg) {
1022                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc start : Fail to make message ");
1023                 return VC_ERROR_OPERATION_FAILED;
1024         } else {
1025                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc start : pid(%d), silence(%d)", pid, silence);
1026         }
1027
1028         DBusMessageIter args;
1029         dbus_message_iter_init_append(msg, &args);
1030
1031         /* Append result*/
1032         dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(pid));
1033         dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(silence));
1034
1035         DBusError err;
1036         dbus_error_init(&err);
1037
1038         DBusMessage* result_msg;
1039         int result = VC_ERROR_OPERATION_FAILED;
1040
1041         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
1042         dbus_message_unref(msg);
1043
1044         if (dbus_error_is_set(&err)) {
1045                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
1046                 dbus_error_free(&err);
1047         }
1048
1049         if (NULL != result_msg) {
1050                 dbus_message_get_args(result_msg, &err,
1051                                                           DBUS_TYPE_INT32, &result,
1052                                                           DBUS_TYPE_INVALID);
1053
1054                 if (dbus_error_is_set(&err)) {
1055                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
1056                         dbus_error_free(&err);
1057                         result = VC_ERROR_OPERATION_FAILED;
1058                 }
1059                 dbus_message_unref(result_msg);
1060
1061                 if (0 == result) {
1062                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc start : result = %d", result);
1063                 } else {
1064                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc start : result = %d", result);
1065                 }
1066         } else {
1067                 SLOG(LOG_DEBUG, TAG_VCC, "@@ Result Message is NULL");
1068                 vc_dbus_reconnect();
1069                 result = VC_ERROR_TIMED_OUT;
1070         }
1071
1072         return result;
1073 }
1074
1075 int vc_dbus_request_stop(int pid)
1076 {
1077         DBusMessage* msg;
1078
1079         /* create a signal & check for errors */
1080         msg = dbus_message_new_method_call(
1081                           VC_SERVER_SERVICE_NAME,
1082                           VC_SERVER_SERVICE_OBJECT_PATH,
1083                           VC_SERVER_SERVICE_INTERFACE,
1084                           VC_METHOD_REQUEST_STOP);
1085
1086         if (NULL == msg) {
1087                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc stop : Fail to make message ");
1088                 return VC_ERROR_OPERATION_FAILED;
1089         } else {
1090                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc stop : pid(%d)", pid);
1091         }
1092
1093         dbus_message_append_args(msg,
1094                                                          DBUS_TYPE_INT32, &pid,
1095                                                          DBUS_TYPE_INVALID);
1096
1097         DBusError err;
1098         dbus_error_init(&err);
1099
1100         DBusMessage* result_msg;
1101         int result = VC_ERROR_OPERATION_FAILED;
1102
1103         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
1104         dbus_message_unref(msg);
1105
1106         if (dbus_error_is_set(&err)) {
1107                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
1108                 dbus_error_free(&err);
1109         }
1110
1111         if (NULL != result_msg) {
1112                 dbus_message_get_args(result_msg, &err,
1113                                                           DBUS_TYPE_INT32, &result,
1114                                                           DBUS_TYPE_INVALID);
1115
1116                 if (dbus_error_is_set(&err)) {
1117                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
1118                         dbus_error_free(&err);
1119                         result = VC_ERROR_OPERATION_FAILED;
1120                 }
1121                 dbus_message_unref(result_msg);
1122
1123                 if (0 == result) {
1124                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc stop : result = %d", result);
1125                 } else {
1126                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc stop : result = %d", result);
1127                 }
1128         } else {
1129                 SLOG(LOG_DEBUG, TAG_VCC, "@@ Result Message is NULL");
1130                 vc_dbus_reconnect();
1131                 result = VC_ERROR_TIMED_OUT;
1132         }
1133
1134         return result;
1135 }
1136
1137 int vc_dbus_request_cancel(int pid)
1138 {
1139         DBusMessage* msg;
1140
1141         /* create a signal & check for errors */
1142         msg = dbus_message_new_method_call(
1143                           VC_SERVER_SERVICE_NAME,
1144                           VC_SERVER_SERVICE_OBJECT_PATH,        /* object name of the signal */
1145                           VC_SERVER_SERVICE_INTERFACE,  /* interface name of the signal */
1146                           VC_METHOD_REQUEST_CANCEL);    /* name of the signal */
1147
1148         if (NULL == msg) {
1149                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc cancel : Fail to make message ");
1150                 return VC_ERROR_OPERATION_FAILED;
1151         } else {
1152                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc cancel : pid(%d)", pid);
1153         }
1154
1155         dbus_message_append_args(msg,
1156                                                          DBUS_TYPE_INT32, &pid,
1157                                                          DBUS_TYPE_INVALID);
1158
1159         DBusError err;
1160         dbus_error_init(&err);
1161
1162         DBusMessage* result_msg;
1163         int result = VC_ERROR_OPERATION_FAILED;
1164
1165         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
1166         dbus_message_unref(msg);
1167
1168         if (dbus_error_is_set(&err)) {
1169                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
1170                 dbus_error_free(&err);
1171         }
1172
1173         if (NULL != result_msg) {
1174                 dbus_message_get_args(result_msg, &err,
1175                                                           DBUS_TYPE_INT32, &result,
1176                                                           DBUS_TYPE_INVALID);
1177
1178                 if (dbus_error_is_set(&err)) {
1179                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
1180                         dbus_error_free(&err);
1181                         result = VC_ERROR_OPERATION_FAILED;
1182                 }
1183                 dbus_message_unref(result_msg);
1184
1185                 if (0 == result) {
1186                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc cancel : result = %d", result);
1187                 } else {
1188                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc cancel : result = %d", result);
1189                 }
1190         } else {
1191                 SLOG(LOG_DEBUG, TAG_VCC, "@@ Result Message is NULL");
1192                 vc_dbus_reconnect();
1193                 result = VC_ERROR_TIMED_OUT;
1194         }
1195
1196         return result;
1197 }
1198 #endif
1199
1200 /* Authority */
1201 int vc_dbus_request_auth_enable(int pid, int mgr_pid)
1202 {
1203         if (0 != __dbus_check()) {
1204                 return VC_ERROR_OPERATION_FAILED;
1205         }
1206
1207         DBusMessage* msg;
1208
1209         char service_name[64] = {0,};
1210         char object_path[64] = {0,};
1211         char target_if_name[128] = {0,};
1212
1213         snprintf(service_name, 64, "%s%d", VC_MANAGER_SERVICE_NAME, mgr_pid);
1214         snprintf(object_path, 64, "%s", VC_MANAGER_SERVICE_OBJECT_PATH);
1215         snprintf(target_if_name, 128, "%s%d", VC_MANAGER_SERVICE_INTERFACE, mgr_pid);
1216
1217         /* create a signal & check for errors */
1218         msg = dbus_message_new_method_call(
1219                           service_name,
1220                           object_path,  /* object name of the signal */
1221                           target_if_name,       /* interface name of the signal */
1222                           VC_METHOD_AUTH_ENABLE);       /* name of the signal */
1223
1224         if (NULL == msg) {
1225                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc auth enable : Fail to make message ");
1226                 return VC_ERROR_OPERATION_FAILED;
1227         } else {
1228                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc auth enable : pid(%d)", pid);
1229         }
1230
1231         dbus_message_append_args(msg,
1232                                                          DBUS_TYPE_INT32, &pid,
1233                                                          DBUS_TYPE_INVALID);
1234
1235         DBusError err;
1236         dbus_error_init(&err);
1237
1238         DBusMessage* result_msg;
1239         int result = VC_ERROR_OPERATION_FAILED;
1240
1241         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
1242         dbus_message_unref(msg);
1243
1244         if (dbus_error_is_set(&err)) {
1245                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
1246                 dbus_error_free(&err);
1247         }
1248
1249         if (NULL != result_msg) {
1250                 dbus_message_get_args(result_msg, &err,
1251                                                           DBUS_TYPE_INT32, &result,
1252                                                           DBUS_TYPE_INVALID);
1253
1254                 if (dbus_error_is_set(&err)) {
1255                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
1256                         dbus_error_free(&err);
1257                         result = VC_ERROR_OPERATION_FAILED;
1258                 }
1259                 dbus_message_unref(result_msg);
1260
1261                 if (0 == result) {
1262                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc auth enable : result = %d", result);
1263                 } else {
1264                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc auth enable : result = %d", result);
1265                 }
1266         } else {
1267                 SLOG(LOG_DEBUG, TAG_VCC, "@@ Result Message is NULL");
1268                 vc_dbus_reconnect();
1269                 result = VC_ERROR_TIMED_OUT;
1270         }
1271
1272         return result;
1273 }
1274
1275 int vc_dbus_request_auth_disable(int pid, int mgr_pid)
1276 {
1277         if (0 != __dbus_check()) {
1278                 return VC_ERROR_OPERATION_FAILED;
1279         }
1280
1281         DBusMessage* msg;
1282
1283         char service_name[64] = {0,};
1284         char object_path[64] = {0,};
1285         char target_if_name[128] = {0,};
1286
1287         snprintf(service_name, 64, "%s%d", VC_MANAGER_SERVICE_NAME, mgr_pid);
1288         snprintf(object_path, 64, "%s", VC_MANAGER_SERVICE_OBJECT_PATH);
1289         snprintf(target_if_name, 128, "%s%d", VC_MANAGER_SERVICE_INTERFACE, mgr_pid);
1290
1291         /* create a signal & check for errors */
1292         msg = dbus_message_new_method_call(
1293                           service_name,
1294                           object_path,  /* object name of the signal */
1295                           target_if_name,       /* interface name of the signal */
1296                           VC_METHOD_AUTH_DISABLE);      /* name of the signal */
1297
1298         if (NULL == msg) {
1299                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc auth disable : Fail to make message ");
1300                 return VC_ERROR_OPERATION_FAILED;
1301         } else {
1302                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc auth disable : pid(%d)", pid);
1303         }
1304
1305         dbus_message_append_args(msg,
1306                                                          DBUS_TYPE_INT32, &pid,
1307                                                          DBUS_TYPE_INVALID);
1308
1309         DBusError err;
1310         dbus_error_init(&err);
1311
1312         DBusMessage* result_msg;
1313         int result = VC_ERROR_OPERATION_FAILED;
1314
1315         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
1316         dbus_message_unref(msg);
1317
1318         if (dbus_error_is_set(&err)) {
1319                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
1320                 dbus_error_free(&err);
1321         }
1322
1323         if (NULL != result_msg) {
1324                 dbus_message_get_args(result_msg, &err,
1325                                                           DBUS_TYPE_INT32, &result,
1326                                                           DBUS_TYPE_INVALID);
1327
1328                 if (dbus_error_is_set(&err)) {
1329                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
1330                         dbus_error_free(&err);
1331                         result = VC_ERROR_OPERATION_FAILED;
1332                 }
1333                 dbus_message_unref(result_msg);
1334
1335                 if (0 == result) {
1336                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc auth disable : result = %d", result);
1337                 } else {
1338                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc auth disable : result = %d", result);
1339                 }
1340         } else {
1341                 SLOG(LOG_DEBUG, TAG_VCC, "@@ Result Message is NULL");
1342                 vc_dbus_reconnect();
1343                 result = VC_ERROR_TIMED_OUT;
1344         }
1345
1346         return result;
1347 }
1348
1349 int vc_dbus_request_auth_start(int pid, int mgr_pid)
1350 {
1351         if (0 != __dbus_check()) {
1352                 return VC_ERROR_OPERATION_FAILED;
1353         }
1354
1355         DBusMessage* msg;
1356
1357         char service_name[64] = {0,};
1358         char object_path[64] = {0,};
1359         char target_if_name[128] = {0,};
1360
1361         snprintf(service_name, 64, "%s%d", VC_MANAGER_SERVICE_NAME, mgr_pid);
1362         snprintf(object_path, 64, "%s", VC_MANAGER_SERVICE_OBJECT_PATH);
1363         snprintf(target_if_name, 128, "%s%d", VC_MANAGER_SERVICE_INTERFACE, mgr_pid);
1364
1365         /* create a signal & check for errors */
1366         msg = dbus_message_new_method_call(
1367                           service_name,
1368                           object_path,
1369                           target_if_name,
1370                           VC_METHOD_AUTH_START);
1371
1372         if (NULL == msg) {
1373                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc auth start : Fail to make message ");
1374                 return VC_ERROR_OPERATION_FAILED;
1375         } else {
1376                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc auth start : pid(%d)", pid);
1377         }
1378
1379         DBusMessageIter args;
1380         dbus_message_iter_init_append(msg, &args);
1381
1382         /* Append result*/
1383         dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(pid));
1384
1385         DBusError err;
1386         dbus_error_init(&err);
1387
1388         DBusMessage* result_msg;
1389         int result = VC_ERROR_OPERATION_FAILED;
1390
1391         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
1392         dbus_message_unref(msg);
1393
1394         if (dbus_error_is_set(&err)) {
1395                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
1396                 dbus_error_free(&err);
1397         }
1398
1399         if (NULL != result_msg) {
1400                 dbus_message_get_args(result_msg, &err,
1401                                                           DBUS_TYPE_INT32, &result,
1402                                                           DBUS_TYPE_INVALID);
1403
1404                 if (dbus_error_is_set(&err)) {
1405                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
1406                         dbus_error_free(&err);
1407                         result = VC_ERROR_OPERATION_FAILED;
1408                 }
1409                 dbus_message_unref(result_msg);
1410
1411                 if (0 == result) {
1412                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc auth start : result = %d", result);
1413                 } else {
1414                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc auth start : result = %d", result);
1415                 }
1416         } else {
1417                 SLOG(LOG_DEBUG, TAG_VCC, "@@ Result Message is NULL");
1418                 vc_dbus_reconnect();
1419                 result = VC_ERROR_TIMED_OUT;
1420         }
1421
1422         return result;
1423 }
1424
1425 int vc_dbus_request_auth_stop(int pid, int mgr_pid)
1426 {
1427         if (0 != __dbus_check()) {
1428                 return VC_ERROR_OPERATION_FAILED;
1429         }
1430
1431         DBusMessage* msg;
1432
1433         char service_name[64] = {0,};
1434         char object_path[64] = {0,};
1435         char target_if_name[128] = {0,};
1436
1437         snprintf(service_name, 64, "%s%d", VC_MANAGER_SERVICE_NAME, mgr_pid);
1438         snprintf(object_path, 64, "%s", VC_MANAGER_SERVICE_OBJECT_PATH);
1439         snprintf(target_if_name, 128, "%s%d", VC_MANAGER_SERVICE_INTERFACE, mgr_pid);
1440
1441         /* create a signal & check for errors */
1442         msg = dbus_message_new_method_call(
1443                           service_name,
1444                           object_path,
1445                           target_if_name,
1446                           VC_METHOD_AUTH_STOP);
1447
1448         if (NULL == msg) {
1449                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc auth stop : Fail to make message ");
1450                 return VC_ERROR_OPERATION_FAILED;
1451         } else {
1452                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc auth stop : pid(%d)", pid);
1453         }
1454
1455         dbus_message_append_args(msg,
1456                                                          DBUS_TYPE_INT32, &pid,
1457                                                          DBUS_TYPE_INVALID);
1458
1459         DBusError err;
1460         dbus_error_init(&err);
1461
1462         DBusMessage* result_msg;
1463         int result = VC_ERROR_OPERATION_FAILED;
1464
1465         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
1466         dbus_message_unref(msg);
1467
1468         if (dbus_error_is_set(&err)) {
1469                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
1470                 dbus_error_free(&err);
1471         }
1472
1473         if (NULL != result_msg) {
1474                 dbus_message_get_args(result_msg, &err,
1475                                                           DBUS_TYPE_INT32, &result,
1476                                                           DBUS_TYPE_INVALID);
1477
1478                 if (dbus_error_is_set(&err)) {
1479                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
1480                         dbus_error_free(&err);
1481                         result = VC_ERROR_OPERATION_FAILED;
1482                 }
1483                 dbus_message_unref(result_msg);
1484
1485                 if (0 == result) {
1486                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc auth stop : result = %d", result);
1487                 } else {
1488                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc auth stop : result = %d", result);
1489                 }
1490         } else {
1491                 SLOG(LOG_DEBUG, TAG_VCC, "@@ Result Message is NULL");
1492                 vc_dbus_reconnect();
1493                 result = VC_ERROR_TIMED_OUT;
1494         }
1495
1496         return result;
1497 }
1498
1499 int vc_dbus_request_auth_cancel(int pid, int mgr_pid)
1500 {
1501         if (0 != __dbus_check()) {
1502                 return VC_ERROR_OPERATION_FAILED;
1503         }
1504
1505         DBusMessage* msg;
1506
1507         char service_name[64] = {0,};
1508         char object_path[64] = {0,};
1509         char target_if_name[128] = {0,};
1510
1511         snprintf(service_name, 64, "%s%d", VC_MANAGER_SERVICE_NAME, mgr_pid);
1512         snprintf(object_path, 64, "%s", VC_MANAGER_SERVICE_OBJECT_PATH);
1513         snprintf(target_if_name, 128, "%s%d", VC_MANAGER_SERVICE_INTERFACE, mgr_pid);
1514
1515         /* create a signal & check for errors */
1516         msg = dbus_message_new_method_call(
1517                           service_name,
1518                           object_path,  /* object name of the signal */
1519                           target_if_name,       /* interface name of the signal */
1520                           VC_METHOD_AUTH_CANCEL);       /* name of the signal */
1521
1522         if (NULL == msg) {
1523                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc auth cancel : Fail to make message ");
1524                 return VC_ERROR_OPERATION_FAILED;
1525         } else {
1526                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc auth cancel : pid(%d)", pid);
1527         }
1528
1529         dbus_message_append_args(msg,
1530                                                          DBUS_TYPE_INT32, &pid,
1531                                                          DBUS_TYPE_INVALID);
1532
1533         DBusError err;
1534         dbus_error_init(&err);
1535
1536         DBusMessage* result_msg;
1537         int result = VC_ERROR_OPERATION_FAILED;
1538
1539         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
1540         dbus_message_unref(msg);
1541
1542         if (dbus_error_is_set(&err)) {
1543                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
1544                 dbus_error_free(&err);
1545         }
1546
1547         if (NULL != result_msg) {
1548                 dbus_message_get_args(result_msg, &err,
1549                                                           DBUS_TYPE_INT32, &result,
1550                                                           DBUS_TYPE_INVALID);
1551
1552                 if (dbus_error_is_set(&err)) {
1553                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
1554                         dbus_error_free(&err);
1555                         result = VC_ERROR_OPERATION_FAILED;
1556                 }
1557                 dbus_message_unref(result_msg);
1558
1559                 if (0 == result) {
1560                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc auth cancel : result = %d", result);
1561                 } else {
1562                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc auth cancel : result = %d", result);
1563                 }
1564         } else {
1565                 SLOG(LOG_DEBUG, TAG_VCC, "@@ Result Message is NULL");
1566                 vc_dbus_reconnect();
1567                 result = VC_ERROR_TIMED_OUT;
1568         }
1569
1570         return result;
1571 }