#include <gum/gum-user-service.h>
#include <gum/common/gum-user-types.h>
+#include "ma_defs.h"
+
/* Define EXPORT_API */
#ifndef EXPORT_API
#define EXPORT_API __attribute__((visibility("default")))
#endif
#define LOG_TAG "ma-assistant-parser"
-#define MA_TAG_ASSISTANT_BASE "multi-assistant"
-#define MA_TAG_ASSISTANT_NAME "name"
-#define MA_TAG_ASSISTANT_ID "id"
-#define MA_TAG_ASSISTANT_ICON_PATH "icon-path"
-#define MA_TAG_ASSISTANT_LANGUAGE_SET "languages"
-#define MA_TAG_ASSISTANT_LANGUAGE "lang"
-#define MA_TAG_ASSISTANT_WAKEUP_WORD_SET "wakeup-words"
-#define MA_TAG_ASSISTANT_WAKEUP_WORD "wakeup-word"
-
-#define MA_CONFIG_BASE tzplatform_mkpath(TZ_USER_HOME, "share/.multiassistant")
-#define MA_HOME tzplatform_mkpath(TZ_USER_HOME, "share/.multiassistant/ma")
-#define MA_ASSISTANT_BASE tzplatform_mkpath(TZ_USER_HOME, "share/.multiassistant/ma/1.0")
-#define MA_ASSISTANT_INFO tzplatform_mkpath(TZ_USER_HOME, "share/.multiassistant/ma/1.0/assistant-info")
-
#define MA_GLOBAL_CONFIG_BASE "/etc/skel/share/.multiassistant"
#define MA_GLOBAL_HOME "/etc/skel/share/.multiassistant/ma"
#define MA_GLOBAL_ASSISTANT_BASE "/etc/skel/share/.multiassistant/ma/1.0"
* limitations under the License.
*/
+#include <libxml/parser.h>
+#include <sys/types.h>
+#include <dirent.h>
#include "ma_config_mgr.h"
#include "ma_main.h"
return 0;
}
+int ma_config_mgr_parse_assistant_info(ma_ui_assistant_info_cb callback, void* user_data, const char *path)
+{
+ xmlDocPtr doc = NULL;
+ xmlNodePtr cur = NULL;
+ xmlChar *key;
+
+ int loop;
+ int retry_count = 0;
+
+ while (NULL == doc) {
+ doc = xmlParseFile(path);
+ if (NULL != doc) {
+ break;
+ }
+
+ if (MA_RETRY_COUNT == retry_count++) {
+ SLOG(LOG_ERROR, TAG_MACONFIG, "[ERROR] Fail to parse file error : %s", path);
+ xmlCleanupParser();
+ return -1;
+ }
+ usleep(10000);
+ }
+
+ cur = xmlDocGetRootElement(doc);
+ if (cur == NULL) {
+ SLOG(LOG_ERROR, TAG_MACONFIG, "[ERROR] Empty document");
+ xmlFreeDoc(doc);
+ return -1;
+ }
+
+ if (xmlStrcmp(cur->name, (const xmlChar *) MA_TAG_ASSISTANT_BASE)) {
+ SLOG(LOG_ERROR, TAG_MACONFIG, "[ERROR] The wrong type, root node is NOT %s", MA_TAG_ASSISTANT_BASE);
+ xmlFreeDoc(doc);
+ return -1;
+ }
+
+ cur = cur->xmlChildrenNode;
+ if (cur == NULL) {
+ SLOG(LOG_ERROR, TAG_MACONFIG, "[ERROR] Empty document");
+ xmlFreeDoc(doc);
+ return -1;
+ }
+
+ /* alloc assistant info */
+ ma_assistant_info_s* temp;
+ temp = (ma_assistant_info_s*)calloc(1, sizeof(ma_assistant_info_s));
+ if (NULL == temp) {
+ SLOG(LOG_ERROR, TAG_MACONFIG, "[ERROR] Fail to allocate memory");
+ xmlFreeDoc(doc);
+ return MA_ERROR_OUT_OF_MEMORY;
+ }
+
+ temp->app_id = NULL;
+ temp->name = NULL;
+ temp->icon_path = NULL;
+ memset(temp->wakeup_list, 0x00, sizeof(temp->wakeup_list));
+ temp->cnt_wakeup = 0;
+ memset(temp->supported_lang, 0x00, sizeof(temp->supported_lang));
+ temp->cnt_lang = 0;
+
+ while (cur != NULL) {
+ if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar *)MA_TAG_ASSISTANT_LANGUAGE_SET)) {
+ xmlNodePtr child_node = cur->xmlChildrenNode;
+ while (child_node != NULL) {
+ if (child_node->name && 0 == xmlStrcmp(child_node->name, (const xmlChar*)MA_TAG_ASSISTANT_LANGUAGE)) {
+ key = xmlNodeGetContent(child_node);
+ if (key) {
+ temp->supported_lang[temp->cnt_lang++] = strdup((const char*)key);
+ SLOG(LOG_DEBUG, TAG_MACONFIG, "Language : %s", key);
+ xmlFree(key);
+ }
+ }
+
+ child_node = child_node->next;
+ }
+ } else if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar *)MA_TAG_ASSISTANT_WAKEUP_WORD_SET)) {
+ xmlNodePtr child_node = cur->xmlChildrenNode;
+ while (child_node != NULL) {
+ if (child_node->name && 0 == xmlStrcmp(child_node->name, (const xmlChar*)MA_TAG_ASSISTANT_WAKEUP_WORD)) {
+ key = xmlNodeGetContent(child_node);
+ if (key) {
+ temp->wakeup_list[temp->cnt_wakeup++] = strdup((const char*)key);
+ SLOG(LOG_DEBUG, TAG_MACONFIG, "Wakeup Word : %s", key);
+ xmlFree(key);
+ }
+ }
+
+ child_node = child_node->next;
+ }
+ } else if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar*)MA_TAG_ASSISTANT_NAME)) {
+ key = xmlNodeGetContent(cur);
+ if (key) {
+ temp->name = strdup((const char*)key);
+ SLOG(LOG_DEBUG, TAG_MACONFIG, "Name : %s", key);
+ xmlFree(key);
+ }
+ } else if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar*)MA_TAG_ASSISTANT_ID)) {
+ key = xmlNodeGetContent(cur);
+ if (key) {
+ temp->app_id = strdup((const char*)key);
+ SLOG(LOG_DEBUG, TAG_MACONFIG, "ID : %s", key);
+ xmlFree(key);
+ }
+ } else if (cur->name && 0 == xmlStrcmp(cur->name, (const xmlChar*)MA_TAG_ASSISTANT_ICON_PATH)) {
+ key = xmlNodeGetContent(cur);
+ if (key) {
+ temp->icon_path = strdup((const char*)key);
+ SLOG(LOG_DEBUG, TAG_MACONFIG, "Icon Path : %s", key);
+ xmlFree(key);
+ }
+ }
+
+ cur = cur->next;
+ }
+
+ if (callback) {
+ callback(temp->app_id, temp->name, temp->icon_path,
+ temp->wakeup_list, temp->cnt_wakeup, temp->supported_lang, temp->cnt_lang, user_data);
+ }
+
+ if (temp->app_id) {
+ free((void*)temp->app_id);
+ }
+ if (temp->name) {
+ free((void*)temp->name);
+ }
+ if (temp->icon_path) {
+ free((void*)temp->icon_path);
+ }
+ for (loop = 0; loop < temp->cnt_wakeup; loop++) {
+ if (temp->wakeup_list[loop]) {
+ free((void*)(temp->wakeup_list[loop]));
+ }
+ }
+ for (loop = 0; loop < temp->cnt_lang; loop++) {
+ if (temp->supported_lang[loop]) {
+ free((void*)(temp->supported_lang[loop]));
+ }
+ }
+
+ free(temp);
+ xmlFreeDoc(doc);
+
+ return 0;
+}
+
int ma_config_mgr_get_assistant_info(ma_ui_assistant_info_cb callback, void* user_data)
{
+ const char *suffix = ".xml";
+
+ DIR *d;
+ struct dirent *dir;
+ d = opendir(MA_ASSISTANT_INFO);
+
+ if (d) {
+ while (NULL != (dir = readdir(d))) {
+ if (suffix && dir->d_name && strlen(suffix) <= strlen(dir->d_name)) {
+ if (0 == strcmp(dir->d_name + strlen(dir->d_name) - strlen(suffix), suffix)) {
+ char fullpath[_POSIX_PATH_MAX];
+ snprintf(fullpath, _POSIX_PATH_MAX - 1, "%s/%s", MA_ASSISTANT_INFO, dir->d_name);
+ SLOG(LOG_DEBUG, TAG_MAUI, "Parsing file : %s\n", fullpath);
+ ma_config_mgr_parse_assistant_info(callback, user_data, fullpath);
+ }
+ }
+ }
+ closedir(d);
+ }
+
return 0;
}
#define MA_BASE tzplatform_mkpath(tzplatform_getid("TZ_USER_HOME"), "share/.multiassistant");
#define MA_CONFIG tzplatform_mkpath(tzplatform_getid("TZ_USER_HOME"), "share/.multiassistant/ma-config.xml")
+#define MA_CONFIG_BASE tzplatform_mkpath(TZ_USER_HOME, "share/.multiassistant")
+#define MA_HOME tzplatform_mkpath(TZ_USER_HOME, "share/.multiassistant/ma")
+#define MA_ASSISTANT_BASE tzplatform_mkpath(TZ_USER_HOME, "share/.multiassistant/ma/1.0")
+#define MA_ASSISTANT_INFO tzplatform_mkpath(TZ_USER_HOME, "share/.multiassistant/ma/1.0/assistant-info")
+
/**************************************************************************************
*** Definitions for DBus
*************************************************************************************/
#define MAS_UI_METHOD_SEND_RESULT "mas_ui_method_send_result"
#define MAS_UI_METHOD_ERROR "mas_ui_method_error"
+/**************************************************************************************
+ *** Definitions for xml file
+ *************************************************************************************/
+#define MA_TAG_ASSISTANT_BASE "multi-assistant"
+#define MA_TAG_ASSISTANT_NAME "name"
+#define MA_TAG_ASSISTANT_ID "id"
+#define MA_TAG_ASSISTANT_ICON_PATH "icon-path"
+#define MA_TAG_ASSISTANT_LANGUAGE_SET "languages"
+#define MA_TAG_ASSISTANT_LANGUAGE "lang"
+#define MA_TAG_ASSISTANT_WAKEUP_WORD_SET "wakeup-words"
+#define MA_TAG_ASSISTANT_WAKEUP_WORD "wakeup-word"
/**************************************************************************************
*** Definitions for ETC