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 int stt_parser_get_engine_info(const char* path, stt_engine_info_s** engine_info)
56 if (NULL == path || NULL == engine_info) {
57 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
62 xmlNodePtr cur = NULL;
65 doc = xmlParseFile(path);
70 cur = xmlDocGetRootElement(doc);
73 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
79 if (xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_BASE_TAG)) {
81 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] The wrong type, root node is NOT 'stt-engine'");
87 cur = cur->xmlChildrenNode;
90 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
96 /* alloc engine info */
97 stt_engine_info_s* temp;
98 temp = (stt_engine_info_s*)calloc(1, sizeof(stt_engine_info_s));
102 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to allocate memory");
109 temp->setting = NULL;
110 temp->agreement = NULL;
111 temp->default_lang = NULL;
112 temp->languages = NULL;
113 temp->support_silence_detection = false;
114 temp->need_credential = false;
116 bool is_default_lang_set = false;
118 while (cur != NULL) {
119 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_NAME)) {
120 key = xmlNodeGetContent(cur);
122 /* SLOG(LOG_DEBUG, TAG_STTCONFIG, "Engine name : %s", (char *)key); */
123 if (NULL != temp->name) free(temp->name);
124 temp->name = strdup((char*)key);
127 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] <%s> has no content", STT_TAG_ENGINE_NAME); //LCOV_EXCL_LINE
129 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_ID)) {
130 key = xmlNodeGetContent(cur);
132 /* SLOG(LOG_DEBUG, TAG_STTCONFIG, "Engine uuid : %s", (char *)key); */
133 if (NULL != temp->uuid) free(temp->uuid);
134 temp->uuid = strdup((char*)key);
137 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] <%s> has no content", STT_TAG_ENGINE_ID); //LCOV_EXCL_LINE
139 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_SETTING)) {
140 key = xmlNodeGetContent(cur);
142 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Engine setting : %s", (char *)key);
143 if (NULL != temp->setting) free(temp->setting);
144 temp->setting = strdup((char*)key);
147 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] <%s> has no content", STT_TAG_ENGINE_SETTING); //LCOV_EXCL_LINE
149 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_AGREEMENT)) {
150 key = xmlNodeGetContent(cur);
152 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Engine agreement : %s", (char *)key);
153 if (NULL != temp->agreement) free(temp->agreement);
154 temp->agreement = strdup((char*)key);
157 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] <%s> has no content", STT_TAG_ENGINE_AGREEMENT); //LCOV_EXCL_LINE
159 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_DEFAULT)) {
160 key = xmlNodeGetContent(cur);
162 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Engine agreement : %s", (char *)key);
163 if (NULL != temp->default_lang) free(temp->default_lang);
164 temp->default_lang = strdup((char*)key);
166 is_default_lang_set = true;
169 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] <%s> has no content", STT_TAG_ENGINE_DEFAULT);
171 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_LANGUAGE_SET)) {
172 xmlNodePtr lang_node = NULL;
173 char* temp_lang = NULL;
175 lang_node = cur->xmlChildrenNode;
177 while (lang_node != NULL) {
178 if (0 == xmlStrcmp(lang_node->name, (const xmlChar *)STT_TAG_ENGINE_LANGUAGE)) {
179 key = xmlNodeGetContent(lang_node);
181 /* SLOG(LOG_DEBUG, TAG_STTCONFIG, "language : %s", (char *)key); */
182 temp_lang = strdup((char*)key);
183 temp->languages = g_slist_append(temp->languages, temp_lang);
185 if (false == is_default_lang_set) {
186 if (NULL != temp->default_lang) free(temp->default_lang);
187 temp->default_lang = strdup((char*)key);
189 is_default_lang_set = true;
193 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] <%s> has no content", STT_TAG_ENGINE_LANGUAGE); //LCOV_EXCL_LINE
197 lang_node = lang_node->next;
199 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_SILENCE_SUPPORT)) {
200 key = xmlNodeGetContent(cur);
202 /*SLOG(LOG_DEBUG, TAG_STTCONFIG, "silence-detection-support : %s", (char *)key); */
204 if (0 == xmlStrcmp(key, (const xmlChar *)"true"))
205 temp->support_silence_detection = true;
207 temp->support_silence_detection = false;
211 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] <%s> has no content", STT_TAG_ENGINE_SILENCE_SUPPORT); //LCOV_EXCL_LINE
213 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_ENGINE_CREDENTIAL_NEED)) {
214 key = xmlNodeGetContent(cur);
216 //SLOG(LOG_DEBUG, TAG_STTCONFIG, "app-credential-need : %s", (char *)key);
218 if (0 == xmlStrcmp(key, (const xmlChar *)"true"))
219 temp->need_credential = true;
221 temp->need_credential = false;
225 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] <%s> has no content", STT_TAG_ENGINE_CREDENTIAL_NEED);
236 if (NULL == temp->name || NULL == temp->uuid) {
238 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Invalid engine : %s", path);
239 stt_parser_free_engine_info(temp);
248 int stt_parser_free_engine_info(stt_engine_info_s* engine_info)
250 if (NULL == engine_info) {
251 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
255 if (NULL != engine_info->name) free(engine_info->name);
256 if (NULL != engine_info->uuid) free(engine_info->uuid);
257 if (NULL != engine_info->setting) free(engine_info->setting);
258 if (NULL != engine_info->agreement) free(engine_info->agreement);
260 int count = g_slist_length(engine_info->languages);
265 for (i = 0; i < count ; i++) {
266 temp_lang = g_slist_nth_data(engine_info->languages, 0);
268 if (NULL != temp_lang) {
269 engine_info->languages = g_slist_remove(engine_info->languages, temp_lang);
271 if (NULL != temp_lang)
276 if (NULL != engine_info) free(engine_info);
282 int stt_parser_print_engine_info(stt_engine_info_s* engine_info)
284 if (NULL == engine_info)
287 SLOG(LOG_DEBUG, TAG_STTCONFIG, "== get engine info ==");
288 SLOG(LOG_DEBUG, TAG_STTCONFIG, " name : %s", engine_info->name);
289 SLOG(LOG_DEBUG, TAG_STTCONFIG, " id : %s", engine_info->uuid);
290 if (NULL != engine_info->setting) SLOG(LOG_DEBUG, TAG_STTCONFIG, " setting : %s", engine_info->setting);
292 SLOG(LOG_DEBUG, TAG_STTCONFIG, " languages");
295 if (g_slist_length(engine_info->languages) > 0) {
296 /* Get a first item */
297 iter = g_slist_nth(engine_info->languages, 0);
300 while (NULL != iter) {
301 /*Get handle data from list*/
304 SLOG(LOG_DEBUG, TAG_STTCONFIG, " [%dth] %s", i, lang);
307 iter = g_slist_next(iter);
311 SLOG(LOG_ERROR, TAG_STTCONFIG, " language is NONE");
313 SLOG(LOG_DEBUG, TAG_STTCONFIG, " silence support : %s", engine_info->support_silence_detection ? "true" : "false");
314 SLOG(LOG_DEBUG, TAG_STTCONFIG, " credential need : %s", engine_info->need_credential ? "true" : "false");
315 SLOG(LOG_DEBUG, TAG_STTCONFIG, "=====================");
321 int stt_parser_load_config(stt_config_s** config_info)
323 if (NULL == config_info) {
324 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
328 xmlDocPtr doc = NULL;
329 xmlNodePtr cur = NULL;
331 bool is_default_open = false;
333 if (0 != access(STT_CONFIG, F_OK)) {
334 doc = xmlParseFile(STT_DEFAULT_CONFIG);
336 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to parse file error : %s", STT_DEFAULT_CONFIG); //LCOV_EXCL_LINE
340 is_default_open = true;
344 while (NULL == doc) {
345 doc = xmlParseFile(STT_CONFIG);
352 if (STT_RETRY_COUNT == retry_count) {
353 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to parse file error : %s", STT_CONFIG);
354 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Remove the file");
357 doc = xmlParseFile(STT_DEFAULT_CONFIG);
359 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to parse file error : %s", STT_DEFAULT_CONFIG);
363 is_default_open = true;
369 cur = xmlDocGetRootElement(doc);
371 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document"); //LCOV_EXCL_LINE
376 if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
377 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG); //LCOV_EXCL_LINE
382 cur = cur->xmlChildrenNode;
384 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document"); //LCOV_EXCL_LINE
389 /* alloc engine info */
391 temp = (stt_config_s*)calloc(1, sizeof(stt_config_s));
394 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to allocate memory"); //LCOV_EXCL_LINE
398 temp->engine_id = NULL;
399 temp->setting = NULL;
400 temp->language = NULL;
402 while (cur != NULL) {
403 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_ENGINE_ID)) {
404 key = xmlNodeGetContent(cur);
406 /*SLOG(LOG_DEBUG, TAG_STTCONFIG, "Engine id : %s", (char *)key); */
407 if (NULL != temp->engine_id) free(temp->engine_id);
408 temp->engine_id = strdup((char*)key);
411 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] engine id is NULL"); //LCOV_EXCL_LINE
413 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_ENGINE_SETTING)) {
414 key = xmlNodeGetContent(cur);
416 /*SLOG(LOG_DEBUG, TAG_STTCONFIG, "Setting path : %s", (char *)key); */
417 if (NULL != temp->setting) free(temp->setting);
418 temp->setting = strdup((char*)key);
421 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] setting path is NULL"); //LCOV_EXCL_LINE
424 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_AUTO_LANGUAGE)) {
425 key = xmlNodeGetContent(cur);
427 /*SLOG(LOG_DEBUG, TAG_STTCONFIG, "Auto language : %s", (char *)key); */
429 if (0 == xmlStrcmp(key, (const xmlChar *)"on")) {
430 temp->auto_lang = true;
431 } else if (0 == xmlStrcmp(key, (const xmlChar *)"off")) {
432 temp->auto_lang = false;
434 SLOG(LOG_ERROR, TAG_STTCONFIG, "Auto voice is wrong"); //LCOV_EXCL_LINE
435 temp->auto_lang = true;
440 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] auto language is NULL"); //LCOV_EXCL_LINE
442 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_LANGUAGE)) {
443 key = xmlNodeGetContent(cur);
445 /*SLOG(LOG_DEBUG, TAG_STTCONFIG, "language : %s", (char *)key); */
446 if (NULL != temp->language) free(temp->language);
447 temp->language = strdup((char*)key);
450 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] language is NULL"); //LCOV_EXCL_LINE
452 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_SILENCE_DETECTION)) {
453 key = xmlNodeGetContent(cur);
455 /*SLOG(LOG_DEBUG, TAG_STTCONFIG, "silence-detection : %s", (char *)key); */
457 if (0 == xmlStrcmp(key, (const xmlChar *)"on"))
458 temp->silence_detection = true;
460 temp->silence_detection = false;
464 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] silence-detection is NULL"); //LCOV_EXCL_LINE
466 } else if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_CREDENTIAL)) {
467 key = xmlNodeGetContent(cur);
469 //SLOG(LOG_DEBUG, TAG_STTCONFIG, "credential : %s", (char *)key);
471 if (0 == xmlStrcmp(key, (const xmlChar *)"true"))
472 temp->credential = true;
474 temp->credential = false;
478 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] credential is NULL"); //LCOV_EXCL_LINE
490 if (is_default_open) {
494 ret = xmlSaveFile(STT_CONFIG, g_config_doc);
500 if (STT_RETRY_COUNT == retry_count) {
501 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Save result : %d", ret);
507 if (0 > chmod(STT_CONFIG, 0600)) {
508 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to change file mode : %d", ret);
512 if (0 > chown(STT_CONFIG, 5000, 5000)) {
513 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to change file owner : %d", ret);
515 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Default config is changed : pid(%d)", getpid());
521 int stt_parser_unload_config(stt_config_s* config_info)
523 if (NULL != g_config_doc) {
524 xmlFreeDoc(g_config_doc);
527 if (NULL != config_info) {
528 if (NULL != config_info->engine_id) {
529 free(config_info->engine_id);
530 config_info->engine_id = NULL;
532 if (NULL != config_info->setting) {
533 free(config_info->setting);
534 config_info->setting = NULL;
536 if (NULL != config_info->language) {
537 free(config_info->language);
538 config_info->language = NULL;
547 int stt_parser_set_engine(const char* engine_id, const char* setting, const char* language, bool silence, bool credential)
549 if (NULL == g_config_doc || NULL == engine_id)
552 xmlNodePtr cur = NULL;
553 cur = xmlDocGetRootElement(g_config_doc);
555 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
559 if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
560 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
564 cur = cur->xmlChildrenNode;
566 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
570 while (cur != NULL) {
571 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_ENGINE_ID)) {
572 xmlNodeSetContent(cur, (const xmlChar *)engine_id);
575 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_LANGUAGE)) {
576 xmlNodeSetContent(cur, (const xmlChar *)language);
579 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_ENGINE_SETTING)) {
580 xmlNodeSetContent(cur, (const xmlChar *)setting);
583 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_SILENCE_DETECTION)) {
585 xmlNodeSetContent(cur, (const xmlChar *)"on");
587 xmlNodeSetContent(cur, (const xmlChar *)"off");
590 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_CREDENTIAL)) {
591 if (true == credential)
592 xmlNodeSetContent(cur, (const xmlChar *)"true");
594 xmlNodeSetContent(cur, (const xmlChar *)"false");
600 int ret = xmlSaveFile(STT_CONFIG, g_config_doc);
601 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Save result : %d", ret);
606 int stt_parser_set_language(const char* language)
608 if (NULL == g_config_doc || NULL == language)
611 xmlNodePtr cur = NULL;
612 cur = xmlDocGetRootElement(g_config_doc);
614 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
618 if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
619 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
623 cur = cur->xmlChildrenNode;
625 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
629 while (cur != NULL) {
630 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_LANGUAGE)) {
631 xmlNodeSetContent(cur, (const xmlChar *)language);
637 int ret = xmlSaveFile(STT_CONFIG, g_config_doc);
638 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Save result : %d", ret);
645 int stt_parser_set_auto_lang(bool value)
647 if (NULL == g_config_doc)
650 xmlNodePtr cur = NULL;
651 cur = xmlDocGetRootElement(g_config_doc);
653 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
657 if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
658 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
662 cur = cur->xmlChildrenNode;
664 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
668 while (cur != NULL) {
669 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_AUTO_LANGUAGE)) {
671 xmlNodeSetContent(cur, (const xmlChar *)"on");
672 } else if (false == value) {
673 xmlNodeSetContent(cur, (const xmlChar *)"off");
675 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] The wrong value of auto voice");
683 int ret = xmlSaveFile(STT_CONFIG, g_config_doc);
684 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Save result : %d", ret);
689 int stt_parser_set_silence_detection(bool value)
691 if (NULL == g_config_doc)
694 xmlNodePtr cur = NULL;
695 cur = xmlDocGetRootElement(g_config_doc);
697 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
701 if (xmlStrcmp(cur->name, (const xmlChar *) STT_TAG_CONFIG_BASE_TAG)) {
702 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
706 cur = cur->xmlChildrenNode;
708 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
712 while (cur != NULL) {
713 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_CONFIG_SILENCE_DETECTION)) {
715 xmlNodeSetContent(cur, (const xmlChar *)"on");
717 xmlNodeSetContent(cur, (const xmlChar *)"off");
723 int ret = xmlSaveFile(STT_CONFIG, g_config_doc);
724 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Save result : %d", ret);
730 int stt_parser_find_config_changed(char** engine, char** setting, int* auto_lang, char** language, int* silence, int* credential)
732 if (NULL == engine || NULL == language || NULL == silence || NULL == credential) {
733 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Input parameter is NULL"); //LCOV_EXCL_LINE
737 xmlDocPtr doc = NULL;
738 xmlNodePtr cur_new = NULL;
739 xmlNodePtr cur_old = NULL;
744 doc = xmlParseFile(STT_CONFIG);
746 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to parse file error : %s", STT_CONFIG);
750 cur_new = xmlDocGetRootElement(doc);
751 cur_old = xmlDocGetRootElement(g_config_doc);
752 if (cur_new == NULL || cur_old == NULL) {
754 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
760 if (xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_BASE_TAG) || xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_BASE_TAG)) {
762 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] The wrong type, root node is NOT %s", STT_TAG_CONFIG_BASE_TAG);
768 cur_new = cur_new->xmlChildrenNode;
769 cur_old = cur_old->xmlChildrenNode;
770 if (cur_new == NULL || cur_old == NULL) {
772 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
782 while (cur_new != NULL && cur_old != NULL) {
783 if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_ENGINE_ID)) {
784 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_ENGINE_ID)) {
785 key_old = xmlNodeGetContent(cur_old);
786 if (NULL != key_old) {
787 key_new = xmlNodeGetContent(cur_new);
788 if (NULL != key_new) {
789 if (0 != xmlStrcmp(key_old, key_new)) {
790 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Old engine id(%s), New engine(%s)", (char*)key_old, (char*)key_new);
791 if (NULL != *engine) free(*engine);
792 *engine = strdup((char*)key_new);
799 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Old config and new config are different"); //LCOV_EXCL_LINE
801 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_ENGINE_SETTING)) {
802 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_ENGINE_SETTING)) {
803 key_old = xmlNodeGetContent(cur_old);
804 if (NULL != key_old) {
805 key_new = xmlNodeGetContent(cur_new);
806 if (NULL != key_new) {
807 if (0 != xmlStrcmp(key_old, key_new)) {
808 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Old engine setting(%s), New engine setting(%s)", (char*)key_old, (char*)key_new); //LCOV_EXCL_LINE
809 if (NULL != *setting) free(*setting);
810 *setting = strdup((char*)key_new);
817 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Old config and new config are different"); //LCOV_EXCL_LINE
819 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_AUTO_LANGUAGE)) {
820 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_AUTO_LANGUAGE)) {
821 key_old = xmlNodeGetContent(cur_old);
822 if (NULL != key_old) {
823 key_new = xmlNodeGetContent(cur_new);
824 if (NULL != key_new) {
825 if (0 != xmlStrcmp(key_old, key_new)) {
826 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Old auto lang(%s), New auto lang(%s)", (char*)key_old, (char*)key_new); //LCOV_EXCL_LINE
827 if (0 == xmlStrcmp((const xmlChar*)"on", key_new)) {
828 *auto_lang = (int)true;
830 *auto_lang = (int)false;
839 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] old config and new config are different"); //LCOV_EXCL_LINE
841 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_LANGUAGE)) {
842 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_LANGUAGE)) {
843 key_old = xmlNodeGetContent(cur_old);
844 if (NULL != key_old) {
845 key_new = xmlNodeGetContent(cur_new);
846 if (NULL != key_new) {
847 if (0 != xmlStrcmp(key_old, key_new)) {
848 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Old language(%s), New language(%s)", (char*)key_old, (char*)key_new); //LCOV_EXCL_LINE
849 if (NULL != *language) free(*language);
850 *language = strdup((char*)key_new);
857 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] old config and new config are different"); //LCOV_EXCL_LINE
859 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_SILENCE_DETECTION)) {
860 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_SILENCE_DETECTION)) {
861 key_old = xmlNodeGetContent(cur_old);
862 if (NULL != key_old) {
863 key_new = xmlNodeGetContent(cur_new);
864 if (NULL != key_new) {
865 if (0 != xmlStrcmp(key_old, key_new)) {
866 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Old silence(%s), New silence(%s)", (char*)key_old, (char*)key_new); //LCOV_EXCL_LINE
867 if (0 == xmlStrcmp(key_new, (const xmlChar*)"on")) {
879 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] old config and new config are different"); //LCOV_EXCL_LINE
881 } else if (0 == xmlStrcmp(cur_new->name, (const xmlChar*)STT_TAG_CONFIG_CREDENTIAL)) {
882 if (0 == xmlStrcmp(cur_old->name, (const xmlChar*)STT_TAG_CONFIG_CREDENTIAL)) {
883 key_old = xmlNodeGetContent(cur_old);
884 if (NULL != key_old) {
885 key_new = xmlNodeGetContent(cur_new);
886 if (NULL != key_new) {
887 if (0 != xmlStrcmp(key_old, key_new)) {
888 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Old credential(%s), New credential(%s)", (char*)key_old, (char*)key_new); //LCOV_EXCL_LINE
889 if (0 == xmlStrcmp(key_new, (const xmlChar*)"true")) {
900 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] old config and new config are different"); //LCOV_EXCL_LINE
907 cur_new = cur_new->next;
908 cur_old = cur_old->next;
911 xmlFreeDoc(g_config_doc);
921 int stt_parser_set_time_info(GSList* time_list)
923 if (0 == g_slist_length(time_list)) {
924 SLOG(LOG_WARN, TAG_STTCONFIG, "[WARNING] There is no time info to save");
928 if (-1 == remove(STT_TIME_INFO_PATH)) {
929 SLOG(LOG_WARN, TAG_STTCONFIG, "[PLAYER WARNING] Fail to remove file(%s)", STT_TIME_INFO_PATH);
932 xmlDocPtr doc = NULL;
933 xmlNodePtr cur = NULL;
935 doc = xmlNewDoc((const xmlChar*)"1.0");
937 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Fail to make new doc");
941 cur = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_BASE_TAG);
942 xmlDocSetRootElement(doc, cur);
944 xmlNodePtr inode = NULL;
946 stt_result_time_info_s *data = NULL;
949 snprintf(temp_str, 256, "%d", g_slist_length(time_list));
951 inode = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_INDEX);
952 xmlNewProp(inode, (const xmlChar*)STT_TAG_TIME_COUNT, (const xmlChar*)temp_str);
953 xmlAddChild(cur, inode);
955 /* Get a first item */
956 iter = g_slist_nth(time_list, 0);
957 while (NULL != iter) {
961 SLOG(LOG_DEBUG, TAG_STTCONFIG, "data is NULL");
965 xmlNodePtr temp_node = NULL;
967 SECURE_SLOG(LOG_DEBUG, TAG_STTCONFIG, "[%d] i(%d) t(%s) s(%ld) e(%ld)",
968 data->index, data->event, data->text, data->start_time, data->end_time);
970 temp_node = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_TEXT);
971 xmlNodeSetContent(temp_node, (const xmlChar*)data->text);
972 xmlAddChild(inode, temp_node);
974 temp_node = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_START);
975 snprintf(temp_str, 256, "%ld", data->start_time);
976 xmlNodeSetContent(temp_node, (const xmlChar*)temp_str);
977 xmlAddChild(inode, temp_node);
979 temp_node = xmlNewNode(NULL, (const xmlChar*)STT_TAG_TIME_END);
980 snprintf(temp_str, 256, "%ld", data->end_time);
981 xmlNodeSetContent(temp_node, (const xmlChar*)temp_str);
982 xmlAddChild(inode, temp_node);
985 iter = g_slist_next(iter);
988 int ret = xmlSaveFormatFile(STT_TIME_INFO_PATH, doc, 1);
989 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Save result : %d", ret);
996 void __stt_parser_time_info_free(void* data)
998 stt_result_time_info_s* time_info = (stt_result_time_info_s*)data;
1000 if (NULL != time_info) {
1001 if (NULL != time_info->text) {
1002 free(time_info->text);
1003 time_info->text = NULL;
1010 int stt_parser_get_time_info(GSList** time_list)
1012 if (NULL == time_list) {
1013 SLOG(LOG_ERROR, TAG_STTCONFIG, "Invalid parameter : text is NULL"); //LCOV_EXCL_LINE
1017 xmlDocPtr doc = NULL;
1018 xmlNodePtr cur = NULL;
1020 doc = xmlParseFile(STT_TIME_INFO_PATH);
1022 SLOG(LOG_WARN, TAG_STTCONFIG, "[WARNING] File is not exist"); //LCOV_EXCL_LINE
1026 cur = xmlDocGetRootElement(doc);
1029 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
1035 if (xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_TIME_BASE_TAG)) {
1037 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] The wrong type, root node is NOT '%s'", STT_TAG_TIME_BASE_TAG);
1043 cur = cur->xmlChildrenNode;
1046 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Empty document");
1052 /* alloc time info */
1053 GSList *temp_time_list = NULL;
1054 xmlChar *key = NULL;
1057 while (cur != NULL) {
1058 if (0 == xmlStrcmp(cur->name, (const xmlChar *)STT_TAG_TIME_INDEX)) {
1059 key = xmlGetProp(cur, (const xmlChar*)STT_TAG_TIME_COUNT);
1061 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] <%s> has no content", STT_TAG_TIME_COUNT); //LCOV_EXCL_LINE
1063 if (NULL != temp_time_list) {
1064 g_slist_free_full(temp_time_list, __stt_parser_time_info_free);
1065 temp_time_list = NULL;
1071 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Count : %s", (char *)key);
1073 /* Get time count */
1075 count = atoi((char*)key);
1079 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] count is invalid : %d", count); //LCOV_EXCL_LINE
1083 xmlNodePtr time_node = NULL;
1084 time_node = cur->xmlChildrenNode;
1087 for (i = 0; i < count; i++) {
1089 time_node = time_node->next;
1091 stt_result_time_info_s* temp_info;
1092 temp_info = (stt_result_time_info_s*)calloc(1, sizeof(stt_result_time_info_s));
1094 if (NULL == temp_info) {
1095 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] Memory alloc error!!"); //LCOV_EXCL_LINE
1097 if (NULL != temp_time_list) {
1098 g_slist_free_full(temp_time_list, __stt_parser_time_info_free);
1099 temp_time_list = NULL;
1105 temp_info->index = index;
1106 SLOG(LOG_DEBUG, TAG_STTCONFIG, "index : %d", temp_info->index);
1108 if (0 == i) temp_info->event = 0;
1109 else if (count -1 == i) temp_info->event = 2;
1110 else temp_info->event = 1;
1112 if (0 == xmlStrcmp(time_node->name, (const xmlChar *)STT_TAG_TIME_TEXT)) {
1113 key = xmlNodeGetContent(time_node);
1115 SLOG(LOG_DEBUG, TAG_STTCONFIG, "text : %s", (char *)key); //LCOV_EXCL_LINE
1116 temp_info->text = strdup((char*)key);
1119 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] <%s> has no content", STT_TAG_TIME_TEXT); //LCOV_EXCL_LINE
1126 time_node = time_node->next;
1127 time_node = time_node->next;
1129 if (0 == xmlStrcmp(time_node->name, (const xmlChar *)STT_TAG_TIME_START)) {
1130 key = xmlNodeGetContent(time_node);
1132 SLOG(LOG_DEBUG, TAG_STTCONFIG, "Start time : %s", (char *)key); //LCOV_EXCL_LINE
1133 temp_info->start_time = atoi((char*)key);
1136 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] <%s> has no content", STT_TAG_TIME_START); //LCOV_EXCL_LINE
1137 if (NULL != temp_info->text) free(temp_info->text);
1144 time_node = time_node->next;
1145 time_node = time_node->next;
1147 if (0 == xmlStrcmp(time_node->name, (const xmlChar *)STT_TAG_TIME_END)) {
1148 key = xmlNodeGetContent(time_node);
1150 SLOG(LOG_DEBUG, TAG_STTCONFIG, "End time : %s", (char *)key); //LCOV_EXCL_LINE
1151 temp_info->end_time = atoi((char*)key);
1154 SLOG(LOG_ERROR, TAG_STTCONFIG, "[ERROR] <%s> has no content", STT_TAG_TIME_END); //LCOV_EXCL_LINE
1155 if (NULL != temp_info->text) free(temp_info->text);
1161 time_node = time_node->next;
1163 temp_time_list = g_slist_append(temp_time_list, temp_info);
1172 *time_list = temp_time_list;
1177 int stt_parser_clear_time_info()
1179 if (-1 == remove(STT_TIME_INFO_PATH)) {
1180 /* SLOG(LOG_WARN, TAG_STTCONFIG, "[PLAYER WARNING] Fail to remove file(%s)", STT_TIME_INFO_PATH); */