Remove conditional statement about D-Bus method 98/129598/6
authorKichan Kwon <k_c.kwon@samsung.com>
Wed, 17 May 2017 06:36:42 +0000 (15:36 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Thu, 18 May 2017 06:36:09 +0000 (15:36 +0900)
- To simplify code before other methods are added

Change-Id: Ieb34b0f3f9cfa9637404ef6ace5882df5f432e25
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
src/runtime_info_usage.c

index 2699e2b..8b5908f 100644 (file)
 #define RESOURCED_BUS_NAME "org.tizen.resourced"
 #define RESOURCED_USAGE_OBJECT_NAME "/Org/Tizen/ResourceD/Process"
 #define RESOURCED_USAGE_INTERFACE_NAME "org.tizen.resourced.process"
-#define RESOURCED_MEMORY_USAGE_METHOD "ProcMemoryUsage"
-#define RESOURCED_CPU_USAGE_METHOD "ProcCpuUsage"
 #define DBUS_REPLY_TIMEOUT (120 * 1000)
-#define MEMORY_USAGE 1
-#define CPU_USAGE 2
+
+typedef enum {
+       USAGE_TYPE_PROCESS_MEMORY,
+       USAGE_TYPE_PROCESS_CPU,
+} runtime_info_usage_type_e;
+
+typedef struct {
+       const char *method_name;
+       const char *caption;
+} runtime_info_dbus_info_s;
+
+/* The index of this array must be matched with runtime_info_usage_type_e */
+static const runtime_info_dbus_info_s dbus_info[] = {
+       { "ProcMemoryUsage", "process memory" },
+       { "ProcCpuUsage", "process cpu" },
+};
 
 #define kBtoKiB(val) (int)(((long long)val * 1024)/1000)
 #define pagetoKiB(val) (int)((long long)val * 4096 / 1000)
@@ -62,7 +74,8 @@ static int runtime_info_get_dbus_error(const char *err_name)
  * Creates the method call to resourced and receives the reply (if successful)
  * Return the received reply (if received) else NULL to signify failed call to resourced
  */
-static DBusMessage *runtime_info_dbus_process_usage_info(int *pid, int size, int info, int *error)
+static DBusMessage *runtime_info_dbus_process_usage_info(int *pid, int size,
+               runtime_info_usage_type_e type, int *error)
 {
        DBusConnection *conn;
        DBusMessage *msg;
@@ -89,33 +102,30 @@ static DBusMessage *runtime_info_dbus_process_usage_info(int *pid, int size, int
                //LCOV_EXCL_STOP
        }
 
-       if (info == MEMORY_USAGE) {
-               _D("Process %d: received query to get process memory info of %d processes",
-                               getpid(), size);
-               msg = dbus_message_new_method_call(RESOURCED_BUS_NAME,
-                               RESOURCED_USAGE_OBJECT_NAME,
-                               RESOURCED_USAGE_INTERFACE_NAME,
-                               RESOURCED_MEMORY_USAGE_METHOD);
-       } else if (info == CPU_USAGE) {
-               _D("Process %d: received query to get process cpu usage of %d processes",
-                               getpid(), size);
-               msg = dbus_message_new_method_call(RESOURCED_BUS_NAME,
-                               RESOURCED_USAGE_OBJECT_NAME,
-                               RESOURCED_USAGE_INTERFACE_NAME,
-                               RESOURCED_CPU_USAGE_METHOD);
-       } else {
-               _E("INVALID_PARAMETER(0x%08x): info parameter should be %d or %d",
-                               MEMORY_USAGE, CPU_USAGE, RUNTIME_INFO_ERROR_INVALID_PARAMETER);
+       if (type != USAGE_TYPE_PROCESS_MEMORY && type != USAGE_TYPE_PROCESS_CPU) {
+               //LCOV_EXCL_START : system error
+               _E("INVALID_PARAMETER(0x%08x): invalid info parameter",
+                               RUNTIME_INFO_ERROR_INVALID_PARAMETER);
                *error = RUNTIME_INFO_ERROR_INVALID_PARAMETER;
                return NULL;
+               //LCOV_EXCL_STOP
        }
 
+       _D("Process %d: received query to get %s usage of %d processes",
+                       getpid(), dbus_info[type].caption, size);
+
+       msg = dbus_message_new_method_call(RESOURCED_BUS_NAME,
+                               RESOURCED_USAGE_OBJECT_NAME,
+                               RESOURCED_USAGE_INTERFACE_NAME,
+                               dbus_info[type].method_name);
        if (!msg) {
+               //LCOV_EXCL_START : system error
                _E("DBUS_METHOD_CALL: not able to create method call (%s:%s-%s)",
                                RESOURCED_USAGE_OBJECT_NAME, RESOURCED_USAGE_INTERFACE_NAME,
-                               RESOURCED_MEMORY_USAGE_METHOD);
+                               dbus_info[type].method_name);
                *error = RUNTIME_INFO_ERROR_OUT_OF_MEMORY;
                return NULL;
+               //LCOV_EXCL_STOP
        }
 
        ret = dbus_message_append_args(msg, DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, &pid,
@@ -132,12 +142,9 @@ static DBusMessage *runtime_info_dbus_process_usage_info(int *pid, int size, int
        dbus_error_init(&err);
        *error = 0;
 
-       if (info == MEMORY_USAGE)
-               _D("Process %d: Sending dbus message to resourced for process memory usage info",
-                               getpid());
-       else
-               _D("Process %d: Sending dbus message to resourced for process cpu usage info",
-                               getpid());
+       _D("Process %d: Sending dbus message to resourced for %s usage info",
+                       getpid(), dbus_info[type].caption);
+
        reply = dbus_connection_send_with_reply_and_block(conn, msg,
                        DBUS_REPLY_TIMEOUT, &err);
        if (!reply)
@@ -222,7 +229,7 @@ API int runtime_info_get_process_memory_info(int *pid, int size, process_memory_
        *info = NULL;
 
        /* Get the needed information from resourced daemon using dbus */
-       replymsg = runtime_info_dbus_process_usage_info(pid, size, MEMORY_USAGE, &error);
+       replymsg = runtime_info_dbus_process_usage_info(pid, size, USAGE_TYPE_PROCESS_MEMORY, &error);
        if (!replymsg) {
                //LCOV_EXCL_START : system error
                _E("DBUS_METHOD_CALL: call to resourced not successful");
@@ -351,7 +358,7 @@ API int runtime_info_get_process_cpu_usage(int *pid, int size, process_cpu_usage
        *usage = NULL;
 
        /* Get the needed information from resourced daemon using dbus */
-       replymsg = runtime_info_dbus_process_usage_info(pid, size, CPU_USAGE, &error);
+       replymsg = runtime_info_dbus_process_usage_info(pid, size, USAGE_TYPE_PROCESS_CPU, &error);
 
        if (!replymsg) {
                //LCOV_EXCL_START : system error