OpenWrt Forum Archive

Topic: [TL-WR1043ND] QSS button as WLAN/IPsec switch

The content of this topic has been archived on 16 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hi,

is it possible to use the QSS button on the TL-WR1043ND as control for WLAN and/or IPsec?

Press QSS button for less than 2 seconds: WLAN starts/stops
Press QSS button for minimum 2 seconds: IPsec starts/stops

The actual status should be announced by the LEDs QSS (IPsec) and WLAN.

IPsec running but not all tunnels up or all down: QSS-LED is flashing
IPsec running and all tunnels up: QSS-LED is on
IPsec stopped: QSS-LED is off

WLAN is on: WLAN-LED is on
WLAN is off: WLAN-LED is off
WLAN data transfer: WLAN-LED is flashing very fast (netdev, 'link rx tx')

Did anybody have already wrote such scripts?

(Last edited by burnersk on 24 Apr 2011, 16:02)

^ first two sections, I reworked that part of the wiki while I was setting up my QSS button. The WLAN LED can be configured very easily from Luci, set the trigger to netdev:wlan0 and check Link ON / TX /RX.

As for using the QSS LED that way, you will likely have to manipulate it using defaulton/none/timer.

Frex wrote:

As for using the QSS LED that way, you will likely have to manipulate it using defaulton/none/timer.

I think it's better leave it unset or off in the system configuration and manually control it in the hotplug event.

/usr/bin/gpioctl dirout 5
/usr/bin/gpioctl set 5
/usr/bin/gpioctl clear 5

Here's how I control wlan by reset button on Fon2201.

/etc/hotplug.d/button/00-button:

. /etc/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" ] && {
        if [ -z "$min" ]; then
            [ -z "$max" -o "$max" -ge $SEEN ] && eval $handler
        elif [ -z "$max" ]; then
            [ $min -le $SEEN ] && eval $handler
        else
            [ $min -le $SEEN -a $max -ge $SEEN ] && eval $handler
        fi
    }
}

#logger "$BUTTON was $ACTION for $SEEN seconds"
config_load system
config_foreach do_button button

/etc/config/system:

config 'button' 'woggle'
    option 'button' 'reset'
    option 'action' 'released'
    option 'handler' '/sbin/woggle'
    option 'min' '0'
    option 'max' '1'

config 'button' 'reset'
    option 'button' 'reset'
    option 'action' 'released'
    option 'handler' 'firstboot && reboot'
    option 'min' '30'
    option 'max' '60'

/sbin/woggle: (make it executable by 'chmod +x /sbin/woggle')

#!/bin/sh
if [ -d /var/run/hostapd-ath0 -o -d /var/run/hostapd-ath1 ]; then
    /usr/bin/logger "WiFi button used: WiFi down"
    /sbin/wifi down
    /usr/bin/gpioctl dirout 2 > /dev/null
    /usr/bin/gpioctl clear 2 > /dev/null
    /usr/bin/gpioctl dirout 1 > /dev/null
    /usr/bin/gpioctl set 1 > /dev/null
else
    /usr/bin/logger "WiFi button used: WiFi up"
    /usr/bin/gpioctl dirout 1 > /dev/null
    /usr/bin/gpioctl clear 1 > /dev/null
    /sbin/uci set wireless.wifi0.disabled=0
    /sbin/wifi up
    /sbin/uci revert wireless.wifi0.disabled
fi

/etc/rc.local: (I have wlan disabled by default)

if [ -d /var/run/hostapd-ath0 -o -d /var/run/hostapd-ath1 ] || {
    /usr/bin/gpioctl dirout 2 > /dev/null
    /usr/bin/gpioctl clear 2 > /dev/null
    /usr/bin/gpioctl dirout 1 > /dev/null
    /usr/bin/gpioctl set 1 > /dev/null
}
exit 0

Fon2201 led gpio no.:

wlan:green - gpio 2
wlan:orange - gpio 1

Hi,

I created a simple LED control script with GPIO (tutorial) but it won't work. Get the "done" message but LED state did not change and the next request status returns the original state.
Any hints?

#!/bin/sh
## Simple LED contol script

# Read from script arguments
LEDID=$1
ACTION=$2

if [ "$LEDID" == "" ]; then
        echo "Missing LED-ID"
        exit 1
fi

# Register LED with GPIO if not exists
if [ ! -d "/sys/class/gpio/gpio$LEDID" ]; then
        echo $LEDID > /sys/class/gpio/export
        if [ ! -d "/sys/class/gpio/gpio$LEDID" ]; then
                echo "LED #$LEDID unknown"
                logger "LED #$LEDID unknown"
                exit 1
        fi
fi

# No Action (2nd argument): print LED status
if [ "$ACTION" == "" ]; then
        echo -n "Current state of LED #$LEDID is: "
        echo in > /sys/class/gpio/gpio$LEDID/direction
        CSTATE=`cat /sys/class/gpio/gpio$LEDID/value`
        if [ "$CSTATE" == "1" ]; then
                echo "on"
        else
                echo "off"
        fi
        exit 0
else
        # Activate Action (2nd argument): turn on LED
        if [ "$ACTION" == "1" ]; then
                echo -n "Trying to turn on LED #$LEDID... "
                echo out > /sys/class/gpio/gpio$LEDID/direction
                echo 1 > /sys/class/gpio/gpio$LEDID/value
                echo in > /sys/class/gpio/gpio$LEDID/direction
                CSTATE1=`cat /sys/class/gpio/gpio$LEDID/value`
                if [ "$CSTATE1" == "1" ]; then
                        echo "done"
                        exit 0
                else
                        echo "failed"
                        exit 1
                fi
        else
                # Deactivate Action (2nd argument): turn off LED
                if [ "$ACTION" == "0" ]; then
                        echo -n "Trying to turn off LED #$LEDID... "
                        echo out > /sys/class/gpio/gpio$LEDID/direction
                        echo 0 > /sys/class/gpio/gpio$LEDID/value
                        echo in > /sys/class/gpio/gpio$LEDID/direction
                        CSTATE0=`cat /sys/class/gpio/gpio$LEDID/value`
                        if [ "$CSTATE0" == "0" ]; then
                                echo "done"
                                exit 0
                        else
                                echo "failed"
                                exit 1
                        fi
                else
                        echo "Unknown action"
                        exit 1
                fi
        fi
fi

echo "You should not be here!"
exit 1

result:

Current state of LED #0 is: on
LED #1 unknown
LED #2 unknown
LED #3 unknown
Current state of LED #4 is: on
LED #5 unknown
Current state of LED #6 is: on
LED #7 unknown
Current state of LED #8 is: on
LED #9 unknown
Current state of LED #10 is: on
Current state of LED #11 is: on
Current state of LED #12 is: on
Current state of LED #13 is: off
Current state of LED #14 is: on
Current state of LED #15 is: on
Current state of LED #16 is: on
Current state of LED #17 is: on
1LED #18 unknown
1LED #19 unknown
Current state of LED #20 is: on
Current state of LED #21 is: on
2LED #22 unknown

Usage:
* Request status: `sh led.sh LED-ID` (sh led.sh 0)
* Set status: `sh led.sh LED-ID LED-STATE` (sh led.sh 0 0)

Edit: "/usr/bin/gpioctl" won't work. /dev/gpio is not available.

(Last edited by burnersk on 29 Apr 2011, 21:18)

LED control script still not working - I do not find my fault. Here is my working "buttons" script:

#!/bin/sh
### /etc/hotplug.d/button/buttons
## Button event script

# Actual unix timestamp
NOW=`date +%s`

wlan_toggle()
{
        logger -t "buttons" -p user.debug "wlan_toggle() called"
        WLANSTATUS=`ifconfig radio0`
        if [ "$WLANSTATUS" == "" ]; then
                logger -t "buttons" -p user.info "wlan_toggle() - WLAN stopped"
        else
                logger -t "buttons" -p user.info "wlan_toggle() - WLAN running"
        fi
}

ipsec_toggle()
{
        logger -t "buttons" -p user.debug "ipsec_toggle() called"
        IPSECSTATUS=`ipsec status`
        if [ "$IPSECSTATUS" == "" ]; then
                logger -t "buttons" -p user.info "ipsec_toggle() - IPsec stopped"
        else
                logger -t "buttons" -p user.info "ipsec_toggle() - IPsec running"
        fi
}

# QSS button
if [ "$BUTTON" == "BTN_1" ]; then
        if [ "$ACTION" == "pressed" ]; then
                logger -t "buttons" -p user.debug "QSS button pressed"
                # Store pressed unix timestamp cache file
                echo -n "$NOW" > /tmp/button.qss.pressed.time
        fi
        if [ "$ACTION" == "released" ]; then
                # Read pressed unix timestamp cache file
                PAST=`cat /tmp/button.qss.pressed.time`
                # Remove pressed unix timestamp cache file
                rm -f /tmp/button.qss.pressed.time
                # Calculate pressed time in seconds - subtract actual unix timestamp with pressed unix timestamp.
                SECS_PRESSED=$(( $NOW - $PAST ))
                logger -t "buttons"  -p user.debug "QSS button released (was pressed for $SECS_PRESSED seconds)."
                if [ "$SECS_PRESSED" -lt "2" ]; then
                        logger -t "buttons" -p user.info "QSS button - wlan_toggle()"
                        wlan_toggle
                else
                        logger -t "buttons" -p user.info "QSS button - ipsec_toggle()"
                        ipsec_toggle
                fi
        fi
fi

(Last edited by burnersk on 6 May 2011, 11:13)

Did nobody have some hints for me to solve the LED-readonly problem?

burnersk wrote:

https://forum.openwrt.org/viewtopic.php … 09#p134309

#!/bin/sh
### /sbin/leds
## Simple LED contol script

# Read from script arguments
LEDID=$1
ACTION=$2

if [ "$LEDID" == "" ]; then
        echo "Missing LED-ID"
        exit 1
fi

# Register LED with GPIO if not exists
if [ ! -d "/sys/class/gpio/gpio$LEDID" ]; then
        echo $LEDID > /sys/class/gpio/export
        if [ ! -d "/sys/class/gpio/gpio$LEDID" ]; then
                echo "LED #$LEDID unknown"
                logger "LED #$LEDID unknown"
                exit 1
        fi
fi

# No Action (2nd argument): print LED status
if [ "$ACTION" == "" ]; then
        echo -n "Current state of LED #$LEDID is: "
        echo in > /sys/class/gpio/gpio$LEDID/direction
        CSTATE=`cat /sys/class/gpio/gpio$LEDID/value`
        if [ "$CSTATE" == "1" ]; then
                echo "on"
        else
                echo "off"
        fi
        exit 0
else
        # Activate Action (2nd argument): turn on LED
        if [ "$ACTION" == "1" ]; then
                echo -n "Trying to turn on LED #$LEDID... "
                echo out > /sys/class/gpio/gpio$LEDID/direction
                echo 1 > /sys/class/gpio/gpio$LEDID/value
                echo in > /sys/class/gpio/gpio$LEDID/direction
                CSTATE1=`cat /sys/class/gpio/gpio$LEDID/value`
                if [ "$CSTATE1" == "1" ]; then
                        echo "done"
                        exit 0
                else
                        echo "failed"
                        exit 1
                fi
        else
                # Deactivate Action (2nd argument): turn off LED
                if [ "$ACTION" == "0" ]; then
                        echo -n "Trying to turn off LED #$LEDID... "
                        echo out > /sys/class/gpio/gpio$LEDID/direction
                        echo 0 > /sys/class/gpio/gpio$LEDID/value
                        echo in > /sys/class/gpio/gpio$LEDID/direction
                        CSTATE0=`cat /sys/class/gpio/gpio$LEDID/value`
                        if [ "$CSTATE0" == "0" ]; then
                                echo "done"
                                exit 0
                        else
                                echo "failed"
                                exit 1
                        fi
                else
                        echo "Unknown action"
                        exit 1
                fi
        fi
fi

echo "You should not be here!"
exit 1

(Last edited by burnersk on 6 May 2011, 11:13)

The discussion might have continued from here.