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.
Kotlin airtagalprandroidandroid-autoanti-surveillancebluetooth-low-energybody-cameracounter-surveillancedrone-detectionflock-safetyimsi-catcher-detectorpolice-detectorpolice-scannerprivacyremote-idrtl-sdrsdrsurveillance-detectiontracker-detectionwifi
Pagers: report sample stalls, count lone POCSAG batches, larger USB reads
CIS-C04fa23229c07a
diff --git a/app/src/main/java/com/rfsentinel/app/sdr/PagerHunt.kt b/app/src/main/java/com/rfsentinel/app/sdr/PagerHunt.ktindex 09c811b..64af691 100644--- a/app/src/main/java/com/rfsentinel/app/sdr/PagerHunt.kt+++ b/app/src/main/java/com/rfsentinel/app/sdr/PagerHunt.kt@@ -6,12 +6,18 @@ package com.rfsentinel.app.sdr* stays until nothing has decoded for [QUIET_S]. Paging transmitters can be silent between pages, so a channel missed now* shows up on a later look. Frequencies, capcodes and text stay off the log: counts and levels only.*/-class PagerHunt(private val sdr: SdrDevice, private val speeds: () -> Set<Int> = { PagerDemod.SPEEDS }, private val log: (String) -> Unit = {}) {+class PagerHunt(+ private val sdr: SdrDevice,+ private val speeds: () -> Set<Int> = { PagerDemod.SPEEDS },+ xfer: Int = XFER,+ private val onStall: (Boolean) -> Unit = {},+ private val log: (String) -> Unit = {},+) {/** Channels listened to in the last hunt, for the log. */var tried = 0; private set- private val buf = ByteArray(XFER)+ private val buf = ByteArray(xfer)/** The first paging channel that decodes, strongest first, or null. Pages heard on the way go to [onPage]. */fun hunt(stop: () -> Boolean, onPage: (PagerLog.Page, Long) -> Unit): Long? {@@ -49,7 +55,15 @@ class PagerHunt(private val sdr: SdrDevice, private val speeds: () -> Set<Int> =var samples = 0L; var lastGood = 0L; var seen = 0; var fails = 0while (!stop()) {val n = sdr.read(buf)- if (n <= 0) { if (++fails % STALL_READS == 0) sdr.resetBuffer(); continue }+ if (n <= 0) {+ if (++fails % STALL_READS == 0) {+ if (fails <= STALL_READS * 3) log("Pager: no samples for $fails reads in a row - restarting the stream")+ onStall(true)+ sdr.resetBuffer()+ }+ continue+ }+ if (fails >= STALL_READS) onStall(false)fails = 0demod.feed(buf, n / 2); samples += n / 2if (demod.frames > seen) {diff --git a/app/src/main/java/com/rfsentinel/app/sdr/Pocsag.kt b/app/src/main/java/com/rfsentinel/app/sdr/Pocsag.ktindex 77eb98a..dff5b6e 100644--- a/app/src/main/java/com/rfsentinel/app/sdr/Pocsag.kt+++ b/app/src/main/java/com/rfsentinel/app/sdr/Pocsag.kt@@ -57,7 +57,7 @@ internal fun pageText(codes: List<Int>): String = buildString {*/class Pocsag(private val baud: Int, private val onPage: (PagerLog.Page) -> Unit) {- /** Batches that ended in a sync: a real POCSAG signal, even with no page in them. */+ /** Batches that ended in a sync or held a page: a real POCSAG signal. */var batches = 0; private setprivate var reg = 0@@ -88,8 +88,10 @@ class Pocsag(private val baud: Int, private val onPage: (PagerLog.Page) -> Unit)private fun word(w: Int) {if (at == BATCH) {- if (bitCount(w xor SYNC) <= SYNC_ERRORS) { batches++; at = 0; bad = 0 }- else { end(false); synced = false }+ val sync = bitCount(w xor SYNC) <= SYNC_ERRORS+ if (sync) { at = 0; bad = 0 } else { end(false); synced = false }+ // A short page is one batch with no sync after it, but it passed the same checks as any page shown.+ if (sync || pending.isNotEmpty()) batches++pending.forEach(onPage); pending.clear()return}diff --git a/app/src/main/java/com/rfsentinel/app/sdr/SdrRadio.kt b/app/src/main/java/com/rfsentinel/app/sdr/SdrRadio.ktindex e2b8948..590afd7 100644--- a/app/src/main/java/com/rfsentinel/app/sdr/SdrRadio.kt+++ b/app/src/main/java/com/rfsentinel/app/sdr/SdrRadio.kt@@ -32,6 +32,8 @@ object SdrRadio {private const val NET = "net:" // runningDevice of a network SDRprivate const val NET_RETRY_MS = 10_000Lprivate const val XFER = 16_384 // bytes per USB read (8192 samples)+ /** 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 XFERprivate const val READS_PER_CHUNK = 8 // ~32 ms of samples per tuned chunkprivate const val TETRA_READS = 16 // ~64 ms: more than one TETRA frame, so a radio's slot is always in itprivate const val STALL_CHUNKS = 8 // chunks in a row without samples before the stream is restarted@@ -357,8 +359,7 @@ object SdrRadio {sdr.resetBuffer()UsbWifi.log("$deviceLabel: ADS-B receiver on 1090 MHz, ${sdr.sampleRate} S/s, gain ${ADSB_GAIN_TENTHS_DB / 10.0} dB")status = "$deviceLabel · ADS-B · listening on 1090 MHz…" + turnNote()- // One read is ~33 ms of samples; Android before 9 caps a transfer at 16 KB.- val xfer = if (android.os.Build.VERSION.SDK_INT >= 28) 131_072 else XFER+ val xfer = STREAM_XFERval buf = ByteArray(xfer)val mag = IntArray(xfer / 2 + 2 * ModeS.OVERLAP)var have = 0@@ -668,7 +669,11 @@ object SdrRadio {private fun runPager(sdr: SdrDevice, name: String) {sdr.setGain(GAIN_TENTHS_DB)sdr.resetBuffer()- val hunt = PagerHunt(sdr, { appContext?.let { com.rfsentinel.app.util.Prefs.pagerSpeeds(it) } ?: PagerDemod.SPEEDS }) {+ var shown = ""+ fun show(text: String) { shown = text; status = text }+ val hunt = PagerHunt(sdr, { appContext?.let { com.rfsentinel.app.util.Prefs.pagerSpeeds(it) } ?: PagerDemod.SPEEDS }, STREAM_XFER, { stalled ->+ status = if (stalled) "$deviceLabel · pagers · no samples from the SDR · restarting it" + turnNote() else shown+ }) {UsbWifi.log("$deviceLabel: $it")}var lastCheck = System.currentTimeMillis(); var away = false@@ -692,24 +697,24 @@ object SdrRadio {val mine = typed.filter { it == picked }.ifEmpty { typed }if (mine.isNotEmpty()) {val hz = mine[next++ % mine.size]- status = "$deviceLabel · pagers · listening on %.4f MHz".format(java.util.Locale.US, hz / 1e6) + turnNote()+ show("$deviceLabel · pagers · listening on %.4f MHz".format(java.util.Locale.US, hz / 1e6) + turnNote())val from = System.currentTimeMillis()hunt.listen(hz, { stop() || pagerChannel != picked }, onPage)if (System.currentTimeMillis() - from < 1_000L) Thread.sleep(1_000L) // the tuner didn't lock: don't spincontinue}val known = pagerHz- if (known == null) status = "$deviceLabel · pagers · looking for a paging channel…" + turnNote()+ if (known == null) show("$deviceLabel · pagers · looking for a paging channel…" + turnNote())val hz = known ?: hunt.hunt(stop, onPage)if (hz == null) {if (stop()) break- status = "$deviceLabel · pagers · nothing decoding here · looking again shortly" + turnNote()+ show("$deviceLabel · pagers · nothing decoding here · looking again shortly" + turnNote())val until = System.currentTimeMillis() + PAGER_RETRY_MSwhile (!stop() && System.currentTimeMillis() < until) Thread.sleep(200)continue}pagerHz = hz- status = "$deviceLabel · pagers · listening on %.4f MHz".format(java.util.Locale.US, hz / 1e6) + turnNote()+ show("$deviceLabel · pagers · listening on %.4f MHz".format(java.util.Locale.US, hz / 1e6) + turnNote())val before = pagerLog.totalval quiet = hunt.listen(hz, stop, onPage)if (quiet) pagerHz = nulldiff --git a/app/src/test/java/com/rfsentinel/app/sdr/PagerTest.kt b/app/src/test/java/com/rfsentinel/app/sdr/PagerTest.ktindex eacdb81..48876e6 100644--- a/app/src/test/java/com/rfsentinel/app/sdr/PagerTest.kt+++ b/app/src/test/java/com/rfsentinel/app/sdr/PagerTest.kt@@ -270,6 +270,8 @@ class PagerTest {override val tunerWriteFails = 0override val settleReads = 1var reads = 0+ /** The next reads that fail. */+ var stall = 0private var tuned = 0Lprivate var iq = ByteArray(0)private var at = 0@@ -288,6 +290,7 @@ class PagerTest {override fun resetBuffer() {}override fun read(buf: ByteArray, len: Int): Int {reads+++ if (stall > 0) { stall--; return -1 }for (k in 0 until len) {buf[k] = if (iq.isEmpty()) (127.5 + rnd.nextGaussian() * 3).roundToInt().coerceIn(0, 255).toByte() else iq[at]if (iq.isNotEmpty()) at = (at + 1) % iq.size@@ -309,6 +312,23 @@ class PagerTest {assertTrue(pages.any { it.text == "Found you" })}+ @Test fun `listen reports a stall and when the samples come back`() {+ val dongle = FakeDongle(929_612_500L, Fsk())+ val stalls = ArrayList<Boolean>(); val logs = ArrayList<String>()+ val hunt = PagerHunt(dongle, onStall = { stalls += it }, log = { logs += it })+ dongle.stall = 9 // the settle read takes one, then one stall's worth+ hunt.listen(929_612_500L, { dongle.reads > 20 }) { _, _ -> }+ assertEquals(listOf(true, false), stalls)+ assertEquals(1, logs.size)+ }++ @Test fun `a lone batch counts as a frame, so the hunt stays on a channel of short pages`() {+ val words = pocsag(listOf(1234567 to listOf(address(1234567, 1)))).take(17)+ val (pages, frames) = decode(pocsagSignal(1200, words).iq(OFFSET + CARRIER_ERROR_HZ))+ assertEquals(listOf(PagerLog.Page("POCSAG 1200", 1234567, PagerLog.Kind.TONE, "")), pages)+ assertEquals(1, frames)+ }+@Test fun `nothing on the air means the hunt gives up`() {val dongle = FakeDongle(100_000_000L, Fsk())assertNull(PagerHunt(dongle).hunt({ false }) { _, _ -> })