Refactor duplicated code for handling dbus request 09/257109/2
authorSuyeon Hwang <stom.hwang@samsung.com>
Mon, 23 Nov 2020 06:26:59 +0000 (15:26 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Thu, 27 May 2021 04:45:11 +0000 (13:45 +0900)
Change-Id: I7519e7144ed9c423304bcbc41f0e3beb752d96dd
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
client/tts.c
client/tts_core.c

index 81dfaaf3cf3c22be685370e6a2fbd651cfe1e7d5..b14dff4a0c37d49a2d3e697e53c37989d8c2d3a1 100644 (file)
@@ -340,6 +340,7 @@ int tts_destroy(tts_h tts)
                if (0 != tts_core_unprepare(client)) {
                        SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to unprepare");
                }
+               tts_client_set_current_state(client, TTS_STATE_CREATED);
        case TTS_STATE_CREATED:
                /* Unset registered callbacks */
                tts_client_unset_all_cb(client);
@@ -651,6 +652,8 @@ int tts_unprepare(tts_h tts)
                return ret;
        }
 
+       tts_core_set_current_state(client, TTS_STATE_CREATED);
+
        SLOG(LOG_DEBUG, TAG_TTSC, "@@@");
        return TTS_ERROR_NONE;
 }
index e1870223e0e5fa490103545caecf5537781124fc..ec2e6f2bc6662315d6280e458427f4a8b62a1484 100644 (file)
@@ -633,6 +633,28 @@ static void __screen_reader_state_changed_cb(bool value)
 }
 //LCOV_EXCL_STOP
 
+static inline bool __handle_dbus_request_result(tts_client_s* client, tts_error_e ret, bool* is_prepared)
+{
+       if (TTS_ERROR_NONE == ret) {
+               SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success the dbus request");
+               return false;
+       } else if (TTS_ERROR_INVALID_PARAMETER == ret && false == *is_prepared) {
+               tts_client_set_current_state(client, TTS_STATE_CREATED);
+               if (0 == tts_core_prepare_sync(client)) {
+                       *is_prepared = true;
+                       SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
+               }
+       } else if (TTS_ERROR_TIMED_OUT != ret) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
+               return false;
+       } else {
+               SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry play : %s", __tts_get_error_code(ret));
+               usleep(10000);
+       }
+
+       return true;
+}
+
 static inline int __request_add_text(tts_client_s* client, const char* text, const char* language, int voice_type, int speed, int* utt_id)
 {
        /* change default language value */
@@ -648,36 +670,20 @@ static inline int __request_add_text(tts_client_s* client, const char* text, con
        bool is_prepared = false;
        while (TTS_RETRY_COUNT > count) {
                ret = tts_ipc_request_add_text(uid, text, convert_language, voice_type, speed, new_utt_id, client->credential);
-               if (0 == ret) {
-                       SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_ipc_request_add_text");
-                       *utt_id = new_utt_id;
+               if (false == __handle_dbus_request_result(client, ret, &is_prepared)) {
                        break;
                }
 
-               //LCOV_EXCL_START
-               if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
-                       tts_client_set_current_state(client, TTS_STATE_CREATED);
-                       if (0 == tts_core_prepare_sync(client)) {
-                               is_prepared = true;
-                               SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
-                       }
-               } else if (TTS_ERROR_TIMED_OUT != ret) {
-                       SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
-                       return ret;
-               } else {
-                       SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry add text : %s", __tts_get_error_code(ret));
-                       usleep(10000);
-               }
-               //LCOV_EXCL_STOP
-
                count++;
        }
 
-       if (TTS_RETRY_COUNT == count) {
+       if (TTS_ERROR_NONE != ret) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
-               return TTS_ERROR_TIMED_OUT;
+               return ret;
        }
 
+       SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_dbus_request_add_text");
+       *utt_id = new_utt_id;
        return TTS_ERROR_NONE;
 }
 
@@ -689,33 +695,19 @@ static inline int __request_play(tts_client_s* client)
        bool is_prepared = false;
        while (TTS_RETRY_COUNT > count) {
                ret = tts_ipc_request_play(uid, client->credential);
-               if (0 == ret) {
-                       SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_ipc_request_play");
+               if (false == __handle_dbus_request_result(client, ret, &is_prepared)) {
                        break;
                }
 
-               if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
-                       tts_client_set_current_state(client, TTS_STATE_CREATED);
-                       if (0 == tts_core_prepare_sync(client)) {
-                               is_prepared = true;
-                               SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
-                       }
-               } else if (TTS_ERROR_TIMED_OUT != ret) {
-                       SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
-                       return ret;
-               } else {
-                       SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry play : %s", __tts_get_error_code(ret));
-                       usleep(10000);
-               }
-
                count++;
        }
 
-       if (TTS_RETRY_COUNT == count) {
+       if (TTS_ERROR_NONE != ret) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
-               return TTS_ERROR_TIMED_OUT;
+               return ret;
        }
 
+       SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_dbus_request_play");
        return tts_core_set_current_state(client, TTS_STATE_PLAYING);
 }
 
@@ -1151,34 +1143,19 @@ int tts_core_unprepare(tts_client_s* client)
        } else {
                bool is_prepared = false;
 
-               do {
+               while(TTS_RETRY_COUNT > count) {
                        ret = tts_ipc_request_finalize(uid);
-                       if (0 != ret) {
-                               //LCOV_EXCL_START
-                               if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
-                                       tts_client_set_current_state(client, TTS_STATE_CREATED);
-                                       if (0 == tts_core_prepare_sync(client)) {
-                                               SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
-                                               is_prepared = true;
-                                       }
-                               } else if (TTS_ERROR_TIMED_OUT != ret) {
-                                       SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
-                                       break;
-                               } else {
-                                       SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry finalize : %s", __tts_get_error_code(ret));
-                                       usleep(10000);
-                                       count++;
-                                       if (TTS_RETRY_COUNT == count) {
-                                               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
-                                               break;
-                                       }
-                               }
-                               //LCOV_EXCL_STOP
+                       if (false == __handle_dbus_request_result(client, ret, &is_prepared)) {
+                               break;
                        }
-               } while (0 != ret);
-       }
 
-       tts_core_set_current_state(client, TTS_STATE_CREATED);
+                       count++;
+               }
+
+               if (TTS_ERROR_NONE == ret) {
+                       SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_dbus_request_finalize");
+               }
+       }
 
        return TTS_ERROR_NONE;
 }
@@ -1310,31 +1287,16 @@ int tts_core_stop(tts_client_s* client)
        bool is_prepared = false;
        while (TTS_RETRY_COUNT > count) {
                ret = tts_ipc_request_stop(uid);
-               if (0 == ret) {
-                       SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_ipc_request_play");
+               if (false == __handle_dbus_request_result(client, ret, &is_prepared)) {
                        break;
                }
 
-               if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
-                       tts_client_set_current_state(client, TTS_STATE_CREATED);
-                       if (0 == tts_core_prepare_sync(client)) {
-                               is_prepared = true;
-                               SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
-                       }
-               } else if (TTS_ERROR_TIMED_OUT != ret) {
-                       SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
-                       return ret;
-               } else {
-                       SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry play : %s", __tts_get_error_code(ret));
-                       usleep(10000);
-               }
-
                count++;
        }
 
-       if (TTS_RETRY_COUNT == count) {
+       if (TTS_ERROR_NONE != ret) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
-               return TTS_ERROR_TIMED_OUT;
+               return ret;
        }
 
        return tts_core_set_current_state(client, TTS_STATE_READY);
@@ -1353,33 +1315,19 @@ int tts_core_pause(tts_client_s* client)
        bool is_prepared = false;
        while (TTS_RETRY_COUNT > count) {
                ret = tts_ipc_request_pause(uid);
-               if (0 == ret) {
-                       SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_ipc_request_pause");
+               if (false == __handle_dbus_request_result(client, ret, &is_prepared)) {
                        break;
                }
 
-               if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
-                       tts_client_set_current_state(client, TTS_STATE_CREATED);
-                       if (0 == tts_core_prepare_sync(client)) {
-                               is_prepared = true;
-                               SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
-                       }
-               } else if (TTS_ERROR_TIMED_OUT != ret) {
-                       SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
-                       return ret;
-               } else {
-                       SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry pause : %s", __tts_get_error_code(ret));
-                       usleep(10000);
-               }
-
                count++;
        }
 
-       if (TTS_RETRY_COUNT == count) {
+       if (TTS_ERROR_NONE != ret) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
-               return TTS_ERROR_TIMED_OUT;
+               return ret;
        }
 
+       SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_dbus_request_pause");
        return tts_core_set_current_state(client, TTS_STATE_PAUSED);
 }
 
@@ -1435,33 +1383,19 @@ int tts_core_add_pcm(tts_client_s* client, int event, const void* data, unsigned
        bool is_prepared = false;
        while (TTS_RETRY_COUNT > count) {
                ret = tts_ipc_request_add_pcm(uid, event, data, data_size, audio_type, rate);
-               if (0 == ret) {
-                       SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_ipc_request_add_pcm");
+               if (false == __handle_dbus_request_result(client, ret, &is_prepared)) {
                        break;
                }
 
-               if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
-                       tts_client_set_current_state(client, TTS_STATE_CREATED);
-                       if (0 == tts_core_prepare_sync(client)) {
-                               is_prepared = true;
-                               SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
-                       }
-               } else if (TTS_ERROR_TIMED_OUT != ret) {
-                       SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
-                       return ret;
-               } else {
-                       SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry add pcm : %s", __tts_get_error_code(ret));
-                       usleep(10000);
-               }
-
                count++;
        }
 
-       if (TTS_RETRY_COUNT == count) {
+       if (TTS_ERROR_NONE != ret) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
-               return TTS_ERROR_TIMED_OUT;
+               return ret;
        }
 
+       SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_dbus_request_add_pcm");
        return TTS_ERROR_NONE;
 }
 
@@ -1478,33 +1412,19 @@ int tts_core_play_pcm(tts_client_s* client)
        bool is_prepared = false;
        while (TTS_RETRY_COUNT > count) {
                ret = tts_ipc_request_play_pcm(uid);
-               if (0 == ret) {
-                       SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_ipc_request_play_pcm");
+               if (false == __handle_dbus_request_result(client, ret, &is_prepared)) {
                        break;
                }
 
-               if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
-                       tts_client_set_current_state(client, TTS_STATE_CREATED);
-                       if (0 == tts_core_prepare_sync(client)) {
-                               is_prepared = true;
-                               SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
-                       }
-               } else if (TTS_ERROR_TIMED_OUT != ret) {
-                       SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
-                       return ret;
-               } else {
-                       SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry play pcm : %s", __tts_get_error_code(ret));
-                       usleep(10000);
-               }
-
                count++;
        }
 
-       if (TTS_RETRY_COUNT == count) {
+       if (TTS_ERROR_NONE != ret) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
-               return TTS_ERROR_TIMED_OUT;
+               return ret;
        }
 
+       SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_dbus_request_play_pcm");
        return tts_core_set_current_state(client, TTS_STATE_PLAYING);
 }
 
@@ -1521,32 +1441,18 @@ int tts_core_stop_pcm(tts_client_s* client)
        bool is_prepared = false;
        while (TTS_RETRY_COUNT > count) {
                ret = tts_ipc_request_stop_pcm(uid);
-               if (0 == ret) {
-                       SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_ipc_request_stop_pcm");
+               if (false == __handle_dbus_request_result(client, ret, &is_prepared)) {
                        break;
                }
 
-               if (TTS_ERROR_INVALID_PARAMETER == ret && false == is_prepared) {
-                       tts_client_set_current_state(client, TTS_STATE_CREATED);
-                       if (0 == tts_core_prepare_sync(client)) {
-                               is_prepared = true;
-                               SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_prepare_sync");
-                       }
-               } else if (TTS_ERROR_TIMED_OUT != ret) {
-                       SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
-                       return ret;
-               } else {
-                       SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry stop pcm : %s", __tts_get_error_code(ret));
-                       usleep(10000);
-               }
-
                count++;
        }
 
-       if (TTS_RETRY_COUNT == count) {
+       if (TTS_ERROR_NONE != ret) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
-               return TTS_ERROR_TIMED_OUT;
+               return ret;
        }
 
+       SLOG(LOG_INFO, TAG_TTSC, "[INFO] Success tts_dbus_request_stop_pcm");
        return tts_core_set_current_state(client, TTS_STATE_READY);
 }