From 73d45c3e24d559c2a398739ed939a803cf1bb031 Mon Sep 17 00:00:00 2001 From: Marcel Date: Sun, 18 Oct 2026 18:06:33 -0400 Subject: [PATCH 01/16] PlutoSDR: find libiio 1.x headers and flag the 1.x API libiio 1.x installs its header as include/iio/iio.h instead of include/iio.h. Look for the 1.x layout first and fall back to the 0.x one, and set LIBIIO_V1 so plutosdrdevice can select the API at compile time. The bundled external build (v0.26) keeps the 0.x path. The input and output plugin headers only need the iio_buffer type, so forward-declare it instead of including iio.h. Co-Authored-By: Claude Opus 5.5 --- cmake/Modules/FindLibIIO.cmake | 28 ++++++++++++++++--- devices/plutosdr/CMakeLists.txt | 4 +++ .../plutosdroutput/plutosdroutput.h | 3 +- .../plutosdrinput/plutosdrinput.h | 3 +- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/cmake/Modules/FindLibIIO.cmake b/cmake/Modules/FindLibIIO.cmake index 29bb5d8c9..b6afa651f 100644 --- a/cmake/Modules/FindLibIIO.cmake +++ b/cmake/Modules/FindLibIIO.cmake @@ -3,14 +3,30 @@ if(NOT LIBIIO_FOUND) pkg_check_modules (LIBIIO_PKG libiio>=0.7) if(LIBIIO_PKG_FOUND OR (DEFINED IIO_DIR)) - find_path(LIBIIO_INCLUDE_DIR - NAMES iio.h + # libiio 1.x installs its header as /include/iio/iio.h + find_path(LIBIIO_V1_INCLUDE_DIR + NAMES iio/iio.h HINTS ${IIO_DIR}/include ${LIBIIO_PKG_INCLUDE_DIRS} PATHS /usr/include /usr/local/include ) + if(LIBIIO_V1_INCLUDE_DIR) + set(LIBIIO_INCLUDE_DIR ${LIBIIO_V1_INCLUDE_DIR} CACHE PATH "libiio include directory") + set(LIBIIO_V1 TRUE CACHE INTERNAL "libiio 1.x API") + else() + # libiio 0.x installs its header as /include/iio.h + find_path(LIBIIO_INCLUDE_DIR + NAMES iio.h + HINTS ${IIO_DIR}/include + ${LIBIIO_PKG_INCLUDE_DIRS} + PATHS /usr/include + /usr/local/include + ) + set(LIBIIO_V1 FALSE CACHE INTERNAL "libiio 1.x API") + endif() + find_library(LIBIIO_LIBRARIES NAMES iio HINTS ${IIO_DIR}/lib @@ -21,13 +37,17 @@ if(NOT LIBIIO_FOUND) if(LIBIIO_INCLUDE_DIR AND LIBIIO_LIBRARIES) set(LIBIIO_FOUND TRUE CACHE INTERNAL "libiio found") - message(STATUS "Found libiio: ${LIBIIO_INCLUDE_DIR}, ${LIBIIO_LIBRARIES}") + if(LIBIIO_V1) + message(STATUS "Found libiio (1.x API): ${LIBIIO_INCLUDE_DIR}, ${LIBIIO_LIBRARIES}") + else() + message(STATUS "Found libiio (0.x API): ${LIBIIO_INCLUDE_DIR}, ${LIBIIO_LIBRARIES}") + endif() else(LIBIIO_INCLUDE_DIR AND LIBIIO_LIBRARIES) set(LIBIIO_FOUND FALSE CACHE INTERNAL "libiio found") message(STATUS "libiio not found.") endif(LIBIIO_INCLUDE_DIR AND LIBIIO_LIBRARIES) - mark_as_advanced(LIBIIO_INCLUDE_DIR LIBIIO_LIBRARIES) + mark_as_advanced(LIBIIO_INCLUDE_DIR LIBIIO_V1_INCLUDE_DIR LIBIIO_LIBRARIES) endif(LIBIIO_PKG_FOUND OR (DEFINED IIO_DIR)) diff --git a/devices/plutosdr/CMakeLists.txt b/devices/plutosdr/CMakeLists.txt index db56a61d0..2a0e14ba6 100644 --- a/devices/plutosdr/CMakeLists.txt +++ b/devices/plutosdr/CMakeLists.txt @@ -35,6 +35,10 @@ target_link_libraries(plutosdrdevice ${ICONV_LIBRARY} ) +if(LIBIIO_V1) + target_compile_definitions(plutosdrdevice PRIVATE LIBIIO_V1) +endif() + if(LIBIIO_EXTERNAL) add_dependencies(plutosdrdevice libiio) endif() diff --git a/plugins/samplesink/plutosdroutput/plutosdroutput.h b/plugins/samplesink/plutosdroutput/plutosdroutput.h index aa94aecb8..717f7c3bf 100644 --- a/plugins/samplesink/plutosdroutput/plutosdroutput.h +++ b/plugins/samplesink/plutosdroutput/plutosdroutput.h @@ -25,13 +25,14 @@ #include #include -#include "iio.h" #include "dsp/devicesamplesink.h" #include "util/message.h" #include "plutosdr/deviceplutosdrshared.h" #include "plutosdr/deviceplutosdrbox.h" #include "plutosdroutputsettings.h" +struct iio_buffer; + class QNetworkAccessManager; class QNetworkReply; class DeviceAPI; diff --git a/plugins/samplesource/plutosdrinput/plutosdrinput.h b/plugins/samplesource/plutosdrinput/plutosdrinput.h index 099ce7396..387f8a50b 100644 --- a/plugins/samplesource/plutosdrinput/plutosdrinput.h +++ b/plugins/samplesource/plutosdrinput/plutosdrinput.h @@ -26,13 +26,14 @@ #include #include -#include "iio.h" #include "dsp/devicesamplesource.h" #include "util/message.h" #include "plutosdr/deviceplutosdrshared.h" #include "plutosdr/deviceplutosdrbox.h" #include "plutosdrinputsettings.h" +struct iio_buffer; + class QNetworkAccessManager; class QNetworkReply; class DeviceAPI; From 49a8b6055fbfb5d5fd6584c148d46ca0483482f9 Mon Sep 17 00:00:00 2001 From: Marcel Date: Sun, 18 Oct 2026 18:06:51 -0400 Subject: [PATCH 02/16] PlutoSDR: use iio_scan() for device discovery with libiio 1.x iio_create_scan_context() and the iio_context_info list were removed in libiio 1.x. Use iio_scan() and the iio_scan_get_*() accessors when LIBIIO_V1 is set. Also release the 0.x scan context when listing fails. libiio 1.x reports network contexts as a bare mDNS host name with no "ip:" prefix, so the probe failed and the hostname-to-IP rewrite and backend labels never applied. Add the prefix when no backend is given, and probe the rewritten URI: the .local name can resolve to another interface (e.g. the USB gadget network) than the one that was scanned. Co-Authored-By: Claude Opus 5.5 --- devices/plutosdr/deviceplutosdrscan.cpp | 59 ++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/devices/plutosdr/deviceplutosdrscan.cpp b/devices/plutosdr/deviceplutosdrscan.cpp index 8999435dc..e8d4e1821 100644 --- a/devices/plutosdr/deviceplutosdrscan.cpp +++ b/devices/plutosdr/deviceplutosdrscan.cpp @@ -23,7 +23,11 @@ #include #include #include +#ifdef LIBIIO_V1 +#include +#else #include +#endif #include #include @@ -31,9 +35,39 @@ #include "deviceplutosdrbox.h" #include "deviceplutosdrscan.h" +#ifdef LIBIIO_V1 +// libiio 1.x reports network contexts as a bare host name, without the "ip:" prefix +static std::string withBackendPrefix(const char *uri) +{ + static const char *backends[] = {"ip:", "usb:", "local:", "serial:", "xml:"}; + + for (const char *backend : backends) + { + if (std::strncmp(uri, backend, std::strlen(backend)) == 0) { + return std::string(uri); + } + } + + return std::string("ip:") + uri; +} +#endif + void DevicePlutoSDRScan::scan() { int i, num_contexts; +#ifdef LIBIIO_V1 + struct iio_scan *scan_ctx; + + scan_ctx = iio_scan(nullptr, nullptr); + + if (iio_err(scan_ctx)) + { + qCritical("PlutoSDRScan::scan: could not scan contexts"); + return; + } + + num_contexts = (int) iio_scan_get_results_count(scan_ctx); +#else struct iio_scan_context *scan_ctx; struct iio_context_info **info; @@ -50,8 +84,10 @@ void DevicePlutoSDRScan::scan() if (num_contexts < 0) { qCritical("PlutoSDRScan::scan: could not get contexts"); + iio_scan_context_destroy(scan_ctx); return; } +#endif m_scans.clear(); m_scans.reserve(num_contexts); @@ -59,16 +95,33 @@ void DevicePlutoSDRScan::scan() for (i = 0; i < num_contexts; i++) { +#ifdef LIBIIO_V1 + const char *description = iio_scan_get_description(scan_ctx, i); + const char *scannedUri = iio_scan_get_uri(scan_ctx, i); + + if (!scannedUri) { + continue; + } + + const std::string prefixedUri = withBackendPrefix(scannedUri); + const char *uri = prefixedUri.c_str(); +#else const char *description = iio_context_info_get_description(info[i]); const char *uri = iio_context_info_get_uri(info[i]); +#endif - if (!DevicePlutoSDRBox::probeURI(std::string(uri)) || !description) { + if (!description) { continue; } // For uri which are ip:*.local replace with the ip address std::string fixedUri = replaceHostnameWithIP(uri, description); + // Probe the address that will actually be used: the mDNS name may resolve to another interface + if (!DevicePlutoSDRBox::probeURI(fixedUri)) { + continue; + } + qDebug("PlutoSDRScan::scan: %d: %s [%s] [%s]", i, description, uri, fixedUri.c_str()); const std::string_view descriptionView = std::string_view{description}; const bool isPlutoDescription = @@ -102,8 +155,12 @@ void DevicePlutoSDRScan::scan() } } +#ifdef LIBIIO_V1 + iio_scan_destroy(scan_ctx); +#else iio_context_info_list_free(info); iio_scan_context_destroy(scan_ctx); +#endif } const std::string* DevicePlutoSDRScan::getURIAt(unsigned int index) const From 9f448dfd77eaf50232dd98e554743f86fd318202 Mon Sep 17 00:00:00 2001 From: Marcel Date: Sun, 18 Oct 2026 18:07:31 -0400 Subject: [PATCH 03/16] PlutoSDR: port context creation and attribute access to libiio 1.x With LIBIIO_V1, create contexts with iio_create_context() and check for pointer-encoded errors. Attribute access goes through struct iio_attr: iio_device_identify_filename() is gone, so a small helper resolves the sysfs file name to a channel, device or debug attribute in the same order 0.x used. Values are read with iio_attr_read_raw() and written with iio_attr_write_string() / iio_attr_write_raw(). Co-Authored-By: Claude Opus 5.5 --- devices/plutosdr/deviceplutosdrbox.cpp | 107 ++++++++++++++++++++++++- 1 file changed, 106 insertions(+), 1 deletion(-) diff --git a/devices/plutosdr/deviceplutosdrbox.cpp b/devices/plutosdr/deviceplutosdrbox.cpp index f55a20e89..a2c308cde 100644 --- a/devices/plutosdr/deviceplutosdrbox.cpp +++ b/devices/plutosdr/deviceplutosdrbox.cpp @@ -22,8 +22,13 @@ #include #include #include +#include #include +#ifdef LIBIIO_V1 +#include +#else #include +#endif #include #include @@ -31,6 +36,52 @@ #include "deviceplutosdr.h" #include "deviceplutosdrbox.h" +#ifdef LIBIIO_V1 +// libiio 1.x has no iio_device_identify_filename(): match the sysfs file name against +// the channel attributes first, then against the device and debug attribute names. +// type is set to 0 for a channel, 1 for a device and 2 for a debug attribute. +static const struct iio_attr *findAttrByFilename(const struct iio_device *dev, const char *filename, int& type) +{ + unsigned int nbChannels = iio_device_get_channels_count(dev); + + for (unsigned int i = 0; i < nbChannels; i++) + { + const struct iio_channel *chn = iio_device_get_channel(dev, i); + unsigned int nbAttrs = iio_channel_get_attrs_count(chn); + + for (unsigned int j = 0; j < nbAttrs; j++) + { + const struct iio_attr *attr = iio_channel_get_attr(chn, j); + const char *attrFilename = iio_attr_get_filename(attr); + + if (attrFilename && (strcmp(attrFilename, filename) == 0)) + { + type = 0; + return attr; + } + } + } + + const struct iio_attr *attr = iio_device_find_attr(dev, filename); + + if (attr) + { + type = 1; + return attr; + } + + attr = iio_device_find_debug_attr(dev, filename); + + if (attr) + { + type = 2; + return attr; + } + + return nullptr; +} +#endif + DevicePlutoSDRBox::DevicePlutoSDRBox(const std::string& uri) : m_devSampleRate(0), m_LOppmTenths(0), @@ -50,7 +101,15 @@ DevicePlutoSDRBox::DevicePlutoSDRBox(const std::string& uri) : m_rxSampleBytes(0), m_txSampleBytes(0) { +#ifdef LIBIIO_V1 + m_ctx = iio_create_context(nullptr, uri.c_str()); + + if (iio_err(m_ctx)) { + m_ctx = nullptr; + } +#else m_ctx = iio_create_context_from_uri(uri.c_str()); +#endif if (m_ctx) { @@ -118,7 +177,15 @@ bool DevicePlutoSDRBox::probeURI(const std::string& uri) bool retVal; struct iio_context *ctx; +#ifdef LIBIIO_V1 + ctx = iio_create_context(nullptr, uri.c_str()); + + if (iio_err(ctx)) { + ctx = nullptr; + } +#else ctx = iio_create_context_from_uri(uri.c_str()); +#endif retVal = (ctx != 0); if (ctx) { @@ -151,8 +218,10 @@ void DevicePlutoSDRBox::set_params(DeviceType devType, for (std::vector::const_iterator it = params.begin(); it != params.end(); ++it) { +#ifndef LIBIIO_V1 struct iio_channel *chn = 0; const char *attr = 0; +#endif std::size_t pos; int ret; int type; @@ -168,6 +237,17 @@ void DevicePlutoSDRBox::set_params(DeviceType devType, std::string key = it->substr(0, pos); std::string val = it->substr(pos + 1, std::string::npos); +#ifdef LIBIIO_V1 + const struct iio_attr *attr = findAttrByFilename(dev, key.c_str(), type); + + if (!attr) + { + std::cerr << "DevicePlutoSDRBox::set_params: Parameter not recognized: " << key << std::endl; + continue; + } + + ret = (int) iio_attr_write_string(attr, val.c_str()); +#else ret = iio_device_identify_filename(dev, key.c_str(), &chn, &attr); if (ret) @@ -186,6 +266,7 @@ void DevicePlutoSDRBox::set_params(DeviceType devType, ret = iio_device_debug_attr_write(dev, attr, val.c_str()); type = 2; } +#endif if (ret < 0) { @@ -219,10 +300,12 @@ void DevicePlutoSDRBox::set_params(DeviceType devType, bool DevicePlutoSDRBox::get_param(DeviceType devType, const std::string ¶m, std::string &value) { +#ifndef LIBIIO_V1 struct iio_channel *chn = 0; const char *attr = 0; - char valuestr[256]; int ret; +#endif + char valuestr[256]; ssize_t nchars; iio_device *dev; @@ -242,6 +325,22 @@ bool DevicePlutoSDRBox::get_param(DeviceType devType, const std::string ¶m, break; } +#ifdef LIBIIO_V1 + int type; + const struct iio_attr *attr = findAttrByFilename(dev, param.c_str(), type); + + if (!attr) + { + std::cerr << "DevicePlutoSDRBox::get_param: Parameter not recognized: " << param << std::endl; + return false; + } + + nchars = iio_attr_read_raw(attr, valuestr, sizeof(valuestr) - 1); + + if (nchars >= 0) { + valuestr[nchars] = '\0'; + } +#else ret = iio_device_identify_filename(dev, param.c_str(), &chn, &attr); if (ret) @@ -257,6 +356,7 @@ bool DevicePlutoSDRBox::get_param(DeviceType devType, const std::string ¶m, } else { nchars = iio_device_debug_attr_read(dev, attr, valuestr, 256); } +#endif if (nchars < 0) { @@ -274,7 +374,12 @@ void DevicePlutoSDRBox::setFilter(const std::string &filterConfigStr) { int ret; +#ifdef LIBIIO_V1 + const struct iio_attr *attr = iio_device_find_attr(m_devPhy, "filter_fir_config"); + ret = attr ? (int) iio_attr_write_raw(attr, filterConfigStr.c_str(), filterConfigStr.size()) : -ENOENT; +#else ret = iio_device_attr_write_raw(m_devPhy, "filter_fir_config", filterConfigStr.c_str(), filterConfigStr.size()); +#endif if (ret < 0) { From 71222d29c81d04bb80b059004d638907668fc208 Mon Sep 17 00:00:00 2001 From: Marcel Date: Sun, 18 Oct 2026 18:08:36 -0400 Subject: [PATCH 04/16] PlutoSDR: port Rx streaming to libiio 1.x With LIBIIO_V1, Rx channels are enabled in an iio_channels_mask and samples are streamed through an iio_stream with 4 blocks in flight. rxBufferRefill() fetches the next filled block, which hands the previous one back to the hardware, and rxBufferFirst()/End()/Step() read from that block. The box API is unchanged, so the Rx threads need no changes. Co-Authored-By: Claude Opus 5.5 --- devices/plutosdr/deviceplutosdrbox.cpp | 144 +++++++++++++++++++++++-- devices/plutosdr/deviceplutosdrbox.h | 6 ++ 2 files changed, 142 insertions(+), 8 deletions(-) diff --git a/devices/plutosdr/deviceplutosdrbox.cpp b/devices/plutosdr/deviceplutosdrbox.cpp index a2c308cde..036d23212 100644 --- a/devices/plutosdr/deviceplutosdrbox.cpp +++ b/devices/plutosdr/deviceplutosdrbox.cpp @@ -80,7 +80,31 @@ static const struct iio_attr *findAttrByFilename(const struct iio_device *dev, c return nullptr; } + +// Number of DMA blocks kept in flight by an iio_stream (libiio recommends 4) +static const size_t nbStreamBlocks = 4; +#endif + +// In libiio 1.x channels are enabled in a mask that is applied when the stream is created +static void enableChannel(struct iio_channel *chn, struct iio_channels_mask *mask) +{ +#ifdef LIBIIO_V1 + iio_channel_enable(chn, mask); +#else + (void) mask; + iio_channel_enable(chn); #endif +} + +static void disableChannel(struct iio_channel *chn, struct iio_channels_mask *mask) +{ +#ifdef LIBIIO_V1 + iio_channel_disable(chn, mask); +#else + (void) mask; + iio_channel_disable(chn); +#endif +} DevicePlutoSDRBox::DevicePlutoSDRBox(const std::string& uri) : m_devSampleRate(0), @@ -96,6 +120,9 @@ DevicePlutoSDRBox::DevicePlutoSDRBox(const std::string& uri) : m_devTx(nullptr), m_rxBuf(nullptr), m_txBuf(nullptr), + m_rxMask(nullptr), + m_rxStream(nullptr), + m_rxBlock(nullptr), m_xoInitial(0), m_temp(0.0f), m_rxSampleBytes(0), @@ -122,7 +149,15 @@ DevicePlutoSDRBox::DevicePlutoSDRBox(const std::string& uri) : qCritical("DevicePlutoSDRBox::DevicePlutoSDRBox: cannot create context for uri: %s", uri.c_str()); } +#ifdef LIBIIO_V1 + if (m_ctx && m_devRx) { + m_rxMask = iio_create_channels_mask(iio_device_get_channels_count(m_devRx)); + } + + m_valid = m_ctx && m_devPhy && m_devRx && m_devTx && m_rxMask; +#else m_valid = m_ctx && m_devPhy && m_devRx && m_devTx; +#endif if (m_valid) { @@ -169,6 +204,9 @@ DevicePlutoSDRBox::~DevicePlutoSDRBox() deleteTxBuffer(); closeRx(); closeTx(); +#ifdef LIBIIO_V1 + if (m_rxMask) { iio_channels_mask_destroy(m_rxMask); } +#endif if (m_ctx) { iio_context_destroy(m_ctx); } } @@ -393,7 +431,7 @@ bool DevicePlutoSDRBox::openRx() if (m_rxChannels.size() > 0) { - iio_channel_enable(m_rxChannels.at(0)); + enableChannel(m_rxChannels.at(0), m_rxMask); const struct iio_data_format *df = iio_channel_get_data_format(m_rxChannels.at(0)); qDebug("DevicePlutoSDRBox::openRx channel I: length: %u bits: %u shift: %u signed: %s be: %s with_scale: %s scale: %lf repeat: %u", @@ -415,7 +453,7 @@ bool DevicePlutoSDRBox::openRx() if (m_rxChannels.size() > 1) { - iio_channel_enable(m_rxChannels.at(1)); + enableChannel(m_rxChannels.at(1), m_rxMask); const struct iio_data_format* df = iio_channel_get_data_format(m_rxChannels.at(1)); qDebug("DevicePlutoSDRBox::openRx channel Q: length: %u bits: %u shift: %u signed: %s be: %s with_scale: %s scale: %lf repeat: %u", @@ -442,7 +480,7 @@ bool DevicePlutoSDRBox::openSecondRx() if (m_rxChannels.size() > 2) { - iio_channel_enable(m_rxChannels.at(2)); + enableChannel(m_rxChannels.at(2), m_rxMask); const struct iio_data_format *df = iio_channel_get_data_format(m_rxChannels.at(2)); qDebug("DevicePlutoSDRBox::openSecondRx channel I: length: %u bits: %u shift: %u signed: %s be: %s with_scale: %s scale: %lf repeat: %u", @@ -464,7 +502,7 @@ bool DevicePlutoSDRBox::openSecondRx() if (m_rxChannels.size() > 3) { - iio_channel_enable(m_rxChannels.at(3)); + enableChannel(m_rxChannels.at(3), m_rxMask); const struct iio_data_format* df = iio_channel_get_data_format(m_rxChannels.at(3)); qDebug("DevicePlutoSDRBox::openSecondRx channel Q: length: %u bits: %u shift: %u signed: %s be: %s with_scale: %s scale: %lf repeat: %u", @@ -581,14 +619,16 @@ bool DevicePlutoSDRBox::openSecondTx() void DevicePlutoSDRBox::closeRx() { - if (m_rxChannels.size() > 0) { iio_channel_disable(m_rxChannels.at(0)); } - if (m_rxChannels.size() > 1) { iio_channel_disable(m_rxChannels.at(1)); } + if (!m_valid) { return; } + if (m_rxChannels.size() > 0) { disableChannel(m_rxChannels.at(0), m_rxMask); } + if (m_rxChannels.size() > 1) { disableChannel(m_rxChannels.at(1), m_rxMask); } } void DevicePlutoSDRBox::closeSecondRx() { - if (m_rxChannels.size() > 2) { iio_channel_disable(m_rxChannels.at(2)); } - if (m_rxChannels.size() > 3) { iio_channel_disable(m_rxChannels.at(3)); } + if (!m_valid) { return; } + if (m_rxChannels.size() > 2) { disableChannel(m_rxChannels.at(2), m_rxMask); } + if (m_rxChannels.size() > 3) { disableChannel(m_rxChannels.at(3), m_rxMask); } } void DevicePlutoSDRBox::closeTx() @@ -605,11 +645,38 @@ void DevicePlutoSDRBox::closeSecondTx() struct iio_buffer *DevicePlutoSDRBox::createRxBuffer(unsigned int size, bool cyclic) { +#ifdef LIBIIO_V1 + (void) cyclic; // cyclic mode only exists for output buffers in libiio 1.x + m_rxBuf = nullptr; + m_rxBlock = nullptr; + + if (m_valid) + { + struct iio_buffer *buf = iio_device_get_buffer(m_devRx, 0); + + if (buf) + { + m_rxStream = iio_buffer_create_stream(buf, nbStreamBlocks, size, m_rxMask); + int err = iio_err(m_rxStream); + + if (err) + { + qCritical("DevicePlutoSDRBox::createRxBuffer: cannot create stream: %s (%d)", strerror(-err), err); + m_rxStream = nullptr; + } + else + { + m_rxBuf = buf; + } + } + } +#else if (m_devRx) { m_rxBuf = iio_device_create_buffer(m_devRx, size, cyclic ? '\1' : '\0'); } else { m_rxBuf = nullptr; } +#endif return m_rxBuf; } @@ -627,11 +694,23 @@ struct iio_buffer *DevicePlutoSDRBox::createTxBuffer(unsigned int size, bool cyc void DevicePlutoSDRBox::deleteRxBuffer() { +#ifdef LIBIIO_V1 + // The iio_buffer itself belongs to the device: only the stream is destroyed + if (m_rxStream) + { + iio_stream_destroy(m_rxStream); + m_rxStream = nullptr; + } + + m_rxBlock = nullptr; + m_rxBuf = nullptr; +#else if (m_rxBuf) { iio_buffer_destroy(m_rxBuf); m_rxBuf = nullptr; } +#endif } void DevicePlutoSDRBox::deleteTxBuffer() @@ -645,9 +724,15 @@ void DevicePlutoSDRBox::deleteTxBuffer() ssize_t DevicePlutoSDRBox::getRxSampleSize() { +#ifdef LIBIIO_V1 + if (m_devRx && m_rxMask) { + return iio_device_get_sample_size(m_devRx, m_rxMask); + } else { +#else if (m_devRx) { return iio_device_get_sample_size(m_devRx); } else { +#endif return 0; } } @@ -663,11 +748,30 @@ ssize_t DevicePlutoSDRBox::getTxSampleSize() ssize_t DevicePlutoSDRBox::rxBufferRefill() { +#ifdef LIBIIO_V1 + if (!m_rxStream) { + return 0; + } + + // Hands the previous block back to the hardware and waits for the next filled one + const struct iio_block *block = iio_stream_get_next_block(m_rxStream); + int err = iio_err(block); + + if (err) + { + m_rxBlock = nullptr; + return err; + } + + m_rxBlock = block; + return (char *) iio_block_end(block) - (char *) iio_block_start(block); +#else if (m_rxBuf) { return iio_buffer_refill(m_rxBuf); } else { return 0; } +#endif } ssize_t DevicePlutoSDRBox::txBufferPush() @@ -681,29 +785,53 @@ ssize_t DevicePlutoSDRBox::txBufferPush() std::ptrdiff_t DevicePlutoSDRBox::rxBufferStep() { +#ifdef LIBIIO_V1 + if (m_rxStream) { + return iio_device_get_sample_size(m_devRx, m_rxMask); + } else { + return 0; + } +#else if (m_rxBuf) { return iio_buffer_step(m_rxBuf); } else { return 0; } +#endif } char* DevicePlutoSDRBox::rxBufferEnd() { +#ifdef LIBIIO_V1 + if (m_rxBlock) { + return (char *) iio_block_end(m_rxBlock); + } else { + return nullptr; + } +#else if (m_rxBuf) { return (char *) iio_buffer_end(m_rxBuf); } else { return nullptr; } +#endif } char* DevicePlutoSDRBox::rxBufferFirst() { +#ifdef LIBIIO_V1 + if (m_rxBlock) { + return (char *) iio_block_first(m_rxBlock, m_rxChannels.at(0)); + } else { + return nullptr; + } +#else if (m_rxBuf) { return (char *) iio_buffer_first(m_rxBuf, m_rxChannels.at(0)); } else { return nullptr; } +#endif } std::ptrdiff_t DevicePlutoSDRBox::txBufferStep() diff --git a/devices/plutosdr/deviceplutosdrbox.h b/devices/plutosdr/deviceplutosdrbox.h index 46b0ad517..0fe27cca6 100644 --- a/devices/plutosdr/deviceplutosdrbox.h +++ b/devices/plutosdr/deviceplutosdrbox.h @@ -36,6 +36,9 @@ typedef SSIZE_T ssize_t; #endif struct iio_channel; +struct iio_channels_mask; // libiio 1.x +struct iio_stream; // libiio 1.x +struct iio_block; // libiio 1.x class DEVICES_API DevicePlutoSDRBox { @@ -135,6 +138,9 @@ private: struct iio_device *m_devTx; struct iio_buffer *m_rxBuf; struct iio_buffer *m_txBuf; + struct iio_channels_mask *m_rxMask; //!< libiio 1.x: Rx channels enabled for streaming + struct iio_stream *m_rxStream; //!< libiio 1.x: Rx block stream + const struct iio_block *m_rxBlock; //!< libiio 1.x: last Rx block received bool m_valid; int64_t m_xoInitial; float m_temp; From 29375d39190591b7eeb9cdbaee11dbd57df086ff Mon Sep 17 00:00:00 2001 From: Marcel Date: Sun, 18 Oct 2026 18:09:19 -0400 Subject: [PATCH 05/16] PlutoSDR: port Tx streaming to libiio 1.x With LIBIIO_V1, Tx channels are enabled in an iio_channels_mask and samples are sent through an iio_stream with 4 blocks. createTxBuffer() takes the first empty block without queueing anything; txBufferPush() queues the filled block and waits for the next free one, so txBufferFirst()/End() always point at the block being filled. Cyclic buffers are not available through iio_stream and SDRangel never requests them. Co-Authored-By: Claude Opus 5.5 --- devices/plutosdr/deviceplutosdrbox.cpp | 135 +++++++++++++++++++++++-- devices/plutosdr/deviceplutosdrbox.h | 3 + 2 files changed, 129 insertions(+), 9 deletions(-) diff --git a/devices/plutosdr/deviceplutosdrbox.cpp b/devices/plutosdr/deviceplutosdrbox.cpp index 036d23212..1fce4e2ae 100644 --- a/devices/plutosdr/deviceplutosdrbox.cpp +++ b/devices/plutosdr/deviceplutosdrbox.cpp @@ -123,6 +123,9 @@ DevicePlutoSDRBox::DevicePlutoSDRBox(const std::string& uri) : m_rxMask(nullptr), m_rxStream(nullptr), m_rxBlock(nullptr), + m_txMask(nullptr), + m_txStream(nullptr), + m_txBlock(nullptr), m_xoInitial(0), m_temp(0.0f), m_rxSampleBytes(0), @@ -154,7 +157,11 @@ DevicePlutoSDRBox::DevicePlutoSDRBox(const std::string& uri) : m_rxMask = iio_create_channels_mask(iio_device_get_channels_count(m_devRx)); } - m_valid = m_ctx && m_devPhy && m_devRx && m_devTx && m_rxMask; + if (m_ctx && m_devTx) { + m_txMask = iio_create_channels_mask(iio_device_get_channels_count(m_devTx)); + } + + m_valid = m_ctx && m_devPhy && m_devRx && m_devTx && m_rxMask && m_txMask; #else m_valid = m_ctx && m_devPhy && m_devRx && m_devTx; #endif @@ -206,6 +213,7 @@ DevicePlutoSDRBox::~DevicePlutoSDRBox() closeTx(); #ifdef LIBIIO_V1 if (m_rxMask) { iio_channels_mask_destroy(m_rxMask); } + if (m_txMask) { iio_channels_mask_destroy(m_txMask); } #endif if (m_ctx) { iio_context_destroy(m_ctx); } } @@ -529,7 +537,7 @@ bool DevicePlutoSDRBox::openTx() if (m_txChannels.size() > 0) { - iio_channel_enable(m_txChannels.at(0)); + enableChannel(m_txChannels.at(0), m_txMask); const struct iio_data_format *df = iio_channel_get_data_format(m_txChannels.at(0)); qDebug("DevicePlutoSDRBox::openTx: channel I: length: %u bits: %u shift: %u signed: %s be: %s with_scale: %s scale: %lf repeat: %u", df->length, @@ -550,7 +558,7 @@ bool DevicePlutoSDRBox::openTx() if (m_txChannels.size() > 1) { - iio_channel_enable(m_txChannels.at(1)); + enableChannel(m_txChannels.at(1), m_txMask); const struct iio_data_format *df = iio_channel_get_data_format(m_txChannels.at(1)); qDebug("DevicePlutoSDRBox::openTx: channel Q: length: %u bits: %u shift: %u signed: %s be: %s with_scale: %s scale: %lf repeat: %u", df->length, @@ -576,7 +584,7 @@ bool DevicePlutoSDRBox::openSecondTx() if (m_txChannels.size() > 2) { - iio_channel_enable(m_txChannels.at(2)); + enableChannel(m_txChannels.at(2), m_txMask); const struct iio_data_format *df = iio_channel_get_data_format(m_txChannels.at(2)); qDebug("DevicePlutoSDRBox::openSecondTx: channel I: length: %u bits: %u shift: %u signed: %s be: %s with_scale: %s scale: %lf repeat: %u", df->length, @@ -597,7 +605,7 @@ bool DevicePlutoSDRBox::openSecondTx() if (m_txChannels.size() > 3) { - iio_channel_enable(m_txChannels.at(3)); + enableChannel(m_txChannels.at(3), m_txMask); const struct iio_data_format *df = iio_channel_get_data_format(m_txChannels.at(3)); qDebug("DevicePlutoSDRBox::openSecondTx: channel Q: length: %u bits: %u shift: %u signed: %s be: %s with_scale: %s scale: %lf repeat: %u", df->length, @@ -633,14 +641,16 @@ void DevicePlutoSDRBox::closeSecondRx() void DevicePlutoSDRBox::closeTx() { - if (m_txChannels.size() > 0) { iio_channel_disable(m_txChannels.at(0)); } - if (m_txChannels.size() > 1) { iio_channel_disable(m_txChannels.at(1)); } + if (!m_valid) { return; } + if (m_txChannels.size() > 0) { disableChannel(m_txChannels.at(0), m_txMask); } + if (m_txChannels.size() > 1) { disableChannel(m_txChannels.at(1), m_txMask); } } void DevicePlutoSDRBox::closeSecondTx() { - if (m_txChannels.size() > 2) { iio_channel_disable(m_txChannels.at(2)); } - if (m_txChannels.size() > 3) { iio_channel_disable(m_txChannels.at(3)); } + if (!m_valid) { return; } + if (m_txChannels.size() > 2) { disableChannel(m_txChannels.at(2), m_txMask); } + if (m_txChannels.size() > 3) { disableChannel(m_txChannels.at(3), m_txMask); } } struct iio_buffer *DevicePlutoSDRBox::createRxBuffer(unsigned int size, bool cyclic) @@ -683,11 +693,56 @@ struct iio_buffer *DevicePlutoSDRBox::createRxBuffer(unsigned int size, bool cyc struct iio_buffer *DevicePlutoSDRBox::createTxBuffer(unsigned int size, bool cyclic) { +#ifdef LIBIIO_V1 + if (cyclic) { // iio_stream does not do cyclic transfers and SDRangel never asks for them + qWarning("DevicePlutoSDRBox::createTxBuffer: cyclic mode is not supported with libiio 1.x"); + } + + m_txBuf = nullptr; + m_txBlock = nullptr; + + if (m_valid) + { + struct iio_buffer *buf = iio_device_get_buffer(m_devTx, 0); + + if (buf) + { + m_txStream = iio_buffer_create_stream(buf, nbStreamBlocks, size, m_txMask); + int err = iio_err(m_txStream); + + if (err) + { + qCritical("DevicePlutoSDRBox::createTxBuffer: cannot create stream: %s (%d)", strerror(-err), err); + m_txStream = nullptr; + } + else + { + // The first call only returns an empty block to fill: nothing is sent + // to the hardware until txBufferPush() + const struct iio_block *block = iio_stream_get_next_block(m_txStream); + err = iio_err(block); + + if (err) + { + qCritical("DevicePlutoSDRBox::createTxBuffer: cannot get block: %s (%d)", strerror(-err), err); + iio_stream_destroy(m_txStream); + m_txStream = nullptr; + } + else + { + m_txBlock = block; + m_txBuf = buf; + } + } + } + } +#else if (m_devTx) { m_txBuf = iio_device_create_buffer(m_devTx, size, cyclic ? '\1' : '\0'); } else { m_txBuf = nullptr; } +#endif return m_txBuf; } @@ -715,11 +770,23 @@ void DevicePlutoSDRBox::deleteRxBuffer() void DevicePlutoSDRBox::deleteTxBuffer() { +#ifdef LIBIIO_V1 + // The iio_buffer itself belongs to the device: only the stream is destroyed + if (m_txStream) + { + iio_stream_destroy(m_txStream); + m_txStream = nullptr; + } + + m_txBlock = nullptr; + m_txBuf = nullptr; +#else if (m_txBuf) { iio_buffer_destroy(m_txBuf); m_txBuf = nullptr; } +#endif } ssize_t DevicePlutoSDRBox::getRxSampleSize() @@ -739,9 +806,15 @@ ssize_t DevicePlutoSDRBox::getRxSampleSize() ssize_t DevicePlutoSDRBox::getTxSampleSize() { +#ifdef LIBIIO_V1 + if (m_devTx && m_txMask) { + return iio_device_get_sample_size(m_devTx, m_txMask); + } else { +#else if (m_devTx) { return iio_device_get_sample_size(m_devTx); } else { +#endif return 0; } } @@ -776,11 +849,31 @@ ssize_t DevicePlutoSDRBox::rxBufferRefill() ssize_t DevicePlutoSDRBox::txBufferPush() { +#ifdef LIBIIO_V1 + if (!m_txBlock) { + return 0; + } + + // Queues the filled block for sending and waits for a free one to fill next + ssize_t nbytes = (char *) iio_block_end(m_txBlock) - (char *) iio_block_start(m_txBlock); + const struct iio_block *block = iio_stream_get_next_block(m_txStream); + int err = iio_err(block); + + if (err) + { + m_txBlock = nullptr; + return err; + } + + m_txBlock = block; + return nbytes; +#else if (m_txBuf) { return iio_buffer_push(m_txBuf); } else { return 0; } +#endif } std::ptrdiff_t DevicePlutoSDRBox::rxBufferStep() @@ -836,29 +929,53 @@ char* DevicePlutoSDRBox::rxBufferFirst() std::ptrdiff_t DevicePlutoSDRBox::txBufferStep() { +#ifdef LIBIIO_V1 + if (m_txStream) { + return iio_device_get_sample_size(m_devTx, m_txMask); + } else { + return 0; + } +#else if (m_txBuf) { return iio_buffer_step(m_txBuf); } else { return 0; } +#endif } char* DevicePlutoSDRBox::txBufferEnd() { +#ifdef LIBIIO_V1 + if (m_txBlock) { + return (char *) iio_block_end(m_txBlock); + } else { + return nullptr; + } +#else if (m_txBuf) { return (char *) iio_buffer_end(m_txBuf); } else { return nullptr; } +#endif } char* DevicePlutoSDRBox::txBufferFirst() { +#ifdef LIBIIO_V1 + if (m_txBlock) { + return (char *) iio_block_first(m_txBlock, m_txChannels.at(0)); + } else { + return nullptr; + } +#else if (m_txBuf) { return (char *) iio_buffer_first(m_txBuf, m_txChannels.at(0)); } else { return nullptr; } +#endif } void DevicePlutoSDRBox::txChannelConvert(int16_t *dst, int16_t *src) diff --git a/devices/plutosdr/deviceplutosdrbox.h b/devices/plutosdr/deviceplutosdrbox.h index 0fe27cca6..a5a65d25b 100644 --- a/devices/plutosdr/deviceplutosdrbox.h +++ b/devices/plutosdr/deviceplutosdrbox.h @@ -141,6 +141,9 @@ private: struct iio_channels_mask *m_rxMask; //!< libiio 1.x: Rx channels enabled for streaming struct iio_stream *m_rxStream; //!< libiio 1.x: Rx block stream const struct iio_block *m_rxBlock; //!< libiio 1.x: last Rx block received + struct iio_channels_mask *m_txMask; //!< libiio 1.x: Tx channels enabled for streaming + struct iio_stream *m_txStream; //!< libiio 1.x: Tx block stream + const struct iio_block *m_txBlock; //!< libiio 1.x: Tx block being filled bool m_valid; int64_t m_xoInitial; float m_temp; From 1fa4355c5e3469f8917073a7eed3709980aa81b9 Mon Sep 17 00:00:00 2001 From: Marcel Date: Sun, 18 Oct 2026 20:51:09 -0400 Subject: [PATCH 06/16] PlutoSDR: keep scan keys unique when a device has several IP addresses A Pluto reachable both on its USB gadget network (192.168.2.1) and on a LAN address is reported as two "ip" contexts. Both got the same key (_ip), so m_serialMap kept one of them arbitrarily and selecting "PlutoSDR0 (ip)" could open the wrong address. When keys collide, append the address to the backend part of the key, e.g. _ip:192.168.50.20, which also shows in the device name. A device with a single address keeps its usual key, so saved device references still match. Exact duplicate URIs are skipped, and the serial and URI maps are now cleared on each scan instead of keeping entries from previous scans. Co-Authored-By: Claude Opus 5.5 --- devices/plutosdr/deviceplutosdrscan.cpp | 32 +++++++++++++++++++++++-- devices/plutosdr/deviceplutosdrscan.h | 1 + 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/devices/plutosdr/deviceplutosdrscan.cpp b/devices/plutosdr/deviceplutosdrscan.cpp index e8d4e1821..50ced1685 100644 --- a/devices/plutosdr/deviceplutosdrscan.cpp +++ b/devices/plutosdr/deviceplutosdrscan.cpp @@ -90,6 +90,8 @@ void DevicePlutoSDRScan::scan() #endif m_scans.clear(); + m_serialMap.clear(); + m_urilMap.clear(); m_scans.reserve(num_contexts); qDebug("PlutoSDRScan::scan: found %d contexts", num_contexts); @@ -128,7 +130,7 @@ void DevicePlutoSDRScan::scan() (descriptionView.find("PlutoSDR") != std::string_view::npos) || (descriptionView.find("AD93") != std::string_view::npos); - if (isPlutoDescription) + if (isPlutoDescription && (m_urilMap.find(fixedUri) == m_urilMap.end())) // skip exact duplicates { // As device scan is used across multiple vectors it's best to use a // managed pointer, as to keep track of when it's safe to delete. @@ -150,11 +152,12 @@ void DevicePlutoSDRScan::scan() // A device can be discovered through multiple backends (e.g. USB and IP). // Keep the backend in the serial key so each scanned entry remains unique. m_scans.back()->m_serial = desc_match[1].str() + createBackendSuffix(uri); - m_serialMap[m_scans.back()->m_serial] = m_scans.back(); } } } + disambiguateSerials(); + #ifdef LIBIIO_V1 iio_scan_destroy(scan_ctx); #else @@ -163,6 +166,31 @@ void DevicePlutoSDRScan::scan() #endif } +void DevicePlutoSDRScan::disambiguateSerials() +{ + // The same device can be reachable at several addresses on one backend (e.g. both the + // USB gadget network and the LAN). Append the address to the backend part of the key + // only when it collides, so a device with a single address keeps its usual key. + std::map keyCounts; + + for (const auto& scan : m_scans) { + keyCounts[scan->m_serial]++; + } + + for (const auto& scan : m_scans) + { + if (scan->m_serial == "TBD") { // no serial in the description: not addressable by serial + continue; + } + + if ((keyCounts[scan->m_serial] > 1) && (scan->m_uri.rfind("ip:", 0) == 0)) { + scan->m_serial += ":" + scan->m_uri.substr(3); + } + + m_serialMap[scan->m_serial] = scan; + } +} + const std::string* DevicePlutoSDRScan::getURIAt(unsigned int index) const { if (index < m_scans.size()) { diff --git a/devices/plutosdr/deviceplutosdrscan.h b/devices/plutosdr/deviceplutosdrscan.h index eda8f79b9..73f6c6e28 100644 --- a/devices/plutosdr/deviceplutosdrscan.h +++ b/devices/plutosdr/deviceplutosdrscan.h @@ -58,6 +58,7 @@ private: std::string createBackendSuffix(const char *uri) const; QString getBackendLabel(const std::string& serial) const; std::string getPhysicalSerial(const std::string& serial) const; + void disambiguateSerials(); std::vector> m_scans; std::map> m_serialMap; From ad02539347db9fc8e30fab87e19c14e8e471f2e0 Mon Sep 17 00:00:00 2001 From: Marcel Date: Mon, 19 Oct 2026 11:12:58 -0400 Subject: [PATCH 07/16] PlutoSDR: use std::string prefix check in scan URI helper Replace strncmp()/strlen() in withBackendPrefix() with std::string::rfind(prefix, 0), as done elsewhere in this file. The inputs were always null-terminated, but static analysis (Flawfinder, CWE-126) flags any strlen() use. Co-Authored-By: Claude Opus 5.5 --- devices/plutosdr/deviceplutosdrscan.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/devices/plutosdr/deviceplutosdrscan.cpp b/devices/plutosdr/deviceplutosdrscan.cpp index 50ced1685..cc4cb66e6 100644 --- a/devices/plutosdr/deviceplutosdrscan.cpp +++ b/devices/plutosdr/deviceplutosdrscan.cpp @@ -37,18 +37,18 @@ #ifdef LIBIIO_V1 // libiio 1.x reports network contexts as a bare host name, without the "ip:" prefix -static std::string withBackendPrefix(const char *uri) +static std::string withBackendPrefix(const std::string& uri) { - static const char *backends[] = {"ip:", "usb:", "local:", "serial:", "xml:"}; + static const std::string backends[] = {"ip:", "usb:", "local:", "serial:", "xml:"}; - for (const char *backend : backends) + for (const std::string& backend : backends) { - if (std::strncmp(uri, backend, std::strlen(backend)) == 0) { - return std::string(uri); + if (uri.rfind(backend, 0) == 0) { + return uri; } } - return std::string("ip:") + uri; + return "ip:" + uri; } #endif From 0819619a2dea37f9ed02f921aaaa709a0b6cabf9 Mon Sep 17 00:00:00 2001 From: Marcel Date: Mon, 19 Oct 2026 12:24:41 -0400 Subject: [PATCH 08/16] PlutoSDR: make libiio 1.x opt-in and take header and library from one install libiio 1.0 is new and not yet packaged by distributions, so keep the 0.x API as the default and add ENABLE_LIBIIO_V1 (default OFF) to build against 1.x explicitly. The finder used to look for the 1.x header in IIO_DIR and in the system paths before falling back to 0.x, independently of where it found the library. With 1.x installed system-wide and IIO_DIR pointing at 0.26, it combined 1.x headers with the 0.26 library and the link failed. Now each candidate install (IIO_DIR, then the one pkg-config reports, then the system paths) must provide both the header and the library for the requested API; an install with the other API is skipped with a message. Changing ENABLE_LIBIIO_V1 triggers detection again in an existing build directory. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 1 + cmake/Modules/FindLibIIO.cmake | 117 +++++++++++++++++++++++---------- 2 files changed, 85 insertions(+), 33 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 187be1f8f..eacb14259 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -120,6 +120,7 @@ option(ENABLE_BLADERF "Enable bladeRF support" ON) option(ENABLE_FUNCUBE "Enable FUNcube support" ON) option(ENABLE_HACKRF "Enable HackRF support" ON) option(ENABLE_IIO "Enable liniio support like PlutoSDR" ON) +option(ENABLE_LIBIIO_V1 "Build PlutoSDR support against the libiio 1.x API (default: 0.x)" OFF) option(ENABLE_LIMESUITE "Enable limesuite support" ON) option(ENABLE_METIS "Enable Metis support" ON) option(ENABLE_MIRISDR "Enable LibMiri for old SDRPlay" ON) diff --git a/cmake/Modules/FindLibIIO.cmake b/cmake/Modules/FindLibIIO.cmake index b6afa651f..182e0ad5b 100644 --- a/cmake/Modules/FindLibIIO.cmake +++ b/cmake/Modules/FindLibIIO.cmake @@ -1,53 +1,104 @@ +# Finds libiio for PlutoSDR support. +# +# The libiio 0.x API is used by default. Set ENABLE_LIBIIO_V1=ON to build +# against libiio 1.x instead. The header and the library always come from the +# same install: IIO_DIR if set, else the one pkg-config reports, else the +# system paths. An install with the other API is skipped. +# +# Sets LIBIIO_FOUND, LIBIIO_INCLUDE_DIR, LIBIIO_LIBRARIES and LIBIIO_V1. + +if(ENABLE_LIBIIO_V1) + set(_LIBIIO_WANT_V1 TRUE) +else() + set(_LIBIIO_WANT_V1 FALSE) +endif() + +# Detect again when ENABLE_LIBIIO_V1 changes in an existing build directory +if(LIBIIO_FOUND AND NOT LIBIIO_EXTERNAL AND NOT (LIBIIO_V1 STREQUAL _LIBIIO_WANT_V1)) + unset(LIBIIO_FOUND CACHE) + unset(LIBIIO_INCLUDE_DIR CACHE) + unset(LIBIIO_LIBRARIES CACHE) +endif() + +# Checks one install for the requested API: header and library must both be found there. +# Sets _LIBIIO_INSTALL_FOUND in the caller's scope. +function(libiio_check_install label include_dirs lib_dirs) + unset(_LIBIIO_INC_V1 CACHE) + unset(_LIBIIO_INC_V0 CACHE) + unset(_LIBIIO_LIB CACHE) + find_path(_LIBIIO_INC_V1 NAMES iio/iio.h PATHS ${include_dirs} NO_DEFAULT_PATH) + find_path(_LIBIIO_INC_V0 NAMES iio.h PATHS ${include_dirs} NO_DEFAULT_PATH) + find_library(_LIBIIO_LIB NAMES iio PATHS ${lib_dirs} + PATH_SUFFIXES ${CMAKE_LIBRARY_ARCHITECTURE} NO_DEFAULT_PATH) + + if(_LIBIIO_WANT_V1) + set(include_dir ${_LIBIIO_INC_V1}) + set(other_include_dir ${_LIBIIO_INC_V0}) + set(other_api "0.x") + set(option_state "ON") + else() + set(include_dir ${_LIBIIO_INC_V0}) + set(other_include_dir ${_LIBIIO_INC_V1}) + set(other_api "1.x") + set(option_state "OFF") + endif() + + set(found FALSE) + + if(include_dir AND _LIBIIO_LIB) + set(LIBIIO_INCLUDE_DIR ${include_dir} CACHE PATH "libiio include directory" FORCE) + set(LIBIIO_LIBRARIES ${_LIBIIO_LIB} CACHE FILEPATH "libiio library" FORCE) + set(found TRUE) + elseif(other_include_dir AND _LIBIIO_LIB) + message(STATUS "Ignoring libiio ${other_api} in ${label}: ENABLE_LIBIIO_V1 is ${option_state}") + endif() + + unset(_LIBIIO_INC_V1 CACHE) + unset(_LIBIIO_INC_V0 CACHE) + unset(_LIBIIO_LIB CACHE) + set(_LIBIIO_INSTALL_FOUND ${found} PARENT_SCOPE) +endfunction() + if(NOT LIBIIO_FOUND) pkg_check_modules (LIBIIO_PKG libiio>=0.7) + set(LIBIIO_V1 FALSE CACHE INTERNAL "libiio 1.x API") if(LIBIIO_PKG_FOUND OR (DEFINED IIO_DIR)) - # libiio 1.x installs its header as /include/iio/iio.h - find_path(LIBIIO_V1_INCLUDE_DIR - NAMES iio/iio.h - HINTS ${IIO_DIR}/include - ${LIBIIO_PKG_INCLUDE_DIRS} - PATHS /usr/include - /usr/local/include - ) - - if(LIBIIO_V1_INCLUDE_DIR) - set(LIBIIO_INCLUDE_DIR ${LIBIIO_V1_INCLUDE_DIR} CACHE PATH "libiio include directory") - set(LIBIIO_V1 TRUE CACHE INTERNAL "libiio 1.x API") - else() - # libiio 0.x installs its header as /include/iio.h - find_path(LIBIIO_INCLUDE_DIR - NAMES iio.h - HINTS ${IIO_DIR}/include - ${LIBIIO_PKG_INCLUDE_DIRS} - PATHS /usr/include - /usr/local/include - ) - set(LIBIIO_V1 FALSE CACHE INTERNAL "libiio 1.x API") + set(_LIBIIO_INSTALL_FOUND FALSE) + + if(DEFINED IIO_DIR) + libiio_check_install("IIO_DIR (${IIO_DIR})" + "${IIO_DIR}/include" + "${IIO_DIR}/lib;${IIO_DIR}/lib64") endif() - find_library(LIBIIO_LIBRARIES - NAMES iio - HINTS ${IIO_DIR}/lib - ${LIBIIO_PKG_LIBRARY_DIRS} - PATHS /usr/lib - /usr/local/lib - ) + if(NOT _LIBIIO_INSTALL_FOUND AND LIBIIO_PKG_FOUND) + libiio_check_install("pkg-config (libiio ${LIBIIO_PKG_VERSION})" + "${LIBIIO_PKG_INCLUDEDIR};${LIBIIO_PKG_INCLUDE_DIRS}" + "${LIBIIO_PKG_LIBDIR};${LIBIIO_PKG_LIBRARY_DIRS}") + endif() - if(LIBIIO_INCLUDE_DIR AND LIBIIO_LIBRARIES) + if(NOT _LIBIIO_INSTALL_FOUND) + libiio_check_install("system paths" + "/usr/include;/usr/local/include" + "/usr/lib;/usr/local/lib") + endif() + + if(_LIBIIO_INSTALL_FOUND) set(LIBIIO_FOUND TRUE CACHE INTERNAL "libiio found") + set(LIBIIO_V1 ${_LIBIIO_WANT_V1} CACHE INTERNAL "libiio 1.x API") if(LIBIIO_V1) message(STATUS "Found libiio (1.x API): ${LIBIIO_INCLUDE_DIR}, ${LIBIIO_LIBRARIES}") else() message(STATUS "Found libiio (0.x API): ${LIBIIO_INCLUDE_DIR}, ${LIBIIO_LIBRARIES}") endif() - else(LIBIIO_INCLUDE_DIR AND LIBIIO_LIBRARIES) + else() set(LIBIIO_FOUND FALSE CACHE INTERNAL "libiio found") message(STATUS "libiio not found.") - endif(LIBIIO_INCLUDE_DIR AND LIBIIO_LIBRARIES) + endif() - mark_as_advanced(LIBIIO_INCLUDE_DIR LIBIIO_V1_INCLUDE_DIR LIBIIO_LIBRARIES) + mark_as_advanced(LIBIIO_INCLUDE_DIR LIBIIO_LIBRARIES) endif(LIBIIO_PKG_FOUND OR (DEFINED IIO_DIR)) From 1ad53cc953238b7ae949725deb434b7f67245b39 Mon Sep 17 00:00:00 2001 From: Andrew Hirst Date: Mon, 19 Oct 2026 21:10:05 +0100 Subject: [PATCH 09/16] Fix WebAPI settings read from the wrong plugin type. RTTY, NAVTEX and ILS demods read logFilename/logEnabled from getAdsbDemodSettings(), which is null for those channels, so a PATCH of either key crashes SDRangel. Also fix M17 Mod reverseAPIChannelIndex (read from NFM Mod settings), Audio CAT SISO settings get and XTRX MIMO reverse API settings and report (Input instead of MIMO types), and BladeRF2 Output title (BladeRF1 settings). --- plugins/channelrx/demodils/ilsdemod.cpp | 4 ++-- plugins/channelrx/demodnavtex/navtexdemod.cpp | 4 ++-- plugins/channelrx/demodrtty/rttydemod.cpp | 4 ++-- plugins/channeltx/modm17/m17mod.cpp | 2 +- plugins/samplemimo/audiocatsiso/audiocatsiso.cpp | 4 ++-- plugins/samplemimo/xtrxmimo/xtrxmimo.cpp | 12 ++++++------ plugins/samplesink/bladerf2output/bladerf2output.cpp | 6 +++--- 7 files changed, 18 insertions(+), 18 deletions(-) diff --git a/plugins/channelrx/demodils/ilsdemod.cpp b/plugins/channelrx/demodils/ilsdemod.cpp index 147aa8604..9c806b279 100644 --- a/plugins/channelrx/demodils/ilsdemod.cpp +++ b/plugins/channelrx/demodils/ilsdemod.cpp @@ -536,10 +536,10 @@ void ILSDemod::webapiUpdateChannelSettings( settings.m_udpPort = response.getIlsDemodSettings()->getUdpPort(); } if (channelSettingsKeys.contains("logFilename")) { - settings.m_logFilename = *response.getAdsbDemodSettings()->getLogFilename(); + settings.m_logFilename = *response.getIlsDemodSettings()->getLogFilename(); } if (channelSettingsKeys.contains("logEnabled")) { - settings.m_logEnabled = response.getAdsbDemodSettings()->getLogEnabled(); + settings.m_logEnabled = response.getIlsDemodSettings()->getLogEnabled(); } if (channelSettingsKeys.contains("rgbColor")) { settings.m_rgbColor = response.getIlsDemodSettings()->getRgbColor(); diff --git a/plugins/channelrx/demodnavtex/navtexdemod.cpp b/plugins/channelrx/demodnavtex/navtexdemod.cpp index 5f7fb4e37..93ad2a610 100644 --- a/plugins/channelrx/demodnavtex/navtexdemod.cpp +++ b/plugins/channelrx/demodnavtex/navtexdemod.cpp @@ -449,10 +449,10 @@ void NavtexDemod::webapiUpdateChannelSettings( settings.m_udpPort = response.getNavtexDemodSettings()->getUdpPort(); } if (channelSettingsKeys.contains("logFilename")) { - settings.m_logFilename = *response.getAdsbDemodSettings()->getLogFilename(); + settings.m_logFilename = *response.getNavtexDemodSettings()->getLogFilename(); } if (channelSettingsKeys.contains("logEnabled")) { - settings.m_logEnabled = response.getAdsbDemodSettings()->getLogEnabled(); + settings.m_logEnabled = response.getNavtexDemodSettings()->getLogEnabled(); } if (channelSettingsKeys.contains("rgbColor")) { settings.m_rgbColor = response.getNavtexDemodSettings()->getRgbColor(); diff --git a/plugins/channelrx/demodrtty/rttydemod.cpp b/plugins/channelrx/demodrtty/rttydemod.cpp index d08906664..765340004 100644 --- a/plugins/channelrx/demodrtty/rttydemod.cpp +++ b/plugins/channelrx/demodrtty/rttydemod.cpp @@ -490,10 +490,10 @@ void RttyDemod::webapiUpdateChannelSettings( settings.m_squelch = response.getRttyDemodSettings()->getSquelch(); } if (channelSettingsKeys.contains("logFilename")) { - settings.m_logFilename = *response.getAdsbDemodSettings()->getLogFilename(); + settings.m_logFilename = *response.getRttyDemodSettings()->getLogFilename(); } if (channelSettingsKeys.contains("logEnabled")) { - settings.m_logEnabled = response.getAdsbDemodSettings()->getLogEnabled(); + settings.m_logEnabled = response.getRttyDemodSettings()->getLogEnabled(); } if (channelSettingsKeys.contains("rgbColor")) { settings.m_rgbColor = response.getRttyDemodSettings()->getRgbColor(); diff --git a/plugins/channeltx/modm17/m17mod.cpp b/plugins/channeltx/modm17/m17mod.cpp index 0d7ea0645..52f796cd6 100644 --- a/plugins/channeltx/modm17/m17mod.cpp +++ b/plugins/channeltx/modm17/m17mod.cpp @@ -501,7 +501,7 @@ void M17Mod::webapiUpdateChannelSettings( settings.m_reverseAPIDeviceIndex = response.getM17ModSettings()->getReverseApiDeviceIndex(); } if (channelSettingsKeys.contains("reverseAPIChannelIndex")) { - settings.m_reverseAPIChannelIndex = response.getNfmModSettings()->getReverseApiChannelIndex(); + settings.m_reverseAPIChannelIndex = response.getM17ModSettings()->getReverseApiChannelIndex(); } if (channelSettingsKeys.contains("sourceCall")) { settings.m_sourceCall = *response.getM17ModSettings()->getSourceCall(); diff --git a/plugins/samplemimo/audiocatsiso/audiocatsiso.cpp b/plugins/samplemimo/audiocatsiso/audiocatsiso.cpp index f2f08092d..59bbd7e2d 100644 --- a/plugins/samplemimo/audiocatsiso/audiocatsiso.cpp +++ b/plugins/samplemimo/audiocatsiso/audiocatsiso.cpp @@ -695,8 +695,8 @@ int AudioCATSISO::webapiSettingsGet( ) { (void) errorMessage; - response.setAudioInputSettings(new SWGSDRangel::SWGAudioInputSettings()); - response.getAudioInputSettings()->init(); + response.setAudioCatsisoSettings(new SWGSDRangel::SWGAudioCATSISOSettings()); + response.getAudioCatsisoSettings()->init(); webapiFormatDeviceSettings(response, m_settings); return 200; diff --git a/plugins/samplemimo/xtrxmimo/xtrxmimo.cpp b/plugins/samplemimo/xtrxmimo/xtrxmimo.cpp index f8c8fdb0e..d7d5893e3 100644 --- a/plugins/samplemimo/xtrxmimo/xtrxmimo.cpp +++ b/plugins/samplemimo/xtrxmimo/xtrxmimo.cpp @@ -1239,16 +1239,16 @@ void XTRXMIMO::webapiUpdateDeviceSettings( settings.m_gpioPins = response.getXtrxMimoSettings()->getGpioPins(); } if (deviceSettingsKeys.contains("useReverseAPI")) { - settings.m_useReverseAPI = response.getXtrxInputSettings()->getUseReverseApi() != 0; + settings.m_useReverseAPI = response.getXtrxMimoSettings()->getUseReverseApi() != 0; } if (deviceSettingsKeys.contains("reverseAPIAddress")) { - settings.m_reverseAPIAddress = *response.getXtrxInputSettings()->getReverseApiAddress(); + settings.m_reverseAPIAddress = *response.getXtrxMimoSettings()->getReverseApiAddress(); } if (deviceSettingsKeys.contains("reverseAPIPort")) { - settings.m_reverseAPIPort = response.getXtrxInputSettings()->getReverseApiPort(); + settings.m_reverseAPIPort = response.getXtrxMimoSettings()->getReverseApiPort(); } if (deviceSettingsKeys.contains("reverseAPIDeviceIndex")) { - settings.m_reverseAPIDeviceIndex = response.getXtrxInputSettings()->getReverseApiDeviceIndex(); + settings.m_reverseAPIDeviceIndex = response.getXtrxMimoSettings()->getReverseApiDeviceIndex(); } // Rx if (deviceSettingsKeys.contains("rxDevSampleRate")) { @@ -1445,8 +1445,8 @@ int XTRXMIMO::webapiReportGet( QString& errorMessage) { (void) errorMessage; - response.setXtrxInputReport(new SWGSDRangel::SWGXtrxInputReport()); - response.getXtrxInputReport()->init(); + response.setXtrxMimoReport(new SWGSDRangel::SWGXtrxMIMOReport()); + response.getXtrxMimoReport()->init(); webapiFormatDeviceReport(response); return 200; } diff --git a/plugins/samplesink/bladerf2output/bladerf2output.cpp b/plugins/samplesink/bladerf2output/bladerf2output.cpp index c4ea05718..b603583f1 100644 --- a/plugins/samplesink/bladerf2output/bladerf2output.cpp +++ b/plugins/samplesink/bladerf2output/bladerf2output.cpp @@ -1033,10 +1033,10 @@ int BladeRF2Output::webapiReportGet(SWGSDRangel::SWGDeviceReport& response, QStr void BladeRF2Output::webapiFormatDeviceSettings(SWGSDRangel::SWGDeviceSettings& response, const BladeRF2OutputSettings& settings) { - if (response.getBladeRf1OutputSettings()->getTitle()) { - *response.getBladeRf1OutputSettings()->getTitle() = settings.m_title; + if (response.getBladeRf2OutputSettings()->getTitle()) { + *response.getBladeRf2OutputSettings()->getTitle() = settings.m_title; } else { - response.getBladeRf1OutputSettings()->setTitle(new QString(settings.m_title)); + response.getBladeRf2OutputSettings()->setTitle(new QString(settings.m_title)); } response.getBladeRf2OutputSettings()->setCenterFrequency(settings.m_centerFrequency); From c9b28fb8c97ccbb4899cc4fa3f1fb49ed93a28c4 Mon Sep 17 00:00:00 2001 From: Marcel Date: Tue, 20 Oct 2026 09:15:24 -0400 Subject: [PATCH 10/16] PlutoSDR: build libiio 1.x in external/ when ENABLE_LIBIIO_V1 is on With ENABLE_LIBIIO_V1, the Linux external build now fetches libiio v1.0.0 instead of v0.26, points LIBIIO_INCLUDE_DIR at its include/ directory (for iio/iio.h) and sets LIBIIO_V1. The 0.x compatibility library, utilities, iiod, examples and tests are not built. libiio 1.x additionally needs libzstd and libavahi-client. Building libiio 1.x from source is not set up for Windows and macOS yet: there ENABLE_LIBIIO_V1 prints a warning and the 0.x build is used. Nothing changes when ENABLE_LIBIIO_V1 is off. Co-Authored-By: Claude Opus 5.5 --- external/CMakeLists.txt | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt index 056b3ab3c..08644c454 100644 --- a/external/CMakeLists.txt +++ b/external/CMakeLists.txt @@ -15,7 +15,9 @@ set(SERIALDV_TAG "v1.1.5") set(DSDCC_TAG "v1.9.6") set(LIMESUITE_TAG 524cd2e548b11084e6f739b2dfe0f958c2e30354) set(BLADERF_TAG "2025.10") -if (WIN32) +if (ENABLE_LIBIIO_V1 AND LINUX) + set(LIBIIO_TAG "v1.0.0") +elseif (WIN32) # Using old version of libiio on windows, as we get unresolved symbols in dll: serial_create_context_from_uri set(LIBIIO_TAG 826563e41b5ce9890b75506f672017de8d76d52d) else() @@ -1262,20 +1264,36 @@ if (LINUX) # apt install libiio-dev # needs pkgconfig, libusb, libxml2, lzma, xz, libiconv + # libiio 1.x (ENABLE_LIBIIO_V1) also needs libzstd and libavahi-client if (ENABLE_IIO AND (NOT LIBIIO_FOUND OR LIBIIO_EXTERNAL)) set_global_cache(LIBIIO_FOUND ON) set(LIBIIO_EXTERNAL ON CACHE INTERNAL "") - set(LIBIIO_INCLUDE_DIR "${EXTERNAL_BUILD_LIBRARIES}/libiio/src/libiio" CACHE INTERNAL "") set(LIBIIO_LIBRARIES "${EXTERNAL_BUILD_LIBRARIES}/lib${LIB_SUFFIX}/libiio.so" CACHE INTERNAL "") + if (ENABLE_LIBIIO_V1) + # libiio 1.x keeps its public header in include/iio/iio.h + set(LIBIIO_INCLUDE_DIR "${EXTERNAL_BUILD_LIBRARIES}/libiio/src/libiio/include" CACHE INTERNAL "") + set(LIBIIO_V1 TRUE CACHE INTERNAL "libiio 1.x API") + set(LIBIIO_CMAKE_ARGS + -DLIBIIO_COMPAT=OFF + -DWITH_UTILS=OFF + -DWITH_IIOD=OFF + -DWITH_EXAMPLES=OFF + -DWITH_TESTS=OFF + -DWITH_MAN=OFF) + else () + set(LIBIIO_INCLUDE_DIR "${EXTERNAL_BUILD_LIBRARIES}/libiio/src/libiio" CACHE INTERNAL "") + set(LIBIIO_V1 FALSE CACHE INTERNAL "libiio 1.x API") + set(LIBIIO_CMAKE_ARGS + -DOSX_PACKAGE=OFF + -DWITH_DOC=OFF + -DWITH_MATLAB_BINDINGS_API=OFF + -DWITH_TESTS=OFF) + endif () ExternalProject_Add(libiio GIT_REPOSITORY https://github.com/analogdevicesinc/libiio.git GIT_TAG ${LIBIIO_TAG} PREFIX "${EXTERNAL_BUILD_LIBRARIES}/libiio" - CMAKE_ARGS ${COMMON_CMAKE_ARGS} - -DOSX_PACKAGE=OFF - -DWITH_DOC=OFF - -DWITH_MATLAB_BINDINGS_API=OFF - -DWITH_TESTS=OFF + CMAKE_ARGS ${COMMON_CMAKE_ARGS} ${LIBIIO_CMAKE_ARGS} BUILD_BYPRODUCTS "${LIBIIO_LIBRARIES}" INSTALL_COMMAND "" TEST_COMMAND "" @@ -1612,6 +1630,13 @@ if (WIN32 OR APPLE) endif () endif (ENABLE_HACKRF) + if (ENABLE_IIO AND ENABLE_LIBIIO_V1) + # Building libiio 1.x from source is only set up for Linux so far + message(WARNING "ENABLE_LIBIIO_V1 is not supported with external libraries on Windows and macOS: " + "building PlutoSDR support against libiio 0.x") + set(LIBIIO_V1 FALSE CACHE INTERNAL "libiio 1.x API") + endif () + if (ENABLE_IIO) if (WIN32) ExternalProject_Add(libxml2 From 3648f458f8d4c507601c4d4df680e6423aeb2724 Mon Sep 17 00:00:00 2001 From: Marcel Date: Tue, 20 Oct 2026 09:15:25 -0400 Subject: [PATCH 11/16] CI: build the PlutoSDR plugins against libiio 1.x All existing jobs use libiio 0.x (distribution package or external/), so the libiio 1.x code path was never compiled in CI. Add a server-only Ubuntu job that configures with ENABLE_LIBIIO_V1, lets external/ build libiio 1.0.0, checks that the 1.x API was selected and builds the PlutoSDR device library and plugins. It only runs when PlutoSDR or libiio related files change. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/plutosdr-libiio1.yml | 58 ++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 .github/workflows/plutosdr-libiio1.yml diff --git a/.github/workflows/plutosdr-libiio1.yml b/.github/workflows/plutosdr-libiio1.yml new file mode 100644 index 000000000..898602ada --- /dev/null +++ b/.github/workflows/plutosdr-libiio1.yml @@ -0,0 +1,58 @@ +name: PlutoSDR libiio 1.x build + +# The release builds use libiio 0.x. This job builds the PlutoSDR plugins +# against libiio 1.x (ENABLE_LIBIIO_V1) so that code path is compiled too. +# libiio-dev (0.x) is deliberately not installed: external/ builds libiio 1.x. + +on: + pull_request: + paths: + - 'devices/plutosdr/**' + - 'plugins/samplesource/plutosdrinput/**' + - 'plugins/samplesink/plutosdroutput/**' + - 'plugins/samplemimo/plutosdrmimo/**' + - 'cmake/Modules/FindLibIIO.cmake' + - 'external/CMakeLists.txt' + - '.github/workflows/plutosdr-libiio1.yml' + push: + paths: + - 'devices/plutosdr/**' + - 'plugins/samplesource/plutosdrinput/**' + - 'plugins/samplesink/plutosdroutput/**' + - 'plugins/samplemimo/plutosdrmimo/**' + - 'cmake/Modules/FindLibIIO.cmake' + - 'external/CMakeLists.txt' + - '.github/workflows/plutosdr-libiio1.yml' + workflow_dispatch: + +permissions: + contents: read + +jobs: + build: + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@v4 + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get install -y ninja-build \ + qt6-base-dev qt6-base-private-dev qt6-multimedia-dev \ + qt6-websockets-dev qt6-positioning-dev qt6-charts-dev \ + qt6-serialport-dev \ + libfftw3-dev libusb-1.0-0-dev libflac-dev libboost-dev libopus-dev \ + libxml2-dev libzstd-dev libavahi-client-dev + - name: Configure server build with libiio 1.x + run: >- + cmake -S . -B build-libiio1 -G Ninja -DCMAKE_BUILD_TYPE=Release + -DENABLE_QT6=ON -DENABLE_LIBIIO_V1=ON + -DBUILD_GUI=OFF -DBUILD_SERVER=ON -DBUILD_BENCH=OFF + -DENABLE_EXTERNAL_LIBRARIES=AUTO -DENABLE_LIBUNWIND=OFF + -DENABLE_FEATURE=OFF -DENABLE_CHANNELRX=OFF -DENABLE_CHANNELTX=OFF + -DENABLE_CHANNELMIMO=OFF + - name: Check that libiio 1.x is used + run: grep -q "^LIBIIO_V1:INTERNAL=TRUE" build-libiio1/CMakeCache.txt + - name: Build PlutoSDR device and plugins + run: >- + cmake --build build-libiio1 --parallel 2 --target + plutosdrdevice inputplutosdrsrv outputplutosdrsrv mimoplutosdrsrv From 32f11118387d1959e52f5f57621779f82c8f4d05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20S=C5=82omkowski?= Date: Sun, 18 Oct 2026 23:12:07 +0200 Subject: [PATCH 12/16] PlutoSDR: power the Rx/Tx LO up on start and down on stop Other applications (e.g. SoapyPlutoSDR, used by gqrx) leave the RX LO powered down when they close, so SDRangel only received the AD9361's own noise. In FDD mode the Tx LO also stays up while no stream runs, radiating LO leakage when the Tx device is merely opened. Set the LO powerdown attribute explicitly: power the LO up when a stream starts, down when it stops or fails, and power the Tx LO down when the Tx device is opened. Applies to the input, output and MIMO plugins. Co-Authored-By: Claude Opus 5.5 --- devices/plutosdr/deviceplutosdrbox.cpp | 16 ++++++++++++++++ devices/plutosdr/deviceplutosdrbox.h | 2 ++ plugins/samplemimo/plutosdrmimo/plutosdrmimo.cpp | 6 ++++++ .../samplesink/plutosdroutput/plutosdroutput.cpp | 11 +++++++++++ .../samplesource/plutosdrinput/plutosdrinput.cpp | 6 ++++++ 5 files changed, 41 insertions(+) diff --git a/devices/plutosdr/deviceplutosdrbox.cpp b/devices/plutosdr/deviceplutosdrbox.cpp index 1fce4e2ae..c5fe3b496 100644 --- a/devices/plutosdr/deviceplutosdrbox.cpp +++ b/devices/plutosdr/deviceplutosdrbox.cpp @@ -653,6 +653,22 @@ void DevicePlutoSDRBox::closeSecondTx() if (m_txChannels.size() > 3) { disableChannel(m_txChannels.at(3), m_txMask); } } +// Other applications (e.g. SoapyPlutoSDR) may leave an LO powered down, and the Tx LO +// stays up in FDD mode even with no stream running, so set the LO state explicitly +void DevicePlutoSDRBox::setRxLOPowerdown(bool powerdown) +{ + std::vector params; + params.push_back(std::string("out_altvoltage0_RX_LO_powerdown=") + (powerdown ? "1" : "0")); + set_params(DEVICE_PHY, params); +} + +void DevicePlutoSDRBox::setTxLOPowerdown(bool powerdown) +{ + std::vector params; + params.push_back(std::string("out_altvoltage1_TX_LO_powerdown=") + (powerdown ? "1" : "0")); + set_params(DEVICE_PHY, params); +} + struct iio_buffer *DevicePlutoSDRBox::createRxBuffer(unsigned int size, bool cyclic) { #ifdef LIBIIO_V1 diff --git a/devices/plutosdr/deviceplutosdrbox.h b/devices/plutosdr/deviceplutosdrbox.h index a5a65d25b..9229b0e4e 100644 --- a/devices/plutosdr/deviceplutosdrbox.h +++ b/devices/plutosdr/deviceplutosdrbox.h @@ -93,6 +93,8 @@ public: void closeTx(); //!< Close first Tx (Tx0) void closeSecondRx(); //!< Close second Rx (Rx1) void closeSecondTx(); //!< Close second Tx (Tx1) + void setRxLOPowerdown(bool powerdown); //!< Power down (true) or up (false) the Rx LO + void setTxLOPowerdown(bool powerdown); //!< Power down (true) or up (false) the Tx LO int getNbRx() const { return m_rxChannels.size() / 2; } int getNbTx() const { return m_txChannels.size() / 2; } int getRxSampleBytes() const { return m_rxSampleBytes; } diff --git a/plugins/samplemimo/plutosdrmimo/plutosdrmimo.cpp b/plugins/samplemimo/plutosdrmimo/plutosdrmimo.cpp index a6b5ab79e..2094f710c 100644 --- a/plugins/samplemimo/plutosdrmimo/plutosdrmimo.cpp +++ b/plugins/samplemimo/plutosdrmimo/plutosdrmimo.cpp @@ -138,6 +138,8 @@ bool PlutoSDRMIMO::openDevice() } } + m_plutoParams->getBox()->setTxLOPowerdown(true); // do not radiate LO leakage until Tx is started + return true; } @@ -181,6 +183,7 @@ bool PlutoSDRMIMO::startRx() } qDebug("PlutoSDRMIMO::startRx"); + m_plutoParams->getBox()->setRxLOPowerdown(false); m_sourceThread = new PlutoSDRMIThread(m_plutoParams->getBox()); connect(m_sourceThread, &PlutoSDRMIThread::error, this, &PlutoSDRMIMO::handleError, Qt::QueuedConnection); @@ -220,6 +223,7 @@ bool PlutoSDRMIMO::startTx() } qDebug("PlutoSDRMIMO::startTx"); + m_plutoParams->getBox()->setTxLOPowerdown(false); m_sinkThread = new PlutoSDRMOThread(m_plutoParams->getBox()); connect(m_sinkThread, &PlutoSDRMOThread::error, this, &PlutoSDRMIMO::handleError, Qt::QueuedConnection); @@ -272,6 +276,7 @@ void PlutoSDRMIMO::stopRx() m_plutoParams->getBox()->deleteRxBuffer(); m_plutoTxBuffer = nullptr; + m_plutoParams->getBox()->setRxLOPowerdown(true); } void PlutoSDRMIMO::stopTx() @@ -303,6 +308,7 @@ void PlutoSDRMIMO::stopTx() m_plutoParams->getBox()->deleteTxBuffer(); m_plutoRxBuffer = nullptr; + m_plutoParams->getBox()->setTxLOPowerdown(true); } void PlutoSDRMIMO::handleError(int errorCode) diff --git a/plugins/samplesink/plutosdroutput/plutosdroutput.cpp b/plugins/samplesink/plutosdroutput/plutosdroutput.cpp index c52d45ed8..cb6206185 100644 --- a/plugins/samplesink/plutosdroutput/plutosdroutput.cpp +++ b/plugins/samplesink/plutosdroutput/plutosdroutput.cpp @@ -124,6 +124,8 @@ bool PlutoSDROutput::start() return false; } + m_deviceShared.m_deviceParams->getBox()->setTxLOPowerdown(false); + // start / stop streaming is done in the thread. m_plutoSDROutputThread = new PlutoSDROutputThread(PLUTOSDR_BLOCKSIZE_SAMPLES, m_deviceShared.m_deviceParams->getBox(), &m_sampleSourceFifo); @@ -172,6 +174,10 @@ void PlutoSDROutput::stop() delete m_plutoSDROutputThread; m_plutoSDROutputThread = nullptr; } + + if (m_deviceShared.m_deviceParams && m_deviceShared.m_deviceParams->getBox()) { + m_deviceShared.m_deviceParams->getBox()->setTxLOPowerdown(true); + } } void PlutoSDROutput::handleError(int errorCode) @@ -193,6 +199,10 @@ void PlutoSDROutput::handleError(int errorCode) m_plutoSDROutputThread = nullptr; } + if (m_deviceShared.m_deviceParams && m_deviceShared.m_deviceParams->getBox()) { + m_deviceShared.m_deviceParams->getBox()->setTxLOPowerdown(true); + } + const QString errorMessage = tr("Radio needs a restart: %1 (%2)") .arg(strerror(-errorCode)) @@ -398,6 +408,7 @@ bool PlutoSDROutput::openDevice() } m_plutoTxBuffer = plutoBox->createTxBuffer(PLUTOSDR_BLOCKSIZE_SAMPLES, false); // PlutoSDR buffer size is counted in number of (I,Q) samples + plutoBox->setTxLOPowerdown(true); // do not radiate LO leakage until started return true; } diff --git a/plugins/samplesource/plutosdrinput/plutosdrinput.cpp b/plugins/samplesource/plutosdrinput/plutosdrinput.cpp index 87633906a..c8b97e630 100644 --- a/plugins/samplesource/plutosdrinput/plutosdrinput.cpp +++ b/plugins/samplesource/plutosdrinput/plutosdrinput.cpp @@ -124,6 +124,8 @@ bool PlutoSDRInput::start() return false; } + m_deviceShared.m_deviceParams->getBox()->setRxLOPowerdown(false); + // start / stop streaming is done in the thread. m_plutoSDRInputThread = new PlutoSDRInputThread(PLUTOSDR_BLOCKSIZE_SAMPLES, m_deviceShared.m_deviceParams->getBox(), &m_sampleFifo); @@ -173,6 +175,10 @@ void PlutoSDRInput::stop() delete m_plutoSDRInputThread; m_plutoSDRInputThread = nullptr; } + + if (m_deviceShared.m_deviceParams && m_deviceShared.m_deviceParams->getBox()) { + m_deviceShared.m_deviceParams->getBox()->setRxLOPowerdown(true); + } } void PlutoSDRInput::handleError(int errorCode) From caa0cebbf39e7108a109cf98532558c63931b1ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20S=C5=82omkowski?= Date: Sun, 18 Oct 2026 23:31:09 +0200 Subject: [PATCH 13/16] WebAPI: truncate file when exporting a preset or configuration Exporting opened the file ReadWrite without truncation, so exporting over a longer earlier export left its tail in the file and the import then failed with "Cannot deserialize configuration from file". Co-Authored-By: Claude Opus 5.5 --- sdrbase/webapi/webapiadapter.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdrbase/webapi/webapiadapter.cpp b/sdrbase/webapi/webapiadapter.cpp index ae2c5373e..1afea094c 100644 --- a/sdrbase/webapi/webapiadapter.cpp +++ b/sdrbase/webapi/webapiadapter.cpp @@ -1134,7 +1134,7 @@ int WebAPIAdapter::instancePresetFilePost( QFile file(filePath); - if (file.open(QIODevice::ReadWrite | QIODevice::Text)) + if (file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) { QTextStream outstream(&file); outstream << base64Str; @@ -1495,7 +1495,7 @@ int WebAPIAdapter::instanceConfigurationFilePost( QFile file(filePath); - if (file.open(QIODevice::ReadWrite | QIODevice::Text)) + if (file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) { QTextStream outstream(&file); outstream << base64Str; From 9fc140bddb40bdb733a5953c752ab72eb0467dc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20S=C5=82omkowski?= Date: Wed, 21 Oct 2026 00:11:22 +0200 Subject: [PATCH 14/16] AudioInputDevice: guard the FIFO list against concurrent changes writeData() runs on the audio input thread and iterates m_audioFifos without a lock, while addFifo()/removeFifo() are called from other threads. A modulator with audio input (e.g. SSB Mod) removes and re-adds its FIFO each time it starts, so starting a Tx device while audio is flowing could crash in AudioFifo::write() (SIGSEGV in QBasicMutex::lockInternal) when the list changed under the iteration. Keying the transmitter often (e.g. FT8 via WSJT-X) hit it within hours. Protect the list with its own mutex, held only for the list operations. m_mutex is left alone, so this does not bring back the lockups the commented out lock in writeData() was removed for. Co-Authored-By: Claude Opus 5.5 --- sdrbase/audio/audioinputdevice.cpp | 7 +++++-- sdrbase/audio/audioinputdevice.h | 4 +++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/sdrbase/audio/audioinputdevice.cpp b/sdrbase/audio/audioinputdevice.cpp index d966a9e2c..be72d5021 100644 --- a/sdrbase/audio/audioinputdevice.cpp +++ b/sdrbase/audio/audioinputdevice.cpp @@ -187,14 +187,14 @@ void AudioInputDevice::stop() void AudioInputDevice::addFifo(AudioFifo* audioFifo) { - QMutexLocker mutexLocker(&m_mutex); + QMutexLocker mutexLocker(&m_fifosMutex); m_audioFifos.push_back(audioFifo); } void AudioInputDevice::removeFifo(AudioFifo* audioFifo) { - QMutexLocker mutexLocker(&m_mutex); + QMutexLocker mutexLocker(&m_fifosMutex); m_audioFifos.remove(audioFifo); } @@ -230,6 +230,9 @@ qint64 AudioInputDevice::writeData(const char *data, qint64 len) return 0; } + // FIFOs are added and removed from other threads (e.g. a modulator being started) + QMutexLocker mutexLocker(&m_fifosMutex); + for (std::list::iterator it = m_audioFifos.begin(); it != m_audioFifos.end(); ++it) { (*it)->write(reinterpret_cast(data), len/4); diff --git a/sdrbase/audio/audioinputdevice.h b/sdrbase/audio/audioinputdevice.h index df267d2df..c685044e6 100644 --- a/sdrbase/audio/audioinputdevice.h +++ b/sdrbase/audio/audioinputdevice.h @@ -22,6 +22,7 @@ #ifndef SDRBASE_AUDIO_AUDIOINPUTDEVICE_H_ #define SDRBASE_AUDIO_AUDIOINPUTDEVICE_H_ +#include #include #include #include @@ -114,7 +115,7 @@ public: void addFifo(AudioFifo* audioFifo); void removeFifo(AudioFifo* audioFifo); - int getNbFifos() const { return m_audioFifos.size(); } + int getNbFifos() const { QMutexLocker mutexLocker(&m_fifosMutex); return m_audioFifos.size(); } uint getRate() const { return m_audioFormat.sampleRate(); } void setOnExit(bool onExit) { m_onExit = onExit; } @@ -126,6 +127,7 @@ public: private: QRecursiveMutex m_mutex; + mutable QMutex m_fifosMutex; //!< guards m_audioFifos, which writeData reads on the audio thread #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) QAudioSource* m_audioInput; #else From e433f26c2d99d5c32f0b82a98b52796ab1d1e4be Mon Sep 17 00:00:00 2001 From: jvn314 Date: Thu, 22 Oct 2026 01:17:31 -0500 Subject: [PATCH 15/16] demodmeshtastic: carry whole-chip STO wrap into FIFO advance After adding the SFO drift (4.25 * sfo_hat) to the fractional STO, the value was wrapped back into [-0.5, 0.5] but the removed whole chip was discarded, misaligning subsequent symbols by m_osFactor samples. This mostly bites at high SF with clock drift, where the drift term can reach ~0.3+ chips. - Fold the wrapped chip(s) into the returned FIFO advance (-chips * os). - Wrap via loRaRound() (half away from zero), so |sto_frac| >= 0.5 now wraps; previously exactly 0.5 did not. Equivalent timing either way. - Reset non-finite STO before rounding (float->int cast of NaN/inf is UB). - Log when the FIFO advance is clamped to 1. --- .../demodmeshtastic/meshtasticdemodsink.cpp | 34 +++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/plugins/channelrx/demodmeshtastic/meshtasticdemodsink.cpp b/plugins/channelrx/demodmeshtastic/meshtasticdemodsink.cpp index f2ae65498..e8ba15013 100644 --- a/plugins/channelrx/demodmeshtastic/meshtasticdemodsink.cpp +++ b/plugins/channelrx/demodmeshtastic/meshtasticdemodsink.cpp @@ -1167,14 +1167,42 @@ int MeshtasticDemodSink::processLoRaFrameSyncStep() m_headerFeedbackWaitSteps = 0U; m_demodActive = true; m_loRaSTOFrac += m_loRaSFOHat * 4.25f; - if (std::abs(m_loRaSTOFrac) > 0.5f) { - m_loRaSTOFrac += (m_loRaSTOFrac > 0.0f) ? -1.0f : 1.0f; + + // Guard against NaN/inf from a degenerate STO/SFO estimate: loRaRound() + // casts float to int, which is undefined behaviour for non-finite + // values (this also protects the stoQuant computation below). + if (!std::isfinite(m_loRaSTOFrac)) + { + qWarning("MeshtasticDemodSink::processLoRaFrameSyncStep: non-finite STO (%f), resetting", m_loRaSTOFrac); + m_loRaSTOFrac = 0.0f; } + + // Fold any whole-chip wrap of the fractional STO into the FIFO advance + // instead of dropping it. One chip = m_osFactor samples. A positive + // wrap means consume fewer samples (and vice versa), the same sign + // convention as the SFO compensation step. loRaRound() rounds half away + // from zero, so |sto_frac| >= 0.5 wraps, consistent with stoQuant below. + const int stoChips = loRaRound(m_loRaSTOFrac); + m_loRaSTOFrac -= static_cast(stoChips); + const int stoCarry = -stoChips * static_cast(m_osFactor); + const float stoQuant = static_cast(loRaRound(m_loRaSTOFrac * static_cast(m_osFactor))); m_loRaSFOCum = ((m_loRaSTOFrac * static_cast(m_osFactor)) - stoQuant) / static_cast(m_osFactor); m_loRaState = LoRaStateSFOCompensation; m_loRaSyncState = LoRaSyncNetId1; - return std::max(1, static_cast(m_loRaSymbolSpan / 4U + static_cast(m_osFactor) * m_loRaCFOInt)); + + const int advance = static_cast(m_loRaSymbolSpan / 4U) + + static_cast(m_osFactor) * m_loRaCFOInt + + stoCarry; + + if (advance < 1) + { + // The CFO/STO correction is partly lost when clamped; make it visible. + qDebug("MeshtasticDemodSink::processLoRaFrameSyncStep: FIFO advance %d clamped to 1 (cfoInt=%d stoCarry=%d)", + advance, static_cast(m_loRaCFOInt), stoCarry); + } + + return std::max(1, advance); } return static_cast(m_loRaSymbolSpan); From 73a90c5ba767ce79b5a36e3216c93a50757c7110 Mon Sep 17 00:00:00 2001 From: jvn314 Date: Fri, 23 Oct 2026 23:35:14 -0500 Subject: [PATCH 16/16] demodmeshtastic: try unmodified header values first Search header symbol-value deltas in the order 0, -1, 1, -2, 2. Prefer an unmodified valid header before testing shifted candidates. --- plugins/channelrx/demodmeshtastic/meshtasticdemodsink.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/channelrx/demodmeshtastic/meshtasticdemodsink.cpp b/plugins/channelrx/demodmeshtastic/meshtasticdemodsink.cpp index e8ba15013..5b7da761d 100644 --- a/plugins/channelrx/demodmeshtastic/meshtasticdemodsink.cpp +++ b/plugins/channelrx/demodmeshtastic/meshtasticdemodsink.cpp @@ -348,7 +348,8 @@ void MeshtasticDemodSink::tryHeaderLock() { const std::vector baseHeaderSlice(symbols.begin() + offset, symbols.begin() + offset + 8U); - for (int delta = -2; delta <= 2; delta++) + // Prefer the unmodified header, then try progressively larger value shifts. + for (int delta : {0, -1, 1, -2, 2}) { std::vector headerSlice(baseHeaderSlice);