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