#define BUFF_MAX 255
-static int sys_read_buf(char *file, char *buf)
+static int sys_read_buf(const char *file, char *buf)
{
int fd;
int r;
return ret;
}
-int sys_get_str(char *fname, char *str)
+int sys_get_str(const char *fname, char *str)
{
char buf[BUFF_MAX] = {0};
return -1;
}
-int sys_strtol(char *str)
+int sys_strtol(const char *str)
{
long value;
return (int)value;
}
+
+const char *get_string_from_object(json_object *obj, const char *key)
+{
+ json_object *tmp = NULL;
+
+ if (!json_object_object_get_ex(obj, key, &tmp))
+ return NULL;
+
+ return json_object_get_string(tmp);
+}
+
+const int get_int_from_object(json_object *obj, const char *key)
+{
+ json_object *tmp = NULL;
+
+ if (!json_object_object_get_ex(obj, key, &tmp))
+ return -EINVAL;
+
+ return json_object_get_int(tmp);
+}
+
+const double get_double_from_object(json_object *obj, const char *key)
+{
+ json_object *tmp = NULL;
+
+ if (!json_object_object_get_ex(obj, key, &tmp))
+ return -EINVAL;
+
+ return json_object_get_double(tmp);
+}
+
+const int get_boolean_from_object(json_object *obj, const char *key)
+{
+ json_object *tmp = NULL;
+
+ if (!json_object_object_get_ex(obj, key, &tmp))
+ return -EINVAL;
+
+ return (int)json_object_get_boolean(tmp);
+}
#include <errno.h>
#include <string.h>
+#include <gio/gio.h>
+#include <glib-object.h>
+
#include <pass/log.h>
#include <pass/common.h>
-#include <pass/config-parser.h>
#include "pmqos.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 PMQoS(Power Management Quality of Service).
- * @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 pmqos_parse_scenario(struct parse_result *result, void *user_data, unsigned int index)
+static int pmqos_parse_scenario(json_object *obj, struct scenario *scenario)
{
- struct pmqos_scenario *scenarios = (struct pmqos_scenario *)user_data;
-
- assert(result);
- assert(result->section && result->name && result->value);
-
- /* Parse 'PassScenario' section */
- if (MATCH(result->section, "PassScenario")) {
- if (MATCH(result->name, "scenario_support"))
- scenarios->support = is_supported(result->value);
- else if (MATCH(result->name, "scenario_num")) {
- 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 memory for scenario");
- return -errno;
- }
-
- scenarios->num = num;
- }
- }
+ const char *name;
+ int max_duration_ms;
+ int support;
+
+ /* Get property values */
+ name = get_string_from_object(obj, "name");
+ max_duration_ms = get_int_from_object(obj, "max_duration_ms");
+ support = get_boolean_from_object(obj, "support");
+
+ /* Check the mandatory property values are valid or not */
+ if (!name) {
+ _E("Failed to get 'name' property of scenario section\n");
+ return -EINVAL;
}
- /* Do not support pmqos scenario */
- if (!scenarios->support)
- return 0;
-
- /* Do not have pmqos scenario */
- if (!scenarios->num)
- return 0;
-
- /* No item to parse */
- if (index >= scenarios->num)
- return 0;
-
- /* 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);
- else if (MATCH(result->name, "max_duration_ms")) {
- int max_duration_ms = sys_strtol(result->value);
-
- if (max_duration_ms < 0) {
- _E("failed to parse max_duration_ms property (%d)\n",
- max_duration_ms);
- return -EINVAL;
- }
-
- /*
- * If maximum duration is zero, it measn that this scenario is
- * mode without any maximum duration.
- */
- scenarios->list[index].max_duration_ms = max_duration_ms;
- }
+ /* Initialize config_data from property values of confiugartion file */
+ snprintf(scenario->name, strlen(name) + 1, "%s", name);
+ scenario->max_duration_ms = (max_duration_ms < 0) ? 0 : max_duration_ms;
+ scenario->support = (support < 0) ? 1 : support;
return 0;
}
* @brief Parse configuration to get information of supported scenarios
* for PMQoS(Power Management Quality of Service) such as
* AppLaunch, UltraPowerSaving.
- * @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 pmqos_load_config(struct parse_result *result, void *user_data)
+static int pmqos_load_config(json_object *obj, struct pmqos_scenario *scenarios)
{
- struct pmqos_scenario *scenarios = (struct pmqos_scenario *)user_data;
- char name[NAME_MAX];
- int ret;
- static int index;
-
- if (!result)
- return 0;
-
- if (!result->section || !result->name || !result->value)
+ int pmqos_support;
+ json_object *pmqos_scenario_list = NULL;
+ int num_scenarios = 0, ret, i;
+
+ /* Get property values */
+ pmqos_support = get_boolean_from_object(obj, "pmqos_support");
+ if (json_object_object_get_ex(obj, "pmqos_scenario_list",
+ &pmqos_scenario_list))
+ num_scenarios = json_object_array_length(pmqos_scenario_list);
+
+ /* Check the mandatory property values are valid or not */
+ if (pmqos_support <= 0 || num_scenarios <= 0) {
+ _I("Disable PMQOS module\n");
return 0;
+ }
- /* Parsing 'PassScenario' section */
- if (MATCH(result->section, "PassScenario")) {
- ret = pmqos_parse_scenario(result, user_data, -1);
- if (ret < 0) {
- _E("failed to parse [PassScenario] section : %d", ret);
- return ret;
- }
- goto out;
+ /* Initialize config_data from property values of confiugartion file */
+ scenarios->support = pmqos_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 'Scenario' section */
- for (index = 0; index < scenarios->num; ++index) {
- snprintf(name, sizeof(name), "Scenario%d", index);
-
- if (MATCH(result->section, name)) {
- ret = pmqos_parse_scenario(result, user_data, index);
- if (ret < 0) {
- _E("failed to parse [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(pmqos_scenario_list, i);
+ struct scenario *scenario = &(scenarios->list[i]);
+
+ ret = pmqos_parse_scenario(scenario_obj, scenario);
+ if (ret < 0) {
+ _E("cannot parse 'pmqos.scenario%d' section\n", i);
+ goto err;
}
}
-out:
return 0;
+err:
+ free(scenarios->list);
+ scenarios->list = NULL;
+ scenarios->num = 0;
+
+ return ret;
}
/**
int pmqos_get_scenario(const char *path, struct pmqos_scenario *scenarios)
{
int ret;
+ json_object *obj = NULL;
/* Initialize the variables before parsing the pass-pmqos.conf */
scenarios->num = 0;
- /* get configuration file */
- ret = config_parse(path, pmqos_load_config, scenarios);
- if (ret < 0) {
- pmqos_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 = pmqos_load_config(obj, scenarios);
+ if (ret < 0)
+ pmqos_put_scenario(scenarios);
+
+ json_object_put(obj);
+
+ return ret;
}