Support language runtime property for boolean feature 44/133344/6 submit/tizen/20170703.030310
authorKichan Kwon <k_c.kwon@samsung.com>
Fri, 9 Jun 2017 08:03:20 +0000 (17:03 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Mon, 3 Jul 2017 02:57:37 +0000 (11:57 +0900)
- Some keys are needed to have different value depend on runtime
- To support different value, model-config will have runtime property
  - e.g. < ... capi="on" webapi="off" csapi="on">VALUE</key>

- init_db stores the value for all runtimes with default value
- Before system-info reads key, it check RUNTIME_TYPE
  - If RUNTIME_TYPE is invalid or NULL, return default value

Change-Id: I247245270ddf58ae2da5254a23b0fa2188e77899
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
include/system_info_private.h
src/init_db/system_info_db_init.c
src/system_info.c

index 05cef6a..53ea717 100644 (file)
@@ -45,6 +45,36 @@ extern "C"
 #define DBL_TYPE       "double"
 #define STR_TYPE       "string"
 
+enum language {
+       DEFAULT  = 0, /* Without language property */
+       C        = 1, /* C and C++ */
+       WEB      = 2, /* Web (JS, html) */
+       DOTNET   = 3, /* C# */
+       LANG_MAX = 8
+};
+
+struct runtime {
+       enum language lang; /* Lauguage used by runtime */
+       char *xml_prop;     /* Property name in model-config */
+       char *runtime_type; /* Value of RUNTIME_TYPE env */
+};
+
+/**
+ * Notice
+ *
+ * In current, each runtime_type has their own prefix with length = RT_PREFIX.
+ * So, system-info check only first RT_PREFIX characters for performance.
+ * If you add another language with having same prefix, you should change this logic.
+ */
+#define RT_PREFIX 2
+static const struct runtime runtime[LANG_MAX] = {
+       { C,        "capi",   "capp"   },
+       { C,        "capi",   "c++app" },
+       { WEB,      "webapi", "webapp" },
+       { DOTNET,   "csapi",  "dotnet" },
+       { LANG_MAX,  NULL,     NULL    }
+};
+
 int system_info_get_file(const char *key, char *value, size_t len);
 int system_info_get_type_file(const char *key, system_info_type_e *type);
 
index 5fd5b0d..98245c9 100644 (file)
@@ -51,7 +51,9 @@
 #define MODEL_CONFIG_TAG "model-config"
 #define KEY_PREFIX "http://"
 
-static int db_set_value(GDBM_FILE *db, char *tag, char *name, char *type, char *value)
+extern const struct runtime runtime[LANG_MAX];
+
+static int db_set_value(GDBM_FILE *db, char *tag, char *name, char *type, char *value, int val_len)
 {
        char key[KEY_MAX];
        datum d_key;
@@ -70,7 +72,7 @@ static int db_set_value(GDBM_FILE *db, char *tag, char *name, char *type, char *
        d_key.dsize = strlen(key) + 1;
 
        d_data.dptr = value;
-       d_data.dsize = strlen(value) + 1;
+       d_data.dsize = val_len + 1;
 
        ret = gdbm_store(*db, d_key, d_data, GDBM_REPLACE);
        if (ret != 0) {
@@ -85,6 +87,34 @@ static int db_set_value(GDBM_FILE *db, char *tag, char *name, char *type, char *
        return 0;
 }
 
+static int db_set_value_foreach_runtime(GDBM_FILE *db, xmlNode *node,
+                                               char *tag, char *name, char *type, char *value)
+{
+       int rt;
+       xmlChar *prop_val;
+       char value_intg[LANG_MAX];
+       int ret;
+
+       memset(value_intg, strncmp(value, "true", 4) ? false : true, sizeof(value_intg));
+
+       for (rt = 0; rt < LANG_MAX; rt++) {
+               if (!runtime[rt].xml_prop)
+                       break;
+
+               prop_val = xmlGetProp(node, (xmlChar *)(runtime[rt].xml_prop));
+               if (!prop_val)
+                       continue;
+
+               value_intg[runtime[rt].lang] = xmlStrcmp(prop_val, (xmlChar *)"on") ? false : true;
+
+               xmlFree(prop_val);
+       }
+
+       ret = db_set_value(db, tag, name, type, value_intg, sizeof(value_intg));
+
+       return ret;
+}
+
 static int system_info_get_values_config_xml(GDBM_FILE *db)
 {
        xmlDocPtr doc;
@@ -150,7 +180,11 @@ static int system_info_get_values_config_xml(GDBM_FILE *db)
                                continue;
                        }
 
-                       ret = db_set_value(db, tag, name, type, value);
+                       if (!strncmp(type, "bool", 4))
+                               ret = db_set_value_foreach_runtime(db, cur_node, tag, name, type, value);
+                       else
+                               ret = db_set_value(db, tag, name, type, value, strlen(value));
+
                        if (ret < 0)
                                _E("Failed to set value (%d)", ret);
 
@@ -204,7 +238,7 @@ static int system_info_get_values_ini(GDBM_FILE *db)
                        continue;
                }
 
-               ret = db_set_value(db, TAG_TYPE_PLATFORM_STR, ini_keys[i].key, STR_TYPE, value);
+               ret = db_set_value(db, TAG_TYPE_PLATFORM_STR, ini_keys[i].key, STR_TYPE, value, strlen(value));
                if (ret < 0)
                        _E("Failed to set value (%d)", ret);
        }
@@ -244,7 +278,8 @@ static void show_help(void)
        printf("  -h --help         Show this help\n");
        printf("  -k --key=KEY      System info key to update\n");
        printf("  -t --type=TYPE    System info type to update (int/bool/double/string)\n");
-       printf("  -g --tag=TAGE     System info tag to update (platform/custom)\n");
+       printf("  -g --tag=TAG      System info tag to update (platform/custom)\n");
+       printf("  -l --lang=LANG    System info value to update (capi/webapi/csapi\n");
        printf("  -v --value=VALUE  System info value to update\n");
 }
 
@@ -254,21 +289,24 @@ static int system_info_update_db(int argc, char *argv[])
        GDBM_FILE db;
        int opt;
        bool failed = false;
-       char key[KEY_MAX] = { 0, };
-       char type[KEY_MAX] = { 0, };
-       char tag[KEY_MAX] = { 0, };
-       char value[KEY_MAX] = { 0, };
+       char key[KEY_MAX] = {0};
+       char type[KEY_MAX] = {0};
+       char tag[KEY_MAX] = {0};
+       char value[KEY_MAX] = {0};
+       char lang[KEY_MAX] = {0};
+
        struct option long_options[] = {
                { "key",   required_argument, 0, 0 },
                { "type",  required_argument, 0, 0 },
                { "tag",   required_argument, 0, 0 },
                { "value", required_argument, 0, 0 },
+               { "lang",  required_argument, 0, 0 },
                { "help",  no_argument,       0, 0 },
                { 0,       0,                 0, 0 },
        };
 
        while (1) {
-               opt = getopt_long(argc, argv, "k:t:g:v:h",
+               opt = getopt_long(argc, argv, "k:t:g:v:l:h",
                                long_options, NULL);
                if (opt < 0)
                        break;
@@ -285,6 +323,9 @@ static int system_info_update_db(int argc, char *argv[])
                case 'v':
                        snprintf(value, sizeof(value), "%s", optarg);
                        break;
+               case 'l':
+                       snprintf(lang, sizeof(lang), "%s", optarg);
+                       break;
                case 'h':
                default:
                        show_help();
@@ -326,7 +367,7 @@ static int system_info_update_db(int argc, char *argv[])
                return -ENOENT;
        }
 
-       ret = db_set_value(&db, tag, key, type, value);
+       ret = db_set_value(&db, tag, key, type, value, strlen(value));
        if (ret != 0)
                _E("Failed to set value (%d)", ret);
 
index 0db044e..487aa28 100644 (file)
@@ -38,6 +38,8 @@
 
 #define GDBM_CACHE_SIZE 10 /* GDBM default == 100 */
 
+extern const struct runtime runtime[LANG_MAX];
+
 GHashTable *hashtable = NULL;
 static pthread_mutex_t fmutex = PTHREAD_MUTEX_INITIALIZER;
 
@@ -100,11 +102,9 @@ static int db_get_value(enum tag_type tag, const char *key,
        }
 
        if (strstr(key, KEY_PREFIX) == key)
-               snprintf(key_internal, sizeof(key_internal),
-                               "%s:%s:%s", key, type, tag_s);
+               snprintf(key_internal, sizeof(key_internal), "%s:%s:%s", key, type, tag_s);
        else
-               snprintf(key_internal, sizeof(key_internal),
-                               "%s%s:%s:%s", KEY_PREFIX, key, type, tag_s);
+               snprintf(key_internal, sizeof(key_internal), "%s%s:%s:%s", KEY_PREFIX, key, type, tag_s);
 
        pthread_mutex_lock(&fmutex);
        if (!hashtable) {
@@ -258,35 +258,50 @@ static int system_info_get_bool(enum tag_type tag, const char *key, bool *value)
        char val[8];
        char *valp;
        size_t len;
+       const char *runtime_type;
+       int rt;
 
        if (!key || !value)
                return SYSTEM_INFO_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
 
        ret = db_get_value(tag, key, BOOL_TYPE, val, sizeof(val));
-       if (ret == SYSTEM_INFO_ERROR_NONE)
-               goto out;
+       if (ret == SYSTEM_INFO_ERROR_NONE) {
+               runtime_type = getenv("RUNTIME_TYPE");
+               if (runtime_type) {
+                       for (rt = 0; rt < LANG_MAX; rt++) {
+                               if (!runtime[rt].runtime_type)
+                                       break;
+                               if (!strncmp(runtime_type, runtime[rt].runtime_type, RT_PREFIX)) {
+                                       *value = val[runtime[rt].lang];
+                                       return SYSTEM_INFO_ERROR_NONE;
+                               }
+                       }
+                       _I("Unknown RUNTIME_TYPE, return default value");
+               }
+
+               *value = val[runtime[DEFAULT].lang];
+               return SYSTEM_INFO_ERROR_NONE;
+       }
 
        if (tag == TAG_TYPE_CUSTOM) {
                ret = external_get_value(TAG_TYPE_CUSTOM_STR, key, BOOL_TYPE, &valp);
                if (ret == SYSTEM_INFO_ERROR_NONE) {
                        snprintf(val, sizeof(val), "%s", valp); //LCOV_EXCL_LINE
                        free(valp); //LCOV_EXCL_LINE
-                       goto out; //LCOV_EXCL_LINE
+
+                       len = strlen(val) + 0;
+                       if (!strncmp(val, "true", len) || !strncmp(val, "TRUE", len))
+                               *value = true;
+                       else
+                               *value = false;
+
+                       return SYSTEM_INFO_ERROR_NONE;
                }
        }
 
        _E("Invalid key (%s), type:bool", key);
 
        return SYSTEM_INFO_ERROR_INVALID_PARAMETER;
-
-out:
-       len = strlen(val) + 1;
-       if (!strncmp(val, "true", len) || !strncmp(val, "TRUE", len))
-               *value = true;
-       else
-               *value = false;
-
-       return SYSTEM_INFO_ERROR_NONE;
 }
 
 static int system_info_get_int(enum tag_type tag, const char *key, int *value)