2 * Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
20 #include "stt_config_parser.h"
23 #define STT_TAG_ENGINE_BASE_TAG "stt-engine"
24 #define STT_TAG_ENGINE_NAME "name"
25 #define STT_TAG_ENGINE_ID "id"
26 #define STT_TAG_ENGINE_SETTING "setting"
27 #define STT_TAG_ENGINE_AGREEMENT "agreement"
28 #define STT_TAG_ENGINE_DEFAULT "default"
29 #define STT_TAG_ENGINE_LANGUAGE_SET "languages"
30 #define STT_TAG_ENGINE_LANGUAGE "lang"
31 #define STT_TAG_ENGINE_SILENCE_SUPPORT "silence-detection-support"
32 #define STT_TAG_ENGINE_CREDENTIAL_NEED "app-credential-need"
34 #define STT_TAG_CONFIG_BASE_TAG "stt-config"
35 #define STT_TAG_CONFIG_ENGINE_ID "engine"
36 #define STT_TAG_CONFIG_ENGINE_SETTING "engine-setting"
37 #define STT_TAG_CONFIG_AUTO_LANGUAGE "auto"
38 #define STT_TAG_CONFIG_LANGUAGE "language"
39 #define STT_TAG_CONFIG_SILENCE_DETECTION "silence-detection"
40 #define STT_TAG_CONFIG_CREDENTIAL "credential"
43 #define STT_TAG_TIME_BASE_TAG "stt-time"
44 #define STT_TAG_TIME_INDEX "index"
45 #define STT_TAG_TIME_COUNT "count"
46 #define STT_TAG_TIME_TIME_INFO "time-info"
47 #define STT_TAG_TIME_TEXT "text-info"
48 #define STT_TAG_TIME_START "start"
49 #define STT_TAG_TIME_END "end"
52 static xmlDocPtr g_config_doc = NULL;
54 static int __stt_parser_get_base_node_from_config(xmlDocPtr config_doc, xmlNodePtr* node_ptr, const char* base_tag)
56 xmlNodePtr cur = xmlDocGetRootElement(config_doc);
59 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
64 if (xmlStrcmp(cur->name, (const xmlChar *)base_tag)) {
66 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] The wrong type, root node is NOT %s", base_tag);
71 cur = cur->xmlChildrenNode;
74 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
82 int stt_parser_get_engine_info(const char* path, stt_engine_info_s** engine_info)
84 if (NULL == path || NULL == engine_info) {
85 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
90 xmlNodePtr cur = NULL;
93 doc = xmlParseFile(path);
98 if (0 != __stt_parser_get_base_node_from_config(doc, &cur, STT_TAG_ENGINE_BASE_TAG)) {
99 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to get base node from config");
104 /* alloc engine info */
105 stt_engine_info_s* temp;
106 temp = (stt_engine_info_s*)calloc(1, sizeof(stt_engine_info_s));
110 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to allocate memory");
117 temp->setting = NULL;
118 temp->agreement = NULL;
119 temp->default_lang = NULL;
120 temp->languages = NULL;
121 temp->support_silence_detection = false;
122 temp->need_credential = false;
124 bool is_default_lang_set = false;
126 while (cur != NULL) {
127 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_NAME)) {
128 key = xmlNodeGetContent(cur);
130 /* SLOG(LOG_DEBUG, TAG_STTCONFIG, "Engine name : %s", (char *)key); */
131 if (NULL != temp->name) free(temp->name);
132 temp->name = strdup((char*)key);
135 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] <%s> has no content", STT_TAG_ENGINE_NAME); //LCOV_EXCL_LINE
137 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_ID)) {
138 key = xmlNodeGetContent(cur);
140 /* SLOG(LOG_DEBUG, TAG_STTCONFIG, "Engine uuid : %s", (char *)key); */
141 if (NULL != temp->uuid) free(temp->uuid);
142 temp->uuid = strdup((char*)key);
145 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] <%s> has no content", STT_TAG_ENGINE_ID); //LCOV_EXCL_LINE
147 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_SETTING)) {
148 key = xmlNodeGetContent(cur);
150 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Engine setting : %s", (char *)key);
151 if (NULL != temp->setting) free(temp->setting);
152 temp->setting = strdup((char*)key);
155 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] <%s> has no content", STT_TAG_ENGINE_SETTING); //LCOV_EXCL_LINE
157 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_AGREEMENT)) {
158 key = xmlNodeGetContent(cur);
160 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Engine agreement : %s", (char *)key);
161 if (NULL != temp->agreement) free(temp->agreement);
162 temp->agreement = strdup((char*)key);
165 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] <%s> has no content", STT_TAG_ENGINE_AGREEMENT); //LCOV_EXCL_LINE
167 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_DEFAULT)) {
168 key = xmlNodeGetContent(cur);
170 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Engine agreement : %s", (char *)key);
171 if (NULL != temp->default_lang) free(temp->default_lang);
172 temp->default_lang = strdup((char*)key);
174 is_default_lang_set = true;
177 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] <%s> has no content", STT_TAG_ENGINE_DEFAULT);
179 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_LANGUAGE_SET)) {
180 xmlNodePtr lang_node = NULL;
181 char* temp_lang = NULL;
183 lang_node = cur->xmlChildrenNode;
185 while (lang_node != NULL) {
186 if (0 == xmlStrcmp(lang_node->name, (const xmlChar *)STT_TAG_ENGINE_LANGUAGE)) {
187 key = xmlNodeGetContent(lang_node);
189 /* SLOG(LOG_DEBUG, TAG_STTCONFIG, "language : %s", (char *)key); */
190 temp_lang = strdup((char*)key);
191 temp->languages = g_slist_append(temp->languages, temp_lang);
193 if (false == is_default_lang_set) {
194 if (NULL != temp->default_lang) free(temp->default_lang);
195 temp->default_lang = strdup((char*)key);
197 is_default_lang_set = true;
201 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] <%s> has no content", STT_TAG_ENGINE_LANGUAGE); //LCOV_EXCL_LINE
205 lang_node = lang_node->next;
207 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_SILENCE_SUPPORT)) {
208 key = xmlNodeGetContent(cur);
210 /*SLOG(LOG_DEBUG, TAG_STTCONFIG, "silence-detection-support : %s", (char *)key); */
212 if (0 == xmlStrcmp(key, (const xmlChar *)"true"))
213 temp->support_silence_detection = true;
215 temp->support_silence_detection = false;
219 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] <%s> has no content", STT_TAG_ENGINE_SILENCE_SUPPORT); //LCOV_EXCL_LINE
221 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_CREDENTIAL_NEED)) {
222 key = xmlNodeGetContent(cur);
224 //SLOG(LOG_DEBUG, TAG_STTCONFIG, "app-credential-need : %s", (char *)key);
226 if (0 == xmlStrcmp(key, (const xmlChar *)"true"))
227 temp->need_credential = true;
229 temp->need_credential = false;
233 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] <%s> has no content", STT_TAG_ENGINE_CREDENTIAL_NEED);
244 if (NULL == temp->name || NULL == temp->uuid) {
246 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Invalid engine : %s", path);
247 stt_parser_free_engine_info(temp);
256 int stt_parser_free_engine_info(stt_engine_info_s* engine_info)
258 if (NULL == engine_info) {
259 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
263 if (NULL != engine_info->name) free(engine_info->name);
264 if (NULL != engine_info->uuid) free(engine_info->uuid);
265 if (NULL != engine_info->setting) free(engine_info->setting);
266 if (NULL != engine_info->agreement) free(engine_info->agreement);
268 int count = g_slist_length(engine_info->languages);
273 for (i = 0; i < count ; i++) {
274 temp_lang = g_slist_nth_data(engine_info->languages, 0);
276 if (NULL != temp_lang) {
277 engine_info->languages = g_slist_remove(engine_info->languages, temp_lang);
279 if (NULL != temp_lang)
284 if (NULL != engine_info) free(engine_info);
290 int stt_parser_print_engine_info(stt_engine_info_s* engine_info)
292 if (NULL == engine_info)
295 SLOG(LOG_DEBUG, TAG_STTCONFIG, "== get engine info ==");
296 SLOG(LOG_DEBUG, TAG_STTCONFIG, " name : %s", engine_info->name);
297 SLOG(LOG_DEBUG, TAG_STTCONFIG, " id : %s", engine_info->uuid);
298 if (NULL != engine_info->setting) SLOG(LOG_DEBUG, TAG_STTCONFIG, " setting : %s", engine_info->setting);
300 SLOG(LOG_DEBUG, TAG_STTCONFIG, " languages");
303 if (g_slist_length(engine_info->languages) > 0) {
304 /* Get a first item */
305 iter = g_slist_nth(engine_info->languages, 0);
308 while (NULL != iter) {
309 /*Get handle data from list*/
312 SLOG(LOG_DEBUG, TAG_STTCONFIG, " [%dth] %s", i, lang);
315 iter = g_slist_next(iter);
319 SLOG(LOG_ERROR, TAG_STTCONFIG, " language is NONE");
321 SLOG(LOG_DEBUG, TAG_STTCONFIG, " silence support : %s", engine_info->support_silence_detection ? "true" : "false");
322 SLOG(LOG_DEBUG, TAG_STTCONFIG, " credential need : %s", engine_info->need_credential ? "true" : "false");
323 SLOG(LOG_DEBUG, TAG_STTCONFIG, "=====================");
329 int stt_parser_load_config(stt_config_s** config_info)
331 if (NULL == config_info) {
332 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
336 xmlDocPtr doc = NULL;
337 xmlNodePtr cur = NULL;
339 bool is_default_open = false;
341 if (0 != access(STT_CONFIG, F_OK)) {
342 doc = xmlParseFile(STT_DEFAULT_CONFIG);
344 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to parse file error : %s", STT_DEFAULT_CONFIG); //LCOV_EXCL_LINE
348 is_default_open = true;
352 while (NULL == doc) {
353 doc = xmlParseFile(STT_CONFIG);
360 if (STT_RETRY_COUNT == retry_count) {
361 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to parse file error : %s", STT_CONFIG);
362 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Remove the file");
365 doc = xmlParseFile(STT_DEFAULT_CONFIG);
367 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to parse file error : %s", STT_DEFAULT_CONFIG);
371 is_default_open = true;
377 if (0 != __stt_parser_get_base_node_from_config(doc, &cur, STT_TAG_CONFIG_BASE_TAG)) {
378 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to get base node from config");
383 /* alloc engine info */
385 temp = (stt_config_s*)calloc(1, sizeof(stt_config_s));
388 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to allocate memory"); //LCOV_EXCL_LINE
392 temp->engine_id = NULL;
393 temp->setting = NULL;
394 temp->language = NULL;
396 while (cur != NULL) {
397 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_ENGINE_ID)) {
398 key = xmlNodeGetContent(cur);
400 /*SLOG(LOG_DEBUG, TAG_STTCONFIG, "Engine id : %s", (char *)key); */
401 if (NULL != temp->engine_id) free(temp->engine_id);
402 temp->engine_id = strdup((char*)key);
405 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] engine id is NULL"); //LCOV_EXCL_LINE
407 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_ENGINE_SETTING)) {
408 key = xmlNodeGetContent(cur);
410 /*SLOG(LOG_DEBUG, TAG_STTCONFIG, "Setting path : %s", (char *)key); */
411 if (NULL != temp->setting) free(temp->setting);
412 temp->setting = strdup((char*)key);
415 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] setting path is NULL"); //LCOV_EXCL_LINE
418 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_AUTO_LANGUAGE)) {
419 key = xmlNodeGetContent(cur);
421 /*SLOG(LOG_DEBUG, TAG_STTCONFIG, "Auto language : %s", (char *)key); */
423 if (0 == xmlStrcmp(key, (const xmlChar *)"on")) {
424 temp->auto_lang = true;
425 } else if (0 == xmlStrcmp(key, (const xmlChar *)"off")) {
426 temp->auto_lang = false;
428 SLOG(LOG_ERROR, TAG_STTCONFIG, "Auto voice is wrong"); //LCOV_EXCL_LINE
429 temp->auto_lang = true;
434 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] auto language is NULL"); //LCOV_EXCL_LINE
436 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_LANGUAGE)) {
437 key = xmlNodeGetContent(cur);
439 /*SLOG(LOG_DEBUG, TAG_STTCONFIG, "language : %s", (char *)key); */
440 if (NULL != temp->language) free(temp->language);
441 temp->language = strdup((char*)key);
444 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] language is NULL"); //LCOV_EXCL_LINE
446 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_SILENCE_DETECTION)) {
447 key = xmlNodeGetContent(cur);
449 /*SLOG(LOG_DEBUG, TAG_STTCONFIG, "silence-detection : %s", (char *)key); */
451 if (0 == xmlStrcmp(key, (const xmlChar *)"on"))
452 temp->silence_detection = true;
454 temp->silence_detection = false;
458 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] silence-detection is NULL"); //LCOV_EXCL_LINE
460 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_CREDENTIAL)) {
461 key = xmlNodeGetContent(cur);
463 //SLOG(LOG_DEBUG, TAG_STTCONFIG, "credential : %s", (char *)key);
465 if (0 == xmlStrcmp(key, (const xmlChar *)"true"))
466 temp->credential = true;
468 temp->credential = false;
472 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] credential is NULL"); //LCOV_EXCL_LINE
484 if (is_default_open) {
488 ret = xmlSaveFile(STT_CONFIG, g_config_doc);
494 if (STT_RETRY_COUNT == retry_count) {
495 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Save result : %d", ret);
501 if (0 > chmod(STT_CONFIG, 0600)) {
502 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to change file mode : %d", ret);
506 if (0 > chown(STT_CONFIG, 5000, 5000)) {
507 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to change file owner : %d", ret);
509 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Default config is changed : pid(%d)", getpid());
515 int stt_parser_unload_config(stt_config_s* config_info)
517 if (NULL != g_config_doc) {
518 xmlFreeDoc(g_config_doc);
521 if (NULL != config_info) {
522 if (NULL != config_info->engine_id) {
523 free(config_info->engine_id);
524 config_info->engine_id = NULL;
526 if (NULL != config_info->setting) {
527 free(config_info->setting);
528 config_info->setting = NULL;
530 if (NULL != config_info->language) {
531 free(config_info->language);
532 config_info->language = NULL;
541 int stt_parser_set_engine(const char* engine_id, const char* setting, const char* language, bool silence, bool credential)
543 if (NULL == g_config_doc || NULL == engine_id)
546 xmlNodePtr cur = NULL;
547 if (0 != __stt_parser_get_base_node_from_config(g_config_doc, &cur, STT_TAG_CONFIG_BASE_TAG)) {
548 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to get base node from config");
552 while (cur != NULL) {
553 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_ENGINE_ID)) {
554 xmlNodeSetContent(cur, (const xmlChar *)engine_id);
557 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_LANGUAGE)) {
558 xmlNodeSetContent(cur, (const xmlChar *)language);
561 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_ENGINE_SETTING)) {
562 xmlNodeSetContent(cur, (const xmlChar *)setting);
565 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_SILENCE_DETECTION)) {
567 xmlNodeSetContent(cur, (const xmlChar *)"on");
569 xmlNodeSetContent(cur, (const xmlChar *)"off");
572 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_CREDENTIAL)) {
573 if (true == credential)
574 xmlNodeSetContent(cur, (const xmlChar *)"true");
576 xmlNodeSetContent(cur, (const xmlChar *)"false");
582 int ret = xmlSaveFile(STT_CONFIG, g_config_doc);
583 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Save result : %d", ret);
588 int stt_parser_set_language(const char* language)
590 if (NULL == g_config_doc || NULL == language)
593 xmlNodePtr cur = NULL;
594 if (0 != __stt_parser_get_base_node_from_config(g_config_doc, &cur, STT_TAG_CONFIG_BASE_TAG)) {
595 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to get base node from config");
599 while (cur != NULL) {
600 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_LANGUAGE)) {
601 xmlNodeSetContent(cur, (const xmlChar *)language);
607 int ret = xmlSaveFile(STT_CONFIG, g_config_doc);
608 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Save result : %d", ret);
615 int stt_parser_set_auto_lang(bool value)
617 if (NULL == g_config_doc)
620 xmlNodePtr cur = NULL;
621 if (0 != __stt_parser_get_base_node_from_config(g_config_doc, &cur, STT_TAG_CONFIG_BASE_TAG)) {
622 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to get base node from config");
626 while (cur != NULL) {
627 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_AUTO_LANGUAGE)) {
629 xmlNodeSetContent(cur, (const xmlChar *)"on");
630 } else if (false == value) {
631 xmlNodeSetContent(cur, (const xmlChar *)"off");
633 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] The wrong value of auto voice");
641 int ret = xmlSaveFile(STT_CONFIG, g_config_doc);
642 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Save result : %d", ret);
647 int stt_parser_set_silence_detection(bool value)
649 if (NULL == g_config_doc)
652 xmlNodePtr cur = NULL;
653 if (0 != __stt_parser_get_base_node_from_config(g_config_doc, &cur, STT_TAG_CONFIG_BASE_TAG)) {
654 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to get base node from config");
658 while (cur != NULL) {
659 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_SILENCE_DETECTION)) {
661 xmlNodeSetContent(cur, (const xmlChar *)"on");
663 xmlNodeSetContent(cur, (const xmlChar *)"off");
669 int ret = xmlSaveFile(STT_CONFIG, g_config_doc);
670 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Save result : %d", ret);
676 int stt_parser_find_config_changed(char** engine, char** setting, int* auto_lang, char** language, int* silence, int* credential)
678 if (NULL == engine || NULL == language || NULL == silence || NULL == credential) {
679 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
683 xmlDocPtr doc = NULL;
684 xmlNodePtr cur_new = NULL;
685 xmlNodePtr cur_old = NULL;
690 doc = xmlParseFile(STT_CONFIG);
692 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to parse file error : %s", STT_CONFIG);
696 cur_new = xmlDocGetRootElement(doc);
697 cur_old = xmlDocGetRootElement(g_config_doc);
698 if (cur_new == NULL || cur_old == NULL) {
700 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
706 if (xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_BASE_TAG) || xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_BASE_TAG)) {
708 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
714 cur_new = cur_new->xmlChildrenNode;
715 cur_old = cur_old->xmlChildrenNode;
716 if (cur_new == NULL || cur_old == NULL) {
718 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
728 while (cur_new != NULL && cur_old != NULL) {
729 if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_ENGINE_ID)) {
730 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_ENGINE_ID)) {
731 key_old = xmlNodeGetContent(cur_old);
732 if (NULL != key_old) {
733 key_new = xmlNodeGetContent(cur_new);
734 if (NULL != key_new) {
735 if (0 != xmlStrcmp(key_old, key_new)) {
736 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Old engine id(%s), New engine(%s)", (char*)key_old, (char*)key_new);
737 if (NULL != *engine) free(*engine);
738 *engine = strdup((char*)key_new);
745 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Old config and new config are different"); //LCOV_EXCL_LINE
747 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_ENGINE_SETTING)) {
748 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_ENGINE_SETTING)) {
749 key_old = xmlNodeGetContent(cur_old);
750 if (NULL != key_old) {
751 key_new = xmlNodeGetContent(cur_new);
752 if (NULL != key_new) {
753 if (0 != xmlStrcmp(key_old, key_new)) {
754 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Old engine setting(%s), New engine setting(%s)", (char*)key_old, (char*)key_new); //LCOV_EXCL_LINE
755 if (NULL != *setting) free(*setting);
756 *setting = strdup((char*)key_new);
763 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Old config and new config are different"); //LCOV_EXCL_LINE
765 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_AUTO_LANGUAGE)) {
766 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_AUTO_LANGUAGE)) {
767 key_old = xmlNodeGetContent(cur_old);
768 if (NULL != key_old) {
769 key_new = xmlNodeGetContent(cur_new);
770 if (NULL != key_new) {
771 if (0 != xmlStrcmp(key_old, key_new)) {
772 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Old auto lang(%s), New auto lang(%s)", (char*)key_old, (char*)key_new); //LCOV_EXCL_LINE
773 if (0 == xmlStrcmp((const xmlChar*)"on", key_new)) {
774 *auto_lang = (int)true;
776 *auto_lang = (int)false;
785 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] old config and new config are different"); //LCOV_EXCL_LINE
787 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_LANGUAGE)) {
788 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_LANGUAGE)) {
789 key_old = xmlNodeGetContent(cur_old);
790 if (NULL != key_old) {
791 key_new = xmlNodeGetContent(cur_new);
792 if (NULL != key_new) {
793 if (0 != xmlStrcmp(key_old, key_new)) {
794 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Old language(%s), New language(%s)", (char*)key_old, (char*)key_new); //LCOV_EXCL_LINE
795 if (NULL != *language) free(*language);
796 *language = strdup((char*)key_new);
803 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] old config and new config are different"); //LCOV_EXCL_LINE
805 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_SILENCE_DETECTION)) {
806 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_SILENCE_DETECTION)) {
807 key_old = xmlNodeGetContent(cur_old);
808 if (NULL != key_old) {
809 key_new = xmlNodeGetContent(cur_new);
810 if (NULL != key_new) {
811 if (0 != xmlStrcmp(key_old, key_new)) {
812 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Old silence(%s), New silence(%s)", (char*)key_old, (char*)key_new); //LCOV_EXCL_LINE
813 if (0 == xmlStrcmp(key_new, (const xmlChar*)"on")) {
825 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] old config and new config are different"); //LCOV_EXCL_LINE
827 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_CREDENTIAL)) {
828 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_CREDENTIAL)) {
829 key_old = xmlNodeGetContent(cur_old);
830 if (NULL != key_old) {
831 key_new = xmlNodeGetContent(cur_new);
832 if (NULL != key_new) {
833 if (0 != xmlStrcmp(key_old, key_new)) {
834 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Old credential(%s), New credential(%s)", (char*)key_old, (char*)key_new); //LCOV_EXCL_LINE
835 if (0 == xmlStrcmp(key_new, (const xmlChar*)"true")) {
846 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] old config and new config are different"); //LCOV_EXCL_LINE
853 cur_new = cur_new->next;
854 cur_old = cur_old->next;
857 xmlFreeDoc(g_config_doc);
867 int stt_parser_set_time_info(GSList* time_list)
869 if (0 == g_slist_length(time_list)) {
870 SLOG(LOG_WARN, TAG_STTCONFIG, "[WARNING] There is no time info to save");
874 if (-1 == remove(STT_TIME_INFO_PATH)) {
875 SLOG(LOG_WARN, TAG_STTCONFIG, "[PLAYER WARNING] Fail to remove file(%s)", STT_TIME_INFO_PATH);
878 xmlDocPtr doc = NULL;
879 xmlNodePtr cur = NULL;
881 doc = xmlNewDoc((const xmlChar*)"1.0");
883 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to make new doc");
887 cur = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_BASE_TAG);
888 xmlDocSetRootElement(doc, cur);
890 xmlNodePtr inode = NULL;
892 stt_result_time_info_s *data = NULL;
895 snprintf(temp_str, 256, "%d", g_slist_length(time_list));
897 inode = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_INDEX);
898 xmlNewProp(inode, (const xmlChar*)STT_TAG_TIME_COUNT, (const xmlChar*)temp_str);
899 xmlAddChild(cur, inode);
901 /* Get a first item */
902 iter = g_slist_nth(time_list, 0);
903 while (NULL != iter) {
907 SLOG(LOG_DEBUG, TAG_STTCONFIG, "data is NULL");
911 xmlNodePtr temp_node = NULL;
913 SECURE_SLOG(LOG_DEBUG, TAG_STTCONFIG, "[%d] i(%d) t(%s) s(%ld) e(%ld)",
914 data->index, data->event, data->text, data->start_time, data->end_time);
916 temp_node = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_TEXT);
917 xmlNodeSetContent(temp_node, (const xmlChar*)data->text);
918 xmlAddChild(inode, temp_node);
920 temp_node = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_START);
921 snprintf(temp_str, 256, "%ld", data->start_time);
922 xmlNodeSetContent(temp_node, (const xmlChar*)temp_str);
923 xmlAddChild(inode, temp_node);
925 temp_node = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_END);
926 snprintf(temp_str, 256, "%ld", data->end_time);
927 xmlNodeSetContent(temp_node, (const xmlChar*)temp_str);
928 xmlAddChild(inode, temp_node);
931 iter = g_slist_next(iter);
934 int ret = xmlSaveFormatFile(STT_TIME_INFO_PATH, doc, 1);
935 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Save result : %d", ret);
942 void __stt_parser_time_info_free(void* data)
944 stt_result_time_info_s* time_info = (stt_result_time_info_s*)data;
946 if (NULL != time_info) {
947 if (NULL != time_info->text) {
948 free(time_info->text);
949 time_info->text = NULL;
956 int stt_parser_get_time_info(GSList** time_list)
958 if (NULL == time_list) {
959 SLOG(LOG_ERROR, TAG_STTCONFIG, "Invalid parameter : text is NULL"); //LCOV_EXCL_LINE
963 xmlDocPtr doc = NULL;
964 xmlNodePtr cur = NULL;
966 doc = xmlParseFile(STT_TIME_INFO_PATH);
968 SLOG(LOG_WARN, TAG_STTCONFIG, "[WARNING] File is not exist"); //LCOV_EXCL_LINE
972 if (0 != __stt_parser_get_base_node_from_config(doc, &cur, STT_TAG_TIME_BASE_TAG)) {
973 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to get base node from config");
978 /* alloc time info */
979 GSList *temp_time_list = NULL;
983 while (cur != NULL) {
984 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_TIME_INDEX)) {
985 key = xmlGetProp(cur, (const xmlChar*)STT_TAG_TIME_COUNT);
987 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] <%s> has no content", STT_TAG_TIME_COUNT); //LCOV_EXCL_LINE
989 if (NULL != temp_time_list) {
990 g_slist_free_full(temp_time_list, __stt_parser_time_info_free);
991 temp_time_list = NULL;
997 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Count : %s", (char *)key);
1001 count = atoi((char*)key);
1005 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] count is invalid : %d", count); //LCOV_EXCL_LINE
1009 xmlNodePtr time_node = NULL;
1010 time_node = cur->xmlChildrenNode;
1013 for (i = 0; i < count; i++) {
1015 time_node = time_node->next;
1017 stt_result_time_info_s* temp_info;
1018 temp_info = (stt_result_time_info_s*)calloc(1, sizeof(stt_result_time_info_s));
1020 if (NULL == temp_info) {
1021 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Memory alloc error!!"); //LCOV_EXCL_LINE
1023 if (NULL != temp_time_list) {
1024 g_slist_free_full(temp_time_list, __stt_parser_time_info_free);
1025 temp_time_list = NULL;
1031 temp_info->index = index;
1032 SLOG(LOG_DEBUG, TAG_STTCONFIG, "index : %d", temp_info->index);
1034 if (0 == i) temp_info->event = 0;
1035 else if (count -1 == i) temp_info->event = 2;
1036 else temp_info->event = 1;
1038 if (0 == xmlStrcmp(time_node->name, (const xmlChar *)STT_TAG_TIME_TEXT)) {
1039 key = xmlNodeGetContent(time_node);
1041 SLOG(LOG_DEBUG, TAG_STTCONFIG, "text : %s", (char *)key); //LCOV_EXCL_LINE
1042 temp_info->text = strdup((char*)key);
1045 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] <%s> has no content", STT_TAG_TIME_TEXT); //LCOV_EXCL_LINE
1052 time_node = time_node->next;
1053 time_node = time_node->next;
1055 if (0 == xmlStrcmp(time_node->name, (const xmlChar *)STT_TAG_TIME_START)) {
1056 key = xmlNodeGetContent(time_node);
1058 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Start time : %s", (char *)key); //LCOV_EXCL_LINE
1059 temp_info->start_time = atoi((char*)key);
1062 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] <%s> has no content", STT_TAG_TIME_START); //LCOV_EXCL_LINE
1063 if (NULL != temp_info->text) free(temp_info->text);
1070 time_node = time_node->next;
1071 time_node = time_node->next;
1073 if (0 == xmlStrcmp(time_node->name, (const xmlChar *)STT_TAG_TIME_END)) {
1074 key = xmlNodeGetContent(time_node);
1076 SLOG(LOG_DEBUG, TAG_STTCONFIG, "End time : %s", (char *)key); //LCOV_EXCL_LINE
1077 temp_info->end_time = atoi((char*)key);
1080 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] <%s> has no content", STT_TAG_TIME_END); //LCOV_EXCL_LINE
1081 if (NULL != temp_info->text) free(temp_info->text);
1087 time_node = time_node->next;
1089 temp_time_list = g_slist_append(temp_time_list, temp_info);
1098 *time_list = temp_time_list;
1103 int stt_parser_clear_time_info()
1105 if (-1 == remove(STT_TIME_INFO_PATH)) {
1106 /* SLOG(LOG_WARN, TAG_STTCONFIG, "[PLAYER WARNING] Fail to remove file(%s)", STT_TIME_INFO_PATH); */