~gcollura/phablet-tools/click-buddy

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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/sh
# This program is free software: you can redistribute it and/or modify it
# under the terms of the the GNU General Public License version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the applicable version of the GNU General Public
# License for more details.
#.
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2012 Canonical, Ltd.

if [ -f "$(dirname $0)/shell-adb-common.sh" ]; then
    . "$(dirname $0)/shell-adb-common.sh"
else
    . "/usr/share/phabletutils/shell-adb-common.sh"
fi

export LC_ALL=C

usage() {
cat <<EOF
usage: $0 [OPTIONS]

Copies ACTIVE network manager connection into device

OPTIONS:
  -h	Show this message
  -s    Specify the serial of the device to install
  -n    Select network file
  -t    Timeout for waiting on network connection to become active.
        Default=$WAIT_TIMEOUT

  --skip-setup   Skip setting up a network-manager connection on the target.
  --skip-wait    Skip waiting for the network connection to become active.

EOF
}

OPTION_NETWORK_FILE=""
SKIP_SETUP=""
SKIP_WAIT=""
WAIT_TIMEOUT="80s"

ARGS=$(getopt -o n:s:t:h -l "help,skip-setup,skip-wait,timeout:" -n "$0" -- "$@")
if [ $? -ne 0 ] ; then
    exit 1
fi
eval set -- "$ARGS"

while true; do
    case "$1" in
        -h|--help)
            usage
            exit 0
            ;;
        -n)
            shift;
            OPTION_NETWORK_FILE=$1
            shift;
            ;;
        -s)
            shift;
            SERIAL="$1"
            shift;
            ;;
        -t|--timeout)
            shift;
            WAIT_TIMEOUT=$1
            shift;
            ;;
        --skip-wait)
            shift;
	        SKIP_WAIT=1
            ;;
	    --skip-setup)
            shift;
            SKIP_SETUP=1
            ;;
        --)
            shift;
            break;
            ;;
    esac
done


# exporting ANDROID_SERIAL to support multiple devices connected
# seamlessly
[ -z "$SERIAL" ] || export ANDROID_SERIAL="$SERIAL"

NETWORK_MANAGER=/etc/NetworkManager/system-connections

find_active_network() {
    wireless_adapter="$(nmcli -t -f device,type dev | egrep wireless | cut -d: -f1)"
    if [ -z "$wireless_adapter" ]
    then
        echo "No wireless adapter found, exiting"
        exit 1
    fi
   network_active_uuid="$(LC_ALL=C nmcli -t -f name,uuid,devices,vpn con status | grep $wireless_adapter | egrep ':no$'| cut -d: -f2)"
    if [ -z "$network_active_uuid" ]
    then
        echo "No active wifi network connection, exiting"
        exit 1
    fi
    network_file=$(sudo grep -H "$network_active_uuid" $NETWORK_MANAGER/* | grep uuid | cut -f1 -d:)

    echo "$network_file"
}

setup_connection() {
    if [ -z "$OPTION_NETWORK_FILE" ]; then
        network_file="$(find_active_network)"
    else
        network_file="$OPTION_NETWORK_FILE"
    fi

    if [ ! -f "$network_file" ]
    then
        echo "Network connection file \"$network_file\" cannot be read"
        exit 1
    fi

    echo Network file is $network_file

    if [ ! -r "$network_file" ]; then
        SUDO="sudo"
    fi
    SSID="$($SUDO grep ^ssid= "$network_file"|sed 's/^ssid=//')"
    KEY="$($SUDO grep ^psk= "$network_file"|sed 's/^psk=//')"

    # No PSK ? Let's check for WEP
    [ -z "$KEY" ] && KEY="$(sudo grep ^wep-key0= "$network_file"|sed 's/^wep-key0=//')"

    # Still no key ?
    if [ -z "$KEY" ]; then
        echo "Could neither find WEP nor WPA-PSK key !"
        exit 1
    fi

    echo Provisioning network on device
    adb shell "nmcli device wifi connect ${SSID} password '${KEY}' 2>/dev/null"

    echo
    echo Network setup complete
}

wait_for_network() {
    timeout $WAIT_TIMEOUT sh <<EOF
        while true ; do
	    # adb shell doesn't return errors from the command
	    # also, ping can be blocked like it is in the QA lab. however,
	    # the logic below will ensure that DNS resolution part of the
	    # ping worked, which should be enough to prove to us that the
	    # network stack is ready
            if adb shell ping -c1 launchpad.net | grep PING ; then
                exit 0
            fi
            done
EOF
    if [ $? -ne 0 ] ; then
        echo "Network connection failed to become active."
	exit 1
    fi
}

check_devices

[ -n "$SKIP_SETUP" ] || setup_connection
[ -n "$SKIP_WAIT" ] || wait_for_network