Extract precondition for start 26/277326/2
authorwn.jang <wn.jang@samsung.com>
Tue, 5 Jul 2022 07:22:29 +0000 (16:22 +0900)
committerwn.jang <wn.jang@samsung.com>
Wed, 6 Jul 2022 01:18:50 +0000 (10:18 +0900)
Change-Id: I39dd1765782564a956d7edf92d799aeba2b17789

server/sttd_server.c

index f6bf986..5eeb0f6 100644 (file)
@@ -1153,13 +1153,8 @@ void __sttd_start_sound_completed_cb(int id, void *user_data)
        return;
 }
 
-int sttd_server_start(unsigned int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential)
+static int __sttd_server_check_precondition_to_start(unsigned int uid, const char* appid)
 {
-       if (NULL == lang || NULL == recognition_type) {
-               SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
-               return STTD_ERROR_INVALID_PARAMETER;
-       }
-
        /* check if uid is valid */
        app_state_e state;
        if (0 != sttd_client_get_state(uid, &state)) {
@@ -1173,7 +1168,7 @@ int sttd_server_start(unsigned int uid, const char* lang, const char* recognitio
                return STTD_ERROR_INVALID_STATE;
        }
 
-       int ret = 0;
+       int ret = STTD_ERROR_NONE;
        if (false == stt_client_get_app_agreed(uid)) {
                bool temp = false;
                ret = sttd_engine_agent_check_app_agreed(appid, &temp);
@@ -1197,6 +1192,21 @@ int sttd_server_start(unsigned int uid, const char* lang, const char* recognitio
                        return STTD_ERROR_OUT_OF_NETWORK;
                }
        }
+       return ret;
+}
+
+int sttd_server_start(unsigned int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential)
+{
+       if (NULL == lang || NULL == recognition_type) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL");
+               return STTD_ERROR_INVALID_PARAMETER;
+       }
+
+       int ret = __sttd_server_check_precondition_to_start(uid, appid);
+       if (0 != ret) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to satisfy preconditions, ret(0x%x)", ret);
+               return ret;
+       }
 
        if (NULL != g_recording_timer) {
                ecore_timer_del(g_recording_timer);
@@ -1508,10 +1518,8 @@ int sttd_server_stop(unsigned int uid)
        return STTD_ERROR_NONE;
 }
 
-int sttd_server_cancel(unsigned int uid)
+static int __sttd_server_cancel(unsigned int uid)
 {
-       SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter sttd_server_cancel");
-
        /* check if uid is valid */
        app_state_e state;
        if (0 != sttd_client_get_state(uid, &state)) {
@@ -1562,9 +1570,20 @@ int sttd_server_cancel(unsigned int uid)
        /* Notify uid state change */
        sttdc_send_set_state(uid, APP_STATE_READY);
 
-       SLOG(LOG_INFO, TAG_STTD, "[Server INFO] End sttd_server_cancel");
+       return ret;
+}
 
-       return STTD_ERROR_NONE;
+int sttd_server_cancel(unsigned int uid)
+{
+       SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter sttd_server_cancel");
+
+       int ret = __sttd_server_cancel(uid);
+       if (0 != ret) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(0x%x)", ret);
+       }
+
+       SLOG(LOG_INFO, TAG_STTD, "[Server INFO] End sttd_server_cancel");
+       return ret;
 }
 
 int sttd_server_start_file(unsigned int uid, const char* lang, const char* recognition_type, int silence, const char* appid, const char* credential,
@@ -1575,42 +1594,10 @@ int sttd_server_start_file(unsigned int uid, const char* lang, const char* recog
                return STTD_ERROR_INVALID_PARAMETER;
        }
 
-       /* check if uid is valid */
-       app_state_e state;
-       if (0 != sttd_client_get_state(uid, &state)) {
-               SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
-               return STTD_ERROR_INVALID_PARAMETER;
-       }
-
-       /* check uid state */
-       if (APP_STATE_READY != state) {
-               SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] sttd_server_start : current state is not ready");
-               return STTD_ERROR_INVALID_STATE;
-       }
-
-       int ret = 0;
-       if (false == stt_client_get_app_agreed(uid)) {
-               bool temp = false;
-               ret = sttd_engine_agent_check_app_agreed(appid, &temp);
-               if (0 != ret) {
-                       SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine available : %d", ret);
-                       return ret;
-               }
-
-               if (false == temp) {
-                       SLOG(LOG_ERROR, TAG_STTD, "[Server] App(%s) NOT confirmed that engine is available", appid);
-                       return STTD_ERROR_PERMISSION_DENIED;
-               }
-
-               stt_client_set_app_agreed(uid);
-       }
-
-       /* check if engine use network */
-       if (true == sttd_engine_agent_need_network()) {
-               if (false == stt_network_is_connected()) {
-                       SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Disconnect network. Current engine needs to network connection.");
-                       return STTD_ERROR_OUT_OF_NETWORK;
-               }
+       int ret = __sttd_server_check_precondition_to_start(uid, appid);
+       if (0 != ret) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to satisfy preconditions, ret(0x%x)", ret);
+               return ret;
        }
 
        if (0 != stt_client_set_current_recognition(uid)) {
@@ -1686,54 +1673,13 @@ int sttd_server_start_file(unsigned int uid, const char* lang, const char* recog
 
 int sttd_server_cancel_file(unsigned int uid)
 {
-       /* check if uid is valid */
-       app_state_e state;
-       if (0 != sttd_client_get_state(uid, &state)) {
-               SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid ");
-               return STTD_ERROR_INVALID_PARAMETER;
-       }
+       SLOG(LOG_INFO, TAG_STTD, "[Server INFO] Enter sttd_server_cancel_file");
 
-       /* check uid state */
-       if (APP_STATE_READY == state) {
-               SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is ready");
-               return STTD_ERROR_NONE;
-       }
-
-       stt_client_unset_current_recognition();
-
-       if (NULL != g_recording_timer) {
-               ecore_timer_del(g_recording_timer);
-               g_recording_timer = NULL;
-       }
-
-       if (NULL != g_processing_timer) {
-               SLOG(LOG_INFO, TAG_STTD, "[INFO] Delete g_processing_timer");
-               ecore_timer_del(g_processing_timer);
-               g_processing_timer = NULL;
-       }
-
-       if (APP_STATE_RECORDING == state) {
-               /* Unset audio session */
-               int ret = sttd_recorder_unset_audio_session();
-               if (0 != ret) {
-                       SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unset session : %d", ret);
-                       return ret;
-               }
-       }
-
-       /* change uid state */
-       sttd_client_set_state(uid, APP_STATE_READY);
-
-       /* cancel engine recognition */
-       int ret = sttd_engine_agent_recognize_cancel();
+       int ret = __sttd_server_cancel(uid);
        if (0 != ret) {
-               SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret);
-               return ret;
+               SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel file : result(0x%x)", ret);
        }
 
-       /* Notify uid state change */
-       sttdc_send_set_state(uid, APP_STATE_READY);
-
-       return STTD_ERROR_NONE;
-}
-
+       SLOG(LOG_INFO, TAG_STTD, "[Server INFO] End sttd_server_cancel_file");
+       return ret;
+}
\ No newline at end of file