halapi: power: Remove duplicate code by adding isValidParam() function 87/276387/4
authorChanwoo Choi <cw00.choi@samsung.com>
Wed, 15 Jun 2022 09:40:29 +0000 (18:40 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Tue, 21 Jun 2022 01:31:31 +0000 (10:31 +0900)
All hal-api-power funcitons should check the parameter validation.
Add new isValidParam() functions in order to remove the duplicate codes
and then in order to add easily new parameter validation rule.

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

index 61a3e092d7e217a73aeea3da09662827ab85fb29..5917f128897e634350f4a84a30ed1741b3b82ed5 100644 (file)
@@ -192,17 +192,43 @@ EXPORT int hal_power_put_backend(void)
        return 0;
 }
 
-/* Get and set the current governor. */
-EXPORT int hal_power_dvfs_get_curr_governor(unsigned int res_type,
-                                               char *res_name, char *governor)
+static int is_valid_param(char *res_name)
 {
-       struct pass_resource_dvfs_ops *dvfs;
+       if (!g_power_funcs)
+               return -ENOTSUP;
+       else if (!res_name)
+               return -EINVAL;
+       return 0;
+}
 
+static int is_valid_param_with_str(char *res_name, char *str)
+{
        if (!g_power_funcs)
                return -ENOTSUP;
+       else if (!res_name || !str)
+               return -EINVAL;
+       return 0;
+}
 
-       if (!res_name || !governor)
+static int is_valid_param_with_int(char *res_name, int val)
+{
+       if (!g_power_funcs)
+               return -ENOTSUP;
+       else if (!res_name || val < 0)
                return -EINVAL;
+       return 0;
+}
+
+/* Get and set the current governor. */
+EXPORT int hal_power_dvfs_get_curr_governor(unsigned int res_type,
+                                               char *res_name, char *governor)
+{
+       struct pass_resource_dvfs_ops *dvfs;
+       int ret;
+
+       ret = is_valid_param_with_str(res_name, governor);
+       if (ret < 0)
+               return ret;
 
        dvfs = get_dvfs(g_power_funcs, res_type);
        if (!dvfs)
@@ -217,12 +243,11 @@ EXPORT int hal_power_dvfs_get_curr_governor(unsigned int res_type,
 EXPORT int hal_power_dvfs_set_curr_governor(unsigned int res_type, char *res_name, char *governor)
 {
        struct pass_resource_dvfs_ops *dvfs;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!res_name || !governor)
-               return -EINVAL;
+       ret = is_valid_param_with_str(res_name, governor);
+       if (ret < 0)
+               return ret;
 
        dvfs = get_dvfs(g_power_funcs, res_type);
        if (!dvfs)
@@ -258,12 +283,11 @@ EXPORT int hal_power_dvfs_get_avail_governor(unsigned int res_type, char *res_na
 EXPORT int hal_power_dvfs_get_curr_freq(unsigned int res_type, char *res_name)
 {
        struct pass_resource_dvfs_ops *dvfs;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!res_name)
-               return -EINVAL;
+       ret = is_valid_param(res_name);
+       if (ret < 0)
+               return ret;
 
        dvfs = get_dvfs(g_power_funcs, res_type);
        if (!dvfs)
@@ -279,12 +303,11 @@ EXPORT int hal_power_dvfs_get_curr_freq(unsigned int res_type, char *res_name)
 EXPORT int hal_power_dvfs_get_min_freq(unsigned int res_type, char *res_name)
 {
        struct pass_resource_dvfs_ops *dvfs;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!res_name)
-               return -EINVAL;
+       ret = is_valid_param(res_name);
+       if (ret < 0)
+               return ret;
 
        dvfs = get_dvfs(g_power_funcs, res_type);
        if (!dvfs)
@@ -299,12 +322,11 @@ EXPORT int hal_power_dvfs_get_min_freq(unsigned int res_type, char *res_name)
 EXPORT int hal_power_dvfs_set_min_freq(unsigned int res_type, char *res_name, int freq)
 {
        struct pass_resource_dvfs_ops *dvfs;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!res_name || freq < 0)
-               return -EINVAL;
+       ret = is_valid_param_with_int(res_name, freq);
+       if (ret < 0)
+               return ret;
 
        dvfs = get_dvfs(g_power_funcs, res_type);
        if (!dvfs)
@@ -320,12 +342,11 @@ EXPORT int hal_power_dvfs_set_min_freq(unsigned int res_type, char *res_name, in
 EXPORT int hal_power_dvfs_get_max_freq(unsigned int res_type, char *res_name)
 {
        struct pass_resource_dvfs_ops *dvfs;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!res_name)
-               return -EINVAL;
+       ret = is_valid_param(res_name);
+       if (ret < 0)
+               return ret;
 
        dvfs = get_dvfs(g_power_funcs, res_type);
        if (!dvfs)
@@ -340,12 +361,11 @@ EXPORT int hal_power_dvfs_get_max_freq(unsigned int res_type, char *res_name)
 EXPORT int hal_power_dvfs_set_max_freq(unsigned int res_type, char *res_name, int freq)
 {
        struct pass_resource_dvfs_ops *dvfs;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!res_name || freq < 0)
-               return -EINVAL;
+       ret = is_valid_param_with_int(res_name, freq);
+       if (ret < 0)
+               return ret;
 
        dvfs = get_dvfs(g_power_funcs, res_type);
        if (!dvfs)
@@ -361,12 +381,11 @@ EXPORT int hal_power_dvfs_set_max_freq(unsigned int res_type, char *res_name, in
 EXPORT int hal_power_dvfs_get_available_min_freq(unsigned int res_type, char *res_name)
 {
        struct pass_resource_dvfs_ops *dvfs;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!res_name)
-               return -EINVAL;
+       ret = is_valid_param(res_name);
+       if (ret < 0)
+               return ret;
 
        dvfs = get_dvfs(g_power_funcs, res_type);
        if (!dvfs)
@@ -381,12 +400,11 @@ EXPORT int hal_power_dvfs_get_available_min_freq(unsigned int res_type, char *re
 EXPORT int hal_power_dvfs_get_available_max_freq(unsigned int res_type, char *res_name)
 {
        struct pass_resource_dvfs_ops *dvfs;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!res_name)
-               return -EINVAL;
+       ret = is_valid_param(res_name);
+       if (ret < 0)
+               return ret;
 
        dvfs = get_dvfs(g_power_funcs, res_type);
        if (!dvfs)
@@ -402,12 +420,11 @@ EXPORT int hal_power_dvfs_get_available_max_freq(unsigned int res_type, char *re
 EXPORT int hal_power_dvfs_get_up_threshold(unsigned int res_type, char *res_name)
 {
        struct pass_resource_dvfs_ops *dvfs;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!res_name)
-               return -EINVAL;
+       ret = is_valid_param(res_name);
+       if (ret < 0)
+               return ret;
 
        dvfs = get_dvfs(g_power_funcs, res_type);
        if (!dvfs)
@@ -422,12 +439,11 @@ EXPORT int hal_power_dvfs_get_up_threshold(unsigned int res_type, char *res_name
 EXPORT int hal_power_dvfs_set_up_threshold(unsigned int res_type, char *res_name, int up_threshold)
 {
        struct pass_resource_dvfs_ops *dvfs;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!res_name || up_threshold < 0)
-               return -EINVAL;
+       ret = is_valid_param_with_int(res_name, up_threshold);
+       if (ret < 0)
+               return ret;
 
        dvfs = get_dvfs(g_power_funcs, res_type);
        if (!dvfs)
@@ -443,12 +459,11 @@ EXPORT int hal_power_dvfs_set_up_threshold(unsigned int res_type, char *res_name
 EXPORT int hal_power_dvfs_get_load_table(unsigned int res_type, char *res_name, void *pass_cpu_load_table)
 {
        struct pass_resource_dvfs_ops *dvfs;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!res_name)
-               return -EINVAL;
+       ret = is_valid_param(res_name);
+       if (ret < 0)
+               return ret;
 
        dvfs = get_dvfs(g_power_funcs, res_type);
        if (!dvfs)
@@ -467,12 +482,11 @@ EXPORT int hal_power_dvfs_get_load_table(unsigned int res_type, char *res_name,
 EXPORT int hal_power_hotplug_get_online_state(unsigned int res_type, char *res_name, int cpu)
 {
        struct pass_resource_hotplug_ops *hotplug;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!res_name || cpu < 0)
-               return -EINVAL;
+       ret = is_valid_param_with_int(res_name, cpu);
+       if (ret < 0)
+               return ret;
 
        hotplug = get_hotplug(g_power_funcs, res_type);
        if (!hotplug)
@@ -487,11 +501,13 @@ EXPORT int hal_power_hotplug_get_online_state(unsigned int res_type, char *res_n
 EXPORT int hal_power_hotplug_set_online_state(unsigned int res_type, char *res_name, int cpu, int on)
 {
        struct pass_resource_hotplug_ops *hotplug;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
+       ret = is_valid_param_with_int(res_name, cpu);
+       if (ret < 0)
+               return ret;
 
-       if (!res_name || cpu < 0 || on < 0)
+       if (on < 0)
                return -EINVAL;
 
        hotplug = get_hotplug(g_power_funcs, res_type);
@@ -508,12 +524,11 @@ EXPORT int hal_power_hotplug_set_online_state(unsigned int res_type, char *res_n
 EXPORT int hal_power_hotplug_get_online_min_num(unsigned int res_type, char *res_name)
 {
        struct pass_resource_hotplug_ops *hotplug;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!res_name)
-               return -EINVAL;
+       ret = is_valid_param(res_name);
+       if (ret < 0)
+               return ret;
 
        hotplug = get_hotplug(g_power_funcs, res_type);
        if (!hotplug)
@@ -529,12 +544,11 @@ EXPORT int hal_power_hotplug_set_online_min_num(unsigned int res_type,
                                                char *res_name, int min_num)
 {
        struct pass_resource_hotplug_ops *hotplug;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!res_name || (min_num < 0))
-               return -EINVAL;
+       ret = is_valid_param_with_int(res_name, min_num);
+       if (ret < 0)
+               return ret;
 
        hotplug = get_hotplug(g_power_funcs, res_type);
        if (!hotplug)
@@ -551,12 +565,11 @@ EXPORT int hal_power_hotplug_get_online_max_num(unsigned int res_type,
                                                char *res_name)
 {
        struct pass_resource_hotplug_ops *hotplug;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!res_name)
-               return -EINVAL;
+       ret = is_valid_param(res_name);
+       if (ret < 0)
+               return ret;
 
        hotplug = get_hotplug(g_power_funcs, res_type);
        if (!hotplug)
@@ -572,12 +585,11 @@ EXPORT int hal_power_hotplug_set_online_max_num(unsigned int res_type,
                                                char *res_name, int max_num)
 {
        struct pass_resource_hotplug_ops *hotplug;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!res_name || (max_num < 0))
-               return -EINVAL;
+       ret = is_valid_param_with_int(res_name, max_num);
+       if (ret < 0)
+               return ret;
 
        hotplug = get_hotplug(g_power_funcs, res_type);
        if (!hotplug)
@@ -597,12 +609,11 @@ EXPORT int hal_power_thermal_get_temp(unsigned int res_type,
                                                char *res_thermal_name)
 {
        struct pass_resource_tmu_ops *tmu;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!res_thermal_name)
-               return -EINVAL;
+       ret = is_valid_param(res_thermal_name);
+       if (ret < 0)
+               return ret;
 
        tmu = get_tmu(g_power_funcs, res_type);
        if (!tmu)
@@ -624,12 +635,11 @@ EXPORT int hal_power_thermal_get_policy(unsigned int res_type,
                                                char *policy)
 {
        struct pass_resource_tmu_ops *tmu;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!res_thermal_name || !policy)
-               return -EINVAL;
+       ret = is_valid_param_with_str(res_thermal_name, policy);
+       if (ret < 0)
+               return ret;
 
        tmu = get_tmu(g_power_funcs, res_type);
        if (!tmu)
@@ -651,12 +661,11 @@ EXPORT int hal_power_thermal_set_cooling_device_state(unsigned int device_type,
                                                        int state)
 {
        struct pass_resource_tmu_ops *tmu;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!cooling_device_name || state < 0)
-               return -EINVAL;
+       ret = is_valid_param_with_int(cooling_device_name, state);
+       if (ret < 0)
+               return ret;
 
        tmu = get_tmu(g_power_funcs, device_type);
        if (!tmu)
@@ -676,12 +685,11 @@ EXPORT int hal_power_thermal_get_cooling_device_state(unsigned int device_type,
                                                        char *cooling_device_name)
 {
        struct pass_resource_tmu_ops *tmu;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!cooling_device_name)
-               return -EINVAL;
+       ret = is_valid_param(cooling_device_name);
+       if (ret < 0)
+               return ret;
 
        tmu = get_tmu(g_power_funcs, device_type);
        if (!tmu)
@@ -701,12 +709,11 @@ EXPORT int hal_power_thermal_get_cooling_device_max_state(unsigned int device_ty
                                                        char *cooling_device_name)
 {
        struct pass_resource_tmu_ops *tmu;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!cooling_device_name)
-               return -EINVAL;
+       ret = is_valid_param(cooling_device_name);
+       if (ret < 0)
+               return ret;
 
        tmu = get_tmu(g_power_funcs, device_type);
        if (!tmu)
@@ -727,12 +734,11 @@ EXPORT int hal_power_battery_set_charging_status(unsigned int device_type,
                                                        int state)
 {
        struct pass_resource_battery_ops *charging;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!res_name)
-               return -EINVAL;
+       ret = is_valid_param_with_int(res_name, state);
+       if (ret < 0)
+               return ret;
 
        charging = get_charging(g_power_funcs, device_type);
        if (!charging)
@@ -748,12 +754,11 @@ EXPORT int hal_power_battery_get_charging_status(unsigned int device_type,
                                                        char *res_name)
 {
        struct pass_resource_battery_ops *charging;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!res_name)
-               return -EINVAL;
+       ret = is_valid_param(res_name);
+       if (ret < 0)
+               return ret;
 
        charging = get_charging(g_power_funcs, device_type);
        if (!charging)
@@ -770,12 +775,11 @@ EXPORT int hal_power_battery_set_charging_current(unsigned int device_type,
                                                        int charing_current_uA)
 {
        struct pass_resource_battery_ops *charging;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!res_name)
-               return -EINVAL;
+       ret = is_valid_param_with_int(res_name, charing_current_uA);
+       if (ret < 0)
+               return ret;
 
        charging = get_charging(g_power_funcs, device_type);
        if (!charging)
@@ -791,12 +795,11 @@ EXPORT int hal_power_battery_get_charging_current(unsigned int device_type,
                                                        char *res_name)
 {
        struct pass_resource_battery_ops *charging;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!res_name)
-               return -EINVAL;
+       ret = is_valid_param(res_name);
+       if (ret < 0)
+               return ret;
 
        charging = get_charging(g_power_funcs, device_type);
        if (!charging)
@@ -816,12 +819,11 @@ EXPORT int hal_power_memory_get_fault_around_bytes(unsigned int res_type,
                                                char *res_name)
 {
        struct pass_resource_memory *memory;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!res_name)
-               return -EINVAL;
+       ret = is_valid_param(res_name);
+       if (ret < 0)
+               return ret;
 
        switch (res_type) {
        case PASS_RESOURCE_MEMORY_ID:
@@ -842,12 +844,11 @@ EXPORT int hal_power_memory_set_fault_around_bytes(unsigned int res_type,
                                                int fault_around_bytes)
 {
        struct pass_resource_memory *memory;
+       int ret;
 
-       if (!g_power_funcs)
-               return -ENOTSUP;
-
-       if (!res_name || fault_around_bytes < 0)
-               return -EINVAL;
+       ret = is_valid_param_with_int(res_name, fault_around_bytes);
+       if (ret < 0)
+               return ret;
 
        switch (res_type) {
        case PASS_RESOURCE_MEMORY_ID: