Modify device_display_change_state() to be asynchronous 90/181990/3
authorlokilee73 <changjoo.lee@samsung.com>
Tue, 19 Jun 2018 11:31:52 +0000 (20:31 +0900)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Tue, 19 Jun 2018 11:41:04 +0000 (20:41 +0900)
To change from Off to Normal, time of more than 200ms was required.

Change-Id: I36075931343a5adde860624c7aa762be8de92cc7
Signed-off-by: lokilee73 <changjoo.lee@samsung.com>
Signed-off-by: Hyotaek Shim <hyotaek.shim@samsung.com>
src/common.c
src/common.h [changed mode: 0644->0755]
src/display.c
src/haptic.c [changed mode: 0644->0755]
src/power.c

index f663111..104c168 100755 (executable)
@@ -20,6 +20,7 @@
 #include <stdlib.h>
 #include <system_info.h>
 #include "common.h"
+#include <sys/time.h>
 
 #define MAX_LINE       128
 #define MAX_SECTION    64
@@ -167,3 +168,46 @@ tizen_profile_t _get_tizen_profile()
 
        return profile;
 }
+
+int check_async_call_rate(void)
+{
+       static int status;
+       static long num_calls;
+       static struct timeval prev;
+       struct timeval cur;
+       int ret;
+
+       if (status < 0)
+               return status;
+
+       ret = gettimeofday(&cur, NULL);
+       if (ret < 0)
+               return 0;
+
+       if (num_calls > 0) {
+               long rate;
+               struct timeval diff;
+               timersub(&cur, &prev, &diff);
+
+               if (diff.tv_sec > 0) {
+                       rate = num_calls / diff.tv_sec;
+
+                       if (rate > CHECK_RATE_THRESHOLD) {
+                               status = -1;
+                               return status;
+                       }
+
+                       if (diff.tv_sec > CHECK_RATE_PERIOD_LEN) {
+                               /* Reset the check period */
+                               num_calls = 0;
+                               return 0;
+                       }
+               }
+       } else {
+               /* Start a new check period */
+               prev = cur;
+       }
+
+       num_calls++;
+       return 0;
+}
old mode 100644 (file)
new mode 100755 (executable)
index 0fe6bc2..cd633ac
@@ -76,6 +76,10 @@ typedef enum {
 } tizen_profile_t;
 extern tizen_profile_t _get_tizen_profile();
 
+#define CHECK_RATE_THRESHOLD   100
+#define CHECK_RATE_PERIOD_LEN  5
+int check_async_call_rate(void);
+
 #define TIZEN_FEATURE_TRACKER (_get_tizen_profile() == TIZEN_PROFILE_TV)
 
 #endif /* __COMMON_H__ */
index 883132d..77ec7bf 100755 (executable)
@@ -223,28 +223,64 @@ static char *get_state_str(display_state_e state)
        return NULL;
 }
 
+static void change_cb(void *data, GVariant *result, GError *err)
+{
+       int ret;
+
+       if (!result) {
+//LCOV_EXCL_START System Error
+               _E("no message : %s", err->message);
+               return;
+//LCOV_EXCL_STOP
+       }
+
+       g_variant_get(result, "(i)", &ret);
+       _D("%s-%s : %d", DEVICED_INTERFACE_DISPLAY, METHOD_CHANGE_STATE, ret);
+}
+
 int device_display_change_state(display_state_e state)
 {
        char *str, *arr[1];
        int ret;
+       static int privilege = -1;
 
        _W("DEPRECATION WARNING: device_display_change_state() is deprecated and will be removed from next release.");
+       if (check_async_call_rate() < 0) {
+               _E("Rejected by too frequent calls; %d (calls per sec.) limit is violated."
+                               , CHECK_RATE_THRESHOLD);
+               return DEVICE_ERROR_OPERATION_FAILED;
+       }
+
+       if (privilege == 0)
+               return -EACCES;
+
        if (state < DISPLAY_STATE_NORMAL || state > DISPLAY_STATE_SCREEN_OFF)
                return DEVICE_ERROR_INVALID_PARAMETER;
 
+       if (privilege < 0) {
+               arr[0] = "privilege check";
+
+               ret = dbus_method_sync(DEVICED_BUS_NAME,
+                               DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
+                               METHOD_CHANGE_STATE, "s", arr);
+//LCOV_EXCL_START System Error
+               if (ret == -EACCES || ret == -ECOMM || ret == -EPERM) {
+                       privilege = 0;
+                       return -EACCES;
+//LCOV_EXCL_STOP
+               } else
+                       privilege = 1;
+       }
+
        str = get_state_str(state);
        if (!str)
                return DEVICE_ERROR_INVALID_PARAMETER;
 
        arr[0] = str;
 
-       ret = dbus_method_sync(DEVICED_BUS_NAME,
+       return dbus_method_async_with_reply(DEVICED_BUS_NAME,
                        DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
-                       METHOD_CHANGE_STATE, "s", arr);
-       if (ret < 0)
-               return errno_to_device_error(ret);
-
-       return DEVICE_ERROR_NONE;
+                       METHOD_CHANGE_STATE, "s", arr, change_cb, -1, NULL);
 }
 
 //LCOV_EXCL_START Not used function
@@ -379,6 +415,9 @@ int device_display_set_brightness_state(int display_index, display_state_e state
                snprintf(str_val, sizeof(str_val), "%d", brightness);
                arr[1] = str_val;
                break;
+
+       default:
+               break;
        }
 
        ret = dbus_method_sync(DEVICED_BUS_NAME,
old mode 100644 (file)
new mode 100755 (executable)
index 14aaf03..0b7f559
@@ -20,6 +20,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <stdlib.h>
+#include <system_info.h>
 
 #include "haptic.h"
 #include "common.h"
index 4029d2a..344afb1 100755 (executable)
@@ -498,7 +498,7 @@ int device_power_request_lock(power_lock_e type, int timeout_ms)
        _I("power_lock_e = %d", type);
 
        if (check_rate() < 0) {
-               _E("Rejected by too frequent calls; %d (calls per sec.) limit is over."
+               _E("Rejected by too frequent calls; %d (calls per sec.) limit is violated."
                                , CHECK_RATE_THRESHOLD);
                return DEVICE_ERROR_OPERATION_FAILED;
        }
@@ -528,7 +528,7 @@ int device_power_release_lock(power_lock_e type)
        _I("power_lock_e = %d", type);
 
        if (check_rate() < 0) {
-               _E("Rejected by too frequent calls; %d (calls per sec.) limit is over."
+               _E("Rejected by too frequent calls; %d (calls per sec.) limit is violated."
                                , CHECK_RATE_THRESHOLD);
                return DEVICE_ERROR_OPERATION_FAILED;
        }