fix(dual-screen): read DiRT Showdown's shorter telemetry packet

DiRT Showdown sends 152-byte packets (38 floats) on port 20777 even with
extradata 3; the page only accepted 256 bytes and stayed empty. The first
38 floats carry speed, rpm, gear, pedals, lap and lap time in the same
places, so they are read now and the rest (lap count, position, fuel, max
rpm) stays empty for these games. The page also says when a port gets
packets it cannot read, with their count and size.
This commit is contained in:
The412Banner committed 2026-10-31 19:49:41 +00:00
1 parent 9e28a11270
commit 30e009a651
4 files changed
+55 -8

No files matched your search

@@ -23,7 +23,7 @@ data class Telemetry(
/**
* Racing telemetry games send over UDP to 127.0.0.1. Each format has its own port, the one the game or
* its common tools use by default:
* - [CODEMASTERS_PORT]: DiRT Rally 1/2, DiRT 4, GRID ("extradata 3" float array).
* - [CODEMASTERS_PORT]: DiRT Rally 1/2, DiRT 4, GRID ("extradata 3" float array), DiRT Showdown (152-byte packet).
* - [FORZA_PORT]: Forza Motorsport 7/2023 and Forza Horizon 4/5 "Data Out" (Sled and Dash).
* - [OUTGAUGE_PORT]: BeamNG.drive and Live for Speed OutGauge.
*/
@@ -45,7 +45,8 @@ object GameTelemetry {
private const val MPS_TO_KMH = 3.6f
// Float indexes of the Codemasters "extradata 3" packet; engine rpm values are sent divided by 10.
// Float indexes of the Codemasters packet; engine rpm values are sent divided by 10. Older games
// (DiRT Showdown) send only the first 38 floats, so lap count, position, fuel and max rpm are missing there.
private const val CM_LAP_TIME = 1
private const val CM_SPEED = 7
private const val CM_THROTTLE = 29
@@ -59,11 +60,12 @@ object GameTelemetry {
private const val CM_LAPS = 60
private const val CM_LAST_LAP = 62
private const val CM_MAX_RPM = 63
private const val CM_FLOATS = 64
private const val CM_MIN_FLOATS = 38
internal fun codemasters(buf: ByteBuffer, length: Int): Telemetry? {
if (length < CM_FLOATS * 4) return null
fun f(i: Int) = buf.getFloat(i * 4)
if (length < CM_MIN_FLOATS * 4) return null
val floats = length / 4
fun f(i: Int) = if (i < floats) buf.getFloat(i * 4) else 0f
val gear = f(CM_GEAR).toInt()
val capacity = f(CM_FUEL_CAPACITY)
return Telemetry(
@@ -63,6 +63,15 @@ private const val THUNDER_RETRY_MS = 3_000L
private const val ELITE_MS = 1_000L
private const val ELITE_SEARCH_MS = 10_000L
/** What arrives on one telemetry port; written only by that port's listener. */
private class PortTraffic {
@Volatile var packets = 0L
@Volatile var length = 0
@Volatile var readable = true
}
private data class UnknownTraffic(val port: Int, val packets: Long, val length: Int)
@Composable
fun BottomTelemetryPage(fps: () -> Float?) {
val context = LocalContext.current
@@ -74,6 +83,8 @@ fun BottomTelemetryPage(fps: () -> Float?) {
var thunder by remember { mutableStateOf<List<Pair<String, String>>>(emptyList()) }
var elite by remember { mutableStateOf<GameStatusFiles.Elite?>(null) }
var busyPorts by remember { mutableStateOf<Set<Int>>(emptySet()) }
val traffic = remember { GameTelemetry.PORTS.associateWith { PortTraffic() } }
var unknown by remember { mutableStateOf<List<UnknownTraffic>>(emptyList()) }
LaunchedEffect(Unit) {
for (port in GameTelemetry.PORTS) launch(Dispatchers.IO) {
@@ -88,6 +99,7 @@ fun BottomTelemetryPage(fps: () -> Float?) {
withContext(Dispatchers.Main) { busyPorts = busyPorts + port }
return@launch
}
val seen = traffic.getValue(port)
socket.use {
val data = ByteArray(2048)
val packet = DatagramPacket(data, data.size)
@@ -95,7 +107,11 @@ fun BottomTelemetryPage(fps: () -> Float?) {
try {
packet.length = data.size
it.receive(packet)
GameTelemetry.parse(port, data, packet.length)?.let { t -> latest.set(System.currentTimeMillis() to t) }
val t = GameTelemetry.parse(port, data, packet.length)
seen.packets++
seen.length = packet.length
seen.readable = t != null
if (t != null) latest.set(System.currentTimeMillis() to t)
} catch (_: SocketTimeoutException) {
} catch (_: java.io.IOException) {
break
@@ -107,6 +123,7 @@ fun BottomTelemetryPage(fps: () -> Float?) {
val reading = latest.get()
race = reading?.takeIf { System.currentTimeMillis() - it.first < TELEMETRY_STALE_MS }?.second
fpsNow = fps()
unknown = traffic.filter { (_, t) -> t.packets > 0 && !t.readable }.map { (port, t) -> UnknownTraffic(port, t.packets, t.length) }
delay(DRAW_MS)
}
}
@@ -165,6 +182,10 @@ fun BottomTelemetryPage(fps: () -> Float?) {
Text(stringResource(R.string.bottom_telemetry_help, GameTelemetry.CODEMASTERS_PORT, GameTelemetry.FORZA_PORT, GameTelemetry.OUTGAUGE_PORT),
fontSize = 14.sp, color = colors.onSurfaceVariant, modifier = Modifier.padding(horizontal = 4.dp))
}
for (u in unknown) {
Text(stringResource(R.string.bottom_telemetry_unknown_format, u.port, u.packets, u.length), fontSize = 13.sp,
color = colors.error, modifier = Modifier.padding(horizontal = 4.dp))
}
if (busyPorts.isNotEmpty()) {
Text(stringResource(R.string.bottom_telemetry_port_busy, busyPorts.sorted().joinToString()), fontSize = 13.sp,
color = colors.error, modifier = Modifier.padding(horizontal = 4.dp))
+2 -1
View File
@@ -1921,7 +1921,8 @@
<string name="bottom_telemetry_balance">Balance</string>
<string name="bottom_telemetry_war_thunder">War Thunder</string>
<string name="bottom_telemetry_elite">Elite Dangerous</string>
<string name="bottom_telemetry_help">Racing games show a live dash here when they send telemetry to 127.0.0.1: DiRT Rally, DiRT 4 and GRID on port %1$d (UDP on, extradata 3), Forza "Data Out" on port %2$d, BeamNG.drive and Live for Speed OutGauge on port %3$d. War Thunder and Elite Dangerous show up on their own. FPS needs the HUD on.</string>
<string name="bottom_telemetry_help">Racing games show a live dash here when they send telemetry to 127.0.0.1: DiRT Showdown, DiRT Rally, DiRT 4 and GRID on port %1$d (motion or UDP output on, extradata 3), Forza "Data Out" on port %2$d, BeamNG.drive and Live for Speed OutGauge on port %3$d. War Thunder and Elite Dangerous show up on their own. FPS needs the HUD on.</string>
<string name="bottom_telemetry_unknown_format">Port %1$d receives data (%2$d packets of %3$d bytes), but in a format this page cannot read yet.</string>
<string name="bottom_telemetry_port_busy">Another program already uses port %1$s, so that game cannot show here.</string>
<string name="rtinst_removing">Removing Linux runtime</string>
@@ -34,6 +34,29 @@ class GameTelemetryTest {
assertEquals(0.5f, t.fuel!!, 0.001f)
}
// Values from a DiRT Showdown packet recorded on a Retroid Pocket Duo (extradata 3 still sends 152 bytes there).
@Test fun codemastersShortPacketFromDirtShowdown() {
val data = packet(152) {
putFloat(1 * 4, 80.085f)
putFloat(7 * 4, 10.739f)
putFloat(29 * 4, 0.816f)
putFloat(33 * 4, 1f)
putFloat(37 * 4, 827.531f)
}
val t = GameTelemetry.parse(GameTelemetry.CODEMASTERS_PORT, data, data.size)!!
assertEquals(38.66f, t.speedKmh!!, 0.01f)
assertEquals("1", t.gear)
assertEquals(8275.31f, t.rpm!!, 0.01f)
assertEquals(0.816f, t.throttle!!, 0.001f)
assertEquals(1, t.lap)
assertEquals(80.085f, t.currentLapS!!, 0.001f)
assertNull(t.maxRpm)
assertNull(t.laps)
assertNull(t.position)
assertNull(t.fuel)
assertNull(t.lastLapS)
}
@Test fun codemastersReverseAndNeutral() {
fun gear(g: Float) = GameTelemetry.parse(GameTelemetry.CODEMASTERS_PORT, packet(264) { putFloat(33 * 4, g) }, 264)!!.gear
assertEquals("N", gear(0f))
@@ -90,7 +113,7 @@ class GameTelemetryTest {
}
@Test fun shortPacketsAreIgnored() {
assertNull(GameTelemetry.parse(GameTelemetry.CODEMASTERS_PORT, ByteArray(100), 100))
assertNull(GameTelemetry.parse(GameTelemetry.CODEMASTERS_PORT, ByteArray(148), 148))
assertNull(GameTelemetry.parse(GameTelemetry.OUTGAUGE_PORT, ByteArray(40), 40))
assertNull(GameTelemetry.parse(1234, ByteArray(400), 400))
}