Update hello protocol
[platform/core/uifw/tts.git] / client / tts.c
index cf37dc6..b7176fc 100644 (file)
@@ -41,6 +41,7 @@ static int g_max_text_size = -1;
 
 static Ecore_Timer* g_check_state_timer = NULL;
 
+static Ecore_Timer* g_hello_timer;
 
 /* for repetition */
 static char* g_language = NULL;
@@ -746,7 +747,7 @@ static Eina_Bool __tts_connect_daemon(void *data)
        }
 
        /* Send hello */
-       if (0 != tts_dbus_request_hello(client->uid)) {
+       if (0 != tts_dbus_request_hello_sync(client->uid)) {
                return EINA_TRUE;
        }
 
@@ -822,6 +823,97 @@ static Eina_Bool __tts_connect_daemon(void *data)
        return EINA_FALSE;
 }
 
+int __tts_cb_hello(int uid, int ret, int credential_needed)
+{
+       tts_client_s* client = tts_client_get_by_uid(uid);
+       if (NULL == client) {
+               SLOG(LOG_ERROR, TAG_TTSC, "Fail to get TTS client or ignore this uid(%d)", uid);
+               return TTS_ERROR_OPERATION_FAILED;
+       }
+
+       if (g_hello_timer) {
+               ecore_timer_del(g_hello_timer);
+               g_hello_timer = NULL;
+       }
+
+       if (TTS_STATE_READY == client->current_state) {
+               SLOG(LOG_INFO, TAG_TTSC, "[INFO] tts client is already READY");
+               return TTS_ERROR_NONE;
+       }
+
+       if (TTS_ERROR_ENGINE_NOT_FOUND == ret) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to initialize : %s", __tts_get_error_code(ret));
+
+               client->reason = TTS_ERROR_ENGINE_NOT_FOUND;
+               client->utt_id = -1;
+
+               ecore_timer_add(0, __tts_notify_error, (void*)client->tts);
+               return TTS_ERROR_OPERATION_FAILED;
+
+       } else if (TTS_ERROR_PERMISSION_DENIED == ret) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to initialize : %s", __tts_get_error_code(ret));
+
+               client->reason = TTS_ERROR_PERMISSION_DENIED;
+               client->utt_id = -1;
+
+               ecore_timer_add(0, __tts_notify_error, (void*)client->tts);
+               return TTS_ERROR_PERMISSION_DENIED;
+
+       } else if (TTS_ERROR_NONE != ret) {
+               SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Fail to connection. Retry to connect : %s", __tts_get_error_code(ret));
+               return TTS_ERROR_OPERATION_FAILED;
+
+       } else {
+               /* success to connect tts-daemon */
+               client->credential_needed = credential_needed;
+               SLOG(LOG_ERROR, TAG_TTSC, "Supported options : credential(%s)", credential_needed ? "need" : "no need");
+       }
+
+       client->before_state = client->current_state;
+       client->current_state = TTS_STATE_READY;
+
+       ecore_timer_add(0.0, __tts_notify_state_changed, client->tts);
+
+       SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
+       return TTS_ERROR_NONE;
+}
+
+static Eina_Bool __send_hello(void *data)
+{
+       tts_h tts = (tts_h)data;
+       tts_client_s* client = tts_client_get(tts);
+
+       /* check handle */
+       if (NULL == client) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
+               g_hello_timer = NULL;
+               return EINA_FALSE;
+       }
+
+       /* check state */
+       if (client->current_state == TTS_STATE_READY) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[INFO] TTS client has been already connected to tts service"); //LCOV_EXCL_LINE
+               SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
+               g_hello_timer = NULL;
+               return EINA_FALSE;
+       }
+
+       /* Send hello */
+       int ret = tts_dbus_request_hello(client->uid);
+       if (0 != ret)  {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request hello !!"); //LCOV_EXCL_LINE
+       } else {
+               SLOG(LOG_ERROR, TAG_TTSC, "@@@ Send Hello");
+       }
+
+       if (!g_hello_timer) {
+               SLOG(LOG_ERROR, TAG_TTSC, "@@@ Call checking Hello timer callback");
+               g_hello_timer = ecore_timer_add(0.5, __send_hello, tts);
+               return EINA_FALSE;
+       }
+       return EINA_TRUE;
+}
+
 int tts_prepare(tts_h tts)
 {
        if (0 != __tts_get_feature_enabled()) {
@@ -846,13 +938,12 @@ int tts_prepare(tts_h tts)
                return TTS_ERROR_INVALID_STATE;
        }
 
-       ecore_thread_main_loop_begin();
-       if (client->conn_timer) {
-               ecore_timer_del(client->conn_timer);
-               client->conn_timer = NULL;
+       if (NULL == g_hello_timer) {
+               SLOG(LOG_ERROR, TAG_TTSC, "@@@ Call checking Hello timer callback");
+               ecore_thread_main_loop_begin();
+               ecore_timer_add(0.0, __send_hello, (void*)tts);
+               ecore_thread_main_loop_end();
        }
-       client->conn_timer = ecore_timer_add(0.02, __tts_connect_daemon, (void*)tts);
-       ecore_thread_main_loop_end();
 
        SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
 
@@ -2176,6 +2267,7 @@ int __tts_cb_error(int uid, tts_error_e reason, int utt_id, char* err_msg)
                                        SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Service Reset");
 
                                        data->current_state = TTS_STATE_CREATED;
+                                       data->reason = 0;
                                        ecore_thread_run(__start_reprepare_thread, __end_reprepare_thread, NULL, data);
                                }
 
@@ -2211,7 +2303,7 @@ int __tts_cb_error(int uid, tts_error_e reason, int utt_id, char* err_msg)
                        SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Service Reset");
 
                        client->current_state = TTS_STATE_CREATED;
-
+                       client->reason = 0;
                        ecore_thread_run(__start_reprepare_thread, __end_reprepare_thread, NULL, client);
                }
        }