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 doc = xmlParseFile(STT_DEFAULT_CONFIG);
357 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to parse file error : %s", STT_DEFAULT_CONFIG);
361 is_default_open = true;
367 cur = xmlDocGetRootElement(doc);
369 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document"); //LCOV_EXCL_LINE
374 if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
375 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG); //LCOV_EXCL_LINE
380 cur = cur->xmlChildrenNode;
382 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document"); //LCOV_EXCL_LINE
387 /* alloc engine info */
389 temp = (stt_config_s*)calloc(1, sizeof(stt_config_s));
392 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to allocate memory"); //LCOV_EXCL_LINE
396 temp->engine_id = NULL;
397 temp->setting = NULL;
398 temp->language = NULL;
400 while (cur != NULL) {
401 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_ENGINE_ID)) {
402 key = xmlNodeGetContent(cur);
404 /*SLOG(LOG_DEBUG, stt_tag(), "Engine id : %s", (char *)key); */
405 if (NULL != temp->engine_id) free(temp->engine_id);
406 temp->engine_id = strdup((char*)key);
409 SLOG(LOG_ERROR, stt_tag(), "[ERROR] engine id is NULL"); //LCOV_EXCL_LINE
411 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_ENGINE_SETTING)) {
412 key = xmlNodeGetContent(cur);
414 /*SLOG(LOG_DEBUG, stt_tag(), "Setting path : %s", (char *)key); */
415 if (NULL != temp->setting) free(temp->setting);
416 temp->setting = strdup((char*)key);
419 SLOG(LOG_ERROR, stt_tag(), "[ERROR] setting path is NULL"); //LCOV_EXCL_LINE
422 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_AUTO_LANGUAGE)) {
423 key = xmlNodeGetContent(cur);
425 /*SLOG(LOG_DEBUG, stt_tag(), "Auto language : %s", (char *)key); */
427 if (0 == xmlStrcmp(key, (const xmlChar *)"on")) {
428 temp->auto_lang = true;
429 } else if (0 == xmlStrcmp(key, (const xmlChar *)"off")) {
430 temp->auto_lang = false;
432 SLOG(LOG_ERROR, stt_tag(), "Auto voice is wrong"); //LCOV_EXCL_LINE
433 temp->auto_lang = true;
438 SLOG(LOG_ERROR, stt_tag(), "[ERROR] auto language is NULL"); //LCOV_EXCL_LINE
440 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_LANGUAGE)) {
441 key = xmlNodeGetContent(cur);
443 /*SLOG(LOG_DEBUG, stt_tag(), "language : %s", (char *)key); */
444 if (NULL != temp->language) free(temp->language);
445 temp->language = strdup((char*)key);
448 SLOG(LOG_ERROR, stt_tag(), "[ERROR] language is NULL"); //LCOV_EXCL_LINE
450 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_SILENCE_DETECTION)) {
451 key = xmlNodeGetContent(cur);
453 /*SLOG(LOG_DEBUG, stt_tag(), "silence-detection : %s", (char *)key); */
455 if (0 == xmlStrcmp(key, (const xmlChar *)"on"))
456 temp->silence_detection = true;
458 temp->silence_detection = false;
462 SLOG(LOG_ERROR, stt_tag(), "[ERROR] silence-detection is NULL"); //LCOV_EXCL_LINE
464 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_CREDENTIAL)) {
465 key = xmlNodeGetContent(cur);
467 //SLOG(LOG_DEBUG, stt_tag(), "credential : %s", (char *)key);
469 if (0 == xmlStrcmp(key, (const xmlChar *)"true"))
470 temp->credential = true;
472 temp->credential = false;
476 SLOG(LOG_ERROR, stt_tag(), "[ERROR] credential is NULL"); //LCOV_EXCL_LINE
488 if (is_default_open) {
492 ret = xmlSaveFile(STT_CONFIG, g_config_doc);
498 if (STT_RETRY_COUNT == retry_count) {
499 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Save result : %d", ret);
505 if (0 > chmod(STT_CONFIG, 0600)) {
506 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to change file mode : %d", ret);
510 if (0 > chown(STT_CONFIG, 5000, 5000)) {
511 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to change file owner : %d", ret);
513 SLOG(LOG_DEBUG, stt_tag(), "Default config is changed : pid(%d)", getpid());
519 int stt_parser_unload_config(stt_config_s* config_info)
521 if (NULL != g_config_doc) xmlFreeDoc(g_config_doc);
522 if (NULL != config_info) {
523 if (NULL != config_info->engine_id) {
524 free(config_info->engine_id);
525 config_info->engine_id = NULL;
527 if (NULL != config_info->setting) {
528 free(config_info->setting);
529 config_info->setting = NULL;
531 if (NULL != config_info->language) {
532 free(config_info->language);
533 config_info->language = NULL;
542 int stt_parser_set_engine(const char* engine_id, const char* setting, const char* language, bool silence, bool credential)
544 if (NULL == g_config_doc || NULL == engine_id)
547 xmlNodePtr cur = NULL;
548 cur = xmlDocGetRootElement(g_config_doc);
550 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
554 if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
555 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
559 cur = cur->xmlChildrenNode;
561 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
565 while (cur != NULL) {
566 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_ENGINE_ID)) {
567 xmlNodeSetContent(cur, (const xmlChar *)engine_id);
570 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_LANGUAGE)) {
571 xmlNodeSetContent(cur, (const xmlChar *)language);
574 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_ENGINE_SETTING)) {
575 xmlNodeSetContent(cur, (const xmlChar *)setting);
578 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_SILENCE_DETECTION)) {
580 xmlNodeSetContent(cur, (const xmlChar *)"on");
582 xmlNodeSetContent(cur, (const xmlChar *)"off");
585 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_CREDENTIAL)) {
586 if (true == credential)
587 xmlNodeSetContent(cur, (const xmlChar *)"true");
589 xmlNodeSetContent(cur, (const xmlChar *)"false");
595 int ret = xmlSaveFile(STT_CONFIG, g_config_doc);
596 SLOG(LOG_DEBUG, stt_tag(), "Save result : %d", ret);
601 int stt_parser_set_language(const char* language)
603 if (NULL == g_config_doc || NULL == language)
606 xmlNodePtr cur = NULL;
607 cur = xmlDocGetRootElement(g_config_doc);
609 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
613 if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
614 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
618 cur = cur->xmlChildrenNode;
620 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
624 while (cur != NULL) {
625 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_LANGUAGE)) {
626 xmlNodeSetContent(cur, (const xmlChar *)language);
632 int ret = xmlSaveFile(STT_CONFIG, g_config_doc);
633 SLOG(LOG_DEBUG, stt_tag(), "Save result : %d", ret);
640 int stt_parser_set_auto_lang(bool value)
642 if (NULL == g_config_doc)
645 xmlNodePtr cur = NULL;
646 cur = xmlDocGetRootElement(g_config_doc);
648 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
652 if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
653 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
657 cur = cur->xmlChildrenNode;
659 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
663 while (cur != NULL) {
664 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_AUTO_LANGUAGE)) {
666 xmlNodeSetContent(cur, (const xmlChar *)"on");
667 } else if (false == value) {
668 xmlNodeSetContent(cur, (const xmlChar *)"off");
670 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong value of auto voice");
678 int ret = xmlSaveFile(STT_CONFIG, g_config_doc);
679 SLOG(LOG_DEBUG, stt_tag(), "Save result : %d", ret);
684 int stt_parser_set_silence_detection(bool value)
686 if (NULL == g_config_doc)
689 xmlNodePtr cur = NULL;
690 cur = xmlDocGetRootElement(g_config_doc);
692 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
696 if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
697 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
701 cur = cur->xmlChildrenNode;
703 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
707 while (cur != NULL) {
708 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_SILENCE_DETECTION)) {
710 xmlNodeSetContent(cur, (const xmlChar *)"on");
712 xmlNodeSetContent(cur, (const xmlChar *)"off");
718 int ret = xmlSaveFile(STT_CONFIG, g_config_doc);
719 SLOG(LOG_DEBUG, stt_tag(), "Save result : %d", ret);
725 int stt_parser_find_config_changed(char** engine, char** setting, int* auto_lang, char** language, int* silence, int* credential)
727 if (NULL == engine || NULL == language || NULL == silence || NULL == credential) {
728 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
732 xmlDocPtr doc = NULL;
733 xmlNodePtr cur_new = NULL;
734 xmlNodePtr cur_old = NULL;
739 doc = xmlParseFile(STT_CONFIG);
741 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to parse file error : %s", STT_CONFIG);
745 cur_new = xmlDocGetRootElement(doc);
746 cur_old = xmlDocGetRootElement(g_config_doc);
747 if (cur_new == NULL || cur_old == NULL) {
749 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
755 if (xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_BASE_TAG) || xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_BASE_TAG)) {
757 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
763 cur_new = cur_new->xmlChildrenNode;
764 cur_old = cur_old->xmlChildrenNode;
765 if (cur_new == NULL || cur_old == NULL) {
767 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
777 while (cur_new != NULL && cur_old != NULL) {
778 if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_ENGINE_ID)) {
779 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_ENGINE_ID)) {
780 key_old = xmlNodeGetContent(cur_old);
781 if (NULL != key_old) {
782 key_new = xmlNodeGetContent(cur_new);
783 if (NULL != key_new) {
784 if (0 != xmlStrcmp(key_old, key_new)) {
785 SLOG(LOG_DEBUG, stt_tag(), "Old engine id(%s), New engine(%s)", (char*)key_old, (char*)key_new);
786 if (NULL != *engine) free(*engine);
787 *engine = strdup((char*)key_new);
794 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Old config and new config are different"); //LCOV_EXCL_LINE
796 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_ENGINE_SETTING)) {
797 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_ENGINE_SETTING)) {
798 key_old = xmlNodeGetContent(cur_old);
799 if (NULL != key_old) {
800 key_new = xmlNodeGetContent(cur_new);
801 if (NULL != key_new) {
802 if (0 != xmlStrcmp(key_old, key_new)) {
803 SLOG(LOG_DEBUG, stt_tag(), "Old engine setting(%s), New engine setting(%s)", (char*)key_old, (char*)key_new); //LCOV_EXCL_LINE
804 if (NULL != *setting) free(*setting);
805 *setting = strdup((char*)key_new);
812 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Old config and new config are different"); //LCOV_EXCL_LINE
814 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_AUTO_LANGUAGE)) {
815 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_AUTO_LANGUAGE)) {
816 key_old = xmlNodeGetContent(cur_old);
817 if (NULL != key_old) {
818 key_new = xmlNodeGetContent(cur_new);
819 if (NULL != key_new) {
820 if (0 != xmlStrcmp(key_old, key_new)) {
821 SLOG(LOG_DEBUG, stt_tag(), "Old auto lang(%s), New auto lang(%s)", (char*)key_old, (char*)key_new); //LCOV_EXCL_LINE
822 if (0 == xmlStrcmp((const xmlChar*)"on", key_new)) {
823 *auto_lang = (int)true;
825 *auto_lang = (int)false;
834 SLOG(LOG_ERROR, stt_tag(), "[ERROR] old config and new config are different"); //LCOV_EXCL_LINE
836 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_LANGUAGE)) {
837 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_LANGUAGE)) {
838 key_old = xmlNodeGetContent(cur_old);
839 if (NULL != key_old) {
840 key_new = xmlNodeGetContent(cur_new);
841 if (NULL != key_new) {
842 if (0 != xmlStrcmp(key_old, key_new)) {
843 SLOG(LOG_DEBUG, stt_tag(), "Old language(%s), New language(%s)", (char*)key_old, (char*)key_new); //LCOV_EXCL_LINE
844 if (NULL != *language) free(*language);
845 *language = strdup((char*)key_new);
852 SLOG(LOG_ERROR, stt_tag(), "[ERROR] old config and new config are different"); //LCOV_EXCL_LINE
854 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_SILENCE_DETECTION)) {
855 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_SILENCE_DETECTION)) {
856 key_old = xmlNodeGetContent(cur_old);
857 if (NULL != key_old) {
858 key_new = xmlNodeGetContent(cur_new);
859 if (NULL != key_new) {
860 if (0 != xmlStrcmp(key_old, key_new)) {
861 SLOG(LOG_DEBUG, stt_tag(), "Old silence(%s), New silence(%s)", (char*)key_old, (char*)key_new); //LCOV_EXCL_LINE
862 if (0 == xmlStrcmp(key_new, (const xmlChar*)"on")) {
874 SLOG(LOG_ERROR, stt_tag(), "[ERROR] old config and new config are different"); //LCOV_EXCL_LINE
876 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_CREDENTIAL)) {
877 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_CREDENTIAL)) {
878 key_old = xmlNodeGetContent(cur_old);
879 if (NULL != key_old) {
880 key_new = xmlNodeGetContent(cur_new);
881 if (NULL != key_new) {
882 if (0 != xmlStrcmp(key_old, key_new)) {
883 SLOG(LOG_DEBUG, stt_tag(), "Old credential(%s), New credential(%s)", (char*)key_old, (char*)key_new); //LCOV_EXCL_LINE
884 if (0 == xmlStrcmp(key_new, (const xmlChar*)"true")) {
895 SLOG(LOG_ERROR, stt_tag(), "[ERROR] old config and new config are different"); //LCOV_EXCL_LINE
902 cur_new = cur_new->next;
903 cur_old = cur_old->next;
906 xmlFreeDoc(g_config_doc);
916 int stt_parser_set_time_info(GSList* time_list)
918 if (0 == g_slist_length(time_list)) {
919 SLOG(LOG_WARN, stt_tag(), "[WARNING] There is no time info to save");
923 if (-1 == remove(STT_TIME_INFO_PATH)) {
924 SLOG(LOG_WARN, stt_tag(), "[PLAYER WARNING] Fail to remove file(%s)", STT_TIME_INFO_PATH);
927 xmlDocPtr doc = NULL;
928 xmlNodePtr cur = NULL;
930 doc = xmlNewDoc((const xmlChar*)"1.0");
932 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to make new doc");
936 cur = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_BASE_TAG);
937 xmlDocSetRootElement(doc, cur);
939 xmlNodePtr inode = NULL;
941 stt_result_time_info_s *data = NULL;
944 snprintf(temp_str, 256, "%d", g_slist_length(time_list));
946 inode = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_INDEX);
947 xmlNewProp(inode, (const xmlChar*)STT_TAG_TIME_COUNT, (const xmlChar*)temp_str);
948 xmlAddChild(cur, inode);
950 /* Get a first item */
951 iter = g_slist_nth(time_list, 0);
952 while (NULL != iter) {
956 SLOG(LOG_DEBUG, stt_tag(), "data is NULL");
960 xmlNodePtr temp_node = NULL;
962 SECURE_SLOG(LOG_DEBUG, stt_tag(), "[%d] i(%d) t(%s) s(%ld) e(%ld)",
963 data->index, data->event, data->text, data->start_time, data->end_time);
965 temp_node = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_TEXT);
966 xmlNodeSetContent(temp_node, (const xmlChar*)data->text);
967 xmlAddChild(inode, temp_node);
969 temp_node = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_START);
970 snprintf(temp_str, 256, "%ld", data->start_time);
971 xmlNodeSetContent(temp_node, (const xmlChar*)temp_str);
972 xmlAddChild(inode, temp_node);
974 temp_node = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_END);
975 snprintf(temp_str, 256, "%ld", data->end_time);
976 xmlNodeSetContent(temp_node, (const xmlChar*)temp_str);
977 xmlAddChild(inode, temp_node);
980 iter = g_slist_next(iter);
983 int ret = xmlSaveFormatFile(STT_TIME_INFO_PATH, doc, 1);
984 SLOG(LOG_DEBUG, stt_tag(), "Save result : %d", ret);
991 void __stt_parser_time_info_free(void* data)
993 stt_result_time_info_s* time_info = (stt_result_time_info_s*)data;
995 if (NULL != time_info) {
996 if (NULL != time_info->text) {
997 free(time_info->text);
998 time_info->text = NULL;
1005 int stt_parser_get_time_info(GSList** time_list)
1007 if (NULL == time_list) {
1008 SLOG(LOG_ERROR, stt_tag(), "Invalid parameter : text is NULL"); //LCOV_EXCL_LINE
1012 xmlDocPtr doc = NULL;
1013 xmlNodePtr cur = NULL;
1015 doc = xmlParseFile(STT_TIME_INFO_PATH);
1017 SLOG(LOG_WARN, stt_tag(), "[WARNING] File is not exist"); //LCOV_EXCL_LINE
1021 cur = xmlDocGetRootElement(doc);
1024 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
1030 if (xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_TIME_BASE_TAG)) {
1032 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong type, root node is NOT '%s'", STT_TAG_TIME_BASE_TAG);
1038 cur = cur->xmlChildrenNode;
1041 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
1047 /* alloc time info */
1048 GSList *temp_time_list = NULL;
1049 xmlChar *key = NULL;
1052 while (cur != NULL) {
1053 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_TIME_INDEX)) {
1054 key = xmlGetProp(cur, (const xmlChar*)STT_TAG_TIME_COUNT);
1056 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_TIME_COUNT); //LCOV_EXCL_LINE
1058 if (NULL != temp_time_list) {
1059 g_slist_free_full(temp_time_list, __stt_parser_time_info_free);
1060 temp_time_list = NULL;
1066 SLOG(LOG_DEBUG, stt_tag(), "Count : %s", (char *)key);
1068 /* Get time count */
1070 count = atoi((char*)key);
1074 SLOG(LOG_ERROR, stt_tag(), "[ERROR] count is invalid : %d", count); //LCOV_EXCL_LINE
1078 xmlNodePtr time_node = NULL;
1079 time_node = cur->xmlChildrenNode;
1082 for (i = 0; i < count; i++) {
1084 time_node = time_node->next;
1086 stt_result_time_info_s* temp_info;
1087 temp_info = (stt_result_time_info_s*)calloc(1, sizeof(stt_result_time_info_s));
1089 if (NULL == temp_info) {
1090 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Memory alloc error!!"); //LCOV_EXCL_LINE
1092 if (NULL != temp_time_list) {
1093 g_slist_free_full(temp_time_list, __stt_parser_time_info_free);
1094 temp_time_list = NULL;
1100 temp_info->index = index;
1101 SLOG(LOG_DEBUG, stt_tag(), "index : %d", temp_info->index);
1103 if (0 == i) temp_info->event = 0;
1104 else if (count -1 == i) temp_info->event = 2;
1105 else temp_info->event = 1;
1107 if (0 == xmlStrcmp(time_node->name, (const xmlChar *)STT_TAG_TIME_TEXT)) {
1108 key = xmlNodeGetContent(time_node);
1110 SLOG(LOG_DEBUG, stt_tag(), "text : %s", (char *)key); //LCOV_EXCL_LINE
1111 temp_info->text = strdup((char*)key);
1114 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_TIME_TEXT); //LCOV_EXCL_LINE
1121 time_node = time_node->next;
1122 time_node = time_node->next;
1124 if (0 == xmlStrcmp(time_node->name, (const xmlChar *)STT_TAG_TIME_START)) {
1125 key = xmlNodeGetContent(time_node);
1127 SLOG(LOG_DEBUG, stt_tag(), "Start time : %s", (char *)key); //LCOV_EXCL_LINE
1128 temp_info->start_time = atoi((char*)key);
1131 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_TIME_START); //LCOV_EXCL_LINE
1132 if (NULL != temp_info->text) free(temp_info->text);
1139 time_node = time_node->next;
1140 time_node = time_node->next;
1142 if (0 == xmlStrcmp(time_node->name, (const xmlChar *)STT_TAG_TIME_END)) {
1143 key = xmlNodeGetContent(time_node);
1145 SLOG(LOG_DEBUG, stt_tag(), "End time : %s", (char *)key); //LCOV_EXCL_LINE
1146 temp_info->end_time = atoi((char*)key);
1149 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_TIME_END); //LCOV_EXCL_LINE
1150 if (NULL != temp_info->text) free(temp_info->text);
1156 time_node = time_node->next;
1158 temp_time_list = g_slist_append(temp_time_list, temp_info);
1167 *time_list = temp_time_list;
1172 int stt_parser_clear_time_info()
1174 if (-1 == remove(STT_TIME_INFO_PATH)) {
1175 /* SLOG(LOG_WARN, stt_tag(), "[PLAYER WARNING] Fail to remove file(%s)", STT_TIME_INFO_PATH); */