system-info: add function to return types of the keys 95/58695/4 accepted/tizen/common/20160301.120614 accepted/tizen/ivi/20160222.081733 accepted/tizen/mobile/20160222.081544 accepted/tizen/tv/20160222.081618 accepted/tizen/wearable/20160222.081659 submit/tizen/20160222.060529 submit/tizen_common/20160229.190608
authorTaeyoung Kim <ty317.kim@samsung.com>
Wed, 3 Feb 2016 02:31:34 +0000 (11:31 +0900)
committerTaeyoung Kim <ty317.kim@samsung.com>
Mon, 22 Feb 2016 06:03:18 +0000 (15:03 +0900)
- The function system_info_get_platform_type() returns
  the type of the system keys. The api is just for
  internal use currently.

Change-Id: I812aae1f40da26bffde8525953b965e6cdf4a2ff
Signed-off-by: Taeyoung Kim <ty317.kim@samsung.com>
include/system_info.h
include/system_info_private.h
src/system_info.c
src/system_info_file.c
src/system_info_parse.c

index 2a5b657..e68bcc5 100644 (file)
@@ -172,6 +172,29 @@ int system_info_get_value_double(system_info_key_e key, double *value);
 int system_info_get_value_string(system_info_key_e key, char **value);
 
 /**
+ * @internal
+ * @brief It is not decided if it should be opened to public
+ */
+typedef enum {
+       SYSTEM_INFO_BOOL,
+       SYSTEM_INFO_INT,
+       SYSTEM_INFO_DOUBLE,
+       SYSTEM_INFO_STRING,
+} system_info_type_e;
+
+/**
+ * @internal
+ * @brief It is not decided if it should be opened to public
+ */
+int system_info_get_platform_type(const char *key, system_info_type_e *type);
+
+/**
+ * @internal
+ * @brief It is not decided if it should be opened to public
+ */
+int system_info_get_custom_type(const char *key, system_info_type_e *type);
+
+/**
  * @}
  */
 
index 13e3433..db614c1 100644 (file)
@@ -53,9 +53,11 @@ typedef enum {
 
 int system_info_ini_get_string(char *ini_file, char *key, char **output);
 int system_info_get_value_from_config_xml(char *feature_tag, const char *name_field, char *type_field, char **value);
-
+int system_info_get_type_from_config_xml(const char *feature_tag,
+               const char *name_field, char *type_field, size_t len);
 
 int system_info_get_file(const char *key, void **value);
+int system_info_get_type_file(const char *key);
 
 int external_get_value(const char *tag, const char *key, const char *type, char **value);
 
index 33887d2..2cc56d3 100644 (file)
@@ -288,3 +288,74 @@ API int system_info_get_custom_string(const char *key, char **value)
 
        return SYSTEM_INFO_ERROR_NONE;
 }
+
+static int get_type_from_str(char *type)
+{
+       size_t len;
+
+       if (!type)
+               return SYSTEM_INFO_ERROR_INVALID_PARAMETER;
+
+       len = strlen(type) + 1;
+       if (!strncmp(BOOL_TYPE, type, len))
+               return SYSTEM_INFO_BOOL;
+       if (!strncmp(INT_TYPE, type, len))
+               return SYSTEM_INFO_INT;
+       if (!strncmp(DBL_TYPE, type, len))
+               return SYSTEM_INFO_DOUBLE;
+       if (!strncmp(STR_TYPE, type, len))
+               return SYSTEM_INFO_STRING;
+
+       return SYSTEM_INFO_ERROR_INVALID_PARAMETER;
+}
+
+static int system_info_get_type(const char *key,
+               const char *tag, system_info_type_e *type)
+{
+       char type_str[16];
+       int ret;
+
+       if (!key || !tag || !type)
+               return SYSTEM_INFO_ERROR_INVALID_PARAMETER;
+
+       if (access(CONFIG_FILE_PATH, R_OK)) {
+               _E("cannot find file %s!!!", CONFIG_FILE_PATH);
+               if (errno == EPERM || errno == EACCES)
+                       return SYSTEM_INFO_ERROR_PERMISSION_DENIED;
+               return SYSTEM_INFO_ERROR_IO_ERROR;
+       }
+
+       ret = system_info_get_type_from_config_xml(tag,
+                       key, type_str, sizeof(type_str));
+       if (ret != SYSTEM_INFO_ERROR_NONE) {
+               if (!strncmp(tag, PLATFORM_TAG, sizeof(PLATFORM_TAG))) {
+                       ret = system_info_get_type_file(key);
+                       if (ret >= 0)
+                               goto out;
+               }
+               _E("Failed to get type of key (%s)", key);
+               return ret;
+       }
+
+       ret = get_type_from_str(type_str);
+       if (ret < 0) {
+               _E("Invalid type (key:%s, type:%s)", key, type_str);
+               return SYSTEM_INFO_ERROR_INVALID_PARAMETER;
+       }
+
+out:
+       *type = ret;
+
+       return SYSTEM_INFO_ERROR_NONE;
+}
+
+
+API int system_info_get_platform_type(const char *key, system_info_type_e *type)
+{
+       return system_info_get_type(key, PLATFORM_TAG, type);
+}
+
+API int system_info_get_custom_type(const char *key, system_info_type_e *type)
+{
+       return system_info_get_type(key, CUSTOM_TAG, type);
+}
index a58e55b..b593869 100644 (file)
@@ -28,6 +28,8 @@
 #define SERIAL_TOK_DELIMITER ","
 #define BUF_MAX 256
 
+#define ARRAY_SIZE(name) (sizeof(name)/sizeof(name[0]))
+
 static int get_tizenid(char **value)
 {
        char id[BUF_MAX];
@@ -57,9 +59,36 @@ static int get_tizenid(char **value)
        return 0;
 }
 
+static int get_build_date(char **value)
+{
+       return system_info_ini_get_string(INFO_FILE_PATH, "build:date", value);
+}
+
+static int get_build_str(char **value)
+{
+       return system_info_ini_get_string(INFO_FILE_PATH, "version:build", value);
+}
+
+static int get_build_time(char **value)
+{
+       return system_info_ini_get_string(INFO_FILE_PATH, "build:time", value);
+}
+
+struct system_info_file_key {
+       const char *key;
+       int (*get_value)(char **val);
+       system_info_type_e type;
+} info_file_key [] = {
+       { "tizen.org/system/tizenid",      get_tizenid,    SYSTEM_INFO_STRING },
+       { "tizen.org/system/build.date",   get_build_date, SYSTEM_INFO_STRING },
+       { "tizen.org/system/build.string", get_build_str,  SYSTEM_INFO_STRING },
+       { "tizen.org/system/build.time",   get_build_time, SYSTEM_INFO_STRING },
+};
+
 int system_info_get_file(const char *key, void **value)
 {
        char *p_key;
+       int i, len;
 
        if (!key || !value)
                return -EINVAL;
@@ -70,17 +99,33 @@ int system_info_get_file(const char *key, void **value)
        else
                p_key = (char *)key;
 
-       if (!strncmp(p_key, "tizen.org/system/tizenid", strlen(p_key)))
-               return get_tizenid((char **)value);
+       len = strlen(p_key) + 1;
+       for (i = 0 ; i < ARRAY_SIZE(info_file_key); i++)
+               if (!strncmp(p_key, info_file_key[i].key, len))
+                       return info_file_key[i].get_value((char **)value);
+
+       return -ENOENT;
+}
+
+int system_info_get_type_file(const char *key)
+{
+       char *p_key;
+       int i, len;
 
-       if (!strncmp(p_key, "tizen.org/system/build.date", strlen(p_key)))
-               return system_info_ini_get_string(INFO_FILE_PATH, "build:date", (char **)value);
+       if (!key)
+               return SYSTEM_INFO_ERROR_INVALID_PARAMETER;
 
-       if (!strncmp(p_key, "tizen.org/system/build.string", strlen(p_key)))
-               return system_info_ini_get_string(INFO_FILE_PATH, "version:build", (char **)value);
+       p_key = strstr(key, "http://");
+       if (p_key && p_key == key)
+               p_key = (char *)key + strlen("http://");
+       else
+               p_key = (char *)key;
 
-       if (!strncmp(p_key, "tizen.org/system/build.time", strlen(p_key)))
-               return system_info_ini_get_string(INFO_FILE_PATH, "build:time", (char **)value);
+       len = strlen(p_key) + 1;
+       for (i = 0 ; i < ARRAY_SIZE(info_file_key); i++) {
+               if (!strncmp(p_key, info_file_key[i].key, len))
+                       return info_file_key[i].type;
+       }
 
-       return -ENOENT;
+       return SYSTEM_INFO_ERROR_INVALID_PARAMETER;
 }
index 20c9a06..4eba9ab 100644 (file)
@@ -167,3 +167,86 @@ int system_info_get_value_from_config_xml(char *feature_tag, const char *name_fi
        xmlFreeDoc(doc);
        return SYSTEM_INFO_ERROR_NONE;
 }
+
+int system_info_get_type_from_config_xml(const char *feature_tag,
+               const char *name_field, char *type_field, size_t len)
+{
+       xmlDocPtr doc;
+       xmlNodePtr cur, model_node;
+       xmlNode *cur_node;
+       char *name, *p_name, *type;
+       int ret;
+
+       doc = xmlParseFile(CONFIG_FILE_PATH);
+       if (!doc) {
+               _E("cannot file open %s file!!!", CONFIG_FILE_PATH);
+               return SYSTEM_INFO_ERROR_IO_ERROR;
+       }
+
+       cur = xmlDocGetRootElement(doc);
+       if (!cur) {
+               _E("empty document %s file!!!", CONFIG_FILE_PATH);
+               ret = SYSTEM_INFO_ERROR_IO_ERROR;
+               goto out;
+       }
+
+       for (cur_node = cur; cur_node ; cur_node = cur_node->next) {
+               if (!xmlStrcmp(cur->name, (const xmlChar*)MODEL_CONFIG_TAG))
+                       break;
+       }
+       if (!cur_node) {
+               _E("cannot find %s root element file!!!", MODEL_CONFIG_TAG);
+               ret = SYSTEM_INFO_ERROR_IO_ERROR;
+               goto out;
+       }
+
+       cur = cur->xmlChildrenNode;
+
+       model_node = NULL;
+       for (cur_node = cur; cur_node; cur_node = cur_node->next) {
+               if (!xmlStrcmp(cur_node->name, (const xmlChar*)feature_tag))
+                       model_node = cur_node;
+       }
+       if (!model_node) {
+               _E("cannot find %s field from %s file!!!", name_field, CONFIG_FILE_PATH);
+               ret = SYSTEM_INFO_ERROR_INVALID_PARAMETER;
+               goto out;
+       }
+
+       ret = SYSTEM_INFO_ERROR_INVALID_PARAMETER;
+       cur = model_node->xmlChildrenNode;
+       for (cur_node = cur; cur_node ; cur_node = cur_node->next) {
+               if (cur_node->type != XML_ELEMENT_NODE)
+                       continue;
+
+               name = (char *)xmlGetProp(cur_node, (const xmlChar*)"name");
+
+               p_name = strstr(name_field, "http://");
+               if (p_name && p_name == name_field)
+                       p_name = (char *)name_field + strlen("http://");
+               else
+                       p_name = (char *)name_field;
+
+               ret = strncmp(name, p_name, strlen(name) + 1);
+               xmlFree(name);
+               if (ret != 0)
+                       continue;
+
+               type = (char *)xmlGetProp(cur_node, (const xmlChar*)"type");
+               if (!type) {
+                       _E("Failed to get type of key (%s)", name_field);
+                       ret = SYSTEM_INFO_ERROR_IO_ERROR;
+                       goto out;
+               }
+
+               snprintf(type_field, len, "%s", type);
+               xmlFree(type);
+               ret = SYSTEM_INFO_ERROR_NONE;
+               break;
+       }
+
+out:
+       if (doc)
+               xmlFreeDoc(doc);
+       return ret;
+}