thermal: Convert pass-thermal.conf format to json to improve readability 56/261956/6
authorChanwoo Choi <cw00.choi@samsung.com>
Thu, 29 Jul 2021 12:44:15 +0000 (21:44 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Mon, 2 Aug 2021 05:50:02 +0000 (14:50 +0900)
The legacy pass-thermal.conf file format is not better on side of readability.
So that change the configuration file format to json style to improve
readability.

Change-Id: I228559fbc348b097284263a6bd8c57c1b2e6fa37
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
CMakeLists.txt
packaging/pass.spec
scripts/pass-thermal.conf [deleted file]
scripts/pass-thermal.json [new file with mode: 0644]
src/thermal/thermal-parser.c
src/thermal/thermal.c

index 6bd9240..be26532 100644 (file)
@@ -99,7 +99,7 @@ TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LDFLAGS} -ldl -lm)
 INSTALL(TARGETS ${PROJECT_NAME} DESTINATION bin)
 INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/scripts/${PROJECT_NAME}.conf DESTINATION /etc/dbus-1/system.d)
 INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/scripts/pass-pmqos.json DESTINATION /etc/pass)
-INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/scripts/pass-thermal.conf DESTINATION /etc/pass)
+INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/scripts/pass-thermal.json DESTINATION /etc/pass)
 CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/systemd/${PROJECT_NAME}.service.in ${CMAKE_SOURCE_DIR}/systemd/${PROJECT_NAME}.service @ONLY)
 INSTALL(FILES ${CMAKE_SOURCE_DIR}/systemd/org.tizen.system.pass.service DESTINATION /usr/share/dbus-1/system-services)
 INSTALL(FILES ${CMAKE_SOURCE_DIR}/systemd/org.tizen.system.thermal.service DESTINATION /usr/share/dbus-1/system-services)
index b1fd8a0..3856fc3 100644 (file)
@@ -91,7 +91,7 @@ systemctl daemon-reload
 %license LICENSE
 %config %{_sysconfdir}/dbus-1/system.d/%{daemon_name}.conf
 %config %{_sysconfdir}/pass/pass-pmqos.json
-%config %{_sysconfdir}/pass/pass-thermal.conf
+%config %{_sysconfdir}/pass/pass-thermal.json
 %{_bindir}/%{daemon_name}
 %{_unitdir}/delayed.target.wants/%{daemon_name}.service
 %{_unitdir}/%{daemon_name}.service
diff --git a/scripts/pass-thermal.conf b/scripts/pass-thermal.conf
deleted file mode 100644 (file)
index f15ad2c..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-[thermal]
-# set to "yes" thermal_support (Default value is no)
-# set thermal_number_of_scenario to be tested
-thermal_support=yes
-thermal_number_of_scenario=4
-
-# describe the scenario section as follows
-#[Scenario0]
-#name=Release
-#support=yes
-[thermal.scenario0]
-name=Release
-support=yes
-
-[thermal.scenario1]
-name=Warning
-support=yes
-
-[thermal.scenario2]
-name=LimitAction
-support=yes
-
-[thermal.scenario3]
-name=Shutdown
-support=yes
diff --git a/scripts/pass-thermal.json b/scripts/pass-thermal.json
new file mode 100644 (file)
index 0000000..83fe051
--- /dev/null
@@ -0,0 +1,19 @@
+{
+       "thermal_support"       : true,
+       "thermal_scenario_list" :
+       [
+               {
+                       "name"                  : "Release",
+                       "support"               : true
+               }, {
+                       "name"                  : "Warning",
+                       "support"               : true
+               }, {
+                       "name"                  : "LimitAction",
+                       "support"               : true
+               }, {
+                       "name"                  : "Shutdown",
+                       "support"               : true
+               }
+       ]
+}
index 58df42e..8a4a9f0 100644 (file)
 
 #include <pass/log.h>
 #include <pass/common.h>
-#include <pass/config-parser.h>
 
 #include "thermal.h"
 
-#define MAX_NUM_OF_SCENARIOS           255
-
-static bool is_supported(const char *value)
-{
-       assert(value);
-
-       if (MATCH(value, "yes"))
-               return true;
-       return false;
-}
-
 /**
  * @brief      Parse scenario section to get a scenario information
  *             for Thermal Monitor.
- * @param      [in] result Parsed raw data from configuration
- * @param      [in] user_data Instance for each scenario
- * @param      [in] index Index of each scenario
+ * @param      [in] obj Instance of json_object for each scenario
+ * @param      [in] scenario Instance for each scenario
  * @return     @c 0 on success, otherwise error value
  */
-static int thermal_parse_scenario(struct parse_result *result, void *user_data,
-                                       unsigned int index)
+static int thermal_parse_scenario(json_object *obj, struct scenario *scenario)
 {
-       struct thermal_scenario *scenarios = (struct thermal_scenario *)user_data;
-
-       assert(result);
-       assert(result->section && result->name && result->value);
-
-       /* Parse 'thermal' section */
-       if (MATCH(result->section, "thermal")) {
-               if (MATCH(result->name, "thermal_support"))
-                       scenarios->support = is_supported(result->value);
-               else if (MATCH(result->name, "thermal_number_of_scenario")) {
-                       int num;
-
-                       num = sys_strtol(result->value);
-                       if (num < 0)
-                               return num;
-
-                       if (num > MAX_NUM_OF_SCENARIOS)
-                               return -EINVAL;
-
-                       if (num > 0) {
-                               scenarios->list = calloc(num,
-                                               sizeof(struct scenario));
-                               if (!scenarios->list) {
-                                       _E("failed to allocate scenario memory");
-                                       return -errno;
-                               }
-
-                               scenarios->num = num;
-                       }
-               }
-       }
+       const char *name;
+       int support;
 
-       if (!scenarios->support || !scenarios->num)
-               return 0;
+       /* Get property values */
+       name = get_string_from_object(obj, "name");
+       support = get_boolean_from_object(obj, "support");
 
-       if (index >= scenarios->num)
-               return 0;
+       /* Check the mandatory property values are valid or not */
+       if (!name) {
+               _E("Failed to get 'name' property of scenario section\n");
+               return -EINVAL;
+       }
 
-       /* Parse 'Scenario' section */
-       if (MATCH(result->name, "name"))
-               snprintf(scenarios->list[index].name,
-                       strlen(result->value) + 1, "%s", result->value);
-       else if (MATCH(result->name, "support"))
-               scenarios->list[index].support = is_supported(result->value);
+       /* Initialize config_data from property values of confiugartion file */
+       snprintf(scenario->name, strlen(name) + 1, "%s", name);
+       scenario->support = (support < 0) ? 1 : support;
 
        return 0;
 }
@@ -114,51 +72,56 @@ static int thermal_parse_scenario(struct parse_result *result, void *user_data,
 /**
  * @brief      Parse configuration to get information of supported scenarios
  *             for Thermal Monitor such as Release, Warning.
- * @param      [in] result Parsed raw data from configuration
- * @param      [in] user_data Instance for each scenario
+ * @param      [in] obj Instance of json_object of thermal configuration file
+ * @param      [in] scenarios Instance for all scenarios
  * @return     @c 0 on success, otherwise error value
  */
-static int thermal_load_config(struct parse_result *result, void *user_data)
+static int thermal_load_config(json_object *obj, struct thermal_scenario *scenarios)
 {
-       struct thermal_scenario *scenarios
-                               = (struct thermal_scenario *)user_data;
-       char name[NAME_MAX];
-       int ret;
-       static int index;
-
-       if (!result)
-               return 0;
-
-       if (!result->section || !result->name || !result->value)
+       int thermal_support;
+       json_object *thermal_scenario_list = NULL;
+       int num_scenarios = 0, ret, i;
+
+       /* Get property values */
+       thermal_support = get_boolean_from_object(obj, "thermal_support");
+       if (json_object_object_get_ex(obj, "thermal_scenario_list",
+                               &thermal_scenario_list))
+               num_scenarios = json_object_array_length(thermal_scenario_list);
+
+       /* Check the mandatory property values are valid or not */
+       if (thermal_support <= 0 || num_scenarios <= 0) {
+               _I("Disable thermal module\n");
                return 0;
+       }
 
-       /* Parsing 'thermal' section */
-       if (MATCH(result->section, "thermal")) {
-               ret = thermal_parse_scenario(result, user_data, -1);
-               if (ret < 0) {
-                       _E("failed to parse [thermal] section : %d", ret);
-                       return ret;
-               }
-               goto out;
+       /* Initialize config_data from property values of confiugartion file */
+       scenarios->support = thermal_support;
+       scenarios->list = calloc(num_scenarios, sizeof(struct scenario));
+       if (!scenarios->list) {
+               _E("failed to allocate memory for scenario");
+               return -ENOMEM;
        }
+       scenarios->num = num_scenarios;
 
-       /* Parsing 'thermal.scenario' section */
-       for (index = 0; index < scenarios->num; ++index) {
-               snprintf(name, sizeof(name), "thermal.scenario%d", index);
-
-               if (MATCH(result->section, name)) {
-                       ret = thermal_parse_scenario(result, user_data, index);
-                       if (ret < 0) {
-                               _E("failed to parse [thermal.scenario%d] section : %d",
-                                                       index, ret);
-                               return ret;
-                       }
-                       goto out;
+       for (i = 0; i < scenarios->num ; i++) {
+               json_object *scenario_obj
+                       = json_object_array_get_idx(thermal_scenario_list, i);
+               struct scenario *scenario = &(scenarios->list[i]);
+
+               ret = thermal_parse_scenario(scenario_obj, scenario);
+               if (ret < 0) {
+                       _E("cannot parse 'thermal.scenario%d' section\n", i);
+                       goto err;
                }
        }
 
-out:
        return 0;
+err:
+       free(scenarios->list);
+       scenarios->list = NULL;
+       scenarios->num = 0;
+
+       return ret;
 }
 
 /**
@@ -189,16 +152,24 @@ int thermal_put_scenario(struct thermal_scenario *scenarios)
 int thermal_get_scenario(const char *path, struct thermal_scenario *scenarios)
 {
        int ret;
+       json_object *obj = NULL;
 
        /* Initialize the variables before parsing the pass-thermal.conf */
        scenarios->num = 0;
 
-       /* get configuration file */
-       ret = config_parse(path, thermal_load_config, scenarios);
-       if (ret < 0) {
-               thermal_put_scenario(scenarios);
-               return ret;
+       /* Parse configuration file */
+       obj = json_object_from_file(path);
+       if (!obj) {
+               _E("Failed to get json_object from %s (%s)\n",
+                               path, json_util_get_last_err());
+               return -EINVAL;
        }
 
-       return 0;
+       ret = thermal_load_config(obj, scenarios);
+       if (ret < 0)
+               thermal_put_scenario(scenarios);
+
+       json_object_put(obj);
+
+       return ret;
 }
index 4d43fd2..cca20dc 100644 (file)
@@ -38,7 +38,7 @@
 
 #include "thermal.h"
 
-#define THERMAL_CONF_PATH              "/etc/pass/pass-thermal.conf"
+#define THERMAL_JSON_PATH              "/etc/pass/pass-thermal.json"
 
 /**
  * @brief      Global instance indicating the Thermal Monitor D-Bus interface
@@ -116,7 +116,7 @@ static int thermal_init_done(void *data, void *user_data)
                return -ENOMEM;
        }
 
-       ret = thermal_get_scenario(THERMAL_CONF_PATH, g_thermal);
+       ret = thermal_get_scenario(THERMAL_JSON_PATH, g_thermal);
        if (ret < 0) {
                _E("failed to get Thermal Monitor scenario\n");
                free(g_thermal);