Browse Source

Merge pull request #3024 from ve3ukw/libiio-1x

Libiio 1x (opt-in)
chirpmodem-v8.0.0
Edouard Griffiths 1 month ago committed by GitHub
parent
commit
b1c1f5361b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 58
      .github/workflows/plutosdr-libiio1.yml
  2. 1
      CMakeLists.txt
  3. 111
      cmake/Modules/FindLibIIO.cmake
  4. 4
      devices/plutosdr/CMakeLists.txt
  5. 384
      devices/plutosdr/deviceplutosdrbox.cpp
  6. 9
      devices/plutosdr/deviceplutosdrbox.h
  7. 91
      devices/plutosdr/deviceplutosdrscan.cpp
  8. 1
      devices/plutosdr/deviceplutosdrscan.h
  9. 39
      external/CMakeLists.txt
  10. 3
      plugins/samplesink/plutosdroutput/plutosdroutput.h
  11. 3
      plugins/samplesource/plutosdrinput/plutosdrinput.h

58
.github/workflows/plutosdr-libiio1.yml

@ -0,0 +1,58 @@ @@ -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

1
CMakeLists.txt

@ -120,6 +120,7 @@ option(ENABLE_BLADERF "Enable bladeRF support" ON) @@ -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)

111
cmake/Modules/FindLibIIO.cmake

@ -1,31 +1,102 @@ @@ -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)

4
devices/plutosdr/CMakeLists.txt

@ -35,6 +35,10 @@ target_link_libraries(plutosdrdevice @@ -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()

384
devices/plutosdr/deviceplutosdrbox.cpp

@ -22,8 +22,13 @@ @@ -22,8 +22,13 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <regex>
#ifdef LIBIIO_V1
#include <iio/iio.h>
#else
#include <iio.h>
#endif
#include <boost/lexical_cast.hpp>
#include <QtGlobal>
@ -31,6 +36,76 @@ @@ -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) : @@ -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) : @@ -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() @@ -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) @@ -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, @@ -151,8 +264,10 @@ void DevicePlutoSDRBox::set_params(DeviceType devType,
for (std::vector<std::string>::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, @@ -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, @@ -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, @@ -219,10 +346,12 @@ void DevicePlutoSDRBox::set_params(DeviceType devType,
bool DevicePlutoSDRBox::get_param(DeviceType devType, const std::string &param, 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 &param, @@ -242,6 +371,22 @@ bool DevicePlutoSDRBox::get_param(DeviceType devType, const std::string &param,
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 &param, @@ -257,6 +402,7 @@ bool DevicePlutoSDRBox::get_param(DeviceType devType, const std::string &param,
} 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) @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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,355 @@ bool DevicePlutoSDRBox::openSecondTx() @@ -476,156 +627,355 @@ 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); }
}
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)

9
devices/plutosdr/deviceplutosdrbox.h

@ -36,6 +36,9 @@ typedef SSIZE_T ssize_t; @@ -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,12 @@ private: @@ -135,6 +138,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;

91
devices/plutosdr/deviceplutosdrscan.cpp

@ -23,7 +23,11 @@ @@ -23,7 +23,11 @@
#include <regex>
#include <memory>
#include <string_view>
#ifdef LIBIIO_V1
#include <iio/iio.h>
#else
#include <iio.h>
#endif
#include <utility>
#include <QtGlobal>
@ -31,9 +35,39 @@ @@ -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() @@ -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() @@ -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<std::string, int> 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

1
devices/plutosdr/deviceplutosdrscan.h

@ -58,6 +58,7 @@ private: @@ -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<std::shared_ptr<DeviceScan>> m_scans;
std::map<std::string, std::shared_ptr<DeviceScan>> m_serialMap;

39
external/CMakeLists.txt vendored

@ -15,7 +15,9 @@ set(SERIALDV_TAG "v1.1.5") @@ -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) @@ -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) @@ -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

3
plugins/samplesink/plutosdroutput/plutosdroutput.h

@ -25,13 +25,14 @@ @@ -25,13 +25,14 @@
#include <QString>
#include <QNetworkRequest>
#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;

3
plugins/samplesource/plutosdrinput/plutosdrinput.h

@ -26,13 +26,14 @@ @@ -26,13 +26,14 @@
#include <QByteArray>
#include <QNetworkRequest>
#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;

Loading…
Cancel
Save