ADB.pm

filter: Statementstmt 61.8% 159 / 257 Branchbran 35.4% 34 / 96 Conditioncond 10.5% 4 / 38 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 70.2% 33 / 47 Podpod 100% 17 / 17 totaltotal 51.5% 247 / 479 CC 66 SCAR 24.8
CC
66
Cov
50%
pair
56.0
forward
42.0
connect
20.0
CRAP 11.9
« ADB/Device.pm ↑ Summary ADB/Shell/Result.pm »
1 package Net::ADB;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Errno qw(EINTR);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use IO::Socket::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Time::HiRes();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use Net::ADB::Device;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Net::ADB::Util qw(wantlist croak_unknown_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Net::ADB::Shell::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 our $VERSION = '0.01';
20 2 our @CARP_NOT = qw(Net::ADB::Device Net::ADB::Shell::Result Net::ADB::Util);
21  
22 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 SHELL_ID_STDIN => 0,
24 SHELL_ID_STDOUT => 1,
25 SHELL_ID_STDERR => 2,
26 SHELL_ID_EXIT => 3,
27 SHELL_ID_CLOSE_STDIN => 4,
28 SHELL_HEADER_SIZE => 5,
29 2 };
30  
31 1 ▶sub new {
Subroutine new: called CC=4 SCAR=14.5
Pod documented
32 1 my ($class, %params) = @_;
33  
34 1 ▶ my $host = delete $params{host} // '127.0.0.1';
Condition50%1/2
delete $params{"host"} // "127.0.0.1"
l!l
01
MC/DC0%0/1
delete $params{"host"} // "127.0.0.1"
delete $params{"host"}
Truth table50%1/2
A
A= delete $params{"host"}
Aresult
11
00
35 1 ▶ my $port = delete $params{port} // 5037;
Condition50%1/2
delete $params{"port"} // 5037
l!l
10
MC/DC0%0/1
delete $params{"port"} // 5037
delete $params{"port"}
Truth table50%1/2
A
A= delete $params{"port"}
Aresult
11
00
36 1 ▶ my $timeout = delete $params{timeout} // 10;
Condition50%1/2
delete $params{"timeout"} // 10
l!l
10
MC/DC0%0/1
delete $params{"timeout"} // 10
delete $params{"timeout"}
Truth table50%1/2
A
A= delete $params{"timeout"}
Aresult
11
00
37  
38 1 my $self = {host => $host, port => $port, timeout => $timeout};
39  
40 1 return bless $self, $class;
41 }
42  
43  
44 # Wire protocol
45  
46 4 ▶sub _open {
Subroutine _open: called CC=2 SCAR=7.0
47 4 my $self = shift;
48  
49 my $socket = IO::Socket::IP->new(
50 PeerHost => $self->{host},
51 PeerPort => $self->{port},
52 Proto => 'tcp',
53 Timeout => $self->{timeout},
54 4 ▶ ) or Carp::croak("Cannot connect to adb server at $self->{host}:$self->{port}: $@");
Branch: 1 branch
BranchTrueFalseTotal
my $socket = "IO::Socket::IP"->new("PeerHost", $self->{"host"}, "PeerPort", $self->{"port"}, "Proto", "tcp", "Timeout", $self->{"timeout"}) or Carp::croak(qq[Cannot connect to adb server at $self->{"host"}:$self->{"port"}: $@]) 4 0 4
55  
56 4 binmode $socket;
57 4 return $socket;
58 }
59  
60 5 ▶sub _send {
Subroutine _send: called CC=5 SCAR=17.6
61 5 my ($self, $socket, $bytes) = @_;
62  
63 5 ▶ $log->tracef('>> %s', $bytes) if $log->is_trace;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::ADB::log->is_trace 0 5 5
64  
65 5 local $SIG{PIPE} = 'IGNORE';
66  
67 5 my $offset = 0;
68  
69 5 ▶ while ($offset < length $bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < length $bytes) 5 5 10
70 5 my $num = syswrite $socket, $bytes, length($bytes) - $offset, $offset;
71  
72 5 ▶ unless (defined $num) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $num) 0 5 5
73 0 ▶ next if $! == EINTR;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 4 0 0 0
74 0 Carp::croak("write error: $!");
75 }
76  
77 5 $offset += $num;
78 }
79  
80 5 return;
81 }
82  
83 15 ▶sub _read {
Subroutine _read: called CC=6 SCAR=19.9
84 15 my ($self, $socket, $length) = @_;
85  
86 15 my $select = IO::Select->new($socket);
87 15 my $buffer = '';
88  
89 15 ▶ while (length $buffer < $length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buffer < $length) 15 15 30
90 15 ▶ $select->can_read($self->{timeout}) or Carp::croak("timeout after $self->{timeout}s");
Branch: 1 branch
BranchTrueFalseTotal
$select->can_read($self->{"timeout"}) or Carp::croak(qq[timeout after $self->{"timeout"}s]) 15 0 15
91  
92 15 my $num = sysread $socket, $buffer, $length - length $buffer, length $buffer;
93  
94 15 ▶ unless (defined $num) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $num) 0 15 15
95 0 ▶ next if $! == EINTR;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 4 0 0 0
96 0 Carp::croak("read error: $!");
97 }
98  
99 15 ▶ Carp::croak(sprintf 'connection closed (got %d of %d bytes)', length $buffer, $length) if $num == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $num == 0 0 15 15
100 }
101  
102 15 return $buffer;
103 }
104  
105 3 ▶sub _read_string {
Subroutine _read_string: called CC=2 SCAR=7.1
106 3 my ($self, $socket) = @_;
107 3 my $length = hex $self->_read($socket, 4);
108 3 ▶ return $length ? $self->_read($socket, $length) : '';
Branch: 1 branch
BranchTrueFalseTotal
$length ? : 3 0 3
109 }
110  
111 5 ▶sub _encode_request {
Subroutine _encode_request: called CC=4 SCAR=14.3
112 5 my $request = shift;
113 5 ▶ Carp::croak('Request is undefined') unless defined $request;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $request 0 5 5
114  
115 5 my $bytes = $request;
116 5 ▶ utf8::downgrade($bytes, 1) or Carp::croak('Request contains wide characters; encode it to bytes first');
Branch: 1 branch
BranchTrueFalseTotal
utf8::downgrade($bytes, 1) or Carp::croak("Request contains wide characters; encode it to bytes first") 5 0 5
117  
118 5 my $length = length $bytes;
119 5 ▶ Carp::croak("Request too long ($length bytes, max 65535)") if $length > 0xFFFF;
Branch: 1 branch
BranchTrueFalseTotal
if $length > 65535 0 5 5
120  
121 5 return sprintf('%04x%s', $length, $bytes);
122 }
123  
124 5 ▶sub _request {
Subroutine _request: called CC=1 SCAR=0.0
125 5 my ($self, $socket, $request) = @_;
126  
127 5 $self->_send($socket, _encode_request($request));
128 5 $self->_check_status($socket);
129  
130 4 return;
131 }
132  
133 5 ▶sub _check_status {
Subroutine _check_status: called CC=3 SCAR=11.3
134 5 my ($self, $socket) = @_;
135  
136 5 my $status = $self->_read($socket, 4);
137  
138 5 ▶ return if $status eq 'OKAY';
Branch: 1 branch
BranchTrueFalseTotal
if $status eq "OKAY" 4 1 5
139  
140 1 ▶ Carp::croak('adb FAIL: ' . $self->_read_string($socket)) if $status eq 'FAIL';
Branch: 1 branch
BranchTrueFalseTotal
if $status eq "FAIL" 1 0 1
141 0 Carp::croak("unexpected status '$status'");
142 }
143  
144  
145 # Device selection
146  
147 2 ▶sub _extract_target {
Subroutine _extract_target: called CC=3 SCAR=11.0
148 2 my $params = shift;
149 2 ▶ my %target = map { exists $params->{$_} ? ($_ => delete $params->{$_}) : () } qw(serial usb tcp);
Branch: 1 branch
BranchTrueFalseTotal
exists $params->{$_} ? : 1 5 6
150  
151 2 ▶ Carp::croak("Use only one of 'serial', 'usb', 'tcp'") if keys %target > 1;
Branch: 1 branch
BranchTrueFalseTotal
if keys %target > 1 0 2 2
152 2 croak_unknown_params($params);
153  
154 2 return %target;
155 }
156  
157 1 ▶sub _host_prefix {
Subroutine _host_prefix: called CC=4 SCAR=23.2
158 1 my ($self, %target) = @_;
159 1 ▶ return "host-serial:$target{serial}" if defined $target{serial};
Branch: 1 branch
BranchTrueFalseTotal
if defined $target{"serial"} 1 0 1
160 0 ▶ return 'host-usb' if $target{usb};
Branch: 1 branch
BranchTrueFalseTotal
if $target{"usb"} 0 0 0
161 0 ▶ return 'host-local' if $target{tcp};
Branch: 1 branch
BranchTrueFalseTotal
if $target{"tcp"} 0 0 0
162 0 return 'host';
163 }
164  
165 1 ▶sub _transport_request {
Subroutine _transport_request: called CC=4 SCAR=14.6
166 1 my ($self, %target) = @_;
167 1 ▶ return "host:transport:$target{serial}" if defined $target{serial};
Branch: 1 branch
BranchTrueFalseTotal
if defined $target{"serial"} 0 1 1
168 1 ▶ return 'host:transport-usb' if $target{usb};
Branch: 1 branch
BranchTrueFalseTotal
if $target{"usb"} 0 1 1
169 1 ▶ return 'host:transport-local' if $target{tcp};
Branch: 1 branch
BranchTrueFalseTotal
if $target{"tcp"} 0 1 1
170 1 return 'host:transport-any';
171 }
172  
173  
174 # Services
175  
176 3 ▶sub _host_service {
Subroutine _host_service: called CC=1 SCAR=0.0
177 3 my ($self, $service) = @_;
178  
179 3 my $socket = $self->_open;
180 3 $self->_request($socket, $service);
181  
182 2 my $response = $self->_read_string($socket);
183 2 close $socket;
184  
185 2 return $response;
186 }
187  
188 0 ▶sub _host_command {
Subroutine _host_command: not called CC=2 SCAR=17.9
189 0 my ($self, $service, %opts) = @_;
190  
191 0 my $socket = $self->_open;
192 0 $self->_request($socket, $service);
193 0 $self->_check_status($socket);
194  
195 0 ▶ my $response = $opts{reply} ? $self->_read_string($socket) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'reply'} ? : 0 0 0
196 0 close $socket;
197  
198 0 return $response;
199 }
200  
201 1 ▶sub _transport {
Subroutine _transport: called CC=1 SCAR=0.0
202 1 my ($self, %target) = @_;
203  
204 1 my $socket = $self->_open;
205 1 $self->_request($socket, $self->_transport_request(%target));
206  
207 1 return $socket;
208 }
209  
210 1 ▶sub _local_service {
Subroutine _local_service: called CC=1 SCAR=0.0
211 1 my ($self, $target, $service) = @_;
212  
213 1 my $socket = $self->_transport(%{$target});
214 1 $self->_request($socket, $service);
215  
216 1 return $socket;
217 }
218  
219  
220 # Server
221  
222 1 ▶sub version { hex shift->_host_service('host:version') }
Subroutine version: called CC=1 SCAR=0.0
Pod documented
223  
224 1 ▶sub devices {
Subroutine devices: called CC=3 SCAR=11.0
Pod documented
225 1 my $self = shift;
226 1 my $raw = $self->_host_service('host:devices-l');
227  
228 1 my @devices = ();
229  
230 1 for my $line (split /\n/, $raw) {
231 3 ▶ next unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 3 3
232 3 push @devices, _parse_device($line);
233 }
234  
235 1 return wantlist(\@devices);
236 }
237  
238 3 ▶sub _parse_device {
Subroutine _parse_device: called CC=2 SCAR=6.9
239 3 my $string = shift;
240  
241 3 my ($serial, $state, @attrs) = split /\s+/, $string;
242  
243 3 my %device = (serial => $serial, state => $state);
244  
245 3 foreach my $attr (@attrs) {
246 10 my ($key, $value) = split(/:/, $attr, 2);
247 10 $device{$key} = $value;
248 }
249  
250 3 return \%device;
251 }
252  
253 0 ▶sub kill {
Subroutine kill: not called CC=1 SCAR=6.9
Pod documented
254 0 my $self = shift;
255 0 my $socket = $self->_open;
256  
257 0 $self->_request($socket, 'host:kill');
258 0 close $socket;
259  
260 0 return 1;
261 }
262  
263  
264 # Device
265  
266 0 ▶sub device {
Subroutine device: not called CC=2 SCAR=17.9
Pod documented
267 0 my ($self, %params) = @_;
268  
269 0 my %target = _extract_target(\%params);
270 0 ▶ my $serial = $target{serial} // $self->serialno(%target);
Condition0%0/3
$target{"serial"} // $self->serialno(%target)
l!l&&r!l&&!r
000
MC/DC0%0/2
$target{"serial"} // $self->serialno(%target)
$target{"serial"} $self->serialno(%target)
Truth table0%0/3
A // B
A= $target{"serial"}B= $self->serialno(%target)
ABresult
1X1
011
000
271  
272 0 return Net::ADB::Device->new(client => $self, serial => $serial);
273 }
274  
275 0 ▶sub features {
Subroutine features: not called CC=1 SCAR=6.9
Pod documented
276 0 my ($self, %params) = @_;
277 0 my %target = _extract_target(\%params);
278 0 my @features = split /,/, $self->_host_service($self->_host_prefix(%target) . ':features');
279 0 return wantlist(\@features);
280 }
281  
282 1 ▶sub state {
Subroutine state: called CC=1 SCAR=0.0
Pod documented
283 1 my ($self, %params) = @_;
284 1 my %target = _extract_target(\%params);
285 1 return $self->_host_service($self->_host_prefix(%target) . ':get-state');
286 }
287  
288 0 ▶sub serialno {
Subroutine serialno: not called CC=1 SCAR=6.9
Pod documented
289 0 my ($self, %params) = @_;
290 0 my %target = _extract_target(\%params);
291 0 return $self->_host_service($self->_host_prefix(%target) . ':get-serialno');
292 }
293  
294 0 ▶sub devpath {
Subroutine devpath: not called CC=1 SCAR=6.9
Pod documented
295 0 my ($self, %params) = @_;
296 0 my %target = _extract_target(\%params);
297 0 return $self->_host_service($self->_host_prefix(%target) . ':get-devpath');
298 }
299  
300  
301 # Port forwarding
302  
303 0 ▶sub forward {
Subroutine forward: not called CC=6 SCAR=37.4
Pod documented
304 0 my ($self, %params) = @_;
305  
306 0 my $local = delete $params{local};
307 0 my $remote = delete $params{remote};
308 0 my $norebind = delete $params{norebind};
309  
310 0 my %target = _extract_target(\%params);
311  
312 0 ▶ Carp::croak("Missing 'local'") unless defined $local && length $local;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $local and length $local 0 0 0
Condition0%0/3
defined $local and length $local
!ll&&!rl&&r
000
MC/DC0%0/2
defined $local and length $local
defined $local length $local
Truth table0%0/3
A and B
A= defined $localB= length $local
ABresult
0X0
100
111
313 0 ▶ Carp::croak("Missing 'remote'") unless defined $remote && length $remote;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $remote and length $remote 0 0 0
Condition0%0/3
defined $remote and length $remote
!ll&&!rl&&r
000
MC/DC0%0/2
defined $remote and length $remote
defined $remote length $remote
Truth table0%0/3
A and B
A= defined $remoteB= length $remote
ABresult
0X0
100
111
314  
315 0 ▶ my $service = $self->_host_prefix(%target) . ':forward:' . ($norebind ? 'norebind:' : '') . "$local;$remote";
Branch: 1 branch
BranchTrueFalseTotal
$norebind ? : 0 0 0
316  
317 # The server replies with the bound local port (useful with "tcp:0")
318 0 return $self->_host_command($service, reply => 1);
319 }
320  
321 0 ▶sub killforward {
Subroutine killforward: not called CC=3 SCAR=24.8
Pod documented
322 0 my ($self, %params) = @_;
323  
324 0 my $local = delete $params{local};
325 0 my %target = _extract_target(\%params);
326  
327 0 ▶ Carp::croak("Missing 'local'") unless defined $local && length $local;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $local and length $local 0 0 0
Condition0%0/3
defined $local and length $local
!ll&&!rl&&r
000
MC/DC0%0/2
defined $local and length $local
defined $local length $local
Truth table0%0/3
A and B
A= defined $localB= length $local
ABresult
0X0
100
111
328  
329 0 $self->_host_command($self->_host_prefix(%target) . ":killforward:$local");
330  
331 0 return 1;
332 }
333  
334 0 ▶sub killforward_all {
Subroutine killforward_all: not called CC=1 SCAR=6.9
Pod documented
335 0 my ($self, %params) = @_;
336 0 my %target = _extract_target(\%params);
337 0 $self->_host_command($self->_host_prefix(%target) . ':killforward-all');
338 0 return 1;
339 }
340  
341 0 ▶sub list_forward {
Subroutine list_forward: not called CC=3 SCAR=24.8
Pod documented
342 0 my ($self, %params) = @_;
343  
344 0 my %target = _extract_target(\%params);
345 0 my $raw = $self->_host_service($self->_host_prefix(%target) . ':list-forward');
346  
347 0 my @res = ();
348  
349 0 for my $line (split /\n/, $raw) {
350 0 ▶ next unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 0 0
351 0 my ($serial, $local, $remote) = split /\s+/, $line, 3;
352 0 push @res, {serial => $serial, local => $local, remote => $remote};
353 }
354  
355 0 return wantlist(\@res);
356 }
357  
358  
359 # Network
360  
361 0 ▶sub connect {
Subroutine connect: not called CC=4 SCAR=30.0
Pod documented
362 0 my ($self, %params) = @_;
363  
364 0 my $host = delete $params{host};
365 0 my $port = delete $params{port};
366  
367 0 ▶ Carp::croak("Missing 'host'") unless defined $host && length $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host and length $host 0 0 0
Condition0%0/3
defined $host and length $host
!ll&&!rl&&r
000
MC/DC0%0/2
defined $host and length $host
defined $host length $host
Truth table0%0/3
A and B
A= defined $hostB= length $host
ABresult
0X0
100
111
368 0 croak_unknown_params(\%params);
369  
370 0 my $response = $self->_host_service('host:connect:' . _tcp_address($host, $port));
371  
372 # The server replies OKAY even when the connection fails: the outcome is in the message
373 0 ▶ Carp::croak("adb connect: $response") unless $response =~ /^(?:already\s)?connected\sto\s/x;
Branch: 1 branch
BranchTrueFalseTotal
unless $response =~ /^(?:already\s)?connected\sto\s/ux 0 0 0
374  
375 0 return $response;
376 }
377  
378 0 ▶sub disconnect {
Subroutine disconnect: not called CC=4 SCAR=30.0
Pod documented
379 0 my ($self, %params) = @_;
380  
381 0 ▶ my $host = delete $params{host} // '';
Condition0%0/2
delete $params{"host"} // ""
l!l
00
MC/DC0%0/1
delete $params{"host"} // ""
delete $params{"host"}
Truth table0%0/2
A
A= delete $params{"host"}
Aresult
11
00
382 0 my $port = delete $params{port};
383  
384 0 ▶ Carp::croak("'port' requires 'host'") if defined $port && !length $host;
Branch: 1 branch
BranchTrueFalseTotal
if defined $port and not length $host 0 0 0
Condition0%0/3
defined $port and not length $host
!ll&&!rl&&r
000
MC/DC0%0/2
defined $port and not length $host
defined $port not length $host
Truth table0%0/3
A and not(B)
A= defined $portB= not length $host
ABresult
0X0
100
111
385 0 croak_unknown_params(\%params);
386  
387 # An empty address disconnects every TCP/IP device
388 0 return $self->_host_service('host:disconnect:' . _tcp_address($host, $port));
389 }
390  
391 0 ▶sub pair {
Subroutine pair: not called CC=7 SCAR=40.3
Pod documented
392 0 my ($self, %params) = @_;
393  
394 0 my $host = delete $params{host};
395 0 my $port = delete $params{port};
396 0 my $code = delete $params{code};
397  
398 0 ▶ Carp::croak("Missing 'host'") unless defined $host && length $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host and length $host 0 0 0
Condition0%0/3
defined $host and length $host
!ll&&!rl&&r
000
MC/DC0%0/2
defined $host and length $host
defined $host length $host
Truth table0%0/3
A and B
A= defined $hostB= length $host
ABresult
0X0
100
111
399 0 ▶ Carp::croak("Missing 'code'") unless defined $code && length $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code and length $code 0 0 0
Condition0%0/3
defined $code and length $code
!ll&&!rl&&r
000
MC/DC0%0/2
defined $code and length $code
defined $code length $code
Truth table0%0/3
A and B
A= defined $codeB= length $code
ABresult
0X0
100
111
400 0 croak_unknown_params(\%params);
401  
402 0 my $socket = $self->_open;
403 0 $self->_request($socket, "host:pair:$code:" . _tcp_address($host, $port));
404  
405 # pair_service replies with FAIL + message on error, or with the bare message on success
406 0 my $head = $self->_read($socket, 4);
407 0 ▶ Carp::croak('adb pair: ' . $self->_read_string($socket)) if $head eq 'FAIL';
Branch: 1 branch
BranchTrueFalseTotal
if $head eq "FAIL" 0 0 0
408 0 ▶ Carp::croak("unexpected reply '$head'") unless $head =~ /^[[:xdigit:]]{4}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $head =~ /^[[:xdigit:]]{4}$/u 0 0 0
409  
410 0 my $response = $self->_read($socket, hex $head);
411 0 close $socket;
412  
413 0 return $response;
414 }
415  
416 0 ▶sub _tcp_address {
Subroutine _tcp_address: not called CC=4 SCAR=30.0
417 0 my ($host, $port) = @_;
418  
419 0 ▶ return $host unless defined $port;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $port 0 0 0
420  
421 # IPv6
422 0 ▶ $host = "[$host]" if $host =~ /:/ && $host !~ /^\[/;
Branch: 1 branch
BranchTrueFalseTotal
if $host =~ /:/u and not $host =~ /^\[/u 0 0 0
Condition0%0/3
$host =~ /:/u and not $host =~ /^\[/u
!ll&&!rl&&r
000
MC/DC0%0/2
$host =~ /:/u and not $host =~ /^\[/u
$host =~ /:/u not $host =~ /^\[/u
Truth table0%0/3
A and not(B)
A= $host =~ /:/uB= not $host =~ /^\[/u
ABresult
0X0
100
111
423  
424 0 return "$host:$port";
425 }
426  
427  
428 # Shell
429  
430 1 ▶sub shell_v2 {
Subroutine shell_v2: called CC=10 SCAR=24.8
Pod documented
431 1 my ($self, %params) = @_;
432  
433 1 my $command = delete $params{command};
434 1 my $timeout = delete $params{timeout};
435 1 my %target = _extract_target(\%params);
436  
437 1 ▶ local $self->{timeout} = $timeout if defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if defined $timeout 0 1 1
438  
439 1 ▶ Carp::croak("Missing 'command'") unless defined $command && length $command;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $command and length $command 0 1 1
Condition33%1/3
defined $command and length $command
!ll&&!rl&&r
001
MC/DC0%0/2
defined $command and length $command
defined $command length $command
Truth table33%1/3
A and B
A= defined $commandB= length $command
ABresult
0X0
100
111
440  
441 1 ▶ $log->debugf('shell %s: %s', $self->_transport_request(%target), $command) if $log->is_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::ADB::log->is_debug 0 1 1
442  
443 1 my $start = Time::HiRes::time;
444 1 my $socket = $self->_local_service(\%target, "shell,v2:$command");
445  
446 1 my %result = (command => $command, stdout => '', stderr => '', exit => undef, duration => undef);
447  
448 1 while (1) {
449  
450 2 my $header = $self->_read($socket, SHELL_HEADER_SIZE);
451  
452 2 my ($id, $length) = unpack 'C V', $header;
453 2 ▶ my $data = $length ? $self->_read($socket, $length) : '';
Branch: 1 branch
BranchTrueFalseTotal
$length ? : 2 0 2
454  
455 2 ▶ if ($id == SHELL_ID_STDOUT) { $result{stdout} .= $data }
Branch: 3 branches
BranchTrueFalseTotal
if ($id == 1) { } 1 1 2
elsif ($id == 2) { } 0 1 1
elsif ($id == 3) { } 1 0 1
456 0 elsif ($id == SHELL_ID_STDERR) { $result{stderr} .= $data }
457 1 elsif ($id == SHELL_ID_EXIT) { $result{exit} = unpack 'C', $data; last }
458 0 else { $log->warning("ignoring unknown shell packet id $id ($length bytes)") }
459  
460 }
461  
462 1 close $socket;
463  
464 1 $result{duration} = Time::HiRes::time - $start;
465  
466 1 my $res = Net::ADB::Shell::Result->new(%result);
467  
468 1 ▶ if ($log->is_debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::ADB::log->is_debug) 0 1 1
469 $log->debugf(
470 'shell exit %d in %.3fs (%d bytes stdout, %d bytes stderr)',
471 $result{exit}, $result{duration},
472 length $result{stdout},
473 length $result{stderr}
474 0 );
475 }
476  
477 1 return $res;
478 }
479 2 1;
480  
481 __END__
« ADB/Device.pm ↑ Summary ADB/Shell/Result.pm »