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(pid_t 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(pid_t 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 #if USE_TRUSTED_MESSAGE_PORT
250 int ret = message_port_send_trusted_message((*appid).c_str(), message_port, b);
252 int ret = message_port_send_message((*appid).c_str(), message_port, b);
254 if (MESSAGE_PORT_ERROR_NONE != ret)
255 masc_message_port_error(ret);
257 MAS_LOGE("AppID for PID %d not found!!!", pid);
260 pending_buffer_size = 0;
263 MAS_LOGE("Bundle creation failed!!!");
266 if (-1 != last_serial_waiting_for_flush) {
267 MAS_LOGI("flushing streaming data, serial : %d", last_serial_waiting_for_flush);
268 last_serial_waiting_for_flush = -1;
272 if (MAS_SPEECH_STREAMING_EVENT_FINISH == event) {
273 MAS_LOGE("Sending FINISH event");
274 bundle *b = bundle_create();
276 bundle_add_byte(b, "content", buffer, total_size);
277 boost::optional<std::string> appid = mApplicationManager->get_appid_by_pid(pid);
279 #if USE_TRUSTED_MESSAGE_PORT
280 int ret = message_port_send_trusted_message((*appid).c_str(), message_port, b);
282 int ret = message_port_send_message((*appid).c_str(), message_port, b);
284 if (MESSAGE_PORT_ERROR_NONE != ret)
285 masc_message_port_error(ret);
287 MAS_LOGE("AppID for PID %d not found!!!", pid);
292 MAS_LOGE("Bundle creation failed!!!");
295 memcpy(pending_buffer + pending_buffer_size, buffer, total_size);
296 pending_buffer_size += total_size;
301 int CServiceIpcDbus::change_active_state(pid_t pid, int state)
303 if (0 != __dbus_check()) {
304 return -1; //MAS_ERROR_OPERATION_FAILED;
310 dbus_error_init(&err);
312 char service_name[64];
313 memset(service_name, '\0', 64);
314 snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
316 msg = dbus_message_new_method_call(
318 MA_CLIENT_SERVICE_OBJECT_PATH,
319 MA_CLIENT_SERVICE_INTERFACE,
320 MAS_METHOD_ACTIVE_STATE_CHANGE);
323 MAS_LOGE(">>>> Request mas send activate message : Fail to make message");
324 return -1; // MAS_ERROR_OPERATION_FAILED;
326 MAS_LOGD(">>>> Request mas send activate message : %s", service_name);
329 if (true != dbus_message_append_args(msg,
330 DBUS_TYPE_INT32, &state,
331 DBUS_TYPE_INVALID)) {
332 dbus_message_unref(msg);
333 MAS_LOGE("[ERROR] Fail to append args");
337 dbus_message_set_no_reply(msg, TRUE);
339 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
340 MAS_LOGE("[Dbus ERROR] Fail to Send");
341 return -1; // MAS_ERROR_OPERATION_FAILED;
343 MAS_LOGI("[Dbus DEBUG] Success to Send activate message : %d %d", pid, state);
344 dbus_connection_flush(mConnectionSender);
347 dbus_message_unref(msg);
351 int CServiceIpcDbus::send_preprocessing_information(pid_t pid, const char* app_id)
353 if (0 != __dbus_check()) {
354 return -1; //MAS_ERROR_OPERATION_FAILED;
360 dbus_error_init(&err);
362 char service_name[64];
363 memset(service_name, '\0', 64);
364 snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
366 msg = dbus_message_new_method_call(
368 MA_CLIENT_SERVICE_OBJECT_PATH,
369 MA_CLIENT_SERVICE_INTERFACE,
370 MAS_METHOD_SEND_PREPROCESSING_INFORMATION);
373 MAS_LOGE(">>>> Request mas send preprocessing assistant information : Fail to make message");
374 return -1; // MAS_ERROR_OPERATION_FAILED;
376 MAS_LOGD(">>>> Request mas send preprocessing assistant information : %s", service_name);
379 char* temp_app_id = NULL;
381 temp_app_id = strdup("#NULL");
383 temp_app_id = strdup(app_id);
385 if (true != dbus_message_append_args(msg,
386 DBUS_TYPE_STRING, &temp_app_id,
387 DBUS_TYPE_INVALID)) {
388 dbus_message_unref(msg);
389 MAS_LOGE("[ERROR] Fail to append args");
395 dbus_message_set_no_reply(msg, TRUE);
397 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
398 MAS_LOGE("[Dbus ERROR] Fail to Send");
401 return -1; // MAS_ERROR_OPERATION_FAILED;
403 MAS_LOGI("[Dbus DEBUG] Success to Send preprocessing assistant information : %d %s", pid, app_id);
404 dbus_connection_flush(mConnectionSender);
407 dbus_message_unref(msg);
415 int CServiceIpcDbus::send_streaming_section_changed(pid_t pid, int section)
417 if (nullptr == mClientManager) {
418 MAS_LOGE("mClientManager variable is NULL");
422 bundle *b = bundle_create();
424 streaming_data_header header;
425 header.streaming_data_type = 1;
426 header.streaming_data_size = sizeof(streaming_data_header) + sizeof(streaming_data_streaming_section_header);
427 header.streaming_data_serial = mStreamingDataSerial++;
429 streaming_data_streaming_section_header streaming_section_header;
430 streaming_section_header.section = section;
432 unsigned char buffer[STREAMING_BUFFER_SIZE];
433 size_t total_size = 0;
434 memcpy(buffer, &header, sizeof(header));
435 total_size += sizeof(header);
436 memcpy(buffer + total_size, &streaming_section_header, sizeof(streaming_section_header));
437 total_size += sizeof(streaming_section_header);
439 bundle_add_byte(b, "content", buffer, total_size);
440 #if USE_TRUSTED_MESSAGE_PORT
441 int ret = message_port_send_trusted_message(
442 mClientManager->find_client_appid_by_pid(pid).c_str(), message_port, b);
444 int ret = message_port_send_message(
445 mClientManager->find_client_appid_by_pid(pid).c_str(), message_port, b);
447 if (MESSAGE_PORT_ERROR_NONE != ret)
448 masc_message_port_error(ret);
455 int CServiceIpcDbus::send_preprocessing_result(pid_t pid, bool result)
457 if (0 != __dbus_check()) {
458 return -1; //MAS_ERROR_OPERATION_FAILED;
464 dbus_error_init(&err);
466 char service_name[64];
467 memset(service_name, '\0', 64);
468 snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
470 msg = dbus_message_new_method_call(
472 MA_CLIENT_SERVICE_OBJECT_PATH,
473 MA_CLIENT_SERVICE_INTERFACE,
474 MAS_METHOD_SEND_PREPROCESSING_RESULT);
477 MAS_LOGE(">>>> Request mas send preprocessing result : Fail to make message");
478 return -1; // MAS_ERROR_OPERATION_FAILED;
480 MAS_LOGD(">>>> Request mas send preprocessing result : %s", service_name);
483 int temp_result = result;
485 if (true != dbus_message_append_args(msg,
486 DBUS_TYPE_INT32, &temp_result,
487 DBUS_TYPE_INVALID)) {
488 dbus_message_unref(msg);
489 MAS_LOGE("[ERROR] Fail to append args");
493 dbus_message_set_no_reply(msg, TRUE);
495 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
496 MAS_LOGE("[Dbus ERROR] Fail to Send");
497 return -1; // MAS_ERROR_OPERATION_FAILED;
499 MAS_LOGI("[Dbus DEBUG] Success to Send preprocessing result : %d %d", pid, temp_result);
500 dbus_connection_flush(mConnectionSender);
503 dbus_message_unref(msg);
508 int CServiceIpcDbus::send_wakeup_engine_command(pid_t pid, const char* command)
510 if (0 != __dbus_check()) {
511 return -1; //MAS_ERROR_OPERATION_FAILED;
517 dbus_error_init(&err);
519 char service_name[64];
520 memset(service_name, '\0', 64);
521 snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
523 msg = dbus_message_new_method_call(
525 MA_CLIENT_SERVICE_OBJECT_PATH,
526 MA_CLIENT_SERVICE_INTERFACE,
527 MAS_METHOD_SEND_WAKEUP_ENGINE_COMMAND);
530 MAS_LOGE(">>>> Request mas send wakeup engine command : Fail to make message");
531 return -1; // MAS_ERROR_OPERATION_FAILED;
533 MAS_LOGD(">>>> Request mas send wakeup engine command : %s", service_name);
536 char* temp_command = NULL;
538 temp_command = strdup("#NULL");
540 temp_command = strdup(command);
542 if (true != dbus_message_append_args(msg,
543 DBUS_TYPE_STRING, &temp_command,
544 DBUS_TYPE_INVALID)) {
545 dbus_message_unref(msg);
546 MAS_LOGE("[ERROR] Fail to append args");
552 dbus_message_set_no_reply(msg, TRUE);
554 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
555 MAS_LOGE("[Dbus ERROR] Fail to Send");
558 return -1; // MAS_ERROR_OPERATION_FAILED;
560 MAS_LOGI("[Dbus DEBUG] Success to Send wakeup_engine_command : %d %s", pid, command);
561 dbus_connection_flush(mConnectionSender);
564 dbus_message_unref(msg);
572 int CServiceIpcDbus::change_service_state(pid_t pid, int state)
574 if (0 != __dbus_check()) {
575 return -1; //MAS_ERROR_OPERATION_FAILED;
581 dbus_error_init(&err);
583 char service_name[64];
584 memset(service_name, '\0', 64);
585 snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
587 msg = dbus_message_new_method_call(
589 MA_CLIENT_SERVICE_OBJECT_PATH,
590 MA_CLIENT_SERVICE_INTERFACE,
591 MAS_METHOD_SERVICE_STATE_CHANGE);
594 MAS_LOGE(">>>> Request mas send service state message : Fail to make message");
595 return -1; // MAS_ERROR_OPERATION_FAILED;
597 MAS_LOGD(">>>> Request mas send service state message : %s", service_name);
600 if (true != dbus_message_append_args(msg,
601 DBUS_TYPE_INT32, &state,
602 DBUS_TYPE_INVALID)) {
603 dbus_message_unref(msg);
604 MAS_LOGE("[ERROR] Fail to append args");
608 dbus_message_set_no_reply(msg, TRUE);
610 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
611 MAS_LOGE("[Dbus ERROR] Fail to Send");
612 return -1; // MAS_ERROR_OPERATION_FAILED;
614 MAS_LOGI("[Dbus DEBUG] Success to Send service state message : %d %d", pid, state);
615 dbus_connection_flush(mConnectionSender);
618 dbus_message_unref(msg);
622 int CServiceIpcDbus::change_voice_key_status(pid_t pid, int status)
624 if (0 != __dbus_check()) {
625 return -1; //MAS_ERROR_OPERATION_FAILED;
631 dbus_error_init(&err);
633 char service_name[64];
634 memset(service_name, '\0', 64);
635 snprintf(service_name, 64, "%s_%d", MA_CLIENT_SERVICE_NAME, pid);
637 msg = dbus_message_new_method_call(
639 MA_CLIENT_SERVICE_OBJECT_PATH,
640 MA_CLIENT_SERVICE_INTERFACE,
641 MAS_METHOD_VOICE_KEY_STATUS_CHANGE);
644 MAS_LOGE(">>>> Request mas send voice key status change message : Fail to make message");
645 return -1; // MAS_ERROR_OPERATION_FAILED;
647 MAS_LOGD(">>>> Request mas send voice key status change message : %s", service_name);
650 if (true != dbus_message_append_args(msg,
651 DBUS_TYPE_INT32, &status,
652 DBUS_TYPE_INVALID)) {
653 dbus_message_unref(msg);
654 MAS_LOGE("[ERROR] Fail to append args");
658 dbus_message_set_no_reply(msg, TRUE);
660 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
661 MAS_LOGE("[Dbus ERROR] Fail to Send");
662 return -1; // MAS_ERROR_OPERATION_FAILED;
664 MAS_LOGI("[Dbus DEBUG] Success to Send voice key status change : %d %d", pid, status);
665 dbus_connection_flush(mConnectionSender);
668 dbus_message_unref(msg);
672 int CServiceIpcDbus::masc_ui_dbus_send_hello(void)
674 if (0 != __dbus_check()) {
675 return -1; //MAS_ERROR_OPERATION_FAILED;
681 dbus_error_init(&err);
683 msg = dbus_message_new_method_call(
684 MA_UI_CLIENT_SERVICE_NAME,
685 MA_UI_CLIENT_SERVICE_OBJECT_PATH,
686 MA_UI_CLIENT_SERVICE_INTERFACE,
690 MAS_LOGE("[DBus ERROR] Request masc hello : Fail to make message");
694 DBusMessage* result_msg = NULL;
697 result_msg = dbus_connection_send_with_reply_and_block(mConnectionSender, msg, 500, &err);
699 if (dbus_error_is_set(&err)) {
700 MAS_LOGE("[Dbus ERROR] Dbus Error (%s)", err.message);
701 dbus_error_free(&err);
704 dbus_message_unref(msg);
706 if (NULL != result_msg) {
707 dbus_message_unref(result_msg);
710 result = -1; //ERROR_TIMED_OUT;
716 int CServiceIpcDbus::masc_ui_dbus_send_asr_result(pid_t pid, int event, const char* asr_result)
718 if (0 != __dbus_check()) {
719 return -1; //MAS_ERROR_OPERATION_FAILED;
724 msg = dbus_message_new_method_call(
725 MA_UI_CLIENT_SERVICE_NAME,
726 MA_UI_CLIENT_SERVICE_OBJECT_PATH,
727 MA_UI_CLIENT_SERVICE_INTERFACE,
728 MAS_UI_METHOD_SEND_ASR_RESULT);
731 MAS_LOGE("@@ Request multi-assistant send ASR result : Fail to make message");
732 return -1; //MA_ERROR_OPERATION_FAILED;
734 MAS_LOGD("[DEBUG] multi-assistant send ASR result, asr_result(%p)", asr_result);
737 char* temp_asr_result = NULL;
739 temp_asr_result = strdup("#NULL");
741 temp_asr_result = strdup(asr_result);
743 dbus_message_append_args(msg,
744 DBUS_TYPE_INT32, &pid,
745 DBUS_TYPE_INT32, &event,
746 DBUS_TYPE_STRING, &temp_asr_result,
749 dbus_message_set_no_reply(msg, TRUE);
751 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
752 MAS_LOGE("[Dbus ERROR] Fail to Send");
753 if (NULL != temp_asr_result) {
754 free(temp_asr_result);
755 temp_asr_result = NULL;
757 return -1; // MAS_ERROR_OPERATION_FAILED;
759 MAS_LOGD("[Dbus DEBUG] Success to Send ASR result");
760 dbus_connection_flush(mConnectionSender);
763 dbus_message_unref(msg);
766 free(temp_asr_result);
770 int CServiceIpcDbus::masc_ui_dbus_send_result(pid_t pid, const char* display_text, const char* utterance_text, const char* result_json)
772 if (0 != __dbus_check()) {
773 return -1; //MA_ERROR_OPERATION_FAILED;
778 msg = dbus_message_new_method_call(
779 MA_UI_CLIENT_SERVICE_NAME,
780 MA_UI_CLIENT_SERVICE_OBJECT_PATH,
781 MA_UI_CLIENT_SERVICE_INTERFACE,
782 MAS_UI_METHOD_SEND_RESULT);
785 MAS_LOGE("@@ Request multi-assistant send result : Fail to make message");
786 return -1; //MA_ERROR_OPERATION_FAILED;
788 MAS_LOGD("[DEBUG] multi-assistant send result");
790 char* temp_display_text = NULL;
791 char* temp_utterance_text = NULL;
792 char* temp_result_json = NULL;
795 dbus_message_append_args(msg,
796 DBUS_TYPE_INT32, &pid,
797 DBUS_TYPE_STRING, &display_text,
798 DBUS_TYPE_STRING, &utterance_text,
799 DBUS_TYPE_STRING, &result_json,
802 dbus_message_set_no_reply(msg, TRUE);
804 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
805 MAS_LOGE("[Dbus ERROR] Fail to Send");
806 return -1; //MA_ERROR_OPERATION_FAILED;
808 MAS_LOGD("[Dbus DEBUG] Success to Send result");
809 dbus_connection_flush(mConnectionSender);
812 dbus_message_unref(msg);
815 temp_display_text = strdup("#NULL");
817 temp_display_text = strdup(display_text);
819 temp_utterance_text = strdup("#NULL");
821 temp_utterance_text = strdup(utterance_text);
823 temp_result_json = strdup("#NULL");
825 temp_result_json = strdup(result_json);
827 dbus_message_append_args(msg,
828 DBUS_TYPE_INT32, &pid,
829 DBUS_TYPE_STRING, &temp_display_text,
830 DBUS_TYPE_STRING, &temp_utterance_text,
831 DBUS_TYPE_STRING, &temp_result_json,
834 dbus_message_set_no_reply(msg, TRUE);
836 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
837 MAS_LOGE("[Dbus ERROR] Fail to Send");
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);
844 return -1; //MA_ERROR_OPERATION_FAILED;
846 MAS_LOGD("[Dbus DEBUG] Success to Send result");
847 dbus_connection_flush(mConnectionSender);
850 dbus_message_unref(msg);
852 if (temp_display_text)
853 free(temp_display_text);
854 if (temp_utterance_text)
855 free(temp_utterance_text);
856 if (temp_result_json)
857 free(temp_result_json);
862 int CServiceIpcDbus::masc_ui_dbus_change_assistant(const char* app_id)
864 if (0 != __dbus_check()) {
865 return -1; //MAS_ERROR_OPERATION_FAILED;
868 if (NULL == app_id) {
869 MAS_LOGE("@@ Request multi-assistant send change assistant request : Fail to make message");
870 return -1; //MA_ERROR_OPERATION_FAILED;
872 MAS_LOGD("[DEBUG] multi-assistant send change assistant request app_id(%s)", app_id);
877 msg = dbus_message_new_method_call(
878 MA_UI_CLIENT_SERVICE_NAME,
879 MA_UI_CLIENT_SERVICE_OBJECT_PATH,
880 MA_UI_CLIENT_SERVICE_INTERFACE,
881 MAS_UI_METHOD_CHANGE_ASSISTANT);
883 dbus_message_append_args(msg,
884 DBUS_TYPE_STRING, &app_id,
887 dbus_message_set_no_reply(msg, TRUE);
889 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
890 MAS_LOGE("[Dbus ERROR] Fail to Send");
891 return -1; // MAS_ERROR_OPERATION_FAILED;
893 MAS_LOGD("[Dbus DEBUG] Success to Send change assistant request");
894 dbus_connection_flush(mConnectionSender);
897 dbus_message_unref(msg);
902 int CServiceIpcDbus::masc_ui_dbus_send_error_message(int reason, const char* err_msg)
904 if (NULL == mConnectionSender) {
905 MAS_LOGE("[Dbus ERROR] Dbus connection is not available");
909 DBusMessage* msg = NULL;
911 /* create a message */
912 msg = dbus_message_new_signal(
913 MA_UI_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
914 MA_UI_CLIENT_SERVICE_INTERFACE, /* interface name of the signal */
915 MAS_UI_METHOD_ERROR); /* name of the signal */
918 MAS_LOGE("[Dbus ERROR] Fail to create error message");
922 char* temp_err_msg = NULL;
925 temp_err_msg = strdup(err_msg);
927 temp_err_msg = strdup("#NULL");
929 dbus_message_append_args(msg,
930 DBUS_TYPE_INT32, &reason,
931 DBUS_TYPE_STRING, &temp_err_msg,
934 dbus_message_set_no_reply(msg, TRUE);
936 if (!dbus_connection_send(mConnectionSender, msg, NULL)) {
937 MAS_LOGE("[Dbus ERROR] <<<< error message : Out Of Memory !");
939 MAS_LOGD("<<<< Send error message : reason(%d), err_msg(%s)", reason, temp_err_msg);
940 dbus_connection_flush(mConnectionSender);
943 dbus_message_unref(msg);
950 int CServiceIpcDbus::masc_ui_dbus_send_recognition_result(pid_t pid, int result)
952 if (0 != __dbus_check()) {
953 return -1; //MAS_ERROR_OPERATION_FAILED;
958 msg = dbus_message_new_method_call(
959 MA_UI_CLIENT_SERVICE_NAME,
960 MA_UI_CLIENT_SERVICE_OBJECT_PATH,
961 MA_UI_CLIENT_SERVICE_INTERFACE,
962 MAS_UI_METHOD_SEND_RECOGNITION_RESULT);
965 MAS_LOGE("@@ Request multi-assistant send recognition result : Fail to make message");
966 return -1; //MA_ERROR_OPERATION_FAILED;
968 MAS_LOGD("[DEBUG] multi-assistant send recognition result(%d)", result);
971 dbus_message_append_args(msg,
972 DBUS_TYPE_INT32, &pid,
973 DBUS_TYPE_INT32, &result,
976 dbus_message_set_no_reply(msg, TRUE);
978 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
979 MAS_LOGE("[Dbus ERROR] Fail to Send");
980 return -1; // MAS_ERROR_OPERATION_FAILED;
982 MAS_LOGD("[Dbus DEBUG] Success to Send ASR result");
983 dbus_connection_flush(mConnectionSender);
986 dbus_message_unref(msg);
991 int CServiceIpcDbus::masc_ui_dbus_enable_common_ui(int enable)
993 if (0 != __dbus_check()) {
994 return -1; //MAS_ERROR_OPERATION_FAILED;
999 msg = dbus_message_new_method_call(
1000 MA_UI_CLIENT_SERVICE_NAME,
1001 MA_UI_CLIENT_SERVICE_OBJECT_PATH,
1002 MA_UI_CLIENT_SERVICE_INTERFACE,
1003 MAS_UI_METHOD_ENABLE_COMMON_UI);
1006 MAS_LOGE("@@ Request multi-assistant enable common ui : Fail to make message");
1007 return -1; //MA_ERROR_OPERATION_FAILED;
1009 MAS_LOGD("[DEBUG] multi-assistant enable common ui (%d)", enable);
1012 dbus_message_append_args(msg,
1013 DBUS_TYPE_INT32, &enable,
1016 dbus_message_set_no_reply(msg, TRUE);
1018 if (1 != dbus_connection_send(mConnectionSender, msg, NULL)) {
1019 MAS_LOGE("[Dbus ERROR] Fail to Send");
1020 return -1; // MAS_ERROR_OPERATION_FAILED;
1022 MAS_LOGD("[Dbus DEBUG] Success to Send ASR result");
1023 dbus_connection_flush(mConnectionSender);
1026 dbus_message_unref(msg);
1031 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
1033 CServiceIpcDbus* service_ipc = static_cast<CServiceIpcDbus*>(data);
1034 if (NULL == service_ipc) {
1035 MAS_LOGE("Error : service_ipc NULL");
1036 return ECORE_CALLBACK_RENEW;
1039 DBusConnection* mConnectionListener = service_ipc->get_connection_listener();
1040 if (NULL == mConnectionListener) {
1041 MAS_LOGE("Error : mConnectionListener NULL");
1042 return ECORE_CALLBACK_RENEW;
1045 CServiceIpcDbusDispatcher* dispatcher = service_ipc->get_dispatcher();
1046 if (NULL == dispatcher) {
1047 MAS_LOGE("Error : dispatcher NULL");
1048 return ECORE_CALLBACK_RENEW;
1051 dbus_connection_read_write_dispatch(mConnectionListener, 50);
1054 DBusMessage* msg = NULL;
1055 msg = dbus_connection_pop_message(mConnectionListener);
1057 if (true != dbus_connection_get_is_connected(mConnectionListener)) {
1058 MAS_LOGE("[ERROR] Connection is disconnected");
1059 return ECORE_CALLBACK_RENEW;
1062 /* loop again if we haven't read a message */
1064 return ECORE_CALLBACK_RENEW;
1068 if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_HELLO)) {
1069 dispatcher->on_hello(mConnectionListener, msg);
1071 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_INITIALIZE)) {
1072 dispatcher->on_initialize(mConnectionListener, msg);
1074 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_DEINITIALIZE)) {
1075 dispatcher->on_deinitialize(mConnectionListener, msg);
1077 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_GET_RECORDING_AUDIO_FORMAT)) {
1078 dispatcher->on_get_audio_format(mConnectionListener, msg);
1080 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_GET_RECORDING_AUDIO_SOURCE_TYPE)) {
1081 dispatcher->on_get_audio_source_type(mConnectionListener, msg);
1083 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_ASR_RESULT)) {
1084 dispatcher->on_send_asr_result(mConnectionListener, msg);
1086 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_RESULT)) {
1087 dispatcher->on_send_result(mConnectionListener, msg);
1089 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_RECOGNITION_RESULT)) {
1090 dispatcher->on_send_recognition_result(mConnectionListener, msg);
1092 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_START_STREAMING_AUDIO_DATA)) {
1093 dispatcher->on_start_streaming_audio_data(mConnectionListener, msg);
1095 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_STOP_STREAMING_AUDIO_DATA)) {
1096 dispatcher->on_stop_streaming_audio_data(mConnectionListener, msg);
1098 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_UPDATE_VOICE_FEEDBACK_STATE)) {
1099 dispatcher->on_update_voice_feedback_state(mConnectionListener, msg);
1101 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_ASSISTANT_SPECIFIC_COMMAND)) {
1102 dispatcher->on_send_assistant_specific_command(mConnectionListener, msg);
1104 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SET_BACKGROUND_VOLUME)) {
1105 dispatcher->on_set_background_volume(mConnectionListener, msg);
1107 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SET_PREPROCESSING_ALLOW_MODE)) {
1108 dispatcher->on_set_preprocessing_allow_mode(mConnectionListener, msg);
1110 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SEND_PREPROCESSING_RESULT)) {
1111 dispatcher->on_send_preprocessing_result(mConnectionListener, msg);
1113 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SET_WAKE_WORD_AUDIO_REQUIRE_FLAG)) {
1114 dispatcher->on_set_wake_word_audio_require_flag(mConnectionListener, msg);
1116 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_SET_ASSISTANT_WAKEUP_LANGUAGE)) {
1117 dispatcher->on_set_assistant_language(mConnectionListener, msg);
1119 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_ADD_WAKE_WORD)) {
1120 dispatcher->on_add_wake_word(mConnectionListener, msg);
1122 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_METHOD_REMOVE_WAKE_WORD)) {
1123 dispatcher->on_remove_wake_word(mConnectionListener, msg);
1125 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_UI_METHOD_INITIALIZE)) {
1126 dispatcher->on_ui_initialize(mConnectionListener, msg);
1128 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_UI_METHOD_DEINITIALIZE)) {
1129 dispatcher->on_ui_deinitialize(mConnectionListener, msg);
1131 } else if (dbus_message_is_method_call(msg, MA_SERVER_SERVICE_INTERFACE, MA_UI_METHOD_CHANGE_ASSISTANT)) {
1132 dispatcher->on_ui_change_assistant(mConnectionListener, msg);
1135 MAS_LOGD("Message is NOT valid");
1136 /* Invalid method */
1138 /* free the message */
1139 dbus_message_unref(msg);
1142 return ECORE_CALLBACK_RENEW;
1145 void CServiceIpcDbus::connection_free()
1147 if (NULL != mConnectionListener) {
1148 dbus_connection_close(mConnectionListener);
1149 dbus_connection_unref(mConnectionListener);
1150 mConnectionListener = NULL;
1152 if (NULL != mConnectionSender) {
1153 dbus_connection_close(mConnectionSender);
1154 dbus_connection_unref(mConnectionSender);
1155 mConnectionSender = NULL;
1159 int CServiceIpcDbus::open_connection()
1162 dbus_error_init(&err);
1166 /* Create connection for sender */
1167 mConnectionSender = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
1169 if (dbus_error_is_set(&err)) {
1170 MAS_LOGE("[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
1171 dbus_error_free(&err);
1174 if (NULL == mConnectionSender) {
1175 MAS_LOGE("[Dbus ERROR] Fail to get dbus connection");
1179 dbus_connection_set_exit_on_disconnect(mConnectionSender, false);
1181 /* connect to the bus and check for errors */
1182 mConnectionListener = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
1184 if (dbus_error_is_set(&err)) {
1185 MAS_LOGE("[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
1186 dbus_error_free(&err);
1189 if (NULL == mConnectionListener) {
1190 MAS_LOGE("[Dbus ERROR] Fail to get dbus connection");
1195 dbus_connection_set_exit_on_disconnect(mConnectionListener, false);
1197 /* request our name on the bus and check for errors */
1198 ret = dbus_bus_request_name(mConnectionListener, MA_SERVER_SERVICE_NAME, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
1200 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
1201 printf("Fail to be primary owner in dbus request.");
1202 MAS_LOGE("[Dbus ERROR] Fail to be primary owner");
1207 if (dbus_error_is_set(&err)) {
1208 MAS_LOGE("[Dbus ERROR] dbus_bus_request_name() : %s", err.message);
1209 dbus_error_free(&err);
1214 /* Flush messages which are received before fd event handler registration */
1215 while (DBUS_DISPATCH_DATA_REMAINS == dbus_connection_get_dispatch_status(mConnectionListener)) {
1216 listener_event_callback(this, NULL);
1219 /* add a rule for getting signal */
1221 snprintf(rule, 128, "type='signal',interface='%s'", MA_SERVER_SERVICE_INTERFACE);
1223 /* add a rule for which messages we want to see */
1224 dbus_bus_add_match(mConnectionListener, rule, &err);/* see signals from the given interface */
1226 if (dbus_error_is_set(&err)) {
1227 MAS_LOGE("[Dbus ERROR] dbus_bus_add_match() : %s", err.message);
1228 dbus_error_free(&err);
1234 if (1 != dbus_connection_get_unix_fd(mConnectionListener, &fd)) {
1235 MAS_LOGE("fail to get fd from dbus ");
1239 MAS_LOGD("Get fd from dbus : %d", fd);
1242 mFdHandler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)listener_event_callback, this, NULL, NULL);
1244 if (NULL == mFdHandler) {
1245 MAS_LOGE("[Dbus ERROR] Fail to get fd handler");
1253 int CServiceIpcDbus::close_connection()
1256 dbus_error_init(&err);
1258 if (NULL != mFdHandler) {
1259 ecore_main_fd_handler_del(mFdHandler);
1263 if (NULL != mConnectionListener) {
1264 dbus_bus_release_name(mConnectionListener, MA_SERVER_SERVICE_NAME, &err);
1265 if (dbus_error_is_set(&err)) {
1266 MAS_LOGE("[Dbus ERROR] dbus_bus_release_name() : %s", err.message);
1267 dbus_error_free(&err);
1270 MAS_LOGE("mConnectionListener is NULL!!");