halapi: power: Add exec_func helper to simplify the function call 53/277453/2 accepted/tizen/unified/20220715.141321 submit/tizen/20220714.022550 submit/tizen/20220715.015941
authorChanwoo Choi <cw00.choi@samsung.com>
Wed, 15 Jun 2022 10:23:11 +0000 (19:23 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Fri, 8 Jul 2022 08:18:24 +0000 (17:18 +0900)
hal-api-power functions checks the validation of function pointer
and then execute the initialized function pointer with proper argument.

Almost hal-api-power funcitons uses the same pattern style
to both check and execute the function pointer.

So that add exec_func_* helper functions to simplify
the same pattern and keep the consistent coding style.

Change-Id: I6c09fc027bdbc03977e4cecd9e6ae7f375560385
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
src/hal-api-power.c

index 82d1d098ef259802be50a1f1e7ccc9d9803541fb..f41db3f918de15404e657a3c712014f44eebfe0f 100644 (file)
@@ -220,6 +220,29 @@ static int is_valid_param_with_int(char *res_name, int val)
        return 0;
 }
 
+static inline int exec_func(int (*func)(char *res_name), char *res_name)
+{
+       if (!func)
+               return -ENOTSUP;
+       return func(res_name);
+}
+
+static inline int exec_func_with_str(int (*func)(char *res_name, char *str),
+                          char *res_name, char *str)
+{
+       if (!func)
+               return -ENOTSUP;
+       return func(res_name, str);
+}
+
+static inline int exec_func_with_int(int (*func)(char *res_name, int val),
+                          char *res_name, int val)
+{
+       if (!func)
+               return -ENOTSUP;
+       return func(res_name, val);
+}
+
 /* Get and set the current governor. */
 EXPORT int hal_power_dvfs_get_curr_governor(unsigned int res_type,
                                                char *res_name, char *governor)
@@ -235,10 +258,7 @@ EXPORT int hal_power_dvfs_get_curr_governor(unsigned int res_type,
        if (!dvfs)
                return -EPERM;
 
-       if (!dvfs->get_curr_governor)
-               return -ENOTSUP;
-
-       return dvfs->get_curr_governor(res_name, governor);
+       return exec_func_with_str(dvfs->get_curr_governor, res_name, governor);
 }
 
 EXPORT int hal_power_dvfs_set_curr_governor(unsigned int res_type, char *res_name, char *governor)
@@ -254,10 +274,7 @@ EXPORT int hal_power_dvfs_set_curr_governor(unsigned int res_type, char *res_nam
        if (!dvfs)
                return -EPERM;
 
-       if (!dvfs->set_curr_governor)
-               return -ENOTSUP;
-
-       return dvfs->set_curr_governor(res_name, governor);
+       return exec_func_with_str(dvfs->set_curr_governor, res_name, governor);
 }
 
 EXPORT int hal_power_dvfs_get_avail_governor(unsigned int res_type, char *res_name, char **avail_governor)
@@ -274,7 +291,7 @@ EXPORT int hal_power_dvfs_get_avail_governor(unsigned int res_type, char *res_na
        if (!dvfs)
                return -EPERM;
 
-       if (!dvfs->get_avail_governor)
+       if (dvfs->get_avail_governor)
                return -ENOTSUP;
 
        return dvfs->get_avail_governor(res_name, avail_governor);
@@ -294,10 +311,7 @@ EXPORT int hal_power_dvfs_get_curr_freq(unsigned int res_type, char *res_name)
        if (!dvfs)
                return -EPERM;
 
-       if (!dvfs->get_curr_freq)
-               return -ENOTSUP;
-
-       return dvfs->get_curr_freq(res_name);
+       return exec_func(dvfs->get_curr_freq, res_name);
 }
 
 /* Get and set the minimum frequency. */
@@ -314,10 +328,7 @@ EXPORT int hal_power_dvfs_get_min_freq(unsigned int res_type, char *res_name)
        if (!dvfs)
                return -EPERM;
 
-       if (!dvfs->get_min_freq)
-               return -ENOTSUP;
-
-       return dvfs->get_min_freq(res_name);
+       return exec_func(dvfs->get_min_freq, res_name);
 }
 
 EXPORT int hal_power_dvfs_set_min_freq(unsigned int res_type, char *res_name, int freq)
@@ -333,10 +344,7 @@ EXPORT int hal_power_dvfs_set_min_freq(unsigned int res_type, char *res_name, in
        if (!dvfs)
                return -EPERM;
 
-       if (!dvfs->set_min_freq)
-               return -ENOTSUP;
-
-       return dvfs->set_min_freq(res_name, freq);
+       return exec_func_with_int(dvfs->set_min_freq, res_name, freq);
 }
 
 /* Get and set the maximum frequency. */
@@ -353,10 +361,7 @@ EXPORT int hal_power_dvfs_get_max_freq(unsigned int res_type, char *res_name)
        if (!dvfs)
                return -EPERM;
 
-       if (!dvfs->get_max_freq)
-               return -ENOTSUP;
-
-       return dvfs->get_max_freq(res_name);
+       return exec_func(dvfs->get_max_freq, res_name);
 }
 
 EXPORT int hal_power_dvfs_set_max_freq(unsigned int res_type, char *res_name, int freq)
@@ -372,10 +377,7 @@ EXPORT int hal_power_dvfs_set_max_freq(unsigned int res_type, char *res_name, in
        if (!dvfs)
                return -EPERM;
 
-       if (!dvfs->set_max_freq)
-               return -ENOTSUP;
-
-       return dvfs->set_max_freq(res_name, freq);
+       return exec_func_with_int(dvfs->set_max_freq, res_name, freq);
 }
 
 /* Get the minimum/maximum frequency which can be set to resource. */
@@ -392,10 +394,7 @@ EXPORT int hal_power_dvfs_get_available_min_freq(unsigned int res_type, char *re
        if (!dvfs)
                return -EPERM;
 
-       if (!dvfs->get_available_min_freq)
-               return -ENOTSUP;
-
-       return dvfs->get_available_min_freq(res_name);
+       return exec_func(dvfs->get_available_min_freq, res_name);
 }
 
 EXPORT int hal_power_dvfs_get_available_max_freq(unsigned int res_type, char *res_name)
@@ -411,10 +410,7 @@ EXPORT int hal_power_dvfs_get_available_max_freq(unsigned int res_type, char *re
        if (!dvfs)
                return -EPERM;
 
-       if (!dvfs->get_available_max_freq)
-               return -ENOTSUP;
-
-       return dvfs->get_available_max_freq(res_name);
+       return exec_func(dvfs->get_available_max_freq, res_name);
 }
 
 /* Get and set the up_threshold to support boosting. */
@@ -431,10 +427,7 @@ EXPORT int hal_power_dvfs_get_up_threshold(unsigned int res_type, char *res_name
        if (!dvfs)
                return -EPERM;
 
-       if (!dvfs->get_up_threshold)
-               return -ENOTSUP;
-
-       return dvfs->get_up_threshold(res_name);
+       return exec_func(dvfs->get_up_threshold, res_name);
 }
 
 EXPORT int hal_power_dvfs_set_up_threshold(unsigned int res_type, char *res_name, int up_threshold)
@@ -450,10 +443,7 @@ EXPORT int hal_power_dvfs_set_up_threshold(unsigned int res_type, char *res_name
        if (!dvfs)
                return -EPERM;
 
-       if (!dvfs->set_up_threshold)
-               return -ENOTSUP;
-
-       return dvfs->set_up_threshold(res_name, up_threshold);
+       return exec_func_with_int(dvfs->set_up_threshold, res_name, up_threshold);
 }
 
 /* Get the load_table of each resource to estimate the system load. */
@@ -535,10 +525,7 @@ EXPORT int hal_power_hotplug_get_online_min_num(unsigned int res_type, char *res
        if (!hotplug)
                return -EPERM;
 
-       if (!hotplug->get_online_min_num)
-               return -ENOTSUP;
-
-       return hotplug->get_online_min_num(res_name);
+       return exec_func(hotplug->get_online_min_num, res_name);
 }
 
 EXPORT int hal_power_hotplug_set_online_min_num(unsigned int res_type,
@@ -555,10 +542,7 @@ EXPORT int hal_power_hotplug_set_online_min_num(unsigned int res_type,
        if (!hotplug)
                return -EPERM;
 
-       if (!hotplug->set_online_min_num)
-               return -ENOTSUP;
-
-       return hotplug->set_online_min_num(res_name, min_num);
+       return exec_func_with_int(hotplug->set_online_min_num, res_name, min_num);
 }
 
 /* Get and set the maximum number of online CPUs */
@@ -576,10 +560,7 @@ EXPORT int hal_power_hotplug_get_online_max_num(unsigned int res_type,
        if (!hotplug)
                return -EPERM;
 
-       if (!hotplug->get_online_max_num)
-               return -ENOTSUP;
-
-       return hotplug->get_online_max_num(res_name);
+       return exec_func(hotplug->get_online_max_num, res_name);
 }
 
 EXPORT int hal_power_hotplug_set_online_max_num(unsigned int res_type,
@@ -596,10 +577,7 @@ EXPORT int hal_power_hotplug_set_online_max_num(unsigned int res_type,
        if (!hotplug)
                return -EPERM;
 
-       if (!hotplug->set_online_max_num)
-               return -ENOTSUP;
-
-       return hotplug->set_online_max_num(res_name, max_num);
+       return exec_func_with_int(hotplug->set_online_max_num, res_name, max_num);
 }
 
 /**
@@ -620,14 +598,11 @@ EXPORT int hal_power_thermal_get_temp(unsigned int res_type,
        if (!tmu)
                return -EPERM;
 
-       if (!tmu->get_temp)
-               return -ENOTSUP;
-
        /*
         * In the case of the HAL TMU ops, res_thermal_name is used
         * as the first argument instead of res_name.
         */
-       return tmu->get_temp(res_thermal_name);
+       return exec_func(tmu->get_temp, res_thermal_name);
 }
 
 /* Get the policy of thermal management unit. */
@@ -646,14 +621,11 @@ EXPORT int hal_power_thermal_get_policy(unsigned int res_type,
        if (!tmu)
                return -EPERM;
 
-       if (!tmu->get_policy)
-               return -ENOTSUP;
-
        /*
         * In the case of the HAL TMU ops, res_thermal_name is used
         * as the first argument instead of res_name.
         */
-       return tmu->get_policy(res_thermal_name, policy);
+       return exec_func_with_str(tmu->get_policy, res_thermal_name, policy);
 }
 
 /* Get and set the state of thermal cooling-device */
@@ -672,14 +644,11 @@ EXPORT int hal_power_thermal_set_cooling_device_state(unsigned int device_type,
        if (!tmu)
                return -EPERM;
 
-       if (!tmu->set_cooling_device_state)
-               return -ENOTSUP;
-
        /*
         * In the case of the HAL TMU ops, cooling_device_name is used
         * as the first argument instead of res_name.
         */
-       return tmu->set_cooling_device_state(cooling_device_name, state);
+       return exec_func_with_int(tmu->set_cooling_device_state, cooling_device_name, state);
 }
 
 EXPORT int hal_power_thermal_get_cooling_device_state(unsigned int device_type,
@@ -696,14 +665,11 @@ EXPORT int hal_power_thermal_get_cooling_device_state(unsigned int device_type,
        if (!tmu)
                return -EPERM;
 
-       if (!tmu->get_cooling_device_state)
-               return -ENOTSUP;
-
        /*
         * In the case of the HAL TMU ops, cooling_device_name is used
         * as the first argument instead of res_name.
         */
-       return tmu->get_cooling_device_state(cooling_device_name);
+       return exec_func(tmu->get_cooling_device_state, cooling_device_name);
 }
 
 EXPORT int hal_power_thermal_get_cooling_device_max_state(unsigned int device_type,
@@ -720,14 +686,11 @@ EXPORT int hal_power_thermal_get_cooling_device_max_state(unsigned int device_ty
        if (!tmu)
                return -EPERM;
 
-       if (!tmu->get_cooling_device_max_state)
-               return -ENOTSUP;
-
        /*
         * In the case of the HAL TMU ops, cooling_device_name is used
         * as the first argument instead of res_name.
         */
-       return tmu->get_cooling_device_max_state(cooling_device_name);
+       return exec_func(tmu->get_cooling_device_max_state, cooling_device_name);
 }
 
 EXPORT int hal_power_battery_set_charging_status(unsigned int device_type,
@@ -745,10 +708,7 @@ EXPORT int hal_power_battery_set_charging_status(unsigned int device_type,
        if (!charging)
                return -EPERM;
 
-       if (!charging->set_charging_status)
-               return -ENOTSUP;
-
-       return charging->set_charging_status(res_name, state);
+       return exec_func_with_int(charging->set_charging_status, res_name, state);
 }
 
 EXPORT int hal_power_battery_get_charging_status(unsigned int device_type,
@@ -765,10 +725,7 @@ EXPORT int hal_power_battery_get_charging_status(unsigned int device_type,
        if (!charging)
                return -EPERM;
 
-       if (!charging->get_charging_status)
-               return -ENOTSUP;
-
-       return charging->get_charging_status(res_name);
+       return exec_func(charging->get_charging_status, res_name);
 }
 
 EXPORT int hal_power_battery_set_charging_current(unsigned int device_type,
@@ -786,10 +743,7 @@ EXPORT int hal_power_battery_set_charging_current(unsigned int device_type,
        if (!charging)
                return -EPERM;
 
-       if (!charging->set_charging_current)
-               return -ENOTSUP;
-
-       return charging->set_charging_current(res_name, charing_current_uA);
+       return exec_func_with_int(charging->set_charging_current, res_name, charing_current_uA);
 }
 
 EXPORT int hal_power_battery_get_charging_current(unsigned int device_type,
@@ -806,10 +760,7 @@ EXPORT int hal_power_battery_get_charging_current(unsigned int device_type,
        if (!charging)
                return -EPERM;
 
-       if (!charging->get_charging_current)
-               return -ENOTSUP;
-
-       return charging->get_charging_current(res_name);
+       return exec_func(charging->get_charging_current, res_name);
 }
 
 /**
@@ -834,10 +785,7 @@ EXPORT int hal_power_memory_get_fault_around_bytes(unsigned int res_type,
                return -EPERM;
        }
 
-       if (!memory->get_fault_around_bytes)
-               return -ENOTSUP;
-
-       return memory->get_fault_around_bytes(res_name);
+       return exec_func(memory->get_fault_around_bytes, res_name);
 }
 
 EXPORT int hal_power_memory_set_fault_around_bytes(unsigned int res_type,
@@ -869,10 +817,7 @@ EXPORT int hal_power_memory_set_fault_around_bytes(unsigned int res_type,
                return -EPERM;
        }
 
-       if (!memory->set_fault_around_bytes)
-               return -ENOTSUP;
-
-       return memory->set_fault_around_bytes(res_name, fault_around_bytes);
+       return exec_func_with_int(memory->set_fault_around_bytes, res_name, fault_around_bytes);
 }
 
 /**