Merge "Change uid and gid for service files" into tizen
[platform/core/uifw/tts.git] / server / ttsd_dbus.c
index a6fc673..c69cd30 100644 (file)
@@ -1,5 +1,5 @@
 /*
-*  Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved 
+*  Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  limitations under the License.
 */
 
-
 #include <dbus/dbus.h>
+#include <dirent.h>
+#include <dlfcn.h>
 #include <Ecore.h>
 
+#include "tts_config_mgr.h"
 #include "ttsd_main.h"
 #include "ttsd_dbus_server.h"
 #include "ttsd_dbus.h"
 #include "ttsd_server.h"
 
+static DBusConnection* g_conn_sender = NULL;
+static DBusConnection* g_conn_listener = NULL;
+
+static Ecore_Fd_Handler* g_dbus_fd_handler = NULL;
+
+//static int g_waiting_time = 3000;
+
+static char *g_service_name = NULL;
+static char *g_service_object = NULL;
+static char *g_service_interface = NULL;
+
+const char* __ttsd_get_error_code(ttsd_error_e err)
+{
+       switch (err) {
+       case TTSD_ERROR_NONE:                   return "TTS_ERROR_NONE";
+       case TTSD_ERROR_OUT_OF_MEMORY:          return "TTS_ERROR_OUT_OF_MEMORY";
+       case TTSD_ERROR_IO_ERROR:               return "TTS_ERROR_IO_ERROR";
+       case TTSD_ERROR_INVALID_PARAMETER:      return "TTS_ERROR_INVALID_PARAMETER";
+       case TTSD_ERROR_OUT_OF_NETWORK:         return "TTS_ERROR_OUT_OF_NETWORK";
+       case TTSD_ERROR_INVALID_STATE:          return "TTS_ERROR_INVALID_STATE";
+       case TTSD_ERROR_INVALID_VOICE:          return "TTS_ERROR_INVALID_VOICE";
+       case TTSD_ERROR_ENGINE_NOT_FOUND:       return "TTS_ERROR_ENGINE_NOT_FOUND";
+       case TTSD_ERROR_TIMED_OUT:              return "TTS_ERROR_TIMED_OUT";
+       case TTSD_ERROR_OPERATION_FAILED:       return "TTS_ERROR_OPERATION_FAILED";
+       case TTSD_ERROR_AUDIO_POLICY_BLOCKED:   return "TTS_ERROR_AUDIO_POLICY_BLOCKED";
+       case TTSD_ERROR_NOT_SUPPORTED_FEATURE:  return "TTSD_ERROR_NOT_SUPPORTED_FEATURE";
+       case TTSD_ERROR_SERVICE_RESET:          return "TTSD_ERROR_SERVICE_RESET";
+       default:
+               return "Invalid error code";
+       }
+
+       return NULL;
+}
 
-static DBusConnection* g_conn;
+int ttsdc_send_hello(int pid, int uid)
+{
+#if 0
+       if (NULL == g_conn_sender) {
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Dbus connection is not available");
+               return -1;
+       }
 
-int ttsdc_send_message(int pid, int uid, int uttid, char *method)
-{   
        char service_name[64];
        memset(service_name, 0, 64);
        snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
@@ -36,25 +75,83 @@ int ttsdc_send_message(int pid, int uid, int uttid, char *method)
 
        /* create a message & check for errors */
        msg = dbus_message_new_method_call(
-               service_name, 
-               TTS_CLIENT_SERVICE_OBJECT_PATH, 
-               target_if_name, 
-               method);
+               service_name,
+               TTS_CLIENT_SERVICE_OBJECT_PATH,
+               target_if_name,
+               TTSD_METHOD_HELLO);
+
+       if (NULL == msg) {
+               SLOG(LOG_ERROR, tts_tag(), "<<<< [Dbus ERROR] Fail to create hello message : uid(%d)", uid);
+               return -1;
+       } else {
+               SLOG(LOG_DEBUG, tts_tag(), "<<<< [Dbus] Send hello message : uid(%d)", uid);
+       }
+
+       dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);
+
+       DBusError err;
+       dbus_error_init(&err);
+
+       DBusMessage* result_msg;
+       int result = -1;
+
+       result_msg = dbus_connection_send_with_reply_and_block(g_conn_sender, msg, g_waiting_time, &err);
+       dbus_message_unref(msg);
+       if (dbus_error_is_set(&err)) {
+               SLOG(LOG_ERROR, tts_tag(), "[ERROR] Send error (%s)", err.message);
+               dbus_error_free(&err);
+       }
+
+       if (NULL != result_msg) {
+               dbus_message_get_args(result_msg, &err, DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
+
+               if (dbus_error_is_set(&err)) {
+                       SLOG(LOG_ERROR, tts_tag(), ">>>> [Dbus] Get arguments error (%s)", err.message);
+                       dbus_error_free(&err);
+                       result = -1;
+               }
+
+               dbus_message_unref(result_msg);
+       } else {
+               SLOG(LOG_DEBUG, tts_tag(), ">>>> [Dbus] Result message is NULL. Client is not available");
+               result = 0;
+       }
+
+       return result;
+#endif
+       return 0;
+}
 
-       if (NULL == msg) { 
-               SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] Fail to create message : type(%s), uid(%d)\n", method, uid); 
+int ttsdc_send_message(int pid, int uid, int data, const char *method)
+{
+       if (NULL == g_conn_sender) {
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Dbus connection is not available");
                return -1;
        }
 
-       dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INT32, &uttid, DBUS_TYPE_INVALID);
+       DBusMessage* msg = NULL;
+
+       /* create a message */
+       msg = dbus_message_new_signal(
+               TTS_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
+               TTS_CLIENT_SERVICE_INTERFACE,   /* interface name of the signal */
+               method);                        /* name of the signal */
 
-       /* send the message and flush the connection */
-       if (!dbus_connection_send(g_conn, msg, NULL)) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] <<<< send message : Out Of Memory, type(%s), ifname(%s), uid(%d), uttid(%d)", method, target_if_name, uid, uttid); 
+       if (NULL == msg) {
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to create message : %s", method);
+               return -1;
        } else {
-               SLOG(LOG_DEBUG, TAG_TTSD, "<<<< send message : type(%s), uid(%d), uttid(%d)", method, uid, uttid);
+               SLOG(LOG_DEBUG, tts_tag(), "[Dbus] Send %s message : uid(%d) data(%d)", method, uid, data);
+       }
+
+       dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INT32, &data, DBUS_TYPE_INVALID);
 
-               dbus_connection_flush(g_conn);
+       if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to Send");
+               return -1;
+       } else {
+               SLOG(LOG_DEBUG, tts_tag(), "[Dbus] SUCCESS Send");
+               dbus_connection_flush(g_conn_sender);
        }
 
        dbus_message_unref(msg);
@@ -64,52 +161,54 @@ int ttsdc_send_message(int pid, int uid, int uttid, char *method)
 
 int ttsdc_send_utt_start_message(int pid, int uid, int uttid)
 {
-       return ttsdc_send_message(pid, uid, uttid, TTS_METHOD_UTTERANCE_STARTED);
+       return ttsdc_send_message(pid, uid, uttid, TTSD_METHOD_UTTERANCE_STARTED);
 }
 
-int ttsdc_send_utt_finish_message(int pid, int uid, int uttid) 
+int ttsdc_send_utt_finish_message(int pid, int uid, int uttid)
 {
-       return ttsdc_send_message(pid, uid, uttid, TTS_METHOD_UTTERANCE_COMPLETED);
+       return ttsdc_send_message(pid, uid, uttid, TTSD_METHOD_UTTERANCE_COMPLETED);
 }
 
-int ttsdc_send_interrupt_message(int pid, int uid, ttsd_interrupted_code_e code)
+int ttsdc_send_set_state_message(int pid, int uid, int state)
 {
-       return ttsdc_send_message(pid, uid, (int)code, TTS_METHOD_INTERRUPT);
+       return ttsdc_send_message(pid, uid, state, TTSD_METHOD_SET_STATE);
 }
 
-int ttsdc_send_error_message(int pid, int uid, int uttid, int reason)
+int ttsdc_send_error_message(int pid, int uid, int uttid, int reason, char* err_msg)
 {
-       char service_name[64];
-       memset(service_name, 0, 64);
-       snprintf(service_name, 64, "%s%d", TTS_CLIENT_SERVICE_NAME, pid);
-
-       char target_if_name[128];
-       snprintf(target_if_name, sizeof(target_if_name), "%s%d", TTS_CLIENT_SERVICE_INTERFACE, pid);
+       if (NULL == g_conn_sender) {
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Dbus connection is not available");
+               return -1;
+       }
 
-       DBusMessage* msg;
+       DBusMessage* msg = NULL;
 
-       msg = dbus_message_new_method_call(
-               service_name, 
-               TTS_CLIENT_SERVICE_OBJECT_PATH, 
-               target_if_name, 
-               TTS_METHOD_ERROR);
+       /* create a message */
+       msg = dbus_message_new_signal(
+               TTS_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
+               TTS_CLIENT_SERVICE_INTERFACE,   /* interface name of the signal */
+               TTSD_METHOD_ERROR);             /* name of the signal */
 
-       if (NULL == msg) { 
-               SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] Fail to create error message : uid(%d)\n", uid); 
+       if (NULL == msg) {
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to create error message : uid(%d)", uid);
                return -1;
        }
 
-       dbus_message_append_args( msg, 
-               DBUS_TYPE_INT32, &uid, 
-               DBUS_TYPE_INT32, &uttid, 
-               DBUS_TYPE_INT32, &reason, 
+       dbus_message_append_args(msg,
+               DBUS_TYPE_INT32, &uid,
+               DBUS_TYPE_INT32, &uttid,
+               DBUS_TYPE_INT32, &reason,
+               DBUS_TYPE_STRING, &err_msg,
                DBUS_TYPE_INVALID);
-       
-       if (!dbus_connection_send(g_conn, msg, NULL)) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] <<<< error message : Out Of Memory !\n"); 
+
+       dbus_message_set_no_reply(msg, TRUE);
+
+       if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] <<<< error message : Out Of Memory !");
        } else {
-               SLOG(LOG_DEBUG, TAG_TTSD, "<<<< Send error signal : uid(%d), reason(%d), uttid(%d)", uid, reason, uttid);
-               dbus_connection_flush(g_conn);
+               SLOG(LOG_DEBUG, tts_tag(), "<<<< Send error message : uid(%d), reason(%s), uttid(%d), err_msg(%s)",
+                        uid, __ttsd_get_error_code(reason), uttid, (NULL == err_msg) ? "NULL" : err_msg);
+               dbus_connection_flush(g_conn_sender);
        }
 
        dbus_message_unref(msg);
@@ -117,239 +216,286 @@ int ttsdc_send_error_message(int pid, int uid, int uttid, int reason)
        return 0;
 }
 
-
 static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handler)
 {
-       DBusConnection* conn = (DBusConnection*)data;
+       if (NULL == g_conn_listener)    return ECORE_CALLBACK_RENEW;
 
-       if (NULL == conn)       return ECORE_CALLBACK_RENEW;
+       dbus_connection_read_write_dispatch(g_conn_listener, 50);
 
-       dbus_connection_read_write_dispatch(conn, 50);
+       while (1) {
+               DBusMessage* msg = NULL;
+               msg = dbus_connection_pop_message(g_conn_listener);
 
-       DBusMessage* msg = NULL;
-       msg = dbus_connection_pop_message(conn);
+               if (true != dbus_connection_get_is_connected(g_conn_listener)) {
+                       SLOG(LOG_ERROR, tts_tag(), "[ERROR] Connection is disconnected");
+                       return ECORE_CALLBACK_RENEW;
+               }
 
-       /* loop again if we haven't read a message */
-       if (NULL == msg) { 
-               return ECORE_CALLBACK_RENEW;
-       }
-       
-       /* client event */
-       if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_HELLO))
-               ttsd_dbus_server_hello(conn, msg);
-
-       else if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_INITIALIZE) )
-               ttsd_dbus_server_initialize(conn, msg);
-       
-       else if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_FINALIZE) )
-               ttsd_dbus_server_finalize(conn, msg);
-       
-       else if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_GET_SUPPORT_VOICES) )
-               ttsd_dbus_server_get_support_voices(conn, msg);
-
-       else if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_GET_CURRENT_VOICE) )
-               ttsd_dbus_server_get_current_voice(conn, msg);
-
-       else if( dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_ADD_QUEUE) )
-               ttsd_dbus_server_add_text(conn, msg);
+               /* loop again if we haven't read a message */
+               if (NULL == msg) {
+                       return ECORE_CALLBACK_RENEW;
+               }
 
-       else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_PLAY)) 
-               ttsd_dbus_server_play(conn, msg);
-       
-       else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_STOP)) 
-               ttsd_dbus_server_stop(conn, msg);
+               /* client event */
+               if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_HELLO)) {
+                       ttsd_dbus_server_hello(g_conn_listener, msg);
 
-       else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_PAUSE)) 
-               ttsd_dbus_server_pause(conn, msg);
+               } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_INITIALIZE)) {
+                       ttsd_dbus_server_initialize(g_conn_listener, msg);
 
-       /* setting event */
-       else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_HELLO))
-               ttsd_dbus_server_hello(conn, msg);
+               } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_FINALIZE)) {
+                       ttsd_dbus_server_finalize(g_conn_listener, msg);
 
-       else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_INITIALIZE) )
-               ttsd_dbus_server_setting_initialize(conn, msg);
+               } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_GET_SUPPORT_VOICES)) {
+                       ttsd_dbus_server_get_support_voices(g_conn_listener, msg);
 
-       else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_FINALIZE) )
-               ttsd_dbus_server_setting_finalize(conn, msg);
+               } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_GET_CURRENT_VOICE)) {
+                       ttsd_dbus_server_get_current_voice(g_conn_listener, msg);
 
-       else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_ENGINE_LIST) )
-               ttsd_dbus_server_setting_get_engine_list(conn, msg);
+               } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_ADD_QUEUE)) {
+                       ttsd_dbus_server_add_text(g_conn_listener, msg);
 
-       else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_ENGINE) )
-               ttsd_dbus_server_setting_get_engine(conn, msg);
+               } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_PLAY)) {
+                       ttsd_dbus_server_play(g_conn_listener, msg);
 
-       else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_SET_ENGINE) )
-               ttsd_dbus_server_setting_set_engine(conn, msg);
+               } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_STOP)) {
+                       ttsd_dbus_server_stop(g_conn_listener, msg);
 
-       else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_VOICE_LIST) )
-               ttsd_dbus_server_setting_get_voice_list(conn, msg);
+               } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_PAUSE)) {
+                       ttsd_dbus_server_pause(g_conn_listener, msg);
 
-       else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_DEFAULT_VOICE) )
-               ttsd_dbus_server_setting_get_default_voice(conn, msg);
+               } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_SET_PRIVATE_DATA)) {
+                       ttsd_dbus_server_set_private_data(g_conn_listener, msg);
 
-       else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_SET_DEFAULT_VOICE) )
-               ttsd_dbus_server_setting_set_default_voice(conn, msg);
+               } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_GET_PRIVATE_DATA)) {
+                       ttsd_dbus_server_get_private_data(g_conn_listener, msg);
 
-       else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_DEFAULT_SPEED) )
-               ttsd_dbus_server_setting_get_speed(conn, msg);
+               } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_PLAY_PCM)) {
+                       ttsd_dbus_server_play_pcm(g_conn_listener, msg);
 
-       else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_SET_DEFAULT_SPEED) )
-               ttsd_dbus_server_setting_set_speed(conn, msg);
+               } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_STOP_PCM)) {
+                       ttsd_dbus_server_stop_pcm(g_conn_listener, msg);
 
-       else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_GET_ENGINE_SETTING) )
-               ttsd_dbus_server_setting_get_engine_setting(conn, msg);
+               } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_ADD_PCM)) {
+                       ttsd_dbus_server_add_pcm(g_conn_listener, msg);
 
-       else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_SETTING_METHOD_SET_ENGINE_SETTING) )
-               ttsd_dbus_server_setting_set_engine_setting(conn, msg);
+               } else {
+                       SLOG(LOG_DEBUG, tts_tag(), "Message is NOT valid");
+                       /* Invalid method */
+               }
+               /* free the message */
+               dbus_message_unref(msg);
+       }
 
+       return ECORE_CALLBACK_RENEW;
+}
 
-       else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_NEXT_PLAY)) 
-               ttsd_dbus_server_start_next_play(msg);
+void __ttsd_dbus_service_free()
+{
+       if (NULL != g_service_name) {
+               free(g_service_name);
+               g_service_name = NULL;
+       }
 
-       else if (dbus_message_is_method_call(msg, TTS_SERVER_SERVICE_INTERFACE, TTS_METHOD_NEXT_SYNTHESIS)) 
-               ttsd_dbus_server_start_next_synthesis(msg);
-       
+       if (NULL != g_service_object) {
+               free(g_service_object);
+               g_service_object = NULL;
+       }
 
-       /* free the message */
-       dbus_message_unref(msg);
+       if (NULL != g_service_interface) {
+               free(g_service_interface);
+               g_service_interface = NULL;
+       }
+}
 
-       return ECORE_CALLBACK_RENEW;
+void __ttsd_dbus_connection_free()
+{
+       if (NULL != g_conn_listener) {
+               dbus_connection_close(g_conn_listener);
+               dbus_connection_unref(g_conn_listener);
+               g_conn_listener = NULL;
+       }
+       if (NULL != g_conn_sender) {
+               dbus_connection_close(g_conn_sender);
+               dbus_connection_unref(g_conn_sender);
+               g_conn_sender = NULL;
+       }
 }
 
 int ttsd_dbus_open_connection()
 {
+       SLOG(LOG_DEBUG, tts_tag(), "@@@ start dbus open connection");
        DBusError err;
        dbus_error_init(&err);
 
        int ret;
 
-       /* connect to the bus and check for errors */
-       g_conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
+       /* Create connection for sender */
+       g_conn_sender = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
+       if (dbus_error_is_set(&err)) {
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
+               dbus_error_free(&err);
+       }
 
-       if (dbus_error_is_set(&err)) { 
-               SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] fail dbus_bus_get : %s\n", err.message);
-               dbus_error_free(&err); 
+       if (NULL == g_conn_sender) {
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to get dbus connection sender");
+               return -1;
        }
 
-       if (NULL == g_conn) { 
-               SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] fail to get dbus connection \n" );
+       dbus_connection_set_exit_on_disconnect(g_conn_sender, false);
+
+       /* connect to the bus and check for errors */
+       g_conn_listener = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
+       if (dbus_error_is_set(&err)) {
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
+               dbus_error_free(&err);
+               __ttsd_dbus_connection_free();
                return -1;
        }
 
-       /* request our name on the bus and check for errors */
-       ret = dbus_bus_request_name(g_conn, TTS_SERVER_SERVICE_NAME, DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
+       if (NULL == g_conn_listener) {
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to get dbus connection");
+               __ttsd_dbus_connection_free();
+               return -1;
+       }
 
+       dbus_connection_set_exit_on_disconnect(g_conn_listener, false);
+
+       __ttsd_dbus_service_free();
+
+       if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
+               g_service_name = (char*)calloc(strlen(TTS_SR_SERVER_SERVICE_NAME) + 1, sizeof(char));
+               g_service_object = (char*)calloc(strlen(TTS_SR_SERVER_SERVICE_OBJECT_PATH) + 1, sizeof(char));
+               g_service_interface = (char*)calloc(strlen(TTS_SR_SERVER_SERVICE_INTERFACE) + 1, sizeof(char));
+
+               if (NULL == g_service_name || NULL == g_service_object || NULL == g_service_interface) {
+                       SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to allocate memory");
+                       __ttsd_dbus_service_free();
+                       __ttsd_dbus_connection_free();
+                       return -1;
+               }
+
+               snprintf(g_service_name, strlen(TTS_SR_SERVER_SERVICE_NAME) + 1, "%s", TTS_SR_SERVER_SERVICE_NAME);
+               snprintf(g_service_object, strlen(TTS_SR_SERVER_SERVICE_OBJECT_PATH) + 1, "%s", TTS_SR_SERVER_SERVICE_OBJECT_PATH);
+               snprintf(g_service_interface, strlen(TTS_SR_SERVER_SERVICE_INTERFACE) + 1, "%s", TTS_SR_SERVER_SERVICE_INTERFACE);
+       } else if (TTSD_MODE_NOTIFICATION == ttsd_get_mode()) {
+               g_service_name = (char*)calloc(strlen(TTS_NOTI_SERVER_SERVICE_NAME) + 1, sizeof(char));
+               g_service_object = (char*)calloc(strlen(TTS_NOTI_SERVER_SERVICE_OBJECT_PATH) + 1, sizeof(char));
+               g_service_interface = (char*)calloc(strlen(TTS_NOTI_SERVER_SERVICE_INTERFACE) + 1, sizeof(char));
+
+               if (NULL == g_service_name || NULL == g_service_object || NULL == g_service_interface) {
+                       SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to allocate memory");
+                       __ttsd_dbus_service_free();
+                       __ttsd_dbus_connection_free();
+                       return -1;
+               }
+
+               snprintf(g_service_name, strlen(TTS_NOTI_SERVER_SERVICE_NAME) + 1, "%s", TTS_NOTI_SERVER_SERVICE_NAME);
+               snprintf(g_service_object, strlen(TTS_NOTI_SERVER_SERVICE_OBJECT_PATH) + 1, "%s", TTS_NOTI_SERVER_SERVICE_OBJECT_PATH);
+               snprintf(g_service_interface, strlen(TTS_NOTI_SERVER_SERVICE_INTERFACE) + 1, "%s", TTS_NOTI_SERVER_SERVICE_INTERFACE);
+       } else if (TTSD_MODE_INTERRUPT == ttsd_get_mode()) {
+               g_service_name = (char*)calloc(strlen(TTS_INTERRUPT_SERVER_SERVICE_NAME) + 1, sizeof(char));
+               g_service_object = (char*)calloc(strlen(TTS_INTERRUPT_SERVER_SERVICE_OBJECT_PATH) + 1, sizeof(char));
+               g_service_interface = (char*)calloc(strlen(TTS_INTERRUPT_SERVER_SERVICE_INTERFACE) + 1, sizeof(char));
+
+               if (NULL == g_service_name || NULL == g_service_object || NULL == g_service_interface) {
+                       SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to allocate memory");
+                       __ttsd_dbus_service_free();
+                       __ttsd_dbus_connection_free();
+                       return -1;
+               }
+
+               snprintf(g_service_name, strlen(TTS_INTERRUPT_SERVER_SERVICE_NAME) + 1, "%s", TTS_INTERRUPT_SERVER_SERVICE_NAME);
+               snprintf(g_service_object, strlen(TTS_INTERRUPT_SERVER_SERVICE_OBJECT_PATH) + 1, "%s", TTS_INTERRUPT_SERVER_SERVICE_OBJECT_PATH);
+               snprintf(g_service_interface, strlen(TTS_INTERRUPT_SERVER_SERVICE_INTERFACE) + 1, "%s", TTS_INTERRUPT_SERVER_SERVICE_INTERFACE);
+       } else {
+               g_service_name = (char*)calloc(strlen(TTS_SERVER_SERVICE_NAME) + 1, sizeof(char));
+               g_service_object = (char*)calloc(strlen(TTS_SERVER_SERVICE_OBJECT_PATH) + 1, sizeof(char));
+               g_service_interface = (char*)calloc(strlen(TTS_SERVER_SERVICE_INTERFACE) + 1, sizeof(char));
+
+               if (NULL == g_service_name || NULL == g_service_object || NULL == g_service_interface) {
+                       SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to allocate memory");
+                       __ttsd_dbus_service_free();
+                       __ttsd_dbus_connection_free();
+                       return -1;
+               }
+
+               snprintf(g_service_name, strlen(TTS_SERVER_SERVICE_NAME) + 1, "%s", TTS_SERVER_SERVICE_NAME);
+               snprintf(g_service_object, strlen(TTS_SERVER_SERVICE_OBJECT_PATH) + 1, "%s", TTS_SERVER_SERVICE_OBJECT_PATH);
+               snprintf(g_service_interface, strlen(TTS_SERVER_SERVICE_INTERFACE) + 1, "%s", TTS_SERVER_SERVICE_INTERFACE);
+       }
+
+       /* request our name on the bus and check for errors */
+       ret = dbus_bus_request_name(g_conn_listener, g_service_name, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
        if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
-               printf("Fail to be primary owner in dbus request. \n");
-               SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] fail to be primary owner \n");
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to be primary owner");
+               __ttsd_dbus_connection_free();
                return -1;
        }
 
-       if (dbus_error_is_set(&err)) { 
-               SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] dbus_bus_request_name() : %s \n", err.message);
-               dbus_error_free(&err); 
-
+       if (dbus_error_is_set(&err)) {
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to request dbus name : %s", err.message);
+               dbus_error_free(&err);
+               __ttsd_dbus_connection_free();
                return -1;
        }
 
+       /* Flush messages which are received before fd event handler registration */
+       while (DBUS_DISPATCH_DATA_REMAINS == dbus_connection_get_dispatch_status(g_conn_listener)) {
+               listener_event_callback(NULL, NULL);
+       }
+
        /* add a rule for getting signal */
        char rule[128];
-       snprintf(rule, 128, "type='signal',interface='%s'", TTS_SERVER_SERVICE_INTERFACE);
+       snprintf(rule, 128, "type='method_call',interface='%s'", g_service_interface);
 
        /* add a rule for which messages we want to see */
-       dbus_bus_add_match(g_conn, rule, &err); /* see signals from the given interface */
-       dbus_connection_flush(g_conn);
+       dbus_bus_add_match(g_conn_listener, rule, &err);/* see signals from the given interface */
+       dbus_connection_flush(g_conn_listener);
 
-       if (dbus_error_is_set(&err)) { 
-               SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] dbus_bus_add_match() : %s \n", err.message);
-               return -1; 
+       if (dbus_error_is_set(&err)) {
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] dbus_bus_add_match() : %s", err.message);
+               __ttsd_dbus_connection_free();
+               return -1;
        }
 
        int fd = 0;
-       dbus_connection_get_unix_fd(g_conn, &fd);
-
-       Ecore_Fd_Handler* fd_handler;
-       fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ , (Ecore_Fd_Cb)listener_event_callback, g_conn, NULL, NULL);
+       dbus_connection_get_unix_fd(g_conn_listener, &fd);
 
-       if (NULL == fd_handler) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] ecore_main_fd_handler_add() : fail to get fd handler \n");
+       g_dbus_fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)listener_event_callback, g_conn_listener, NULL, NULL);
+       if (NULL == g_dbus_fd_handler) {
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to get fd handler");
+               __ttsd_dbus_connection_free();
                return -1;
        }
 
-       SLOG(LOG_DEBUG, TAG_TTSD, "[Dbus SUCCESS] Open connection. ");
+       SLOG(LOG_DEBUG, tts_tag(), "@@@");
        return 0;
 }
 
 int ttsd_dbus_close_connection()
 {
+       SLOG(LOG_DEBUG, tts_tag(), "@@@ start dbus close connection");
        DBusError err;
        dbus_error_init(&err);
 
-       dbus_bus_release_name (g_conn, TTS_SERVER_SERVICE_NAME, &err);
-
-       if (dbus_error_is_set(&err)) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] dbus_bus_release_name() : %s\n", err.message); 
-               dbus_error_free(&err); 
-               return -1;
+       if (NULL != g_dbus_fd_handler) {
+               ecore_main_fd_handler_del(g_dbus_fd_handler);
+               g_dbus_fd_handler = NULL;
        }
 
-       SLOG(LOG_DEBUG, TAG_TTSD, "[Dbus SUCCESS] Close connection. ");
-
-       return 0;
-}
-
-int ttsd_send_start_next_play_message(int uid)
-{
-       DBusMessage* msg;
-
-       msg = dbus_message_new_method_call(
-               TTS_SERVER_SERVICE_NAME, 
-               TTS_SERVER_SERVICE_OBJECT_PATH, 
-               TTS_SERVER_SERVICE_INTERFACE, 
-               TTS_METHOD_NEXT_PLAY);
-
-       if (NULL == msg) { 
-               SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] >>>> Fail to make message for 'start next play'"); 
-               return -1;
-       } 
-
-       dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);        
-
-       if (!dbus_connection_send(g_conn, msg, NULL)) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] >>>> Fail to send message for 'start next play'\n"); 
-               return -1;
+       if (NULL != g_conn_listener) {
+               dbus_bus_release_name(g_conn_listener, g_service_name, &err);
+               if (dbus_error_is_set(&err)) {
+                       SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] dbus_bus_release_name() : %s", err.message);
+                       dbus_error_free(&err);
+               }
        }
 
-       dbus_connection_flush(g_conn);
-       dbus_message_unref(msg);
-
-       return 0;
-}
-
-int ttsd_send_start_next_synthesis_message(int uid)
-{
-       DBusMessage* msg;
+       __ttsd_dbus_connection_free();
+       __ttsd_dbus_service_free();
 
-       msg = dbus_message_new_method_call(
-               TTS_SERVER_SERVICE_NAME, 
-               TTS_SERVER_SERVICE_OBJECT_PATH, 
-               TTS_SERVER_SERVICE_INTERFACE, 
-               TTS_METHOD_NEXT_SYNTHESIS);
-
-       if (NULL == msg) { 
-               SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] >>>> Fail to make message for 'start next synthesis'\n"); 
-               return -1;
-       } 
-       
-       dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);        
-       
-       if (!dbus_connection_send(g_conn, msg, NULL)) {
-               SLOG(LOG_ERROR, TAG_TTSD, "[Dbus ERROR] >>>> Fail to send message for 'start next synthesis'\n"); 
-               return -1;
-       }
-
-       dbus_connection_flush(g_conn);
-       dbus_message_unref(msg);
+       SLOG(LOG_DEBUG, tts_tag(), "@@@");
 
        return 0;
 }