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.

USB WiFi adapter in monitor mode: RTL8811AU / RTL8821AU (AWUS036ACS) driver ported from Wardrive Go (GPL-3.0, PMKID capture removed); client devices, USB WIFI badge, Settings status and shareable driver log

CIS-C0197f38027add
diff --git a/README.md b/README.md
index 00c3c33..7e7f160 100644
--- a/README.md
+++ b/README.md
@@ -137,6 +137,7 @@ Every match shows its evidence, its source and a confidence tier: **weak** (< 50
**Hardware**
- ESP32 boards over USB OTG (OUI-Spy, GhostESP) or Bluetooth (OUI-SPY App-Controlled) add their detections and extend range
+- **USB WiFi adapters in monitor mode** (RTL8811AU / RTL8821AU, e.g. ALFA AWUS036ACS) over OTG, no root: continuous 2.4 GHz channel hopping with no Android scan limit, longer range, and **client devices** (laptops, phones, cameras connected to a network) the phone's WiFi scan can't see
**Interface**
- Live list and radar view, filter chips with live counts (flagged, trackers, drones, new, ESP32, favorites, Bluetooth, WiFi), search
@@ -311,6 +312,24 @@ In that mode the board sends standard WiFi probe requests; turn **Settings →
OUI-SPY: relay every network and Bluetooth device** off to keep it listening
only. The board's UniPwn, PCAP and Foxhunter engines are never used.
+### USB WiFi adapter (monitor mode)
+
+Plug a supported USB WiFi adapter into the phone with an OTG cable while
+scanning and allow USB access on the prompt. RF Sentinel drives it directly
+over Android's USB host API (no root, no kernel driver), puts it in
+**receive-only monitor mode** and hops the 2.4 GHz channels. Access points
+(beacons, probe responses) and client devices (probe requests, data frames)
+go through RF Sentinel's own rules like everything else, with a purple
+**USB WIFI** badge.
+
+| Chip | Example adapters | Status |
+|---|---|---|
+| Realtek RTL8811AU / RTL8821AU (`0bda:0811`) | ALFA AWUS036ACS | Supported |
+| RTL8812AU, RTL8814AU, AR9271, MT7612U, RT3070, RTL8187 | AWUS036ACH, AWUS1900, AWUS036NHA... | Detected and named, not supported yet |
+
+The adapter never transmits. Settings shows its status and can share its
+driver log for troubleshooting.
+
## Themes
Pick a theme in the setup wizard or under **Settings → Appearance**.
@@ -535,7 +554,12 @@ detection and WiFi identification, is in [docs/SIGNATURES.md](docs/SIGNATURES.md
Inspired by the nyanBOX hardware device and the RF Party app, which was based
on Alan Meekins' DEF CON 31 talk *"Snoop Unto Them As They Snoop Unto Us"*.
Some feature ideas come from SØPHIA and BLE Radar (MetaRadar). No code from
-those projects is included.
+those projects is included. The USB WiFi monitor-mode driver for RTL8811AU /
+RTL8821AU adapters (`app/src/main/java/com/rfsentinel/app/usb/`) is ported from
+[Wardrive Go](https://github.com/RocketGod-git/wardrive-go) by RocketGod (GPL-3.0),
+whose register tables come from the Realtek 88xxau Linux driver; its handshake /
+PMKID capture was left out. The map's anchored point layer follows Wardrive Go's
+approach - thank you.
Part of the MAC-prefix data was cross-checked with, and extended from, the lists
in [Flock You](https://github.com/colonelpanichacks/flock-you) and
diff --git a/app/src/main/java/com/rfsentinel/app/MainActivity.kt b/app/src/main/java/com/rfsentinel/app/MainActivity.kt
index 05ffdb1..2d5d761 100644
--- a/app/src/main/java/com/rfsentinel/app/MainActivity.kt
+++ b/app/src/main/java/com/rfsentinel/app/MainActivity.kt
@@ -242,7 +242,8 @@ class MainActivity : AppCompatActivity() {
?.let { " · ${it.second} scan restarted " +
java.text.DateFormat.getTimeInstance(java.text.DateFormat.SHORT).format(java.util.Date(it.first)) } ?: "") +
com.rfsentinel.app.esp.OuiSpyBle.status.takeIf { it.isNotBlank() }?.let { "\n$it" }.orEmpty() +
- com.rfsentinel.app.esp.EspBoards.status.takeIf { it.isNotBlank() }?.let { "\n$it" }.orEmpty()
+ com.rfsentinel.app.esp.EspBoards.status.takeIf { it.isNotBlank() }?.let { "\n$it" }.orEmpty() +
+ com.rfsentinel.app.usb.UsbWifi.status.takeIf { it.isNotBlank() }?.let { "\n$it" }.orEmpty()
}
}
@@ -355,6 +356,7 @@ class MainActivity : AppCompatActivity() {
(s.following || (best!!.confidence >= threshold && best.category != Category.TRACKER)),
bold = alert,
heardByEsp = com.rfsentinel.app.esp.HeardBy.esp.recent(s.mac, now),
+ heardByUsb = com.rfsentinel.app.esp.HeardBy.usb.recent(s.mac, now),
heardByPhone = com.rfsentinel.app.esp.HeardBy.phone.recent(s.mac, now)
)
}
diff --git a/app/src/main/java/com/rfsentinel/app/detect/Advert.kt b/app/src/main/java/com/rfsentinel/app/detect/Advert.kt
index 3f2e098..4f8ee98 100644
--- a/app/src/main/java/com/rfsentinel/app/detect/Advert.kt
+++ b/app/src/main/java/com/rfsentinel/app/detect/Advert.kt
@@ -39,7 +39,9 @@ class Advert(
val capabilities: String,
val standard: String?,
/** Beacon information elements (id, payload), API 30+ only. */
- val infoElements: List<Pair<Int, ByteArray>>
+ val infoElements: List<Pair<Int, ByteArray>>,
+ /** A client device (laptop, phone, camera) heard by a monitor-mode adapter, not an access point. */
+ val client: Boolean = false
)
val isBle get() = source == Source.BLE
diff --git a/app/src/main/java/com/rfsentinel/app/detect/DeviceIntel.kt b/app/src/main/java/com/rfsentinel/app/detect/DeviceIntel.kt
index e3d17bf..b40cbda 100644
--- a/app/src/main/java/com/rfsentinel/app/detect/DeviceIntel.kt
+++ b/app/src/main/java/com/rfsentinel/app/detect/DeviceIntel.kt
@@ -232,10 +232,12 @@ object DeviceIntel {
}
facts += "Band" to "$band (${w.frequencyMhz} MHz, channel ${channelOf(w.frequencyMhz)})"
w.standard?.let { facts += "Standard" to it }
- facts += "Security" to security(w.capabilities)
+ if (!w.client) facts += "Security" to security(w.capabilities)
+ else facts += "Heard by" to "USB WiFi adapter (monitor mode)"
}
val ssid = a.name
val type = when {
+ w?.client == true -> "WiFi device (laptop, phone or camera on a network)"
ssid.isNullOrEmpty() -> "Hidden WiFi network"
Regex("iphone|androidap|galaxy|pixel|hotspot|'s phone", RegexOption.IGNORE_CASE).containsMatchIn(ssid) -> "Phone hotspot"
Regex("direct-|printer|hp-print|epson|canon", RegexOption.IGNORE_CASE).containsMatchIn(ssid) -> "WiFi Direct / printer"
diff --git a/app/src/main/java/com/rfsentinel/app/esp/HeardBy.kt b/app/src/main/java/com/rfsentinel/app/esp/HeardBy.kt
index 651305a..b43f3d3 100644
--- a/app/src/main/java/com/rfsentinel/app/esp/HeardBy.kt
+++ b/app/src/main/java/com/rfsentinel/app/esp/HeardBy.kt
@@ -29,5 +29,7 @@ object HeardBy {
const val WINDOW_MS = 5 * 60_000L
val esp = RecentMacs()
+ /** A USB WiFi adapter in monitor mode. */
+ val usb = RecentMacs()
val phone = RecentMacs()
}
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 f31c193..d9ed7d4 100644
--- a/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt
+++ b/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt
@@ -258,6 +258,8 @@ class ScanForegroundService : Service() {
if (Prefs.wifiEnabled(this)) startWifiPolling() else stopWifiPolling()
// ESP32 boards on USB (OUI-Spy / GhostESP): their reports join the same pipeline.
com.rfsentinel.app.esp.EspBoards.start(this) { list -> pipeline.post { list.forEach(::processEsp) } }
+ // A USB WiFi adapter in monitor mode (e.g. AWUS036ACS): access points and client devices.
+ com.rfsentinel.app.usb.UsbWifi.start(this) { list -> pipeline.post { list.forEach(::processUsbWifi) } }
// An OUI-SPY board paired over Bluetooth (App-Controlled firmware).
Prefs.ouiSpyBoard(this)?.let { addr ->
com.rfsentinel.app.esp.OuiSpyBle.start(this, addr, Prefs.ouiSpyRelayAll(this)) { list -> pipeline.post { list.forEach(::processEsp) } }
@@ -478,6 +480,18 @@ class ScanForegroundService : Service() {
private val espHits = HashMap<String, Pair<Long, List<Hit>>>()
/** One ESP32 report: becomes a normal observation, with the board's own matches added. */
+ private fun processUsbWifi(m: com.rfsentinel.app.usb.MonitorSighting) {
+ val now = System.currentTimeMillis()
+ val mac = com.rfsentinel.app.util.MacUtil.normalize(m.mac)
+ com.rfsentinel.app.esp.HeardBy.usb.mark(mac, now)
+ process(Advert(
+ mac = mac, source = Advert.Source.WIFI, rssi = m.rssi, name = m.ssid,
+ addressType = com.rfsentinel.app.detect.AddressType.ofWifi(mac),
+ wifi = Advert.WifiInfo(m.frequencyMhz, "", null, emptyList(), client = !m.isAccessPoint),
+ timestamp = now
+ ), null)
+ }
+
private fun processEsp(e: com.rfsentinel.app.esp.EspSighting) {
val now = System.currentTimeMillis()
com.rfsentinel.app.esp.HeardBy.esp.mark(e.mac, now)
@@ -826,6 +840,7 @@ class ScanForegroundService : Service() {
override fun onDestroy() {
com.rfsentinel.app.esp.EspBoards.stop(this)
+ com.rfsentinel.app.usb.UsbWifi.stop(this)
com.rfsentinel.app.esp.OuiSpyBle.stop()
bleEngine.stop()
wifiEngine.stop()
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 7ff632b..1977db2 100644
--- a/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.kt
+++ b/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.kt
@@ -183,6 +183,8 @@ class SettingsActivity : AppCompatActivity() {
val s = com.rfsentinel.app.esp.EspBoards.status
binding.espStatusText.text = "ESP32 on USB (OUI-Spy or GhostESP): " +
s.ifBlank { "plug one in with an OTG cable while scanning to add its detections" }
+ binding.usbWifiText.text = "USB WiFi adapter in monitor mode (RTL8811AU / 8821AU, e.g. ALFA AWUS036ACS): " +
+ com.rfsentinel.app.usb.UsbWifi.status.ifBlank { "plug one in with an OTG cable while scanning - longer range, and it hears devices connected to networks" }
val board = Prefs.ouiSpyBoard(this)
binding.ouiSpyText.text = if (board == null)
"OUI-SPY over Bluetooth (App-Controlled firmware): not paired"
@@ -396,6 +398,13 @@ class SettingsActivity : AppCompatActivity() {
com.rfsentinel.app.esp.EspBoards.onStatusChanged = { runOnUiThread { updateEspStatus() } }
com.rfsentinel.app.esp.OuiSpyBle.onStatusChanged = { runOnUiThread { updateEspStatus() } }
binding.ouiSpyButton.setOnClickListener { pairOuiSpy() }
+ binding.usbWifiLogButton.setOnClickListener {
+ val log = com.rfsentinel.app.usb.UsbWifi.logText().ifBlank { "No USB WiFi adapter activity yet." }
+ startActivity(android.content.Intent.createChooser(
+ android.content.Intent(android.content.Intent.ACTION_SEND).setType("text/plain")
+ .putExtra(android.content.Intent.EXTRA_SUBJECT, "RF Sentinel USB WiFi adapter log")
+ .putExtra(android.content.Intent.EXTRA_TEXT, log), "Share adapter log"))
+ }
binding.trackerIgnoreButton.setOnClickListener {
com.rfsentinel.app.data.TrackerMutes.clear(this)
Prefs.setTrackerFollowPausedUntil(this, 0L)
diff --git a/app/src/main/java/com/rfsentinel/app/ui/DeviceAdapter.kt b/app/src/main/java/com/rfsentinel/app/ui/DeviceAdapter.kt
index 4fe9042..f016d6a 100644
--- a/app/src/main/java/com/rfsentinel/app/ui/DeviceAdapter.kt
+++ b/app/src/main/java/com/rfsentinel/app/ui/DeviceAdapter.kt
@@ -27,15 +27,17 @@ data class DeviceRow(
val bold: Boolean,
/** Heard by an ESP32 board (blue badge) and/or the phone's own radios (green badge). */
val heardByEsp: Boolean = false,
+ val heardByUsb: Boolean = false,
val heardByPhone: Boolean = false
)
private const val ESP_BADGE = 0xFF1E88E5.toInt()
private const val PHONE_BADGE = 0xFF2E7D32.toInt()
+private const val USB_BADGE = 0xFF6A1B9A.toInt()
/** The meta line, followed by small coloured "ESP32" / "INTERNAL" badges. */
private fun metaWithBadges(row: DeviceRow): CharSequence {
- if (!row.heardByEsp && !row.heardByPhone) return row.meta
+ if (!row.heardByEsp && !row.heardByUsb && !row.heardByPhone) return row.meta
val sb = android.text.SpannableStringBuilder(row.meta)
fun badge(label: String, color: Int) {
sb.append(" ")
@@ -47,6 +49,7 @@ private fun metaWithBadges(row: DeviceRow): CharSequence {
sb.setSpan(android.text.style.StyleSpan(Typeface.BOLD), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
if (row.heardByEsp) badge("ESP32", ESP_BADGE)
+ if (row.heardByUsb) badge("USB WIFI", USB_BADGE)
if (row.heardByPhone) badge("INTERNAL", PHONE_BADGE)
return sb
}
diff --git a/app/src/main/java/com/rfsentinel/app/ui/DeviceFilter.kt b/app/src/main/java/com/rfsentinel/app/ui/DeviceFilter.kt
index 3e5c57b..187400c 100644
--- a/app/src/main/java/com/rfsentinel/app/ui/DeviceFilter.kt
+++ b/app/src/main/java/com/rfsentinel/app/ui/DeviceFilter.kt
@@ -25,7 +25,7 @@ enum class DeviceFilter(val label: String) {
s.deviceType.contains("tracker", true) || s.deviceType.contains("Find My", true)
DRONES -> s.hits.any { it.category == Category.DRONE } || s.remoteId != null
NEW -> s.isNew
- ESP32 -> com.rfsentinel.app.esp.HeardBy.esp.recent(s.mac)
+ ESP32 -> com.rfsentinel.app.esp.HeardBy.esp.recent(s.mac) || com.rfsentinel.app.esp.HeardBy.usb.recent(s.mac)
FAVORITES -> Favorites.contains(s.mac)
BLE -> Advert.Source.BLE in s.sources
WIFI -> Advert.Source.WIFI in s.sources
diff --git a/app/src/main/java/com/rfsentinel/app/usb/Rtl8821auMonitor.kt b/app/src/main/java/com/rfsentinel/app/usb/Rtl8821auMonitor.kt
new file mode 100644
index 0000000..d9efb23
--- /dev/null
+++ b/app/src/main/java/com/rfsentinel/app/usb/Rtl8821auMonitor.kt
@@ -0,0 +1,395 @@
+package com.rfsentinel.app.usb
+
+/*
+ * Receive-only monitor mode for Realtek RTL8811AU / RTL8821AU USB WiFi adapters
+ * (e.g. ALFA AWUS036ACS), driven from userspace over Android's USB host API -
+ * no root, no kernel driver. Hops the 2.4 GHz channels and reports the access
+ * points (beacons, probe responses) and client devices (probe requests, data
+ * frames) it hears. It never transmits.
+ *
+ * Ported from Wardrive Go by RocketGod (https://github.com/RocketGod-git/wardrive-go,
+ * GPL-3.0) and adapted to RF Sentinel: its handshake / PMKID capture was removed.
+ * Used under the GNU GPL v3.
+ */
+
+import android.content.Context
+import android.hardware.usb.UsbConstants
+import android.hardware.usb.UsbDevice
+import android.hardware.usb.UsbDeviceConnection
+import android.hardware.usb.UsbEndpoint
+import android.hardware.usb.UsbManager
+import android.hardware.usb.UsbRequest
+import java.nio.ByteBuffer
+import java.util.concurrent.TimeoutException
+
+object Rtl8821auMonitor {
+ @Volatile var running = false; private set
+ @Volatile private var stop = false
+ @Volatile var captureClients = true
+
+ private class Sighting(val mac: String, var isAp: Boolean) {
+ var ssid: String? = null; var bssid: String? = null
+ var rssi = -128; var channel = 0
+ var good = false; var hits = 0; var lastSeen = 0L
+ fun trusted() = good || hits >= 2
+ }
+ private val sightings = LinkedHashMap<String, Sighting>()
+ private val HOP = intArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
+ private const val DWELL_MS = 350L
+
+ private val FRESH_MS get() = (HOP.size * DWELL_MS * 9 / 5).coerceAtLeast(3000L)
+ private const val PRUNE_MS = 60_000L
+ private const val REPORT_MS = 10_000L
+ private const val NOMINAL_RSSI = -60
+ private fun freqOf(ch: Int) = 2407 + ch * 5
+
+ private const val OUT_VENDOR = 0x40; private const val IN_VENDOR = 0xC0
+ private const val VREQ = 0x05
+ private const val REG_RX_DRVINFO_SZ = 0x060F; private const val RCR_APP_PHYST_RXFF = 0x10000000
+
+ private const val rA_LSSIWrite = 0xC90; private const val rHSSIRead = 0x8B0; private const val rA_PIRead = 0xD04
+
+ private val rfShadow = HashMap<Int, Int>()
+
+ fun abort() { stop = true }
+
+ fun run(ctx: Context, device: UsbDevice, onStatus: (String) -> Unit,
+ onFrame: (List<MonitorSighting>) -> Unit) {
+ if (running) return
+ running = true; stop = false; sightings.clear(); rfShadow.clear()
+ val mgr = ctx.getSystemService(Context.USB_SERVICE) as UsbManager
+ val conn = mgr.openDevice(device) ?: run { onStatus("RTL8821AU: couldn't reopen adapter"); running = false; return }
+ var epRx: UsbEndpoint? = null
+ try {
+ val intf = device.getInterface(0)
+ conn.claimInterface(intf, true)
+ for (i in 0 until intf.endpointCount) {
+ val ep = intf.getEndpoint(i)
+ if (ep.type == UsbConstants.USB_ENDPOINT_XFER_BULK && ep.direction == UsbConstants.USB_DIR_IN) { epRx = ep; break }
+ }
+ val rx = epRx ?: run { UsbWifi.log("RTL8821AU: no bulk-IN endpoint"); onStatus("RTL8821AU: no RX endpoint"); return }
+ UsbWifi.log("=== RTL8821AU bring-up (88xxau) ===")
+ UsbWifi.log("RTL8821AU: RX EP=0x%02x".format(rx.address))
+
+ onStatus("RTL8821AU · powering on…")
+ resetMac(conn)
+ if (!powerOn(conn)) UsbWifi.log("RTL8821AU: power-on poll timed out (continuing)")
+ w16(conn, Rtl8821auTables.REG_CR, 0x0000)
+ w16(conn, Rtl8821auTables.REG_CR, Rtl8821auTables.CR_INIT16)
+ w16(conn, Rtl8821auTables.REG_RXFF_BNDY, Rtl8821auTables.RX_DMA_BOUNDARY)
+ w16(conn, Rtl8821auTables.REG_CR, r16(conn, Rtl8821auTables.REG_CR) or Rtl8821auTables.CR_MACEN8)
+ val cr = r16(conn, Rtl8821auTables.REG_CR)
+ UsbWifi.log("RTL8821AU: MAC on, REG_CR=0x%04x".format(cr))
+ if (cr == 0xEAEA) { onStatus("RTL8821AU: MAC didn't power on (replug)"); return }
+
+ onStatus("RTL8821AU · loading MAC/BB/RF…")
+ applyRegTable(conn, Rtl8821auTables.MAC_REG, rf = false)
+
+ run { val v = r8(conn, 0x02); w8(conn, 0x02, v or 0x04); w8(conn, 0x02, v or 0x07); w8(conn, 0x1F, 0x07) }
+ sleep(1)
+ applyRegTable(conn, Rtl8821auTables.PHY_REG, rf = false)
+ applyRegTable(conn, Rtl8821auTables.AGC_TAB, rf = false)
+ applyRegTable(conn, Rtl8821auTables.RADIOA, rf = true)
+
+ w8(conn, REG_RX_DRVINFO_SZ, 0x04)
+ w32(conn, Rtl8821auTables.REG_RCR, Rtl8821auTables.RCR_MONITOR or RCR_APP_PHYST_RXFF)
+ w16(conn, Rtl8821auTables.REG_RXFLTMAP0, Rtl8821auTables.RXFLTMAP_ALL)
+ w16(conn, Rtl8821auTables.REG_RXFLTMAP1, Rtl8821auTables.RXFLTMAP_ALL)
+ w16(conn, Rtl8821auTables.REG_RXFLTMAP2, Rtl8821auTables.RXFLTMAP_ALL)
+ channelTune(conn, HOP[0])
+ UsbWifi.log("RTL8821AU: RCR=0x%08x BB(0x800)=0x%08x — monitor live ch ${HOP[0]}".format(
+ r32(conn, Rtl8821auTables.REG_RCR), r32(conn, 0x800)))
+ onStatus("RTL8821AU · live · 2.4 GHz monitor")
+
+ rxLoop(conn, rx, onFrame, onStatus)
+ } catch (e: Exception) {
+ UsbWifi.log("RTL8821AU: bring-up error — ${e.message}")
+ onStatus("RTL8821AU: error · ${e.message}")
+ } finally {
+ runCatching { conn.close() }
+ running = false
+ }
+ }
+
+ private fun resetMac(conn: UsbDeviceConnection) {
+ runCatching {
+ val v = r8(conn, 0x05); w8(conn, 0x05, v or 0x02)
+ val end = System.currentTimeMillis() + 100
+ while ((r8(conn, 0x05) and 0x02) != 0 && System.currentTimeMillis() < end) sleep(1)
+ }
+ }
+
+ private fun powerOn(conn: UsbDeviceConnection): Boolean {
+ var ok = true
+ for (s in Rtl8821auTables.PWR_ON) {
+ val cmd = s[0]; val off = s[1]; val msk = s[2]; val v = s[3]
+ when (cmd) {
+ Rtl8821auTables.E -> return ok
+ Rtl8821auTables.D -> sleep(v)
+ Rtl8821auTables.W -> { val cur = r8(conn, off); w8(conn, off, (cur and msk.inv()) or (v and msk)) }
+ Rtl8821auTables.P -> {
+ var hit = false; val end = System.currentTimeMillis() + 500
+ while (System.currentTimeMillis() < end) { if ((r8(conn, off) and msk) == (v and msk)) { hit = true; break }; sleep(1) }
+ if (!hit) ok = false
+ }
+ }
+ }
+ return ok
+ }
+
+ private fun applyRegTable(conn: UsbDeviceConnection, tab: Array<IntArray>, rf: Boolean) {
+ for (e in tab) {
+ if (e[0] == Rtl8821auTables.DELAY) { sleep(e[1]); continue }
+ if (rf) rfWrite(conn, e[0], e[1]) else w32(conn, e[0], e[1])
+ }
+ }
+
+ private fun channelTune(conn: UsbDeviceConnection, ch: Int) {
+ for (op in Rtl8821auTables.channelTune(ch)) {
+ val kind = op[0]; val addr = op[1]; val mask = op[2]; val v = op[3]
+ when (kind) {
+ Rtl8821auTables.RF -> rfMask(conn, addr, mask, v)
+ Rtl8821auTables.MAC8 -> macMask(conn, addr, mask, v, 1)
+ Rtl8821auTables.MAC16 -> macMask(conn, addr, mask, v, 2)
+ else -> bbMask(conn, addr, mask, v)
+ }
+ }
+ }
+
+ private class RxState {
+ var total = 0L; var bufs = 0L; var lastLog = 0L; var hopAt = 0L; var lastEmit = 0L; var hopIdx = 0
+ fun start() { val t = System.currentTimeMillis(); lastLog = t; hopAt = t; lastEmit = t }
+ }
+
+ private fun rxTick(conn: UsbDeviceConnection, now: Long, st: RxState,
+ onFrame: (List<MonitorSighting>) -> Unit, onStatus: (String) -> Unit) {
+ if (now - st.lastLog > 3000) {
+ val shown = sightings.values.filter { it.trusted() && now - it.lastSeen < REPORT_MS }
+ val aps = shown.count { it.isAp }
+ UsbWifi.log("RTL8821AU: RX ${st.bufs} bufs / ${st.total} B ch ${HOP[st.hopIdx]} | ${shown.size} live ($aps AP · ${shown.size - aps} client, ${sightings.size} tracked)")
+ st.lastLog = now
+ }
+ if (now - st.lastEmit > 2000) { emitSightings(onFrame); st.lastEmit = now }
+ if (now - st.hopAt > DWELL_MS) {
+ val ni = (st.hopIdx + 1) % HOP.size
+ if (ni != st.hopIdx) { st.hopIdx = ni; runCatching { channelTune(conn, HOP[st.hopIdx]) } }
+ val shown = sightings.values.filter { it.trusted() && now - it.lastSeen < REPORT_MS }
+ val aps = shown.count { it.isAp }
+ onStatus("RTL8811AU / 8821AU · live · $aps access points · ${shown.size - aps} devices · ch ${HOP[st.hopIdx]}")
+ st.hopAt = now
+ }
+ }
+
+ private fun rxLoop(conn: UsbDeviceConnection, epRx: UsbEndpoint,
+ onFrame: (List<MonitorSighting>) -> Unit, onStatus: (String) -> Unit) {
+ val nbuf = 24; val bufSz = 16384
+ val buffers = Array(nbuf) { ByteBuffer.allocateDirect(bufSz) }
+ val reqs = ArrayList<UsbRequest>(nbuf)
+ var initOk = true
+ for (i in 0 until nbuf) {
+ val r = UsbRequest()
+ if (!r.initialize(conn, epRx)) { initOk = false; break }
+ r.clientData = i; buffers[i].clear()
+ if (!r.queue(buffers[i])) { initOk = false; break }
+ reqs.add(r)
+ }
+ if (!initOk) {
+ UsbWifi.log("RTL8821AU: async RX unavailable (${reqs.size}/$nbuf) — synchronous RX")
+ reqs.forEach { runCatching { it.cancel() }; runCatching { it.close() } }
+ rxLoopSync(conn, epRx, onFrame, onStatus); return
+ }
+ UsbWifi.log("RTL8821AU: async RX live — $nbuf buffers in flight")
+ val scratch = ByteArray(bufSz)
+ val st = RxState().apply { start() }
+ try {
+ while (!stop) {
+ val req = try { conn.requestWait(100L) } catch (e: TimeoutException) { null }
+ val now = System.currentTimeMillis()
+ if (req != null) {
+ val idx = req.clientData as? Int ?: -1
+ if (idx in 0 until nbuf) {
+ val b = buffers[idx]
+ val n = b.position()
+ val parseLen = if (n in 1..bufSz) {
+ st.total += n; st.bufs++
+ b.rewind(); b.get(scratch, 0, n); n
+ } else 0
+
+ b.clear()
+ if (!runCatching { req.queue(b) }.getOrDefault(false)) {
+ runCatching { req.close() }
+ val nr = UsbRequest()
+ if (runCatching { nr.initialize(conn, epRx) }.getOrDefault(false)) {
+ nr.clientData = idx; b.clear()
+ if (runCatching { nr.queue(b) }.getOrDefault(false)) reqs[idx] = nr
+ }
+ }
+ if (parseLen > 0) runCatching { parseRxBuf(scratch, parseLen, HOP[st.hopIdx]) }
+ }
+ }
+ rxTick(conn, now, st, onFrame, onStatus)
+ }
+ } finally {
+ reqs.forEach { runCatching { it.cancel() }; runCatching { it.close() } }
+ }
+ emitSightings(onFrame)
+ UsbWifi.log("RTL8821AU: RX loop stopped (${st.total} B)")
+ }
+
+ private fun rxLoopSync(conn: UsbDeviceConnection, epRx: UsbEndpoint,
+ onFrame: (List<MonitorSighting>) -> Unit, onStatus: (String) -> Unit) {
+ val buf = ByteArray(16384)
+ val st = RxState().apply { start() }
+ while (!stop) {
+ val n = conn.bulkTransfer(epRx, buf, buf.size, 500)
+ val now = System.currentTimeMillis()
+ if (n > 0) { st.total += n; st.bufs++; runCatching { parseRxBuf(buf, n, HOP[st.hopIdx]) } }
+ rxTick(conn, now, st, onFrame, onStatus)
+ }
+ emitSightings(onFrame)
+ UsbWifi.log("RTL8821AU: RX loop stopped (${st.total} B)")
+ }
+
+ private fun parseRxBuf(b: ByteArray, n: Int, ch: Int) {
+ val size = Rtl8821auTables.RXDESC_SIZE
+ var off = 0
+ while (off + size <= n) {
+ val d0 = le32(b, off)
+ val pkt = d0 and 0x3fff
+ if (pkt == 0 || pkt > 4096) break
+ val crc = (d0 ushr 14) and 1
+ val drv = ((d0 ushr 16) and 0xf) * 8
+ val shift = (d0 ushr 24) and 3
+ val fs = off + size + drv + shift
+ val fe = minOf(n, fs + pkt)
+ if (crc == 0 && fe - fs >= 24) {
+ val rate = b[off + 12].toInt() and 0x7f
+ val rssi = if (drv >= 8 && off + size + drv <= n) rtlRssi(b, off + size, rate) else NOMINAL_RSSI
+ parse80211(b, fs, fe, ch, rssi)
+ }
+ off += (size + drv + shift + pkt + 7) and 7.inv()
+ }
+ }
+
+ private fun parse80211(b: ByteArray, d: Int, end: Int, ch: Int, rssi: Int) {
+ fun u8(i: Int) = b[i].toInt() and 0xff
+ val fc = u8(d); val ftype = (fc shr 2) and 3; val sub = (fc shr 4) and 0xf
+ val fc1 = u8(d + 1); val toDs = fc1 and 0x01; val fromDs = (fc1 shr 1) and 0x01
+ val a1 = d + 4; val a2 = d + 10; val a3 = d + 16
+ fun mac(at: Int) = (at until at + 6).joinToString(":") { "%02x".format(u8(it)) }
+ fun isReal(m: String) = m != "ff:ff:ff:ff:ff:ff" && m != "00:00:00:00:00:00" &&
+ !m.startsWith("01:00:5e") && !m.startsWith("33:33") && !m.startsWith("01:80:c2")
+ fun readSsid(from: Int): String? {
+ var i = from
+ while (i + 2 <= end) {
+ val tag = u8(i); val tl = u8(i + 1)
+ if (i + 2 + tl > end) break
+ if (tag == 0) return String(b, i + 2, tl, Charsets.UTF_8).trim { it <= ' ' }
+ i += 2 + tl
+ }
+ return null
+ }
+ fun touch(m: String, ap: Boolean): Sighting? {
+ if (!isReal(m)) return null
+ val s = sightings.getOrPut(m) { Sighting(m, ap) }
+ s.hits++; s.good = true
+ if (rssi > s.rssi) s.rssi = rssi
+ s.channel = ch; s.lastSeen = System.currentTimeMillis()
+ return s
+ }
+ when (ftype) {
+ 0 -> when (sub) {
+ 8, 5 -> if (d + 36 <= end) {
+ val ssid = readSsid(d + 36)
+ touch(mac(a3), ap = true)?.let { if (ssid != null && it.ssid == null) it.ssid = ssid }
+ }
+ // Probe request: a device looking for networks (e.g. a laptop or a Flock camera).
+ 4 -> if (captureClients) touch(mac(a2), ap = false)
+ 0, 2, 10, 11, 12 -> if (captureClients) touch(mac(a2), ap = false)?.let { it.bssid = mac(a3) }
+ }
+ 2 -> {
+ if (captureClients) when {
+ toDs == 1 && fromDs == 0 -> touch(mac(a2), ap = false)?.let { it.bssid = mac(a1) }
+ toDs == 0 && fromDs == 1 -> touch(mac(a1), ap = false)?.let { it.bssid = mac(a2) }
+ else -> touch(mac(a2), ap = false)
+ }
+ }
+ }
+ }
+
+ private fun rtlRssi(b: ByteArray, di: Int, rate: Int): Int {
+ fun u8(i: Int) = b[i].toInt() and 0xff
+ return if (rate <= 3) {
+ val agc = u8(di + 5); val lna = (agc and 0xE0) shr 5; val vga = agc and 0x1F
+ when (lna) {
+ 7 -> if (vga <= 27) -94 + 2 * (27 - vga) else -94
+ 6 -> -42 + 2 * (2 - vga)
+ 5 -> -36 + 2 * (7 - vga)
+ 4 -> -30 + 2 * (7 - vga)
+ 3 -> -18 + 2 * (7 - vga)
+ 2 -> 2 * (5 - vga)
+ 1 -> 14 - 2 * vga
+ else -> 20 - 2 * vga
+ }
+ } else {
+ ((u8(di + 4) shr 1) and 0x7f) - 110
+ }
+ }
+
+ private fun emitSightings(onFrame: (List<MonitorSighting>) -> Unit) {
+ val now = System.currentTimeMillis()
+ val batch = sightings.values.filter { it.trusted() && now - it.lastSeen < FRESH_MS }.map { s ->
+ MonitorSighting(
+ mac = s.mac,
+ ssid = s.ssid,
+ rssi = s.rssi.coerceIn(-120, -20),
+ frequencyMhz = if (s.channel > 0) freqOf(s.channel) else 0,
+ isAccessPoint = s.isAp,
+ bssid = s.bssid
+ )
+ }
+ sightings.entries.removeAll { now - it.value.lastSeen > PRUNE_MS }
+ if (batch.isNotEmpty()) onFrame(batch)
+ }
+
+ private fun r(conn: UsbDeviceConnection, addr: Int, len: Int): ByteArray {
+ val b = ByteArray(len)
+ return if (conn.controlTransfer(IN_VENDOR, VREQ, addr, 0, b, len, 500) == len) b else ByteArray(len)
+ }
+ private fun w(conn: UsbDeviceConnection, addr: Int, data: ByteArray) {
+ conn.controlTransfer(OUT_VENDOR, VREQ, addr, 0, data, data.size, 500)
+ }
+ private fun r8(conn: UsbDeviceConnection, a: Int) = r(conn, a, 1)[0].toInt() and 0xff
+ private fun r16(conn: UsbDeviceConnection, a: Int): Int { val b = r(conn, a, 2); return (b[0].toInt() and 0xff) or ((b[1].toInt() and 0xff) shl 8) }
+ private fun r32(conn: UsbDeviceConnection, a: Int) = le32(r(conn, a, 4), 0)
+ private fun w8(conn: UsbDeviceConnection, a: Int, v: Int) = w(conn, a, byteArrayOf(v.toByte()))
+ private fun w16(conn: UsbDeviceConnection, a: Int, v: Int) = w(conn, a, byteArrayOf(v.toByte(), (v ushr 8).toByte()))
+ private fun w32(conn: UsbDeviceConnection, a: Int, v: Int) = w(conn, a, byteArrayOf(v.toByte(), (v ushr 8).toByte(), (v ushr 16).toByte(), (v ushr 24).toByte()))
+
+ private fun bbMask(conn: UsbDeviceConnection, a: Int, mask: Int, v: Int) {
+ val cur = r32(conn, a); w32(conn, a, (cur and mask.inv()) or ((v shl sh(mask)) and mask))
+ }
+ private fun macMask(conn: UsbDeviceConnection, a: Int, mask: Int, v: Int, width: Int) {
+ val s = sh(mask)
+ if (width == 1) { val cur = r8(conn, a); val m = mask and 0xff; w8(conn, a, (cur and m.inv()) or ((v shl s) and m)) }
+ else { val cur = r16(conn, a); val m = mask and 0xffff; w16(conn, a, (cur and m.inv()) or ((v shl s) and m)) }
+ }
+
+ private fun rfWrite(conn: UsbDeviceConnection, addr: Int, v: Int) {
+ val d = v and 0xfffff
+ w32(conn, rA_LSSIWrite, (((addr and 0xff) shl 20) or d) and 0x0fffffff)
+ rfShadow[addr] = d
+ }
+ private fun rfRead(conn: UsbDeviceConnection, addr: Int): Int {
+ rfShadow[addr]?.let { return it }
+ bbMask(conn, rHSSIRead, 0xff, addr and 0xff); sleep(1)
+ return r32(conn, rA_PIRead) and 0xfffff
+ }
+ private fun rfMask(conn: UsbDeviceConnection, addr: Int, mask: Int, v: Int) {
+ val cur = rfRead(conn, addr); rfWrite(conn, addr, (cur and mask.inv()) or ((v shl sh(mask)) and mask))
+ }
+
+ private fun sh(mask: Int) = Integer.numberOfTrailingZeros(mask)
+ private fun le32(b: ByteArray, i: Int) = (b[i].toInt() and 0xff) or ((b[i+1].toInt() and 0xff) shl 8) or ((b[i+2].toInt() and 0xff) shl 16) or ((b[i+3].toInt() and 0xff) shl 24)
+ private fun sleep(ms: Int) = try { Thread.sleep(ms.toLong()) } catch (_: InterruptedException) {}
+}
diff --git a/app/src/main/java/com/rfsentinel/app/usb/Rtl8821auTables.kt b/app/src/main/java/com/rfsentinel/app/usb/Rtl8821auTables.kt
new file mode 100644
index 0000000..addc4dc
--- /dev/null
+++ b/app/src/main/java/com/rfsentinel/app/usb/Rtl8821auTables.kt
@@ -0,0 +1,767 @@
+package com.rfsentinel.app.usb
+
+/*
+ * RTL8811AU / RTL8821AU register tables (power-on sequence, MAC/BB/AGC/RF tables,
+ * channel tuning) for receive-only monitor mode over USB.
+ *
+ * Ported unchanged from Wardrive Go by RocketGod
+ * (https://github.com/RocketGod-git/wardrive-go, GPL-3.0); the values come from
+ * the Realtek 88xxau Linux driver. Used under the GNU GPL v3.
+ */
+
+object Rtl8821auTables {
+
+ const val W = 0; const val P = 1; const val D = 2; const val E = 3
+
+ const val DELAY = -2
+
+ const val BB = 0; const val RF = 1; const val MAC8 = 2; const val MAC16 = 3
+
+ val PWR_ON = arrayOf(
+ intArrayOf(W, 0x20, 0x1, 0x1),
+ intArrayOf(W, 0x67, 0x10, 0x0),
+ intArrayOf(D, 0x0, 0x0, 0x1),
+ intArrayOf(W, 0x0, 0x20, 0x0),
+ intArrayOf(W, 0x5, 0x1C, 0x0),
+ intArrayOf(P, 0x6, 0x2, 0x2),
+ intArrayOf(W, 0x6, 0x1, 0x1),
+ intArrayOf(W, 0x5, 0x80, 0x0),
+ intArrayOf(W, 0x5, 0x18, 0x0),
+ intArrayOf(W, 0x5, 0x1, 0x1),
+ intArrayOf(P, 0x5, 0x1, 0x0),
+ intArrayOf(W, 0x4F, 0x1, 0x1),
+ intArrayOf(W, 0x67, 0x30, 0x30),
+ intArrayOf(W, 0x25, 0x40, 0x0),
+ intArrayOf(W, 0x2E, 0xFF, 0x82),
+ intArrayOf(E, 0x0, 0x0, 0x0)
+ )
+
+ val MAC_REG = arrayOf(
+ intArrayOf(0x421, 0xF),
+ intArrayOf(0x428, 0xA),
+ intArrayOf(0x429, 0x10),
+ intArrayOf(0x430, 0x0),
+ intArrayOf(0x431, 0x0),
+ intArrayOf(0x432, 0x0),
+ intArrayOf(0x433, 0x1),
+ intArrayOf(0x434, 0x4),
+ intArrayOf(0x435, 0x5),
+ intArrayOf(0x436, 0x7),
+ intArrayOf(0x437, 0x8),
+ intArrayOf(0x43C, 0x4),
+ intArrayOf(0x43D, 0x5),
+ intArrayOf(0x43E, 0x7),
+ intArrayOf(0x43F, 0x8),
+ intArrayOf(0x440, 0x5D),
+ intArrayOf(0x441, 0x1),
+ intArrayOf(0x442, 0x0),
+ intArrayOf(0x444, 0x10),
+ intArrayOf(0x445, 0x0),
+ intArrayOf(0x446, 0x0),
+ intArrayOf(0x447, 0x0),
+ intArrayOf(0x448, 0x0),
+ intArrayOf(0x449, 0xF0),
+ intArrayOf(0x44A, 0xF),
+ intArrayOf(0x44B, 0x3E),
+ intArrayOf(0x44C, 0x10),
+ intArrayOf(0x44D, 0x0),
+ intArrayOf(0x44E, 0x0),
+ intArrayOf(0x44F, 0x0),
+ intArrayOf(0x450, 0x0),
+ intArrayOf(0x451, 0xF0),
+ intArrayOf(0x452, 0xF),
+ intArrayOf(0x453, 0x0),
+ intArrayOf(0x456, 0x5E),
+ intArrayOf(0x460, 0x66),
+ intArrayOf(0x461, 0x66),
+ intArrayOf(0x4C8, 0x3F),
+ intArrayOf(0x4C9, 0xFF),
+ intArrayOf(0x4CC, 0xFF),
+ intArrayOf(0x4CD, 0xFF),
+ intArrayOf(0x4CE, 0x1),
+ intArrayOf(0x500, 0x26),
+ intArrayOf(0x501, 0xA2),
+ intArrayOf(0x502, 0x2F),
+ intArrayOf(0x503, 0x0),
+ intArrayOf(0x504, 0x28),
+ intArrayOf(0x505, 0xA3),
+ intArrayOf(0x506, 0x5E),
+ intArrayOf(0x507, 0x0),
+ intArrayOf(0x508, 0x2B),
+ intArrayOf(0x509, 0xA4),
+ intArrayOf(0x50A, 0x5E),
+ intArrayOf(0x50B, 0x0),
+ intArrayOf(0x50C, 0x4F),
+ intArrayOf(0x50D, 0xA4),
+ intArrayOf(0x50E, 0x0),
+ intArrayOf(0x50F, 0x0),
+ intArrayOf(0x512, 0x1C),
+ intArrayOf(0x514, 0xA),
+ intArrayOf(0x516, 0xA),
+ intArrayOf(0x525, 0x4F),
+ intArrayOf(0x550, 0x10),
+ intArrayOf(0x551, 0x10),
+ intArrayOf(0x559, 0x2),
+ intArrayOf(0x55C, 0x50),
+ intArrayOf(0x55D, 0xFF),
+ intArrayOf(0x605, 0x30),
+ intArrayOf(0x607, 0x7),
+ intArrayOf(0x608, 0xE),
+ intArrayOf(0x609, 0x2A),
+ intArrayOf(0x620, 0xFF),
+ intArrayOf(0x621, 0xFF),
+ intArrayOf(0x622, 0xFF),
+ intArrayOf(0x623, 0xFF),
+ intArrayOf(0x624, 0xFF),
+ intArrayOf(0x625, 0xFF),
+ intArrayOf(0x626, 0xFF),
+ intArrayOf(0x627, 0xFF),
+ intArrayOf(0x638, 0x50),
+ intArrayOf(0x63C, 0xA),
+ intArrayOf(0x63D, 0xA),
+ intArrayOf(0x63E, 0xE),
+ intArrayOf(0x63F, 0xE),
+ intArrayOf(0x640, 0x40),
+ intArrayOf(0x642, 0x40),
+ intArrayOf(0x643, 0x0),
+ intArrayOf(0x652, 0xC8),
+ intArrayOf(0x66E, 0x5),
+ intArrayOf(0x700, 0x21),
+ intArrayOf(0x701, 0x43),
+ intArrayOf(0x702, 0x65),
+ intArrayOf(0x703, 0x87),
+ intArrayOf(0x708, 0x21),
+ intArrayOf(0x709, 0x43),
+ intArrayOf(0x70A, 0x65),
+ intArrayOf(0x70B, 0x87),
+ intArrayOf(0x718, 0x40)
+ )
+
+ val PHY_REG = arrayOf(
+ intArrayOf(0x800, 0x20D090),
+ intArrayOf(0x804, 0x80112E0),
+ intArrayOf(0x808, 0xE028211),
+ intArrayOf(0x80C, 0x92131111.toInt()),
+ intArrayOf(0x810, 0x20101261),
+ intArrayOf(0x814, 0x20C3D10),
+ intArrayOf(0x818, 0x3A00385),
+ intArrayOf(0x820, 0x0),
+ intArrayOf(0x824, 0x30FE0),
+ intArrayOf(0x828, 0x0),
+ intArrayOf(0x82C, 0x2081DD),
+ intArrayOf(0x830, 0x2AAAEEC8),
+ intArrayOf(0x834, 0x37A706),
+ intArrayOf(0x838, 0x6489B44),
+ intArrayOf(0x83C, 0x95B),
+ intArrayOf(0x840, 0xC0000001.toInt()),
+ intArrayOf(0x844, 0x40003CDE),
+ intArrayOf(0x848, 0x62103F8B),
+ intArrayOf(0x84C, 0x6CFDFFB8),
+ intArrayOf(0x850, 0x28874706),
+ intArrayOf(0x854, 0x1520C),
+ intArrayOf(0x858, 0x8060E000.toInt()),
+ intArrayOf(0x85C, 0x74210168),
+ intArrayOf(0x860, 0x6929C321),
+ intArrayOf(0x864, 0x79727432),
+ intArrayOf(0x868, 0x8CA7A314.toInt()),
+ intArrayOf(0x86C, 0x888C2878.toInt()),
+ intArrayOf(0x870, 0x8888888),
+ intArrayOf(0x874, 0x31612C2E),
+ intArrayOf(0x878, 0x152),
+ intArrayOf(0x87C, 0xFD000),
+ intArrayOf(0x8A0, 0x13),
+ intArrayOf(0x8A4, 0x7F7F7F7F),
+ intArrayOf(0x8A8, 0xA2000338.toInt()),
+ intArrayOf(0x8AC, 0xFF0FA0A),
+ intArrayOf(0x8B4, 0xFC080),
+ intArrayOf(0x8B8, 0x6C10D7FF),
+ intArrayOf(0x8BC, 0xCA52090),
+ intArrayOf(0x8C0, 0x1BF00020),
+ intArrayOf(0x8C4, 0x0),
+ intArrayOf(0x8C8, 0x13169),
+ intArrayOf(0x8CC, 0x8248492),
+ intArrayOf(0x8D4, 0x940008A0.toInt()),
+ intArrayOf(0x8D8, 0x290B5612),
+ intArrayOf(0x8F8, 0x400002C0),
+ intArrayOf(0x8FC, 0x0),
+ intArrayOf(0x900, 0x700),
+ intArrayOf(0x90C, 0x0),
+ intArrayOf(0x910, 0xFC00),
+ intArrayOf(0x914, 0x404),
+ intArrayOf(0x918, 0x1C1028C0),
+ intArrayOf(0x91C, 0x64B11A1C),
+ intArrayOf(0x920, 0xE0767233.toInt()),
+ intArrayOf(0x924, 0x55AA500),
+ intArrayOf(0x928, 0x4),
+ intArrayOf(0x92C, 0xFFFE0000.toInt()),
+ intArrayOf(0x930, 0xFFFFFFFE.toInt()),
+ intArrayOf(0x934, 0x1FFFFF),
+ intArrayOf(0x960, 0x0),
+ intArrayOf(0x964, 0x0),
+ intArrayOf(0x968, 0x0),
+ intArrayOf(0x96C, 0x0),
+ intArrayOf(0x970, 0x801FFFFF.toInt()),
+ intArrayOf(0x974, 0x3FF),
+ intArrayOf(0x978, 0x0),
+ intArrayOf(0x97C, 0x0),
+ intArrayOf(0x980, 0x0),
+ intArrayOf(0x984, 0x0),
+ intArrayOf(0x988, 0x0),
+ intArrayOf(0x990, 0x27100000),
+ intArrayOf(0x994, 0xFFFF0100.toInt()),
+ intArrayOf(0x998, 0xFFFFFF5C.toInt()),
+ intArrayOf(0x99C, 0xFFFFFFFF.toInt()),
+ intArrayOf(0x9A0, 0xFF),
+ intArrayOf(0x9A4, 0x480080),
+ intArrayOf(0x9A8, 0x0),
+ intArrayOf(0x9AC, 0x0),
+ intArrayOf(0x9B0, 0x81081008.toInt()),
+ intArrayOf(0x9B4, 0x1081008),
+ intArrayOf(0x9B8, 0x1081008),
+ intArrayOf(0x9BC, 0x1081008),
+ intArrayOf(0x9D0, 0x0),
+ intArrayOf(0x9D4, 0x0),
+ intArrayOf(0x9D8, 0x0),
+ intArrayOf(0x9DC, 0x0),
+ intArrayOf(0x9E0, 0x5D00),
+ intArrayOf(0x9E4, 0x3),
+ intArrayOf(0x9E8, 0x1),
+ intArrayOf(0xA00, 0xD047C8),
+ intArrayOf(0xA04, 0x1FF800C),
+ intArrayOf(0xA08, 0x8C8A8300.toInt()),
+ intArrayOf(0xA0C, 0x2E68000F),
+ intArrayOf(0xA10, 0x9500BB78.toInt()),
+ intArrayOf(0xA14, 0x11144028),
+ intArrayOf(0xA18, 0x881117),
+ intArrayOf(0xA1C, 0x89140F00.toInt()),
+ intArrayOf(0xA20, 0x1A1B0000),
+ intArrayOf(0xA24, 0x90E1317),
+ intArrayOf(0xA28, 0x204),
+ intArrayOf(0xA2C, 0x900000),
+ intArrayOf(0xA70, 0x101FFF00),
+ intArrayOf(0xA74, 0x8),
+ intArrayOf(0xA78, 0x900),
+ intArrayOf(0xA7C, 0x225B0606),
+ intArrayOf(0xA80, 0x21805490),
+ intArrayOf(0xA84, 0x1F0000),
+ intArrayOf(0xB00, 0x3100040),
+ intArrayOf(0xB04, 0xB000),
+ intArrayOf(0xB08, 0xAE0201EB.toInt()),
+ intArrayOf(0xB0C, 0x1003207),
+ intArrayOf(0xB10, 0x9807),
+ intArrayOf(0xB14, 0x1000000),
+ intArrayOf(0xB18, 0x2),
+ intArrayOf(0xB1C, 0x2),
+ intArrayOf(0xB20, 0x1F),
+ intArrayOf(0xB24, 0x3020100),
+ intArrayOf(0xB28, 0x7060504),
+ intArrayOf(0xB2C, 0xB0A0908),
+ intArrayOf(0xB30, 0xF0E0D0C),
+ intArrayOf(0xB34, 0x13121110),
+ intArrayOf(0xB38, 0x17161514),
+ intArrayOf(0xB3C, 0x3A),
+ intArrayOf(0xB40, 0x0),
+ intArrayOf(0xB44, 0x0),
+ intArrayOf(0xB48, 0x13000032),
+ intArrayOf(0xB4C, 0x48080000),
+ intArrayOf(0xB50, 0x0),
+ intArrayOf(0xB54, 0x0),
+ intArrayOf(0xB58, 0x0),
+ intArrayOf(0xB5C, 0x0),
+ intArrayOf(0xC00, 0x7),
+ intArrayOf(0xC04, 0x42020),
+ intArrayOf(0xC08, 0x80410231.toInt()),
+ intArrayOf(0xC0C, 0x0),
+ intArrayOf(0xC10, 0x100),
+ intArrayOf(0xC14, 0x1000000),
+ intArrayOf(0xC1C, 0x40000003),
+ intArrayOf(0xC20, 0x2C2C2C2C),
+ intArrayOf(0xC24, 0x30303030),
+ intArrayOf(0xC28, 0x30303030),
+ intArrayOf(0xC2C, 0x2C2C2C2C),
+ intArrayOf(0xC30, 0x2C2C2C2C),
+ intArrayOf(0xC34, 0x2C2C2C2C),
+ intArrayOf(0xC38, 0x2C2C2C2C),
+ intArrayOf(0xC3C, 0x2A2A2A2A),
+ intArrayOf(0xC40, 0x2A2A2A2A),
+ intArrayOf(0xC44, 0x2A2A2A2A),
+ intArrayOf(0xC48, 0x2A2A2A2A),
+ intArrayOf(0xC4C, 0x2A2A2A2A),
+ intArrayOf(0xC50, 0x20),
+ intArrayOf(0xC54, 0x1C1208),
+ intArrayOf(0xC58, 0x30000C1C),
+ intArrayOf(0xC5C, 0x58),
+ intArrayOf(0xC60, 0x34344443),
+ intArrayOf(0xC64, 0x7003333),
+ intArrayOf(0xC68, 0x19791979),
+ intArrayOf(0xC6C, 0x19791979),
+ intArrayOf(0xC70, 0x19791979),
+ intArrayOf(0xC74, 0x19791979),
+ intArrayOf(0xC78, 0x19791979),
+ intArrayOf(0xC7C, 0x19791979),
+ intArrayOf(0xC80, 0x19791979),
+ intArrayOf(0xC84, 0x19791979),
+ intArrayOf(0xC94, 0x100005C),
+ intArrayOf(0xC98, 0x0),
+ intArrayOf(0xC9C, 0x0),
+ intArrayOf(0xCA0, 0x29),
+ intArrayOf(0xCA4, 0x8040201),
+ intArrayOf(0xCA8, 0x80402010.toInt()),
+ intArrayOf(0xCB0, 0x77775747),
+ intArrayOf(0xCB4, 0x10000077),
+ intArrayOf(0xCB8, 0x508240)
+ )
+
+ val AGC_TAB = arrayOf(
+ intArrayOf(0x81C, 0xBF000001.toInt()),
+ intArrayOf(0x81C, 0xBF020001.toInt()),
+ intArrayOf(0x81C, 0xBF040001.toInt()),
+ intArrayOf(0x81C, 0xBF060001.toInt()),
+ intArrayOf(0x81C, 0xBE080001.toInt()),
+ intArrayOf(0x81C, 0xBD0A0001.toInt()),
+ intArrayOf(0x81C, 0xBC0C0001.toInt()),
+ intArrayOf(0x81C, 0xBA0E0001.toInt()),
+ intArrayOf(0x81C, 0xB9100001.toInt()),
+ intArrayOf(0x81C, 0xB8120001.toInt()),
+ intArrayOf(0x81C, 0xB7140001.toInt()),
+ intArrayOf(0x81C, 0xB6160001.toInt()),
+ intArrayOf(0x81C, 0xB5180001.toInt()),
+ intArrayOf(0x81C, 0xB41A0001.toInt()),
+ intArrayOf(0x81C, 0xB31C0001.toInt()),
+ intArrayOf(0x81C, 0xB21E0001.toInt()),
+ intArrayOf(0x81C, 0xB1200001.toInt()),
+ intArrayOf(0x81C, 0xB0220001.toInt()),
+ intArrayOf(0x81C, 0xAF240001.toInt()),
+ intArrayOf(0x81C, 0xAE260001.toInt()),
+ intArrayOf(0x81C, 0xAD280001.toInt()),
+ intArrayOf(0x81C, 0xAC2A0001.toInt()),
+ intArrayOf(0x81C, 0xAB2C0001.toInt()),
+ intArrayOf(0x81C, 0xAA2E0001.toInt()),
+ intArrayOf(0x81C, 0xA9300001.toInt()),
+ intArrayOf(0x81C, 0xA8320001.toInt()),
+ intArrayOf(0x81C, 0xA7340001.toInt()),
+ intArrayOf(0x81C, 0xA6360001.toInt()),
+ intArrayOf(0x81C, 0xA5380001.toInt()),
+ intArrayOf(0x81C, 0xA43A0001.toInt()),
+ intArrayOf(0x81C, 0x683C0001),
+ intArrayOf(0x81C, 0x673E0001),
+ intArrayOf(0x81C, 0x66400001),
+ intArrayOf(0x81C, 0x65420001),
+ intArrayOf(0x81C, 0x64440001),
+ intArrayOf(0x81C, 0x63460001),
+ intArrayOf(0x81C, 0x62480001),
+ intArrayOf(0x81C, 0x614A0001),
+ intArrayOf(0x81C, 0x474C0001),
+ intArrayOf(0x81C, 0x464E0001),
+ intArrayOf(0x81C, 0x45500001),
+ intArrayOf(0x81C, 0x44520001),
+ intArrayOf(0x81C, 0x43540001),
+ intArrayOf(0x81C, 0x42560001),
+ intArrayOf(0x81C, 0x41580001),
+ intArrayOf(0x81C, 0x285A0001),
+ intArrayOf(0x81C, 0x275C0001),
+ intArrayOf(0x81C, 0x265E0001),
+ intArrayOf(0x81C, 0x25600001),
+ intArrayOf(0x81C, 0x24620001),
+ intArrayOf(0x81C, 0xA640001),
+ intArrayOf(0x81C, 0x9660001),
+ intArrayOf(0x81C, 0x8680001),
+ intArrayOf(0x81C, 0x76A0001),
+ intArrayOf(0x81C, 0x66C0001),
+ intArrayOf(0x81C, 0x56E0001),
+ intArrayOf(0x81C, 0x4700001),
+ intArrayOf(0x81C, 0x3720001),
+ intArrayOf(0x81C, 0x2740001),
+ intArrayOf(0x81C, 0x1760001),
+ intArrayOf(0x81C, 0x1780001),
+ intArrayOf(0x81C, 0x17A0001),
+ intArrayOf(0x81C, 0x17C0001),
+ intArrayOf(0x81C, 0x17E0001),
+ intArrayOf(0x81C, 0xFF000101.toInt()),
+ intArrayOf(0x81C, 0xFF020101.toInt()),
+ intArrayOf(0x81C, 0xFE040101.toInt()),
+ intArrayOf(0x81C, 0xFD060101.toInt()),
+ intArrayOf(0x81C, 0xFC080101.toInt()),
+ intArrayOf(0x81C, 0xFD0A0101.toInt()),
+ intArrayOf(0x81C, 0xFC0C0101.toInt()),
+ intArrayOf(0x81C, 0xFB0E0101.toInt()),
+ intArrayOf(0x81C, 0xFA100101.toInt()),
+ intArrayOf(0x81C, 0xF9120101.toInt()),
+ intArrayOf(0x81C, 0xF8140101.toInt()),
+ intArrayOf(0x81C, 0xF7160101.toInt()),
+ intArrayOf(0x81C, 0xF6180101.toInt()),
+ intArrayOf(0x81C, 0xF51A0101.toInt()),
+ intArrayOf(0x81C, 0xF41C0101.toInt()),
+ intArrayOf(0x81C, 0xF31E0101.toInt()),
+ intArrayOf(0x81C, 0xF2200101.toInt()),
+ intArrayOf(0x81C, 0xF1220101.toInt()),
+ intArrayOf(0x81C, 0xF0240101.toInt()),
+ intArrayOf(0x81C, 0xEF260101.toInt()),
+ intArrayOf(0x81C, 0xEE280101.toInt()),
+ intArrayOf(0x81C, 0xED2A0101.toInt()),
+ intArrayOf(0x81C, 0xEC2C0101.toInt()),
+ intArrayOf(0x81C, 0xEB2E0101.toInt()),
+ intArrayOf(0x81C, 0xEA300101.toInt()),
+ intArrayOf(0x81C, 0xE9320101.toInt()),
+ intArrayOf(0x81C, 0xE8340101.toInt()),
+ intArrayOf(0x81C, 0xE7360101.toInt()),
+ intArrayOf(0x81C, 0xE6380101.toInt()),
+ intArrayOf(0x81C, 0xE53A0101.toInt()),
+ intArrayOf(0x81C, 0xE43C0101.toInt()),
+ intArrayOf(0x81C, 0xE33E0101.toInt()),
+ intArrayOf(0x81C, 0xA5400101.toInt()),
+ intArrayOf(0x81C, 0xA4420101.toInt()),
+ intArrayOf(0x81C, 0xA3440101.toInt()),
+ intArrayOf(0x81C, 0x87460101.toInt()),
+ intArrayOf(0x81C, 0x86480101.toInt()),
+ intArrayOf(0x81C, 0x854A0101.toInt()),
+ intArrayOf(0x81C, 0x844C0101.toInt()),
+ intArrayOf(0x81C, 0x694E0101),
+ intArrayOf(0x81C, 0x68500101),
+ intArrayOf(0x81C, 0x67520101),
+ intArrayOf(0x81C, 0x66540101),
+ intArrayOf(0x81C, 0x49560101),
+ intArrayOf(0x81C, 0x48580101),
+ intArrayOf(0x81C, 0x475A0101),
+ intArrayOf(0x81C, 0x2A5C0101),
+ intArrayOf(0x81C, 0x295E0101),
+ intArrayOf(0x81C, 0x28600101),
+ intArrayOf(0x81C, 0x27620101),
+ intArrayOf(0x81C, 0x26640101),
+ intArrayOf(0x81C, 0x25660101),
+ intArrayOf(0x81C, 0x24680101),
+ intArrayOf(0x81C, 0x236A0101),
+ intArrayOf(0x81C, 0x56C0101),
+ intArrayOf(0x81C, 0x46E0101),
+ intArrayOf(0x81C, 0x3700101),
+ intArrayOf(0x81C, 0x2720101),
+ intArrayOf(0x81C, 0x1740101),
+ intArrayOf(0x81C, 0x1760101),
+ intArrayOf(0x81C, 0x1780101),
+ intArrayOf(0x81C, 0x17A0101),
+ intArrayOf(0x81C, 0x17C0101),
+ intArrayOf(0x81C, 0x17E0101),
+ intArrayOf(0xC50, 0x22),
+ intArrayOf(0xC50, 0x20)
+ )
+
+ val RADIOA = arrayOf(
+ intArrayOf(0x18, 0x1712A),
+ intArrayOf(0x56, 0x51CF2),
+ intArrayOf(0x66, 0x40000),
+ intArrayOf(0x0, 0x10000),
+ intArrayOf(0x1E, 0x80000),
+ intArrayOf(0x82, 0x830),
+ intArrayOf(0x83, 0x21800),
+ intArrayOf(0x84, 0x28000),
+ intArrayOf(0x85, 0x48000),
+ intArrayOf(0x86, 0x94838),
+ intArrayOf(0x87, 0x44980),
+ intArrayOf(0x88, 0x48000),
+ intArrayOf(0x89, 0xD480),
+ intArrayOf(0x8A, 0x42240),
+ intArrayOf(0x8B, 0xF0380),
+ intArrayOf(0x8C, 0x90000),
+ intArrayOf(0x8D, 0x22852),
+ intArrayOf(0x8E, 0x65540),
+ intArrayOf(0x8F, 0x88001),
+ intArrayOf(0xEF, 0x20000),
+ intArrayOf(0x3E, 0x380),
+ intArrayOf(0x3F, 0x90018),
+ intArrayOf(0x3E, 0x20380),
+ intArrayOf(0x3F, 0xA0018),
+ intArrayOf(0x3E, 0x40308),
+ intArrayOf(0x3F, 0xA0018),
+ intArrayOf(0x3E, 0x60018),
+ intArrayOf(0x3F, 0xA0018),
+ intArrayOf(0xEF, 0x0),
+ intArrayOf(0x18, 0x1712A),
+ intArrayOf(0x89, 0x80),
+ intArrayOf(0x8B, 0x80180),
+ intArrayOf(0xEF, 0x1000),
+ intArrayOf(0x3A, 0x244),
+ intArrayOf(0x3B, 0x38027),
+ intArrayOf(0x3C, 0x82000),
+ intArrayOf(0x3A, 0x244),
+ intArrayOf(0x3B, 0x30113),
+ intArrayOf(0x3C, 0x82000),
+ intArrayOf(0x3A, 0x14C),
+ intArrayOf(0x3B, 0x28027),
+ intArrayOf(0x3C, 0x82000),
+ intArrayOf(0x3A, 0xCC),
+ intArrayOf(0x3B, 0x27027),
+ intArrayOf(0x3C, 0x42000),
+ intArrayOf(0x3A, 0x14C),
+ intArrayOf(0x3B, 0x1F913),
+ intArrayOf(0x3C, 0x42000),
+ intArrayOf(0x3A, 0x10C),
+ intArrayOf(0x3B, 0x17F10),
+ intArrayOf(0x3C, 0x12000),
+ intArrayOf(0x3A, 0xD0),
+ intArrayOf(0x3B, 0x8027),
+ intArrayOf(0x3C, 0xCA000),
+ intArrayOf(0x3A, 0x244),
+ intArrayOf(0x3B, 0x78027),
+ intArrayOf(0x3C, 0x82000),
+ intArrayOf(0x3A, 0x244),
+ intArrayOf(0x3B, 0x70113),
+ intArrayOf(0x3C, 0x82000),
+ intArrayOf(0x3A, 0x14C),
+ intArrayOf(0x3B, 0x68027),
+ intArrayOf(0x3C, 0x82000),
+ intArrayOf(0x3A, 0xCC),
+ intArrayOf(0x3B, 0x67027),
+ intArrayOf(0x3C, 0x42000),
+ intArrayOf(0x3A, 0x14C),
+ intArrayOf(0x3B, 0x5F913),
+ intArrayOf(0x3C, 0x42000),
+ intArrayOf(0x3A, 0x10C),
+ intArrayOf(0x3B, 0x57F10),
+ intArrayOf(0x3C, 0x12000),
+ intArrayOf(0x3A, 0xD0),
+ intArrayOf(0x3B, 0x48027),
+ intArrayOf(0x3C, 0xCA000),
+ intArrayOf(0x3A, 0x244),
+ intArrayOf(0x3B, 0xB8027),
+ intArrayOf(0x3C, 0x82000),
+ intArrayOf(0x3A, 0x244),
+ intArrayOf(0x3B, 0xB0113),
+ intArrayOf(0x3C, 0x82000),
+ intArrayOf(0x3A, 0x14C),
+ intArrayOf(0x3B, 0xA8027),
+ intArrayOf(0x3C, 0x82000),
+ intArrayOf(0x3A, 0xCC),
+ intArrayOf(0x3B, 0xA7027),
+ intArrayOf(0x3C, 0x42000),
+ intArrayOf(0x3A, 0x14C),
+ intArrayOf(0x3B, 0x9F913),
+ intArrayOf(0x3C, 0x42000),
+ intArrayOf(0x3A, 0x10C),
+ intArrayOf(0x3B, 0x97F10),
+ intArrayOf(0x3C, 0x12000),
+ intArrayOf(0x3A, 0xD0),
+ intArrayOf(0x3B, 0x88027),
+ intArrayOf(0x3C, 0xCA000),
+ intArrayOf(0xEF, 0x0),
+ intArrayOf(0xEF, 0x1100),
+ intArrayOf(0x34, 0x4ADF5),
+ intArrayOf(0x34, 0x49DF2),
+ intArrayOf(0x34, 0x48DEF),
+ intArrayOf(0x34, 0x47DEC),
+ intArrayOf(0x34, 0x46DE9),
+ intArrayOf(0x34, 0x45CCB),
+ intArrayOf(0x34, 0x4488D),
+ intArrayOf(0x34, 0x4348D),
+ intArrayOf(0x34, 0x4248A),
+ intArrayOf(0x34, 0x4108D),
+ intArrayOf(0x34, 0x4008A),
+ intArrayOf(0x34, 0x2ADF4),
+ intArrayOf(0x34, 0x29DF1),
+ intArrayOf(0x34, 0x28DEE),
+ intArrayOf(0x34, 0x27DEB),
+ intArrayOf(0x34, 0x26CCD),
+ intArrayOf(0x34, 0x25CCA),
+ intArrayOf(0x34, 0x2488C),
+ intArrayOf(0x34, 0x2384C),
+ intArrayOf(0x34, 0x22849),
+ intArrayOf(0x34, 0x21449),
+ intArrayOf(0x34, 0x2004D),
+ intArrayOf(0x34, 0xADF7),
+ intArrayOf(0x34, 0x9DF4),
+ intArrayOf(0x34, 0x8DF1),
+ intArrayOf(0x34, 0x7DEE),
+ intArrayOf(0x34, 0x6DCD),
+ intArrayOf(0x34, 0x5CCD),
+ intArrayOf(0x34, 0x4CCA),
+ intArrayOf(0x34, 0x388C),
+ intArrayOf(0x34, 0x2888),
+ intArrayOf(0x34, 0x1488),
+ intArrayOf(0x34, 0x486),
+ intArrayOf(0xEF, 0x0),
+ intArrayOf(0x18, 0x1712A),
+ intArrayOf(0xEF, 0x40),
+ intArrayOf(0x35, 0x128),
+ intArrayOf(0x35, 0x8128),
+ intArrayOf(0x35, 0x10128),
+ intArrayOf(0x35, 0x201C8),
+ intArrayOf(0x35, 0x281C8),
+ intArrayOf(0x35, 0x301C8),
+ intArrayOf(0x35, 0x401C8),
+ intArrayOf(0x35, 0x481C8),
+ intArrayOf(0x35, 0x501C8),
+ intArrayOf(0xEF, 0x0),
+ intArrayOf(0x18, 0x1712A),
+ intArrayOf(0xEF, 0x10),
+ intArrayOf(0x36, 0x63B5),
+ intArrayOf(0x36, 0xE3B5),
+ intArrayOf(0x36, 0x163B5),
+ intArrayOf(0x36, 0x1E3B5),
+ intArrayOf(0x36, 0x263B5),
+ intArrayOf(0x36, 0x2E3B5),
+ intArrayOf(0x36, 0x363B5),
+ intArrayOf(0x36, 0x3E3B5),
+ intArrayOf(0x36, 0x463B5),
+ intArrayOf(0x36, 0x4E3B5),
+ intArrayOf(0x36, 0x563B5),
+ intArrayOf(0x36, 0x5E3B5),
+ intArrayOf(0xEF, 0x0),
+ intArrayOf(0xEF, 0x8),
+ intArrayOf(0x3C, 0x1B6),
+ intArrayOf(0x3C, 0x492),
+ intArrayOf(0x3C, 0x800),
+ intArrayOf(0xEF, 0x0),
+ intArrayOf(0x18, 0x1712A),
+ intArrayOf(0xEF, 0x2),
+ intArrayOf(0x8, 0x2000),
+ intArrayOf(0xEF, 0x0),
+ intArrayOf(0xDF, 0xC0),
+ intArrayOf(0x1F, 0x64),
+ intArrayOf(0x58, 0x81184),
+ intArrayOf(0x59, 0x6016C),
+ intArrayOf(0x61, 0xEFD83),
+ intArrayOf(0x62, 0x93FCC),
+ intArrayOf(0x63, 0x110EB),
+ intArrayOf(0x64, 0x1C27C),
+ intArrayOf(0x65, 0x93016),
+ intArrayOf(0x18, 0x6),
+ intArrayOf(0xEF, 0x2000),
+ intArrayOf(0x3B, 0x3824B),
+ intArrayOf(0x3B, 0x3024B),
+ intArrayOf(0x3B, 0x2844B),
+ intArrayOf(0x3B, 0x20F4B),
+ intArrayOf(0x3B, 0x18F4B),
+ intArrayOf(0x3B, 0x104B2),
+ intArrayOf(0x3B, 0x8049),
+ intArrayOf(0x3B, 0x148),
+ intArrayOf(0x3B, 0x7824B),
+ intArrayOf(0x3B, 0x7024B),
+ intArrayOf(0x3B, 0x6824B),
+ intArrayOf(0x3B, 0x60F4B),
+ intArrayOf(0x3B, 0x58F4B),
+ intArrayOf(0x3B, 0x504B2),
+ intArrayOf(0x3B, 0x48049),
+ intArrayOf(0x3B, 0x40148),
+ intArrayOf(0xEF, 0x0),
+ intArrayOf(0xEF, 0x100),
+ intArrayOf(0x34, 0xADF3),
+ intArrayOf(0x34, 0x9DF0),
+ intArrayOf(0x34, 0x8D70),
+ intArrayOf(0x34, 0x7D6D),
+ intArrayOf(0x34, 0x6CEE),
+ intArrayOf(0x34, 0x5CCC),
+ intArrayOf(0x34, 0x44EC),
+ intArrayOf(0x34, 0x34AC),
+ intArrayOf(0x34, 0x246D),
+ intArrayOf(0x34, 0x106F),
+ intArrayOf(0x34, 0x6C),
+ intArrayOf(0xEF, 0x0),
+ intArrayOf(0xED, 0x10),
+ intArrayOf(0x44, 0xADF2),
+ intArrayOf(0x44, 0x9DEF),
+ intArrayOf(0x44, 0x8DEC),
+ intArrayOf(0x44, 0x7DE9),
+ intArrayOf(0x44, 0x6CEC),
+ intArrayOf(0x44, 0x5CE9),
+ intArrayOf(0x44, 0x44EC),
+ intArrayOf(0x44, 0x34E9),
+ intArrayOf(0x44, 0x246C),
+ intArrayOf(0x44, 0x1469),
+ intArrayOf(0x44, 0x6C),
+ intArrayOf(0xED, 0x0),
+ intArrayOf(0xED, 0x1),
+ intArrayOf(0x40, 0x38DA7),
+ intArrayOf(0x40, 0x300C2),
+ intArrayOf(0x40, 0x288E2),
+ intArrayOf(0x40, 0x200B8),
+ intArrayOf(0x40, 0x188A5),
+ intArrayOf(0x40, 0x10FBC),
+ intArrayOf(0x40, 0x8F71),
+ intArrayOf(0x40, 0x240),
+ intArrayOf(0xED, 0x0),
+ intArrayOf(0xEF, 0x20A2),
+ intArrayOf(0xDF, 0x80),
+ intArrayOf(0x35, 0x120),
+ intArrayOf(0x35, 0x8120),
+ intArrayOf(0x35, 0x10120),
+ intArrayOf(0x36, 0x85),
+ intArrayOf(0x36, 0x8085),
+ intArrayOf(0x36, 0x10085),
+ intArrayOf(0x36, 0x18085),
+ intArrayOf(0xEF, 0x0),
+ intArrayOf(0x51, 0xC31),
+ intArrayOf(0x52, 0x622),
+ intArrayOf(0x53, 0xFC70B),
+ intArrayOf(0x54, 0x17E),
+ intArrayOf(0x56, 0x51DF3),
+ intArrayOf(0x51, 0xC01),
+ intArrayOf(0x52, 0x6D6),
+ intArrayOf(0x53, 0xFC649),
+ intArrayOf(0x70, 0x49661),
+ intArrayOf(0x71, 0x7843E),
+ intArrayOf(0x72, 0x382),
+ intArrayOf(0x74, 0x51400),
+ intArrayOf(0x35, 0x160),
+ intArrayOf(0x35, 0x8160),
+ intArrayOf(0x35, 0x10160),
+ intArrayOf(0x36, 0x124),
+ intArrayOf(0x36, 0x8124),
+ intArrayOf(0x36, 0x10124),
+ intArrayOf(0x36, 0x18124),
+ intArrayOf(0xED, 0xC),
+ intArrayOf(0x45, 0x140),
+ intArrayOf(0x45, 0x8140),
+ intArrayOf(0x45, 0x10140),
+ intArrayOf(0x46, 0x124),
+ intArrayOf(0x46, 0x8124),
+ intArrayOf(0x46, 0x10124),
+ intArrayOf(0x46, 0x18124),
+ intArrayOf(0xDF, 0x88),
+ intArrayOf(0xB3, 0xF0E18),
+ intArrayOf(0xB4, 0x1214C),
+ intArrayOf(0xB7, 0x3000C),
+ intArrayOf(0x1C, 0x539D2),
+ intArrayOf(0xC4, 0xAFE00),
+ intArrayOf(0x18, 0x1F12A),
+ intArrayOf(DELAY, 50),
+ intArrayOf(DELAY, 50),
+ intArrayOf(0x18, 0x1712A)
+ )
+
+ fun channelTune(ch: Int): Array<IntArray> = arrayOf(
+ intArrayOf(BB, 0x808, 0x30000000, 0x3),
+ intArrayOf(BB, 0xCB0, 0xF000, 0x7),
+ intArrayOf(BB, 0xCB0, 0xF0, 0x7),
+ intArrayOf(BB, 0xCB4, 0x100000, 0x0),
+ intArrayOf(BB, 0xCB4, 0x400000, 0x0),
+ intArrayOf(BB, 0xCB0, 0x7, 0x7),
+ intArrayOf(BB, 0xCB0, 0x700, 0x7),
+ intArrayOf(BB, 0xC1C, 0xF00, 0x0),
+ intArrayOf(BB, 0x82C, 0x3, 0x0),
+ intArrayOf(BB, 0x80C, 0xF0, 0x1),
+ intArrayOf(BB, 0xA04, 0xF000000, 0x1),
+ intArrayOf(MAC8, 0x454, 0x80, 0x0),
+ intArrayOf(BB, 0x860, 0x1FFE0000, 0x96A),
+ intArrayOf(RF, 0x18, 0x70300, 0x0),
+ intArrayOf(RF, 0x18, 0xFF, ch),
+ intArrayOf(MAC16, 0x668, 0x180, 0x0),
+ intArrayOf(MAC8, 0x483, 0xFF, 0x0),
+ intArrayOf(BB, 0x8AC, 0x3003C3, 0x300200),
+ intArrayOf(BB, 0x8C4, 0x40000000, 0x0),
+ intArrayOf(BB, 0x848, 0x3C00000, 0x8),
+ intArrayOf(RF, 0x18, 0xC00, 0x3)
+ )
+
+ const val REG_CR = 0x100
+ const val REG_RXFF_BNDY = 0x116
+ const val REG_RCR = 0x608
+ const val REG_RXFLTMAP0 = 0x6A0
+ const val REG_RXFLTMAP1 = 0x6A2
+ const val REG_RXFLTMAP2 = 0x6A4
+ const val CR_INIT16 = 0x63F
+ const val CR_MACEN8 = 0xC0
+ const val RX_DMA_BOUNDARY = 0x3E7F
+
+ const val RCR_MONITOR = 0x80003B2F.toInt()
+ const val RXFLTMAP_ALL = 0xFFFF
+ const val RXDESC_SIZE = 24
+}
diff --git a/app/src/main/java/com/rfsentinel/app/usb/UsbWifi.kt b/app/src/main/java/com/rfsentinel/app/usb/UsbWifi.kt
new file mode 100644
index 0000000..b48b5f3
--- /dev/null
+++ b/app/src/main/java/com/rfsentinel/app/usb/UsbWifi.kt
@@ -0,0 +1,149 @@
+package com.rfsentinel.app.usb
+
+import android.app.PendingIntent
+import android.content.BroadcastReceiver
+import android.content.Context
+import android.content.Intent
+import android.content.IntentFilter
+import android.hardware.usb.UsbDevice
+import android.hardware.usb.UsbManager
+import android.util.Log
+import androidx.core.content.ContextCompat
+
+/** One device heard by a USB WiFi adapter in monitor mode. */
+data class MonitorSighting(
+ val mac: String,
+ /** Network name (access points), null for client devices and hidden networks. */
+ val ssid: String?,
+ val rssi: Int,
+ val frequencyMhz: Int,
+ /** Access point (beacon / probe response) or a client device (probe request, data). */
+ val isAccessPoint: Boolean,
+ /** The access point a client was talking to, when known. */
+ val bssid: String? = null
+)
+
+/**
+ * USB WiFi adapters on an OTG cable, put in receive-only monitor mode while
+ * scanning: no Android scan throttling, longer range, and client devices (laptops,
+ * phones, cameras talking to a router) that the phone's own WiFi scan never sees.
+ * Android asks once per adapter for USB permission.
+ */
+object UsbWifi {
+
+ private const val TAG = "UsbWifi"
+ private const val ACTION_PERMISSION = "com.rfsentinel.app.USB_WIFI_PERMISSION"
+
+ /** USB IDs with a working driver: vendor:product -> chip. */
+ private val SUPPORTED = mapOf(
+ (0x0bda to 0x0811) to "RTL8811AU / RTL8821AU" // ALFA AWUS036ACS and other 88xxau dongles
+ )
+ /** Known monitor-mode chips without a driver here yet (named in the status so users know why). */
+ private val KNOWN = mapOf(
+ (0x0bda to 0x8812) to "RTL8812AU", (0x0bda to 0x881a) to "RTL8812AU", (0x0bda to 0x8813) to "RTL8814AU",
+ (0x0cf3 to 0x9271) to "AR9271", (0x0e8d to 0x7612) to "MT7612U", (0x148f to 0x3070) to "RT3070",
+ (0x0bda to 0x8187) to "RTL8187"
+ )
+
+ private var receiver: BroadcastReceiver? = null
+ private var onSightings: ((List<MonitorSighting>) -> Unit)? = null
+ private var thread: Thread? = null
+ @Volatile private var runningDevice: String? = null
+
+ /** Latest adapter status for the main screen and Settings ("" when none is plugged in). */
+ @Volatile var status: String = ""
+ private set
+
+ /** Recent driver log lines (bring-up, RX stats, errors), for troubleshooting from Settings. */
+ private val logLines = ArrayDeque<String>()
+
+ fun log(line: String) {
+ Log.d(TAG, line)
+ synchronized(logLines) {
+ logLines.addLast(java.text.SimpleDateFormat("HH:mm:ss", java.util.Locale.US).format(java.util.Date()) + " " + line)
+ while (logLines.size > 300) logLines.removeFirst()
+ }
+ }
+
+ fun logText(): String = synchronized(logLines) { logLines.joinToString("\n") }
+
+ fun chipOf(d: UsbDevice): String? = SUPPORTED[d.vendorId to d.productId]
+ private fun knownOnly(d: UsbDevice): String? = KNOWN[d.vendorId to d.productId]
+
+ @Synchronized
+ fun start(context: Context, sightings: (List<MonitorSighting>) -> Unit) {
+ val app = context.applicationContext
+ onSightings = sightings
+ if (receiver == null) {
+ val r = object : BroadcastReceiver() {
+ override fun onReceive(c: Context, intent: Intent) {
+ when (intent.action) {
+ UsbManager.ACTION_USB_DEVICE_ATTACHED, ACTION_PERMISSION -> connect(app)
+ UsbManager.ACTION_USB_DEVICE_DETACHED -> {
+ val d = deviceOf(intent)
+ if (d != null && d.deviceName == runningDevice) stopAdapter("USB WiFi adapter unplugged")
+ }
+ }
+ }
+ }
+ val filter = IntentFilter().apply {
+ addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED)
+ addAction(UsbManager.ACTION_USB_DEVICE_DETACHED)
+ addAction(ACTION_PERMISSION)
+ }
+ ContextCompat.registerReceiver(app, r, filter, ContextCompat.RECEIVER_NOT_EXPORTED)
+ receiver = r
+ }
+ connect(app)
+ }
+
+ @Synchronized
+ fun stop(context: Context) {
+ receiver?.let { runCatching { context.applicationContext.unregisterReceiver(it) } }
+ receiver = null
+ stopAdapter("")
+ onSightings = null
+ }
+
+ private fun deviceOf(intent: Intent): UsbDevice? =
+ if (android.os.Build.VERSION.SDK_INT >= 33) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE, UsbDevice::class.java)
+ else @Suppress("DEPRECATION") intent.getParcelableExtra(UsbManager.EXTRA_DEVICE)
+
+ @Synchronized
+ private fun connect(context: Context) {
+ if (runningDevice != null) return
+ val mgr = context.getSystemService(UsbManager::class.java) ?: return
+ val all = runCatching { mgr.deviceList.values.toList() }.getOrDefault(emptyList())
+ val d = all.firstOrNull { chipOf(it) != null }
+ if (d == null) {
+ status = all.firstNotNullOfOrNull { knownOnly(it) }
+ ?.let { "USB WiFi adapter ($it) found - this chip isn't supported yet" }.orEmpty()
+ return
+ }
+ if (!mgr.hasPermission(d)) {
+ val pi = PendingIntent.getBroadcast(context, 1, Intent(ACTION_PERMISSION).setPackage(context.packageName),
+ PendingIntent.FLAG_MUTABLE)
+ status = "USB WiFi adapter found - allow USB access on the prompt"
+ runCatching { mgr.requestPermission(d, pi) }
+ return
+ }
+ val chip = chipOf(d)!!
+ runningDevice = d.deviceName
+ status = "USB WiFi ($chip) · starting…"
+ log("Starting $chip (${"%04x:%04x".format(d.vendorId, d.productId)})")
+ thread = Thread({
+ runCatching {
+ Rtl8821auMonitor.run(context, d, { s -> status = s }) { list -> onSightings?.invoke(list) }
+ }.onFailure { log("Driver stopped: ${it.message}"); status = "USB WiFi adapter error: ${it.message}" }
+ synchronized(this) { if (runningDevice == d.deviceName) runningDevice = null }
+ }, "usb-wifi").also { it.start() }
+ }
+
+ @Synchronized
+ private fun stopAdapter(newStatus: String) {
+ Rtl8821auMonitor.abort()
+ runningDevice = null
+ thread = null
+ status = newStatus
+ }
+}
diff --git a/app/src/main/res/layout/activity_settings.xml b/app/src/main/res/layout/activity_settings.xml
index 7df20be..e3cc8a9 100644
--- a/app/src/main/res/layout/activity_settings.xml
+++ b/app/src/main/res/layout/activity_settings.xml
@@ -461,6 +461,19 @@
android:paddingTop="6dp"
android:textSize="12sp" />
+ <TextView
+ android:id="@+id/usbWifiText"
+ style="@style/SettingsLabel"
+ android:paddingTop="6dp"
+ android:textSize="12sp" />
+
+ <Button
+ android:id="@+id/usbWifiLogButton"
+ style="@style/Widget.MaterialComponents.Button.TextButton"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="Share USB WiFi adapter log" />
+
<TextView
android:id="@+id/ouiSpyText"
style="@style/SettingsLabel"