#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;
}
/**
* @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;
}
/**
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;
}