|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""ADB automation for OOB teleop (``--setup-oob``): open the headset bookmark URL via USB adb. |
| 5 | +
|
| 6 | +The headset is connected via USB cable for adb commands only. Streaming and |
| 7 | +web-page access use WiFi — no ``adb reverse`` or USB tethering. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import logging |
| 13 | +import os |
| 14 | +import shlex |
| 15 | +import shutil |
| 16 | +import subprocess |
| 17 | + |
| 18 | +from .oob_teleop_env import ( |
| 19 | + DEFAULT_WEB_CLIENT_ORIGIN, |
| 20 | + build_headset_bookmark_url, |
| 21 | + client_ui_fields_from_env, |
| 22 | + resolve_lan_host_for_oob, |
| 23 | + web_client_base_override_from_env, |
| 24 | +) |
| 25 | + |
| 26 | +log = logging.getLogger("oob-teleop-adb") |
| 27 | + |
| 28 | + |
| 29 | +class OobAdbError(Exception): |
| 30 | + """``--setup-oob`` adb step failed; ``str(exception)`` is formatted for users (print without traceback).""" |
| 31 | + |
| 32 | + |
| 33 | +def _adb_output_text(proc: subprocess.CompletedProcess[str]) -> str: |
| 34 | + return (proc.stderr or proc.stdout or "").strip() |
| 35 | + |
| 36 | + |
| 37 | +def adb_automation_failure_hint(diagnostic: str) -> str: |
| 38 | + """Human-readable next steps for common ``adb`` failures.""" |
| 39 | + d = diagnostic.lower() |
| 40 | + if "unauthorized" in d: |
| 41 | + return ( |
| 42 | + "Device is unauthorized: unlock the headset, confirm the USB debugging (RSA) prompt, " |
| 43 | + "and run `adb devices` until the device shows `device` not `unauthorized`. " |
| 44 | + "If this persists, try `adb kill-server` and reconnect the cable." |
| 45 | + ) |
| 46 | + if ( |
| 47 | + "no devices/emulators" in d |
| 48 | + or "no devices found" in d |
| 49 | + or "device not found" in d |
| 50 | + ): |
| 51 | + return ( |
| 52 | + "No adb device: plug in the USB cable, enable USB debugging on the headset, " |
| 53 | + "and check `adb devices`." |
| 54 | + ) |
| 55 | + if "more than one device" in d: |
| 56 | + return "Multiple adb devices: unplug extras so only one headset shows in `adb devices`." |
| 57 | + if "offline" in d: |
| 58 | + return "Device offline: reconnect the USB cable and confirm USB debugging on the headset." |
| 59 | + return "" |
| 60 | + |
| 61 | + |
| 62 | +def oob_adb_automation_message(rc: int, detail: str, hint: str) -> str: |
| 63 | + d = detail.strip() if detail else "(no output from adb)" |
| 64 | + lines = [ |
| 65 | + f"OOB adb automation failed (adb exit code {rc}).", |
| 66 | + "", |
| 67 | + d, |
| 68 | + ] |
| 69 | + if hint.strip(): |
| 70 | + lines.extend(["", hint]) |
| 71 | + lines.extend( |
| 72 | + [ |
| 73 | + "", |
| 74 | + "To run the WSS proxy and OOB hub without adb, omit --setup-oob and open the teleop URL on the headset yourself.", |
| 75 | + ] |
| 76 | + ) |
| 77 | + return "\n".join(lines) |
| 78 | + |
| 79 | + |
| 80 | +def require_adb_on_path() -> None: |
| 81 | + """Raise :exc:`OobAdbError` if ``adb`` is missing.""" |
| 82 | + if shutil.which("adb"): |
| 83 | + return |
| 84 | + raise OobAdbError( |
| 85 | + "Cannot use --setup-oob: `adb` was not found on PATH.\n\n" |
| 86 | + "Install Android Platform Tools and ensure `adb` is available, or omit --setup-oob and open " |
| 87 | + "the teleop bookmark URL on the headset yourself." |
| 88 | + ) |
| 89 | + |
| 90 | + |
| 91 | +def assert_exactly_one_adb_device() -> None: |
| 92 | + """Fail unless exactly one device is in ``device`` state.""" |
| 93 | + try: |
| 94 | + proc = subprocess.run( |
| 95 | + ["adb", "devices"], |
| 96 | + capture_output=True, |
| 97 | + text=True, |
| 98 | + timeout=30, |
| 99 | + check=False, |
| 100 | + ) |
| 101 | + except FileNotFoundError as e: |
| 102 | + raise OobAdbError( |
| 103 | + "Cannot use --setup-oob: `adb` was not found on PATH.\n\n" |
| 104 | + "Install Android Platform Tools and ensure `adb` is available, or omit --setup-oob." |
| 105 | + ) from e |
| 106 | + if proc.returncode != 0: |
| 107 | + diag = _adb_output_text(proc) |
| 108 | + raise OobAdbError( |
| 109 | + f"adb devices failed (exit code {proc.returncode}).\n\n" |
| 110 | + f"{diag}\n\n" |
| 111 | + "Check your adb installation and USB connection." |
| 112 | + ) |
| 113 | + text = (proc.stdout or "") + "\n" + (proc.stderr or "") |
| 114 | + ready: list[str] = [] |
| 115 | + for line in text.strip().splitlines()[1:]: |
| 116 | + line = line.strip() |
| 117 | + if not line: |
| 118 | + continue |
| 119 | + parts = line.split() |
| 120 | + if len(parts) >= 2 and parts[-1] == "device": |
| 121 | + ready.append(parts[0]) |
| 122 | + if len(ready) == 0: |
| 123 | + raise OobAdbError( |
| 124 | + "No adb device found for --setup-oob.\n\n" |
| 125 | + "Plug in the USB cable, enable USB debugging on the headset, and check `adb devices`. " |
| 126 | + "Or omit --setup-oob and open the teleop URL on the headset yourself." |
| 127 | + ) |
| 128 | + if len(ready) > 1: |
| 129 | + listed = ", ".join(ready) |
| 130 | + raise OobAdbError( |
| 131 | + "Too many adb devices for --setup-oob.\n\n" |
| 132 | + f"Currently connected: {listed}\n\n" |
| 133 | + "Unplug extras so only one headset is connected, then retry. " |
| 134 | + "Or omit --setup-oob and open the teleop URL manually." |
| 135 | + ) |
| 136 | + |
| 137 | + |
| 138 | +def run_adb_headset_bookmark(*, resolved_port: int) -> tuple[int, str]: |
| 139 | + """Open the teleop bookmark URL on the headset via ``am start``. |
| 140 | +
|
| 141 | + Uses the PC's LAN address — the headset reaches the proxy over WiFi. |
| 142 | + ``resolved_port`` is used as the stream port unless ``TELEOP_STREAM_PORT`` |
| 143 | + is set explicitly. Returns ``(exit_code, diagnostic)``. |
| 144 | + """ |
| 145 | + env_port = os.environ.get("TELEOP_STREAM_PORT", "").strip() |
| 146 | + signaling_port = int(env_port) if env_port else resolved_port |
| 147 | + proxy_host = resolve_lan_host_for_oob() |
| 148 | + stream_cfg: dict = { |
| 149 | + "serverIP": proxy_host, |
| 150 | + "port": signaling_port, |
| 151 | + **client_ui_fields_from_env(), |
| 152 | + } |
| 153 | + |
| 154 | + ovr = web_client_base_override_from_env() |
| 155 | + web_base = ovr if ovr else DEFAULT_WEB_CLIENT_ORIGIN |
| 156 | + token = os.environ.get("CONTROL_TOKEN") or None |
| 157 | + url = build_headset_bookmark_url( |
| 158 | + web_client_base=web_base, |
| 159 | + stream_config=stream_cfg, |
| 160 | + control_token=token, |
| 161 | + ) |
| 162 | + |
| 163 | + shell_cmd = "am start -a android.intent.action.VIEW -d " + shlex.quote(url) |
| 164 | + full = ["adb", "shell", shell_cmd] |
| 165 | + log.info("ADB automation: %s", " ".join(shlex.quote(c) for c in full)) |
| 166 | + proc = subprocess.run(full, capture_output=True, text=True) |
| 167 | + if proc.returncode != 0: |
| 168 | + diag = _adb_output_text(proc) |
| 169 | + return proc.returncode, diag |
| 170 | + log.info("ADB automation: am start completed") |
| 171 | + return 0, "" |
0 commit comments