Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / dhu
dhu75 lines · 3.1 KiBRaw
| 1 | #!/usr/bin/env bash |
| 2 | # Runs the Desktop Head Unit loopback against the Pixel 8: adb forward, |
| 3 | # fresh logcat capture, and the DHU in its own window. |
| 4 | # |
| 5 | # Usage: |
| 6 | # ./dhu # full loop, log to analysis/dhu-<timestamp>.log |
| 7 | # ./dhu --no-log # skip the logcat capture (DHU only) |
| 8 | # ./dhu --device-id 44050DLJH001PC # target a different device |
| 9 | # # (default: the Pixel 8, else the first non-emulator device) |
| 10 | # |
| 11 | # Notes: |
| 12 | # DHU is interactive: piping its stdout closes stdin and it exits, so it |
| 13 | # always launches detached. `adb logcat -d` has proven unreliable (stale |
| 14 | # buffer); this captures live to a file instead. |
| 15 | set -euo pipefail |
| 16 | |
| 17 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 18 | DEVICE_ID="" |
| 19 | NO_LOG=0 |
| 20 | |
| 21 | while [[ $# -gt 0 ]]; do |
| 22 | case "$1" in |
| 23 | -h|--help|help) |
| 24 | sed -n '2,/^set -euo/p' "$SCRIPT_DIR/dhu" | sed 's/^# \{0,1\}//' | sed '$d'; exit 0;; |
| 25 | --no-log|--nolog|-NoLog) NO_LOG=1; shift;; |
| 26 | --device-id|-DeviceId) DEVICE_ID="${2:?--device-id needs a value}"; shift 2;; |
| 27 | --device-id=*) DEVICE_ID="${1#--device-id=}"; shift;; |
| 28 | *) echo "Error: unknown arg '$1'. Usage: ./dhu [--no-log] [--device-id SERIAL]" >&2; exit 1;; |
| 29 | esac |
| 30 | done |
| 31 | |
| 32 | # SDK location: prefer env, then the usual per-OS defaults. |
| 33 | DHU_EXE="" |
| 34 | for base in "${ANDROID_HOME:-}" "${ANDROID_SDK_ROOT:-}" "$HOME/Android/Sdk" "$HOME/Library/Android/sdk" "/c/Android/Sdk" "C:/Android/Sdk"; do |
| 35 | [[ -n "$base" && -x "$base/extras/google/auto/desktop-head-unit" ]] && { DHU_EXE="$base/extras/google/auto/desktop-head-unit"; break; } |
| 36 | [[ -n "$base" && -x "$base/extras/google/auto/desktop-head-unit.exe" ]] && { DHU_EXE="$base/extras/google/auto/desktop-head-unit.exe"; break; } |
| 37 | done |
| 38 | if [[ -z "$DHU_EXE" ]]; then |
| 39 | echo "Error: DHU not found (looked under ANDROID_HOME, ~/Android/Sdk, ~/Library/Android/sdk, /c/Android/Sdk)." >&2 |
| 40 | exit 1 |
| 41 | fi |
| 42 | |
| 43 | # The phone listens; the DHU reaches it through the forward. |
| 44 | if [[ -z "$DEVICE_ID" ]]; then |
| 45 | attached="$(adb devices | awk 'NR>1 && $2=="device" && $1 !~ /^emulator/ {print $1}')" |
| 46 | if grep -qx "44050DLJH001PC" <<<"$attached"; then |
| 47 | DEVICE_ID="44050DLJH001PC" |
| 48 | else |
| 49 | DEVICE_ID="$(head -n1 <<<"$attached")" |
| 50 | fi |
| 51 | [[ -n "$DEVICE_ID" ]] || { echo "Error: no physical device attached." >&2; exit 1; } |
| 52 | fi |
| 53 | adb -s "$DEVICE_ID" forward tcp:5277 tcp:5277 |
| 54 | echo "forward tcp:5277 -> $DEVICE_ID" |
| 55 | |
| 56 | if [[ $NO_LOG -eq 0 ]]; then |
| 57 | adb -s "$DEVICE_ID" logcat -c |
| 58 | stamp="$(date +%Y%m%d-%H%M%S)" |
| 59 | logfile="$SCRIPT_DIR/analysis/dhu-$stamp.log" |
| 60 | mkdir -p "$SCRIPT_DIR/analysis" |
| 61 | nohup adb -s "$DEVICE_ID" logcat -v time \ |
| 62 | MaAuto.Service:V MaAuto.Server:V MaAuto.Video:V MaAuto.Sensors:V \ |
| 63 | MaAuto.Input:V MaAuto.HostManager:V MaAuto.HostSession:V '*:S' \ |
| 64 | >"$logfile" 2>&1 & |
| 65 | echo "logcat -> $logfile (pid $!)" |
| 66 | fi |
| 67 | |
| 68 | if [[ "${OS:-}" == "Windows_NT" ]]; then |
| 69 | # git-bash: `start` detaches the GUI like Start-Process did. |
| 70 | start "" "$DHU_EXE" |
| 71 | else |
| 72 | # DHU exits on stdin EOF, and nohup points stdin at an unreadable file; keep a pipe open. |
| 73 | (cd "$(dirname "$DHU_EXE")" && nohup sh -c 'tail -f /dev/null | "$0"' "$DHU_EXE" >/dev/null 2>&1 &) |
| 74 | fi |
| 75 | echo "DHU launched (detached)" |