Skip to content

RF Sentinel (RFSentinel): free, open-source police detector app for Android. Spots police body cams, Flock ALPR cameras, AirTag trackers, Remote ID drones, smart glasses and fake cell towers from Bluetooth LE and WiFi. No root.

SDR: mark DMR handsets by their time-slot bursts on the shared bands

CIS-C0e3dd0cf2ad59
diff --git a/app/src/main/java/com/rfsentinel/app/sdr/RadioSettings.kt b/app/src/main/java/com/rfsentinel/app/sdr/RadioSettings.kt
index cda672f..b4cad32 100644
--- a/app/src/main/java/com/rfsentinel/app/sdr/RadioSettings.kt
+++ b/app/src/main/java/com/rfsentinel/app/sdr/RadioSettings.kt
@@ -30,7 +30,8 @@ object RadioSettings {
(if (Prefs.radioTetraOn(context)) listOf(RadioWatch.TETRA_BAND) else emptyList()),
excluded = ranges(Prefs.radioExcluded(context)).map { it.first },
targets = targets(Prefs.radioTargets(context)),
- minSnrDb = Prefs.radioMinSnr(context)
+ minSnrDb = Prefs.radioMinSnr(context),
+ dmr = Prefs.radioDmrOn(context)
)
}
diff --git a/app/src/main/java/com/rfsentinel/app/sdr/RadioWatch.kt b/app/src/main/java/com/rfsentinel/app/sdr/RadioWatch.kt
index 25f3c26..b8c75d9 100644
--- a/app/src/main/java/com/rfsentinel/app/sdr/RadioWatch.kt
+++ b/app/src/main/java/com/rfsentinel/app/sdr/RadioWatch.kt
@@ -23,6 +23,10 @@ import kotlin.math.sin
* handsets transmitting on 380-385 MHz. A TETRA handset sends in time slots: a ~14 ms burst every
* ~57 ms frame. On that band the power is also kept in 2 ms steps ([Bursts]) and a burst rhythm
* marks the signal as a TETRA radio. Still energy only - TETRA can't and won't be decoded here.
+ *
+ * A DMR handset does the same in its own rhythm: a 27.5 ms burst every 60 ms frame, so with [Config.dmr] the
+ * shared bands get that check too. A repeater fills both slots and stays on, so only handsets show it; P25
+ * Phase 2 keeps the same time. NXDN has no time slots, so it stays a plain transmission.
*/
class RadioWatch {
@@ -40,17 +44,24 @@ class RadioWatch {
val excluded: List<LongRange> = emptyList(),
val targets: List<Target> = emptyList(),
/** A transmission counts from this many dB above the noise (lower = more sensitive). */
- val minSnrDb: Int = DEFAULT_MIN_SNR
- )
+ val minSnrDb: Int = DEFAULT_MIN_SNR,
+ /** Also look for DMR time slots on the shared bands (each chunk then takes twice as long). */
+ val dmr: Boolean = false
+ ) {
+ /** Whether [band] gets the time-slot check. */
+ fun slotted(band: Band) = band.kind == Kind.TETRA || (dmr && band.kind == Kind.SHARED)
+ }
/**
* A strong transmission on a normally quiet channel. [repeat]: the same channel again within
* [REPEAT_MS] - not a new alert, but it keeps the live list and the closer / farther trend current.
- * [tdma]: it came in TETRA-like time-slot bursts.
+ * [tdma]: it came in time-slot bursts, named by [slots].
*/
data class Event(val freqHz: Long, val snrDb: Int, val band: Band, val confidence: Int,
val target: Target? = null, val repeat: Boolean = false, val tdma: Boolean = false) {
val mhz: String get() = "%.4f".format(java.util.Locale.US, freqHz / 1e6)
+ /** The radio system its time slots point to; null without them. */
+ val slots: String? get() = if (!tdma) null else if (band.kind == Kind.TETRA) "TETRA" else "DMR"
}
@Volatile var config = Config()
@@ -84,7 +95,7 @@ class RadioWatch {
/**
* One tuned chunk: [psdDb] is the averaged power spectrum (FFT-shifted, DC in the middle)
* at [centerHz] and [sampleRate]. Returns the transmissions worth reporting. [bursts]: the
- * same samples in time steps, for the TETRA band's time-slot check.
+ * same samples in time steps, for the time-slot check.
*/
fun analyze(centerHz: Long, psdDb: FloatArray, sampleRate: Int, now: Long, bursts: Bursts? = null): List<Event> {
val n = psdDb.size
@@ -106,7 +117,7 @@ class RadioWatch {
for (k in mid - half..mid + half) if (k in 0 until n && psdDb[k] > p) p = psdDb[k]
var snr = (p - floor).roundToInt()
var tdma = false
- if (band.kind == Kind.TETRA && bursts != null && bursts.groups.isNotEmpty()) {
+ if (cfg.slotted(band) && bursts != null && bursts.groups.isNotEmpty()) {
// A burst is only on part of the time: its own level, not the average, is the signal.
val levels = FloatArray(bursts.groups.size) { g ->
var q = -200f
@@ -114,7 +125,9 @@ class RadioWatch {
q - floor
}
snr = maxOf(snr, levels.max().roundToInt())
- tdma = isTdma(levels, bursts.groupMs, activeDb(cfg.minSnrDb))
+ val active = activeDb(cfg.minSnrDb)
+ tdma = if (band.kind == Kind.TETRA) isTdma(levels, bursts.groupMs, active)
+ else isTdma(levels, bursts.groupMs, active, DMR_BURST_MS, DMR_FRAME_MS, slots = 1)
}
val ch = channels.getOrPut(f) { Channel() }
val active = snr >= ACTIVE_DB
@@ -269,16 +282,21 @@ class RadioWatch {
/** A TETRA slot: ~14.2 ms on air in a ~56.7 ms frame. */
private const val SLOT_MS = 14.17
private const val FRAME_MS = 56.67
+ /** A DMR handset: 27.5 ms on air in a 60 ms frame. */
+ private const val DMR_BURST_MS = 27.5
+ private const val DMR_FRAME_MS = 60.0
/** Time step of [burstSpectrum]: short enough to see a slot's edges. */
private const val GROUP_MS = 2.0
/**
* Time-slot bursts in one channel's level over time ([levels], dB above the noise, one per
* [groupMs]): on for about one TETRA slot at a time, and truly off in between - not a carrier
- * that stays on, and not noise. Also true for 2 slots of a frame, as a data call may use.
+ * that stays on, and not noise. Also true for up to [slots] slots of a frame, as a TETRA data call may use.
+ * shortcut: one window holds one burst, so a short data burst can pass too; compare windows if that shows up.
*/
- fun isTdma(levels: FloatArray, groupMs: Double, active: Int = ACTIVE_DB): Boolean {
- if (levels.size * groupMs < FRAME_MS * 0.9) return false
+ fun isTdma(levels: FloatArray, groupMs: Double, active: Int = ACTIVE_DB, slotMs: Double = SLOT_MS,
+ frameMs: Double = FRAME_MS, slots: Int = 2): Boolean {
+ if (levels.size * groupMs < frameMs * 0.9) return false
val peak = levels.max()
if (peak < active + 6) return false
val on = BooleanArray(levels.size) { levels[it] >= peak - 10 && levels[it] >= active }
@@ -289,7 +307,7 @@ class RadioWatch {
}
val longestMs = longest * groupMs
return runs >= 1 && quiet >= levels.size / 3 &&
- longestMs >= SLOT_MS * 0.5 && longestMs <= 2 * SLOT_MS + 2 * groupMs &&
+ longestMs >= slotMs * 0.5 && longestMs <= slots * slotMs + 2 * groupMs &&
on.count { it } <= levels.size * 0.6
}
diff --git a/app/src/main/java/com/rfsentinel/app/sdr/SdrRadio.kt b/app/src/main/java/com/rfsentinel/app/sdr/SdrRadio.kt
index d48036f..28e08d6 100644
--- a/app/src/main/java/com/rfsentinel/app/sdr/SdrRadio.kt
+++ b/app/src/main/java/com/rfsentinel/app/sdr/SdrRadio.kt
@@ -36,7 +36,7 @@ object SdrRadio {
/** For continuous decoding: one read is ~33 ms of samples; Android before 9 caps a transfer at 16 KB. */
private val STREAM_XFER = if (android.os.Build.VERSION.SDK_INT >= 28) 131_072 else XFER
private const val READS_PER_CHUNK = 8 // ~32 ms of samples per tuned chunk
- private const val TETRA_READS = 16 // ~64 ms: more than one TETRA frame, so a radio's slot is always in it
+ private const val SLOT_READS = 16 // ~64 ms: more than one TETRA or DMR frame, so a radio's slot is always in it
private const val STALL_CHUNKS = 8 // chunks in a row without samples before the stream is restarted
private const val ADSB_GAIN_TENTHS_DB = 496 // the tuner's most: aircraft are far and faint
private const val ADSB_PUSH_MS = 2_000L
@@ -427,7 +427,7 @@ object SdrRadio {
", reporting from ${cfg.minSnrDb} dB, USB errors ${sdr.ctlErrors}")
logPlan()
val buf = ByteArray(XFER)
- val iq = ByteArray(XFER * maxOf(READS_PER_CHUNK, TETRA_READS))
+ val iq = ByteArray(XFER * maxOf(READS_PER_CHUNK, SLOT_READS))
var lastLog = 0L; var sweepStart = System.currentTimeMillis(); var sweepMs = 0L
var events = 0; var unlocked = 0; var readErrors = 0; var failedChunks = 0
var lastCheck = System.currentTimeMillis()
@@ -445,10 +445,10 @@ object SdrRadio {
}
if (!sdr.tune(center)) { unlocked++; skipped(); continue }
repeat(sdr.settleReads) { sdr.read(buf) }
- // TETRA chunks also keep the power in 2 ms steps, to see a radio's 14 ms time-slot bursts.
- val tetra = cfg.bands.any { it.kind == RadioWatch.Kind.TETRA && RadioWatch.covers(it, center) }
+ // TETRA and DMR chunks also keep the power in 2 ms steps, to see a radio's time-slot bursts.
+ val slotted = cfg.bands.any { cfg.slotted(it) && RadioWatch.covers(it, center) }
var got = 0
- for (i in 0 until if (tetra) TETRA_READS else READS_PER_CHUNK) {
+ for (i in 0 until if (slotted) SLOT_READS else READS_PER_CHUNK) {
val n = sdr.read(buf)
if (n <= 0) { readErrors++; break }
System.arraycopy(buf, 0, iq, got, n); got += n
@@ -463,7 +463,7 @@ object SdrRadio {
continue
}
failedChunks = 0
- val bursts = if (tetra) RadioWatch.burstSpectrum(iq, got / 2, sdr.sampleRate) else null
+ val bursts = if (slotted) RadioWatch.burstSpectrum(iq, got / 2, sdr.sampleRate) else null
val psd = bursts?.psd ?: RadioWatch.powerSpectrum(iq, got / 2)
if (SdrWaterfall.listening) row += SdrWaterfall.segment(center, psd, sdr.sampleRate, RadioWatch.USABLE_HZ,
cfg.bands.firstOrNull { RadioWatch.covers(it, center) }?.label)
@@ -471,7 +471,7 @@ object SdrRadio {
if (!e.repeat) {
events++
UsbWifi.log("$deviceLabel: transmission ${e.mhz} MHz, ${e.snrDb} dB above the noise (${e.band.label})" +
- if (e.tdma) ", TETRA time slots" else "")
+ (e.slots?.let { ", $it time slots" } ?: ""))
}
onEvent?.invoke(e)
}
diff --git a/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt b/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt
index 1729bfd..1778845 100644
--- a/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt
+++ b/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt
@@ -1602,7 +1602,8 @@ class ScanForegroundService : Service() {
else "Emergency-service TETRA handsets transmit on this band, but this signal had no TETRA time slots; a weak sign on its own."
com.rfsentinel.app.sdr.RadioWatch.Kind.TARGET -> "One of the frequencies you watch (Settings > RTL-SDR radio)."
com.rfsentinel.app.sdr.RadioWatch.Kind.CUSTOM -> "A band you added in Settings > RTL-SDR radio."
- else -> "Police use this band, but so do businesses, schools and transit; a weak sign on its own."
+ else -> (if (e.tdma) "It came in time-slot bursts like a DMR handset's (a P25 Phase 2 radio looks the same). Nothing decoded. " else "") +
+ "Police use this band, but so do businesses, schools and transit; a weak sign on its own."
}
val cellDetail = heardBy + "A cellular transmitter is on the air nearby on ${e.mhz} MHz (${e.band.label}), " +
"${e.snrDb} dB above the quiet level - $near. Something is transmitting on the phone / modem side of an " +
@@ -1612,16 +1613,16 @@ class ScanForegroundService : Service() {
val label = when {
cell -> "Cellular transmitter nearby (LTE uplink)"
name != null -> "Radio transmitting: $name"
- e.tdma -> "TETRA radio transmitting nearby"
+ e.tdma -> "${e.slots} radio transmitting nearby"
else -> "Two-way radio transmitting nearby"
} + (via?.let { " · via $it" } ?: "")
val hit = Hit(Category.RADIO, label, e.confidence, if (cell) cellDetail else detail,
- if (e.tdma) "SDR signal strength and TETRA burst timing (no decoding)" else "SDR signal strength (no decoding)")
+ if (e.tdma) "SDR signal strength and ${e.slots} burst timing (no decoding)" else "SDR signal strength (no decoding)")
// Spoken: the frequency (and its name) when that's on, else the short phrase.
val spoken = when {
cell -> "Cellular transmitter nearby"
- Prefs.radioSpeakFreq(this) -> (if (e.tdma) "TETRA radio " else "Radio ") + spokenMhz(e.freqHz) + (name?.let { ", $it" } ?: "")
- e.tdma -> "TETRA radio nearby"
+ Prefs.radioSpeakFreq(this) -> (if (e.tdma) "${e.slots} radio " else "Radio ") + spokenMhz(e.freqHz) + (name?.let { ", $it" } ?: "")
+ e.tdma -> "${e.slots} radio nearby"
else -> "Radio transmitting nearby"
} + (via?.let { ", via $it" } ?: "")
radioDetection(hit, "RADIO " + e.mhz + " MHz", e.snrDb, now, "radio:" + e.freqHz, spoken, e.confidence >= threshold, via)
diff --git a/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.kt b/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.kt
index d2a63ba..3b99b04 100644
--- a/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.kt
+++ b/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.kt
@@ -1772,6 +1772,10 @@ class SettingsActivity : AppCompatActivity() {
"Watches 380-385 MHz, where emergency-service TETRA handsets transmit in Europe and many other countries. " +
"A handset sends short time-slot bursts; that rhythm marks it as a TETRA radio. Energy only: TETRA is encrypted " +
"and nothing is decoded. On by default when the phone's region is in Europe.") { Prefs.setRadioTetraOn(this, it) }
+ box.toggle("Detect DMR radios", Prefs.radioDmrOn(this),
+ "Experimental. On the VHF and UHF bands, marks a handset sending DMR's 60 ms time-slot bursts. Energy only, nothing " +
+ "decoded. Businesses use DMR too, and a P25 Phase 2 radio looks the same, so it is a weak sign on its own. " +
+ "NXDN has no time slots and can't be told apart. Adds time to each sweep.") { Prefs.setRadioDmrOn(this, it) }
slider(0, 20, Prefs.radioTrendDb(this), { if (it == 0) "Closer / moving away: off" else "Say \"getting closer\" / \"moving away\" when the signal changes by $it dB" }) {
Prefs.setRadioTrendDb(this, it)
}
diff --git a/app/src/main/java/com/rfsentinel/app/util/Prefs.kt b/app/src/main/java/com/rfsentinel/app/util/Prefs.kt
index 9c3f712..3bdbcaf 100644
--- a/app/src/main/java/com/rfsentinel/app/util/Prefs.kt
+++ b/app/src/main/java/com/rfsentinel/app/util/Prefs.kt
@@ -648,6 +648,9 @@ object Prefs {
/** Also sweep the cellular UPLINK bands for a transmitter travelling near you (experimental, off by default). */
fun radioCellOn(context: Context): Boolean = bool(context, "radio_cell_on", false)
fun setRadioCellOn(context: Context, value: Boolean) = setBool(context, "radio_cell_on", value)
+ /** Also look for DMR handsets' time slots on the shared bands (experimental, off by default: sweeps take longer). */
+ fun radioDmrOn(context: Context): Boolean = bool(context, "radio_dmr_on", false)
+ fun setRadioDmrOn(context: Context, value: Boolean) = setBool(context, "radio_dmr_on", value)
/** Also sweep the TETRA handset band (380-385 MHz); on by default where the phone's region uses TETRA. */
fun radioTetraOn(context: Context): Boolean = bool(context, "radio_tetra_on", tetraRegion(java.util.Locale.getDefault().country))
fun setRadioTetraOn(context: Context, value: Boolean) = setBool(context, "radio_tetra_on", value)
diff --git a/app/src/test/java/com/rfsentinel/app/sdr/TetraTest.kt b/app/src/test/java/com/rfsentinel/app/sdr/TetraTest.kt
index 2b82f8d..d842468 100644
--- a/app/src/test/java/com/rfsentinel/app/sdr/TetraTest.kt
+++ b/app/src/test/java/com/rfsentinel/app/sdr/TetraTest.kt
@@ -87,6 +87,53 @@ class TetraTest {
assertTrue(RadioWatch.isTdma(FloatArray(32) { if (it in 5..11) 40f else 0f }, 2.0))
}
+ // ---- DMR: the same check on the shared bands, in its own rhythm --------------------------------
+
+ private val uhf = 460_000_000L
+
+ /** A learned watch of the UHF band; a tone at [radio] lands 212.5 kHz above [uhf]. */
+ private fun uhfWatch(dmr: Boolean): Pair<RadioWatch, Long> {
+ val w = RadioWatch()
+ w.config = RadioWatch.Config(bands = listOf(RadioWatch.BANDS[3]), dmr = dmr)
+ var t = 0L
+ repeat(8) { s ->
+ val b = RadioWatch.burstSpectrum(iq(null, seed = s), count, rate)
+ w.analyze(uhf, b.psd, rate, t, b); w.sweepDone(); t += 2_500
+ }
+ return w to t
+ }
+
+ /** One 27.5 ms burst per 60 ms frame, starting [start] ms in. */
+ private fun dmrBurst(start: Double) = { t: Double -> ((t - start) % 60.0 + 60.0) % 60.0 < 27.5 }
+
+ @Test
+ fun aDmrHandsetsBurstsAreMarkedWhereverTheyFallInTheChunk() {
+ for (start in listOf(0.0, 12.0, 25.0, 40.0, 50.0)) {
+ val (w, t) = uhfWatch(dmr = true)
+ val b = RadioWatch.burstSpectrum(iq(radio, on = dmrBurst(start)), count, rate)
+ val ev = w.analyze(uhf, b.psd, rate, t, b)
+ assertTrue("burst at $start ms: $ev", ev.isNotEmpty() && ev.all { it.tdma })
+ assertEquals("DMR", ev.first().slots)
+ assertTrue(ev.all { it.confidence <= 55 }) // still a shared band: a weak sign
+ }
+ }
+
+ @Test
+ fun aRepeaterThatStaysOnIsNotDmr() {
+ val (w, t) = uhfWatch(dmr = true)
+ val b = RadioWatch.burstSpectrum(iq(radio), count, rate)
+ val ev = w.analyze(uhf, b.psd, rate, t, b)
+ assertTrue(ev.isNotEmpty() && ev.none { it.tdma })
+ assertTrue(ev.all { it.slots == null })
+ }
+
+ @Test
+ fun withTheSettingOffNothingIsMarked() {
+ val (w, t) = uhfWatch(dmr = false)
+ val b = RadioWatch.burstSpectrum(iq(radio, on = dmrBurst(10.0)), count, rate)
+ assertTrue(w.analyze(uhf, b.psd, rate, t, b).none { it.tdma })
+ }
+
@Test
fun theBandIsOnlyTheHandsetSide() {
val b = RadioWatch.TETRA_BAND