==============================================================================
 tenni-client — HOW TO GET RUNNING
 GPS -> RadioReference -> OP25 trunking scanner
==============================================================================

This walks you from a fresh machine to a live scan. It was developed on
Manjaro/Arch (Python 3.14, RTL-SDR + u-blox GPS) but the installer also supports
Debian/Ubuntu (apt), Fedora (dnf), and openSUSE (zypper).
Commands that need root are shown with `sudo`.

  tenni-client lives in:  wherever you unpacked this archive  (called RRSCAN_DIR below)
  OP25 lives in:    ~/op25  (the installer's default; set OP25_DIR to change)

QUICK PATH: from RRSCAN_DIR just run  ./install.sh  — it installs the system
packages, builds OP25, whisper.cpp (speech-to-text) and the air-traffic tools
(readsb, dump978-fa, rx_tools), creates settings.ini, and drops a `tenni-client`
launcher in ~/.local/bin. Then skip to step 4 (edit settings.ini) and step 5 (run). The
sections below are the manual equivalent / reference if the installer hiccups.


------------------------------------------------------------------------------
0. WHAT YOU NEED
------------------------------------------------------------------------------
  * An SDR, either:
      - an RTL-SDR dongle (RTL2832U), the simple default; or
      - a PlutoSDR-class device (ADALM-Pluto / AntSDR / "Fishball" FB-7020),
        usable over USB or the network by IP — wider bandwidth, so it can watch
        a whole system at once without retuning (see step 4 + section 8).
    Plus an antenna for your local bands (700/800 MHz for most P25; VHF/UHF
    for DMR).
  * Optional: a USB GPS (this box has a u-blox on /dev/ttyACM0). Without one,
    you can set a static location in settings.ini (see step 4).
  * Your Tenni API key (from the map's admin). No RadioReference account is
    needed: the first start can sweep the airwaves for the systems you hear
    (step 5). A RadioReference login still works if you prefer it.


------------------------------------------------------------------------------
1. SYSTEM PACKAGES  (one time)
------------------------------------------------------------------------------
Install GNU Radio, the osmosdr SDR source, and RTL-SDR tools. On Arch/Manjaro
the working combination is the *repo* GNU Radio (built for the current Python),
NOT the AUR gnuradio-git:

    sudo pacman -S gnuradio gnuradio-osmosdr rtl-sdr

For a PlutoSDR/AntSDR/Fishball, also install the SoapySDR Pluto backend (this
gr-osmosdr has no native plutosdr driver, so it reaches the Pluto via Soapy):

    sudo pacman -S soapysdr soapyplutosdr libiio
    # Debian/Ubuntu:  sudo apt install soapysdr-tools soapysdr-module-plutosdr libiio-utils
    # Fedora:         sudo dnf install SoapySDR SoapyPlutoSDR libiio

If pacman says "gnuradio-git conflicts with gnuradio", answer y to replace it.

Verify GNU Radio + osmosdr import under your Python:

    python3 -c "import gnuradio, osmosdr; from gnuradio import gr; print(gr.version())"

Python libraries (already present here): requests, beautifulsoup4, lxml.
Install if missing:

    sudo pacman -S python-requests python-beautifulsoup4 python-lxml


------------------------------------------------------------------------------
2. BUILD OP25  (one time)
------------------------------------------------------------------------------
OP25's own GNU Radio blocks must be compiled for your GNU Radio + Python.
Use the gr310 branch (pybind11 bindings, matches GNU Radio 3.10):

    cd ~/op25
    git checkout gr310
    git pull

Build from the repo root (builds both gr-op25 and gr-op25_repeater). The two
extra cmake flags handle CMake 4.x with OP25's older build files:

    rm -rf build && mkdir build && cd build
    cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_POLICY_VERSION_MINIMUM=3.5 ..
    make -j$(nproc)
    sudo make install
    sudo ldconfig

IMPORTANT: -DCMAKE_INSTALL_PREFIX=/usr is required so the modules land in
/usr/lib/python3.XX/site-packages/gnuradio/ (on your Python path). Installing
to the default /usr/local/ will NOT be found.

Verify both OP25 modules import:

    python3 -c "import gnuradio.op25, gnuradio.op25_repeater; print('op25 OK')"


------------------------------------------------------------------------------
3. FREE THE RTL-SDR FROM THE TV DRIVER  (one time)
------------------------------------------------------------------------------
The kernel DVB-T driver grabs the dongle. Blacklist it:

    sudo cp ~/op25/blacklist-rtl.conf /etc/modprobe.d/
    # then reboot, or for this session:
    sudo rmmod dvb_usb_rtl28xxu rtl2832 2>/dev/null

Confirm the SDR is seen:

    rtl_test -t        # should report RTL2832U / R820T, then Ctrl-C

With more than one dongle, list them (index + serial number):

    timeout 2 rtl_test

    Found 2 device(s):
      0:  Realtek, RTL2838UHIDIR, SN: 00000444
      1:  Realtek, RTL2838UHIDIR, SN: 00000001

Use the index or (better: it doesn't change when you re-plug) the serial as
`device =` in each radio's section (step 9). Two dongles with the same serial?
Give one a new one (unplug/replug after):   rtl_eeprom -d 1 -s 00001090


------------------------------------------------------------------------------
4. CONFIGURE tenni-client
------------------------------------------------------------------------------
    cd RRSCAN_DIR
    cp settings.ini.example settings.ini      # if you don't have one yet
    nano settings.ini

Fill in:

  [dispatch]
  api_key    = tk_...         # the key the Tenni admin gave you
  station_id = home-rx1       # a short name for this receiver

  [rr]                        # ONLY if you want to use your own RadioReference login
  username = YOUR_RR_LOGIN
  password = YOUR_RR_PASSWORD
      (or leave blank and export RR_USER / RR_PASS in your shell instead)

  [scan]
  area = adjacent             # your county + bordering counties (default)
  radius_miles = 10           # only used with area = radius

  [sdr]
  # --- RTL-SDR (the default) ---
  type  = rtl
  args  = rtl                 # 'rtl=0' to pick a specific dongle by index
  ppm   = 0.0                 # frequency correction; set if a system won't lock
  gains =                     # blank = auto-detect tuner & pick defaults

  # --- OR a PlutoSDR / AntSDR / Fishball 7020 over USB or the network ---
  # type = plutosdr
  # ip   = 192.168.2.1        # device IP/hostname (libiio URI is built as ip:<addr>)
  # gains =                   # blank = PGA:64; the Pluto's RF stage is "PGA" (0-73 dB)
  # wideband       = true     # capture the whole system at once, no retuning
  # sample_rate    = 15000000 # capture width in Hz (15 MHz safe; 20000000 maxes it)
  # voice_channels = 3        # control channel + this many simultaneous calls
  #   (Pluto wideband streams audio to the web UI per channel — see step 5)

  [gps]
  device  =                   # blank = autodetect /dev/ttyACM* and /dev/ttyUSB*
  timeout = 90                # seconds to wait for a GPS fix before falling back
  lat =                       # Location source order: --lat/--lon, THEN these,
  lon =                       #   THEN GPS, THEN IP geolocation. Setting lat/lon
                              #   here PINS your spot and skips the GPS wait.
                              #   e.g. lat = 36.1627 / lon = -86.7816

GPS device permissions: your user must be able to read the serial port. On
Manjaro that's usually the `uucp` group:

    sudo usermod -aG uucp $USER     # then log out/in


------------------------------------------------------------------------------
5. RUN IT
------------------------------------------------------------------------------
    cd RRSCAN_DIR
    python3 tenni-client.py          # or just `tenni-client` if you used install.sh

The first time (no radio has  system =  in settings.ini and there's no
RadioReference login) it asks how to choose what to monitor:

  [1] Automatic: scans 700/800/900 MHz, UHF and VHF for P25 control channels
      (about a minute), locks OP25 onto each to read its system and site, and
      asks the Tenni server which of them still needs a receiver. Pick the
      starred one (Enter) or another, then a site.
  [2] Manual: state -> system -> site from the Tenni system list.
  [3] Your own RadioReference login (the menus described below).

The choice is saved as  system = ...  under [sdr] (each radio its own); from
then on tenni-client starts straight on that site. Run  tenni-client --setup
to run the setup wizard again (radios, sweep, systems, service).

Running as a service (the wizard offers to install it):
  tenni-client status          what each radio is receiving right now
  tenni-client service logs    the service's log (Ctrl-C to leave)
  tenni-client service stop | start | restart | enable | disable

With a RadioReference login, what happens:
  * Gets your location (static lat/lon from settings.ini if set, else GPS,
    else IP geolocation).
  * Looks up nearby/statewide trunked systems on RadioReference.
  * Shows a MENU of systems (closest first). Type a number, Enter.
  * Shows a MENU of that system's sites (closest first; your county marked *).
    Type a number, Enter.
  * Writes the OP25 config to out/cfg.json and launches OP25.

Once running, the terminal shows a STATUS SCREEN (refreshed every 2 s) instead
of OP25's log: for every radio, whether it's running, its OP25 web UI port,
whether the control channel is decoding (msg/s — 0 means check antenna, gain,
ppm or site), what each voice receiver is on, and what has been transcribed
and sent to the map. OP25's own log is in out/op25.log.

Open the live scanner UI in a browser:   http://127.0.0.1:8080
Ctrl-C stops everything (OP25, collectors, every radio).

Press Enter at a menu to accept the default [0] (nearest system / main site).

PlutoSDR wideband audio: in wideband mode each voice channel streams to the web
UI on its own websocket port. Open http://127.0.0.1:8080, click anywhere to let
the browser start audio, then use the headphones icon to toggle audio per
channel. (Needs an OP25 build with browser-audio support — boatbod's gr310.)


------------------------------------------------------------------------------
6. HANDY FLAGS  (skip the menus / scripting)
------------------------------------------------------------------------------
  --no-menu                use nearest system + its main site, no prompts
  --pick N                 choose system by its ranked index
  --site <id|name>         pin a site, e.g. --site Antioch  or  --site 029
  --sid <RR id>            configure one RR system id directly
  --radius N               use radius discovery (N miles) instead of counties
  --lat X --lon Y          use a manual position (skip GPS)
  --dry-run                just list what it would pick; don't write or launch
  --no-launch              write the config but don't start OP25
  --background             launch everything detached (logs in out/)
  --op25-log               show OP25's own output instead of the status screen
                           (single radio only)
  --no-cache               re-fetch RR pages instead of using cache/

Examples:
    python3 tenni-client.py --pick 1 --site Antioch
    python3 tenni-client.py --no-menu --background
    python3 tenni-client.py --sid 6355


------------------------------------------------------------------------------
7. TROUBLESHOOTING
------------------------------------------------------------------------------
OP25 won't start: "No module named 'osmosdr'" / "gnuradio.op25"
    GNU Radio or the OP25 modules aren't installed for your Python. Redo
    steps 1-2; make sure CMAKE_INSTALL_PREFIX=/usr and re-run the import checks.

"Failed to open rtlsdr device" / usb_open error -6 / device busy
    The DVB-T driver is holding the dongle. Do step 3
    (sudo rmmod dvb_usb_rtl28xxu rtl2832), then relaunch.

GPS never gets a fix / "waiting for fix..." seems stuck
    NMEA needs sky view; try near a window/outdoors. tenni-client waits up to
    [gps] timeout seconds, then falls back to IP geolocation. The cleanest fix
    for a fixed install is to set [gps] lat/lon — that's used FIRST and skips
    GPS entirely (no wait). If a previous run is hung on the port, kill it
    (it holds /dev/ttyACM0); tenni-client reads the serial device directly and does
    NOT use gpsd, so stop gpsd if it's competing for the port.

PlutoSDR / Fishball won't connect
    Confirm the device is reachable and Soapy can see it:
        SoapySDRUtil --probe="driver=plutosdr,uri=ip:192.168.2.1"
    Check [sdr] ip matches the device, that soapyplutosdr + libiio are
    installed, and (wideband) that the link sustains the sample_rate — drop
    sample_rate / voice_channels if you see overruns ('O' in OP25's output).

Stuck cycling "control channel timeout", never decodes
    The control channel isn't being received/decoded. Usually:
      - wrong/quiet site: in the site menu pick a closer/bigger site
        (more control channels = main simulcast cell);
      - frequency error: set a `ppm` value in [sdr] (RTL dongles drift);
      - antenna/gain: use an antenna for that band; try a higher `gains`.

Decodes the control channel but never follows voice / no audio
    - If it shows talkgroups but stays put: encrypted calls are being skipped
      (by design). Edit crypt_behavior in out/cfg.json: 0=hear noise,
      1=stay silent on the call, 2=skip (default).
    - If no audio at all but it IS following calls: check your audio. OP25
      plays via PulseAudio/PipeWire; confirm `pactl info` shows a default sink.

A system you expect isn't listed
    - Only systems with a site in your county or a bordering county are
      listed. For a wider net use  area = radius  with a larger radius_miles
      (or --radius N).
    - If it says "no site/freq data (may need login)", add your RR credentials
      and re-run with --no-cache to re-fetch that system logged in.

RadioReference login fails
    Check credentials in settings.ini. RR posts to /login/ with action=auth;
    if they change the form, the login code is in rrscan/rr_client.py.


------------------------------------------------------------------------------
8. ONE-SDR REALITY (and what the Pluto changes)
------------------------------------------------------------------------------
One SDR watches one system per run: it captures a single chunk of spectrum, so
systems whose control channels are far apart (P25 at ~857 MHz vs DMR at
~151/444 MHz) can't be received together. Switch systems by re-running and
picking another from the menu, or add another SDR per system as [sdr2],
[sdr3], ... (step 9) — one tenni-client runs them all.

The difference is WITHIN a system:
  * RTL-SDR (~850 kHz usable) follows the control channel and RETUNES for each
    voice call — one call at a time.
  * PlutoSDR in wideband mode captures the whole system at once (e.g. 15 MHz)
    and decodes the control channel PLUS several simultaneous calls in software,
    with no retuning and no calls missed during a grant. Its two hardware
    receivers share one PLL (same center frequency), so they DON'T help cover
    two far-apart frequencies — and they don't need to: one wide capture already
    contains the control channel and all in-band voice.



------------------------------------------------------------------------------
9. MORE RADIOS: A SECOND SCANNER, AIR TRAFFIC CONTROL, ADS-B / UAT
------------------------------------------------------------------------------
One tenni-client can drive several SDRs at once. Add a section per extra
radio to settings.ini — [sdr2], [sdr3], ... — each with its own dongle
(`device =`, see step 3) and a `service`:

  service = rrscan     another trunked system through its own OP25. After the
                       first radio's menus you get the same system / site /
                       service menus for this one. Its web UI port is picked
                       automatically (8081, 8082, ...) and shown on the status
                       screen. Files go to out/sdr2/ etc.

  service = airband    one AM aircraft-band channel, e.g. your airport tower.
                       Transmissions are transcribed here and appear on the
                       map as ATC, matched to the aircraft being talked to.
                          freq = 119.600      (MHz)
                          name = BNA Tower    (label on the map)

  service = adsb       aircraft positions for the map, with flight paths.
                          mode = 1090   ADS-B on 1090 MHz (default) — airliners
                                        and most aircraft (uses readsb)
                          mode = uat    UAT on 978 MHz — US general aviation
                                        below 18,000 ft (uses dump978-fa)
                       For both, use two radios (two dongles), one per mode —
                       or the [adsb] section below, if you run the feeds.

 [adsb] section       the same aircraft, from existing network feeds — no
                      SDR at all. Point it at a running dump1090/readsb SBS
                      feed (sbs = host:port) and/or a dump978 JSON feed
                      (json = host:port); set both and the feeds merge:

                      [adsb]
                      sbs = 127.0.0.1:30003    (1090 MHz SBS feed)
                      json = 127.0.0.1:9900    (UAT JSON feed, optional)
                      interval = 2             (seconds between uploads)

Example — a P25 scanner on dongle 0, tower on dongle 1, ADS-B on dongle 2:

  [sdr]
  type = rtl
  device = 00000444

  [sdr2]
  service = airband
  type = rtl
  device = 00000001
  freq = 119.600
  name = BNA Tower

  [sdr3]
  service = adsb
  mode = 1090
  type = rtl
  device = 00001090

A 1090 MHz antenna matters for ADS-B (a scanner antenna for 800 MHz picks up
little at 1090); airband is 118-137 MHz. See settings.ini.example for every
option (gain, squelch, ppm, existing dump1090/dump978 feeds).

Troubleshooting more radios:
  * "both use RTL device N"     give each radio its own  device =
  * ADS-B "0 msg/s"             antenna / gain; check readsb is installed
                                (`which readsb` — ./install.sh builds it)
  * UAT "needs dump978-fa"      re-run ./install.sh (or SKIP_OP25=1 ./install.sh
                                to only add the missing tools)
  * airband "NO AUDIO"          check `freq`, gain; try  squelch =  blank

See README.md for how it all works internally.
==============================================================================
