summaryrefslogtreecommitdiffstats
path: root/src/wireless
blob: b5ae1410d717766483e4997d846bebd9ebe215da (plain)
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
# Uses wireless_tools, to check for association to a network.
# wep_check interface [timeout]
wep_check()
{
    local INTERFACE="$1" TIMEOUT="${2:-15}" timeout=0 bssid

    while [[ $timeout -lt "$TIMEOUT" ]]; do
        bssid=$(iwgetid "$INTERFACE" -ra)
        [[ -n "$bssid" && "$bssid" != "00:00:00:00:00:00" ]] && return 0
        sleep 1
        let timeout++
    done
    return 1
}

# Check if a particular network is within range
# find_essid interface essid connection
find_essid() {
    local INTERFACE="$1" ESSID="$2" RETRIES=10 try=0 res scanned
    while [[ "$try" -lt "$RETRIES" ]]; do
        sleep 0.5
        let try++
        found=$(
                res=$(iwlist "$INTERFACE" scan 2>/dev/null)
                [[ -z "$res" ]] && exit 1
                # if results were non-null, process them and exit 0
                echo "$res" | sed -nr 's/^\s+ESSID:"([^"]*)"$/\1/p' | fgrep -xm1 "$ESSID"
               ) && {
                scanned=1
                report_debug find_essid "\"$found\""
                # we only bother with at most 5 successful scans
                if (( try < RETRIES-4 )); then try=$((RETRIES-4)); fi
              }
        if [[ -n "$found" ]]; then
            echo "$found"       # JP: echo literal ESSID
            return 0 # network found
        fi
    done
    if [[ "$scanned" -ne 1 ]]; then
        report_debug find_essid "unable to scan"
    fi
    return 1
}

# Check if a particular network is within range
# find_ap interface ap
find_ap() {
    local INTERFACE="$1" ap=$(echo "$2" | tr 'abcdef' 'ABCDEF') RETRIES=10 try=0 res scanned
    while [[ "$try" -lt "$RETRIES" ]]; do
        sleep 0.5
        let try++
        found=$(
            res=$(iwlist "$INTERFACE" scan 2> /dev/null)
            [[ -z "$res" ]] && exit 1
            # if results were non-null, process them and exit 0
            echo "$res" | sed -nr '/^\s+Cell .. - Address: ([[:xdigit:]:]+)$/ { s//\1/; N; s/(.*)\n\s+ESSID:"([^"]*)"$/\1\t\2/p }' \
            | egrep -m1 "^$ap\t"
            ) && {
                    scanned=1
                    report_debug find_ap "\"$found\""
                    # we only bother with at most 5 successful scans
                    if (( try < RETRIES-4 )); then try=$((RETRIES-4)); fi
                 }
        if [[ -n "$found" ]]; then
            echo "$found" | cut -f2     # JP: echo literal ESSID
            return 0
        fi
    done
    if [[ "$scanned" -ne 1 ]]; then
        report_debug find_ap "unable to scan"
    fi
    return 1
}

set_rf_state() {
    local INTERFACE="$1" state="$2" RFKILL_NAME="$3"

    if [[ "$RFKILL" == "hard" ]]; then
        report_fail "Cannot set state on hardware rfkill switch"
        return 1
    fi
    local path=$(get_rf_path "$INTERFACE" "$RFKILL_NAME") || return 1
    case "$state" in
        enabled)
            echo 1 > "$path/state"
        ;;
        disabled)
            echo 0 > "$path/state"
        ;;
    esac
}

get_rf_path() {
    local INTERFACE="$1" RFKILL_NAME="$2" path

    if [[ -n "$RFKILL_NAME" ]]; then
        for path in /sys/class/rfkill/*; do
            if [[ "$(cat "$path/name")" == "$RFKILL_NAME" ]]; then
                echo "$path"
                return 0
            fi
        done
        report_fail "no rfkill switch with name $RFKILL_NAME"
    else
        path="/sys/class/net/$INTERFACE/rfkill"
        if [[ -d "$path" ]]; then
            echo "$path"
            return 0
        fi
        report_fail "no rfkill switch available on interface $INTERFACE"
    fi
    return 1
}

get_rf_state() {
    local INTERFACE="$1" PROFILE="$2" path state

    path=$(get_rf_path "$INTERFACE" "$RFKILL_NAME") || return 1
    state=$(cat "$path/state")

    case "$state" in
        0|2)
            echo "disabled";;
        1)
            echo "enabled";;
        *)
            echo "$state";;
    esac
}

enable_rf() {
    local INTERFACE="$1" RFKILL="$2" RFKILL_NAME="$3"
    # Enable rfkill if necessary, or fail if it is hardware
    if [[ -n "$RFKILL" ]]; then
        local state=$(get_rf_state "$INTERFACE") || return 1
        if [[ "$state" != "enabled" ]]; then
            if [[ "$RFKILL" == "soft" ]]; then
                set_rf_state "$INTERFACE" enabled $RFKILL_NAME
                sleep 1
            else
                report_fail "radio is disabled on $INTERFACE"
                return 1
            fi
        fi
    fi
}

# vim: ft=sh ts=4 et sw=4: