Add dbus methods to lock/unlock/change display 84/14684/1
authorjy910.yun <jy910.yun@samsung.com>
Sun, 30 Jun 2013 07:25:34 +0000 (16:25 +0900)
committerKrzysztof Sasiak <k.sasiak@samsung.com>
Thu, 9 Jan 2014 16:36:13 +0000 (17:36 +0100)
Change-Id: I394bea1ee71aed294bf10bf44af4787407f89503
Signed-off-by: Krzysztof Sasiak <k.sasiak@samsung.com>
display/display-dbus.c
display/poll.h
src/core/edbus-handler.c
src/core/edbus-handler.h

index aa09dec..da490d9 100644 (file)
@@ -15,7 +15,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
-*/
+ */
 
 
 /**
@@ -24,6 +24,7 @@
  *
  */
 
+#include <error.h>
 #include <Ecore.h>
 
 #include "util.h"
 #include "device-node.h"
 #include "core/common.h"
 
+
 #define DISP_INDEX_BIT 4
 #define COMBINE_DISP_CMD(cmd, prop, index) (cmd = (prop | (index << DISP_INDEX_BIT)))
 
-static DBusMessage* e_dbus_start_cb(E_DBus_Object *obj, DBusMessage* msg)
+static DBusMessage *e_dbus_start_cb(E_DBus_Object *obj, DBusMessage *msg)
 {
        start_pm_main();
        return dbus_message_new_method_return(msg);
 }
 
-static DBusMessage* e_dbus_stop_cb(E_DBus_Object *obj, DBusMessage* msg)
+static DBusMessage *e_dbus_stop_cb(E_DBus_Object *obj, DBusMessage *msg)
 {
        end_pm_main();
        return dbus_message_new_method_return(msg);
 }
 
-static DBusMessage* e_dbus_getbrightness_cb(E_DBus_Object *obj, DBusMessage* msg)
+static DBusMessage *e_dbus_lockstate_cb(E_DBus_Object *obj, DBusMessage *msg)
+{
+       DBusError err;
+       DBusMessageIter iter;
+       DBusMessage *reply;
+       char *state_str;
+       char *option1_str;
+       char *option2_str;
+       int timeout;
+       pid_t pid;
+       int state;
+       int flag;
+       int ret;
+
+       dbus_error_init(&err);
+
+       if (!dbus_message_get_args(msg, &err,
+                   DBUS_TYPE_STRING, &state_str,
+                   DBUS_TYPE_STRING, &option1_str,
+                   DBUS_TYPE_STRING, &option2_str,
+                   DBUS_TYPE_INT32, &timeout, DBUS_TYPE_INVALID)) {
+               LOGERR("there is no message");
+               ret = -EINVAL;
+               goto out;
+       }
+
+       if (!state_str || timeout < 0) {
+               LOGERR("message is invalid!");
+               ret = -EINVAL;
+               goto out;
+       }
+
+       pid = get_edbus_sender_pid(msg);
+       if (kill(pid, 0) == -1) {
+               LOGERR("%d process does not exist, dbus ignored!", pid);
+               ret = -ESRCH;
+               goto out;
+       }
+
+       if (!strcmp(state_str, PM_LCDON_STR))
+               state = LCD_NORMAL;
+       else if (!strcmp(state_str, PM_LCDDIM_STR))
+               state = LCD_DIM;
+       else if (!strcmp(state_str, PM_LCDOFF_STR))
+               state = LCD_OFF;
+       else {
+               LOGERR("%s state is invalid, dbus ignored!", state_str);
+               ret = -EINVAL;
+               goto out;
+       }
+
+       if (!strcmp(option1_str, STAYCURSTATE_STR))
+               flag = STAY_CUR_STATE;
+       else if (!strcmp(option1_str, GOTOSTATENOW_STR))
+               flag = GOTO_STATE_NOW;
+       else {
+               LOGERR("%s option is invalid. set default option!", option1_str);
+               flag = STAY_CUR_STATE;
+       }
+
+       ret = pm_lock_internal(pid, state, flag, timeout);
+out:
+       reply = dbus_message_new_method_return(msg);
+       dbus_message_iter_init_append(reply, &iter);
+       dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
+
+       return reply;
+}
+
+static DBusMessage *e_dbus_unlockstate_cb(E_DBus_Object *obj, DBusMessage *msg)
+{
+       DBusError err;
+       DBusMessageIter iter;
+       DBusMessage *reply;
+       char *state_str;
+       char *option_str;
+       pid_t pid;
+       int state;
+       int flag;
+       int ret;
+
+       dbus_error_init(&err);
+
+       if (!dbus_message_get_args(msg, &err,
+                   DBUS_TYPE_STRING, &state_str,
+                   DBUS_TYPE_STRING, &option_str, DBUS_TYPE_INVALID)) {
+               LOGERR("there is no message");
+               ret = -EINVAL;
+               goto out;
+       }
+
+       if (!state_str) {
+               LOGERR("message is invalid!");
+               ret = -EINVAL;
+               goto out;
+       }
+
+       pid = get_edbus_sender_pid(msg);
+       if (kill(pid, 0) == -1) {
+               LOGERR("%d process does not exist, dbus ignored!", pid);
+               ret = -ESRCH;
+               goto out;
+       }
+
+       if (!strcmp(state_str, PM_LCDON_STR))
+               state = LCD_NORMAL;
+       else if (!strcmp(state_str, PM_LCDDIM_STR))
+               state = LCD_DIM;
+       else if (!strcmp(state_str, PM_LCDOFF_STR))
+               state = LCD_OFF;
+       else {
+               LOGERR("%s state is invalid, dbus ignored!", state_str);
+               ret = -EINVAL;
+               goto out;
+       }
+
+       if (!strcmp(option_str, SLEEP_MARGIN_STR))
+               flag = PM_SLEEP_MARGIN;
+       else if (!strcmp(option_str, RESET_TIMER_STR))
+               flag = PM_RESET_TIMER;
+       else if (!strcmp(option_str, KEEP_TIMER_STR))
+               flag = PM_KEEP_TIMER;
+       else {
+               LOGERR("%s option is invalid. set default option!", option_str);
+               flag = PM_RESET_TIMER;
+       }
+
+       ret = pm_unlock_internal(pid, state, flag);
+out:
+       reply = dbus_message_new_method_return(msg);
+       dbus_message_iter_init_append(reply, &iter);
+       dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
+
+       return reply;
+}
+
+static DBusMessage *e_dbus_changestate_cb(E_DBus_Object *obj, DBusMessage *msg)
+{
+       DBusError err;
+       DBusMessageIter iter;
+       DBusMessage *reply;
+       char *state_str;
+       pid_t pid;
+       int state;
+       int ret;
+
+       dbus_error_init(&err);
+
+       if (!dbus_message_get_args(msg, &err,
+                   DBUS_TYPE_STRING, &state_str, DBUS_TYPE_INVALID)) {
+               LOGERR("there is no message");
+               ret = -EINVAL;
+               goto out;
+       }
+
+       if (!state_str) {
+               LOGERR("message is invalid!");
+               ret = -EINVAL;
+               goto out;
+       }
+
+       pid = get_edbus_sender_pid(msg);
+       if (kill(pid, 0) == -1) {
+               LOGERR("%d process does not exist, dbus ignored!", pid);
+               ret = -ESRCH;
+               goto out;
+       }
+
+       if (!strcmp(state_str, PM_LCDON_STR))
+               state = LCD_NORMAL;
+       else if (!strcmp(state_str, PM_LCDDIM_STR))
+               state = LCD_DIM;
+       else if (!strcmp(state_str, PM_LCDOFF_STR))
+               state = LCD_OFF;
+       else {
+               LOGERR("%s state is invalid, dbus ignored!", state_str);
+               ret = -EINVAL;
+               goto out;
+       }
+
+       ret = pm_change_internal(pid, state);
+out:
+       reply = dbus_message_new_method_return(msg);
+       dbus_message_iter_init_append(reply, &iter);
+       dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret);
+
+       return reply;
+}
+
+static DBusMessage *e_dbus_getbrightness_cb(E_DBus_Object *obj, DBusMessage *msg)
 {
        DBusMessageIter iter;
        DBusMessage *reply;
@@ -66,7 +257,7 @@ static DBusMessage* e_dbus_getbrightness_cb(E_DBus_Object *obj, DBusMessage* msg
        return reply;
 }
 
-static DBusMessage* e_dbus_setbrightness_cb(E_DBus_Object *obj, DBusMessage* msg)
+static DBusMessage *e_dbus_setbrightness_cb(E_DBus_Object *obj, DBusMessage *msg)
 {
        DBusMessageIter iter;
        DBusMessage *reply;
@@ -95,11 +286,14 @@ static struct edbus_method {
        const char *reply_signature;
        E_DBus_Method_Cb func;
 } edbus_methods[] = {
-        { "start",           NULL,  NULL, e_dbus_start_cb },
-        { "stop",            NULL,  NULL, e_dbus_stop_cb },
-        { "getbrightness",   NULL,   "i", e_dbus_getbrightness_cb },
-        { "setbrightness",    "i",   "i", e_dbus_setbrightness_cb },
-        /* Add methods here */
+       { "start",           NULL,  NULL, e_dbus_start_cb },
+       { "stop",            NULL,  NULL, e_dbus_stop_cb },
+       { "lockstate",     "sssi",   "i", e_dbus_lockstate_cb },
+       { "unlockstate",     "ss",   "i", e_dbus_unlockstate_cb },
+       { "changestate",      "s",   "i", e_dbus_changestate_cb },
+       { "getbrightness",   NULL,   "i", e_dbus_getbrightness_cb },
+       { "setbrightness",    "i",   "i", e_dbus_setbrightness_cb }
+       /* Add methods here */
 };
 
 int init_pm_dbus(void)
index 7e74eb6..8fcbe5c 100644 (file)
@@ -51,8 +51,8 @@ enum {
 #define LCD_OFF                0x4             /**< LCD off state */
 #define POWER_OFF      0x16    /**< Sleep state */
 
-#define STAY_CUR_STATE 0x0
-#define GOTO_STATE_NOW 0x1
+#define STAY_CUR_STATE 0x1
+#define GOTO_STATE_NOW 0x2
 
 #define PM_SLEEP_MARGIN        0x0     /**< keep guard time for unlock */
 #define PM_RESET_TIMER 0x1     /**< reset timer for unlock */
@@ -66,6 +66,13 @@ enum {
 #define PM_LCDDIM_STR  "lcddim"
 #define PM_LCDON_STR   "lcdon"
 
+#define STAYCURSTATE_STR "staycurstate"
+#define GOTOSTATENOW_STR "gotostatenow"
+
+#define SLEEP_MARGIN_STR "sleepmargin"
+#define RESET_TIMER_STR  "resettimer"
+#define KEEP_TIMER_STR   "keeptimer"
+
 typedef struct {
        pid_t pid;
        unsigned int cond;
index b8eacd0..346714c 100644 (file)
@@ -83,6 +83,69 @@ E_DBus_Interface *get_edbus_interface(const char *path)
        return NULL;
 }
 
+pid_t get_edbus_sender_pid(DBusMessage *msg)
+{
+       const char *sender;
+       DBusMessage *send_msg;
+       DBusPendingCall *pending;
+       DBusMessageIter iter;
+       int ret;
+       pid_t pid;
+
+       if (!msg) {
+               PRT_TRACE_ERR("invalid argument!");
+               return -1;
+       }
+
+       sender = dbus_message_get_sender(msg);
+       if (!sender) {
+               PRT_TRACE_ERR("invalid sender!");
+               return -1;
+       }
+
+       send_msg = dbus_message_new_method_call(DBUS_SERVICE_DBUS,
+                                   DBUS_PATH_DBUS,
+                                   DBUS_INTERFACE_DBUS,
+                                   "GetConnectionUnixProcessID");
+       if (!send_msg) {
+               PRT_TRACE_ERR("invalid send msg!");
+               return -1;
+       }
+
+       ret = dbus_message_append_args(send_msg, DBUS_TYPE_STRING,
+                                   &sender, DBUS_TYPE_INVALID);
+       if (!ret) {
+               PRT_TRACE_ERR("fail to append args!");
+               dbus_message_unref(send_msg);
+               return -1;
+       }
+
+       pending = e_dbus_message_send(edbus_conn, send_msg, NULL, -1, NULL);
+       if (!pending) {
+               PRT_TRACE_ERR("pending is null!");
+               dbus_message_unref(send_msg);
+               return -1;
+       }
+
+       dbus_message_unref(send_msg);
+
+       /* block until reply is received */
+       dbus_pending_call_block(pending);
+
+       msg = dbus_pending_call_steal_reply(pending);
+       dbus_pending_call_unref(pending);
+       if (!msg) {
+               PRT_TRACE_ERR("reply msg is null!");
+               return -1;
+       }
+
+       dbus_message_iter_init(msg, &iter);
+       dbus_message_iter_get_basic(&iter, &pid);
+       dbus_message_unref(msg);
+
+       return pid;
+}
+
 static void unregister_edbus_signal_handle(void)
 {
        Eina_List *tmp;
index 302ac8f..2060cd9 100644 (file)
@@ -48,5 +48,6 @@ void edbus_init(void);
 void edbus_fini(void);
 int register_edbus_signal_handler(char *signal_name, E_DBus_Signal_Cb cb);
 E_DBus_Interface *get_edbus_interface(const char *path);
+pid_t get_edbus_sender_pid(DBusMessage *msg);
 
 #endif /* __SS_EDBUS_HANDLE_H__ */