1#!/bin/bash
2
3#
4# Copyright 2015 The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19# Builds and pushes the test vendor library to a connected device and starts
20# logcat with project-specific tag filters. If the --test-channel flag is set,
21# logcat is started in a separate process and the test channel is run in the
22# current shell. The kTestChannelEnabled flag must be set in the vendor manager
23# if the test channel is to be used. Also ensure that 'lunch' has been run for
24# the appropriate device.
25
26if [[ "$#" -ne 2 && "$#" -ne 4 ]]; then
27  echo "Usage:"
28  echo "./build_and_run.sh [/path/to/aosp] [device_name] or"
29  echo "./build_and_run.sh [/path/to/aosp] [device_name] --test-channel [port]"
30  exit 1
31fi
32
33# Exit the script if any command fails.
34set -e
35
36# The home directory for AOSP.
37AOSP_ABS=$1
38# The name of the device to build for.
39DEVICE=$2
40
41# The location of Bluetooth within AOSP.
42BT_REL=/system/bt
43BT_ABS=${AOSP_ABS}${BT_REL}
44
45# The location of the test vendor library.
46TEST_VENDOR_LIB_REL=/vendor_libs/test_vendor_lib
47TEST_VENDOR_LIB_ABS=${BT_ABS}${TEST_VENDOR_LIB_REL}
48
49DEVICE_TARGET_REL=/out/target/product
50DEVICE_TARGET_ABS=${AOSP_ABS}${DEVICE_TARGET_REL}
51
52VENDOR_SYMBOLS_REL=/symbols/system/vendor/lib
53VENDOR_SYMBOLS_ABS=${DEVICE_TARGET_ABS}/${DEVICE}/${VENDOR_SYMBOLS_REL}
54
55# The name of the object built by the test vendor library.
56TEST_VENDOR_LIB=test-vendor.so
57# The name of the regular vendor object to be replaced by $TEST_VENDOR_LIB.
58VENDOR_LIB=libbt-vendor.so
59# The config file specifying controller properties.
60CONTROLLER_PROPERTIES=controller_properties.json
61
62if [[ "$#" -eq 4 && $3 == "--test-channel" ]]; then
63  TEST_CHANNEL_PORT=$4
64  TEST_CHANNEL_REL=/scripts
65  TEST_CHANNEL_ABS=${TEST_VENDOR_LIB_ABS}${TEST_CHANNEL_REL}
66
67  # Start logcat in a subshell.
68  x-terminal-emulator -e "scripts/build_and_run.sh ${AOSP_ABS} ${DEVICE}"
69
70  echo "Setting up build environment."
71  cd ${AOSP_ABS}
72  source build/envsetup.sh
73
74  # Forward local port to the same port on the device.
75  echo "Forwarding port ${TEST_CHANNEL_PORT} to device."
76  adb forward tcp:${TEST_CHANNEL_PORT} tcp:${TEST_CHANNEL_PORT}
77
78  # Turn Bluetooth on. Requires user approval via a dialog on the device.
79  echo "Enabling Bluetooth. Please see dialog on device."
80  adb shell am start -a android.bluetooth.adapter.action.REQUEST_ENABLE
81
82  # Start the test channel once Bluetooth is on and logcat has started.
83  read -p "Press [ENTER] once Bluetooth is enabling AND logcat has started."
84
85  # Start the test channel.
86  python ${TEST_CHANNEL_ABS}/test_channel.py localhost ${TEST_CHANNEL_PORT}
87else
88  echo "Setting up build environment."
89  cd ${AOSP_ABS}
90  source build/envsetup.sh
91
92  echo "Navigating to test vendor library: ${TEST_VENDOR_LIB_ABS}"
93  cd ${TEST_VENDOR_LIB_ABS}
94
95  echo "Building test vendor library."
96  mm
97
98  echo "Remounting device rootfs."
99  adb shell mount -o remount,rw /
100  adb remount
101
102  # Replace the actual vendor library with the test vendor library.
103  mv ${DEVICE_TARGET_ABS}/${DEVICE}/system/lib/${TEST_VENDOR_LIB} \
104    ${VENDOR_SYMBOLS_ABS}/${VENDOR_LIB}
105
106  # Push the test vendor library to the device.
107  echo "Pushing the test vendor library to device: $DEVICE"
108  adb push ${VENDOR_SYMBOLS_ABS}/${VENDOR_LIB} /vendor/lib
109
110  echo "Pushing controller properties."
111  adb push ${TEST_VENDOR_LIB_ABS}/data/${CONTROLLER_PROPERTIES} /etc/bluetooth/
112
113  echo "Pushing libevent."
114  adb push ${DEVICE_TARGET_ABS}/${DEVICE}/system/lib/libevent.so /system/lib/
115
116  echo "Pushing libchrome."
117  adb push ${DEVICE_TARGET_ABS}/${DEVICE}/system/lib/libchrome.so /system/lib/
118
119  # Clear logcat.
120  adb logcat -c
121
122  # Run logcat with filters.
123  adb logcat bt_btif:D bt_btif_core:D bt_hci:D bt_main:D bt_vendor:D \
124   bte_logmsg:D command_packet:D dual_mode_controller:D event_packet:D \
125   hci_transport:D hci_handler:D packet:D packet_stream:D \
126   test_channel_transport:D vendor_manager:D *:S
127fi
128