summarylogtreecommitdiffstats
path: root/wifi.hook
blob: 5f82581764d3f2009991aa3ef82b8599a30109a8 (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
#!/usr/bin/ash

check_net_device () {
    ip link show $1 > /dev/null 2>&1
}

poll_net_device () {
    local device=$1 seconds=${2//[!0-9]}

    [ "${seconds:-x}" = x ] && seconds=10

    check_net_device $device && return 0

    msg "Waiting $seconds seconds for network device $device ..." >&2
    while ! check_net_device $device && [ "$seconds" -gt 0 ]; do
        sleep 1
        seconds=$(( seconds - 1 ))
    done

    check_net_device $device
}

check_wpa_supplicant_done () {
    grep "CTRL-EVENT-CONNECTED" $1 > /dev/null
}

poll_wpa_completion () {
    local logfile=$1 seconds=${2//[!0-9]}

    [ "${seconds:-x}" = x ] && seconds=10

    check_wpa_supplicant_done $logfile && return 0

    msg "Waiting $seconds seconds for wpa_supplicant ..." >&2
    while ! check_wpa_supplicant_done $logfile && [ "$seconds" -gt 0 ]; do
        sleep 1
        seconds=$(( seconds - 1 ))
    done

    check_wpa_supplicant_done $logfile
}

run_hook ()
{
    local device="wlan0"
    local logfile="/tmp-wpa-supplicant-log"

    # wait for wlan-device
    poll_net_device $device 15

    msg "Starting wifi"

    # set wlan-device to up
    ip link set $device up || return 1

    # assocciate with wifi network
    wpa_supplicant -B -i $device -c /etc/wpa_supplicant/initcpio.conf -f $logfile

    # wait for wpa_supplicant
    poll_wpa_completion $logfile 15

    # wlan-device should now be connected and ready to be assigned an ip by the net hook
}

run_cleanuphook ()
{
    local device="wlan0"
    local logfile="/tmp-wpa-supplicant-log"

    # kill wpa_supplicant running in the background
    killall wpa_supplicant

    # set wlan-device link down
    ip link set $device down

    # wlan-device should now be fully disconnected from the wifi network
    rm $logfile
}