Merge "Change uid and gid for service files" into tizen
[platform/core/uifw/tts.git] / server / ttsd_dbus.c
index a089213..c69cd30 100644 (file)
@@ -48,6 +48,7 @@ const char* __ttsd_get_error_code(ttsd_error_e err)
        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";
        }
@@ -59,7 +60,7 @@ int ttsdc_send_hello(int pid, int uid)
 {
 #if 0
        if (NULL == g_conn_sender) {
-               SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Dbus connection is not available");
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Dbus connection is not available");
                return -1;
        }
 
@@ -74,16 +75,16 @@ int ttsdc_send_hello(int pid, int uid)
 
        /* create a message & check for errors */
        msg = dbus_message_new_method_call(
-               service_name, 
-               TTS_CLIENT_SERVICE_OBJECT_PATH, 
-               target_if_name, 
+               service_name,
+               TTS_CLIENT_SERVICE_OBJECT_PATH,
+               target_if_name,
                TTSD_METHOD_HELLO);
 
        if (NULL == msg) {
-               SLOG(LOG_ERROR, get_tag(), "<<<< [Dbus ERROR] Fail to create hello message : uid(%d)", uid);
+               SLOG(LOG_ERROR, tts_tag(), "<<<< [Dbus ERROR] Fail to create hello message : uid(%d)", uid);
                return -1;
        } else {
-               SLOG(LOG_DEBUG, get_tag(), "<<<< [Dbus] Send hello message : uid(%d)", uid);
+               SLOG(LOG_DEBUG, tts_tag(), "<<<< [Dbus] Send hello message : uid(%d)", uid);
        }
 
        dbus_message_append_args(msg, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID);
@@ -97,7 +98,7 @@ int ttsdc_send_hello(int pid, int uid)
        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, get_tag(), "[ERROR] Send error (%s)", err.message);
+               SLOG(LOG_ERROR, tts_tag(), "[ERROR] Send error (%s)", err.message);
                dbus_error_free(&err);
        }
 
@@ -105,14 +106,14 @@ int ttsdc_send_hello(int pid, int uid)
                dbus_message_get_args(result_msg, &err, DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
 
                if (dbus_error_is_set(&err)) {
-                       SLOG(LOG_ERROR, get_tag(), ">>>> [Dbus] Get arguments error (%s)", err.message);
+                       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, get_tag(), ">>>> [Dbus] Result message is NULL. Client is not available");
+               SLOG(LOG_DEBUG, tts_tag(), ">>>> [Dbus] Result message is NULL. Client is not available");
                result = 0;
        }
 
@@ -124,7 +125,7 @@ int ttsdc_send_hello(int pid, int uid)
 int ttsdc_send_message(int pid, int uid, int data, const char *method)
 {
        if (NULL == g_conn_sender) {
-               SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Dbus connection is not available");
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Dbus connection is not available");
                return -1;
        }
 
@@ -137,19 +138,19 @@ int ttsdc_send_message(int pid, int uid, int data, const char *method)
                method);                        /* name of the signal */
 
        if (NULL == msg) {
-               SLOG(LOG_ERROR, get_tag(), "<<<< [Dbus ERROR] Fail to create message : %s", method);
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to create message : %s", method);
                return -1;
        } else {
-               SLOG(LOG_DEBUG, get_tag(), "<<<< [Dbus] Send %s message : uid(%d) data(%d)", method, uid, data);
+               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);
 
        if (1 != dbus_connection_send(g_conn_sender, msg, NULL)) {
-               SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Fail to Send");
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to Send");
                return -1;
        } else {
-               SLOG(LOG_DEBUG, get_tag(), "[Dbus] SUCCESS Send");
+               SLOG(LOG_DEBUG, tts_tag(), "[Dbus] SUCCESS Send");
                dbus_connection_flush(g_conn_sender);
        }
 
@@ -176,7 +177,7 @@ int ttsdc_send_set_state_message(int pid, int uid, int state)
 int ttsdc_send_error_message(int pid, int uid, int uttid, int reason, char* err_msg)
 {
        if (NULL == g_conn_sender) {
-               SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Dbus connection is not available");
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Dbus connection is not available");
                return -1;
        }
 
@@ -189,7 +190,7 @@ int ttsdc_send_error_message(int pid, int uid, int uttid, int reason, char* err_
                TTSD_METHOD_ERROR);             /* name of the signal */
 
        if (NULL == msg) {
-               SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Fail to create error message : uid(%d)", uid);
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to create error message : uid(%d)", uid);
                return -1;
        }
 
@@ -197,15 +198,15 @@ int ttsdc_send_error_message(int pid, int uid, int uttid, int reason, char* err_
                DBUS_TYPE_INT32, &uid,
                DBUS_TYPE_INT32, &uttid,
                DBUS_TYPE_INT32, &reason,
-               DBUS_TYPE_INT32, &err_msg,
+               DBUS_TYPE_STRING, &err_msg,
                DBUS_TYPE_INVALID);
 
        dbus_message_set_no_reply(msg, TRUE);
 
        if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
-               SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] <<<< error message : Out Of Memory !");
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] <<<< error message : Out Of Memory !");
        } else {
-               SLOG(LOG_DEBUG, get_tag(), "<<<< Send error message : uid(%d), reason(%s), uttid(%d), err_msg(%d)",
+               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);
        }
@@ -221,100 +222,159 @@ static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handle
 
        dbus_connection_read_write_dispatch(g_conn_listener, 50);
 
-       DBusMessage* msg = NULL;
-       msg = dbus_connection_pop_message(g_conn_listener);
+       while (1) {
+               DBusMessage* msg = NULL;
+               msg = dbus_connection_pop_message(g_conn_listener);
 
-       if (true != dbus_connection_get_is_connected(g_conn_listener)) {
-               SLOG(LOG_ERROR, get_tag(), "[ERROR] Connection is disconnected");
-               return ECORE_CALLBACK_RENEW;
-       }
+               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;
-       }
+               /* 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, g_service_interface, TTS_METHOD_HELLO)) {
-               ttsd_dbus_server_hello(g_conn_listener, 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, g_service_interface, TTS_METHOD_INITIALIZE)) {
-               ttsd_dbus_server_initialize(g_conn_listener, msg);
+               } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_INITIALIZE)) {
+                       ttsd_dbus_server_initialize(g_conn_listener, 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, g_service_interface, TTS_METHOD_FINALIZE)) {
+                       ttsd_dbus_server_finalize(g_conn_listener, 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, 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, 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, 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, g_service_interface, TTS_METHOD_ADD_QUEUE)) {
-               ttsd_dbus_server_add_text(g_conn_listener, 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, g_service_interface, TTS_METHOD_PLAY)) {
-               ttsd_dbus_server_play(g_conn_listener, 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, g_service_interface, TTS_METHOD_STOP)) {
-               ttsd_dbus_server_stop(g_conn_listener, 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, g_service_interface, TTS_METHOD_PAUSE)) {
-               ttsd_dbus_server_pause(g_conn_listener, 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, 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, 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, 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, g_service_interface, TTS_METHOD_GET_PRIVATE_DATA)) {
+                       ttsd_dbus_server_get_private_data(g_conn_listener, msg);
 
-       } else {
-               /* Invalid method */
-       }
+               } else if (dbus_message_is_method_call(msg, g_service_interface, TTS_METHOD_PLAY_PCM)) {
+                       ttsd_dbus_server_play_pcm(g_conn_listener, msg);
 
-       /* free the message */
-       dbus_message_unref(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, g_service_interface, TTS_METHOD_ADD_PCM)) {
+                       ttsd_dbus_server_add_pcm(g_conn_listener, msg);
+
+               } else {
+                       SLOG(LOG_DEBUG, tts_tag(), "Message is NOT valid");
+                       /* Invalid method */
+               }
+               /* free the message */
+               dbus_message_unref(msg);
+       }
 
        return ECORE_CALLBACK_RENEW;
 }
 
+void __ttsd_dbus_service_free()
+{
+       if (NULL != g_service_name) {
+               free(g_service_name);
+               g_service_name = NULL;
+       }
+
+       if (NULL != g_service_object) {
+               free(g_service_object);
+               g_service_object = NULL;
+       }
+
+       if (NULL != g_service_interface) {
+               free(g_service_interface);
+               g_service_interface = NULL;
+       }
+}
+
+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;
 
        /* Create connection for sender */
-       g_conn_sender = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
+       g_conn_sender = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
        if (dbus_error_is_set(&err)) {
-               SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
                dbus_error_free(&err);
        }
 
        if (NULL == g_conn_sender) {
-               SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Fail to get dbus connection sender");
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to get dbus connection sender");
                return -1;
        }
 
+       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_SESSION, &err);
+       g_conn_listener = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
        if (dbus_error_is_set(&err)) {
-               SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
                dbus_error_free(&err);
+               __ttsd_dbus_connection_free();
                return -1;
        }
 
        if (NULL == g_conn_listener) {
-               SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Fail to get dbus connection");
+               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);
@@ -323,14 +383,43 @@ int ttsd_dbus_open_connection()
                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);
@@ -339,16 +428,23 @@ int ttsd_dbus_open_connection()
        /* 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) {
-               SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] Fail to be primary owner");
+               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, get_tag(), "[Dbus ERROR] Fail to request dbus name : %s", err.message);
+               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='method_call',interface='%s'", g_service_interface);
@@ -358,7 +454,8 @@ int ttsd_dbus_open_connection()
        dbus_connection_flush(g_conn_listener);
 
        if (dbus_error_is_set(&err)) {
-               SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] dbus_bus_add_match() : %s", err.message);
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] dbus_bus_add_match() : %s", err.message);
+               __ttsd_dbus_connection_free();
                return -1;
        }
 
@@ -367,15 +464,18 @@ int ttsd_dbus_open_connection()
 
        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, get_tag(), "[Dbus ERROR] Fail to get fd handler");
+               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to get fd handler");
+               __ttsd_dbus_connection_free();
                return -1;
        }
 
+       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);
 
@@ -384,24 +484,18 @@ int ttsd_dbus_close_connection()
                g_dbus_fd_handler = NULL;
        }
 
-       dbus_bus_release_name(g_conn_listener, g_service_name, &err);
-       if (dbus_error_is_set(&err)) {
-               SLOG(LOG_ERROR, get_tag(), "[Dbus ERROR] dbus_bus_release_name() : %s", err.message);
-               dbus_error_free(&err);
+       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_close(g_conn_sender);
-       dbus_connection_close(g_conn_listener);
-
-       dbus_connection_unref(g_conn_sender);
-       dbus_connection_unref(g_conn_listener);
-
-       g_conn_listener = NULL;
-       g_conn_sender = NULL;
+       __ttsd_dbus_connection_free();
+       __ttsd_dbus_service_free();
 
-       if (NULL != g_service_name)     free(g_service_name);
-       if (NULL != g_service_object)   free(g_service_object);
-       if (NULL != g_service_interface)free(g_service_interface);
+       SLOG(LOG_DEBUG, tts_tag(), "@@@");
 
        return 0;
 }