-
Notifications
You must be signed in to change notification settings - Fork 201
Add ADB (Android Debug Bridge) Support #1564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ep1cman
wants to merge
2
commits into
labgrid-project:master
Choose a base branch
from
ep1cman:adb
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import shlex | ||
import subprocess | ||
|
||
import attr | ||
|
||
from ..factory import target_factory | ||
from ..protocol import CommandProtocol, FileTransferProtocol, ResetProtocol | ||
from ..resource.adb import ADBDevice, NetworkADBDevice, RemoteADBDevice | ||
from ..step import step | ||
from ..util.proxy import proxymanager | ||
from .commandmixin import CommandMixin | ||
from .common import Driver | ||
|
||
# Default timeout for adb commands, in seconds | ||
ADB_TIMEOUT = 10 | ||
|
||
|
||
@target_factory.reg_driver | ||
@attr.s(eq=False) | ||
class ADBDriver(CommandMixin, Driver, CommandProtocol, FileTransferProtocol, ResetProtocol): | ||
"""ADB driver to execute commands, transfer files and reset devices via ADB.""" | ||
|
||
bindings = {"device": {"ADBDevice", "NetworkADBDevice", "RemoteADBDevice"}} | ||
|
||
def __attrs_post_init__(self): | ||
super().__attrs_post_init__() | ||
if self.target.env: | ||
self.tool = self.target.env.config.get_tool("adb") | ||
else: | ||
self.tool = "adb" | ||
|
||
if isinstance(self.device, ADBDevice): | ||
self._base_command = [self.tool, "-s", self.device.serialno] | ||
|
||
elif isinstance(self.device, NetworkADBDevice): | ||
self._host, self._port = proxymanager.get_host_and_port(self.device) | ||
self._base_command = [self.tool, "-H", self._host, "-P", str(self._port), "-s", self.device.serialno] | ||
|
||
elif isinstance(self.device, RemoteADBDevice): | ||
self._host, self._port = proxymanager.get_host_and_port(self.device) | ||
# ADB does not automatically remove a network device from its | ||
# devices list when the connection is broken by the remote, so the | ||
# adb connection may have gone "stale", resulting in adb blocking | ||
# indefinitely when making calls to the device. To avoid this, | ||
# always disconnect first. | ||
subprocess.run( | ||
["adb", "disconnect", f"{self._host}:{str(self._port)}"], | ||
stderr=subprocess.DEVNULL, | ||
timeout=ADB_TIMEOUT, | ||
check=True, | ||
) | ||
subprocess.run( | ||
["adb", "connect", f"{self._host}:{str(self._port)}"], | ||
stdout=subprocess.DEVNULL, | ||
timeout=ADB_TIMEOUT, | ||
check=True, | ||
) # Connect adb client to TCP adb device | ||
self._base_command = [self.tool, "-s", f"{self._host}:{str(self._port)}"] | ||
|
||
def on_deactivate(self): | ||
if isinstance(self.device, RemoteADBDevice): | ||
# Clean up TCP adb device once the driver is deactivated | ||
subprocess.run( | ||
["adb", "disconnect", f"{self._host}:{str(self._port)}"], | ||
stderr=subprocess.DEVNULL, | ||
timeout=ADB_TIMEOUT, | ||
check=True, | ||
) | ||
|
||
# Command Protocol | ||
|
||
def _run(self, cmd, *, timeout=30.0, codec="utf-8", decodeerrors="strict"): | ||
cmd = [*self._base_command, "shell", *shlex.split(cmd)] | ||
result = subprocess.run( | ||
cmd, | ||
text=True, # Automatically decode using default UTF-8 | ||
capture_output=True, | ||
timeout=timeout, | ||
) | ||
return ( | ||
result.stdout.splitlines(), | ||
result.stderr.splitlines(), | ||
result.returncode, | ||
) | ||
|
||
@Driver.check_active | ||
@step(args=["cmd"], result=True) | ||
def run(self, cmd, timeout=30.0, codec="utf-8", decodeerrors="strict"): | ||
return self._run(cmd, timeout=timeout, codec=codec, decodeerrors=decodeerrors) | ||
|
||
@step() | ||
def get_status(self): | ||
return 1 | ||
|
||
# File Transfer Protocol | ||
|
||
@Driver.check_active | ||
@step(args=["filename", "remotepath", "timeout"]) | ||
def put(self, filename: str, remotepath: str, timeout: float = ADB_TIMEOUT): | ||
subprocess.run([*self._base_command, "push", filename, remotepath], timeout=timeout, check=True) | ||
|
||
@Driver.check_active | ||
@step(args=["filename", "destination", "timeout"]) | ||
def get(self, filename: str, destination: str, timeout: float = ADB_TIMEOUT): | ||
subprocess.run([*self._base_command, "pull", filename, destination], timeout=timeout, check=True) | ||
|
||
# Reset Protocol | ||
|
||
@Driver.check_active | ||
@step(args=["mode"]) | ||
def reset(self, mode=None): | ||
valid_modes = ["bootloader", "recovery", "sideload", "sideload-auto-reboot"] | ||
cmd = [*self._base_command, "reboot"] | ||
|
||
if mode: | ||
if mode not in valid_modes: | ||
raise ValueError(f"{mode} must be one of: {', '.join(valid_modes)}") | ||
cmd.append(mode) | ||
|
||
subprocess.run(cmd, timeout=ADB_TIMEOUT, check=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.