Remove duplicated codes 37/276937/4
authorSuyeon Hwang <stom.hwang@samsung.com>
Tue, 28 Jun 2022 04:46:04 +0000 (13:46 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Wed, 29 Jun 2022 04:42:28 +0000 (13:42 +0900)
- Issue:
The codes for checking preconditions are repeated in every functions. And also,
the codes for clearing list are repeated.

- Solution:
This patch makes new inline functions for simplify the precondtion
checking code. Using theses new functions, we can remove many duplicated
precondition checking code.
And also, this patch makes new function for clearing voice information list.
Through this function, we can remove duplicated list clearing code.

Change-Id: I611c798238b76b3e2d0b50942dc3b3c5d0eb2372
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
server/ttsd_engine_agent.c

index 7376b4e..c59a261 100644 (file)
@@ -91,9 +91,47 @@ static const char* __ttsd_get_engine_error_code(ttse_error_e err)
        }
 }
 
+inline static bool __is_agent_initialized()
+{
+       if (false == g_agent_init) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
+               return false;
+       }
+
+       if (NULL == g_engine_info) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] No engine info");
+               return false;
+       }
+
+       return true;
+}
+
+inline static bool __is_engine_loaded()
+{
+       if (false == g_engine_info->is_loaded) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] No loaded engine");
+               return false;
+       }
+
+       return true;
+}
+
+inline static int __check_engine_initialized_and_loaded()
+{
+       if (false == __is_agent_initialized()) {
+               return TTSD_ERROR_INVALID_STATE;
+       }
+
+       if (false == __is_engine_loaded()) {
+               return TTSD_ERROR_ENGINE_NOT_FOUND;
+       }
+
+       return TTSD_ERROR_NONE;
+}
+
 int ttsd_engine_agent_init()
 {
-       if (true == g_agent_init) {
+       if (__is_agent_initialized()) {
                SLOG(LOG_WARN, tts_tag(), "[Engine Agent] Already initialized");
                return TTSD_ERROR_NONE;
        }
@@ -135,13 +173,13 @@ int ttsd_engine_agent_init()
 
 int ttsd_engine_agent_release()
 {
-       if (false == g_agent_init) {
+       if (false == __is_agent_initialized()) {
                SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
                return TTSD_ERROR_INVALID_STATE;
        }
 
        /* unload current engine */
-       if (g_engine_info->is_loaded) {
+       if (__is_engine_loaded()) {
                ttsd_engine_agent_unload_current_engine();
        }
 
@@ -210,38 +248,42 @@ static bool __set_voice_info_cb(const char* language, int type, void* user_data)
        return true;
 }
 
-static int __update_voice_list()
+static void __clear_voice_list()
 {
-       if (false == g_agent_init) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
-               return TTSD_ERROR_INVALID_STATE;
+       if (NULL == g_cur_voices) {
+               return;
        }
 
-       /* Get voice list */
-       if (NULL != g_cur_voices) {
-               GSList *iter = NULL;
-               ttsvoice_s* data = NULL;
-
-               iter = g_slist_nth(g_cur_voices, 0);
-               while (NULL != iter) {
-                       data = iter->data;
-
-                       if (NULL != data) {
-                               if (NULL != data->lang) {
-                                       free(data->lang);
-                                       data->lang = NULL;
-                               }
+       GSList *iter = g_slist_nth(g_cur_voices, 0);
+       while (NULL != iter) {
+               ttsvoice_s* data = iter->data;
 
-                               g_cur_voices = g_slist_remove(g_cur_voices, data);
-                               free(data);
-                               data = NULL;
+               if (NULL != data) {
+                       if (NULL != data->lang) {
+                               free(data->lang);
+                               data->lang = NULL;
                        }
 
-                       iter = g_slist_nth(g_cur_voices, 0);
+                       g_cur_voices = g_slist_remove(g_cur_voices, data);
+                       free(data);
+                       data = NULL;
                }
+
+               iter = g_slist_nth(g_cur_voices, 0);
        }
 
+       g_slist_free(g_cur_voices);
        g_cur_voices = NULL;
+}
+
+static int __update_voice_list()
+{
+       if (false == __is_agent_initialized()) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
+               return TTSD_ERROR_INVALID_STATE;
+       }
+
+       __clear_voice_list();
 
        int ret = -1;
        ret = g_engine_info->callbacks->foreach_voices(__set_voice_info_cb, NULL);
@@ -261,8 +303,7 @@ int ttsd_engine_agent_load_current_engine(ttse_request_callback_s* callback)
 {
        SLOG(LOG_DEBUG, tts_tag(), "[Engine Agent DEBUG] load current engine START");
 
-
-       if (false == g_agent_init) {
+       if (false == __is_agent_initialized()) {
                SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
                return TTSD_ERROR_INVALID_STATE;
        }
@@ -284,7 +325,7 @@ int ttsd_engine_agent_load_current_engine(ttse_request_callback_s* callback)
        }
 
        /* check whether current engine is loaded or not */
-       if (true == g_engine_info->is_loaded) {
+       if (__is_engine_loaded()) {
                SLOG(LOG_INFO, tts_tag(), "[Engine Agent] Engine has already been loaded ");
                return TTSD_ERROR_NONE;
        }
@@ -360,12 +401,12 @@ int ttsd_engine_agent_load_current_engine(ttse_request_callback_s* callback)
 
 int ttsd_engine_agent_unload_current_engine()
 {
-       if (false == g_agent_init) {
+       if (false == __is_agent_initialized()) {
                SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
                return TTSD_ERROR_INVALID_STATE;
        }
 
-       if (false == g_engine_info->is_loaded) {
+       if (false == __is_engine_loaded()) {
                SLOG(LOG_DEBUG, tts_tag(), "[Engine Agent] Engine has already been unloaded ");
                return TTSD_ERROR_NONE;
        }
@@ -380,25 +421,7 @@ int ttsd_engine_agent_unload_current_engine()
        /* reset current engine data */
        g_engine_info->is_loaded = false;
 
-       GSList *iter = NULL;
-       ttsvoice_s* data = NULL;
-
-       iter = g_slist_nth(g_cur_voices, 0);
-       while (NULL != iter) {
-               data = iter->data;
-
-               if (NULL != data) {
-                       if (NULL != data->lang) {
-                               free(data->lang);
-                               data->lang = NULL;
-                       }
-                       g_cur_voices = g_slist_remove(g_cur_voices, data);
-                       free(data);
-                       data = NULL;
-               }
-
-               iter = g_slist_nth(g_cur_voices, 0);
-       }
+       __clear_voice_list();
 
        SLOG(LOG_DEBUG, tts_tag(), "[Engine Agent Success] Unload current engine");
 
@@ -407,7 +430,7 @@ int ttsd_engine_agent_unload_current_engine()
 
 bool ttsd_engine_agent_need_network()
 {
-       if (false == g_agent_init) {
+       if (false == __is_agent_initialized()) {
                SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
                return false;
        }
@@ -417,23 +440,14 @@ bool ttsd_engine_agent_need_network()
 
 bool ttsd_engine_agent_is_same_engine(const char* engine_id)
 {
-       if (false == g_agent_init) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
-               return false;
-       }
-
        if (NULL == engine_id) {
                SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] engine_id is NULL");
                return false;
        }
 
-       if (NULL == g_engine_info) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] g_engine_info is NULL");
-               return false;
-       }
-
-       if (false == g_engine_info->is_loaded) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not loaded engine");
+       int ret = __check_engine_initialized_and_loaded();
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret));
                return false;
        }
 
@@ -451,13 +465,9 @@ bool ttsd_engine_select_valid_voice(const char* lang, int type, char** out_lang,
                return false;
        }
 
-       if (NULL == g_engine_info) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] g_engine_info is NULL");
-               return false;
-       }
-
-       if (false == g_engine_info->is_loaded) {
-               SLOG(LOG_WARN, tts_tag(), "[Engine Agent WARNING] Not loaded engine");
+       int ret = __check_engine_initialized_and_loaded();
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret));
                return false;
        }
 
@@ -478,7 +488,6 @@ bool ttsd_engine_select_valid_voice(const char* lang, int type, char** out_lang,
 
        /* Get voice list */
        GList* voice_list = NULL;
-       int ret = 0;
        ret = g_engine_info->callbacks->foreach_voices(__supported_voice_cb, &voice_list);
        if (0 != ret || 0 >= g_list_length(voice_list)) {
                SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] fail to get voice list : result(%d)", ret);
@@ -611,22 +620,12 @@ int ttsd_engine_agent_set_default_voice(const char* language, int vctype)
                return TTSD_ERROR_INVALID_PARAMETER;
        }
 
-       if (false == g_agent_init) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
-               return TTSD_ERROR_INVALID_STATE;
-       }
-
-       if (NULL == g_engine_info) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] g_engine_info is NULL");
-               return TTSD_ERROR_INVALID_STATE;
-       }
-
-       if (false == g_engine_info->is_loaded) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] No loaded engine");
-               return TTSD_ERROR_ENGINE_NOT_FOUND;
+       int ret = __check_engine_initialized_and_loaded();
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret));
+               return ret;
        }
 
-       int ret = -1;
        bool is_valid = false;
        ret = g_engine_info->callbacks->is_valid_voice(language, vctype, &is_valid);
        if (0 == ret) {
@@ -704,7 +703,7 @@ int ttsd_engine_agent_set_default_voice(const char* language, int vctype)
 
 int ttsd_engine_agent_set_default_speed(int speed)
 {
-       if (false == g_agent_init) {
+       if (false == __is_agent_initialized()) {
                SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
                return TTSD_ERROR_INVALID_STATE;
        }
@@ -716,22 +715,13 @@ int ttsd_engine_agent_set_default_speed(int speed)
 
 int ttsd_engine_agent_set_default_pitch(int pitch)
 {
-       if (false == g_agent_init) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
-               return TTSD_ERROR_INVALID_STATE;
-       }
-
-       if (NULL == g_engine_info) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] No engine info");
-               return TTSD_ERROR_INVALID_STATE;
-       }
-
-       if (false == g_engine_info->is_loaded) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] No loaded engine");
-               return TTSD_ERROR_ENGINE_NOT_FOUND;
+       int ret = __check_engine_initialized_and_loaded();
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret));
+               return ret;
        }
 
-       int ret = g_engine_info->callbacks->set_pitch(pitch);
+       ret = g_engine_info->callbacks->set_pitch(pitch);
        if (0 != ret) {
                SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Fail to set pitch : pitch(%d), result(%s)",
                         pitch, __ttsd_get_engine_error_code(ret));
@@ -751,14 +741,10 @@ int ttsd_engine_agent_is_credential_needed(unsigned int uid, bool* credential_ne
                return TTSD_ERROR_INVALID_PARAMETER;
        }
 
-       if (false == g_agent_init) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
-               return TTSD_ERROR_INVALID_STATE;
-       }
-
-       if (false == g_engine_info->is_loaded) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not loaded engine");
-               return TTSD_ERROR_OPERATION_FAILED;
+       int ret = __check_engine_initialized_and_loaded();
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret));
+               return ret;
        }
 
        if (NULL == g_engine_info->callbacks->need_app_credential) {
@@ -780,14 +766,10 @@ int ttsd_engine_agent_is_credential_needed(unsigned int uid, bool* credential_ne
 
 int ttsd_engine_load_voice(const char* lang, const int vctype)
 {
-       if (false == g_agent_init) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
-               return TTSD_ERROR_INVALID_STATE;
-       }
-
-       if (false == g_engine_info->is_loaded) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] No loaded engine");
-               return TTSD_ERROR_ENGINE_NOT_FOUND;
+       int ret = __check_engine_initialized_and_loaded();
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret));
+               return ret;
        }
 
        if (NULL == lang) {
@@ -796,7 +778,6 @@ int ttsd_engine_load_voice(const char* lang, const int vctype)
        }
 
        /* 1. Find voice info */
-       int ret = -1;
        ttsvoice_s* data = __get_ttsvoice(lang, vctype);
        if (NULL == data) {
                SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] This voice is not supported voice : lang(%s) type(%d)", lang, vctype);
@@ -834,14 +815,10 @@ int ttsd_engine_load_voice(const char* lang, const int vctype)
 
 int ttsd_engine_unload_voice(const char* lang, const int vctype)
 {
-       if (false == g_agent_init) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
-               return TTSD_ERROR_INVALID_STATE;
-       }
-
-       if (false == g_engine_info->is_loaded) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] No loaded engine");
-               return TTSD_ERROR_ENGINE_NOT_FOUND;
+       int ret = __check_engine_initialized_and_loaded();
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret));
+               return ret;
        }
 
        if (NULL == lang) {
@@ -850,7 +827,6 @@ int ttsd_engine_unload_voice(const char* lang, const int vctype)
        }
 
        /* 1. Find voice info */
-       int ret = -1;
        ttsvoice_s* data = __get_ttsvoice(lang, vctype);
        if (NULL == data) {
                SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] This voice is not supported voice : lang(%s) type(%d)", lang, vctype);
@@ -892,14 +868,10 @@ int ttsd_engine_start_synthesis(const char* lang, int vctype, const char* text,
                return TTSD_ERROR_INVALID_PARAMETER;
        }
 
-       if (false == g_agent_init) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
-               return TTSD_ERROR_INVALID_STATE;
-       }
-
-       if (false == g_engine_info->is_loaded) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] No loaded engine");
-               return TTSD_ERROR_ENGINE_NOT_FOUND;
+       int ret = __check_engine_initialized_and_loaded();
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret));
+               return ret;
        }
 
        /* select voice for default */
@@ -926,7 +898,6 @@ int ttsd_engine_start_synthesis(const char* lang, int vctype, const char* text,
        }
 
        /* synthesize text */
-       int ret = 0;
        ret = g_engine_info->callbacks->start_synth(temp_lang, temp_type, text, temp_speed, appid, credential, user_param);
        if (0 != ret) {
                SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] ***************************************");
@@ -948,19 +919,14 @@ int ttsd_engine_start_synthesis(const char* lang, int vctype, const char* text,
 
 int ttsd_engine_cancel_synthesis()
 {
-       if (false == g_agent_init) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
-               return TTSD_ERROR_INVALID_STATE;
-       }
-
-       if (false == g_engine_info->is_loaded) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] No loaded engine");
-               return TTSD_ERROR_ENGINE_NOT_FOUND;
+       int ret = __check_engine_initialized_and_loaded();
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret));
+               return ret;
        }
 
        SLOG(LOG_INFO, tts_tag(), "[Engine Agent] Cancel synth");
        /* stop synthesis */
-       int ret = 0;
        ret = g_engine_info->callbacks->cancel_synth();
        if (0 != ret) {
                SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] fail cancel synthesis : %s", __ttsd_get_engine_error_code(ret));
@@ -998,17 +964,12 @@ int ttsd_engine_get_voice_list(GList** voice_list)
                return TTSD_ERROR_INVALID_PARAMETER;
        }
 
-       if (false == g_agent_init) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
-               return TTSD_ERROR_INVALID_STATE;
-       }
-
-       if (false == g_engine_info->is_loaded) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] No loaded engine");
-               return TTSD_ERROR_ENGINE_NOT_FOUND;
+       int ret = __check_engine_initialized_and_loaded();
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret));
+               return ret;
        }
 
-       int ret = 0;
        ret = g_engine_info->callbacks->foreach_voices(__supported_voice_cb, (void*)voice_list);
        if (0 != ret) {
                SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Fail to get voice list : %s", __ttsd_get_engine_error_code(ret));
@@ -1019,14 +980,10 @@ int ttsd_engine_get_voice_list(GList** voice_list)
 
 int ttsd_engine_get_default_voice(char** lang, int* vctype)
 {
-       if (false == g_agent_init) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
-               return TTSD_ERROR_INVALID_STATE;
-       }
-
-       if (false == g_engine_info->is_loaded) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] No loaded engine");
-               return TTSD_ERROR_ENGINE_NOT_FOUND;
+       int ret = __check_engine_initialized_and_loaded();
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret));
+               return ret;
        }
 
        if (NULL == lang || NULL == vctype) {
@@ -1049,14 +1006,10 @@ int ttsd_engine_get_default_voice(char** lang, int* vctype)
 
 int ttsd_engine_set_private_data(const char* key, const char* data)
 {
-       if (false == g_agent_init) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
-               return TTSD_ERROR_INVALID_STATE;
-       }
-
-       if (false == g_engine_info->is_loaded) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] No loaded engine");
-               return TTSD_ERROR_ENGINE_NOT_FOUND;
+       int ret = __check_engine_initialized_and_loaded();
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret));
+               return ret;
        }
 
        if (NULL == key) {
@@ -1069,8 +1022,7 @@ int ttsd_engine_set_private_data(const char* key, const char* data)
                return TTSD_ERROR_NOT_SUPPORTED_FEATURE;
        }
 
-       int ret = g_engine_info->callbacks->private_data_set(key, data);
-
+       ret = g_engine_info->callbacks->private_data_set(key, data);
        if (0 != ret) {
                SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Fail to set private data(%d)", ret);
        }
@@ -1080,14 +1032,10 @@ int ttsd_engine_set_private_data(const char* key, const char* data)
 
 int ttsd_engine_get_private_data(const char* key, char** data)
 {
-       if (false == g_agent_init) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
-               return TTSD_ERROR_INVALID_STATE;
-       }
-
-       if (false == g_engine_info->is_loaded) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] No loaded engine");
-               return TTSD_ERROR_ENGINE_NOT_FOUND;
+       int ret = __check_engine_initialized_and_loaded();
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret));
+               return ret;
        }
 
        if (NULL == key || NULL == data) {
@@ -1102,7 +1050,6 @@ int ttsd_engine_get_private_data(const char* key, char** data)
        }
 
        char* temp = NULL;
-       int ret = 0;
        ret = g_engine_info->callbacks->private_data_requested(key, &temp);
        if (0 != ret) {
                SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Fail to get private data(%d)", ret);
@@ -1118,14 +1065,10 @@ int ttsd_engine_get_private_data(const char* key, char** data)
 
 int ttsd_engine_check_app_agreed(const char* appid, bool* is_agreed)
 {
-       if (false == g_agent_init) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
-               return TTSD_ERROR_INVALID_STATE;
-       }
-
-       if (false == g_engine_info->is_loaded) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] No loaded engine");
-               return TTSD_ERROR_ENGINE_NOT_FOUND;
+       int ret = __check_engine_initialized_and_loaded();
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret));
+               return ret;
        }
 
        if (NULL == appid || NULL == is_agreed) {
@@ -1139,7 +1082,6 @@ int ttsd_engine_check_app_agreed(const char* appid, bool* is_agreed)
                return TTSD_ERROR_NOT_SUPPORTED_FEATURE;
        }
 
-       int ret = 0;
        bool tmp = true; // default value is true until now
        ret = g_engine_info->callbacks->check_app_agreed(appid, &tmp);
        if (0 != ret) {
@@ -1290,19 +1232,10 @@ static int __internal_get_engine_info(ttse_request_callback_s* callback)
 /** Set callbacks of the current engine */
 int ttsd_engine_agent_set_private_data_set_cb(ttse_private_data_set_cb callback)
 {
-       if (false == g_agent_init) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
-               return TTSD_ERROR_INVALID_STATE;
-       }
-
-       if (NULL == g_engine_info) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] The engine is not valid");
-               return TTSD_ERROR_INVALID_STATE;
-       }
-
-       if (false == g_engine_info->is_loaded) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not loaded engine");
-               return TTSD_ERROR_OPERATION_FAILED;
+       int ret = __check_engine_initialized_and_loaded();
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret));
+               return ret;
        }
 
        g_engine_info->callbacks->private_data_set = callback;
@@ -1312,19 +1245,10 @@ int ttsd_engine_agent_set_private_data_set_cb(ttse_private_data_set_cb callback)
 
 int ttsd_engine_agent_set_private_data_requested_cb(ttse_private_data_requested_cb callback)
 {
-       if (false == g_agent_init) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not Initialized");
-               return TTSD_ERROR_INVALID_STATE;
-       }
-
-       if (NULL == g_engine_info) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] The engine is not valid");
-               return TTSD_ERROR_INVALID_STATE;
-       }
-
-       if (false == g_engine_info->is_loaded) {
-               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Not loaded engine");
-               return TTSD_ERROR_OPERATION_FAILED;
+       int ret = __check_engine_initialized_and_loaded();
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, tts_tag(), "[Engine Agent ERROR] Precondition is not met(%s)", get_error_message(ret));
+               return ret;
        }
 
        g_engine_info->callbacks->private_data_requested = callback;