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.

P25: alert on unusual talkgroup activity against the call log's baseline (off by default)

CIS-C02402d9723984
diff --git a/app/src/main/java/com/rfsentinel/app/sdr/P25History.kt b/app/src/main/java/com/rfsentinel/app/sdr/P25History.kt
index 543b4f0..b943b52 100644
--- a/app/src/main/java/com/rfsentinel/app/sdr/P25History.kt
+++ b/app/src/main/java/com/rfsentinel/app/sdr/P25History.kt
@@ -24,8 +24,17 @@ object P25History {
val busiestHour get() = byHour.indices.maxByOrNull { byHour[it] }?.takeIf { byHour[it] > 0 }
}
+ /** A talkgroup much busier than usual: [recent] calls in the last hour against [usual], its average for that same
+ * hour over the [days] before. [new]: never heard before in the kept log. */
+ data class Unusual(val tg: Int, val recent: Int, val usual: Double, val days: Int, val new: Boolean)
+
const val HEADER = "time,talkgroup,private_to,radio,channel,frequency_hz,emergency,encrypted,site"
private const val DAY_MS = 24 * 60 * 60_000L
+ private const val HOUR_MS = 60 * 60_000L
+ /** Days of log needed before anything counts as unusual. */
+ const val BASELINE_DAYS = 3
+ /** Calls in the last hour below this are never unusual, however quiet the talkgroup usually is. */
+ private const val MIN_RECENT = 5
private const val PRUNE_EVERY_MS = 60 * 60_000L
private val io = Executors.newSingleThreadExecutor()
@@ -71,6 +80,24 @@ object P25History {
f[4].toIntOrNull(), f[5].toLongOrNull(), f[6] == "1", f[7] == "1", f[8].ifEmpty { null })
}
+ /**
+ * Talkgroups at least 3 times (plus 3 calls) busier in the last hour than in the same hour of the previous days.
+ * Nothing until the log covers [BASELINE_DAYS] days. Private calls are left out.
+ */
+ fun unusual(calls: List<Call>, now: Long): List<Unusual> {
+ val oldest = calls.minOfOrNull { it.time } ?: return emptyList()
+ val days = ((now - oldest) / DAY_MS).toInt()
+ if (days < BASELINE_DAYS) return emptyList()
+ return calls.filter { it.tg != null }.groupBy { it.tg!! }.mapNotNull { (tg, list) ->
+ val recent = list.count { now - it.time in 0 until HOUR_MS }
+ if (recent < MIN_RECENT) return@mapNotNull null
+ val before = (1..days).sumOf { d -> list.count { now - d * DAY_MS - it.time in 0 until HOUR_MS } }
+ val usual = before.toDouble() / days
+ if (recent < 3 * usual + 3) null
+ else Unusual(tg, recent, usual, days, list.none { now - it.time >= HOUR_MS })
+ }.sortedByDescending { it.recent }
+ }
+
/** Talkgroups (and private calls) by most recent call, counted over the last day, week and everything kept. */
fun summarize(calls: List<Call>, now: Long): List<Summary> = calls.groupBy { it.tg to (if (it.tg == null) it.target else null) }
.map { (k, list) ->
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 f68ff36..67fd503 100644
--- a/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt
+++ b/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt
@@ -75,6 +75,11 @@ class ScanForegroundService : Service() {
private const val RADAR_HOLD_MS = 120_000L
/** A watched talkgroup's call: the user asked for it, like a watched frequency. */
private const val P25_CONFIDENCE = 70
+ /** A talkgroup much busier than usual: probable, not certain (an event, a shift change, a drill). */
+ private const val P25_UNUSUAL_CONFIDENCE = 55
+ private const val P25_UNUSUAL_CHECK_MS = 5 * 60_000L
+ /** One unusual-activity alert per talkgroup this often. */
+ private const val P25_UNUSUAL_AGAIN_MS = 2 * 60 * 60_000L
/** "GPS connected" again only after the fix was lost this long. */
private const val GPS_RECONNECT_MS = 120_000L
/** The latest position's time and accuracy, for the GPS dot (null: none while scanning). */
@@ -1537,6 +1542,7 @@ class ScanForegroundService : Service() {
/** A call started on a P25 talkgroup: reported when it is one you watch (Settings > RTL-SDR radio). */
private fun onP25Call(tg: Int) {
if (!Prefs.categoryEnabled(this, Category.RADIO)) return
+ checkP25Unusual()
val name = com.rfsentinel.app.sdr.P25Log.watched(Prefs.p25Talkgroups(this))[tg] ?: return
val row = com.rfsentinel.app.sdr.SdrRadio.p25Log.rows().firstOrNull { it.tg == tg }
val called = name.ifEmpty { "talkgroup $tg" }
@@ -1550,6 +1556,35 @@ class ScanForegroundService : Service() {
P25_CONFIDENCE >= Prefs.alertThreshold(this, Category.RADIO))
}
+ private var p25UnusualAt = 0L
+ private val p25UnusualAlerted = HashMap<Int, Long>()
+
+ /** Every few minutes while calls come in: talkgroups much busier than usual for the hour (Settings › RTL-SDR radio). */
+ private fun checkP25Unusual() {
+ val now = System.currentTimeMillis()
+ if (!Prefs.p25Unusual(this) || Prefs.p25LogDays(this) == 0 || now - p25UnusualAt < P25_UNUSUAL_CHECK_MS) return
+ p25UnusualAt = now
+ serviceScope.launch(kotlinx.coroutines.Dispatchers.IO) {
+ val found = com.rfsentinel.app.sdr.P25History.unusual(com.rfsentinel.app.sdr.P25History.read(this@ScanForegroundService), now)
+ if (found.isNotEmpty()) pipeline.post { found.forEach { onP25Unusual(it, now) } }
+ }
+ }
+
+ private fun onP25Unusual(u: com.rfsentinel.app.sdr.P25History.Unusual, now: Long) {
+ if (now - (p25UnusualAlerted[u.tg] ?: 0L) < P25_UNUSUAL_AGAIN_MS) return
+ p25UnusualAlerted[u.tg] = now
+ val name = com.rfsentinel.app.sdr.P25Log.watched(Prefs.p25Talkgroups(this))[u.tg]?.ifEmpty { null }
+ val called = name ?: "talkgroup ${u.tg}"
+ val detail = "P25 talkgroup ${u.tg}${name?.let { " ($it)" }.orEmpty()}: ${u.recent} calls in the last hour" +
+ (if (u.new) ", never heard before in ${u.days} days of log. "
+ else String.format(java.util.Locale.US, ", against %.1f on average at this hour over the last %d days. ", u.usual, u.days)) +
+ "Busier than usual can mean an incident, an event or a planned operation. Heard on the site's control channel: " +
+ "the radios may be anywhere the site covers."
+ val hit = Hit(Category.RADIO, "Unusual activity: $called", P25_UNUSUAL_CONFIDENCE, detail, "P25 call log (signalling only, no voice)")
+ radioDetection(hit, "P25 TG ${u.tg}", 0, detail, now, "p25u:${u.tg}", "Unusual activity on $called",
+ P25_UNUSUAL_CONFIDENCE >= Prefs.alertThreshold(this, Category.RADIO))
+ }
+
/** "154.43" for 154.430 MHz: three decimals, trailing zeros dropped, read well by the voice. */
private fun spokenMhz(hz: Long): String =
"%.3f".format(java.util.Locale.US, hz / 1e6).trimEnd('0').trimEnd('.')
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 c8ce4b8..dab3ec8 100644
--- a/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.kt
+++ b/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.kt
@@ -1386,7 +1386,11 @@ class SettingsActivity : AppCompatActivity() {
info(box.getChildAt(box.childCount - 2), "Every call the control channel grants (time, talkgroup, radio, channel) is kept on the " +
"phone this long, for P25 talkgroups › History and Export: no internet or talkgroup list needed. About 1 MB per busy day. " +
"0 keeps nothing and deletes the log.", "Keep the P25 call log")
- val talkgroupViews = (box.childCount - 4 until box.childCount).map { box.getChildAt(it) }
+ scan("Alert on unusual talkgroup activity", Prefs.p25Unusual(this),
+ "Compares each talkgroup's calls in the last hour with the same hour on the days before, from the call log. Alerts when " +
+ "one is at least three times busier than usual, or busy for the first time. Starts after the log has " +
+ "${com.rfsentinel.app.sdr.P25History.BASELINE_DAYS} days in it, and needs the call log kept.") { Prefs.setP25Unusual(this, it) }
+ val talkgroupViews = (box.childCount - 5 until box.childCount).map { box.getChildAt(it) }
var sweepViews = emptyList<View>() // the sweep's own settings, below
showSdrOptions = {
val ticked = listOf(sweepBox, adsbBox, p25Box).count { it.isChecked }
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 832cf39..578a59b 100644
--- a/app/src/main/java/com/rfsentinel/app/util/Prefs.kt
+++ b/app/src/main/java/com/rfsentinel/app/util/Prefs.kt
@@ -651,6 +651,9 @@ object Prefs {
/** How many days the P25 call log is kept on the phone (0: not kept). */
fun p25LogDays(context: Context): Int = int(context, "p25_log_days", 14).coerceIn(0, 60)
fun setP25LogDays(context: Context, days: Int) = setInt(context, "p25_log_days", days.coerceIn(0, 60))
+ /** Alert when a talkgroup is much busier than usual for the hour ([com.rfsentinel.app.sdr.P25History.unusual]). Off by default. */
+ fun p25Unusual(context: Context): Boolean = bool(context, "p25_unusual", false)
+ fun setP25Unusual(context: Context, v: Boolean) = setBool(context, "p25_unusual", v)
/** Seconds the SDR gives each ticked scan in turn, 30-300: a first P25 search takes up to about 15 s. */
fun sdrTurnS(context: Context): Int = int(context, "sdr_turn_s", 60).coerceIn(30, 300)
fun setSdrTurnS(context: Context, v: Int) = setInt(context, "sdr_turn_s", v.coerceIn(30, 300))
diff --git a/app/src/test/java/com/rfsentinel/app/sdr/P25HistoryTest.kt b/app/src/test/java/com/rfsentinel/app/sdr/P25HistoryTest.kt
index 62df401..7234e88 100644
--- a/app/src/test/java/com/rfsentinel/app/sdr/P25HistoryTest.kt
+++ b/app/src/test/java/com/rfsentinel/app/sdr/P25HistoryTest.kt
@@ -26,4 +26,22 @@ class P25HistoryTest {
assertEquals(777, s.first { it.tg == null }.target)
assertEquals(1234, s.first().tg) // most recent call first
}
+
+ @Test
+ fun unusualNeedsABaselineAndAClearRise() {
+ val day = 24 * 60 * 60_000L
+ val now = 20 * day
+ fun call(t: Long, tg: Int) = P25History.Call(t, tg, null, 1, null, null, emergency = false, encrypted = false, site = null)
+ // Talkgroup 100: 2 calls in this hour on each of the 4 days before, 12 now. Talkgroup 200: as busy as ever.
+ val past = (1..4).flatMap { d -> listOf(call(now - d * day - 60_000, 100), call(now - d * day - 120_000, 100)) +
+ (1..6).map { call(now - d * day - it * 60_000L, 200) } }
+ val nowCalls = (1..12).map { call(now - it * 60_000L, 100) } + (1..6).map { call(now - it * 60_000L, 200) } +
+ (1..5).map { call(now - it * 60_000L, 300) }
+ val u = P25History.unusual(past + nowCalls, now)
+ assertEquals(listOf(100, 300), u.map { it.tg })
+ assertEquals(2.0, u[0].usual, 0.001)
+ assertTrue(!u[0].new && u[1].new)
+ // Under 3 days of log: no baseline yet.
+ assertTrue(P25History.unusual(nowCalls + call(now - 2 * day, 100), now).isEmpty())
+ }
}