Fix bugs
[platform/core/uifw/stt.git] / common / stt_engine.c
index 1b315bb..0311c51 100644 (file)
@@ -1,5 +1,5 @@
 /*
-*  Copyright (c) 2011-2014 Samsung Electronics Co., Ltd All Rights Reserved
+*  Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
@@ -48,9 +48,14 @@ static const char* __stt_get_engine_error_code(sttp_error_e err)
        case STTP_ERROR_OUT_OF_MEMORY:          return "STTP_ERROR_OUT_OF_MEMORY";
        case STTP_ERROR_IO_ERROR:               return "STTP_ERROR_IO_ERROR";
        case STTP_ERROR_INVALID_PARAMETER:      return "STTP_ERROR_INVALID_PARAMETER";
+       case STTP_ERROR_TIMED_OUT:              return "STTP_ERROR_TIMED_OUT";
+       case STTP_ERROR_RECORDER_BUSY:          return "STTP_ERROR_RECORDER_BUSY";
        case STTP_ERROR_OUT_OF_NETWORK:         return "STTP_ERROR_OUT_OF_NETWORK";
+       case STTP_ERROR_PERMISSION_DENIED:      return "STTP_ERROR_PERMISSION_DENIED";
+       case STTP_ERROR_NOT_SUPPORTED:          return "STTP_ERROR_NOT_SUPPORTED";
        case STTP_ERROR_INVALID_STATE:          return "STTP_ERROR_INVALID_STATE";
        case STTP_ERROR_INVALID_LANGUAGE:       return "STTP_ERROR_INVALID_LANGUAGE";
+       case STTP_ERROR_ENGINE_NOT_FOUND:       return "STTP_ERROR_ENGINE_NOT_FOUND";
        case STTP_ERROR_OPERATION_FAILED:       return "STTP_ERROR_OPERATION_FAILED";
        case STTP_ERROR_NOT_SUPPORTED_FEATURE:  return "STTP_ERROR_NOT_SUPPORTED_FEATURE";
        default:
@@ -171,7 +176,7 @@ int stt_engine_load(int engine_id, const char* filepath)
                free(engine->pdfuncs);
                free(engine->engine_path);
                free(engine);
-               return STTP_ERROR_OPERATION_FAILED;
+               return ret;
        }
 
        /* engine error check */
@@ -206,7 +211,7 @@ int stt_engine_load(int engine_id, const char* filepath)
                return STTP_ERROR_OPERATION_FAILED;
        }
 
-       SECURE_SLOG(LOG_DEBUG, stt_tag(), "[Engine Success] Load engine : version(%d), size(%d)", engine->pefuncs->version, engine->pefuncs->size);
+       SLOG(LOG_DEBUG, stt_tag(), "[Engine Success] Load engine : version(%d), size(%d)", engine->pefuncs->version, engine->pefuncs->size);
 
        g_engine_list = g_slist_append(g_engine_list, engine);
 
@@ -258,10 +263,9 @@ int stt_engine_initialize(int engine_id, sttpe_result_cb result_cb, sttpe_silenc
        ret = engine->pefuncs->initialize(result_cb, silence_cb);
        if (0 != ret) {
                SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to initialize : %s", __stt_get_engine_error_code(ret));
-               return STTP_ERROR_OPERATION_FAILED;
        }
 
-       return 0;
+       return ret;
 }
 
 int stt_engine_deinitialize(int engine_id)
@@ -279,7 +283,7 @@ int stt_engine_deinitialize(int engine_id)
                SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] Fail to deinitialize : %s", __stt_get_engine_error_code(ret));
        }
 
-       return 0;
+       return ret;
 }
 
 static bool __supported_language_cb(const char* language, void* user_data)
@@ -317,10 +321,9 @@ int stt_engine_get_supported_langs(int engine_id, GSList** lang_list)
        ret = engine->pefuncs->foreach_langs(__supported_language_cb, (void*)lang_list);
        if (0 != ret) {
                SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] get language list error : %s", __stt_get_engine_error_code(ret));
-               return STTP_ERROR_OPERATION_FAILED;
        }
 
-       return 0;
+       return ret;
 }
 
 int stt_engine_is_valid_language(int engine_id, const char* language, bool *is_valid)
@@ -345,6 +348,53 @@ int stt_engine_is_valid_language(int engine_id, const char* language, bool *is_v
        return 0;
 }
 
+int stt_engine_set_private_data(int engine_id, const char* key, const char* data)
+{
+       if (NULL == key || NULL == data) {
+               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
+               return STTP_ERROR_INVALID_PARAMETER;
+       }
+
+       sttengine_s* engine = NULL;
+       engine = __get_engine(engine_id);
+       if (NULL == engine) {
+               SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
+               return STTP_ERROR_INVALID_PARAMETER;
+       }
+
+       int ret = engine->pefuncs->set_private_data(key, data);
+       if (0 != ret) {
+               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to set private data(%d)", ret);
+       }
+       return ret;
+}
+
+int stt_engine_get_private_data(int engine_id, const char* key, char** data)
+{
+       if (NULL == key || NULL == data) {
+               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
+               return STTP_ERROR_INVALID_PARAMETER;
+       }
+
+       sttengine_s* engine = NULL;
+       engine = __get_engine(engine_id);
+       if (NULL == engine) {
+               SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
+               return STTP_ERROR_INVALID_PARAMETER;
+       }
+
+       char* temp = NULL;
+       int ret = engine->pefuncs->get_private_data(key, &temp);
+       if (0 != ret) {
+               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to get private data(%d)", ret);
+               return ret;
+       }
+
+       *data = strdup(temp);
+
+       return STTP_ERROR_NONE;
+}
+
 int stt_engine_get_first_language(int engine_id, char** language)
 {
        if (NULL == language) {
@@ -364,7 +414,7 @@ int stt_engine_get_first_language(int engine_id, char** language)
        ret = engine->pefuncs->foreach_langs(__supported_language_cb, &lang_list);
        if (0 != ret) {
                SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] get language list error : %s", __stt_get_engine_error_code(ret));
-               return STTP_ERROR_OPERATION_FAILED;
+               return ret;
        }
 
        GSList *iter = NULL;
@@ -422,6 +472,31 @@ int stt_engine_support_silence(int engine_id, bool* support)
        return 0;
 }
 
+
+int stt_engine_need_app_credential(int engine_id, bool* need)
+{
+       if (NULL == need) {
+               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
+               return STTP_ERROR_INVALID_PARAMETER;
+       }
+
+       sttengine_s* engine = NULL;
+       engine = __get_engine(engine_id);
+       if (NULL == engine) {
+               SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
+               return STTP_ERROR_INVALID_PARAMETER;
+       }
+
+       bool result;
+       if (NULL != engine->pefuncs->need_app_credential) {
+               result = engine->pefuncs->need_app_credential();
+               *need = result;
+               return STTP_ERROR_NONE;
+       }
+
+       return STTP_ERROR_OPERATION_FAILED;
+}
+
 int stt_engine_support_recognition_type(int engine_id, const char* type, bool* support)
 {
        if (NULL == type || NULL == support) {
@@ -464,10 +539,9 @@ int stt_engine_get_audio_type(int engine_id, sttp_audio_type_e* types, int* rate
        ret = engine->pefuncs->get_audio_format(types, rate, channels);
        if (0 != ret) {
                SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to get audio format : %s", __stt_get_engine_error_code(ret));
-               return STTP_ERROR_OPERATION_FAILED;
        }
 
-       return 0;
+       return ret;
 }
 
 /* Set option */
@@ -486,10 +560,9 @@ int stt_engine_set_silence_detection(int engine_id, bool value)
                return STTP_ERROR_NOT_SUPPORTED_FEATURE;
        } else if (0 != ret) {
                SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to set silence detection : %d", ret);
-               return STTP_ERROR_OPERATION_FAILED;
        }
 
-       return 0;
+       return ret;
 }
 
 int stt_engine_check_app_agreed(int engine_id, const char* appid, bool* value)
@@ -511,14 +584,13 @@ int stt_engine_check_app_agreed(int engine_id, const char* appid, bool* value)
        if (0 != ret) {
                SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to get app agreement : %s", __stt_get_engine_error_code(ret));
                *value = false;
-               return STTP_ERROR_OPERATION_FAILED;
        }
 
-       return 0;
+       return ret;
 }
 
 /* Recognition */
-int stt_engine_recognize_start(int engine_id, const char* lang, const char* recognition_type, void* user_param)
+int stt_engine_recognize_start(int engine_id, const char* lang, const char* recognition_type, const char* credential, void* user_param)
 {
        if (NULL == lang || NULL == recognition_type) {
                SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
@@ -532,13 +604,13 @@ int stt_engine_recognize_start(int engine_id, const char* lang, const char* reco
                return STTP_ERROR_INVALID_PARAMETER;
        }
 
-       int ret = engine->pefuncs->start(lang, recognition_type, user_param);
+       int ret = engine->pefuncs->start(lang, recognition_type, credential, user_param);
        if (0 != ret) {
                SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to start recognition : %s", __stt_get_engine_error_code(ret));
-               return STTP_ERROR_OPERATION_FAILED;
+               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to start recognition : lang(%s), recognition_type(%s), credential(%s)", lang, recognition_type, credential);
        }
 
-       return 0;
+       return ret;
 }
 
 int stt_engine_set_recording_data(int engine_id, const void* data, unsigned int length)
@@ -558,10 +630,9 @@ int stt_engine_set_recording_data(int engine_id, const void* data, unsigned int
        int ret = engine->pefuncs->set_recording(data, length);
        if (0 != ret) {
                SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] Fail to set recording : %s", __stt_get_engine_error_code(ret));
-               return STTP_ERROR_OPERATION_FAILED;
        }
 
-       return 0;
+       return ret;
 }
 
 int stt_engine_recognize_stop(int engine_id)
@@ -576,10 +647,9 @@ int stt_engine_recognize_stop(int engine_id)
        int ret = engine->pefuncs->stop();
        if (0 != ret) {
                SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to stop : %s", __stt_get_engine_error_code(ret));
-               return STTP_ERROR_OPERATION_FAILED;
        }
 
-       return 0;
+       return ret;
 }
 
 int stt_engine_recognize_cancel(int engine_id)
@@ -594,10 +664,9 @@ int stt_engine_recognize_cancel(int engine_id)
        int ret = engine->pefuncs->cancel();
        if (0 != ret) {
                SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to cancel : %s", __stt_get_engine_error_code(ret));
-               return STTP_ERROR_OPERATION_FAILED;
        }
 
-       return 0;
+       return ret;
 }
 
 int stt_engine_foreach_result_time(int engine_id, void* time_info, sttpe_result_time_cb callback, void* user_data)
@@ -612,10 +681,9 @@ int stt_engine_foreach_result_time(int engine_id, void* time_info, sttpe_result_
        int ret = engine->pefuncs->foreach_result_time(time_info, callback, user_data);
        if (0 != ret) {
                SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to foreach result time : %s", __stt_get_engine_error_code(ret));
-               return STTP_ERROR_OPERATION_FAILED;
        }
 
-       return 0;
+       return ret;
 }
 
 int stt_engine_recognize_start_file(int engine_id, const char* lang, const char* recognition_type, 
@@ -641,10 +709,9 @@ int stt_engine_recognize_start_file(int engine_id, const char* lang, const char*
        int ret = engine->pefuncs->start_file(lang, recognition_type, filepath, audio_type, sample_rate, user_param);
        if (0 != ret) {
                SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to start file recognition : %s", __stt_get_engine_error_code(ret));
-               return STTP_ERROR_OPERATION_FAILED;
        }
 
-       return 0;
+       return ret;
 }
 
 int stt_engine_recognize_cancel_file(int engine_id)
@@ -664,8 +731,7 @@ int stt_engine_recognize_cancel_file(int engine_id)
        int ret = engine->pefuncs->cancel_file();
        if (0 != ret) {
                SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to start file recognition : %s", __stt_get_engine_error_code(ret));
-               return STTP_ERROR_OPERATION_FAILED;
        }
 
-       return 0;
-}
\ No newline at end of file
+       return ret;
+}