Skip to content

Add ADB (Android Debug Bridge) Support#1564

Open
ep1cman wants to merge 2 commits intolabgrid-project:masterfrom
ep1cman:adb
Open

Add ADB (Android Debug Bridge) Support#1564
ep1cman wants to merge 2 commits intolabgrid-project:masterfrom
ep1cman:adb

Conversation

@ep1cman
Copy link
Copy Markdown
Contributor

@ep1cman ep1cman commented Jan 13, 2025

This pull request add support for ADB (Android Debug Bridge) devices in labgrid.

  • It supports USB (remote and local) and TCP adb devices.
  • It adds two new client commands
    • adb for interfacing with the device by the adb cli
    • scrcpy for remote screen/touch/keyboard/clipboard/etc. sharing

Checklist

  • Documentation for the feature
  • Tests for the feature
  • The arguments and description in doc/configuration.rst have been updated
  • Add a section on how to use the feature to doc/usage.rst
  • Add a section on how to use the feature to doc/development.rst
  • PR has been tested
  • Man pages have been regenerated

@ep1cman ep1cman force-pushed the adb branch 2 times, most recently from d234f34 to 15a4ce4 Compare January 13, 2025 12:18
@codecov
Copy link
Copy Markdown

codecov bot commented Jan 13, 2025

Codecov Report

❌ Patch coverage is 31.28205% with 134 lines in your changes missing coverage. Please review.
✅ Project coverage is 46.5%. Comparing base (bfb6ff2) to head (79aca51).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
labgrid/remote/client.py 6.6% 56 Missing ⚠️
labgrid/remote/exporter.py 0.0% 45 Missing ⚠️
labgrid/driver/adb.py 53.5% 33 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##           master   #1564     +/-   ##
========================================
- Coverage    46.7%   46.5%   -0.3%     
========================================
  Files         179     181      +2     
  Lines       14202   14394    +192     
========================================
+ Hits         6645    6704     +59     
- Misses       7557    7690    +133     
Flag Coverage Δ
3.10 46.5% <31.2%> (-0.3%) ⬇️
3.11 46.5% <31.2%> (-0.3%) ⬇️
3.12 46.5% <31.2%> (-0.3%) ⬇️
3.13 46.5% <31.2%> (-0.3%) ⬇️
3.14 46.5% <31.2%> (-0.3%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ep1cman ep1cman force-pushed the adb branch 4 times, most recently from e346334 to 892693a Compare January 14, 2025 12:26
Copy link
Copy Markdown
Member

@Emantor Emantor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor comments in regard to subcommand handling and attribute naming.

Comment thread labgrid/driver/adb.py
Comment thread labgrid/driver/adb.py Outdated
Comment thread labgrid/driver/adb.py Outdated
Comment thread labgrid/remote/client.py Outdated
if resource.extra.get("proxy_required") or self.args.proxy:
proxy = resource.extra.get("proxy")
scrcpy_cmd.append(f"--tunnel-host={ip_addr}")
scrcpy_cmd.append(f"--tunnel-port={sshmanager.request_forward(proxy, host, 27183)}")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this always the same port? How will this interact with multiple devices connected over ADB to the same host?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I had not tested multiple simultaneous screen sharing sessions. I have added some code to find a free port on the exporter and use that. It is based on the "get_free_port" code within labgrid that gets a free port on the client machine.

Is this worth moving into a more general location? Maybe within SSHConnection?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_free_port should already be in util.helper, so you should be able to import it directly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That returns a free port on the machine running the labgrid client, I require a free port on the exporter. The way scrcpy works is that it runs a server on the android device and creates a port forward between it and the ADB host (the exporter in this case)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might be able to handle that within the remote resource for the exporter, the SerialPortExport does something similar to choose a free port for ser2net.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an additional complication to the scrcpy command.

I already use get_free_port in the ADBExport to get a port for the ADB server, which is started by the ADBExport.

The scrcpy server is not setup at this time, it is started by the scrcpy client when it connects to the device. It first connects via ADB, transfers over the binaries, starts the server, then uses ADB to create a port forward between the device and the ADB host (the exporter).

I am unable to know at the time of starting the exporter what ports will be available at the time scrcpy is launched, which could be weeks later.

Comment thread labgrid/resource/adb.py Outdated
@alextercete
Copy link
Copy Markdown
Contributor

Any updates on this? It would be great to get this merged.

@ep1cman ep1cman force-pushed the adb branch 4 times, most recently from 33c3683 to 65102d5 Compare September 16, 2025 14:57
@ep1cman
Copy link
Copy Markdown
Contributor Author

ep1cman commented Sep 16, 2025

@Emantor I believe there was one remaining comment thread, I provided an explanation for why I did things that way, awaiting your feedback. I have update the PR to resolve merge conflicts with the latest master branch.

Comment thread labgrid/resource/adb.py Outdated
Comment thread labgrid/resource/adb.py Outdated
Comment thread labgrid/remote/client.py Outdated
Comment thread labgrid/driver/adb.py
# 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(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use processwrapper from util.helper, see for example the FastbootDriver.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two questions about this:

  • processwrapper doesn't support a timeout, how should we handle this?
  • I don't think I can also do this in _run as I need to return a separate stdout, stderr and exit code.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should extend the processwrapper for timeout support, but IMO this is out of scope for this PR. Can you add a TODO entry to the first occasion in the driver? Than the subprocess usage should be fine.

I agree for _run since this needs explicit handling.

Comment thread labgrid/driver/adb.py Outdated
Some forms of reset can reset the device into different modes. For example:
- Reboot
- Reboot into bootloader mode
- Reboot into recovery mode

Signed-off-by: Sebastian Goscik <sebastian.goscik@arm.com>
@ep1cman ep1cman force-pushed the adb branch 2 times, most recently from 6cc31a5 to 76403ef Compare March 20, 2026 18:25
@Emantor
Copy link
Copy Markdown
Member

Emantor commented Mar 23, 2026

It looks like we are missing DCOs for the additional commits, you can either squash them down into the original commit and give credit there or ask the committers to add the DCO 👍

ADB is expected to be installed and working on the exporter and client machines.
For screensharing "scrcpy" needs to be installed on the client.

Signed-off-by: Sebastian Goscik <sebastian.goscik@arm.com>
Co-authored-by: David Brown <david.brown@arm.com>
Co-authored-by: freedrikp <freedrikp@users.noreply.github.com>
Co-authored-by: Luke Hackwell <luke.hackwell@arm.com>
@ep1cman
Copy link
Copy Markdown
Contributor Author

ep1cman commented Mar 23, 2026

It looks like we are missing DCOs for the additional commits, you can either squash them down into the original commit and give credit there or ask the committers to add the DCO 👍

I have squash the commits down and added the others as co-authors. Only outstanding issue now is the use of processwrapper, I left some questions above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants