rtklib: move to github
[openwrt.org/packages.git] / utils / wifitoggle.hotplug
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# /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

        if ubus list network.wireless >/dev/null 2>/dev/null; then
                ubus call network reload
        else
                wifi
        fi
}


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
		else
			disable
		fi
        }
}

config_load wifitoggle
config_foreach load_wifitoggle wifitoggle

comments