From 059d0dc4f2d4ff3cd04646a7085715121fd812ee Mon Sep 17 00:00:00 2001 From: f4exb Date: Sun, 12 Jul 2020 02:39:14 +0200 Subject: [PATCH] AirspyHF: refactored Thread to Worker object moved to thread. Equivalent to FileInput changes --- plugins/samplesource/airspyhf/CMakeLists.txt | 4 +- .../samplesource/airspyhf/airspyhfinput.cpp | 75 +++++++++++------ plugins/samplesource/airspyhf/airspyhfinput.h | 8 +- ...{airspyhfthread.cpp => airspyhfworker.cpp} | 81 ++++++++----------- .../{airspyhfthread.h => airspyhfworker.h} | 23 +++--- 5 files changed, 100 insertions(+), 91 deletions(-) rename plugins/samplesource/airspyhf/{airspyhfthread.cpp => airspyhfworker.cpp} (78%) rename plugins/samplesource/airspyhf/{airspyhfthread.h => airspyhfworker.h} (83%) diff --git a/plugins/samplesource/airspyhf/CMakeLists.txt b/plugins/samplesource/airspyhf/CMakeLists.txt index 8cc48f4ef..55ccb224a 100644 --- a/plugins/samplesource/airspyhf/CMakeLists.txt +++ b/plugins/samplesource/airspyhf/CMakeLists.txt @@ -5,7 +5,7 @@ set(airspyhf_SOURCES airspyhfplugin.cpp airspyhfsettings.cpp airspyhfwebapiadapter.cpp - airspyhfthread.cpp + airspyhfworker.cpp ) set(airspyhf_HEADERS @@ -13,7 +13,7 @@ set(airspyhf_HEADERS airspyhfplugin.h airspyhfsettings.h airspyhfwebapiadapter.h - airspyhfthread.h + airspyhfworker.h ) include_directories( diff --git a/plugins/samplesource/airspyhf/airspyhfinput.cpp b/plugins/samplesource/airspyhf/airspyhfinput.cpp index 7b3eef680..6b9679e3f 100644 --- a/plugins/samplesource/airspyhf/airspyhfinput.cpp +++ b/plugins/samplesource/airspyhf/airspyhfinput.cpp @@ -38,7 +38,7 @@ #include "airspyhfplugin.h" #include "airspyhfsettings.h" -#include "airspyhfthread.h" +#include "airspyhfworker.h" MESSAGE_CLASS_DEFINITION(AirspyHFInput::MsgConfigureAirspyHF, Message) MESSAGE_CLASS_DEFINITION(AirspyHFInput::MsgStartStop, Message) @@ -53,7 +53,7 @@ AirspyHFInput::AirspyHFInput(DeviceAPI *deviceAPI) : m_deviceAPI(deviceAPI), m_settings(), m_dev(0), - m_airspyHFThread(nullptr), + m_airspyHFWorker(nullptr), m_deviceDescription("AirspyHF"), m_running(false) { @@ -167,9 +167,12 @@ bool AirspyHFInput::start() return false; } - if (m_running) { stop(); } + if (m_running) { + stop(); + } - m_airspyHFThread = new AirspyHFThread(m_dev, &m_sampleFifo); + m_airspyHFWorker = new AirspyHFWorker(m_dev, &m_sampleFifo); + m_airspyHFWorker->moveToThread(&m_airspyHFWorkerThread); int sampleRateIndex = m_settings.m_devSampleRateIndex; if (m_settings.m_devSampleRateIndex >= m_sampleRates.size()) { @@ -177,21 +180,45 @@ bool AirspyHFInput::start() } if (sampleRateIndex >= 0) { - m_airspyHFThread->setSamplerate(m_sampleRates[sampleRateIndex]); + m_airspyHFWorker->setSamplerate(m_sampleRates[sampleRateIndex]); } - m_airspyHFThread->setLog2Decimation(m_settings.m_log2Decim); - m_airspyHFThread->setIQOrder(m_settings.m_iqOrder); - m_airspyHFThread->startWork(); + m_airspyHFWorker->setLog2Decimation(m_settings.m_log2Decim); + m_airspyHFWorker->setIQOrder(m_settings.m_iqOrder); + mutexLocker.unlock(); - mutexLocker.unlock(); + if (startWorker()) + { + qDebug("AirspyHFInput::startInput: started"); + applySettings(m_settings, true); + m_running = true; + } + else + { + m_running = false; + } - applySettings(m_settings, true); + return m_running; +} - qDebug("AirspyHFInput::startInput: started"); - m_running = true; +bool AirspyHFInput::startWorker() +{ + if (m_airspyHFWorker->startWork()) + { + m_airspyHFWorkerThread.start(); + return true; + } + else + { + return false; + } +} - return true; +void AirspyHFInput::stopWorker() +{ + m_airspyHFWorker->stopWork(); + m_airspyHFWorkerThread.quit(); + m_airspyHFWorkerThread.wait(); } void AirspyHFInput::closeDevice() @@ -211,11 +238,11 @@ void AirspyHFInput::stop() qDebug("AirspyHFInput::stop"); QMutexLocker mutexLocker(&m_mutex); - if (m_airspyHFThread) + if (m_airspyHFWorker) { - m_airspyHFThread->stopWork(); - delete m_airspyHFThread; - m_airspyHFThread = nullptr; + stopWorker(); + delete m_airspyHFWorker; + m_airspyHFWorker = nullptr; } m_running = false; @@ -446,10 +473,10 @@ bool AirspyHFInput::applySettings(const AirspyHFSettings& settings, bool force) { qCritical("AirspyHFInput::applySettings: could not set sample rate index %u (%d S/s)", sampleRateIndex, m_sampleRates[sampleRateIndex]); } - else if (m_airspyHFThread) + else if (m_airspyHFWorker) { qDebug("AirspyHFInput::applySettings: sample rate set to index: %u (%d S/s)", sampleRateIndex, m_sampleRates[sampleRateIndex]); - m_airspyHFThread->setSamplerate(m_sampleRates[sampleRateIndex]); + m_airspyHFWorker->setSamplerate(m_sampleRates[sampleRateIndex]); } } } @@ -459,9 +486,9 @@ bool AirspyHFInput::applySettings(const AirspyHFSettings& settings, bool force) reverseAPIKeys.append("log2Decim"); forwardChange = true; - if (m_airspyHFThread) + if (m_airspyHFWorker) { - m_airspyHFThread->setLog2Decimation(settings.m_log2Decim); + m_airspyHFWorker->setLog2Decimation(settings.m_log2Decim); qDebug() << "AirspyInput: set decimation to " << (1<setIQOrder(settings.m_iqOrder); + if (m_airspyHFWorker) { + m_airspyHFWorker->setIQOrder(settings.m_iqOrder); } } @@ -487,7 +514,7 @@ bool AirspyHFInput::applySettings(const AirspyHFSettings& settings, bool force) { qCritical("AirspyHFInput::applySettings: could not set LO ppm correction to %f", settings.m_LOppmTenths / 10.0f); } - else if (m_airspyHFThread) + else if (m_airspyHFWorker) { qDebug("AirspyHFInput::applySettings: LO ppm correction set to %f", settings.m_LOppmTenths / 10.0f); } diff --git a/plugins/samplesource/airspyhf/airspyhfinput.h b/plugins/samplesource/airspyhf/airspyhfinput.h index d1ef48636..0de51f54b 100644 --- a/plugins/samplesource/airspyhf/airspyhfinput.h +++ b/plugins/samplesource/airspyhf/airspyhfinput.h @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -30,7 +31,7 @@ class QNetworkAccessManager; class QNetworkReply; class DeviceAPI; -class AirspyHFThread; +class AirspyHFWorker; class FileRecord; class AirspyHFInput : public DeviceSampleSource { @@ -165,7 +166,8 @@ private: QMutex m_mutex; AirspyHFSettings m_settings; airspyhf_device_t* m_dev; - AirspyHFThread* m_airspyHFThread; + AirspyHFWorker* m_airspyHFWorker; + QThread m_airspyHFWorkerThread; QString m_deviceDescription; std::vector m_sampleRates; bool m_running; @@ -173,6 +175,8 @@ private: QNetworkAccessManager *m_networkManager; QNetworkRequest m_networkRequest; + bool startWorker(); + void stopWorker(); bool openDevice(); void closeDevice(); bool applySettings(const AirspyHFSettings& settings, bool force); diff --git a/plugins/samplesource/airspyhf/airspyhfthread.cpp b/plugins/samplesource/airspyhf/airspyhfworker.cpp similarity index 78% rename from plugins/samplesource/airspyhf/airspyhfthread.cpp rename to plugins/samplesource/airspyhf/airspyhfworker.cpp index f9dd48a8a..606896089 100644 --- a/plugins/samplesource/airspyhf/airspyhfthread.cpp +++ b/plugins/samplesource/airspyhf/airspyhfworker.cpp @@ -20,12 +20,12 @@ #include #include "dsp/samplesinkfifo.h" -#include "airspyhfthread.h" +#include "airspyhfworker.h" -AirspyHFThread *AirspyHFThread::m_this = 0; +AirspyHFWorker *AirspyHFWorker::m_this = 0; -AirspyHFThread::AirspyHFThread(airspyhf_device_t* dev, SampleSinkFifo* sampleFifo, QObject* parent) : - QThread(parent), +AirspyHFWorker::AirspyHFWorker(airspyhf_device_t* dev, SampleSinkFifo* sampleFifo, QObject* parent) : + QObject(parent), m_running(false), m_dev(dev), m_convertBuffer(AIRSPYHF_BLOCKSIZE), @@ -38,60 +38,34 @@ AirspyHFThread::AirspyHFThread(airspyhf_device_t* dev, SampleSinkFifo* sampleFif m_this = this; } -AirspyHFThread::~AirspyHFThread() +AirspyHFWorker::~AirspyHFWorker() { stopWork(); m_this = 0; } -void AirspyHFThread::startWork() +bool AirspyHFWorker::startWork() { - m_startWaitMutex.lock(); - start(); - while(!m_running) - m_startWaiter.wait(&m_startWaitMutex, 100); - m_startWaitMutex.unlock(); -} + qDebug("AirspyThread::startWork"); + airspyhf_error rc = (airspyhf_error) airspyhf_start(m_dev, rx_callback, 0); -void AirspyHFThread::stopWork() -{ - qDebug("AirspyThread::stopWork"); - m_running = false; - wait(); -} - -void AirspyHFThread::setSamplerate(uint32_t samplerate) -{ - m_samplerate = samplerate; -} - -void AirspyHFThread::setLog2Decimation(unsigned int log2_decim) -{ - m_log2Decim = log2_decim; -} - -void AirspyHFThread::run() -{ - airspyhf_error rc; - - m_running = true; - m_startWaiter.wakeAll(); - - rc = (airspyhf_error) airspyhf_start(m_dev, rx_callback, 0); - - if (rc != AIRSPYHF_SUCCESS) + if (rc == AIRSPYHF_SUCCESS) { - qCritical("AirspyHFFThread::run: failed to start Airspy HF Rx"); + m_running = (airspyhf_is_streaming(m_dev) != 0); } else { - while ((m_running) && (airspyhf_is_streaming(m_dev) != 0)) - { - sleep(1); - } + qCritical("AirspyHFFThread::run: failed to start Airspy HF Rx"); + m_running = false; } - rc = (airspyhf_error) airspyhf_stop(m_dev); + return m_running; +} + +void AirspyHFWorker::stopWork() +{ + qDebug("AirspyThread::stopWork"); + airspyhf_error rc = (airspyhf_error) airspyhf_stop(m_dev); if (rc == AIRSPYHF_SUCCESS) { qDebug("AirspyHFFThread::run: stopped Airspy HF Rx"); @@ -99,11 +73,20 @@ void AirspyHFThread::run() qDebug("AirspyHFFThread::run: failed to stop Airspy HF Rx"); } - m_running = false; + m_running = false;} + +void AirspyHFWorker::setSamplerate(uint32_t samplerate) +{ + m_samplerate = samplerate; +} + +void AirspyHFWorker::setLog2Decimation(unsigned int log2_decim) +{ + m_log2Decim = log2_decim; } // Decimate according to specified log2 (ex: log2=4 => decim=16) -void AirspyHFThread::callbackIQ(const float* buf, qint32 len) +void AirspyHFWorker::callbackIQ(const float* buf, qint32 len) { SampleVector::iterator it = m_convertBuffer.begin(); @@ -137,7 +120,7 @@ void AirspyHFThread::callbackIQ(const float* buf, qint32 len) m_sampleFifo->write(m_convertBuffer.begin(), it); } -void AirspyHFThread::callbackQI(const float* buf, qint32 len) +void AirspyHFWorker::callbackQI(const float* buf, qint32 len) { SampleVector::iterator it = m_convertBuffer.begin(); @@ -171,7 +154,7 @@ void AirspyHFThread::callbackQI(const float* buf, qint32 len) m_sampleFifo->write(m_convertBuffer.begin(), it); } -int AirspyHFThread::rx_callback(airspyhf_transfer_t* transfer) +int AirspyHFWorker::rx_callback(airspyhf_transfer_t* transfer) { qint32 nbIAndQ = transfer->sample_count * 2; diff --git a/plugins/samplesource/airspyhf/airspyhfthread.h b/plugins/samplesource/airspyhf/airspyhfworker.h similarity index 83% rename from plugins/samplesource/airspyhf/airspyhfthread.h rename to plugins/samplesource/airspyhf/airspyhfworker.h index b9c9d734d..05f4f3235 100644 --- a/plugins/samplesource/airspyhf/airspyhfthread.h +++ b/plugins/samplesource/airspyhf/airspyhfworker.h @@ -15,35 +15,31 @@ // along with this program. If not, see . // /////////////////////////////////////////////////////////////////////////////////// -#ifndef INCLUDE_AIRSPYHFTHREAD_H -#define INCLUDE_AIRSPYHFTHREAD_H +#ifndef INCLUDE_AIRSPYHFWORKER_H +#define INCLUDE_AIRSPYHFWORKER_H #include -#include -#include -#include +#include #include #include "dsp/samplesinkfifo.h" #define AIRSPYHF_BLOCKSIZE (1<<17) -class AirspyHFThread : public QThread { +class AirspyHFWorker : public QObject { Q_OBJECT public: - AirspyHFThread(airspyhf_device_t* dev, SampleSinkFifo* sampleFifo, QObject* parent = 0); - ~AirspyHFThread(); + AirspyHFWorker(airspyhf_device_t* dev, SampleSinkFifo* sampleFifo, QObject* parent = 0); + ~AirspyHFWorker(); - void startWork(); + bool startWork(); void stopWork(); void setSamplerate(uint32_t samplerate); void setLog2Decimation(unsigned int log2_decim); void setIQOrder(bool iqOrder) { m_iqOrder = iqOrder; } private: - QMutex m_startWaitMutex; - QWaitCondition m_startWaiter; bool m_running; airspyhf_device_t* m_dev; @@ -54,15 +50,14 @@ private: int m_samplerate; unsigned int m_log2Decim; bool m_iqOrder; - static AirspyHFThread *m_this; + static AirspyHFWorker *m_this; DecimatorsFI m_decimatorsIQ; DecimatorsFI m_decimatorsQI; - void run(); void callbackIQ(const float* buf, qint32 len); void callbackQI(const float* buf, qint32 len); static int rx_callback(airspyhf_transfer_t* transfer); }; -#endif // INCLUDE_AIRSPYHFTHREAD_H +#endif // INCLUDE_AIRSPYHFWORKER_H