Fix dlog format argument error
[platform/core/uifw/stt.git] / server / sttd_dbus.c
index 40d47e3..0f68473 100644 (file)
@@ -26,6 +26,7 @@
 
 static DBusConnection* g_conn_sender = NULL;
 static DBusConnection* g_conn_listener = NULL;
+static DBusConnection* g_conn_custom = NULL;
 
 static Ecore_Fd_Handler* g_dbus_fd_handler = NULL;
 
@@ -197,6 +198,50 @@ int sttdc_send_set_state(int uid, int state)
        return 0;
 }
 
+int sttdc_send_speech_status(int uid, int status)
+{
+       int pid = sttd_client_get_pid(uid);
+
+       if (0 > pid) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] pid is NOT valid");
+               return -1;
+       }
+
+       char service_name[64];
+       memset(service_name, 0, 64);
+       snprintf(service_name, 64, "%s%d", STT_CLIENT_SERVICE_NAME, pid);
+
+       char target_if_name[128];
+       snprintf(target_if_name, sizeof(target_if_name), "%s%d", STT_CLIENT_SERVICE_INTERFACE, pid);
+
+       DBusMessage* msg = NULL;
+       msg = dbus_message_new_signal(
+               STT_CLIENT_SERVICE_OBJECT_PATH, /* object name of the signal */
+               target_if_name,                 /* interface name of the signal */
+               STTD_METHOD_SPEECH_STATUS);     /* name of the signal */
+
+       if (NULL == msg) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to create message");
+               return -1;
+       }
+
+       dbus_message_append_args(msg,
+               DBUS_TYPE_INT32, &uid,
+               DBUS_TYPE_INT32, &status,
+               DBUS_TYPE_INVALID);
+
+       if (!dbus_connection_send(g_conn_sender, msg, NULL)) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to send speech status message : Out Of Memory !");
+       } else {
+               SLOG(LOG_DEBUG, TAG_STTD, "<<<< Send speech status message : uid(%d), status(%d)", uid, status);
+               dbus_connection_flush(g_conn_sender);
+       }
+
+       dbus_message_unref(msg);
+
+       return 0;
+}
+
 int sttdc_send_result(int uid, int event, const char** data, int data_count, const char* result_msg)
 {
        int pid = sttd_client_get_pid(uid);
@@ -455,6 +500,25 @@ void __sttd_dbus_service_free()
        }
 }
 
+void __sttd_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;
+       }
+       if (NULL != g_conn_custom) {
+               dbus_connection_close(g_conn_custom);
+               dbus_connection_unref(g_conn_custom);
+               g_conn_custom = NULL;
+       }
+}
+
 int __sttd_get_buxtonkey()
 {
        /* request our name on the bus and check for errors */
@@ -544,11 +608,13 @@ int __sttd_get_buxtonkey()
                bux_cli = NULL;
                bux_layer = NULL;
                bux_val = NULL;
+
+               __sttd_dbus_service_free();
                return STTD_ERROR_OPERATION_FAILED;
        }
 
        char* ret_char = strchr(g_server_service_interface, '-');
-       while(NULL != ret_char) {
+       while (NULL != ret_char) {
                ret_char[0] = '_';
                ret_char = strchr(g_server_service_interface, '-');
        }
@@ -569,6 +635,50 @@ int __sttd_get_buxtonkey()
 
 }
 
+int __sttd_request_custom_dbus_name()
+{
+       DBusError err;
+       dbus_error_init(&err);
+
+       /* Create connection for sender */
+       g_conn_custom = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
+       if (dbus_error_is_set(&err)) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
+               dbus_error_free(&err);
+               __sttd_dbus_connection_free();
+               return STTD_ERROR_OPERATION_FAILED;
+       }
+
+       int ret = dbus_bus_request_name(g_conn_custom, STT_SERVER_CUSTOM_SERVICE_NAME, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
+       if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to be primary owner");
+               __sttd_dbus_connection_free();
+               return STTD_ERROR_OPERATION_FAILED;
+       }
+
+       if (dbus_error_is_set(&err)) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] dbus_bus_request_name() : %s", err.message);
+               dbus_error_free(&err);
+               __sttd_dbus_connection_free();
+               return STTD_ERROR_OPERATION_FAILED;
+       }
+
+       dbus_bus_release_name(g_conn_custom, STT_SERVER_CUSTOM_SERVICE_NAME, &err);
+       if (dbus_error_is_set(&err)) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] dbus_bus_release_name() : %s", err.message);
+               dbus_error_free(&err);
+               __sttd_dbus_connection_free();
+               return STTD_ERROR_OPERATION_FAILED;
+       }
+
+       dbus_connection_close(g_conn_custom);
+       dbus_connection_unref(g_conn_custom);
+       g_conn_custom = NULL;
+
+       SLOG(LOG_DEBUG, TAG_STTD, "==");
+       return 0;
+}
+
 int sttd_dbus_open_connection()
 {
        DBusError err;
@@ -598,6 +708,7 @@ int sttd_dbus_open_connection()
 
        if (NULL == g_conn_listener) {
                SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to get dbus connection");
+               __sttd_dbus_connection_free();
                return STTD_ERROR_OPERATION_FAILED;
        }
 
@@ -605,6 +716,16 @@ int sttd_dbus_open_connection()
        ret = __sttd_get_buxtonkey();
        if (0 != ret) {
                SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to get buxton key");
+               __sttd_dbus_service_free();
+               __sttd_dbus_connection_free();
+               return ret;
+       }
+
+       ret = __sttd_request_custom_dbus_name();
+       if (0 != ret) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to request custom dbus name");
+               __sttd_dbus_service_free();
+               __sttd_dbus_connection_free();
                return ret;
        }
 
@@ -612,12 +733,16 @@ int sttd_dbus_open_connection()
 
        if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
                SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] Fail to be primary owner");
+               __sttd_dbus_service_free();
+               __sttd_dbus_connection_free();
                return STTD_ERROR_OPERATION_FAILED;
        }
 
        if (dbus_error_is_set(&err)) {
                SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] dbus_bus_request_name() : %s", err.message);
                dbus_error_free(&err);
+               __sttd_dbus_service_free();
+               __sttd_dbus_connection_free();
                return STTD_ERROR_OPERATION_FAILED;
        }
 
@@ -632,6 +757,8 @@ int sttd_dbus_open_connection()
        if (dbus_error_is_set(&err)) {
                SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] dbus_bus_add_match() : %s", err.message);
                dbus_error_free(&err);
+               __sttd_dbus_service_free();
+               __sttd_dbus_connection_free();
                return STTD_ERROR_OPERATION_FAILED;
        }
 
@@ -641,6 +768,8 @@ int sttd_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, TAG_STTD, "[Dbus ERROR] Fail to get fd handler");
+               __sttd_dbus_service_free();
+               __sttd_dbus_connection_free();
                return STTD_ERROR_OPERATION_FAILED;
        }
 
@@ -657,22 +786,15 @@ int sttd_dbus_close_connection()
                g_dbus_fd_handler = NULL;
        }
 
-       dbus_bus_release_name(g_conn_listener, g_server_service_name, &err);
-
-       if (dbus_error_is_set(&err)) {
-               SLOG(LOG_ERROR, TAG_STTD, "[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_server_service_name, &err);
+               if (dbus_error_is_set(&err)) {
+                       SLOG(LOG_ERROR, TAG_STTD, "[Dbus ERROR] dbus_bus_release_name() : %s", err.message);
+                       dbus_error_free(&err);
+               }
        }
 
-       dbus_connection_close(g_conn_listener);
-       dbus_connection_close(g_conn_sender);
-
-       dbus_connection_unref(g_conn_sender);
-       dbus_connection_unref(g_conn_listener);
-
-       g_conn_listener = NULL;
-       g_conn_sender = NULL;
-
+       __sttd_dbus_connection_free();
        __sttd_dbus_service_free();
 
        SLOG(LOG_DEBUG, TAG_STTD, "[Server] Close dbus connection");