Author SHA1 Message Date
nilsandClaude Opus 5.5 e90031d6b0 App 2.40.3: the signal recorder listens to the assistance properties
The ACC recording of 2.32.0 only polled, and the AAD_* properties read
their defaults. The head unit's own assistance manager never polls them:
it subscribes to each for change events, and a vendor HAL may fill a
value only while someone listens. EVHardware gains a read-only
listenToProperties and AssistBlock (global vendor properties 0x21x0A1xx,
all 108 AAD_* ids); the recorder subscribes to that block for its three
minutes and lists the properties heard, those with changing values first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LhzAVaUyqcw5PkGjz4mWW6
2026-10-27 00:55:26 +01:00
nilsandClaude Opus 5.5 f7c489858e App 2.40.2: the Grid map moves on the GPU
The map was hardware-rendered, but every frame recomputed every road
point, rebuilt every path and drew the big roads three times, at 20
frames a second, and the car jumped with each GPS fix. Now it is its own
view under the Grid (MapLayerView, a hardware layer): drawn once into a
GPU texture and only redrawn when tiles change or after about 230 m;
each display frame only sets its translation and rotation. The car
glides between fixes, and the Grid over the map redraws only with the
readings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LhzAVaUyqcw5PkGjz4mWW6
2026-10-27 00:32:50 +01:00
nilsandClaude Opus 5.5 c3c47f5159 App 2.40.1: an update always reaches the installer screen
The car refuses the installer session, and only a refusal thrown at
once opened the installer screen; one that came back through the
session's result ended in a toast, so the update downloaded and then
nothing seemed to happen. Now any result but success or the driver's
cancel opens the installer screen with the verified APK, as does a
confirmation that cannot be shown, and a session silent for 20 s is
abandoned for it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LhzAVaUyqcw5PkGjz4mWW6
2026-10-27 00:18:31 +01:00
nilsandClaude Opus 5.5 9ef058cb44 SPECS: app 2.40.0 published
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LhzAVaUyqcw5PkGjz4mWW6
2026-10-27 00:12:47 +01:00
nilsandClaude Opus 5.5 69b1adee2b App 2.40.0: Grid with map straight from above, full height
The map fills the whole screen, top-down and heading up, about 400 m
ahead and 450 m to each side, over a ground grid that turns with it.
The speed moves to a panel under the gears; the battery wall, the
clock, the gears and the disc stand on dark panels over the map.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LhzAVaUyqcw5PkGjz4mWW6
2026-10-27 00:12:06 +01:00
nilsandClaude Opus 5.5 90c2c75fc6 SPECS: app 2.39.2 published
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LhzAVaUyqcw5PkGjz4mWW6
2026-10-27 00:06:51 +01:00
nilsandClaude Opus 5.5 2d7b324899 App 2.39.2: Grid with map seen from higher up
The map gets its own camera, tilted 45 degrees down from about 300 m up,
so roads keep their shape some 400 m ahead instead of collapsing into
the horizon. In map mode the floor starts at half height, the speed is a
little smaller, the floor's grid is fixed to the ground and turns with
the roads, and the power disc has a dark backing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LhzAVaUyqcw5PkGjz4mWW6
2026-10-27 00:06:07 +01:00
nilsandClaude Opus 5.5 09cbd21634 k8s: pin server 2.8.0; app 2.39.1 published
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LhzAVaUyqcw5PkGjz4mWW6
2026-10-26 23:56:28 +01:00
nilsandClaude Opus 5.5 6be2ba5193 App 2.39.1, server 2.8.0: map roads from OpenFreeMap, on demand
The public Overpass answered 429/502 and needs ~40 s a tile while the
car gives up after 8 s, so the server cancelled its queries and the
car's retries added load. Now the server fetches only the tiles a car
asks for, as OpenStreetMap vector tiles from OpenFreeMap (MAP_TILES_URL),
decodes the transportation layer with a small reader of its own, and
keeps each tile 30 days on a volume (MAP_CACHE_DIR). A fetch finishes
even after the car has stopped waiting; the car hears 503 with
Retry-After and gets the tile on its next try.

App: OpenFreeMap/OpenMapTiles attribution on the map, retry after 15 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LhzAVaUyqcw5PkGjz4mWW6
2026-10-26 23:54:30 +01:00
29 changed files with 1657 additions and 501 deletions
+3 -3
View File
@@ -21,7 +21,7 @@ MG4 head unit (Android 9, AAOS, MT2712)
- **server/** — Go 1.27 API at `https://car.k8s.stinnesbeck.com`: stores telemetry, consumption - **server/** — Go 1.27 API at `https://car.k8s.stinnesbeck.com`: stores telemetry, consumption
and charging in InfluxDB, pairs cars by QR code, carries settings typed on a phone, relays and charging in InfluxDB, pairs cars by QR code, carries settings typed on a phone, relays
app releases from the private forge, and serves the roads for the Grid's map from app releases from the private forge, and serves the roads for the Grid's map from
OpenStreetMap (Overpass). It does **not** talk to ABRP. See `server/README.md`. OpenStreetMap vector tiles (OpenFreeMap), fetched per tile on demand and cached on a volume. It does **not** talk to ABRP. See `server/README.md`.
- **k8s/** — plain Kubernetes manifests (InfluxDB, server, Grafana with three provisioned - **k8s/** — plain Kubernetes manifests (InfluxDB, server, Grafana with three provisioned
dashboards, Traefik routes). See `k8s/README.md`. dashboards, Traefik routes). See `k8s/README.md`.
- **EVHardware/** — vendored shared vehicle layer (PolyForm Noncommercial, see `LICENSE.md`). - **EVHardware/** — vendored shared vehicle layer (PolyForm Noncommercial, see `LICENSE.md`).
@@ -76,12 +76,12 @@ Repository: `ssh://git@git.stinnesbeck.com:2222/nils/mg4.git` (remote `origin`),
| Tab | Contents | | Tab | Contents |
|---|---| |---|---|
| Overview | value tiles (battery, range, range/usage/energy since start, 5-min averages of range, usage, power, speed), vehicle info (odometer, lease allowance and the odometer against it), tyre pressures; read every 0.2 s on a background thread. Styles (Settings → Overview style): Tiles, Gauges (`GaugeView` needles), Cockpit (`RingView` rings, `TraceView` power trace), Retro (`RetroView`, seven-segment VFD), Futuristic (`FlowView`, animated energy flow), Sci-fi (`SciFiView`, starship console), Cartoon (`CartoonView`), MG (`OctagonView`, octagonal dials after the classic MG dashboards), and on `ClusterCanvasView`: Minimal, Terminal, Nixie tubes, Chronograph, Grid (`GridView`, neon lines over a moving grid floor, power as a light-cycle trail from `ClusterValues.powerTrail`), Grid with map (the same `GridView` over the roads round the car: GPS while it is shown, tiles from the server's `/v1/map/roads` through `MapRoads`, cached in `files/map`; `RoadTile`, `MapGeometry`), Time circuits (`TimeCircuitsView`, the time machine's LED rows with `Segments14` letters and a flux capacitor), Quest (`QuestView`, a fantasy game interface), Visor (`VisorView`, a helmet display with a motion tracker of the power trail), Empire (`EmpireView`, a strategy game with an isometric world and ages by odometer, `EmpireAge`); the drawn styles share `ClusterValues`. A style shown in its own palette (Sci-fi, Cartoon, Nixie, Time circuits, Quest, Empire; Retro, Terminal, Grid and Visor with "Original") gives the whole app its colours (`StylePalette`, `res/values/style_palettes.xml`) instead of the accent colour. A button in the navigation's header hides the navigation for a full-width Overview (`PREF_OVERVIEW_FULLSCREEN`); a swipe in from the left edge (`EdgeSwipe`) brings it back; without the app's own full screen the car's sidebar takes that edge, so a tab at the left edge (`edge_tab`) is tapped or swiped instead. Pages change only by the navigation (the pager's own swiping is off) | | Overview | value tiles (battery, range, range/usage/energy since start, 5-min averages of range, usage, power, speed), vehicle info (odometer, lease allowance and the odometer against it), tyre pressures; read every 0.2 s on a background thread. Styles (Settings → Overview style): Tiles, Gauges (`GaugeView` needles), Cockpit (`RingView` rings, `TraceView` power trace), Retro (`RetroView`, seven-segment VFD), Futuristic (`FlowView`, animated energy flow), Sci-fi (`SciFiView`, starship console), Cartoon (`CartoonView`), MG (`OctagonView`, octagonal dials after the classic MG dashboards), and on `ClusterCanvasView`: Minimal, Terminal, Nixie tubes, Chronograph, Grid (`GridView`, neon lines over a moving grid floor, power as a light-cycle trail from `ClusterValues.powerTrail`), Grid with map (the same `GridView` over the roads round the car, top-down: GPS while it is shown, tiles from the server's `/v1/map/roads` through `MapRoads`, cached in `files/map`; `RoadTile`, `MapGeometry`; the map is `MapLayerView`, a hardware layer drawn once and moved and turned by the GPU every display frame), Time circuits (`TimeCircuitsView`, the time machine's LED rows with `Segments14` letters and a flux capacitor), Quest (`QuestView`, a fantasy game interface), Visor (`VisorView`, a helmet display with a motion tracker of the power trail), Empire (`EmpireView`, a strategy game with an isometric world and ages by odometer, `EmpireAge`); the drawn styles share `ClusterValues`. A style shown in its own palette (Sci-fi, Cartoon, Nixie, Time circuits, Quest, Empire; Retro, Terminal, Grid and Visor with "Original") gives the whole app its colours (`StylePalette`, `res/values/style_palettes.xml`) instead of the accent colour. A button in the navigation's header hides the navigation for a full-width Overview (`PREF_OVERVIEW_FULLSCREEN`); a swipe in from the left edge (`EdgeSwipe`) brings it back; without the app's own full screen the car's sidebar takes that edge, so a tab at the left edge (`edge_tab`) is tapped or swiped instead. Pages change only by the navigation (the pager's own swiping is off) |
| Consumption / Charging | period totals, daily log, charging sessions and curve | | Consumption / Charging | period totals, daily log, charging sessions and curve |
| Live data | on/off, ABRP API key and user token, upload frequency | | Live data | on/off, ABRP API key and user token, upload frequency |
| Server | URL, pairing by QR, ingest token, installation ID (read-only), config import | | Server | URL, pairing by QR, ingest token, installation ID (read-only), config import |
| Settings | drive mode (Eco, Normal, Sport, Snow, Custom; the lit one is what the car reports; changes only in P at a standstill, `DriveModeWrite`; "Set at start" sets the last confirmed choice again when the service starts with the head unit), full screen, voice key, Overview style with the accent colour beside it (offered only for styles drawn in it, `OverviewStyle.usesAccent`; Retro, Terminal, Grid (and Grid with map, sharing Grid's choice) and Visor add "Original" for their own palette, `hasOriginal`), tyre unit, battery and charging price (one card on wide screens), version, update check, install from USB, About | | Settings | drive mode (Eco, Normal, Sport, Snow, Custom; the lit one is what the car reports; changes only in P at a standstill, `DriveModeWrite`; "Set at start" sets the last confirmed choice again when the service starts with the head unit), full screen, voice key, Overview style with the accent colour beside it (offered only for styles drawn in it, `OverviewStyle.usesAccent`; Retro, Terminal, Grid (and Grid with map, sharing Grid's choice) and Visor add "Original" for their own palette, `hasOriginal`), tyre unit, battery and charging price (one card on wide screens), version, update check, install from USB, About |
| Logs | signal recorder (`SignalRecorder`/`SignalRecording`: every property and vendor getter via `VendorSurfaceProbe.capture`, again and again for 3 minutes, plus steering-wheel key broadcasts; lists what changed, rarest first; saved to the app and a USB stick; how to find where the car publishes something), update log (every install step, with the platform's words), upload log | | Logs | signal recorder (`SignalRecorder`/`SignalRecording`: every property and vendor getter via `VendorSurfaceProbe.capture`, again and again for 3 minutes, plus change events of the driver-assistance block it subscribes to, `EVHardware.listenToProperties` with `AssistBlock`, and steering-wheel key broadcasts; lists what changed, rarest first; saved to the app and a USB stick; how to find where the car publishes something), update log (every install step, with the platform's words), upload log |
Key classes (`app/src/main/java/com/evsuite/abrp/`): `AbrpUploadService` (foreground service, Key classes (`app/src/main/java/com/evsuite/abrp/`): `AbrpUploadService` (foreground service,
15 s tick), `TelemetryUploader` (+ `TelemetryQueue`, persisted), `TelemetryPayload` (server 15 s tick), `TelemetryUploader` (+ `TelemetryQueue`, persisted), `TelemetryPayload` (server
@@ -0,0 +1,24 @@
package com.evsuite.hardware
/**
* The block of vendor properties the head unit's driver-assistance manager
* (`CarAdvancedAssistedDrivingManager`, its `AAD_*` ids) lives in: global vendor properties
* `0x21x0A1xx`, of any value type — `0x2140A101` (int) to `0x2160A15B` (float, `AAD_EPTCCTRGTSPD`,
* by its name the cruise control's target speed) and the rest of the same block. Used to choose
* what the signal recorder listens to; nothing here is ever written. Pure, unit-tested.
*/
object AssistBlock {
private const val GROUP_MASK = 0xF0000000.toInt()
private const val GROUP_VENDOR = 0x20000000
private const val AREA_MASK = 0x0F000000
private const val AREA_GLOBAL = 0x01000000
private const val BLOCK_MASK = 0x0000FF00
private const val BLOCK = 0x0000A100
/** Whether [propertyId] is a global vendor property in the assistance block. */
fun contains(propertyId: Int): Boolean =
propertyId and GROUP_MASK == GROUP_VENDOR &&
propertyId and AREA_MASK == AREA_GLOBAL &&
propertyId and BLOCK_MASK == BLOCK
}
@@ -320,6 +320,105 @@ object EVHardware {
ignitionCallbacks.remove(callback) ignitionCallbacks.remove(callback)
} }
// -------------------------------------------------------------------------
// Listening to properties (the app's signal recorder) — read only
// -------------------------------------------------------------------------
/** A subscription made by [listenToProperties]; [close] ends it. */
class PropertyListening internal constructor(
/** Properties subscribed to, and those the car service refused. */
val subscribed: Int,
val refused: Int,
private val stop: () -> Unit,
) : AutoCloseable {
override fun close() = stop()
}
/**
* Subscribes to every CarPropertyManager property whose id [admit] accepts, the way the
* head unit's own managers do (`CarAdvancedAssistedDrivingManager` registers each of its
* `AAD_*` ids for change events and never polls them), and hands each change event to
* [onEvent] as (property id, area, value as text) on the car service's thread. A vendor HAL
* may fill a property from the car network only while someone listens, so a poll alone can
* read its default. ON_CHANGE properties are asked for every change, CONTINUOUS ones for
* 1 Hz within their limits. Read only: nothing is set. Null when there is no
* CarPropertyManager or no way to subscribe; [PropertyListening.close] unsubscribes.
*/
fun listenToProperties(admit: (Int) -> Boolean, onEvent: (Int, Int, String) -> Unit): PropertyListening? {
val cpm = sCarPropertyManager ?: return null
return try {
val register = cpm.javaClass.methods.firstOrNull {
it.name == "registerCallback" && it.parameterCount == 3
} ?: return null
val unregister = cpm.javaClass.methods.firstOrNull {
it.name == "unregisterCallback" && it.parameterCount == 1
}
val callbackType = register.parameterTypes[0]
if (!callbackType.isInterface) return null
val proxy = Proxy.newProxyInstance(callbackType.classLoader, arrayOf(callbackType)) { self, method, args ->
when (method.name) {
"onChangeEvent" -> {
val cpv = args?.getOrNull(0)
if (cpv != null) {
try {
val type = cpv.javaClass
val id = type.getMethod("getPropertyId").invoke(cpv) as Int
val area = type.getMethod("getAreaId").invoke(cpv) as Int
onEvent(id, area, com.evsuite.hardware.probe.VendorSurfaceRules.render(
type.getMethod("getValue").invoke(cpv)))
} catch (e: Exception) {
AppLogger.d(TAG, "listen: event unreadable: ${e.readFailureReason()}")
}
}
null
}
"onErrorEvent" -> {
val id = args?.getOrNull(0) as? Int
val area = args?.getOrNull(1) as? Int
if (id != null) onEvent(id, area ?: AREA_GLOBAL, "error event")
null
}
"equals" -> self === args?.getOrNull(0)
"hashCode" -> System.identityHashCode(self)
"toString" -> "PropertyListening"
else -> null
}
}
val configs = cpm.javaClass.getMethod("getPropertyList").invoke(cpm) as? List<*> ?: return null
var subscribed = 0
var refused = 0
for (config in configs) {
if (config == null) continue
val type = config.javaClass
val id = runCatching { type.getMethod("getPropertyId").invoke(config) as Int }.getOrNull() ?: continue
if (!admit(id)) continue
val continuous = runCatching { type.getMethod("getChangeMode").invoke(config) as Int }.getOrNull() == 2
val rate = if (!continuous) 0f else {
val min = runCatching { type.getMethod("getMinSampleRate").invoke(config) as Float }.getOrNull() ?: 0f
val max = runCatching { type.getMethod("getMaxSampleRate").invoke(config) as Float }.getOrNull() ?: 1f
1f.coerceIn(min, maxOf(min, max))
}
try {
if (register.invoke(cpm, proxy, id, rate) == false) refused++ else subscribed++
} catch (e: Exception) {
refused++
AppLogger.d(TAG, "listen: 0x%08X refused: %s".format(id, e.readFailureReason()))
}
}
AppLogger.i(TAG, "listening to $subscribed properties ($refused refused)")
PropertyListening(subscribed, refused) {
try {
unregister?.invoke(cpm, proxy)
} catch (e: Exception) {
AppLogger.d(TAG, "listen: unsubscribe failed: ${e.readFailureReason()}")
}
}
} catch (e: Exception) {
AppLogger.w(TAG, "listenToProperties: ${e.readFailureReason()}")
null
}
}
/** Unregisters the CarPropertyManager proxy (call from Service.onDestroy). */ /** Unregisters the CarPropertyManager proxy (call from Service.onDestroy). */
fun unregisterIgnitionPropertyCallback() { fun unregisterIgnitionPropertyCallback() {
val cpm = sCarPropertyManager ?: return val cpm = sCarPropertyManager ?: return
@@ -0,0 +1,29 @@
package com.evsuite.hardware
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
class AssistBlockTest {
@Test
fun `the assistance manager's properties are in the block`() {
// First and last int, the target speed and the object distance (floats).
for (id in intArrayOf(0x2140A101, 0x2140A171, 0x2160A15B, 0x2160A12C, 0x2160A114)) {
assertTrue("0x%08X".format(id), AssistBlock.contains(id))
}
}
@Test
fun `other properties are not`() {
for (id in intArrayOf(
0x2140A0FF, // the block before
0x2140A201, // the block after
0x11400400, // a standard (system) property
0x2340A101, // not global
0x0140A101, // no group
)) {
assertFalse("0x%08X".format(id), AssistBlock.contains(id))
}
}
}
+7 -4
View File
@@ -21,8 +21,10 @@ software. `LICENSE` names all of them; keep every line.
## Map data ## Map data
The roads of the "Grid with map" style are © OpenStreetMap contributors, under the The roads of the "Grid with map" style are © OpenStreetMap contributors, under the
[Open Database Licence](https://opendatacommons.org/licenses/odbl/); the style shows that line [Open Database Licence](https://opendatacommons.org/licenses/odbl/), in vector tiles
whenever it draws them. The server fetches them from an Overpass API (`server/maps.go`). © OpenMapTiles served by [OpenFreeMap](https://openfreemap.org). The style shows
"OpenFreeMap © OpenMapTiles · © OpenStreetMap contributors" whenever it draws them. The server
fetches the tiles (`server/maps.go`).
## The EVHardware library ## The EVHardware library
@@ -37,8 +39,9 @@ top. Those are Fatih's reads the car needs and upstream does not have — the ve
ids `0x21401553`–`56`, the vendor mileage `0x21401566` for the odometer, the charging status ids `0x21401553`–`56`, the vendor mileage `0x21401566` for the odometer, the charging status
from `0x2140F409`, SoC, range, power, pack voltage and current on SWI69 as on SWI68, and SWI69 from `0x2140F409`, SoC, range, power, pack voltage and current on SWI69 as on SWI68, and SWI69
speed already in km/h — plus translated comments and, from this fork, the gear's raw code speed already in km/h — plus translated comments and, from this fork, the gear's raw code
(`EVHardware.getGearRaw`, `EnergySnapshot.gearRaw`) and a drive mode write without the binder (`EVHardware.getGearRaw`, `EnergySnapshot.gearRaw`), a drive mode write without the binder
fallback (`writeDriveModeProperty`). To take upstream again, merge three ways with fallback (`writeDriveModeProperty`), and read-only listening to properties for the signal
recorder (`listenToProperties`, `AssistBlock`). To take upstream again, merge three ways with
`e4e1158` as the base. `e4e1158` as the base.
The car app links EVHardware into its APK, so the **built app** may only be used and passed on The car app links EVHardware into its APK, so the **built app** may only be used and passed on
+7
View File
@@ -110,3 +110,10 @@ That screen needs a `file://` URI on the car build: a read grant on our FileProv
when it comes from the system user (`not exported from UID 1000`), and the installer reports the when it comes from the system user (`not exported from UID 1000`), and the installer reports the
unreadable file as "There was a problem parsing the package". On the emulator the session still unreadable file as "There was a problem parsing the package". On the emulator the session still
installs silently. installs silently.
The refusal does not always come at once: the platform may also answer a moment later, through
the session's result (`OtaInstallResultReceiver`). Since 2.40.1 that answer opens the installer
screen too, as does a confirmation the platform asks for but cannot show; only **Cancel** on that
screen ends the update there. And if the session says nothing at all for 20 s, the app abandons
it and opens the installer screen. Before 2.40.1 a late refusal ended in a short message only:
the update downloaded and then nothing seemed to happen.
+3 -3
View File
@@ -82,20 +82,20 @@ car ──live telemetry───▶ car.k8s.stinnesbeck.com (Go)
The car sends live data to ABRP itself, with the ABRP credentials set on the Live data tab, so The car sends live data to ABRP itself, with the ABRP credentials set on the Live data tab, so
ABRP and the server do not depend on each other. The server stores, relays the app's updates ABRP and the server do not depend on each other. The server stores, relays the app's updates
from the forge and serves the roads for the Grid's map (from OpenStreetMap). The car authenticates from the forge and serves the roads for the Grid's map (OpenStreetMap, through OpenFreeMap). The car authenticates
with the server using a token of its own, obtained by pairing with a phone (below). with the server using a token of its own, obtained by pairing with a phone (below).
## The app ## The app
| Tab | What it shows | | Tab | What it shows |
|---|---| |---|---|
| **Overview** | Battery, range, power and speed; range, usage and energy since start; 5-minute averages of range, usage, power and speed; vehicle info (odometer, lease allowance, climate setpoint) and tyre pressures. Read five times a second. Looks to choose from in Settings → Overview style: Tiles, Gauges, Cockpit, Retro, Futuristic, Sci-fi, Cartoon, MG, Minimal, Terminal, Nixie tubes, Chronograph, Grid, Grid with map, Time circuits, Quest, Visor and Empire ([THEMES.md](THEMES.md) shows them all). A style with its own colours (Sci-fi, Cartoon, Nixie tubes, Time circuits, Quest, Empire, and Retro, Terminal, Grid and Visor with "Original"). Grid with map lays the roads round the car on the Grid's floor: the app asks the car's server for the map tile it is in (not its position), the server fetches the roads from OpenStreetMap's Overpass API and keeps them, and the GPS runs for it only while that style is on screen colours the whole app to match, e.g. turquoise for Retro. The small button next to the MG4 logo in the navigation hides the navigation for a full-width Overview; swipe in from the left edge (a faint handle marks it) to bring it back; with Settings → Full screen off, the car's own sidebar sits at that edge, so tap the small tab at the left edge (›) instead. Pages change only by the navigation, not by swiping sideways | | **Overview** | Battery, range, power and speed; range, usage and energy since start; 5-minute averages of range, usage, power and speed; vehicle info (odometer, lease allowance, climate setpoint) and tyre pressures. Read five times a second. Looks to choose from in Settings → Overview style: Tiles, Gauges, Cockpit, Retro, Futuristic, Sci-fi, Cartoon, MG, Minimal, Terminal, Nixie tubes, Chronograph, Grid, Grid with map, Time circuits, Quest, Visor and Empire ([THEMES.md](THEMES.md) shows them all). A style with its own colours (Sci-fi, Cartoon, Nixie tubes, Time circuits, Quest, Empire, and Retro, Terminal, Grid and Visor with "Original") colours the whole app to match, e.g. turquoise for Retro. Grid with map lays the roads round the car on the Grid's floor: the app asks the car's server for the map tile it is in (not its position), the server fetches only that tile from OpenFreeMap's OpenStreetMap vector tiles and keeps it, and the GPS runs for it only while that style is on screen. The small button next to the MG4 logo in the navigation hides the navigation for a full-width Overview; swipe in from the left edge (a faint handle marks it) to bring it back; with Settings → Full screen off, the car's own sidebar sits at that edge, so tap the small tab at the left edge (›) instead. Pages change only by the navigation, not by swiping sideways |
| **Consumption** | Lifetime, last 7 and 30 days, Trip A and B (distance, energy, average consumption and speed, SOC change), and a daily log of the last four months | | **Consumption** | Lifetime, last 7 and 30 days, Trip A and B (distance, energy, average consumption and speed, SOC change), and a daily log of the last four months |
| **Charging** | The current session (SOC, voltage, current, power, energy into the battery and from the grid, time, cost) and its SOC/power curve | | **Charging** | The current session (SOC, voltage, current, power, energy into the battery and from the grid, time, cost) and its SOC/power curve |
| **Live data** | Live data on/off, ABRP credentials for sending directly from the car, upload frequency | | **Live data** | Live data on/off, ABRP credentials for sending directly from the car, upload frequency |
| **Server** | Server URL, pairing by QR code, ingest token, installation ID, config import | | **Server** | Server URL, pairing by QR code, ingest token, installation ID, config import |
| **Settings** | Full screen, voice key, Overview style and accent colour, tyre pressure unit; drive mode (Eco, Normal, Sport, Snow, Custom: changes only in P at a standstill; the highlighted one is what the car reports; with "Set at start" the last confirmed choice is set again when the car starts, while it is still in P); battery (manual SoH, usable capacity) and charging price; entry on the phone; version, update check, install from USB stick, About | | **Settings** | Full screen, voice key, Overview style and accent colour, tyre pressure unit; drive mode (Eco, Normal, Sport, Snow, Custom: changes only in P at a standstill; the highlighted one is what the car reports; with "Set at start" the last confirmed choice is set again when the car starts, while it is still in P); battery (manual SoH, usable capacity) and charging price; entry on the phone; version, update check, install from USB stick, About |
| **Logs** | Signal recorder (records every value the head unit offers for 3 minutes and lists what changed, to find where the car publishes something; saved to a USB stick too), update log (every install step), upload log | | **Logs** | Signal recorder (records every value the head unit offers for 3 minutes, listens to the driver-assistance values for every change they report, and lists what changed, to find where the car publishes something; saved to a USB stick too), update log (every install step), upload log |
- **Opening the app:** the MG4 launcher does not list third-party apps. **Double-press the - **Opening the app:** the MG4 launcher does not list third-party apps. **Double-press the
voice key** on the steering wheel and the app comes to the front (switch on the Settings voice key** on the steering wheel and the app comes to the front (switch on the Settings
+85 -1
View File
@@ -78,7 +78,9 @@ tyre pressures, GPS (lat, lon, elevation, heading) and the is_* flags. It does *
(`AAD_EPTCCTRGTSPD` `0x2160A15B` 0.0, `AAD_ACC_OBJ_*` 0.0 / -128.0, modes 0). A signal recording (`AAD_EPTCCTRGTSPD` `0x2160A15B` 0.0, `AAD_ACC_OBJ_*` 0.0 / -128.0, modes 0). A signal recording
of all 1,194 values while the set speed was 101, 90 and 80 km/h shows none of those numbers of all 1,194 values while the set speed was 101, 90 and 80 km/h shows none of those numbers
(also tried as m/s, mph and ×2/×10/×16/×64) outside the actual speed, and the ACC buttons send (also tried as m/s, mph and ×2/×10/×16/×64) outside the actual speed, and the ACC buttons send
no key broadcast. Most likely only the instrument cluster has it. no key broadcast. Most likely only the instrument cluster has it. Not yet tried: listening
(subscribing) instead of polling, as the head unit's own manager does — the recorder does it
since 2.40.3; a drive with ACC will tell.
## To do ## To do
@@ -138,6 +140,88 @@ tyre pressures, GPS (lat, lon, elevation, heading) and the is_* flags. It does *
confirmed car facts, app structure, UI conventions, build/test/ship, documents. It replaces confirmed car facts, app structure, UI conventions, build/test/ship, documents. It replaces
upstream's EVABRPUploader text. The test emulator is now in the flake: `nix run .#car-emulator` upstream's EVABRPUploader text. The test emulator is now in the flake: `nix run .#car-emulator`
(Android 9 AOSP at 1920×720, 160 dpi; the car build installs as the system user — checked). (Android 9 AOSP at 1920×720, 160 dpi; the car build installs as the system user — checked).
- 2026-10-27 — App 2.40.3: **the signal recorder listens** (owner: "is there really nothing we
can do about the readout of the cruise control set point"). The 2.32.0 recording only polled.
The head unit's `CarAdvancedAssistedDrivingManager` never polls its 108 `AAD_*` properties: it
registers a listener for each (`registerListener(…, 0.0f)`, on change), and a vendor HAL may
fill a value from the car network only while someone listens — which would read as the
defaults we saw. EVHardware gains `listenToProperties` (read only: the CarPropertyManager's
`registerCallback` per admitted property, rate 0 for ON_CHANGE, 1 Hz within limits for
CONTINUOUS, one proxy, `unregisterCallback` on close) and `AssistBlock` (global vendor
properties `0x21x0A1xx`, which holds all 108 `AAD_*` ids; tested). The recorder subscribes to
that block for its 3 minutes; `SignalRecording.event` counts every event and puts value changes
on the timeline; the file and the Logs tab list the properties heard, those with changing
values first ("heard …"). Emulator (no car service): the recorder notes that listening is not
possible, records, saves the new section; no crash. The subscription itself is the same
reflection as the ignition listener that works on the car, but untried on the car.
- 2026-10-27 — App 2.40.2: **the map moves on the GPU** (owner: "is the map screen gpu
accelerated? it looks a bit slow on update"). It was hardware-rendered, but every frame (20 a
second) recomputed every road point on the main thread, rebuilt all paths, and drew the big
roads three times for the glow; and the car jumped with each GPS fix (1 Hz). Now the map is
its own view under the Grid (`MapLayerView`, `LAYER_TYPE_HARDWARE`): drawn once into a GPU
texture round an anchor and only redrawn when tiles come or go or the car is ~280 layout units
(≈230 m) from the anchor; each display frame (`postOnAnimation`, only while that style is on
screen and the map shows) sets its translation, pivot and rotation, which the GPU composites.
The car glides towards each fix (eased, snaps beyond 200 m), the heading eases; GridView over
the map no longer animates itself (only the readings redraw it), the "far roads fainter" sort
is a fixed dark gradient towards the top. Layer size on the car about 2500 px square (~25 MB).
Measured on the emulator: with SwiftShader (software GPU) old and new both ~57 ms a frame, and
with the host's Intel GPU (`-gpu host`) both run at the display rate (~18 ms) — the emulator
is too fast to show the car's slowness, so the gain on the head unit is reasoned, not
measured. Not tried on the car.
- 2026-10-27 — App 2.40.1: **an update that downloaded but never reached the installer** (owner:
"sometimes … downloads the update but then doesn't start the installer"). The car refuses the
installer session ("Not allowed to install non-system apps on internal storage"); only a
refusal thrown at once led to the installer screen. A refusal that came back through the
session's result was a toast and nothing else. Now any answer but success, the confirmation and
the driver's own cancel opens the installer screen with the verified APK (path and session id
ride on the result intent; `goAsync` for the copy), a confirmation that cannot be shown does
too, and a session silent for 20 s is abandoned for it. Emulator, with a throwaway build whose
session bytes were damaged (refused later, status 4 `INSTALL_PARSE_FAILED_NOT_APK`) and which
read the update from Downloads: the installer screen opened by itself, Install, now 2.40.1; the
update log shows every step. The 20 s watch is untested (no way to make a session stay silent).
Not tried on the car. OTA.md updated.
- 2026-10-27 — App 2.40.0 published (`car-2.40.0`).
- 2026-10-27 — App 2.40.0: **Grid with map straight from above, full height** (owner: 2.39.2
"does not look right"; wanted the map over the whole height and fully top-down). The map is
the whole background in map mode: orthographic, heading up, 1.2 layout units per metre (about
400 m ahead, 125 m behind, 450 m to each side on the car's screen), the car 150 units above
the bottom edge; ground grid every 50 m fixed to the ground; roads from 250 m ahead fainter;
the camera, the sky and the floor are gone there. The speed moves to a panel under the gears
(120 units), and the battery wall, the clock, the gears and the disc get dark panels. The
plain Grid is unchanged. Emulator with the Berlin tiles, both layouts. Not tried on the car.
- 2026-10-27 — App 2.39.2 published (`car-2.39.2`).
- 2026-10-27 — App 2.39.2: **Grid with map seen from higher up** (owner: the map looked like a
fish-eye; wanted it more top-down). The map has its own pinhole camera now: 45° down, 300 m
up, the car on its axis just above the read-outs, the top of the floor about 420 m ahead
(scale there ~60 % of the car's, before ~1/20 at 900 m); the floor starts at half height in
map mode and the speed is 170 instead of 210 units; the floor's grid becomes a ground grid
(a line every 50 m, fixed to the ground, turning with the roads); roads from 250 m ahead
fainter; the power disc gets a dark backing over the map. Emulator with the Berlin tiles,
both layouts. Not tried on the car.
- 2026-10-26 — Server 2.8.0 image pushed and pinned in `k8s/server.yaml` by digest
`sha256:8b8d94e0562833d196162866b7fdc6910d5dd219f6eb84572d4d5c91b74f7ff1` (checked with skopeo);
app 2.39.1 published (`car-2.39.1`). Not applied yet: the owner applies it.
- 2026-10-26 — App 2.39.1 and server 2.8.0: **the map's roads come from OpenFreeMap, on
demand.** In the car, server 2.7.0 logged `overpass answered 502`, `overpass could not be
reached`, and the owner saw 429s: the public Overpass limits per IP, and a tile takes it ~40 s
while the car gives up after 8 s (`ServerHttp.TIMEOUT_MS`) — the server then cancelled the
query, threw the work away, and the car's retries added to the load. Owner's choice: fetch
only the tiles the car asks for (a self-hosted Overpass would need a whole region imported).
Server: the zoom-14 OpenStreetMap vector tile (OpenMapTiles schema) from the TileJSON at
`MAP_TILES_URL` (default OpenFreeMap, free, no key, no limits; its template re-read every 6 h,
https only), decoded by our own minimal protobuf/MVT reader (`mvt.go`, the `transportation`
layer; classes motorway/trunk, primary/secondary, tertiary, minor), simplified as before. A
fetch runs to its end without the car (requests wait ≤ 6 s, then 503 with `Retry-After: 10`),
at most two at once, one per tile; tiles kept 30 days in `MAP_CACHE_DIR` (a 1 Gi volume
`mg4-server-maps`, pod `fsGroup` 65532) and the last 300 in memory; `OVERPASS_URL` is gone.
Tests with an encoded vector tile and a fake tile host (classes, split lines, scaled extent,
malformed tiles, gzip from the host, fetched once and kept across a restart, a slow fetch
finished for the next try, failures not cached and no tile in the log, http template refused,
config). Real: the 9 Berlin tiles in 0.7 s (9–15 KB each). App 2.39.1: "OpenFreeMap ©
OpenMapTiles · © OpenStreetMap contributors" on the map, a tile the server did not have yet is
asked for again after 15 s (was a minute). Emulator with those tiles: same roads as before.
Not tried: the deployed server and the car.
- 2026-10-26 — Server 2.7.0 image pushed (`git.stinnesbeck.com/nils/mg4-server:2.7.0`, Go 1.27.3, - 2026-10-26 — Server 2.7.0 image pushed (`git.stinnesbeck.com/nils/mg4-server:2.7.0`, Go 1.27.3,
govulncheck clean) and pinned in `k8s/server.yaml` by digest govulncheck clean) and pinned in `k8s/server.yaml` by digest
`sha256:e5da9452a9dbdfabe3340766cc51f18bb1418b2588ab534714550470abe1ce29` (checked with skopeo); `sha256:e5da9452a9dbdfabe3340766cc51f18bb1418b2588ab534714550470abe1ce29` (checked with skopeo);
+6 -4
View File
@@ -165,11 +165,13 @@ films' other side.
## Grid with map ## Grid with map
The Grid, with the roads round the car from OpenStreetMap laid on its floor as light lanes, in The Grid over a map: the roads round the car from OpenStreetMap (through OpenFreeMap) fill the
the floor's perspective and turned with the direction you drive: motorways glow brightest, then whole screen as light lanes, straight from above and turned with the direction you drive, about
main roads, then the smaller streets, fading towards the horizon. The car is the chevron on the 400 m ahead and 450 m to each side: motorways glow brightest, then main roads, then the smaller
streets, the far ones fainter, over a ground grid every 50 m that turns and slides with them.
The speed, the battery wall, the power disc and the clock stand on dark panels over the map. The car is the chevron on the
floor, and between GPS fixes it runs on with the speed. The roads come through the car's own floor, and between GPS fixes it runs on with the speed. The roads come through the car's own
server, which fetches them from OpenStreetMap and keeps them (see the README); the car also keeps server, which fetches only the map tiles you drive through and keeps them (see the README); the car also keeps
the last few hundred tiles itself. Without GPS, the server or the roads, the floor says so and the last few hundred tiles itself. Without GPS, the server or the roads, the floor says so and
stays the plain grid. The light-cycle trail makes way for the map here; power stays on the disc. stays the plain grid. The light-cycle trail makes way for the map here; power stays on the disc.
Shown with sample values over real roads in central Berlin. Shown with sample values over real roads in central Berlin.
+2 -2
View File
@@ -34,8 +34,8 @@ android {
applicationId "com.stinnesbeck.nilsmg4" applicationId "com.stinnesbeck.nilsmg4"
minSdk 27 minSdk 27
targetSdk 36 targetSdk 36
versionCode 142 versionCode 148
versionName "2.39.0" versionName "2.40.3"
// Nothing about the server ships in the APK: its URL and ingest token are entered in // Nothing about the server ships in the APK: its URL and ingest token are entered in
// the app's Server tab (ServerConfig), and the database is only reachable by the server. // the app's Server tab (ServerConfig), and the database is only reachable by the server.
+87 -132
View File
@@ -23,10 +23,14 @@ import android.os.SystemClock;
* the trail; the floor stands still when the speed is unread. * the trail; the floor stands still when the speed is unread.
* *
* With a map ("Grid with map", {@link MapRoads}): the roads round the car from OpenStreetMap lie * With a map ("Grid with map", {@link MapRoads}): the roads round the car from OpenStreetMap lie
* on the floor in its perspective, heading up, the bigger ones glowing brighter, with the car * under everything, heading up, the bigger ones glowing brighter, with the car as a light
* as a light marker a little way up the floor; between GPS fixes the car's position runs on * marker low on the screen; between GPS fixes the car's position runs on
* with the speed. The floor's tiles are then 40 m, so they move with the roads, and the trail * with the speed. The map is then the whole background, straight from above (about
* makes way for the map. Without a fix, a server or the roads, the plain floor with a line * 400 m ahead, 450 m to each side), over a ground grid (a line every 50 m) that turns and slides
* with the roads, fading into the dark towards the top. It is its own view under this one
* ({@link MapLayerView}): drawn once into a hardware layer and moved and turned by the GPU each
* display frame, the car gliding between fixes; the speed, the battery wall, the disc and the clock stand on dark panels over
* it, and the trail makes way for the map. Without a fix, a server or the roads, the plain floor with a line
* saying why. * saying why.
* *
* Two palettes: the original cyan, or the chosen accent colour ({@link #setAccentPalette}). A * Two palettes: the original cyan, or the chosen accent colour ({@link #setAccentPalette}). A
@@ -39,11 +43,6 @@ final class GridView extends ClusterCanvasView {
/** Battery segments in the light wall, disc segments, metres per grid tile. */ /** Battery segments in the light wall, disc segments, metres per grid tile. */
private static final int WALL = 20, DISC = 60; private static final int WALL = 20, DISC = 60;
private static final float TILE_M = 4f; private static final float TILE_M = 4f;
/**
* The map: metres per depth step of the floor, the car's depth (just above the read-outs),
* the nearest depth drawn, how far ahead, and the depth from which roads are drawn fainter.
*/
private static final float MAP_UNIT_M = 40f, CAR_DEPTH = 1.6f, NEAR = 0.6f, FAR_M = 900f, FADE_DEPTH = 5f;
private final Paint line = new Paint(Paint.ANTI_ALIAS_FLAG); private final Paint line = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Paint fill = new Paint(Paint.ANTI_ALIAS_FLAG); private final Paint fill = new Paint(Paint.ANTI_ALIAS_FLAG);
@@ -58,13 +57,10 @@ final class GridView extends ClusterCanvasView {
private final int accent, accentLight; private final int accent, accentLight;
/** Null for the plain Grid. */ /** Null for the plain Grid. */
private final MapRoads map; private final MapRoads map;
/** Per road class, near and far (from {@link #FADE_DEPTH}). */ /** The map under this view (a hardware layer, moved by the GPU); null for the plain Grid. */
private final Path[] roads = new Path[RoadTile.CLASSES * 2]; private final MapLayerView mapLayer;
private float heading = Float.NaN; private final Paint fade = new Paint();
/** The pen while a road is added: the last point, and whether it continues from there. */ private int fadeFor = -1;
private float penX, penY;
private boolean penDown;
private Path penPath;
private int main, light, dim, alert; private int main, light, dim, alert;
/** The floor's position within one tile (0..1) and the disc's inner ring angle. */ /** The floor's position within one tile (0..1) and the disc's inner ring angle. */
@@ -79,7 +75,7 @@ final class GridView extends ClusterCanvasView {
GridView(Context context, MapRoads map) { GridView(Context context, MapRoads map) {
super(context); super(context);
this.map = map; this.map = map;
for (int i = 0; i < roads.length; i++) roads[i] = new Path(); mapLayer = map == null ? null : new MapLayerView(context, map);
accent = AccentColor.resolve(context, R.attr.evAccent); accent = AccentColor.resolve(context, R.attr.evAccent);
accentLight = AccentColor.resolve(context, R.attr.evAccentLight); accentLight = AccentColor.resolve(context, R.attr.evAccentLight);
line.setStyle(Paint.Style.STROKE); line.setStyle(Paint.Style.STROKE);
@@ -95,9 +91,24 @@ final class GridView extends ClusterCanvasView {
dim = AccentColor.withAlpha(main, 0x40); dim = AccentColor.withAlpha(main, 0x40);
alert = useAccent ? 0xFFFFFFFF : 0xFFFF8A1F; alert = useAccent ? 0xFFFFFFFF : 0xFFFF8A1F;
skyFor = -1; skyFor = -1;
if (mapLayer != null) mapLayer.setColours(main, light);
invalidate(); invalidate();
} }
/** The map layer, for the pane to put under this view; null for the plain Grid. */
MapLayerView mapLayer() {
return mapLayer;
}
@Override
void setValues(ClusterValues values) {
super.setValues(values);
if (mapLayer != null) {
mapLayer.setSpeed(v.speedKmh);
mapLayer.kick();
}
}
@Override @Override
protected void onDraw(Canvas c) { protected void onDraw(Canvas c) {
super.onDraw(c); super.onDraw(c);
@@ -105,25 +116,32 @@ final class GridView extends ClusterCanvasView {
if (w <= 0 || h <= 0) return; if (w <= 0 || h <= 0) return;
float u = Math.min(h / 640f, w / 1100f); float u = Math.min(h / 640f, w / 1100f);
advance(); advance();
c.drawColor(0xFF000000); // With a map, the map layer underneath is the whole background (this view stays clear
// there), and the rest stands on dark panels over it.
boolean mapShown = mapLayer != null && mapLayer.isShowing();
float horizon = h * 0.55f; float horizon = h * 0.55f;
if (mapShown) {
drawCar(c, w, h, u);
} else {
c.drawColor(0xFF000000);
drawSky(c, w, horizon); drawSky(c, w, horizon);
drawFloor(c, w, h, horizon, u); drawFloor(c, w, h, horizon, u);
boolean mapShown = map != null && drawMap(c, w, h, horizon, u); }
if (map != null && !mapShown) drawMapState(c, w, h, horizon, u); if (map != null && !mapShown) drawMapState(c, w, h, horizon, u);
if (mapShown) drawPanels(c, w, horizon, u);
drawGears(c, w, u); drawGears(c, w, u);
drawClock(c, w, u); drawClock(c, w, u);
drawSpeed(c, w, horizon, u); if (mapShown) drawMapSpeed(c, w, u);
else drawSpeed(c, w, horizon, u);
drawWall(c, horizon, u); drawWall(c, horizon, u);
drawDisc(c, w, u); drawDisc(c, w, mapShown, u);
if (!mapShown) drawTrail(c, w, h, u); if (!mapShown) drawTrail(c, w, h, u);
drawReadouts(c, w, h, u); drawReadouts(c, w, h, u);
if (mapShown) drawAttribution(c, w, h, u); if (mapShown) drawAttribution(c, w, h, u);
boolean moving = v.speedKmh != null && v.speedKmh > 0.5f; boolean moving = v.speedKmh != null && v.speedKmh > 0.5f;
// The map costs more per frame: 20 a second is smooth enough for it. // Over the map only the readings change; the map layer moves itself.
keepAnimating(moving, mapShown ? 50 : 33); keepAnimating(moving && !mapShown, 33);
} }
/** Moves the floor by the distance driven since the last frame, and turns the disc. */ /** Moves the floor by the distance driven since the last frame, and turns the disc. */
@@ -132,8 +150,7 @@ final class GridView extends ClusterCanvasView {
float dt = lastFrame == 0 ? 0f : Math.min(0.2f, (now - lastFrame) / 1000f); float dt = lastFrame == 0 ? 0f : Math.min(0.2f, (now - lastFrame) / 1000f);
lastFrame = now; lastFrame = now;
if (v.speedKmh != null && v.speedKmh > 0.5f) { if (v.speedKmh != null && v.speedKmh > 0.5f) {
float tile = map != null ? MAP_UNIT_M : TILE_M; floorPhase = (floorPhase + v.speedKmh / 3.6f * dt / TILE_M) % 1f;
floorPhase = (floorPhase + v.speedKmh / 3.6f * dt / tile) % 1f;
} }
if (v.powerKw != null) discTurn = (discTurn + v.powerKw * 3f * dt) % 360f; if (v.powerKw != null) discTurn = (discTurn + v.powerKw * 3f * dt) % 360f;
} }
@@ -172,118 +189,51 @@ final class GridView extends ClusterCanvasView {
// ---------- Map ---------- // ---------- Map ----------
/** The roads round the car on the floor; false when there is nothing to draw. */ /** Over the map: the far roads fading into the dark towards the top, and the car. */
private boolean drawMap(Canvas c, float w, float h, float horizon, float u) { private void drawCar(Canvas c, float w, float h, float u) {
MapRoads.Fix fix = map.fix(); float carSx = w / 2f, carSy = h - MapLayerView.CAR_FROM_BOTTOM * u;
java.util.Collection<RoadTile> tiles = map.tiles(); if (fadeFor != (int) (carSy + h)) {
if (fix == null || tiles.isEmpty()) return false; fade.setShader(new LinearGradient(0, 0, 0, carSy, 0xB0000000, 0x00000000, Shader.TileMode.CLAMP));
// Heading up: ease round the shorter way; north up until the car has moved. fadeFor = (int) (carSy + h);
float target = Float.isNaN(fix.bearing) ? (Float.isNaN(heading) ? 0f : heading) : fix.bearing;
if (Float.isNaN(heading)) heading = target;
else heading = (heading + (((target - heading) % 360f + 540f) % 360f - 180f) * 0.2f + 360f) % 360f;
// Between fixes the car runs on with the speed, for at most 2.5 s.
double mpu = MapGeometry.metresPerUnit(fix.lat);
float ran = 0f;
if (v.speedKmh != null && !Float.isNaN(fix.bearing)) {
ran = v.speedKmh / 3.6f * Math.min(2.5f, (SystemClock.elapsedRealtime() - fix.elapsedMs) / 1000f);
} }
double run = Math.toRadians(Float.isNaN(fix.bearing) ? 0f : fix.bearing); c.drawRect(0, 0, w, carSy, fade);
double carX = MapGeometry.worldX(fix.lon) + ran * Math.sin(run) / mpu; // A light-cycle chevron pointing ahead.
double carY = MapGeometry.worldY(fix.lat) - ran * Math.cos(run) / mpu;
float sin = (float) Math.sin(Math.toRadians(heading)), cos = (float) Math.cos(Math.toRadians(heading));
float lane = w * 0.09f, depth = h - horizon, vx = w / 2f, m = (float) mpu;
double range = FAR_M / mpu;
for (Path p : roads) p.reset();
for (RoadTile tile : tiles) {
double ox = (double) tile.x * MapGeometry.EXTENT - carX, oy = (double) tile.y * MapGeometry.EXTENT - carY;
if (ox > range || oy > range || ox + 2 * MapGeometry.EXTENT < -range || oy + 2 * MapGeometry.EXTENT < -range) {
continue;
}
float fx = (float) ox, fy = (float) oy;
for (int i = 0; i < tile.points.length; i++) {
int[] b = tile.bounds[i];
if (fx + b[2] < -range || fx + b[0] > range || fy + b[3] < -range || fy + b[1] > range) continue;
int cls = tile.classes[i];
int[] pts = tile.points[i];
penDown = false;
float pa = 0f, pf = 0f;
for (int k = 0; k < pts.length; k += 2) {
float east = (fx + pts[k]) * m, north = -(fy + pts[k + 1]) * m;
float fwd = east * sin + north * cos, side = east * cos - north * sin;
if (k > 0) segment(cls, pa, pf, side, fwd, vx, horizon, lane, depth);
pa = side;
pf = fwd;
}
}
}
c.save();
c.clipRect(0, horizon, w, h);
line.setStrokeJoin(Paint.Join.ROUND);
line.setStrokeCap(Paint.Cap.ROUND);
// Far roads thin and faint, so the horizon stays a line rather than a tangle.
for (int cls = RoadTile.CLASSES - 1; cls >= 0; cls--) {
line.setColor(AccentColor.withAlpha(cls == 0 ? light : main, cls >= 3 ? 0x30 : 0x50));
line.setStrokeWidth((cls == 0 ? 1.6f : 1f) * u);
c.drawPath(roads[cls * 2 + 1], line);
}
line.setColor(AccentColor.withAlpha(main, 0x80));
line.setStrokeWidth(1.4f * u);
c.drawPath(roads[3 * 2], line);
line.setColor(AccentColor.withAlpha(main, 0xC0));
line.setStrokeWidth(2f * u);
c.drawPath(roads[2 * 2], line);
glow(c, roads[2], main, 2.4f * u);
glow(c, roads[0], light, 3.2f * u);
line.setStrokeJoin(Paint.Join.MITER);
line.setStrokeCap(Paint.Cap.SQUARE);
c.restore();
// The car: a light-cycle chevron pointing ahead.
float cy = horizon + depth / CAR_DEPTH;
path.reset(); path.reset();
path.moveTo(vx, cy - 16f * u); path.moveTo(carSx, carSy - 18f * u);
path.lineTo(vx + 11f * u, cy + 10f * u); path.lineTo(carSx + 12f * u, carSy + 11f * u);
path.lineTo(vx, cy + 4f * u); path.lineTo(carSx, carSy + 5f * u);
path.lineTo(vx - 11f * u, cy + 10f * u); path.lineTo(carSx - 12f * u, carSy + 11f * u);
path.close(); path.close();
fill.setColor(light); fill.setColor(light);
c.drawPath(path, fill); c.drawPath(path, fill);
glow(c, path, light, 1.5f * u); glow(c, path, light, 1.5f * u);
return true;
} }
/** One stretch of road from (a0, f0) to (a1, f1) in metres (side, ahead), cut at the near edge. */ /** Dark panels under the battery wall and the clock, so the map does not run through them. */
private void segment(int cls, float a0, float f0, float a1, float f1, float vx, float horizon, float lane, private void drawPanels(Canvas c, float w, float horizon, float u) {
float depth) { chamfer(16f * u, 78f * u, 360f * u, horizon + 86f * u, 14f * u);
float d0 = CAR_DEPTH + f0 / MAP_UNIT_M, d1 = CAR_DEPTH + f1 / MAP_UNIT_M; fill.setColor(0xC8000000);
Path p = roads[cls * 2 + (Math.min(d0, d1) > FADE_DEPTH ? 1 : 0)]; c.drawPath(path, fill);
if ((d0 < NEAR && d1 < NEAR) || (f0 > FAR_M && f1 > FAR_M)) { chamfer(w - 210f * u, 12f * u, w - 12f * u, 82f * u, 10f * u);
penDown = false; c.drawPath(path, fill);
return; // Behind the gears as well.
chamfer(w / 2f - 150f * u, 12f * u, w / 2f + 150f * u, 72f * u, 10f * u);
c.drawPath(path, fill);
} }
boolean cut = false;
if (d0 < NEAR || d1 < NEAR) { /** The speed over the map: smaller, on a panel under the gears. */
float t = (NEAR - d0) / (d1 - d0); private void drawMapSpeed(Canvas c, float w, float u) {
float a = a0 + (a1 - a0) * t, d = NEAR; chamfer(w / 2f - 130f * u, 80f * u, w / 2f + 130f * u, 232f * u, 14f * u);
if (d0 < NEAR) { fill.setColor(0xC8000000);
a0 = a; c.drawPath(path, fill);
d0 = d; line.setColor(dim);
cut = true; line.setStrokeWidth(1.5f * u);
} else { c.drawPath(path, line);
a1 = a; text.setTypeface(thin);
d1 = d; text.setTextSize(120f * u);
} text.setTextAlign(Paint.Align.CENTER);
} glowText(c, fmt(v.speedKmh, "%.0f", "—"), w / 2f, 194f * u, light, u);
float x0 = vx + a0 / MAP_UNIT_M * lane / d0, y0 = horizon + depth / d0; caption(c, "KM/H", w / 2f, 220f * u, u, Paint.Align.CENTER);
float x1 = vx + a1 / MAP_UNIT_M * lane / d1, y1 = horizon + depth / d1;
if (!penDown || cut || p != penPath || x0 != penX || y0 != penY) p.moveTo(x0, y0);
p.lineTo(x1, y1);
penPath = p;
penX = x1;
penY = y1;
penDown = true;
} }
/** Why the map is not there, on the floor. */ /** Why the map is not there, on the floor. */
@@ -296,14 +246,14 @@ final class GridView extends ClusterCanvasView {
caption(c, why, w / 2f, horizon + (h - horizon) * 0.2f, u, Paint.Align.CENTER); caption(c, why, w / 2f, horizon + (h - horizon) * 0.2f, u, Paint.Align.CENTER);
} }
/** OpenStreetMap's licence asks for its name wherever its data is shown. */ /** The tiles' and the data's licences ask for their names wherever the map is shown. */
private void drawAttribution(Canvas c, float w, float h, float u) { private void drawAttribution(Canvas c, float w, float h, float u) {
text.setTypeface(label); text.setTypeface(label);
text.setTextSize(12f * u); text.setTextSize(12f * u);
text.setTextAlign(Paint.Align.RIGHT); text.setTextAlign(Paint.Align.RIGHT);
text.setStyle(Paint.Style.FILL); text.setStyle(Paint.Style.FILL);
text.setColor(dim); text.setColor(dim);
c.drawText("© OpenStreetMap contributors", w - 26f * u, h - 80f * u, text); c.drawText("OpenFreeMap © OpenMapTiles · © OpenStreetMap contributors", w - 26f * u, h - 80f * u, text);
} }
// ---------- Top ---------- // ---------- Top ----------
@@ -393,8 +343,13 @@ final class GridView extends ClusterCanvasView {
// ---------- Power disc ---------- // ---------- Power disc ----------
/** Zero at the top, −50 kW a quarter turn anticlockwise, 150 kW three quarters clockwise. */ /** Zero at the top, −50 kW a quarter turn anticlockwise, 150 kW three quarters clockwise. */
private void drawDisc(Canvas c, float w, float u) { private void drawDisc(Canvas c, float w, boolean overMap, float u) {
float cx = w - 170f * u, cy = 238f * u, outer = 132f * u, ring = 22f * u; float cx = w - 170f * u, cy = 238f * u, outer = 132f * u, ring = 22f * u;
if (overMap) {
// The map runs under the disc: a dark backing keeps it readable.
fill.setColor(0xD0000000);
c.drawCircle(cx, cy, outer + 16f * u, fill);
}
line.setColor(dim); line.setColor(dim);
line.setStrokeWidth(1.5f * u); line.setStrokeWidth(1.5f * u);
c.drawCircle(cx, cy, outer + 10f * u, line); c.drawCircle(cx, cy, outer + 10f * u, line);
@@ -358,6 +358,8 @@ public class MainActivity extends AppCompatActivity {
new QuestView(this), visorView, new EmpireView(this)); new QuestView(this), visorView, new EmpireView(this));
View[] canvasPanes = new View[canvasStyles.size()]; View[] canvasPanes = new View[canvasStyles.size()];
for (int i = 0; i < canvasPanes.length; i++) canvasPanes[i] = drawnPane(canvasStyles.get(i)); for (int i = 0; i < canvasPanes.length; i++) canvasPanes[i] = drawnPane(canvasStyles.get(i));
// Grid with map: its map layer goes under it, in the same pane.
((android.widget.FrameLayout) canvasPanes[canvasStyles.indexOf(gridMapView)]).addView(gridMapView.mapLayer(), 0);
View[] fixedPanes = { vehiclePane, gaugesPane, cockpitPane, retroPane, View[] fixedPanes = { vehiclePane, gaugesPane, cockpitPane, retroPane,
futuristicPane, sciFiPane, cartoonPane, mgPane }; futuristicPane, sciFiPane, cartoonPane, mgPane };
// By OverviewStyle ordinal: the fixed styles, then the canvas styles in their order. // By OverviewStyle ordinal: the fixed styles, then the canvas styles in their order.
@@ -0,0 +1,223 @@
package com.evsuite.abrp;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.SystemClock;
import android.view.View;
/**
* The map under "Grid with map": the roads round the car and a ground grid, straight from
* above, heading up — drawn once into a hardware layer (a GPU texture) and then only moved and
* turned, by the view's own translation and rotation, once per display frame. Nothing is
* redrawn while driving until new tiles arrive or the car nears the layer's edge; then it is
* drawn again round the car. {@link GridView} draws everything else on top.
*
* The layer is a square about twice the distance from the car to the farthest screen corner,
* plus a margin, so it covers the screen at any angle; the car sits {@link #CAR_FROM_BOTTOM}
* layout units above the bottom edge.
*/
final class MapLayerView extends View {
/** Pixels (layout units) per metre, the car's height above the bottom edge, grid spacing. */
static final float MAP_SCALE = 1.2f, CAR_FROM_BOTTOM = 150f;
private static final float GRID_M = 50f;
/** How far the car may move from the layer's centre before it is drawn again, in layout units. */
private static final float MARGIN = 280f;
private final MapRoads map;
private final Paint line = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Path[] roads = new Path[RoadTile.CLASSES];
private final Path grid = new Path();
private int main = 0xFF18E6FF, light = 0xFFC8FCFF;
/** The layer's centre (world tile units) and what it was drawn from. */
private double anchorX = Double.NaN, anchorY;
private int drawnVersion = -1;
private float drawnScale;
/** The car as shown, eased towards where it is (world tile units), and the heading. */
private double shownX = Double.NaN, shownY;
private float heading = Float.NaN;
private boolean showing, ticking;
/** The car's speed, from the readings; NaN when unread. */
private float speedMps = Float.NaN;
MapLayerView(Context context, MapRoads map) {
super(context);
this.map = map;
for (int i = 0; i < roads.length; i++) roads[i] = new Path();
line.setStyle(Paint.Style.STROKE);
line.setStrokeJoin(Paint.Join.ROUND);
line.setStrokeCap(Paint.Cap.ROUND);
setLayerType(LAYER_TYPE_HARDWARE, null);
setVisibility(INVISIBLE);
setWillNotDraw(false);
}
void setColours(int main, int light) {
this.main = main;
this.light = light;
invalidate();
}
/** Whether the map is there to be seen (a fix and roads). */
boolean isShowing() {
return showing;
}
/** Layout units, as GridView uses them, for this size. */
private float unit() {
View parent = (View) getParent();
int w = parent == null ? getWidth() : parent.getWidth() - parent.getPaddingLeft() - parent.getPaddingRight();
int h = parent == null ? getHeight() : parent.getHeight() - parent.getPaddingTop() - parent.getPaddingBottom();
return Math.min(h / 640f, w / 1100f);
}
@Override
protected void onMeasure(int widthSpec, int heightSpec) {
int w = MeasureSpec.getSize(widthSpec), h = MeasureSpec.getSize(heightSpec);
float u = Math.min(h / 640f, w / 1100f);
int side = (int) (2 * (Math.hypot(w / 2f, h - CAR_FROM_BOTTOM * u) + MARGIN * u));
setMeasuredDimension(Math.max(side, 1), Math.max(side, 1));
}
/** The car's speed in km/h, or null when unread; GridView passes it with every reading. */
void setSpeed(Float kmh) {
speedMps = kmh == null ? Float.NaN : kmh / 3.6f;
}
/** Starts the frame loop if it is not running; GridView calls it with every reading. */
void kick() {
if (!ticking && isAttachedToWindow()) {
ticking = true;
postOnAnimation(this::tick);
}
}
/** One display frame: where the car is, which way, and the layer moved there. */
private void tick() {
View pane = (View) getParent();
if (pane == null || !pane.isShown()) {
ticking = false;
return;
}
MapRoads.Fix fix = map.fix();
boolean ready = fix != null && !map.tiles().isEmpty() && getWidth() > 0;
if (ready != showing) {
showing = ready;
setVisibility(ready ? VISIBLE : INVISIBLE);
}
if (!ready) {
ticking = false;
return;
}
float target = Float.isNaN(fix.bearing) ? (Float.isNaN(heading) ? 0f : heading) : fix.bearing;
if (Float.isNaN(heading)) heading = target;
else heading = (heading + (((target - heading) % 360f + 540f) % 360f - 180f) * 0.12f + 360f) % 360f;
// Between fixes the car runs on with the speed (at most 2.5 s), and what is shown glides
// there rather than jumping with each fix.
double mpu = MapGeometry.metresPerUnit(fix.lat);
float ran = Float.isNaN(fix.bearing) || Float.isNaN(speedMps) ? 0f
: speedMps * Math.min(2.5f, (SystemClock.elapsedRealtime() - fix.elapsedMs) / 1000f);
double run = Math.toRadians(Float.isNaN(fix.bearing) ? 0f : fix.bearing);
double carX = MapGeometry.worldX(fix.lon) + ran * Math.sin(run) / mpu;
double carY = MapGeometry.worldY(fix.lat) - ran * Math.cos(run) / mpu;
if (Double.isNaN(shownX) || Math.hypot(carX - shownX, carY - shownY) * mpu > 200) {
shownX = carX;
shownY = carY;
} else {
shownX += (carX - shownX) * 0.15;
shownY += (carY - shownY) * 0.15;
}
float u = unit(), px = MAP_SCALE * u;
if (drawnVersion != map.version() || Double.isNaN(anchorX) || drawnScale != px
|| Math.hypot(shownX - anchorX, shownY - anchorY) * mpu * px > MARGIN * u) {
build(shownX, shownY, mpu);
drawnScale = px;
invalidate(); // the only redraw of the layer
}
// The car's spot in the layer goes to its spot on screen, turned round it.
float half = getWidth() / 2f;
float inX = half + (float) ((shownX - anchorX) * mpu) * px;
float inY = half + (float) ((shownY - anchorY) * mpu) * px;
float carSx = pane.getPaddingLeft() + (pane.getWidth() - pane.getPaddingLeft() - pane.getPaddingRight()) / 2f;
float carSy = pane.getHeight() - pane.getPaddingBottom() - CAR_FROM_BOTTOM * u;
setPivotX(inX);
setPivotY(inY);
setRotation(-heading);
setTranslationX(carSx - getLeft() - inX);
setTranslationY(carSy - getTop() - inY);
postOnAnimation(this::tick);
}
/** The roads and the grid in metres east and south of an anchor at the car. */
private void build(double atX, double atY, double mpu) {
anchorX = atX;
anchorY = atY;
drawnVersion = map.version();
float m = (float) mpu;
for (Path p : roads) p.reset();
float u = unit();
double reach = getWidth() / 2f / (MAP_SCALE * u);
double range = reach / mpu;
for (RoadTile tile : map.tiles()) {
double ox = (double) tile.x * MapGeometry.EXTENT - atX, oy = (double) tile.y * MapGeometry.EXTENT - atY;
if (ox > range || oy > range || ox + MapGeometry.EXTENT < -range || oy + MapGeometry.EXTENT < -range) {
continue;
}
float fx = (float) ox, fy = (float) oy;
for (int i = 0; i < tile.points.length; i++) {
int[] b = tile.bounds[i];
if (fx + b[2] < -range || fx + b[0] > range || fy + b[3] < -range || fy + b[1] > range) continue;
Path p = roads[tile.classes[i]];
int[] pts = tile.points[i];
p.moveTo((fx + pts[0]) * m, (fy + pts[1]) * m);
for (int k = 2; k < pts.length; k += 2) p.lineTo((fx + pts[k]) * m, (fy + pts[k + 1]) * m);
}
}
// Grid lines on whole multiples of GRID_M east and north, so they stay put on the ground.
grid.reset();
float r = (float) reach;
float offE = (float) ((atX * mpu) % GRID_M), offS = (float) ((atY * mpu) % GRID_M);
int lines = (int) (r / GRID_M) + 1;
for (int k = -lines; k <= lines; k++) {
float x = k * GRID_M - offE, y = k * GRID_M - offS;
grid.moveTo(x, -r);
grid.lineTo(x, r);
grid.moveTo(-r, y);
grid.lineTo(r, y);
}
}
@Override
protected void onDraw(Canvas c) {
if (Double.isNaN(anchorX) || drawnScale <= 0f) return;
float u = unit(), px = drawnScale;
c.save();
c.translate(getWidth() / 2f, getHeight() / 2f);
c.scale(px, px);
stroke(c, grid, AccentColor.withAlpha(main, 0x26), 1f * u / px);
stroke(c, roads[3], AccentColor.withAlpha(main, 0xA0), 1.6f * u / px);
stroke(c, roads[2], AccentColor.withAlpha(main, 0xD8), 2.2f * u / px);
glow(c, roads[1], main, 2.6f * u / px);
glow(c, roads[0], light, 3.4f * u / px);
c.restore();
}
private void stroke(Canvas c, Path p, int colour, float width) {
line.setColor(colour);
line.setStrokeWidth(width);
c.drawPath(p, line);
}
/** As GridView's glow: two wide faint strokes under a sharp one. */
private void glow(Canvas c, Path p, int colour, float width) {
stroke(c, p, AccentColor.withAlpha(colour, 0x18), width * 6f);
stroke(c, p, AccentColor.withAlpha(colour, 0x40), width * 3f);
stroke(c, p, colour, width);
}
}
@@ -49,7 +49,8 @@ final class MapRoads {
private static final String TAG = "MapRoads"; private static final String TAG = "MapRoads";
private static final int DISK_TILES = 400; private static final int DISK_TILES = 400;
private static final long DISK_AGE_MS = 30L * 24 * 60 * 60 * 1000; private static final long DISK_AGE_MS = 30L * 24 * 60 * 60 * 1000;
private static final long RETRY_MS = 60_000L; /** A tile the server did not have yet (it goes on fetching) is asked for again after this. */
private static final long RETRY_MS = 15_000L;
/** A fix older than this is not drawn from. */ /** A fix older than this is not drawn from. */
static final long FIX_FRESH_MS = 15_000L; static final long FIX_FRESH_MS = 15_000L;
@@ -64,6 +65,8 @@ final class MapRoads {
return t; return t;
}); });
private volatile Fix fix; private volatile Fix fix;
/** Goes up whenever a tile arrives or is dropped, so a view knows to rebuild its shapes. */
private volatile int version;
/** The last bearing while moving; GPS has none at a standstill. */ /** The last bearing while moving; GPS has none at a standstill. */
private volatile float bearing = Float.NaN; private volatile float bearing = Float.NaN;
private volatile boolean serverMissing; private volatile boolean serverMissing;
@@ -99,6 +102,10 @@ final class MapRoads {
return f == null || SystemClock.elapsedRealtime() - f.elapsedMs > FIX_FRESH_MS ? null : f; return f == null || SystemClock.elapsedRealtime() - f.elapsedMs > FIX_FRESH_MS ? null : f;
} }
int version() {
return version;
}
Collection<RoadTile> tiles() { Collection<RoadTile> tiles() {
return tiles.values(); return tiles.values();
} }
@@ -115,7 +122,10 @@ final class MapRoads {
/** Queues the 3×3 tiles round (cx, cy), the car's own first, and forgets far ones. */ /** Queues the 3×3 tiles round (cx, cy), the car's own first, and forgets far ones. */
private void want(int cx, int cy) { private void want(int cx, int cy) {
for (Long key : tiles.keySet()) { for (Long key : tiles.keySet()) {
if (Math.abs(MapGeometry.keyX(key) - cx) > 2 || Math.abs(MapGeometry.keyY(key) - cy) > 2) tiles.remove(key); if (Math.abs(MapGeometry.keyX(key) - cx) > 2 || Math.abs(MapGeometry.keyY(key) - cy) > 2) {
tiles.remove(key);
version++;
}
} }
int[][] order = { {0, 0}, {0, -1}, {1, 0}, {0, 1}, {-1, 0}, {1, -1}, {1, 1}, {-1, 1}, {-1, -1} }; int[][] order = { {0, 0}, {0, -1}, {1, 0}, {0, 1}, {-1, 0}, {1, -1}, {1, 1}, {-1, 1}, {-1, -1} };
long now = SystemClock.elapsedRealtime(); long now = SystemClock.elapsedRealtime();
@@ -151,6 +161,7 @@ final class MapRoads {
} }
if (cached != null) { if (cached != null) {
tiles.put(key, cached); tiles.put(key, cached);
version++;
if (System.currentTimeMillis() - file.lastModified() < DISK_AGE_MS) return; if (System.currentTimeMillis() - file.lastModified() < DISK_AGE_MS) return;
} }
ServerConfig config = ServerConfig.get(context); ServerConfig config = ServerConfig.get(context);
@@ -171,6 +182,7 @@ final class MapRoads {
} }
failedAt.remove(key); failedAt.remove(key);
tiles.put(key, tile); tiles.put(key, tile);
version++;
save(file, r.body); save(file, r.body);
} catch (Exception e) { } catch (Exception e) {
Log.w(TAG, "Map tile not loaded: " + e.getClass().getSimpleName()); Log.w(TAG, "Map tile not loaded: " + e.getClass().getSimpleName());
@@ -7,6 +7,8 @@ import android.content.IntentFilter;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import com.evsuite.hardware.AssistBlock;
import com.evsuite.hardware.EVHardware;
import com.evsuite.hardware.probe.VendorSurfaceProbe; import com.evsuite.hardware.probe.VendorSurfaceProbe;
import java.io.File; import java.io.File;
@@ -26,7 +28,11 @@ import java.util.Map;
* The signal recorder on the Logs tab: for {@link #DURATION_MS} it reads every value the * The signal recorder on the Logs tab: for {@link #DURATION_MS} it reads every value the
* head unit offers (`VendorSurfaceProbe.capture`: every CarPropertyManager property and every * head unit offers (`VendorSurfaceProbe.capture`: every CarPropertyManager property and every
* value getter of the vendor objects, read only) again and again, and every steering-wheel key * value getter of the vendor objects, read only) again and again, and every steering-wheel key
* the head unit reports, into a {@link SignalRecording}. The driver notes the times they do * the head unit reports, into a {@link SignalRecording}. It also listens, as the head unit's own
* driver-assistance manager does, to the properties of the assistance block
* ({@link AssistBlock}): subscribed for change events the whole time, because a value that is
* only filled while someone listens reads as its default when polled (2.40.3, still the
* cruise control). The driver notes the times they do
* what is to be found; the values that change then are the leads. (Built to find the adaptive * what is to be found; the values that change then are the leads. (Built to find the adaptive
* cruise control, 2.32.0: on SWI68 the head unit gets none of it, see SPECS.md.) * cruise control, 2.32.0: on SWI68 the head unit gets none of it, see SPECS.md.)
* *
@@ -85,6 +91,21 @@ final class SignalRecorder {
} catch (RuntimeException ignored) { } catch (RuntimeException ignored) {
// No key reports: the properties are recorded all the same. // No key reports: the properties are recorded all the same.
} }
EVHardware.PropertyListening listening = null;
try {
listening = EVHardware.INSTANCE.listenToProperties(AssistBlock.INSTANCE::contains,
(id, area, value) -> {
rec.event(System.currentTimeMillis(),
String.format(Locale.US, "CPM 0x%08X/%d", id, area), value);
return kotlin.Unit.INSTANCE;
});
} catch (Throwable t) {
rec.note(System.currentTimeMillis(), "listening failed: " + t.getClass().getSimpleName());
}
rec.note(System.currentTimeMillis(), listening == null
? "listening to the assistance properties: not possible (no car service)"
: "listening to " + listening.getSubscribed() + " assistance properties ("
+ listening.getRefused() + " refused)");
try { try {
long end = startedMs + DURATION_MS; long end = startedMs + DURATION_MS;
while (running && System.currentTimeMillis() < end) { while (running && System.currentTimeMillis() < end) {
@@ -99,6 +120,13 @@ final class SignalRecorder {
} catch (Throwable t) { } catch (Throwable t) {
rec.note(System.currentTimeMillis(), "recorder stopped: " + t.getClass().getSimpleName()); rec.note(System.currentTimeMillis(), "recorder stopped: " + t.getClass().getSimpleName());
} finally { } finally {
if (listening != null) {
try {
listening.close();
} catch (Throwable ignored) {
// the car service is gone
}
}
if (keysRegistered) { if (keysRegistered) {
try { try {
app.unregisterReceiver(keys); app.unregisterReceiver(keys);
@@ -130,7 +158,10 @@ final class SignalRecorder {
text.add("Steering-wheel keys and notes:"); text.add("Steering-wheel keys and notes:");
text.addAll(rec.notes()); text.addAll(rec.notes());
text.add(""); text.add("");
text.add("Changed values, the rarest first:"); text.add("Assistance properties heard (" + rec.events() + " change events), changing ones first:");
text.addAll(rec.heard());
text.add("");
text.add("Changed values (polled), the rarest first:");
text.addAll(rec.summary()); text.addAll(rec.summary());
text.add(""); text.add("");
text.add("Everything, in order:"); text.add("Everything, in order:");
@@ -168,14 +199,17 @@ final class SignalRecorder {
long elapsed = (running ? System.currentTimeMillis() : startedMs + DURATION_MS) - startedMs; long elapsed = (running ? System.currentTimeMillis() : startedMs + DURATION_MS) - startedMs;
if (running) { if (running) {
out.add(context.getString(R.string.signal_recorder_running, elapsed / 60_000, (elapsed / 1000) % 60, out.add(context.getString(R.string.signal_recorder_running, elapsed / 60_000, (elapsed / 1000) % 60,
recording.captures())); recording.captures(), recording.events()));
} else { } else {
out.add(context.getString(R.string.signal_recorder_done, SignalRecording.time(startedMs), out.add(context.getString(R.string.signal_recorder_done, SignalRecording.time(startedMs),
recording.captures(), result == null ? "…" : result)); recording.captures(), recording.events(), result == null ? "…" : result));
} }
// Steering-wheel keys and the like first: few, and each one a lead. // Steering-wheel keys and the like first: few, and each one a lead.
List<String> notes = recording.notes(); List<String> notes = recording.notes();
out.addAll(notes.subList(Math.max(0, notes.size() - 10), notes.size())); out.addAll(notes.subList(Math.max(0, notes.size() - 10), notes.size()));
// Then what the listening heard, changing properties first.
List<String> heard = recording.heard();
for (String line : heard.subList(0, Math.min(12, heard.size()))) out.add("heard " + line);
out.addAll(recording.summary()); out.addAll(recording.summary());
return out; return out;
} }
@@ -16,6 +16,10 @@ import java.util.TreeSet;
* feature on and off, changes a setting). It keeps what changed, when, and per value how * feature on and off, changes a setting). It keeps what changed, when, and per value how
* often and through which values, so the value that follows the action stands out: it * often and through which values, so the value that follows the action stands out: it
* changes a few times, at the moments the driver noted. * changes a few times, at the moments the driver noted.
*
* Besides the captures (polled), it keeps the change events of the properties the recorder
* listens to ({@link #event}): how many per property and through which values. A value the
* head unit only fills while someone listens shows up there and nowhere else.
* Pure Java, unit-tested. * Pure Java, unit-tested.
*/ */
final class SignalRecording { final class SignalRecording {
@@ -31,6 +35,10 @@ final class SignalRecording {
private final List<String> notes = new ArrayList<>(); private final List<String> notes = new ArrayList<>();
private final Map<String, Integer> changes = new HashMap<>(); private final Map<String, Integer> changes = new HashMap<>();
private final Map<String, LinkedHashSet<String>> seen = new HashMap<>(); private final Map<String, LinkedHashSet<String>> seen = new HashMap<>();
private int events;
private final Map<String, String> lastEvent = new HashMap<>();
private final Map<String, Integer> eventCounts = new HashMap<>();
private final Map<String, LinkedHashSet<String>> eventValues = new HashMap<>();
static String time(long ms) { static String time(long ms) {
return new SimpleDateFormat("HH:mm:ss", Locale.US).format(new Date(ms)); return new SimpleDateFormat("HH:mm:ss", Locale.US).format(new Date(ms));
@@ -65,6 +73,45 @@ final class SignalRecording {
notes.add(time(ms) + " " + what); notes.add(time(ms) + " " + what);
} }
/**
* A change event from a property listened to. Every one is counted; the timeline gets a
* line only when the value differs from that property's last event.
*/
synchronized void event(long ms, String key, String value) {
events++;
eventCounts.merge(key, 1, Integer::sum);
String before = lastEvent.put(key, value);
if (value.equals(before)) return;
lines.add(time(ms) + " heard " + key + " = " + value);
LinkedHashSet<String> values = eventValues.computeIfAbsent(key, k -> new LinkedHashSet<>());
if (values.size() < VALUES_KEPT) values.add(shorten(value));
}
synchronized int events() {
return events;
}
/**
* Every property that sent events: first those that sent different values, the fewest
* events first (the leads), then those that only ever repeated one value.
*/
synchronized List<String> heard() {
List<String> keys = new ArrayList<>(eventCounts.keySet());
keys.sort((a, b) -> {
boolean va = eventValues.get(a).size() > 1, vb = eventValues.get(b).size() > 1;
if (va != vb) return va ? -1 : 1;
int c = Integer.compare(eventCounts.get(a), eventCounts.get(b));
return c != 0 ? c : a.compareTo(b);
});
List<String> out = new ArrayList<>();
for (String key : keys) {
LinkedHashSet<String> values = eventValues.get(key);
out.add(key + " " + eventCounts.get(key) + " events " + String.join(" → ", values)
+ (values.size() >= VALUES_KEPT ? " …" : ""));
}
return out;
}
/** The notes alone, in order. */ /** The notes alone, in order. */
synchronized List<String> notes() { synchronized List<String> notes() {
return new ArrayList<>(notes); return new ArrayList<>(notes);
+3 -3
View File
@@ -223,11 +223,11 @@
<string name="drive_mode_unreadable">Not changed: speed or gear unreadable.</string> <string name="drive_mode_unreadable">Not changed: speed or gear unreadable.</string>
<string name="drive_mode_unknown">an unknown mode (%1$s)</string> <string name="drive_mode_unknown">an unknown mode (%1$s)</string>
<string name="label_signal_recorder">Signal recorder</string> <string name="label_signal_recorder">Signal recorder</string>
<string name="signal_recorder_hint">Records every value the head unit offers for 3 minutes, plus the steering-wheel keys it reports, and lists what changed, the rarest first. To find a signal: start it, note the times you switch the feature on and off or change it, and look for the values that changed then. With a USB stick plugged in, the whole recording is saved on it too (Android/data/…/files).</string> <string name="signal_recorder_hint">Records every value the head unit offers for 3 minutes, listens to the driver-assistance properties for every change they report, and notes the steering-wheel keys; then lists what changed, the rarest first. To find a signal: start it, note the times you switch the feature on and off or change it, and look for the values that changed then. With a USB stick plugged in, the whole recording is saved on it too (Android/data/…/files).</string>
<string name="btn_signal_record">Record 3 minutes</string> <string name="btn_signal_record">Record 3 minutes</string>
<string name="btn_signal_stop">Stop recording</string> <string name="btn_signal_stop">Stop recording</string>
<string name="signal_recorder_running">Recording: %1$d:%2$02d of 3:00, %3$d captures. Keys reported, then values changed so far, the rarest first:</string> <string name="signal_recorder_running">Recording: %1$d:%2$02d of 3:00, %3$d captures, %4$d change events heard. Keys reported, then assistance properties heard, then values changed so far, the rarest first:</string>
<string name="signal_recorder_done">Recording from %1$s: %2$d captures, %3$s. Keys reported, then values changed, the rarest first:</string> <string name="signal_recorder_done">Recording from %1$s: %2$d captures, %3$d change events heard, %4$s. Keys reported, then assistance properties heard, then values changed, the rarest first:</string>
<string name="call_log_empty">No upload attempted yet.</string> <string name="call_log_empty">No upload attempted yet.</string>
<string name="update_log_title">Updates, newest first:</string> <string name="update_log_title">Updates, newest first:</string>
<string name="btn_flush_now">Upload now</string> <string name="btn_flush_now">Upload now</string>
@@ -63,4 +63,22 @@ public class SignalRecordingTest {
assertTrue(r.lines().get(4).endsWith("key 287 down")); assertTrue(r.lines().get(4).endsWith("key 287 down"));
assertEquals(1, r.notes().size()); assertEquals(1, r.notes().size());
} }
@Test
public void heardPropertiesWithChangingValuesComeFirst() {
SignalRecording r = new SignalRecording();
for (int i = 0; i < 5; i++) r.event(i * 1000, "CPM 0x2140A10A/0", "1"); // repeats one value
r.event(1000, "CPM 0x2160A15B/0", "0.0");
r.event(2000, "CPM 0x2160A15B/0", "101.0");
r.event(3000, "CPM 0x2160A15B/0", "101.0");
r.event(4000, "CPM 0x2160A15B/0", "90.0");
List<String> heard = r.heard();
assertEquals(2, heard.size());
assertEquals("CPM 0x2160A15B/0 4 events 0.0 → 101.0 → 90.0", heard.get(0));
assertEquals("CPM 0x2140A10A/0 5 events 1", heard.get(1));
assertEquals(9, r.events());
// The timeline holds only the changes: 0.0, 101.0, 90.0 and the first 1.
long lines = r.lines().stream().filter(l -> l.contains("heard ")).count();
assertEquals(4, lines);
}
} }
@@ -16,10 +16,17 @@ import android.widget.Toast;
* STATUS_PENDING_USER_ACTION is the case worth understanding: the platform is saying it will * STATUS_PENDING_USER_ACTION is the case worth understanding: the platform is saying it will
* install, but wants the driver to confirm. That happens when the app is not granted a silent * install, but wants the driver to confirm. That happens when the app is not granted a silent
* install, and the only correct response is to launch the confirmation activity it hands us. * install, and the only correct response is to launch the confirmation activity it hands us.
*
* Any other failure — on the car the session is refused, "Not allowed to install non-system
* apps on internal storage", and that answer can come here rather than as an exception — opens
* Android's installer screen with the verified APK, as {@link OtaUpdater#install} does for an
* immediate refusal. Only a cancel by the driver ends there.
*/ */
public final class OtaInstallResultReceiver extends BroadcastReceiver { public final class OtaInstallResultReceiver extends BroadcastReceiver {
static final String ACTION_INSTALL_RESULT = "com.stinnesbeck.nilsmg4.OTA_INSTALL_RESULT"; static final String ACTION_INSTALL_RESULT = "com.stinnesbeck.nilsmg4.OTA_INSTALL_RESULT";
static final String EXTRA_APK = "com.stinnesbeck.nilsmg4.extra.APK";
static final String EXTRA_SESSION = "com.stinnesbeck.nilsmg4.extra.SESSION";
private static final String TAG = "EVABRP.Update"; private static final String TAG = "EVABRP.Update";
@@ -31,12 +38,16 @@ public final class OtaInstallResultReceiver extends BroadcastReceiver {
Log.i(TAG, "OTA install result: status=" + status + " msg=" + message); Log.i(TAG, "OTA install result: status=" + status + " msg=" + message);
UpdateLog.record(context, "Install result: status " + status UpdateLog.record(context, "Install result: status " + status
+ (message != null ? ", " + message : "")); + (message != null ? ", " + message : ""));
int session = intent.getIntExtra(EXTRA_SESSION, -1);
if (session >= 0) OtaUpdater.noteAnswer(session);
String apkPath = intent.getStringExtra(EXTRA_APK);
switch (status) { switch (status) {
case PackageInstaller.STATUS_PENDING_USER_ACTION: case PackageInstaller.STATUS_PENDING_USER_ACTION:
Intent confirm = intent.getParcelableExtra(Intent.EXTRA_INTENT); Intent confirm = intent.getParcelableExtra(Intent.EXTRA_INTENT);
if (confirm == null) { if (confirm == null) {
toast(context, context.getString(R.string.ota_install_failed, "no confirm intent")); UpdateLog.record(context, "No confirmation screen offered");
openInstallerScreen(context, apkPath, "no confirm intent");
return; return;
} }
confirm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); confirm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
@@ -44,23 +55,52 @@ public final class OtaInstallResultReceiver extends BroadcastReceiver {
context.startActivity(confirm); context.startActivity(confirm);
} catch (Throwable t) { } catch (Throwable t) {
Log.e(TAG, "OTA confirm UI failed: " + t.getMessage()); Log.e(TAG, "OTA confirm UI failed: " + t.getMessage());
toast(context, context.getString(R.string.ota_install_failed, UpdateLog.record(context, "Confirmation screen failed: " + t.getClass().getSimpleName());
t.getClass().getSimpleName())); openInstallerScreen(context, apkPath, t.getClass().getSimpleName());
} }
break; break;
case PackageInstaller.STATUS_SUCCESS: case PackageInstaller.STATUS_SUCCESS:
toast(context, context.getString(R.string.ota_install_success)); toast(context, context.getString(R.string.ota_install_success));
break; break;
case PackageInstaller.STATUS_FAILURE_ABORTED:
// The driver cancelled: nothing opens again.
toast(context, context.getString(R.string.ota_install_failed,
message != null ? message : "cancelled"));
break;
default: default:
// The status message is the package manager's own words — INSTALL_FAILED_* // The status message is the package manager's own words — INSTALL_FAILED_*
// and friends. Surfaced verbatim, because guessing at it is what made the // and friends — kept verbatim in the update log, because guessing at it is what
// previous failure unreadable. // made an earlier failure unreadable. Then the installer screen, which the car
toast(context, context.getString(R.string.ota_install_failed, // allows where it refuses the session.
message != null ? message : "status " + status)); openInstallerScreen(context, apkPath, message != null ? message : "status " + status);
break; break;
} }
} }
/**
* Opens Android's installer screen for the APK at [apkPath] off the main thread (it is
* copied to Downloads first); a toast with [why] when there is none or it cannot open.
*/
private void openInstallerScreen(Context context, String apkPath, String why) {
java.io.File apk = apkPath == null ? null : new java.io.File(apkPath);
if (apk == null || !apk.isFile()) {
toast(context, context.getString(R.string.ota_install_failed, why));
return;
}
PendingResult pending = goAsync();
Context app = context.getApplicationContext();
new Thread(() -> {
try {
if (!OtaUpdater.openInstallerScreen(app, apk)) {
new android.os.Handler(android.os.Looper.getMainLooper()).post(() ->
toast(app, app.getString(R.string.ota_install_failed, why)));
}
} finally {
pending.finish();
}
}, "ota-fallback").start();
}
private static void toast(Context context, String text) { private static void toast(Context context, String text) {
Toast.makeText(context.getApplicationContext(), text, Toast.LENGTH_LONG).show(); Toast.makeText(context.getApplicationContext(), text, Toast.LENGTH_LONG).show();
} }
@@ -464,21 +464,57 @@ final class OtaUpdater {
static InstallResult install(Context context, File apk) { static InstallResult install(Context context, File apk) {
if (!archiveIsThisPackage(context, apk)) return InstallResult.UNREADABLE_ARCHIVE; if (!archiveIsThisPackage(context, apk)) return InstallResult.UNREADABLE_ARCHIVE;
try { try {
commitSession(context, apk); int sessionId = commitSession(context, apk);
watchForAnswer(context, sessionId, apk);
return InstallResult.SESSION_STARTED; return InstallResult.SESSION_STARTED;
} catch (Throwable t) { } catch (Throwable t) {
Log.w(TAG, "OTA session install failed; trying system install UI", t); Log.w(TAG, "OTA session install failed; trying system install UI", t);
UpdateLog.record(context, "Installer session failed: " + t); UpdateLog.record(context, "Installer session failed: " + t);
return openInstallerScreen(context, apk) ? InstallResult.INSTALL_UI_STARTED : InstallResult.SESSION_FAILED;
}
}
/** The installer screen for [apk], as the fallback; false if even that could not open. */
static boolean openInstallerScreen(Context context, File apk) {
try { try {
launchInstallerActivity(context, apk); launchInstallerActivity(context, apk);
UpdateLog.record(context, "Opened the system installer instead"); UpdateLog.record(context, "Opened the system installer instead");
return InstallResult.INSTALL_UI_STARTED; return true;
} catch (Throwable fallback) { } catch (Throwable fallback) {
Log.w(TAG, "OTA install UI failed", fallback); Log.w(TAG, "OTA install UI failed", fallback);
UpdateLog.record(context, "System installer failed too: " + fallback); UpdateLog.record(context, "System installer failed too: " + fallback);
return InstallResult.SESSION_FAILED; return false;
} }
} }
/** How long a committed session may stay silent before the installer screen is opened. */
private static final long ANSWER_WAIT_MS = 20_000L;
/** Sessions the platform has answered (any status), so the watch knows to stand down. */
private static final java.util.Set<Integer> answered =
java.util.Collections.synchronizedSet(new java.util.HashSet<>());
/** The platform said something about [sessionId]; called by {@link OtaInstallResultReceiver}. */
static void noteAnswer(int sessionId) {
answered.add(sessionId);
}
/**
* A committed session normally answers within a second or two: installed (the app
* restarts), refused, or "confirm on screen". If it says nothing at all, the update would
* end silently after its download, so the installer screen is opened instead.
*/
private static void watchForAnswer(Context context, int sessionId, File apk) {
Context app = context.getApplicationContext();
new android.os.Handler(android.os.Looper.getMainLooper()).postDelayed(() -> {
if (answered.contains(sessionId)) return;
UpdateLog.record(app, "No answer from the installer after " + ANSWER_WAIT_MS / 1000 + " s");
try {
app.getPackageManager().getPackageInstaller().abandonSession(sessionId);
} catch (Throwable ignored) {
// already gone
}
new Thread(() -> openInstallerScreen(app, apk), "ota-fallback").start();
}, ANSWER_WAIT_MS);
} }
/** /**
@@ -510,7 +546,8 @@ final class OtaUpdater {
} }
} }
private static void commitSession(Context context, File apk) throws Exception { /** Commits [apk] to a new installer session and returns its id. */
private static int commitSession(Context context, File apk) throws Exception {
PackageInstaller installer = context.getPackageManager().getPackageInstaller(); PackageInstaller installer = context.getPackageManager().getPackageInstaller();
PackageInstaller.SessionParams params = new PackageInstaller.SessionParams( PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
PackageInstaller.SessionParams.MODE_FULL_INSTALL); PackageInstaller.SessionParams.MODE_FULL_INSTALL);
@@ -533,7 +570,10 @@ final class OtaUpdater {
} }
Intent callback = new Intent(context, OtaInstallResultReceiver.class) Intent callback = new Intent(context, OtaInstallResultReceiver.class)
.setAction(OtaInstallResultReceiver.ACTION_INSTALL_RESULT) .setAction(OtaInstallResultReceiver.ACTION_INSTALL_RESULT)
.setPackage(context.getPackageName()); .setPackage(context.getPackageName())
// Where the verified APK is, for the installer screen if the session fails.
.putExtra(OtaInstallResultReceiver.EXTRA_APK, apk.getAbsolutePath())
.putExtra(OtaInstallResultReceiver.EXTRA_SESSION, sessionId);
// MUTABLE: the platform fills in EXTRA_STATUS and, when it wants the driver to // MUTABLE: the platform fills in EXTRA_STATUS and, when it wants the driver to
// confirm, EXTRA_INTENT. An immutable PendingIntent would arrive empty. // confirm, EXTRA_INTENT. An immutable PendingIntent would arrive empty.
PendingIntent pending = PendingIntent.getBroadcast(context, sessionId, callback, PendingIntent pending = PendingIntent.getBroadcast(context, sessionId, callback,
@@ -543,6 +583,7 @@ final class OtaUpdater {
session.commit(pending.getIntentSender()); session.commit(pending.getIntentSender());
committed = true; committed = true;
Log.i(TAG, "OTA session " + sessionId + " committed (" + apk.length() + " bytes)"); Log.i(TAG, "OTA session " + sessionId + " committed (" + apk.length() + " bytes)");
return sessionId;
} finally { } finally {
// Abandon failed staging, but always close our local handle after a commit too. // Abandon failed staging, but always close our local handle after a commit too.
if (!committed) { if (!committed) {
+32 -5
View File
@@ -1,5 +1,21 @@
# The car server (Go). One replica on purpose: pending pairing requests and the wrong-password # The car server (Go). One replica on purpose: pending pairing requests and the wrong-password
# counters are kept in memory. # counters are kept in memory.
# The roads of the app's Grid map, fetched on demand, are kept on this small volume.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mg4-server-maps
namespace: mg4
labels:
app.kubernetes.io/name: mg4-server
app.kubernetes.io/part-of: mg4
spec:
accessModes: ["ReadWriteOnce"]
# No storageClassName: the cluster's default class is used.
resources:
requests:
storage: 1Gi
---
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
@@ -25,12 +41,13 @@ spec:
runAsNonRoot: true runAsNonRoot: true
runAsUser: 65532 runAsUser: 65532
runAsGroup: 65532 runAsGroup: 65532
fsGroup: 65532
seccompProfile: seccompProfile:
type: RuntimeDefault type: RuntimeDefault
containers: containers:
- name: server - name: server
# 2.6.2, pinned by digest so a re-pushed tag cannot change what runs. # Pinned by digest so a re-pushed tag cannot change what runs (version in SPECS.md).
image: git.stinnesbeck.com/nils/mg4-server@sha256:e5da9452a9dbdfabe3340766cc51f18bb1418b2588ab534714550470abe1ce29 image: git.stinnesbeck.com/nils/mg4-server@sha256:8b8d94e0562833d196162866b7fdc6910d5dd219f6eb84572d4d5c91b74f7ff1
ports: ports:
- name: http - name: http
containerPort: 8080 containerPort: 8080
@@ -50,9 +67,12 @@ spec:
value: https://git.stinnesbeck.com value: https://git.stinnesbeck.com
- name: FORGE_REPO - name: FORGE_REPO
value: nils/mg4 value: nils/mg4
# The roads for the app's Grid map, from OpenStreetMap; "off" switches maps off. # The roads for the app's Grid map: OpenStreetMap vector tiles, fetched only for the
- name: OVERPASS_URL # tiles a car asks for and kept on the volume below; "off" switches maps off.
value: https://overpass-api.de/api/interpreter - name: MAP_TILES_URL
value: https://tiles.openfreemap.org/planet
- name: MAP_CACHE_DIR
value: /var/cache/mg4-maps
- name: INFLUX_TOKEN - name: INFLUX_TOKEN
valueFrom: valueFrom:
secretKeyRef: secretKeyRef:
@@ -84,6 +104,13 @@ spec:
readOnlyRootFilesystem: true readOnlyRootFilesystem: true
capabilities: capabilities:
drop: ["ALL"] drop: ["ALL"]
volumeMounts:
- name: maps
mountPath: /var/cache/mg4-maps
volumes:
- name: maps
persistentVolumeClaim:
claimName: mg4-server-maps
--- ---
apiVersion: v1 apiVersion: v1
kind: Service kind: Service
Binary file not shown.

Before

Width:  |  Height:  |  Size: 70 KiB

After

Width:  |  Height:  |  Size: 105 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 66 KiB

After

Width:  |  Height:  |  Size: 105 KiB

+18 -12
View File
@@ -23,7 +23,8 @@ Environment only; the server refuses to start when a required value is missing.
| `APPROVAL_PASSWORD` | optional, at least 12 characters; switches pairing on (see below) | | `APPROVAL_PASSWORD` | optional, at least 12 characters; switches pairing on (see below) |
| `PUBLIC_URL` | the address a phone uses, for the pairing QR code; derived from the request if empty | | `PUBLIC_URL` | the address a phone uses, for the pairing QR code; derived from the request if empty |
| `FORGE_URL`, `FORGE_REPO`, `FORGE_TOKEN` | optional; app updates (below). The token only needs to read the repository | | `FORGE_URL`, `FORGE_REPO`, `FORGE_TOKEN` | optional; app updates (below). The token only needs to read the repository |
| `OVERPASS_URL` | the Overpass API the map's roads come from; default `https://overpass-api.de/api/interpreter`, `off` switches maps off, otherwise https only | | `MAP_TILES_URL` | a TileJSON naming the vector tiles the map's roads come from; default OpenFreeMap's `https://tiles.openfreemap.org/planet`, `off` switches maps off, otherwise https only |
| `MAP_CACHE_DIR` | where fetched map tiles are kept across restarts (the manifest mounts a volume there); empty keeps them in memory only |
| `LISTEN_ADDR` | default `:8080` | | `LISTEN_ADDR` | default `:8080` |
## Endpoints ## Endpoints
@@ -60,18 +61,23 @@ download cut short`. The forge's address and token never appear in the log.
## Map roads ## Map roads
`POST /v1/map/roads` `{z, x, y}`, with the car's token: the drivable roads (motorway to `POST /v1/map/roads` `{z, x, y}`, with the car's token: the drivable roads (motorway to minor
residential) of one web-mercator tile at zoom 14, for the app's "Grid with map" style. The car streets) of one web-mercator tile at zoom 14, for the app's "Grid with map" style. The car names
names the tile, not its position, and in the body, so it stays out of access logs. The answer is the tile, not its position, and in the body, so it stays out of access logs. The answer is
`{z, x, y, extent: 4096, ways: [{c, p: [x, y, …]}]}`: per road its class (0 motorway and trunk, `{z, x, y, extent: 4096, ways: [{c, p: [x, y, …]}]}`: per road its class (0 motorway and trunk,
1 primary and secondary, 2 tertiary and unclassified, 3 residential and living streets) and its 1 primary and secondary, 2 tertiary, 3 minor streets) and its points in tile units, simplified to
points in tile units, simplified to about 1.5 m; gzipped when the car accepts it (20–30 KB a tile about 1.5 m; gzipped when the car accepts it (10–15 KB a tile in a city before that).
in a city before that). The roads come from OpenStreetMap through `OVERPASS_URL`, one request at
a time and at most one a second, with a User-Agent naming this service, and are kept in memory The roads are fetched **only for the tiles a car asks for**: the OpenStreetMap vector tile
for 30 days (up to 3000 tiles); the car keeps its own copy too. 400 for anything but zoom 14 or (OpenMapTiles schema) of that square from the TileJSON at `MAP_TILES_URL` (by default
a tile outside it, 503 when Overpass is busy or unreachable (not cached, the car tries again a OpenFreeMap: free, no key, no request limits; its template moves with each weekly build and is
minute later), 404 when maps are off. The log says `map tile fetched` with the car's installation looked up every 6 hours), decoded by a small reader of our own (`mvt.go`, the `transportation`
and the size, never the tile. Map data © OpenStreetMap contributors, ODbL. layer only). Each tile is fetched once and kept for 30 days in `MAP_CACHE_DIR` (and the last 300
in memory); at most two fetches run at once. A request waits up to 6 s; a slower fetch goes on
and is kept, and the car hears 503 with `Retry-After: 10` and gets the tile on its next try.
400 for anything but zoom 14 or a tile outside it, 503 when the tile host fails (not cached),
404 when maps are off. The log says `map tile fetched` with the size, never the tile.
Map data © OpenStreetMap contributors (ODbL), tiles © OpenMapTiles, served by OpenFreeMap.
## Settings from a phone ## Settings from a phone
+1 -1
View File
@@ -32,7 +32,7 @@ type API struct {
Handoff *Handoff Handoff *Handoff
// Releases relays the app's releases from the private forge to the car (releases.go). // Releases relays the app's releases from the private forge to the car (releases.go).
Releases *Releases Releases *Releases
// Maps serves the roads for the Overview's map from OpenStreetMap (maps.go); nil is off. // Maps serves the roads for the Overview's map from OpenStreetMap vector tiles (maps.go); nil is off.
Maps *Maps Maps *Maps
// PublicURL is the server's address as a phone reaches it, for the pairing QR code; // PublicURL is the server's address as a phone reaches it, for the pairing QR code;
// derived from the request when empty. // derived from the request when empty.
+19 -11
View File
@@ -34,7 +34,8 @@ type config struct {
forgeURL string forgeURL string
forgeRepo string forgeRepo string
forgeToken string forgeToken string
overpassURL string mapTilesURL string
mapCacheDir string
} }
// loadConfig reads the environment and fails closed: a missing InfluxDB setting or a // loadConfig reads the environment and fails closed: a missing InfluxDB setting or a
@@ -52,7 +53,8 @@ func loadConfig(getenv func(string) string) (config, error) {
forgeURL: getenv("FORGE_URL"), forgeURL: getenv("FORGE_URL"),
forgeRepo: getenv("FORGE_REPO"), forgeRepo: getenv("FORGE_REPO"),
forgeToken: getenv("FORGE_TOKEN"), forgeToken: getenv("FORGE_TOKEN"),
overpassURL: getenv("OVERPASS_URL"), mapTilesURL: getenv("MAP_TILES_URL"),
mapCacheDir: getenv("MAP_CACHE_DIR"),
} }
if c.listenAddr == "" { if c.listenAddr == "" {
c.listenAddr = ":8080" c.listenAddr = ":8080"
@@ -75,16 +77,16 @@ func loadConfig(getenv func(string) string) (config, error) {
if c.forgeURL != "" && !strings.HasPrefix(c.forgeURL, "https://") { if c.forgeURL != "" && !strings.HasPrefix(c.forgeURL, "https://") {
return config{}, errors.New("FORGE_URL must be an https URL") return config{}, errors.New("FORGE_URL must be an https URL")
} }
// The map's roads come from an Overpass API: the public one unless another is named, none // The map's roads come from vector tiles named by a TileJSON: OpenFreeMap unless another is
// with "off". Only https, like the forge. // named, none with "off". Only https, like the forge.
switch c.overpassURL { switch c.mapTilesURL {
case "": case "":
c.overpassURL = DefaultOverpassURL c.mapTilesURL = DefaultMapTilesURL
case "off": case "off":
c.overpassURL = "" c.mapTilesURL = ""
default: default:
if !strings.HasPrefix(c.overpassURL, "https://") { if !strings.HasPrefix(c.mapTilesURL, "https://") {
return config{}, errors.New("OVERPASS_URL must be an https URL or \"off\"") return config{}, errors.New("MAP_TILES_URL must be an https URL or \"off\"")
} }
} }
if c.approvalPass != "" && len(c.approvalPass) < minApprovalPasswordLength { if c.approvalPass != "" && len(c.approvalPass) < minApprovalPasswordLength {
@@ -116,8 +118,14 @@ func main() {
releases := &Releases{ForgeURL: cfg.forgeURL, Repo: cfg.forgeRepo, Token: cfg.forgeToken} releases := &Releases{ForgeURL: cfg.forgeURL, Repo: cfg.forgeRepo, Token: cfg.forgeToken}
api := &API{Store: store, IngestToken: cfg.ingestToken, Pairing: pairing, Handoff: NewHandoff(), api := &API{Store: store, IngestToken: cfg.ingestToken, Pairing: pairing, Handoff: NewHandoff(),
Releases: releases, PublicURL: cfg.publicURL} Releases: releases, PublicURL: cfg.publicURL}
if cfg.overpassURL != "" { if cfg.mapTilesURL != "" {
api.Maps = NewMaps(cfg.overpassURL, mapUserAgent) if cfg.mapCacheDir != "" {
if err := os.MkdirAll(cfg.mapCacheDir, 0o700); err != nil {
slog.Error("map cache directory not usable", "error", err)
os.Exit(1)
}
}
api.Maps = NewMaps(cfg.mapTilesURL, cfg.mapCacheDir, mapUserAgent)
} }
srv := &http.Server{ srv := &http.Server{
+254 -186
View File
@@ -12,53 +12,69 @@ import (
"math" "math"
"net/http" "net/http"
"net/url" "net/url"
"os"
"path/filepath"
"strconv"
"strings" "strings"
"sync" "sync"
"time" "time"
) )
// Maps serves the roads around the car for the Overview's map (the "Grid with map" style), // Maps serves the roads around the car for the Overview's map (the "Grid with map" style). It
// taken from OpenStreetMap through an Overpass API and cached here, so the car only ever // fetches only the tiles a car asks for, as OpenStreetMap vector tiles (OpenMapTiles schema,
// talks to this server and asks Overpass nothing itself. // by default from OpenFreeMap), keeps their roads and stores them, so each tile is fetched
// once and the car only ever talks to this server.
// //
// POST /v1/map/roads {"z":14,"x":…,"y":…} // POST /v1/map/roads {"z":14,"x":…,"y":…}
// → {"z","x","y","extent":4096,"ways":[{"c":class,"p":[x,y,x,y,…]}]} // → {"z","x","y","extent":4096,"ways":[{"c":class,"p":[x,y,x,y,…]}]}
// //
// The car names a web-mercator tile at zoom 14 (about 1.5 km across in central Europe), not // The car names a web-mercator tile at zoom 14 (about 1.5 km across in central Europe), not
// its position, and the request goes in the body so the tile stays out of access logs. Points // its position, and in the body so the tile stays out of access logs. Points are tile-local
// are tile-local in 0..4096 (they may lie a little outside: a road is kept up to the first // in 0..4096 (a little beyond at the edges), simplified to about 1.5 m. Classes: 0 motorway and
// point beyond the edge), simplified to about 1.5 m. Classes: 0 motorway and trunk, 1 primary // trunk, 1 primary and secondary, 2 tertiary, 3 minor streets (residential, unclassified,
// and secondary, 2 tertiary and unclassified, 3 residential and living streets. // living streets).
// //
// Overpass is asked one tile at a time, at most once a second; a tile is kept for 30 days, // A fetch runs to its end even when the car stops waiting (the car gives up after a few
// up to mapCacheTiles of them. The tile's coordinates never appear in the log. // seconds), so its next try finds the tile. Tiles are kept in memory (the last mapMemoryTiles)
// and, with a cache directory, on disk for mapCacheAge. The tile's coordinates never appear
// in the log.
type Maps struct { type Maps struct {
OverpassURL string // TileJSONURL names the vector tiles: a TileJSON document whose first "tiles" template is
// used (OpenFreeMap moves its template with every weekly build).
TileJSONURL string
// CacheDir keeps fetched tiles across restarts; empty keeps them in memory only.
CacheDir string
UserAgent string UserAgent string
Client *http.Client Client *http.Client
mu sync.Mutex mu sync.Mutex
tiles map[mapTile]mapEntry memory map[mapTile]mapEntry
inflight map[mapTile]*mapFlight
fetchMu sync.Mutex // one Overpass request at a time template string
lastFetch time.Time tmplAt time.Time
gap time.Duration // slots limits fetches running at once.
slots chan struct{}
} }
const ( const (
mapZoom = 14 mapZoom = 14
mapExtent = 4096 mapExtent = 4096
mapCacheTiles = 3000 mapMemoryTiles = 300
mapCacheAge = 30 * 24 * time.Hour mapCacheAge = 30 * 24 * time.Hour
maxOverpassBytes = 32 << 20 maxTileBytes = 8 << 20
mapFetchTime = 60 * time.Second
templateAge = 6 * time.Hour
// The simplification tolerance in tile units (about 0.37 m each at 52° N). // The simplification tolerance in tile units (about 0.37 m each at 52° N).
mapTolerance = 4.0 mapTolerance = 4.0
// DefaultOverpassURL is the public instance of the Overpass API. // DefaultMapTilesURL is OpenFreeMap's TileJSON: free OpenStreetMap vector tiles, no key.
DefaultOverpassURL = "https://overpass-api.de/api/interpreter" DefaultMapTilesURL = "https://tiles.openfreemap.org/planet"
// mapUserAgent names this service to Overpass, as its usage policy asks. // mapUserAgent names this service to the tile host.
mapUserAgent = "nils-mg4-server/2.7 (private car dashboard; roads for an in-car map)" mapUserAgent = "nils-mg4-server/2.8 (private car dashboard; roads for an in-car map)"
) )
// mapWait is how long a request waits for a fetch: less than the car's own 8 s.
var mapWait = 6 * time.Second
type mapTile struct{ Z, X, Y int } type mapTile struct{ Z, X, Y int }
type mapEntry struct { type mapEntry struct {
@@ -66,12 +82,19 @@ type mapEntry struct {
at time.Time at time.Time
} }
// roadClasses maps OSM's highway values to the four classes the car draws. type mapFlight struct {
done chan struct{}
body []byte
err error
}
// roadClasses maps the OpenMapTiles transportation classes to the four the car draws; the
// rest (service, track, path, rail, ferry…) are left out.
var roadClasses = map[string]int{ var roadClasses = map[string]int{
"motorway": 0, "motorway_link": 0, "trunk": 0, "trunk_link": 0, "motorway": 0, "trunk": 0,
"primary": 1, "primary_link": 1, "secondary": 1, "secondary_link": 1, "primary": 1, "secondary": 1,
"tertiary": 2, "tertiary_link": 2, "unclassified": 2, "tertiary": 2,
"residential": 3, "living_street": 3, "minor": 3,
} }
type roadTile struct { type roadTile struct {
@@ -87,92 +110,227 @@ type road struct {
P []int `json:"p"` P []int `json:"p"`
} }
func NewMaps(overpassURL, userAgent string) *Maps { func NewMaps(tileJSONURL, cacheDir, userAgent string) *Maps {
return &Maps{OverpassURL: overpassURL, UserAgent: userAgent, gap: time.Second} return &Maps{TileJSONURL: tileJSONURL, CacheDir: cacheDir, UserAgent: userAgent}
} }
func (m *Maps) client() *http.Client { func (m *Maps) client() *http.Client {
if m.Client != nil { if m.Client != nil {
return m.Client return m.Client
} }
return &http.Client{Timeout: 60 * time.Second} return &http.Client{Timeout: mapFetchTime}
} }
// tileBounds is the tile's south, west, north and east edge in degrees. // Tile returns the tile's roads as JSON and whether it came from a cache. Waiting ends with
func tileBounds(t mapTile) (s, w, n, e float64) { // ctx; the fetch itself goes on and is kept.
scale := math.Exp2(float64(t.Z)) func (m *Maps) Tile(ctx context.Context, t mapTile) ([]byte, bool, error) {
lon := func(x float64) float64 { return x/scale*360 - 180 } if body, ok := m.cached(t); ok {
lat := func(y float64) float64 { return math.Atan(math.Sinh(math.Pi*(1-2*y/scale))) * 180 / math.Pi } return body, true, nil
return lat(float64(t.Y + 1)), lon(float64(t.X)), lat(float64(t.Y)), lon(float64(t.X + 1)) }
m.mu.Lock()
if m.inflight == nil {
m.inflight = map[mapTile]*mapFlight{}
m.slots = make(chan struct{}, 2)
}
f, running := m.inflight[t]
if !running {
f = &mapFlight{done: make(chan struct{})}
m.inflight[t] = f
go m.run(t, f)
}
m.mu.Unlock()
select {
case <-f.done:
return f.body, false, f.err
case <-ctx.Done():
return nil, false, errors.New("still fetching")
}
} }
// tilePoint is a position in the tile's own units: 0..extent across, y downwards. func (m *Maps) run(t mapTile, f *mapFlight) {
func tilePoint(t mapTile, lat, lon float64) (int, int) { defer func() {
scale := math.Exp2(float64(t.Z)) * mapExtent m.mu.Lock()
x := (lon + 180) / 360 * scale delete(m.inflight, t)
r := lat * math.Pi / 180 m.mu.Unlock()
y := (1 - math.Log(math.Tan(r)+1/math.Cos(r))/math.Pi) / 2 * scale close(f.done)
return int(math.Round(x - float64(t.X)*mapExtent)), int(math.Round(y - float64(t.Y)*mapExtent)) }()
m.slots <- struct{}{}
defer func() { <-m.slots }()
ctx, cancel := context.WithTimeout(context.Background(), mapFetchTime)
defer cancel()
raw, err := m.fetch(ctx, t)
if err == nil {
f.body, err = buildTile(t, raw)
}
if err != nil {
f.err = err
return
}
m.store(t, f.body)
slog.Info("map tile fetched", "ways", strings.Count(string(f.body), `"c":`), "bytes", len(f.body))
} }
// overpassQuery asks for the drivable roads in the tile, with a small margin so that roads func (m *Maps) cachePath(t mapTile) string {
// crossing its edge reach a point beyond it. return filepath.Join(m.CacheDir, fmt.Sprintf("%d_%d_%d.json", t.Z, t.X, t.Y))
func overpassQuery(t mapTile) string {
s, w, n, e := tileBounds(t)
dy, dx := (n-s)*0.03, (e-w)*0.03
box := fmt.Sprintf("%.6f,%.6f,%.6f,%.6f", s-dy, w-dx, n+dy, e+dx)
return "[out:json][timeout:50];way[\"highway\"~\"^(motorway|motorway_link|trunk|trunk_link|primary|primary_link|" +
"secondary|secondary_link|tertiary|tertiary_link|unclassified|residential|living_street)$\"](" + box +
");out geom(" + box + ");"
} }
type overpassResult struct { func (m *Maps) cached(t mapTile) ([]byte, bool) {
Elements []struct { m.mu.Lock()
Type string `json:"type"` e, ok := m.memory[t]
Tags map[string]string `json:"tags"` m.mu.Unlock()
Geometry []*struct { if ok && time.Since(e.at) <= mapCacheAge {
Lat float64 `json:"lat"` return e.body, true
Lon float64 `json:"lon"` }
} `json:"geometry"` if m.CacheDir == "" {
} `json:"elements"` return nil, false
}
info, err := os.Stat(m.cachePath(t))
if err != nil || time.Since(info.ModTime()) > mapCacheAge {
return nil, false
}
body, err := os.ReadFile(m.cachePath(t))
if err != nil || !json.Valid(body) {
return nil, false
}
m.remember(t, body, info.ModTime())
return body, true
} }
// buildTile turns Overpass's answer into the tile the car reads. A missing point (outside func (m *Maps) store(t mapTile, body []byte) {
// the clipped geometry) splits a road in two. m.remember(t, body, time.Now())
if m.CacheDir == "" {
return
}
tmp := m.cachePath(t) + ".tmp"
if err := os.WriteFile(tmp, body, 0o600); err != nil {
slog.Warn("map tile not stored", "error", err)
return
}
if err := os.Rename(tmp, m.cachePath(t)); err != nil {
slog.Warn("map tile not stored", "error", err)
}
}
// remember keeps the tile in memory, dropping the oldest when full.
func (m *Maps) remember(t mapTile, body []byte, at time.Time) {
m.mu.Lock()
defer m.mu.Unlock()
if m.memory == nil {
m.memory = map[mapTile]mapEntry{}
}
if _, ok := m.memory[t]; !ok && len(m.memory) >= mapMemoryTiles {
var oldest mapTile
var first time.Time
for k, e := range m.memory {
if first.IsZero() || e.at.Before(first) {
oldest, first = k, e.at
}
}
delete(m.memory, oldest)
}
m.memory[t] = mapEntry{body: body, at: at}
}
func (m *Maps) get(ctx context.Context, u string, limit int64) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", m.UserAgent)
resp, err := m.client().Do(req)
if err != nil {
// The URL holds the tile: name the failure, not the request.
if errors.Is(err, context.DeadlineExceeded) || errors.Is(ctx.Err(), context.DeadlineExceeded) {
return nil, errors.New("tile host timed out")
}
return nil, errors.New("tile host could not be reached")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
_, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, 1<<16))
return nil, fmt.Errorf("tile host answered %d", resp.StatusCode)
}
body, err := io.ReadAll(io.LimitReader(resp.Body, limit+1))
if err != nil {
return nil, errors.New("tile host answer could not be read")
}
if int64(len(body)) > limit {
return nil, errors.New("tile host answer is too large")
}
return body, nil
}
// tileTemplate is the current {z}/{x}/{y} URL from the TileJSON, looked up every few hours.
func (m *Maps) tileTemplate(ctx context.Context) (string, error) {
m.mu.Lock()
tmpl, at := m.template, m.tmplAt
m.mu.Unlock()
if tmpl != "" && time.Since(at) < templateAge {
return tmpl, nil
}
body, err := m.get(ctx, m.TileJSONURL, 1<<20)
if err != nil {
return "", err
}
var doc struct {
Tiles []string `json:"tiles"`
}
if err := json.Unmarshal(body, &doc); err != nil || len(doc.Tiles) == 0 {
return "", errors.New("tile index has no tiles")
}
tmpl = doc.Tiles[0]
u, err := url.Parse(strings.NewReplacer("{z}", "0", "{x}", "0", "{y}", "0").Replace(tmpl))
if err != nil || u.Scheme != "https" || u.Host == "" ||
!strings.Contains(tmpl, "{z}") || !strings.Contains(tmpl, "{x}") || !strings.Contains(tmpl, "{y}") {
return "", errors.New("tile index names no usable https template")
}
m.mu.Lock()
m.template, m.tmplAt = tmpl, time.Now()
m.mu.Unlock()
return tmpl, nil
}
func (m *Maps) fetch(ctx context.Context, t mapTile) ([]byte, error) {
tmpl, err := m.tileTemplate(ctx)
if err != nil {
return nil, err
}
u := strings.NewReplacer("{z}", strconv.Itoa(t.Z), "{x}", strconv.Itoa(t.X), "{y}", strconv.Itoa(t.Y)).Replace(tmpl)
raw, err := m.get(ctx, u, maxTileBytes)
if err != nil {
return nil, err
}
// Some hosts send the tile gzipped whatever the request said.
if len(raw) > 2 && raw[0] == 0x1f && raw[1] == 0x8b {
zr, err := gzip.NewReader(bytes.NewReader(raw))
if err != nil {
return nil, errors.New("tile is not readable")
}
raw, err = io.ReadAll(io.LimitReader(zr, maxTileBytes+1))
if err != nil || len(raw) > maxTileBytes {
return nil, errors.New("tile is not readable")
}
}
return raw, nil
}
// buildTile keeps the roads of the vector tile's transportation layer, in the car's format.
func buildTile(t mapTile, raw []byte) ([]byte, error) { func buildTile(t mapTile, raw []byte) ([]byte, error) {
var res overpassResult lines, err := decodeRoads(raw)
if err := json.Unmarshal(raw, &res); err != nil { if err != nil {
return nil, fmt.Errorf("overpass answer: %w", err) return nil, err
} }
out := roadTile{Z: t.Z, X: t.X, Y: t.Y, Extent: mapExtent, Ways: []road{}} out := roadTile{Z: t.Z, X: t.X, Y: t.Y, Extent: mapExtent, Ways: []road{}}
for _, el := range res.Elements { for _, l := range lines {
class, ok := roadClasses[el.Tags["highway"]] pts := simplify(l.points, mapTolerance)
if el.Type != "way" || !ok { if len(pts) < 2 {
continue continue
} }
var run [][2]int
flush := func() {
if pts := simplify(run, mapTolerance); len(pts) >= 2 {
flat := make([]int, 0, len(pts)*2) flat := make([]int, 0, len(pts)*2)
for _, p := range pts { for _, p := range pts {
flat = append(flat, p[0], p[1]) flat = append(flat, p[0], p[1])
} }
out.Ways = append(out.Ways, road{C: class, P: flat}) out.Ways = append(out.Ways, road{C: l.class, P: flat})
}
run = nil
}
for _, g := range el.Geometry {
if g == nil {
flush()
continue
}
x, y := tilePoint(t, g.Lat, g.Lon)
if n := len(run); n > 0 && run[n-1] == [2]int{x, y} {
continue
}
run = append(run, [2]int{x, y})
}
flush()
} }
return json.Marshal(out) return json.Marshal(out)
} }
@@ -216,100 +374,11 @@ func segmentDistance(p, a, b [2]int) float64 {
if dx == 0 && dy == 0 { if dx == 0 && dy == 0 {
return math.Hypot(px-ax, py-ay) return math.Hypot(px-ax, py-ay)
} }
u := math.Max(0, math.Min(1, ((px-ax)*dx+(py-ay)*dy)/(dx*dx+dy*dy))) u := ((px-ax)*dx + (py-ay)*dy) / (dx*dx + dy*dy)
u = max(0, min(1, u))
return math.Hypot(px-ax-u*dx, py-ay-u*dy) return math.Hypot(px-ax-u*dx, py-ay-u*dy)
} }
// Tile returns the tile's roads as JSON, from the cache or from Overpass, and whether it
// came from the cache.
func (m *Maps) Tile(ctx context.Context, t mapTile) ([]byte, bool, error) {
if body, ok := m.cached(t); ok {
return body, true, nil
}
m.fetchMu.Lock()
defer m.fetchMu.Unlock()
// Another request may have fetched it while this one waited.
if body, ok := m.cached(t); ok {
return body, true, nil
}
if wait := m.gap - time.Since(m.lastFetch); wait > 0 {
select {
case <-time.After(wait):
case <-ctx.Done():
return nil, false, ctx.Err()
}
}
m.lastFetch = time.Now()
raw, err := m.fetch(ctx, t)
if err != nil {
return nil, false, err
}
body, err := buildTile(t, raw)
if err != nil {
return nil, false, err
}
m.store(t, body)
return body, false, nil
}
func (m *Maps) cached(t mapTile) ([]byte, bool) {
m.mu.Lock()
defer m.mu.Unlock()
e, ok := m.tiles[t]
if !ok || time.Since(e.at) > mapCacheAge {
return nil, false
}
return e.body, true
}
// store keeps the tile, dropping the oldest when the cache is full.
func (m *Maps) store(t mapTile, body []byte) {
m.mu.Lock()
defer m.mu.Unlock()
if m.tiles == nil {
m.tiles = map[mapTile]mapEntry{}
}
if len(m.tiles) >= mapCacheTiles {
var oldest mapTile
var at time.Time
for k, e := range m.tiles {
if at.IsZero() || e.at.Before(at) {
oldest, at = k, e.at
}
}
delete(m.tiles, oldest)
}
m.tiles[t] = mapEntry{body: body, at: time.Now()}
}
// fetch asks Overpass. The query goes in the body, so its errors never carry the tile.
func (m *Maps) fetch(ctx context.Context, t mapTile) ([]byte, error) {
form := url.Values{"data": {overpassQuery(t)}}.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, m.OverpassURL, strings.NewReader(form))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", m.UserAgent)
resp, err := m.client().Do(req)
if err != nil {
return nil, errors.New("overpass could not be reached")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
_, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, 1<<16))
return nil, fmt.Errorf("overpass answered %d", resp.StatusCode)
}
raw, err := io.ReadAll(io.LimitReader(resp.Body, maxOverpassBytes+1))
if err != nil {
return nil, errors.New("overpass answer could not be read")
}
if len(raw) > maxOverpassBytes {
return nil, errors.New("overpass answer is too large")
}
return raw, nil
}
func (a *API) mapRoutes(mux *http.ServeMux) { func (a *API) mapRoutes(mux *http.ServeMux) {
mux.HandleFunc("POST /v1/map/roads", a.ingest(a.mapRoads)) mux.HandleFunc("POST /v1/map/roads", a.ingest(a.mapRoads))
} }
@@ -332,17 +401,16 @@ func (a *API) mapRoads(w http.ResponseWriter, r *http.Request, c caller, body []
if *req.X < 0 || *req.X >= limit || *req.Y < 0 || *req.Y >= limit { if *req.X < 0 || *req.X >= limit || *req.Y < 0 || *req.Y >= limit {
return invalid("x and y must be within the zoom level") return invalid("x and y must be within the zoom level")
} }
t := mapTile{Z: *req.Z, X: *req.X, Y: *req.Y} // The car waits a few seconds at most; the fetch goes on without it.
data, cached, err := a.Maps.Tile(r.Context(), t) wait, cancel := context.WithTimeout(r.Context(), mapWait)
defer cancel()
data, _, err := a.Maps.Tile(wait, mapTile{Z: *req.Z, X: *req.X, Y: *req.Y})
if err != nil { if err != nil {
slog.Warn("map tile not available", "installation", who(c), "error", err) slog.Warn("map tile not available yet", "installation", who(c), "error", err)
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "map data not available, try again later"}) w.Header().Set("Retry-After", "10")
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "map data not available yet, try again shortly"})
return nil return nil
} }
if !cached {
slog.Info("map tile fetched", "installation", who(c), "ways", strings.Count(string(data), `"c":`),
"bytes", len(data))
}
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
var buf bytes.Buffer var buf bytes.Buffer
+245 -96
View File
@@ -12,50 +12,140 @@ import (
"strings" "strings"
"sync/atomic" "sync/atomic"
"testing" "testing"
"time"
) )
// A tile in central Berlin. // A tile in central Berlin.
var testTile = mapTile{Z: 14, X: 8802, Y: 5373} var testTile = mapTile{Z: 14, X: 8802, Y: 5373}
// fakeOverpass answers every query with a primary road (with a gap in its geometry), a // ---------- A minimal vector-tile encoder for the tests ----------
// residential street with a needless middle point, and a footway the car does not draw.
func fakeOverpass(t *testing.T, calls *atomic.Int32, status int) *httptest.Server { func pbVarint(v uint64) []byte {
t.Helper() var b []byte
s, w, n, e := tileBounds(testTile) for v >= 0x80 {
lat := func(f float64) float64 { return s + (n-s)*f } b = append(b, byte(v)|0x80)
lon := func(f float64) float64 { return w + (e-w)*f } v >>= 7
answer := fmt.Sprintf(`{"elements":[
{"type":"way","tags":{"highway":"primary"},"geometry":[
{"lat":%f,"lon":%f},{"lat":%f,"lon":%f},null,{"lat":%f,"lon":%f},{"lat":%f,"lon":%f}]},
{"type":"way","tags":{"highway":"residential"},"geometry":[
{"lat":%f,"lon":%f},{"lat":%f,"lon":%f},{"lat":%f,"lon":%f}]},
{"type":"way","tags":{"highway":"footway"},"geometry":[
{"lat":%f,"lon":%f},{"lat":%f,"lon":%f}]}]}`,
lat(0.1), lon(0.1), lat(0.2), lon(0.2), lat(0.6), lon(0.6), lat(0.7), lon(0.7),
lat(0.5), lon(0.1), lat(0.5), lon(0.5), lat(0.5), lon(0.9),
lat(0.3), lon(0.3), lat(0.4), lon(0.4))
return httptest.NewTLSServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
calls.Add(1)
if r.Method != http.MethodPost || !strings.Contains(r.UserAgent(), "nils-mg4-server") {
t.Errorf("request %s %q", r.Method, r.UserAgent())
} }
_ = r.ParseForm() return append(b, byte(v))
if q := r.PostForm.Get("data"); !strings.Contains(q, "out geom(") || !strings.Contains(q, "residential") {
t.Errorf("query %q", q)
}
if status != http.StatusOK {
http.Error(rw, "busy", status)
return
}
_, _ = rw.Write([]byte(answer))
}))
} }
func mapsAPI(overpass *httptest.Server) http.Handler { func pbKey(num, wire int) []byte { return pbVarint(uint64(num<<3 | wire)) }
func pbBytes(num int, data []byte) []byte {
return append(append(pbKey(num, 2), pbVarint(uint64(len(data)))...), data...)
}
func pbUint(num int, v uint64) []byte { return append(pbKey(num, 0), pbVarint(v)...) }
func pbPacked(num int, vs []uint32) []byte {
var d []byte
for _, v := range vs {
d = append(d, pbVarint(uint64(v))...)
}
return pbBytes(num, d)
}
func zz(v int) uint32 { return uint32((v << 1) ^ (v >> 31)) }
// lineCmds encodes line strings as MoveTo/LineTo commands with relative moves.
func lineCmds(lines ...[][2]int) []uint32 {
var out []uint32
x, y := 0, 0
for _, l := range lines {
out = append(out, 1|1<<3, zz(l[0][0]-x), zz(l[0][1]-y))
x, y = l[0][0], l[0][1]
out = append(out, uint32(2|(len(l)-1)<<3))
for _, p := range l[1:] {
out = append(out, zz(p[0]-x), zz(p[1]-y))
x, y = p[0], p[1]
}
}
return out
}
type testFeature struct {
class int // index into the values below
geomType uint64
geometry []uint32
}
// testVectorTile has a water layer (ignored) and a transportation layer with the given extent.
func testVectorTile(extent uint64) []byte {
values := []string{"primary", "minor", "service", "motorway"}
features := []testFeature{
// A primary road with a needless middle point.
{0, 2, lineCmds([][2]int{{100, 100}, {200, 200}, {300, 300}})},
// A minor street in two pieces.
{1, 2, lineCmds([][2]int{{0, 500}, {400, 500}}, [][2]int{{600, 500}, {900, 520}})},
// A service road and a motorway as a polygon: neither is drawn.
{2, 2, lineCmds([][2]int{{10, 10}, {20, 20}})},
{3, 3, lineCmds([][2]int{{10, 10}, {20, 20}})},
}
var layer []byte
layer = append(layer, pbUint(15, 2)...)
layer = append(layer, pbBytes(1, []byte("transportation"))...)
for _, f := range features {
var feat []byte
feat = append(feat, pbPacked(2, []uint32{0, uint32(f.class)})...)
feat = append(feat, pbUint(3, f.geomType)...)
feat = append(feat, pbPacked(4, f.geometry)...)
layer = append(layer, pbBytes(2, feat)...)
}
layer = append(layer, pbBytes(3, []byte("class"))...)
for _, v := range values {
layer = append(layer, pbBytes(4, pbBytes(1, []byte(v)))...)
}
layer = append(layer, pbUint(5, extent)...)
water := append(pbBytes(1, []byte("water")), pbUint(5, 4096)...)
return append(pbBytes(3, water), pbBytes(3, layer)...)
}
// ---------- A fake tile host ----------
type fakeHost struct {
*httptest.Server
tiles atomic.Int32
status int
delay time.Duration
gzip bool
}
func newFakeHost(t *testing.T) *fakeHost {
t.Helper()
h := &fakeHost{status: http.StatusOK}
mux := http.NewServeMux()
mux.HandleFunc("GET /planet", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `{"tilejson":"3.0.0","tiles":["%s/planet/build1/{z}/{x}/{y}.pbf"]}`, h.URL)
})
mux.HandleFunc("GET /planet/build1/{z}/{x}/{y}", func(w http.ResponseWriter, r *http.Request) {
h.tiles.Add(1)
if !strings.Contains(r.UserAgent(), "nils-mg4-server") {
t.Errorf("user agent %q", r.UserAgent())
}
time.Sleep(h.delay)
if h.status != http.StatusOK {
http.Error(w, "busy", h.status)
return
}
body := testVectorTile(4096)
if h.gzip {
var buf bytes.Buffer
zw := gzip.NewWriter(&buf)
_, _ = zw.Write(body)
_ = zw.Close()
body = buf.Bytes()
}
_, _ = w.Write(body)
})
h.Server = httptest.NewTLSServer(mux)
return h
}
func mapsAPIWith(host *fakeHost, cacheDir string) http.Handler {
api, _ := newTestAPI() api, _ := newTestAPI()
api.Maps = NewMaps(overpass.URL, mapUserAgent) api.Maps = NewMaps(host.URL+"/planet", cacheDir, mapUserAgent)
api.Maps.Client = overpass.Client() api.Maps.Client = host.Client()
api.Maps.gap = 0
return api.Routes() return api.Routes()
} }
@@ -63,15 +153,8 @@ func tileBody(t mapTile) string {
return fmt.Sprintf(`{"z":%d,"x":%d,"y":%d}`, t.Z, t.X, t.Y) return fmt.Sprintf(`{"z":%d,"x":%d,"y":%d}`, t.Z, t.X, t.Y)
} }
func TestMapTileRoadsAreClassifiedSplitAndSimplified(t *testing.T) { func decodeAnswer(t *testing.T, rec *httptest.ResponseRecorder) roadTile {
var calls atomic.Int32 t.Helper()
overpass := fakeOverpass(t, &calls, http.StatusOK)
defer overpass.Close()
h := mapsAPI(overpass)
if rec := do(t, h, "POST", "/v1/map/roads", "", tileBody(testTile)); rec.Code != 401 {
t.Fatalf("without the car's token: %d", rec.Code)
}
rec := do(t, h, "POST", "/v1/map/roads", testToken, tileBody(testTile))
if rec.Code != 200 { if rec.Code != 200 {
t.Fatalf("%d %s", rec.Code, rec.Body) t.Fatalf("%d %s", rec.Code, rec.Body)
} }
@@ -79,39 +162,100 @@ func TestMapTileRoadsAreClassifiedSplitAndSimplified(t *testing.T) {
if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil { if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if got.Extent != 4096 || got.X != testTile.X || got.Y != testTile.Y { return got
}
// ---------- Tests ----------
func TestVectorTileRoadsAreClassifiedSplitAndSimplified(t *testing.T) {
lines, err := decodeRoads(testVectorTile(4096))
if err != nil {
t.Fatal(err)
}
if len(lines) != 3 || lines[0].class != 1 || lines[1].class != 3 || lines[2].class != 3 {
t.Fatalf("lines %+v", lines)
}
if fmt.Sprint(lines[1].points) != "[[0 500] [400 500]]" || fmt.Sprint(lines[2].points) != "[[600 500] [900 520]]" {
t.Fatalf("pieces %v %v", lines[1].points, lines[2].points)
}
// Another extent is scaled to 4096.
half, err := decodeRoads(testVectorTile(8192))
if err != nil || fmt.Sprint(half[0].points[2]) != "[150 150]" {
t.Fatalf("scaled %v %v", half, err)
}
body, err := buildTile(testTile, testVectorTile(4096))
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(body), `{"c":1,"p":[100,100,300,300]}`) {
t.Fatalf("the straight road keeps only its ends: %s", body)
}
}
func TestMalformedVectorTilesAreRefused(t *testing.T) {
good := testVectorTile(4096)
for name, raw := range map[string][]byte{
"cut short": good[:len(good)-7],
"bad wire type": {0x1f, 0x00},
"length too long": {0x1a, 0x7f, 0x01},
} {
if _, err := decodeRoads(raw); err == nil {
t.Errorf("%s: accepted", name)
}
}
if lines, err := decodeRoads(nil); err != nil || len(lines) != 0 {
t.Fatalf("an empty tile is an empty map: %v %v", lines, err)
}
}
func TestMapTilesAreFetchedOnceAndKeptOnDisk(t *testing.T) {
host := newFakeHost(t)
host.gzip = true // some hosts gzip whatever was asked
defer host.Close()
dir := t.TempDir()
h := mapsAPIWith(host, dir)
if rec := do(t, h, "POST", "/v1/map/roads", "", tileBody(testTile)); rec.Code != 401 {
t.Fatalf("without the car's token: %d", rec.Code)
}
got := decodeAnswer(t, do(t, h, "POST", "/v1/map/roads", testToken, tileBody(testTile)))
if got.Extent != 4096 || got.X != testTile.X || got.Y != testTile.Y || len(got.Ways) != 3 {
t.Fatalf("tile %+v", got) t.Fatalf("tile %+v", got)
} }
// The primary road in two pieces, the street straightened to its ends, no footway. decodeAnswer(t, do(t, h, "POST", "/v1/map/roads", testToken, tileBody(testTile)))
if len(got.Ways) != 3 || got.Ways[0].C != 1 || got.Ways[1].C != 1 || got.Ways[2].C != 3 { // A restart: a new server with the same directory asks the host nothing.
t.Fatalf("ways %+v", got.Ways) decodeAnswer(t, do(t, mapsAPIWith(host, dir), "POST", "/v1/map/roads", testToken, tileBody(testTile)))
} if n := host.tiles.Load(); n != 1 {
if len(got.Ways[2].P) != 4 { t.Fatalf("tile fetched %d times", n)
t.Fatalf("the straight street keeps only its ends: %v", got.Ways[2].P)
}
// Tile-local: 0.1 of the way in is about 410 units from the west edge.
if x := got.Ways[0].P[0]; x < 400 || x > 420 {
t.Fatalf("x %d", x)
} }
}
// Cached: the second car asking costs Overpass nothing. func TestASlowTileIsFinishedForTheCarsNextTry(t *testing.T) {
if rec := do(t, h, "POST", "/v1/map/roads", testToken, tileBody(testTile)); rec.Code != 200 { previous := mapWait
t.Fatalf("%d", rec.Code) mapWait = 50 * time.Millisecond
defer func() { mapWait = previous }()
host := newFakeHost(t)
host.delay = 300 * time.Millisecond
defer host.Close()
h := mapsAPIWith(host, "")
rec := do(t, h, "POST", "/v1/map/roads", testToken, tileBody(testTile))
if rec.Code != 503 || rec.Header().Get("Retry-After") == "" {
t.Fatalf("the car should hear 'try again': %d", rec.Code)
} }
if calls.Load() != 1 { time.Sleep(600 * time.Millisecond)
t.Fatalf("overpass asked %d times", calls.Load()) decodeAnswer(t, do(t, h, "POST", "/v1/map/roads", testToken, tileBody(testTile)))
if n := host.tiles.Load(); n != 1 {
t.Fatalf("tile fetched %d times", n)
} }
} }
func TestMapTilesAreGzippedWhenTheCarAcceptsIt(t *testing.T) { func TestMapTilesAreGzippedWhenTheCarAcceptsIt(t *testing.T) {
var calls atomic.Int32 host := newFakeHost(t)
overpass := fakeOverpass(t, &calls, http.StatusOK) defer host.Close()
defer overpass.Close()
req := httptest.NewRequest("POST", "/v1/map/roads", strings.NewReader(tileBody(testTile))) req := httptest.NewRequest("POST", "/v1/map/roads", strings.NewReader(tileBody(testTile)))
req.Header.Set("Authorization", "Bearer "+testToken) req.Header.Set("Authorization", "Bearer "+testToken)
req.Header.Set("Accept-Encoding", "gzip") req.Header.Set("Accept-Encoding", "gzip")
rec := httptest.NewRecorder() rec := httptest.NewRecorder()
mapsAPI(overpass).ServeHTTP(rec, req) mapsAPIWith(host, "").ServeHTTP(rec, req)
if rec.Code != 200 || rec.Header().Get("Content-Encoding") != "gzip" { if rec.Code != 200 || rec.Header().Get("Content-Encoding") != "gzip" {
t.Fatalf("%d %v", rec.Code, rec.Header()) t.Fatalf("%d %v", rec.Code, rec.Header())
} }
@@ -126,84 +270,89 @@ func TestMapTilesAreGzippedWhenTheCarAcceptsIt(t *testing.T) {
} }
func TestMapRequestsAreChecked(t *testing.T) { func TestMapRequestsAreChecked(t *testing.T) {
var calls atomic.Int32 host := newFakeHost(t)
overpass := fakeOverpass(t, &calls, http.StatusOK) defer host.Close()
defer overpass.Close() h := mapsAPIWith(host, "")
h := mapsAPI(overpass)
for _, body := range []string{`{}`, `{"z":15,"x":1,"y":1}`, `{"z":14,"x":-1,"y":1}`, for _, body := range []string{`{}`, `{"z":15,"x":1,"y":1}`, `{"z":14,"x":-1,"y":1}`,
`{"z":14,"x":1,"y":16384}`, `not json`} { `{"z":14,"x":1,"y":16384}`, `not json`} {
if rec := do(t, h, "POST", "/v1/map/roads", testToken, body); rec.Code != 400 { if rec := do(t, h, "POST", "/v1/map/roads", testToken, body); rec.Code != 400 {
t.Errorf("%s: %d", body, rec.Code) t.Errorf("%s: %d", body, rec.Code)
} }
} }
if calls.Load() != 0 { if host.tiles.Load() != 0 {
t.Fatal("a bad request reached Overpass") t.Fatal("a bad request reached the tile host")
} }
} }
func TestABusyOverpassIsATemporaryFailureAndNotCached(t *testing.T) { func TestAFailingHostIsATemporaryFailureAndNotCached(t *testing.T) {
var logged bytes.Buffer var logged bytes.Buffer
previous := slog.Default() previous := slog.Default()
slog.SetDefault(slog.New(slog.NewTextHandler(&logged, nil))) slog.SetDefault(slog.New(slog.NewTextHandler(&logged, nil)))
defer slog.SetDefault(previous) defer slog.SetDefault(previous)
var calls atomic.Int32 host := newFakeHost(t)
overpass := fakeOverpass(t, &calls, http.StatusTooManyRequests) host.status = http.StatusBadGateway
defer overpass.Close() defer host.Close()
h := mapsAPI(overpass) h := mapsAPIWith(host, t.TempDir())
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
if rec := do(t, h, "POST", "/v1/map/roads", testToken, tileBody(testTile)); rec.Code != 503 { if rec := do(t, h, "POST", "/v1/map/roads", testToken, tileBody(testTile)); rec.Code != 503 {
t.Fatalf("%d", rec.Code) t.Fatalf("%d", rec.Code)
} }
} }
if calls.Load() != 2 { if n := host.tiles.Load(); n != 2 {
t.Fatalf("a failure was cached: %d calls", calls.Load()) t.Fatalf("a failure was cached: %d fetches", n)
} }
// Where the car is stays out of the log. // Where the car is stays out of the log.
out := logged.String() out := logged.String()
if !strings.Contains(out, "overpass answered 429") || strings.Contains(out, "8802") || if !strings.Contains(out, "tile host answered 502") || strings.Contains(out, "8802") ||
strings.Contains(out, "5373") { strings.Contains(out, "5373") {
t.Fatalf("log:\n%s", out) t.Fatalf("log:\n%s", out)
} }
} }
func TestMapsAreOffWithoutOverpass(t *testing.T) { func TestATileIndexWithoutHttpsIsRefused(t *testing.T) {
index := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`{"tiles":["http://tiles.example.com/{z}/{x}/{y}.pbf"]}`))
}))
defer index.Close()
api, _ := newTestAPI()
api.Maps = NewMaps(index.URL, "", mapUserAgent)
api.Maps.Client = index.Client()
if rec := do(t, api.Routes(), "POST", "/v1/map/roads", testToken, tileBody(testTile)); rec.Code != 503 {
t.Fatalf("%d", rec.Code)
}
}
func TestMapsAreOffWithoutATileSource(t *testing.T) {
api, _ := newTestAPI() api, _ := newTestAPI()
if rec := do(t, api.Routes(), "POST", "/v1/map/roads", testToken, tileBody(testTile)); rec.Code != 404 { if rec := do(t, api.Routes(), "POST", "/v1/map/roads", testToken, tileBody(testTile)); rec.Code != 404 {
t.Fatalf("%d", rec.Code) t.Fatalf("%d", rec.Code)
} }
} }
func TestOverpassURLConfiguration(t *testing.T) { func TestMapTilesURLConfiguration(t *testing.T) {
base := map[string]string{"INFLUX_URL": "http://influx", "INFLUX_TOKEN": "t", "INFLUX_ORG": "o", base := map[string]string{"INFLUX_URL": "http://influx", "INFLUX_TOKEN": "t", "INFLUX_ORG": "o",
"INGEST_TOKEN": "0123456789abcdef01234567"} "INGEST_TOKEN": "0123456789abcdef01234567"}
load := func(overpass string) (config, error) { load := func(tiles string) (config, error) {
return loadConfig(func(k string) string { return loadConfig(func(k string) string {
if k == "OVERPASS_URL" { if k == "MAP_TILES_URL" {
return overpass return tiles
} }
return base[k] return base[k]
}) })
} }
if c, err := load(""); err != nil || c.overpassURL != DefaultOverpassURL { if c, err := load(""); err != nil || c.mapTilesURL != DefaultMapTilesURL {
t.Fatalf("default: %q %v", c.overpassURL, err) t.Fatalf("default: %q %v", c.mapTilesURL, err)
} }
if c, err := load("off"); err != nil || c.overpassURL != "" { if c, err := load("off"); err != nil || c.mapTilesURL != "" {
t.Fatalf("off: %q %v", c.overpassURL, err) t.Fatalf("off: %q %v", c.mapTilesURL, err)
} }
if _, err := load("http://overpass.example.com/api/interpreter"); err == nil { if _, err := load("http://tiles.example.com/planet"); err == nil {
t.Fatal("plain http accepted") t.Fatal("plain http accepted")
} }
} }
func TestTilePointsAndSimplification(t *testing.T) { func TestSimplification(t *testing.T) {
s, w, n, e := tileBounds(testTile)
if x, y := tilePoint(testTile, n, w); x != 0 || y != 0 {
t.Fatalf("north-west corner %d,%d", x, y)
}
if x, y := tilePoint(testTile, s, e); x != 4096 || y != 4096 {
t.Fatalf("south-east corner %d,%d", x, y)
}
line := [][2]int{{0, 0}, {10, 1}, {20, 0}, {30, 40}} line := [][2]int{{0, 0}, {10, 1}, {20, 0}, {30, 40}}
got := simplify(line, 4) got := simplify(line, 4)
if len(got) != 3 || got[1] != [2]int{20, 0} { if len(got) != 3 || got[1] != [2]int{20, 0} {
+278
View File
@@ -0,0 +1,278 @@
package main
import (
"errors"
"math"
)
// The roads of a Mapbox Vector Tile (spec 2.1) in the OpenMapTiles schema: the line features
// of the "transportation" layer whose "class" the car draws (roadClasses), scaled to
// mapExtent. A minimal protobuf reader, enough for that and nothing more; anything malformed
// is an error, never a guess.
type roadLine struct {
class int
points [][2]int
}
var errBadTile = errors.New("vector tile is malformed")
// pbReader walks protobuf fields.
type pbReader struct {
b []byte
i int
}
func (r *pbReader) done() bool { return r.i >= len(r.b) }
func (r *pbReader) varint() (uint64, error) {
var v uint64
for shift := uint(0); shift < 64; shift += 7 {
if r.i >= len(r.b) {
return 0, errBadTile
}
c := r.b[r.i]
r.i++
v |= uint64(c&0x7f) << shift
if c < 0x80 {
return v, nil
}
}
return 0, errBadTile
}
// field reads one field's key and, for length-delimited ones, its bytes.
func (r *pbReader) field() (num int, wire int, val uint64, data []byte, err error) {
key, err := r.varint()
if err != nil {
return 0, 0, 0, nil, err
}
num, wire = int(key>>3), int(key&7)
switch wire {
case 0:
val, err = r.varint()
case 1:
if r.i+8 > len(r.b) {
return 0, 0, 0, nil, errBadTile
}
r.i += 8
case 2:
var n uint64
n, err = r.varint()
if err == nil {
if n > uint64(len(r.b)-r.i) {
return 0, 0, 0, nil, errBadTile
}
data = r.b[r.i : r.i+int(n)]
r.i += int(n)
}
case 5:
if r.i+4 > len(r.b) {
return 0, 0, 0, nil, errBadTile
}
r.i += 4
default:
return 0, 0, 0, nil, errBadTile
}
return num, wire, val, data, err
}
// packed reads a repeated uint32 field, packed or not.
func packed(wire int, val uint64, data []byte, into []uint32) ([]uint32, error) {
if wire == 0 {
return append(into, uint32(val)), nil
}
if wire != 2 {
return nil, errBadTile
}
r := pbReader{b: data}
for !r.done() {
v, err := r.varint()
if err != nil {
return nil, err
}
into = append(into, uint32(v))
}
return into, nil
}
// decodeRoads returns the drawable roads of the tile.
func decodeRoads(tile []byte) ([]roadLine, error) {
r := pbReader{b: tile}
for !r.done() {
num, _, _, data, err := r.field()
if err != nil {
return nil, err
}
if num != 3 { // layers
continue
}
lines, isRoads, err := decodeLayer(data)
if err != nil {
return nil, err
}
if isRoads {
return lines, nil
}
}
return nil, nil // a tile without roads (sea, desert) is an empty one
}
type rawFeature struct {
tags []uint32
geomType uint64
geometry []uint32
}
func decodeLayer(layer []byte) ([]roadLine, bool, error) {
r := pbReader{b: layer}
var name string
var keys []string
var values []string // only string values matter for "class"
var features []rawFeature
extent := uint64(4096)
for !r.done() {
num, wire, val, data, err := r.field()
if err != nil {
return nil, false, err
}
switch num {
case 1:
name = string(data)
case 2:
f, err := decodeFeature(data)
if err != nil {
return nil, false, err
}
features = append(features, f)
case 3:
keys = append(keys, string(data))
case 4:
values = append(values, valueString(data))
case 5:
if wire != 0 || val == 0 {
return nil, false, errBadTile
}
extent = val
}
}
if name != "transportation" {
return nil, false, nil
}
scale := float64(mapExtent) / float64(extent)
var lines []roadLine
for _, f := range features {
if f.geomType != 2 { // LINESTRING
continue
}
class, ok := -1, false
for i := 0; i+1 < len(f.tags); i += 2 {
k, v := int(f.tags[i]), int(f.tags[i+1])
if k >= len(keys) || v >= len(values) {
return nil, false, errBadTile
}
if keys[k] == "class" {
class, ok = roadClasses[values[v]]
if !ok {
class = -1
}
}
}
if class < 0 {
continue
}
parts, err := lineGeometry(f.geometry, scale)
if err != nil {
return nil, false, err
}
for _, p := range parts {
lines = append(lines, roadLine{class: class, points: p})
}
}
return lines, true, nil
}
func decodeFeature(data []byte) (rawFeature, error) {
var f rawFeature
r := pbReader{b: data}
for !r.done() {
num, wire, val, d, err := r.field()
if err != nil {
return f, err
}
switch num {
case 2:
if f.tags, err = packed(wire, val, d, f.tags); err != nil {
return f, err
}
case 3:
f.geomType = val
case 4:
if f.geometry, err = packed(wire, val, d, f.geometry); err != nil {
return f, err
}
}
}
if len(f.tags)%2 != 0 {
return f, errBadTile
}
return f, nil
}
// valueString is a Value's string_value, "" for any other kind.
func valueString(data []byte) string {
r := pbReader{b: data}
for !r.done() {
num, _, _, d, err := r.field()
if err != nil {
return ""
}
if num == 1 {
return string(d)
}
}
return ""
}
// lineGeometry follows the MoveTo/LineTo commands of a (multi)line string.
func lineGeometry(cmds []uint32, scale float64) ([][][2]int, error) {
var parts [][][2]int
var cur [][2]int
x, y := 0, 0
zig := func(v uint32) int { return int(int32(v>>1) ^ -int32(v&1)) }
at := func() [2]int {
return [2]int{int(math.Round(float64(x) * scale)), int(math.Round(float64(y) * scale))}
}
for i := 0; i < len(cmds); {
id, count := cmds[i]&7, int(cmds[i]>>3)
i++
switch id {
case 1, 2: // MoveTo, LineTo
if i+2*count > len(cmds) {
return nil, errBadTile
}
for k := 0; k < count; k++ {
x += zig(cmds[i])
y += zig(cmds[i+1])
i += 2
if id == 1 {
if len(cur) >= 2 {
parts = append(parts, cur)
}
cur = [][2]int{at()}
} else {
if p := at(); len(cur) == 0 || cur[len(cur)-1] != p {
cur = append(cur, p)
}
}
}
case 7: // ClosePath, not in a line; ignored
default:
return nil, errBadTile
}
}
if len(cur) >= 2 {
parts = append(parts, cur)
}
return parts, nil
}