Skip to content

Commit

Permalink
procd: (APP-3185, PR #268) allow button timeouts to timeout
Browse files Browse the repository at this point in the history
This allows visual feedback when timeouts are reached.

In our case, there is a single button which on a normal
press/release will initiate DPP, but longer presses will
first reboot then factory reset. So users know they've
held it down long enough, it's nice to change a status LED.

This also fixes what seems to me a bug, in that you can
attach a timeout to a 'release' action but the timing was
reset (so you can't distinguish between a released timeout
vs a pressed timeout).

See:

openwrt/procd#10


Approved-by: Sophronia Koilpillai
Approved-by: Arien Judge
  • Loading branch information
wryun authored and morse-arienjudge committed Sep 18, 2024
1 parent cb09b38 commit 5a6c980
Showing 1 changed file with 77 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
From 7e338e24a552fe14bd218371cf84c227be3a7f89 Mon Sep 17 00:00:00 2001
From: James Haggerty <[email protected]>
Date: Wed, 24 Jul 2024 11:37:46 +1000
Subject: [PATCH] hotplug: allow button timeout actions to trigger a subsequent
timeout

This allows visual feedback when timeouts are reached.

In our case, there is a single button which on a normal press/release
will initiate DPP, but longer presses will first reboot then
factory reset. So users know they've held it down long enough,
it's nice to change the state of an LED.

This also fixes what seems to me a bug, in that you can attach
a timeout to a 'release' action but the timing was reset
(so you can't distinguish between a released timeout vs
a pressed timeout).
---
plug/hotplug.c | 22 +++++++++++++++++++++-
1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/plug/hotplug.c b/plug/hotplug.c
index 53f3383..7bdb0b7 100644
--- a/plug/hotplug.c
+++ b/plug/hotplug.c
@@ -361,6 +361,17 @@ static struct cmd_handler {
},
};

+/* This is a non-public handler used when a button event
+ * return a non-zero value, which is used as a timeout
+ * to subsequently call the same script.
+ */
+static struct cmd_handler BUTTON_TIMEOUT_HANDLER = {
+ .name = "button_timeout",
+ .handler = handle_exec,
+ .complete = handle_button_complete,
+};
+
+
static void queue_next(void)
{
struct cmd_queue *c;
@@ -441,13 +452,15 @@ static void handle_button_timeout(struct uloop_timeout *t)
blobmsg_add_string(&button_buf, "ACTION", "timeout");
snprintf(seen, sizeof(seen), "%d", b->seen);
blobmsg_add_string(&button_buf, "SEEN", seen);
- queue_add(&handlers[HANDLER_EXEC], button_buf.head, b->data);
+ queue_add(&BUTTON_TIMEOUT_HANDLER, button_buf.head, b->data);
button_free(b);
}

static void handle_button_complete(struct blob_attr *msg, struct blob_attr *data, int ret)
{
char *name = hotplug_msg_find_var(msg, "BUTTON");
+ char *action = hotplug_msg_find_var(msg, "ACTION");
+ char *seen = hotplug_msg_find_var(msg, "SEEN");
struct button_timeout *b;
int timeout = ret >> 8;

@@ -465,6 +478,13 @@ static void handle_button_complete(struct blob_attr *msg, struct blob_attr *data
b->name = strdup(name);
b->seen = timeout;

+ if (action && seen && (!strcmp(action, "timeout") || !strcmp(action, "released"))) {
+ /* If this happening due to a previous timeout or a released action,
+ * then we should add on the previous SEEN time.
+ */
+ b->seen += atoi(seen);
+ }
+
memcpy(b->data, data, blob_pad_len(data));
b->timeout.cb = handle_button_timeout;

--
2.25.1

0 comments on commit 5a6c980

Please sign in to comment.