Modify device_display_change_state() to be asynchronous 25/182125/1 accepted/tizen/4.0/unified/20180621.141402 submit/tizen_4.0/20180621.024300
authorlokilee73 <changjoo.lee@samsung.com>
Wed, 20 Jun 2018 12:56:02 +0000 (21:56 +0900)
committerlokilee73 <changjoo.lee@samsung.com>
Wed, 20 Jun 2018 12:56:07 +0000 (21:56 +0900)
To change from Off to Normal, time of more than 200ms was required.

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

index c6fd0a1057f58d97d1f1326ab8d38ca816e273ed..7157b42c9ea007ffda9245140d58134af31ec47a 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
@@ -161,3 +162,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__ */
old mode 100644 (file)
new mode 100755 (executable)
index e633d8c..6817f28
@@ -207,26 +207,62 @@ 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);
 }
old mode 100644 (file)
new mode 100755 (executable)
index 10704b5..25a0a30
@@ -20,6 +20,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <stdlib.h>
+#include <system_info.h>
 
 #include "haptic.h"
 #include "common.h"