add wifitoggle, Script to toogle WiFi with a button and UCI config (closes: #6081)
authorNuno Goncalves <nunojpg@gmail.com>
Thu, 22 Apr 2010 16:31:05 +0000 (16:31 +0000)
committerNuno Goncalves <nunojpg@gmail.com>
Thu, 22 Apr 2010 16:31:05 +0000 (16:31 +0000)
SVN-Revision: 21093

utils/wifitoggle/Makefile [new file with mode: 0644]
utils/wifitoggle/files/etc/config/wifitoggle [new file with mode: 0755]
utils/wifitoggle/files/etc/hotplug.d/button/30-wifitoggle [new file with mode: 0755]

diff --git a/utils/wifitoggle/Makefile b/utils/wifitoggle/Makefile
new file mode 100644 (file)
index 0000000..017b3de
--- /dev/null
@@ -0,0 +1,39 @@
+# 
+# Copyright (C) 2010 segal.di.ubi.pt 
+#
+# This is free software, licensed under the GNU General Public License v2.
+# See /LICENSE for more information.
+#
+
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=wifitoggle
+PKG_VERSION:=1
+PKG_RELEASE:=1
+
+include $(INCLUDE_DIR)/package.mk
+
+define Package/wifitoggle
+  SECTION:=utils
+  CATEGORY:=Utilities
+  TITLE:=Script to toogle WiFi with a button and UCI config
+  MAINTAINER:=Nuno Goncalves <nunojpg@gmail.com>
+endef
+
+define Package/wifitoggle/description
+Very versatile script to toogle WiFi with a button. Allows to set
+timeouts, persist changes after boot, and set leds acording to the state.
+endef
+
+define Package/wifitoggle/conffiles
+/etc/config/wifitoggle
+endef
+
+define Build/Compile
+endef
+
+define Package/wifitoggle/install
+       $(CP) ./files/* $(1)
+endef
+
+$(eval $(call BuildPackage,wifitoggle))
diff --git a/utils/wifitoggle/files/etc/config/wifitoggle b/utils/wifitoggle/files/etc/config/wifitoggle
new file mode 100755 (executable)
index 0000000..f3bec29
--- /dev/null
@@ -0,0 +1,11 @@
+config wifitoggle
+       option button   'wps'
+
+       option persistent       '0' 
+       option timer            '600'
+
+       option led_sysfs                'wrt160nl:amber:wps'
+       option led_enable_trigger       'timer' 
+       option led_enable_delayon       '500'
+       option led_enable_delayoff      '500'
+       option led_disable_default      '0'
diff --git a/utils/wifitoggle/files/etc/hotplug.d/button/30-wifitoggle b/utils/wifitoggle/files/etc/hotplug.d/button/30-wifitoggle
new file mode 100755 (executable)
index 0000000..fdeeb74
--- /dev/null
@@ -0,0 +1,170 @@
+# /bin/sh
+
+setled() {
+        if [ -e /sys/class/leds/${led_sysfs}/brightness ]
+       then
+                [ $led_default -eq 1 ] && echo 1 >/sys/class/leds/${led_sysfs}/brightness
+                [ $led_default -eq 1 ] || echo 0 >/sys/class/leds/${led_sysfs}/brightness
+
+                echo $led_trigger > /sys/class/leds/${led_sysfs}/trigger
+
+                [ "$led_trigger" == "netdev" -a -n "$led_dev" ] && {
+                        echo $led_dev > /sys/class/leds/${led_sysfs}/device_name
+                        echo $led_mode > /sys/class/leds/${led_sysfs}/mode
+                }
+
+                [ "$led_trigger" == "timer" ] && {
+                        [ -n "$led_delayon" ] && echo $led_delayon > /sys/class/leds/${led_sysfs}/delay_on
+                        [ -n "$led_delayoff" ] && echo $led_delayoff > /sys/class/leds/${led_sysfs}/delay_off
+                }
+        else
+               logger -p user.err -t "wifitoggle" "led: ${led_sysfs} not found"
+       fi
+}
+
+load_wireless() {
+       disabled="$(uci get wireless.${1}.disabled)"
+}
+
+save_wireless() {
+       [ "$device" = "all" -o "$device" = "${1}" ] && {
+               uci set "wireless.${1}.disabled=$disabled"
+       }
+}
+
+setwifi() {
+        setled
+
+        config_load wireless
+        config_foreach save_wireless wifi-device
+        wifi
+}
+
+
+save_system() {
+        config_get sysfs $1 sysfs
+        [ "$sysfs" = "$led_sysfs" ] && {
+                uci set "system.${1}.trigger=$led_trigger"
+                uci set "system.${1}.dev=$led_dev"
+                uci set "system.${1}.mode=$led_mode"
+                uci set "system.${1}.default=$led_default"
+                uci set "system.${1}.delayon=$led_delayon"
+                uci set "system.${1}.delayoff=$led_delayoff"
+
+                ledset=1
+        }
+}
+
+commit_changes() {
+        ledset=""
+
+        config_load system
+        config_foreach save_system led
+
+        [ "$ledset" -eq 1 ] || {
+                section=$(uci add system led)
+                uci set "system.${section}.sysfs=$led_sysfs"
+                uci set "system.${section}.trigger=$led_trigger"
+                uci set "system.${section}.dev=$led_dev"
+                uci set "system.${section}.mode=$led_mode"
+                uci set "system.${section}.default=$led_default"
+                uci set "system.${section}.delayon=$led_delayon"
+                uci set "system.${section}.delayoff=$led_delayoff"
+        }
+
+        uci commit
+}
+
+enable() {
+
+       led_trigger="$led_enable_trigger"
+       led_trigger="$led_enable_trigger"
+       led_dev="$led_enable_dev"
+       led_mode="$led_enable_mode"
+       led_default="$led_enable_default"
+       led_delayon="$led_enable_delayon"
+       led_delayoff="$led_enable_delayoff"
+       disabled=0
+       
+       setwifi
+
+       [ "$timer" -le 0 -a "$persistent" -eq 1 ] && commit_changes
+       
+       logger -p user.info -t "wifitoggle" "device($device) enabled"
+
+       [ "$timer" -gt 0 ] && {
+               sleep "$timer" && disable && rm /tmp/run/wirelesstoggle_${1}.pid &
+                echo $! > /tmp/run/wirelesstoggle_${1}.pid
+               logger -p user.info -t "wifitoggle" "auto-disable timer started($timer seconds)"
+       }
+}
+
+disable() {
+
+       led_trigger="$led_disable_trigger"
+       led_trigger="$led_disable_trigger"
+       led_dev="$led_disable_dev"
+       led_mode="$led_disable_mode"
+       led_default="$led_disable_default"
+       led_delayon="$led_disable_delayon"
+       led_delayoff="$led_disable_delayoff"
+       disabled=1
+
+       setwifi
+
+       [ "$timer" -le 0 -a "$persistent" -eq 1 ] && commit_changes
+       
+       logger -p user.info -t "wifitoggle" "device($device) disabled"
+}
+
+
+load_wifitoggle() {
+       config_get device $1 device "all"
+        config_get button $1 button "wps"
+        config_get action $1 action "pressed"
+        config_get_bool persistent $1 persistent "0"
+        config_get timer $1 timer "0"
+
+        config_get led_sysfs $1 led_sysfs
+
+       config_get led_enable_trigger $1 led_enable_trigger "none"
+       config_get led_enable_dev $1 led_enable_dev
+       config_get led_enable_mode $1 led_enable_mode "link"
+       config_get_bool led_enable_default $1 led_enable_default "1"
+       config_get led_enable_delayon $1 led_enable_delayon
+       config_get led_enable_delayoff $1 led_enable_delayoff
+
+       config_get led_disable_trigger $1 led_disable_trigger "none"
+       config_get led_disable_dev $1 led_disable_dev
+       config_get led_disable_mode $1 led_disable_mode "link"
+       config_get_bool led_disable_default $1 led_disable_default "0"
+       config_get led_disable_delayon $1 led_disable_delayon
+       config_get led_disable_delayoff $1 led_disable_delayoff
+
+        [ "$ACTION" = "$action" -a "$BUTTON" = "$button" ] && {
+
+               [ -f /tmp/run/wirelesstoggle_${1}.pid ] && read PID < /tmp/run/wirelesstoggle_${1}.pid && kill $PID && rm /tmp/run/wirelesstoggle_${1}.pid
+
+               if [ "$device" = "all" ]
+               then
+                       config_load wireless
+                       config_foreach load_wireless wifi-device
+               else
+                       disabled="$(uci get wireless."$device".disabled)"
+               fi
+
+
+               if [ "$disabled" = "1" ] 
+               then
+                       enable $1
+               elif [ "$disabled" = "0" ]
+               then
+                       disable
+               else
+                       logger -p user.err -t "wifitoggle" "device($device) not recognized"
+               fi
+        }
+}
+
+config_load wifitoggle
+config_foreach load_wifitoggle wifitoggle