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