Support database search for HAL system-info 29/260329/5
authorYoungjae Cho <y0.cho@samsung.com>
Wed, 23 Jun 2021 08:39:15 +0000 (17:39 +0900)
committerYoungjae Cho <y0.cho@samsung.com>
Thu, 24 Jun 2021 05:37:35 +0000 (14:37 +0900)
In addition to platform system-info database(/etc/system_info_db),
system-info of HAL(/hal/system_info_db) can be searched for a requested
key.
 If a key exists only in one database, use it.
 If a key exists in both of the databases,
   - if value of key is type of bool, the two value are ANDed.
   - if value of key is type of other than bool, the value of HAL is
     used.

Change-Id: If36a4b7a4143107ec550bb1311dafb470f492167
Signed-off-by: Youngjae Cho <y0.cho@samsung.com>
CMakeLists.txt
packaging/capi-system-info.spec
src/system_info.c

index a33601168120b8b284ca4a271eb4b9529211274e..dd5466ed3602343e938913c8e67bd4d3ea7d721d 100644 (file)
@@ -36,6 +36,7 @@ ADD_DEFINITIONS("-DTIZEN_ID_PATH=\"${TIZEN_ID_PATH}\"")
 ADD_DEFINITIONS("-DLIBPATH=\"${LIB_INSTALL_DIR}\"")
 ADD_DEFINITIONS("-DSYSTEM_INFO_DB_RO_PATH=\"${DB_RO_PATH}\"")
 ADD_DEFINITIONS("-DSYSTEM_INFO_DB_RW_PATH=\"${DB_RW_PATH}\"")
+ADD_DEFINITIONS("-DSYSTEM_INFO_HAL_DB_RO_PATH=\"${HAL_DB_RO_PATH}\"")
 ADD_DEFINITIONS("-DPLUGIN_DIR=\"${PLUGIN_DIR}\"")
 ADD_DEFINITIONS(-D_GNU_SOURCE) # to make scandirat() available
 
index 4b08b80982d5324add85fb691d5d2559f1aeada9..9729006224197533ba0508ea802d455ca6f45f8e 100644 (file)
@@ -52,6 +52,7 @@ cp %{SOURCE1001} .
 
 %define model_config_ro_path %{_sysconfdir}/config/model-config.xml
 %define db_ro_path %{TZ_SYS_RO_ETC}/system_info_db
+%define hal_db_ro_path %{_hal_prefix}/system_info_db
 %define model_config_rw_dir /opt/system/model-config
 %define db_rw_path %{model_config_rw_dir}/system_info_db
 
@@ -67,8 +68,9 @@ MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'`
             -DTIZEN_ID_PATH=%{tizen_id_path} \
             -DDB_RO_PATH=%{db_ro_path} \
             -DDB_RW_PATH=%{db_rw_path} \
+            -DHAL_DB_RO_PATH=%{hal_db_ro_path} \
             -DPLUGIN_DIR=%{plugin_dir} \
-                -DGCOV=%{?gcov:1}%{!?gcov:0}
+            -DGCOV=%{?gcov:1}%{!?gcov:0}
 
 %__make %{?_smp_mflags}
 
index 78b3a504cd37072dcc8ae31684d070f324fdf5ae..5309c548e555553e1314a6020b61ee7ee8b740f6 100644 (file)
@@ -74,14 +74,73 @@ static void destroy_key_value(gpointer data)
 }
 //LCOV_EXCL_STOP
 
+static int db_join_value(const char *value_platform, const char *value_hal, const char *type, char *out, int len)
+{
+       if (!value_platform || !value_hal || !type)
+               return SYSTEM_INFO_ERROR_INVALID_PARAMETER;
+
+       /* If the type is other than bool type, then
+        * the value_hal takes precedence over the value_platform */
+       if (strncmp(type, BOOL_TYPE, sizeof(BOOL_TYPE)) != 0) {
+               strncpy(out, value_hal, len);
+               return SYSTEM_INFO_ERROR_NONE;
+       }
+
+       /* If the type is bool type, then
+        * the result is AND of those two value */
+       if (value_platform[0] == 'F')
+               strncpy(out, value_platform, len);
+       else if (value_hal[0] == 'F')
+               strncpy(out, value_hal, len);
+       else
+               strncpy(out, value_platform, len);
+
+       return SYSTEM_INFO_ERROR_NONE;
+}
+
+static int db_search_value(const char *db_path, char *key_internal, char *value, int len)
+{
+       FILE *fp = NULL;
+       int key_internal_len;
+       char buf[PATH_MAX];     // buffer size should be larger than KEY_MAX
+       size_t value_internal_len;
+       char *temp;
+
+       if (!db_path || !key_internal || !value)
+               return SYSTEM_INFO_ERROR_INVALID_PARAMETER;
+
+       snprintf(buf, sizeof(buf), "%s/%lu", db_path, simple_hash(key_internal));
+
+       fp = fopen(buf, "r");
+       if (!fp) {
+               _E("fopen for %s failed (%d)", buf, errno); //LCOV_EXCL_LINE
+               return SYSTEM_INFO_ERROR_IO_ERROR;
+       }
+
+       key_internal_len = strlen(key_internal);
+       while ((temp = fgets(buf, sizeof(buf), fp))) {
+               if (!strncmp(buf, key_internal, key_internal_len) && buf[key_internal_len] == ' ') {
+                       value_internal_len = strcspn(buf + key_internal_len + 1, "\n") + 1;
+                       snprintf(value, len < value_internal_len ? len : value_internal_len,
+                                       "%s", buf + key_internal_len + 1);
+                       break;
+               }
+       }
+       fclose(fp);
+
+       if (!temp) {
+               _D("Failed to find key=%s in DB=%s", key_internal, db_path);
+               return SYSTEM_INFO_ERROR_IO_ERROR;
+       }
+
+       _D("key=%s in DB=%s, found value=%s", key_internal, db_path, value);
+       return SYSTEM_INFO_ERROR_NONE;
+}
+
 static int db_get_value(enum tag_type tag, const char *key,
                const char *type, char *value, size_t len)
 {
        char key_internal[KEY_MAX];
-       size_t key_internal_len;
-       char buf[PATH_MAX];     // buffer size should be larger than KEY_MAX
-       size_t value_internal_len;
-       FILE *fp = NULL;
        int ret;
        char *tag_s;
        char *temp;
@@ -117,44 +176,47 @@ static int db_get_value(enum tag_type tag, const char *key,
                }
        }
 
-       if (access(SYSTEM_INFO_DB_RW_PATH, R_OK) == 0)
-               snprintf(buf, sizeof(buf), SYSTEM_INFO_DB_RW_PATH"/%lu", simple_hash(key_internal));
-       else
-               snprintf(buf, sizeof(buf), SYSTEM_INFO_DB_RO_PATH"/%lu", simple_hash(key_internal));
-
-       fp = fopen(buf, "r");
-       if (!fp) {
-               if (errno == ENOENT)
-                       _D("Failed to find key in DB (%s, %s)", key, type);
-               else
-                       _E("fopen for %s failed (%d)", buf, errno); //LCOV_EXCL_LINE
-               ret = SYSTEM_INFO_ERROR_IO_ERROR; //LCOV_EXCL_LINE
-               goto out;
-       }
+       if (access(SYSTEM_INFO_DB_RW_PATH, R_OK) == 0) {
+               ret = db_search_value(SYSTEM_INFO_DB_RW_PATH, key_internal, value, len);
+       } else {
+               int retval1;
+               int retval2;
+               char *buffer1;
+               char *buffer2;
+
+               buffer1 = (char *) calloc(len, sizeof(char));
+               buffer2 = (char *) calloc(len, sizeof(char));
+               if (!buffer1 || !buffer2) {
+                       free(buffer1);
+                       free(buffer2);
+                       pthread_mutex_unlock(&fmutex);
+                       return SYSTEM_INFO_ERROR_OUT_OF_MEMORY;
+               }
 
-       key_internal_len = strlen(key_internal);
-       while ((temp = fgets(buf, sizeof(buf), fp))) {
-               if (!strncmp(buf, key_internal, key_internal_len) && buf[key_internal_len] == ' ') {
-                       value_internal_len = strcspn(buf + key_internal_len + 1, "\n") + 1;
-                       snprintf(value, len < value_internal_len ? len : value_internal_len,
-                                       "%s", buf + key_internal_len + 1);
-                       break;
+               retval1 = db_search_value(SYSTEM_INFO_DB_RO_PATH, key_internal, buffer1, len);
+               retval2 = db_search_value(SYSTEM_INFO_HAL_DB_RO_PATH, key_internal, buffer2, len);
+
+               if (retval1 == SYSTEM_INFO_ERROR_NONE && retval2 == SYSTEM_INFO_ERROR_NONE) {
+                       ret = db_join_value(buffer1, buffer2, type, value, len);
+               } else if (retval1 == SYSTEM_INFO_ERROR_NONE) {
+                       ret = SYSTEM_INFO_ERROR_NONE;
+                       strncpy(value, buffer1, len);
+               } else if (retval2 == SYSTEM_INFO_ERROR_NONE) {
+                       ret = SYSTEM_INFO_ERROR_NONE;
+                       strncpy(value, buffer2, len);
+               } else {
+                       ret = SYSTEM_INFO_ERROR_IO_ERROR;
                }
-       }
 
-       if (!temp) {
-               _D("Failed to find key in DB (%s)", key_internal);
-               ret = SYSTEM_INFO_ERROR_IO_ERROR;
-               goto out;
+               free(buffer1);
+               free(buffer2);
        }
 
-       ret = SYSTEM_INFO_ERROR_NONE;
+       if (ret == SYSTEM_INFO_ERROR_NONE)
+               g_hash_table_insert(hashtable, strdup(key_internal), strdup(value));
 
-       g_hash_table_insert(hashtable, strdup(key_internal), strdup(value));
-out:
-       if (fp)
-               fclose(fp);
        pthread_mutex_unlock(&fmutex);
+
        return ret;
 }