1 # Copyright (C) 2013-2016 Free Software Foundation, Inc.
3 # This file is part of DejaGnu.
5 # DejaGnu is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 2 of the License, or
8 # (at your option) any later version.
10 # DejaGnu is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with DejaGnu. If not, see <http://www.gnu.org/licenses/>.
19 # Get serial number in case of multiple devices
22 # If the user has ADB_SERIAL set, use that, otherwise default to the
24 set serial "[getenv ADB_SERIAL]"
25 if { $serial eq "" } {
26 set status [catch "exec adb devices |& wc -l" output]
28 perror "Set ADB_SERIAL in your environment to specify the correct target device!"
37 # Connect to hostname using adb
39 proc adb_open { hostname } {
45 if {[board_info $hostname exists shell_prompt]} {
46 set shell_prompt [board_info $hostname shell_prompt]
48 set shell_prompt "root@android:/ # "
51 if {[board_info $hostname exists fileid]} {
52 unset board_info($hostname,fileid)
55 set serial [adb_serial]
56 if { $serial ne "" } {
57 spawn adb [adb_serial] shell
61 if { $spawn_id < 0 } {
62 perror "invalid spawn id from adb"
67 while { $tries <= 3 } {
69 -re ".*$shell_prompt.*$" {
70 verbose "Got prompt\n"
75 warning "adb shell: timed out trying to connect."
78 perror "adb shell: got EOF while trying to connect."
86 # perror "adb shell: couldn't connect after $tries tries."
87 catch "close -i $spawn_id"
90 set board_info($hostname,fileid) $spawn_id
97 # Download $srcfile to $destfile on $desthost.
100 proc adb_download {desthost srcfile destfile} {
101 set serial [adb_serial]
102 verbose "Removing old executable: adb $serial shell rm $destfile" 3
103 set status [catch "exec adb $serial shell rm $destfile |& cat" output]
104 verbose "Downloading: adb $serial shell push $srcfile $destfile" 3
105 set status [catch "exec adb $serial push $srcfile $destfile |& cat" output]
106 if { $status == 0 } {
107 verbose "Copied $srcfile to $destfile" 2
110 verbose "Download to target failed, $output."
115 proc adb_file {dest op args} {
116 set file [lindex $args 0]
117 verbose "Executing command: $op $args" 2
120 set status [catch "exec adb [adb_serial] shell ls |& cat" out]
123 set status [catch "exec adb [adb_serial] shell rm $file |& cat" rmout]
126 return [eval remote_raw_file \"$dest\" \"$op\" $args]
129 proc adb_upload {desthost srcfile destfile} {
130 set status [catch "exec adb [adb_serial] pull $srcfile $destfile |& cat" output]
131 if { $status == 0 } {
132 verbose "Copied $srcfile to $destfile" 2
135 verbose "Upload from $desthost failed, $output."
141 # Execute "$cmd $args[0]" on $boardname.
143 proc adb_exec { boardname cmd args } {
146 if { [llength $args] > 0 } {
147 set pargs [lindex $args 0]
148 if { [llength $args] > 1 } {
149 set inp [lindex $args 1]
158 # If CMD sends any output to stderr, exec will think it failed. More often
159 # than not that will be true, but it doesn't catch the case where there is
160 # no output but the exit code is non-zero.
165 verbose "Executing on $boardname: $cmd $pargs < $inp"
167 # Execute commands only from temporary folder, therefore do "cd" first
168 global android_tmp_dir
169 set status [catch "exec cat $inp | adb [adb_serial] shell cd $android_tmp_dir \&\& \( $cmd $pargs \) \\; echo XYZ\\\$\\\{\?\\\}ZYX |& cat" output]
171 # `status' doesn't mean much here other than adb worked ok.
172 # What we want is whether $cmd ran ok.
173 if { $status != 0 } {
174 regsub "XYZ(\[0-9\]*)ZYX\n?" $output "" output
175 return [list -1 "adb to $boardname failed for $cmd, $output"]
178 regexp "XYZ(\[0-9\]*)ZYX" $output junk status
179 verbose "adb_exec: status:$status text:$output" 4
180 if { $status eq "" } {
181 return [list -1 "Couldn't parse adb output, $output."]
183 regsub "XYZ(\[0-9\]*)ZYX\n?" $output "" output
184 # Delete one trailing \n because that is what `exec' will do and we want
185 # to behave identical to it.
186 regsub "\n$" $output "" output
187 return [list [expr {$status != 0}] $output]