2 * Copyright 2020 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.
20 #include <message_port.h>
22 #include "service_common.h"
23 #include "service_main.h"
24 #include "service_ipc_dbus.h"
26 int CServiceIpcDbus::reconnect()
28 if (!mConnectionSender || !mConnectionListener) {
31 if (0 != open_connection()) {
32 MAS_LOGE("[ERROR] Fail to reconnect");
36 MAS_LOGD("[DBUS] Reconnect");
40 bool sender_connected = dbus_connection_get_is_connected(mConnectionSender);
41 bool listener_connected = dbus_connection_get_is_connected(mConnectionListener);
42 MAS_LOGW("[DBUS] Sender(%s) Listener(%s)",
43 sender_connected ? "Connected" : "Not connected", listener_connected ? "Connected" : "Not connected");
45 if (false == sender_connected || false == listener_connected) {
48 if (0 != open_connection()) {
49 MAS_LOGE("[ERROR] Fail to reconnect");
53 MAS_LOGD("[DBUS] Reconnect");
59 int CServiceIpcDbus::__dbus_check()
61 if (NULL == mConnectionSender || NULL == mConnectionListener) {
62 MAS_LOGE("[ERROR] NULL connection");
68 int CServiceIpcDbus::mas_check_dbus_connection()
70 if (NULL == mConnectionSender || NULL == mConnectionListener) {
71 MAS_LOGE("[ERROR] NULL connection sender(%p), listener(%p)", mConnectionSender, mConnectionListener);
77 int CServiceIpcDbus::send_hello(int pid)
79 if (0 != __dbus_check()) {
80 return -1; //MAS_ERROR_OPERATION_FAILED;
85 dbus_error_init(&err);
87 msg = dbus_message_new_method_call(
88 MA_CLIENT_SERVICE_NAME,
89 MA_CLIENT_SERVICE_OBJECT_PATH,
90 MA_CLIENT_SERVICE_INTERFACE,
94 MAS_LOGE("[DBus ERROR] Request masc hello : Fail to make message");
99 DBusMessage* result_msg = NULL;
100 result_msg = dbus_connection_send_with_reply_and_block(mConnectionSender, msg, 500, &err);
102 if (dbus_error_is_set(&err)) {
103 MAS_LOGE("[Dbus ERROR] Dbus Error (%s)", err.message);
104 dbus_error_free(&err);
107 dbus_message_unref(msg);
109 if (NULL != result_msg) {
110 dbus_message_unref(result_msg);
113 result = -1; //MAS_ERROR_TIMED_OUT;
119 int CServiceIpcDbus::send_error_message(int reason, const char* err_msg)
121 if (0 != __dbus_check()) {
122 return -1; //MAS_ERROR_OPERATION_FAILED;
125 if (NULL == mConnectionSender) {
126 MAS_LOGE("[Dbus ERROR] Dbus connection is not available");
130 DBusMessage* msg = NULL;
132 /* create a message */
133 msg = dbus_message_new_signal(
134 MA_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
135 MA_CLIENT_SERVICE_INTERFACE, /* interface name of the signal */
136 MAS_METHOD_ERROR); /* name of the signal */
139 MAS_LOGE("[Dbus ERROR] Fail to create error message");
143 char* temp_err_msg = NULL;
146 temp_err_msg = strdup(err_msg);
148 temp_err_msg = strdup("#NULL");
150 dbus_message_append_args(msg,
151 DBUS_TYPE_INT32, &reason,
152 DBUS_TYPE_STRING, &temp_err_msg,
155 dbus_message_set_no_reply(msg, TRUE);
157 if (!dbus_connection_send(mConnectionSender, msg, NULL)) {
158 MAS_LOGE("[Dbus ERROR] <<<< error message : Out Of Memory !");
160 MAS_LOGI("<<<< Send error message : reason(%d), err_msg(%s)", reason, temp_err_msg);
161 dbus_connection_flush(mConnectionSender);
164 dbus_message_unref(msg);
171 const char *message_port = "ma_streaming_port";
173 #define STREAMING_BUFFER_SIZE 4096
176 streaming_data_type_audio_data,
177 streaming_data_type_streaming_section
178 } streaming_data_type_e;
181 unsigned int streaming_data_size;
182 int streaming_data_type;
183 int streaming_data_serial;
184 } streaming_data_header;
188 unsigned int data_size;
189 } streaming_data_audio_data_header;
193 } streaming_data_streaming_section_header;
195 static void masc_message_port_error(int error)
197 MAS_LOGE("message_port error found : %s",
198 (MESSAGE_PORT_ERROR_NONE == error) ? "MESSAGE_PORT_ERROR_NONE" :
199 (MESSAGE_PORT_ERROR_IO_ERROR == error) ? "MESSAGE_PORT_ERROR_IO_ERROR" :
200 (MESSAGE_PORT_ERROR_OUT_OF_MEMORY == error) ? "MESSAGE_PORT_ERROR_OUT_OF_MEMORY" :
201 (MESSAGE_PORT_ERROR_INVALID_PARAMETER == error) ? "MESSAGE_PORT_ERROR_INVALID_PARAMETER" :
202 (MESSAGE_PORT_ERROR_PORT_NOT_FOUND == error) ? "MESSAGE_PORT_ERROR_PORT_NOT_FOUND" :
203 (MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH == error) ? "MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH" :
204 (MESSAGE_PORT_ERROR_MAX_EXCEEDED == error) ? "MESSAGE_PORT_ERROR_MAX_EXCEEDED" :
205 (MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE == error) ? "MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE" :
206 "MESSAGE_PORT_ERROR_UNKNOWN");
209 int CServiceIpcDbus::send_streaming_audio_data(int pid, int event, void* data, unsigned int data_size)
211 if (nullptr == mClientManager) {
212 MAS_LOGE("mClientManager variable is NULL");
216 static unsigned char pending_buffer[STREAMING_BUFFER_SIZE];
217 static unsigned int pending_buffer_size = 0;
219 streaming_data_header header;
220 header.streaming_data_type = 0;
221 header.streaming_data_size = sizeof(streaming_data_header) + sizeof(streaming_data_audio_data_header) + data_size;
222 header.streaming_data_serial = mStreamingDataSerial++;
224 streaming_data_audio_data_header audio_data_header;
225 audio_data_header.event = event;
226 audio_data_header.data_size = data_size;
228 unsigned char buffer[STREAMING_BUFFER_SIZE];
229 size_t total_size = 0;
230 memcpy(buffer, &header, sizeof(header));
231 total_size += sizeof(header);
232 memcpy(buffer + total_size, &audio_data_header, sizeof(audio_data_header));
233 total_size += sizeof(audio_data_header);
234 memcpy(buffer + total_size, data, data_size);
235 total_size += data_size;
237 static int last_serial_waiting_for_flush = -1;
238 if (0 == header.streaming_data_serial % 50) {
239 last_serial_waiting_for_flush = header.streaming_data_serial;
240 MAS_LOGI("queueing streaming data, serial : %d", last_serial_waiting_for_flush);
242 if (pending_buffer_size + total_size > STREAMING_BUFFER_SIZE ||
243 MAS_SPEECH_STREAMING_EVENT_FINISH == event) {
244 bundle *b = bundle_create();
246 bundle_add_byte(b, "content", pending_buffer, pending_buffer_size);
247 int ret = message_port_send_message(
248 mClientManager->find_client_appid_by_pid(pid).c_str(), message_port, b);
249 if (MESSAGE_PORT_ERROR_NONE != ret)
250 masc_message_port_error(ret);
252 pending_buffer_size = 0;
255 MAS_LOGE("Bundle creation failed!!!");
258 if (-1 != last_serial_waiting_for_flush) {
259 MAS_LOGI("flushing streaming data, serial : %d", last_serial_waiting_for_flush);
260 last_serial_waiting_for_flush = -1;
264 if (MAS_SPEECH_STREAMING_EVENT_FINISH == event) {
265 bundle *b = bundle_create();
267 bundle_add_byte(b, "content", buffer, total_size);
268 int ret = message_port_send_message(
269 mClientManager->find_client_appid_by_pid(pid).c_str(), message_port, b);
270 if (MESSAGE_PORT_ERROR_NONE != ret)
271 masc_message_port_error(ret);
275 MAS_LOGE("Bundle creation failed!!!");
278 memcpy(pending_buffer + pending_buffer_size, buffer, total_size);
279 pending_buffer_size += total_size;
284 int CServiceIpcDbus::change_active_state(int pid, int state)
286 if (0 != __dbus_check()) {
287 return -1; //MAS_ERROR_OPERATION_FAILED;
293 dbus_error_init(&err);
295 char service_name[64];
296 memset(service_name, '\0', 64);
297 snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
299 msg = dbus_message_new_method_call(
301 MA_CLIENT_SERVICE_OBJECT_PATH,
302 MA_CLIENT_SERVICE_INTERFACE,
303 MAS_METHOD_ACTIVE_STATE_CHANGE);
306 MAS_LOGE(">>>> Request mas send activate message : Fail to make message");
307 return -1; // MAS_ERROR_OPERATION_FAILED;
309 MAS_LOGD(">>>> Request mas send activate message : %s", service_name);
312 if (true != dbus_message_append_args(msg,
313 DBUS_TYPE_INT32, &state,
314 DBUS_TYPE_INVALID)) {
315 dbus_message_unref(msg);
316 MAS_LOGE("[ERROR] Fail to append args");
320 dbus_message_set_no_reply(msg, TRUE);
322 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
323 MAS_LOGE("[Dbus ERROR] Fail to Send");
324 return -1; // MAS_ERROR_OPERATION_FAILED;
326 MAS_LOGI("[Dbus DEBUG] Success to Send activate message : %d %d", pid, state);
327 dbus_connection_flush(mConnectionSender);
330 dbus_message_unref(msg);
334 int CServiceIpcDbus::send_preprocessing_information(int pid, const char* app_id)
336 if (0 != __dbus_check()) {
337 return -1; //MAS_ERROR_OPERATION_FAILED;
343 dbus_error_init(&err);
345 char service_name[64];
346 memset(service_name, '\0', 64);
347 snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
349 msg = dbus_message_new_method_call(
351 MA_CLIENT_SERVICE_OBJECT_PATH,
352 MA_CLIENT_SERVICE_INTERFACE,
353 MAS_METHOD_SEND_PREPROCESSING_INFORMATION);
356 MAS_LOGE(">>>> Request mas send preprocessing assistant information : Fail to make message");
357 return -1; // MAS_ERROR_OPERATION_FAILED;
359 MAS_LOGD(">>>> Request mas send preprocessing assistant information : %s", service_name);
362 char* temp_app_id = NULL;
364 temp_app_id = strdup("#NULL");
366 temp_app_id = strdup(app_id);
368 if (true != dbus_message_append_args(msg,
369 DBUS_TYPE_STRING, &temp_app_id,
370 DBUS_TYPE_INVALID)) {
371 dbus_message_unref(msg);
372 MAS_LOGE("[ERROR] Fail to append args");
378 dbus_message_set_no_reply(msg, TRUE);
380 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
381 MAS_LOGE("[Dbus ERROR] Fail to Send");
384 return -1; // MAS_ERROR_OPERATION_FAILED;
386 MAS_LOGI("[Dbus DEBUG] Success to Send preprocessing assistant information : %d %s", pid, app_id);
387 dbus_connection_flush(mConnectionSender);
390 dbus_message_unref(msg);
398 int CServiceIpcDbus::send_streaming_section_changed(int pid, int section)
400 if (nullptr == mClientManager) {
401 MAS_LOGE("mClientManager variable is NULL");
405 bundle *b = bundle_create();
407 streaming_data_header header;
408 header.streaming_data_type = 1;
409 header.streaming_data_size = sizeof(streaming_data_header) + sizeof(streaming_data_streaming_section_header);
410 header.streaming_data_serial = mStreamingDataSerial++;
412 streaming_data_streaming_section_header streaming_section_header;
413 streaming_section_header.section = section;
415 unsigned char buffer[STREAMING_BUFFER_SIZE];
416 size_t total_size = 0;
417 memcpy(buffer, &header, sizeof(header));
418 total_size += sizeof(header);
419 memcpy(buffer + total_size, &streaming_section_header, sizeof(streaming_section_header));
420 total_size += sizeof(streaming_section_header);
422 bundle_add_byte(b, "content", buffer, total_size);
423 int ret = message_port_send_message(
424 mClientManager->find_client_appid_by_pid(pid).c_str(), message_port, b);
425 if (MESSAGE_PORT_ERROR_NONE != ret)
426 masc_message_port_error(ret);
433 int CServiceIpcDbus::send_preprocessing_result(int pid, bool result)
435 if (0 != __dbus_check()) {
436 return -1; //MAS_ERROR_OPERATION_FAILED;
442 dbus_error_init(&err);
444 char service_name[64];
445 memset(service_name, '\0', 64);
446 snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
448 msg = dbus_message_new_method_call(
450 MA_CLIENT_SERVICE_OBJECT_PATH,
451 MA_CLIENT_SERVICE_INTERFACE,
452 MAS_METHOD_SEND_PREPROCESSING_RESULT);
455 MAS_LOGE(">>>> Request mas send preprocessing result : Fail to make message");
456 return -1; // MAS_ERROR_OPERATION_FAILED;
458 MAS_LOGD(">>>> Request mas send preprocessing result : %s", service_name);
461 int temp_result = result;
463 if (true != dbus_message_append_args(msg,
464 DBUS_TYPE_INT32, &temp_result,
465 DBUS_TYPE_INVALID)) {
466 dbus_message_unref(msg);
467 MAS_LOGE("[ERROR] Fail to append args");
471 dbus_message_set_no_reply(msg, TRUE);
473 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
474 MAS_LOGE("[Dbus ERROR] Fail to Send");
475 return -1; // MAS_ERROR_OPERATION_FAILED;
477 MAS_LOGI("[Dbus DEBUG] Success to Send preprocessing result : %d %d", pid, temp_result);
478 dbus_connection_flush(mConnectionSender);
481 dbus_message_unref(msg);
486 int CServiceIpcDbus::send_wakeup_engine_command(int pid, const char* command)
488 if (0 != __dbus_check()) {
489 return -1; //MAS_ERROR_OPERATION_FAILED;
495 dbus_error_init(&err);
497 char service_name[64];
498 memset(service_name, '\0', 64);
499 snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
501 msg = dbus_message_new_method_call(
503 MA_CLIENT_SERVICE_OBJECT_PATH,
504 MA_CLIENT_SERVICE_INTERFACE,
505 MAS_METHOD_SEND_WAKEUP_ENGINE_COMMAND);
508 MAS_LOGE(">>>> Request mas send wakeup engine command : Fail to make message");
509 return -1; // MAS_ERROR_OPERATION_FAILED;
511 MAS_LOGD(">>>> Request mas send wakeup engine command : %s", service_name);
514 char* temp_command = NULL;
516 temp_command = strdup("#NULL");
518 temp_command = strdup(command);
520 if (true != dbus_message_append_args(msg,
521 DBUS_TYPE_STRING, &temp_command,
522 DBUS_TYPE_INVALID)) {
523 dbus_message_unref(msg);
524 MAS_LOGE("[ERROR] Fail to append args");
530 dbus_message_set_no_reply(msg, TRUE);
532 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
533 MAS_LOGE("[Dbus ERROR] Fail to Send");
536 return -1; // MAS_ERROR_OPERATION_FAILED;
538 MAS_LOGI("[Dbus DEBUG] Success to Send wakeup_engine_command : %d %s", pid, command);
539 dbus_connection_flush(mConnectionSender);
542 dbus_message_unref(msg);
550 int CServiceIpcDbus::change_service_state(int pid, int state)
552 if (0 != __dbus_check()) {
553 return -1; //MAS_ERROR_OPERATION_FAILED;
559 dbus_error_init(&err);
561 char service_name[64];
562 memset(service_name, '\0', 64);
563 snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
565 msg = dbus_message_new_method_call(
567 MA_CLIENT_SERVICE_OBJECT_PATH,
568 MA_CLIENT_SERVICE_INTERFACE,
569 MAS_METHOD_SERVICE_STATE_CHANGE);
572 MAS_LOGE(">>>> Request mas send service state message : Fail to make message");
573 return -1; // MAS_ERROR_OPERATION_FAILED;
575 MAS_LOGD(">>>> Request mas send service state message : %s", service_name);
578 if (true != dbus_message_append_args(msg,
579 DBUS_TYPE_INT32, &state,
580 DBUS_TYPE_INVALID)) {
581 dbus_message_unref(msg);
582 MAS_LOGE("[ERROR] Fail to append args");
586 dbus_message_set_no_reply(msg, TRUE);
588 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
589 MAS_LOGE("[Dbus ERROR] Fail to Send");
590 return -1; // MAS_ERROR_OPERATION_FAILED;
592 MAS_LOGI("[Dbus DEBUG] Success to Send service state message : %d %d", pid, state);
593 dbus_connection_flush(mConnectionSender);
596 dbus_message_unref(msg);
600 int CServiceIpcDbus::change_voice_key_status(int pid, int status)
602 if (0 != __dbus_check()) {
603 return -1; //MAS_ERROR_OPERATION_FAILED;
609 dbus_error_init(&err);
611 char service_name[64];
612 memset(service_name, '\0', 64);
613 snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
615 msg = dbus_message_new_method_call(
617 MA_CLIENT_SERVICE_OBJECT_PATH,
618 MA_CLIENT_SERVICE_INTERFACE,
619 MAS_METHOD_VOICE_KEY_STATUS_CHANGE);
622 MAS_LOGE(">>>> Request mas send voice key status change message : Fail to make message");
623 return -1; // MAS_ERROR_OPERATION_FAILED;
625 MAS_LOGD(">>>> Request mas send voice key status change message : %s", service_name);
628 if (true != dbus_message_append_args(msg,
629 DBUS_TYPE_INT32, &status,
630 DBUS_TYPE_INVALID)) {
631 dbus_message_unref(msg);
632 MAS_LOGE("[ERROR] Fail to append args");
636 dbus_message_set_no_reply(msg, TRUE);
638 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
639 MAS_LOGE("[Dbus ERROR] Fail to Send");
640 return -1; // MAS_ERROR_OPERATION_FAILED;
642 MAS_LOGI("[Dbus DEBUG] Success to Send voice key status change : %d %d", pid, status);
643 dbus_connection_flush(mConnectionSender);
646 dbus_message_unref(msg);
650 int CServiceIpcDbus::masc_ui_dbus_send_hello(void)
652 if (0 != __dbus_check()) {
653 return -1; //MAS_ERROR_OPERATION_FAILED;
659 dbus_error_init(&err);
661 msg = dbus_message_new_method_call(
662 MA_UI_CLIENT_SERVICE_NAME,
663 MA_UI_CLIENT_SERVICE_OBJECT_PATH,
664 MA_UI_CLIENT_SERVICE_INTERFACE,
668 MAS_LOGE("[DBus ERROR] Request masc hello : Fail to make message");
672 DBusMessage* result_msg = NULL;
675 result_msg = dbus_connection_send_with_reply_and_block(mConnectionSender, msg, 500, &err);
677 if (dbus_error_is_set(&err)) {
678 MAS_LOGE("[Dbus ERROR] Dbus Error (%s)", err.message);
679 dbus_error_free(&err);
682 dbus_message_unref(msg);
684 if (NULL != result_msg) {
685 dbus_message_unref(result_msg);
688 result = -1; //ERROR_TIMED_OUT;
694 int CServiceIpcDbus::masc_ui_dbus_send_asr_result(int pid, int event, const char* asr_result)
696 if (0 != __dbus_check()) {
697 return -1; //MAS_ERROR_OPERATION_FAILED;
702 msg = dbus_message_new_method_call(
703 MA_UI_CLIENT_SERVICE_NAME,
704 MA_UI_CLIENT_SERVICE_OBJECT_PATH,
705 MA_UI_CLIENT_SERVICE_INTERFACE,
706 MAS_UI_METHOD_SEND_ASR_RESULT);
709 MAS_LOGE("@@ Request multi-assistant send ASR result : Fail to make message");
710 return -1; //MA_ERROR_OPERATION_FAILED;
712 MAS_LOGD("[DEBUG] multi-assistant send ASR result, asr_result(%p)", asr_result);
715 char* temp_asr_result = NULL;
717 temp_asr_result = strdup("#NULL");
719 temp_asr_result = strdup(asr_result);
721 dbus_message_append_args(msg,
722 DBUS_TYPE_INT32, &pid,
723 DBUS_TYPE_INT32, &event,
724 DBUS_TYPE_STRING, &temp_asr_result,
727 dbus_message_set_no_reply(msg, TRUE);
729 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
730 MAS_LOGE("[Dbus ERROR] Fail to Send");
731 if (NULL != temp_asr_result) {
732 free(temp_asr_result);
733 temp_asr_result = NULL;
735 return -1; // MAS_ERROR_OPERATION_FAILED;
737 MAS_LOGD("[Dbus DEBUG] Success to Send ASR result");
738 dbus_connection_flush(mConnectionSender);
741 dbus_message_unref(msg);
744 free(temp_asr_result);
748 int CServiceIpcDbus::masc_ui_dbus_send_result(int pid, const char* display_text, const char* utterance_text, const char* result_json)
750 if (0 != __dbus_check()) {
751 return -1; //MA_ERROR_OPERATION_FAILED;
756 msg = dbus_message_new_method_call(
757 MA_UI_CLIENT_SERVICE_NAME,
758 MA_UI_CLIENT_SERVICE_OBJECT_PATH,
759 MA_UI_CLIENT_SERVICE_INTERFACE,
760 MAS_UI_METHOD_SEND_RESULT);
763 MAS_LOGE("@@ Request multi-assistant send result : Fail to make message");
764 return -1; //MA_ERROR_OPERATION_FAILED;
766 MAS_LOGD("[DEBUG] multi-assistant send result");
768 char* temp_display_text = NULL;
769 char* temp_utterance_text = NULL;
770 char* temp_result_json = NULL;
773 dbus_message_append_args(msg,
774 DBUS_TYPE_INT32, &pid,
775 DBUS_TYPE_STRING, &display_text,
776 DBUS_TYPE_STRING, &utterance_text,
777 DBUS_TYPE_STRING, &result_json,
780 dbus_message_set_no_reply(msg, TRUE);
782 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
783 MAS_LOGE("[Dbus ERROR] Fail to Send");
784 return -1; //MA_ERROR_OPERATION_FAILED;
786 MAS_LOGD("[Dbus DEBUG] Success to Send result");
787 dbus_connection_flush(mConnectionSender);
790 dbus_message_unref(msg);
793 temp_display_text = strdup("#NULL");
795 temp_display_text = strdup(display_text);
797 temp_utterance_text = strdup("#NULL");
799 temp_utterance_text = strdup(utterance_text);
801 temp_result_json = strdup("#NULL");
803 temp_result_json = strdup(result_json);
805 dbus_message_append_args(msg,
806 DBUS_TYPE_INT32, &pid,
807 DBUS_TYPE_STRING, &temp_display_text,
808 DBUS_TYPE_STRING, &temp_utterance_text,
809 DBUS_TYPE_STRING, &temp_result_json,
812 dbus_message_set_no_reply(msg, TRUE);
814 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
815 MAS_LOGE("[Dbus ERROR] Fail to Send");
816 if (temp_display_text)
817 free(temp_display_text);
818 if (temp_utterance_text)
819 free(temp_utterance_text);
820 if (temp_result_json)
821 free(temp_result_json);
822 return -1; //MA_ERROR_OPERATION_FAILED;
824 MAS_LOGD("[Dbus DEBUG] Success to Send result");
825 dbus_connection_flush(mConnectionSender);
828 dbus_message_unref(msg);
830 if (temp_display_text)
831 free(temp_display_text);
832 if (temp_utterance_text)
833 free(temp_utterance_text);
834 if (temp_result_json)
835 free(temp_result_json);
840 int CServiceIpcDbus::masc_ui_dbus_change_assistant(const char* app_id)
842 if (0 != __dbus_check()) {
843 return -1; //MAS_ERROR_OPERATION_FAILED;
846 if (NULL == app_id) {
847 MAS_LOGE("@@ Request multi-assistant send change assistant request : Fail to make message");
848 return -1; //MA_ERROR_OPERATION_FAILED;
850 MAS_LOGD("[DEBUG] multi-assistant send change assistant request app_id(%s)", app_id);
855 msg = dbus_message_new_method_call(
856 MA_UI_CLIENT_SERVICE_NAME,
857 MA_UI_CLIENT_SERVICE_OBJECT_PATH,
858 MA_UI_CLIENT_SERVICE_INTERFACE,
859 MAS_UI_METHOD_CHANGE_ASSISTANT);
861 dbus_message_append_args(msg,
862 DBUS_TYPE_STRING, &app_id,
865 dbus_message_set_no_reply(msg, TRUE);
867 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
868 MAS_LOGE("[Dbus ERROR] Fail to Send");
869 return -1; // MAS_ERROR_OPERATION_FAILED;
871 MAS_LOGD("[Dbus DEBUG] Success to Send change assistant request");
872 dbus_connection_flush(mConnectionSender);
875 dbus_message_unref(msg);
880 int CServiceIpcDbus::masc_ui_dbus_send_error_message(int reason, const char* err_msg)
882 if (NULL == mConnectionSender) {
883 MAS_LOGE("[Dbus ERROR] Dbus connection is not available");
887 DBusMessage* msg = NULL;
889 /* create a message */
890 msg = dbus_message_new_signal(
891 MA_UI_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
892 MA_UI_CLIENT_SERVICE_INTERFACE, /* interface name of the signal */
893 MAS_UI_METHOD_ERROR); /* name of the signal */
896 MAS_LOGE("[Dbus ERROR] Fail to create error message");
900 char* temp_err_msg = NULL;
903 temp_err_msg = strdup(err_msg);
905 temp_err_msg = strdup("#NULL");
907 dbus_message_append_args(msg,
908 DBUS_TYPE_INT32, &reason,
909 DBUS_TYPE_STRING, &temp_err_msg,
912 dbus_message_set_no_reply(msg, TRUE);
914 if (!dbus_connection_send(mConnectionSender, msg, NULL)) {
915 MAS_LOGE("[Dbus ERROR] <<<< error message : Out Of Memory !");
917 MAS_LOGD("<<<< Send error message : reason(%d), err_msg(%s)", reason, temp_err_msg);
918 dbus_connection_flush(mConnectionSender);
921 dbus_message_unref(msg);
928 int CServiceIpcDbus::masc_ui_dbus_send_recognition_result(int pid, int result)
930 if (0 != __dbus_check()) {
931 return -1; //MAS_ERROR_OPERATION_FAILED;
936 msg = dbus_message_new_method_call(
937 MA_UI_CLIENT_SERVICE_NAME,
938 MA_UI_CLIENT_SERVICE_OBJECT_PATH,
939 MA_UI_CLIENT_SERVICE_INTERFACE,
940 MAS_UI_METHOD_SEND_RECOGNITION_RESULT);
943 MAS_LOGE("@@ Request multi-assistant send recognition result : Fail to make message");
944 return -1; //MA_ERROR_OPERATION_FAILED;
946 MAS_LOGD("[DEBUG] multi-assistant send recognition result(%d)", result);
949 dbus_message_append_args(msg,
950 DBUS_TYPE_INT32, &pid,
951 DBUS_TYPE_INT32, &result,
954 dbus_message_set_no_reply(msg, TRUE);
956 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
957 MAS_LOGE("[Dbus ERROR] Fail to Send");
958 return -1; // MAS_ERROR_OPERATION_FAILED;
960 MAS_LOGD("[Dbus DEBUG] Success to Send ASR result");
961 dbus_connection_flush(mConnectionSender);
964 dbus_message_unref(msg);
969 int CServiceIpcDbus::masc_ui_dbus_enable_common_ui(int enable)
971 if (0 != __dbus_check()) {
972 return -1; //MAS_ERROR_OPERATION_FAILED;
977 msg = dbus_message_new_method_call(
978 MA_UI_CLIENT_SERVICE_NAME,
979 MA_UI_CLIENT_SERVICE_OBJECT_PATH,
980 MA_UI_CLIENT_SERVICE_INTERFACE,
981 MAS_UI_METHOD_ENABLE_COMMON_UI);
984 MAS_LOGE("@@ Request multi-assistant enable common ui : Fail to make message");
985 return -1; //MA_ERROR_OPERATION_FAILED;
987 MAS_LOGD("[DEBUG] multi-assistant enable common ui (%d)", enable);
990 dbus_message_append_args(msg,
991 DBUS_TYPE_INT32, &enable,
994 dbus_message_set_no_reply(msg, TRUE);
996 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
997 MAS_LOGE("[Dbus ERROR] Fail to Send");
998 return -1; // MAS_ERROR_OPERATION_FAILED;
1000 MAS_LOGD("[Dbus DEBUG] Success to Send ASR result");
1001 dbus_connection_flush(mConnectionSender);
1004 dbus_message_unref(msg);
1009 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
1011 CServiceIpcDbus* service_ipc = static_cast<CServiceIpcDbus*>(data);
1012 if (NULL == service_ipc) {
1013 MAS_LOGE("Error : service_ipc NULL");
1014 return ECORE_CALLBACK_RENEW;
1017 DBusConnection* mConnectionListener = service_ipc->get_connection_listener();
1018 if (NULL == mConnectionListener) {
1019 MAS_LOGE("Error : mConnectionListener NULL");
1020 return ECORE_CALLBACK_RENEW;
1023 CServiceIpcDbusDispatcher* dispatcher = service_ipc->get_dispatcher();
1024 if (NULL == dispatcher) {
1025 MAS_LOGE("Error : dispatcher NULL");
1026 return ECORE_CALLBACK_RENEW;
1029 dbus_connection_read_write_dispatch(mConnectionListener, 50);
1032 DBusMessage* msg = NULL;
1033 msg = dbus_connection_pop_message(mConnectionListener);
1035 if (true != dbus_connection_get_is_connected(mConnectionListener)) {
1036 MAS_LOGE("[ERROR] Connection is disconnected");
1037 return ECORE_CALLBACK_RENEW;
1040 /* loop again if we haven't read a message */
1042 return ECORE_CALLBACK_RENEW;
1046 if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_HELLO)) {
1047 dispatcher->on_hello(mConnectionListener, msg);
1049 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_INITIALIZE)) {
1050 dispatcher->on_initialize(mConnectionListener, msg);
1052 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_DEINITIALIZE)) {
1053 dispatcher->on_deinitialize(mConnectionListener, msg);
1055 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_GET_RECORDING_AUDIO_FORMAT)) {
1056 dispatcher->on_get_audio_format(mConnectionListener, msg);
1058 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_GET_RECORDING_AUDIO_SOURCE_TYPE)) {
1059 dispatcher->on_get_audio_source_type(mConnectionListener, msg);
1061 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_ASR_RESULT)) {
1062 dispatcher->on_send_asr_result(mConnectionListener, msg);
1064 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_RESULT)) {
1065 dispatcher->on_send_result(mConnectionListener, msg);
1067 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_RECOGNITION_RESULT)) {
1068 dispatcher->on_send_recognition_result(mConnectionListener, msg);
1070 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_START_STREAMING_AUDIO_DATA)) {
1071 dispatcher->on_start_streaming_audio_data(mConnectionListener, msg);
1073 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_STOP_STREAMING_AUDIO_DATA)) {
1074 dispatcher->on_stop_streaming_audio_data(mConnectionListener, msg);
1076 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_UPDATE_VOICE_FEEDBACK_STATE)) {
1077 dispatcher->on_update_voice_feedback_state(mConnectionListener, msg);
1079 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_ASSISTANT_SPECIFIC_COMMAND)) {
1080 dispatcher->on_send_assistant_specific_command(mConnectionListener, msg);
1082 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SET_BACKGROUND_VOLUME)) {
1083 dispatcher->on_set_background_volume(mConnectionListener, msg);
1085 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SET_PREPROCESSING_ALLOW_MODE)) {
1086 dispatcher->on_set_preprocessing_allow_mode(mConnectionListener, msg);
1088 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_PREPROCESSING_RESULT)) {
1089 dispatcher->on_send_preprocessing_result(mConnectionListener, msg);
1091 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SET_WAKE_WORD_AUDIO_REQUIRE_FLAG)) {
1092 dispatcher->on_set_wake_word_audio_require_flag(mConnectionListener, msg);
1094 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SET_ASSISTANT_WAKEUP_LANGUAGE)) {
1095 dispatcher->on_set_assistant_language(mConnectionListener, msg);
1097 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_ADD_WAKE_WORD)) {
1098 dispatcher->on_add_wake_word(mConnectionListener, msg);
1100 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_REMOVE_WAKE_WORD)) {
1101 dispatcher->on_remove_wake_word(mConnectionListener, msg);
1103 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_UI_METHOD_INITIALIZE)) {
1104 dispatcher->on_ui_initialize(mConnectionListener, msg);
1106 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_UI_METHOD_DEINITIALIZE)) {
1107 dispatcher->on_ui_deinitialize(mConnectionListener, msg);
1109 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_UI_METHOD_CHANGE_ASSISTANT)) {
1110 dispatcher->on_ui_change_assistant(mConnectionListener, msg);
1113 MAS_LOGD("Message is NOT valid");
1114 /* Invalid method */
1116 /* free the message */
1117 dbus_message_unref(msg);
1120 return ECORE_CALLBACK_RENEW;
1123 void CServiceIpcDbus::connection_free()
1125 if (NULL != mConnectionListener) {
1126 dbus_connection_close(mConnectionListener);
1127 dbus_connection_unref(mConnectionListener);
1128 mConnectionListener = NULL;
1130 if (NULL != mConnectionSender) {
1131 dbus_connection_close(mConnectionSender);
1132 dbus_connection_unref(mConnectionSender);
1133 mConnectionSender = NULL;
1137 int CServiceIpcDbus::open_connection()
1140 dbus_error_init(&err);
1144 /* Create connection for sender */
1145 mConnectionSender = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
1147 if (dbus_error_is_set(&err)) {
1148 MAS_LOGE("[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
1149 dbus_error_free(&err);
1152 if (NULL == mConnectionSender) {
1153 MAS_LOGE("[Dbus ERROR] Fail to get dbus connection");
1157 dbus_connection_set_exit_on_disconnect(mConnectionSender, false);
1159 /* connect to the bus and check for errors */
1160 mConnectionListener = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
1162 if (dbus_error_is_set(&err)) {
1163 MAS_LOGE("[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
1164 dbus_error_free(&err);
1167 if (NULL == mConnectionListener) {
1168 MAS_LOGE("[Dbus ERROR] Fail to get dbus connection");
1173 dbus_connection_set_exit_on_disconnect(mConnectionListener, false);
1175 /* request our name on the bus and check for errors */
1176 ret = dbus_bus_request_name(mConnectionListener, MA_SERVER_SERVICE_NAME, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
1178 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
1179 printf("Fail to be primary owner in dbus request.");
1180 MAS_LOGE("[Dbus ERROR] Fail to be primary owner");
1185 if (dbus_error_is_set(&err)) {
1186 MAS_LOGE("[Dbus ERROR] dbus_bus_request_name() : %s", err.message);
1187 dbus_error_free(&err);
1192 /* Flush messages which are received before fd event handler registration */
1193 while (DBUS_DISPATCH_DATA_REMAINS == dbus_connection_get_dispatch_status(mConnectionListener)) {
1194 listener_event_callback(this, NULL);
1197 /* add a rule for getting signal */
1199 snprintf(rule, 128, "type='signal',interface='%s'", MA_SERVER_SERVICE_INTERFACE);
1201 /* add a rule for which messages we want to see */
1202 dbus_bus_add_match(mConnectionListener, rule, &err);/* see signals from the given interface */
1204 if (dbus_error_is_set(&err)) {
1205 MAS_LOGE("[Dbus ERROR] dbus_bus_add_match() : %s", err.message);
1206 dbus_error_free(&err);
1212 if (1 != dbus_connection_get_unix_fd(mConnectionListener, &fd)) {
1213 MAS_LOGE("fail to get fd from dbus ");
1217 MAS_LOGD("Get fd from dbus : %d", fd);
1220 mFdHandler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)listener_event_callback, this, NULL, NULL);
1222 if (NULL == mFdHandler) {
1223 MAS_LOGE("[Dbus ERROR] Fail to get fd handler");
1231 int CServiceIpcDbus::close_connection()
1234 dbus_error_init(&err);
1236 if (NULL != mFdHandler) {
1237 ecore_main_fd_handler_del(mFdHandler);
1241 dbus_bus_release_name(mConnectionListener, MA_SERVER_SERVICE_NAME, &err);
1243 if (dbus_error_is_set(&err)) {
1244 MAS_LOGE("[Dbus ERROR] dbus_bus_release_name() : %s", err.message);
1245 dbus_error_free(&err);