Root/package/base-files/files/sbin/wifi

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

Archive Download this file



interactive