pass: thermal: Define thermal_update function to reduce duplicate code 87/224287/4
authorChanwoo Choi <cw00.choi@samsung.com>
Fri, 7 Feb 2020 10:28:55 +0000 (19:28 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Mon, 10 Feb 2020 10:01:19 +0000 (19:01 +0900)
thermal_moitor_func() and thermal_get_scenario() functions contains
the same code for deciding the next thermal scenario by using the
thermal raw data. So that define thermal_update() function to reduce
duplicate code from both thermal_moitor_func() and thermal_get_scenario().

Change-Id: Ia0bfef053163a848e657efb36c2dccb232037a04
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
src/pass/pass-thermal.c

index 16fd87cb2c97a00031f28d00dc30871d1a404ff1..8e9e970ceb2cce12b5a4581f8d52b87ba9110399 100644 (file)
 
 #define DEFAULT_TEMPERATURE            (-1000)
 
-/**
- * @brief      Decide the proper thermal scenario by using both the monitored
- *             raw data of thermal information and the parsed thermal
- *             information from scenario section. And then notify the decided
- *             thermal scenario to Thermal Monitor featrue (thermal) which
- *             provides the external D-bus interface for Thermal Monitor.
- * thermal
- * @param      [in] result Monitored data of thermal will be saved to it.
- * @param      [in] user_data Instance of a resource monitor
- * @return     @c 0 on success, otherwise error value
- */
-static int thermal_monitor_func(void *result, void *user_data)
+static int thermal_update(struct pass_resource *res,
+                       struct resmon_result_src_thermal *thermal_result)
 {
-       struct pass_resource *res = user_data;
-       struct resmon_result_src_thermal *thermal_result = result;
-       struct pass_thermal *thermal;
+       struct pass_thermal *thermal = &res->thermal;;
        int timer_interval_of_scenario, timer_interval;
-       int curr_temp, prev_temp;
-       int temp;
-       int ret;
+       int curr_temp, prev_temp, temp;
        int i, scenario_idx;
+       int ret;
 
        if (!res || !thermal_result) {
                _E("invalid parameter for thermal monitor\n");
                return -EINVAL;
        }
 
-       thermal = &res->thermal;
        prev_temp = thermal->curr_temp;
        curr_temp = thermal_result->temp;
 
@@ -92,17 +78,6 @@ static int thermal_monitor_func(void *result, void *user_data)
        if (thermal->curr_scenario_idx == scenario_idx)
                return 0;
 
-       /*
-        * Send notification with thermal scenario name according to
-        * measured temperature of each h/w resource.
-        */
-       device_notify(DEVICE_NOTIFIER_THERMAL,
-                               (void *)thermal->scenarios[scenario_idx].name);
-
-       _I("Monitor '%s' scenario for '%s' resource ('%d' degrees Celsius)\n",
-                               thermal->scenarios[scenario_idx].name,
-                               res->config_data.res_name, curr_temp);
-
        /* Get the new timer interval of each scenario */
        timer_interval_of_scenario
                = thermal->scenarios[scenario_idx].thermal.timer_interval;
@@ -122,6 +97,51 @@ static int thermal_monitor_func(void *result, void *user_data)
        thermal->curr_temp = curr_temp;
        thermal->curr_scenario_idx = scenario_idx;
 
+       _I("Monitor '%-12s' of '%s' resource ('%3d' degrees,'%5d' ms)\n",
+                               thermal->scenarios[scenario_idx].name,
+                               res->config_data.res_name,
+                               curr_temp, timer_interval);
+
+       return 0;
+}
+
+/**
+ * @brief      Decide the proper thermal scenario by using both the monitored
+ *             raw data of thermal information and the parsed thermal
+ *             information from scenario section. And then notify the decided
+ *             thermal scenario to Thermal Monitor featrue (thermal) which
+ *             provides the external D-bus interface for Thermal Monitor.
+ * thermal
+ * @param      [in] result Monitored data of thermal will be saved to it.
+ * @param      [in] user_data Instance of a resource monitor
+ * @return     @c 0 on success, otherwise error value
+ */
+static int thermal_monitor_func(void *result, void *user_data)
+{
+       struct pass_resource *res = user_data;
+       struct pass_thermal *thermal = &res->thermal;;
+       struct resmon_result_src_thermal *thermal_result;
+       int ret;
+
+       /* Get thermal_result raw data by monitoring */
+       thermal_result = result;
+
+       /*
+        * Update thermal scenario by using both the monitored raw data
+        * of thermal information and the parsed thermal information
+        * from scenario section.
+        */
+       ret = thermal_update(res, thermal_result);
+       if (ret < 0)
+               return ret;
+
+       /*
+        * Send notification with thermal scenario name according to
+        * measured temperature of each h/w resource.
+        */
+       device_notify(DEVICE_NOTIFIER_THERMAL,
+               (void *)thermal->scenarios[thermal->curr_scenario_idx].name);
+
        return 0;
 }
 
@@ -134,65 +154,27 @@ static int thermal_get_scenario(void *data, void *user_data)
 {
        void **scenario = data;
        struct pass_resource *res = user_data;
+       struct pass_thermal *thermal = &res->thermal;;
        struct resmon_result_src_thermal *thermal_result;
-       struct pass_thermal *thermal;
-       int timer_interval_of_scenario, timer_interval;
-       int curr_temp, temp;
-       int i, scenario_idx;
        int ret;
 
-       if (!res) {
-               _E("invalid parameter\n");
-               return -EINVAL;
-       }
-
-       thermal = &res->thermal;
-
+       /* Get thermal_result raw data by reading directly */
        thermal_result = get_thermal_result(res);
-       if (!thermal_result)
-               return -EINVAL;
-
-       curr_temp = thermal_result->temp;
-
-       for (i = thermal->num_scenarios - 1; i > 0; i--) {
-               temp = thermal->scenarios[i].thermal.temperature;
-
-               if (thermal->scenarios[i].state != PASS_ON)
-                       continue;
 
-               if (temp <= curr_temp)
-                       break;
-       }
-       scenario_idx = i;
+       /*
+        * Update thermal scenario by using both the monitored raw data
+        * of thermal information and the parsed thermal information
+        * from scenario section.
+        */
+       ret = thermal_update(res, thermal_result);
+       if (ret < 0)
+               return ret;
 
        /*
-        * Inform the current thermal scenario name to
+        * Inform the thermal scenario name to
         * DEVICE_NOTIFIER_THERMAL_GET_SCENARIO client.
         */
-       *scenario = (void *)thermal->scenarios[scenario_idx].name;
-
-       _I("Monitor '%s' scenario for '%s' resource ('%d' degrees Celsius)\n",
-                               thermal->scenarios[scenario_idx].name,
-                               res->config_data.res_name, curr_temp);
-
-       /* Get the new timer interval of each scenario */
-       timer_interval_of_scenario
-               = thermal->scenarios[scenario_idx].thermal.timer_interval;
-       if (timer_interval_of_scenario > 0)
-               timer_interval = timer_interval_of_scenario;
-       else
-               timer_interval = res->thermal.timer_interval;
-
-       ret = pass_resmon_update_timer_interval(res, RESMON_SRC_THERMAL,
-                               timer_interval);
-       if (ret < 0) {
-               _W("failed to update interval of timer-based monitor " \
-                       "(res_name:%s, src_type: 0x%x)\n",
-                       res->config_data.res_name, RESMON_SRC_THERMAL);
-       }
-
-       thermal->curr_temp = curr_temp;
-       thermal->curr_scenario_idx = scenario_idx;
+       *scenario = (void *)thermal->scenarios[thermal->curr_scenario_idx].name;
 
        return 0;
 }