From 2d58f6ba5fa3c845b073601b6a27fb68d366e037 Mon Sep 17 00:00:00 2001 From: Youngjae Cho Date: Thu, 9 Dec 2021 15:42:46 +0900 Subject: [PATCH] power: fix sleep/wakeup dbus signal handler Now sleep/wakeup callback is invoked by dbus signal "ChangeState". The signal param contains type(0:sleep, 1:wakeup) as well as reason. Change-Id: I63dbb9c1f2ee3dfb6557d5ce1e5f1d322d976292 Signed-off-by: Youngjae Cho --- include/power-internal.h | 18 ++++----- src/power-internal.c | 98 ++++++++++++++++-------------------------------- 2 files changed, 40 insertions(+), 76 deletions(-) diff --git a/include/power-internal.h b/include/power-internal.h index 787c38d..f4aa0ea 100644 --- a/include/power-internal.h +++ b/include/power-internal.h @@ -42,23 +42,19 @@ extern "C" { */ int device_power_poweroff(void); -enum sleep_ready { +enum { DEVICE_SLEEP_READY, DEVICE_WAKEUP_READY = DEVICE_SLEEP_READY, - DEVICE_SLEEP_NOT_READY, + DEVICE_NOT_READY, }; -enum device_siginfo { - DEVICE_SIG_SLEEP_SHORTKEY, - DEVICE_SIG_WAKEUP_SHORTKEY, - DEVICE_SIG_UNKNOWN, +struct device_event_info { + int event; /* 0: sleep, 1: wakeup */ + int reason; }; -/* This callback is invoked when system is going to sleep. - * There can be only one callbacks per process. - * Return DEVICE_SLEEP_READY to notify system that I am ready to sleep - * when siginfo == DEVICE_SIG_SLEEP_SHORTKEY. */ -typedef int (*sleep_callback) (guint64 sig_time_ms, enum device_siginfo siginfo, void *user_data); +/* return DEVICE_SLEEP_READY or DEVICE_WAKEUP_READY on finishing todo list */ +typedef int (*sleep_callback) (const struct device_event_info *info, void *user_data); /* return DEVICE_ERROR_NONE on success */ int device_power_add_sleep_callback(sleep_callback cb, void *user_data); diff --git a/src/power-internal.c b/src/power-internal.c index 5f57e9d..9fdd1e6 100644 --- a/src/power-internal.c +++ b/src/power-internal.c @@ -6,17 +6,18 @@ #include "power-internal.h" #include "common.h" -#define SLEEP_DBUS_SIGNAME "sleep" -#define WAKEUP_DBUS_SIGNAME "wakeup" +#define CHANGE_STATE_DBUS_SIGNAME "ChangeState" #define DBUS_METHOD_SYNC_CALL_TIMEOUT_MS 10000 /* 10 second */ +#define EVENT_TYPE_SLEEP 0 +#define EVENT_TYPE_WAKEUP 1 + struct userdata { void *data; sleep_callback callback; }; -static int sleep_signal_subscribe_id = -1; -static int wakeup_signal_subscribe_id = -1; +static int change_state_signal_id = -1; static void signal_unsubscribed_callback(void *data) { @@ -25,26 +26,34 @@ static void signal_unsubscribed_callback(void *data) free(ud); } -static void handle_sleep_signal(GDBusConnection *connection, GVariant *parameters, void *user_data) +static void signal_callback(GDBusConnection *connection, + const gchar *sender_name, + const gchar *object_path, + const gchar *interface_name, + const gchar *signal_name, + GVariant *parameters, + gpointer user_data) { - struct userdata *ud = (struct userdata *) user_data; - guint64 sig_time_ms; - guint64 sleep_id; + struct device_event_info info; + struct userdata *ud; + guint64 event_id; int retval; + ud = (struct userdata *) user_data; + if (!ud || !ud->callback) return; - g_variant_get(parameters, "(tt)", &sig_time_ms, &sleep_id); + g_variant_get(parameters, "(iti)", &info.event, &event_id, &info.reason); - retval = ud->callback(sig_time_ms, DEVICE_SIG_SLEEP_SHORTKEY, ud->data); - if (retval == DEVICE_SLEEP_READY) { + retval = ud->callback(&info, ud->data); + if (info.event == EVENT_TYPE_SLEEP && retval == DEVICE_SLEEP_READY) { g_dbus_connection_call_sync(connection, DEVICED_BUS_NAME, DEVICED_PATH_POWER, DEVICED_INTERFACE_POWER, "ConfirmSleepWait", - g_variant_new("(i)", sleep_id), + g_variant_new("(i)", event_id), NULL, G_DBUS_CALL_FLAGS_NONE, DBUS_METHOD_SYNC_CALL_TIMEOUT_MS, @@ -53,34 +62,6 @@ static void handle_sleep_signal(GDBusConnection *connection, GVariant *parameter } } -static void handle_wakeup_signal(GVariant *parameters, void *user_data) -{ - struct userdata *ud = (struct userdata *) user_data; - guint64 sig_time_ms; - enum device_siginfo siginfo; - - if (!ud || !ud->callback) - return; - - g_variant_get(parameters, "(tt)", &sig_time_ms, &siginfo); - ud->callback(sig_time_ms, siginfo, ud->data); -} - -static void signal_callback(GDBusConnection *connection, - const gchar *sender_name, - const gchar *object_path, - const gchar *interface_name, - const gchar *signal_name, - GVariant *parameters, - gpointer user_data) -{ - - if (!strncmp(signal_name, SLEEP_DBUS_SIGNAME, sizeof(SLEEP_DBUS_SIGNAME))) - handle_sleep_signal(connection, parameters, user_data); - else if (!strncmp(signal_name, WAKEUP_DBUS_SIGNAME, sizeof(WAKEUP_DBUS_SIGNAME))) - handle_wakeup_signal(parameters, user_data); -} - int device_power_add_sleep_callback(sleep_callback cb, void *data) { GDBusConnection *connection; @@ -88,7 +69,7 @@ int device_power_add_sleep_callback(sleep_callback cb, void *data) GError *err = NULL; struct userdata *ud; - if (sleep_signal_subscribe_id != -1) + if (change_state_signal_id != -1) return DEVICE_ERROR_ALREADY_IN_PROGRESS; connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err); @@ -98,12 +79,6 @@ int device_power_add_sleep_callback(sleep_callback cb, void *data) return DEVICE_ERROR_OPERATION_FAILED; } - ud = malloc(sizeof(struct userdata)); - if (!ud) { - _E("Failed to alloc user data"); - return DEVICE_ERROR_OPERATION_FAILED; - } - retval = g_dbus_connection_call_sync(connection, DEVICED_BUS_NAME, DEVICED_PATH_POWER, @@ -121,13 +96,19 @@ int device_power_add_sleep_callback(sleep_callback cb, void *data) return DEVICE_ERROR_OPERATION_FAILED; } + ud = malloc(sizeof(struct userdata)); + if (!ud) { + _E("Failed to alloc user data"); + return DEVICE_ERROR_OPERATION_FAILED; + } + ud->data = data; ud->callback = cb; - sleep_signal_subscribe_id = g_dbus_connection_signal_subscribe(connection, + change_state_signal_id = g_dbus_connection_signal_subscribe(connection, DEVICED_BUS_NAME, DEVICED_INTERFACE_POWER, - SLEEP_DBUS_SIGNAME, + CHANGE_STATE_DBUS_SIGNAME, DEVICED_PATH_POWER, NULL, G_DBUS_SIGNAL_FLAGS_NONE, @@ -135,17 +116,6 @@ int device_power_add_sleep_callback(sleep_callback cb, void *data) ud, signal_unsubscribed_callback); - wakeup_signal_subscribe_id = g_dbus_connection_signal_subscribe(connection, - DEVICED_BUS_NAME, - DEVICED_INTERFACE_POWER, - WAKEUP_DBUS_SIGNAME, - DEVICED_PATH_POWER, - NULL, - G_DBUS_SIGNAL_FLAGS_NONE, - signal_callback, - ud, - NULL); - return DEVICE_ERROR_NONE; } @@ -154,7 +124,7 @@ void device_power_remove_sleep_callback(void) GError *err = NULL; GDBusConnection *connection; - if (sleep_signal_subscribe_id == -1) + if (change_state_signal_id == -1) return; connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err); @@ -164,10 +134,8 @@ void device_power_remove_sleep_callback(void) return; } - g_dbus_connection_signal_unsubscribe(connection, sleep_signal_subscribe_id); - sleep_signal_subscribe_id = -1; - g_dbus_connection_signal_unsubscribe(connection, wakeup_signal_subscribe_id); - wakeup_signal_subscribe_id = -1; + g_dbus_connection_signal_unsubscribe(connection, change_state_signal_id); + change_state_signal_id = -1; g_dbus_connection_call_sync(connection, DEVICED_BUS_NAME, -- 2.7.4