2026-08-10 14:09:18 +02:00
|
|
|
package com.hawhamburg.micr0bu.service
|
|
|
|
|
|
|
|
|
|
import android.content.Context
|
2026-09-10 14:47:30 +02:00
|
|
|
import android.os.SystemClock
|
2026-08-10 14:09:18 +02:00
|
|
|
import com.hawhamburg.micr0bu.data.GnssReading
|
2026-09-10 14:47:30 +02:00
|
|
|
import com.hawhamburg.micr0bu.data.GnssTimeSource
|
2026-08-10 14:09:18 +02:00
|
|
|
import com.hawhamburg.micr0bu.data.SensorRepository
|
2026-09-10 14:47:30 +02:00
|
|
|
import com.hawhamburg.micr0bu.data.transport.GnPositionVector
|
2026-08-10 14:09:18 +02:00
|
|
|
import com.hawhamburg.micr0bu.data.transport.UsbSerialTransport
|
|
|
|
|
import com.hawhamburg.micr0bu.domain.asn1.RealAsn1UperCodec
|
2026-09-02 15:25:31 +02:00
|
|
|
import com.hawhamburg.micr0bu.domain.cam.OwnStationIds
|
2026-08-10 14:09:18 +02:00
|
|
|
import com.hawhamburg.micr0bu.domain.cam.PhoneCamBuilder
|
|
|
|
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
|
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
|
import kotlinx.coroutines.Job
|
|
|
|
|
import kotlinx.coroutines.SupervisorJob
|
|
|
|
|
import kotlinx.coroutines.coroutineScope
|
|
|
|
|
import kotlinx.coroutines.delay
|
|
|
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
|
|
|
|
import kotlinx.coroutines.flow.StateFlow
|
|
|
|
|
import kotlinx.coroutines.flow.asStateFlow
|
|
|
|
|
import kotlinx.coroutines.flow.update
|
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
import javax.inject.Inject
|
|
|
|
|
import javax.inject.Singleton
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Manual bench-test CAM transmitter for the ESP32-C5 path (Phase 03) — a fixed-rate (1 Hz) CAM
|
|
|
|
|
* ping built from the phone's real GNSS and gyroscope, independent of [CamTransmitLoop] and not
|
|
|
|
|
* tied to an active trip recording. Purpose: verify the phone <-> ESP32-C5 serial link and the
|
|
|
|
|
* ESP32's TX/RX radio path end-to-end without needing a full recording session — the CAM
|
|
|
|
|
* equivalent of the CiT One path's manual DENM trigger
|
|
|
|
|
* ([com.hawhamburg.micr0bu.data.mqtt.MqttRepository.activateDenm]).
|
|
|
|
|
*
|
|
|
|
|
* Uses the same [PhoneCamBuilder] as the real transmit path, so what goes on air here is a
|
|
|
|
|
* properly populated CAM — real position, speed, heading, yaw rate and along-track acceleration —
|
|
|
|
|
* not a synthetic frame. Previously this beaconed a hardcoded bench coordinate with speed and
|
|
|
|
|
* heading pinned to zero, which exercised the link but told you nothing about whether the sensor
|
|
|
|
|
* pipeline produced sane CAM content.
|
|
|
|
|
*
|
|
|
|
|
* Requires a GNSS fix: with no fix there is no position to put in a CAM, so the loop sends
|
|
|
|
|
* nothing and reports that via [hasFix] rather than transmitting a placeholder.
|
|
|
|
|
*
|
|
|
|
|
* Entirely user-triggered (Start/Stop in the V2X Monitor screen) — never started automatically,
|
|
|
|
|
* and does not interact with [CamTransmitLoop]'s recording-gated loop. Both could in theory run at
|
|
|
|
|
* once (nothing prevents it); they use distinct station IDs so the two streams stay separable.
|
|
|
|
|
*/
|
|
|
|
|
@Singleton
|
|
|
|
|
class CamPinger @Inject constructor(
|
|
|
|
|
@ApplicationContext private val context: Context,
|
|
|
|
|
private val usbSerialTransport: UsbSerialTransport,
|
|
|
|
|
private val codec: RealAsn1UperCodec,
|
|
|
|
|
) {
|
|
|
|
|
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
|
|
|
|
private val sensorRepository = SensorRepository(context)
|
|
|
|
|
private var job: Job? = null
|
|
|
|
|
|
|
|
|
|
@Volatile private var latestGnss: GnssReading? = null
|
|
|
|
|
@Volatile private var latestGyroZ: Float? = null
|
|
|
|
|
@Volatile private var previousGnss: GnssReading? = null
|
|
|
|
|
|
|
|
|
|
private val _isActive = MutableStateFlow(false)
|
|
|
|
|
val isActive: StateFlow<Boolean> = _isActive.asStateFlow()
|
|
|
|
|
|
|
|
|
|
private val _sentCount = MutableStateFlow(0)
|
|
|
|
|
/** Number of CAM pings sent since [start] was last called. Reset to 0 on each [start]. */
|
|
|
|
|
val sentCount: StateFlow<Int> = _sentCount.asStateFlow()
|
|
|
|
|
|
|
|
|
|
private val _hasFix = MutableStateFlow(false)
|
|
|
|
|
/** False while the pinger is running but has no GNSS fix yet — nothing is being transmitted. */
|
|
|
|
|
val hasFix: StateFlow<Boolean> = _hasFix.asStateFlow()
|
|
|
|
|
|
2026-09-10 14:47:30 +02:00
|
|
|
/** [SystemClock.elapsedRealtime] when the pinger last stopped, or null if it never ran. */
|
|
|
|
|
@Volatile private var stoppedAtElapsedMs: Long? = null
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* True while station [OwnStationIds.BENCH_PING] on air is this phone's own ping: while the
|
|
|
|
|
* pinger runs, and briefly after it stops, so a frame sent just before Stop is not taken for a
|
|
|
|
|
* stranger. Uses elapsed realtime, so changing the wall clock cannot move the window.
|
|
|
|
|
*
|
|
|
|
|
* Otherwise that ID belongs to someone else, typically another MicrOBU phone pinging on the
|
|
|
|
|
* same bench, and must be shown like any remote station. See [OwnStationIds.benchPingIsOurs].
|
|
|
|
|
*/
|
|
|
|
|
fun benchPingIsOurs(): Boolean =
|
|
|
|
|
OwnStationIds.benchPingIsOurs(_isActive.value, stoppedAtElapsedMs, SystemClock.elapsedRealtime())
|
|
|
|
|
|
2026-08-10 14:09:18 +02:00
|
|
|
fun start() {
|
|
|
|
|
if (job?.isActive == true) return
|
|
|
|
|
_sentCount.value = 0
|
|
|
|
|
_hasFix.value = false
|
|
|
|
|
previousGnss = null
|
|
|
|
|
latestGnss = null
|
|
|
|
|
_isActive.value = true
|
|
|
|
|
job = scope.launch { runPingLoop() }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private suspend fun runPingLoop() = coroutineScope {
|
|
|
|
|
launch { sensorRepository.gnssFlow().collect { latestGnss = it } }
|
|
|
|
|
launch { sensorRepository.gyroscopeFlow().collect { latestGyroZ = it.z } }
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
val gnss = latestGnss
|
|
|
|
|
_hasFix.value = gnss != null
|
|
|
|
|
if (gnss != null) {
|
|
|
|
|
val cam = PhoneCamBuilder.build(
|
2026-09-10 14:47:30 +02:00
|
|
|
// Stamped on GNSS time rather than the phone clock; see GnssTimeSource.
|
|
|
|
|
gnss = gnss.copy(timestamp = GnssTimeSource.correct(gnss.timestamp)),
|
2026-08-10 14:09:18 +02:00
|
|
|
gyroZRadPerSec = latestGyroZ,
|
2026-09-02 15:25:31 +02:00
|
|
|
stationId = OwnStationIds.BENCH_PING,
|
2026-08-10 14:09:18 +02:00
|
|
|
longitudinalAccelMps2 = longitudinalAccel(gnss),
|
|
|
|
|
)
|
|
|
|
|
val bytes = codec.encodeCam(cam)
|
2026-09-10 14:47:30 +02:00
|
|
|
// Fixed bench identity on every layer, the link-layer address included, so a ping
|
|
|
|
|
// stays recognisable in a capture and never rotates.
|
|
|
|
|
val pv = GnPositionVector.fromCam(cam, gnss.accuracyM, OwnStationIds.BENCH_PING_MAC)
|
|
|
|
|
if (usbSerialTransport.sendCamTx(bytes, pv)) {
|
2026-08-10 14:09:18 +02:00
|
|
|
_sentCount.update { it + 1 }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
delay(PING_INTERVAL_MS)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Along-track acceleration from successive GNSS speed samples — same derivation and same
|
|
|
|
|
* reasoning as [CamTransmitLoop.longitudinalAccel] (the accelerometer reads in the device
|
|
|
|
|
* frame with gravity mixed in, so it can't give signed along-track acceleration without a
|
|
|
|
|
* full orientation estimate). Null outside a usable sample gap, which encodes as the ASN.1
|
|
|
|
|
* `unavailable` sentinel.
|
|
|
|
|
*/
|
|
|
|
|
private fun longitudinalAccel(gnss: GnssReading): Double? {
|
|
|
|
|
val prev = previousGnss
|
|
|
|
|
previousGnss = gnss
|
|
|
|
|
if (prev == null) return null
|
|
|
|
|
|
|
|
|
|
val dtSec = (gnss.timestamp - prev.timestamp) / 1000.0
|
|
|
|
|
if (dtSec < MIN_ACCEL_DT_SEC || dtSec > MAX_ACCEL_DT_SEC) return null
|
|
|
|
|
|
|
|
|
|
return (gnss.speedMs.toDouble() - prev.speedMs.toDouble()) / dtSec
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun stop() {
|
2026-09-10 14:47:30 +02:00
|
|
|
// Only a real stop opens the grace window. stop() is also called unconditionally on
|
|
|
|
|
// teardown, and that must not make a phone that never pinged claim 999999 for a while.
|
|
|
|
|
if (_isActive.value) stoppedAtElapsedMs = SystemClock.elapsedRealtime()
|
2026-08-10 14:09:18 +02:00
|
|
|
job?.cancel()
|
|
|
|
|
job = null
|
|
|
|
|
_isActive.value = false
|
|
|
|
|
_hasFix.value = false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
private const val PING_INTERVAL_MS = 1_000L
|
|
|
|
|
|
|
|
|
|
private const val MIN_ACCEL_DT_SEC = 0.2
|
|
|
|
|
private const val MAX_ACCEL_DT_SEC = 3.0
|
|
|
|
|
|
2026-09-02 15:25:31 +02:00
|
|
|
// The station id these pings go out under lives in
|
|
|
|
|
// [com.hawhamburg.micr0bu.domain.cam.OwnStationIds.BENCH_PING], not here. It is not a
|
|
|
|
|
// private detail of this class: the ESP32 hears these frames back off the air, so the
|
|
|
|
|
// receive path has to recognise the same value, and a second copy of it is exactly how
|
|
|
|
|
// the two sides would drift apart.
|
2026-08-10 14:09:18 +02:00
|
|
|
}
|
|
|
|
|
}
|