pmqos: Clean-up code and remove unnecessary function 61/174361/5
authorChanwoo Choi <cw00.choi@samsung.com>
Wed, 28 Mar 2018 12:31:59 +0000 (21:31 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Thu, 5 Apr 2018 09:49:26 +0000 (18:49 +0900)
Clean-up code and just remove unnecessary get_methods_from_conf().

Change-Id: I158603675172f32d01151b7269a73ba69d002258
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
src/pmqos/pmqos.c

index b2a04d6b549c36b6553de8e16425d85abba31899..6a6a89cc1122c776226c99962483ad67526a8bc9 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * PASS (Power Aware System Service)
  *
- * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2012 - 2018 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the License);
  * you may not use this file except in compliance with the License.
@@ -43,8 +43,7 @@
 #define DBUS_PMQOS_PATH                                "/Org/Tizen/System/Pass/Pmqos"
 
 static SystemPassPmqos *g_gdbus_instance = NULL;
-static struct pmqos_scenario *g_pmqos_scenarios = NULL;
-static bool g_initialized = false;
+static struct pmqos_scenario *g_pmqos = NULL;
 
 struct pmqos_cpu {
        char name[NAME_MAX];
@@ -89,7 +88,7 @@ static gboolean dbus_cb_pmqos_start(SystemPassPmqos *obj,
        int ret = 0;
        int booting_done = 1;
 
-       if (g_initialized)
+       if (g_pmqos)
                _I("PASS PMQoS is already started\n");
        else
                ret = pmqos_init_done(&booting_done, NULL);
@@ -107,7 +106,7 @@ static gboolean dbus_cb_pmqos_start(SystemPassPmqos *obj,
 static gboolean dbus_cb_pmqos_stop(SystemPassPmqos *obj,
                GDBusMethodInvocation *invoc, gpointer user_data)
 {
-       if (!g_initialized)
+       if (!g_pmqos)
                _I("PASS PMQoS is already stopped\n");
        else
                pmqos_free();
@@ -128,17 +127,17 @@ static gboolean dbus_cb_pmqos_legacy_scenario(SystemPassPmqos *obj,
        int i, ret = 0;
        gboolean ret_out = TRUE;
 
-       if (!g_initialized) {
+       if (!g_pmqos) {
                _E("PASS PMQoS is stopped\n");
                ret_out = FALSE;
                goto out;
        } else {
                name_from = g_dbus_method_invocation_get_method_name(invoc);
                ret = -ENOTSUP;
-               if (g_pmqos_scenarios != NULL) {
-                       for (i = 0; i < g_pmqos_scenarios->num; i++) {
-                               name = g_pmqos_scenarios->list[i].name;
-                               support = g_pmqos_scenarios->list[i].support;
+               if (g_pmqos) {
+                       for (i = 0; i < g_pmqos->num; i++) {
+                               name = g_pmqos->list[i].name;
+                               support = g_pmqos->list[i].support;
                                if (!strncmp(name, name_from, strlen(name)) &&
                                                support) {
                                        ret = 0;
@@ -224,9 +223,9 @@ static int set_pmqos(const char *name, int val)
        char scenario[100];
 
        if (val)
-               _D("Set pm scenario : [Lock  ]%s", name);
+               _D("Set pm scenario : Lock   \'%s\'", name);
        else
-               _D("Set pm scenario : [Unlock]%s", name);
+               _D("Set pm scenario : Unlock \'%s\'", name);
        snprintf(scenario, sizeof(scenario), "%s%s", name, (val ? "Lock" : "Unlock"));
        device_notify(DEVICE_NOTIFIER_PMQOS, (void *)scenario);
 
@@ -392,79 +391,56 @@ err:
        return ret;
 }
 
-static int get_methods_from_conf(const char *path,
-               struct pmqos_scenario **pmqos_scenarios)
+static int pmqos_init_done(void *data, void *user_data)
 {
-       struct pmqos_scenario *scenarios;
-       int i, ret, count = 0;
+       int *booting_done;
+       int ret;
+       int i;
+
+       /*
+        * As a callback function for the booting-done event, it is notified
+        * with the result of the booting_finished() function by using the
+        * first argument, void *data. When it is successfully booted up, an int
+        * value, 1 is passed with the first argument.
+        */
+       booting_done = (int *)data;
+       if (!(*booting_done))
+               return -EINVAL;
 
-       scenarios = malloc(sizeof(*scenarios));
-       if (scenarios == NULL)
+       if (g_pmqos)
+               return 0;
+
+       g_pmqos = calloc(1, sizeof(*g_pmqos));
+       if (!g_pmqos)
                return -ENOMEM;
 
-       /* get pmqos table from conf */
-       ret = pmqos_get_scenario(path, scenarios);
+       ret = pmqos_get_scenario(PMQOS_CONF_PATH, g_pmqos);
        if (ret < 0) {
-               pmqos_put_scenario(scenarios);
+               _E("failed to get PMQoS scenario\n");
                return ret;
        }
 
-       /* if do not support scenario */
-       if (!scenarios->support)
-               return 0;
-
-       /* if do not have scenarios */
-       if (scenarios->num <= 0)
-               return 0;
+       if (!g_pmqos->support || g_pmqos->num <= 0) {
+               ret = pmqos_put_scenario(g_pmqos);
+               if (ret < 0)
+                       _E("failed to put PMQoS scenario\n");
+               return ret;
+       }
 
-       /* set edbus_methods structure */
-       for (i = 0; i < scenarios->num; ++i) {
-               /* if this scenario does not support */
-               if (!scenarios->list[i].support)
-                       continue;
+       for (i = 0; i < g_pmqos->num; ++i)
+               if (g_pmqos->list[i].support)
+                       _I("Support \'%s\' scenario", g_pmqos->list[i].name);
 
-               count++;
-               _I("support [%s] scenario", scenarios->list[i].name);
-       }
        /*
         * Set maximum timeout for the sceanrio by using the parsed data from
         * pass-pmqos.conf. But, if there is no setting from pass-pmqos.conf
         * pmqos uses the default timeout value (3000 millisecond).
         */
-       if (scenarios->max_timeout_ms)
-               unlock_max_timeout_ms = scenarios->max_timeout_ms;
+       if (g_pmqos->max_timeout_ms)
+               unlock_max_timeout_ms = g_pmqos->max_timeout_ms;
        else
                unlock_max_timeout_ms = DEFAULT_PMQOS_TIMER;
 
-       ret = count;
-       *pmqos_scenarios = scenarios;
-
-       return ret;
-}
-
-static int pmqos_init_done(void *data, void *user_data)
-{
-       int *booting_done;
-       int size;
-
-       /*
-        * As a callback function for the booting-done event, it is notified
-        * with the result of the booting_finished() function by using the
-        * first argument, void *data. When it is successfully booted up, an int
-        * value, 1 is passed with the first argument.
-        */
-       booting_done = (int *)data;
-       if (!(*booting_done))
-               return -EINVAL;
-
-       size = get_methods_from_conf(PMQOS_CONF_PATH, &g_pmqos_scenarios);
-       if (size < 0) {
-               _E("failed to load configuration file(%s)", PMQOS_CONF_PATH);
-               return -EINVAL;
-       }
-
-       g_initialized = true;
-
        return 0;
 }
 
@@ -476,26 +452,28 @@ static void pmqos_init(void *data)
 
 static void pmqos_free(void)
 {
-       /* Assign 0 to static variables */
+       int ret;
+
        memset(&unlock_timer_start_st, 0, sizeof(struct timespec));
        memset(&unlock_timer_end_st, 0, sizeof(struct timespec));
        memset(&unlock_timer_owner, 0, sizeof(struct pmqos_cpu));
 
-       /* Clean up pmqos_head */
        if (pmqos_head) {
                g_list_free_full(pmqos_head, free);
                pmqos_head = NULL;
        }
 
-       free(g_pmqos_scenarios);
-       g_pmqos_scenarios = NULL;
+       ret = pmqos_put_scenario(g_pmqos);
+       if (ret < 0)
+               _E("failed to put PMQoS scenario\n");
+
+       free(g_pmqos);
+       g_pmqos = NULL;
 
        if (g_unlock_timeout_id) {
                g_source_remove(g_unlock_timeout_id);
                g_unlock_timeout_id = 0;
        }
-
-       g_initialized = false;
 }
 
 static void pmqos_exit(void *data)
@@ -520,7 +498,7 @@ static int pmqos_probe(void *data)
        int ret = 0;
 
        g_gdbus_instance = pass_gdbus_get_instance_pmqos();
-       if (g_gdbus_instance == NULL) {
+       if (!g_gdbus_instance) {
                _E("cannot get a dbus instance for the %s interface\n",
                                DBUS_PMQOS_INTERFACE);
                return -ENOSYS;