openwrt的button机制

Attach functions to a push button

There several ways for controlling buttons in OpenWrt.

Kernel configuration
If a target platform is known to support buttons, appropriate kernel modules are selected by default.
If a platform is not known to support buttons, you are required to install various kernel modules yourself such as diag,input-gpio-buttonsgpio-button-hotplug, and others.
However, installing various modules will not necessarily yield a successful result.

procd buttons

native button handling in procd is handled by scripts in '/etc/rc.button/*'

These scripts receive the same environment as older style hotplug buttons received. However, the script files have to be named after the button. I am unaware of a way of getting the button name. (Other than using hotplug compatible scripts with procd ;)

Button ActionScript EnvironmentScript return value
PressACTION="pressed"Seconds before "timeout"
Held "timeout" secondsACTION="timeout" SEEN="<timeout secs>"n/a
Release1ACTION="released" SEEN="<seconds held>"n/a

1 - "released" action is sent on release even if "timeout" has been sent.

Hotplug Buttons

Note that after the introduction of procd into OpenWrt in r37132 the package hotplug2 has been removed from the default packages. However at the time of writing, r37336: procd: make old button hotplug rules work until all packages are migrated is still in effect. See also procd.buttons
FIXMEPlease read the articles wifitogglebuttons and nslu2.hardware.button and eventually merge them into this one article

Preliminary steps

The cat /sys/kernel/debug/gpio command queries the current pin state. Lo, low level, hi is high level

The first step is to find out the internal name of the button you want to use: some images use generic names such as BTN_1BTN_2, others have more specific ones like reset,wps, etc. Run the following:

# mkdir -p /etc/hotplug.d/button

Create the file /etc/hotplug.d/button/buttons with your favorite text editor, paste the following:

#!/bin/shlogger the button was $BUTTON and the action was $ACTION

Save and exit. Now press the button you want to use, then run logread.

Jan 1 00:01:15 OpenWrt user.notice root: BTN_1   
Jan 1 00:01:15 OpenWrt user.notice root: pressed   
Jan 1 00:01:16 OpenWrt user.notice root: BTN_1    
Jan 1 00:01:16 OpenWrt user.notice root: released

BTN_1 is the name of the button you want to use. If you want or need to use another button, replace every instance of BTN_1 in the rest of this document with the correct text. From now on, there are several possible approaches: the first uses the 00-button script from the atheros target, the other a simpler shell script.

notice

If you want to run programs from hotplug's scripts you need to be sure PATH and the like are initialized properly, scripts invoked by hotplug only have a default env. Especially if you install stuff into nonstandard locations like /opt/usr/bin. It's possible by adding . /etc/profile after #!/bin/sh

#!/bin/sh. /etc/profile

Using Atheros' 00-button + UCI

If you've installed the full version of wget, run the following:

# wget -O /etc/hotplug.d/button/00-button https://dev.openwrt.org/export/36332/trunk/target/linux/atheros/base-files/etc/hotplug.d/button/00-button

If you only have wget-nossl and don't want to or can't upgrade, create /etc/hotplug.d/button/00-button with your favorite editor, then paste the following:

#!/bin/sh. /lib/functions.sh
do_button () {
        local button
        local action        local handler        local min        local max
 
        config_get button $1 button
        config_get action $1 action
        config_get handler $1 handler
        config_get min $1 min
        config_get max $1 max
         [ "$ACTION" = "$action" -a "$BUTTON" = "$button" -a -n "$handler" ] && {
                [ -z "$min" -o -z "$max" ] && eval $handler
                [ -n "$min" -a -n "$max" ] && {
                        [ $min -le $SEEN -a $max -ge $SEEN ] && eval $handler
                }
        }} 
config_load system
config_foreach do_button button

Please note that after r34793 /etc/functions.sh → /lib/functions.sh so if you are using an old version change it!

Save and exit, then issue these commands:

uci add system button    uci set system.@button[-1].button=BTN_1
uci set system.@button[-1].action=pressed
uci set system.@button[-1].handler='logger BTN_1 pressed'uci commit system

button is the name as the buttonaction is the event (two values: pressed and released), handler contains the command line to be run when the event is detected (can be a script as well).

You may need to reboot the router the make the change effective (mine would work with the simple shell script just fine but wouldn't budge when using the 00-button script — Frex 2011/03/25 22:29). If this works, you can change the handler to something more useful, and add more button handlers.

Examples

Example 1: Toggle Wi-Fi radio with a button press

uci add system button    uci set system.@button[-1].button=wps    
uci set system.@button[-1].action=pressed
uci set system.@button[-1].handler='uci set wireless.@wifi-device[0].disabled=1 && wifi'uci commit system

Example 2: Assign two different functions to the same button: short press VS long press. This relies on tracking the released event rather than the pressed event.

uci add system buttonuci set system.@button[-1].button=BTN_1
uci set system.@button[-1].action=released
uci set system.@button[-1].handler='logger timed pressed: 0-3s'uci set system.@button[-1].min=0uci set system.@button[-1].max=3uci add system buttonuci set system.@button[-1].button=BTN_1
uci set system.@button[-1].action=released
uci set system.@button[-1].handler='logger timed pressed: 8-10s'uci set system.@button[-1].min=8uci set system.@button[-1].max=10uci commit system

Example 3: Unmount USB storage using a long-ish press

uci add system buttonuci set system.@button[-1].button=BTN_1
uci set system.@button[-1].action=released
uci set system.@button[-1].handler="for i in \$(mount | awk '/dev\/sd[b-z]/ { print \$1}'); do umount \$i; done"uci set system.@button[-1].min=5uci set system.@button[-1].max=10uci commit system

Example 4: Restore defaults

config button
        option button   reset
        option action   released
        option handler  "firstboot && reboot"
        option min              5
        option max             30

Example 5: Toggle Wi-Fi using a script

config button
        option button   wps
        option action   released
        option handler  "/usr/bin/wifionoff"
        option min      0
        option max      3

You'll have to create the file /usr/bin/wifionoff and paste this:

#!/bin/shSW=$(uci -q get wireless.@wifi-device[0].disabled)[ "$SW" == "1" ] && uci set wireless.@wifi-device[0].disabled=0[ "$SW" == "1" ] || uci set wireless.@wifi-device[0].disabled=1wifi

Another option for wifionoff is this script (doesn't store the state in uci, so it remains what is set in the configuration) You can also call this script eg. from cron, to switch off your wifi at night.

#!/bin/shSTATEFILE="/tmp/wifionoff.state" if [ $# -eq 1 ]; then
  case $1 in
    "up"|"on")
      STATE=off      ;;
    "down"|"off")
      STATE=on      ;;
  esacelse
  if [ ! -e ${STATEFILE} ]; then
    STATE=on  else
    . ${STATEFILE}
  fifiif [ -z ${STATE} ]; then
  STATE=onfi if [ ${STATE} == "on" ]; then
  /sbin/wifi down  STATE=offelse
  /sbin/wifi up  STATE=onfi echo "STATE=${STATE}" > ${STATEFILE}

Example 6: Set transmission-daemon alt-speed, enable or disable.Short press will activate alt-speed or longer press will deactivate alt-speed and also turns on qss led about speed status on tl-wr1043nd

Edit your alt-speed limits from transmission-daemon , settings.json file.To execute script, you need to install transmission-remote package from opkg.

uci add system button    uci set system.@button[-1].button=BTN_1
uci set system.@button[-1].action=pressed
uci set system.@button[-1].handler='transmission-remote -as'uci add system button    uci set system.@button[-1].button=BTN_1
uci set system.@button[-1].action=pressed
uci set system.@button[-1].handler='echo 1 > /sys/class/leds/tl-wr1043nd:green:qss/brightness'uci add system buttonuci set system.@button[-1].button=BTN_1
uci set system.@button[-1].action=released
uci set system.@button[-1].handler='transmission-remote -AS'uci set system.@button[-1].min=1uci set system.@button[-1].max=4uci add system buttonuci set system.@button[-1].button=BTN_1
uci set system.@button[-1].action=released
uci set system.@button[-1].handler='echo 0 > /sys/class/leds/tl-wr1043nd:green:qss/brightness'uci set system.@button[-1].min=1uci set system.@button[-1].max=4uci commit system

Leftovers from a previous version

FIXME

mkdir -p /etc/hotplug.d/buttontouch /etc/hotplug.d/button/00-button
if [ "$ACTION" = "pressed" ]; then
    if [ "$BUTTON" = "BTN_0" ]; then BTN_0    elif [ "$BUTTON" = "BTN_1" ]; then BTN_1    fi fi
mkdir -p /etc/hotplug.d/buttonwget -O /etc/hotplug.d/button/00-button http://dev.openwrt.org/export/21216/trunk/target/linux/atheros/base-files/etc/hotplug.d/button/00-button wget -O http://dev.openwrt.org/export/21216/trunk/target/linux/atheros/base-files/etc/hotplug.d/button/00-button
#!/bin/sh[ "$BUTTON" = "BTN_1" ] && [ "$ACTION" = "pressed" ] && {SW=$(uci get wireless.@wifi-device[0].disabled)[ $SW == '0' ] && uci set wireless.@wifi-device[0].disabled=1[ $SW == '0' ] || uci set wireless.@wifi-device[0].disabled=0wifi}

WR1043ND

If you decide to use the wifitoggle package, you will need to change a few things on the default configuration. The following will work and make the QSS led blink "slowly" when wifi is on:

uci show wifitoggle
uci set wifitoggle.@wifitoggle[0]=wifitoggle
uci set wifitoggle.@wifitoggle[0].led_enable_trigger=timer
uci set wifitoggle.@wifitoggle[0].persistent=1uci set wifitoggle.@wifitoggle[0].button=BTN_1
uci set wifitoggle.@wifitoggle[0].led_sysfs=tl-wr1043nd:green:qss
uci set wifitoggle.@wifitoggle[0].led_enable_delayon=2000uci set wifitoggle.@wifitoggle[0].led_disable_default=1uci set wifitoggle.@wifitoggle[0].led_enable_delayoff=3000uci set wifitoggle.@wifitoggle[0].timer=0

:!: You can probably get similar behaviour with phy0tpt trigger.

HID buttons

triggerhappy

To manage the router buttons and also other HID buttons (i.e pad buttons or keys of an usb device) we can use an application like triggerhappy.

Installation
  1. Install the triggerhappy package and the kmod-hid kernel module

  2. list your available buttons: execute

    thd --dump /dev/input/event*

    press your buttons 
    EV_KEY  KEY_WPS_BUTTON  1       /dev/input/event0 # KEY_WPS_BUTTON        1       command EV_KEY  KEY_WPS_BUTTON  0       /dev/input/event0 # KEY_WPS_BUTTON        0       command EV_KEY  KEY_VOLUMEDOWN  1       /dev/input/event1 # KEY_VOLUMEDOWN        1       command EV_KEY  KEY_VOLUMEDOWN  0       /dev/input/event1 # KEY_VOLUMEDOWN        0       command

  3. Now associate your buttons to commands or scripts 
    path /etc/triggerhappy/triggers.d/example.conf

    KEY_WPS_BUTTON 1 /etc/mywifiscript.sh
    KEY_VOLUMEUP 1 amixer -q set Speaker 3%+
    KEY_VOLUMEDOWN 1 amixer -q set Speaker 3%-
  4. run triggerhappy 

    /etc/init.d/triggerhappy start
  5. enable triggerhappy permanently

    /etc/init.d/triggerhappy enable

Notes

  • triggerhappy repeats commands twice: see bug https://dev.openwrt.org/ticket/14995

  • kernel modules: kmod-hid and kmod-hid-generic both should be installed
    The kmod-hid-generic kernel module must be installed for buttons on USB devices such as USB sound cards to work in OpenWrt trunk. Only then the /dev/input/event0 node for the buttons was created on the DIR-505 router with attached USB sound card.

    [   31.720000] input: C-Media USB Headphone Set   as /devices/platform/ehci-platform/usb1/1-1/1-1:1.3/input/input0
    [   31.760000] hid-generic 0003:0D8C:000C.0001: input,hidraw0: USB HID v1.00 Device [C-Media USB Headphone Set  ] on usb-ehci-platform-1/input3
    [   31.800000] usbcore: registered new interface driver usbhid
    [   31.800000] usbhid: USB HID core driver

    This is also noted in https://dev.openwrt.org/ticket/12631

cmdpad

Another simpler application to manage buttons.

original text:http://bi.du.haochici.xyz/browse.php?u=9edoPwh0ar%2FhtXlJ%2Fi1D0z42oVUjVm61%2FTsWdC26vfy8cmeN3%2B62Dl4TDp6Mew%3D%3D&b=13



本文转自 Linux_woniu 51CTO博客,原文链接:http://blog.51cto.com/linuxcgi/2047945


再次更新,添加RGA100支持,添加LED触发器类型, 删除了按钮脚本,要自定义功能自己写脚本放在/etc/hotplud.d/button/ 下面 openwrt-RG100A_DB120-squashfs-cfe.bin http://115.com/file/c2bjz3px# 我的DB120-WG,双UBS,看着那么多的LED无法使用,于是泡论坛,看教程, 经过无数次的make, make V=99,终于修正了DB120的所有LED驱动,共9个LED, power和internet为双色LED,触发用time, 调整红色和绿色分量,可以显示绿色,橙色,红色等, 美中不足的是红灯太亮了,绿灯太弱,有条件的换下LED限流电阻 2012-2-1 增加了3个按钮 BTN_0 RESET 按住8秒后放开,系统复位 BTN_1 WLAN 无线开关 BTN_2 WPS umount 以后不要手贱,随便捅菊花了,结果你懂的 基本完美了,发挥你的想象吧 集成的软件都是我自己要用的,如motion做监控,图片保存在移动硬盘上, 当画面有变化时 mutt和ssmtp 发送邮件到139邮箱,有实时短信提醒. 通过N2N,配合视频监控软件可以随时随地查看家里状况 看到有个帖子里面用用Mplayer做网络收音机,又塞了个mplayer进去 集成USB声卡驱动,基本影音全能了 基于官方 OpenWrt Backfire 10.03.1 编译而成 软件包名称 版本 alsa-lib 1.0.24.1-1 alsa-utils 1.0.24.2-1 base-files 43.32-r29685 block-mount 0.1.0-2.2 bridge 1.4-1 busybox 1.15.3-3.4 bzip2 1.0.6-1 crda 1.1.1-1 dnsmasq 2.55-6.1 dropbear 0.53.1-5 firewall 2-34.8 gpioctl 1.0-1 hd-idle 1.03-1 hotplug2 1.0-beta-3 iptables 1.4.6-3.1 iptables-mod-conntrack 1.4.6-3.1 iptables-mod-conntrack-extra 1.4.6-3.1 iptables-mod-filter 1.4.6-3.1 iptables-mod-imq 1.4.6-3.1 iptables-mod-ipopt 1.4.6-3.1 iptables-mod-nat 1.4.6-3.1 iw 0.9.22-2 kernel 2.6.32.27-1 kmod-b43 2.6.32.27+2011-12-01-1 kmod-button-hotplug 2.6.32.27-1 kmod-cfg80211 2.6.32.27+2011-12-01-1 kmod-crc-ccitt 2.6.32.27-1 kmod-crypto-aes 2.6.32.27-1 kmod-crypto-arc4 2.6.32.27-1 kmod-crypto-core 2.6.32.27-1 kmod-fs-ext2 2.6.32.27-1 kmod-fs-ext3 2.6.32.27-1 kmod-fuse 2.6.32.27-1 kmod-i2c-core 2.6.32.27-1 kmod-input-core 2.6.32.27-1 kmod-input-gpio-buttons 2.6.32.27-1 kmod-input-polldev 2.6.32.27-1 kmod-ipt-conntrack 2.6.32.27-1 kmod-ipt-conntrack-extra 2.6.32.27-1 kmod-ipt-core 2.6.32.27-1 kmod-ipt-filter 2.6.32.27-1 kmod-ipt-imq 2.6.32.27-1 kmod-ipt-ipopt 2.6.32.27-1 kmod-ipt-nat 2.6.32.27-1 kmod-mac80211 2.6.32.27+2011-12-01-1 kmod-nls-cp437 2.6.32.27-1 kmod-nls-iso8859-1 2.6.32.27-1 kmod-nls-utf8 2.6.32.27-1 kmod-ppp 2.6.32.27-1 kmod-sched 2.6.32.27-1 kmod-scsi-core 2.6.32.27-1 kmod-sound-core 2.6.32.27-1 kmod-switch 2.6.32.27-4 kmod-textsearch 2.6.32.27-1 kmod-tun 2.6.32.27-1 kmod-usb-audio 2.6.32.27-1 kmod-usb-core 2.6.32.27-1 kmod-usb-ohci 2.6.32.27-1 kmod-usb-printer 2.6.32.27-1 kmod-usb-storage 2.6.32.27-1 kmod-usb-uhci 2.6.32.27-1 kmod-usb2 2.6.32.27-1 kmod-video-core 2.6.32.27-1 kmod-video-uvc 2.6.32.27-1 kmod-zd1211rw 2.6.32.27+2011-12-01-1 lame-lib 398-2-3 libao 1.1.0-1 libc 0.9.30.1-43.32 libevent 1.4.14b-1 libfaad2 2.7-1 libffmpeg 0.5.4-2 libfuse 2.8.3-1 libgcc 4.3.3+cs-43.32 libgsm 1.0.13-1 libiconv 5 libiconv-full 1.11.1-1 libid3tag 0.15.1b-3 libip4tc 1.4.6-3.1 libiwinfo 18 libiwinfo-lua 18 libjpeg 6b-1 libltdl 2.4-1 liblua 5.1.4-7 liblzo 2.04-1 libmad 0.15.1b-3 libncurses 5.7-2 libnl-tiny 0.1-1 libogg 1.1.4-2 libopenssl 0.9.8s-1 libpthread 0.9.30.1-43.32 librrd1 1.0.50-1 librt 0.9.30.1-43.32 libsamplerate 0.1.7-1 libsndfile 1.0.21-1 libuci 12012009.7-4 libuci-lua 12012009.7-4 libusb-1.0 1.0.8-1 libv4l 0.6.1-1 libvorbis 1.2.3-1 libvorbisidec 1.0.2+svn14261-1 libxtables 1.4.6-3.1 lua 5.1.4-7 luci 0.10.0-1 luci-app-firewall 0.10.0-1 luci-app-hd-idle 0.10.0-1 luci-app-ntpc 0.10.0-1 luci-app-qos 0.10.0-1 luci-app-samba 0.10.0-1 luci-app-voice-core 0.10.0-1 luci-i18n-chinese 0.10.0-1 luci-i18n-english 0.10.0-1 luci-lib-core 0.10.0-1 luci-lib-ipkg 0.10.0-1 luci-lib-lmo 0.10.0-1 luci-lib-lucid 0.10.0-1 luci-lib-lucid-http 0.10.0-1 luci-lib-nixio 0.10.0-1 luci-lib-px5g 0.10.0-1 luci-lib-sys 0.10.0-1 luci-lib-web 0.10.0-1 luci-mod-admin-core 0.10.0-1 luci-mod-admin-full 0.10.0-1 luci-proto-core 0.10.0-1 luci-proto-ppp 0.10.0-1 luci-sgi-cgi 0.10.0-1 luci-theme-base 0.10.0-1 luci-theme-openwrt 0.10.0-1 mjpg-streamer r136-1 motion 3.2.11.1-1 mtd 13 mutt 1.5.21-1 n2n 3875-1 ntfs-3g 2011.4.12-1-fuseext ntpclient 2007_365-4 openssl-util 0.9.8s-1 opkg 576-2 qos-scripts 1.2.1-3.2 resolveip 1 samba3 3.0.24-8 screen 4.0.3-2 sox 14.0.1-3 ssmtp 2.64-3 tc 2.6.29-1-2 uci 12012009.7-4 udevtrigger 106-1 uhttpd 28 usbutils 003-1 wireless-tools 29-4 wpad-mini 20111103-2 zlib 1.2.3-5
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值