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");
63 xmlNodePtr cur = NULL;
66 doc = xmlParseFile(path);
71 cur = xmlDocGetRootElement(doc);
73 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
78 if (xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_BASE_TAG)) {
79 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong type, root node is NOT 'stt-engine'");
84 cur = cur->xmlChildrenNode;
86 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
91 /* alloc engine info */
92 stt_engine_info_s* temp;
93 temp = (stt_engine_info_s*)calloc(1, sizeof(stt_engine_info_s));
96 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to allocate memory");
102 temp->setting = NULL;
103 temp->agreement = NULL;
104 temp->default_lang = NULL;
105 temp->languages = NULL;
106 temp->support_silence_detection = false;
107 temp->need_credential = false;
109 bool is_default_lang_set = false;
111 while (cur != NULL) {
112 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_NAME)) {
113 key = xmlNodeGetContent(cur);
115 /* SLOG(LOG_DEBUG, stt_tag(), "Engine name : %s", (char *)key); */
116 if (NULL != temp->name) free(temp->name);
117 temp->name = strdup((char*)key);
120 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_ENGINE_NAME);
122 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_ID)) {
123 key = xmlNodeGetContent(cur);
125 /* SLOG(LOG_DEBUG, stt_tag(), "Engine uuid : %s", (char *)key); */
126 if (NULL != temp->uuid) free(temp->uuid);
127 temp->uuid = strdup((char*)key);
130 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_ENGINE_ID);
132 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_SETTING)) {
133 key = xmlNodeGetContent(cur);
135 SLOG(LOG_DEBUG, stt_tag(), "Engine setting : %s", (char *)key);
136 if (NULL != temp->setting) free(temp->setting);
137 temp->setting = strdup((char*)key);
140 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_ENGINE_SETTING);
142 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_AGREEMENT)) {
143 key = xmlNodeGetContent(cur);
145 SLOG(LOG_DEBUG, stt_tag(), "Engine agreement : %s", (char *)key);
146 if (NULL != temp->agreement) free(temp->agreement);
147 temp->agreement = strdup((char*)key);
150 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_ENGINE_AGREEMENT);
152 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_DEFAULT)) {
153 key = xmlNodeGetContent(cur);
155 SLOG(LOG_DEBUG, stt_tag(), "Engine agreement : %s", (char *)key);
156 if (NULL != temp->default_lang) free(temp->default_lang);
157 temp->default_lang = strdup((char*)key);
159 is_default_lang_set = true;
162 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_ENGINE_DEFAULT);
164 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_LANGUAGE_SET)) {
165 xmlNodePtr lang_node = NULL;
166 char* temp_lang = NULL;
168 lang_node = cur->xmlChildrenNode;
170 while (lang_node != NULL) {
171 if (0 == xmlStrcmp(lang_node->name, (const xmlChar *)STT_TAG_ENGINE_LANGUAGE)) {
172 key = xmlNodeGetContent(lang_node);
174 /* SLOG(LOG_DEBUG, stt_tag(), "language : %s", (char *)key); */
175 temp_lang = strdup((char*)key);
176 temp->languages = g_slist_append(temp->languages, temp_lang);
178 if (false == is_default_lang_set) {
179 if (NULL != temp->default_lang) free(temp->default_lang);
180 temp->default_lang = strdup((char*)key);
182 is_default_lang_set = true;
186 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_ENGINE_LANGUAGE);
190 lang_node = lang_node->next;
192 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_SILENCE_SUPPORT)) {
193 key = xmlNodeGetContent(cur);
195 /*SLOG(LOG_DEBUG, stt_tag(), "silence-detection-support : %s", (char *)key); */
197 if (0 == xmlStrcmp(key, (const xmlChar *)"true"))
198 temp->support_silence_detection = true;
200 temp->support_silence_detection = false;
204 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_ENGINE_SILENCE_SUPPORT);
206 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_CREDENTIAL_NEED)) {
207 key = xmlNodeGetContent(cur);
209 //SLOG(LOG_DEBUG, stt_tag(), "app-credential-need : %s", (char *)key);
211 if (0 == xmlStrcmp(key, (const xmlChar *)"true"))
212 temp->need_credential = true;
214 temp->need_credential = false;
218 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_ENGINE_CREDENTIAL_NEED);
229 if (NULL == temp->name || NULL == temp->uuid) {
231 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Invalid engine : %s", path);
232 stt_parser_free_engine_info(temp);
241 int stt_parser_free_engine_info(stt_engine_info_s* engine_info)
243 if (NULL == engine_info) {
244 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Input parameter is NULL");
248 if (NULL != engine_info->name) free(engine_info->name);
249 if (NULL != engine_info->uuid) free(engine_info->uuid);
250 if (NULL != engine_info->setting) free(engine_info->setting);
251 if (NULL != engine_info->agreement) free(engine_info->agreement);
253 int count = g_slist_length(engine_info->languages);
258 for (i = 0; i < count ; i++) {
259 temp_lang = g_slist_nth_data(engine_info->languages, 0);
261 if (NULL != temp_lang) {
262 engine_info->languages = g_slist_remove(engine_info->languages, temp_lang);
264 if (NULL != temp_lang)
269 if (NULL != engine_info) free(engine_info);
274 int stt_parser_print_engine_info(stt_engine_info_s* engine_info)
276 if (NULL == engine_info)
279 SLOG(LOG_DEBUG, stt_tag(), "== get engine info ==");
280 SLOG(LOG_DEBUG, stt_tag(), " name : %s", engine_info->name);
281 SLOG(LOG_DEBUG, stt_tag(), " id : %s", engine_info->uuid);
282 if (NULL != engine_info->setting) SLOG(LOG_DEBUG, stt_tag(), " setting : %s", engine_info->setting);
284 SLOG(LOG_DEBUG, stt_tag(), " languages");
287 if (g_slist_length(engine_info->languages) > 0) {
288 /* Get a first item */
289 iter = g_slist_nth(engine_info->languages, 0);
292 while (NULL != iter) {
293 /*Get handle data from list*/
296 SLOG(LOG_DEBUG, stt_tag(), " [%dth] %s", i, lang);
299 iter = g_slist_next(iter);
303 SLOG(LOG_ERROR, stt_tag(), " language is NONE");
305 SLOG(LOG_DEBUG, stt_tag(), " silence support : %s", engine_info->support_silence_detection ? "true" : "false");
306 SLOG(LOG_DEBUG, stt_tag(), " credential need : %s", engine_info->need_credential ? "true" : "false");
307 SLOG(LOG_DEBUG, stt_tag(), "=====================");
312 int stt_parser_load_config(stt_config_s** config_info)
314 if (NULL == config_info) {
315 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Input parameter is NULL");
319 xmlDocPtr doc = NULL;
320 xmlNodePtr cur = NULL;
322 bool is_default_open = false;
324 if (0 != access(STT_CONFIG, F_OK)) {
325 doc = xmlParseFile(STT_DEFAULT_CONFIG);
327 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to parse file error : %s", STT_DEFAULT_CONFIG);
331 is_default_open = true;
335 while (NULL == doc) {
336 doc = xmlParseFile(STT_CONFIG);
343 if (STT_RETRY_COUNT == retry_count) {
344 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to parse file error : %s", STT_CONFIG);
345 doc = xmlParseFile(STT_DEFAULT_CONFIG);
347 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to parse file error : %s", STT_DEFAULT_CONFIG);
351 is_default_open = true;
357 cur = xmlDocGetRootElement(doc);
359 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
364 if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
365 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
370 cur = cur->xmlChildrenNode;
372 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
377 /* alloc engine info */
379 temp = (stt_config_s*)calloc(1, sizeof(stt_config_s));
382 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to allocate memory");
386 temp->engine_id = NULL;
387 temp->setting = NULL;
388 temp->language = NULL;
390 while (cur != NULL) {
391 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_ENGINE_ID)) {
392 key = xmlNodeGetContent(cur);
394 /*SLOG(LOG_DEBUG, stt_tag(), "Engine id : %s", (char *)key); */
395 if (NULL != temp->engine_id) free(temp->engine_id);
396 temp->engine_id = strdup((char*)key);
399 SLOG(LOG_ERROR, stt_tag(), "[ERROR] engine id is NULL");
401 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_ENGINE_SETTING)) {
402 key = xmlNodeGetContent(cur);
404 /*SLOG(LOG_DEBUG, stt_tag(), "Setting path : %s", (char *)key); */
405 if (NULL != temp->setting) free(temp->setting);
406 temp->setting = strdup((char*)key);
409 SLOG(LOG_ERROR, stt_tag(), "[ERROR] setting path is NULL");
412 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_AUTO_LANGUAGE)) {
413 key = xmlNodeGetContent(cur);
415 /*SLOG(LOG_DEBUG, stt_tag(), "Auto language : %s", (char *)key); */
417 if (0 == xmlStrcmp(key, (const xmlChar *)"on")) {
418 temp->auto_lang = true;
419 } else if (0 == xmlStrcmp(key, (const xmlChar *)"off")) {
420 temp->auto_lang = false;
422 SLOG(LOG_ERROR, stt_tag(), "Auto voice is wrong");
423 temp->auto_lang = true;
428 SLOG(LOG_ERROR, stt_tag(), "[ERROR] auto langauge is NULL");
430 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_LANGUAGE)) {
431 key = xmlNodeGetContent(cur);
433 /*SLOG(LOG_DEBUG, stt_tag(), "language : %s", (char *)key); */
434 if (NULL != temp->language) free(temp->language);
435 temp->language = strdup((char*)key);
438 SLOG(LOG_ERROR, stt_tag(), "[ERROR] language is NULL");
440 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_SILENCE_DETECTION)) {
441 key = xmlNodeGetContent(cur);
443 /*SLOG(LOG_DEBUG, stt_tag(), "silence-detection : %s", (char *)key); */
445 if (0 == xmlStrcmp(key, (const xmlChar *)"on"))
446 temp->silence_detection = true;
448 temp->silence_detection = false;
452 SLOG(LOG_ERROR, stt_tag(), "[ERROR] silence-detection is NULL");
454 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_CREDENTIAL)) {
455 key = xmlNodeGetContent(cur);
457 //SLOG(LOG_DEBUG, stt_tag(), "credential : %s", (char *)key);
459 if (0 == xmlStrcmp(key, (const xmlChar *)"true"))
460 temp->credential = true;
462 temp->credential = false;
466 SLOG(LOG_ERROR, stt_tag(), "[ERROR] credential is NULL");
478 if (is_default_open) {
482 ret = xmlSaveFile(STT_CONFIG, g_config_doc);
488 if (STT_RETRY_COUNT == retry_count) {
489 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Save result : %d", ret);
495 if (0 > chmod(STT_CONFIG, 0600)) {
496 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to change file mode : %d", ret);
500 if (0 > chown(STT_CONFIG, 5000, 5000)) {
501 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to change file owner : %d", ret);
503 SLOG(LOG_DEBUG, stt_tag(), "Default config is changed : pid(%d)", getpid());
509 int stt_parser_unload_config(stt_config_s* config_info)
511 if (NULL != g_config_doc) xmlFreeDoc(g_config_doc);
512 if (NULL != config_info) {
513 if (NULL != config_info->engine_id) {
514 free(config_info->engine_id);
515 config_info->engine_id = NULL;
517 if (NULL != config_info->setting) {
518 free(config_info->setting);
519 config_info->setting = NULL;
521 if (NULL != config_info->language) {
522 free(config_info->language);
523 config_info->language = NULL;
532 int stt_parser_set_engine(const char* engine_id, const char* setting, const char* language, bool silence, bool credential)
534 if (NULL == g_config_doc || NULL == engine_id)
537 xmlNodePtr cur = NULL;
538 cur = xmlDocGetRootElement(g_config_doc);
540 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
544 if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
545 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
549 cur = cur->xmlChildrenNode;
551 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
555 while (cur != NULL) {
556 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_ENGINE_ID)) {
557 xmlNodeSetContent(cur, (const xmlChar *)engine_id);
560 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_LANGUAGE)) {
561 xmlNodeSetContent(cur, (const xmlChar *)language);
564 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_ENGINE_SETTING)) {
565 xmlNodeSetContent(cur, (const xmlChar *)setting);
568 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_SILENCE_DETECTION)) {
570 xmlNodeSetContent(cur, (const xmlChar *)"on");
572 xmlNodeSetContent(cur, (const xmlChar *)"off");
575 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_CREDENTIAL)) {
576 if (true == credential)
577 xmlNodeSetContent(cur, (const xmlChar *)"true");
579 xmlNodeSetContent(cur, (const xmlChar *)"false");
585 int ret = xmlSaveFile(STT_CONFIG, g_config_doc);
586 SLOG(LOG_DEBUG, stt_tag(), "Save result : %d", ret);
591 int stt_parser_set_language(const char* language)
593 if (NULL == g_config_doc || NULL == language)
596 xmlNodePtr cur = NULL;
597 cur = xmlDocGetRootElement(g_config_doc);
599 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
603 if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
604 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
608 cur = cur->xmlChildrenNode;
610 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
614 while (cur != NULL) {
615 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_LANGUAGE)) {
616 xmlNodeSetContent(cur, (const xmlChar *)language);
622 int ret = xmlSaveFile(STT_CONFIG, g_config_doc);
623 SLOG(LOG_DEBUG, stt_tag(), "Save result : %d", ret);
629 int stt_parser_set_auto_lang(bool value)
631 if (NULL == g_config_doc)
634 xmlNodePtr cur = NULL;
635 cur = xmlDocGetRootElement(g_config_doc);
637 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
641 if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
642 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
646 cur = cur->xmlChildrenNode;
648 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
652 while (cur != NULL) {
653 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_AUTO_LANGUAGE)) {
655 xmlNodeSetContent(cur, (const xmlChar *)"on");
656 } else if (false == value) {
657 xmlNodeSetContent(cur, (const xmlChar *)"off");
659 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong value of auto voice");
667 int ret = xmlSaveFile(STT_CONFIG, g_config_doc);
668 SLOG(LOG_DEBUG, stt_tag(), "Save result : %d", ret);
673 int stt_parser_set_silence_detection(bool value)
675 if (NULL == g_config_doc)
678 xmlNodePtr cur = NULL;
679 cur = xmlDocGetRootElement(g_config_doc);
681 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
685 if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
686 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
690 cur = cur->xmlChildrenNode;
692 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
696 while (cur != NULL) {
697 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_SILENCE_DETECTION)) {
699 xmlNodeSetContent(cur, (const xmlChar *)"on");
701 xmlNodeSetContent(cur, (const xmlChar *)"off");
707 int ret = xmlSaveFile(STT_CONFIG, g_config_doc);
708 SLOG(LOG_DEBUG, stt_tag(), "Save result : %d", ret);
713 int stt_parser_find_config_changed(char** engine, char** setting, int* auto_lang, char** language, int* silence, int* credential)
715 if (NULL == engine || NULL == language || NULL == silence || NULL == credential) {
716 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Input parameter is NULL");
720 xmlDocPtr doc = NULL;
721 xmlNodePtr cur_new = NULL;
722 xmlNodePtr cur_old = NULL;
727 doc = xmlParseFile(STT_CONFIG);
729 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to parse file error : %s", STT_CONFIG);
733 cur_new = xmlDocGetRootElement(doc);
734 cur_old = xmlDocGetRootElement(g_config_doc);
735 if (cur_new == NULL || cur_old == NULL) {
736 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
741 if (xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_BASE_TAG) || xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_BASE_TAG)) {
742 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
747 cur_new = cur_new->xmlChildrenNode;
748 cur_old = cur_old->xmlChildrenNode;
749 if (cur_new == NULL || cur_old == NULL) {
750 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
759 while (cur_new != NULL && cur_old != NULL) {
760 if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_ENGINE_ID)) {
761 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_ENGINE_ID)) {
762 key_old = xmlNodeGetContent(cur_old);
763 if (NULL != key_old) {
764 key_new = xmlNodeGetContent(cur_new);
765 if (NULL != key_new) {
766 if (0 != xmlStrcmp(key_old, key_new)) {
767 SLOG(LOG_DEBUG, stt_tag(), "Old engine id(%s), New engine(%s)", (char*)key_old, (char*)key_new);
768 if (NULL != *engine) free(*engine);
769 *engine = strdup((char*)key_new);
776 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Old config and new config are different");
778 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_ENGINE_SETTING)) {
779 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_ENGINE_SETTING)) {
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 setting(%s), New engine setting(%s)", (char*)key_old, (char*)key_new);
786 if (NULL != *setting) free(*setting);
787 *setting = strdup((char*)key_new);
794 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Old config and new config are different");
796 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_AUTO_LANGUAGE)) {
797 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_AUTO_LANGUAGE)) {
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 auto lang(%s), New auto lang(%s)", (char*)key_old, (char*)key_new);
804 if (0 == xmlStrcmp((const xmlChar*)"on", key_new)) {
805 *auto_lang = (int)true;
807 *auto_lang = (int)false;
816 SLOG(LOG_ERROR, stt_tag(), "[ERROR] old config and new config are different");
818 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_LANGUAGE)) {
819 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_LANGUAGE)) {
820 key_old = xmlNodeGetContent(cur_old);
821 if (NULL != key_old) {
822 key_new = xmlNodeGetContent(cur_new);
823 if (NULL != key_new) {
824 if (0 != xmlStrcmp(key_old, key_new)) {
825 SLOG(LOG_DEBUG, stt_tag(), "Old language(%s), New language(%s)", (char*)key_old, (char*)key_new);
826 if (NULL != *language) free(*language);
827 *language = strdup((char*)key_new);
834 SLOG(LOG_ERROR, stt_tag(), "[ERROR] old config and new config are different");
836 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_SILENCE_DETECTION)) {
837 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_SILENCE_DETECTION)) {
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 silence(%s), New silence(%s)", (char*)key_old, (char*)key_new);
844 if (0 == xmlStrcmp(key_new, (const xmlChar*)"on")) {
856 SLOG(LOG_ERROR, stt_tag(), "[ERROR] old config and new config are different");
858 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_CREDENTIAL)) {
859 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_CREDENTIAL)) {
860 key_old = xmlNodeGetContent(cur_old);
861 if (NULL != key_old) {
862 key_new = xmlNodeGetContent(cur_new);
863 if (NULL != key_new) {
864 if (0 != xmlStrcmp(key_old, key_new)) {
865 SLOG(LOG_DEBUG, stt_tag(), "Old credential(%s), New credential(%s)", (char*)key_old, (char*)key_new);
866 if (0 == xmlStrcmp(key_new, (const xmlChar*)"true")) {
877 SLOG(LOG_ERROR, stt_tag(), "[ERROR] old config and new config are different");
884 cur_new = cur_new->next;
885 cur_old = cur_old->next;
888 xmlFreeDoc(g_config_doc);
898 int stt_parser_set_time_info(GSList* time_list)
900 if (0 == g_slist_length(time_list)) {
901 SLOG(LOG_WARN, stt_tag(), "[WARNING] There is no time info to save");
905 if (-1 == remove(STT_TIME_INFO_PATH)) {
906 SLOG(LOG_WARN, stt_tag(), "[PLAYER WARNING] Fail to remove file(%s)", STT_TIME_INFO_PATH);
909 xmlDocPtr doc = NULL;
910 xmlNodePtr cur = NULL;
912 doc = xmlNewDoc((const xmlChar*)"1.0");
914 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to make new doc");
918 cur = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_BASE_TAG);
919 xmlDocSetRootElement(doc, cur);
921 xmlNodePtr inode = NULL;
923 stt_result_time_info_s *data = NULL;
926 snprintf(temp_str, 256, "%d", g_slist_length(time_list));
928 inode = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_INDEX);
929 xmlNewProp(inode, (const xmlChar*)STT_TAG_TIME_COUNT, (const xmlChar*)temp_str);
930 xmlAddChild(cur, inode);
932 /* Get a first item */
933 iter = g_slist_nth(time_list, 0);
934 while (NULL != iter) {
938 SLOG(LOG_DEBUG, stt_tag(), "data is NULL");
942 xmlNodePtr temp_node = NULL;
944 SECURE_SLOG(LOG_DEBUG, stt_tag(), "[%d] i(%d) t(%s) s(%ld) e(%ld)",
945 data->index, data->event, data->text, data->start_time, data->end_time);
947 temp_node = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_TEXT);
948 xmlNodeSetContent(temp_node, (const xmlChar*)data->text);
949 xmlAddChild(inode, temp_node);
951 temp_node = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_START);
952 snprintf(temp_str, 256, "%ld", data->start_time);
953 xmlNodeSetContent(temp_node, (const xmlChar*)temp_str);
954 xmlAddChild(inode, temp_node);
956 temp_node = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_END);
957 snprintf(temp_str, 256, "%ld", data->end_time);
958 xmlNodeSetContent(temp_node, (const xmlChar*)temp_str);
959 xmlAddChild(inode, temp_node);
962 iter = g_slist_next(iter);
965 int ret = xmlSaveFormatFile(STT_TIME_INFO_PATH, doc, 1);
966 SLOG(LOG_DEBUG, stt_tag(), "Save result : %d", ret);
972 void __stt_parser_time_info_free(void* data)
974 stt_result_time_info_s* time_info = (stt_result_time_info_s*)data;
976 if (NULL != time_info) {
977 if (NULL != time_info->text) {
978 free(time_info->text);
979 time_info->text = NULL;
986 int stt_parser_get_time_info(GSList** time_list)
988 if (NULL == time_list) {
989 SLOG(LOG_ERROR, stt_tag(), "Invalid paramter : text is NULL");
993 xmlDocPtr doc = NULL;
994 xmlNodePtr cur = NULL;
996 doc = xmlParseFile(STT_TIME_INFO_PATH);
998 SLOG(LOG_WARN, stt_tag(), "[WARNING] File is not exist");
1002 cur = xmlDocGetRootElement(doc);
1004 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
1009 if (xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_TIME_BASE_TAG)) {
1010 SLOG(LOG_ERROR, stt_tag(), "[ERROR] The wrong type, root node is NOT '%s'", STT_TAG_TIME_BASE_TAG);
1015 cur = cur->xmlChildrenNode;
1017 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Empty document");
1022 /* alloc time info */
1023 GSList *temp_time_list = NULL;
1024 xmlChar *key = NULL;
1027 while (cur != NULL) {
1028 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_TIME_INDEX)) {
1029 key = xmlGetProp(cur, (const xmlChar*)STT_TAG_TIME_COUNT);
1031 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_TIME_COUNT);
1033 if (NULL != temp_time_list) {
1034 g_slist_free_full(temp_time_list, __stt_parser_time_info_free);
1035 temp_time_list = NULL;
1041 SLOG(LOG_DEBUG, stt_tag(), "Count : %s", (char *)key);
1043 /* Get time count */
1045 count = atoi((char*)key);
1049 SLOG(LOG_ERROR, stt_tag(), "[ERROR] count is invalid : %d", count);
1053 xmlNodePtr time_node = NULL;
1054 time_node = cur->xmlChildrenNode;
1057 for (i = 0; i < count; i++) {
1059 time_node = time_node->next;
1061 stt_result_time_info_s* temp_info;
1062 temp_info = (stt_result_time_info_s*)calloc(1, sizeof(stt_result_time_info_s));
1064 if (NULL == temp_info) {
1065 SLOG(LOG_ERROR, stt_tag(), "[ERROR] Memory alloc error!!");
1067 if (NULL != temp_time_list) {
1068 g_slist_free_full(temp_time_list, __stt_parser_time_info_free);
1069 temp_time_list = NULL;
1075 temp_info->index = index;
1076 SLOG(LOG_DEBUG, stt_tag(), "index : %d", temp_info->index);
1078 if (0 == i) temp_info->event = 0;
1079 else if (count -1 == i) temp_info->event = 2;
1080 else temp_info->event = 1;
1082 if (0 == xmlStrcmp(time_node->name, (const xmlChar *)STT_TAG_TIME_TEXT)) {
1083 key = xmlNodeGetContent(time_node);
1085 SLOG(LOG_DEBUG, stt_tag(), "text : %s", (char *)key);
1086 temp_info->text = strdup((char*)key);
1089 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_TIME_TEXT);
1096 time_node = time_node->next;
1097 time_node = time_node->next;
1099 if (0 == xmlStrcmp(time_node->name, (const xmlChar *)STT_TAG_TIME_START)) {
1100 key = xmlNodeGetContent(time_node);
1102 SLOG(LOG_DEBUG, stt_tag(), "Start time : %s", (char *)key);
1103 temp_info->start_time = atoi((char*)key);
1106 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_TIME_START);
1107 if (NULL != temp_info->text) free(temp_info->text);
1114 time_node = time_node->next;
1115 time_node = time_node->next;
1117 if (0 == xmlStrcmp(time_node->name, (const xmlChar *)STT_TAG_TIME_END)) {
1118 key = xmlNodeGetContent(time_node);
1120 SLOG(LOG_DEBUG, stt_tag(), "End time : %s", (char *)key);
1121 temp_info->end_time = atoi((char*)key);
1124 SLOG(LOG_ERROR, stt_tag(), "[ERROR] <%s> has no content", STT_TAG_TIME_END);
1125 if (NULL != temp_info->text) free(temp_info->text);
1131 time_node = time_node->next;
1133 temp_time_list = g_slist_append(temp_time_list, temp_info);
1142 *time_list = temp_time_list;
1147 int stt_parser_clear_time_info()
1149 if (-1 == remove(STT_TIME_INFO_PATH)) {
1150 /* SLOG(LOG_WARN, stt_tag(), "[PLAYER WARNING] Fail to remove file(%s)", STT_TIME_INFO_PATH); */