|
| 1 | +"""Device handles for the lab driver (run.py). |
| 2 | +
|
| 3 | +AndroidDevice drives a phone over adb by serial: port forward, the two |
| 4 | +debug.phyphox.* switches, asset launches, media volume for the audio |
| 5 | +loopback. IOSDevice drives an iPhone/iPad from a macOS host via devicectl |
| 6 | +(launch with the -phyphox* arguments) and a user-provided port forward |
| 7 | +(iproxy or pymobiledevice3 usbmux forward - started by run.py when the |
| 8 | +lab.yml entry names a local port). The iOS paths were written on the |
| 9 | +Linux machine and are UNVERIFIED until the first MacBook run - anything |
| 10 | +that needed fixing there is a finding for the docs session. |
| 11 | +""" |
| 12 | + |
| 13 | +import subprocess |
| 14 | +import time |
| 15 | +import urllib.parse |
| 16 | +import urllib.request |
| 17 | + |
| 18 | +ANDROID_BUNDLE = "de.rwth_aachen.phyphox" |
| 19 | +IOS_BUNDLE = "de.rwth-aachen.physics.phyphox" |
| 20 | + |
| 21 | + |
| 22 | +def sh(cmd, timeout=30): |
| 23 | + """One visible retry on timeout, then a failed stand-in (the |
| 24 | + t1_experiments convention - a wedged tool fails one step, not the |
| 25 | + run).""" |
| 26 | + for attempt in (1, 2): |
| 27 | + try: |
| 28 | + r = subprocess.run(cmd, capture_output=True, text=True, |
| 29 | + timeout=timeout) |
| 30 | + if attempt == 2: |
| 31 | + print(f" ~ retried after a timeout: {' '.join(cmd[:4])} ...") |
| 32 | + return r |
| 33 | + except subprocess.TimeoutExpired: |
| 34 | + if attempt == 2: |
| 35 | + print(f" ~ command timed out twice: {' '.join(cmd[:4])} ...") |
| 36 | + |
| 37 | + class T: |
| 38 | + returncode, stdout, stderr = -1, "", "timed out" |
| 39 | + return T() |
| 40 | + |
| 41 | + |
| 42 | +def api(base, path, timeout=5): |
| 43 | + try: |
| 44 | + with urllib.request.urlopen(base + path, timeout=timeout) as r: |
| 45 | + return r.status, r.read() |
| 46 | + except urllib.error.HTTPError as e: |
| 47 | + return e.code, e.read() |
| 48 | + except Exception as e: |
| 49 | + return None, str(e).encode() |
| 50 | + |
| 51 | + |
| 52 | +def wait_api(base, seconds, probe_timeout=2): |
| 53 | + t0 = time.time() |
| 54 | + while time.time() - t0 < seconds: |
| 55 | + status, _ = api(base, "/config", timeout=probe_timeout) |
| 56 | + if status == 200: |
| 57 | + return time.time() - t0 |
| 58 | + time.sleep(0.5) |
| 59 | + return None |
| 60 | + |
| 61 | + |
| 62 | +class AndroidDevice: |
| 63 | + platform = "android" |
| 64 | + |
| 65 | + def __init__(self, serial, port): |
| 66 | + self.serial, self.port = serial, port |
| 67 | + self.base = f"http://127.0.0.1:{port}" |
| 68 | + self.adb = ["adb", "-s", serial] |
| 69 | + |
| 70 | + def prepare(self, fixture_port=None): |
| 71 | + sh(self.adb + ["forward", f"tcp:{self.port}", "tcp:8080"]) |
| 72 | + if fixture_port: |
| 73 | + # the phone reaches the host's fixture server at 127.0.0.1 |
| 74 | + sh(self.adb + ["reverse", f"tcp:{fixture_port}", |
| 75 | + f"tcp:{fixture_port}"]) |
| 76 | + for prop in ("remote", "autoConfirm"): |
| 77 | + sh(self.adb + ["shell", "setprop", f"debug.phyphox.{prop}", "1"]) |
| 78 | + for perm in ("RECORD_AUDIO", "CAMERA", "ACCESS_FINE_LOCATION", |
| 79 | + "ACCESS_COARSE_LOCATION"): |
| 80 | + sh(self.adb + ["shell", "pm", "grant", ANDROID_BUNDLE, |
| 81 | + f"android.permission.{perm}"]) |
| 82 | + |
| 83 | + def cleanup(self): |
| 84 | + for prop in ("remote", "autoConfirm"): |
| 85 | + sh(self.adb + ["shell", "setprop", f"debug.phyphox.{prop}", "''"]) |
| 86 | + |
| 87 | + def launch(self, asset_path): |
| 88 | + self.stop_app() |
| 89 | + url = "phyphox://asset=" + urllib.parse.quote(asset_path, safe="") |
| 90 | + r = sh(self.adb + ["shell", "am", "start", "-W", "-a", |
| 91 | + "android.intent.action.VIEW", "-d", url]) |
| 92 | + return r.returncode == 0 and "Error" not in r.stdout |
| 93 | + |
| 94 | + def stop_app(self): |
| 95 | + sh(self.adb + ["shell", "am", "force-stop", ANDROID_BUNDLE]) |
| 96 | + |
| 97 | + def fixture_host(self): |
| 98 | + return "127.0.0.1" # via adb reverse |
| 99 | + |
| 100 | + def open_url(self, phyphox_url): |
| 101 | + """Open a phyphox:// URL (e.g. a fixture served by run.py).""" |
| 102 | + self.stop_app() |
| 103 | + r = sh(self.adb + ["shell", "am", "start", "-W", "-a", |
| 104 | + "android.intent.action.VIEW", "-d", phyphox_url]) |
| 105 | + return r.returncode == 0 and "Error" not in r.stdout |
| 106 | + |
| 107 | + def set_media_volume_max(self): |
| 108 | + sh(self.adb + ["shell", "cmd", "media_session", "volume", |
| 109 | + "--stream", "3", "--set", "15"]) |
| 110 | + sh(self.adb + ["shell", "media", "volume", "--stream", "3", |
| 111 | + "--set", "15"]) |
| 112 | + |
| 113 | + |
| 114 | +class IOSDevice: |
| 115 | + platform = "ios" |
| 116 | + |
| 117 | + def __init__(self, udid, port): |
| 118 | + self.udid, self.port = udid, port |
| 119 | + self.base = f"http://127.0.0.1:{port}" |
| 120 | + self._forward = None |
| 121 | + |
| 122 | + def prepare(self, fixture_port=None): |
| 123 | + self.fixture_port = fixture_port |
| 124 | + # forward local port -> device port 80 (the app's server on |
| 125 | + # hardware). pymobiledevice3 is the portable choice; iproxy works |
| 126 | + # too. UNVERIFIED on hardware. |
| 127 | + self._forward = subprocess.Popen( |
| 128 | + ["pymobiledevice3", "usbmux", "forward", str(self.port), "80", |
| 129 | + "--serial", self.udid], |
| 130 | + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
| 131 | + time.sleep(1) |
| 132 | + |
| 133 | + def cleanup(self): |
| 134 | + if self._forward: |
| 135 | + self._forward.terminate() |
| 136 | + |
| 137 | + def launch(self, asset_path): |
| 138 | + url = "phyphox://asset=" + urllib.parse.quote(asset_path, safe="") |
| 139 | + r = sh(["xcrun", "devicectl", "device", "process", "launch", |
| 140 | + "--terminate-existing", "--device", self.udid, IOS_BUNDLE, |
| 141 | + "-phyphoxUrl", url, "-phyphoxRemote", |
| 142 | + "-phyphoxRemotePort", "80", "-phyphoxAutoConfirm"], |
| 143 | + timeout=60) |
| 144 | + return r.returncode == 0 |
| 145 | + |
| 146 | + def stop_app(self): |
| 147 | + pass # --terminate-existing on launch |
| 148 | + |
| 149 | + def fixture_host(self): |
| 150 | + # an iOS device reaches the host over the LAN; run.py passes the |
| 151 | + # host address from lab.yml (host_ip) |
| 152 | + return getattr(self, "host_ip", None) or "HOST-IP-UNSET" |
| 153 | + |
| 154 | + def open_url(self, phyphox_url): |
| 155 | + r = sh(["xcrun", "devicectl", "device", "process", "launch", |
| 156 | + "--terminate-existing", "--device", self.udid, IOS_BUNDLE, |
| 157 | + "-phyphoxUrl", phyphox_url, "-phyphoxRemote", |
| 158 | + "-phyphoxRemotePort", "80", "-phyphoxAutoConfirm"], |
| 159 | + timeout=60) |
| 160 | + return r.returncode == 0 |
| 161 | + |
| 162 | + def set_media_volume_max(self): |
| 163 | + pass # no CLI path; the unlock-once checklist sets the volume |
0 commit comments