Add to set false as exit when dbus disconnects
[platform/core/uifw/stt.git] / client / stt_dbus.c
index f6ddbf0..2f3292a 100644 (file)
@@ -648,6 +648,20 @@ static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handle
        return ECORE_CALLBACK_RENEW;
 }
 
+static void __stt_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 stt_dbus_open_connection()
 {
        if (NULL != g_conn_sender && NULL != g_conn_listener) {
@@ -687,9 +701,12 @@ int stt_dbus_open_connection()
 
        if (NULL == g_conn_listener) {
                SLOG(LOG_ERROR, TAG_STTC, "Fail to get dbus connection");
+               __stt_dbus_connection_free();
                return STT_ERROR_OPERATION_FAILED;
        }
 
+       dbus_connection_set_exit_on_disconnect(g_conn_listener, false);
+
        int pid = getpid();
 
        char service_name[64];
@@ -716,12 +733,14 @@ int stt_dbus_open_connection()
        if (dbus_error_is_set(&err)) {
                SLOG(LOG_ERROR, TAG_STTC, "Match Error (%s)", err.message);
                dbus_error_free(&err);
+               __stt_dbus_connection_free();
                return STT_ERROR_OPERATION_FAILED;
        }
 
        int fd = 0;
        if (true != dbus_connection_get_unix_fd(g_conn_listener, &fd)) {
                SLOG(LOG_ERROR, TAG_STTC, "Fail to get fd from dbus");
+               __stt_dbus_connection_free();
                return STT_ERROR_OPERATION_FAILED;
        } else {
                SLOG(LOG_DEBUG, TAG_STTC, "Get fd from dbus : %d", fd);
@@ -730,6 +749,7 @@ int stt_dbus_open_connection()
        g_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_fd_handler) {
                SLOG(LOG_ERROR, TAG_STTC, "fail to get fd handler from ecore");
+               __stt_dbus_connection_free();
                return STT_ERROR_OPERATION_FAILED;
        }
 
@@ -758,15 +778,7 @@ int stt_dbus_close_connection()
                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_sender = NULL;
-       g_conn_listener = NULL;
-
+       __stt_dbus_connection_free();
        __stt_dbus_service_free();
 
        return 0;
@@ -774,6 +786,18 @@ int stt_dbus_close_connection()
 
 int stt_dbus_reconnect()
 {
+       if (!g_conn_sender || !g_conn_listener) {
+               stt_dbus_close_connection();
+
+               if (0 != stt_dbus_open_connection()) {
+                       SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to reconnect");
+                       return -1;
+               }
+
+               SLOG(LOG_DEBUG, TAG_STTC, "[DBUS] Reconnect");
+               return 0;
+       }
+
        bool sender_connected = dbus_connection_get_is_connected(g_conn_sender);
        bool listener_connected = dbus_connection_get_is_connected(g_conn_listener);
        SLOG(LOG_DEBUG, TAG_STTC, "[DBUS] Sender(%s) Listener(%s)",
@@ -2117,3 +2141,118 @@ int stt_dbus_request_cancel(int uid)
        return result;
 #endif
 }
+
+int stt_dbus_request_start_file(int uid, const char* lang, const char* type, int silence, const char* appid, const char* credential, const char* filepath, stt_audio_type_e audio_type, int sample_rate)
+{
+       if (NULL == lang || NULL == type || NULL == appid) {
+               SLOG(LOG_ERROR, TAG_STTC, "Input parameter is NULL");
+               return STT_ERROR_INVALID_PARAMETER;
+       }
+
+       DBusMessage* msg;
+
+       /* create a signal & check for errors */
+       msg = dbus_message_new_method_call(
+               g_server_service_name,
+               g_server_service_object,
+               g_server_service_interface,
+               STT_METHOD_START_FILE);
+
+       if (NULL == msg) {
+               SLOG(LOG_ERROR, TAG_STTC, ">>>> stt start file : Fail to make message");
+               return STT_ERROR_OPERATION_FAILED;
+       } else {
+               SLOG(LOG_DEBUG, TAG_STTC, ">>>> stt start file : uid(%d), language(%s), type(%s), appid(%s), filepath(%s), audio_type(%d), sample_rate(%d)", uid, lang, type, appid, filepath, audio_type, sample_rate);
+       }
+
+       char *temp = NULL;
+       if (NULL == credential) {
+               temp = strdup("NULL");
+       } else {
+               temp = strdup(credential);
+       }
+
+       dbus_message_append_args(msg,
+               DBUS_TYPE_INT32, &uid,
+               DBUS_TYPE_STRING, &lang,
+               DBUS_TYPE_STRING, &type,
+               DBUS_TYPE_INT32, &silence,
+               DBUS_TYPE_STRING, &appid,
+               DBUS_TYPE_STRING, &temp,
+               DBUS_TYPE_STRING, &filepath,
+               DBUS_TYPE_INT32, &audio_type,
+               DBUS_TYPE_INT32, &sample_rate,
+               DBUS_TYPE_INVALID);
+       if (g_conn_sender) {
+               dbus_message_set_no_reply(msg, TRUE);
+
+               if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
+                       SLOG(LOG_ERROR, TAG_STTC, "[Dbus ERROR] <<<< stt start message : Out Of Memory !");
+                       if (NULL != temp) {
+                               free(temp);
+                               temp = NULL;
+                       }
+                       return STT_ERROR_OUT_OF_MEMORY;
+               } else {
+                       dbus_connection_flush(g_conn_sender);
+               }
+
+               dbus_message_unref(msg);
+
+       } else {
+               SLOG(LOG_WARN, TAG_STTC, "[WARN] dbus connection handle is null (%p)", g_conn_sender);
+               if (NULL != temp) {
+                               free(temp);
+                               temp = NULL;
+               }
+               return STT_ERROR_OPERATION_FAILED;
+       }
+
+       if (NULL != temp) {
+                       free(temp);
+                       temp = NULL;
+       }
+       return 0;
+}
+
+int stt_dbus_request_cancel_file(int uid)
+{
+       DBusMessage* msg;
+
+       /* create a signal & check for errors */
+       msg = dbus_message_new_method_call(
+               g_server_service_name,
+               g_server_service_object,
+               g_server_service_interface,
+               STT_METHOD_CANCEL_FILE);
+
+       if (NULL == msg) {
+               SLOG(LOG_ERROR, TAG_STTC, ">>>> stt cancel file : Fail to make message");
+               return STT_ERROR_OPERATION_FAILED;
+       } else {
+               SLOG(LOG_DEBUG, TAG_STTC, ">>>> stt cancel file : uid(%d)", uid);
+       }
+
+       dbus_message_append_args(msg,
+               DBUS_TYPE_INT32, &uid,
+               DBUS_TYPE_INVALID);
+
+       if (g_conn_sender) {
+               dbus_message_set_no_reply(msg, TRUE);
+
+               if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
+                       SLOG(LOG_ERROR, TAG_STTC, "[Dbus ERROR] <<<< stt stop message : Out Of Memory !");
+                       return STT_ERROR_OUT_OF_MEMORY;
+               } else {
+                       dbus_connection_flush(g_conn_sender);
+               }
+
+               dbus_message_unref(msg);
+       } else {
+               SLOG(LOG_WARN, TAG_STTC, "[WARN] dbus connection handle is null (%p)", g_conn_sender);
+               return STT_ERROR_OPERATION_FAILED;
+       }
+
+       return 0;
+}
+