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.

Pagers: keep a USB read waiting while decoding, so samples aren't lost between reads

CIS-C03aadc5845d88
diff --git a/app/src/main/java/com/rfsentinel/app/sdr/PagerHunt.kt b/app/src/main/java/com/rfsentinel/app/sdr/PagerHunt.kt
index 29a8104..0bb59c5 100644
--- a/app/src/main/java/com/rfsentinel/app/sdr/PagerHunt.kt
+++ b/app/src/main/java/com/rfsentinel/app/sdr/PagerHunt.kt
@@ -54,8 +54,9 @@ class PagerHunt(
val demod = PagerDemod(OFFSET_HZ, speeds()) { onPage(it, hz) }
var samples = 0L; var lastGood = 0L; var seen = 0; var fails = 0
val began = System.nanoTime()
- while (!stop()) {
- val n = sdr.read(buf)
+ val ahead = ReadAhead(sdr, buf.size)
+ try { while (!stop()) {
+ val n = ahead.read(buf)
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")
@@ -73,7 +74,7 @@ class PagerHunt(
if (once) break
}
if (samples - lastGood > quietS * P25Demod.SAMPLE_RATE) break
- }
+ } } finally { ahead.close() }
// Counts and levels only. Under 100%: the dongle dropped samples. A rise with no syncs: a signal that isn't paging, or won't decode.
val secs = (System.nanoTime() - began) / 1e9
if (samples > 0) log("Pager listen: %.0f s, %.0f%% of the samples, channel rose %.0f dB, %d syncs, %d frames".format(
diff --git a/app/src/main/java/com/rfsentinel/app/sdr/ReadAhead.kt b/app/src/main/java/com/rfsentinel/app/sdr/ReadAhead.kt
new file mode 100644
index 0000000..39c87c9
--- /dev/null
+++ b/app/src/main/java/com/rfsentinel/app/sdr/ReadAhead.kt
@@ -0,0 +1,46 @@
+package com.rfsentinel.app.sdr
+
+import java.util.concurrent.ArrayBlockingQueue
+import java.util.concurrent.TimeUnit
+
+/**
+ * Keeps a read waiting on the dongle while the caller decodes the last one. The RTL-SDR's own buffer is small: with read,
+ * decode, read, it overflows during each decode and samples go missing (a tester's log showed 85% arriving), which breaks
+ * every POCSAG codeword that spans a gap. [read] returns what [SdrDevice.read] returned, in order.
+ */
+class ReadAhead(private val sdr: SdrDevice, size: Int, slots: Int = 8) : AutoCloseable {
+ private class Chunk(val buf: ByteArray) { var n = 0 }
+ private val free = ArrayBlockingQueue<Chunk>(slots)
+ private val full = ArrayBlockingQueue<Chunk>(slots)
+ @Volatile private var running = true
+ private val thread: Thread
+
+ init {
+ repeat(slots) { free.add(Chunk(ByteArray(size))) }
+ thread = Thread({
+ try {
+ while (running) {
+ val c = free.take()
+ c.n = sdr.read(c.buf)
+ full.put(c)
+ }
+ } catch (_: InterruptedException) {}
+ }, "sdr-read-ahead").apply { isDaemon = true; start() }
+ }
+
+ /** The next read's bytes, copied into [into]: its byte count, or 0 when nothing came for 2 s. */
+ fun read(into: ByteArray): Int {
+ val c = full.poll(2, TimeUnit.SECONDS) ?: return 0
+ val n = c.n
+ if (n > 0) System.arraycopy(c.buf, 0, into, 0, n)
+ free.put(c)
+ return n
+ }
+
+ /** Waits for the read in flight (up to its USB timeout), so the caller can retune safely. */
+ override fun close() {
+ running = false
+ thread.interrupt()
+ thread.join(2_000)
+ }
+}
diff --git a/app/src/test/java/com/rfsentinel/app/sdr/PagerTest.kt b/app/src/test/java/com/rfsentinel/app/sdr/PagerTest.kt
index 4c5abb3..97766e1 100644
--- a/app/src/test/java/com/rfsentinel/app/sdr/PagerTest.kt
+++ b/app/src/test/java/com/rfsentinel/app/sdr/PagerTest.kt
@@ -270,7 +270,7 @@ class PagerTest {
override val lost = false
override val tunerWriteFails = 0
override val settleReads = 1
- var reads = 0
+ @Volatile var reads = 0
/** The next reads that fail. */
var stall = 0
private var tuned = 0L
diff --git a/app/src/test/java/com/rfsentinel/app/sdr/ReadAheadTest.kt b/app/src/test/java/com/rfsentinel/app/sdr/ReadAheadTest.kt
new file mode 100644
index 0000000..5ac513c
--- /dev/null
+++ b/app/src/test/java/com/rfsentinel/app/sdr/ReadAheadTest.kt
@@ -0,0 +1,42 @@
+package com.rfsentinel.app.sdr
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertTrue
+import org.junit.Test
+
+class ReadAheadTest {
+ /** Read k fills the buffer with k, and every third read fails. */
+ private class Counting : SdrDevice {
+ override val label = "count"; override val sampleRate = 0; override val ctlErrors = 0
+ override val lost = false; override val tunerWriteFails = 0; override val settleReads = 0
+ @Volatile var reads = 0
+ override fun open() = true
+ override fun close() {}
+ override fun setSampleRate(rate: Int) {}
+ override fun tune(hz: Long) = true
+ override fun setGain(tenthsDb: Int) {}
+ override fun resetBuffer() {}
+ override fun read(buf: ByteArray, len: Int): Int {
+ val k = ++reads
+ if (k % 3 == 0) return -1
+ buf.fill(k.toByte(), 0, len)
+ return len - k % 2 // short reads too
+ }
+ }
+
+ @Test fun `reads come back in order with their counts, failures included`() {
+ val sdr = Counting()
+ val into = ByteArray(16)
+ ReadAhead(sdr, 16, slots = 3).use { ahead ->
+ for (k in 1..30) {
+ val n = ahead.read(into)
+ if (k % 3 == 0) assertEquals(-1, n)
+ else { assertEquals(16 - k % 2, n); assertEquals(k.toByte(), into[n - 1]) }
+ }
+ }
+ val after = sdr.reads
+ Thread.sleep(50)
+ assertEquals("the reader stopped on close", after, sdr.reads)
+ assertTrue("it stays only a few reads ahead", after <= 30 + 3 + 1)
+ }
+}