1#!/bin/bash
2
3DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
4
5function menu-adb() {
6    TMP=$(adb devices -l | grep -v "List of device" | awk '{ print $1 }')
7    # TODO(optedoblivion): If the device doesn't have a name (offline), it misnames them
8    NTMP=$(adb devices -l | grep -v "List of device" | awk '{ print $6 }' | cut -d ':' -f 2)
9    SERIALS=($TMP)
10    DEVICES=($NTMP)
11    LEN=${#SERIALS[@]}
12    result=0
13    if [ $LEN -lt 1 ]; then
14        echo "No devices connected!"
15        return 1
16    fi
17
18    if [ "$LEN" == "" ]; then
19        LEN=0
20    fi
21
22    answer=0
23
24    DEVICE_NAME="$1 device"
25
26    if [ $LEN -gt 1 ]; then
27        echo "+-------------------------------------------------+" 1>&2
28        echo "| Choose a ${DEVICE_NAME}:                         " 1>&2
29        echo "+-------------------------------------------------+" 1>&2
30        echo "|                                                 |" 1>&2
31        let fixed_len=$LEN-1
32        for i in `seq 0 $fixed_len`;
33        do
34            serial=${SERIALS[i]}
35            device=${DEVICES[i]}
36            echo "| $i) $serial $device" 1>&2
37            ## TODO[MSB]: Find character count, fill with space and ending box wall
38        done
39        echo "|                                                 |" 1>&2
40        echo "+-------------------------------------------------+" 1>&2
41        echo 1>&2
42        echo -n "Index number: " 1>&2
43        read answer
44    fi
45
46    if [ $answer -ge $LEN ]; then
47        echo
48        echo "Please choose a correct index!" 1>&2
49        echo
50        return 1
51    fi
52
53    SERIAL=${SERIALS[$answer]}
54    echo $SERIAL
55}
56
57function UpFind {
58    while [[ $PWD != / ]] ; do
59        rc=$(find "$PWD" -maxdepth 1 "$@")
60        if [ -n "$rc" ]; then
61            echo $(dirname "$rc")
62            return
63        fi
64        cd ..
65    done
66}
67
68
69function get-android-root() {
70    android_root=$(UpFind -name dalvik -type d)
71    if [[ -z $android_root ]] ; then
72        echo
73        echo "Needs to be ran in the android tree"
74        echo
75        return 1
76    fi
77    echo "${android_root}"
78}
79
80function banner() {
81    echo
82    echo "GD On Device Cert Test"
83    echo
84}
85
86## Main
87banner
88
89DRY_RUN=""
90DO_BUILD=0
91echo "$@"
92if [ $# -gt 0 ]; then
93    for var in "$@"
94    do
95        if [ "$var" == "-h" ]; then
96            echo
97            echo "Usage: $0 [-h|-d]"
98            echo
99            echo "Available Options:"
100            echo "=================="
101            echo " -h | Help(this) Menu"
102            echo " -d | Dry run; just prints commands"
103            echo
104            return 0
105        elif [ "$var" == "-d" ]; then
106            DRY_RUN="echo"
107        elif [ "$var" == "-b" ]; then
108            DO_BUILD=1
109        fi
110    done
111fi
112
113## Verify devices connected and sane
114DUT_SERIAL="$(menu-adb DUT)"
115DUT_ADB="adb -s ${DUT_SERIAL}"
116DUT_NAME="$(adb devices -l | grep -v "List of device" | grep ${DUT_SERIAL} | awk '{ print $6 }' | cut -d ':' -f 2)"
117
118CERT_SERIAL="$(menu-adb CERT)"
119CERT_ADB="adb -s ${CERT_SERIAL}"
120CERT_NAME="$(adb devices -l | grep -v "List of device" | grep ${CERT_SERIAL} | awk '{ print $6 }' | cut -d ':' -f 2)"
121
122if [ "${CERT_SERIAL}" == "${DUT_SERIAL}" ]; then
123    echo
124    echo "ERROR: CERT and DUT cannot be the same device, or you only have one device connected!"
125    echo
126    return 1
127fi
128
129## Start builds
130if [ $DO_BUILD == 1 ]; then
131    $DRY_RUN cd $(get-android-root)
132    $DRY_RUN . build/envsetup.sh
133    #DUT
134    $DRY_RUN lunch $DUT_NAME
135    $DRY_RUN cd $(get-android-root)/system/bt/gd
136    $DRY_RUN mma -j `cat /proc/cpuinfo | grep core | wc -l`
137    $DRY_RUN cd $(get-android-root)
138    # CERT
139    $DRY_RUN lunch $CERT_NAME
140    $DRY_RUN cd $(get-android-root)/system/bt/gd
141    $DRY_RUN mma -j `cat /proc/cpuinfo | grep core | wc -l`
142    $DRY_RUN cd $(get-android-root)
143fi
144
145## Set android devices in config
146pushd .
147cd "${DIR}"
148# Reset in case user chooses different item in menu
149git checkout android_devices_config.json
150popd
151$DRY_RUN sed -i "s/\"DUT\"/\"${DUT_SERIAL}\"/g" ${DIR}/android_devices_config.json
152$DRY_RUN sed -i "s/\"CERT\"/\"${CERT_SERIAL}\"/g" ${DIR}/android_devices_config.json
153
154## ACTS
155#$DRY_RUN source $(get-android-root)/system/bt/gd/cert/set_up_acts.sh
156
157## Start test
158$DRY_RUN $(get-android-root)/system/bt/gd/cert/run
159