2 * Copyright 2018-2019 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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.
17 #include <dbus/dbus.h>
22 #include "multi_assistant_main.h"
23 #include "multi_assistant_dbus_server.h"
24 #include "multi_assistant_dbus.h"
26 static DBusConnection* g_conn_sender = NULL;
27 static DBusConnection* g_conn_listener = NULL;
29 static Ecore_Fd_Handler* g_dbus_fd_handler = NULL;
31 int mas_dbus_reconnect()
33 if (!g_conn_sender || !g_conn_listener) {
34 mas_dbus_close_connection();
36 if (0 != mas_dbus_open_connection()) {
37 MAS_LOGE("[ERROR] Fail to reconnect");
41 MAS_LOGD("[DBUS] Reconnect");
45 bool sender_connected = dbus_connection_get_is_connected(g_conn_sender);
46 bool listener_connected = dbus_connection_get_is_connected(g_conn_listener);
47 MAS_LOGW("[DBUS] Sender(%s) Listener(%s)",
48 sender_connected ? "Connected" : "Not connected", listener_connected ? "Connected" : "Not connected");
50 if (false == sender_connected || false == listener_connected) {
51 mas_dbus_close_connection();
53 if (0 != mas_dbus_open_connection()) {
54 MAS_LOGE("[ERROR] Fail to reconnect");
58 MAS_LOGD("[DBUS] Reconnect");
64 static int __dbus_check()
66 if (NULL == g_conn_sender || NULL == g_conn_listener) {
67 MAS_LOGE("[ERROR] NULL connection");
68 return mas_dbus_reconnect();
73 int mas_check_dbus_connection()
75 if (NULL == g_conn_sender || NULL == g_conn_listener) {
76 MAS_LOGE("[ERROR] NULL connection sender(%p), listener(%p)", g_conn_sender, g_conn_listener);
82 int masc_dbus_send_hello(int pid)
84 if (0 != __dbus_check()) {
85 return -1; //MAS_ERROR_OPERATION_FAILED;
90 dbus_error_init(&err);
92 msg = dbus_message_new_method_call(
93 MA_CLIENT_SERVICE_NAME,
94 MA_CLIENT_SERVICE_OBJECT_PATH,
95 MA_CLIENT_SERVICE_INTERFACE,
99 MAS_LOGE("[DBus ERROR] Request masc hello : Fail to make message");
104 DBusMessage* result_msg = NULL;
105 result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, 500, &err);
107 if (dbus_error_is_set(&err)) {
108 MAS_LOGE("[Dbus ERROR] Dbus Error (%s)", err.message);
109 dbus_error_free(&err);
112 dbus_message_unref(msg);
114 if (NULL != result_msg) {
115 dbus_message_unref(result_msg);
118 result = -1; //MAS_ERROR_TIMED_OUT;
124 int masc_dbus_send_error_message(int reason, const char* err_msg)
126 if (0 != __dbus_check()) {
127 return -1; //MAS_ERROR_OPERATION_FAILED;
130 if (NULL == g_conn_sender) {
131 MAS_LOGE("[Dbus ERROR] Dbus connection is not available");
135 DBusMessage* msg = NULL;
137 /* create a message */
138 msg = dbus_message_new_signal(
139 MA_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
140 MA_CLIENT_SERVICE_INTERFACE, /* interface name of the signal */
141 MAS_METHOD_ERROR); /* name of the signal */
144 MAS_LOGE("[Dbus ERROR] Fail to create error message");
148 char* temp_err_msg = NULL;
151 temp_err_msg = strdup(err_msg);
153 temp_err_msg = strdup("#NULL");
155 dbus_message_append_args(msg,
156 DBUS_TYPE_INT32, &reason,
157 DBUS_TYPE_STRING, &temp_err_msg,
160 dbus_message_set_no_reply(msg, TRUE);
162 if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
163 MAS_LOGE("[Dbus ERROR] <<<< error message : Out Of Memory !");
165 MAS_LOGD("<<<< Send error message : reason(%d), err_msg(%s)", reason, temp_err_msg);
166 dbus_connection_flush(g_conn_sender);
169 dbus_message_unref(msg);
176 int masc_dbus_send_streaming_audio_data(int pid, int event, unsigned char* data, unsigned int data_size)
178 if (0 != __dbus_check()) {
179 return -1; //MAS_ERROR_OPERATION_FAILED;
184 dbus_error_init(&err);
186 char service_name[64];
187 memset(service_name, '\0', 64);
188 snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
190 msg = dbus_message_new_method_call(
192 MA_CLIENT_SERVICE_OBJECT_PATH,
193 MA_CLIENT_SERVICE_INTERFACE,
194 MAS_METHOD_STREAMING_AUDIO_DATA);
196 static int count = 0;
198 MAS_LOGE(">>>> Request mas send utterance stream : Fail to make message");
199 return -1; // MAS_ERROR_OPERATION_FAILED;
201 MAS_LOGD(">>>> Request mas send utterance stream : %s event(%d) %d", service_name, event, count++);
204 if (true != dbus_message_append_args(msg,
205 DBUS_TYPE_INT32, &event,
206 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
208 DBUS_TYPE_INVALID)) {
209 dbus_message_unref(msg);
210 MAS_LOGE("[ERROR] Fail to append args");
214 dbus_message_set_no_reply(msg, TRUE);
216 if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
217 MAS_LOGE("[Dbus ERROR] Fail to Send");
218 return -1; // MAS_ERROR_OPERATION_FAILED;
220 MAS_LOGD("[Dbus DEBUG] Success to Send utterance stream");
221 dbus_connection_flush(g_conn_sender);
224 dbus_message_unref(msg);
228 int masc_dbus_active_state_change(int pid, int state)
230 if (0 != __dbus_check()) {
231 return -1; //MAS_ERROR_OPERATION_FAILED;
237 dbus_error_init(&err);
239 char service_name[64];
240 memset(service_name, '\0', 64);
241 snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
243 msg = dbus_message_new_method_call(
245 MA_CLIENT_SERVICE_OBJECT_PATH,
246 MA_CLIENT_SERVICE_INTERFACE,
247 MAS_METHOD_ACTIVE_STATE_CHANGE);
249 static int count = 0;
251 MAS_LOGE(">>>> Request mas send activate message : Fail to make message");
252 return -1; // MAS_ERROR_OPERATION_FAILED;
254 MAS_LOGD(">>>> Request mas send activate message : %s", service_name);
257 if (true != dbus_message_append_args(msg,
258 DBUS_TYPE_INT32, &state,
259 DBUS_TYPE_INVALID)) {
260 dbus_message_unref(msg);
261 MAS_LOGE("[ERROR] Fail to append args");
265 dbus_message_set_no_reply(msg, TRUE);
267 if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
268 MAS_LOGE("[Dbus ERROR] Fail to Send");
269 return -1; // MAS_ERROR_OPERATION_FAILED;
271 MAS_LOGD("[Dbus DEBUG] Success to Send activate message : %d", state);
272 dbus_connection_flush(g_conn_sender);
275 dbus_message_unref(msg);
279 int masc_dbus_send_preprocessing_information(int pid, const char* app_id)
281 if (0 != __dbus_check()) {
282 return -1; //MAS_ERROR_OPERATION_FAILED;
288 dbus_error_init(&err);
290 char service_name[64];
291 memset(service_name, '\0', 64);
292 snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
294 msg = dbus_message_new_method_call(
296 MA_CLIENT_SERVICE_OBJECT_PATH,
297 MA_CLIENT_SERVICE_INTERFACE,
298 MAS_METHOD_SEND_PREPROCESSING_INFORMATION);
300 static int count = 0;
302 MAS_LOGE(">>>> Request mas send preprocessing assistant information : Fail to make message");
303 return -1; // MAS_ERROR_OPERATION_FAILED;
305 MAS_LOGD(">>>> Request mas send preprocessing assistant information : %s", service_name);
308 char* temp_app_id = NULL;
310 temp_app_id = strdup("#NULL");
312 temp_app_id = strdup(app_id);
314 if (true != dbus_message_append_args(msg,
315 DBUS_TYPE_STRING, &temp_app_id,
316 DBUS_TYPE_INVALID)) {
317 dbus_message_unref(msg);
318 MAS_LOGE("[ERROR] Fail to append args");
324 dbus_message_set_no_reply(msg, TRUE);
326 if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
327 MAS_LOGE("[Dbus ERROR] Fail to Send");
330 return -1; // MAS_ERROR_OPERATION_FAILED;
332 MAS_LOGD("[Dbus DEBUG] Success to Send preprocessing assistant information : %s", app_id);
333 dbus_connection_flush(g_conn_sender);
336 dbus_message_unref(msg);
344 int masc_dbus_send_streaming_section_changed(int pid, int section)
346 if (0 != __dbus_check()) {
347 return -1; //MAS_ERROR_OPERATION_FAILED;
353 dbus_error_init(&err);
355 char service_name[64];
356 memset(service_name, '\0', 64);
357 snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
359 msg = dbus_message_new_method_call(
361 MA_CLIENT_SERVICE_OBJECT_PATH,
362 MA_CLIENT_SERVICE_INTERFACE,
363 MAS_METHOD_AUDIO_STREAMING_DATA_SECTION);
365 static int count = 0;
367 MAS_LOGE(">>>> Request mas send streaming section changed information : Fail to make message");
368 return -1; // MAS_ERROR_OPERATION_FAILED;
370 MAS_LOGD(">>>> Request mas send streaming section changed information : %s", service_name);
373 if (true != dbus_message_append_args(msg,
374 DBUS_TYPE_INT32, §ion,
375 DBUS_TYPE_INVALID)) {
376 dbus_message_unref(msg);
377 MAS_LOGE("[ERROR] Fail to append args");
381 dbus_message_set_no_reply(msg, TRUE);
383 if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
384 MAS_LOGE("[Dbus ERROR] Fail to Send");
385 return -1; // MAS_ERROR_OPERATION_FAILED;
387 MAS_LOGD("[Dbus DEBUG] Success to Send streaming section changed information : %d", section);
388 dbus_connection_flush(g_conn_sender);
391 dbus_message_unref(msg);
396 int masc_dbus_send_preprocessing_result(int pid, bool result)
398 if (0 != __dbus_check()) {
399 return -1; //MAS_ERROR_OPERATION_FAILED;
405 dbus_error_init(&err);
407 char service_name[64];
408 memset(service_name, '\0', 64);
409 snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
411 msg = dbus_message_new_method_call(
413 MA_CLIENT_SERVICE_OBJECT_PATH,
414 MA_CLIENT_SERVICE_INTERFACE,
415 MAS_METHOD_SEND_PREPROCESSING_RESULT);
417 static int count = 0;
419 MAS_LOGE(">>>> Request mas send preprocessing result : Fail to make message");
420 return -1; // MAS_ERROR_OPERATION_FAILED;
422 MAS_LOGD(">>>> Request mas send preprocessing result : %s", service_name);
425 int temp_result = result;
427 if (true != dbus_message_append_args(msg,
428 DBUS_TYPE_INT32, &temp_result,
429 DBUS_TYPE_INVALID)) {
430 dbus_message_unref(msg);
431 MAS_LOGE("[ERROR] Fail to append args");
435 dbus_message_set_no_reply(msg, TRUE);
437 if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
438 MAS_LOGE("[Dbus ERROR] Fail to Send");
439 return -1; // MAS_ERROR_OPERATION_FAILED;
441 MAS_LOGD("[Dbus DEBUG] Success to Send preprocessing result : %d", temp_result);
442 dbus_connection_flush(g_conn_sender);
445 dbus_message_unref(msg);
450 int masc_dbus_send_wakeup_engine_command(int pid, const char* command)
452 if (0 != __dbus_check()) {
453 return -1; //MAS_ERROR_OPERATION_FAILED;
459 dbus_error_init(&err);
461 char service_name[64];
462 memset(service_name, '\0', 64);
463 snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
465 msg = dbus_message_new_method_call(
467 MA_CLIENT_SERVICE_OBJECT_PATH,
468 MA_CLIENT_SERVICE_INTERFACE,
469 MAS_METHOD_SEND_WAKEUP_ENGINE_COMMAND);
471 static int count = 0;
473 MAS_LOGE(">>>> Request mas send wakeup engine command : Fail to make message");
474 return -1; // MAS_ERROR_OPERATION_FAILED;
476 MAS_LOGD(">>>> Request mas send wakeup engine command : %s", service_name);
479 char* temp_command = NULL;
481 temp_command = strdup("#NULL");
483 temp_command = strdup(command);
485 if (true != dbus_message_append_args(msg,
486 DBUS_TYPE_STRING, &temp_command,
487 DBUS_TYPE_INVALID)) {
488 dbus_message_unref(msg);
489 MAS_LOGE("[ERROR] Fail to append args");
495 dbus_message_set_no_reply(msg, TRUE);
497 if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
498 MAS_LOGE("[Dbus ERROR] Fail to Send");
501 return -1; // MAS_ERROR_OPERATION_FAILED;
503 MAS_LOGD("[Dbus DEBUG] Success to Send wakeup_engine_command : %s", command);
504 dbus_connection_flush(g_conn_sender);
507 dbus_message_unref(msg);
515 int masc_ui_dbus_send_hello(void)
517 if (0 != __dbus_check()) {
518 return -1; //MAS_ERROR_OPERATION_FAILED;
524 dbus_error_init(&err);
526 msg = dbus_message_new_method_call(
527 MA_UI_CLIENT_SERVICE_NAME,
528 MA_UI_CLIENT_SERVICE_OBJECT_PATH,
529 MA_UI_CLIENT_SERVICE_INTERFACE,
533 MAS_LOGE("[DBus ERROR] Request masc hello : Fail to make message");
537 DBusMessage* result_msg = NULL;
540 result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, 500, &err);
542 if (dbus_error_is_set(&err)) {
543 MAS_LOGE("[Dbus ERROR] Dbus Error (%s)", err.message);
544 dbus_error_free(&err);
547 dbus_message_unref(msg);
549 if (NULL != result_msg) {
550 dbus_message_unref(result_msg);
553 result = -1; //ERROR_TIMED_OUT;
559 int masc_ui_dbus_send_asr_result(int pid, int event, char* asr_result)
561 if (0 != __dbus_check()) {
562 return -1; //MAS_ERROR_OPERATION_FAILED;
567 msg = dbus_message_new_method_call(
568 MA_UI_CLIENT_SERVICE_NAME,
569 MA_UI_CLIENT_SERVICE_OBJECT_PATH,
570 MA_UI_CLIENT_SERVICE_INTERFACE,
571 MAS_UI_METHOD_SEND_ASR_RESULT);
574 MAS_LOGE("@@ Request multi-assistant send ASR result : Fail to make message");
575 return -1; //MA_ERROR_OPERATION_FAILED;
577 MAS_LOGD("[DEBUG] multi-assistant send ASR result, asr_result(%p)", asr_result);
580 char* temp_asr_result = NULL;
582 temp_asr_result = strdup("#NULL");
584 temp_asr_result = strdup(asr_result);
586 dbus_message_append_args(msg,
587 DBUS_TYPE_INT32, &pid,
588 DBUS_TYPE_INT32, &event,
589 DBUS_TYPE_STRING, &temp_asr_result,
592 dbus_message_set_no_reply(msg, TRUE);
594 if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
595 MAS_LOGE("[Dbus ERROR] Fail to Send");
596 if (NULL != temp_asr_result) {
597 free(temp_asr_result);
598 temp_asr_result = NULL;
600 return -1; // MAS_ERROR_OPERATION_FAILED;
602 MAS_LOGD("[Dbus DEBUG] Success to Send ASR result");
603 dbus_connection_flush(g_conn_sender);
606 dbus_message_unref(msg);
609 free(temp_asr_result);
613 int masc_ui_dbus_send_result(int pid, const char* display_text, const char* utterance_text, const char* result_json)
615 if (0 != __dbus_check()) {
616 return -1; //MA_ERROR_OPERATION_FAILED;
621 msg = dbus_message_new_method_call(
622 MA_UI_CLIENT_SERVICE_NAME,
623 MA_UI_CLIENT_SERVICE_OBJECT_PATH,
624 MA_UI_CLIENT_SERVICE_INTERFACE,
625 MAS_UI_METHOD_SEND_RESULT);
628 MAS_LOGE("@@ Request multi-assistant send result : Fail to make message");
629 return -1; //MA_ERROR_OPERATION_FAILED;
631 MAS_LOGD("[DEBUG] multi-assistant send result");
633 char* temp_display_text = NULL;
634 char* temp_utterance_text = NULL;
635 char* temp_result_json = NULL;
638 dbus_message_append_args(msg,
639 DBUS_TYPE_INT32, &pid,
640 DBUS_TYPE_STRING, &display_text,
641 DBUS_TYPE_STRING, &utterance_text,
642 DBUS_TYPE_STRING, &result_json,
645 dbus_message_set_no_reply(msg, TRUE);
647 if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
648 MAS_LOGE("[Dbus ERROR] Fail to Send");
649 return -1; //MA_ERROR_OPERATION_FAILED;
651 MAS_LOGD("[Dbus DEBUG] Success to Send result");
652 dbus_connection_flush(g_conn_sender);
655 dbus_message_unref(msg);
658 temp_display_text = strdup("#NULL");
660 temp_display_text = strdup(display_text);
662 temp_utterance_text = strdup("#NULL");
664 temp_utterance_text = strdup(utterance_text);
666 temp_result_json = strdup("#NULL");
668 temp_result_json = strdup(result_json);
670 dbus_message_append_args(msg,
671 DBUS_TYPE_INT32, &pid,
672 DBUS_TYPE_STRING, &temp_display_text,
673 DBUS_TYPE_STRING, &temp_utterance_text,
674 DBUS_TYPE_STRING, &temp_result_json,
677 dbus_message_set_no_reply(msg, TRUE);
679 if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
680 MAS_LOGE("[Dbus ERROR] Fail to Send");
681 if (temp_display_text)
682 free(temp_display_text);
683 if (temp_utterance_text)
684 free(temp_utterance_text);
685 if (temp_result_json)
686 free(temp_result_json);
687 return -1; //MA_ERROR_OPERATION_FAILED;
689 MAS_LOGD("[Dbus DEBUG] Success to Send result");
690 dbus_connection_flush(g_conn_sender);
693 dbus_message_unref(msg);
695 if (temp_display_text)
696 free(temp_display_text);
697 if (temp_utterance_text)
698 free(temp_utterance_text);
699 if (temp_result_json)
700 free(temp_result_json);
705 int masc_ui_dbus_change_assistant(char* app_id)
707 if (0 != __dbus_check()) {
708 return -1; //MAS_ERROR_OPERATION_FAILED;
711 if (NULL == app_id) {
712 MAS_LOGE("@@ Request multi-assistant send change assistant request : Fail to make message");
713 return -1; //MA_ERROR_OPERATION_FAILED;
715 MAS_LOGD("[DEBUG] multi-assistant send change assistant request app_id(%s)", app_id);
720 msg = dbus_message_new_method_call(
721 MA_UI_CLIENT_SERVICE_NAME,
722 MA_UI_CLIENT_SERVICE_OBJECT_PATH,
723 MA_UI_CLIENT_SERVICE_INTERFACE,
724 MAS_UI_METHOD_CHANGE_ASSISTANT);
726 dbus_message_append_args(msg,
727 DBUS_TYPE_STRING, &app_id,
730 dbus_message_set_no_reply(msg, TRUE);
732 if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
733 MAS_LOGE("[Dbus ERROR] Fail to Send");
734 return -1; // MAS_ERROR_OPERATION_FAILED;
736 MAS_LOGD("[Dbus DEBUG] Success to Send change assistant request");
737 dbus_connection_flush(g_conn_sender);
740 dbus_message_unref(msg);
745 int masc_ui_dbus_send_error_message(int reason, const char* err_msg)
747 if (NULL == g_conn_sender) {
748 MAS_LOGE("[Dbus ERROR] Dbus connection is not available");
752 DBusMessage* msg = NULL;
754 /* create a message */
755 msg = dbus_message_new_signal(
756 MA_UI_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
757 MA_UI_CLIENT_SERVICE_INTERFACE, /* interface name of the signal */
758 MAS_UI_METHOD_ERROR); /* name of the signal */
761 MAS_LOGE("[Dbus ERROR] Fail to create error message");
765 char* temp_err_msg = NULL;
768 temp_err_msg = strdup(err_msg);
770 temp_err_msg = strdup("#NULL");
772 dbus_message_append_args(msg,
773 DBUS_TYPE_INT32, &reason,
774 DBUS_TYPE_STRING, &temp_err_msg,
777 dbus_message_set_no_reply(msg, TRUE);
779 if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
780 MAS_LOGE("[Dbus ERROR] <<<< error message : Out Of Memory !");
782 MAS_LOGD("<<<< Send error message : reason(%d), err_msg(%s)", reason, temp_err_msg);
783 dbus_connection_flush(g_conn_sender);
786 dbus_message_unref(msg);
793 int masc_ui_dbus_send_recognition_result(int pid, int result)
795 if (0 != __dbus_check()) {
796 return -1; //MAS_ERROR_OPERATION_FAILED;
801 msg = dbus_message_new_method_call(
802 MA_UI_CLIENT_SERVICE_NAME,
803 MA_UI_CLIENT_SERVICE_OBJECT_PATH,
804 MA_UI_CLIENT_SERVICE_INTERFACE,
805 MAS_UI_METHOD_SEND_RECOGNITION_RESULT);
808 MAS_LOGE("@@ Request multi-assistant send recognition result : Fail to make message");
809 return -1; //MA_ERROR_OPERATION_FAILED;
811 MAS_LOGD("[DEBUG] multi-assistant send recognition result(%d)", result);
814 dbus_message_append_args(msg,
815 DBUS_TYPE_INT32, &pid,
816 DBUS_TYPE_INT32, &result,
819 dbus_message_set_no_reply(msg, TRUE);
821 if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
822 MAS_LOGE("[Dbus ERROR] Fail to Send");
823 return -1; // MAS_ERROR_OPERATION_FAILED;
825 MAS_LOGD("[Dbus DEBUG] Success to Send ASR result");
826 dbus_connection_flush(g_conn_sender);
829 dbus_message_unref(msg);
834 int masc_ui_dbus_enable_common_ui(int enable)
836 if (0 != __dbus_check()) {
837 return -1; //MAS_ERROR_OPERATION_FAILED;
842 msg = dbus_message_new_method_call(
843 MA_UI_CLIENT_SERVICE_NAME,
844 MA_UI_CLIENT_SERVICE_OBJECT_PATH,
845 MA_UI_CLIENT_SERVICE_INTERFACE,
846 MAS_UI_METHOD_ENABLE_COMMON_UI);
849 MAS_LOGE("@@ Request multi-assistant enable common ui : Fail to make message");
850 return -1; //MA_ERROR_OPERATION_FAILED;
852 MAS_LOGD("[DEBUG] multi-assistant enable common ui (%d)", enable);
855 dbus_message_append_args(msg,
856 DBUS_TYPE_INT32, &enable,
859 dbus_message_set_no_reply(msg, TRUE);
861 if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
862 MAS_LOGE("[Dbus ERROR] Fail to Send");
863 return -1; // MAS_ERROR_OPERATION_FAILED;
865 MAS_LOGD("[Dbus DEBUG] Success to Send ASR result");
866 dbus_connection_flush(g_conn_sender);
869 dbus_message_unref(msg);
874 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
876 if (NULL == g_conn_listener) return ECORE_CALLBACK_RENEW;
878 dbus_connection_read_write_dispatch(g_conn_listener, 50);
881 DBusMessage* msg = NULL;
882 msg = dbus_connection_pop_message(g_conn_listener);
884 if (true != dbus_connection_get_is_connected(g_conn_listener)) {
885 MAS_LOGE("[ERROR] Connection is disconnected");
886 return ECORE_CALLBACK_RENEW;
889 /* loop again if we haven't read a message */
891 return ECORE_CALLBACK_RENEW;
895 if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_HELLO)) {
896 ma_service_dbus_hello(g_conn_listener, msg);
898 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_INITIALIZE)) {
899 ma_service_dbus_initialize(g_conn_listener, msg);
901 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_DEINITIALIZE)) {
902 ma_service_dbus_deinitialize(g_conn_listener, msg);
904 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_GET_RECORDING_AUDIO_FORMAT)) {
905 ma_service_dbus_get_audio_format(g_conn_listener, msg);
907 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_GET_RECORDING_AUDIO_SOURCE_TYPE)) {
908 ma_service_dbus_get_audio_source_type(g_conn_listener, msg);
910 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_ASR_RESULT)) {
911 ma_service_dbus_send_asr_result(g_conn_listener, msg);
913 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_RESULT)) {
914 ma_service_dbus_send_result(g_conn_listener, msg);
916 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_RECOGNITION_RESULT)) {
917 ma_service_dbus_send_recognition_result(g_conn_listener, msg);
919 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_START_STREAMING_AUDIO_DATA)) {
920 ma_service_dbus_start_streaming_audio_data(g_conn_listener, msg);
922 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_STOP_STREAMING_AUDIO_DATA)) {
923 ma_service_dbus_stop_streaming_audio_data(g_conn_listener, msg);
925 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_UPDATE_VOICE_FEEDBACK_STATE)) {
926 ma_service_dbus_update_voice_feedback_state(g_conn_listener, msg);
928 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_ASSISTANT_SPECIFIC_COMMAND)) {
929 ma_service_dbus_send_assistant_specific_command(g_conn_listener, msg);
931 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SET_BACKGROUND_VOLUME)) {
932 ma_service_dbus_set_background_volume(g_conn_listener, msg);
934 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SET_PREPROCESSING_ALLOW_MODE)) {
935 ma_service_dbus_set_preprocessing_allow_mode(g_conn_listener, msg);
937 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_PREPROCESSING_RESULT)) {
938 ma_service_dbus_send_preprocessing_result(g_conn_listener, msg);
940 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SET_WAKE_WORD_AUDIO_REQUIRE_FLAG)) {
941 ma_service_dbus_set_wake_word_audio_require_flag(g_conn_listener, msg);
943 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SET_ASSISTANT_LANGUAGE)) {
944 ma_service_dbus_set_assistant_language(g_conn_listener, msg);
946 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_UI_METHOD_INITIALIZE)) {
947 ma_service_ui_dbus_initialize(g_conn_listener, msg);
949 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_UI_METHOD_DEINITIALIZE)) {
950 ma_service_ui_dbus_deinitialize(g_conn_listener, msg);
952 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_UI_METHOD_CHANGE_ASSISTANT)) {
953 ma_service_ui_dbus_change_assistant(g_conn_listener, msg);
956 MAS_LOGD("Message is NOT valid");
959 /* free the message */
960 dbus_message_unref(msg);
963 return ECORE_CALLBACK_RENEW;
966 static void __mas_dbus_connection_free()
968 if (NULL != g_conn_listener) {
969 dbus_connection_close(g_conn_listener);
970 dbus_connection_unref(g_conn_listener);
971 g_conn_listener = NULL;
973 if (NULL != g_conn_sender) {
974 dbus_connection_close(g_conn_sender);
975 dbus_connection_unref(g_conn_sender);
976 g_conn_sender = NULL;
980 int mas_dbus_open_connection()
983 dbus_error_init(&err);
987 /* Create connection for sender */
988 g_conn_sender = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
990 if (dbus_error_is_set(&err)) {
991 MAS_LOGE("[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
992 dbus_error_free(&err);
995 if (NULL == g_conn_sender) {
996 MAS_LOGE("[Dbus ERROR] Fail to get dbus connection");
1000 dbus_connection_set_exit_on_disconnect(g_conn_sender, false);
1002 /* connect to the bus and check for errors */
1003 g_conn_listener = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
1005 if (dbus_error_is_set(&err)) {
1006 MAS_LOGE("[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
1007 dbus_error_free(&err);
1010 if (NULL == g_conn_listener) {
1011 MAS_LOGE("[Dbus ERROR] Fail to get dbus connection");
1012 __mas_dbus_connection_free();
1016 dbus_connection_set_exit_on_disconnect(g_conn_listener, false);
1018 /* request our name on the bus and check for errors */
1019 ret = dbus_bus_request_name(g_conn_listener, MA_SERVER_SERVICE_NAME, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
1021 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
1022 printf("Fail to be primary owner in dbus request.");
1023 MAS_LOGE("[Dbus ERROR] Fail to be primary owner");
1024 __mas_dbus_connection_free();
1028 if (dbus_error_is_set(&err)) {
1029 MAS_LOGE("[Dbus ERROR] dbus_bus_request_name() : %s", err.message);
1030 dbus_error_free(&err);
1031 __mas_dbus_connection_free();
1035 /* Flush messages which are received before fd event handler registration */
1036 while (DBUS_DISPATCH_DATA_REMAINS == dbus_connection_get_dispatch_status(g_conn_listener)) {
1037 listener_event_callback(NULL, NULL);
1040 /* add a rule for getting signal */
1042 snprintf(rule, 128, "type='signal',interface='%s'", MA_SERVER_SERVICE_INTERFACE);
1044 /* add a rule for which messages we want to see */
1045 dbus_bus_add_match(g_conn_listener, rule, &err);/* see signals from the given interface */
1047 if (dbus_error_is_set(&err)) {
1048 MAS_LOGE("[Dbus ERROR] dbus_bus_add_match() : %s", err.message);
1049 dbus_error_free(&err);
1050 __mas_dbus_connection_free();
1055 if (1 != dbus_connection_get_unix_fd(g_conn_listener, &fd)) {
1056 MAS_LOGE("fail to get fd from dbus ");
1057 __mas_dbus_connection_free();
1060 MAS_LOGD("Get fd from dbus : %d", fd);
1064 g_dbus_fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)listener_event_callback, g_conn_listener, NULL, NULL);
1066 if (NULL == g_dbus_fd_handler) {
1067 MAS_LOGE("[Dbus ERROR] Fail to get fd handler");
1068 __mas_dbus_connection_free();
1075 int mas_dbus_close_connection()
1078 dbus_error_init(&err);
1080 if (NULL != g_dbus_fd_handler) {
1081 ecore_main_fd_handler_del(g_dbus_fd_handler);
1082 g_dbus_fd_handler = NULL;
1085 dbus_bus_release_name(g_conn_listener, MA_SERVER_SERVICE_NAME, &err);
1087 if (dbus_error_is_set(&err)) {
1088 MAS_LOGE("[Dbus ERROR] dbus_bus_release_name() : %s", err.message);
1089 dbus_error_free(&err);
1092 __mas_dbus_connection_free();