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