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
P25 voice step 2: decode voice frames (LDU1/LDU2) to IMBE codewords
CIS-C0390e5b833672
diff --git a/app/src/main/java/com/rfsentinel/app/sdr/P25Demod.kt b/app/src/main/java/com/rfsentinel/app/sdr/P25Demod.ktindex 6b9bb14..7f765ee 100644--- a/app/src/main/java/com/rfsentinel/app/sdr/P25Demod.kt+++ b/app/src/main/java/com/rfsentinel/app/sdr/P25Demod.kt@@ -15,9 +15,10 @@ import kotlin.math.sqrt* sliced and handed to [onFrame] (status symbols still in: the frame decoder takes them out).* A slow frequency loop keeps the channel centred while the dongle's crystal drifts.*- * Signalling only: it never demodulates voice. Pure (no Android): unit-tested on a synthetic signal.+ * [frameDibits] longer than the default takes in a whole voice frame ([P25Voice]). Pure (no Android): unit-tested on+ * a synthetic signal.*/-class P25Demod(offsetHz: Double, private val onFrame: (IntArray) -> Unit) {+class P25Demod(offsetHz: Double, private val frameDibits: Int = FRAME_DIBITS, private val onFrame: (IntArray) -> Unit) {/** Where the channel is, from the tuned centre; the frequency loop moves it while it tracks. */var offsetHz = offsetHz; private set@@ -44,8 +45,8 @@ class P25Demod(offsetHz: Double, private val onFrame: (IntArray) -> Unit) {private val box = DoubleArray(SPS); private var boxAt = 0; private var boxSum = 0.0private val hist = DoubleArray(HIST); private var n = 0L- private class Collect(val start: Long, val dc: Double, val level: Double) {- val dibits = IntArray(FRAME_DIBITS); var got = 0+ private class Collect(val start: Long, val dc: Double, val level: Double, size: Int) {+ val dibits = IntArray(size); var got = 0}private val collecting = ArrayList<Collect>()private var lastSync = Long.MIN_VALUE / 2@@ -71,6 +72,9 @@ class P25Demod(offsetHz: Double, private val onFrame: (IntArray) -> Unit) {}}+ /** One sample of the channel already mixed down and at [CHANNEL_RATE], as [P25Record] saves it. */+ fun feedChannel(r: Double, i: Double) = channelSample(r, i)+private fun channelSample(r: Double, i: Double) {firR[firAt] = r; firI[firAt] = ivar yr = 0.0; var yi = 0.0; var j = firAt@@ -104,7 +108,7 @@ class P25Demod(offsetHz: Double, private val onFrame: (IntArray) -> Unit) {val c = it.next()if ((n - c.start) % SPS != 0L) continuec.dibits[c.got++] = slice((v - c.dc) / c.level)- if (c.got == FRAME_DIBITS) { it.remove(); onFrame(c.dibits) }+ if (c.got == c.dibits.size) { it.remove(); onFrame(c.dibits) }}}// Sync search: the last 24 symbols against the pattern, normalized.@@ -136,10 +140,10 @@ class P25Demod(offsetHz: Double, private val onFrame: (IntArray) -> Unit) {val level = (hi - lo) / 2if (level <= 0) returnsyncs++- val c = Collect(end, (hi + lo) / 2, level)+ val c = Collect(end, (hi + lo) / 2, level, frameDibits)// Samples already past since the sync's end: catch up those dibits from the history.var t = end + SPS- while (t <= n && c.got < FRAME_DIBITS) { c.dibits[c.got++] = slice((at((n - t).toInt()) - c.dc) / c.level); t += SPS }+ while (t <= n && c.got < frameDibits) { c.dibits[c.got++] = slice((at((n - t).toInt()) - c.dc) / c.level); t += SPS }collecting += c}diff --git a/app/src/main/java/com/rfsentinel/app/sdr/P25Voice.kt b/app/src/main/java/com/rfsentinel/app/sdr/P25Voice.ktnew file mode 100644index 0000000..0d215a8--- /dev/null+++ b/app/src/main/java/com/rfsentinel/app/sdr/P25Voice.kt@@ -0,0 +1,167 @@+package com.rfsentinel.app.sdr++/**+ * P25 Phase 1 voice, second step: a voice frame (LDU1 / LDU2, after its sync: [P25Demod] with [LDU_DIBITS]) to its+ * nine IMBE voice codewords, error-corrected. Each 144-bit codeword is de-interleaved into eight words; the first+ * (Golay 23,12) is corrected and seeds a PN sequence that unscrambles the next six; those are corrected (three+ * Golay 23,12, three Hamming 15,11) and the last 7 bits are taken as they are: 88 bits for the IMBE decoder.+ * Link control / encryption sync sit between the codewords; from LDU2 the algorithm ID says whether the call is clear.+ * Layout and tables as in DSD and mbelib (ISC licence), after TIA-102.BAAA and BABA. Pure: unit-tested.+ */+object P25Voice {++ const val DUID_LDU1 = 0x5+ const val DUID_LDU2 = 0xA+ /** An LDU after its sync: NID, nine codewords, LC / ES, low speed data, status symbols. */+ const val LDU_DIBITS = 840+ /** The algorithm ID of a clear (not encrypted) call. */+ const val ALGID_CLEAR = 0x80++ /** One codeword: [bits] its 88 info bits (0 / 1), [errors] bits corrected, [first] of them in the first word. */+ class Imbe(val bits: ByteArray, val errors: Int, val first: Int)++ /** A voice frame: its codewords, and [algid] from an LDU2 (null from an LDU1, or when damaged). */+ class Frame(val nid: P25Frame.Nid, val voice: List<Imbe>, val algid: Int?) {+ val encrypted get() = algid != null && algid != ALGID_CLEAR+ }++ /** Where the nine codewords start, in dibits after the NID's first (status symbols out). */+ internal val VOICE_AT = intArrayOf(32, 104, 196, 288, 380, 472, 564, 656, 744)+ /** The LDU2's 4th group of hex words, after codeword 5: the algorithm ID is in its first two. */+ private const val ALGID_AT = 452++ /** A voice frame, or null when it isn't one (or its NID is beyond repair). */+ fun decode(frame: IntArray): Frame? {+ val d = P25Frame.stripStatus(frame)+ val nid = P25Frame.nid(d) ?: return null+ if (nid.duid != DUID_LDU1 && nid.duid != DUID_LDU2 || d.size < VOICE_AT.last() + 72) return null+ val algid = if (nid.duid != DUID_LDU2) null else {+ val a = hex(d, ALGID_AT); val b = hex(d, ALGID_AT + 5)+ if (a == null || b == null) null else a shl 2 or (b shr 4)+ }+ return Frame(nid, VOICE_AT.map { imbe(d, it) }, algid)+ }++ /** A hex word (6 bits + 4 Hamming 10,6 parity, 5 dibits) at [at], corrected; null with more than one error. */+ internal fun hex(d: IntArray, at: Int): Int? {+ var v = 0+ for (i in 0 until 5) v = v shl 2 or d[at + i]+ val syn = hexParity(v shr 4) xor (v and 0xf)+ if (syn == 0) return v shr 4+ val bit = HEX_SYNDROME[syn]+ return if (bit < 0) null else (v xor (1 shl bit)) shr 4+ }++ /** Hamming(10,6,3) parity of 6 data bits (TIA-102.BAAA; rows of G after its identity part). */+ internal fun hexParity(data: Int): Int {+ var p = 0+ for (i in 0 until 6) if (data shr (5 - i) and 1 == 1) p = p xor HEX_G[i]+ return p+ }+ private val HEX_G = intArrayOf(0b1110, 0b1101, 0b1011, 0b0111, 0b0011, 0b1100)+ private val HEX_SYNDROME = IntArray(16) { -1 }.also { t ->+ for (bit in 0 until 10) { val e = 1 shl bit; t[hexParity(e shr 4) xor (e and 0xf)] = bit }+ }++ // ---- IMBE codewords ----++ /** The 88 info bits of the codeword at dibit [at]. */+ internal fun imbe(d: IntArray, at: Int): Imbe {+ val fr = Array(8) { IntArray(23) }+ for (k in 0 until 72) {+ val v = d[at + k]+ fr[IW[k]][IX[k]] = v shr 1 and 1+ fr[IY[k]][IZ[k]] = v and 1+ }+ val w = IntArray(8) { c -> var x = 0; for (j in SIZE[c] - 1 downTo 0) x = x shl 1 or fr[c][j]; x }+ val (u0, e0) = golay(w[0])+ val pn = pn(u0)+ var k = 0+ for (c in 1..6) for (j in SIZE[c] - 1 downTo 0) { if (pn[k++] == 1) w[c] = w[c] xor (1 shl j) }+ val u = IntArray(8); u[0] = u0+ var errors = e0+ for (c in 1..3) golay(w[c]).let { (v, e) -> u[c] = v; errors += e }+ for (c in 4..6) hamming(w[c]).let { (v, e) -> u[c] = v; errors += e }+ u[7] = w[7]+ val bits = ByteArray(88)+ var b = 0+ for (c in 0 until 8) for (j in INFO[c] - 1 downTo 0) bits[b++] = (u[c] shr j and 1).toByte()+ return Imbe(bits, errors, e0)+ }++ /** The scrambling sequence for words 1-6, from word 0's 12 info bits (114 bits, 0 / 1). */+ internal fun pn(u0: Int): IntArray {+ var p = 16 * u0+ return IntArray(114) { p = (173 * p + 13849) and 0xffff; p shr 15 }+ }++ /** Word sizes in bits, and their info bits. */+ internal val SIZE = intArrayOf(23, 23, 23, 23, 15, 15, 15, 7)+ internal val INFO = intArrayOf(12, 12, 12, 12, 11, 11, 11, 7)++ /** Golay(23,12) parity of 12 data bits: data bit i (MSB first) adds GOLAY_G[i]. */+ internal fun golayParity(data: Int): Int {+ var p = 0+ for (i in 0 until 12) if (data shr (11 - i) and 1 == 1) p = p xor GOLAY_G[i]+ return p+ }+ private val GOLAY_G = intArrayOf(0x63a, 0x31d, 0x7b4, 0x3da, 0x1ed, 0x6cc, 0x366, 0x1b3, 0x6e3, 0x54b, 0x49f, 0x475)+ private fun golaySyndrome(w: Int) = golayParity(w shr 11) xor (w and 0x7ff)+ /** Syndrome → the error pattern (up to 3 bits): the code is perfect, every syndrome has one. */+ private val GOLAY_ERR: IntArray by lazy {+ val t = IntArray(2048)+ for (a in 0 until 23) for (b in a until 23) for (c in b until 23) {+ val e = (1 shl a) or (1 shl b) or (1 shl c) // a == b == c is one bit, a == b < c two+ t[golaySyndrome(e)] = e+ }+ t+ }++ /** A Golay(23,12) word corrected: its 12 info bits and the bits fixed. */+ internal fun golay(w: Int): Pair<Int, Int> {+ val e = GOLAY_ERR[golaySyndrome(w)]+ return ((w xor e) shr 11) to Integer.bitCount(e)+ }++ /** Hamming(15,11) parity checks (mbelib): codeword bit 14 first, the 4 parity bits last. */+ private val HAMMING_H = intArrayOf(0x7f08, 0x78e4, 0x66d2, 0x55b1)+ internal fun hammingSyndrome(w: Int): Int { var s = 0; for (h in HAMMING_H) s = s shl 1 or (Integer.bitCount(w and h) and 1); return s }+ private val HAMMING_ERR = IntArray(16).also { t -> for (bit in 0 until 15) t[hammingSyndrome(1 shl bit)] = 1 shl bit }++ /** A Hamming(15,11) word corrected: its 11 info bits and the bits fixed. */+ internal fun hamming(w: Int): Pair<Int, Int> {+ val e = HAMMING_ERR[hammingSyndrome(w)]+ return ((w xor e) shr 4) to Integer.bitCount(e)+ }++ // The interleave: dibit k's first bit goes to word IW[k] bit IX[k], its second to IY[k] bit IZ[k] (DSD).+ internal val IW = intArrayOf(0, 2, 4, 1, 3, 5, 0, 2, 4, 1, 3, 6, 0, 2, 4, 1, 3, 6, 0, 2, 4, 1, 3, 6, 0, 2, 4, 1, 3, 6,+ 0, 2, 4, 1, 3, 6, 0, 2, 5, 1, 3, 6, 0, 2, 5, 1, 3, 6, 0, 2, 5, 1, 3, 7, 0, 2, 5, 1, 3, 7, 0, 2, 5, 1, 4, 7, 0, 3, 5, 2, 4, 7)+ internal val IX = intArrayOf(22, 20, 10, 20, 18, 0, 20, 18, 8, 18, 16, 13, 18, 16, 6, 16, 14, 11, 16, 14, 4, 14, 12, 9,+ 14, 12, 2, 12, 10, 7, 12, 10, 0, 10, 8, 5, 10, 8, 13, 8, 6, 3, 8, 6, 11, 6, 4, 1, 6, 4, 9, 4, 2, 6, 4, 2, 7, 2, 0, 4,+ 2, 0, 5, 0, 13, 2, 0, 21, 3, 21, 11, 0)+ internal val IY = intArrayOf(1, 3, 5, 0, 2, 4, 1, 3, 6, 0, 2, 4, 1, 3, 6, 0, 2, 4, 1, 3, 6, 0, 2, 4, 1, 3, 6, 0, 2, 4,+ 1, 3, 6, 0, 2, 5, 1, 3, 6, 0, 2, 5, 1, 3, 6, 0, 2, 5, 1, 3, 6, 0, 2, 5, 1, 3, 7, 0, 2, 5, 1, 4, 7, 0, 3, 5, 2, 4, 7, 1, 3, 5)+ internal val IZ = intArrayOf(21, 19, 1, 21, 19, 9, 19, 17, 14, 19, 17, 7, 17, 15, 12, 17, 15, 5, 15, 13, 10, 15, 13, 3,+ 13, 11, 8, 13, 11, 1, 11, 9, 6, 11, 9, 14, 9, 7, 4, 9, 7, 12, 7, 5, 2, 7, 5, 10, 5, 3, 0, 5, 3, 8, 3, 1, 5, 3, 1, 6,+ 1, 14, 3, 1, 22, 4, 22, 12, 1, 22, 20, 2)++ // ---- recordings ----++ /** What a [P25Record] recording held: its voice frames, and how many frame syncs were found. */+ class Decoded(val frames: List<Frame>, val syncs: Int)++ /** Runs a recording (2-channel 16-bit WAV at [P25Record.RATE]) through the demodulator and this decoder. */+ fun decodeRecording(wav: ByteArray): Decoded {+ val b = java.nio.ByteBuffer.wrap(wav).order(java.nio.ByteOrder.LITTLE_ENDIAN)+ // The data chunk: after the 12-byte RIFF header, chunk by chunk.+ var at = 12+ while (at + 8 <= wav.size && String(wav, at, 4) != "data") at += 8 + b.getInt(at + 4)+ val end = if (at + 8 <= wav.size) minOf(wav.size, at + 8 + b.getInt(at + 4)) else at+ val frames = ArrayList<Frame>()+ val demod = P25Demod(0.0, LDU_DIBITS) { f -> decode(f)?.let { frames += it } }+ var k = at + 8+ while (k + 4 <= end) { demod.feedChannel(b.getShort(k).toDouble(), b.getShort(k + 2).toDouble()); k += 4 }+ return Decoded(frames, demod.syncs)+ }+}diff --git a/app/src/test/java/com/rfsentinel/app/sdr/P25RecordingsCheck.kt b/app/src/test/java/com/rfsentinel/app/sdr/P25RecordingsCheck.ktnew file mode 100644index 0000000..a6319a7--- /dev/null+++ b/app/src/test/java/com/rfsentinel/app/sdr/P25RecordingsCheck.kt@@ -0,0 +1,27 @@+package com.rfsentinel.app.sdr++import org.junit.Assume.assumeTrue+import org.junit.Test+import java.io.File++/**+ * Real calls recorded on the phone ([P25Record]), checked on this computer: set P25_REC_DIR to a folder of them.+ * Skipped otherwise. Recordings stay out of the repository: they are other people's radio calls.+ */+class P25RecordingsCheck {++ @Test+ fun decodeRecordedCalls() {+ val dir = System.getenv("P25_REC_DIR")?.let(::File)+ assumeTrue(dir?.isDirectory == true)+ for (f in dir!!.listFiles { x -> x.name.endsWith(".wav") }.orEmpty().sortedBy { it.name }) {+ val out = P25Voice.decodeRecording(f.readBytes())+ val words = out.frames.flatMap { it.voice }+ val errors = words.sumOf { it.errors }+ println("${f.name}: ${out.syncs} syncs, ${out.frames.size} voice frames (" ++ "${out.frames.count { it.nid.duid == P25Voice.DUID_LDU1 }} LDU1), ${words.size} codewords, " ++ "%.2f bit errors each, algid ${out.frames.mapNotNull { it.algid }.distinct().joinToString { "%02X".format(it) }}"+ .format(java.util.Locale.US, if (words.isEmpty()) 0.0 else errors.toDouble() / words.size))+ }+ }+}diff --git a/app/src/test/java/com/rfsentinel/app/sdr/P25VoiceTest.kt b/app/src/test/java/com/rfsentinel/app/sdr/P25VoiceTest.ktnew file mode 100644index 0000000..32df2e8--- /dev/null+++ b/app/src/test/java/com/rfsentinel/app/sdr/P25VoiceTest.kt@@ -0,0 +1,108 @@+package com.rfsentinel.app.sdr++import org.junit.Assert.assertArrayEquals+import org.junit.Assert.assertEquals+import org.junit.Assert.assertFalse+import org.junit.Assert.assertNull+import org.junit.Assert.assertTrue+import org.junit.Test+import kotlin.math.PI+import kotlin.math.cos+import kotlin.math.roundToInt+import kotlin.math.sin++class P25VoiceTest {++ /** A codeword as a transmitter builds it from 88 info bits: coded, scrambled, interleaved into 72 dibits. */+ private fun codeword(bits: ByteArray): IntArray {+ var b = 0+ val u = IntArray(8) { c -> var x = 0; repeat(P25Voice.INFO[c]) { x = x shl 1 or bits[b++].toInt() }; x }+ val w = IntArray(8)+ for (c in 0..3) w[c] = u[c] shl 11 or P25Voice.golayParity(u[c])+ for (c in 4..6) w[c] = (0 until 16).map { u[c] shl 4 or it }.first { P25Voice.hammingSyndrome(it) == 0 }+ w[7] = u[7]+ val pn = P25Voice.pn(u[0]); var k = 0+ for (c in 1..6) for (j in P25Voice.SIZE[c] - 1 downTo 0) { if (pn[k++] == 1) w[c] = w[c] xor (1 shl j) }+ return IntArray(72) { i ->+ (w[P25Voice.IW[i]] shr P25Voice.IX[i] and 1) shl 1 or (w[P25Voice.IY[i]] shr P25Voice.IZ[i] and 1)+ }+ }++ private fun hexWord(v: Int): IntArray = P25TestSignal.dibitsOf((v shl 4 or P25Voice.hexParity(v)).toLong(), 5)++ /** An LDU after its sync, status symbols in: NID, the nine codewords, the algorithm ID for an LDU2. */+ private fun ldu(duid: Int, voice: List<ByteArray>, algid: Int = P25Voice.ALGID_CLEAR, hit: (IntArray) -> Unit = {}): IntArray {+ val d = IntArray(816)+ P25TestSignal.dibitsOf(P25Frame.nidCodeword(0x293 shl 4 or duid) shl 1, 32).copyInto(d)+ for (i in 0 until 9) codeword(voice[i]).copyInto(d, P25Voice.VOICE_AT[i])+ hexWord(algid shr 2).copyInto(d, 452); hexWord(algid and 3 shl 4).copyInto(d, 457)+ hit(d)+ val data = ArrayDeque(d.toList())+ return IntArray(P25Voice.LDU_DIBITS) { i -> if ((i + 25) % 36 == 0) 2 else data.removeFirst() }+ }++ private fun voice(seed: Long): List<ByteArray> {+ val rnd = java.util.Random(seed)+ return List(9) { ByteArray(88) { rnd.nextInt(2).toByte() } }+ }++ @Test+ fun theInterleaveCoversEveryBitOnce() {+ val seen = HashSet<Int>()+ for (k in 0 until 72) { seen += P25Voice.IW[k] * 32 + P25Voice.IX[k]; seen += P25Voice.IY[k] * 32 + P25Voice.IZ[k] }+ assertEquals(144, seen.size)+ for (c in 0 until 8) for (j in 0 until P25Voice.SIZE[c]) assertTrue("word $c bit $j", c * 32 + j in seen)+ }++ @Test+ fun codewordsComeBackAndErrorsAreCorrected() {+ val v = voice(1)+ val f = P25Voice.decode(ldu(P25Voice.DUID_LDU2, v))!!+ assertEquals(0x293, f.nid.nac)+ assertEquals(9, f.voice.size)+ for (i in 0 until 9) { assertArrayEquals(v[i], f.voice[i].bits); assertEquals(0, f.voice[i].errors) }+ assertEquals(P25Voice.ALGID_CLEAR, f.algid)+ assertFalse(f.encrypted)++ // A few dibits hit in the third codeword: Golay and Hamming put it right.+ val hit = ldu(P25Voice.DUID_LDU1, v) { d -> for (k in listOf(2, 30, 61)) d[P25Voice.VOICE_AT[2] + k] = d[P25Voice.VOICE_AT[2] + k] xor 1 }+ val g = P25Voice.decode(hit)!!+ assertNull(g.algid)+ assertArrayEquals(v[2], g.voice[2].bits)+ assertTrue(g.voice[2].errors > 0)+ }++ @Test+ fun anEncryptedCallIsTold() {+ assertTrue(P25Voice.decode(ldu(P25Voice.DUID_LDU2, voice(2), algid = 0x84))!!.encrypted)+ }++ @Test+ fun aRecordedCallIsDecoded() {+ // Two LDUs on the air, as the dongle hears them 250 kHz up (plus 700 Hz of crystal error), recorded as P25Record does.+ val v1 = voice(3); val v2 = voice(4)+ val dibits = IntArray(60) { (it * 5 + 1) % 4 } + P25TestSignal.SYNC + ldu(P25Voice.DUID_LDU1, v1) ++ P25TestSignal.SYNC + ldu(P25Voice.DUID_LDU2, v2) + IntArray(60) { it % 4 }+ val rate = P25Demod.SAMPLE_RATE+ val perSymbol = rate / P25Demod.SYMBOL_RATE+ val iq = ByteArray(dibits.size * perSymbol * 2)+ val rnd = java.util.Random(9)+ var phase = 0.0; var k = 0+ for (d in dibits) repeat(perSymbol) {+ phase += 2 * PI * (250_700.0 + P25TestSignal.hz(d)) / rate+ iq[k++] = (127.5 + 50 * cos(phase) + rnd.nextGaussian() * 4).roundToInt().coerceIn(0, 255).toByte()+ iq[k++] = (127.5 + 50 * sin(phase) + rnd.nextGaussian() * 4).roundToInt().coerceIn(0, 255).toByte()+ }+ val rec = P25Record.Baseband(250_000L)+ var at = 0+ while (at < iq.size) { val len = minOf(16_384, iq.size - at); rec.feed(iq.copyOfRange(at, at + len), len / 2); at += len }++ val out = P25Voice.decodeRecording(rec.wav())+ assertEquals(2, out.frames.size)+ for (i in 0 until 9) {+ assertArrayEquals(v1[i], out.frames[0].voice[i].bits)+ assertArrayEquals(v2[i], out.frames[1].voice[i].bits)+ }+ assertEquals(P25Voice.ALGID_CLEAR, out.frames[1].algid)+ }+}