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