OpenRSP Net for SoapySDR
========================

A SoapySDR driver ("openrsp_net") for receivers on an OpenRSP server (the headless openrsp-iqd,
or the OpenRSP Server app on Android): RSPs, RTL-SDR dongles, Airspy R2 / Mini and Airspy HF+.
Any SoapySDR program (GQRX, CubicSDR, SDRangel, GNU Radio, SoapySDRUtil, Python, ...) then sees
the server's receivers as devices and streams their IQ over the network.

SoapySDR loads only drivers built for its own version, so this zip holds the source: build it
once against the SoapySDR on your computer (section 1).


1. Build and install
--------------------

You need CMake 3.16 or newer, a C++17 compiler, SoapySDR's development files and an internet
connection during the build (it fetches Mbed TLS 3.6.7).

Debian, Ubuntu, Raspberry Pi OS
    sudo apt install cmake g++ libsoapysdr-dev soapysdr-tools
Fedora
    sudo dnf install cmake gcc-c++ SoapySDR-devel SoapySDR
macOS (Homebrew)
    brew install cmake soapysdr
Windows
    SoapySDR from PothosSDR or radioconda, CMake, and Visual Studio's "Desktop development
    with C++"

Then, in the folder this file is in:

    cmake -S soapy -B build -DCMAKE_BUILD_TYPE=Release
    cmake --build build --config Release
    sudo cmake --install build --config Release

(Windows: run the last line without sudo, in a command prompt opened as administrator.)

The driver goes into SoapySDR's modules folder, for example
/usr/local/lib/SoapySDR/modules0.8/libopenrsp_netSupport.so. Check that SoapySDR finds it:

    SoapySDRUtil --info          "openrsp_net" is among the "Available factories"

If CMake cannot find SoapySDR, tell it where SoapySDR is installed:

    cmake -S soapy -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=/opt/homebrew
    (Windows: -DCMAKE_PREFIX_PATH="C:\Program Files\PothosSDR", or your radioconda folder)

Without administrator rights, install it in your own folder and point SoapySDR at it:

    cmake --install build --prefix ~/.local
    export SOAPY_SDR_PLUGIN_PATH=~/.local/lib/SoapySDR/modules0.8

To remove it, delete libopenrsp_netSupport.so (openrsp_netSupport.dll on Windows) from that folder.


2. Connect to a server
----------------------

The server's link is shown by the OpenRSP Server app and on openrsp-iqd's admin page, for example

    https://192.168.1.20:8073/#token=k3Jd9...

The token in it gives control of a receiver (its frequency, rate and gain). Without the token you
can still listen to a receiver someone else is running.

List the server's receivers, with the arguments that open each one:

    SoapySDRUtil --find="driver=openrsp_net,url=https://192.168.1.20:8073/#token=k3Jd9..."

The server's own certificate
    A server on your network presents a certificate of its own making, which the driver refuses
    until you accept it. The refusal names its SHA-256 fingerprint:

        [WARNING] OpenRSP Net: https://192.168.1.20:8073/#token=****: certificate ... not trusted
        ... To accept this certificate, add pin=33:ed:6e:...:4e to the device arguments

    Check it against the fingerprint the app or openrsp-iqd shows (its log, its admin page), then
    add pin= (colons optional) and search again:

        SoapySDRUtil --find="driver=openrsp_net,url=https://192.168.1.20:8073/#token=k3Jd9...,pin=33:ed:6e:...:4e"

    A server behind a certificate your computer already trusts (a tunnel's, or one from a public
    certificate authority) needs no pin.

Once a search or a program has used a server, the driver keeps its link, token and certificate
(readable only by you) in

    Linux     ~/.config/openrsp-net/soapy-servers
    macOS     ~/Library/Application Support/openrsp-net/soapy-servers
    Windows   %APPDATA%\openrsp-net\soapy-servers

and from then on every program's device list shows its receivers, for example
"OpenRSP Net: RSPduo 223408D934 tuner 1 (192.168.1.20:8073)". OpenRSP servers on your local
network are listed too, when your computer trusts their certificate or they are kept above. To
forget a server, delete its line from that file.


3. Device arguments
-------------------

    driver=openrsp_net    always
    url=      the server's link, token included; or its address alone (listening only, where the
              server needs a token for control)
    token=    the token on its own, instead of in url
    pin=      the fingerprint of a certificate your computer does not trust
    serial=   the receiver (default: the first free one); --find lists them
    tuner=    an RSPduo's tuner, 1 or 2 (default 1)
    antenna=  an RSPduo's tuner 1 input at the start: "Tuner 1 50 ohm" or "Tuner 1 Hi-Z"
    margin=   quality: 0 lossless, else how far the rounding stays below the band's own noise, in
              dB: 10 (least data, the default), 13, 16, 20 or 26 (practically lossless). A server
              may allow less than lossless.
    narrow=0  always stream the whole band (see sample rates below)

Examples

    An RTL-SDR:
        driver=openrsp_net,url=https://192.168.1.20:8073/#token=k3Jd9...,pin=33:ed:...,serial=00000001
    An RSPduo's tuner 2:
        driver=openrsp_net,url=https://192.168.1.20:8073/#token=k3Jd9...,pin=33:ed:...,serial=223408D934,tuner=2
    Listening only, lossless:
        driver=openrsp_net,url=https://192.168.1.20:8073,pin=33:ed:...,serial=223408D934,margin=0
    Through the server's internet tunnel (a trusted certificate, no pin):
        driver=openrsp_net,url=https://example.trycloudflare.com/#token=Xy7...,serial=223408D934


4. Programs
-----------

GQRX
    File > I/O devices: pick "OpenRSP Net: ..." under Device, or choose "Other..." and type the
    device string with soapy=0 in front:
        soapy=0,driver=openrsp_net,url=https://192.168.1.20:8073/#token=k3Jd9...,pin=33:ed:...,serial=223408D934,tuner=1
    Pick the input rate in the same dialog. Under "Input controls": the gains, the antenna (an
    RSPduo's tuner 1) and "Freq. correction" (ppm).

CubicSDR, SDRangel, Pothos and other SoapySDR programs
    Pick "OpenRSP Net: ..." from the device list (after one search or use, as in section 2).

GNU Radio Companion
    "Soapy Custom Source" (or "osmocom Source"), Device arguments:
        driver=openrsp_net,url=https://192.168.1.20:8073/#token=k3Jd9...,pin=33:ed:...,serial=00000001

Python
    import SoapySDR, numpy as np
    from SoapySDR import SOAPY_SDR_RX, SOAPY_SDR_CF32
    dev = SoapySDR.Device("driver=openrsp_net,url=https://192.168.1.20:8073/#token=k3Jd9...,pin=33:ed:...,serial=00000001")
    dev.setSampleRate(SOAPY_SDR_RX, 0, 2e6)
    dev.setFrequency(SOAPY_SDR_RX, 0, 100e6)
    st = dev.setupStream(SOAPY_SDR_RX, SOAPY_SDR_CF32)
    dev.activateStream(st)
    buf = np.zeros(65536, np.complex64)
    sr = dev.readStream(st, [buf], len(buf), timeoutUs=1000000)   # sr.ret samples in buf
    dev.deactivateStream(st); dev.closeStream(st)

A quick test from the command line (streams and prints the rate it gets):

    SoapySDRUtil --args="driver=openrsp_net,url=...,pin=...,serial=00000001" --rate=2e6 --direction=RX


5. What the driver offers
-------------------------

Sample rates
    The receiver's own rates (up to the server's limit for it), and 250 kS/s, 500 kS/s and 1 MS/s
    where the server cuts a narrow channel out of the receiver's band: only that channel crosses
    the network. narrow=0 streams the whole band at those rates instead, where the receiver has
    them.

Frequency
    The receiver's range ("RF"). "CORR", or the program's frequency correction, is a correction in
    ppm of this computer's own, on top of the one the server applies to the receiver.

Gain
    RSPs: IFGR, the IF gain reduction (20 - 59 dB, more is less gain), and RFGR, the LNA state (0 the
    most gain; how many states depends on the model, the band and the input: an RSPduo has 0 - 6
    below 60 MHz, 0 - 4 on its Hi-Z input, 0 - 9 above). An LNA state the band or input does not
    have is applied as the nearest one it has, and the one asked for comes back with a band or
    input that has it. An overall gain (0 dB the least) sets both.
    RTL-SDR, Airspy, Airspy HF+: TUNER, applied as the receiver's nearest gain step (an Airspy HF+'s
    is its attenuator, as a negative gain).
    Automatic gain: the receiver's AGC.

Antenna
    An RSPduo's tuner 1: "Tuner 1 50 ohm" or "Tuner 1 Hi-Z". Every other receiver: "RX"; an RSP2's or
    RSPdx's antenna port is set by the server's owner (openrsp-iqd's admin page).

DC removal
    On by default: takes out the receiver's residual DC at the band's centre (a notch of a couple of
    hundred hertz). Programs turn it off with SoapySDR's DC offset mode.

Settings (SoapySDRUtil --probe lists them)
    margin          quality, as the margin= argument, changed while streaming
    take_control    true: take control of a shared receiver (needs the link's token)
    status          read only: connecting, streaming (rate, frequency, data rate), listening or in
                    control, or why it stopped

Sample formats: CF32 (native, full scale 1.0) and CS16.

Shared receivers
    One user controls a receiver; the others listen to the band it sends. A device that is only
    listening follows the controlling user's frequency and rate: its own tuning, gain and antenna
    have no effect. When nobody runs the receiver, a listener waits until somebody starts it.


6. When something goes wrong
----------------------------

The driver's messages start with "OpenRSP Net:". Start the program from a terminal to see them.

    "certificate ... not trusted ... add pin=..."    see section 2
    "listening ... give url= the link with its token to take control"
                                                     the url has no token, or not this server's
    "This link's token is no longer valid"          the server's token changed: use its new link
    "The server is full ..."                        the driver connects by itself when a place is free
    "Blocked by the server"                         the server's owner has blocked this computer
    "the server has no receiver free"               every receiver is in use by someone who does not
                                                    share it
    "Out of range: this receiver covers ..."        tuned to the nearest frequency it has
    Nothing in the program's device list            search once with url= (section 2); check
                                                    SoapySDRUtil --info lists openrsp_net


7. The source
-------------

    soapy/                         the driver (src/SoapyOpenRspNet.cpp), its CMakeLists.txt and
                                   live tests (test/)
    sdrpp/src/client/, net/        the OpenRSP Net client it shares with the SDR++ source:
                                   protocol, TLS, HTTP, WebSocket, mDNS
    include/                       the receiver API's headers
    third_party/openrsp-iq-codec/  the IQ codec
    android/common/rsplna/         the RSP LNA state table
