Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / dhu.bat
dhu.bat138 lines · 5.1 KiBRaw
| 1 | <# : |
| 2 | @echo off |
| 3 | setlocal |
| 4 | where pwsh >nul 2>nul |
| 5 | if %errorlevel%==0 ( set "PSBIN=pwsh" ) else ( set "PSBIN=powershell" ) |
| 6 | %PSBIN% -NoProfile -ExecutionPolicy Bypass -Command "$env:_BATROOT='%~dp0'; $env:_BATPATH='%~f0'; & ([scriptblock]::Create((Get-Content -Raw '%~f0'))) %*" |
| 7 | exit /b %errorlevel% |
| 8 | : end batch #> |
| 9 | # dhu.bat - Desktop Head Unit loopback against the Pixel 8: adb forward, |
| 10 | # fresh logcat capture, and the DHU in its own window. (PowerShell twin of ./dhu.) |
| 11 | # |
| 12 | # Usage (cmd): |
| 13 | # dhu.bat # full loop, log to analysis/dhu-<timestamp>.log |
| 14 | # dhu.bat --no-log # skip the logcat capture (DHU only) |
| 15 | # dhu.bat --device-id 44050DLJH001PC # target a different device |
| 16 | # |
| 17 | # Notes: |
| 18 | # DHU is interactive: piping its stdout closes stdin and it exits, so it |
| 19 | # always launches detached. `adb logcat -d` has proven unreliable (stale |
| 20 | # buffer); this captures live to a file instead. |
| 21 | $ErrorActionPreference = 'Stop' |
| 22 | |
| 23 | $DhuScriptDir = if ($PSScriptRoot) { $PSScriptRoot } else { $env:_BATROOT.TrimEnd('\') } |
| 24 | $DhuDeviceId = '44050DLJH001PC' |
| 25 | $DhuNoLog = $false |
| 26 | |
| 27 | function Show-DhuHelp { |
| 28 | @' |
| 29 | Usage: |
| 30 | dhu.bat # full loop, log to analysis/dhu-<timestamp>.log |
| 31 | dhu.bat --no-log # skip the logcat capture (DHU only) |
| 32 | dhu.bat --device-id 44050DLJH001PC # target a different device |
| 33 | |
| 34 | Notes: |
| 35 | DHU is interactive: piping its stdout closes stdin and it exits, so it |
| 36 | always launches detached. `adb logcat -d` has proven unreliable (stale |
| 37 | buffer); this captures live to a file instead. |
| 38 | '@ |
| 39 | } |
| 40 | |
| 41 | $DhuIdx = 0 |
| 42 | while ($DhuIdx -lt $args.Count) { |
| 43 | $DhuArg = [string]$args[$DhuIdx] |
| 44 | if ($DhuArg -eq '-h' -or $DhuArg -eq '--help' -or $DhuArg -eq 'help') { |
| 45 | Show-DhuHelp |
| 46 | exit 0 |
| 47 | } |
| 48 | elseif ($DhuArg -eq '--no-log' -or $DhuArg -eq '--nolog' -or $DhuArg -eq '-NoLog') { |
| 49 | $DhuNoLog = $true |
| 50 | $DhuIdx++ |
| 51 | } |
| 52 | elseif ($DhuArg -eq '--device-id' -or $DhuArg -eq '-DeviceId') { |
| 53 | if ($DhuIdx + 1 -ge $args.Count) { |
| 54 | [Console]::Error.WriteLine('Error: --device-id needs a value.') |
| 55 | exit 1 |
| 56 | } |
| 57 | $DhuDeviceId = [string]$args[$DhuIdx + 1] |
| 58 | $DhuIdx += 2 |
| 59 | } |
| 60 | elseif ($DhuArg -like '--device-id=*') { |
| 61 | $DhuDeviceId = $DhuArg.Substring('--device-id='.Length) |
| 62 | $DhuIdx++ |
| 63 | } |
| 64 | else { |
| 65 | [Console]::Error.WriteLine("Error: unknown arg '$DhuArg'. Usage: dhu.bat [--no-log] [--device-id SERIAL]") |
| 66 | exit 1 |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | # SDK location: prefer env, then the usual per-OS defaults (plus Windows LOCALAPPDATA). |
| 71 | $DhuBases = @() |
| 72 | if ($env:ANDROID_HOME) { $DhuBases += $env:ANDROID_HOME } |
| 73 | if ($env:ANDROID_SDK_ROOT) { $DhuBases += $env:ANDROID_SDK_ROOT } |
| 74 | $DhuBases += (Join-Path $HOME 'Android/Sdk') |
| 75 | $DhuBases += (Join-Path $HOME 'Library/Android/sdk') |
| 76 | $DhuBases += '/c/Android/Sdk' |
| 77 | $DhuBases += 'C:/Android/Sdk' |
| 78 | if ($env:LOCALAPPDATA) { $DhuBases += (Join-Path $env:LOCALAPPDATA 'Android/Sdk') } |
| 79 | |
| 80 | $DhuExe = '' |
| 81 | foreach ($DhuBase in $DhuBases) { |
| 82 | if (-not $DhuBase) { continue } |
| 83 | $DhuCand = Join-Path $DhuBase 'extras/google/auto/desktop-head-unit' |
| 84 | if (Test-Path -LiteralPath $DhuCand -PathType Leaf) { $DhuExe = $DhuCand; break } |
| 85 | $DhuCandExe = "$DhuCand.exe" |
| 86 | if (Test-Path -LiteralPath $DhuCandExe -PathType Leaf) { $DhuExe = $DhuCandExe; break } |
| 87 | } |
| 88 | if (-not $DhuExe) { |
| 89 | [Console]::Error.WriteLine('Error: DHU not found (looked under ANDROID_HOME, ~/Android/Sdk, ~/Library/Android/sdk, /c/Android/Sdk).') |
| 90 | exit 1 |
| 91 | } |
| 92 | |
| 93 | # adb: prefer PATH, then platform-tools under the same SDK bases. |
| 94 | $DhuAdbCmd = Get-Command adb -ErrorAction SilentlyContinue |
| 95 | $DhuAdb = if ($DhuAdbCmd) { $DhuAdbCmd.Source } else { '' } |
| 96 | if (-not $DhuAdb) { |
| 97 | foreach ($DhuBase in $DhuBases) { |
| 98 | if (-not $DhuBase) { continue } |
| 99 | foreach ($DhuName in @('platform-tools/adb', 'platform-tools/adb.exe')) { |
| 100 | $DhuCand = Join-Path $DhuBase $DhuName |
| 101 | if (Test-Path -LiteralPath $DhuCand -PathType Leaf) { $DhuAdb = $DhuCand; break } |
| 102 | } |
| 103 | if ($DhuAdb) { break } |
| 104 | } |
| 105 | } |
| 106 | if (-not $DhuAdb) { |
| 107 | [Console]::Error.WriteLine('Error: adb not found (install platform-tools or set ANDROID_HOME).') |
| 108 | exit 1 |
| 109 | } |
| 110 | |
| 111 | # The phone listens; the DHU reaches it through the forward. |
| 112 | & $DhuAdb -s $DhuDeviceId forward tcp:5277 tcp:5277 |
| 113 | if ($LASTEXITCODE -ne 0) { |
| 114 | [Console]::Error.WriteLine('Error: adb forward failed.') |
| 115 | exit 1 |
| 116 | } |
| 117 | Write-Output "forward tcp:5277 -> $DhuDeviceId" |
| 118 | |
| 119 | if (-not $DhuNoLog) { |
| 120 | & $DhuAdb -s $DhuDeviceId logcat -c |
| 121 | $DhuStamp = Get-Date -Format 'yyyyMMdd-HHmmss' |
| 122 | $DhuLogFile = Join-Path $DhuScriptDir "analysis/dhu-$DhuStamp.log" |
| 123 | New-Item -ItemType Directory -Force -Path (Split-Path -Parent $DhuLogFile) | Out-Null |
| 124 | $DhuTags = @( |
| 125 | '-s', $DhuDeviceId, 'logcat', '-v', 'time', |
| 126 | 'MaAuto.Service:V', 'MaAuto.Server:V', 'MaAuto.Video:V', 'MaAuto.Sensors:V', |
| 127 | 'MaAuto.Input:V', 'MaAuto.HostManager:V', 'MaAuto.HostSession:V', '*:S' |
| 128 | ) |
| 129 | $DhuJob = Start-Job -Name "dhu-logcat-$DhuStamp" -ScriptBlock { |
| 130 | param($AdbBin, $LogArgs, $LogPath) |
| 131 | & $AdbBin @LogArgs *>> $LogPath |
| 132 | } -ArgumentList $DhuAdb, $DhuTags, $DhuLogFile |
| 133 | Write-Output "logcat -> $DhuLogFile (job $($DhuJob.Id))" |
| 134 | } |
| 135 | |
| 136 | Start-Process -FilePath $DhuExe | Out-Null |
| 137 | Write-Output 'DHU launched (detached)' |
| 138 | exit 0 |