Remove extern definition from tts_dbus 90/246890/1
authorSuyeon Hwang <stom.hwang@samsung.com>
Mon, 2 Nov 2020 05:20:36 +0000 (14:20 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Thu, 5 Nov 2020 03:33:48 +0000 (12:33 +0900)
Extern keyword makes easy to use functions in other .c files, but also it makes difficult to trace
call stack and breaks boundary between private and public functions.

This patch removes extern definition from tts_dbus.c and implements these functions on tts_core.

Change-Id: I7d0f039757aedd961f94590489182de688e3d3f2
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
client/tts.c
client/tts_core.c
client/tts_core.h
client/tts_dbus.c

index 4b50e781ee6368f5ec1569714b7272cc48fd85f0..e2ce767482721eef7f22ea56f8ccf7720b70a0fa 100644 (file)
@@ -53,15 +53,6 @@ static pthread_mutex_t g_pkgmgr_mutex = PTHREAD_MUTEX_INITIALIZER;
 static Ecore_Thread* g_pkgmgr_thread = NULL;
 static volatile bool g_is_finished_pkgmgr_thread = false;
 
-/* Function definition */
-static Eina_Bool __tts_notify_state_changed(void *data);
-static Eina_Bool __tts_notify_error(void *data);
-int __tts_cb_error(int uid, tts_error_e reason, int utt_id, char* err_msg);
-static void __start_reprepare_thread(void* data, Ecore_Thread* thread);
-static void __end_reprepare_thread(void* data, Ecore_Thread* thread);
-static void __cancel_reprepare_thread(void* data, Ecore_Thread* thread);
-
-
 static int __tts_get_feature_enabled()
 {
        if (0 == g_feature_enabled) {
@@ -471,6 +462,7 @@ int tts_create(tts_h* tts)
                return __tts_convert_config_error_code(ret);
        }
 
+       //TODO: move to tts_core
        ecore_main_loop_thread_safe_call_async(__pkgmgr_thread, *tts);
 
        SLOG(LOG_INFO, TAG_TTSC, "[INFO] call ecore thread for creating pkgmgr thread");
@@ -1428,13 +1420,7 @@ static void __tts_play_async(void *data)
        if (0 != ret) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to play tts : %s", __tts_get_error_code(ret));
 
-               client->reason = ret;
-               client->utt_id = -1;
-
-               if (NULL != client->notify_error_timer) {
-                       ecore_timer_del(client->notify_error_timer);
-               }
-               client->notify_error_timer = ecore_timer_add(0, __tts_notify_error, client->tts);
+               tts_core_notify_error_async(client, -1, ret);
                return;
        }
 
@@ -1629,13 +1615,7 @@ static void __tts_stop_async(void *data)
        if (0 != ret) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to stop tts : %s", __tts_get_error_code(ret));
 
-               client->reason = ret;
-               client->utt_id = -1;
-
-               if (NULL != client->notify_error_timer) {
-                       ecore_timer_del(client->notify_error_timer);
-               }
-               client->notify_error_timer = ecore_timer_add(0, __tts_notify_error, client->tts);
+               tts_core_notify_error_async(client, -1, ret);
                return;
        }
 
@@ -1818,13 +1798,7 @@ static void __tts_pause_async(void *data)
        if (0 != ret) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to pause tts : %s", __tts_get_error_code(ret));
 
-               client->reason = ret;
-               client->utt_id = -1;
-
-               if (NULL != client->notify_error_timer) {
-                       ecore_timer_del(client->notify_error_timer);
-               }
-               client->notify_error_timer = ecore_timer_add(0, __tts_notify_error, client->tts);
+               tts_core_notify_error_async(data, -1, ret);
                return;
        }
 
@@ -2106,305 +2080,8 @@ int tts_get_private_data(tts_h tts, const char* key, char** data)
 }
 
 //LCOV_EXCL_START
-static Eina_Bool __tts_notify_error(void *data)
-{
-       tts_h tts = (tts_h)data;
-
-       tts_client_s* client = tts_client_get(tts);
-
-       /* check handle */
-       if (NULL == client) {
-               SLOG(LOG_WARN, TAG_TTSC, "Fail to notify error msg : A handle is not valid");
-               return EINA_FALSE;
-       }
-
-       SLOG(LOG_DEBUG, TAG_TTSC, "Error data : uttid(%d) reason(%s)", client->utt_id, __tts_get_error_code(client->reason));
-
-       tts_core_notify_error(client, client->utt_id, client->reason);
-
-       client->notify_error_timer = NULL;
-
-       return EINA_FALSE;
-}
-
-static void __start_reprepare_thread(void* data, Ecore_Thread* thread)
-{
-       SLOG(LOG_ERROR, TAG_TTSC, "[DEBUG] start reprepare thread. engine update status(%d)", g_engine_update_status);
-
-       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] client is null");
-               return;
-       }
-
-       int cnt = 0;
-       while (!g_engine_update_status && cnt < 20) {
-               SLOG(LOG_WARN, TAG_TTSC, "[WARNING] wait for starting update");
-               /* Checking handle which can be destroyed on other thread */
-               if (false == tts_client_is_valid(tts)) {
-                       SLOG(LOG_INFO, TAG_TTSC, "[INFO] client is already destroyed");
-                       return;
-               }
-
-               usleep(50000);
-               cnt++;
-
-               /* Checking thread is canceled or not */
-               if (ecore_thread_check(client->thread)) {
-                       SLOG(LOG_WARN, TAG_TTSC, "[WARNING] client thread is canceled. Exit");
-                       return;
-               }
-       }
-
-       SLOG(LOG_ERROR, TAG_TTSC, "[DEBUG] update status(%d)", g_engine_update_status);
-
-       while (g_engine_update_status && (NULL != g_pkgmgr)) {
-               /* Checking handle which can be destroyed on other thread */
-               if (false == tts_client_is_valid(tts)) {
-                       SLOG(LOG_INFO, TAG_TTSC, "[INFO] client is already destroyed");
-                       return;
-               }
-
-               usleep(200000);
-
-               /* Checking thread is canceled or not */
-               if (ecore_thread_check(client->thread)) {
-                       SLOG(LOG_WARN, TAG_TTSC, "[WARNING] client thread is canceled. Exit");
-                       return;
-               }
-       }
-
-       SLOG(LOG_INFO, TAG_TTSC, "[INFO] finish updating. request to prepare");
-
-       if (0 != tts_prepare(client->tts)) {
-               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to prepare");
-       }
-
-       return ;
-}
-
-static void __end_reprepare_thread(void* data, Ecore_Thread* thread)
-{
-       SLOG(LOG_INFO, TAG_TTSC, "[INFO] end reprepare thread");
-
-       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] client is null");
-               return;
-       }
-       client->thread = NULL;
-}
-
-static void __cancel_reprepare_thread(void* data, Ecore_Thread* thread)
-{
-       SLOG(LOG_INFO, TAG_TTSC, "[INFO] cancel reprepare thread");
-
-       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] client is null");
-               return;
-       }
-       client->thread = NULL;
-}
-
-int __tts_cb_error(int uid, tts_error_e reason, int utt_id, char* err_msg)
-{
-       if (-1 == uid) {
-               GList* client_list = NULL;
-               client_list = tts_client_get_client_list();
-
-               if (NULL == client_list) {
-                       SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get client list");
-                       return TTS_ERROR_OPERATION_FAILED;
-               }
-
-               GList *iter = NULL;
-               tts_client_s *data = NULL;
-
-               if (g_list_length(client_list) > 0) {
-                       /* Get a first item */
-                       iter = g_list_first(client_list);
-
-                       while (NULL != iter) {
-                               data = iter->data;
-
-                               data->utt_id = utt_id;
-                               data->reason = reason;
-                               if (NULL != data->err_msg) {
-                                       free(data->err_msg);
-                                       data->err_msg = NULL;
-                               }
-                               if (NULL != err_msg)
-                                       data->err_msg = strdup(err_msg);
-
-                               /* call callback function */
-                               if (NULL != data->error_cb) {
-                                       if (NULL != data->notify_error_timer) {
-                                               ecore_timer_del(data->notify_error_timer);
-                                       }
-                                       data->notify_error_timer = ecore_timer_add(0, __tts_notify_error, data->tts);
-                               } else {
-                                       SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of error ");
-                               }
-
-                               if (TTS_ERROR_SERVICE_RESET == reason) {
-                                       SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Service Reset");
-
-                                       tts_client_s* client = tts_client_get(data->tts);
-                                       if (NULL == client) {
-                                               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
-                                               return TTS_ERROR_INVALID_PARAMETER;
-                                       }
-
-                                       data->current_state = TTS_STATE_CREATED;
-                                       data->reason = 0;
-                                       tts_dbus_remove_match(client->mode);
-                                       if (NULL == data->thread) {
-                                               SLOG(LOG_INFO, TAG_TTSC, "[INFO] ecore thread run: start_reprepare_thread");
-                                               data->thread = ecore_thread_run(__start_reprepare_thread, __end_reprepare_thread, __cancel_reprepare_thread, data->tts);
-                                       }
-                               }
-
-                               /* Next item */
-                               iter = g_list_next(iter);
-                       }
-               }
-
-               g_list_free(client_list);
-       } else {
-               tts_client_s* client = tts_client_get_by_uid(uid);
-
-               if (NULL == client) {
-                       SLOG(LOG_WARN, TAG_TTSC, "[WARNING] A handle is not valid");
-                       return TTS_ERROR_INVALID_PARAMETER;
-               }
-
-               client->utt_id = utt_id;
-               client->reason = reason;
-               if (NULL != client->err_msg) {
-                       free(client->err_msg);
-                       client->err_msg = NULL;
-               }
-               if (NULL != err_msg)
-                       client->err_msg = strdup(err_msg);
-
-               /* call callback function */
-               if (NULL != client->error_cb) {
-                       if (NULL != client->notify_error_timer) {
-                               ecore_timer_del(client->notify_error_timer);
-                       }
-                       client->notify_error_timer = ecore_timer_add(0, __tts_notify_error, client->tts);
-               } else {
-                       SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of error ");
-               }
-
-               if (TTS_ERROR_SERVICE_RESET == reason) {
-                       SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Service Reset");
-
-                       client->current_state = TTS_STATE_CREATED;
-                       client->reason = 0;
-                       tts_dbus_remove_match(client->mode);
-                       if (NULL == client->thread) {
-                               SLOG(LOG_INFO, TAG_TTSC, "[INFO] ecore thread run: start_reprepare_thread");
-                               client->thread = ecore_thread_run(__start_reprepare_thread, __end_reprepare_thread, __cancel_reprepare_thread, client->tts);
-                       }
-               }
-       }
-
-       return 0;
-}
-
-static Eina_Bool __tts_notify_state_changed(void *data)
-{
-       tts_h tts = (tts_h)data;
-
-       tts_client_s* client = tts_client_get(tts);
-
-       /* check handle */
-       if (NULL == client) {
-               SLOG(LOG_WARN, TAG_TTSC, "Fail to notify state changed : A handle is not valid");
-               return EINA_FALSE;
-       }
-
-       tts_core_notify_state_changed(client, client->before_state, client->current_state);
-
-       client->notify_state_timer = NULL;
-       return EINA_FALSE;
-}
-
-int __tts_cb_set_state(int uid, int state)
-{
-       tts_client_s* client = tts_client_get_by_uid(uid);
-       if (NULL == client) {
-               SLOG(LOG_WARN, TAG_TTSC, "[WARNING] The handle is not valid");
-               return -1;
-       }
-
-       tts_state_e state_from_daemon = (tts_state_e)state;
-
-       if (client->current_state == state_from_daemon) {
-               SLOG(LOG_DEBUG, TAG_TTSC, "Current state has already been %d", client->current_state);
-               return 0;
-       }
-
-       if (NULL != client->state_changed_cb) {
-               if (NULL != client->notify_state_timer) {
-                       ecore_timer_del(client->notify_state_timer);
-               }
-               client->notify_state_timer = ecore_timer_add(0, __tts_notify_state_changed, client->tts);
-       } else {
-               SLOG(LOG_WARN, TAG_TTSC, "[WARNING] State changed callback is null");
-       }
-
-       client->before_state = client->current_state;
-       client->current_state = state_from_daemon;
-
-       return 0;
-}
 //LCOV_EXCL_STOP
 
-int __tts_cb_utt_started(int uid, int utt_id)
-{
-       tts_client_s* client = tts_client_get_by_uid(uid);
-
-       if (NULL == client) {
-               SLOG(LOG_WARN, TAG_TTSC, "[WARNING] A handle is not valid");
-               return TTS_ERROR_INVALID_PARAMETER;
-       }
-
-       SLOG(LOG_INFO, TAG_TTSC, "utterance started : utt id(%d) ", utt_id);
-
-       client->utt_id = utt_id;
-       tts_core_notify_utt_started(client, client->utt_id);
-
-       return 0;
-}
-
-int __tts_cb_utt_completed(int uid, int utt_id)
-{
-       tts_client_s* client = tts_client_get_by_uid(uid);
-
-       if (NULL == client) {
-               SLOG(LOG_WARN, TAG_TTSC, "[WARNING] A handle is not valid");
-               return TTS_ERROR_INVALID_PARAMETER;
-       }
-
-       SLOG(LOG_INFO, TAG_TTSC, "utterance completed : uttid(%d) ", utt_id);
-
-       client->utt_id = utt_id;
-
-       /* call callback function */
-       tts_core_notify_utt_completeted(client, client->utt_id);
-
-       return 0;
-}
-
 int tts_set_state_changed_cb(tts_h tts, tts_state_changed_cb callback, void* user_data)
 {
        if (0 != __tts_get_feature_enabled()) {
index cff606ecd159655b16189d515ffa4302184e7bee..f22a4793f3f35af6ece113a8a3aebca0fb2fffcb 100644 (file)
@@ -75,6 +75,30 @@ static char* __get_engine_appid(int mode) {
        return appid;
 }
 
+static inline void __client_error_cb(tts_client_s* client, int utt_id, tts_error_e reason)
+{
+       if (NULL != client->error_cb) {
+               SLOG(LOG_DEBUG, TAG_TTSC, "Notify error");
+               tts_client_use_callback(client);
+               client->error_cb(client->tts, utt_id, reason, client->error_user_data);
+               tts_client_not_use_callback(client);
+       } else {
+               SLOG(LOG_WARN, TAG_TTSC, "No registered callback(error)");
+       }
+}
+
+static inline void __client_state_changed_cb(tts_client_s* client, tts_state_e before_state, tts_state_e current_state)
+{
+       if (NULL != client->state_changed_cb) {
+               SLOG(LOG_DEBUG, TAG_TTSC, "Notify state changed");
+               tts_client_use_callback(client);
+               client->state_changed_cb(client->tts, before_state, current_state, client->state_changed_user_data);
+               tts_client_not_use_callback(client);
+       } else {
+               SLOG(LOG_WARN, TAG_TTSC, "No registered callback(state_changed)");
+       }
+}
+
 static Eina_Bool __notify_error_timer_cb(void *data)
 {
        tts_client_s* client = (tts_client_s*)data;
@@ -82,11 +106,10 @@ static Eina_Bool __notify_error_timer_cb(void *data)
        if (NULL == client || false == tts_client_is_valid(client->tts)) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Handle is not valid.");
        } else {
-               tts_core_notify_error(client, client->utt_id, client->reason);
+               __client_error_cb(client, client->utt_id, client->reason);
+               client->notify_error_timer = NULL;
        }
 
-       client->notify_error_timer = NULL;
-
        return EINA_FALSE;
 }
 
@@ -97,10 +120,10 @@ static Eina_Bool __notify_state_timer_cb(void *data)
        if (NULL == client || false == tts_client_is_valid(client->tts)) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Handle is not valid.");
        } else {
-               tts_core_notify_state_changed(client, client->before_state, client->current_state);
+               __client_state_changed_cb(client, client->before_state, client->current_state);
+               client->notify_state_timer = NULL;
        }
 
-       client->notify_state_timer = NULL;
        return EINA_FALSE;
 }
 
@@ -185,7 +208,7 @@ static void __start_reprepare_thread(void* data, Ecore_Thread* thread)
                }
        }
 
-       SLOG(LOG_ERROR, TAG_TTSC, "[DEBUG] update status(%d)", g_engine_update_status);
+       SLOG(LOG_INFO, TAG_TTSC, "[DEBUG] update status(%d)", g_engine_update_status);
 
        while (g_engine_update_status && (NULL != g_pkgmgr)) {
                /* Checking handle which can be destroyed on other thread */
@@ -240,6 +263,16 @@ static void __cancel_reprepare_thread(void* data, Ecore_Thread* thread)
        client->thread = NULL;
 }
 
+static inline void __run_client_reprepare_thread(tts_client_s* client)
+{
+       if (NULL == client->thread) {
+               SLOG(LOG_INFO, TAG_TTSC, "[INFO] ecore thread run: start_reprepare_thread");
+               client->thread = ecore_thread_run(__start_reprepare_thread, __end_reprepare_thread, __cancel_reprepare_thread, client);
+       } else {
+               SLOG(LOG_INFO, TAG_TTSC, "[INFO] Reprepare thread is already running");
+       }
+}
+
 static int __send_hello_msg(tts_client_s* client)
 {
        if (NULL == client) {
@@ -263,10 +296,7 @@ static int __send_hello_msg(tts_client_s* client)
                if (g_engine_update_status) {
                        /* suyeon wait engine update */
                        SLOG(LOG_INFO, TAG_TTSC, "[INFO] cannot prepare due to engine update");
-                       if (NULL == client->thread) {
-                               SLOG(LOG_INFO, TAG_TTSC, "[INFO] ecore thread run: start_reprepare_thread");
-                               client->thread = ecore_thread_run(__start_reprepare_thread, __end_reprepare_thread, __cancel_reprepare_thread, client);
-                       }
+                       __run_client_reprepare_thread(client);
                        return TTS_ERROR_INVALID_STATE;
                }
        }
@@ -356,10 +386,7 @@ static Eina_Bool __prepare_sync_cb(tts_client_s* client)
                /* check whether engine is updating or not */
                if (g_engine_update_status) {
                        SLOG(LOG_ERROR, TAG_TTSC, "[DEBUG] cannot prepare due to engine update");
-                       if (NULL == client->thread) {
-                               SLOG(LOG_INFO, TAG_TTSC, "[INFO] ecore thread run: start_reprepare_thread");
-                               client->thread = ecore_thread_run(__start_reprepare_thread, __end_reprepare_thread, __cancel_reprepare_thread, client);
-                       }
+                       __run_client_reprepare_thread(client);
                        return EINA_FALSE;
                }
        }
@@ -381,14 +408,7 @@ static Eina_Bool __prepare_sync_cb(tts_client_s* client)
 
        if (TTS_ERROR_ENGINE_NOT_FOUND == ret || TTS_ERROR_PERMISSION_DENIED == ret) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to initialize : %s", __tts_get_error_code(ret));
-
-               client->reason = ret;
-               client->utt_id = -1;
-
-               if (NULL != client->notify_error_timer) {
-                       ecore_timer_del(client->notify_error_timer);
-               }
-               client->notify_error_timer = ecore_timer_add(0, __notify_error_timer_cb, (void*)client);
+               tts_core_notify_error_async(client, -1, ret);
 
                return EINA_FALSE;
        } else if (TTS_ERROR_NONE != ret) {
@@ -409,13 +429,9 @@ static Eina_Bool __prepare_sync_cb(tts_client_s* client)
 
        client->before_state = client->current_state;
        client->current_state = TTS_STATE_READY;
-
-       if (0 != tts_core_notify_state_changed(client, client->before_state, client->current_state)) {
-               return EINA_FALSE;
-       }
+       __client_state_changed_cb(client, client->before_state, client->current_state);
 
        SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
-
        return EINA_FALSE;
 }
 
@@ -430,14 +446,31 @@ int tts_core_notify_state_changed(tts_client_s* client, tts_state_e before_state
        }
 
        SLOG(LOG_DEBUG, TAG_TTSC, "State changed data : before_state(%d) curret_state(%d)", before_state, current_state);
+       __client_state_changed_cb(client, before_state, current_state);
+
+       return TTS_ERROR_NONE;
+}
+
+int tts_core_notify_state_changed_async(tts_client_s* client, tts_state_e before_state, tts_state_e current_state)
+{
+       /* check handle */
+       if (NULL == client || false == tts_client_is_valid(client->tts)) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Handle is not valid.");
+               return TTS_ERROR_INVALID_PARAMETER;
+       }
+
+       SLOG(LOG_DEBUG, TAG_TTSC, "State changed data : before_state(%d) curret_state(%d)", before_state, current_state);
+       client->before_state = before_state;
+       client->current_state = current_state;
 
        if (NULL != client->state_changed_cb) {
-               SLOG(LOG_DEBUG, TAG_TTSC, "Notify state changed");
-               tts_client_use_callback(client);
-               client->state_changed_cb(client->tts, before_state, current_state, client->state_changed_user_data);
-               tts_client_not_use_callback(client);
+               SLOG(LOG_DEBUG, TAG_TTSC, "Notify state changed asynchronously");
+               if (NULL != client->notify_state_timer) {
+                       ecore_timer_del(client->notify_state_timer);
+               }
+               client->notify_state_timer = ecore_timer_add(0, __notify_state_timer_cb, (void*)client);
        } else {
-               SLOG(LOG_WARN, TAG_TTSC, "No registered callback(state_changed)");
+               SLOG(LOG_WARN, TAG_TTSC, "No registered callback(state changed)");
        }
 
        return TTS_ERROR_NONE;
@@ -496,12 +529,29 @@ int tts_core_notify_error(tts_client_s* client, int utt_id, tts_error_e reason)
        }
 
        SLOG(LOG_DEBUG, TAG_TTSC, "Error data : utt_id(%d) reason(%s)", utt_id, __tts_get_error_code(reason));
+       __client_error_cb(client, utt_id, reason);
+
+       return TTS_ERROR_NONE;
+}
+
+int tts_core_notify_error_async(tts_client_s* client, int utt_id, tts_error_e reason)
+{
+       /* check handle */
+       if (NULL == client || false == tts_client_is_valid(client->tts)) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Handle is not valid.");
+               return TTS_ERROR_INVALID_PARAMETER;
+       }
+
+       SLOG(LOG_DEBUG, TAG_TTSC, "Error data : utt_id(%d) reason(%s)", utt_id, __tts_get_error_code(reason));
+       client->utt_id = utt_id;
+       client->reason = reason;
 
        if (NULL != client->error_cb) {
-               SLOG(LOG_DEBUG, TAG_TTSC, "Notify error");
-               tts_client_use_callback(client);
-               client->error_cb(client->tts, utt_id, reason, client->error_user_data);
-               tts_client_not_use_callback(client);
+               SLOG(LOG_DEBUG, TAG_TTSC, "Notify error asynchronously");
+               if (NULL != client->notify_error_timer) {
+                       ecore_timer_del(client->notify_error_timer);
+               }
+               client->notify_error_timer = ecore_timer_add(0, __notify_error_timer_cb, (void*)client);
        } else {
                SLOG(LOG_WARN, TAG_TTSC, "No registered callback(error)");
        }
@@ -600,26 +650,19 @@ int tts_core_receive_hello(int uid, int ret, int credential_needed)
                return TTS_ERROR_OPERATION_FAILED;
        }
 
-       if (client->hello_timer) {
-               ecore_timer_del(client->hello_timer);
-               client->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 (client->hello_timer) {
+               ecore_timer_del(client->hello_timer);
+               client->hello_timer = NULL;
+       }
+
        if (TTS_ERROR_ENGINE_NOT_FOUND == ret || TTS_ERROR_PERMISSION_DENIED == ret) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to initialize : %s", __tts_get_error_code(ret));
-
-               client->reason = ret;
-               client->utt_id = -1;
-
-               if (NULL != client->notify_error_timer) {
-                       ecore_timer_del(client->notify_error_timer);
-               }
-               client->notify_error_timer = ecore_timer_add(0, __notify_error_timer_cb, (void*)client);
+               tts_core_notify_error_async(client, -1, ret);
 
                return TTS_ERROR_OPERATION_FAILED;
        } else if (TTS_ERROR_NONE != ret) {
@@ -633,13 +676,7 @@ int tts_core_receive_hello(int uid, int ret, int credential_needed)
                }
        }
 
-       client->before_state = client->current_state;
-       client->current_state = TTS_STATE_READY;
-
-       if (NULL != client->notify_state_timer) {
-               ecore_timer_del(client->notify_state_timer);
-       }
-       client->notify_state_timer = ecore_timer_add(0, __notify_state_timer_cb, client);
+       tts_core_notify_state_changed_async(client, client->current_state, TTS_STATE_READY);
 
        SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
        return TTS_ERROR_NONE;
@@ -770,8 +807,22 @@ int tts_core_unprepare(tts_client_s* client, bool is_screen_reader_on)
 
        client->before_state = client->current_state;
        client->current_state = TTS_STATE_CREATED;
+       __client_state_changed_cb(client, client->before_state, client->current_state);
 
-       tts_core_notify_state_changed(client, client->before_state, client->current_state);
+       return TTS_ERROR_NONE;
+}
+
+int tts_core_reprepare(tts_client_s* client)
+{
+       /* check handle */
+       if (NULL == client || false == tts_client_is_valid(client->tts)) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Handle is not valid.");
+               return TTS_ERROR_INVALID_PARAMETER;
+       }
+
+       client->current_state = TTS_STATE_CREATED;
+       client->reason = 0;
+       __run_client_reprepare_thread(client);
 
        return TTS_ERROR_NONE;
 }
index 9c8b7d28f18f44caebf7270b8abbded19affe1e0..3d294ddd3d7e9a4abef034133665b50d73400799 100644 (file)
@@ -23,9 +23,11 @@ extern "C" {
 
 // common function
 int tts_core_notify_state_changed(tts_client_s* client, tts_state_e before_state, tts_state_e current_state);
+int tts_core_notify_state_changed_async(tts_client_s* client, tts_state_e before_state, tts_state_e current_state);
 int tts_core_notify_utt_started(tts_client_s* client, int utt_id);
 int tts_core_notify_utt_completeted(tts_client_s* client, int utt_id);
 int tts_core_notify_error(tts_client_s* client, int utt_id, tts_error_e reason);
+int tts_core_notify_error_async(tts_client_s* client, int utt_id, tts_error_e reason);
 int tts_core_notify_default_voice_changed(tts_client_s* client, const char* before_lang, int before_voice_type, const char* language, int voice_type);
 int tts_core_notify_engine_changed(tts_client_s* client, const char* engine_id, const char* language, int voice_type, bool need_credential);
 int tts_core_notify_supported_voice(tts_client_s* client, const char* language, int voice_type);
@@ -37,6 +39,7 @@ const char* tts_core_get_engine_name();
 int tts_core_prepare(tts_client_s* client);
 int tts_core_prepare_sync(tts_client_s* client);
 int tts_core_unprepare(tts_client_s* client, bool is_screen_reader_on);
+int tts_core_reprepare(tts_client_s* client);
 
 // called by tts_dbus.c
 int tts_core_receive_hello(int uid, int ret, int credential_needed);
index 1dcd55d4f96a9947dabf0e8375b38e0daeb163c6..f068c9d390550d1145db52f0e473a41b966cb631 100644 (file)
@@ -33,14 +33,130 @@ static DBusConnection* g_conn_listener = NULL;
 static Ecore_Fd_Handler* g_dbus_fd_handler = NULL;
 
 
-extern int __tts_cb_error(int uid, tts_error_e reason, int utt_id, char *err_msg);
+static int __tts_cb_error(int uid, tts_error_e reason, int utt_id, char* err_msg)
+{
+       if (-1 == uid) {
+               GList* client_list = NULL;
+               client_list = tts_client_get_client_list();
+
+               if (NULL == client_list) {
+                       SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get client list");
+                       return TTS_ERROR_OPERATION_FAILED;
+               }
+
+               GList *iter = NULL;
+               tts_client_s *data = NULL;
+
+               if (g_list_length(client_list) > 0) {
+                       /* Get a first item */
+                       iter = g_list_first(client_list);
+
+                       while (NULL != iter) {
+                               data = iter->data;
+
+                               if (NULL != data->err_msg) {
+                                       free(data->err_msg);
+                                       data->err_msg = NULL;
+                               }
+                               if (NULL != err_msg)
+                                       data->err_msg = strdup(err_msg);
+
+                               /* call callback function */
+                               tts_core_notify_error_async(data, utt_id, reason);
+
+                               if (TTS_ERROR_SERVICE_RESET == reason) {
+                                       SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Service Reset");
+
+                                       tts_client_s* client = tts_client_get(data->tts);
+                                       if (NULL == client) {
+                                               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid");
+                                               return TTS_ERROR_INVALID_PARAMETER;
+                                       }
+
+                                       tts_core_reprepare(data);
+                               }
+
+                               /* Next item */
+                               iter = g_list_next(iter);
+                       }
+               }
+
+               g_list_free(client_list);
+       } else {
+               tts_client_s* client = tts_client_get_by_uid(uid);
+
+               if (NULL == client) {
+                       SLOG(LOG_WARN, TAG_TTSC, "[WARNING] A handle is not valid");
+                       return TTS_ERROR_INVALID_PARAMETER;
+               }
+
+               if (NULL != client->err_msg) {
+                       free(client->err_msg);
+                       client->err_msg = NULL;
+               }
+               if (NULL != err_msg)
+                       client->err_msg = strdup(err_msg);
+
+               tts_core_notify_error_async(client, utt_id, reason);
+
+               if (TTS_ERROR_SERVICE_RESET == reason) {
+                       SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Service Reset");
+
+                       tts_core_reprepare(client);
+               }
+       }
+
+       return 0;
+}
+
+static int __tts_cb_set_state(int uid, int state)
+{
+       tts_client_s* client = tts_client_get_by_uid(uid);
+       if (NULL == client) {
+               SLOG(LOG_WARN, TAG_TTSC, "[WARNING] The handle is not valid");
+               return -1;
+       }
+
+       tts_state_e state_from_daemon = (tts_state_e)state;
+
+       if (client->current_state == state_from_daemon) {
+               SLOG(LOG_DEBUG, TAG_TTSC, "Current state has already been %d", client->current_state);
+               return 0;
+       }
+
+       tts_core_notify_state_changed_async(client, client->current_state, state_from_daemon);
+       return 0;
+}
+
+static int __tts_cb_utt_started(int uid, int utt_id)
+{
+       tts_client_s* client = tts_client_get_by_uid(uid);
+       if (NULL == client) {
+               SLOG(LOG_WARN, TAG_TTSC, "[WARNING] A handle is not valid");
+               return TTS_ERROR_INVALID_PARAMETER;
+       }
 
-extern int __tts_cb_set_state(int uid, int state);
+       client->utt_id = utt_id;
+       tts_core_notify_utt_started(client, client->utt_id);
 
-extern int __tts_cb_utt_started(int uid, int utt_id);
+       return 0;
+}
 
-extern int __tts_cb_utt_completed(int uid, int utt_id);
+static int __tts_cb_utt_completed(int uid, int utt_id)
+{
+       tts_client_s* client = tts_client_get_by_uid(uid);
+       if (NULL == client) {
+               SLOG(LOG_WARN, TAG_TTSC, "[WARNING] A handle is not valid");
+               return TTS_ERROR_INVALID_PARAMETER;
+       }
+
+       SLOG(LOG_INFO, TAG_TTSC, "utterance completed : uttid(%d) ", utt_id);
 
+       client->utt_id = utt_id;
+       tts_core_notify_utt_completeted(client, client->utt_id);
+
+       return 0;
+}
 
 static int __tts_dbus_add_match(int uid)
 {