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
, write to the Free Software Foundation
,
17 # Inc.
, 59 Temple Place
- Suite
330, Boston
, MA
02111-1307, USA.
20 #
Get serial number in case of multiple devices
23 #
If the user has ADB_SERIAL
set, use that
, otherwise default to the
25 set serial
"[getenv ADB_SERIAL]"
26 if { $serial eq
"" } {
27 set status [catch
"exec adb devices |& wc -l" output]
29 perror
"Set ADB_SERIAL in your environment to specify the correct target device!"
38 #
Connect to hostname using adb
40 proc adb_open
{ hostname
} {
46 if {[board_info $hostname
exists shell_prompt
]} {
47 set shell_prompt
[board_info $hostname shell_prompt
]
49 set shell_prompt
"root@android:/ # "
52 if {[board_info $hostname
exists fileid
]} {
53 unset board_info
($hostname
,fileid
)
56 set serial
[adb_serial
]
57 if { $serial ne
"" } {
58 spawn adb
[adb_serial
] shell
62 if { $spawn_id
< 0 } {
63 perror
"invalid spawn id from adb"
68 while { $tries
<= 3 } {
70 -re
".*$shell_prompt.*$" {
71 verbose
"Got prompt\n"
76 warning
"adb shell: timed out trying to connect."
79 perror
"adb shell: got EOF while trying to connect."
87 # perror
"adb shell: couldn't connect after $tries tries."
88 catch
"close -i $spawn_id"
91 set board_info
($hostname
,fileid
) $spawn_id
98 # Download $srcfile to $destfile
on $desthost.
101 proc adb_download
{desthost srcfile destfile
} {
102 set serial
[adb_serial
]
103 verbose
"Removing old executable: adb $serial shell rm $destfile" 3
104 set status [catch
"exec adb $serial shell rm $destfile |& cat" output]
105 verbose
"Downloading: adb $serial shell push $srcfile $destfile" 3
106 set status [catch
"exec adb $serial push $srcfile $destfile |& cat" output]
107 if { $
status == 0 } {
108 verbose
"Copied $srcfile to $destfile" 2
111 verbose
"Download to target failed, $output."
116 proc adb_file
{dest op
args} {
117 set file
[lindex $
args 0]
118 verbose
"Executing command: $op $args" 2
121 set status [catch
"exec adb [adb_serial] shell ls |& cat" out]
124 set status [catch
"exec adb [adb_serial] shell rm $file |& cat" rmout]
127 return [eval remote_raw_file
\"$dest
\" \"$op
\" $
args]
130 proc adb_upload
{desthost srcfile destfile
} {
131 set status [catch
"exec adb [adb_serial] pull $srcfile $destfile |& cat" output]
132 if { $
status == 0 } {
133 verbose
"Copied $srcfile to $destfile" 2
136 verbose
"Upload from $desthost failed, $output."
142 #
Execute "$cmd $args[0]" on $boardname.
144 proc adb_exec
{ boardname cmd
args } {
147 if { [llength $
args] > 0 } {
148 set pargs
[lindex $
args 0]
149 if { [llength $
args] > 1 } {
150 set inp
[lindex $
args 1]
159 #
If CMD sends
any output to stderr
, exec will think it failed. More often
160 # than not that will be true
, but it doesn
't catch the case where there is
161 # no output but the exit code is non-zero.
166 verbose "Executing on $boardname: $cmd $pargs < $inp"
168 # Execute commands only from temporary folder, therefore do "cd" first
169 global android_tmp_dir
170 set status [catch "exec cat $inp | adb [adb_serial] shell cd $android_tmp_dir \&\& \( $cmd $pargs \) \\; echo XYZ\\\$\\\{\?\\\}ZYX |& cat" output]
172 # `status' doesn
't mean much here other than adb worked ok.
173 # What we want is whether $cmd ran ok.
174 if { $status != 0 } {
175 regsub "XYZ(\[0-9\]*)ZYX\n?" $output "" output
176 return [list -1 "adb to $boardname failed for $cmd, $output"]
179 regexp "XYZ(\[0-9\]*)ZYX" $output junk status
180 verbose "adb_exec: status:$status text:$output" 4
181 if { $status eq "" } {
182 return [list -1 "Couldn't
parse adb output
, $output.
"]
184 regsub
"XYZ(\[0-9\]*)ZYX\n?" $output "" output
185 #
Delete one trailing
\n because that is what `exec
' will do and we want
186 # to behave identical to it.
187 regsub "\n$" $output "" output
188 return [list [expr {$status != 0}] $output]