Fix bugs
[platform/core/uifw/stt.git] / server / sttd_engine_agent.c
index 30c498a..a00e33e 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
@@ -51,6 +51,7 @@ typedef struct _sttengine_info {
        char*   first_lang;
        bool    silence_detection;
        bool    support_silence_detection;
+       bool    need_credential;
 } sttengine_info_s;
 
 /** stt engine agent init */
@@ -77,17 +78,17 @@ static silence_dectection_callback g_silence_cb;
 
 
 /** callback functions */
-void __result_cb(sttp_result_event_e event, const char* type, const char** data, int data_count, 
+void __result_cb(sttp_result_event_e event, const char* type, const char** data, int data_count,
                 const char* msg, void* time_info, void *user_data);
 
-bool __result_time_cb(int index, sttp_result_time_event_e event, const char* text, 
+bool __result_time_cb(int index, sttp_result_time_event_e event, const char* text,
                      long start_time, long end_time, void* user_data);
 
 void __detect_silence_cb(sttp_silence_type_e type, void* user_data);
 
 bool __supported_language_cb(const char* language, void* user_data);
 
-void __engine_info_cb(const char* engine_uuid, const char* engine_name, const char* setting_ug_name, 
+void __engine_info_cb(const char* engine_uuid, const char* engine_name, const char* setting_ug_name,
                      bool use_network, void* user_data);
 
 /*
@@ -102,7 +103,7 @@ int __log_enginelist();
 /*
 * STT Engine Agent Interfaces
 */
-int sttd_engine_agent_init(result_callback result_cb, result_time_callback time_cb, 
+int sttd_engine_agent_init(result_callback result_cb, result_time_callback time_cb,
                           silence_dectection_callback silence_cb)
 {
        /* initialize static data */
@@ -220,7 +221,7 @@ int sttd_engine_agent_release()
        return 0;
 }
 
-void __engine_info_cb(const char* engine_uuid, const char* engine_name, const char* setting_ug_name, 
+void __engine_info_cb(const char* engine_uuid, const char* engine_name, const char* setting_ug_name,
                      bool use_network, void* user_data)
 {
        sttengine_info_s* temp = (sttengine_info_s*)user_data;
@@ -245,7 +246,7 @@ int __internal_get_engine_info(const char* filepath, sttengine_info_s** info)
        handle = dlopen(filepath, RTLD_LAZY);
 
        if (!handle) {
-               SECURE_SLOG(LOG_WARN, TAG_STTD, "[Engine Agent] Invalid engine : %s", filepath);
+               SECURE_SLOG(LOG_WARN, TAG_STTD, "[Engine Agent] Invalid engine : %s, error(%s)", filepath, (NULL == dlerror()) ? "NULL" : dlerror());
                return STTD_ERROR_ENGINE_NOT_FOUND;
        }
 
@@ -387,47 +388,6 @@ int sttd_engine_agent_initialize_engine_list()
                SLOG(LOG_WARN, TAG_STTD, "[Engine Agent WARNING] Fail to open default directory");
        }
 
-       /* Get file name from downloadable engine directory */
-       dp  = opendir(STT_DOWNLOAD_ENGINE);
-       if (NULL != dp) {
-               do {
-                       ret = readdir_r(dp, &entry, &dirp);
-                       if (0 != ret) {
-                               SLOG(LOG_ERROR, TAG_STTD, "[File ERROR] Fail to read directory");
-                               break;
-                       }
-
-                       if (NULL != dirp) {
-                               sttengine_info_s* info;
-                               char* filepath;
-                               int filesize;
-
-                               filesize = strlen(STT_DOWNLOAD_ENGINE) + strlen(dirp->d_name) + 5;
-                               filepath = (char*)calloc(filesize, sizeof(char));
-
-                               if (NULL != filepath) {
-                                       snprintf(filepath, filesize, "%s/%s", STT_DOWNLOAD_ENGINE, dirp->d_name);
-                               } else {
-                                       SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Memory not enouth!!");
-                                       continue;
-                               }
-
-                               /* get its info and update engine list */
-                               if (0 == __internal_get_engine_info(filepath, &info)) {
-                                       /* add engine info to g_engine_list */
-                                       g_engine_list = g_slist_append(g_engine_list, info);
-                               }
-
-                               if (NULL != filepath)
-                                       free(filepath);
-                       }
-               } while (NULL != dirp);
-
-               closedir(dp);
-       } else {
-               SLOG(LOG_WARN, TAG_STTD, "[Engine Agent WARNING] Fail to open downloadable directory");
-       }
-
        if (0 >= g_slist_length(g_engine_list)) {
                SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] No Engine");
                return STTD_ERROR_ENGINE_NOT_FOUND;
@@ -695,13 +655,13 @@ int sttd_engine_agent_load_current_engine(int uid, const char* engine_uuid)
        ret = stt_engine_load(engine->engine_id, engine->engine_path);
        if (0 != ret) {
                SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to load engine : id(%d) path(%s)", engine->engine_id, engine->engine_path);
-               return STTD_ERROR_OPERATION_FAILED;
+               return ret;
        }
 
        ret = stt_engine_initialize(engine->engine_id, __result_cb, __detect_silence_cb);
        if (0 != ret) {
                SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to initialize engine : id(%d) path(%s)", engine->engine_id, engine->engine_path);
-               return STTD_ERROR_OPERATION_FAILED;
+               return ret;
        }
 
        ret = stt_engine_set_silence_detection(engine->engine_id, g_default_silence_detected);
@@ -722,7 +682,7 @@ int sttd_engine_agent_load_current_engine(int uid, const char* engine_uuid)
                free(tmp_lang);
        } else {
                SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to get first language from engine : %d %s", engine->engine_id, engine->engine_name);
-               return STTD_ERROR_OPERATION_FAILED;
+               return ret;
        }
 
 #ifndef AUDIO_CREATE_ON_START
@@ -734,13 +694,13 @@ int sttd_engine_agent_load_current_engine(int uid, const char* engine_uuid)
        ret = stt_engine_get_audio_type(engine->engine_id, &atype, &rate, &channels);
        if (0 != ret) {
                SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to get audio type : %d %s", engine->engine_id, engine->engine_name);
-               return STTD_ERROR_OPERATION_FAILED;
+               return ret;
        }
 
-       ret = sttd_recorder_create(engine->engine_id, atype, channels, rate);
+       ret = sttd_recorder_create(engine->engine_id, uid, atype, channels, rate);
        if (0 != ret) {
                SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to create recorder : %d %s", engine->engine_id, engine->engine_name);
-               return STTD_ERROR_OPERATION_FAILED;
+               return ret;
        }
 #endif
 
@@ -915,10 +875,9 @@ int sttd_engine_agent_supported_langs(int uid, GSList** lang_list)
        int ret = stt_engine_get_supported_langs(engine->engine_id, lang_list);
        if (0 != ret) {
                SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] get language list error(%d)", ret);
-               return STTD_ERROR_OPERATION_FAILED;
        }
 
-       return 0;
+       return ret;
 }
 
 int sttd_engine_agent_get_default_lang(int uid, char** lang)
@@ -948,9 +907,11 @@ int sttd_engine_agent_get_default_lang(int uid, char** lang)
 
        /* get default language */
        bool is_valid = false;
-       if (0 != stt_engine_is_valid_language(engine->engine_id, g_default_language, &is_valid)) {
+       int ret = -1;
+       ret = stt_engine_is_valid_language(engine->engine_id, g_default_language, &is_valid);
+       if (0 != ret) {
                SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to check valid language");
-               return STTD_ERROR_OPERATION_FAILED;
+               return ret;
        }
 
        if (true == is_valid) {
@@ -961,6 +922,76 @@ int sttd_engine_agent_get_default_lang(int uid, char** lang)
        return 0;
 }
 
+int sttd_engine_agent_set_private_data(int uid, const char* key, const char* data)
+{
+       if (false == g_agent_init) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized");
+               return STTD_ERROR_OPERATION_FAILED;
+       }
+
+       if (NULL == key || NULL == data) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Invalid Parameter");
+               return STTD_ERROR_INVALID_PARAMETER;
+       }
+
+       sttengine_info_s* engine = NULL;
+       engine = __engine_agent_get_engine_by_uid(uid);
+
+       if (NULL == engine) {
+               SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] The engine of uid(%d) is not valid", uid);
+               return STTD_ERROR_INVALID_PARAMETER;
+       }
+
+       if (false == engine->is_loaded) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not loaded engine");
+               return STTD_ERROR_OPERATION_FAILED;
+       }
+
+       /* set private data */
+       int ret = -1;
+       ret = stt_engine_set_private_data(engine->engine_id, key, data);
+       if (0 != ret) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to set private data");
+       }
+
+       return ret;
+}
+
+int sttd_engine_agent_get_private_data(int uid, const char* key, char** data)
+{
+       if (false == g_agent_init) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized");
+               return STTD_ERROR_OPERATION_FAILED;
+       }
+
+       if (NULL == key || NULL == data) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Invalid Parameter");
+               return STTD_ERROR_INVALID_PARAMETER;
+       }
+
+       sttengine_info_s* engine = NULL;
+       engine = __engine_agent_get_engine_by_uid(uid);
+
+       if (NULL == engine) {
+               SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] The engine of uid(%d) is not valid", uid);
+               return STTD_ERROR_INVALID_PARAMETER;
+       }
+
+       if (false == engine->is_loaded) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not loaded engine");
+               return STTD_ERROR_OPERATION_FAILED;
+       }
+
+       /* get default language */
+       int ret = -1;
+       ret = stt_engine_get_private_data(engine->engine_id, key, data);
+       if (0 != ret) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to get private data");
+       }
+
+       return ret;
+}
+
 int sttd_engine_agent_get_option_supported(int uid, bool* silence)
 {
        if (false == g_agent_init) {
@@ -991,6 +1022,44 @@ int sttd_engine_agent_get_option_supported(int uid, bool* silence)
        return 0;
 }
 
+int sttd_engine_agent_is_credential_needed(int uid, bool* credential)
+{
+       if (false == g_agent_init) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized");
+               return STTD_ERROR_OPERATION_FAILED;
+       }
+
+       if (NULL == credential) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Invalid Parameter");
+               return STTD_ERROR_INVALID_PARAMETER;
+       }
+
+       sttengine_info_s* engine = NULL;
+       engine = __engine_agent_get_engine_by_uid(uid);
+
+       if (NULL == engine) {
+               SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] uid(%d) is not valid", uid);
+               return STTD_ERROR_INVALID_PARAMETER;
+       }
+
+       if (false == engine->is_loaded) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not loaded engine");
+               return STTD_ERROR_OPERATION_FAILED;
+       }
+
+       bool temp = false;
+       int ret;
+
+       ret = stt_engine_need_app_credential(engine->engine_id, &temp);
+       if (0 != ret) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to get to support recognition type : %d", ret);
+               return ret;
+       }
+
+       *credential = temp;
+       return 0;
+}
+
 int sttd_engine_agent_is_recognition_type_supported(int uid, const char* type, bool* support)
 {
        if (false == g_agent_init) {
@@ -1022,7 +1091,7 @@ int sttd_engine_agent_is_recognition_type_supported(int uid, const char* type, b
        ret = stt_engine_support_recognition_type(engine->engine_id, type, &temp);
        if (0 != ret) {
                SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to get to support recognition type : %d", ret);
-               return STTD_ERROR_OPERATION_FAILED;
+               return ret;
        }
 
        *support = temp;
@@ -1066,8 +1135,8 @@ int __set_option(sttengine_info_s* engine, int silence)
        return 0;
 }
 
-int sttd_engine_agent_recognize_start_engine(int uid, const char* lang, const char* recognition_type, 
-                                     int silence, void* user_param)
+int sttd_engine_agent_recognize_start_engine(int uid, const char* lang, const char* recognition_type,
+                                     int silence, const char* credential, void* user_param)
 {
        if (false == g_agent_init) {
                SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized");
@@ -1103,9 +1172,10 @@ int sttd_engine_agent_recognize_start_engine(int uid, const char* lang, const ch
        char* temp = NULL;
        if (0 == strncmp(lang, "default", strlen("default"))) {
                bool is_valid = false;
-               if (0 != stt_engine_is_valid_language(engine->engine_id, g_default_language, &is_valid)) {
+               ret = stt_engine_is_valid_language(engine->engine_id, g_default_language, &is_valid);
+               if (0 != ret) {
                        SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to check valid language");
-                       return STTD_ERROR_OPERATION_FAILED;
+                       return ret;
                }
 
                if (true == is_valid) {
@@ -1121,12 +1191,12 @@ int sttd_engine_agent_recognize_start_engine(int uid, const char* lang, const ch
 
        SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Start engine");
 
-       ret = stt_engine_recognize_start(engine->engine_id, temp, recognition_type, user_param);
+       ret = stt_engine_recognize_start(engine->engine_id, temp, recognition_type, credential, user_param);
        if (NULL != temp)       free(temp);
        if (0 != ret) {
                SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Recognition start error(%d)", ret);
                sttd_recorder_destroy(engine->engine_id);
-               return STTD_ERROR_OPERATION_FAILED;
+               return ret;
        }
 
 #ifdef AUDIO_CREATE_ON_START
@@ -1138,15 +1208,15 @@ int sttd_engine_agent_recognize_start_engine(int uid, const char* lang, const ch
        ret = stt_engine_get_audio_type(engine->engine_id, &atype, &rate, &channels);
        if (0 != ret) {
                SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to get audio type : %d %s", engine->engine_id, engine->engine_name);
-               return STTD_ERROR_OPERATION_FAILED;
+               return ret;
        }
 
        SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Create recorder");
 
-       ret = sttd_recorder_create(engine->engine_id, atype, channels, rate);
+       ret = sttd_recorder_create(engine->engine_id, uid, atype, channels, rate);
        if (0 != ret) {
                SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to create recorder : %d %s", engine->engine_id, engine->engine_name);
-               return STTD_ERROR_OPERATION_FAILED;
+               return ret;
        }
 #endif
 
@@ -1226,10 +1296,9 @@ int sttd_engine_agent_set_recording_data(int uid, const void* data, unsigned int
        int ret = stt_engine_set_recording_data(engine->engine_id, data, length);
        if (0 != ret) {
                SLOG(LOG_WARN, TAG_STTD, "[Engine Agent WARNING] set recording error(%d)", ret);
-               return STTD_ERROR_OPERATION_FAILED;
        }
 
-       return 0;
+       return ret;
 }
 
 int sttd_engine_agent_recognize_stop_recorder(int uid)
@@ -1257,7 +1326,7 @@ int sttd_engine_agent_recognize_stop_recorder(int uid)
        ret = sttd_recorder_stop(engine->engine_id);
        if (0 != ret) {
                SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to stop recorder : result(%d)", ret);
-               return STTD_ERROR_OPERATION_FAILED;
+               return ret;
        }
 
 #ifdef AUDIO_CREATE_ON_START
@@ -1296,7 +1365,7 @@ int sttd_engine_agent_recognize_stop_engine(int uid)
        ret = stt_engine_recognize_stop(engine->engine_id);
        if (0 != ret) {
                SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] stop recognition error(%d)", ret);
-               return STTD_ERROR_OPERATION_FAILED;
+               return ret;
        }
 
        SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent Success] Stop engine");
@@ -1330,7 +1399,7 @@ int sttd_engine_agent_recognize_cancel(int uid)
        ret = stt_engine_recognize_cancel(engine->engine_id);
        if (0 != ret) {
                SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] cancel recognition error(%d)", ret);
-               return STTD_ERROR_OPERATION_FAILED;
+               return ret;
        }
 
        SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Stop recorder");
@@ -1338,7 +1407,7 @@ int sttd_engine_agent_recognize_cancel(int uid)
        ret = sttd_recorder_stop(engine->engine_id);
        if (0 != ret) {
                SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to stop recorder : result(%d)", ret);
-               return STTD_ERROR_OPERATION_FAILED;
+               return ret;
        }
 
 #ifdef AUDIO_CREATE_ON_START
@@ -1396,11 +1465,11 @@ int sttd_engine_agent_set_default_engine(const char* engine_uuid)
                        data = iter->data;
 
                        if (true == data->use_default_engine) {
-                               SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] uid(%d) change engine from id(%d) to id(%d)", 
+                               SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] uid(%d) change engine from id(%d) to id(%d)",
                                        data->uid, data->engine_id, engine->engine_id);
 
                                if (0 != sttd_engine_agent_load_current_engine(data->uid, NULL)) {
-                                       SECURE_SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to load current engine : uid(%d)", data->uid);
+                                       SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Fail to load current engine : uid(%d)", data->uid);
                                }
                        }
 
@@ -1446,7 +1515,7 @@ int sttd_engine_agent_set_silence_detection(bool value)
 int sttd_engine_agent_check_app_agreed(int uid, const char* appid, bool* result)
 {
        if (false == g_agent_init) {
-               SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized"); 
+               SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] Not Initialized");
                return STTD_ERROR_OPERATION_FAILED;
        }
 
@@ -1467,7 +1536,7 @@ int sttd_engine_agent_check_app_agreed(int uid, const char* appid, bool* result)
        ret = stt_engine_check_app_agreed(engine->engine_id, appid, result);
        if (0 != ret) {
                SLOG(LOG_ERROR, TAG_STTD, "[Engine Agent ERROR] cancel recognition error(%d)", ret);
-               return STTD_ERROR_OPERATION_FAILED;
+               return ret;
        }
 
 
@@ -1475,11 +1544,27 @@ int sttd_engine_agent_check_app_agreed(int uid, const char* appid, bool* result)
        return 0;
 }
 
+static void __recorder_destroy(void* data)
+{
+       sttp_result_event_e event = (sttp_result_event_e)data;
+
+       SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Destroy recorder by ecore_thread_safe func");
+
+       if (0 != sttd_recorder_destroy(g_recording_engine_id)) {
+               SECURE_SLOG(LOG_WARN, TAG_STTD, "[Engine Agent] Fail to destroy recorder(%d)", g_recording_engine_id);
+       }
+       
+       if (event == STTP_RESULT_EVENT_FINAL_RESULT || event == STTP_RESULT_EVENT_ERROR) {
+               g_recording_engine_id = -1;
+       }
+}
+
+
 /*
 * STT Engine Callback Functions                                                                                        `                                 *
 */
 
-void __result_cb(sttp_result_event_e event, const char* type, const char** data, int data_count, 
+void __result_cb(sttp_result_event_e event, const char* type, const char** data, int data_count,
                 const char* msg, void* time_info, void *user_data)
 {
        if (false == g_agent_init) {
@@ -1503,16 +1588,21 @@ void __result_cb(sttp_result_event_e event, const char* type, const char** data,
 
 #ifdef AUDIO_CREATE_ON_START
        if (event == STTP_RESULT_EVENT_ERROR) {
+#if 1
+               ecore_main_loop_thread_safe_call_async(__recorder_destroy, (void*)event);
+#else
                SLOG(LOG_DEBUG, TAG_STTD, "[Engine Agent] Destroy recorder");
                if (0 != sttd_recorder_destroy(g_recording_engine_id))
                        SECURE_SLOG(LOG_WARN, TAG_STTD, "[Engine Agent] Fail to destroy recorder(%d)", g_recording_engine_id);
+#endif
        }
 #endif
 
+#ifndef AUDIO_CREATE_ON_START
        if (event == STTP_RESULT_EVENT_FINAL_RESULT || event == STTP_RESULT_EVENT_ERROR) {
                g_recording_engine_id = -1;
        }
-
+#endif
        return;
 }
 
@@ -1570,4 +1660,4 @@ int __log_enginelist()
        }
 
        return 0;
-}
\ No newline at end of file
+}