From 9d265fd6b5cf3f1294a41e1818a53b4f4284b1d2 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Fri, 1 Sep 2017 02:05:05 +0300 Subject: [PATCH 1/1] Initial commit --- Changes | 4 + MANIFEST | 7 ++ Makefile.PL | 22 +++++ README | 31 +++++++ lib/Android/ADB.pm | 189 ++++++++++++++++++++++++++++++++++++++ lib/Android/ADB/Device.pm | 63 +++++++++++++ t/Android-ADB.t | 6 ++ 7 files changed, 322 insertions(+) create mode 100644 Changes create mode 100644 MANIFEST create mode 100644 Makefile.PL create mode 100644 README create mode 100644 lib/Android/ADB.pm create mode 100644 lib/Android/ADB/Device.pm create mode 100644 t/Android-ADB.t diff --git a/Changes b/Changes new file mode 100644 index 0000000..9a22867 --- /dev/null +++ b/Changes @@ -0,0 +1,4 @@ +Revision history for Perl extension Android::ADB. + +0.001 2017-09-01T00:05+01:00 Thu Aug 31 01:16:32 2017 + - Initial release diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..8d38ac4 --- /dev/null +++ b/MANIFEST @@ -0,0 +1,7 @@ +Changes +Makefile.PL +MANIFEST +README +t/Android-ADB.t +lib/Android/ADB.pm +lib/Android/ADB/Device.pm diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..72df034 --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,22 @@ +use 5.014000; +use ExtUtils::MakeMaker; + +WriteMakefile( + NAME => 'Android::ADB', + VERSION_FROM => 'lib/Android/ADB.pm', + ABSTRACT_FROM => 'lib/Android/ADB.pm', + AUTHOR => 'Marius Gavrilescu ', + MIN_PERL_VERSION => '5.14.0', + LICENSE => 'perl', + SIGN => 1, + PREREQ_PM => { + qw/Class::Accessor 0 + File::Slurp 0/, + }, + META_ADD => { + dynamic_config => 0, + resources => { + repository => 'https://git.ieval.ro/?p=android-adb.git', + }, + } +); diff --git a/README b/README new file mode 100644 index 0000000..1381e80 --- /dev/null +++ b/README @@ -0,0 +1,31 @@ +Android-ADB version 0.001 +========================= + +This module is a minimal wrapper over the Android Debug Bridge +(adb) command for manipulating Android devices. + +INSTALLATION + +To install this module type the following: + + perl Makefile.PL + make + make test + make install + +DEPENDENCIES + +This module requires these other modules and libraries: + +* Class::Accessor +* File::Slurp + +COPYRIGHT AND LICENCE + +Copyright (C) 2017 by Marius Gavrilescu + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself, either Perl version 5.24.2 or, +at your option, any later version of Perl 5 you may have available. + + diff --git a/lib/Android/ADB.pm b/lib/Android/ADB.pm new file mode 100644 index 0000000..9c0eb99 --- /dev/null +++ b/lib/Android/ADB.pm @@ -0,0 +1,189 @@ +package Android::ADB; + +use 5.014000; +use strict; +use warnings; + +our $VERSION = '0.001'; + +use Android::ADB::Device; +use Carp; +use File::Slurp; +use IPC::Open2; + +sub new { + my ($class, %args) = @_; + $args{path} //= $ENV{ADB}; + $args{path} //= 'adb'; + $args{args} //= []; + bless \%args, $class +} + +sub run { + my ($self, @args) = @_; + my ($out, $in); + my @dev_args = $self->{device_serial} ? ('-s', $self->{device_serial}) : (); + my $pid = open2 $out, $in, $self->{path}, @{$self->{args}}, @args; + my $result = read_file $out; + close $out; + close $in; + waitpid $pid, 0 or croak "$!"; + $result; +} + +sub start_server { shift->run('start-server') } +sub kill_server { shift->run('kill-server') } + +sub connect { shift->run('connect', @_) } +sub disconnect { shift->run('disconnect', @_) } + +sub devices { + my @devices = split '\n', shift->run('devices', '-l'); + my @result; + for (@devices) { + next if /^List of devices/; + next unless / /; + push @result, Android::ADB::Device->new(split) + } + @result +} + +sub set_device { + my ($self, $device) = @_; + $self->{device_serial} = $device->serial; +} + +sub wait_for_device { shift->run('wait-for-device') } +sub get_state { shift->run('get-state') } +sub get_serialno { shift->run('get-serialno') } +sub get_devpath { shift->run('get-devpath') } +sub remount { shift->run('remount') } +sub reboot { shift->run('reboot', @_) } +sub reboot_bootloader { shift->run('reboot-bootloader') } +sub root { shift->run('root') } +sub usb { shift->run('usb') } +sub tcpip { shift->run('tcpip', @_) } + +sub push { + my ($self, $local, $remote) = @_; + $self->run(push => $local, $remote) +} + +sub pull { + my ($self, $remote, $local) = @_; + $self->run(push => $remote, $local) +} + +sub pull_archive { + my ($self, $remote, $local) = @_; + $self->run(push => '-a', $remote, $local) +} + +sub shell { shift->run(shell => @_) } + +1; +__END__ + +=encoding utf-8 + +=head1 NAME + +Android::ADB - thin wrapper over the 'adb' command + +=head1 SYNOPSIS + + use Android::ADB;; + my $adb = Android::ADB->new(path => '/opt/android/platform-tools/adb'); + my @devices = $adb->devices; + $adb->set_device($devices[0]); + $adb->push('file.txt', '/sdcard/'); + sleep 10; + $adb->reboot('recovery'); + +=head1 DESCRIPTION + +This module is a minimal wrapper over the Android Debug Bridge +(C) command for manipulating Android devices. + +Methods die on non-zero exit code and return the text printed by the +C command. The available methods are: + +=over + +=item Android::ADB->B([I]) + +Create a new Android::ADB object. The available arguments are C, +the path to the C executable (defaults to the value of the +environment variable C or the string C) and C, an +arrayref of arguments passed to every adb command (defaults to []). + +=item $adb->B + +Returns a list of L objects representing +connected devices. + +=item $adb->B(I<$device>) + +Takes an L and directs all further commands to +that device by passing C<-s serialno> to every command. + +=item $adb->B(I<$command>, [I<@args>]) + +Run an arbitrary ADB command and return its output. + +=item $adb->B + +=item $adb->B + +=item $adb->B(I<$host_and_port>) + +=item $adb->B([I<$host_and_port>]) + +=item $adb->B + +=item $adb->B + +=item $adb->B + +=item $adb->B + +=item $adb->B + +=item $adb->B([I<$where>]) + +=item $adb->B + +=item $adb->B + +=item $adb->B + +=item $adb->B(I<$port>) + +=item $adb->B(I<$local>, I<$remote>) + +=item $adb->B(I<$remote>, I<$local>) + +=item $adb->B(I<@args>) + +Analogues of the respective adb commands. + +=item $adb->B(I<$remote>, I<$local>) + +Same as C. + +=back + +=head1 AUTHOR + +Marius Gavrilescu, Emarius@ieval.roE + +=head1 COPYRIGHT AND LICENSE + +Copyright (C) 2017 by Marius Gavrilescu + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself, either Perl version 5.24.2 or, +at your option, any later version of Perl 5 you may have available. + + +=cut diff --git a/lib/Android/ADB/Device.pm b/lib/Android/ADB/Device.pm new file mode 100644 index 0000000..6dde6b3 --- /dev/null +++ b/lib/Android/ADB/Device.pm @@ -0,0 +1,63 @@ +package Android::ADB::Device; + +use 5.014000; +use strict; +use warnings; + +use parent qw/Class::Accessor::Fast/; + +our $VERSION = '0.001'; + +sub new { + my ($class, $serial, $state, @attrs) = @_; + my %attrs = map { split ':', $_, 2 } @attrs; + bless { serial => $serial, state => $state, %attrs }, $class +} + +__PACKAGE__->mk_ro_accessors(qw/serial state usb product model device/); + +1; +__END__ + +=encoding utf-8 + +=head1 NAME + +Android::ADB::Device - information about an Android device + +=head1 SYNOPSIS + + use Android::ADB; + my @devices = $adb->devices; + say $devices[0]->serial; + say $devices[0]->state; # e.g. offline, bootloader, sideload, or device + + # The available attributes depend on your device + say $devices[0]->usb; # e.g. 2-1 + say $devices[0]->product; # e.g. angler + say $devices[0]->model; # e.g. MI_MAX + say $devices[0]->device; # e.g. angler + +=head1 DESCRIPTION + +Information about an Android device in form of a blessed hash with a +few accessors. See SYNPOSIS for a list of accessors. + +=head1 SEE ALSO + +L + +=head1 AUTHOR + +Marius Gavrilescu, Emarius@ieval.roE + +=head1 COPYRIGHT AND LICENSE + +Copyright (C) 2017 by Marius Gavrilescu + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself, either Perl version 5.24.2 or, +at your option, any later version of Perl 5 you may have available. + + +=cut diff --git a/t/Android-ADB.t b/t/Android-ADB.t new file mode 100644 index 0000000..f3b8346 --- /dev/null +++ b/t/Android-ADB.t @@ -0,0 +1,6 @@ +#!/usr/bin/perl +use strict; +use warnings; + +use Test::More tests => 1; +BEGIN { use_ok('Android::ADB') }; -- 2.39.2