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.

2.22.2: MT7921AU listens on both antennas; MT7612U recovers a receiver that went deaf, logs why transfers are dropped and ignores short control frames

CIS-C0b846a54527fa
diff --git a/app/build.gradle.kts b/app/build.gradle.kts
index abc4ddc..894ae56 100644
--- a/app/build.gradle.kts
+++ b/app/build.gradle.kts
@@ -7,8 +7,8 @@ plugins {
}
// Bump both for every release. versionName ends up in the APK file name.
-val appVersionCode = 48
-val appVersionName = "2.22.1"
+val appVersionCode = 49
+val appVersionName = "2.22.2"
// Release signing credentials live in keystore.properties (git-ignored).
// Without that file, release builds are produced unsigned.
diff --git a/app/src/main/java/com/rfsentinel/app/usb/Mt7612uMonitor.kt b/app/src/main/java/com/rfsentinel/app/usb/Mt7612uMonitor.kt
index d7f0929..6eede47 100644
--- a/app/src/main/java/com/rfsentinel/app/usb/Mt7612uMonitor.kt
+++ b/app/src/main/java/com/rfsentinel/app/usb/Mt7612uMonitor.kt
@@ -83,6 +83,7 @@ object Mt7612uMonitor {
/** AUTO_RSP_CFG without the auto-responder (bit 0): the adapter never answers with an ACK or CTS. */
private const val AUTO_RSP_NO_RESPONDER = 0x12
+ private const val MIN_FRAME = 10; private const val MAX_BAD_LOGS = 12; private const val DEAF_MS = 15_000L
private const val FW_ATTEMPTS = 3; private const val CLAIM_WAITS = 10
private const val DMA_HDR = 4; private const val RXWI_LEN = 32; private const val FCE_INFO = 4
/** Room for the largest 802.11ac MPDU (11454 B) plus descriptors: a smaller buffer splits big frames over two transfers. */
@@ -453,7 +454,10 @@ object Mt7612uMonitor {
// ---- receive ----
- private class RxState { var hopAt = 0L; var lastEmit = 0L; var lastRxAt = 0L; var lastCheck = 0L; var hopIdx = 0 }
+ private class RxState {
+ var hopAt = 0L; var lastEmit = 0L; var lastRxAt = 0L; var lastCheck = 0L; var hopIdx = 0
+ var badLogged = 0; var revivals = 0
+ }
private fun rxLoop(chipName: String, epCmd: UsbEndpoint, ep: UsbEndpoint,
onFrame: (List<MonitorSighting>) -> Unit, onStatus: (String) -> Unit) {
@@ -465,7 +469,8 @@ object Mt7612uMonitor {
st.lastRxAt = System.currentTimeMillis()
stats.sample("$n bytes, DMA header + RXWI", b, 0, DMA_HDR + 16)
val ch = HOP[st.hopIdx]
- val f = parseRx(b, n, onBad = { crc -> if (crc) stats.badChecksum++ else stats.malformed++ }) ?: return@onData
+ val f = parseRx(b, n, onBad = { crc -> if (crc) stats.badChecksum++ else stats.malformed++ },
+ why = { if (st.badLogged < MAX_BAD_LOGS) { st.badLogged++; UsbWifi.log("MT7612U: transfer dropped - $it") } }) ?: return@onData
val rssi = f.rawRssi?.let { if (ch <= 14) it + rssiOff2g - lna2g else it + rssiOff5g - lna5g }
stats.frame(rssi)
frames.frame(b, f.start, f.end, ch, rssi)
@@ -483,6 +488,19 @@ object Mt7612uMonitor {
UsbWifi.log("MT7612U: receive stalled (${(now - st.lastRxAt) / 1000} s) - restarting it")
runCatching { reKickRx() }
st.lastRxAt = now
+ } else if (now - st.lastRxAt > DEAF_MS) {
+ // Nothing at all, not even energy on the channel: the receiver itself is off or
+ // lost its calibration (seen after a quick restart without a replug). Switch it
+ // back on and fully retune.
+ val sys = regRd(MAC_SYS_CTRL)
+ if (st.revivals < 5) UsbWifi.log("MT7612U: nothing received for ${(now - st.lastRxAt) / 1000} s (SYS_CTRL 0x%08x) - re-enabling the receiver".format(sys))
+ st.revivals++
+ runCatching {
+ if (sys and SYS_CTRL_ENABLE_RX == 0) macStart() else reKickRx()
+ tune(epCmd, HOP[st.hopIdx], scan = false)
+ }
+ drainCmdResp()
+ st.lastRxAt = now
}
}
if (now - st.lastEmit > 2000) { emit(onFrame); st.lastEmit = now }
@@ -515,17 +533,24 @@ object Mt7612uMonitor {
* DMA header: anything else is a piece of a split transfer, whose "descriptor" is frame
* payload (random lengths and signal values, phantom addresses) and is dropped.
*/
- internal fun parseRx(b: ByteArray, n: Int, onBad: (Boolean) -> Unit = {}): Rx? {
- if (n < DMA_HDR + RXWI_LEN + 24 || n > b.size) { onBad(false); return null }
+ internal fun parseRx(b: ByteArray, n: Int, onBad: (Boolean) -> Unit = {}, why: (String) -> Unit = {}): Rx? {
+ // Only lengths and flags go into [why], never frame bytes (addresses, network names).
+ if (n < DMA_HDR + RXWI_LEN + MIN_FRAME || n > b.size) { onBad(false); why("$n B transfer, too short"); return null }
val dmaLen = (b[0].toInt() and 0xff) or ((b[1].toInt() and 0xff) shl 8)
- if (dmaLen < RXWI_LEN + 24 || dmaLen and 3 != 0 || DMA_HDR + dmaLen > n || n > DMA_HDR + dmaLen + FCE_INFO) { onBad(false); return null }
+ if (dmaLen < RXWI_LEN + MIN_FRAME || dmaLen and 3 != 0 || DMA_HDR + dmaLen > n || n > DMA_HDR + dmaLen + FCE_INFO) {
+ onBad(false); why("$n B transfer, DMA length $dmaLen"); return null
+ }
val rxwi = DMA_HDR
val rxinfo = le32(b, rxwi)
if (rxinfo and RXINFO_CRCERR != 0) { onBad(true); return null }
val mpdu = (le32(b, rxwi + 4) ushr 16) and 0x3fff
var d = rxwi + RXWI_LEN
val pad = if (rxinfo and RXINFO_L2PAD != 0) 2 else 0
- if (mpdu < 24 || d + mpdu + pad > DMA_HDR + dmaLen || DMA_HDR + dmaLen - (d + mpdu + pad) >= 4) { onBad(false); return null }
+ if (mpdu < MIN_FRAME || d + mpdu + pad > DMA_HDR + dmaLen || DMA_HDR + dmaLen - (d + mpdu + pad) >= 4) {
+ onBad(false); why("$n B transfer, DMA length $dmaLen, frame $mpdu B, pad $pad, RX info 0x%08x".format(rxinfo)); return null
+ }
+ // ACK / CTS / RTS / PS-Poll: shorter than a full header and carry no sender worth showing.
+ if (mpdu < 24) { if ((b[d].toInt() shr 2) and 3 != 1) { onBad(false); why("$mpdu B frame that isn't a control frame") }; return null }
if (pad != 0) {
val hdr = headerLength(b, d).coerceAtMost(mpdu)
System.arraycopy(b, d, b, d + pad, hdr)
diff --git a/app/src/main/java/com/rfsentinel/app/usb/Mt7921uMonitor.kt b/app/src/main/java/com/rfsentinel/app/usb/Mt7921uMonitor.kt
index 62615cc..7cf7d7d 100644
--- a/app/src/main/java/com/rfsentinel/app/usb/Mt7921uMonitor.kt
+++ b/app/src/main/java/com/rfsentinel/app/usb/Mt7921uMonitor.kt
@@ -41,6 +41,7 @@ object Mt7921uMonitor {
149, 153, 157, 161, 165).map { 1 to it } +
listOf(5, 21, 37, 53, 69, 85, 101, 117, 133, 149, 165, 181, 197, 213, 229).map { 2 to it }
private const val DWELL_MS = 150L
+ private const val RX_ANTENNAS = 0x3
private val FRESH_MS get() = (HOP.size * DWELL_MS * 9 / 5).coerceAtLeast(3000L)
private const val REPORT_MS = 10_000L
@@ -466,10 +467,14 @@ object Mt7921uMonitor {
return p
}
- /** CHANNEL_SWITCH: 20 MHz on [ch] in [band] (0 = 2.4, 1 = 5, 2 = 6 GHz), two streams. */
+ /**
+ * CHANNEL_SWITCH: 20 MHz on [ch] in [band] (0 = 2.4, 1 = 5, 2 = 6 GHz), two streams. The RX path
+ * is an antenna mask, as mt7921_mcu_set_chan_info sends it (antenna_mask): 0x3 listens on both
+ * antennas; the 2 in mt76-usb-macos's frames is antenna 2 alone.
+ */
internal fun chanInfoPayload(band: Int, ch: Int): ByteArray {
val p = ByteArray(76)
- p[0] = ch.toByte(); p[1] = ch.toByte(); p[2] = 0; p[3] = 2; p[4] = 2; p[10] = band.toByte()
+ p[0] = ch.toByte(); p[1] = ch.toByte(); p[2] = 0; p[3] = 2; p[4] = RX_ANTENNAS.toByte(); p[10] = band.toByte()
return p
}
diff --git a/app/src/test/java/com/rfsentinel/app/usb/Mt7921uDriverTest.kt b/app/src/test/java/com/rfsentinel/app/usb/Mt7921uDriverTest.kt
index f80c48a..c3edf12 100644
--- a/app/src/test/java/com/rfsentinel/app/usb/Mt7921uDriverTest.kt
+++ b/app/src/test/java/com/rfsentinel/app/usb/Mt7921uDriverTest.kt
@@ -82,7 +82,8 @@ class Mt7921uDriverTest {
@Test
fun tuningCommandsMatchTheGoldenFrames() {
- assertEquals(golden["set_chan_info_6g_53_20"], hex(m.cmdFrame(m.extCmd(m.EXT_CHANNEL_SWITCH), m.chanInfoPayload(2, 53), 1)))
+ // the golden frame with the RX antenna mask set to both antennas (0x3), as Linux sends it
+ assertEquals(golden["set_chan_info_6g_53_20"]!!.replace("353500020200", "353500020300"), hex(m.cmdFrame(m.extCmd(m.EXT_CHANNEL_SWITCH), m.chanInfoPayload(2, 53), 1)))
assertEquals(golden["config_sniffer_6g_53_20"], hex(m.uniFrame(m.UNI_SNIFFER, m.snifferConfigPayload(2, 53), 1)))
}
diff --git a/app/src/test/java/com/rfsentinel/app/usb/WardriveGoDriverTest.kt b/app/src/test/java/com/rfsentinel/app/usb/WardriveGoDriverTest.kt
index 8082aa8..c7959ee 100644
--- a/app/src/test/java/com/rfsentinel/app/usb/WardriveGoDriverTest.kt
+++ b/app/src/test/java/com/rfsentinel/app/usb/WardriveGoDriverTest.kt
@@ -115,6 +115,15 @@ class WardriveGoDriverTest {
assertEquals(2, malformed)
}
+ @Test
+ fun mt7612uIgnoresShortControlFramesWithoutCountingThem() {
+ val ack = byteArrayOf(0xd4.toByte(), 0) + ByteArray(8) // ACK: frame control, duration, receiver
+ val t = mtTransfer(ack)
+ var bad = 0; var reason: String? = null
+ assertNull(Mt7612uMonitor.parseRx(t, t.size, onBad = { bad++ }, why = { reason = it }))
+ assertEquals(0, bad); assertNull(reason)
+ }
+
// --- AR9271: stream of | len | 0x4e00 | HTC header (8) | RX status (40) | frame + FCS |, padded to 4 ---
private fun athPacket(frame: ByteArray, status: Int = 0, rssi: Int = 30): ByteArray {