2 * Copyright 2018-2019 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <libxml/parser.h>
18 #include <sys/types.h>
21 #include "multi_assistant_config.h"
22 #include "multi_assistant_main.h"
24 int mas_config_parse_assistant_info(mas_config_assistant_info_cb callback, const char *path, void* user_data)
27 xmlNodePtr cur = NULL;
34 doc = xmlParseFile(path);
39 if (MA_RETRY_COUNT == retry_count++) {
40 MAS_LOGE("[ERROR] Fail to parse file error : %s", path);
47 cur = xmlDocGetRootElement(doc);
49 MAS_LOGE("[ERROR] Empty document");
54 if (xmlStrcmp(cur->name, (const xmlChar *) MA_TAG_ASSISTANT_BASE)) {
55 MAS_LOGE("[ERROR] The wrong type, root node is NOT %s", MA_TAG_ASSISTANT_BASE);
60 cur = cur->xmlChildrenNode;
62 MAS_LOGE("[ERROR] Empty document");
67 /* alloc assistant info */
68 ma_assistant_info_s* temp;
69 temp = (ma_assistant_info_s*)calloc(1, sizeof(ma_assistant_info_s));
71 MAS_LOGE("[ERROR] Fail to allocate memory");
78 temp->icon_path = NULL;
79 memset(temp->wakeup_list, 0x00, sizeof(temp->wakeup_list));
80 memset(temp->wakeup_language, 0x00, sizeof(temp->wakeup_language));
82 memset(temp->supported_lang, 0x00, sizeof(temp->supported_lang));
84 temp->wakeup_engine = NULL;
85 temp->custom_ui_option = false;
88 if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar *)MA_TAG_ASSISTANT_LANGUAGE_SET)) {
89 xmlNodePtr child_node = cur->xmlChildrenNode;
90 while (child_node != NULL) {
91 if (child_node->name && 0 == xmlStrcmp(child_node->name, (const xmlChar*)MA_TAG_ASSISTANT_LANGUAGE)) {
92 key = xmlNodeGetContent(child_node);
94 temp->supported_lang[temp->cnt_lang++] = strdup((const char*)key);
95 MAS_LOGD("Language : %s", key);
100 child_node = child_node->next;
102 } else if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar *)MA_TAG_ASSISTANT_WAKEUP_WORD_SET)) {
103 xmlNodePtr child_node = cur->xmlChildrenNode;
104 while (child_node != NULL) {
105 if (child_node->name && 0 == xmlStrcmp(child_node->name, (const xmlChar*)MA_TAG_ASSISTANT_WAKEUP_WORD)) {
106 key = xmlNodeGetContent(child_node);
108 temp->wakeup_list[temp->cnt_wakeup] = strdup((const char*)key);
109 MAS_LOGD("Wakeup Word : %s", key);
112 xmlChar* prop = xmlNodeGetLang(child_node);
114 temp->wakeup_language[temp->cnt_wakeup] = strdup((const char*)prop);
115 MAS_LOGD("Wakeup Language for %s : %s", temp->wakeup_list[temp->cnt_wakeup], prop);
121 child_node = child_node->next;
123 } else if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar*)MA_TAG_ASSISTANT_NAME)) {
124 key = xmlNodeGetContent(cur);
126 temp->name = strdup((const char*)key);
127 MAS_LOGD("Name : %s", key);
130 } else if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar*)MA_TAG_ASSISTANT_APPID)) {
131 key = xmlNodeGetContent(cur);
133 temp->app_id = strdup((const char*)key);
134 MAS_LOGD("ID : %s", key);
137 } else if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar*)MA_TAG_ASSISTANT_ICON_PATH)) {
138 key = xmlNodeGetContent(cur);
140 temp->icon_path = strdup((const char*)key);
141 MAS_LOGD("Icon Path : %s", key);
144 } else if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar*)MA_TAG_ASSISTANT_WAKEUP_ENGINE_APPID)) {
145 key = xmlNodeGetContent(cur);
147 temp->wakeup_engine = strdup((const char*)key);
148 MAS_LOGD("Wakeup Engine : %s", key);
151 } else if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar*)MA_TAG_ASSISTANT_CUSTOM_UI)) {
152 key = xmlNodeGetContent(cur);
154 if (0 == xmlStrcasecmp(key, "true")) {
155 temp->custom_ui_option = true;
157 MAS_LOGD("Use custom UI : %d", temp->custom_ui_option);
166 callback(temp->app_id, temp->name, temp->icon_path,
167 temp->wakeup_list, temp->wakeup_language, temp->cnt_wakeup,
168 temp->supported_lang, temp->cnt_lang,
169 temp->wakeup_engine, temp->custom_ui_option, user_data);
173 free((void*)temp->app_id);
176 free((void*)temp->name);
178 if (temp->icon_path) {
179 free((void*)temp->icon_path);
181 for (loop = 0; loop < temp->cnt_wakeup; loop++) {
182 if (temp->wakeup_list[loop]) {
183 free((void*)(temp->wakeup_list[loop]));
186 for (loop = 0; loop < temp->cnt_lang; loop++) {
187 if (temp->supported_lang[loop]) {
188 free((void*)(temp->supported_lang[loop]));
191 if (temp->wakeup_engine) {
192 free((void*)temp->wakeup_engine);
201 int mas_config_get_assistant_info(mas_config_assistant_info_cb callback, void* user_data)
203 const char *suffix = ".xml";
207 d = opendir(MA_ASSISTANT_INFO);
210 while (NULL != (dir = readdir(d))) {
211 if (suffix && strlen(suffix) <= strlen(dir->d_name)) {
212 if (0 == strcmp(dir->d_name + strlen(dir->d_name) - strlen(suffix), suffix)) {
213 char fullpath[_POSIX_PATH_MAX];
214 snprintf(fullpath, _POSIX_PATH_MAX - 1, "%s/%s", MA_ASSISTANT_INFO, dir->d_name);
215 MAS_LOGD("Parsing file : %s\n", fullpath);
216 mas_config_parse_assistant_info(callback, fullpath, user_data);