init_db: Apply system-info-generated enum type when constructing db 62/307162/5
authorYoungjae Cho <y0.cho@samsung.com>
Tue, 5 Mar 2024 08:08:56 +0000 (17:08 +0900)
committerYoungjae Cho <y0.cho@samsung.com>
Wed, 6 Mar 2024 10:31:34 +0000 (19:31 +0900)
To convert enumerator into integer, it is necessary to have mapping
table. The mapping table is generated dynamically by SystemInfoGenerator
with option --output-util-source, --output-util-header.

For example, a model-config.xml might have declared an enum like below.
 | <enum typename="test_enum_e">
 |  <enumerator value="10">TEST_ENUM_0</enumerator>
 |  <enumerator>TEST_ENUM_1</enumerator>
 |  <enumerator>TEST_ENUM_2</enumerator>
 |  <enumerator>TEST_ENUM_3</enumerator>
 |  <enumerator>TEST_ENUM_4</enumerator>
 | </enum>

From SystemInfoGenerator with option --output-util-source, it generates
function below that contains mapping of all declared enumerator symbol
to an integer value.
 | int system_info_util_get_enumerator_value(const char *symbol, int *value);

With help of the mapping function, one can get integer value of enumerator.
 | int value;
 | system_info_util_get_enumerator_value("TEST_ENUM_3", &value); // value = 13

The system-info internally treats enum type same as integer type.

Change-Id: I6fea66879ec205513830630491a10902aa6c8ee1
Signed-off-by: Youngjae Cho <y0.cho@samsung.com>
src/init_db/CMakeLists.txt
src/init_db/system_info_db_init.c

index 21e0801ef129eee9221698596507b066d579b202..d939d05cf3ce1239d029e3d3f611d990290bbec8 100755 (executable)
@@ -1,7 +1,7 @@
 CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
 SET(INIT_DB "system_info_init_db")
 
-SET(SRCS ${CMAKE_SOURCE_DIR}/src/init_db/system_info_db_init.c)
+SET(SRCS system_info_db_init.c system_info_generated_util.c)
 
 INCLUDE_DIRECTORIES(include)
 
@@ -23,6 +23,15 @@ SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
 
 SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie")
 
+ADD_CUSTOM_TARGET(SYSTEM_INFO_GENERATED_UTIL ALL
+       WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
+       BYPRODUCTS system_info_generated_util.c system_info_generated_util.h
+       COMMAND ${Python3_EXECUTABLE} -m SystemInfoGenerator
+               --input ${MODEL_CONFIG_RO_PATH}
+               --output-util-source=src/init_db/system_info_generated_util.c
+               --output-util-header=src/init_db/system_info_generated_util.h
+       VERBATIM)
+
 ADD_EXECUTABLE(${INIT_DB} ${SRCS})
 TARGET_LINK_LIBRARIES(${INIT_DB} ${init_db_pkgs_LDFLAGS})
 
index 94b9715c5803af6551fd787e98b42c9ce8333282..1c2715349a5ac947819cc1a53e976f617b171151 100644 (file)
@@ -18,6 +18,7 @@
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #include <string.h>
 #include <unistd.h>
 #include <ctype.h>
@@ -33,6 +34,7 @@
 #include <libxml/tree.h>
 
 #include "system_info_private.h"
+#include "system_info_generated_util.h" /* auto-generated by SystemInfoGenerator */
 
 #ifdef LOG_TAG
 #undef LOG_TAG
 //LCOV_EXCL_START
 extern const struct runtime runtime[LANG_MAX];
 
-static int db_set_value(const char *db_path, char *tag, char *name, char *type, char *value, int val_len)
+static bool is_enum_type(const char *type)
+{
+       if (!type)
+               return false;
+
+       return (strncmp(type, "bool", sizeof("bool")) != 0
+               && strncmp(type, "int", sizeof("int")) != 0
+               && strncmp(type, "double", sizeof("double")) != 0
+               && strncmp(type, "string", sizeof("string")) != 0);
+}
+
+static int db_set_value(const char *db_path,
+       const char *tag, const char *name, const char *type, const char *value, int val_len)
 {
        int ret;
        char key_internal[KEY_MAX];
@@ -65,6 +79,7 @@ static int db_set_value(const char *db_path, char *tag, char *name, char *type,
        FILE *fp = NULL;
        FILE *fp_new = NULL;
        int key_idx;
+       char enumerator_value[32] = { 0 , };
 
        if (!db_path || !tag || !name || !type || !value)
                return -EINVAL;
@@ -72,6 +87,26 @@ static int db_set_value(const char *db_path, char *tag, char *name, char *type,
        if (db_path[0] == '\0')
                db_path = SYSTEM_INFO_DB_RO_PATH;
 
+       if (is_enum_type(type)) {
+               int __enumerator_value;
+
+               ret = system_info_util_get_enumerator_value(value, &__enumerator_value);
+               if (ret < 0) {
+                       _E("cannot find enumerator %s", value);
+                       return ret;
+               }
+
+               ret = snprintf(enumerator_value, sizeof(enumerator_value) - 1, "%d", __enumerator_value);
+               if (ret >= sizeof(enumerator_value) - 1) {
+                       _E("too long digit for enumerator value");
+                       return -EOVERFLOW;
+               }
+
+               /* Internally, treat enum as an integer */
+               type = "int";
+               value = enumerator_value;
+       }
+
        if (name == strstr(name, KEY_PREFIX))
                snprintf(key_internal, sizeof(key_internal), "%s:%s:%s", tag, name + strlen(KEY_PREFIX), type);
        else