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 diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ff61fa11..ccc87cb9d 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 29bb5d8c9..182e0ad5b 100644 --- a/cmake/Modules/FindLibIIO.cmake +++ b/cmake/Modules/FindLibIIO.cmake @@ -1,31 +1,102 @@ +# 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)) - find_path(LIBIIO_INCLUDE_DIR - NAMES iio.h - HINTS ${IIO_DIR}/include - ${LIBIIO_PKG_INCLUDE_DIRS} - PATHS /usr/include - /usr/local/include - ) - - find_library(LIBIIO_LIBRARIES - NAMES iio - HINTS ${IIO_DIR}/lib - ${LIBIIO_PKG_LIBRARY_DIRS} - PATHS /usr/lib - /usr/local/lib - ) - - if(LIBIIO_INCLUDE_DIR AND LIBIIO_LIBRARIES) + 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() + + 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(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") - message(STATUS "Found libiio: ${LIBIIO_INCLUDE_DIR}, ${LIBIIO_LIBRARIES}") - else(LIBIIO_INCLUDE_DIR AND LIBIIO_LIBRARIES) + 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() 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_LIBRARIES) 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/devices/plutosdr/deviceplutosdrbox.cpp b/devices/plutosdr/deviceplutosdrbox.cpp index f55a20e89..c5fe3b496 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,76 @@ #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; +} + +// 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), m_LOppmTenths(0), @@ -45,12 +120,26 @@ 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_txMask(nullptr), + m_txStream(nullptr), + m_txBlock(nullptr), m_xoInitial(0), m_temp(0.0f), 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) { @@ -63,7 +152,19 @@ 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)); + } + + 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 if (m_valid) { @@ -110,6 +211,10 @@ DevicePlutoSDRBox::~DevicePlutoSDRBox() deleteTxBuffer(); closeRx(); 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); } } @@ -118,7 +223,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 +264,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 +283,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 +312,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 +346,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 +371,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 +402,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 +420,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) { @@ -288,7 +439,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", @@ -310,7 +461,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", @@ -337,7 +488,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", @@ -359,7 +510,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", @@ -386,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, @@ -407,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, @@ -433,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, @@ -454,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, @@ -476,156 +627,371 @@ 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() { - 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); } +} + +// 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 + (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; } 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; } 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() { +#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() { +#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; } } 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; } } 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() { +#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() { +#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() { +#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 46b0ad517..9229b0e4e 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 { @@ -90,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; } @@ -135,6 +140,12 @@ 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 + 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; diff --git a/devices/plutosdr/deviceplutosdrscan.cpp b/devices/plutosdr/deviceplutosdrscan.cpp index 8999435dc..cc4cb66e6 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 std::string& uri) +{ + static const std::string backends[] = {"ip:", "usb:", "local:", "serial:", "xml:"}; + + for (const std::string& backend : backends) + { + if (uri.rfind(backend, 0) == 0) { + return uri; + } + } + + return "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,32 +84,53 @@ 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_serialMap.clear(); + m_urilMap.clear(); m_scans.reserve(num_contexts); qDebug("PlutoSDRScan::scan: found %d contexts", num_contexts); 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 = (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. @@ -97,13 +152,43 @@ 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 iio_context_info_list_free(info); iio_scan_context_destroy(scan_ctx); +#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 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; diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt index 7ea17ad7b..c3d0e84c6 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() @@ -1289,20 +1291,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 "" @@ -1639,6 +1657,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 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/demodmeshtastic/meshtasticdemodsink.cpp b/plugins/channelrx/demodmeshtastic/meshtasticdemodsink.cpp index f2ae65498..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); @@ -1167,14 +1168,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); 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/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/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); 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/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.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) 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; 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 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;