Skip to content

Instantly share code, notes, and snippets.

@PhrozenByte
Created May 9, 2016 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PhrozenByte/a1ef94e049a93cd5d2a613d4656025b1 to your computer and use it in GitHub Desktop.
Save PhrozenByte/a1ef94e049a93cd5d2a613d4656025b1 to your computer and use it in GitHub Desktop.
Improved uci/hotplug based button handler for OpenWrt
#!/bin/sh
# Improved uci/hotplug based button handler for OpenWrt
#
# Copy this file to /etc/hotplug.d/button/00-button and make it executable (chmod +x …)
# Add button event sections to /etc/config/system, e.g.
# config button
# option button 'wps'
# option action 'released'
# option handler '/sbin/reboot'
# option min 5
# option max 30
#
# Based on https://dev.openwrt.org/browser/trunk/target/linux/atheros/base-files/etc/hotplug.d/button/00-button?rev=12179
# Copyright 2008 John "blogic" Crispin
# Copyright 2016 Daniel Rudolf <www.daniel-rudolf.de>
. /lib/functions.sh
do_button () {
local CONF_BUTTON
local CONF_ACTION
local CONF_HANDLER
local CONF_MIN
local CONF_MAX
config_get CONF_BUTTON "$1" "button"
config_get CONF_ACTION "$1" "action"
config_get CONF_HANDLER "$1" "handler"
config_get CONF_MIN "$1" "min"
config_get CONF_MAX "$1" "max"
if [ "$ACTION" == "$CONF_ACTION" ] && [ "$BUTTON" == "$CONF_BUTTON" ] && [ -n "$CONF_HANDLER" ]; then
if [ -z "$CONF_MIN" ] || [ "$SEEN" -ge "$CONF_MIN" ]; then
if [ -z "$CONF_MAX" ] || [ "$SEEN" -le "$CONF_MAX" ]; then
eval "$CONF_HANDLER"
fi
fi
fi
}
logger "Button $BUTTON $ACTION (seen $SEEN sec.)"
config_load "system"
config_foreach "do_button" "button"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment