packages/wifitoggle: rename hotplug & uci config files
[openwrt/svn-archive/archive.git] / utils / wifitoggle / files / wifitoggle.hotplug
1 # /bin/sh
2
3 setled() {
4 if [ -e /sys/class/leds/${led_sysfs}/brightness ]
5 then
6 [ $led_default -eq 1 ] && echo 1 >/sys/class/leds/${led_sysfs}/brightness
7 [ $led_default -eq 1 ] || echo 0 >/sys/class/leds/${led_sysfs}/brightness
8
9 echo $led_trigger > /sys/class/leds/${led_sysfs}/trigger
10
11 [ "$led_trigger" == "netdev" -a -n "$led_dev" ] && {
12 echo $led_dev > /sys/class/leds/${led_sysfs}/device_name
13 echo $led_mode > /sys/class/leds/${led_sysfs}/mode
14 }
15
16 [ "$led_trigger" == "timer" ] && {
17 [ -n "$led_delayon" ] && echo $led_delayon > /sys/class/leds/${led_sysfs}/delay_on
18 [ -n "$led_delayoff" ] && echo $led_delayoff > /sys/class/leds/${led_sysfs}/delay_off
19 }
20 else
21 logger -p user.err -t "wifitoggle" "led: ${led_sysfs} not found"
22 fi
23 }
24
25 load_wireless() {
26 disabled="$(uci get wireless.${1}.disabled)"
27 }
28
29 save_wireless() {
30 [ "$device" = "all" -o "$device" = "${1}" ] && {
31 uci set "wireless.${1}.disabled=$disabled"
32 }
33 }
34
35 setwifi() {
36 setled
37
38 config_load wireless
39 config_foreach save_wireless wifi-device
40 wifi
41 }
42
43
44 save_system() {
45 config_get sysfs $1 sysfs
46 [ "$sysfs" = "$led_sysfs" ] && {
47 uci set "system.${1}.trigger=$led_trigger"
48 uci set "system.${1}.dev=$led_dev"
49 uci set "system.${1}.mode=$led_mode"
50 uci set "system.${1}.default=$led_default"
51 uci set "system.${1}.delayon=$led_delayon"
52 uci set "system.${1}.delayoff=$led_delayoff"
53
54 ledset=1
55 }
56 }
57
58 commit_changes() {
59 ledset=""
60
61 config_load system
62 config_foreach save_system led
63
64 [ "$ledset" -eq 1 ] || {
65 section=$(uci add system led)
66 uci set "system.${section}.sysfs=$led_sysfs"
67 uci set "system.${section}.trigger=$led_trigger"
68 uci set "system.${section}.dev=$led_dev"
69 uci set "system.${section}.mode=$led_mode"
70 uci set "system.${section}.default=$led_default"
71 uci set "system.${section}.delayon=$led_delayon"
72 uci set "system.${section}.delayoff=$led_delayoff"
73 }
74
75 uci commit
76 }
77
78 enable() {
79
80 led_trigger="$led_enable_trigger"
81 led_trigger="$led_enable_trigger"
82 led_dev="$led_enable_dev"
83 led_mode="$led_enable_mode"
84 led_default="$led_enable_default"
85 led_delayon="$led_enable_delayon"
86 led_delayoff="$led_enable_delayoff"
87 disabled=0
88
89 setwifi
90
91 [ "$timer" -le 0 -a "$persistent" -eq 1 ] && commit_changes
92
93 logger -p user.info -t "wifitoggle" "device($device) enabled"
94
95 [ "$timer" -gt 0 ] && {
96 sleep "$timer" && disable && rm /tmp/run/wirelesstoggle_${1}.pid &
97 echo $! > /tmp/run/wirelesstoggle_${1}.pid
98 logger -p user.info -t "wifitoggle" "auto-disable timer started($timer seconds)"
99 }
100 }
101
102 disable() {
103
104 led_trigger="$led_disable_trigger"
105 led_trigger="$led_disable_trigger"
106 led_dev="$led_disable_dev"
107 led_mode="$led_disable_mode"
108 led_default="$led_disable_default"
109 led_delayon="$led_disable_delayon"
110 led_delayoff="$led_disable_delayoff"
111 disabled=1
112
113 setwifi
114
115 [ "$timer" -le 0 -a "$persistent" -eq 1 ] && commit_changes
116
117 logger -p user.info -t "wifitoggle" "device($device) disabled"
118 }
119
120
121 load_wifitoggle() {
122 config_get device $1 device "all"
123 config_get button $1 button "wps"
124 config_get action $1 action "pressed"
125 config_get_bool persistent $1 persistent "0"
126 config_get timer $1 timer "0"
127
128 config_get led_sysfs $1 led_sysfs
129
130 config_get led_enable_trigger $1 led_enable_trigger "none"
131 config_get led_enable_dev $1 led_enable_dev
132 config_get led_enable_mode $1 led_enable_mode "link"
133 config_get_bool led_enable_default $1 led_enable_default "1"
134 config_get led_enable_delayon $1 led_enable_delayon
135 config_get led_enable_delayoff $1 led_enable_delayoff
136
137 config_get led_disable_trigger $1 led_disable_trigger "none"
138 config_get led_disable_dev $1 led_disable_dev
139 config_get led_disable_mode $1 led_disable_mode "link"
140 config_get_bool led_disable_default $1 led_disable_default "0"
141 config_get led_disable_delayon $1 led_disable_delayon
142 config_get led_disable_delayoff $1 led_disable_delayoff
143
144 [ "$ACTION" = "$action" -a "$BUTTON" = "$button" ] && {
145
146 [ -f /tmp/run/wirelesstoggle_${1}.pid ] && read PID < /tmp/run/wirelesstoggle_${1}.pid && kill $PID && rm /tmp/run/wirelesstoggle_${1}.pid
147
148 if [ "$device" = "all" ]
149 then
150 config_load wireless
151 config_foreach load_wireless wifi-device
152 else
153 disabled="$(uci get wireless."$device".disabled)"
154 fi
155
156
157 if [ "$disabled" = "1" ]
158 then
159 enable $1
160 elif [ "$disabled" = "0" ]
161 then
162 disable
163 else
164 logger -p user.err -t "wifitoggle" "device($device) not recognized"
165 fi
166 }
167 }
168
169 config_load wifitoggle
170 config_foreach load_wifitoggle wifitoggle