Update IPC for service state and volume, Support recognition mode
[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
18 #include "vc_client.h"
19 #include "vc_dbus.h"
20 #include "vc_main.h"
21
22
23 static int g_waiting_time = 3000;
24
25 static Ecore_Fd_Handler* g_fd_handler = NULL;
26
27 static DBusConnection* g_conn_sender = NULL;
28 static DBusConnection* g_conn_listener = NULL;
29
30 extern int __vc_cb_error(int pid, int reason);
31
32 extern void __vc_cb_result();
33
34 extern int __vc_cb_service_state(int state);
35
36
37 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
38 {
39         if (NULL == g_conn_listener)    return ECORE_CALLBACK_RENEW;
40
41         dbus_connection_read_write_dispatch(g_conn_listener, 50);
42
43         while (1) {
44                 DBusMessage* msg = NULL;
45                 msg = dbus_connection_pop_message(g_conn_listener);
46
47                 /* loop again if we haven't read a message */
48                 if (NULL == msg) {
49                         break;
50                 }
51
52                 DBusError err;
53                 dbus_error_init(&err);
54
55                 char if_name[64] = {0, };
56                 snprintf(if_name, 64, "%s", VC_CLIENT_SERVICE_INTERFACE);
57
58                 if (dbus_message_is_method_call(msg, if_name, VCD_METHOD_HELLO)) {
59                         SLOG(LOG_DEBUG, TAG_VCC, "===== Get Hello");
60                         int pid = 0;
61                         int response = -1;
62
63                         dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &pid, DBUS_TYPE_INVALID);
64
65                         if (dbus_error_is_set(&err)) {
66                                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
67                                 dbus_error_free(&err);
68                         }
69
70                         if (pid > 0) {
71                                 SLOG(LOG_DEBUG, TAG_VCC, "<<<< vc get hello : pid(%d) ", pid);
72                                 response = 1;
73                         } else {
74                                 SLOG(LOG_ERROR, TAG_VCC, "<<<< vc get hello : invalid pid ");
75                         }
76
77                         DBusMessage* reply = NULL;
78                         reply = dbus_message_new_method_return(msg);
79
80                         if (NULL != reply) {
81                                 dbus_message_append_args(reply, DBUS_TYPE_INT32, &response, DBUS_TYPE_INVALID);
82
83                                 if (!dbus_connection_send(g_conn_listener, reply, NULL))
84                                         SLOG(LOG_ERROR, TAG_VCC, ">>>> vc get hello : fail to send reply");
85                                 else
86                                         SLOG(LOG_DEBUG, TAG_VCC, ">>>> vc get hello : result(%d)", response);
87
88                                 dbus_connection_flush(g_conn_listener);
89                                 dbus_message_unref(reply);
90                         } else {
91                                 SLOG(LOG_ERROR, TAG_VCC, ">>>> vc get hello : fail to create reply message");
92                         }
93
94                         SLOG(LOG_DEBUG, TAG_VCC, "=====");
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                         SLOG(LOG_DEBUG, TAG_VCC, " ");
120
121                 } /* VCD_METHOD_RESULT */
122
123                 else if (dbus_message_is_method_call(msg, if_name, VCD_METHOD_ERROR)) {
124                         SLOG(LOG_DEBUG, TAG_VCC, "===== Get Error");
125                         int pid;
126                         int reason;
127                         char* err_msg;
128
129                         dbus_message_get_args(msg, &err,
130                                 DBUS_TYPE_INT32, &pid,
131                                 DBUS_TYPE_INT32, &reason,
132                                 DBUS_TYPE_STRING, &err_msg,
133                                 DBUS_TYPE_INVALID);
134
135                         if (dbus_error_is_set(&err)) {
136                                 SLOG(LOG_ERROR, TAG_VCC, "<<<< vc Get Error message : Get arguments error (%s)", err.message);
137                                 dbus_error_free(&err);
138                         }
139                         else {
140                                 SLOG(LOG_DEBUG, TAG_VCC, "<<<< vc Get Error message : pid(%d), reason(%d), msg(%s)", pid, reason, err_msg);
141                                 __vc_cb_error(pid, reason);
142                         }
143
144                         SLOG(LOG_DEBUG, TAG_VCC, "=====");
145                         SLOG(LOG_DEBUG, TAG_VCC, " ");
146                 } /* VCD_METHOD_ERROR */
147                 
148                 else {
149                         SLOG(LOG_DEBUG, TAG_VCC, "Message is NOT valid");
150                         dbus_message_unref(msg);
151                         break;
152                 }
153
154                 /* free the message */
155                 dbus_message_unref(msg);
156         } /* while(1) */
157
158         return ECORE_CALLBACK_PASS_ON;
159 }
160
161 int vc_dbus_open_connection()
162 {
163         if (NULL != g_conn_sender && NULL != g_conn_listener) {
164                 SLOG(LOG_WARN, TAG_VCC, "already existed connection ");
165                 return 0;
166         }
167
168         DBusError err;
169         int ret;
170
171         /* initialise the error value */
172         dbus_error_init(&err);
173
174         /* connect to the DBUS system bus, and check for errors */
175         g_conn_sender = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
176
177         if (dbus_error_is_set(&err)) {
178                 SLOG(LOG_ERROR, TAG_VCC, "Dbus Connection Error (%s)", err.message);
179                 dbus_error_free(&err);
180         }
181
182         if (NULL == g_conn_sender) {
183                 SLOG(LOG_ERROR, TAG_VCC, "Fail to get dbus connection ");
184                 return VC_ERROR_OPERATION_FAILED;
185         }
186
187         g_conn_listener = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
188
189         if (dbus_error_is_set(&err)) {
190                 SLOG(LOG_ERROR, TAG_VCC, "Dbus Connection Error (%s)", err.message);
191                 dbus_error_free(&err);
192         }
193
194         if (NULL == g_conn_listener) {
195                 SLOG(LOG_ERROR, TAG_VCC, "Fail to get dbus connection ");
196                 return VC_ERROR_OPERATION_FAILED;
197         }
198
199         char service_name[64];
200         memset(service_name, '\0', 64);
201         snprintf(service_name, 64, "%s", VC_CLIENT_SERVICE_NAME);
202
203         SLOG(LOG_DEBUG, TAG_VCC, "service name is %s", service_name);
204
205         /* register our name on the bus, and check for errors */
206         ret = dbus_bus_request_name(g_conn_listener, service_name, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
207
208         if (dbus_error_is_set(&err)) {
209                 SLOG(LOG_ERROR, TAG_VCC, "Name Error (%s)", err.message);
210                 dbus_error_free(&err);
211         }
212
213         if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
214                 SLOG(LOG_ERROR, TAG_VCC, "fail dbus_bus_request_name()");
215                 return -2;
216         }
217
218         if (NULL != g_fd_handler) {
219                 SLOG(LOG_WARN, TAG_VCC, "The handler already exists.");
220                 return 0;
221         }
222
223         char rule[128] = {0, };
224         snprintf(rule, 128, "type='signal',interface='%s'", VC_CLIENT_SERVICE_INTERFACE);
225
226         /* add a rule for which messages we want to see */
227         dbus_bus_add_match(g_conn_listener, rule, &err);
228         dbus_connection_flush(g_conn_listener);
229
230         if (dbus_error_is_set(&err)) {
231                 SLOG(LOG_ERROR, TAG_VCC, "Match Error (%s)", err.message);
232                 dbus_error_free(&err);
233                 return VC_ERROR_OPERATION_FAILED;
234         }
235
236         int fd = 0;
237         if (1 != dbus_connection_get_unix_fd(g_conn_listener, &fd)) {
238                 SLOG(LOG_ERROR, TAG_VCC, "fail to get fd from dbus ");
239                 return VC_ERROR_OPERATION_FAILED;
240         } else {
241                 SLOG(LOG_DEBUG, TAG_VCC, "Get fd from dbus : %d", fd);
242         }
243
244         g_fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)listener_event_callback, g_conn_listener, NULL, NULL);
245         if (NULL == g_fd_handler) {
246                 SLOG(LOG_ERROR, TAG_VCC, "fail to get fd handler from ecore ");
247                 return VC_ERROR_OPERATION_FAILED;
248         }
249
250         return 0;
251 }
252
253 int vc_dbus_close_connection()
254 {
255         DBusError err;
256         dbus_error_init(&err);
257
258         if (NULL != g_fd_handler) {
259                 ecore_main_fd_handler_del(g_fd_handler);
260                 g_fd_handler = NULL;
261         }
262
263         int pid = getpid();
264
265         char service_name[64];
266         memset(service_name, '\0', 64);
267         snprintf(service_name, 64, "%s", VC_CLIENT_SERVICE_NAME);
268
269         dbus_bus_release_name(g_conn_listener, service_name, &err);
270
271         if (dbus_error_is_set(&err)) {
272                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
273                 dbus_error_free(&err);
274         }
275
276         g_conn_sender = NULL;
277         g_conn_listener = NULL;
278
279         return 0;
280 }
281
282 int vc_dbus_reconnect()
283 {
284         bool sender_connected = dbus_connection_get_is_connected(g_conn_sender);
285         bool listener_connected = dbus_connection_get_is_connected(g_conn_listener);
286         SLOG(LOG_DEBUG, TAG_VCC, "[DBUS] Sender(%s) Listener(%s)",
287                  sender_connected ? "Connected" : "Not connected", listener_connected ? "Connected" : "Not connected");
288
289         if (false == sender_connected || false == listener_connected) {
290                 vc_dbus_close_connection();
291
292                 if (0 != vc_dbus_open_connection()) {
293                         SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Fail to reconnect");
294                         return -1;
295                 }
296
297                 SLOG(LOG_DEBUG, TAG_VCC, "[DBUS] Reconnect");
298         }
299
300         return 0;
301 }
302
303 int vc_dbus_request_hello()
304 {
305         DBusMessage* msg;
306
307         msg = dbus_message_new_method_call(
308                           VC_SERVER_SERVICE_NAME,
309                           VC_SERVER_SERVICE_OBJECT_PATH,
310                           VC_SERVER_SERVICE_INTERFACE,
311                           VC_METHOD_HELLO);
312
313         if (NULL == msg) {
314                 SLOG(LOG_ERROR, TAG_VCC, ">>>> Request vc hello : Fail to make message");
315                 return VC_ERROR_OPERATION_FAILED;
316         }
317
318         DBusError err;
319         dbus_error_init(&err);
320
321         DBusMessage* result_msg = NULL;
322         int result = 0;
323
324         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, 500, &err);
325
326         if (dbus_error_is_set(&err)) {
327                 dbus_error_free(&err);
328         }
329
330         dbus_message_unref(msg);
331
332         if (NULL != result_msg) {
333                 dbus_message_unref(result_msg);
334                 result = 0;
335         } else {
336                 result = VC_ERROR_TIMED_OUT;
337         }
338
339         return result;
340 }
341
342
343 int vc_dbus_request_initialize(int pid, int* mgr_pid, int* service_state)
344 {
345         DBusMessage* msg;
346
347         msg = dbus_message_new_method_call(
348                           VC_SERVER_SERVICE_NAME,
349                           VC_SERVER_SERVICE_OBJECT_PATH,
350                           VC_SERVER_SERVICE_INTERFACE,
351                           VC_METHOD_INITIALIZE);
352
353         if (NULL == msg) {
354                 SLOG(LOG_ERROR, TAG_VCC, ">>>> vc initialize : Fail to make message ");
355                 return VC_ERROR_OPERATION_FAILED;
356         } else {
357                 SLOG(LOG_DEBUG, TAG_VCC, ">>>> vc initialize : pid(%d)", pid);
358         }
359
360         dbus_message_append_args(msg,
361                         DBUS_TYPE_INT32, &pid,
362                         DBUS_TYPE_INVALID);
363
364         DBusError err;
365         dbus_error_init(&err);
366
367         DBusMessage* result_msg;
368         int result = VC_ERROR_OPERATION_FAILED;
369
370         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
371         dbus_message_unref(msg);
372
373         if (dbus_error_is_set(&err)) {
374                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
375                 dbus_error_free(&err);
376         }
377
378         if (NULL != result_msg) {
379                 int tmp = -1;
380                 int tmp_service_state = 0;
381                 dbus_message_get_args(result_msg, &err,
382                         DBUS_TYPE_INT32, &result,
383                         DBUS_TYPE_INT32, &tmp,
384                         DBUS_TYPE_INT32, &tmp_service_state,
385                         DBUS_TYPE_INVALID);
386
387                 if (dbus_error_is_set(&err)) {
388                         SLOG(LOG_ERROR, TAG_VCC, "<<<< Get arguments error (%s)", err.message);
389                         dbus_error_free(&err);
390                         result = VC_ERROR_OPERATION_FAILED;
391                 }
392
393                 dbus_message_unref(result_msg);
394
395                 if (0 == result) {
396                         *mgr_pid = tmp;
397                         *service_state = tmp_service_state;
398                         SLOG(LOG_DEBUG, TAG_VCC, "<<<< vc initialize : result = %d mgr = %d service = %d", result, *mgr_pid, *service_state);
399                 } else {
400                         SLOG(LOG_ERROR, TAG_VCC, "<<<< vc initialize : result = %d", result);
401                 }
402         } else {
403                 SLOG(LOG_ERROR, TAG_VCC, "<<<< Result message is NULL ");
404                 vc_dbus_reconnect();
405                 result = VC_ERROR_TIMED_OUT;
406         }
407
408         return result;
409 }
410
411 int vc_dbus_request_finalize(int pid)
412 {
413         DBusMessage* msg;
414
415         msg = dbus_message_new_method_call(
416                           VC_SERVER_SERVICE_NAME,
417                           VC_SERVER_SERVICE_OBJECT_PATH,
418                           VC_SERVER_SERVICE_INTERFACE,
419                           VC_METHOD_FINALIZE);
420
421         if (NULL == msg) {
422                 SLOG(LOG_ERROR, TAG_VCC, ">>>> vc finalize : Fail to make message ");
423                 return VC_ERROR_OPERATION_FAILED;
424         } else {
425                 SLOG(LOG_DEBUG, TAG_VCC, ">>>> vc finalize : pid(%d)", pid);
426         }
427
428         dbus_message_append_args(msg, DBUS_TYPE_INT32, &pid, DBUS_TYPE_INVALID);
429
430         DBusError err;
431         dbus_error_init(&err);
432
433         DBusMessage* result_msg;
434         int result = VC_ERROR_OPERATION_FAILED;
435
436         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
437         dbus_message_unref(msg);
438
439         if (dbus_error_is_set(&err)) {
440                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
441                 dbus_error_free(&err);
442         }
443
444         if (NULL != result_msg) {
445                 dbus_message_get_args(result_msg, &err,
446                                                           DBUS_TYPE_INT32, &result,
447                                                           DBUS_TYPE_INVALID);
448
449                 if (dbus_error_is_set(&err)) {
450                         SLOG(LOG_ERROR, TAG_VCC, "<<<< Get arguments error (%s)", err.message);
451                         dbus_error_free(&err);
452                         result = VC_ERROR_OPERATION_FAILED;
453                 }
454
455                 dbus_message_unref(result_msg);
456
457                 if (0 == result) {
458                         SLOG(LOG_DEBUG, TAG_VCC, "<<<< vc finalize : result = %d", result);
459                 } else {
460                         SLOG(LOG_ERROR, TAG_VCC, "<<<< vc finalize : result = %d", result);
461                 }
462         } else {
463                 SLOG(LOG_ERROR, TAG_VCC, "<<<< Result message is NULL ");
464                 vc_dbus_reconnect();
465                 result = VC_ERROR_TIMED_OUT;
466         }
467
468         return result;
469 }
470
471 int vc_dbus_request_set_exclusive_command(int pid, bool value)
472 {
473         DBusMessage* msg;
474
475         msg = dbus_message_new_method_call(
476                           VC_SERVER_SERVICE_NAME,
477                           VC_SERVER_SERVICE_OBJECT_PATH,
478                           VC_SERVER_SERVICE_INTERFACE,
479                           VC_METHOD_SET_EXCLUSIVE_CMD);
480
481         if (NULL == msg) {
482                 SLOG(LOG_ERROR, TAG_VCC, ">>>> vc set exclusive command : Fail to make message");
483                 return VC_ERROR_OPERATION_FAILED;
484         } else {
485                 SLOG(LOG_DEBUG, TAG_VCC, ">>>> vc set exclusive command : pid(%d)", pid);
486         }
487
488         int temp = value;
489
490         dbus_message_append_args(msg,
491                                                          DBUS_TYPE_INT32, &pid,
492                                                          DBUS_TYPE_INT32, &temp,
493                                                          DBUS_TYPE_INVALID);
494
495         DBusError err;
496         dbus_error_init(&err);
497
498         DBusMessage* result_msg;
499         int result = VC_ERROR_OPERATION_FAILED;
500
501         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
502         dbus_message_unref(msg);
503
504         if (dbus_error_is_set(&err)) {
505                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
506                 dbus_error_free(&err);
507         }
508
509         if (NULL != result_msg) {
510                 dbus_message_get_args(result_msg, &err,
511                                                           DBUS_TYPE_INT32, &result,
512                                                           DBUS_TYPE_INVALID);
513
514                 if (dbus_error_is_set(&err)) {
515                         SLOG(LOG_ERROR, TAG_VCC, "<<<< Get arguments error (%s)", err.message);
516                         dbus_error_free(&err);
517                         result = VC_ERROR_OPERATION_FAILED;
518                 }
519                 dbus_message_unref(result_msg);
520
521                 if (0 == result) {
522                         SLOG(LOG_DEBUG, TAG_VCC, "<<<< vc set exclusive command : result = %d", result);
523                 } else {
524                         SLOG(LOG_DEBUG, TAG_VCC, "<<<< vc set exclusive command : result = %d", result);
525                 }
526         } else {
527                 SLOG(LOG_ERROR, TAG_VCC, "<<<< Result message is NULL");
528                 vc_dbus_reconnect();
529                 result = VC_ERROR_TIMED_OUT;
530         }
531
532         return result;
533 }
534
535 int vc_dbus_request_set_command(int pid, vc_cmd_type_e cmd_type)
536 {
537         DBusMessage* msg;
538
539         msg = dbus_message_new_method_call(
540                           VC_SERVER_SERVICE_NAME,
541                           VC_SERVER_SERVICE_OBJECT_PATH,
542                           VC_SERVER_SERVICE_INTERFACE,
543                           VC_METHOD_SET_COMMAND);
544
545         if (NULL == msg) {
546                 SLOG(LOG_ERROR, TAG_VCC, ">>>> vc set command : Fail to make message");
547                 return VC_ERROR_OPERATION_FAILED;
548         } else {
549                 SLOG(LOG_DEBUG, TAG_VCC, ">>>> vc set command : pid(%d)", pid);
550         }
551
552         dbus_message_append_args(msg,
553                                                          DBUS_TYPE_INT32, &pid,
554                                                          DBUS_TYPE_INT32, &cmd_type,
555                                                          DBUS_TYPE_INVALID);
556
557         DBusError err;
558         dbus_error_init(&err);
559
560         DBusMessage* result_msg;
561         int result = VC_ERROR_OPERATION_FAILED;
562
563         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
564         dbus_message_unref(msg);
565
566         if (dbus_error_is_set(&err)) {
567                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
568                 dbus_error_free(&err);
569         }
570
571         if (NULL != result_msg) {
572                 dbus_message_get_args(result_msg, &err,
573                                                           DBUS_TYPE_INT32, &result,
574                                                           DBUS_TYPE_INVALID);
575
576                 if (dbus_error_is_set(&err)) {
577                         SLOG(LOG_ERROR, TAG_VCC, "<<<< Get arguments error (%s)", err.message);
578                         dbus_error_free(&err);
579                         result = VC_ERROR_OPERATION_FAILED;
580                 }
581                 dbus_message_unref(result_msg);
582
583                 if (0 == result) {
584                         SLOG(LOG_DEBUG, TAG_VCC, "<<<< vc set command : result = %d", result);
585                 } else {
586                         SLOG(LOG_ERROR, TAG_VCC, "<<<< vc set command : result = %d", result);
587                 }
588         } else {
589                 SLOG(LOG_ERROR, TAG_VCC, "<<<< Result message is NULL");
590                 vc_dbus_reconnect();
591                 result = VC_ERROR_TIMED_OUT;
592         }
593
594         return result;
595 }
596
597 int vc_dbus_request_unset_command(int pid, vc_cmd_type_e cmd_type)
598 {
599         DBusMessage* msg;
600
601         msg = dbus_message_new_method_call(
602                           VC_SERVER_SERVICE_NAME,
603                           VC_SERVER_SERVICE_OBJECT_PATH,
604                           VC_SERVER_SERVICE_INTERFACE,
605                           VC_METHOD_UNSET_COMMAND);
606
607         if (NULL == msg) {
608                 SLOG(LOG_ERROR, TAG_VCC, ">>>> vc unset command : Fail to make message");
609                 return VC_ERROR_OPERATION_FAILED;
610         } else {
611                 SLOG(LOG_DEBUG, TAG_VCC, ">>>> vc unset command : pid(%d), type(%d)", pid, cmd_type);
612         }
613
614         dbus_message_append_args(msg,
615                                                          DBUS_TYPE_INT32, &pid,
616                                                          DBUS_TYPE_INT32, &cmd_type,
617                                                          DBUS_TYPE_INVALID);
618
619         DBusError err;
620         dbus_error_init(&err);
621
622         DBusMessage* result_msg;
623         int result = VC_ERROR_OPERATION_FAILED;
624
625         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
626         dbus_message_unref(msg);
627
628         if (dbus_error_is_set(&err)) {
629                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
630                 dbus_error_free(&err);
631         }
632
633         if (NULL != result_msg) {
634                 dbus_message_get_args(result_msg, &err,
635                                                           DBUS_TYPE_INT32, &result,
636                                                           DBUS_TYPE_INVALID);
637
638                 if (dbus_error_is_set(&err)) {
639                         SLOG(LOG_ERROR, TAG_VCC, "<<<< Get arguments error (%s)", err.message);
640                         dbus_error_free(&err);
641                         result = VC_ERROR_OPERATION_FAILED;
642                 }
643                 dbus_message_unref(result_msg);
644
645                 if (0 == result) {
646                         SLOG(LOG_DEBUG, TAG_VCC, "<<<< vc unset command : result = %d", result);
647                 } else {
648                         SLOG(LOG_ERROR, TAG_VCC, "<<<< vc unset command : result = %d", result);
649                 }
650         } else {
651                 SLOG(LOG_ERROR, TAG_VCC, "<<<< Result message is NULL");
652                 vc_dbus_reconnect();
653                 result = VC_ERROR_TIMED_OUT;
654         }
655
656         return result;
657 }
658
659 #if 0
660 int vc_dbus_request_start(int pid, int silence)
661 {
662         DBusMessage* msg;
663
664         /* create a signal & check for errors */
665         msg = dbus_message_new_method_call(
666                           VC_SERVER_SERVICE_NAME,
667                           VC_SERVER_SERVICE_OBJECT_PATH,
668                           VC_SERVER_SERVICE_INTERFACE,
669                           VC_METHOD_REQUEST_START);
670
671         if (NULL == msg) {
672                 SLOG(LOG_ERROR, TAG_VCC, ">>>> vc start : Fail to make message ");
673                 return VC_ERROR_OPERATION_FAILED;
674         } else {
675                 SLOG(LOG_DEBUG, TAG_VCC, ">>>> vc start : pid(%d), silence(%d)", pid, silence);
676         }
677
678         DBusMessageIter args;
679         dbus_message_iter_init_append(msg, &args);
680
681         /* Append result*/
682         dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(pid));
683         dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(silence));
684
685         DBusError err;
686         dbus_error_init(&err);
687
688         DBusMessage* result_msg;
689         int result = VC_ERROR_OPERATION_FAILED;
690
691         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
692         dbus_message_unref(msg);
693
694         if (dbus_error_is_set(&err)) {
695                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
696                 dbus_error_free(&err);
697         }
698
699         if (NULL != result_msg) {
700                 dbus_message_get_args(result_msg, &err,
701                                                           DBUS_TYPE_INT32, &result,
702                                                           DBUS_TYPE_INVALID);
703
704                 if (dbus_error_is_set(&err)) {
705                         SLOG(LOG_ERROR, TAG_VCC, "<<<< Get arguments error (%s)", err.message);
706                         dbus_error_free(&err);
707                         result = VC_ERROR_OPERATION_FAILED;
708                 }
709                 dbus_message_unref(result_msg);
710
711                 if (0 == result) {
712                         SLOG(LOG_DEBUG, TAG_VCC, "<<<< vc start : result = %d", result);
713                 } else {
714                         SLOG(LOG_ERROR, TAG_VCC, "<<<< vc start : result = %d", result);
715                 }
716         } else {
717                 SLOG(LOG_DEBUG, TAG_VCC, "<<<< Result Message is NULL");
718                 vc_dbus_reconnect();
719                 result = VC_ERROR_TIMED_OUT;
720         }
721
722         return result;
723 }
724
725 int vc_dbus_request_stop(int pid)
726 {
727         DBusMessage* msg;
728
729         /* create a signal & check for errors */
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_REQUEST_STOP);
735
736         if (NULL == msg) {
737                 SLOG(LOG_ERROR, TAG_VCC, ">>>> vc stop : Fail to make message ");
738                 return VC_ERROR_OPERATION_FAILED;
739         } else {
740                 SLOG(LOG_DEBUG, TAG_VCC, ">>>> vc stop : pid(%d)", pid);
741         }
742
743         dbus_message_append_args(msg,
744                                                          DBUS_TYPE_INT32, &pid,
745                                                          DBUS_TYPE_INVALID);
746
747         DBusError err;
748         dbus_error_init(&err);
749
750         DBusMessage* result_msg;
751         int result = VC_ERROR_OPERATION_FAILED;
752
753         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
754         dbus_message_unref(msg);
755
756         if (dbus_error_is_set(&err)) {
757                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
758                 dbus_error_free(&err);
759         }
760
761         if (NULL != result_msg) {
762                 dbus_message_get_args(result_msg, &err,
763                                                           DBUS_TYPE_INT32, &result,
764                                                           DBUS_TYPE_INVALID);
765
766                 if (dbus_error_is_set(&err)) {
767                         SLOG(LOG_ERROR, TAG_VCC, "<<<< Get arguments error (%s)", err.message);
768                         dbus_error_free(&err);
769                         result = VC_ERROR_OPERATION_FAILED;
770                 }
771                 dbus_message_unref(result_msg);
772
773                 if (0 == result) {
774                         SLOG(LOG_DEBUG, TAG_VCC, "<<<< vc stop : result = %d", result);
775                 } else {
776                         SLOG(LOG_ERROR, TAG_VCC, "<<<< vc stop : result = %d", result);
777                 }
778         } else {
779                 SLOG(LOG_DEBUG, TAG_VCC, "<<<< Result Message is NULL");
780                 vc_dbus_reconnect();
781                 result = VC_ERROR_TIMED_OUT;
782         }
783
784         return result;
785 }
786
787 int vc_dbus_request_cancel(int pid)
788 {
789         DBusMessage* msg;
790
791         /* create a signal & check for errors */
792         msg = dbus_message_new_method_call(
793                           VC_SERVER_SERVICE_NAME,
794                           VC_SERVER_SERVICE_OBJECT_PATH,        /* object name of the signal */
795                           VC_SERVER_SERVICE_INTERFACE,  /* interface name of the signal */
796                           VC_METHOD_REQUEST_CANCEL);    /* name of the signal */
797
798         if (NULL == msg) {
799                 SLOG(LOG_ERROR, TAG_VCC, ">>>> vc cancel : Fail to make message ");
800                 return VC_ERROR_OPERATION_FAILED;
801         } else {
802                 SLOG(LOG_DEBUG, TAG_VCC, ">>>> vc cancel : pid(%d)", pid);
803         }
804
805         dbus_message_append_args(msg,
806                                                          DBUS_TYPE_INT32, &pid,
807                                                          DBUS_TYPE_INVALID);
808
809         DBusError err;
810         dbus_error_init(&err);
811
812         DBusMessage* result_msg;
813         int result = VC_ERROR_OPERATION_FAILED;
814
815         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
816         dbus_message_unref(msg);
817
818         if (dbus_error_is_set(&err)) {
819                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
820                 dbus_error_free(&err);
821         }
822
823         if (NULL != result_msg) {
824                 dbus_message_get_args(result_msg, &err,
825                                                           DBUS_TYPE_INT32, &result,
826                                                           DBUS_TYPE_INVALID);
827
828                 if (dbus_error_is_set(&err)) {
829                         SLOG(LOG_ERROR, TAG_VCC, "<<<< Get arguments error (%s)", err.message);
830                         dbus_error_free(&err);
831                         result = VC_ERROR_OPERATION_FAILED;
832                 }
833                 dbus_message_unref(result_msg);
834
835                 if (0 == result) {
836                         SLOG(LOG_DEBUG, TAG_VCC, "<<<< vc cancel : result = %d", result);
837                 } else {
838                         SLOG(LOG_ERROR, TAG_VCC, "<<<< vc cancel : result = %d", result);
839                 }
840         } else {
841                 SLOG(LOG_DEBUG, TAG_VCC, "<<<< Result Message is NULL");
842                 vc_dbus_reconnect();
843                 result = VC_ERROR_TIMED_OUT;
844         }
845
846         return result;
847 }
848 #endif
849
850 /* Authority */
851 int vc_dbus_request_auth_enable(int pid, int mgr_pid)
852 {
853         DBusMessage* msg;
854
855         char service_name[64] = {0,};
856         char object_path[64] = {0,};
857         char target_if_name[128] = {0,};
858
859         snprintf(service_name, 64, "%s", VC_MANAGER_SERVICE_NAME);
860         snprintf(object_path, 64, "%s", VC_MANAGER_SERVICE_OBJECT_PATH);
861         snprintf(target_if_name, 128, "%s", VC_MANAGER_SERVICE_INTERFACE);
862
863         /* create a signal & check for errors */
864         msg = dbus_message_new_method_call(
865                           service_name,
866                           object_path,  /* object name of the signal */
867                           target_if_name,       /* interface name of the signal */
868                           VC_METHOD_AUTH_ENABLE);       /* name of the signal */
869
870         if (NULL == msg) {
871                 SLOG(LOG_ERROR, TAG_VCC, ">>>> vc auth enable : Fail to make message ");
872                 return VC_ERROR_OPERATION_FAILED;
873         } else {
874                 SLOG(LOG_DEBUG, TAG_VCC, ">>>> vc auth enable : pid(%d)", pid);
875         }
876
877         dbus_message_append_args(msg,
878                                                          DBUS_TYPE_INT32, &pid,
879                                                          DBUS_TYPE_INVALID);
880
881         DBusError err;
882         dbus_error_init(&err);
883
884         DBusMessage* result_msg;
885         int result = VC_ERROR_OPERATION_FAILED;
886
887         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
888         dbus_message_unref(msg);
889
890         if (dbus_error_is_set(&err)) {
891                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
892                 dbus_error_free(&err);
893         }
894
895         if (NULL != result_msg) {
896                 dbus_message_get_args(result_msg, &err,
897                                                           DBUS_TYPE_INT32, &result,
898                                                           DBUS_TYPE_INVALID);
899
900                 if (dbus_error_is_set(&err)) {
901                         SLOG(LOG_ERROR, TAG_VCC, "<<<< Get arguments error (%s)", err.message);
902                         dbus_error_free(&err);
903                         result = VC_ERROR_OPERATION_FAILED;
904                 }
905                 dbus_message_unref(result_msg);
906
907                 if (0 == result) {
908                         SLOG(LOG_DEBUG, TAG_VCC, "<<<< vc auth enable : result = %d", result);
909                 } else {
910                         SLOG(LOG_ERROR, TAG_VCC, "<<<< vc auth enable : result = %d", result);
911                 }
912         } else {
913                 SLOG(LOG_DEBUG, TAG_VCC, "<<<< Result Message is NULL");
914                 vc_dbus_reconnect();
915                 result = VC_ERROR_TIMED_OUT;
916         }
917
918         return result;
919 }
920
921 int vc_dbus_request_auth_disable(int pid, int mgr_pid)
922 {
923         DBusMessage* msg;
924
925         char service_name[64] = {0,};
926         char object_path[64] = {0,};
927         char target_if_name[128] = {0,};
928
929         snprintf(service_name, 64, "%s", VC_MANAGER_SERVICE_NAME);
930         snprintf(object_path, 64, "%s", VC_MANAGER_SERVICE_OBJECT_PATH);
931         snprintf(target_if_name, 128, "%s", VC_MANAGER_SERVICE_INTERFACE);
932
933         /* create a signal & check for errors */
934         msg = dbus_message_new_method_call(
935                           service_name,
936                           object_path,  /* object name of the signal */
937                           target_if_name,       /* interface name of the signal */
938                           VC_METHOD_AUTH_DISABLE);      /* name of the signal */
939
940         if (NULL == msg) {
941                 SLOG(LOG_ERROR, TAG_VCC, ">>>> vc auth disable : Fail to make message ");
942                 return VC_ERROR_OPERATION_FAILED;
943         } else {
944                 SLOG(LOG_DEBUG, TAG_VCC, ">>>> vc auth disable : pid(%d)", pid);
945         }
946
947         dbus_message_append_args(msg,
948                                                          DBUS_TYPE_INT32, &pid,
949                                                          DBUS_TYPE_INVALID);
950
951         DBusError err;
952         dbus_error_init(&err);
953
954         DBusMessage* result_msg;
955         int result = VC_ERROR_OPERATION_FAILED;
956
957         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
958         dbus_message_unref(msg);
959
960         if (dbus_error_is_set(&err)) {
961                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
962                 dbus_error_free(&err);
963         }
964
965         if (NULL != result_msg) {
966                 dbus_message_get_args(result_msg, &err,
967                                                           DBUS_TYPE_INT32, &result,
968                                                           DBUS_TYPE_INVALID);
969
970                 if (dbus_error_is_set(&err)) {
971                         SLOG(LOG_ERROR, TAG_VCC, "<<<< Get arguments error (%s)", err.message);
972                         dbus_error_free(&err);
973                         result = VC_ERROR_OPERATION_FAILED;
974                 }
975                 dbus_message_unref(result_msg);
976
977                 if (0 == result) {
978                         SLOG(LOG_DEBUG, TAG_VCC, "<<<< vc auth disable : result = %d", result);
979                 } else {
980                         SLOG(LOG_ERROR, TAG_VCC, "<<<< vc auth disable : result = %d", result);
981                 }
982         } else {
983                 SLOG(LOG_DEBUG, TAG_VCC, "<<<< Result Message is NULL");
984                 vc_dbus_reconnect();
985                 result = VC_ERROR_TIMED_OUT;
986         }
987
988         return result;
989 }
990
991 int vc_dbus_request_auth_start(int pid, int mgr_pid)
992 {
993         DBusMessage* msg;
994
995         char service_name[64] = {0,};
996         char object_path[64] = {0,};
997         char target_if_name[128] = {0,};
998
999         snprintf(service_name, 64, "%s", VC_MANAGER_SERVICE_NAME);
1000         snprintf(object_path, 64, "%s", VC_MANAGER_SERVICE_OBJECT_PATH);
1001         snprintf(target_if_name, 128, "%s", VC_MANAGER_SERVICE_INTERFACE);
1002
1003         /* create a signal & check for errors */
1004         msg = dbus_message_new_method_call(
1005                           service_name,
1006                           object_path,
1007                           target_if_name,
1008                           VC_METHOD_AUTH_START);
1009
1010         if (NULL == msg) {
1011                 SLOG(LOG_ERROR, TAG_VCC, ">>>> vc auth start : Fail to make message ");
1012                 return VC_ERROR_OPERATION_FAILED;
1013         } else {
1014                 SLOG(LOG_DEBUG, TAG_VCC, ">>>> vc auth start : pid(%d)", pid);
1015         }
1016
1017         DBusMessageIter args;
1018         dbus_message_iter_init_append(msg, &args);
1019
1020         /* Append result*/
1021         dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(pid));
1022
1023         DBusError err;
1024         dbus_error_init(&err);
1025
1026         DBusMessage* result_msg;
1027         int result = VC_ERROR_OPERATION_FAILED;
1028
1029         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
1030         dbus_message_unref(msg);
1031
1032         if (dbus_error_is_set(&err)) {
1033                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
1034                 dbus_error_free(&err);
1035         }
1036
1037         if (NULL != result_msg) {
1038                 dbus_message_get_args(result_msg, &err,
1039                                                           DBUS_TYPE_INT32, &result,
1040                                                           DBUS_TYPE_INVALID);
1041
1042                 if (dbus_error_is_set(&err)) {
1043                         SLOG(LOG_ERROR, TAG_VCC, "<<<< Get arguments error (%s)", err.message);
1044                         dbus_error_free(&err);
1045                         result = VC_ERROR_OPERATION_FAILED;
1046                 }
1047                 dbus_message_unref(result_msg);
1048
1049                 if (0 == result) {
1050                         SLOG(LOG_DEBUG, TAG_VCC, "<<<< vc auth start : result = %d", result);
1051                 } else {
1052                         SLOG(LOG_ERROR, TAG_VCC, "<<<< vc auth start : result = %d", result);
1053                 }
1054         } else {
1055                 SLOG(LOG_DEBUG, TAG_VCC, "<<<< Result Message is NULL");
1056                 vc_dbus_reconnect();
1057                 result = VC_ERROR_TIMED_OUT;
1058         }
1059
1060         return result;
1061 }
1062
1063 int vc_dbus_request_auth_stop(int pid, int mgr_pid)
1064 {
1065         DBusMessage* msg;
1066
1067         char service_name[64] = {0,};
1068         char object_path[64] = {0,};
1069         char target_if_name[128] = {0,};
1070
1071         snprintf(service_name, 64, "%s", VC_MANAGER_SERVICE_NAME);
1072         snprintf(object_path, 64, "%s", VC_MANAGER_SERVICE_OBJECT_PATH);
1073         snprintf(target_if_name, 128, "%s", VC_MANAGER_SERVICE_INTERFACE);
1074
1075         /* create a signal & check for errors */
1076         msg = dbus_message_new_method_call(
1077                           service_name,
1078                           object_path,
1079                           target_if_name,
1080                           VC_METHOD_AUTH_STOP);
1081
1082         if (NULL == msg) {
1083                 SLOG(LOG_ERROR, TAG_VCC, ">>>> vc auth stop : Fail to make message ");
1084                 return VC_ERROR_OPERATION_FAILED;
1085         } else {
1086                 SLOG(LOG_DEBUG, TAG_VCC, ">>>> vc auth stop : pid(%d)", pid);
1087         }
1088
1089         dbus_message_append_args(msg,
1090                                                          DBUS_TYPE_INT32, &pid,
1091                                                          DBUS_TYPE_INVALID);
1092
1093         DBusError err;
1094         dbus_error_init(&err);
1095
1096         DBusMessage* result_msg;
1097         int result = VC_ERROR_OPERATION_FAILED;
1098
1099         result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
1100         dbus_message_unref(msg);
1101
1102         if (dbus_error_is_set(&err)) {
1103                 SLOG(LOG_ERROR, TAG_VCC, "[ERROR] Dbus Error (%s)", err.message);
1104                 dbus_error_free(&err);
1105         }
1106
1107         if (NULL != result_msg) {
1108                 dbus_message_get_args(result_msg, &err,
1109                                                           DBUS_TYPE_INT32, &result,
1110                                                           DBUS_TYPE_INVALID);
1111
1112                 if (dbus_error_is_set(&err)) {
1113                         SLOG(LOG_ERROR, TAG_VCC, "<<<< Get arguments error (%s)", err.message);
1114                         dbus_error_free(&err);
1115                         result = VC_ERROR_OPERATION_FAILED;
1116                 }
1117                 dbus_message_unref(result_msg);
1118
1119                 if (0 == result) {
1120                         SLOG(LOG_DEBUG, TAG_VCC, "<<<< vc auth stop : result = %d", result);
1121                 } else {
1122                         SLOG(LOG_ERROR, TAG_VCC, "<<<< vc auth stop : result = %d", result);
1123                 }
1124         } else {
1125                 SLOG(LOG_DEBUG, TAG_VCC, "<<<< Result Message is NULL");
1126                 vc_dbus_reconnect();
1127                 result = VC_ERROR_TIMED_OUT;
1128         }
1129
1130         return result;
1131 }
1132
1133 int vc_dbus_request_auth_cancel(int pid, int mgr_pid)
1134 {
1135         DBusMessage* msg;
1136
1137         char service_name[64] = {0,};
1138         char object_path[64] = {0,};
1139         char target_if_name[128] = {0,};
1140
1141         snprintf(service_name, 64, "%s", VC_MANAGER_SERVICE_NAME);
1142         snprintf(object_path, 64, "%s", VC_MANAGER_SERVICE_OBJECT_PATH);
1143         snprintf(target_if_name, 128, "%s", VC_MANAGER_SERVICE_INTERFACE);
1144
1145         /* create a signal & check for errors */
1146         msg = dbus_message_new_method_call(
1147                           service_name,
1148                           object_path,  /* object name of the signal */
1149                           target_if_name,       /* interface name of the signal */
1150                           VC_METHOD_AUTH_CANCEL);       /* name of the signal */
1151
1152         if (NULL == msg) {
1153                 SLOG(LOG_ERROR, TAG_VCC, ">>>> vc auth cancel : Fail to make message ");
1154                 return VC_ERROR_OPERATION_FAILED;
1155         } else {
1156                 SLOG(LOG_DEBUG, TAG_VCC, ">>>> vc auth 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 auth cancel : result = %d", result);
1191                 } else {
1192                         SLOG(LOG_ERROR, TAG_VCC, "<<<< vc auth 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 }