- if (false == g_agent_init) {
- SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
- return TTSD_ERROR_OPERATION_FAILED;
- }
-
- /* unload current engine */
- ttsd_engine_agent_unload_current_engine();
-
- /* release engine list */
- GList *iter = NULL;
- ttsengine_info_s *data = NULL;
-
- if (g_list_length(g_engine_list) > 0) {
- /* Get a first item */
- iter = g_list_first(g_engine_list);
- while (NULL != iter) {
- /* Get data from item */
- data = iter->data;
- iter = g_list_remove(iter, data);
-
- if (NULL != data) {
- if (NULL != data->engine_uuid) free(data->engine_uuid);
- if (NULL != data->engine_name) free(data->engine_name);
- if (NULL != data->setting_ug_path) free(data->setting_ug_path);
- if (NULL != data->engine_path) free(data->engine_path);
- free(data);
- }
- }
- }
- g_list_free(iter);
-
- /* release current engine data */
- if (g_cur_engine.engine_uuid != NULL) free(g_cur_engine.engine_uuid);
- if (g_cur_engine.engine_name != NULL) free(g_cur_engine.engine_name);
- if (g_cur_engine.engine_path != NULL) free(g_cur_engine.engine_path);
-
- if (g_cur_engine.pefuncs != NULL) free(g_cur_engine.pefuncs);
- if (g_cur_engine.pdfuncs != NULL) free(g_cur_engine.pdfuncs);
- if (g_cur_engine.default_lang != NULL) free(g_cur_engine.default_lang);
- g_result_cb = NULL;
- g_agent_init = false;
-
- SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Release Engine Agent");
-
- return 0;
-}
-
-int ttsd_engine_agent_initialize_current_engine()
-{
- if (false == g_agent_init) {
- SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Not Initialized");
- return TTSD_ERROR_OPERATION_FAILED;
- }
-
- /* update engine list */
- if (0 != __internal_update_engine_list()) {
- SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] No engine error");
- return TTSD_ERROR_OPERATION_FAILED;
- }
-
- /* 2. get current engine from config */
- char* cur_engine_uuid = NULL;
- bool is_get_engineid_from_config = false;
-
- if (0 != ttsd_config_get_default_engine(&cur_engine_uuid)) {
- /*not set current engine */
- /*set system default engine*/
- GList *iter = NULL;
- ttsengine_info_s *data = NULL;
-
- if (g_list_length(g_engine_list) > 0) {
- iter = g_list_first(g_engine_list);
- if (NULL == iter) {
- SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] Fail to get a engine of engine list");
- return TTSD_ERROR_OPERATION_FAILED;
- }
- data = iter->data;
-
- if (NULL != data) {
- if (NULL != data->engine_uuid) {
- cur_engine_uuid = strdup(data->engine_uuid);
- ttsd_config_set_default_engine(cur_engine_uuid);
- } else {
- SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Data of current engine is corrupt");
- return TTSD_ERROR_OPERATION_FAILED;
- }
- }
- } else {
- SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] Fail to get a engine of engine list");
- return TTSD_ERROR_OPERATION_FAILED;
- }
-
- is_get_engineid_from_config = false;
- } else {
- is_get_engineid_from_config = true;
- }
-
- if (NULL == cur_engine_uuid) {
- SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] Current engine id is NULL");
- return TTSD_ERROR_OPERATION_FAILED;
- }
-
- /* check whether cur engine uuid is valid or not. */
- if (0 != __internal_check_engine_id(cur_engine_uuid)) {
- SLOG(LOG_WARN, get_tag(), "[Engine Agent WARNING] It is not valid engine id from config");
-
- GList *iter = NULL;
-
- if (g_list_length(g_engine_list) > 0)
- iter = g_list_first(g_engine_list);
- else {
- SLOG(LOG_WARN, get_tag(), "[Engine Agent ERROR] NO TTS Engine !!");
- if (NULL != cur_engine_uuid) {
- free(cur_engine_uuid);
- cur_engine_uuid = NULL;
- }
- return TTSD_ERROR_OPERATION_FAILED;
- }
-
- if (NULL == iter) {
- SLOG(LOG_WARN, get_tag(), "[Engine Agent ERROR] No valid TTS Engine");
- if (NULL != cur_engine_uuid) {
- free(cur_engine_uuid);
- cur_engine_uuid = NULL;
- }
- return TTSD_ERROR_OPERATION_FAILED;
- }
-
- if (cur_engine_uuid != NULL) {
- free(cur_engine_uuid);
- cur_engine_uuid = NULL;
- }
- ttsengine_info_s *data = NULL;
- data = iter->data;
-
- if (NULL == data || NULL == data->engine_uuid) {
- SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] No valid TTS Engine");
- return TTSD_ERROR_OPERATION_FAILED;
- }
-
- cur_engine_uuid = strdup(data->engine_uuid);
-
- is_get_engineid_from_config = false;
- }
-
- if (NULL != cur_engine_uuid)
- SECURE_SLOG(LOG_DEBUG, get_tag(), "[Engine Agent] Current Engine Id : %s", cur_engine_uuid);
- else
- return TTSD_ERROR_OPERATION_FAILED;
-
- /* set current engine */
- if (0 != __internal_set_current_engine(cur_engine_uuid)) {
- SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to set current engine ");
- if (NULL != cur_engine_uuid) free(cur_engine_uuid);
- return TTSD_ERROR_OPERATION_FAILED;
- }
-
- if (false == is_get_engineid_from_config) {
- if (0 != ttsd_config_set_default_engine(cur_engine_uuid)) {
- SLOG(LOG_ERROR, get_tag(), "[Engine Agent ERROR] fail to set id to config");
- }
- }
-
- if (NULL != cur_engine_uuid) free(cur_engine_uuid);
-
- SLOG(LOG_DEBUG, get_tag(), "[Engine Agent SUCCESS] Set current engine");
-
- return 0;
-}
-
-static int __internal_check_engine_id(const char* engine_uuid)
-{
- GList *iter = NULL;
- ttsengine_s *data = NULL;
-
- if (g_list_length(g_engine_list) > 0) {
- iter = g_list_first(g_engine_list);
-
- while (NULL != iter) {
- data = iter->data;
-
- if (0 == strncmp(engine_uuid, data->engine_uuid, strlen(data->engine_uuid)))
- return 0;
-
- iter = g_list_next(iter);
- }
- }
-
- return -1;
-}
-
-void __engine_info_cb(const char* engine_uuid, const char* engine_name, const char* setting_ug_name,
- bool use_network, void* user_data)
-{
- ttsengine_info_s* temp = (ttsengine_info_s*)user_data;
-
- if (NULL != engine_uuid)
- temp->engine_uuid = strdup(engine_uuid);
-
- if (NULL != engine_name)
- temp->engine_name = strdup(engine_name);
-
- if (NULL != setting_ug_name)
- temp->setting_ug_path = strdup(setting_ug_name);
-
- temp->use_network = use_network;
- return;
-}
-
-static int __internal_get_engine_info(const char* filepath, ttsengine_info_s** info)
-{
- char *error;
- void* handle;
-
- handle = dlopen(filepath, RTLD_LAZY);
-
- if (!handle) {
- SECURE_SLOG(LOG_WARN, get_tag(), "[Engine Agent] Invalid engine : %s", filepath);
- return TTSD_ERROR_OPERATION_FAILED;
- }
-
- /* link engine to daemon */
- dlsym(handle, "ttsp_load_engine");
- if ((error = dlerror()) != NULL) {
- SECURE_SLOG(LOG_WARN, get_tag(), "[Engine Agent] Fail to open ttsp_load_engine : path(%s) message(%s)", filepath, error);
- dlclose(handle);
- return TTSD_ERROR_OPERATION_FAILED;
- }
-
- dlsym(handle, "ttsp_unload_engine");
- if ((error = dlerror()) != NULL) {
- SECURE_SLOG(LOG_WARN, get_tag(), "[Engine Agent] Fail to open ttsp_unload_engine : path(%s) message(%s)", filepath, error);
- dlclose(handle);
- return TTSD_ERROR_OPERATION_FAILED;
- }
-
- int (*get_engine_info)(ttsp_engine_info_cb callback, void* user_data);
-
- get_engine_info = (int (*)(ttsp_engine_info_cb, void*))dlsym(handle, "ttsp_get_engine_info");
- if (NULL != (error = dlerror()) || NULL == get_engine_info) {
- SLOG(LOG_WARN, get_tag(), "[Engine Agent] Fail to open ttsp_get_engine_info() :path(%s) message(%s)", filepath, error);
- dlclose(handle);