event: add event functions to shared library 25/276225/11
authorYoungjae Cho <y0.cho@samsung.com>
Mon, 13 Jun 2022 06:09:12 +0000 (15:09 +0900)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Wed, 15 Jun 2022 05:23:19 +0000 (05:23 +0000)
It contains functions that handling wakelock of event, and broadcasting
signal about an event itself. The properties, WakeLockDurationSec= and
ActionBroadcast= of configuration file, utilize those functions.

Change-Id: I926f5e9cf19bfe09042797a200569c69e2b25a21
Signed-off-by: Youngjae Cho <y0.cho@samsung.com>
conf/battery.conf
conf/input-profile-iot-headless.conf
plugins/iot-headless/battery/battery-plugin.c
plugins/iot-headless/input/input-config.c
plugins/iot-headless/input/input-config.h
plugins/iot-headless/input/input-handler.c
src/power/power-event-lock.c
src/shared/device-notifier.c
src/shared/device-notifier.h
src/shared/event.c [new file with mode: 0644]
src/shared/event.h [new file with mode: 0644]

index d3c9c76..1bd6a2c 100644 (file)
@@ -24,9 +24,11 @@ Name=CHARGER_CONNECTED
 Enum=2001
 DeviceNotifier=DEVICE_NOTIFIER_BATTERY_CHARGER_CONNECTED
 ActionBroadcast=yes
+WakeLockDurationSec=5
 
 [EventAction]
 Name=CHARGER_DISCONNECTED
 Enum=2002
 DeviceNotifier=DEVICE_NOTIFIER_BATTERY_CHARGER_DISCONNECTED
 ActionBroadcast=yes
+WakeLockDurationSec=5
index 8cf5d8b..c422d64 100644 (file)
@@ -22,6 +22,7 @@ Keycode=bluetooth
 DetectionRangeMsec=0,2000
 TriggerType=edge
 ActionBroadcast=yes
+WakeLockDurationSec=5
 
 [EventAction]
 Name=SHORTKEY_LEVEL
index 99c9aef..2c03708 100644 (file)
@@ -23,6 +23,7 @@
 
 #include "shared/device-notifier.h"
 #include "shared/log.h"
+#include "shared/event.h"
 #include "battery/battery-ops.h"
 
 #include "power/power.h"
@@ -39,6 +40,9 @@ struct battery_event_handler {
        /* which action to do on receiving an event */
        enum device_notifier_type action;
        void *user_data;
+
+       /* hold wakelock for a duration on detecting the event */
+       int wakelock_duration;
 };
 
 static struct battery_event_handler *handler_connected;
@@ -106,6 +110,8 @@ static void parse_event_action_property(gpointer data, gpointer user_data)
                parse_action(handler, prop->value);
        } else if (MATCH(prop->key, "ActionBroadcast")) {
                handler->broadcast = MATCH(prop->value, "yes");
+       } else if (MATCH(prop->key, "WakeLockDurationSec")) {
+               sscanf(prop->value, "%d", &handler->wakelock_duration);
        }
 }
 
@@ -130,7 +136,12 @@ static int parse_event_action(const struct parse_result *result, void *data)
 static int charger_connected_callback(void *data)
 {
        _D("event=%s(%d), action=%d", handler_connected->name, handler_connected->id, handler_connected->action);
-       device_notify(handler_connected->action, handler_connected->user_data);
+
+       if (handler_connected->wakelock_duration)
+               event_acquire_wakelock(handler_connected->id, handler_connected->wakelock_duration);
+
+       if (handler_connected->action)
+               device_notify(handler_connected->action, handler_connected->user_data);
 
        return 0;
 }
@@ -138,7 +149,12 @@ static int charger_connected_callback(void *data)
 static int charger_disconnected_callback(void *data)
 {
        _D("event=%s(%d), action=%d", handler_disconnected->name, handler_disconnected->id, handler_disconnected->action);
-       device_notify(handler_disconnected->action, handler_disconnected->user_data);
+
+       if (handler_disconnected->wakelock_duration)
+               event_acquire_wakelock(handler_disconnected->id, handler_disconnected->wakelock_duration);
+
+       if (handler_disconnected->action)
+               device_notify(handler_disconnected->action, handler_disconnected->user_data);
 
        return 0;
 }
index a8dd609..4a8715d 100644 (file)
@@ -183,6 +183,8 @@ static void parse_event_action_property(gpointer data, gpointer user_data)
                parse_change_state(ieu, prop->value);
        } else if (MATCH(prop->key, "ActionBroadcast")) {
                ieu->broadcast = MATCH(prop->value, "yes");
+       } else if (MATCH(prop->key, "WakeLockDurationSec")) {
+               sscanf(prop->value, "%d", &ieu->wakelock_duration);
        }
 }
 
index 9d7d060..4f03f30 100644 (file)
@@ -47,18 +47,21 @@ struct input_event_unit {
        enum trigger_type type;
        unsigned long interval[2];
 
+       /* level-trigger timer */
+       int timer;
+
        /* condition for triggering action */
        struct condition_vconf cv;
 
-       /* broadcast upon occuring this event */
+       /* broadcast upon occuring the event */
        int broadcast;
 
        /* which action to do on receiving an event */
        enum device_notifier_type notifier;
        void *user_data;
 
-       /* level-trigger timer */
-       int timer;
+       /* hold wakelock for a duration on detecting the event */
+       int wakelock_duration;
 };
 
 struct input_config {
index dd31b2c..1176963 100644 (file)
@@ -17,6 +17,7 @@
  */
 
 #include <time.h>
+#include <stdint.h>
 #include <libinput.h>
 #include <linux/input.h>
 #include <glib.h>
 #include "shared/devices.h"
 #include "shared/device-notifier.h"
 #include "shared/log.h"
+#include "shared/event.h"
 
 #include "input-config.h"
 
 #define KEYVALUE_PRESS       1
 #define KEYVALUE_RELEASE     0
 
-static gboolean level_triggered(gpointer data)
+static gboolean level_event_detected(gpointer data)
 {
        struct input_event_unit *ieu = (struct input_event_unit *) data;
 
        ieu->timer = 0;
 
+       _D("Detected level event=%s(%d), action=%d", ieu->name, ieu->id, ieu->notifier);
+
        if (check_input_event_condition(ieu) == 0) {
-               _D("Skip(level) event=%s(%d), condition=%s isn't meet", ieu->name, ieu->id, ieu->cv.keyname);
+               _D("Skip triggering event=%s(%d), condition=%s isn't meet", ieu->name, ieu->id, ieu->cv.keyname);
                return G_SOURCE_REMOVE;
        }
 
-       _D("Trigger(level) event=%s(%d), action=%d", ieu->name, ieu->id, ieu->notifier);
-       device_notify(ieu->notifier, ieu->user_data);
+       if (ieu->broadcast)
+               event_broadcast_id(ieu->id);
+
+       if (ieu->wakelock_duration > 0)
+               event_acquire_wakelock(ieu->id, ieu->wakelock_duration);
+
+       if (ieu->notifier)
+               device_notify(ieu->notifier, ieu->user_data);
 
        return G_SOURCE_REMOVE;
 }
 
-static void edge_triggered(struct input_event_unit *ieu)
+static void edge_event_detected(struct input_event_unit *ieu)
 {
+       _D("Detected edge event=%s(%d), action=%d", ieu->name, ieu->id, ieu->notifier);
+
        if (check_input_event_condition(ieu) == 0) {
-               _D("Skip(edge) event=%s(%d), condition=%s isn't meet", ieu->name, ieu->id, ieu->cv.keyname);
+               _D("Skip triggering event=%s(%d), condition=%s isn't meet", ieu->name, ieu->id, ieu->cv.keyname);
                return;
        }
 
-       _D("Trigger(edge) event=%s(%d), action=%d", ieu->name, ieu->id, ieu->notifier);
-       device_notify(ieu->notifier, ieu->user_data);
+       if (ieu->broadcast)
+               event_broadcast_id(ieu->id);
+
+       if (ieu->wakelock_duration > 0)
+               event_acquire_wakelock(ieu->id, ieu->wakelock_duration);
+
+       if (ieu->notifier)
+               device_notify(ieu->notifier, ieu->user_data);
 }
 
 static void start_event_timer(struct input_config *ic)
@@ -74,7 +92,7 @@ static void start_event_timer(struct input_config *ic)
                if (ieu->type == TRIGGER_TYPE_LEVEL) {
                        if (ieu->timer)
                                g_source_remove(ieu->timer);
-                       ieu->timer = g_timeout_add(ieu->interval[0], level_triggered, (gpointer) ieu);
+                       ieu->timer = g_timeout_add(ieu->interval[0], level_event_detected, (gpointer) ieu);
                }
        }
 }
@@ -102,9 +120,9 @@ static void stop_event_timer(struct input_config *ic)
                        ieu->timer = 0;
                }
 
-               /* trigger edge-trigger action */
+               /* detected edge-trigger event */
                if (lapse >= ieu->interval[0] && lapse < ieu->interval[1] && ieu->type == TRIGGER_TYPE_EDGE)
-                       edge_triggered(ieu);
+                       edge_event_detected(ieu);
        }
 }
 
index dc8353c..70bb44c 100644 (file)
@@ -43,6 +43,7 @@ enum eventlock_type {
        EL_KEY_BLUETOOTH,
 
        EL_CHARGER,
+       EL_EVENT_ACTION,
        /* add eventlock type here */
        EL_MAX,
 };
@@ -60,11 +61,11 @@ static void event_wake_lock(enum eventlock_type type)
        set_bit(eventlock, type);
        setcount = count_set_bit(eventlock);
 
-       _D("Set eventlock of type=%d, current number of eventlock=%d", type, setcount);
+       _I("Set eventlock of type=%d, current number of eventlock=%d", type, setcount);
 
        if (setcount == 1) {
                sys_set_str("/sys/power/wake_lock", EVENT_LOCK);
-               _D("Acquired eventlock");
+               _I("Acquired eventlock");
        }
 }
 
@@ -78,11 +79,11 @@ static void event_wake_unlock(enum eventlock_type type)
        clear_bit(eventlock, type);
        setcount = count_set_bit(eventlock);
 
-       _D("Unset eventlock of type=%d, current number of eventlock=%d", type, setcount);
+       _I("Unset eventlock of type=%d, current number of eventlock=%d", type, setcount);
 
        if (setcount == 0) {
                sys_set_str("/sys/power/wake_unlock", EVENT_LOCK);
-               _D("Released eventlock");
+               _I("Released eventlock");
        }
 }
 
@@ -163,10 +164,17 @@ void power_event_lock_init(void)
 
        config_parse(BATTERY_CONF_FILE, check_charger_wakelock, &charger_wakelock);
 
+       /* general events, mostly defined by configuration file */
+       register_power_event_lock_controller(EL_EVENT_ACTION,
+               DEVICE_NOTIFIER_EVENT_ACQUIRE_WAKELOCK,
+               DEVICE_NOTIFIER_EVENT_RELEASE_WAKELOCK);
+
+       /* key input event */
        register_power_event_lock_controller(EL_KEY,
                DEVICE_NOTIFIER_KEY_PRESS,
                DEVICE_NOTIFIER_KEY_RELEASE);
 
+       /* charger event */
        if (charger_wakelock) {
                register_power_event_lock_controller(EL_CHARGER,
                        DEVICE_NOTIFIER_BATTERY_CHARGER_CONNECTED,
index 77980fb..61cd1c2 100644 (file)
@@ -93,6 +93,8 @@ static const char *device_notifier_type_str[DEVICE_NOTIFIER_MAX] = {
        NOTIFY_STR(DEVICE_NOTIFIER_EXTCON_COUNT),
        NOTIFY_STR(DEVICE_NOTIFIER_KEY_PRESS),
        NOTIFY_STR(DEVICE_NOTIFIER_KEY_RELEASE),
+       NOTIFY_STR(DEVICE_NOTIFIER_EVENT_ACQUIRE_WAKELOCK),
+       NOTIFY_STR(DEVICE_NOTIFIER_EVENT_RELEASE_WAKELOCK),
 
        /* Purpose of calling methods of different modules
         * Use prefix DEVICE_NOTIFIER_REQUEST */
index c81db6a..bd1880a 100644 (file)
@@ -66,6 +66,8 @@ enum device_notifier_type {
        DEVICE_NOTIFIER_EXTCON_COUNT,
        DEVICE_NOTIFIER_KEY_PRESS,
        DEVICE_NOTIFIER_KEY_RELEASE,
+       DEVICE_NOTIFIER_EVENT_ACQUIRE_WAKELOCK,
+       DEVICE_NOTIFIER_EVENT_RELEASE_WAKELOCK,
 
        /* Purpose of calling methods of different modules
         * Use prefix DEVICE_NOTIFIER_REQUEST */
diff --git a/src/shared/event.c b/src/shared/event.c
new file mode 100644 (file)
index 0000000..4915f76
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * deviced
+ *
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <glib.h>
+#include <stdint.h>
+
+#include "device-notifier.h"
+#include "log.h"
+
+static int wakelock_counter;
+
+static gboolean event_release_wakelock(gpointer data)
+{
+       int eventid = (int)(intptr_t) data;
+
+       --wakelock_counter;
+
+       _D("Decreased counter=%d, eventid=%d", wakelock_counter, eventid);
+
+       if (wakelock_counter == 0)
+               device_notify(DEVICE_NOTIFIER_EVENT_RELEASE_WAKELOCK, NULL);
+
+       return G_SOURCE_REMOVE;
+}
+
+void event_acquire_wakelock(int eventid, int timeout)
+{
+       if (timeout <= 0)
+               return;
+
+       ++wakelock_counter;
+
+       _D("Increased counter=%d, eventid=%d, timeout=%d", wakelock_counter, eventid, timeout);
+       g_timeout_add_seconds(timeout, event_release_wakelock, (gpointer)(intptr_t) eventid);
+
+       if (wakelock_counter == 1)
+               device_notify(DEVICE_NOTIFIER_EVENT_ACQUIRE_WAKELOCK, NULL);
+}
diff --git a/src/shared/event.h b/src/shared/event.h
new file mode 100644 (file)
index 0000000..44821e5
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * deviced
+ *
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __DD_EVENT_H__
+#define __DD_EVENT_H__
+
+#include <libsyscommon/libgdbus.h>
+
+#define DEVICED_PATH_EVENT          "/Org/Tizen/System/DeviceD/Event"
+#define DEVICED_INTERFACE_EVENT     "org.tizen.system.deviced.Event"
+#define DEVICED_SIGNAME_ID          "Id"
+
+static inline void event_broadcast_id(int id)
+{
+       _I("Broadcast eventid=%d", id);
+
+       gdbus_signal_emit(NULL,
+               DEVICED_PATH_EVENT,
+               DEVICED_INTERFACE_EVENT,
+               DEVICED_SIGNAME_ID,
+               g_variant_new("(i)", id));
+}
+
+/**
+ * Hold wakelock for a specified timeout.
+ * The parameter eventid is only used for logging
+ */
+void event_acquire_wakelock(int eventid, int timeout);
+
+#endif //__DD_EVENT_H__