pass: parser: Add get_property_set to simplify getting properties 55/280755/5
authorChanwoo Choi <cw00.choi@samsung.com>
Wed, 31 Aug 2022 14:28:33 +0000 (23:28 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Wed, 7 Sep 2022 03:13:08 +0000 (03:13 +0000)
And get_property_set function to reduce the get_property function call.
It makes the getting propertis more simpler than before

Also, get the property value from json configuration file
and then stored them to local variables. And fill out the allocated
memory of structure with local variables. But, local variables
are not necessary. In order to reduce the LOC (line of code),
get property value directly without local variables.

Change-Id: Ib993d11d6ad22d3fd12fca54a591ae30cd3b9b20
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
include/util/common.h
src/pass/pass-parser.c
src/pmqos/pmqos-parser.c
src/thermal/thermal-parser.c
src/util/common.c

index a11475e..213ed70 100644 (file)
@@ -123,6 +123,13 @@ enum data_type {
        (type *)((char *)__mptr - offsetof(type, member)); })
 #endif
 
+struct config_property {
+    const char *name;
+    int type;
+    bool mandatory;
+    void *value;
+};
+
 int sys_get_str(const char *fname, char *str);
 int sysfs_get_int(char *path, int *val);
 
@@ -134,4 +141,6 @@ json_object *get_object_from_object(json_object *obj, const char *key);
 
 int get_property(json_object *obj, const char *key, int type, bool mandatory,
                 void *data);
+int get_property_set(json_object *obj, struct config_property *properties,
+                unsigned int num_properties);
 #endif /* __CORE_COMMON_H__ */
index 67e3f51..8bff029 100644 (file)
@@ -107,53 +107,45 @@ static void init_scenario(struct pass_scenario *scenario)
 static int parse_scenario(struct pass_resource *res, json_object *obj,
                                struct pass_scenario *scenario)
 {
-       char name[BUFF_MAX] = {};
-       bool support;
-       int target_level;
-       int cpuhp_min_level;
-       int cpuhp_max_level;
        json_object *temperature;
-       int temp_start;
-       int temp_end;
-       int threshold;
-       int timer_interval_ms;
+       bool support;
        bool overridable;
        int ret = 0;
 
+       if (!res || !obj || !scenario)
+               return -EINVAL;
+
        /* Get property values */
-       ret += get_property(obj, "name", DATA_TYPE_STRING,
-                               true, (void *)name);
-       ret += get_property(obj, "target_level", DATA_TYPE_INT,
-                               true, (void *)&target_level);
-
-       ret += get_property(obj, "support", DATA_TYPE_BOOLEAN,
-                               false, (void *)&support);
-       ret += get_property(obj, "timer_interval_ms", DATA_TYPE_INT,
-                               false, (void *)&timer_interval_ms);
-       ret += get_property(obj, "overridable", DATA_TYPE_BOOLEAN,
-                               false, (void *)&overridable);
-
-       /* - property for only pmqos module */
-       ret += get_property(obj, "cpuhp_min_level", DATA_TYPE_INT,
-                               false, (void *)&cpuhp_min_level);
-       ret += get_property(obj, "cpuhp_max_level", DATA_TYPE_INT,
-                               false, (void *)&cpuhp_max_level);
-
-       /* - property for only thermal module */
+       struct config_property scenario_properties[] = {
+               { "name",               DATA_TYPE_STRING, true,  (void *)&scenario->name },
+               { "target_level",       DATA_TYPE_INT, true,  (void *)&scenario->level },
+               { "support",            DATA_TYPE_BOOLEAN, false, (void *)&support },
+               { "overridable",        DATA_TYPE_BOOLEAN, false, (void *)&overridable },
+               /* - property for only pmqos module */
+               { "cpuhp_min_level",    DATA_TYPE_INT, false, (void *)&scenario->pmqos.min_level },
+               { "cpuhp_max_level",    DATA_TYPE_INT, false, (void *)&scenario->pmqos.max_level },
+               /* - property for only thermal module */
+               { "timer_interval_ms",  DATA_TYPE_INT, false, (void *)&scenario->thermal.timer_interval },
+       };
+
+       ret += get_property_set(obj, scenario_properties, ARRAY_SIZE(scenario_properties));
+
        temperature = get_object_from_object(obj, "temperature");
        if (temperature) {
                ret += get_property(temperature, "start", DATA_TYPE_INT,
-                                       true, (void *)&temp_start);
+                                       true, (void *)&scenario->thermal.temp_start);
                ret += get_property(temperature, "end", DATA_TYPE_INT,
-                                       true, (void *)&temp_end);
+                                       true, (void *)&scenario->thermal.temp_end);
                ret += get_property(temperature, "threshold", DATA_TYPE_INT,
-                                       false, (void *)&threshold);
+                                       false, (void *)&scenario->thermal.threshold);
 
                /* if threshold is not presented, using temp_end as default */
-               if (threshold < 0)
-                       threshold = temp_end;
+               if (scenario->thermal.threshold < 0)
+                       scenario->thermal.threshold = scenario->thermal.temp_end;
        } else {
-               temp_start = temp_end = threshold = 0;
+               scenario->thermal.temp_start = 0;
+               scenario->thermal.temp_end = 0;
+               scenario->thermal.threshold = 0;
        }
 
        /* Check whether the mandatory properties are included or not */
@@ -164,29 +156,19 @@ static int parse_scenario(struct pass_resource *res, json_object *obj,
        }
 
        /* Check the validation of property value */
-       if (temp_start < 0 || temp_end < 0 || temp_end < temp_start) {
+       if (scenario->thermal.temp_start < 0 || scenario->thermal.temp_end < 0
+                       || scenario->thermal.temp_end < scenario->thermal.temp_start) {
                _E("Invalid temperature range in scenario section\n");
                return -EINVAL;
-       } else if (threshold < temp_start || threshold > temp_end) {
+       } else if (scenario->thermal.threshold < scenario->thermal.temp_start
+                       || scenario->thermal.threshold > scenario->thermal.temp_end) {
                _W("Invalid thermal 'threshold', using default as range 'end'\n");
-               threshold = temp_end;
+               scenario->thermal.threshold = scenario->thermal.temp_end;
        }
 
        /* Initialize config_data from property values of confiugartion file */
-       snprintf(scenario->name, NAME_MAX, "%s", name);
-       scenario->state = support;
-       scenario->level = target_level;
-
-       /* - property for only pmqos module */
-       scenario->pmqos.min_level = cpuhp_min_level;
-       scenario->pmqos.max_level = cpuhp_max_level;
-
-       /* - property for only thermal module */
-       scenario->thermal.temp_start = temp_start;
-       scenario->thermal.temp_end = temp_end;
-       scenario->thermal.threshold = threshold;
-       scenario->thermal.timer_interval = timer_interval_ms;
-       scenario->thermal.overridable = overridable;
+       scenario->state = !!support;
+       scenario->thermal.overridable = !!overridable;
 
        return 0;
 }
@@ -259,93 +241,49 @@ static int parse_level(struct pass_resource *res, json_object *obj,
                        struct pass_level *target_level, int level_idx)
 {
        int level;
-       int minimum_frequency_khz;
-       int maximum_frequency_khz;
-       int number_of_minimum_cpu;
-       int number_of_maximum_cpu;
-       int num_down_cond;
-       int num_down_cond_freq;
-       int num_down_cond_nr_running;
-       int num_down_cond_busy_cpu;
-       int num_up_cond;
-       int num_up_cond_freq;
-       int num_up_cond_nr_running;
-       int num_up_cond_busy_cpu;
-       int num_right_cond;
-       int num_right_cond_freq;
-       int num_right_cond_nr_running;
-       int num_right_cond_busy_cpu;
-       int num_left_cond;
-       int num_left_cond_freq;
-       int num_left_cond_nr_running;
-       int num_left_cond_busy_cpu;
-       int governor_timeout_ms;
-       int fault_around_bytes;
-       int cooling_device_state;
-       int charging_status;
-       int charging_current_uA;
        int ret = 0;
 
+       if(!res || !obj || !target_level || level_idx < 0)
+               return -EINVAL;
+
        /* Get property values */
-       ret += get_property(obj, "level", DATA_TYPE_INT,
-                               true, (void *)&level);
-       ret += get_property(obj, "dvfs,minimum_frequency_khz", DATA_TYPE_INT,
-                               false, (void *)&minimum_frequency_khz);
-       ret += get_property(obj, "dvfs,maximum_frequency_khz",  DATA_TYPE_INT,
-                               false, (void *)&maximum_frequency_khz);
-       ret += get_property(obj, "hotplug,number_of_minimum_cpu", DATA_TYPE_INT,
-                               false, (void *)&number_of_minimum_cpu);
-       ret += get_property(obj, "hotplug,number_of_maximum_cpu", DATA_TYPE_INT,
-                               false, (void *)&number_of_maximum_cpu);
-
-       ret += get_property(obj, "hotplug,num_down_cond", DATA_TYPE_INT,
-                               false, (void *)&num_down_cond);
-       ret += get_property(obj, "hotplug,num_down_cond_freq", DATA_TYPE_INT,
-                               false, (void *)&num_down_cond_freq);
-       ret += get_property(obj, "hotplug,num_down_cond_nr_running", DATA_TYPE_INT,
-                               false, (void *)&num_down_cond_nr_running);
-       ret += get_property(obj, "hotplug,num_down_cond_busy_cpu", DATA_TYPE_INT,
-                               false, (void *)&num_down_cond_busy_cpu);
-
-       ret += get_property(obj, "hotplug,num_up_cond", DATA_TYPE_INT,
-                               false, (void *)&num_up_cond);
-       ret += get_property(obj, "hotplug,num_up_cond_freq", DATA_TYPE_INT,
-                               false, (void *)&num_up_cond_freq);
-       ret += get_property(obj, "hotplug,num_up_cond_nr_running", DATA_TYPE_INT,
-                               false, (void *)&num_up_cond_nr_running);
-       ret += get_property(obj, "hotplug,num_up_cond_busy_cpu", DATA_TYPE_INT,
-                               false, (void *)&num_up_cond_busy_cpu);
-
-       ret += get_property(obj, "hotplug,num_right_cond", DATA_TYPE_INT,
-                               false, (void *)&num_right_cond);
-       ret += get_property(obj, "hotplug,num_right_cond_freq", DATA_TYPE_INT,
-                               false, (void *)&num_right_cond_freq);
-       ret += get_property(obj, "hotplug,num_right_cond_nr_running", DATA_TYPE_INT,
-                               false, (void *)&num_right_cond_nr_running);
-       ret += get_property(obj, "hotplug,num_right_cond_busy_cpu", DATA_TYPE_INT,
-                               false, (void *)&num_right_cond_busy_cpu);
-
-       ret += get_property(obj, "hotplug,num_left_cond", DATA_TYPE_INT,
-                               false, (void *)&num_left_cond);
-       ret += get_property(obj, "hotplug,num_left_cond_freq", DATA_TYPE_INT,
-                               false, (void *)&num_left_cond_freq);
-       ret += get_property(obj, "hotplug,num_left_cond_nr_running", DATA_TYPE_INT,
-                               false, (void *)&num_left_cond_nr_running);
-       ret += get_property(obj, "hotplug,num_left_cond_busy_cpu", DATA_TYPE_INT,
-                               false, (void *)&num_left_cond_busy_cpu);
-
-       ret += get_property(obj, "hotplug,governor_timeout_ms", DATA_TYPE_INT,
-                               false, (void *)&governor_timeout_ms);
-
-       ret += get_property(obj, "memory,fault_around_bytes", DATA_TYPE_INT,
-                               false, (void *)&fault_around_bytes);
-
-       ret += get_property(obj, "thermal,cooling_device_state", DATA_TYPE_INT,
-                               false, (void *)&cooling_device_state);
-       ret += get_property(obj, "battery,charging_status", DATA_TYPE_INT,
-                               false, (void *)&charging_status);
-       ret += get_property(obj, "battery,charging_current_uA", DATA_TYPE_INT,
-                               false, (void *)&charging_current_uA);
+       struct config_property level_properties[] = {
+               { "level",                              DATA_TYPE_INT, true, (void *)&level },
+               { "dvfs,minimum_frequency_khz",         DATA_TYPE_INT, false, (void *)&target_level->limit_min_freq },
+               { "dvfs,maximum_frequency_khz",         DATA_TYPE_INT, false, (void *)&target_level->limit_max_freq },
+               { "hotplug,number_of_minimum_cpu",      DATA_TYPE_INT, false, (void *)&target_level->limit_min_cpu },
+               { "hotplug,number_of_maximum_cpu",      DATA_TYPE_INT, false, (void *)&target_level->limit_max_cpu },
+
+               { "hotplug,num_down_cond",              DATA_TYPE_INT, false, (void *)&target_level->num_down_cond },
+               { "hotplug,num_down_cond_freq",         DATA_TYPE_INT, false, (void *)&target_level->down_cond[0].freq },
+               { "hotplug,num_down_cond_nr_running",   DATA_TYPE_INT, false, (void *)&target_level->down_cond[0].nr_running },
+               { "hotplug,num_down_cond_busy_cpu",     DATA_TYPE_INT, false, (void *)&target_level->down_cond[0].busy_cpu },
+
+               { "hotplug,num_up_cond",                DATA_TYPE_INT, false, (void *)&target_level->num_up_cond },
+               { "hotplug,num_up_cond_freq",           DATA_TYPE_INT, false, (void *)&target_level->up_cond[0].freq },
+               { "hotplug,num_up_cond_nr_running",     DATA_TYPE_INT, false, (void *)&target_level->up_cond[0].nr_running },
+               { "hotplug,num_up_cond_busy_cpu",       DATA_TYPE_INT, false, (void *)&target_level->up_cond[0].busy_cpu },
+
+               { "hotplug,num_right_cond",             DATA_TYPE_INT, false, (void *)&target_level->num_right_cond },
+               { "hotplug,num_right_cond_freq",        DATA_TYPE_INT, false, (void *)&target_level->right_cond[0].freq },
+               { "hotplug,num_right_cond_nr_running",  DATA_TYPE_INT, false, (void *)&target_level->right_cond[0].nr_running },
+               { "hotplug,num_right_cond_busy_cpu",    DATA_TYPE_INT, false, (void *)&target_level->right_cond[0].busy_cpu },
+
+               { "hotplug,num_left_cond",              DATA_TYPE_INT, false, (void *)&target_level->num_left_cond },
+               { "hotplug,num_left_cond_freq",         DATA_TYPE_INT, false, (void *)&target_level->left_cond[0].freq },
+               { "hotplug,num_left_cond_nr_running",   DATA_TYPE_INT, false, (void *)&target_level->left_cond[0].nr_running },
+               { "hotplug,num_left_cond_busy_cpu",     DATA_TYPE_INT, false, (void *)&target_level->left_cond[0].busy_cpu },
+
+               { "hotplug,governor_timeout_sec",       DATA_TYPE_DOUBLE, false, (void *)&target_level->gov_timeout },
+
+               { "memory,fault_around_bytes",          DATA_TYPE_INT, false, (void *)&target_level->fault_around_bytes },
+
+               { "thermal,cooling_device_state",       DATA_TYPE_INT, false, (void *)&target_level->cooling_device_state },
+               { "battery,charging_status",            DATA_TYPE_INT, false, (void *)&target_level->charging_status },
+               { "battery,charging_current_uA",        DATA_TYPE_INT, false, (void *)&target_level->charging_current_uA },
+       };
+
+       ret += get_property_set(obj, level_properties, ARRAY_SIZE(level_properties));
 
        /* Check whether the mandatory properties are included or not */
        if (ret < 0) {
@@ -361,97 +299,9 @@ static int parse_level(struct pass_resource *res, json_object *obj,
        }
 
        /* Initialize config_data from property values of confiugartion file */
-
-       /*
-        * Properties for the following h/w resources:
-        * - PASS_RESOURCE_CPU_ID
-        * - PASS_RESOURCE_BUS_ID
-        * - PASS_RESOURCE_GPU_ID
-        */
-       if (minimum_frequency_khz >= 0)
-               target_level->limit_min_freq = minimum_frequency_khz;
-       if (maximum_frequency_khz >= 0)
-               target_level->limit_max_freq = maximum_frequency_khz;
-
-       /*
-        * Properties for the following h/w resources:
-        * - PASS_RESOURCE_CPU_ID
-        */
-       if (number_of_minimum_cpu >= 0)
-               target_level->limit_min_cpu = number_of_minimum_cpu;
-       if (number_of_maximum_cpu >= 0)
-               target_level->limit_max_cpu = number_of_maximum_cpu;
-
-       if (num_down_cond >= 0)
-               target_level->num_down_cond = num_down_cond;
-       if (num_down_cond_freq >= 0)
-               target_level->down_cond[0].freq = num_down_cond_freq;
-       if (num_down_cond_nr_running >= 0)
-               target_level->down_cond[0].nr_running = num_down_cond_nr_running;
-       if (num_down_cond_busy_cpu >= 0)
-               target_level->down_cond[0].busy_cpu = num_down_cond_busy_cpu;
-
-       if (num_up_cond >= 0)
-               target_level->num_up_cond = num_up_cond;
-       if (num_up_cond_freq >= 0)
-               target_level->up_cond[0].freq = num_up_cond_freq;
-       if (num_up_cond_nr_running >= 0)
-               target_level->up_cond[0].nr_running = num_up_cond_nr_running;
-       if (num_up_cond_busy_cpu >= 0)
-               target_level->up_cond[0].busy_cpu = num_up_cond_busy_cpu;
-
-       if (num_left_cond >= 0)
-               target_level->num_left_cond = num_left_cond;
-       if (num_left_cond_freq >= 0)
-               target_level->left_cond[0].freq = num_left_cond_freq;
-       if (num_left_cond_nr_running >= 0)
-               target_level->left_cond[0].nr_running = num_left_cond_nr_running;
-       if (num_left_cond_busy_cpu >= 0)
-               target_level->left_cond[0].busy_cpu = num_left_cond_busy_cpu;
-
-       if (num_right_cond >= 0)
-               target_level->num_right_cond = num_right_cond;
-       if (num_right_cond_freq >= 0)
-               target_level->right_cond[0].freq = num_right_cond_freq;
-       if (num_right_cond_nr_running >= 0)
-               target_level->right_cond[0].nr_running = num_right_cond_nr_running;
-       if (num_right_cond_busy_cpu >= 0)
-               target_level->right_cond[0].busy_cpu = num_right_cond_busy_cpu;
-
-       if (governor_timeout_ms >= 0) {
-               if (governor_timeout_ms < MIN_TIMEOUT_MS
-                       || governor_timeout_ms > MAX_TIMEOUT_MS)
-                       governor_timeout_ms = MIN_TIMEOUT_MS;
-               target_level->gov_timeout = governor_timeout_ms;
-       }
-
-       /*
-        * Properties for the following h/w resources:
-        * - PASS_RESOURCE_MEMORY_ID
-        */
-       if (fault_around_bytes >= 0)
-               target_level->fault_around_bytes = fault_around_bytes;
-
-       /*
-        * Properties for the following h/w resources:
-        * - PASS_RESOURCE_CPU_ID
-        * - PASS_RESOURCE_BUS_ID
-        * - PASS_RESOURCE_GPU_ID
-        * - PASS_RESOURCE_BATTERY_ID
-        * - PASS_RESOURCE_NONSTANDARD_ID
-        */
-       if (cooling_device_state >= 0)
-               target_level->cooling_device_state = cooling_device_state;
-
-       /*
-        * Properties for the following h/w resources:
-        * - PASS_RESOURCE_BATTERY_ID
-        * - PASS_RESOURCE_NONSTANDARD_ID
-        */
-       if (charging_status >= 0)
-               target_level->charging_status = charging_status;
-       if (charging_current_uA >= 0)
-               target_level->charging_current_uA = charging_current_uA;
+       if (target_level->gov_timeout < MIN_TIMEOUT_MS
+               || target_level->gov_timeout > MAX_TIMEOUT_MS)
+               target_level->gov_timeout = MIN_TIMEOUT_MS;
 
        return 0;
 }
@@ -464,13 +314,19 @@ static int parse_level(struct pass_resource *res, json_object *obj,
  */
 static int parse_pmqos(struct pass_resource *res, json_object *obj)
 {
-       int pmqos_support;
        json_object *pmqos_scenario_list = NULL;
        int num_scenarios = 0, ret = 0, i;
 
+       if (!res || !obj)
+               return -EINVAL;
+
        /* Get property values */
-       ret = get_property(obj, "pmqos_support", DATA_TYPE_INT,
-                               true, (void *)&pmqos_support);
+       struct config_property pmqos_scenario_properties[] = {
+               { "pmqos_support", DATA_TYPE_INT, true, (void *)&res->pmqos.state },
+       };
+
+       ret += get_property_set(obj, pmqos_scenario_properties,
+                               ARRAY_SIZE(pmqos_scenario_properties));
        if (json_object_object_get_ex(obj, "pmqos_scenario_list",
                                &pmqos_scenario_list))
                num_scenarios = json_object_array_length(pmqos_scenario_list);
@@ -483,11 +339,10 @@ static int parse_pmqos(struct pass_resource *res, json_object *obj)
        }
 
        /* Check the validation of property value */
-       if (pmqos_support <= 0)
+       if (res->pmqos.state <= 0)
                return 0;
 
        /* Initialize config_data from property values of confiugartion file */
-       res->pmqos.state = pmqos_support;
        res->pmqos.scenarios = calloc(num_scenarios,
                                sizeof(struct pass_scenario));
        if (!res->pmqos.scenarios) {
@@ -520,16 +375,20 @@ static int parse_pmqos(struct pass_resource *res, json_object *obj)
  */
 static int parse_thermal(struct pass_resource *res, json_object *obj)
 {
-       int thermal_support;
-       int thermal_timer_interval_ms;
        json_object *thermal_scenario_list = NULL;
        int num_scenarios = 0, ret = 0, i;
 
+       if (!res || !obj)
+               return -EINVAL;
+
        /* Get property values */
-       ret += get_property(obj, "thermal_support",DATA_TYPE_INT,
-                               true, (void *)&thermal_support);
-       ret += get_property(obj, "thermal_timer_interval_ms", DATA_TYPE_INT,
-                               false, (void *)&thermal_timer_interval_ms);
+       struct config_property thermal_properties[] = {
+               { "thermal_support",            DATA_TYPE_INT, true, (void *)&res->thermal.state },
+               { "thermal_timer_interval_ms",  DATA_TYPE_INT, false, (void *)&res->thermal.timer_interval },
+       };
+
+       ret += get_property_set(obj, thermal_properties, ARRAY_SIZE(thermal_properties));
+
        if (json_object_object_get_ex(obj, "thermal_scenario_list",
                                &thermal_scenario_list))
                num_scenarios = json_object_array_length(thermal_scenario_list);
@@ -542,13 +401,10 @@ static int parse_thermal(struct pass_resource *res, json_object *obj)
        }
 
        /* Check the validation of property value */
-       if (thermal_support <= 0)
+       if (res->thermal.state <= 0)
                return 0;
 
        /* Initialize config_data from property values of confiugartion file */
-       res->thermal.state = thermal_support;
-       res->thermal.timer_interval = thermal_timer_interval_ms;
-
        res->thermal.scenarios = calloc(num_scenarios,
                                sizeof(struct pass_scenario));
        if (!res->thermal.scenarios) {
@@ -581,40 +437,32 @@ static int parse_thermal(struct pass_resource *res, json_object *obj)
  */
 static int parse_cpuhp(struct pass_resource *res, json_object *obj)
 {
-       int cpuhp_support;
-       int cpuhp_governor;
-       int cpuhp_timer_interval_ms;
        int cpuhp_min_level;
        int cpuhp_max_level;
        int cpuhp_init_level;
-       int cpuhp_cpu_threshold;
-       int cpuhp_up_threshold;
-       int cpuhp_down_threshold;
        json_object *cpuhp_level_list = NULL;
-       int cpuhp_num_levels = 0, ret = 0, i;
+       int ret = 0, i;
+
+       if (!res || !obj)
+               return -EINVAL;
 
        /* Get property values */
-       ret += get_property(obj, "cpuhp_support", DATA_TYPE_INT,
-                               true, (void *)&cpuhp_support);
-       ret += get_property(obj, "cpuhp_governor", DATA_TYPE_INT,
-                               false, (void *)&cpuhp_governor);
-       ret += get_property(obj, "cpuhp_timer_interval_ms", DATA_TYPE_INT,
-                               false, (void *)&cpuhp_timer_interval_ms);
-       ret += get_property(obj, "cpuhp_min_level", DATA_TYPE_INT,
-                               false, (void *)&cpuhp_min_level);
-       ret += get_property(obj, "cpuhp_max_level", DATA_TYPE_INT,
-                               false, (void *)&cpuhp_max_level);
-       ret += get_property(obj, "cpuhp_init_level", DATA_TYPE_INT,
-                               false, (void *)&cpuhp_init_level);
-       ret += get_property(obj, "cpuhp_cpu_threshold", DATA_TYPE_INT,
-                               false, (void *)&cpuhp_cpu_threshold);
-       ret += get_property(obj, "cpuhp_up_threshold", DATA_TYPE_INT,
-                               false, (void *)&cpuhp_up_threshold);
-       ret += get_property(obj, "cpuhp_down_threshold", DATA_TYPE_INT,
-                               false, (void *)&cpuhp_down_threshold);
+       struct config_property cpuhp_properties[] = {
+               { "cpuhp_support",              DATA_TYPE_INT, true, (void *)&res->config_data.state },
+               { "cpuhp_governor",             DATA_TYPE_INT, false, (void *)&res->config_data.gov_type },
+               { "cpuhp_timer_interval_ms",    DATA_TYPE_INT, false, (void *)&res->config_data.gov_timeout },
+               { "cpuhp_min_level",            DATA_TYPE_INT, false, (void *)&cpuhp_min_level },
+               { "cpuhp_max_level",            DATA_TYPE_INT, false, (void *)&cpuhp_max_level },
+               { "cpuhp_init_level",           DATA_TYPE_INT, false, (void *)&cpuhp_init_level },
+               { "cpuhp_cpu_threshold",        DATA_TYPE_INT, false, (void *)&res->cpuhp.pass_cpu_threshold },
+               { "cpuhp_up_threshold",         DATA_TYPE_INT, false, (void *)&res->cpuhp.up_threshold },
+               { "cpuhp_down_threshold",       DATA_TYPE_INT, false, (void *)&res->cpuhp.down_threshold },
+       };
+
+       ret += get_property_set(obj, cpuhp_properties, ARRAY_SIZE(cpuhp_properties));
 
        if (json_object_object_get_ex(obj, "cpuhp_level_list", &cpuhp_level_list))
-               cpuhp_num_levels = json_object_array_length(cpuhp_level_list);
+               res->config_data.num_levels = json_object_array_length(cpuhp_level_list);
 
        /* Check whether the mandatory properties are included or not */
        if (ret < 0) {
@@ -624,36 +472,25 @@ static int parse_cpuhp(struct pass_resource *res, json_object *obj)
        }
 
        /* Check the validation of property value */
-       if (cpuhp_support <= 0) {
+       if (res->config_data.state <= 0) {
                return 0;
        } else if (!cpuhp_level_list) {
                _E("Failed to get 'cpuhp_level_list' property\n");
                return -EINVAL;
-       } else if (cpuhp_num_levels <= 0) {
+       } else if (res->config_data.num_levels <= 0) {
                _E("Must need to add at least one level in 'cpuhp_level_list' section\n");
                return -EINVAL;
        }
 
        /* Initialize config_data from property values of confiugartion file */
-       /* - core */
-       res->config_data.state = cpuhp_support;
-       res->config_data.gov_type = cpuhp_governor;
-       res->config_data.gov_timeout = cpuhp_timer_interval_ms;
-       if (res->config_data.gov_timeout < MIN_TIMEOUT_MS
-               || res->config_data.gov_timeout > MAX_TIMEOUT_MS)
-               res->config_data.gov_timeout = MIN_TIMEOUT_MS;
-
-       /* - properties for rescon module */
        res->rescon.min_level = cpuhp_min_level;
        res->rescon.max_level = cpuhp_max_level;
        res->rescon.init_level = cpuhp_init_level;
 
-       /* - properties for cpuhp module */
-       res->cpuhp.pass_cpu_threshold = cpuhp_cpu_threshold;
-       res->cpuhp.up_threshold = cpuhp_up_threshold;
-       res->cpuhp.down_threshold = cpuhp_down_threshold;
+       if (res->config_data.gov_timeout < MIN_TIMEOUT_MS
+               || res->config_data.gov_timeout > MAX_TIMEOUT_MS)
+               res->config_data.gov_timeout = MIN_TIMEOUT_MS;
 
-       res->config_data.num_levels = cpuhp_num_levels;
        res->config_data.levels = calloc(res->config_data.num_levels,
                                        sizeof(struct pass_level));
        if (!res->config_data.levels) {
@@ -688,15 +525,21 @@ static int parse_header(struct pass_resource *res, json_object *obj)
        bool support;
        int init_level;
        json_object *level_list = NULL;
-       int num_levels = 0, ret = 0, i;
+       int ret = 0, i;
+
+       if (!res || !obj)
+               return -EINVAL;
 
        /* Get property values */
-       ret += get_property(obj, "support", DATA_TYPE_BOOLEAN,
-                               true, (void *)&support);
-       ret += get_property(obj, "init_level", DATA_TYPE_INT,
-                               true, (void *)&init_level);
+       struct config_property header_properties[] = {
+               { "support",    DATA_TYPE_BOOLEAN, true, (void *)&support },
+               { "init_level", DATA_TYPE_INT, true, (void *)&init_level },
+       };
+
+       ret += get_property_set(obj, header_properties, ARRAY_SIZE(header_properties));
+
        if (json_object_object_get_ex(obj, "level_list", &level_list))
-               num_levels = json_object_array_length(level_list);
+               res->config_data.num_scenario_levels = json_object_array_length(level_list);
 
        /* Check whether the mandatory properties are included or not */
        if (ret < 0) {
@@ -709,21 +552,21 @@ static int parse_header(struct pass_resource *res, json_object *obj)
        if (!level_list) {
                _E("Failed to get 'level_list' property\n");
                return -EINVAL;
-       } else if (num_levels <= 0) {
+       } else if (res->config_data.num_scenario_levels <= 0) {
                _E("Must need to add at least one level in 'level_list' section\n");
                return -EINVAL;
-       } else if (init_level > num_levels) {
+       } else if (init_level > res->config_data.num_scenario_levels) {
                _E("Invalid 'init_level' in core section\n");
                return -EINVAL;
        }
 
+       /* Initialize config_data from property values of confiugartion file */
+       res->config_data.state = !!support;
+
        if (init_level < 0)
                init_level = 0;
-
-       /* Initialize config_data from property values of confiugartion file */
-       res->config_data.state = support;
-       res->config_data.num_scenario_levels = num_levels;
        res->rescon.init_scenario_level = init_level;
+
        res->config_data.scenario_levels = calloc(
                                        res->config_data.num_scenario_levels,
                                        sizeof(struct pass_level));
@@ -799,37 +642,27 @@ static int parse_resource_data(struct pass *pass, int id, json_object *obj)
        struct pass_resource_config_data *config_data
                                = &(pass->res[id].config_data);
        char device_type[BUFF_MAX] = {};
-       char device_name[BUFF_MAX] = {};
-       char thermal_device_name[BUFF_MAX] = {};
-       char cooling_device_name[BUFF_MAX] = {};
-       char device_config_path[BUFF_MAX] = {};
-       char cpu_load_path[BUFF_MAX] = {};
-       int number_of_cpus;
-       int first_cpu;
-       int thermal_priority;
        int ret = 0;
 
+       if(!pass || id < 0 || !obj)
+               return -EINVAL;
+
        /* Get property values */
-       ret += get_property(obj, "device_type", DATA_TYPE_STRING,
-                               true, (void *)device_type);
-       ret += get_property(obj, "device_name", DATA_TYPE_STRING,
-                               true, (void *)device_name);
-       ret += get_property(obj, "device_config_path", DATA_TYPE_STRING,
-                               true, (void *)device_config_path);
-
-       ret += get_property(obj, "thermal_device_name", DATA_TYPE_STRING,
-                               false, (void *)thermal_device_name);
-       ret += get_property(obj, "cooling_device_name", DATA_TYPE_STRING,
-                               false, (void *)cooling_device_name);
-       ret += get_property(obj, "thermal_priority", DATA_TYPE_INT,
-                               false, (void *)&thermal_priority);
-
-       ret += get_property(obj, "cpu,cpu_load_path", DATA_TYPE_STRING,
-                               false, (void *)cpu_load_path);
-       ret += get_property(obj, "cpu,number_of_cpus", DATA_TYPE_INT,
-                               false, (void *)&number_of_cpus);
-       ret += get_property(obj, "cpu,first_cpu", DATA_TYPE_INT,
-                               false, (void *)&first_cpu);
+       struct config_property resource_properties[] = {
+               { "device_type",        DATA_TYPE_STRING, true, (void *)device_type },
+               { "device_name",        DATA_TYPE_STRING, true, (void *)config_data->res_name },
+               { "device_config_path", DATA_TYPE_STRING, true, (void *)config_data->path_conf_file },
+
+               { "thermal_device_name", DATA_TYPE_STRING, false, (void *)config_data->res_thermal_name },
+               { "cooling_device_name", DATA_TYPE_STRING, false, (void *)config_data->res_cooling_name },
+               { "thermal_priority",   DATA_TYPE_INT, false, (void *)&config_data->res_thermal_priority },
+
+               { "cpu,cpu_load_path",  DATA_TYPE_STRING, false, (void *)config_data->path_load_table },
+               { "cpu,number_of_cpus", DATA_TYPE_INT, false, (void *)&config_data->num_cpus },
+               { "cpu,first_cpu",      DATA_TYPE_INT, false, (void *)&config_data->cpu },
+       };
+
+       ret += get_property_set(obj, resource_properties, ARRAY_SIZE(resource_properties));
 
        /* Check whether the mandatory properties are included or not */
        if (ret < 0) {
@@ -838,14 +671,14 @@ static int parse_resource_data(struct pass *pass, int id, json_object *obj)
        }
 
        /* Check the validation of property value */
-       if (thermal_priority < 0)
-               thermal_priority = INT_MAX;
+       if (config_data->res_thermal_priority < 0)
+               config_data->res_thermal_priority = INT_MAX;
 
        switch (config_data->res_type) {
        case PASS_RESOURCE_CPU_ID:
-               if (number_of_cpus < 0 || first_cpu < 0) {
+               if (config_data->num_cpus < 0 || config_data->cpu < 0) {
                        _E("Invalid 'number_of_cpus'(%d) or 'first_cpu'(%d) property\n",
-                               number_of_cpus, first_cpu);
+                               config_data->num_cpus, config_data->cpu);
                        return -EINVAL;
                }
                break;
@@ -853,7 +686,8 @@ static int parse_resource_data(struct pass *pass, int id, json_object *obj)
                break;
        }
 
-       number_of_cpus = number_of_cpus < 0 ? 0 : number_of_cpus;
+       if (config_data->num_cpus < 0)
+               config_data->num_cpus = 0;
 
        /* Initialize config_data from property values of confiugartion file */
        if (!strncmp(device_type, PASS_RESOURCE_CPU_NAME, strlen(device_type)))
@@ -879,16 +713,6 @@ static int parse_resource_data(struct pass *pass, int id, json_object *obj)
                return -EINVAL;
        }
 
-       snprintf(config_data->res_name, BUFF_MAX, "%s", device_name);
-       snprintf(config_data->res_thermal_name, BUFF_MAX, "%s", thermal_device_name);
-       snprintf(config_data->res_cooling_name, BUFF_MAX, "%s", cooling_device_name);
-       config_data->res_thermal_priority = thermal_priority;
-
-       snprintf(config_data->path_conf_file, BUFF_MAX, "%s", device_config_path);
-       snprintf(config_data->path_load_table, BUFF_MAX, "%s", cpu_load_path);
-       config_data->num_cpus = number_of_cpus;
-       config_data->cpu = first_cpu;
-
        return 0;
 }
 
@@ -906,11 +730,17 @@ static int parse_resource(struct pass *pass, json_object *obj)
        char board_name_path[BUFF_MAX] = {};
        int num_resources = 0, ret = 0, i;
 
+       if (!pass || !obj)
+               return -EINVAL;
+
        /* Get property values */
-       ret += get_property(obj, "board_name", DATA_TYPE_STRING,
-                               false, (void *)board_name);
-       ret += get_property(obj, "board_name_path", DATA_TYPE_STRING,
-                               false, (void *)board_name_path);
+       struct config_property board_properties[] = {
+               { "board_name",         DATA_TYPE_STRING, false, (void *)board_name },
+               { "board_name_path",    DATA_TYPE_STRING, false, (void *)board_name_path },
+       };
+
+       ret += get_property_set(obj, board_properties, ARRAY_SIZE(board_properties));
+
        if (json_object_object_get_ex(obj, "device_list", &device_list))
                num_resources = json_object_array_length(device_list);
 
index 845f441..6c16c4f 100644 (file)
@@ -55,14 +55,15 @@ static int pmqos_parse_scenario(json_object *obj, struct scenario *scenario)
        int max_duration_ms;
        bool support;
        int ret = 0;
+       struct config_property pmqos_scenario_properties[] = {
+               { "name", DATA_TYPE_STRING, true, (void *)name },
+               { "support", DATA_TYPE_BOOLEAN, false, (void *)&support },
+               { "max_duration_ms", DATA_TYPE_INT, false, (void *)&max_duration_ms },
+       };
 
        /* Get property values */
-       ret += get_property(obj, "name", DATA_TYPE_STRING, true,
-                               (void *)name);
-       ret += get_property(obj, "support", DATA_TYPE_BOOLEAN, false,
-                               (void *)&support);
-       ret += get_property(obj, "max_duration_ms", DATA_TYPE_INT, false,
-                               (void *)&max_duration_ms);
+       ret += get_property_set(obj, pmqos_scenario_properties,
+                               ARRAY_SIZE(pmqos_scenario_properties));
 
        /* Check whether the mandatory properties are included or not */
        if (ret < 0) {
@@ -90,11 +91,14 @@ static int pmqos_load_config(json_object *obj, struct pmqos_scenario *scenarios)
 {
        bool pmqos_support;
        json_object *pmqos_scenario_list = NULL;
-       int num_scenarios = 0, ret, i;
+       int num_scenarios = 0, ret = 0, i;
+       struct config_property pmqos_header_properties[] = {
+               {"pmqos_support", DATA_TYPE_BOOLEAN, false, (void *)&pmqos_support },
+       };
 
        /* Get property values */
-       ret = get_property(obj, "pmqos_support", DATA_TYPE_BOOLEAN, false,
-                               (void *)&pmqos_support);
+       ret += get_property_set(obj, pmqos_header_properties,
+                               ARRAY_SIZE(pmqos_header_properties));
        if (json_object_object_get_ex(obj, "pmqos_scenario_list",
                                &pmqos_scenario_list))
                num_scenarios = json_object_array_length(pmqos_scenario_list);
index 09edc5b..781a31d 100644 (file)
@@ -52,12 +52,14 @@ static int thermal_parse_scenario(json_object *obj, struct scenario *scenario)
        char name[BUFF_MAX] = {};
        bool support;
        int ret = 0;
+       struct config_property thermal_scenario_properties[] = {
+               { "name", DATA_TYPE_STRING, true, (void *)name },
+               {"support", DATA_TYPE_BOOLEAN, false, (void *)&support },
+       };
 
        /* Get property values */
-       ret += get_property(obj, "name", DATA_TYPE_STRING, true,
-                               (void *)name);
-       ret += get_property(obj, "support", DATA_TYPE_BOOLEAN, false,
-                               (void *)&support);
+       ret += get_property_set(obj, thermal_scenario_properties,
+                               ARRAY_SIZE(thermal_scenario_properties));
 
        /* Check whether the mandatory properties are included or not */
        if (ret < 0) {
@@ -84,10 +86,13 @@ static int thermal_load_config(json_object *obj, struct thermal_scenario *scenar
        bool thermal_support;
        json_object *thermal_scenario_list = NULL;
        int num_scenarios = 0, ret = 0, i;
+       struct config_property thermal_header_properties[] = {
+               {"thermal_support", DATA_TYPE_BOOLEAN, false, (void *)&thermal_support },
+       };
 
        /* Get property values */
-       ret = get_property(obj, "thermal_support", DATA_TYPE_BOOLEAN, false,
-                               (void *)&thermal_support);
+       ret += get_property_set(obj, thermal_header_properties,
+                               ARRAY_SIZE(thermal_header_properties));
        if (json_object_object_get_ex(obj, "thermal_scenario_list",
                                &thermal_scenario_list))
                num_scenarios = json_object_array_length(thermal_scenario_list);
index 055c169..d58c2e0 100644 (file)
@@ -16,7 +16,6 @@
  * limitations under the License.
  */
 
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdbool.h>
@@ -193,3 +192,17 @@ int get_property(json_object *obj, const char *key,
 
        return ret;
 }
+
+int get_property_set(json_object *obj, struct config_property *properties,
+                       unsigned int num_properties)
+{
+       int i, ret = 0;
+
+       if (!obj || !properties || num_properties >= INT_MAX)
+               return 0;
+
+       for (i = 0; i < num_properties; i++)
+               ret += get_property(obj, properties[i].name, properties[i].type,
+                               properties[i].mandatory, properties[i].value);
+       return ret;
+}