File: state-funcs

package info (click to toggle)
acpi-support 0.143-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 828 kB
  • sloc: sh: 816; ansic: 131; makefile: 38
file content (103 lines) | stat: -rwxr-xr-x 2,703 bytes parent folder | download | duplicates (2)
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
# Paul Sladen, 2006-03-28, 2007-03-26
# Luca Niccoli <lultimouomo@gmail.com>, 2010-06-23
# Cristian Ionescu-Idbohrn <cristian.ionescu-idbohrn@axis.com>, 2010-12-14
# Library functions to check/change status of wireless

# Setup WLAN_RFKILLS list
WLAN_RFKILLS=
for r in /sys/class/rfkill/rfkill*; do
	! read t <$r/type || [ "$t" != wlan ] ||
		WLAN_RFKILLS=${WLAN_RFKILLS:+$WLAN_RFKILLS }$r/state
done

haveDevRfkill() {
	[ -c /dev/rfkill ] && [ -x /usr/sbin/rfkill ]
}

isAnyWirelessPoweredOn() {
	local RFKILL s

	if haveDevRfkill; then
		rfkill list wlan |
			egrep -q "Soft[[:blank:]]+blocked:[[:blank:]]+no"
		return $?
	else
		for RFKILL in $WLAN_RFKILLS ; do
			[ ! -r "$RFKILL" ] || ! read s <$RFKILL ||
				[ "$s" -ne 1 ] || return 0
		done
	fi
	# otherwise return failure
	return 1
}

# Takes no parameters, toggles all wireless devices.
toggleAllWirelessStates() {
	local WIFACE RFKILL get_wifaces zzz=0 max_zzz=7
	local wicd_connect=/usr/share/wicd/daemon/autoconnect.py

	if [ -x /usr/sbin/iw ]; then
		get_wifaces='iw dev | sed -rne "s|^[[:blank:]]+Interface[[:blank:]]+([[:alnum:]]+).*$|\1|p"'
	elif [ -x /sbin/iwconfig ]; then
		get_wifaces='iwconfig 2>/dev/null | grep -o "^[[:alnum:]]*"'
	else
		logger -t${0##*/} -perr -- \
			toggleAllWirelessStates: no way to pick up interfaces
		exit 1
	fi

	if [ -x /sbin/ip ]; then
		if_cmd="ip link set dev"
	elif [ -x /sbin/ifconfig ]; then
		if_cmd=ifconfig
	else
		logger -t${0##*/} -perr -- \
			toggleAllWirelessStates: no way to up/down interfaces
		exit 1
	fi

	# If rfkill is handled by the kernel, don't touch it
	if ! grep -q '^H.*\brfkill\b' /proc/bus/input/devices; then
		if isAnyWirelessPoweredOn; then
			# 'down'ing wireless interfaces, helps with some
			# buggy drivers
			for WIFACE in $(eval $get_wifaces); do
				$if_cmd $WIFACE down 2>/dev/null || :
			done
			if haveDevRfkill; then
				rfkill block wlan
			else
				for RFKILL in $WLAN_RFKILLS; do
					[ ! -w $RFKILL ] || echo 0 >$RFKILL
				done
			fi
		else
			if haveDevRfkill; then
				rfkill unblock wlan
			else
				for RFKILL in $WLAN_RFKILLS; do
					[ ! -w $RFKILL ] || echo 1 >$RFKILL
				done
			fi
		fi
	fi
	# Is wireless on now? Set the interfaces up and poke wicd
	while ! isAnyWirelessPoweredOn && [ $zzz -lt $max_zzz ]; do
		sleep 1
		zzz=$(($zzz + 1))
	done
	if [ $zzz -lt $max_zzz ]; then
		for WIFACE in $(eval $get_wifaces); do
			$if_cmd $WIFACE up 2>/dev/null || :
		done
		[ ! -x $wicd_connect ] || $wicd_connect
	fi
}

# Pass '1' to blink suspending LED and '0' to stop LED
setLEDThinkpadSuspending() {
	local ibm_led=/proc/acpi/ibm/led

	action=$([ "$1" -ne 0 ] && echo blink || echo off)
	[ ! -w $ibm_led ] || printf '7 %s' $action >$ibm_led
}