Refactor parameter check 99/248499/9
authorSuyeon Hwang <stom.hwang@samsung.com>
Mon, 16 Nov 2020 08:58:28 +0000 (17:58 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Thu, 18 Mar 2021 07:58:30 +0000 (16:58 +0900)
Change-Id: I4612d2f94a0064d153f1af8c7da0e4f18e389df1
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
client/tts.c
client/tts_client.c
client/tts_core.c
client/tts_core.h

index d2c3e68..d09f268 100644 (file)
@@ -491,6 +491,11 @@ int tts_set_mode(tts_h tts, tts_mode_e mode)
 
        SLOG(LOG_INFO, TAG_TTSC, "@@@ Set TTS mode(%d)", mode);
 
+       if (TTS_MODE_DEFAULT > mode || TTS_MODE_INTERRUPT < mode) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] mode is not valid : %d", mode);
+               return TTS_ERROR_INVALID_PARAMETER;
+       }
+
        tts_client_s* client = tts_client_get(tts);
        if (NULL == client) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid. tts(%p)", tts);
@@ -503,14 +508,7 @@ int tts_set_mode(tts_h tts, tts_mode_e mode)
                return TTS_ERROR_INVALID_STATE;
        }
 
-       if (TTS_MODE_DEFAULT <= mode && mode <= TTS_MODE_INTERRUPT) {
-               client->mode = mode;
-       } else {
-               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] mode is not valid : %d", mode);
-               SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
-               SLOG(LOG_DEBUG, TAG_TTSC, " ");
-               return TTS_ERROR_INVALID_PARAMETER;
-       }
+       client->mode = mode;
 
        if (TTS_MODE_SCREEN_READER == mode) {
                int ret;
@@ -537,6 +535,11 @@ int tts_get_mode(tts_h tts, tts_mode_e* mode)
 
        SLOG(LOG_DEBUG, TAG_TTSC, "@@@ Get TTS mode");
 
+       if (NULL == mode) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter(mode) is NULL");
+               return TTS_ERROR_INVALID_PARAMETER;
+       }
+
        tts_client_s* client = tts_client_get(tts);
        if (NULL == client) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid. tts(%p)", tts);
@@ -549,16 +552,9 @@ int tts_get_mode(tts_h tts, tts_mode_e* mode)
                return TTS_ERROR_INVALID_STATE;
        }
 
-       if (NULL == mode) {
-               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter(mode) is NULL");
-               return TTS_ERROR_INVALID_PARAMETER;
-       }
-
        *mode = client->mode;
 
        SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
-       SLOG(LOG_DEBUG, TAG_TTSC, " ");
-
        return TTS_ERROR_NONE;
 }
 
@@ -1000,6 +996,11 @@ int tts_add_text(tts_h tts, const char* text, const char* language, int voice_ty
                return TTS_ERROR_INVALID_PARAMETER;
        }
 
+       if (NULL == text || false == tts_core_is_valid_text(text)) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input text is invalid");
+               return TTS_ERROR_INVALID_PARAMETER;
+       }
+
        tts_client_s* client = tts_client_get(tts);
        if (NULL == client) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] A handle is not valid. tts(%p)", tts);
@@ -1032,52 +1033,6 @@ int tts_add_text(tts_h tts, const char* text, const char* language, int voice_ty
                return TTS_ERROR_PERMISSION_DENIED;
        }
 
-       // TODO: move up to parameter check
-       /* check valid utf8 */
-       bool valid = false;
-
-       DBusError err;
-       dbus_error_init(&err);
-
-       valid = dbus_validate_utf8(text, &err);
-       if (dbus_error_is_set(&err)) {
-               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Dbus Error(%s), text(%s)", err.message, text);
-               dbus_error_free(&err);
-               return TTS_ERROR_INVALID_PARAMETER;
-       }
-
-       if (valid != true) {
-               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Text is invalid - '%s'", text);
-               return TTS_ERROR_INVALID_PARAMETER;
-       }
-
-       /* check text size */
-       if (-1 == g_max_text_size) {
-               SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] g_max_text_size is %d byte", g_max_text_size);
-               if (0 != tts_config_mgr_get_max_text_size((unsigned int*)&g_max_text_size)) {
-                       SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get max text size");
-                       // TODO: fix to operation failed
-                       return TTS_ERROR_INVALID_PARAMETER;
-               }
-       }
-       // TODO: move up to parameter check
-
-       if (0 == g_max_text_size) {
-               if (strlen(text) <= 0) {
-                       SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] Max Text Size is %d byte", g_max_text_size);
-                       SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input text size is invalid. (max text size is unlimited.)");
-                       SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
-                       return TTS_ERROR_INVALID_PARAMETER;
-               }
-       } else {
-               SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] g_max_text_size is %d byte", g_max_text_size);
-               if (g_max_text_size < strlen(text) || strlen(text) <= 0) {
-                       SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input text size is invalid. current text_size(%zu byte)", strlen(text));
-                       SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
-                       return TTS_ERROR_INVALID_PARAMETER;
-               }
-       }
-
        SLOG(LOG_DEBUG, TAG_TTSC, "Text is valid - text(%zu byte) is '%s'", strlen(text), text);
 
        /* save texts for repetition */
index 6bdd4e3..1a3eef5 100644 (file)
 
 #include "tts_client.h"
 
-/* Max number of handle */
-static const int g_max_handle = 999;
+/* Constant value */
+static const int MAX_HANDLE_NUM = 999;
+static const int INVALID_HANDLE = -1;
+
 /* allocated handle */
 static int g_allocated_handle = 0;
 /* client list */
@@ -33,7 +35,7 @@ static int __client_generate_uid(int pid)
        pthread_mutex_lock(&g_allocated_handle_mutex);
        g_allocated_handle++;
 
-       if (g_allocated_handle > g_max_handle) {
+       if (g_allocated_handle > MAX_HANDLE_NUM) {
                g_allocated_handle = 1;
        }
 
@@ -86,27 +88,10 @@ static inline void __set_supported_voice_cb(tts_client_s* client, tts_supported_
        client->supported_voice_user_data = user_data;
 }
 
-int tts_client_new(tts_h* tts)
-{
-       tts_client_s* client = NULL;
-       client = (tts_client_s*)calloc(1, sizeof(tts_client_s));
-       if (NULL == client) {
-               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to allocate memory");
-               return TTS_ERROR_OUT_OF_MEMORY;
-       }
-       tts_h temp = (tts_h)calloc(1, sizeof(struct tts_s));
-       if (NULL == temp) {
-               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to allocate memory");
-               free(client);
-               return TTS_ERROR_OUT_OF_MEMORY;
-       }
-       temp->handle = __client_generate_uid(getpid());
-       temp->connected = false;
-
-       /* initialize client data */
-       client->tts = temp;
-       client->pid = getpid();
-       client->uid = temp->handle;
+static inline void __reset_client_data(tts_client_s* client) {
+       client->tts = NULL;
+       client->pid = INVALID_HANDLE;
+       client->uid = INVALID_HANDLE;
        client->current_utt_id = 0;
 
        client->state_changed_cb = NULL;
@@ -144,6 +129,30 @@ int tts_client_new(tts_h* tts)
 
        client->notify_error_timer = NULL;
        client->notify_state_timer = NULL;
+}
+
+int tts_client_new(tts_h* tts)
+{
+       tts_client_s* client = NULL;
+       client = (tts_client_s*)calloc(1, sizeof(tts_client_s));
+       if (NULL == client) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to allocate memory");
+               return TTS_ERROR_OUT_OF_MEMORY;
+       }
+       tts_h temp = (tts_h)calloc(1, sizeof(struct tts_s));
+       if (NULL == temp) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to allocate memory");
+               free(client);
+               return TTS_ERROR_OUT_OF_MEMORY;
+       }
+       temp->handle = __client_generate_uid(getpid());
+       temp->connected = false;
+
+       /* initialize client data */
+       __reset_client_data(client);
+       client->tts = temp;
+       client->pid = getpid();
+       client->uid = temp->handle;
 
        pthread_mutex_lock(&g_client_list_mutex);
        g_client_list = g_list_append(g_client_list, client);
@@ -175,6 +184,7 @@ int tts_client_destroy(tts_h tts)
                while (NULL != iter) {
                        data = iter->data;
                        if (tts->handle == data->tts->handle) {
+                               tts->handle = INVALID_HANDLE;
                                g_client_list = g_list_remove_link(g_client_list, iter);
 
                                while (0 != data->cb_ref_count) {
@@ -183,19 +193,17 @@ int tts_client_destroy(tts_h tts)
 
                                if (NULL != data->err_msg) {
                                        free(data->err_msg);
-                                       data->err_msg = NULL;
                                }
 
                                if (NULL != data->credential) {
                                        free(data->credential);
-                                       data->credential = NULL;
                                }
 
                                if (NULL != data->text_repeat) {
                                        free(data->text_repeat);
-                                       data->text_repeat = NULL;
                                }
 
+                               __reset_client_data(data);
                                int uid = data->uid;
                                free(data);
                                free(tts);
index d7ac852..d56f4d0 100644 (file)
@@ -66,10 +66,10 @@ static const char* __tts_get_error_code(tts_error_e err)
 static const char* __convert_state(tts_state_e state)
 {
        switch (state) {
-       case TTS_STATE_CREATED:         return "Created";
-       case TTS_STATE_READY:           return "Ready";
-       case TTS_STATE_PLAYING:         return "Playing";
-       case TTS_STATE_PAUSED:          return "Paused";
+       case TTS_STATE_CREATED: return "Created";
+       case TTS_STATE_READY:   return "Ready";
+       case TTS_STATE_PLAYING: return "Playing";
+       case TTS_STATE_PAUSED:  return "Paused";
        }
 
        return "Invalid state";
@@ -760,11 +760,6 @@ int tts_core_notify_default_voice_changed(tts_client_s* client, const char* befo
                return TTS_ERROR_INVALID_PARAMETER;
        }
 
-       if (NULL == before_lang || NULL == language) {
-               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid parameter.");
-               return TTS_ERROR_INVALID_PARAMETER;
-       }
-
        SLOG(LOG_DEBUG, TAG_TTSC, "Default voice changed data : before_lang(%s), before_voice_type(%d), language(%s), voice_type(%d)",
                        before_lang, before_voice_type, language, voice_type);
 
@@ -790,11 +785,6 @@ int tts_core_notify_engine_changed(tts_client_s* client, const char* engine_id,
                return TTS_ERROR_INVALID_PARAMETER;
        }
 
-       if (NULL == engine_id || NULL == language) {
-               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid parameter.");
-               return TTS_ERROR_INVALID_PARAMETER;
-       }
-
        SLOG(LOG_DEBUG, TAG_TTSC, "Engine changed data : engine_id(%s) language(%s), voicd_type(%d), need_credential(%d)",
                        engine_id, language, voice_type, need_credential);
 
@@ -828,6 +818,48 @@ int tts_core_set_current_state(tts_client_s* client, tts_state_e state)
        return TTS_ERROR_NONE;
 }
 
+bool tts_core_is_valid_text(const char* text)
+{
+       if (NULL == text) {
+               return false;
+       }
+
+       /* check valid utf8 */
+       DBusError err;
+       dbus_error_init(&err);
+       bool valid = dbus_validate_utf8(text, &err);
+       if (dbus_error_is_set(&err)) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Dbus Error(%s), text(%s)", err.message, text);
+               dbus_error_free(&err);
+               return false;
+       }
+
+       if (false == valid) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Text is invalid - '%s'", text);
+               return false;
+       }
+
+       if (strlen(text) <= 0) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input text size is invalid.");
+               return false;
+       }
+
+       unsigned int max_text_size = 0;
+       /* check text size */
+       if (0 != tts_config_mgr_get_max_text_size(&max_text_size)) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get max text size");
+               return false;
+       }
+
+       SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] max_text_size is %d byte", max_text_size);
+       if (0 < max_text_size && max_text_size < strlen(text)) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input text size is bigger than maximum test size. text_size(%zu byte)", strlen(text));
+               return false;
+       }
+
+       return true;
+}
+
 int tts_core_receive_hello(int uid, int ret, int credential_needed)
 {
        tts_client_s* client = tts_client_get_by_uid(uid);
index 7e52df5..b3d95ca 100644 (file)
@@ -32,6 +32,7 @@ int tts_core_notify_default_voice_changed(tts_client_s* client, const char* befo
 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_set_current_state(tts_client_s* client, tts_state_e state);
+bool tts_core_is_valid_text(const char* text);
 
 // called by tts.c
 int tts_core_initialize();