2 * Copyright (c) 2011-2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "vc_widget_client.h"
20 #include "vc_widget_dbus.h"
23 static int g_w_waiting_time = 3000;
25 static Ecore_Fd_Handler* g_w_fd_handler = NULL;
27 static DBusConnection* g_w_conn_sender = NULL;
28 static DBusConnection* g_w_conn_listener = NULL;
30 extern int __vc_widget_cb_error(int pid, int reason);
32 extern void __vc_widget_cb_show_tooltip(int pid, bool show);
34 extern void __vc_widget_cb_result();
37 static Eina_Bool widget_listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
39 if (NULL == g_w_conn_listener) return ECORE_CALLBACK_RENEW;
41 dbus_connection_read_write_dispatch(g_w_conn_listener, 50);
43 DBusMessage* msg = NULL;
44 msg = dbus_connection_pop_message(g_w_conn_listener);
46 /* loop again if we haven't read a message */
48 return ECORE_CALLBACK_RENEW;
52 dbus_error_init(&err);
55 snprintf(if_name, 64, "%s", VC_WIDGET_SERVICE_INTERFACE);
57 if (dbus_message_is_method_call(msg, if_name, VCD_WIDGET_METHOD_HELLO)) {
58 SLOG(LOG_DEBUG, TAG_VCW, "===== Get widget hello");
62 dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &pid, DBUS_TYPE_INVALID);
64 if (dbus_error_is_set(&err)) {
65 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Dbus Error (%s)", err.message);
66 dbus_error_free(&err);
70 SLOG(LOG_DEBUG, TAG_VCW, "<<<< vc widget get hello : pid(%d) ", pid);
73 SLOG(LOG_ERROR, TAG_VCW, "<<<< vc widget get hello : invalid pid ");
76 DBusMessage* reply = NULL;
77 reply = dbus_message_new_method_return(msg);
80 dbus_message_append_args(reply, DBUS_TYPE_INT32, &response, DBUS_TYPE_INVALID);
82 if (!dbus_connection_send(g_w_conn_listener, reply, NULL))
83 SLOG(LOG_ERROR, TAG_VCW, ">>>> vc widget get hello : fail to send reply");
85 SLOG(LOG_DEBUG, TAG_VCW, ">>>> vc widget get hello : result(%d)", response);
87 dbus_connection_flush(g_w_conn_listener);
88 dbus_message_unref(reply);
90 SLOG(LOG_ERROR, TAG_VCW, ">>>> vc widget get hello : fail to create reply message");
93 SLOG(LOG_DEBUG, TAG_VCW, "=====");
94 SLOG(LOG_DEBUG, TAG_VCW, " ");
95 } /* VCD_WIDGET_METHOD_HELLO */
97 else if (dbus_message_is_method_call(msg, if_name, VCD_WIDGET_METHOD_SHOW_TOOLTIP)) {
98 SLOG(LOG_DEBUG, TAG_VCW, "===== Show / Hide tooltip");
102 dbus_message_get_args(msg, &err,
103 DBUS_TYPE_INT32, &pid,
104 DBUS_TYPE_INT32, &show,
107 if (dbus_error_is_set(&err)) {
108 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Dbus Error (%s)", err.message);
109 dbus_error_free(&err);
113 SLOG(LOG_DEBUG, TAG_VCW, "<<<< vc widget show tooltip : pid(%d), show(%d)", pid, show);
115 __vc_widget_cb_show_tooltip(pid, (bool)show);
117 SLOG(LOG_ERROR, TAG_VCW, "<<<< vc widget show tooltip : invalid pid");
120 SLOG(LOG_DEBUG, TAG_VCW, "=====");
121 SLOG(LOG_DEBUG, TAG_VCW, " ");
122 } /* VCD_WIDGET_METHOD_SHOW_TOOLTIP */
124 else if (dbus_message_is_method_call(msg, if_name, VCD_WIDGET_METHOD_RESULT)) {
125 SLOG(LOG_DEBUG, TAG_VCW, "===== Get widget result");
127 __vc_widget_cb_result();
129 SLOG(LOG_DEBUG, TAG_VCW, "=====");
130 SLOG(LOG_DEBUG, TAG_VCW, " ");
132 } /* VCD_WIDGET_METHOD_RESULT */
134 else if (dbus_message_is_method_call(msg, if_name, VCD_WIDGET_METHOD_ERROR)) {
135 SLOG(LOG_DEBUG, TAG_VCW, "===== Get widget error");
140 dbus_message_get_args(msg, &err,
141 DBUS_TYPE_INT32, &pid,
142 DBUS_TYPE_INT32, &reason,
143 DBUS_TYPE_STRING, &err_msg,
146 if (dbus_error_is_set(&err)) {
147 SLOG(LOG_ERROR, TAG_VCW, "<<<< vc widget get error message : Get arguments error (%s)", err.message);
148 dbus_error_free(&err);
150 SLOG(LOG_DEBUG, TAG_VCW, "<<<< vc widget get error message : pid(%d), reason(%d), msg(%s)", pid, reason, err_msg);
151 __vc_widget_cb_error(pid, reason);
154 SLOG(LOG_DEBUG, TAG_VCW, "=====");
155 SLOG(LOG_DEBUG, TAG_VCW, " ");
156 } /* VCD_WIDGET_METHOD_ERROR */
158 /* free the message */
159 dbus_message_unref(msg);
161 return ECORE_CALLBACK_PASS_ON;
164 int vc_widget_dbus_open_connection()
166 if (NULL != g_w_conn_sender && NULL != g_w_conn_listener) {
167 SLOG(LOG_WARN, TAG_VCW, "Already existed connection ");
174 /* initialise the error value */
175 dbus_error_init(&err);
177 /* connect to the DBUS system bus, and check for errors */
178 g_w_conn_sender = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
180 if (dbus_error_is_set(&err)) {
181 SLOG(LOG_ERROR, TAG_VCW, "Dbus Connection Error (%s)", err.message);
182 dbus_error_free(&err);
185 if (NULL == g_w_conn_sender) {
186 SLOG(LOG_ERROR, TAG_VCW, "Fail to get dbus connection ");
187 return VC_ERROR_OPERATION_FAILED;
190 g_w_conn_listener = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
192 if (dbus_error_is_set(&err)) {
193 SLOG(LOG_ERROR, TAG_VCW, "Dbus Connection Error (%s)", err.message);
194 dbus_error_free(&err);
197 if (NULL == g_w_conn_listener) {
198 SLOG(LOG_ERROR, TAG_VCW, "Fail to get dbus connection ");
199 return VC_ERROR_OPERATION_FAILED;
204 char service_name[64];
205 memset(service_name, '\0', 64);
206 snprintf(service_name, 64, "%s", VC_WIDGET_SERVICE_NAME);
208 SLOG(LOG_DEBUG, TAG_VCW, "service name is %s", service_name);
210 /* register our name on the bus, and check for errors */
211 ret = dbus_bus_request_name(g_w_conn_listener, service_name, DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
213 if (dbus_error_is_set(&err)) {
214 SLOG(LOG_ERROR, TAG_VCW, "Name Error (%s)", err.message);
215 dbus_error_free(&err);
218 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
219 SLOG(LOG_ERROR, TAG_VCW, "fail dbus_bus_request_name()");
223 if( NULL != g_w_fd_handler ) {
224 SLOG(LOG_WARN, TAG_VCW, "The handler already exists.");
229 snprintf(rule, 128, "type='signal',interface='%s'", VC_WIDGET_SERVICE_INTERFACE);
231 /* add a rule for which messages we want to see */
232 dbus_bus_add_match(g_w_conn_listener, rule, &err);
233 dbus_connection_flush(g_w_conn_listener);
235 if (dbus_error_is_set(&err))
237 SLOG(LOG_ERROR, TAG_VCW, "Match Error (%s)", err.message);
238 dbus_error_free(&err);
239 return VC_ERROR_OPERATION_FAILED;
243 if (1 != dbus_connection_get_unix_fd(g_w_conn_listener, &fd)) {
244 SLOG(LOG_ERROR, TAG_VCW, "fail to get fd from dbus ");
245 return VC_ERROR_OPERATION_FAILED;
247 SLOG(LOG_DEBUG, TAG_VCW, "Get fd from dbus : %d", fd);
250 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);
252 if (NULL == g_w_fd_handler) {
253 SLOG(LOG_ERROR, TAG_VCW, "fail to get fd handler from ecore ");
254 return VC_ERROR_OPERATION_FAILED;
260 int vc_widget_dbus_close_connection()
263 dbus_error_init(&err);
265 if (NULL != g_w_fd_handler) {
266 ecore_main_fd_handler_del(g_w_fd_handler);
267 g_w_fd_handler = NULL;
272 char service_name[64];
273 memset(service_name, '\0', 64);
274 snprintf(service_name, 64, "%s", VC_WIDGET_SERVICE_NAME);
276 dbus_bus_release_name(g_w_conn_listener, service_name, &err);
278 if (dbus_error_is_set(&err)) {
279 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Dbus Error (%s)", err.message);
280 dbus_error_free(&err);
283 g_w_conn_sender = NULL;
284 g_w_conn_listener = NULL;
289 int vc_widget_dbus_reconnect()
291 bool sender_connected = dbus_connection_get_is_connected(g_w_conn_sender);
292 bool listener_connected = dbus_connection_get_is_connected(g_w_conn_listener);
293 SLOG(LOG_DEBUG, TAG_VCW, "[DBUS] Sender(%s) Listener(%s)",
294 sender_connected ? "Connected" : "Not connected", listener_connected ? "Connected" : "Not connected");
296 if (false == sender_connected || false == listener_connected) {
297 vc_widget_dbus_close_connection();
299 if (0 != vc_widget_dbus_open_connection()) {
300 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Fail to reconnect");
304 SLOG(LOG_DEBUG, TAG_VCW, "[DBUS] Reconnect");
310 int vc_widget_dbus_request_hello()
314 msg = dbus_message_new_method_call(
315 VC_SERVER_SERVICE_NAME,
316 VC_SERVER_SERVICE_OBJECT_PATH,
317 VC_SERVER_SERVICE_INTERFACE,
321 SLOG(LOG_ERROR, TAG_VCW, ">>>> Request vc hello : Fail to make message");
322 return VC_ERROR_OPERATION_FAILED;
326 dbus_error_init(&err);
328 DBusMessage* result_msg = NULL;
331 result_msg = dbus_connection_send_with_reply_and_block(g_w_conn_sender, msg, 500, &err);
333 if (dbus_error_is_set(&err)) {
334 dbus_error_free(&err);
337 dbus_message_unref(msg);
339 if (NULL != result_msg) {
340 dbus_message_unref(result_msg);
343 result = VC_ERROR_TIMED_OUT;
350 int vc_widget_dbus_request_initialize(int pid)
354 msg = dbus_message_new_method_call(
355 VC_SERVER_SERVICE_NAME,
356 VC_SERVER_SERVICE_OBJECT_PATH,
357 VC_SERVER_SERVICE_INTERFACE,
358 VC_WIDGET_METHOD_INITIALIZE);
361 SLOG(LOG_ERROR, TAG_VCW, ">>>> vc widget initialize : Fail to make message ");
362 return VC_ERROR_OPERATION_FAILED;
364 SLOG(LOG_DEBUG, TAG_VCW, ">>>> vc widget initialize : pid(%d)", pid);
367 dbus_message_append_args( msg,
368 DBUS_TYPE_INT32, &pid,
372 dbus_error_init(&err);
374 DBusMessage* result_msg;
375 int result = VC_ERROR_OPERATION_FAILED;
377 result_msg = dbus_connection_send_with_reply_and_block(g_w_conn_sender, msg, g_w_waiting_time, &err);
378 dbus_message_unref(msg);
380 if (dbus_error_is_set(&err)) {
381 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Dbus Error (%s)", err.message);
382 dbus_error_free(&err);
385 if (NULL != result_msg) {
386 dbus_message_get_args(result_msg, &err,
387 DBUS_TYPE_INT32, &result,
390 if (dbus_error_is_set(&err)) {
391 SLOG(LOG_ERROR, TAG_VCW, "<<<< Get arguments error (%s)", err.message);
392 dbus_error_free(&err);
393 result = VC_ERROR_OPERATION_FAILED;
396 dbus_message_unref(result_msg);
399 SLOG(LOG_DEBUG, TAG_VCW, "<<<< vc widget initialize : result = %d", result);
401 SLOG(LOG_ERROR, TAG_VCW, "<<<< vc widget initialize : result = %d", result);
404 SLOG(LOG_ERROR, TAG_VCW, "<<<< Result message is NULL ");
405 vc_widget_dbus_reconnect();
406 result = VC_ERROR_TIMED_OUT;
412 int vc_widget_dbus_request_finalize(int pid)
416 msg = dbus_message_new_method_call(
417 VC_SERVER_SERVICE_NAME,
418 VC_SERVER_SERVICE_OBJECT_PATH,
419 VC_SERVER_SERVICE_INTERFACE,
420 VC_WIDGET_METHOD_FINALIZE);
423 SLOG(LOG_ERROR, TAG_VCW, ">>>> vc widget finalize : Fail to make message ");
424 return VC_ERROR_OPERATION_FAILED;
426 SLOG(LOG_DEBUG, TAG_VCW, ">>>> vc widget finalize : pid(%d)", pid);
429 dbus_message_append_args(msg, DBUS_TYPE_INT32, &pid, DBUS_TYPE_INVALID);
432 dbus_error_init(&err);
434 DBusMessage* result_msg;
435 int result = VC_ERROR_OPERATION_FAILED;
437 result_msg = dbus_connection_send_with_reply_and_block(g_w_conn_sender, msg, g_w_waiting_time, &err);
438 dbus_message_unref(msg);
440 if (dbus_error_is_set(&err)) {
441 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Dbus Error (%s)", err.message);
442 dbus_error_free(&err);
445 if (NULL != result_msg) {
446 dbus_message_get_args(result_msg, &err,
447 DBUS_TYPE_INT32, &result,
450 if (dbus_error_is_set(&err)) {
451 SLOG(LOG_ERROR, TAG_VCW, "<<<< Get arguments error (%s)", err.message);
452 dbus_error_free(&err);
453 result = VC_ERROR_OPERATION_FAILED;
456 dbus_message_unref(result_msg);
459 SLOG(LOG_DEBUG, TAG_VCW, "<<<< vc widget finalize : result = %d", result);
461 SLOG(LOG_ERROR, TAG_VCW, "<<<< vc widget finalize : result = %d", result);
464 SLOG(LOG_ERROR, TAG_VCW, "<<<< Result message is NULL ");
465 vc_widget_dbus_reconnect();
466 result = VC_ERROR_TIMED_OUT;
472 int vc_widget_dbus_request_start_recording(int pid, bool command)
476 msg = dbus_message_new_method_call(
477 VC_SERVER_SERVICE_NAME,
478 VC_SERVER_SERVICE_OBJECT_PATH,
479 VC_SERVER_SERVICE_INTERFACE,
480 VC_WIDGET_METHOD_START_RECORDING);
483 SLOG(LOG_ERROR, TAG_VCW, ">>>> vc widget start recording : Fail to make message");
484 return VC_ERROR_OPERATION_FAILED;
486 SLOG(LOG_DEBUG, TAG_VCW, ">>>> vc widget start recording : pid(%d)", pid);
489 int temp = (int)command;
491 dbus_message_append_args(msg,
492 DBUS_TYPE_INT32, &pid,
493 DBUS_TYPE_INT32, &temp,
497 dbus_error_init(&err);
499 DBusMessage* result_msg;
500 int result = VC_ERROR_OPERATION_FAILED;
502 result_msg = dbus_connection_send_with_reply_and_block(g_w_conn_sender, msg, g_w_waiting_time, &err);
503 dbus_message_unref(msg);
505 if (dbus_error_is_set(&err)) {
506 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Dbus Error (%s)", err.message);
507 dbus_error_free(&err);
510 if (NULL != result_msg) {
511 dbus_message_get_args(result_msg, &err, DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
513 if (dbus_error_is_set(&err)) {
514 SLOG(LOG_ERROR, TAG_VCW, "<<<< Get arguments error (%s)", err.message);
515 dbus_error_free(&err);
516 result = VC_ERROR_OPERATION_FAILED;
518 dbus_message_unref(result_msg);
521 SLOG(LOG_DEBUG, TAG_VCW, "<<<< vc widget start recording : result = %d", result);
523 SLOG(LOG_ERROR, TAG_VCW, "<<<< vc widget start recording : result = %d", result);
526 SLOG(LOG_ERROR, TAG_VCW, "<<<< Result message is NULL");
527 vc_widget_dbus_reconnect();
528 result = VC_ERROR_TIMED_OUT;
534 int vc_widget_dbus_request_start(int pid, int silence)
538 /* create a signal & check for errors */
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_WIDGET_METHOD_START);
546 SLOG(LOG_ERROR, TAG_VCW, ">>>> vc widget start : Fail to make message ");
547 return VC_ERROR_OPERATION_FAILED;
549 SLOG(LOG_DEBUG, TAG_VCW, ">>>> vc widget start : pid(%d), silence(%d)", pid, silence);
552 DBusMessageIter args;
553 dbus_message_iter_init_append(msg, &args);
556 dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(pid));
557 dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &(silence));
560 dbus_error_init(&err);
562 DBusMessage* result_msg;
563 int result = VC_ERROR_OPERATION_FAILED;
565 result_msg = dbus_connection_send_with_reply_and_block(g_w_conn_sender, msg, g_w_waiting_time, &err);
566 dbus_message_unref(msg);
568 if (dbus_error_is_set(&err)) {
569 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Dbus Error (%s)", err.message);
570 dbus_error_free(&err);
573 if (NULL != result_msg) {
574 dbus_message_get_args(result_msg, &err,
575 DBUS_TYPE_INT32, &result,
578 if (dbus_error_is_set(&err)) {
579 SLOG(LOG_ERROR, TAG_VCW, "<<<< Get arguments error (%s)", err.message);
580 dbus_error_free(&err);
581 result = VC_ERROR_OPERATION_FAILED;
583 dbus_message_unref(result_msg);
586 SLOG(LOG_DEBUG, TAG_VCW, "<<<< vc widget start : result = %d", result);
588 SLOG(LOG_ERROR, TAG_VCW, "<<<< vc widget start : result = %d", result);
591 SLOG(LOG_DEBUG, TAG_VCW, "<<<< Result Message is NULL");
592 vc_widget_dbus_reconnect();
593 result = VC_ERROR_TIMED_OUT;
599 int vc_widget_dbus_request_stop(int pid)
603 /* create a signal & check for errors */
604 msg = dbus_message_new_method_call(
605 VC_SERVER_SERVICE_NAME,
606 VC_SERVER_SERVICE_OBJECT_PATH,
607 VC_SERVER_SERVICE_INTERFACE,
608 VC_WIDGET_METHOD_STOP);
611 SLOG(LOG_ERROR, TAG_VCW, ">>>> vc widget stop : Fail to make message ");
612 return VC_ERROR_OPERATION_FAILED;
614 SLOG(LOG_DEBUG, TAG_VCW, ">>>> vc widget stop : pid(%d)", pid);
617 dbus_message_append_args(msg,
618 DBUS_TYPE_INT32, &pid,
622 dbus_error_init(&err);
624 DBusMessage* result_msg;
625 int result = VC_ERROR_OPERATION_FAILED;
627 result_msg = dbus_connection_send_with_reply_and_block(g_w_conn_sender, msg, g_w_waiting_time, &err);
628 dbus_message_unref(msg);
630 if (dbus_error_is_set(&err)) {
631 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Dbus Error (%s)", err.message);
632 dbus_error_free(&err);
635 if (NULL != result_msg) {
636 dbus_message_get_args(result_msg, &err,
637 DBUS_TYPE_INT32, &result,
640 if (dbus_error_is_set(&err)) {
641 SLOG(LOG_ERROR, TAG_VCW, "<<<< Get arguments error (%s)", err.message);
642 dbus_error_free(&err);
643 result = VC_ERROR_OPERATION_FAILED;
645 dbus_message_unref(result_msg);
648 SLOG(LOG_DEBUG, TAG_VCW, "<<<< vc widget stop : result = %d", result);
650 SLOG(LOG_ERROR, TAG_VCW, "<<<< vc widget stop : result = %d", result);
653 SLOG(LOG_DEBUG, TAG_VCW, "<<<< Result Message is NULL");
654 vc_widget_dbus_reconnect();
655 result = VC_ERROR_TIMED_OUT;
661 int vc_widget_dbus_request_cancel(int pid)
665 /* create a signal & check for errors */
666 msg = dbus_message_new_method_call(
667 VC_SERVER_SERVICE_NAME,
668 VC_SERVER_SERVICE_OBJECT_PATH, /* object name of the signal */
669 VC_SERVER_SERVICE_INTERFACE, /* interface name of the signal */
670 VC_WIDGET_METHOD_CANCEL); /* name of the signal */
673 SLOG(LOG_ERROR, TAG_VCW, ">>>> vc widget cancel : Fail to make message ");
674 return VC_ERROR_OPERATION_FAILED;
676 SLOG(LOG_DEBUG, TAG_VCW, ">>>> vc widget cancel : pid(%d)", pid);
679 dbus_message_append_args(msg,
680 DBUS_TYPE_INT32, &pid,
684 dbus_error_init(&err);
686 DBusMessage* result_msg;
687 int result = VC_ERROR_OPERATION_FAILED;
689 result_msg = dbus_connection_send_with_reply_and_block(g_w_conn_sender, msg, g_w_waiting_time, &err);
690 dbus_message_unref(msg);
692 if (dbus_error_is_set(&err)) {
693 SLOG(LOG_ERROR, TAG_VCW, "[ERROR] Dbus Error (%s)", err.message);
694 dbus_error_free(&err);
697 if (NULL != result_msg) {
698 dbus_message_get_args(result_msg, &err,
699 DBUS_TYPE_INT32, &result,
702 if (dbus_error_is_set(&err)) {
703 SLOG(LOG_ERROR, TAG_VCW, "<<<< Get arguments error (%s)", err.message);
704 dbus_error_free(&err);
705 result = VC_ERROR_OPERATION_FAILED;
707 dbus_message_unref(result_msg);
710 SLOG(LOG_DEBUG, TAG_VCW, "<<<< vc widget cancel : result = %d", result);
712 SLOG(LOG_ERROR, TAG_VCW, "<<<< vc widget cancel : result = %d", result);
715 SLOG(LOG_DEBUG, TAG_VCW, "<<<< Result Message is NULL");
716 vc_widget_dbus_reconnect();
717 result = VC_ERROR_TIMED_OUT;