Fix dbus delay when requesting hello
[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
174                         __vc_cb_error(VC_ERROR_SERVICE_RESET, -1, "Daemon Reset");
175                         SLOG(LOG_DEBUG, TAG_VCC, "@@@");
176                 } /* NameOwnerChanged */
177
178                 else {
179                         SLOG(LOG_DEBUG, TAG_VCC, "Message is NOT valid");
180                         dbus_message_unref(msg);
181                         break;
182                 }
183
184                 /* free the message */
185                 dbus_message_unref(msg);
186         } /* while(1) */
187
188         return ECORE_CALLBACK_PASS_ON;
189 }
190
191 static void __vc_dbus_connection_free()
192 {
193         if (NULL != g_conn_listener) {
194                 dbus_connection_close(g_conn_listener);
195                 dbus_connection_unref(g_conn_listener);
196                 g_conn_listener = NULL;
197         }
198         if (NULL != g_conn_sender) {
199                 dbus_connection_close(g_conn_sender);
200                 dbus_connection_unref(g_conn_sender);
201                 g_conn_sender = NULL;
202         }
203 }
204
205 int vc_dbus_open_connection()
206 {
207         if (NULL != g_conn_sender && NULL != g_conn_listener) {
208                 SLOG(LOG_WARN, TAG_VCC, "already existed connection ");
209                 return 0;
210         }
211
212         DBusError err;
213         int ret;
214
215         /* initialise the error value */
216         dbus_error_init(&err);
217
218         /* connect to the DBUS system bus, and check for errors */
219         g_conn_sender = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
220
221         if (dbus_error_is_set(&err)) {
222                 SLOG(LOG_ERROR, TAG_VCC, "Dbus Connection Error (%s)", err.message);
223                 dbus_error_free(&err);
224         }
225
226         if (NULL == g_conn_sender) {
227                 SLOG(LOG_ERROR, TAG_VCC, "Fail to get dbus connection ");
228                 return VC_ERROR_OPERATION_FAILED;
229         }
230
231         dbus_connection_set_exit_on_disconnect(g_conn_sender, false);
232
233         g_conn_listener = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
234
235         if (dbus_error_is_set(&err)) {
236                 SLOG(LOG_ERROR, TAG_VCC, "Dbus Connection Error (%s)", err.message);
237                 dbus_error_free(&err);
238         }
239
240         if (NULL == g_conn_listener) {
241                 SLOG(LOG_ERROR, TAG_VCC, "Fail to get dbus connection ");
242                 __vc_dbus_connection_free();
243                 return VC_ERROR_OPERATION_FAILED;
244         }
245
246         dbus_connection_set_exit_on_disconnect(g_conn_listener, false);
247
248         int pid = getpid();
249
250         char service_name[64];
251         memset(service_name, '\0', 64);
252         snprintf(service_name, 64, "%s%d", VC_CLIENT_SERVICE_NAME, pid);
253
254         SLOG(LOG_DEBUG, TAG_VCC, "service name is %s", service_name);
255
256         /* register our name on the bus, and check for errors */
257         ret = dbus_bus_request_name(g_conn_listener, service_name, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
258
259         if (dbus_error_is_set(&err)) {
260                 SLOG(LOG_ERROR, TAG_VCC, "Name Error (%s)", err.message);
261                 dbus_error_free(&err);
262         }
263
264         if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
265                 SLOG(LOG_ERROR, TAG_VCC, "fail dbus_bus_request_name()");
266                 __vc_dbus_connection_free();
267                 return -2;
268         }
269
270         if (NULL != g_fd_handler) {
271                 SLOG(LOG_WARN, TAG_VCC, "The handler already exists.");
272                 __vc_dbus_connection_free();
273                 return 0;
274         }
275
276         char rule[256] = {0, };
277         snprintf(rule, 256, "type='signal',interface='%s'", VC_CLIENT_SERVICE_INTERFACE);
278
279         /* add a rule for which messages we want to see */
280         dbus_bus_add_match(g_conn_listener, rule, &err);
281
282         if (dbus_error_is_set(&err)) {
283                 SLOG(LOG_ERROR, TAG_VCC, "Match Error (%s)", err.message);
284                 dbus_error_free(&err);
285                 __vc_dbus_connection_free();
286                 return VC_ERROR_OPERATION_FAILED;
287         }
288
289         int fd = 0;
290         if (1 != dbus_connection_get_unix_fd(g_conn_listener, &fd)) {
291                 SLOG(LOG_ERROR, TAG_VCC, "fail to get fd from dbus ");
292                 __vc_dbus_connection_free();
293                 return VC_ERROR_OPERATION_FAILED;
294         } else {
295                 SLOG(LOG_DEBUG, TAG_VCC, "Get fd from dbus : %d", fd);
296         }
297
298         g_fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)listener_event_callback, g_conn_listener, NULL, NULL);
299         if (NULL == g_fd_handler) {
300                 SLOG(LOG_ERROR, TAG_VCC, "fail to get fd handler from ecore ");
301                 __vc_dbus_connection_free();
302                 return VC_ERROR_OPERATION_FAILED;
303         }
304
305         return 0;
306 }
307
308 int vc_dbus_close_connection()
309 {
310         DBusError err;
311         dbus_error_init(&err);
312
313         if (NULL != g_fd_handler) {
314                 ecore_main_fd_handler_del(g_fd_handler);
315                 g_fd_handler = NULL;
316         }
317
318         if (NULL != g_conn_listener) {
319                 int pid = getpid();
320
321                 char service_name[64];
322                 memset(service_name, '\0', 64);
323                 snprintf(service_name, 64, "%s%d", VC_CLIENT_SERVICE_NAME, pid);
324
325                 dbus_bus_release_name(g_conn_listener, service_name, &err);
326
327                 if (dbus_error_is_set(&err)) {
328                         SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
329                         dbus_error_free(&err);
330                 }
331         }
332
333         __vc_dbus_connection_free();
334
335         return 0;
336 }
337
338 int vc_dbus_reconnect()
339 {
340         if (!g_conn_sender || !g_conn_listener) {
341                 vc_dbus_close_connection();
342
343                 if (0 != vc_dbus_open_connection()) {
344                         SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Fail to reconnect");
345                         return -1;
346                 }
347
348                 SLOG(LOG_DEBUG, TAG_VCC, "[DBUS] Reconnect");
349                 return 0;
350         }
351
352         bool sender_connected = dbus_connection_get_is_connected(g_conn_sender);
353         bool listener_connected = dbus_connection_get_is_connected(g_conn_listener);
354         SLOG(LOG_WARN, TAG_VCC, "[DBUS] Sender(%s) Listener(%s)",
355                  sender_connected ? "Connected" : "Not connected", listener_connected ? "Connected" : "Not connected");
356
357         if (false == sender_connected || false == listener_connected) {
358                 vc_dbus_close_connection();
359
360                 if (0 != vc_dbus_open_connection()) {
361                         SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Fail to reconnect");
362                         return -1;
363                 }
364
365                 SLOG(LOG_DEBUG, TAG_VCC, "[DBUS] Reconnect");
366         }
367
368         return 0;
369 }
370
371 static int __dbus_check()
372 {
373         if (NULL == g_conn_sender) {
374                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] NULL connection");
375                 return vc_dbus_reconnect();
376         }
377         return 0;
378 }
379
380 int vc_dbus_request_hello()
381 {
382         if (0 != __dbus_check()) {
383                 return VC_ERROR_OPERATION_FAILED;
384         }
385
386         DBusMessage* msg;
387
388         msg = dbus_message_new_method_call(
389                           VC_SERVER_SERVICE_NAME,
390                           VC_SERVER_SERVICE_OBJECT_PATH,
391                           VC_SERVER_SERVICE_INTERFACE,
392                           VC_METHOD_HELLO);
393
394         if (NULL == msg) {
395                 SLOG(LOG_ERROR, TAG_VCC, "@@ Request vc hello : Fail to make message");
396                 return VC_ERROR_OPERATION_FAILED;
397         }
398
399         DBusError err;
400         dbus_error_init(&err);
401
402         DBusMessage* result_msg = NULL;
403         int result = 0;
404
405         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, -1, &err);
406
407         if (dbus_error_is_set(&err)) {
408                 SLOG(LOG_DEBUG, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
409                 dbus_error_free(&err);
410         }
411
412         dbus_message_unref(msg);
413
414         if (NULL != result_msg) {
415                 dbus_message_unref(result_msg);
416                 result = 0;
417         } else {
418                 result = VC_ERROR_TIMED_OUT;
419         }
420
421         return result;
422 }
423
424
425 int vc_dbus_request_initialize(int pid, int* mgr_pid, int* service_state, int* daemon_pid)
426 {
427         if (0 != __dbus_check()) {
428                 return VC_ERROR_OPERATION_FAILED;
429         }
430
431         DBusMessage* msg;
432
433         msg = dbus_message_new_method_call(
434                           VC_SERVER_SERVICE_NAME,
435                           VC_SERVER_SERVICE_OBJECT_PATH,
436                           VC_SERVER_SERVICE_INTERFACE,
437                           VC_METHOD_INITIALIZE);
438
439         if (NULL == msg) {
440                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc initialize : Fail to make message ");
441                 return VC_ERROR_OPERATION_FAILED;
442         } else {
443                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc initialize : pid(%d)", pid);
444         }
445
446         dbus_message_append_args(msg,
447                         DBUS_TYPE_INT32, &pid,
448                         DBUS_TYPE_INVALID);
449
450         DBusError err;
451         dbus_error_init(&err);
452
453         DBusMessage* result_msg;
454         int result = VC_ERROR_OPERATION_FAILED;
455
456         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
457         dbus_message_unref(msg);
458
459         if (dbus_error_is_set(&err)) {
460                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
461                 dbus_error_free(&err);
462         }
463
464         if (NULL != result_msg) {
465                 int tmp = -1;
466                 int tmp_service_state = 0;
467                 int tmp_daemon_pid = 0;
468                 dbus_message_get_args(result_msg, &err,
469                         DBUS_TYPE_INT32, &result,
470                         DBUS_TYPE_INT32, &tmp,
471                         DBUS_TYPE_INT32, &tmp_service_state,
472                         DBUS_TYPE_INT32, &tmp_daemon_pid,
473                         DBUS_TYPE_INVALID);
474
475                 if (dbus_error_is_set(&err)) {
476                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
477                         dbus_error_free(&err);
478                         result = VC_ERROR_OPERATION_FAILED;
479                 }
480
481                 dbus_message_unref(result_msg);
482
483                 if (0 == result) {
484                         *mgr_pid = tmp;
485                         *service_state = tmp_service_state;
486                         *daemon_pid = tmp_daemon_pid;
487
488                         /* add a rule for daemon error */
489                         char rule[256] = {0, };
490                         snprintf(rule, 256, "sender='org.freedesktop.DBus',path='/org/freedesktop/DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',type='signal',arg0='%s'", VC_SERVER_SERVICE_INTERFACE);
491                         dbus_bus_add_match(g_conn_listener, rule, &err);
492
493                         if (dbus_error_is_set(&err)) {
494                                 SLOG(LOG_ERROR, TAG_VCC, "Match Error (%s)", err.message);
495                                 dbus_error_free(&err);
496                         }
497
498                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc initialize : result = %d mgr = %d service = %d daemon_pid = %d", result, *mgr_pid, *service_state, *daemon_pid);
499                 } else {
500                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc initialize : result = %d", result);
501                 }
502         } else {
503                 SLOG(LOG_ERROR, TAG_VCC, "@@ Result message is NULL ");
504                 vc_dbus_reconnect();
505                 result = VC_ERROR_TIMED_OUT;
506         }
507
508         return result;
509 }
510
511 int vc_dbus_request_finalize(int pid)
512 {
513         DBusError err;
514         dbus_error_init(&err);
515
516         if (0 != __dbus_check()) {
517                 return VC_ERROR_OPERATION_FAILED;
518         }
519
520         /* remove a rule for daemon error */
521         char rule_err[256] = {0, };
522         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);
523         dbus_bus_remove_match(g_conn_listener, rule_err, &err);
524         dbus_connection_flush(g_conn_listener);
525
526         if (dbus_error_is_set(&err)) {
527                 SLOG(LOG_ERROR, TAG_VCC, "Match Error (%s)", err.message);
528                 dbus_error_free(&err);
529         }
530
531         DBusMessage* msg;
532
533         msg = dbus_message_new_method_call(
534                           VC_SERVER_SERVICE_NAME,
535                           VC_SERVER_SERVICE_OBJECT_PATH,
536                           VC_SERVER_SERVICE_INTERFACE,
537                           VC_METHOD_FINALIZE);
538
539         if (NULL == msg) {
540                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc finalize : Fail to make message ");
541                 return VC_ERROR_OPERATION_FAILED;
542         } else {
543                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc finalize : pid(%d)", pid);
544         }
545
546         dbus_message_append_args(msg, DBUS_TYPE_INT32, &pid, DBUS_TYPE_INVALID);
547
548         DBusMessage* result_msg;
549         int result = VC_ERROR_OPERATION_FAILED;
550
551         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
552         dbus_message_unref(msg);
553
554         if (dbus_error_is_set(&err)) {
555                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
556                 dbus_error_free(&err);
557         }
558
559         if (NULL != result_msg) {
560                 dbus_message_get_args(result_msg, &err,
561                                                           DBUS_TYPE_INT32, &result,
562                                                           DBUS_TYPE_INVALID);
563
564                 if (dbus_error_is_set(&err)) {
565                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
566                         dbus_error_free(&err);
567                         result = VC_ERROR_OPERATION_FAILED;
568                 }
569
570                 dbus_message_unref(result_msg);
571
572                 if (0 == result) {
573                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc finalize : result = %d", result);
574                 } else {
575                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc finalize : result = %d", result);
576                 }
577         } else {
578                 SLOG(LOG_ERROR, TAG_VCC, "@@ Result message is NULL ");
579                 vc_dbus_reconnect();
580                 result = VC_ERROR_TIMED_OUT;
581         }
582
583         return result;
584 }
585
586 int vc_dbus_request_set_exclusive_command(int pid, bool value)
587 {
588         if (0 != __dbus_check()) {
589                 return VC_ERROR_OPERATION_FAILED;
590         }
591
592         DBusMessage* msg;
593
594         msg = dbus_message_new_method_call(
595                           VC_SERVER_SERVICE_NAME,
596                           VC_SERVER_SERVICE_OBJECT_PATH,
597                           VC_SERVER_SERVICE_INTERFACE,
598                           VC_METHOD_SET_EXCLUSIVE_CMD);
599
600         if (NULL == msg) {
601                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc set exclusive command : Fail to make message");
602                 return VC_ERROR_OPERATION_FAILED;
603         } else {
604                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc set exclusive command : pid(%d)", pid);
605         }
606
607         int temp = value;
608
609         dbus_message_append_args(msg,
610                                                          DBUS_TYPE_INT32, &pid,
611                                                          DBUS_TYPE_INT32, &temp,
612                                                          DBUS_TYPE_INVALID);
613
614         DBusError err;
615         dbus_error_init(&err);
616
617         DBusMessage* result_msg;
618         int result = VC_ERROR_OPERATION_FAILED;
619
620         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
621         dbus_message_unref(msg);
622
623         if (dbus_error_is_set(&err)) {
624                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
625                 dbus_error_free(&err);
626         }
627
628         if (NULL != result_msg) {
629                 dbus_message_get_args(result_msg, &err,
630                                                           DBUS_TYPE_INT32, &result,
631                                                           DBUS_TYPE_INVALID);
632
633                 if (dbus_error_is_set(&err)) {
634                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
635                         dbus_error_free(&err);
636                         result = VC_ERROR_OPERATION_FAILED;
637                 }
638                 dbus_message_unref(result_msg);
639
640                 if (0 == result) {
641                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc set exclusive command : result = %d", result);
642                 } else {
643                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc set exclusive command : result = %d", result);
644                 }
645         } else {
646                 SLOG(LOG_ERROR, TAG_VCC, "@@ Result message is NULL");
647                 vc_dbus_reconnect();
648                 result = VC_ERROR_TIMED_OUT;
649         }
650
651         return result;
652 }
653
654 int vc_dbus_request_set_command(int pid, vc_cmd_type_e cmd_type)
655 {
656         if (0 != __dbus_check()) {
657                 return VC_ERROR_OPERATION_FAILED;
658         }
659
660         DBusMessage* msg;
661
662         msg = dbus_message_new_method_call(
663                           VC_SERVER_SERVICE_NAME,
664                           VC_SERVER_SERVICE_OBJECT_PATH,
665                           VC_SERVER_SERVICE_INTERFACE,
666                           VC_METHOD_SET_COMMAND);
667
668         if (NULL == msg) {
669                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc set command : Fail to make message");
670                 return VC_ERROR_OPERATION_FAILED;
671         } else {
672                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc set command : pid(%d)", pid);
673         }
674
675         dbus_message_append_args(msg,
676                                                          DBUS_TYPE_INT32, &pid,
677                                                          DBUS_TYPE_INT32, &cmd_type,
678                                                          DBUS_TYPE_INVALID);
679
680         DBusError err;
681         dbus_error_init(&err);
682
683         DBusMessage* result_msg;
684         int result = VC_ERROR_OPERATION_FAILED;
685
686         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
687         dbus_message_unref(msg);
688
689         if (dbus_error_is_set(&err)) {
690                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
691                 dbus_error_free(&err);
692         }
693
694         if (NULL != result_msg) {
695                 dbus_message_get_args(result_msg, &err,
696                                                           DBUS_TYPE_INT32, &result,
697                                                           DBUS_TYPE_INVALID);
698
699                 if (dbus_error_is_set(&err)) {
700                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
701                         dbus_error_free(&err);
702                         result = VC_ERROR_OPERATION_FAILED;
703                 }
704                 dbus_message_unref(result_msg);
705
706                 if (0 == result) {
707                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc set command : result = %d", result);
708                 } else {
709                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc set command : result = %d", result);
710                 }
711         } else {
712                 SLOG(LOG_ERROR, TAG_VCC, "@@ Result message is NULL");
713                 vc_dbus_reconnect();
714                 result = VC_ERROR_TIMED_OUT;
715         }
716
717         return result;
718 }
719
720 int vc_dbus_request_unset_command(int pid, vc_cmd_type_e cmd_type)
721 {
722         if (0 != __dbus_check()) {
723                 return VC_ERROR_OPERATION_FAILED;
724         }
725
726         DBusMessage* msg;
727
728         msg = dbus_message_new_method_call(
729                           VC_SERVER_SERVICE_NAME,
730                           VC_SERVER_SERVICE_OBJECT_PATH,
731                           VC_SERVER_SERVICE_INTERFACE,
732                           VC_METHOD_UNSET_COMMAND);
733
734         if (NULL == msg) {
735                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc unset command : Fail to make message");
736                 return VC_ERROR_OPERATION_FAILED;
737         } else {
738                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc unset command : pid(%d), type(%d)", pid, cmd_type);
739         }
740
741         dbus_message_append_args(msg,
742                                                          DBUS_TYPE_INT32, &pid,
743                                                          DBUS_TYPE_INT32, &cmd_type,
744                                                          DBUS_TYPE_INVALID);
745
746         DBusError err;
747         dbus_error_init(&err);
748
749         DBusMessage* result_msg;
750         int result = VC_ERROR_OPERATION_FAILED;
751
752         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
753         dbus_message_unref(msg);
754
755         if (dbus_error_is_set(&err)) {
756                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
757                 dbus_error_free(&err);
758         }
759
760         if (NULL != result_msg) {
761                 dbus_message_get_args(result_msg, &err,
762                                                           DBUS_TYPE_INT32, &result,
763                                                           DBUS_TYPE_INVALID);
764
765                 if (dbus_error_is_set(&err)) {
766                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
767                         dbus_error_free(&err);
768                         result = VC_ERROR_OPERATION_FAILED;
769                 }
770                 dbus_message_unref(result_msg);
771
772                 if (0 == result) {
773                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc unset command : result = %d", result);
774                 } else {
775                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc unset command : result = %d", result);
776                 }
777         } else {
778                 SLOG(LOG_ERROR, TAG_VCC, "@@ Result message is NULL");
779                 vc_dbus_reconnect();
780                 result = VC_ERROR_TIMED_OUT;
781         }
782
783         return result;
784 }
785
786 int vc_dbus_set_foreground(int pid, bool value)
787 {
788         if (0 != __dbus_check()) {
789                 return VC_ERROR_OPERATION_FAILED;
790         }
791
792         DBusMessage* msg = NULL;
793         int tmp_value = 0;
794
795         tmp_value = (int)value;
796
797         msg = dbus_message_new_signal(
798                 VC_MANAGER_SERVICE_OBJECT_PATH,
799                 VC_MANAGER_SERVICE_INTERFACE,
800                 VCC_MANAGER_METHOD_SET_FOREGROUND);
801
802         if (NULL == msg) {
803                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc set foreground to manager : Fail to make message");
804                 return VC_ERROR_OPERATION_FAILED;
805         } else {
806                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc set foreground to manager : client pid(%d), value(%s)", pid, tmp_value ? "true" : "false");
807         }
808
809         dbus_message_append_args(msg,
810                 DBUS_TYPE_INT32, &pid,
811                 DBUS_TYPE_INT32, &tmp_value,
812                 DBUS_TYPE_INVALID);
813
814         if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
815                 SLOG(LOG_ERROR, TAG_VCC, "[Dbus ERROR] Fail to Send");
816                 return VC_ERROR_OPERATION_FAILED;
817         }
818
819         dbus_message_unref(msg);
820
821         msg = NULL;
822         msg = dbus_message_new_method_call(
823                 VC_SERVER_SERVICE_NAME,
824                 VC_SERVER_SERVICE_OBJECT_PATH,
825                 VC_SERVER_SERVICE_INTERFACE,
826                 VC_METHOD_SET_FOREGROUND);
827
828         if (NULL == msg) {
829                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc set foreground to daemon : Fail to make message");
830                 return VC_ERROR_OPERATION_FAILED;
831         } else {
832                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc set foreground to daemon : client pid(%d), value(%s)", pid, tmp_value ? "true" : "false");
833         }
834
835         dbus_message_append_args(msg,
836                 DBUS_TYPE_INT32, &pid,
837                 DBUS_TYPE_INT32, &tmp_value,
838                 DBUS_TYPE_INVALID);
839
840         dbus_message_set_no_reply(msg, TRUE);
841
842         if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
843                 SLOG(LOG_ERROR, TAG_VCC, "[Dbus ERROR] Fail to Send");
844                 return VC_ERROR_OPERATION_FAILED;
845         }
846
847         dbus_connection_flush(g_conn_sender);
848
849         dbus_message_unref(msg);
850
851         return 0;
852 }
853
854 int vc_dbus_set_server_dialog(int pid, const char* app_id, const char* credential)
855 {
856         if (NULL == g_conn_sender) {
857                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] NULL connection");
858                 if (0 != vc_dbus_reconnect()) {
859                         SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Fail to reconnect");
860                 }
861         }
862
863         DBusMessage* msg;
864         msg = dbus_message_new_method_call(
865                 VC_SERVER_SERVICE_NAME,
866                 VC_SERVER_SERVICE_OBJECT_PATH,
867                 VC_SERVER_SERVICE_INTERFACE,
868                 VC_METHOD_SET_SERVER_DIALOG);
869
870         if (NULL == msg) {
871                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc set server dialog : Fail to make message");
872                 return VC_ERROR_OPERATION_FAILED;
873         } else {
874                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc set server dialog : pid(%d), app_id(%s)", pid, app_id);
875         }
876
877         dbus_message_append_args(msg,
878                                 DBUS_TYPE_INT32, &pid,
879                                 DBUS_TYPE_STRING, &app_id,
880                                 DBUS_TYPE_STRING, &credential,
881                                 DBUS_TYPE_INVALID);
882
883         DBusError err;
884         dbus_error_init(&err);
885
886         DBusMessage* result_msg;
887         int result = VC_ERROR_OPERATION_FAILED;
888
889         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
890         dbus_message_unref(msg);
891
892         if (dbus_error_is_set(&err)) {
893                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
894                 dbus_error_free(&err);
895         }
896
897         if (NULL != result_msg) {
898                 dbus_message_get_args(result_msg, &err,
899                                   DBUS_TYPE_INT32, &result,
900                                   DBUS_TYPE_INVALID);
901
902                 if (dbus_error_is_set(&err)) {
903                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
904                         dbus_error_free(&err);
905                         result = VC_ERROR_OPERATION_FAILED;
906                 }
907                 dbus_message_unref(result_msg);
908
909                 if (0 == result) {
910                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc set server dialog : result = %d", result);
911                 } else {
912                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc set server dialog : result = %d", result);
913                 }
914         } else {
915                 SLOG(LOG_ERROR, TAG_VCC, "@@ Result message is NULL");
916                 vc_dbus_reconnect();
917                 result = VC_ERROR_TIMED_OUT;
918         }
919
920         return result;
921 }
922
923 int vc_dbus_request_dialog(int pid, const char* disp_text, const char* utt_text, bool continuous)
924 {
925         if (NULL == g_conn_sender) {
926                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] NULL connection");
927                 if (0 != vc_dbus_reconnect()) {
928                         SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Fail to reconnect");
929                 }
930         }
931
932         DBusMessage* msg;
933         int tmp_continue = 0;
934
935         tmp_continue = (int)continuous;
936
937         msg = dbus_message_new_method_call(
938                 VC_SERVER_SERVICE_NAME,
939                 VC_SERVER_SERVICE_OBJECT_PATH,
940                 VC_SERVER_SERVICE_INTERFACE,
941                 VC_METHOD_DIALOG);
942
943         if (NULL == msg) {
944                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc request dialog to manager : Fail to make message");
945                 return VC_ERROR_OPERATION_FAILED;
946         } else {
947                 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);
948         }
949
950         dbus_message_append_args(msg,
951                                 DBUS_TYPE_INT32, &pid,
952                                 DBUS_TYPE_STRING, &disp_text,
953                                 DBUS_TYPE_STRING, &utt_text,
954                                 DBUS_TYPE_INT32, &tmp_continue,
955                                 DBUS_TYPE_INVALID);
956
957         dbus_message_set_no_reply(msg, TRUE);
958
959         if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
960                 SLOG(LOG_ERROR, TAG_VCC, "[Dbus ERROR] Fail to Send");
961                 return VC_ERROR_OPERATION_FAILED;
962         } else {
963                 SLOG(LOG_DEBUG, TAG_VCC, "[Dbus DEBUG] Success to Send");
964                 dbus_connection_flush(g_conn_sender);
965         }
966
967         dbus_message_unref(msg);
968
969         return 0;
970
971 /*
972         DBusError err;
973         dbus_error_init(&err);
974
975         DBusMessage* result_msg;
976         int result = VC_ERROR_OPERATION_FAILED;
977
978         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
979         dbus_message_unref(msg);
980
981         if (dbus_error_is_set(&err)) {
982                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
983                 dbus_error_free(&err);
984         }
985
986         if (NULL != result_msg) {
987                 dbus_message_get_args(result_msg, &err,
988                                         DBUS_TYPE_INT32, &result,
989                                         DBUS_TYPE_INVALID);
990
991                 if (dbus_error_is_set(&err)) {
992                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
993                         dbus_error_free(&err);
994                         result = VC_ERROR_OPERATION_FAILED;
995                 }
996                 dbus_message_unref(result_msg);
997
998                 if (0 == result) {
999                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc set command : result = %d", result);
1000                 } else {
1001                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc set command : result = %d", result);
1002                 }
1003         } else {
1004                 SLOG(LOG_ERROR, TAG_VCC, "@@ Result message is NULL");
1005                 vc_dbus_reconnect();
1006                 result = VC_ERROR_TIMED_OUT;
1007         }
1008
1009         return result;
1010 */
1011 }
1012
1013 int vc_dbus_request_is_system_command_valid(int pid, bool* is_sys_cmd_valid)
1014 {
1015         if (NULL == g_conn_sender) {
1016                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] NULL connection");
1017                 if (0 != vc_dbus_reconnect()) {
1018                         SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Fail to reconnect");
1019                 }
1020         }
1021
1022         DBusMessage* msg;
1023
1024         msg = dbus_message_new_method_call(
1025                           VC_SERVER_SERVICE_NAME,
1026                           VC_SERVER_SERVICE_OBJECT_PATH,
1027                           VC_SERVER_SERVICE_INTERFACE,
1028                           VC_METHOD_IS_SYS_COMMAND_VALID);
1029
1030         if (NULL == msg) {
1031                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc is system command valid : Fail to make message");
1032                 return VC_ERROR_OPERATION_FAILED;
1033         } else {
1034                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc is system command valid : pid(%d)", pid);
1035         }
1036
1037         dbus_message_append_args(msg,
1038                                 DBUS_TYPE_INT32, &pid,
1039                                 DBUS_TYPE_INVALID);
1040
1041         DBusError err;
1042         dbus_error_init(&err);
1043         int tmp_sys_cmd = 0;
1044
1045         DBusMessage* result_msg;
1046         int result = VC_ERROR_OPERATION_FAILED;
1047
1048         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
1049         dbus_message_unref(msg);
1050
1051         if (dbus_error_is_set(&err)) {
1052                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
1053                 dbus_error_free(&err);
1054         }
1055
1056         if (NULL != result_msg) {
1057                 dbus_message_get_args(result_msg, &err,
1058                                         DBUS_TYPE_INT32, &result,
1059                                         DBUS_TYPE_INT32, &tmp_sys_cmd,
1060                                         DBUS_TYPE_INVALID);
1061
1062                 if (dbus_error_is_set(&err)) {
1063                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
1064                         dbus_error_free(&err);
1065                         result = VC_ERROR_OPERATION_FAILED;
1066                 }
1067                 dbus_message_unref(result_msg);
1068
1069                 if (0 == result) {
1070                         *is_sys_cmd_valid = (bool)tmp_sys_cmd;
1071                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc is system command valid : result = %d, is_sys_cmd_valid = %d", result, *is_sys_cmd_valid);
1072                 } else {
1073                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc is system command valid : result = %d", result);
1074                 }
1075         } else {
1076                 SLOG(LOG_ERROR, TAG_VCC, "@@ Result message is NULL");
1077                 vc_dbus_reconnect();
1078                 result = VC_ERROR_TIMED_OUT;
1079         }
1080
1081         return result;
1082 }
1083
1084
1085 #if 0
1086 int vc_dbus_request_start(int pid, int silence)
1087 {
1088         DBusMessage* msg;
1089
1090         /* create a signal & check for errors */
1091         msg = dbus_message_new_method_call(
1092                           VC_SERVER_SERVICE_NAME,
1093                           VC_SERVER_SERVICE_OBJECT_PATH,
1094                           VC_SERVER_SERVICE_INTERFACE,
1095                           VC_METHOD_REQUEST_START);
1096
1097         if (NULL == msg) {
1098                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc start : Fail to make message ");
1099                 return VC_ERROR_OPERATION_FAILED;
1100         } else {
1101                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc start : pid(%d), silence(%d)", pid, silence);
1102         }
1103
1104         DBusMessageIter args;
1105         dbus_message_iter_init_append(msg, &args);
1106
1107         /* Append result*/
1108         dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(pid));
1109         dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(silence));
1110
1111         DBusError err;
1112         dbus_error_init(&err);
1113
1114         DBusMessage* result_msg;
1115         int result = VC_ERROR_OPERATION_FAILED;
1116
1117         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
1118         dbus_message_unref(msg);
1119
1120         if (dbus_error_is_set(&err)) {
1121                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
1122                 dbus_error_free(&err);
1123         }
1124
1125         if (NULL != result_msg) {
1126                 dbus_message_get_args(result_msg, &err,
1127                                                           DBUS_TYPE_INT32, &result,
1128                                                           DBUS_TYPE_INVALID);
1129
1130                 if (dbus_error_is_set(&err)) {
1131                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
1132                         dbus_error_free(&err);
1133                         result = VC_ERROR_OPERATION_FAILED;
1134                 }
1135                 dbus_message_unref(result_msg);
1136
1137                 if (0 == result) {
1138                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc start : result = %d", result);
1139                 } else {
1140                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc start : result = %d", result);
1141                 }
1142         } else {
1143                 SLOG(LOG_DEBUG, TAG_VCC, "@@ Result Message is NULL");
1144                 vc_dbus_reconnect();
1145                 result = VC_ERROR_TIMED_OUT;
1146         }
1147
1148         return result;
1149 }
1150
1151 int vc_dbus_request_stop(int pid)
1152 {
1153         DBusMessage* msg;
1154
1155         /* create a signal & check for errors */
1156         msg = dbus_message_new_method_call(
1157                           VC_SERVER_SERVICE_NAME,
1158                           VC_SERVER_SERVICE_OBJECT_PATH,
1159                           VC_SERVER_SERVICE_INTERFACE,
1160                           VC_METHOD_REQUEST_STOP);
1161
1162         if (NULL == msg) {
1163                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc stop : Fail to make message ");
1164                 return VC_ERROR_OPERATION_FAILED;
1165         } else {
1166                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc stop : pid(%d)", pid);
1167         }
1168
1169         dbus_message_append_args(msg,
1170                                                          DBUS_TYPE_INT32, &pid,
1171                                                          DBUS_TYPE_INVALID);
1172
1173         DBusError err;
1174         dbus_error_init(&err);
1175
1176         DBusMessage* result_msg;
1177         int result = VC_ERROR_OPERATION_FAILED;
1178
1179         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
1180         dbus_message_unref(msg);
1181
1182         if (dbus_error_is_set(&err)) {
1183                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
1184                 dbus_error_free(&err);
1185         }
1186
1187         if (NULL != result_msg) {
1188                 dbus_message_get_args(result_msg, &err,
1189                                                           DBUS_TYPE_INT32, &result,
1190                                                           DBUS_TYPE_INVALID);
1191
1192                 if (dbus_error_is_set(&err)) {
1193                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
1194                         dbus_error_free(&err);
1195                         result = VC_ERROR_OPERATION_FAILED;
1196                 }
1197                 dbus_message_unref(result_msg);
1198
1199                 if (0 == result) {
1200                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc stop : result = %d", result);
1201                 } else {
1202                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc stop : result = %d", result);
1203                 }
1204         } else {
1205                 SLOG(LOG_DEBUG, TAG_VCC, "@@ Result Message is NULL");
1206                 vc_dbus_reconnect();
1207                 result = VC_ERROR_TIMED_OUT;
1208         }
1209
1210         return result;
1211 }
1212
1213 int vc_dbus_request_cancel(int pid)
1214 {
1215         DBusMessage* msg;
1216
1217         /* create a signal & check for errors */
1218         msg = dbus_message_new_method_call(
1219                           VC_SERVER_SERVICE_NAME,
1220                           VC_SERVER_SERVICE_OBJECT_PATH,        /* object name of the signal */
1221                           VC_SERVER_SERVICE_INTERFACE,  /* interface name of the signal */
1222                           VC_METHOD_REQUEST_CANCEL);    /* name of the signal */
1223
1224         if (NULL == msg) {
1225                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc cancel : Fail to make message ");
1226                 return VC_ERROR_OPERATION_FAILED;
1227         } else {
1228                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc cancel : 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 cancel : result = %d", result);
1263                 } else {
1264                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc cancel : 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 #endif
1275
1276 /* Authority */
1277 int vc_dbus_request_auth_enable(int pid, int mgr_pid)
1278 {
1279         if (0 != __dbus_check()) {
1280                 return VC_ERROR_OPERATION_FAILED;
1281         }
1282
1283         DBusMessage* msg;
1284
1285         char service_name[64] = {0,};
1286         char object_path[64] = {0,};
1287         char target_if_name[128] = {0,};
1288
1289         snprintf(service_name, 64, "%s%d", VC_MANAGER_SERVICE_NAME, mgr_pid);
1290         snprintf(object_path, 64, "%s", VC_MANAGER_SERVICE_OBJECT_PATH);
1291         snprintf(target_if_name, 128, "%s%d", VC_MANAGER_SERVICE_INTERFACE, mgr_pid);
1292
1293         /* create a signal & check for errors */
1294         msg = dbus_message_new_method_call(
1295                           service_name,
1296                           object_path,  /* object name of the signal */
1297                           target_if_name,       /* interface name of the signal */
1298                           VC_METHOD_AUTH_ENABLE);       /* name of the signal */
1299
1300         if (NULL == msg) {
1301                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc auth enable : Fail to make message ");
1302                 return VC_ERROR_OPERATION_FAILED;
1303         } else {
1304                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc auth enable : pid(%d)", pid);
1305         }
1306
1307         dbus_message_append_args(msg,
1308                                                          DBUS_TYPE_INT32, &pid,
1309                                                          DBUS_TYPE_INVALID);
1310
1311         DBusError err;
1312         dbus_error_init(&err);
1313
1314         DBusMessage* result_msg;
1315         int result = VC_ERROR_OPERATION_FAILED;
1316
1317         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
1318         dbus_message_unref(msg);
1319
1320         if (dbus_error_is_set(&err)) {
1321                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
1322                 dbus_error_free(&err);
1323         }
1324
1325         if (NULL != result_msg) {
1326                 dbus_message_get_args(result_msg, &err,
1327                                                           DBUS_TYPE_INT32, &result,
1328                                                           DBUS_TYPE_INVALID);
1329
1330                 if (dbus_error_is_set(&err)) {
1331                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
1332                         dbus_error_free(&err);
1333                         result = VC_ERROR_OPERATION_FAILED;
1334                 }
1335                 dbus_message_unref(result_msg);
1336
1337                 if (0 == result) {
1338                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc auth enable : result = %d", result);
1339                 } else {
1340                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc auth enable : result = %d", result);
1341                 }
1342         } else {
1343                 SLOG(LOG_DEBUG, TAG_VCC, "@@ Result Message is NULL");
1344                 vc_dbus_reconnect();
1345                 result = VC_ERROR_TIMED_OUT;
1346         }
1347
1348         return result;
1349 }
1350
1351 int vc_dbus_request_auth_disable(int pid, int mgr_pid)
1352 {
1353         if (0 != __dbus_check()) {
1354                 return VC_ERROR_OPERATION_FAILED;
1355         }
1356
1357         DBusMessage* msg;
1358
1359         char service_name[64] = {0,};
1360         char object_path[64] = {0,};
1361         char target_if_name[128] = {0,};
1362
1363         snprintf(service_name, 64, "%s%d", VC_MANAGER_SERVICE_NAME, mgr_pid);
1364         snprintf(object_path, 64, "%s", VC_MANAGER_SERVICE_OBJECT_PATH);
1365         snprintf(target_if_name, 128, "%s%d", VC_MANAGER_SERVICE_INTERFACE, mgr_pid);
1366
1367         /* create a signal & check for errors */
1368         msg = dbus_message_new_method_call(
1369                           service_name,
1370                           object_path,  /* object name of the signal */
1371                           target_if_name,       /* interface name of the signal */
1372                           VC_METHOD_AUTH_DISABLE);      /* name of the signal */
1373
1374         if (NULL == msg) {
1375                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc auth disable : Fail to make message ");
1376                 return VC_ERROR_OPERATION_FAILED;
1377         } else {
1378                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc auth disable : pid(%d)", pid);
1379         }
1380
1381         dbus_message_append_args(msg,
1382                                                          DBUS_TYPE_INT32, &pid,
1383                                                          DBUS_TYPE_INVALID);
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 disable : result = %d", result);
1413                 } else {
1414                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc auth disable : 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_start(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_START);
1447
1448         if (NULL == msg) {
1449                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc auth start : Fail to make message ");
1450                 return VC_ERROR_OPERATION_FAILED;
1451         } else {
1452                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc auth start : pid(%d)", pid);
1453         }
1454
1455         DBusMessageIter args;
1456         dbus_message_iter_init_append(msg, &args);
1457
1458         /* Append result*/
1459         dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(pid));
1460
1461         DBusError err;
1462         dbus_error_init(&err);
1463
1464         DBusMessage* result_msg;
1465         int result = VC_ERROR_OPERATION_FAILED;
1466
1467         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
1468         dbus_message_unref(msg);
1469
1470         if (dbus_error_is_set(&err)) {
1471                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
1472                 dbus_error_free(&err);
1473         }
1474
1475         if (NULL != result_msg) {
1476                 dbus_message_get_args(result_msg, &err,
1477                                                           DBUS_TYPE_INT32, &result,
1478                                                           DBUS_TYPE_INVALID);
1479
1480                 if (dbus_error_is_set(&err)) {
1481                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
1482                         dbus_error_free(&err);
1483                         result = VC_ERROR_OPERATION_FAILED;
1484                 }
1485                 dbus_message_unref(result_msg);
1486
1487                 if (0 == result) {
1488                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc auth start : result = %d", result);
1489                 } else {
1490                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc auth start : result = %d", result);
1491                 }
1492         } else {
1493                 SLOG(LOG_DEBUG, TAG_VCC, "@@ Result Message is NULL");
1494                 vc_dbus_reconnect();
1495                 result = VC_ERROR_TIMED_OUT;
1496         }
1497
1498         return result;
1499 }
1500
1501 int vc_dbus_request_auth_stop(int pid, int mgr_pid)
1502 {
1503         if (0 != __dbus_check()) {
1504                 return VC_ERROR_OPERATION_FAILED;
1505         }
1506
1507         DBusMessage* msg;
1508
1509         char service_name[64] = {0,};
1510         char object_path[64] = {0,};
1511         char target_if_name[128] = {0,};
1512
1513         snprintf(service_name, 64, "%s%d", VC_MANAGER_SERVICE_NAME, mgr_pid);
1514         snprintf(object_path, 64, "%s", VC_MANAGER_SERVICE_OBJECT_PATH);
1515         snprintf(target_if_name, 128, "%s%d", VC_MANAGER_SERVICE_INTERFACE, mgr_pid);
1516
1517         /* create a signal & check for errors */
1518         msg = dbus_message_new_method_call(
1519                           service_name,
1520                           object_path,
1521                           target_if_name,
1522                           VC_METHOD_AUTH_STOP);
1523
1524         if (NULL == msg) {
1525                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc auth stop : Fail to make message ");
1526                 return VC_ERROR_OPERATION_FAILED;
1527         } else {
1528                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc auth stop : pid(%d)", pid);
1529         }
1530
1531         dbus_message_append_args(msg,
1532                                                          DBUS_TYPE_INT32, &pid,
1533                                                          DBUS_TYPE_INVALID);
1534
1535         DBusError err;
1536         dbus_error_init(&err);
1537
1538         DBusMessage* result_msg;
1539         int result = VC_ERROR_OPERATION_FAILED;
1540
1541         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
1542         dbus_message_unref(msg);
1543
1544         if (dbus_error_is_set(&err)) {
1545                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
1546                 dbus_error_free(&err);
1547         }
1548
1549         if (NULL != result_msg) {
1550                 dbus_message_get_args(result_msg, &err,
1551                                                           DBUS_TYPE_INT32, &result,
1552                                                           DBUS_TYPE_INVALID);
1553
1554                 if (dbus_error_is_set(&err)) {
1555                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
1556                         dbus_error_free(&err);
1557                         result = VC_ERROR_OPERATION_FAILED;
1558                 }
1559                 dbus_message_unref(result_msg);
1560
1561                 if (0 == result) {
1562                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc auth stop : result = %d", result);
1563                 } else {
1564                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc auth stop : result = %d", result);
1565                 }
1566         } else {
1567                 SLOG(LOG_DEBUG, TAG_VCC, "@@ Result Message is NULL");
1568                 vc_dbus_reconnect();
1569                 result = VC_ERROR_TIMED_OUT;
1570         }
1571
1572         return result;
1573 }
1574
1575 int vc_dbus_request_auth_cancel(int pid, int mgr_pid)
1576 {
1577         if (0 != __dbus_check()) {
1578                 return VC_ERROR_OPERATION_FAILED;
1579         }
1580
1581         DBusMessage* msg;
1582
1583         char service_name[64] = {0,};
1584         char object_path[64] = {0,};
1585         char target_if_name[128] = {0,};
1586
1587         snprintf(service_name, 64, "%s%d", VC_MANAGER_SERVICE_NAME, mgr_pid);
1588         snprintf(object_path, 64, "%s", VC_MANAGER_SERVICE_OBJECT_PATH);
1589         snprintf(target_if_name, 128, "%s%d", VC_MANAGER_SERVICE_INTERFACE, mgr_pid);
1590
1591         /* create a signal & check for errors */
1592         msg = dbus_message_new_method_call(
1593                           service_name,
1594                           object_path,  /* object name of the signal */
1595                           target_if_name,       /* interface name of the signal */
1596                           VC_METHOD_AUTH_CANCEL);       /* name of the signal */
1597
1598         if (NULL == msg) {
1599                 SLOG(LOG_ERROR, TAG_VCC, "@@ vc auth cancel : Fail to make message ");
1600                 return VC_ERROR_OPERATION_FAILED;
1601         } else {
1602                 SLOG(LOG_DEBUG, TAG_VCC, "@@ vc auth cancel : pid(%d)", pid);
1603         }
1604
1605         dbus_message_append_args(msg,
1606                                                          DBUS_TYPE_INT32, &pid,
1607                                                          DBUS_TYPE_INVALID);
1608
1609         DBusError err;
1610         dbus_error_init(&err);
1611
1612         DBusMessage* result_msg;
1613         int result = VC_ERROR_OPERATION_FAILED;
1614
1615         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
1616         dbus_message_unref(msg);
1617
1618         if (dbus_error_is_set(&err)) {
1619                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
1620                 dbus_error_free(&err);
1621         }
1622
1623         if (NULL != result_msg) {
1624                 dbus_message_get_args(result_msg, &err,
1625                                                           DBUS_TYPE_INT32, &result,
1626                                                           DBUS_TYPE_INVALID);
1627
1628                 if (dbus_error_is_set(&err)) {
1629                         SLOG(LOG_ERROR, TAG_VCC, "@@ Get arguments error (%s)", err.message);
1630                         dbus_error_free(&err);
1631                         result = VC_ERROR_OPERATION_FAILED;
1632                 }
1633                 dbus_message_unref(result_msg);
1634
1635                 if (0 == result) {
1636                         SLOG(LOG_DEBUG, TAG_VCC, "@@ vc auth cancel : result = %d", result);
1637                 } else {
1638                         SLOG(LOG_ERROR, TAG_VCC, "@@ vc auth cancel : result = %d", result);
1639                 }
1640         } else {
1641                 SLOG(LOG_DEBUG, TAG_VCC, "@@ Result Message is NULL");
1642                 vc_dbus_reconnect();
1643                 result = VC_ERROR_TIMED_OUT;
1644         }
1645
1646         return result;
1647 }