forked from depau/OpenWRT-WPS-Button
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwps_pushbutton
executable file
·136 lines (109 loc) · 2.69 KB
/
wps_pushbutton
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
#!/bin/sh
. /lib/functions/leds.sh
# LED configuration
led_wps="d-link:blue:wps"
led_success="d-link:green:status"
led_failure="d-link:amber:status"
led_wps_delay_on=100
led_wps_delay_off=100
# Temporary status file
tmpfile="/tmp/wps_started"
# Helper function definitions
wps_led_blink() {
led_timer $led_wps $led_wps_delay_on $led_wps_delay_off
}
wps_led_off() {
led_off $led_wps
}
led_get_attr() {
[ -f "/sys/class/leds/$1/$2" ] && cat "/sys/class/leds/$1/$2"
}
led_blink_for_result() {
# Backup LED status
failure_brightness=`led_get_attr $led_failure brightness`
success_brightness=`led_get_attr $led_success brightness`
failure_trigger=`led_get_attr $led_failure trigger`
success_trigger=`led_get_attr $led_success trigger`
# Turn off LEDs
led_off $led_failure
led_off $led_success
# Blink LEDs five times
led_timer $1 100 100
sleep 1
led_off $1
# Restore LED status
led_set_attr $led_failure brightness $failure_brightness
led_set_attr $led_success brightness $success_brightness
led_set_attr $led_failure trigger $failure_trigger
led_set_attr $led_success trigger $success_trigger
}
led_blink_success() {
led_blink_for_result $led_success 100 100
}
led_blink_failure() {
led_blink_for_result $led_failure 100 100
}
wps_cancel_if_active() {
hostapd_cli wps_get_status | grep -q "PBC Status: Active"
if [ $? == 0 ]; then
hostapd_cli wps_cancel
return 0
fi
return 1
}
wps_was_started() {
r=`cat $tmpfile 2> /dev/null`
if [ y$r == yrunning ]; then
return 0
fi
return 1
}
wps_mark_started() {
echo "running" > $tmpfile
}
wps_mark_stopped() {
rm $tmpfile
}
##### Actual logic
# Check if WPS is already active; in that case, cancel it
#hostapd_cli wps_get_status
wps_was_started
if [ $? == 0 ]; then
echo "WPS is active, canceling"
hostapd_cli wps_cancel
wps_mark_stopped
wps_led_off
#led_blink_failure
exit
fi
echo "Activating WPS pushbutton"
wps_mark_started
hostapd_cli wps_pbc
# Blink LED
wps_led_blink
# 3 minutes timeout
for i in `seq 1 180`; do
hostapd_cli wps_get_status | grep -q "PBC Status: Active"
if [ $? != 0 ]; then
break
fi
sleep 1
done
# If WPS is still active, shut it down
wps_cancel_if_active
wps_mark_stopped
# Turn OFF WPS LED, blink status LED for success/failure
wps_led_off
hostapd_cli wps_get_status | grep -q "Last WPS result: Success"
ret1=$?
# Timeout is not reported as failure, but we still want to blink the LED in failure
hostapd_cli wps_get_status | grep -qv "PBC Status: Timed-out" && ret2=1 || ret2=0
# If everything went right, both ret1 and ret2 should be 0
if [ `expr $ret1 + $ret2` == 0 ]; then
led_blink_success
echo "WPS pushbutton was successful"
else
led_blink_failure
echo "WPS pushbutton failed"
fi
hostapd_cli wps_get_status