8528efeed68521d1a1496cdcc0696614dce4b7cb
[openwrt/staging/yousong.git] / package / base-files / files / sbin / wifi
1 #!/bin/sh
2 # Copyright (C) 2006 OpenWrt.org
3
4 . /lib/functions.sh
5
6 usage() {
7 cat <<EOF
8 Usage: $0 [down|detect]
9 enables (default), disables or detects a wifi configuration.
10 EOF
11 exit 1
12 }
13
14 find_net_config() {(
15 local vif="$1"
16 local cfg
17 local ifname
18
19 config_get cfg "$vif" network
20
21 [ -z "$cfg" ] && {
22 include /lib/network
23 scan_interfaces
24
25 config_get ifname "$vif" ifname
26
27 cfg="$(find_config "$ifname")"
28 }
29 [ -z "$cfg" ] && return 0
30 echo "$cfg"
31 )}
32
33
34 bridge_interface() {(
35 local cfg="$1"
36 [ -z "$cfg" ] && return 0
37
38 include /lib/network
39 scan_interfaces
40
41 config_get iftype "$cfg" type
42 [ "$iftype" = bridge ] && config_get "$cfg" ifname
43 prepare_interface_bridge "$cfg"
44 )}
45
46 prepare_key_wep() {
47 local key="$1"
48 local hex=1
49
50 echo -n "$key" | grep -qE "[^a-fA-F0-9]" && hex=0
51 [ "${#key}" -eq 10 -a $hex -eq 1 ] || \
52 [ "${#key}" -eq 26 -a $hex -eq 1 ] || {
53 [ "${key:0:2}" = "s:" ] && key="${key#s:}"
54 key="$(echo -n "$key" | hexdump -ve '1/1 "%02x" ""')"
55 }
56 echo "$key"
57 }
58
59 wifi_fixup_hwmode() {
60 local device="$1"
61 local default="$2"
62 local hwmode hwmode_11n
63
64 config_get channel "$device" channel
65 config_get hwmode "$device" hwmode
66 case "$hwmode" in
67 11bg) hwmode=bg;;
68 11a) hwmode=a;;
69 11b) hwmode=b;;
70 11g) hwmode=g;;
71 11n*)
72 hwmode_11n="${hwmode##11n}"
73 case "$hwmode_11n" in
74 a|g) ;;
75 default) hwmode_11n="$default"
76 esac
77 config_set "$device" hwmode_11n "$hwmode_11n"
78 ;;
79 *)
80 hwmode=
81 if [ "${channel:-0}" -gt 0 ]; then
82 if [ "${channel:-0}" -gt 14 ]; then
83 hwmode=a
84 else
85 hwmode=g
86 fi
87 else
88 hwmode="$default"
89 fi
90 ;;
91 esac
92 config_set "$device" hwmode "$hwmode"
93 }
94
95 wifi_updown() {
96 [ enable = "$1" ] && {
97 wifi_updown disable "$2"
98 scan_wifi
99 }
100 for device in ${2:-$DEVICES}; do (
101 config_get disabled "$device" disabled
102 [ 1 == "$disabled" ] && {
103 echo "'$device' is disabled"
104 set disable
105 }
106 config_get iftype "$device" type
107 if eval "type ${1}_$iftype" 2>/dev/null >/dev/null; then
108 eval "scan_$iftype '$device'"
109 eval "${1}_$iftype '$device'" || echo "$device($iftype): ${1} failed"
110 else
111 echo "$device($iftype): Interface type not supported"
112 fi
113 ); done
114 }
115
116 wifi_detect() {
117 for driver in ${2:-$DRIVERS}; do (
118 if eval "type detect_$driver" 2>/dev/null >/dev/null; then
119 eval "detect_$driver" || echo "$driver: Detect failed" >&2
120 else
121 echo "$driver: Hardware detection not supported" >&2
122 fi
123 ); done
124 }
125
126 start_net() {(
127 local iface="$1"
128 local config="$2"
129 local vifmac="$3"
130
131 [ -f "/var/run/$iface.pid" ] && kill "$(cat /var/run/${iface}.pid)" 2>/dev/null
132 [ -z "$config" ] || {
133 include /lib/network
134 scan_interfaces
135 setup_interface "$iface" "$config" "" "$vifmac"
136 }
137 )}
138
139 set_wifi_up() {
140 local cfg="$1"
141 local ifname="$2"
142 uci_set_state wireless "$cfg" up 1
143 uci_set_state wireless "$cfg" ifname "$ifname"
144 }
145
146 set_wifi_down() {
147 local cfg="$1"
148 local vifs vif vifstr
149
150 [ -f "/var/run/wifi-${cfg}.pid" ] &&
151 kill "$(cat "/var/run/wifi-${cfg}.pid")" 2>/dev/null
152 uci_revert_state wireless "$cfg"
153 config_get vifs "$cfg" vifs
154 for vif in $vifs; do
155 uci_revert_state wireless "$vif"
156 done
157 }
158
159 scan_wifi() {
160 local cfgfile="$1"
161 DEVICES=
162 config_cb() {
163 local type="$1"
164 local section="$2"
165
166 # section start
167 case "$type" in
168 wifi-device)
169 append DEVICES "$section"
170 config_set "$section" vifs ""
171 config_set "$section" ht_capab ""
172 ;;
173 esac
174
175 # section end
176 config_get TYPE "$CONFIG_SECTION" TYPE
177 case "$TYPE" in
178 wifi-iface)
179 config_get device "$CONFIG_SECTION" device
180 config_get vifs "$device" vifs
181 append vifs "$CONFIG_SECTION"
182 config_set "$device" vifs "$vifs"
183 ;;
184 esac
185 }
186 config_load "${cfgfile:-wireless}"
187 }
188
189 DEVICES=
190 DRIVERS=
191 include /lib/wifi
192 scan_wifi
193
194 case "$1" in
195 down) wifi_updown "disable" "$2";;
196 detect) wifi_detect "$2";;
197 --help|help) usage;;
198 *) wifi_updown "enable" "$2";;
199 esac