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 boost::optional<std::string> appid = mApplicationManager->get_appid_by_pid(pid);
249 int ret = message_port_send_message((*appid).c_str(), message_port, b);
250 if (MESSAGE_PORT_ERROR_NONE != ret)
251 masc_message_port_error(ret);
253 MAS_LOGE("AppID for PID %d not found!!!", pid);
256 pending_buffer_size = 0;
259 MAS_LOGE("Bundle creation failed!!!");
262 if (-1 != last_serial_waiting_for_flush) {
263 MAS_LOGI("flushing streaming data, serial : %d", last_serial_waiting_for_flush);
264 last_serial_waiting_for_flush = -1;
268 if (MAS_SPEECH_STREAMING_EVENT_FINISH == event) {
269 bundle *b = bundle_create();
271 bundle_add_byte(b, "content", buffer, total_size);
272 boost::optional<std::string> appid = mApplicationManager->get_appid_by_pid(pid);
274 int ret = message_port_send_message((*appid).c_str(), message_port, b);
275 if (MESSAGE_PORT_ERROR_NONE != ret)
276 masc_message_port_error(ret);
278 MAS_LOGE("AppID for PID %d not found!!!", pid);
283 MAS_LOGE("Bundle creation failed!!!");
286 memcpy(pending_buffer + pending_buffer_size, buffer, total_size);
287 pending_buffer_size += total_size;
292 int CServiceIpcDbus::change_active_state(int pid, int state)
294 if (0 != __dbus_check()) {
295 return -1; //MAS_ERROR_OPERATION_FAILED;
301 dbus_error_init(&err);
303 char service_name[64];
304 memset(service_name, '\0', 64);
305 snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
307 msg = dbus_message_new_method_call(
309 MA_CLIENT_SERVICE_OBJECT_PATH,
310 MA_CLIENT_SERVICE_INTERFACE,
311 MAS_METHOD_ACTIVE_STATE_CHANGE);
314 MAS_LOGE(">>>> Request mas send activate message : Fail to make message");
315 return -1; // MAS_ERROR_OPERATION_FAILED;
317 MAS_LOGD(">>>> Request mas send activate message : %s", service_name);
320 if (true != dbus_message_append_args(msg,
321 DBUS_TYPE_INT32, &state,
322 DBUS_TYPE_INVALID)) {
323 dbus_message_unref(msg);
324 MAS_LOGE("[ERROR] Fail to append args");
328 dbus_message_set_no_reply(msg, TRUE);
330 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
331 MAS_LOGE("[Dbus ERROR] Fail to Send");
332 return -1; // MAS_ERROR_OPERATION_FAILED;
334 MAS_LOGI("[Dbus DEBUG] Success to Send activate message : %d %d", pid, state);
335 dbus_connection_flush(mConnectionSender);
338 dbus_message_unref(msg);
342 int CServiceIpcDbus::send_preprocessing_information(int pid, const char* app_id)
344 if (0 != __dbus_check()) {
345 return -1; //MAS_ERROR_OPERATION_FAILED;
351 dbus_error_init(&err);
353 char service_name[64];
354 memset(service_name, '\0', 64);
355 snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
357 msg = dbus_message_new_method_call(
359 MA_CLIENT_SERVICE_OBJECT_PATH,
360 MA_CLIENT_SERVICE_INTERFACE,
361 MAS_METHOD_SEND_PREPROCESSING_INFORMATION);
364 MAS_LOGE(">>>> Request mas send preprocessing assistant information : Fail to make message");
365 return -1; // MAS_ERROR_OPERATION_FAILED;
367 MAS_LOGD(">>>> Request mas send preprocessing assistant information : %s", service_name);
370 char* temp_app_id = NULL;
372 temp_app_id = strdup("#NULL");
374 temp_app_id = strdup(app_id);
376 if (true != dbus_message_append_args(msg,
377 DBUS_TYPE_STRING, &temp_app_id,
378 DBUS_TYPE_INVALID)) {
379 dbus_message_unref(msg);
380 MAS_LOGE("[ERROR] Fail to append args");
386 dbus_message_set_no_reply(msg, TRUE);
388 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
389 MAS_LOGE("[Dbus ERROR] Fail to Send");
392 return -1; // MAS_ERROR_OPERATION_FAILED;
394 MAS_LOGI("[Dbus DEBUG] Success to Send preprocessing assistant information : %d %s", pid, app_id);
395 dbus_connection_flush(mConnectionSender);
398 dbus_message_unref(msg);
406 int CServiceIpcDbus::send_streaming_section_changed(int pid, int section)
408 if (nullptr == mClientManager) {
409 MAS_LOGE("mClientManager variable is NULL");
413 bundle *b = bundle_create();
415 streaming_data_header header;
416 header.streaming_data_type = 1;
417 header.streaming_data_size = sizeof(streaming_data_header) + sizeof(streaming_data_streaming_section_header);
418 header.streaming_data_serial = mStreamingDataSerial++;
420 streaming_data_streaming_section_header streaming_section_header;
421 streaming_section_header.section = section;
423 unsigned char buffer[STREAMING_BUFFER_SIZE];
424 size_t total_size = 0;
425 memcpy(buffer, &header, sizeof(header));
426 total_size += sizeof(header);
427 memcpy(buffer + total_size, &streaming_section_header, sizeof(streaming_section_header));
428 total_size += sizeof(streaming_section_header);
430 bundle_add_byte(b, "content", buffer, total_size);
431 int ret = message_port_send_message(
432 mClientManager->find_client_appid_by_pid(pid).c_str(), message_port, b);
433 if (MESSAGE_PORT_ERROR_NONE != ret)
434 masc_message_port_error(ret);
441 int CServiceIpcDbus::send_preprocessing_result(int pid, bool result)
443 if (0 != __dbus_check()) {
444 return -1; //MAS_ERROR_OPERATION_FAILED;
450 dbus_error_init(&err);
452 char service_name[64];
453 memset(service_name, '\0', 64);
454 snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
456 msg = dbus_message_new_method_call(
458 MA_CLIENT_SERVICE_OBJECT_PATH,
459 MA_CLIENT_SERVICE_INTERFACE,
460 MAS_METHOD_SEND_PREPROCESSING_RESULT);
463 MAS_LOGE(">>>> Request mas send preprocessing result : Fail to make message");
464 return -1; // MAS_ERROR_OPERATION_FAILED;
466 MAS_LOGD(">>>> Request mas send preprocessing result : %s", service_name);
469 int temp_result = result;
471 if (true != dbus_message_append_args(msg,
472 DBUS_TYPE_INT32, &temp_result,
473 DBUS_TYPE_INVALID)) {
474 dbus_message_unref(msg);
475 MAS_LOGE("[ERROR] Fail to append args");
479 dbus_message_set_no_reply(msg, TRUE);
481 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
482 MAS_LOGE("[Dbus ERROR] Fail to Send");
483 return -1; // MAS_ERROR_OPERATION_FAILED;
485 MAS_LOGI("[Dbus DEBUG] Success to Send preprocessing result : %d %d", pid, temp_result);
486 dbus_connection_flush(mConnectionSender);
489 dbus_message_unref(msg);
494 int CServiceIpcDbus::send_wakeup_engine_command(int pid, const char* command)
496 if (0 != __dbus_check()) {
497 return -1; //MAS_ERROR_OPERATION_FAILED;
503 dbus_error_init(&err);
505 char service_name[64];
506 memset(service_name, '\0', 64);
507 snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
509 msg = dbus_message_new_method_call(
511 MA_CLIENT_SERVICE_OBJECT_PATH,
512 MA_CLIENT_SERVICE_INTERFACE,
513 MAS_METHOD_SEND_WAKEUP_ENGINE_COMMAND);
516 MAS_LOGE(">>>> Request mas send wakeup engine command : Fail to make message");
517 return -1; // MAS_ERROR_OPERATION_FAILED;
519 MAS_LOGD(">>>> Request mas send wakeup engine command : %s", service_name);
522 char* temp_command = NULL;
524 temp_command = strdup("#NULL");
526 temp_command = strdup(command);
528 if (true != dbus_message_append_args(msg,
529 DBUS_TYPE_STRING, &temp_command,
530 DBUS_TYPE_INVALID)) {
531 dbus_message_unref(msg);
532 MAS_LOGE("[ERROR] Fail to append args");
538 dbus_message_set_no_reply(msg, TRUE);
540 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
541 MAS_LOGE("[Dbus ERROR] Fail to Send");
544 return -1; // MAS_ERROR_OPERATION_FAILED;
546 MAS_LOGI("[Dbus DEBUG] Success to Send wakeup_engine_command : %d %s", pid, command);
547 dbus_connection_flush(mConnectionSender);
550 dbus_message_unref(msg);
558 int CServiceIpcDbus::change_service_state(int pid, int state)
560 if (0 != __dbus_check()) {
561 return -1; //MAS_ERROR_OPERATION_FAILED;
567 dbus_error_init(&err);
569 char service_name[64];
570 memset(service_name, '\0', 64);
571 snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
573 msg = dbus_message_new_method_call(
575 MA_CLIENT_SERVICE_OBJECT_PATH,
576 MA_CLIENT_SERVICE_INTERFACE,
577 MAS_METHOD_SERVICE_STATE_CHANGE);
580 MAS_LOGE(">>>> Request mas send service state message : Fail to make message");
581 return -1; // MAS_ERROR_OPERATION_FAILED;
583 MAS_LOGD(">>>> Request mas send service state message : %s", service_name);
586 if (true != dbus_message_append_args(msg,
587 DBUS_TYPE_INT32, &state,
588 DBUS_TYPE_INVALID)) {
589 dbus_message_unref(msg);
590 MAS_LOGE("[ERROR] Fail to append args");
594 dbus_message_set_no_reply(msg, TRUE);
596 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
597 MAS_LOGE("[Dbus ERROR] Fail to Send");
598 return -1; // MAS_ERROR_OPERATION_FAILED;
600 MAS_LOGI("[Dbus DEBUG] Success to Send service state message : %d %d", pid, state);
601 dbus_connection_flush(mConnectionSender);
604 dbus_message_unref(msg);
608 int CServiceIpcDbus::change_voice_key_status(int pid, int status)
610 if (0 != __dbus_check()) {
611 return -1; //MAS_ERROR_OPERATION_FAILED;
617 dbus_error_init(&err);
619 char service_name[64];
620 memset(service_name, '\0', 64);
621 snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
623 msg = dbus_message_new_method_call(
625 MA_CLIENT_SERVICE_OBJECT_PATH,
626 MA_CLIENT_SERVICE_INTERFACE,
627 MAS_METHOD_VOICE_KEY_STATUS_CHANGE);
630 MAS_LOGE(">>>> Request mas send voice key status change message : Fail to make message");
631 return -1; // MAS_ERROR_OPERATION_FAILED;
633 MAS_LOGD(">>>> Request mas send voice key status change message : %s", service_name);
636 if (true != dbus_message_append_args(msg,
637 DBUS_TYPE_INT32, &status,
638 DBUS_TYPE_INVALID)) {
639 dbus_message_unref(msg);
640 MAS_LOGE("[ERROR] Fail to append args");
644 dbus_message_set_no_reply(msg, TRUE);
646 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
647 MAS_LOGE("[Dbus ERROR] Fail to Send");
648 return -1; // MAS_ERROR_OPERATION_FAILED;
650 MAS_LOGI("[Dbus DEBUG] Success to Send voice key status change : %d %d", pid, status);
651 dbus_connection_flush(mConnectionSender);
654 dbus_message_unref(msg);
658 int CServiceIpcDbus::masc_ui_dbus_send_hello(void)
660 if (0 != __dbus_check()) {
661 return -1; //MAS_ERROR_OPERATION_FAILED;
667 dbus_error_init(&err);
669 msg = dbus_message_new_method_call(
670 MA_UI_CLIENT_SERVICE_NAME,
671 MA_UI_CLIENT_SERVICE_OBJECT_PATH,
672 MA_UI_CLIENT_SERVICE_INTERFACE,
676 MAS_LOGE("[DBus ERROR] Request masc hello : Fail to make message");
680 DBusMessage* result_msg = NULL;
683 result_msg = dbus_connection_send_with_reply_and_block(mConnectionSender, msg, 500, &err);
685 if (dbus_error_is_set(&err)) {
686 MAS_LOGE("[Dbus ERROR] Dbus Error (%s)", err.message);
687 dbus_error_free(&err);
690 dbus_message_unref(msg);
692 if (NULL != result_msg) {
693 dbus_message_unref(result_msg);
696 result = -1; //ERROR_TIMED_OUT;
702 int CServiceIpcDbus::masc_ui_dbus_send_asr_result(int pid, int event, const char* asr_result)
704 if (0 != __dbus_check()) {
705 return -1; //MAS_ERROR_OPERATION_FAILED;
710 msg = dbus_message_new_method_call(
711 MA_UI_CLIENT_SERVICE_NAME,
712 MA_UI_CLIENT_SERVICE_OBJECT_PATH,
713 MA_UI_CLIENT_SERVICE_INTERFACE,
714 MAS_UI_METHOD_SEND_ASR_RESULT);
717 MAS_LOGE("@@ Request multi-assistant send ASR result : Fail to make message");
718 return -1; //MA_ERROR_OPERATION_FAILED;
720 MAS_LOGD("[DEBUG] multi-assistant send ASR result, asr_result(%p)", asr_result);
723 char* temp_asr_result = NULL;
725 temp_asr_result = strdup("#NULL");
727 temp_asr_result = strdup(asr_result);
729 dbus_message_append_args(msg,
730 DBUS_TYPE_INT32, &pid,
731 DBUS_TYPE_INT32, &event,
732 DBUS_TYPE_STRING, &temp_asr_result,
735 dbus_message_set_no_reply(msg, TRUE);
737 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
738 MAS_LOGE("[Dbus ERROR] Fail to Send");
739 if (NULL != temp_asr_result) {
740 free(temp_asr_result);
741 temp_asr_result = NULL;
743 return -1; // MAS_ERROR_OPERATION_FAILED;
745 MAS_LOGD("[Dbus DEBUG] Success to Send ASR result");
746 dbus_connection_flush(mConnectionSender);
749 dbus_message_unref(msg);
752 free(temp_asr_result);
756 int CServiceIpcDbus::masc_ui_dbus_send_result(int pid, const char* display_text, const char* utterance_text, const char* result_json)
758 if (0 != __dbus_check()) {
759 return -1; //MA_ERROR_OPERATION_FAILED;
764 msg = dbus_message_new_method_call(
765 MA_UI_CLIENT_SERVICE_NAME,
766 MA_UI_CLIENT_SERVICE_OBJECT_PATH,
767 MA_UI_CLIENT_SERVICE_INTERFACE,
768 MAS_UI_METHOD_SEND_RESULT);
771 MAS_LOGE("@@ Request multi-assistant send result : Fail to make message");
772 return -1; //MA_ERROR_OPERATION_FAILED;
774 MAS_LOGD("[DEBUG] multi-assistant send result");
776 char* temp_display_text = NULL;
777 char* temp_utterance_text = NULL;
778 char* temp_result_json = NULL;
781 dbus_message_append_args(msg,
782 DBUS_TYPE_INT32, &pid,
783 DBUS_TYPE_STRING, &display_text,
784 DBUS_TYPE_STRING, &utterance_text,
785 DBUS_TYPE_STRING, &result_json,
788 dbus_message_set_no_reply(msg, TRUE);
790 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
791 MAS_LOGE("[Dbus ERROR] Fail to Send");
792 return -1; //MA_ERROR_OPERATION_FAILED;
794 MAS_LOGD("[Dbus DEBUG] Success to Send result");
795 dbus_connection_flush(mConnectionSender);
798 dbus_message_unref(msg);
801 temp_display_text = strdup("#NULL");
803 temp_display_text = strdup(display_text);
805 temp_utterance_text = strdup("#NULL");
807 temp_utterance_text = strdup(utterance_text);
809 temp_result_json = strdup("#NULL");
811 temp_result_json = strdup(result_json);
813 dbus_message_append_args(msg,
814 DBUS_TYPE_INT32, &pid,
815 DBUS_TYPE_STRING, &temp_display_text,
816 DBUS_TYPE_STRING, &temp_utterance_text,
817 DBUS_TYPE_STRING, &temp_result_json,
820 dbus_message_set_no_reply(msg, TRUE);
822 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
823 MAS_LOGE("[Dbus ERROR] Fail to Send");
824 if (temp_display_text)
825 free(temp_display_text);
826 if (temp_utterance_text)
827 free(temp_utterance_text);
828 if (temp_result_json)
829 free(temp_result_json);
830 return -1; //MA_ERROR_OPERATION_FAILED;
832 MAS_LOGD("[Dbus DEBUG] Success to Send result");
833 dbus_connection_flush(mConnectionSender);
836 dbus_message_unref(msg);
838 if (temp_display_text)
839 free(temp_display_text);
840 if (temp_utterance_text)
841 free(temp_utterance_text);
842 if (temp_result_json)
843 free(temp_result_json);
848 int CServiceIpcDbus::masc_ui_dbus_change_assistant(const char* app_id)
850 if (0 != __dbus_check()) {
851 return -1; //MAS_ERROR_OPERATION_FAILED;
854 if (NULL == app_id) {
855 MAS_LOGE("@@ Request multi-assistant send change assistant request : Fail to make message");
856 return -1; //MA_ERROR_OPERATION_FAILED;
858 MAS_LOGD("[DEBUG] multi-assistant send change assistant request app_id(%s)", app_id);
863 msg = dbus_message_new_method_call(
864 MA_UI_CLIENT_SERVICE_NAME,
865 MA_UI_CLIENT_SERVICE_OBJECT_PATH,
866 MA_UI_CLIENT_SERVICE_INTERFACE,
867 MAS_UI_METHOD_CHANGE_ASSISTANT);
869 dbus_message_append_args(msg,
870 DBUS_TYPE_STRING, &app_id,
873 dbus_message_set_no_reply(msg, TRUE);
875 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
876 MAS_LOGE("[Dbus ERROR] Fail to Send");
877 return -1; // MAS_ERROR_OPERATION_FAILED;
879 MAS_LOGD("[Dbus DEBUG] Success to Send change assistant request");
880 dbus_connection_flush(mConnectionSender);
883 dbus_message_unref(msg);
888 int CServiceIpcDbus::masc_ui_dbus_send_error_message(int reason, const char* err_msg)
890 if (NULL == mConnectionSender) {
891 MAS_LOGE("[Dbus ERROR] Dbus connection is not available");
895 DBusMessage* msg = NULL;
897 /* create a message */
898 msg = dbus_message_new_signal(
899 MA_UI_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
900 MA_UI_CLIENT_SERVICE_INTERFACE, /* interface name of the signal */
901 MAS_UI_METHOD_ERROR); /* name of the signal */
904 MAS_LOGE("[Dbus ERROR] Fail to create error message");
908 char* temp_err_msg = NULL;
911 temp_err_msg = strdup(err_msg);
913 temp_err_msg = strdup("#NULL");
915 dbus_message_append_args(msg,
916 DBUS_TYPE_INT32, &reason,
917 DBUS_TYPE_STRING, &temp_err_msg,
920 dbus_message_set_no_reply(msg, TRUE);
922 if (!dbus_connection_send(mConnectionSender, msg, NULL)) {
923 MAS_LOGE("[Dbus ERROR] <<<< error message : Out Of Memory !");
925 MAS_LOGD("<<<< Send error message : reason(%d), err_msg(%s)", reason, temp_err_msg);
926 dbus_connection_flush(mConnectionSender);
929 dbus_message_unref(msg);
936 int CServiceIpcDbus::masc_ui_dbus_send_recognition_result(int pid, int result)
938 if (0 != __dbus_check()) {
939 return -1; //MAS_ERROR_OPERATION_FAILED;
944 msg = dbus_message_new_method_call(
945 MA_UI_CLIENT_SERVICE_NAME,
946 MA_UI_CLIENT_SERVICE_OBJECT_PATH,
947 MA_UI_CLIENT_SERVICE_INTERFACE,
948 MAS_UI_METHOD_SEND_RECOGNITION_RESULT);
951 MAS_LOGE("@@ Request multi-assistant send recognition result : Fail to make message");
952 return -1; //MA_ERROR_OPERATION_FAILED;
954 MAS_LOGD("[DEBUG] multi-assistant send recognition result(%d)", result);
957 dbus_message_append_args(msg,
958 DBUS_TYPE_INT32, &pid,
959 DBUS_TYPE_INT32, &result,
962 dbus_message_set_no_reply(msg, TRUE);
964 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
965 MAS_LOGE("[Dbus ERROR] Fail to Send");
966 return -1; // MAS_ERROR_OPERATION_FAILED;
968 MAS_LOGD("[Dbus DEBUG] Success to Send ASR result");
969 dbus_connection_flush(mConnectionSender);
972 dbus_message_unref(msg);
977 int CServiceIpcDbus::masc_ui_dbus_enable_common_ui(int enable)
979 if (0 != __dbus_check()) {
980 return -1; //MAS_ERROR_OPERATION_FAILED;
985 msg = dbus_message_new_method_call(
986 MA_UI_CLIENT_SERVICE_NAME,
987 MA_UI_CLIENT_SERVICE_OBJECT_PATH,
988 MA_UI_CLIENT_SERVICE_INTERFACE,
989 MAS_UI_METHOD_ENABLE_COMMON_UI);
992 MAS_LOGE("@@ Request multi-assistant enable common ui : Fail to make message");
993 return -1; //MA_ERROR_OPERATION_FAILED;
995 MAS_LOGD("[DEBUG] multi-assistant enable common ui (%d)", enable);
998 dbus_message_append_args(msg,
999 DBUS_TYPE_INT32, &enable,
1002 dbus_message_set_no_reply(msg, TRUE);
1004 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
1005 MAS_LOGE("[Dbus ERROR] Fail to Send");
1006 return -1; // MAS_ERROR_OPERATION_FAILED;
1008 MAS_LOGD("[Dbus DEBUG] Success to Send ASR result");
1009 dbus_connection_flush(mConnectionSender);
1012 dbus_message_unref(msg);
1017 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
1019 CServiceIpcDbus* service_ipc = static_cast<CServiceIpcDbus*>(data);
1020 if (NULL == service_ipc) {
1021 MAS_LOGE("Error : service_ipc NULL");
1022 return ECORE_CALLBACK_RENEW;
1025 DBusConnection* mConnectionListener = service_ipc->get_connection_listener();
1026 if (NULL == mConnectionListener) {
1027 MAS_LOGE("Error : mConnectionListener NULL");
1028 return ECORE_CALLBACK_RENEW;
1031 CServiceIpcDbusDispatcher* dispatcher = service_ipc->get_dispatcher();
1032 if (NULL == dispatcher) {
1033 MAS_LOGE("Error : dispatcher NULL");
1034 return ECORE_CALLBACK_RENEW;
1037 dbus_connection_read_write_dispatch(mConnectionListener, 50);
1040 DBusMessage* msg = NULL;
1041 msg = dbus_connection_pop_message(mConnectionListener);
1043 if (true != dbus_connection_get_is_connected(mConnectionListener)) {
1044 MAS_LOGE("[ERROR] Connection is disconnected");
1045 return ECORE_CALLBACK_RENEW;
1048 /* loop again if we haven't read a message */
1050 return ECORE_CALLBACK_RENEW;
1054 if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_HELLO)) {
1055 dispatcher->on_hello(mConnectionListener, msg);
1057 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_INITIALIZE)) {
1058 dispatcher->on_initialize(mConnectionListener, msg);
1060 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_DEINITIALIZE)) {
1061 dispatcher->on_deinitialize(mConnectionListener, msg);
1063 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_GET_RECORDING_AUDIO_FORMAT)) {
1064 dispatcher->on_get_audio_format(mConnectionListener, msg);
1066 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_GET_RECORDING_AUDIO_SOURCE_TYPE)) {
1067 dispatcher->on_get_audio_source_type(mConnectionListener, msg);
1069 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_ASR_RESULT)) {
1070 dispatcher->on_send_asr_result(mConnectionListener, msg);
1072 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_RESULT)) {
1073 dispatcher->on_send_result(mConnectionListener, msg);
1075 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_RECOGNITION_RESULT)) {
1076 dispatcher->on_send_recognition_result(mConnectionListener, msg);
1078 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_START_STREAMING_AUDIO_DATA)) {
1079 dispatcher->on_start_streaming_audio_data(mConnectionListener, msg);
1081 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_STOP_STREAMING_AUDIO_DATA)) {
1082 dispatcher->on_stop_streaming_audio_data(mConnectionListener, msg);
1084 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_UPDATE_VOICE_FEEDBACK_STATE)) {
1085 dispatcher->on_update_voice_feedback_state(mConnectionListener, msg);
1087 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_ASSISTANT_SPECIFIC_COMMAND)) {
1088 dispatcher->on_send_assistant_specific_command(mConnectionListener, msg);
1090 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SET_BACKGROUND_VOLUME)) {
1091 dispatcher->on_set_background_volume(mConnectionListener, msg);
1093 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SET_PREPROCESSING_ALLOW_MODE)) {
1094 dispatcher->on_set_preprocessing_allow_mode(mConnectionListener, msg);
1096 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_PREPROCESSING_RESULT)) {
1097 dispatcher->on_send_preprocessing_result(mConnectionListener, msg);
1099 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SET_WAKE_WORD_AUDIO_REQUIRE_FLAG)) {
1100 dispatcher->on_set_wake_word_audio_require_flag(mConnectionListener, msg);
1102 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SET_ASSISTANT_WAKEUP_LANGUAGE)) {
1103 dispatcher->on_set_assistant_language(mConnectionListener, msg);
1105 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_ADD_WAKE_WORD)) {
1106 dispatcher->on_add_wake_word(mConnectionListener, msg);
1108 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_REMOVE_WAKE_WORD)) {
1109 dispatcher->on_remove_wake_word(mConnectionListener, msg);
1111 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_UI_METHOD_INITIALIZE)) {
1112 dispatcher->on_ui_initialize(mConnectionListener, msg);
1114 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_UI_METHOD_DEINITIALIZE)) {
1115 dispatcher->on_ui_deinitialize(mConnectionListener, msg);
1117 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_UI_METHOD_CHANGE_ASSISTANT)) {
1118 dispatcher->on_ui_change_assistant(mConnectionListener, msg);
1121 MAS_LOGD("Message is NOT valid");
1122 /* Invalid method */
1124 /* free the message */
1125 dbus_message_unref(msg);
1128 return ECORE_CALLBACK_RENEW;
1131 void CServiceIpcDbus::connection_free()
1133 if (NULL != mConnectionListener) {
1134 dbus_connection_close(mConnectionListener);
1135 dbus_connection_unref(mConnectionListener);
1136 mConnectionListener = NULL;
1138 if (NULL != mConnectionSender) {
1139 dbus_connection_close(mConnectionSender);
1140 dbus_connection_unref(mConnectionSender);
1141 mConnectionSender = NULL;
1145 int CServiceIpcDbus::open_connection()
1148 dbus_error_init(&err);
1152 /* Create connection for sender */
1153 mConnectionSender = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
1155 if (dbus_error_is_set(&err)) {
1156 MAS_LOGE("[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
1157 dbus_error_free(&err);
1160 if (NULL == mConnectionSender) {
1161 MAS_LOGE("[Dbus ERROR] Fail to get dbus connection");
1165 dbus_connection_set_exit_on_disconnect(mConnectionSender, false);
1167 /* connect to the bus and check for errors */
1168 mConnectionListener = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
1170 if (dbus_error_is_set(&err)) {
1171 MAS_LOGE("[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
1172 dbus_error_free(&err);
1175 if (NULL == mConnectionListener) {
1176 MAS_LOGE("[Dbus ERROR] Fail to get dbus connection");
1181 dbus_connection_set_exit_on_disconnect(mConnectionListener, false);
1183 /* request our name on the bus and check for errors */
1184 ret = dbus_bus_request_name(mConnectionListener, MA_SERVER_SERVICE_NAME, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
1186 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
1187 printf("Fail to be primary owner in dbus request.");
1188 MAS_LOGE("[Dbus ERROR] Fail to be primary owner");
1193 if (dbus_error_is_set(&err)) {
1194 MAS_LOGE("[Dbus ERROR] dbus_bus_request_name() : %s", err.message);
1195 dbus_error_free(&err);
1200 /* Flush messages which are received before fd event handler registration */
1201 while (DBUS_DISPATCH_DATA_REMAINS == dbus_connection_get_dispatch_status(mConnectionListener)) {
1202 listener_event_callback(this, NULL);
1205 /* add a rule for getting signal */
1207 snprintf(rule, 128, "type='signal',interface='%s'", MA_SERVER_SERVICE_INTERFACE);
1209 /* add a rule for which messages we want to see */
1210 dbus_bus_add_match(mConnectionListener, rule, &err);/* see signals from the given interface */
1212 if (dbus_error_is_set(&err)) {
1213 MAS_LOGE("[Dbus ERROR] dbus_bus_add_match() : %s", err.message);
1214 dbus_error_free(&err);
1220 if (1 != dbus_connection_get_unix_fd(mConnectionListener, &fd)) {
1221 MAS_LOGE("fail to get fd from dbus ");
1225 MAS_LOGD("Get fd from dbus : %d", fd);
1228 mFdHandler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)listener_event_callback, this, NULL, NULL);
1230 if (NULL == mFdHandler) {
1231 MAS_LOGE("[Dbus ERROR] Fail to get fd handler");
1239 int CServiceIpcDbus::close_connection()
1242 dbus_error_init(&err);
1244 if (NULL != mFdHandler) {
1245 ecore_main_fd_handler_del(mFdHandler);
1249 dbus_bus_release_name(mConnectionListener, MA_SERVER_SERVICE_NAME, &err);
1251 if (dbus_error_is_set(&err)) {
1252 MAS_LOGE("[Dbus ERROR] dbus_bus_release_name() : %s", err.message);
1253 dbus_error_free(&err);