From: Suyeon Hwang Date: Thu, 1 Dec 2022 09:54:49 +0000 (+0900) Subject: Make class for managing vc_config_s instance X-Git-Tag: submit/tizen/20230313.042534~12 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6b347e678bb94d999fd50f6503a4493844586641;p=platform%2Fcore%2Fuifw%2Fvoice-control.git Make class for managing vc_config_s instance - Requirement: g_config_info is shared memory, so thread safe access for g_config_info is needed. - Contents: This patch makes new class for accessing vc_config_s instance. Through this patch, all other functions in this module can access configuration property in a thread safe way using method of new class. This new class will protect vc_config_s property using mutex, so the class will assure to prevent race condition. Change-Id: Ifdf2c37874f9dfe53cc6f645149ec28036f8c0d2 Signed-off-by: Suyeon Hwang --- diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 1d144d2..4f9c4e2 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -8,6 +8,7 @@ SET(SRCS ../common/vc_command.c ../common/vc_command_util.c ../common/vc_config_mgr.cpp + ../common/VoiceControlConfig.cpp ../common/vc_config_parser.c ../common/vc_info_parser.c ../common/vc_json_parser.c @@ -18,6 +19,7 @@ SET(SETTING_SRCS vc_setting_proxy.c vc_setting.c ../common/vc_config_mgr.cpp + ../common/VoiceControlConfig.cpp ../common/vc_config_parser.c ) @@ -31,6 +33,7 @@ SET(WIDGET_SRCS ../common/vc_command.c ../common/vc_command_util.c ../common/vc_config_mgr.cpp + ../common/VoiceControlConfig.cpp ../common/vc_config_parser.c ../common/vc_info_parser.c ../common/vc_json_parser.c @@ -50,6 +53,7 @@ SET(MANAGER_SRCS ../common/vc_command.c ../common/vc_command_util.c ../common/vc_config_mgr.cpp + ../common/VoiceControlConfig.cpp ../common/vc_config_parser.c ../common/vc_info_parser.c ../common/vc_json_parser.c diff --git a/common/VoiceControlConfig.cpp b/common/VoiceControlConfig.cpp new file mode 100644 index 0000000..750d245 --- /dev/null +++ b/common/VoiceControlConfig.cpp @@ -0,0 +1,167 @@ +/* +* Copyright (c) 2022 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 +* http://www.apache.org/licenses/LICENSE-2.0 +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + + +#include +#include + +#include "vc_main.h" + +#include "VoiceControlConfig.h" + +using namespace std; + +VoiceControlConfig::VoiceControlConfig() +{ + unique_lock lock(__configMutex); + __config = nullptr; + + if (0 != vc_parser_load_config(&__config)) { + SLOG(LOG_ERROR, TAG_VCCONFIG, "[ERROR] Fail to parse configure information"); + throw new runtime_error("Operation failed"); + } +} + +VoiceControlConfig::~VoiceControlConfig() +{ + unique_lock lock(__configMutex); + vc_parser_unload_config(__config); + __config = nullptr; +} + +void VoiceControlConfig::printOutConfig() +{ + unique_lock lock(__configMutex); + SLOG(LOG_DEBUG, TAG_VCCONFIG, "Engine : %s", __config->engine_id); + SLOG(LOG_DEBUG, TAG_VCCONFIG, "Auto lang : %s", __config->auto_lang ? "true" : "false"); + SLOG(LOG_DEBUG, TAG_VCCONFIG, "Language : %s", __config->language); + SLOG(LOG_DEBUG, TAG_VCCONFIG, "Enabled : %s", __config->enabled ? "true" : "false"); +} + +void VoiceControlConfig::setEngineId(const std::string engineId) +{ + unique_lock lock(__configMutex); + if (nullptr == __config) { + SLOG(LOG_ERROR, TAG_VCCONFIG, "[ERROR] Config info is NULL"); + return; + } + + free(__config->engine_id); + if (engineId.empty()) { + __config->engine_id = nullptr; + } else { + __config->engine_id = strdup(engineId.c_str()); + } + SLOG(LOG_ERROR, TAG_VCCONFIG, "Set Engine ID : %s", __config->engine_id); +} + +std::string VoiceControlConfig::getEngineId() +{ + unique_lock lock(__configMutex); + if (nullptr == __config) { + SLOG(LOG_ERROR, TAG_VCCONFIG, "[ERROR] Config info is NULL"); + return ""; + } + + string result; + if (nullptr != __config->engine_id) { + result = __config->engine_id; + } + + SLOG(LOG_ERROR, TAG_VCCONFIG, "Get Engine ID : %s", result.c_str()); + return result; +} + +void VoiceControlConfig::setAutoLanguageEnabled(bool isAutoLanguageEnabled) +{ + unique_lock lock(__configMutex); + if (nullptr == __config) { + SLOG(LOG_ERROR, TAG_VCCONFIG, "[ERROR] Config info is NULL"); + return; + } + + __config->auto_lang = isAutoLanguageEnabled; + SLOG(LOG_ERROR, TAG_VCCONFIG, "Set Auto language : %s", (__config->auto_lang ? "True" : "False")); +} + +bool VoiceControlConfig::isAutoLanguageEnabled() +{ + unique_lock lock(__configMutex); + if (nullptr == __config) { + SLOG(LOG_ERROR, TAG_VCCONFIG, "[ERROR] Config info is NULL"); + return false; + } + + SLOG(LOG_ERROR, TAG_VCCONFIG, "Get Auto language : %s", (__config->auto_lang ? "True" : "False")); + return __config->auto_lang; +} + +void VoiceControlConfig::setCurrentLanguage(const std::string language) +{ + unique_lock lock(__configMutex); + if (nullptr == __config) { + SLOG(LOG_ERROR, TAG_VCCONFIG, "[ERROR] Config info is NULL"); + return; + } + + free(__config->language); + if (language.empty()) { + __config->language = nullptr; + } else { + __config->language = strdup(language.c_str()); + } + SLOG(LOG_ERROR, TAG_VCCONFIG, "Set Language : %s", __config->language); +} + +std::string VoiceControlConfig::getCurrentLanguage() +{ + unique_lock lock(__configMutex); + if (nullptr == __config) { + SLOG(LOG_ERROR, TAG_VCCONFIG, "[ERROR] Config info is NULL"); + return ""; + } + + string result; + if (nullptr != __config->language) { + result = __config->language; + } + SLOG(LOG_ERROR, TAG_VCCONFIG, "Get Language : %s", result.c_str()); + return result; +} + +void VoiceControlConfig::setEnabled(bool isEnabled) +{ + unique_lock lock(__configMutex); + if (nullptr == __config) { + SLOG(LOG_ERROR, TAG_VCCONFIG, "[ERROR] Config info is NULL"); + return; + } + + __config->enabled = isEnabled; + SLOG(LOG_ERROR, TAG_VCCONFIG, "Set Enabled : %s", (__config->enabled ? "True" : "False")); +} + +bool VoiceControlConfig::isEnabled() +{ + unique_lock lock(__configMutex); + if (nullptr == __config) { + SLOG(LOG_ERROR, TAG_VCCONFIG, "[ERROR] Config info is NULL"); + return false; + } + + SLOG(LOG_ERROR, TAG_VCCONFIG, "Get Enabled : %s", (__config->enabled ? "True" : "False")); + return __config->enabled; +} diff --git a/common/VoiceControlConfig.h b/common/VoiceControlConfig.h new file mode 100644 index 0000000..2f898e4 --- /dev/null +++ b/common/VoiceControlConfig.h @@ -0,0 +1,48 @@ +/* +* Copyright (c) 2022 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 +* http://www.apache.org/licenses/LICENSE-2.0 +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + + +#ifndef __VC_COMMON_VOICE_CONTROL_CONFIG_H_ +#define __VC_COMMON_VOICE_CONTROL_CONFIG_H_ + +#include +#include + +#include "vc_config_parser.h" + +class VoiceControlConfig { +public: + VoiceControlConfig(); + virtual ~VoiceControlConfig(); + + void printOutConfig(); + + void setEngineId(const std::string engineId); + std::string getEngineId(); + + void setAutoLanguageEnabled(bool isAutoLanguageEnabled); + bool isAutoLanguageEnabled(); + + void setCurrentLanguage(const std::string language); + std::string getCurrentLanguage(); + + void setEnabled(bool isEnabled); + bool isEnabled(); + +private: + vc_config_s *__config; + std::mutex __configMutex; +}; + + +#endif /* __VC_COMMON_VOICE_CONTROL_CONFIG_H_ */ diff --git a/common/vc_config_mgr.cpp b/common/vc_config_mgr.cpp index d81775f..2f7940c 100644 --- a/common/vc_config_mgr.cpp +++ b/common/vc_config_mgr.cpp @@ -28,6 +28,9 @@ #include #include #include +#include + +#include "VoiceControlConfig.h" #include "vc_config_mgr.h" #include "vc_config_parser.h" @@ -79,7 +82,7 @@ static GSList* g_engine_list = NULL; static GSList* g_config_client_list = NULL; -static vc_config_s* g_config_info; +static VoiceControlConfig *g_VoiceControlConfig = nullptr; static int g_lang_ref_count; static Ecore_Fd_Handler* g_fd_handler_lang = NULL; @@ -257,6 +260,23 @@ static void __vc_config_mgr_notify_enabled_changed(bool enable) g_slist_foreach(g_config_client_list, __invoke_enabled_cb_in_client_list, &enabled_cb_parameter); } +static int __initialize_config_info() +{ + g_VoiceControlConfig = new(nothrow) VoiceControlConfig(); + if (nullptr == g_VoiceControlConfig) { + SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to construct config"); + return VC_CONFIG_ERROR_OPERATION_FAILED; + } + + return VC_CONFIG_ERROR_NONE; +} + +static void __finalize_config_info() +{ + delete g_VoiceControlConfig; + g_VoiceControlConfig = nullptr; +} + int vc_config_convert_error_code(vc_config_error_e code) { if (code == VC_CONFIG_ERROR_NONE) return VC_ERROR_NONE; @@ -271,7 +291,7 @@ int vc_config_convert_error_code(vc_config_error_e code) return VC_CONFIG_ERROR_NONE; } -int __vc_config_mgr_check_engine_is_valid(const char* engine_id) +int __vc_config_mgr_check_and_set_default_engine_id(const char* engine_id) { if (NULL == engine_id) { SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Input parameter is NULL"); @@ -305,21 +325,12 @@ int __vc_config_mgr_check_engine_is_valid(const char* engine_id) return -1; } - if (NULL == g_config_info) { - SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Config info is NULL"); - return -1; - } - - if (NULL != g_config_info->engine_id) - free(g_config_info->engine_id); - - g_config_info->engine_id = strdup(engine_info->uuid); - - SLOG(LOG_DEBUG, vc_config_tag(), "Default engine is changed : %s", g_config_info->engine_id); - if (0 != vc_parser_set_engine(g_config_info->engine_id)) { + SLOG(LOG_DEBUG, vc_config_tag(), "Default engine is changed : %s", engine_info->uuid); + if (0 != vc_parser_set_engine(engine_info->uuid)) { SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to save config"); return -1; } + g_VoiceControlConfig->setEngineId(engine_info->uuid); return VC_ERROR_NONE; } @@ -549,11 +560,14 @@ static Eina_Bool __vc_config_mgr_engine_config_inotify_event_callback(void* data SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to get engine info when config updated"); } __vc_config_mgr_print_engine_info(); - bool is_language_valid = vc_config_check_default_language_is_valid(g_config_info->language); + string default_language = g_VoiceControlConfig->getCurrentLanguage(); + bool is_language_valid = vc_config_check_default_language_is_valid(default_language.c_str()); if (false == is_language_valid) { SLOG(LOG_DEBUG, vc_config_tag(), "[ERROR] Default language is not valid"); char* temp_lang = NULL; - ret = __vc_config_mgr_select_lang(g_config_info->engine_id, &temp_lang); + string default_engine_id = g_VoiceControlConfig->getEngineId(); + ret = __vc_config_mgr_select_lang(default_engine_id.c_str(), &temp_lang); + if (0 != ret) { SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to get language"); } @@ -562,7 +576,7 @@ static Eina_Bool __vc_config_mgr_engine_config_inotify_event_callback(void* data if (0 != ret) { SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to set language"); } else { - SLOG(LOG_DEBUG, vc_config_tag(), "[DEBUG] Saved default language : lang(%s)", g_config_info->language); + SLOG(LOG_DEBUG, vc_config_tag(), "[DEBUG] Saved default language : lang(%s)", default_language.c_str()); } if (NULL != temp_lang) { free(temp_lang); @@ -694,34 +708,24 @@ Eina_Bool vc_config_mgr_inotify_event_cb(void* data, Ecore_Fd_Handler *fd_handle return ECORE_CALLBACK_PASS_ON; if (-1 != auto_lang) { - g_config_info->auto_lang = auto_lang; + g_VoiceControlConfig->setAutoLanguageEnabled(auto_lang); } /* Only language changed */ if (NULL != lang) { - char* before_lang = NULL; - - before_lang = strdup(g_config_info->language); - - if (NULL != g_config_info->language) free(g_config_info->language); - g_config_info->language = strdup(lang); + string before_lang = g_VoiceControlConfig->getCurrentLanguage(); + g_VoiceControlConfig->setCurrentLanguage(lang); /* Call all callbacks of client*/ - __vc_config_mgr_notify_language_changed(before_lang, lang); + __vc_config_mgr_notify_language_changed(before_lang.c_str(), lang); - if (NULL != before_lang) free(before_lang); free(lang); lang = NULL; } /* Only Engine changed */ if (NULL != engine) { - - if (NULL != g_config_info->engine_id) { - free(g_config_info->engine_id); - g_config_info->engine_id = NULL; - } - g_config_info->engine_id = strdup(engine); + g_VoiceControlConfig->setEngineId(engine); /* Call all callbacks of client*/ __vc_config_mgr_notify_engine_changed(engine); @@ -730,7 +734,7 @@ Eina_Bool vc_config_mgr_inotify_event_cb(void* data, Ecore_Fd_Handler *fd_handle } if (-1 != enabled) { - g_config_info->enabled = enabled; + g_VoiceControlConfig->setEnabled((bool)enabled); /* Call all callbacks of client*/ __vc_config_mgr_notify_enabled_changed((bool)enabled); @@ -760,47 +764,32 @@ int __vc_config_set_auto_language() SLOG(LOG_DEBUG, vc_config_tag(), "[Config] Display language : %s", candidate_lang); - /* Check current config info */ - if (NULL == g_config_info) { - SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Current config info is NULL"); - return VC_CONFIG_ERROR_OPERATION_FAILED; - } - /* Check current language */ - if (NULL == g_config_info->language) { - SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Current config language is NULL"); - return VC_CONFIG_ERROR_OPERATION_FAILED; - } - - if (0 == strncmp(g_config_info->language, candidate_lang, 5)) { - SLOG(LOG_DEBUG, vc_config_tag(), "[Config] VC language(%s) is same with display language", g_config_info->language); + string default_language = g_VoiceControlConfig->getCurrentLanguage(); + if (0 == default_language.compare(candidate_lang)) { + SLOG(LOG_DEBUG, vc_config_tag(), "[Config] VC language(%s) is same with display language", default_language.c_str()); return VC_ERROR_NONE; } - if (true == __vc_config_mgr_check_lang_is_valid(g_config_info->engine_id, candidate_lang)) { + string default_engine_id = g_VoiceControlConfig->getEngineId(); + if (true == __vc_config_mgr_check_lang_is_valid(default_engine_id.c_str(), candidate_lang)) { /* vc default language change */ - char* before_lang = NULL; + string before_language = g_VoiceControlConfig->getCurrentLanguage(); if (0 != vc_parser_set_language(candidate_lang)) { SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to save default language"); return -1; } - - before_lang = strdup(g_config_info->language); - - free(g_config_info->language); - g_config_info->language = strdup(candidate_lang); + g_VoiceControlConfig->setCurrentLanguage(candidate_lang); SLOG(LOG_DEBUG, vc_config_tag(), "[Config] Default language change : before(%s) current(%s)", - before_lang, g_config_info->language); + before_language.c_str(), candidate_lang); /* Call all callbacks of client*/ - __vc_config_mgr_notify_language_changed(before_lang, g_config_info->language); - - if (NULL != before_lang) free(before_lang); + __vc_config_mgr_notify_language_changed(before_language.c_str(), candidate_lang); } else { /* Candidate language is not valid */ char* tmp_language = NULL; - if (0 != __vc_config_mgr_select_lang(g_config_info->engine_id, &tmp_language)) { + if (0 != __vc_config_mgr_select_lang(default_engine_id.c_str(), &tmp_language)) { SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to select language"); return -1; } @@ -810,26 +799,20 @@ int __vc_config_set_auto_language() return -1; } - char* before_lang = NULL; + string before_language = g_VoiceControlConfig->getCurrentLanguage(); if (0 != vc_parser_set_language(tmp_language)) { free(tmp_language); SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to save config"); return -1; } - - before_lang = strdup(g_config_info->language); - - free(g_config_info->language); - g_config_info->language = strdup(tmp_language); - free(tmp_language); + g_VoiceControlConfig->setCurrentLanguage(tmp_language); SLOG(LOG_DEBUG, vc_config_tag(), "[Config] Default language change : before(%s) current(%s)", - before_lang, g_config_info->language); + before_language.c_str(), tmp_language); /* Call all callbacks of client*/ - __vc_config_mgr_notify_language_changed(before_lang, g_config_info->language); - - if (NULL != before_lang) free(before_lang); + __vc_config_mgr_notify_language_changed(before_language.c_str(), tmp_language); + free(tmp_language); } return VC_ERROR_NONE; @@ -838,7 +821,7 @@ int __vc_config_set_auto_language() void __vc_config_language_changed_cb(keynode_t *key, void *data) { pthread_mutex_lock(&vc_config_mgr_mutex); - if (g_config_info && true == g_config_info->auto_lang) { + if (g_VoiceControlConfig->isAutoLanguageEnabled()) { /* Get voice input vconf key */ __vc_config_set_auto_language(); } @@ -899,47 +882,42 @@ static int __initialize_vc_config_mgr() __vc_config_mgr_print_engine_info(); - if (0 != vc_parser_load_config(&g_config_info)) { + if (0 != __initialize_config_info()) { SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to parse configure information"); __vc_config_release_engine(); return VC_CONFIG_ERROR_OPERATION_FAILED; } - if (0 != __vc_config_mgr_check_engine_is_valid(g_config_info->engine_id)) { - SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to get default engine"); + string default_engine_id = g_VoiceControlConfig->getEngineId(); + if (0 != __vc_config_mgr_check_and_set_default_engine_id(default_engine_id.c_str())) { + SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to set default engine"); __vc_config_release_engine(); - vc_parser_unload_config(g_config_info); - g_config_info = NULL; + __finalize_config_info(); return VC_CONFIG_ERROR_ENGINE_NOT_FOUND; } - if (true == g_config_info->auto_lang) { + if (g_VoiceControlConfig->isAutoLanguageEnabled()) { /* Check language with display language */ __vc_config_set_auto_language(); } else { - if (false == __vc_config_mgr_check_lang_is_valid(g_config_info->engine_id, g_config_info->language)) { + string default_language = g_VoiceControlConfig->getCurrentLanguage(); + if (false == __vc_config_mgr_check_lang_is_valid(default_engine_id.c_str(), default_language.c_str())) { /* Default language is not valid */ char* tmp_language; - if (0 != __vc_config_mgr_select_lang(g_config_info->engine_id, &tmp_language)) { + if (0 != __vc_config_mgr_select_lang(default_engine_id.c_str(), &tmp_language)) { SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to select language"); __vc_config_release_engine(); - vc_parser_unload_config(g_config_info); - g_config_info = NULL; + __finalize_config_info(); return VC_CONFIG_ERROR_OPERATION_FAILED; } if (NULL != tmp_language) { - if (NULL != g_config_info->language) { - free(g_config_info->language); - g_config_info->language = strdup(tmp_language); - } - + g_VoiceControlConfig->setCurrentLanguage(tmp_language); if (0 != vc_parser_set_language(tmp_language)) { free(tmp_language); SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to save config"); __vc_config_release_engine(); - vc_parser_unload_config(g_config_info); - g_config_info = NULL; + __finalize_config_info(); return VC_CONFIG_ERROR_OPERATION_FAILED; } @@ -950,9 +928,7 @@ static int __initialize_vc_config_mgr() /* print daemon config */ SLOG(LOG_DEBUG, vc_config_tag(), "@ Daemon config @"); - SLOG(LOG_DEBUG, vc_config_tag(), " engine : %s", g_config_info->engine_id); - SLOG(LOG_DEBUG, vc_config_tag(), " auto language : %s", g_config_info->auto_lang ? "on" : "off"); - SLOG(LOG_DEBUG, vc_config_tag(), " language : %s", g_config_info->language); + g_VoiceControlConfig->printOutConfig(); SLOG(LOG_DEBUG, vc_config_tag(), "@@@@"); g_lang_ref_count = 0; @@ -1034,9 +1010,7 @@ int vc_config_mgr_finalize(unsigned int uid) __vc_config_mgr_unregister_engine_config_updated_event(); vconf_ignore_key_changed(VCONFKEY_LANGSET, __vc_config_language_changed_cb); - - vc_parser_unload_config(g_config_info); - g_config_info = NULL; + __finalize_config_info(); pthread_mutex_unlock(&vc_config_mgr_mutex); SLOG(LOG_DEBUG, vc_config_tag(), "[WARNING] Leave critical section"); @@ -1261,7 +1235,7 @@ int vc_config_mgr_get_auto_language(bool* value) return -1; } - *value = g_config_info->auto_lang; + *value = g_VoiceControlConfig->isAutoLanguageEnabled(); pthread_mutex_unlock(&vc_config_mgr_mutex); return VC_ERROR_NONE; @@ -1277,17 +1251,16 @@ int vc_config_mgr_set_auto_language(bool value) return -1; } - if (g_config_info->auto_lang != value) { + if (g_VoiceControlConfig->isAutoLanguageEnabled() != value) { /* Check language is valid */ if (0 != vc_parser_set_auto_lang(value)) { SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to save engine id"); pthread_mutex_unlock(&vc_config_mgr_mutex); return -1; } + g_VoiceControlConfig->setAutoLanguageEnabled(value); - g_config_info->auto_lang = value; - - if (true == g_config_info->auto_lang) { + if (true == value) { __vc_config_set_auto_language(); } } @@ -1359,13 +1332,13 @@ int vc_config_mgr_get_engine(char** engine) return VC_CONFIG_ERROR_INVALID_PARAMETER; } - if (NULL != g_config_info->engine_id) { - *engine = strdup(g_config_info->engine_id); - } else { + string default_engine_id = g_VoiceControlConfig->getEngineId(); + if (default_engine_id.empty()) { SLOG(LOG_ERROR, vc_config_tag(), " Engine id is NULL"); //LCOV_EXCL_LINE pthread_mutex_unlock(&vc_config_mgr_mutex); return VC_CONFIG_ERROR_ENGINE_NOT_FOUND; } + *engine = strdup(default_engine_id.c_str()); pthread_mutex_unlock(&vc_config_mgr_mutex); return VC_CONFIG_ERROR_NONE; @@ -1436,18 +1409,15 @@ int vc_config_mgr_set_engine(const char* engine) return VC_CONFIG_ERROR_INVALID_STATE; } - if (NULL == g_config_info) { - pthread_mutex_unlock(&vc_config_mgr_mutex); - return VC_CONFIG_ERROR_OPERATION_FAILED; - } - - if (NULL == engine || NULL == g_config_info->engine_id) { + if (NULL == engine) { pthread_mutex_unlock(&vc_config_mgr_mutex); return VC_CONFIG_ERROR_INVALID_PARAMETER; } /* Check current engine id with new engine id */ - if (0 == strncmp(g_config_info->engine_id, engine, strlen(g_config_info->engine_id))) { + string default_engine_id = g_VoiceControlConfig->getEngineId(); + if (0 == default_engine_id.compare(engine)) { + SLOG(LOG_DEBUG, vc_config_tag(), "Engine is not changed : %s", engine); pthread_mutex_unlock(&vc_config_mgr_mutex); return VC_CONFIG_ERROR_NONE; } @@ -1468,18 +1438,20 @@ int vc_config_mgr_set_engine(const char* engine) return VC_CONFIG_ERROR_INVALID_PARAMETER; } - free(g_config_info->engine_id); - g_config_info->engine_id = strdup(engine_info->uuid); + if (0 != vc_parser_set_engine(engine)) { + SLOG(LOG_ERROR, vc_config_tag(), " Fail to save config"); + pthread_mutex_unlock(&vc_config_mgr_mutex); + return VC_CONFIG_ERROR_OPERATION_FAILED; + } + g_VoiceControlConfig->setEngineId(engine); /* Engine is valid*/ - bool is_language_valid = __is_language_valid(engine_info, g_config_info->language); - SLOG(LOG_ERROR, vc_config_tag(), "[INFO] Language(%s), is valid(%d)", (g_config_info->language ? g_config_info->language : "NULL"), (int)is_language_valid); + string default_language = g_VoiceControlConfig->getCurrentLanguage(); + bool is_language_valid = __is_language_valid(engine_info, default_language.c_str()); + SLOG(LOG_ERROR, vc_config_tag(), "[INFO] Language(%s), is valid(%d)", default_language.c_str(), (int)is_language_valid); char* lang = NULL; if (false == is_language_valid) { - free(g_config_info->language); - g_config_info->language = NULL; - GSList *iter_lang = g_slist_nth(engine_info->languages, 0); if (NULL == iter_lang || NULL == iter_lang->data) { SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to get default language. Engine id(%s) is not valid", engine); @@ -1488,20 +1460,11 @@ int vc_config_mgr_set_engine(const char* engine) } lang = static_cast(iter_lang->data); - g_config_info->language = strdup(lang); + g_VoiceControlConfig->setCurrentLanguage(lang); } SLOG(LOG_DEBUG, vc_config_tag(), "[Config] Engine changed"); - SLOG(LOG_DEBUG, vc_config_tag(), "Engine : %s", g_config_info->engine_id); - SLOG(LOG_DEBUG, vc_config_tag(), "Auto lang : %s", g_config_info->auto_lang ? "true" : "false"); - SLOG(LOG_DEBUG, vc_config_tag(), "Language : %s", g_config_info->language); - SLOG(LOG_DEBUG, vc_config_tag(), "Enabled : %s", g_config_info->enabled ? "true" : "false"); - - if (0 != vc_parser_set_engine(g_config_info->engine_id)) { - SLOG(LOG_ERROR, vc_config_tag(), " Fail to save config"); - pthread_mutex_unlock(&vc_config_mgr_mutex); - return VC_CONFIG_ERROR_OPERATION_FAILED; - } + g_VoiceControlConfig->printOutConfig(); pthread_mutex_unlock(&vc_config_mgr_mutex); return VC_CONFIG_ERROR_NONE; @@ -1522,9 +1485,10 @@ int vc_config_mgr_get_language_list(vc_supported_language_cb callback, void* use return -1; } - vc_engine_info_s *engine_info = __find_engine_info(g_config_info->engine_id); + string default_engine_id = g_VoiceControlConfig->getEngineId(); + vc_engine_info_s *engine_info = __find_engine_info(default_engine_id.c_str()); if (NULL == engine_info) { - SLOG(LOG_ERROR, vc_config_tag(), "Fail to find engine information : %s", g_config_info->engine_id); + SLOG(LOG_ERROR, vc_config_tag(), "Fail to find engine information : %s", default_engine_id.c_str()); pthread_mutex_unlock(&vc_config_mgr_mutex); return VC_CONFIG_ERROR_NONE; } @@ -1564,14 +1528,14 @@ int vc_config_mgr_get_default_language(char** language) return -1; } - if (NULL != g_config_info->language) { - *language = strdup(g_config_info->language); - } else { + string default_language = g_VoiceControlConfig->getCurrentLanguage(); + if (default_language.empty()) { SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] language is NULL"); pthread_mutex_unlock(&vc_config_mgr_mutex); return -1; } + *language = strdup(default_language.c_str()); pthread_mutex_unlock(&vc_config_mgr_mutex); return VC_ERROR_NONE; } @@ -1588,18 +1552,18 @@ static int __vc_config_mgr_set_default_language(const char* language) } /* Check language is valid */ - if (NULL != g_config_info->language) { - if (0 != vc_parser_set_language(language)) { - SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to save engine id"); - return -1; - } - free(g_config_info->language); - g_config_info->language = strdup(language); - } else { + string default_language = g_VoiceControlConfig->getCurrentLanguage(); + if (default_language.empty()) { SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] language is NULL"); return -1; } + if (0 != vc_parser_set_language(language)) { + SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Fail to save engine id"); + return -1; + } + g_VoiceControlConfig->setCurrentLanguage(language); + return VC_ERROR_NONE; } @@ -1626,7 +1590,7 @@ int vc_config_mgr_get_enabled(bool* value) return -1; } - *value = g_config_info->enabled; + *value = g_VoiceControlConfig->isEnabled(); pthread_mutex_unlock(&vc_config_mgr_mutex); return VC_ERROR_NONE; @@ -1647,7 +1611,7 @@ int vc_config_mgr_set_enabled(bool value) return -1; } - g_config_info->enabled = value; + g_VoiceControlConfig->setEnabled(value); pthread_mutex_unlock(&vc_config_mgr_mutex); return VC_ERROR_NONE; @@ -1674,15 +1638,10 @@ int vc_config_mgr_get_nonfixed_support(bool* value) return -1; } - if (NULL == g_config_info || NULL == g_config_info->engine_id) { - SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Engine ID is not set in global config!!"); - pthread_mutex_unlock(&vc_config_mgr_mutex); - return VC_CONFIG_ERROR_ENGINE_NOT_FOUND; - } - - vc_engine_info_s *engine_info = __find_engine_info(g_config_info->engine_id); + string default_engine_id = g_VoiceControlConfig->getEngineId(); + vc_engine_info_s *engine_info = __find_engine_info(default_engine_id.c_str()); if (NULL == engine_info) { - SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] There is no engine with id (%s)!!", g_config_info->engine_id); + SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] There is no engine with id (%s)!!", default_engine_id.c_str()); pthread_mutex_unlock(&vc_config_mgr_mutex); return VC_CONFIG_ERROR_ENGINE_NOT_FOUND; }; @@ -1732,18 +1691,15 @@ bool vc_config_check_default_language_is_valid(const char* language) return false; } - if (NULL == g_config_info->engine_id) { - SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] Default engine id is NULL"); - return false; - } - if (0 >= g_slist_length(g_engine_list)) { return false; } - vc_engine_info_s *engine_info = __find_engine_info(g_config_info->engine_id); + + string default_engine_id = g_VoiceControlConfig->getEngineId(); + vc_engine_info_s *engine_info = __find_engine_info(default_engine_id.c_str()); if (NULL == engine_info) { - SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] There is no engine with id (%s)!!", g_config_info->engine_id); + SLOG(LOG_ERROR, vc_config_tag(), "[ERROR] There is no engine with id (%s)!!", default_engine_id.c_str()); return false; }; diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 39c61dc..69608e3 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -3,6 +3,7 @@ SET(SRCS ../common/vc_command_util.c ../common/vc_command.c ../common/vc_config_mgr.cpp + ../common/VoiceControlConfig.cpp ../common/vc_config_parser.c ../common/vc_info_parser.c ../common/vc_json_parser.c