Contributing
pysdrlib.contributing ¶
Adding a device to pysdrlib
A driver makes one radio look like every other: the same calls, the same units, samples as complex64. This page is the whole job, in order. Start from a generated skeleton:
python -m pysdrlib.scaffold my_radio # C++ extension + Python (most hardware)
python -m pysdrlib.scaffold my_radio --python-only # a Device written in Python alone
python -m pysdrlib.scaffold my_radio --out-of-tree # a package of your own, outside pysdrlib (see "Out of tree")
then fill in each TODO, checking as you go with pysdrlib.testing.
What a driver is made of¶
| File | What it holds |
|---|---|
<name>/<name>.py |
The Device subclass: the hooks below |
<name>/__init__.py |
Its docstring (the radio's page in the docs: what it is, specs, what to install), Device = <class> and find() |
<name>/lib/__init__.py |
The compiled extension's import (its error classes, lib.err, come from the extension) |
_ext/<name>.py |
LIB (how the vendor library is found or built) and get() (the extension, for setup.py) |
_native/<name>/ |
<name>_device.hpp/.cpp (the wrapper) and <name>_pybind.cpp (its Python binding) |
tests/devices.py |
A Spec, which puts the driver under every contract test |
Nothing else is edited: pysdrlib.devices.ls() finds a driver by its Device = ... line, and setup.py builds every
_ext/<name>.py whose get() returns an extension. (Until it does, the driver is left out of the build.)
1. Describe the radio: CAPS¶
The class attribute CAPS is a Capabilities: limits, defaults and the gain stages by the names the hardware gives
them. Nothing generic knows these names.
CAPS = Capabilities(
driver="",
freq=[Range(1e6, 6e9)], default_freq=100e6,
rates=[Range(250e3, 20e6)], default_rate=2e6,
gains={"rx": {"AMP": GainStage(8, 0, 40, 16)}, "tx": {}}, # GainStage(step, min, max, default)
)
This is what sdr.caps reports before the device is open. Whatever only the attached unit knows (the tuner it has,
its real gain table, the rates its firmware offers) is added in Device._capabilities(caps) once open.
2. The Device class¶
A driver on an extension subclasses pysdrlib.base.native.NativeDevice, which supplies receiving, the sample queue,
stall and drop detection, and read(). A driver implements only what is particular to its hardware:
| Hook | Does |
|---|---|
_open(serial_number=None, ...) |
Returns the extension's device. Map "not found" to err.NoDevice |
_set_rx_freq(freq), _set_rx_sample_rate(Fs) |
Tune the receiver (and the transmitter too, when DUPLEX is False: one synthesizer). Call self._rate_changed() after a rate change |
_set_tx_freq(freq), _set_tx_sample_rate(Fs) |
Only a DUPLEX transmitter, whose transmit side is tuned separately. Where tx and rx share a synthesizer these call the _set_rx_* hooks already |
_set_<dir>_<stage>_gain(gain) |
Apply one gain stage (one per stage in CAPS). Returns the dB actually set (a float), or nothing |
set_<dir>_<stage>_gain(gain=None) |
Not written: made from CAPS for each stage (write one yourself only to document it better; it calls self._set_gain_stage("RX", "AMP", gain, self._set_rx_amp_gain)) |
_capabilities(caps) |
Refine caps for the attached unit |
_set_bandwidth, _set_antenna, _set_gain_mode, _set_clock_source, _set_bias_t |
Only the controls the hardware has. A control left out raises err.NotSupported |
_set_rx_channels, _set_tx_channels |
Only if it has several channels (see caps.channels, caps.shared) |
_tx_write(samples, timeout, slot), _start_tx, _stop_tx, _tx_check, _tx_stats |
Only if CAN_TRANSMIT |
_rx_lost(), _rx_failed() |
Why the device is gone (err.Lost), or why its receive thread gave up (err.StreamStalled); None otherwise |
Class attributes: NAME, CAPS, CAN_TRANSMIT, DUPLEX (separate receive and transmit synthesizers),
SAMPLE_FORMAT (the raw stream the extension delivers), RETUNE_SAMPLES and RETUNE_SETTLE (what a retune leaves in
flight and how long it takes to settle: measure them, as the comments in the shipped drivers do), SETTINGS (anything else the radio
has, as caps.Settings a user reaches with set_setting), GAIN_RESERVE and GAIN_BALANCE (how a total gain is split
across stages), TUNE_AT_OPEN (True for a radio: open sets the receive rate and frequency get_rx_* report, as the
radio powers on with its own).
Two layers of API stay in step. The generic calls (set_rx_gain as a fraction of the range, set_rx_gain_db in decibels, which split a total over the stages)
work on every device. Each stage also has its own setter (set_rx_amp_gain) under the hardware's name. You provide only
CAPS's declaration of the stage and _set_<dir>_<stage>_gain, which applies it; the setter is made from them.
A hook takes the arguments the base class's does, including its keywords (_set_rx_freq(freq, channel=0),
_set_<dir>_<stage>_gain(gain, channel=0), _set_bandwidth(direction, bandwidth, channel=0), slot=0 on the receive and
transmit hooks): a driver with one channel ignores channel. testing.check_driver reports a hook that needs other
arguments or leaves a keyword out. A Setting whose method takes channel= sets per_channel=True.
What a gain hook returns is taken as the gain: where the hardware sets another one than asked (its steps depend on
the input, say), return the dB actually set, as a float, and that is what the setter returns and get_rx_gain_db
reports. Return None (or nothing) when it is as requested. Return nothing else: not a status code, and not the value of
a vendor call that yields one, since any value but None becomes the gain.
Samples are complex64, full scale +-1. The extension converts from the hardware's format (SAMPLE_FORMAT) on its
streaming thread; do not go through complex128, or float64 at all.
Out-of-range values are clamped with a warning (or raise err.InvalidValue when sdr.strict): the base class does
this from CAPS, so a driver never validates its own limits. _set_gain_stage snaps to the stage's step or table.
3. The extension (C++)¶
_native/<name>/ wraps the vendor's C API in a HardwareDevice (include/hardware_wrapper.hpp) and binds it with
hw::bind_device (include/hardware_pybind.hpp), which adds everything devices share: start and stop, the queue,
statistics. Your code:
- opens and closes the handle, and throws
HardwareError(code, message)on failure; - on its streaming thread, passes each buffer to
dispatch_rx(data, valid_bytes, buffer_bytes), and reports samples the hardware says it lost withskip_rx(count)so readers see the gap. With several channels (one queue slot each), serve slot 0 first every time round, and if it stamps the stream (stamp_rx), stamp it again whenrx_generation()changes (new queues); - a transmitter's thread closes its queues (
tx_) when it exits, and one fed by a library's callback overridestx_died(), so a writer never waits on a dead stream. Start, stop and close takelife_; - exposes the device's controls (
set_freq,set_sample_rate...) as methods, bound in<name>_pybind.cpp.
The error codes you throw are listed once, in <name>_pybind.cpp's hw::register_errors(m, "MyRadioError", {{code, "NAME", "doc"}, ...}): it makes the Python classes (lib.err.NAME, all subclasses of MyRadioError) and raises the right one for each code. _native/rtl_sdr/ (a vendor callback) and
_native/uhd/ (several channels, transmit) are complete examples.
The extension is built without linking the vendor library: its symbols resolve when _ext/<name>.py's LIB loads
the library at first import (installed, or built before by pysdrlib build <name> from the vendored sources with cmake, no
network). Importing never builds it. Vendored
sources go in <name>/vendor/, written by tools/vendor.py from a recipe in tools/vendor/<name>/ (vendor.toml: the upstream repo, a tag or commit, and which files to keep; mode = "copy" for a small library, the default traces a build of the whole upstream tree), so the repository holds only what the build uses. Give LIB.help a message a user can act on, LIB.driver your driver's name (it is what pysdrlib build <name> takes), and LIB.note a warning if building is slow.
4. Finding units: find()¶
<name>/__init__.py defines find(), returning a DeviceInfo(driver, label, serial, index, open_args) for each
attached unit. open_args is what Device.open takes to open that one ({"serial": ...} or {"index": ...}), which
is how pysdrlib.open(info) works. It must not open the radio, and must not raise when none is attached.
USB radios are found by enumerating the bus, and find() takes no arguments. A radio that can be on a network takes
addresses (a list of hosts, or "host:port") and/or timeout (seconds to wait for an answer), which
pysdrlib.find(addresses=..., timeout=...) passes on to the drivers that name them:
def find(addresses=None, timeout=None):
... # list what is attached here; look at `addresses` too, if given. With none, probe no network
Probing a network is opt-in: without addresses, find() must not touch it. Put the address in open_args
({"addr": ...}) and take it in _open. A source that can't be enumerated at all (a TCP stream) has no find() to
write: the default lists nothing, and it is opened by name (pysdrlib.open("tcpiq", host=..., port=...)).
5. Check it¶
After changing a binding, regenerate the extension's type stubs (pip install pybind11-stubgen, then
python -m pysdrlib._ext.stubs): editors read them, and tests/test_stubs.py fails until they list what the extension has.
from pysdrlib import testing
print(*testing.check_package("my_radio"), sep="\n")
testing.check_driver reports a gain stage with no setter, a Setting naming a method that does not exist, a
default outside its range, a missing SAMPLE_FORMAT, CAN_TRANSMIT without a _tx_write. Errors are broken; warnings
are missing polish. tests/test_conformance.py runs it on every driver.
Tests run without hardware. Add a Spec for the driver to tests/devices.py: it says how to encode a known sample
block in the raw format, what a fake of the extension returns, and which calls must fail on a closed device. Every
test_device_contract.py and test_native_contract.py test then runs on the driver over a fake. Put what is particular to
the radio (its gain stages, rate snapping, quirks) in tests/test_<name>_api.py over the same fake. A test that
needs the radio is marked hardware, runs only with PYSDRLIB_HW=1, and skips when none is attached.
6. Document it¶
The reference documentation is generated from docstrings: document the class, and every public method a driver adds,
as you would for a user. Fill in the docstring of <name>/__init__.py: what the radio is, its specs, what to install,
and what you measured (the existing drivers show the shape). Add a row to the supported-devices table in the top-level
README, and its page to API_PAGES in tools/gen_docs.py and under Drivers in zensical.toml (python tools/builder.py -d builds
the site; tests/test_docs.py checks both).
Out of tree¶
A driver does not have to live in pysdrlib. python -m pysdrlib.scaffold my_radio --out-of-tree writes a project,
pysdrlib-my-radio/, with a Python-only Device, a pyproject.toml and a test. The project declares an entry point,
[project.entry-points."pysdrlib.drivers"]
my_radio = "pysdrlib_my_radio:MyRadio"
so once it is installed (pip install -e .) pysdrlib.devices.ls(), find(), open("my_radio"), pysdrlib server and
every program built on them know the radio, with nothing in pysdrlib changed. In a script or a notebook,
@pysdrlib.register("my_radio") on the class does the same for that process. The entry point value is
"module:Class" (or just "module", which must define Device); it is imported only when the driver is first used,
and a driver that fails to import is reported by get and skipped by find. The class's find() classmethod lists its
units (nothing by default). A name that pysdrlib ships can't be taken, except by register(..., replace=True).
testing.check_driver and testing.check_package work on it as on a shipped driver. PYSDRLIB_NO_PLUGINS=1 makes
pysdrlib ignore entry points, for the case that something installed breaks a program. pysdrlib.sim is a complete
Python driver to read.