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"
51 extern const char* stt_tag();
53 static xmlDocPtr g_config_doc = NULL;
55 int stt_parser_get_engine_info(const char* path, stt_engine_info_s** engine_info)
57 if (NULL == path || NULL == engine_info) {
58 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
63 xmlNodePtr cur = NULL;
66 doc = xmlParseFile(path);
71 cur = xmlDocGetRootElement(doc);
74 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
80 if (xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_BASE_TAG)) {
82 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong type, root node is NOT 'stt-engine'");
88 cur = cur->xmlChildrenNode;
91 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
97 /* alloc engine info */
98 stt_engine_info_s* temp;
99 temp = (stt_engine_info_s*)calloc(1, sizeof(stt_engine_info_s));
103 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to allocate memory");
110 temp->setting = NULL;
111 temp->agreement = NULL;
112 temp->default_lang = NULL;
113 temp->languages = NULL;
114 temp->support_silence_detection = false;
115 temp->need_credential = false;
117 bool is_default_lang_set = false;
119 while (cur != NULL) {
120 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_NAME)) {
121 key = xmlNodeGetContent(cur);
123 /* SLOG(LOG_DEBUG, stt_tag(), "Engine name : %s", (char *)key); */
124 if (NULL != temp->name) free(temp->name);
125 temp->name = strdup((char*)key);
128 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_ENGINE_NAME); //LCOV_EXCL_LINE
130 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_ID)) {
131 key = xmlNodeGetContent(cur);
133 /* SLOG(LOG_DEBUG, stt_tag(), "Engine uuid : %s", (char *)key); */
134 if (NULL != temp->uuid) free(temp->uuid);
135 temp->uuid = strdup((char*)key);
138 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_ENGINE_ID); //LCOV_EXCL_LINE
140 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_SETTING)) {
141 key = xmlNodeGetContent(cur);
143 SLOG(LOG_DEBUG, stt_tag(), "Engine setting : %s", (char *)key);
144 if (NULL != temp->setting) free(temp->setting);
145 temp->setting = strdup((char*)key);
148 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_ENGINE_SETTING); //LCOV_EXCL_LINE
150 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_AGREEMENT)) {
151 key = xmlNodeGetContent(cur);
153 SLOG(LOG_DEBUG, stt_tag(), "Engine agreement : %s", (char *)key);
154 if (NULL != temp->agreement) free(temp->agreement);
155 temp->agreement = strdup((char*)key);
158 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_ENGINE_AGREEMENT); //LCOV_EXCL_LINE
160 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_DEFAULT)) {
161 key = xmlNodeGetContent(cur);
163 SLOG(LOG_DEBUG, stt_tag(), "Engine agreement : %s", (char *)key);
164 if (NULL != temp->default_lang) free(temp->default_lang);
165 temp->default_lang = strdup((char*)key);
167 is_default_lang_set = true;
170 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_ENGINE_DEFAULT);
172 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_LANGUAGE_SET)) {
173 xmlNodePtr lang_node = NULL;
174 char* temp_lang = NULL;
176 lang_node = cur->xmlChildrenNode;
178 while (lang_node != NULL) {
179 if (0 == xmlStrcmp(lang_node->name, (const xmlChar *)STT_TAG_ENGINE_LANGUAGE)) {
180 key = xmlNodeGetContent(lang_node);
182 /* SLOG(LOG_DEBUG, stt_tag(), "language : %s", (char *)key); */
183 temp_lang = strdup((char*)key);
184 temp->languages = g_slist_append(temp->languages, temp_lang);
186 if (false == is_default_lang_set) {
187 if (NULL != temp->default_lang) free(temp->default_lang);
188 temp->default_lang = strdup((char*)key);
190 is_default_lang_set = true;
194 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_ENGINE_LANGUAGE); //LCOV_EXCL_LINE
198 lang_node = lang_node->next;
200 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_SILENCE_SUPPORT)) {
201 key = xmlNodeGetContent(cur);
203 /*SLOG(LOG_DEBUG, stt_tag(), "silence-detection-support : %s", (char *)key); */
205 if (0 == xmlStrcmp(key, (const xmlChar *)"true"))
206 temp->support_silence_detection = true;
208 temp->support_silence_detection = false;
212 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_ENGINE_SILENCE_SUPPORT); //LCOV_EXCL_LINE
214 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_CREDENTIAL_NEED)) {
215 key = xmlNodeGetContent(cur);
217 //SLOG(LOG_DEBUG, stt_tag(), "app-credential-need : %s", (char *)key);
219 if (0 == xmlStrcmp(key, (const xmlChar *)"true"))
220 temp->need_credential = true;
222 temp->need_credential = false;
226 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_ENGINE_CREDENTIAL_NEED);
237 if (NULL == temp->name || NULL == temp->uuid) {
239 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Invalid engine : %s", path);
240 stt_parser_free_engine_info(temp);
249 int stt_parser_free_engine_info(stt_engine_info_s* engine_info)
251 if (NULL == engine_info) {
252 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
256 if (NULL != engine_info->name) free(engine_info->name);
257 if (NULL != engine_info->uuid) free(engine_info->uuid);
258 if (NULL != engine_info->setting) free(engine_info->setting);
259 if (NULL != engine_info->agreement) free(engine_info->agreement);
261 int count = g_slist_length(engine_info->languages);
266 for (i = 0; i < count ; i++) {
267 temp_lang = g_slist_nth_data(engine_info->languages, 0);
269 if (NULL != temp_lang) {
270 engine_info->languages = g_slist_remove(engine_info->languages, temp_lang);
272 if (NULL != temp_lang)
277 if (NULL != engine_info) free(engine_info);
283 int stt_parser_print_engine_info(stt_engine_info_s* engine_info)
285 if (NULL == engine_info)
288 SLOG(LOG_DEBUG, stt_tag(), "== get engine info ==");
289 SLOG(LOG_DEBUG, stt_tag(), " name : %s", engine_info->name);
290 SLOG(LOG_DEBUG, stt_tag(), " id : %s", engine_info->uuid);
291 if (NULL != engine_info->setting) SLOG(LOG_DEBUG, stt_tag(), " setting : %s", engine_info->setting);
293 SLOG(LOG_DEBUG, stt_tag(), " languages");
296 if (g_slist_length(engine_info->languages) > 0) {
297 /* Get a first item */
298 iter = g_slist_nth(engine_info->languages, 0);
301 while (NULL != iter) {
302 /*Get handle data from list*/
305 SLOG(LOG_DEBUG, stt_tag(), " [%dth] %s", i, lang);
308 iter = g_slist_next(iter);
312 SLOG(LOG_ERROR, stt_tag(), " language is NONE");
314 SLOG(LOG_DEBUG, stt_tag(), " silence support : %s", engine_info->support_silence_detection ? "true" : "false");
315 SLOG(LOG_DEBUG, stt_tag(), " credential need : %s", engine_info->need_credential ? "true" : "false");
316 SLOG(LOG_DEBUG, stt_tag(), "=====================");
322 int stt_parser_load_config(stt_config_s** config_info)
324 if (NULL == config_info) {
325 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
329 xmlDocPtr doc = NULL;
330 xmlNodePtr cur = NULL;
332 bool is_default_open = false;
334 if (0 != access(STT_CONFIG, F_OK)) {
335 doc = xmlParseFile(STT_DEFAULT_CONFIG);
337 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to parse file error : %s", STT_DEFAULT_CONFIG); //LCOV_EXCL_LINE
341 is_default_open = true;
345 while (NULL == doc) {
346 doc = xmlParseFile(STT_CONFIG);
353 if (STT_RETRY_COUNT == retry_count) {
354 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to parse file error : %s", STT_CONFIG);
355 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Remove the file");
358 doc = xmlParseFile(STT_DEFAULT_CONFIG);
360 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to parse file error : %s", STT_DEFAULT_CONFIG);
364 is_default_open = true;
370 cur = xmlDocGetRootElement(doc);
372 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document"); //LCOV_EXCL_LINE
377 if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
378 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG); //LCOV_EXCL_LINE
383 cur = cur->xmlChildrenNode;
385 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document"); //LCOV_EXCL_LINE
390 /* alloc engine info */
392 temp = (stt_config_s*)calloc(1, sizeof(stt_config_s));
395 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to allocate memory"); //LCOV_EXCL_LINE
399 temp->engine_id = NULL;
400 temp->setting = NULL;
401 temp->language = NULL;
403 while (cur != NULL) {
404 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_ENGINE_ID)) {
405 key = xmlNodeGetContent(cur);
407 /*SLOG(LOG_DEBUG, stt_tag(), "Engine id : %s", (char *)key); */
408 if (NULL != temp->engine_id) free(temp->engine_id);
409 temp->engine_id = strdup((char*)key);
412 SLOG(LOG_ERROR, stt_tag(), "[ERROR] engine id is NULL"); //LCOV_EXCL_LINE
414 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_ENGINE_SETTING)) {
415 key = xmlNodeGetContent(cur);
417 /*SLOG(LOG_DEBUG, stt_tag(), "Setting path : %s", (char *)key); */
418 if (NULL != temp->setting) free(temp->setting);
419 temp->setting = strdup((char*)key);
422 SLOG(LOG_ERROR, stt_tag(), "[ERROR] setting path is NULL"); //LCOV_EXCL_LINE
425 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_AUTO_LANGUAGE)) {
426 key = xmlNodeGetContent(cur);
428 /*SLOG(LOG_DEBUG, stt_tag(), "Auto language : %s", (char *)key); */
430 if (0 == xmlStrcmp(key, (const xmlChar *)"on")) {
431 temp->auto_lang = true;
432 } else if (0 == xmlStrcmp(key, (const xmlChar *)"off")) {
433 temp->auto_lang = false;
435 SLOG(LOG_ERROR, stt_tag(), "Auto voice is wrong"); //LCOV_EXCL_LINE
436 temp->auto_lang = true;
441 SLOG(LOG_ERROR, stt_tag(), "[ERROR] auto language is NULL"); //LCOV_EXCL_LINE
443 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_LANGUAGE)) {
444 key = xmlNodeGetContent(cur);
446 /*SLOG(LOG_DEBUG, stt_tag(), "language : %s", (char *)key); */
447 if (NULL != temp->language) free(temp->language);
448 temp->language = strdup((char*)key);
451 SLOG(LOG_ERROR, stt_tag(), "[ERROR] language is NULL"); //LCOV_EXCL_LINE
453 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_SILENCE_DETECTION)) {
454 key = xmlNodeGetContent(cur);
456 /*SLOG(LOG_DEBUG, stt_tag(), "silence-detection : %s", (char *)key); */
458 if (0 == xmlStrcmp(key, (const xmlChar *)"on"))
459 temp->silence_detection = true;
461 temp->silence_detection = false;
465 SLOG(LOG_ERROR, stt_tag(), "[ERROR] silence-detection is NULL"); //LCOV_EXCL_LINE
467 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_CREDENTIAL)) {
468 key = xmlNodeGetContent(cur);
470 //SLOG(LOG_DEBUG, stt_tag(), "credential : %s", (char *)key);
472 if (0 == xmlStrcmp(key, (const xmlChar *)"true"))
473 temp->credential = true;
475 temp->credential = false;
479 SLOG(LOG_ERROR, stt_tag(), "[ERROR] credential is NULL"); //LCOV_EXCL_LINE
491 if (is_default_open) {
495 ret = xmlSaveFile(STT_CONFIG, g_config_doc);
501 if (STT_RETRY_COUNT == retry_count) {
502 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Save result : %d", ret);
508 if (0 > chmod(STT_CONFIG, 0600)) {
509 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to change file mode : %d", ret);
513 if (0 > chown(STT_CONFIG, 5000, 5000)) {
514 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to change file owner : %d", ret);
516 SLOG(LOG_DEBUG, stt_tag(), "Default config is changed : pid(%d)", getpid());
522 int stt_parser_unload_config(stt_config_s* config_info)
524 if (NULL != g_config_doc) xmlFreeDoc(g_config_doc);
525 if (NULL != config_info) {
526 if (NULL != config_info->engine_id) {
527 free(config_info->engine_id);
528 config_info->engine_id = NULL;
530 if (NULL != config_info->setting) {
531 free(config_info->setting);
532 config_info->setting = NULL;
534 if (NULL != config_info->language) {
535 free(config_info->language);
536 config_info->language = NULL;
545 int stt_parser_set_engine(const char* engine_id, const char* setting, const char* language, bool silence, bool credential)
547 if (NULL == g_config_doc || NULL == engine_id)
550 xmlNodePtr cur = NULL;
551 cur = xmlDocGetRootElement(g_config_doc);
553 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
557 if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
558 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
562 cur = cur->xmlChildrenNode;
564 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
568 while (cur != NULL) {
569 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_ENGINE_ID)) {
570 xmlNodeSetContent(cur, (const xmlChar *)engine_id);
573 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_LANGUAGE)) {
574 xmlNodeSetContent(cur, (const xmlChar *)language);
577 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_ENGINE_SETTING)) {
578 xmlNodeSetContent(cur, (const xmlChar *)setting);
581 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_SILENCE_DETECTION)) {
583 xmlNodeSetContent(cur, (const xmlChar *)"on");
585 xmlNodeSetContent(cur, (const xmlChar *)"off");
588 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_CREDENTIAL)) {
589 if (true == credential)
590 xmlNodeSetContent(cur, (const xmlChar *)"true");
592 xmlNodeSetContent(cur, (const xmlChar *)"false");
598 int ret = xmlSaveFile(STT_CONFIG, g_config_doc);
599 SLOG(LOG_DEBUG, stt_tag(), "Save result : %d", ret);
604 int stt_parser_set_language(const char* language)
606 if (NULL == g_config_doc || NULL == language)
609 xmlNodePtr cur = NULL;
610 cur = xmlDocGetRootElement(g_config_doc);
612 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
616 if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
617 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
621 cur = cur->xmlChildrenNode;
623 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
627 while (cur != NULL) {
628 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_LANGUAGE)) {
629 xmlNodeSetContent(cur, (const xmlChar *)language);
635 int ret = xmlSaveFile(STT_CONFIG, g_config_doc);
636 SLOG(LOG_DEBUG, stt_tag(), "Save result : %d", ret);
643 int stt_parser_set_auto_lang(bool value)
645 if (NULL == g_config_doc)
648 xmlNodePtr cur = NULL;
649 cur = xmlDocGetRootElement(g_config_doc);
651 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
655 if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
656 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
660 cur = cur->xmlChildrenNode;
662 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
666 while (cur != NULL) {
667 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_AUTO_LANGUAGE)) {
669 xmlNodeSetContent(cur, (const xmlChar *)"on");
670 } else if (false == value) {
671 xmlNodeSetContent(cur, (const xmlChar *)"off");
673 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong value of auto voice");
681 int ret = xmlSaveFile(STT_CONFIG, g_config_doc);
682 SLOG(LOG_DEBUG, stt_tag(), "Save result : %d", ret);
687 int stt_parser_set_silence_detection(bool value)
689 if (NULL == g_config_doc)
692 xmlNodePtr cur = NULL;
693 cur = xmlDocGetRootElement(g_config_doc);
695 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
699 if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
700 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
704 cur = cur->xmlChildrenNode;
706 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
710 while (cur != NULL) {
711 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_SILENCE_DETECTION)) {
713 xmlNodeSetContent(cur, (const xmlChar *)"on");
715 xmlNodeSetContent(cur, (const xmlChar *)"off");
721 int ret = xmlSaveFile(STT_CONFIG, g_config_doc);
722 SLOG(LOG_DEBUG, stt_tag(), "Save result : %d", ret);
728 int stt_parser_find_config_changed(char** engine, char** setting, int* auto_lang, char** language, int* silence, int* credential)
730 if (NULL == engine || NULL == language || NULL == silence || NULL == credential) {
731 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
735 xmlDocPtr doc = NULL;
736 xmlNodePtr cur_new = NULL;
737 xmlNodePtr cur_old = NULL;
742 doc = xmlParseFile(STT_CONFIG);
744 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to parse file error : %s", STT_CONFIG);
748 cur_new = xmlDocGetRootElement(doc);
749 cur_old = xmlDocGetRootElement(g_config_doc);
750 if (cur_new == NULL || cur_old == NULL) {
752 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
758 if (xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_BASE_TAG) || xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_BASE_TAG)) {
760 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
766 cur_new = cur_new->xmlChildrenNode;
767 cur_old = cur_old->xmlChildrenNode;
768 if (cur_new == NULL || cur_old == NULL) {
770 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
780 while (cur_new != NULL && cur_old != NULL) {
781 if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_ENGINE_ID)) {
782 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_ENGINE_ID)) {
783 key_old = xmlNodeGetContent(cur_old);
784 if (NULL != key_old) {
785 key_new = xmlNodeGetContent(cur_new);
786 if (NULL != key_new) {
787 if (0 != xmlStrcmp(key_old, key_new)) {
788 SLOG(LOG_DEBUG, stt_tag(), "Old engine id(%s), New engine(%s)", (char*)key_old, (char*)key_new);
789 if (NULL != *engine) free(*engine);
790 *engine = strdup((char*)key_new);
797 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Old config and new config are different"); //LCOV_EXCL_LINE
799 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_ENGINE_SETTING)) {
800 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_ENGINE_SETTING)) {
801 key_old = xmlNodeGetContent(cur_old);
802 if (NULL != key_old) {
803 key_new = xmlNodeGetContent(cur_new);
804 if (NULL != key_new) {
805 if (0 != xmlStrcmp(key_old, key_new)) {
806 SLOG(LOG_DEBUG, stt_tag(), "Old engine setting(%s), New engine setting(%s)", (char*)key_old, (char*)key_new); //LCOV_EXCL_LINE
807 if (NULL != *setting) free(*setting);
808 *setting = strdup((char*)key_new);
815 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Old config and new config are different"); //LCOV_EXCL_LINE
817 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_AUTO_LANGUAGE)) {
818 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_AUTO_LANGUAGE)) {
819 key_old = xmlNodeGetContent(cur_old);
820 if (NULL != key_old) {
821 key_new = xmlNodeGetContent(cur_new);
822 if (NULL != key_new) {
823 if (0 != xmlStrcmp(key_old, key_new)) {
824 SLOG(LOG_DEBUG, stt_tag(), "Old auto lang(%s), New auto lang(%s)", (char*)key_old, (char*)key_new); //LCOV_EXCL_LINE
825 if (0 == xmlStrcmp((const xmlChar*)"on", key_new)) {
826 *auto_lang = (int)true;
828 *auto_lang = (int)false;
837 SLOG(LOG_ERROR, stt_tag(), "[ERROR] old config and new config are different"); //LCOV_EXCL_LINE
839 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_LANGUAGE)) {
840 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_LANGUAGE)) {
841 key_old = xmlNodeGetContent(cur_old);
842 if (NULL != key_old) {
843 key_new = xmlNodeGetContent(cur_new);
844 if (NULL != key_new) {
845 if (0 != xmlStrcmp(key_old, key_new)) {
846 SLOG(LOG_DEBUG, stt_tag(), "Old language(%s), New language(%s)", (char*)key_old, (char*)key_new); //LCOV_EXCL_LINE
847 if (NULL != *language) free(*language);
848 *language = strdup((char*)key_new);
855 SLOG(LOG_ERROR, stt_tag(), "[ERROR] old config and new config are different"); //LCOV_EXCL_LINE
857 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_SILENCE_DETECTION)) {
858 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_SILENCE_DETECTION)) {
859 key_old = xmlNodeGetContent(cur_old);
860 if (NULL != key_old) {
861 key_new = xmlNodeGetContent(cur_new);
862 if (NULL != key_new) {
863 if (0 != xmlStrcmp(key_old, key_new)) {
864 SLOG(LOG_DEBUG, stt_tag(), "Old silence(%s), New silence(%s)", (char*)key_old, (char*)key_new); //LCOV_EXCL_LINE
865 if (0 == xmlStrcmp(key_new, (const xmlChar*)"on")) {
877 SLOG(LOG_ERROR, stt_tag(), "[ERROR] old config and new config are different"); //LCOV_EXCL_LINE
879 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_CREDENTIAL)) {
880 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_CREDENTIAL)) {
881 key_old = xmlNodeGetContent(cur_old);
882 if (NULL != key_old) {
883 key_new = xmlNodeGetContent(cur_new);
884 if (NULL != key_new) {
885 if (0 != xmlStrcmp(key_old, key_new)) {
886 SLOG(LOG_DEBUG, stt_tag(), "Old credential(%s), New credential(%s)", (char*)key_old, (char*)key_new); //LCOV_EXCL_LINE
887 if (0 == xmlStrcmp(key_new, (const xmlChar*)"true")) {
898 SLOG(LOG_ERROR, stt_tag(), "[ERROR] old config and new config are different"); //LCOV_EXCL_LINE
905 cur_new = cur_new->next;
906 cur_old = cur_old->next;
909 xmlFreeDoc(g_config_doc);
919 int stt_parser_set_time_info(GSList* time_list)
921 if (0 == g_slist_length(time_list)) {
922 SLOG(LOG_WARN, stt_tag(), "[WARNING] There is no time info to save");
926 if (-1 == remove(STT_TIME_INFO_PATH)) {
927 SLOG(LOG_WARN, stt_tag(), "[PLAYER WARNING] Fail to remove file(%s)", STT_TIME_INFO_PATH);
930 xmlDocPtr doc = NULL;
931 xmlNodePtr cur = NULL;
933 doc = xmlNewDoc((const xmlChar*)"1.0");
935 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to make new doc");
939 cur = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_BASE_TAG);
940 xmlDocSetRootElement(doc, cur);
942 xmlNodePtr inode = NULL;
944 stt_result_time_info_s *data = NULL;
947 snprintf(temp_str, 256, "%d", g_slist_length(time_list));
949 inode = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_INDEX);
950 xmlNewProp(inode, (const xmlChar*)STT_TAG_TIME_COUNT, (const xmlChar*)temp_str);
951 xmlAddChild(cur, inode);
953 /* Get a first item */
954 iter = g_slist_nth(time_list, 0);
955 while (NULL != iter) {
959 SLOG(LOG_DEBUG, stt_tag(), "data is NULL");
963 xmlNodePtr temp_node = NULL;
965 SECURE_SLOG(LOG_DEBUG, stt_tag(), "[%d] i(%d) t(%s) s(%ld) e(%ld)",
966 data->index, data->event, data->text, data->start_time, data->end_time);
968 temp_node = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_TEXT);
969 xmlNodeSetContent(temp_node, (const xmlChar*)data->text);
970 xmlAddChild(inode, temp_node);
972 temp_node = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_START);
973 snprintf(temp_str, 256, "%ld", data->start_time);
974 xmlNodeSetContent(temp_node, (const xmlChar*)temp_str);
975 xmlAddChild(inode, temp_node);
977 temp_node = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_END);
978 snprintf(temp_str, 256, "%ld", data->end_time);
979 xmlNodeSetContent(temp_node, (const xmlChar*)temp_str);
980 xmlAddChild(inode, temp_node);
983 iter = g_slist_next(iter);
986 int ret = xmlSaveFormatFile(STT_TIME_INFO_PATH, doc, 1);
987 SLOG(LOG_DEBUG, stt_tag(), "Save result : %d", ret);
994 void __stt_parser_time_info_free(void* data)
996 stt_result_time_info_s* time_info = (stt_result_time_info_s*)data;
998 if (NULL != time_info) {
999 if (NULL != time_info->text) {
1000 free(time_info->text);
1001 time_info->text = NULL;
1008 int stt_parser_get_time_info(GSList** time_list)
1010 if (NULL == time_list) {
1011 SLOG(LOG_ERROR, stt_tag(), "Invalid parameter : text is NULL"); //LCOV_EXCL_LINE
1015 xmlDocPtr doc = NULL;
1016 xmlNodePtr cur = NULL;
1018 doc = xmlParseFile(STT_TIME_INFO_PATH);
1020 SLOG(LOG_WARN, stt_tag(), "[WARNING] File is not exist"); //LCOV_EXCL_LINE
1024 cur = xmlDocGetRootElement(doc);
1027 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
1033 if (xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_TIME_BASE_TAG)) {
1035 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong type, root node is NOT '%s'", STT_TAG_TIME_BASE_TAG);
1041 cur = cur->xmlChildrenNode;
1044 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
1050 /* alloc time info */
1051 GSList *temp_time_list = NULL;
1052 xmlChar *key = NULL;
1055 while (cur != NULL) {
1056 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_TIME_INDEX)) {
1057 key = xmlGetProp(cur, (const xmlChar*)STT_TAG_TIME_COUNT);
1059 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_TIME_COUNT); //LCOV_EXCL_LINE
1061 if (NULL != temp_time_list) {
1062 g_slist_free_full(temp_time_list, __stt_parser_time_info_free);
1063 temp_time_list = NULL;
1069 SLOG(LOG_DEBUG, stt_tag(), "Count : %s", (char *)key);
1071 /* Get time count */
1073 count = atoi((char*)key);
1077 SLOG(LOG_ERROR, stt_tag(), "[ERROR] count is invalid : %d", count); //LCOV_EXCL_LINE
1081 xmlNodePtr time_node = NULL;
1082 time_node = cur->xmlChildrenNode;
1085 for (i = 0; i < count; i++) {
1087 time_node = time_node->next;
1089 stt_result_time_info_s* temp_info;
1090 temp_info = (stt_result_time_info_s*)calloc(1, sizeof(stt_result_time_info_s));
1092 if (NULL == temp_info) {
1093 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Memory alloc error!!"); //LCOV_EXCL_LINE
1095 if (NULL != temp_time_list) {
1096 g_slist_free_full(temp_time_list, __stt_parser_time_info_free);
1097 temp_time_list = NULL;
1103 temp_info->index = index;
1104 SLOG(LOG_DEBUG, stt_tag(), "index : %d", temp_info->index);
1106 if (0 == i) temp_info->event = 0;
1107 else if (count -1 == i) temp_info->event = 2;
1108 else temp_info->event = 1;
1110 if (0 == xmlStrcmp(time_node->name, (const xmlChar *)STT_TAG_TIME_TEXT)) {
1111 key = xmlNodeGetContent(time_node);
1113 SLOG(LOG_DEBUG, stt_tag(), "text : %s", (char *)key); //LCOV_EXCL_LINE
1114 temp_info->text = strdup((char*)key);
1117 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_TIME_TEXT); //LCOV_EXCL_LINE
1124 time_node = time_node->next;
1125 time_node = time_node->next;
1127 if (0 == xmlStrcmp(time_node->name, (const xmlChar *)STT_TAG_TIME_START)) {
1128 key = xmlNodeGetContent(time_node);
1130 SLOG(LOG_DEBUG, stt_tag(), "Start time : %s", (char *)key); //LCOV_EXCL_LINE
1131 temp_info->start_time = atoi((char*)key);
1134 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_TIME_START); //LCOV_EXCL_LINE
1135 if (NULL != temp_info->text) free(temp_info->text);
1142 time_node = time_node->next;
1143 time_node = time_node->next;
1145 if (0 == xmlStrcmp(time_node->name, (const xmlChar *)STT_TAG_TIME_END)) {
1146 key = xmlNodeGetContent(time_node);
1148 SLOG(LOG_DEBUG, stt_tag(), "End time : %s", (char *)key); //LCOV_EXCL_LINE
1149 temp_info->end_time = atoi((char*)key);
1152 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_TIME_END); //LCOV_EXCL_LINE
1153 if (NULL != temp_info->text) free(temp_info->text);
1159 time_node = time_node->next;
1161 temp_time_list = g_slist_append(temp_time_list, temp_info);
1170 *time_list = temp_time_list;
1175 int stt_parser_clear_time_info()
1177 if (-1 == remove(STT_TIME_INFO_PATH)) {
1178 /* SLOG(LOG_WARN, stt_tag(), "[PLAYER WARNING] Fail to remove file(%s)", STT_TIME_INFO_PATH); */