Fix error return value properly
[platform/core/uifw/stt.git] / common / stt_engine.c
index 5605bb5..c2a8e16 100644 (file)
@@ -1,5 +1,5 @@
 /*
-*  Copyright (c) 2011-2014 Samsung Electronics Co., Ltd All Rights Reserved 
+*  Copyright (c) 2011-2014 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
@@ -34,7 +34,7 @@ typedef struct {
 
        int (*sttp_load_engine)(sttpd_funcs_s* pdfuncs, sttpe_funcs_s* pefuncs);
        int (*sttp_unload_engine)();
-}sttengine_s;
+} sttengine_s;
 
 extern const char* stt_tag();
 
@@ -43,14 +43,19 @@ static GSList *g_engine_list;
 
 static const char* __stt_get_engine_error_code(sttp_error_e err)
 {
-       switch(err) {
+       switch (err) {
        case STTP_ERROR_NONE:                   return "STTP_ERROR_NONE";
        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:
@@ -88,7 +93,7 @@ static sttengine_s* __get_engine(int engine_id)
 int stt_engine_load(int engine_id, const char* filepath)
 {
        if (NULL == filepath || engine_id < 0) {
-               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter"); 
+               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
                return STTP_ERROR_INVALID_PARAMETER;
        }
 
@@ -147,7 +152,7 @@ int stt_engine_load(int engine_id, const char* filepath)
                return STTP_ERROR_OPERATION_FAILED;
        }
 
-       engine->sttp_load_engine = (int (*)(sttpd_funcs_s*, sttpe_funcs_s*) )dlsym(engine->handle, "sttp_load_engine");
+       engine->sttp_load_engine = (int (*)(sttpd_funcs_s*, sttpe_funcs_s*))dlsym(engine->handle, "sttp_load_engine");
        if (NULL != (error = dlerror()) || NULL == engine->sttp_load_engine) {
                SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to link daemon to sttp_load_engine() : %s", error);
                dlclose(engine->handle);
@@ -162,8 +167,8 @@ int stt_engine_load(int engine_id, const char* filepath)
 
        engine->pdfuncs->version = 1;
        engine->pdfuncs->size = sizeof(sttpd_funcs_s);
-       
-       int ret = engine->sttp_load_engine(engine->pdfuncs, engine->pefuncs); 
+
+       int ret = engine->sttp_load_engine(engine->pdfuncs, engine->pefuncs);
        if (0 != ret) {
                SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail sttp_load_engine() : %s", __stt_get_engine_error_code(ret));
                dlclose(engine->handle);
@@ -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 */
@@ -203,10 +208,10 @@ int stt_engine_load(int engine_id, const char* filepath)
                free(engine->engine_path);
                free(engine);
 
-               return STTP_ERROR_OPERATION_FAILED; 
+               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);
 
@@ -226,7 +231,7 @@ int stt_engine_unload(int engine_id)
        /* unload engine */
        engine->sttp_unload_engine();
        dlclose(engine->handle);
-       
+
        if (NULL != engine->engine_path)        free(engine->engine_path);
        if (NULL != engine->pefuncs)            free(engine->pefuncs);
        if (NULL != engine->pdfuncs)            free(engine->pdfuncs);
@@ -234,7 +239,7 @@ int stt_engine_unload(int engine_id)
        g_engine_list = g_slist_remove(g_engine_list, engine);
 
        free(engine);
-       
+
        return 0;
 }
 
@@ -243,7 +248,7 @@ int stt_engine_unload(int engine_id)
 int stt_engine_initialize(int engine_id, sttpe_result_cb result_cb, sttpe_silence_detected_cb silence_cb)
 {
        if (NULL == result_cb || NULL == silence_cb) {
-               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter"); 
+               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
                return STTP_ERROR_INVALID_PARAMETER;
        }
 
@@ -258,7 +263,7 @@ 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 ret;
        }
 
        return 0;
@@ -302,7 +307,7 @@ static bool __supported_language_cb(const char* language, void* user_data)
 int stt_engine_get_supported_langs(int engine_id, GSList** lang_list)
 {
        if (NULL == lang_list) {
-               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter"); 
+               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
                return STTP_ERROR_INVALID_PARAMETER;
        }
 
@@ -317,7 +322,7 @@ 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 ret;
        }
 
        return 0;
@@ -326,7 +331,7 @@ int stt_engine_get_supported_langs(int engine_id, GSList** lang_list)
 int stt_engine_is_valid_language(int engine_id, const char* language, bool *is_valid)
 {
        if (NULL == language || NULL == is_valid) {
-               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter"); 
+               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
                return STTP_ERROR_INVALID_PARAMETER;
        }
 
@@ -336,7 +341,7 @@ int stt_engine_is_valid_language(int engine_id, const char* language, bool *is_v
                SECURE_SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] engine id(%d) is invalid", engine_id);
                return STTP_ERROR_INVALID_PARAMETER;
        }
-       
+
        bool result;
        result = engine->pefuncs->is_valid_lang(language);
 
@@ -348,7 +353,7 @@ int stt_engine_is_valid_language(int engine_id, const char* language, bool *is_v
 int stt_engine_get_first_language(int engine_id, char** language)
 {
        if (NULL == language) {
-               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter"); 
+               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
                return STTP_ERROR_INVALID_PARAMETER;
        }
 
@@ -364,7 +369,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;
@@ -404,7 +409,7 @@ int stt_engine_get_first_language(int engine_id, char** language)
 int stt_engine_support_silence(int engine_id, bool* support)
 {
        if (NULL == support) {
-               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter"); 
+               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
                return STTP_ERROR_INVALID_PARAMETER;
        }
 
@@ -422,10 +427,35 @@ 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) {
-               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter"); 
+               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
                return STTP_ERROR_INVALID_PARAMETER;
        }
 
@@ -449,7 +479,7 @@ int stt_engine_support_recognition_type(int engine_id, const char* type, bool* s
 int stt_engine_get_audio_type(int engine_id, sttp_audio_type_e* types, int* rate, int* channels)
 {
        if (NULL == types || NULL == rate || NULL == channels) {
-               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter"); 
+               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
                return STTP_ERROR_INVALID_PARAMETER;
        }
 
@@ -464,7 +494,7 @@ 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 ret;
        }
 
        return 0;
@@ -482,13 +512,13 @@ int stt_engine_set_silence_detection(int engine_id, bool value)
 
        int ret = engine->pefuncs->set_silence_detection(value);
        if (STTP_ERROR_NOT_SUPPORTED_FEATURE == ret) {
-               SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] Not support silence detection"); 
+               SLOG(LOG_WARN, stt_tag(), "[Engine WARNING] Not support silence detection");
                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;
+               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Fail to set silence detection : %d", ret);
+               return ret;
        }
-       
+
        return 0;
 }
 
@@ -511,17 +541,17 @@ 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 ret;
        }
-       
+
        return 0;
 }
 
 /* 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"); 
+               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
                return STTP_ERROR_INVALID_PARAMETER;
        }
 
@@ -532,10 +562,11 @@ 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 ret;
        }
 
        return 0;
@@ -544,7 +575,7 @@ int stt_engine_recognize_start(int engine_id, const char* lang, const char* reco
 int stt_engine_set_recording_data(int engine_id, const void* data, unsigned int length)
 {
        if (NULL == data) {
-               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter"); 
+               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
                return STTP_ERROR_INVALID_PARAMETER;
        }
 
@@ -558,7 +589,7 @@ 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 ret;
        }
 
        return 0;
@@ -576,7 +607,7 @@ 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 ret;
        }
 
        return 0;
@@ -594,7 +625,7 @@ 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 ret;
        }
 
        return 0;
@@ -612,9 +643,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 ret;
        }
-       
+
        return 0;
 }
 
@@ -622,7 +653,7 @@ int stt_engine_recognize_start_file(int engine_id, const char* lang, const char*
                                     const char* filepath, sttp_audio_type_e audio_type, int sample_rate, void* user_param)
 {
        if (NULL == filepath || NULL == lang || NULL == recognition_type) {
-               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter"); 
+               SLOG(LOG_ERROR, stt_tag(), "[Engine ERROR] Invalid Parameter");
                return STTP_ERROR_INVALID_PARAMETER;
        }
 
@@ -641,7 +672,7 @@ 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 ret;
        }
 
        return 0;
@@ -664,7 +695,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 ret;
        }
 
        return 0;