Add restore logic and function to get nlp info and pre result
[platform/core/uifw/voice-control.git] / client / vc_widget_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_main.h"
19 #include "vc_widget_client.h"
20 #include "vc_widget_dbus.h"
21
22
23 static int g_w_waiting_time = 3000;
24
25 static Ecore_Fd_Handler* g_w_fd_handler = NULL;
26
27 static DBusConnection* g_w_conn_sender = NULL;
28 static DBusConnection* g_w_conn_listener = NULL;
29
30 extern int __vc_widget_cb_error(int pid, int reason);
31
32 extern void __vc_widget_cb_show_tooltip(int pid, bool show);
33
34 extern void __vc_widget_cb_result();
35
36 extern int __vc_widget_cb_service_state(int state);
37
38
39 static Eina_Bool widget_listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
40 {
41         if (NULL == g_w_conn_listener)  return ECORE_CALLBACK_RENEW;
42
43         dbus_connection_read_write_dispatch(g_w_conn_listener, 50);
44
45         while (1) {
46                 DBusMessage* msg = NULL;
47                 msg = dbus_connection_pop_message(g_w_conn_listener);
48
49                 /* loop again if we haven't read a message */
50                 if (NULL == msg) {
51                         break;
52                 }
53
54                 SLOG(LOG_DEBUG, TAG_VCW, "[DEBUG] Message is arrived");
55
56                 DBusError err;
57                 dbus_error_init(&err);
58
59                 char if_name[64] = {0, };
60                 snprintf(if_name, 64, "%s", VC_WIDGET_SERVICE_INTERFACE);
61
62                 if (dbus_message_is_method_call(msg, if_name, VCD_WIDGET_METHOD_HELLO)) {
63                         SLOG(LOG_DEBUG, TAG_VCW, "===== Get widget hello");
64                         int pid = 0;
65                         int response = -1;
66
67                         dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &pid, DBUS_TYPE_INVALID);
68
69                         if (dbus_error_is_set(&err)) {
70                                 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Dbus Error (%s)", err.message);
71                                 dbus_error_free(&err);
72                         }
73
74                         if (pid > 0) {
75                                 SLOG(LOG_DEBUG, TAG_VCW, "<<<< vc widget get hello : pid(%d) ", pid);
76                                 response = 1;
77                         } else {
78                                 SLOG(LOG_ERROR, TAG_VCW, "<<<< vc widget get hello : invalid pid ");
79                         }
80
81                         DBusMessage* reply = NULL;
82                         reply = dbus_message_new_method_return(msg);
83
84                         if (NULL != reply) {
85                                 dbus_message_append_args(reply, DBUS_TYPE_INT32, &response, DBUS_TYPE_INVALID);
86
87                                 if (!dbus_connection_send(g_w_conn_listener, reply, NULL))
88                                         SLOG(LOG_ERROR, TAG_VCW, ">>>> vc widget get hello : fail to send reply");
89                                 else
90                                         SLOG(LOG_DEBUG, TAG_VCW, ">>>> vc widget get hello : result(%d)", response);
91
92                                 dbus_connection_flush(g_w_conn_listener);
93                                 dbus_message_unref(reply);
94                         } else {
95                                 SLOG(LOG_ERROR, TAG_VCW, ">>>> vc widget get hello : fail to create reply message");
96                         }
97
98                         SLOG(LOG_DEBUG, TAG_VCW, "=====");
99                         SLOG(LOG_DEBUG, TAG_VCW, " ");
100                 } /* VCD_WIDGET_METHOD_HELLO */
101
102                 else if (dbus_message_is_signal(msg, if_name, VCD_WIDGET_METHOD_SET_SERVICE_STATE)) {
103                         int state = 0;
104
105                         dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID);
106                         if (dbus_error_is_set(&err)) {
107                                 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Get arguments error (%s)", err.message);
108                                 dbus_error_free(&err);
109                         }
110
111                         SLOG(LOG_DEBUG, TAG_VCW, "<<<< service state changed : %d", state);
112
113                         __vc_widget_cb_service_state(state);
114
115                 } /* VCD_WIDGET_METHOD_SET_SERVICE_STATE */
116
117                 else if (dbus_message_is_method_call(msg, if_name, VCD_WIDGET_METHOD_SHOW_TOOLTIP)) {
118                         SLOG(LOG_DEBUG, TAG_VCW, "===== Show / Hide tooltip");
119                         int pid = 0;
120                         int show = 0;
121
122                         dbus_message_get_args(msg, &err,
123                                         DBUS_TYPE_INT32, &pid,
124                                         DBUS_TYPE_INT32, &show,
125                                         DBUS_TYPE_INVALID);
126
127                         if (dbus_error_is_set(&err)) {
128                                 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Dbus Error (%s)", err.message);
129                                 dbus_error_free(&err);
130                         }
131
132                         if (pid > 0) {
133                                 SLOG(LOG_DEBUG, TAG_VCW, "<<<< vc widget show tooltip : pid(%d), show(%d)", pid, show);
134                                 __vc_widget_cb_show_tooltip(pid, (bool)show);
135                         } else {
136                                 SLOG(LOG_ERROR, TAG_VCW, "<<<< vc widget show tooltip : invalid pid");
137                         }
138
139                         SLOG(LOG_DEBUG, TAG_VCW, "=====");
140                         SLOG(LOG_DEBUG, TAG_VCW, " ");
141                 } /* VCD_WIDGET_METHOD_SHOW_TOOLTIP */
142
143                 else if (dbus_message_is_method_call(msg, if_name, VCD_WIDGET_METHOD_RESULT)) {
144                         SLOG(LOG_DEBUG, TAG_VCW, "===== Get widget result");
145
146                         __vc_widget_cb_result();
147
148                         /*
149                         reply = dbus_message_new_method_return(msg);
150
151                         if (NULL != reply) {
152                                 if (!dbus_connection_send(g_w_conn_listener, reply, NULL))
153                                         SLOG(LOG_ERROR, TAG_VCW, ">>>> vc widget get result : fail to send reply");
154                                 else
155                                         SLOG(LOG_DEBUG, TAG_VCW, ">>>> vc widget get result");
156
157                                 dbus_connection_flush(g_w_conn_listener);
158                                 dbus_message_unref(reply);
159                         } else {
160                                 SLOG(LOG_ERROR, TAG_VCW, ">>>> vc widget get result : fail to create reply message");
161
162                         */
163
164                         SLOG(LOG_DEBUG, TAG_VCW, "=====");
165                         SLOG(LOG_DEBUG, TAG_VCW, " ");
166
167                 } /* VCD_WIDGET_METHOD_RESULT */
168
169                 else if (dbus_message_is_method_call(msg, if_name, VCD_WIDGET_METHOD_ERROR)) {
170                         SLOG(LOG_DEBUG, TAG_VCW, "===== Get widget error");
171                         int pid;
172                         int reason;
173                         char* err_msg;
174
175                         dbus_message_get_args(msg, &err,
176                                 DBUS_TYPE_INT32, &pid,
177                                 DBUS_TYPE_INT32, &reason,
178                                 DBUS_TYPE_STRING, &err_msg,
179                                 DBUS_TYPE_INVALID);
180
181                         if (dbus_error_is_set(&err)) {
182                                 SLOG(LOG_ERROR, TAG_VCW, "<<<< vc widget get error message : Get arguments error (%s)", err.message);
183                                 dbus_error_free(&err);
184                         } else {
185                                 SLOG(LOG_DEBUG, TAG_VCW, "<<<< vc widget get error message : pid(%d), reason(%d), msg(%s)", pid, reason, err_msg);
186                                 __vc_widget_cb_error(pid, reason);
187                         }
188
189                         /*
190                         reply = dbus_message_new_method_return(msg);
191
192                         if (NULL != reply) {
193                                 if (!dbus_connection_send(g_w_conn_listener, reply, NULL))
194                                         SLOG(LOG_ERROR, TAG_VCW, ">>>> vc widget error message : fail to send reply");
195                                 else
196                                         SLOG(LOG_DEBUG, TAG_VCW, ">>>> vc widget error message");
197
198                                 dbus_connection_flush(g_w_conn_listener);
199                                 dbus_message_unref(reply);
200                         } else {
201                                 SLOG(LOG_ERROR, TAG_VCW, ">>>> vc widget error message : fail to create reply message");
202                         }
203                         */
204
205                         SLOG(LOG_DEBUG, TAG_VCW, "=====");
206                         SLOG(LOG_DEBUG, TAG_VCW, " ");
207                 } /* VCD_WIDGET_METHOD_ERROR */
208
209                 else {
210                         SLOG(LOG_ERROR, TAG_VCW, "Message is NOT valid");
211                         dbus_message_unref(msg);
212                         break;
213                 }
214
215                 /* free the message */
216                 dbus_message_unref(msg);
217         } /* while(1) */
218
219         return ECORE_CALLBACK_PASS_ON;
220 }
221
222 int vc_widget_dbus_open_connection()
223 {
224         if (NULL != g_w_conn_sender && NULL != g_w_conn_listener) {
225                 SLOG(LOG_WARN, TAG_VCW, "Already existed connection ");
226                 return 0;
227         }
228
229         DBusError err;
230         int ret;
231
232         /* initialise the error value */
233         dbus_error_init(&err);
234
235         /* connect to the DBUS system bus, and check for errors */
236         g_w_conn_sender = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
237
238         if (dbus_error_is_set(&err)) {
239                 SLOG(LOG_ERROR, TAG_VCW, "Dbus Connection Error (%s)", err.message);
240                 dbus_error_free(&err);
241         }
242
243         if (NULL == g_w_conn_sender) {
244                 SLOG(LOG_ERROR, TAG_VCW, "Fail to get dbus connection ");
245                 return VC_ERROR_OPERATION_FAILED;
246         }
247
248         g_w_conn_listener = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
249
250         if (dbus_error_is_set(&err)) {
251                 SLOG(LOG_ERROR, TAG_VCW, "Dbus Connection Error (%s)", err.message);
252                 dbus_error_free(&err);
253         }
254
255         if (NULL == g_w_conn_listener) {
256                 SLOG(LOG_ERROR, TAG_VCW, "Fail to get dbus connection ");
257                 return VC_ERROR_OPERATION_FAILED;
258         }
259
260         int pid = getpid();
261
262         char service_name[64];
263         memset(service_name, '\0', 64);
264         snprintf(service_name, 64, "%s%d", VC_WIDGET_SERVICE_NAME, pid);
265
266         SLOG(LOG_DEBUG, TAG_VCW, "service name is %s", service_name);
267
268         /* register our name on the bus, and check for errors */
269         ret = dbus_bus_request_name(g_w_conn_listener, service_name, DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
270
271         if (dbus_error_is_set(&err)) {
272                 SLOG(LOG_ERROR, TAG_VCW, "Name Error (%s)", err.message);
273                 dbus_error_free(&err);
274         }
275
276         if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
277                 SLOG(LOG_ERROR, TAG_VCW, "fail dbus_bus_request_name()");
278                 return -2;
279         }
280
281         if (NULL != g_w_fd_handler) {
282                 SLOG(LOG_WARN, TAG_VCW, "The handler already exists.");
283                 return 0;
284         }
285
286         char rule[128] = {0, };
287         snprintf(rule, 128, "type='signal',interface='%s'", VC_WIDGET_SERVICE_INTERFACE);
288
289         /* add a rule for which messages we want to see */
290         dbus_bus_add_match(g_w_conn_listener, rule, &err);
291         dbus_connection_flush(g_w_conn_listener);
292
293         if (dbus_error_is_set(&err)) {
294                 SLOG(LOG_ERROR, TAG_VCW, "Match Error (%s)", err.message);
295                 dbus_error_free(&err);
296                 return VC_ERROR_OPERATION_FAILED;
297         }
298
299         int fd = 0;
300         if (1 != dbus_connection_get_unix_fd(g_w_conn_listener, &fd)) {
301                 SLOG(LOG_ERROR, TAG_VCW, "fail to get fd from dbus ");
302                 return VC_ERROR_OPERATION_FAILED;
303         } else {
304                 SLOG(LOG_DEBUG, TAG_VCW, "Get fd from dbus : %d", fd);
305         }
306
307         g_w_fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)widget_listener_event_callback, g_w_conn_listener, NULL, NULL);
308
309         if (NULL == g_w_fd_handler) {
310                 SLOG(LOG_ERROR, TAG_VCW, "fail to get fd handler from ecore ");
311                 return VC_ERROR_OPERATION_FAILED;
312         }
313
314         return 0;
315 }
316
317 int vc_widget_dbus_close_connection()
318 {
319         DBusError err;
320         dbus_error_init(&err);
321
322         if (NULL != g_w_fd_handler) {
323                 ecore_main_fd_handler_del(g_w_fd_handler);
324                 g_w_fd_handler = NULL;
325         }
326
327         int pid = getpid();
328
329         char service_name[64];
330         memset(service_name, '\0', 64);
331         snprintf(service_name, 64, "%s%d", VC_WIDGET_SERVICE_NAME, pid);
332
333         dbus_bus_release_name(g_w_conn_listener, service_name, &err);
334
335         if (dbus_error_is_set(&err)) {
336                 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Dbus Error (%s)", err.message);
337                 dbus_error_free(&err);
338         }
339
340         dbus_connection_close(g_w_conn_sender);
341         dbus_connection_close(g_w_conn_listener);
342
343         dbus_connection_unref(g_w_conn_sender);
344         dbus_connection_unref(g_w_conn_listener);
345         
346         g_w_conn_sender = NULL;
347         g_w_conn_listener = NULL;
348
349         return 0;
350 }
351
352 int vc_widget_dbus_reconnect()
353 {
354         bool sender_connected = dbus_connection_get_is_connected(g_w_conn_sender);
355         bool listener_connected = dbus_connection_get_is_connected(g_w_conn_listener);
356         SLOG(LOG_DEBUG, TAG_VCW, "[DBUS] Sender(%s) Listener(%s)",
357                  sender_connected ? "Connected" : "Not connected", listener_connected ? "Connected" : "Not connected");
358
359         if (false == sender_connected || false == listener_connected) {
360                 vc_widget_dbus_close_connection();
361
362                 if (0 != vc_widget_dbus_open_connection()) {
363                         SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Fail to reconnect");
364                         return -1;
365                 }
366
367                 SLOG(LOG_DEBUG, TAG_VCW, "[DBUS] Reconnect");
368         }
369
370         return 0;
371 }
372
373 int vc_widget_dbus_request_hello()
374 {
375         DBusMessage* msg;
376
377         msg = dbus_message_new_method_call(
378                           VC_SERVER_SERVICE_NAME,
379                           VC_SERVER_SERVICE_OBJECT_PATH,
380                           VC_SERVER_SERVICE_INTERFACE,
381                           VC_METHOD_HELLO);
382
383         if (NULL == msg) {
384                 SLOG(LOG_ERROR, TAG_VCW, ">>>> Request vc hello : Fail to make message");
385                 return VC_ERROR_OPERATION_FAILED;
386         }
387
388         DBusError err;
389         dbus_error_init(&err);
390
391         DBusMessage* result_msg = NULL;
392         int result = 0;
393
394         result_msg = dbus_connection_send_with_reply_and_block(g_w_conn_sender, msg, 500, &err);
395
396         if (dbus_error_is_set(&err)) {
397                 dbus_error_free(&err);
398         }
399
400         dbus_message_unref(msg);
401
402         if (NULL != result_msg) {
403                 dbus_message_unref(result_msg);
404                 result = 0;
405         } else {
406                 result = VC_ERROR_TIMED_OUT;
407         }
408
409         return result;
410 }
411
412
413 int vc_widget_dbus_request_initialize(int pid, int* service_state)
414 {
415         DBusMessage* msg;
416
417         msg = dbus_message_new_method_call(
418                           VC_SERVER_SERVICE_NAME,
419                           VC_SERVER_SERVICE_OBJECT_PATH,
420                           VC_SERVER_SERVICE_INTERFACE,
421                           VC_WIDGET_METHOD_INITIALIZE);
422
423         if (NULL == msg) {
424                 SLOG(LOG_ERROR, TAG_VCW, ">>>> vc widget initialize : Fail to make message ");
425                 return VC_ERROR_OPERATION_FAILED;
426         } else {
427                 SLOG(LOG_DEBUG, TAG_VCW, ">>>> vc widget initialize : pid(%d)", pid);
428         }
429
430         dbus_message_append_args(msg,
431                                                          DBUS_TYPE_INT32, &pid,
432                                                          DBUS_TYPE_INVALID);
433
434         DBusError err;
435         dbus_error_init(&err);
436
437         DBusMessage* result_msg;
438         int result = VC_ERROR_OPERATION_FAILED;
439
440         result_msg = dbus_connection_send_with_reply_and_block(g_w_conn_sender, msg, g_w_waiting_time, &err);
441         dbus_message_unref(msg);
442
443         if (dbus_error_is_set(&err)) {
444                 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Dbus Error (%s)", err.message);
445                 dbus_error_free(&err);
446         }
447
448         if (NULL != result_msg) {
449                 int tmp_service_state = 0;
450                 dbus_message_get_args(result_msg, &err,
451                         DBUS_TYPE_INT32, &result,
452                         DBUS_TYPE_INT32, &tmp_service_state,
453                         DBUS_TYPE_INVALID);
454
455                 if (dbus_error_is_set(&err)) {
456                         SLOG(LOG_ERROR, TAG_VCW, "<<<< Get arguments error (%s)", err.message);
457                         dbus_error_free(&err);
458                         result = VC_ERROR_OPERATION_FAILED;
459                 }
460
461                 dbus_message_unref(result_msg);
462
463                 if (0 == result) {
464                         *service_state = tmp_service_state;
465                         SLOG(LOG_DEBUG, TAG_VCW, "<<<< vc widget initialize : result = %d service = %d", result, *service_state);
466                 } else {
467                         SLOG(LOG_ERROR, TAG_VCW, "<<<< vc widget initialize : result = %d", result);
468                 }
469         } else {
470                 SLOG(LOG_ERROR, TAG_VCW, "<<<< Result message is NULL ");
471                 vc_widget_dbus_reconnect();
472                 result = VC_ERROR_TIMED_OUT;
473         }
474
475         return result;
476 }
477
478 int vc_widget_dbus_request_finalize(int pid)
479 {
480         DBusMessage* msg;
481
482         msg = dbus_message_new_method_call(
483                           VC_SERVER_SERVICE_NAME,
484                           VC_SERVER_SERVICE_OBJECT_PATH,
485                           VC_SERVER_SERVICE_INTERFACE,
486                           VC_WIDGET_METHOD_FINALIZE);
487
488         if (NULL == msg) {
489                 SLOG(LOG_ERROR, TAG_VCW, ">>>> vc widget finalize : Fail to make message ");
490                 return VC_ERROR_OPERATION_FAILED;
491         } else {
492                 SLOG(LOG_DEBUG, TAG_VCW, ">>>> vc widget finalize : pid(%d)", pid);
493         }
494
495         dbus_message_append_args(msg, DBUS_TYPE_INT32, &pid, DBUS_TYPE_INVALID);
496
497         DBusError err;
498         dbus_error_init(&err);
499
500         DBusMessage* result_msg;
501         int result = VC_ERROR_OPERATION_FAILED;
502
503         result_msg = dbus_connection_send_with_reply_and_block(g_w_conn_sender, msg, g_w_waiting_time, &err);
504         dbus_message_unref(msg);
505
506         if (dbus_error_is_set(&err)) {
507                 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Dbus Error (%s)", err.message);
508                 dbus_error_free(&err);
509         }
510
511         if (NULL != result_msg) {
512                 dbus_message_get_args(result_msg, &err,
513                                                           DBUS_TYPE_INT32, &result,
514                                                           DBUS_TYPE_INVALID);
515
516                 if (dbus_error_is_set(&err)) {
517                         SLOG(LOG_ERROR, TAG_VCW, "<<<< Get arguments error (%s)", err.message);
518                         dbus_error_free(&err);
519                         result = VC_ERROR_OPERATION_FAILED;
520                 }
521
522                 dbus_message_unref(result_msg);
523
524                 if (0 == result) {
525                         SLOG(LOG_DEBUG, TAG_VCW, "<<<< vc widget finalize : result = %d", result);
526                 } else {
527                         SLOG(LOG_ERROR, TAG_VCW, "<<<< vc widget finalize : result = %d", result);
528                 }
529         } else {
530                 SLOG(LOG_ERROR, TAG_VCW, "<<<< Result message is NULL ");
531                 vc_widget_dbus_reconnect();
532                 result = VC_ERROR_TIMED_OUT;
533         }
534
535         return result;
536 }
537
538 int vc_widget_dbus_request_start_recording(int pid, bool command)
539 {
540         DBusMessage* msg;
541
542         msg = dbus_message_new_method_call(
543                           VC_SERVER_SERVICE_NAME,
544                           VC_SERVER_SERVICE_OBJECT_PATH,
545                           VC_SERVER_SERVICE_INTERFACE,
546                           VC_WIDGET_METHOD_START_RECORDING);
547
548         if (NULL == msg) {
549                 SLOG(LOG_ERROR, TAG_VCW, ">>>> vc widget start recording : Fail to make message");
550                 return VC_ERROR_OPERATION_FAILED;
551         } else {
552                 SLOG(LOG_DEBUG, TAG_VCW, ">>>> vc widget start recording : pid(%d)", pid);
553         }
554
555         int temp = (int)command;
556
557         dbus_message_append_args(msg,
558                                                          DBUS_TYPE_INT32, &pid,
559                                                          DBUS_TYPE_INT32, &temp,
560                                                          DBUS_TYPE_INVALID);
561
562         DBusError err;
563         dbus_error_init(&err);
564
565         DBusMessage* result_msg;
566         int result = VC_ERROR_OPERATION_FAILED;
567
568         result_msg = dbus_connection_send_with_reply_and_block(g_w_conn_sender, msg, g_w_waiting_time, &err);
569         dbus_message_unref(msg);
570
571         if (dbus_error_is_set(&err)) {
572                 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Dbus Error (%s)", err.message);
573                 dbus_error_free(&err);
574         }
575
576         if (NULL != result_msg) {
577                 dbus_message_get_args(result_msg, &err, DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
578
579                 if (dbus_error_is_set(&err)) {
580                         SLOG(LOG_ERROR, TAG_VCW, "<<<< Get arguments error (%s)", err.message);
581                         dbus_error_free(&err);
582                         result = VC_ERROR_OPERATION_FAILED;
583                 }
584                 dbus_message_unref(result_msg);
585
586                 if (0 == result) {
587                         SLOG(LOG_DEBUG, TAG_VCW, "<<<< vc widget start recording : result = %d", result);
588                 } else {
589                         SLOG(LOG_ERROR, TAG_VCW, "<<<< vc widget start recording : result = %d", result);
590                 }
591         } else {
592                 SLOG(LOG_ERROR, TAG_VCW, "<<<< Result message is NULL");
593                 vc_widget_dbus_reconnect();
594                 result = VC_ERROR_TIMED_OUT;
595         }
596
597         return result;
598 }
599
600 int vc_widget_dbus_set_foreground(int pid, bool value)
601 {
602         DBusMessage* msg = NULL;
603         int tmp_value = 0;
604
605         tmp_value = (int)value;
606
607         msg = dbus_message_new_signal(
608                 VC_MANAGER_SERVICE_OBJECT_PATH,
609                 VC_MANAGER_SERVICE_INTERFACE,
610                 VCC_MANAGER_METHOD_SET_FOREGROUND);
611
612         if (NULL == msg) {
613                 SLOG(LOG_ERROR, TAG_VCW, ">>>> vc widget set foreground to manager : Fail to make message");
614                 return VC_ERROR_OPERATION_FAILED;
615         } else {
616                 SLOG(LOG_DEBUG, TAG_VCW, ">>>> vc widget set foreground to manager : client pid(%d), value(%s)", pid, tmp_value ? "true" : "false");
617         }
618
619         dbus_message_append_args(msg,
620                 DBUS_TYPE_INT32, &pid,
621                 DBUS_TYPE_INT32, &tmp_value,
622                 DBUS_TYPE_INVALID);
623
624         if (1 != dbus_connection_send(g_w_conn_sender, msg, NULL)) {
625                 SLOG(LOG_ERROR, TAG_VCW, "[Dbus ERROR] Fail to Send");
626                 return VC_ERROR_OPERATION_FAILED;
627         }
628
629         dbus_message_unref(msg);
630
631         msg = NULL;
632         msg = dbus_message_new_method_call(
633                 VC_SERVER_SERVICE_NAME,
634                 VC_SERVER_SERVICE_OBJECT_PATH,
635                 VC_SERVER_SERVICE_INTERFACE,
636                 VC_METHOD_SET_FOREGROUND);
637
638         if (NULL == msg) {
639                 SLOG(LOG_ERROR, TAG_VCW, ">>>> vc widget set foreground to daemon : Fail to make message");
640                 return VC_ERROR_OPERATION_FAILED;
641         } else {
642                 SLOG(LOG_DEBUG, TAG_VCW, ">>>> vc widget set foreground to daemon : client pid(%d), value(%s)", pid, tmp_value ? "true" : "false");
643         }
644
645         dbus_message_append_args(msg,
646                 DBUS_TYPE_INT32, &pid,
647                 DBUS_TYPE_INT32, &tmp_value,
648                 DBUS_TYPE_INVALID);
649
650         dbus_message_set_no_reply(msg, TRUE);
651
652         if (1 != dbus_connection_send(g_w_conn_sender, msg, NULL)) {
653                 SLOG(LOG_ERROR, TAG_VCW, "[Dbus ERROR] Fail to Send");
654                 return VC_ERROR_OPERATION_FAILED;
655         }
656
657         dbus_connection_flush(g_w_conn_sender);
658
659         dbus_message_unref(msg);
660
661         return 0;
662 }
663
664
665 int vc_widget_dbus_request_start(int pid, int silence)
666 {
667         DBusMessage* msg;
668
669         /* create a signal & check for errors */
670         msg = dbus_message_new_method_call(
671                           VC_SERVER_SERVICE_NAME,
672                           VC_SERVER_SERVICE_OBJECT_PATH,
673                           VC_SERVER_SERVICE_INTERFACE,
674                           VC_WIDGET_METHOD_START);
675
676         if (NULL == msg) {
677                 SLOG(LOG_ERROR, TAG_VCW, ">>>> vc widget start : Fail to make message ");
678                 return VC_ERROR_OPERATION_FAILED;
679         } else {
680                 SLOG(LOG_DEBUG, TAG_VCW, ">>>> vc widget start : pid(%d), silence(%d)", pid, silence);
681         }
682
683         DBusMessageIter args;
684         dbus_message_iter_init_append(msg, &args);
685
686         /* Append result*/
687         dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(pid));
688         dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(silence));
689
690         DBusError err;
691         dbus_error_init(&err);
692
693         DBusMessage* result_msg;
694         int result = VC_ERROR_OPERATION_FAILED;
695
696         result_msg = dbus_connection_send_with_reply_and_block(g_w_conn_sender, msg, g_w_waiting_time, &err);
697         dbus_message_unref(msg);
698
699         if (dbus_error_is_set(&err)) {
700                 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Dbus Error (%s)", err.message);
701                 dbus_error_free(&err);
702         }
703
704         if (NULL != result_msg) {
705                 dbus_message_get_args(result_msg, &err,
706                                                           DBUS_TYPE_INT32, &result,
707                                                           DBUS_TYPE_INVALID);
708
709                 if (dbus_error_is_set(&err)) {
710                         SLOG(LOG_ERROR, TAG_VCW, "<<<< Get arguments error (%s)", err.message);
711                         dbus_error_free(&err);
712                         result = VC_ERROR_OPERATION_FAILED;
713                 }
714                 dbus_message_unref(result_msg);
715
716                 if (0 == result) {
717                         SLOG(LOG_DEBUG, TAG_VCW, "<<<< vc widget start : result = %d", result);
718                 } else {
719                         SLOG(LOG_ERROR, TAG_VCW, "<<<< vc widget start : result = %d", result);
720                 }
721         } else {
722                 SLOG(LOG_DEBUG, TAG_VCW, "<<<< Result Message is NULL");
723                 vc_widget_dbus_reconnect();
724                 result = VC_ERROR_TIMED_OUT;
725         }
726
727         return result;
728 }
729
730 int vc_widget_dbus_request_stop(int pid)
731 {
732         DBusMessage* msg;
733
734         /* create a signal & check for errors */
735         msg = dbus_message_new_method_call(
736                           VC_SERVER_SERVICE_NAME,
737                           VC_SERVER_SERVICE_OBJECT_PATH,
738                           VC_SERVER_SERVICE_INTERFACE,
739                           VC_WIDGET_METHOD_STOP);
740
741         if (NULL == msg) {
742                 SLOG(LOG_ERROR, TAG_VCW, ">>>> vc widget stop : Fail to make message ");
743                 return VC_ERROR_OPERATION_FAILED;
744         } else {
745                 SLOG(LOG_DEBUG, TAG_VCW, ">>>> vc widget stop : pid(%d)", pid);
746         }
747
748         dbus_message_append_args(msg,
749                                                          DBUS_TYPE_INT32, &pid,
750                                                          DBUS_TYPE_INVALID);
751
752         DBusError err;
753         dbus_error_init(&err);
754
755         DBusMessage* result_msg;
756         int result = VC_ERROR_OPERATION_FAILED;
757
758         result_msg = dbus_connection_send_with_reply_and_block(g_w_conn_sender, msg, g_w_waiting_time, &err);
759         dbus_message_unref(msg);
760
761         if (dbus_error_is_set(&err)) {
762                 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Dbus Error (%s)", err.message);
763                 dbus_error_free(&err);
764         }
765
766         if (NULL != result_msg) {
767                 dbus_message_get_args(result_msg, &err,
768                                                           DBUS_TYPE_INT32, &result,
769                                                           DBUS_TYPE_INVALID);
770
771                 if (dbus_error_is_set(&err)) {
772                         SLOG(LOG_ERROR, TAG_VCW, "<<<< Get arguments error (%s)", err.message);
773                         dbus_error_free(&err);
774                         result = VC_ERROR_OPERATION_FAILED;
775                 }
776                 dbus_message_unref(result_msg);
777
778                 if (0 == result) {
779                         SLOG(LOG_DEBUG, TAG_VCW, "<<<< vc widget stop : result = %d", result);
780                 } else {
781                         SLOG(LOG_ERROR, TAG_VCW, "<<<< vc widget stop : result = %d", result);
782                 }
783         } else {
784                 SLOG(LOG_DEBUG, TAG_VCW, "<<<< Result Message is NULL");
785                 vc_widget_dbus_reconnect();
786                 result = VC_ERROR_TIMED_OUT;
787         }
788
789         return result;
790 }
791
792 int vc_widget_dbus_request_cancel(int pid)
793 {
794         DBusMessage* msg;
795
796         /* create a signal & check for errors */
797         msg = dbus_message_new_method_call(
798                           VC_SERVER_SERVICE_NAME,
799                           VC_SERVER_SERVICE_OBJECT_PATH,        /* object name of the signal */
800                           VC_SERVER_SERVICE_INTERFACE,  /* interface name of the signal */
801                           VC_WIDGET_METHOD_CANCEL);     /* name of the signal */
802
803         if (NULL == msg) {
804                 SLOG(LOG_ERROR, TAG_VCW, ">>>> vc widget cancel : Fail to make message ");
805                 return VC_ERROR_OPERATION_FAILED;
806         } else {
807                 SLOG(LOG_DEBUG, TAG_VCW, ">>>> vc widget cancel : pid(%d)", pid);
808         }
809
810         dbus_message_append_args(msg,
811                                                          DBUS_TYPE_INT32, &pid,
812                                                          DBUS_TYPE_INVALID);
813
814         DBusError err;
815         dbus_error_init(&err);
816
817         DBusMessage* result_msg;
818         int result = VC_ERROR_OPERATION_FAILED;
819
820         result_msg = dbus_connection_send_with_reply_and_block(g_w_conn_sender, msg, g_w_waiting_time, &err);
821         dbus_message_unref(msg);
822
823         if (dbus_error_is_set(&err)) {
824                 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Dbus Error (%s)", err.message);
825                 dbus_error_free(&err);
826         }
827
828         if (NULL != result_msg) {
829                 dbus_message_get_args(result_msg, &err,
830                                                           DBUS_TYPE_INT32, &result,
831                                                           DBUS_TYPE_INVALID);
832
833                 if (dbus_error_is_set(&err)) {
834                         SLOG(LOG_ERROR, TAG_VCW, "<<<< Get arguments error (%s)", err.message);
835                         dbus_error_free(&err);
836                         result = VC_ERROR_OPERATION_FAILED;
837                 }
838                 dbus_message_unref(result_msg);
839
840                 if (0 == result) {
841                         SLOG(LOG_DEBUG, TAG_VCW, "<<<< vc widget cancel : result = %d", result);
842                 } else {
843                         SLOG(LOG_ERROR, TAG_VCW, "<<<< vc widget cancel : result = %d", result);
844                 }
845         } else {
846                 SLOG(LOG_DEBUG, TAG_VCW, "<<<< Result Message is NULL");
847                 vc_widget_dbus_reconnect();
848                 result = VC_ERROR_TIMED_OUT;
849         }
850
851         return result;
852 }